🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: news.software.readers
1 messages
1 total messages Started by Dawn Owens-Nicholson Wed, 12 Aug 1998 00:00
[slrn] New macro set: BetweenTheTags.sl
#42701
Author: Dawn Owens-Nicholson
Date: Wed, 12 Aug 1998 00:00
509 lines
17389 bytes
Save this article as BetweenTheTags.sl and edit out this text and all Usenet
headers.  The file starts with the next line.

% Eval on range of articles                   % -*- mode: slang; mode: fold -*-
%
% Copyright 1998 J.B. Nicholson-Owens
% Author: J.B. Nicholson-Owens
% Version: 1.0

#iffalse % Documentation

Inspired by Parand Tony Darugar in <35992446.F7165815@binevolve.com>.
Thanks for the great idea!

Quick docs:

  (1) Put down some numeric tags on headers in article mode.
  (2) Load this file and execute
      "between_numeric_tags_do (&sample_function ());".
  (3) All the headers between each pair of tags should be tagged with an
      asterisk.

With between_numeric_tags_do(Ref_Type f) you can do things between numeric
tag(s), with on_numeric_tags_do(Ref_Type f) you can do things on numeric
tagged articles. Both work with slrn v0.9.5.2 (possibly later versions as
well).

_THIS_ _IS_ _IMPORTANT_: S-Lang code that moves the cursor (arrow or bar) is
not recommended for use with this macro.  In the future I'll try to minimize
the adverse effects of using code like the following:

     define my_function ()               % DO _NOT_ USE THIS!
     {
        call ("toggle_header_tag");
        call ("next");
     }
     eval_range (&my_function ());       % execute "my_function" over range.

but ultimately, this warning should be heeded whenever possible.  This is
bad code to use with this macro set because both call()s will move the
cursor to some other article, throwing off the macro's ability to keep up
with things.  A working version follows:

     define my_function ()               % USE _THIS_ INSTEAD!
     {
        % Set the HEADER_TAGGED bit if it's not already set.

        variable header_flags = get_header_flags ();

	!if (header_flags & HEADER_TAGGED)
	  set_header_flags (header_flags | HEADER_TAGGED);
     }
     eval_range (&my_function ());       % execute "my_function" over range.

All the function my_function() needs to do is describe how to handle each
article in the range.  Moving the cursor to the appropriate articles will be
handled by this macro.  Using set_header_flags() instead accomplishes the
same goal without moving the cursor.  sample_function() is provided and does
precisely what is shown above in my_function().

Inside your function, do not do any of the following:

     * Do not re-order or delete articles from the header list. [Lifting
     this limit would make this macro *really* slow on large header lists.]

     * Do not move the cursor at all. (this limit will be somewhat lifted in
     a future version).  If you *must* move the cursor, move it back to
     where it was before your function exits.  You can do this with a
     function that looks like this:

        define my_function ()
        {
	   variable starting_point = extract_article_header ("Message-ID");
	     .
	     .
	   set_input_string (starting_point);
	   call ("locate_header_by_msgid");
	}
     
     * Don't apply numeric tags -- this will cause frustration because the
     more tags you apply, the more this macro will do what you don't expect.
     Removing numeric tags is not recommended, but (if you know what you're
     doing) should not pose a problem. [Again, lifting this limit poses an
     unreasonably high execution time for long header lists.]

Your function is called many times rather rapidly, so odds are message()s
will go unseen.  Using error() will stop this macro set (this might be handy
if you don't want it to complete its task).  Return values from your
function will be ignored by this macro set; they will accumulate on the
stack (which might also be handy).

on_numeric_tags_do() executes the named function on each tag in turn.
between_numeric_tags_do() executes the named function on pairs of tags 1 &
2, 3 & 4, 5 & 6, etc.  If there is no corresponding higher number tag (for
instance, one header is tagged with 29, but no header is tagged with 30),
the macro acts as if the higher numbered tag is on the bottom-most article
in the bottom-most thread.  Using between_numeric_tags_do(&supplied
sample_function()) and the numeric tags as shown below, the header list
would look like this:

     tag headers   (* is an affected article)
     === =======
      1  ------- *  <--- beginning of the first range
         ------- *
         ------- *
      2  ------- *  <--- end of the first range
         -------    <--- this article is not in any range, it is unaffected
      3  ------- *  <--- beginning of the second range
         ------- *
         ------- *
         ------- *  <--- (implied) end of the second range

Reading the tags from top to bottom, if the higher numbered tag in a pair is
above the lower numbered tag, between_numeric_tags_do() will wrap around to
mark articles.  Again, between_numeric_tags_do(&sample_function()) and the
following numeric tags:

     tag headers   (* is an affected article)
     === =======
         ------- *                         (5th article to be affected)
         ------- *                         (6th article to be affected)
      2  ------- *  <--- End of range      (7th & last article to be affected)
         -------    These two articles will not be affected by the macro
         -------    at all.  They lie outside the range of the 1 & 2 tags.
      1  ------- *  <--- Start of range    (first article to be affected)
         ------- *                         (2nd article to be affected)
         ------- *                         (3rd article to be affected)
         ------- *                         (4th article to be affected)

Compare with this situation where the numeric tags are reversed:

     tag headers   (* is an affected article)
     === =======
         -------
         -------
      1  ------- *  <--- Start of range    (first article to be affected)
         ------- *  These two articles are affected by the macro.        (2nd)
         ------- *  They lie inside the range of the 1 & 2 pair of tags. (3rd)
      2  ------- *  <--- End of range      (4th & last article to be affected)
         -------
         -------
         -------

Both on_numeric_tags_do() and between_numeric_tags_do() will always affect
the articles (or headers, depending on your function) tagged with numeric
tags. between_numeric_tags_do() was written to preserve the threading
display as much as possible, so you shouldn't notice any changes in the
thread display (assuming the macro executes to completion).

To install: Load this file from your slrn.rc or from an S-Lang file.  If you
don't know which one to pick, append the slrn.rc instruction line to your
slrn rc ($HOME/.slrnrc in Unix):

   interpret "BetweenTheTags.sl"                 % slrn.rc instruction
   () = evalfile ("BetweenTheTags.sl");          % S-Lang instruction

   between_numeric_tags_do(Ref_Type f)           % @f _between_ pairs of tags
   on_numeric_tags_do(Ref_Type f)                % @f _on_ each tag in order

Where "f" is a reference to a defined function.  For example:

   between_numeric_tags_do (&sample_function ());
   on_numeric_tags_do (&sample_function ());

Known bugs:
v1.0: None that I know of.  Feel free to find them!

To do:
* export only between_numeric_tags_do() and on_numeric_tags_do(), all other
functions should be local to this file.

* fix the bugs, mow the lawn, wash the clothes, write the songs that make
the who-- wait, that's not my job.

#endif % End documentation

% Code                                                                  %{{{
define thread_size2 ()                     % should be file-local       %{{{
{
   % Returns size of the thread from the cursor to the end of the thread,
   % subthreads are included (unlike thread_size() where only the current
   % header and immediate child articles are included).

   if (is_group_mode ())
     error (_function_name () + " doesn\'t work in group mode!");

   variable
     starting_point = extract_article_header ("Message-ID"),
     thread_was_collapsed = is_thread_collapsed (),
     t_size = 0;

   !if (thread_was_collapsed) collapse_thread ();
   if (extract_article_header ("Message-ID") == starting_point)
     {
	!if (thread_was_collapsed) uncollapse_thread ();
	return (thread_size ());
     }

   t_size = thread_size ();
   uncollapse_thread ();
   loop (t_size - 1)
     {
	!if (header_down (1))
	  error (_function_name () +
		 ": Unexpected inability to move down one header!");
	t_size--;
	if (extract_article_header ("Message-ID") == starting_point)
	  return (t_size);
     }

   error (_function_name () + "should never have reached this point.");
} %}}}
define eval_range_to (tag, function)       % should be file-local       %{{{
{
   % Returns 1 if the tag was found, 0 otherwise (in this case, @function()
   % was executed through the last header in the last thread.

   variable
     t_size = 0,
     thread_was_collapsed = 0,
     index = 0;

   forever
     {
	t_size = thread_size2 ();
	thread_was_collapsed = is_thread_collapsed ();
	if (thread_was_collapsed) uncollapse_thread ();

	for (index = 1; index <= t_size; index++)
	  {
	     % Do something here to record message ID?
	     @function ();
	     % Do something here to move cursor back to affected article?

	     if (tag == get_header_tag_number ())
	       {
		  if (thread_was_collapsed) collapse_thread ();
		  return (1);
	       }
	     if (index < t_size) () = header_down (1);
	  }
	
	if (thread_was_collapsed) collapse_thread ();
	!if (header_down (1))
	  {
	     if (tag == get_header_tag_number ()) return (1);
	     else return (0);
	  }
     }
} %}}}
define find (tag)                          % should be file-local       %{{{
{
   if (get_header_tag_number () == tag) return (1, is_thread_collapsed ());

   variable
     index = 0,
     t_size = 0,
     thread_was_collapsed = 0,
     starting_thread_was_collapsed = is_thread_collapsed (),
     starting_place = extract_article_header ("Message-ID");

   call ("header_bob");
   do
     {
	thread_was_collapsed = is_thread_collapsed ();
	t_size = thread_size ();
	if (thread_was_collapsed) uncollapse_thread ();

	for (index = 1; index <= t_size; index++)
	  {
	     if (get_header_tag_number () == tag)
	       return (1, thread_was_collapsed);
	     if (index != t_size) () = header_down (1);    % next header
	  }

	if (thread_was_collapsed) collapse_thread ();
     }
   while (header_down (1));                                % next thread

   set_input_string (starting_place);
   call ("locate_header_by_msgid");
   if (starting_thread_was_collapsed) collapse_thread ();
   return (0, thread_was_collapsed);
} %}}}

define between_numeric_tags_do (function)  % should remain global       %{{{
{
   if (is_group_mode ())
     error (_function_name () + ": doesn\'t work in group mode!");

   variable
     success = 0,
     thread_was_collapsed = 0,
     d = 1,
     starting_place = extract_article_header ("Message-ID"),
     starting_thread_was_collapsed = is_thread_collapsed ();
   
   (success, thread_was_collapsed) = find (d);
   while (success)
     {
	d++;
	if (eval_range_to (d, function))
	  {
	     find (d - 1);
	     if (thread_was_collapsed) collapse_thread ();
	     d++;
	  }
	else
	  {
	     (success, thread_was_collapsed) = find (d);
	     !if (success)
	       {
		  if (thread_was_collapsed) collapse_thread ();
		  else uncollapse_thread ();
		  break;
	       }
	     call ("header_bob");
	     () = eval_range_to (d, function);
	     d++;
	  }
	
       (success, thread_was_collapsed) = find (d);
     }

   set_input_string (starting_place);
   call ("locate_header_by_msgid");
   if (starting_thread_was_collapsed) collapse_thread ();
} %}}}
define on_numeric_tags_do (function)       % should remain global       %{{{
{
   if (is_group_mode ())
     error (_function_name () + ": doesn\'t work in group mode!");

   variable
     header_tag = 1,
     thread_was_collapsed = 0,
     success = 0,
     % message_ID = "",
     starting_place = extract_article_header ("Message-ID");

   (success, thread_was_collapsed) = find (header_tag);
   while (success)
     {
	% should we record the location of the affected header with
	% message_ID = extract_article_header ("Message-ID");
	
	@function ();
	     
	% should we put the pointer/bar back at the affected article with
	% set_input_string (message_ID);
	% call ("locate_header_by_msgid");
	
        % should we try to account for expunging (deleting) headers?
	% if (andelse
	%      { extract_article_header ("Message-ID") != message_ID }
	%      { get_header_tag_number () <= header_tag })
	%   continue;
	
	if (thread_was_collapsed) collapse_thread ();
	header_tag++;
	(success, thread_was_collapsed) = find (header_tag);
     }

   set_input_string (starting_place);
   call ("locate_header_by_msgid");
   if (thread_was_collapsed) collapse_thread ();
} %}}}
define on_numeric_tags_do2 (function)      % should remain global       %{{{
{
   variable
     new = NULL,
     list = NULL,
     tail = NULL,
     t_size = 0,
     thread_was_collapsed = 0,
     index = 1,
     numeric_tag = 1,
     success = 0,
     starting_thread_was_collapsed = is_thread_collapsed (),
     starting_place = extract_article_header ("Message-ID");

   call ("header_bob");

   % the non-find() version (one pass through entire header list) %{{{
%   do
%     {
%	thread_was_collapsed = is_thread_collapsed ();
%	if (thread_was_collapsed) uncollapse_thread ();
%	t_size = thread_size ();
%
%	for (index = 1; index <= t_size; index++)
%	  {
%	     if (get_header_tag_number () == numeric_tag)
%	       {
%		  new = struct {
%		     message_ID, thread_was_collapsed, numeric_tag, next, prev
%		  };
%		  new.message_ID = extract_article_header ("Message-ID");
%		  new.thread_was_collapsed = thread_was_collapsed;
%		  new.numeric_tag = numeric_tag;
%		  if (list == NULL)
%		    {
%		       list = new;
%		       list.next = list;
%		       list.prev = list;
%		    }
%		  else
%		    {
%		       tail.next = new;
%		       list.prev = new;
%		       new.next = list;
%		       new.prev = tail;
%		    }
%		  tail = new;
%		  numeric_tag++;
%	       }
%	     
%	     if (index < t_size)
%	       () = header_down (1);             % next header
%	  }
%	  
%	if (thread_was_collapsed) collapse_thread ();
%     }
%   while (header_down (1));                      % next thread
%}}}
								      
   % the find() version, possibly many passes over much of header list
   % but shorter code here.
   (success, thread_was_collapsed) = find (numeric_tag);
   while (success)
     {
	new = struct {
	   message_ID, thread_was_collapsed, numeric_tag, next, prev
	};
	new.message_ID = extract_article_header ("Message-ID");
	new.thread_was_collapsed = thread_was_collapsed;
	new.numeric_tag = numeric_tag;
	if (list == NULL)
	  {
	     list = new;
	     list.next = list;
	     list.prev = list;
	  }
	else
	  {
	     tail.next = new;
	     list.prev = new;
	     new.next = list;
	     new.prev = tail;
	  }
	tail = new;
	numeric_tag++;
	if (thread_was_collapsed) collapse_thread ();
	
	(success, thread_was_collapsed) = find (numeric_tag);
     }

   EXIT_BLOCK
     {
	set_input_string (starting_place);
	call ("locate_header_by_msgid");
	if (starting_thread_was_collapsed) collapse_thread ();
     }
   
   if (list == NULL) return ();                  % no numeric header tags!
   
   tail = NULL;
   new = NULL;
   variable desired_numeric_tag = 1;

   do
     {
	if (list.numeric_tag == desired_numeric_tag)
	  {
	     set_input_string (list.message_ID);
	     call ("locate_header_by_msgid");

	     @function ();
	     if (list.thread_was_collapsed) collapse_thread ();
	     desired_numeric_tag++;
	     
	     if (list.next != list)              % delete the node
	       {
		  variable
		    prev = list.prev,
		    next = list.next;

		  prev.next = next;
		  next.prev = prev;
		  list = list.next;
	       }
	     else list = NULL;
	  }
     }
   while (list != NULL);
} %}}}

define sample_function ()                  % should be file-local       %{{{
{
   % Set the HEADER_TAGGED bit if it's not already set.

   variable header_flags = get_header_flags ();

   !if (header_flags & HEADER_TAGGED)
     set_header_flags (header_flags | HEADER_TAGGED);
} %}}}
%}}}

Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads