🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.readers
Article #48070

slrn macro to make * and # tags stick

#48070
From: blue... Dawn Owens-Nicholson
Date: Fri, 16 Apr 1999 00:00
408 lines
12781 bytes
% Sticky tags                                 % -*- mode: slang; mode: fold -*-
%
% Copyright © 1998 J.B. Nicholson-Owens
% Author: J.B. Nicholson-Owens
% Title: StickyTags.sl
% Version: 1.2
% Requires: slrn 0.9.5.3, S-Lang 1.3 or greater

#iffalse % Documentation                                                %{{{

This macro set will give you persistent * and # tags on slrn article
headers; if you quit article mode or quit slrn, the * tags will be in the
right place when you next start slrn (the # tags are not so "sticky" for
technical reasons).  The next time you enter that newsgroup, slrn will
display the headers with the appropriate tags.

To install:

(1) Save a copy of this file (or article) as "stickytags.sl" somewhere so
    slrn can find it.  Your home directory will do if you don't have a
    better place for it.

(2) Append the following lines to your slrn.rc ($HOME/.slrnrc on Unix) to
    make slrn load this file and set up some keys to access the functions:

       interpret "stickytags.sl"

(3) Put the following lines in a separate file (or your hooks file, if you
    have one).  If you don't already have a file for hooks, or if you don't
    know what this is, put the following lines into a file called
    "my_hooks.sl" in your homedir:

       public define startup_hook ()
       {
          StickyTags->startup_hook ();
       }

       public define article_mode_hook ()
       {
          StickyTags->article_mode_hook ();
       }

       public define article_mode_quit_hook ()
       {
          StickyTags->article_mode_quit_hook ();
       }

    If you already have these hooks defined, add the corresponding function
    call to the listed hooks.

(4) If you made a separate file for these hooks, append the following line to
    the bottom of your slrn.rc:

       interpret "my_hooks.sl"

    This will make slrn load the hooks and execute certain functions at the
    appropriate time.

Now you should be able to start (or restart) slrn and have persistent or
"sticky" tags which last between entering article mode and across slrn
sessions.  The default file used to record tag information is
"$HOME/.slrn-stickytags".  If you wish to change this, I suggest making a
new file (call it "my_preferences.sl", put it in your homedir) and loading
it after you load your hooks (which should be executed after loading this
macro set).  Your preferences file should have the following line:

    StickyTags->set_preference ("tagsfile", make_home_filename ("path/file"));

Other preferences (optional settings):

Use StickyTags->set_preference (String_Type preference, setting) to set the
preferences for this macro set.

* show_tags_in_crossposts (default: 1)

    Normally tagged articles remain tagged regardless of the newsgroup in
    which they are seen.  Crossposts (a single article simultaneously posted
    to multiple newsgroups) have a single message-ID and appear tagged in
    all the groups to which they are crossposted.  If you only want the tag
    to be seen in the newsgroup in which it was tagged, set this preference
    to 0.

* save_frequency (default: 1)

    Normally tags are automatically saved every time they change.  If you
    would prefer to save the tags only when you want, change this
    preference to 0.  Looptastic->save_as() can be used at any time to save
    the tagfile regardless of save_frequency's setting.

* save_asterisk_tags, save_numeric_tags (defaults: 1 and 1, respectively)

    Set each preference according to what kind of tag you want to track.

* quiet (default: 0)

    Do not show loading and saving messages when loading or saving tags.

In summary, your slrn.rc file should have the following lines in it:

    interpret "stickytags.sl"
    interpret "my_hooks.sl"
    interpret "my_preferences.sl"

If you set the environment variable STICKYTAGS_FILE to the location of your
tag file, that will be used instead of the tagsfile preference.  This is
handy when you want to use a StickyTags file without having to change your
usual preferences. See your system or shell's documentation for information
on setting an environment variable.

To remove this macro set:

(1) Comment out or delete the three (at most) lines from your slrn.rc:

      interpret "stickytags.sl"
      interpret "my_hooks.sl"
      interpret "my_preferences.sl"

    You will want to keep the my_hooks and my_preferences lines in your
    slrn.rc uncommented if you have other hooks or preferences.

(2) Comment out or delete the references to StickyTags->* functions in your
    hooks:

      public define article_mode_quit_hook ()
      {
         StickyTags->article_mode_quit_hook ();
      }

    should read:

      public define article_mode_quit_hook ()
      {
         % StickyTags->article_mode_quit_hook ();
      }

    If this is the only line in the hook, you can remove the hook definition
    entirely.

(3) Delete the tags file (the default is "$HOME/.slrn-stickytags") and unset
    the STICKYTAGS_FILE environment variable if you used it.

(4) Quit slrn and restart slrn.  Stickytags will no longer be loaded in your
    slrn.  The tags file will not be updated as you mark and unmark headers;
    tags will operate as they did before you installed this macro set.

#endif %}}}
implements ("StickyTags");

private variable
   list = Assoc_Type [],
   list_has_changed = 0,
   prefs = Assoc_Type [];

prefs["tagfile"] = make_home_filename (".slrn-stickytags");
prefs["show_tags_in_crossposts"] = 1;
prefs["save_frequency"] = 1;
prefs["save_asterisk_tags"] = 1;
prefs["save_numeric_tags"] = 1;
prefs["quiet"] = 0;

private define save (file)                                              %{{{
{
   !if (list_has_changed) return ();
   variable fp = fopen (file, "w");
   if (fp == NULL) return ();

   !if (@prefs["quiet"]) vmessage ("saving %s...", file);

   ERROR_BLOCK
     if (fclose (fp) == -1)
       error ("Can\'t close %s: %s", file, errno_string (errno));

   variable
      line = "",
      i = NULL,
      k = NULL,
      v = NULL;
   foreach (list) using ("keys", "values")
     {
	(line, i) = ();

	foreach (@i) using ("keys", "values")
	  {
	     (k, v) = ();
	     line += sprintf (" %s=%s", k, string (@v));
	  }

	if (-1 == fputs (line + "\n", fp))
	  verror ("Couldn\'t write to %s: %s", file, errno_string (errno));
     }

   EXECUTE_ERROR_BLOCK;
   list_has_changed = 0;
   !if (@prefs["quiet"]) vmessage ("saving %s...done", file);
}

%}}}
private define load (file)                                              %{{{
{
   variable fp = fopen (file, "r");
   if (fp == NULL) return ();

   !if (@prefs["quiet"]) vmessage ("loading %s...", file);

   % file format: <msg-ID> key=value[ key=value[ key=value[...]]]

   variable
     line = "",
     ntags = 0,
     pos = 0,
     len = 0,
     message_ID = "",
     word = "",
     key = "",
     value = "",
     i = "";
   while (fgets (&line, fp) != -1)
     {
	line = strtrim (line);
	i = "";

	if (string_match (line, "^%", 1)) continue;
	!if (string_match (line, "^\\(<[^>]+>\\)", 1)) continue;
	
	(, len) = string_match_nth (1);
	message_ID = substr (line, 1, len);
	line = substr (line, len + 1, -1);
	foreach (strtok (line, " \t\n"))
	  {
	     word = ();

	     i = Assoc_Type [];
	     !if (string_match (word, "^\\([^=]+\\)=\\(.*\\)$", 1)) continue;
	     (, len) = string_match_nth (1);
	     key = substr (word, 1, len);
	     (pos, len) = string_match_nth (2);
	     value = substr (word, pos + 1, len);

	     if (string_match (value, "^\\d+$", 1)) value = integer (value);

	     i[key] = value;
	  }
	list[message_ID] = i;
	ntags++;
     }

   () = fclose (fp);
   if (orelse { ntags == 0 } { @prefs["quiet"] }) return ();

   variable s = "s";
   if (ntags == 1) s = "";
   vmessage ("loading %s...done (%i tag%s loaded)\n", file, ntags, s);
}

%}}}

static define version () { return (1.2); }
static define set_preference (preference, value)                        %{{{
{
   !if (assoc_key_exists (prefs, preference))
     error ("Preference does not exist: " + string (preference));

   variable desired_type = typeof (@prefs[preference]);

   if (typeof (value) != desired_type)
     verror ("Wrong type for %s: This preference wants %s not %s",
             string (preference),
             string (desired_type),
             string (typeof (value)));

   prefs[preference] = value;
}

%}}}
static define startup_hook ()                                           %{{{
{
   if (getenv ("STICKYTAGS_FILE") != NULL)
     prefs["tagfile"] = getenv ("STICKYTAGS_FILE");

   load (@prefs["tagfile"]);
}

%}}}
static define article_mode_hook ()                                      %{{{
{
   !if (length (assoc_get_keys (list))) return ();

   variable
     starting_article = extract_article_header ("Message-ID"),
     current_group = current_newsgroup (),
     message_ID = "",
     i = NULL,
     tagname = "",
     value = NULL;

   do
     {
	message_ID = extract_article_header ("Message-ID");
	!if (assoc_key_exists (list, message_ID)) continue;

	i = @list[message_ID];

	if (andelse
	     { @prefs["show_tags_in_crossposts"] == 0 }
	     { i["newsgroup"] != current_group })
	  continue;

	foreach (i) using ("keys", "values")
	  {
	     (tagname, value) = ();

	     switch (tagname)
	       { case ("asterisk_tag"):
		  if (@value)
		    set_header_flags (get_header_flags () | HEADER_TAGGED);
	       }
	       { case ("header_tag_number"):
		  if (@value)
		    {
		       call ("tag_header");
		       set_input_string (message_ID);
		       call ("locate_header_by_msgid");
		    }
	       }
	  }
     }
   while (header_down (1));

   set_input_string (starting_article);
   call ("locate_header_by_msgid");
}

%}}}
static define article_mode_quit_hook ()                                 %{{{
{
   variable
     current_group = current_newsgroup (),
     message_ID = "",
     header_flags = 0,
     thread_was_collapsed = 0,
     header_tag_number = 0,
     i = NULL;

   call ("header_bob");
   do
     {
	message_ID = extract_article_header ("Message-ID");
	header_flags = get_header_flags ();
	thread_was_collapsed = is_thread_collapsed ();
	uncollapse_thread ();

	if (header_flags & HEADER_READ)
	  {
	     if (assoc_key_exists (list, message_ID))
	       {
		  assoc_delete_key (list, message_ID);
		  list_has_changed++;
	       }
	  }
	else
	  {
	     i = Assoc_Type [];                  % inner hash

	     if (andelse
		  { @prefs["save_asterisk_tags"] }
		  { header_flags & HEADER_TAGGED })
	       i["asterisk_tag"] = 1;

	     if (andelse
		  { @prefs["save_numeric_tags"] }
		  { header_tag_number = get_header_tag_number (), header_tag_number })
	       i["header_tag_number"] = header_tag_number;

	     if (andelse
		  { @prefs["show_tags_in_crossposts"] == 0 }
		  { string_match (extract_article_header ("Newsgroups"), "^[^,]+,[^,]+", 1) }
		  { assoc_key_exists (list, message_ID) == 0 })
	       i["newsgroup"] = current_group;

	     if (length (assoc_get_keys (i)))
	       {
		  list[message_ID] = i;
		  list_has_changed++;
	       }
	     else if (assoc_key_exists (list, message_ID))
	       {
		  assoc_delete_key (list, message_ID);
		  list_has_changed++;
	       }
	  }

	if (thread_was_collapsed) collapse_thread ();
     }
   while (header_down (1));

   if (@prefs["save_frequency"]) save (@prefs["tagfile"]);
}

%}}}
static define save_as ()                                                %{{{
{
   variable file = read_mini ("Save tagfile as", @prefs["tagfile"], "");
   save (file);
}

%}}}
Technical Details
Message-ID: <slrn7hde56.4in.bluemoon@ux6.cso.uiuc.edu>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp!not-for-mail

Show Full Headers
Complete Article Headers
Message-ID: <slrn7hde56.4in.bluemoon@ux6.cso.uiuc.edu>
Subject: slrn macro to make * and # tags stick
Date: Fri, 16 Apr 1999 00:00:00 +0000
From: blue...@ux6.cso.uiuc.edu (Dawn Owens-Nicholson)
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp!not-for-mail
X-Deja-AN: 466991473
Content-Transfer-Encoding: 8bit
Followup-To: news.software.readers
Content-Type: text/plain; charset=iso-8859-1
Summary: A macro set for slrn written in S-Lang. It's free, it works, enjoy.
Organization: University of Illinois at Urbana-Champaign
Keywords: slrn, macro, stickytags, 1.2
Mime-Version: 1.0
User-Agent: slrn/0.9.5.4 (UNIX)
Newsgroups: news.software.readers,alt.lang.s-lang