Thread View: news.software.readers
1 messages
1 total messages
Started by J.B. Nicholson
Fri, 09 Feb 2018 02:12
[slrn] Macro tips document suggestions?
Author: J.B. Nicholson
Date: Fri, 09 Feb 2018 02:12
Date: Fri, 09 Feb 2018 02:12
217 lines
9357 bytes
9357 bytes
I'm writing a collection of tips for slrn macro programmers, and I'd
like your feedback. The goal is to help anyone interested in writing
slrn macros get more readable source code and more speed out of their
effort so their macro can scale up to handle large newsgroups.
What you're seeing is a draft of this effort and I need to do
considerable testing.
I'm not sure where this will end up. I thought I'd post an early draft
of this to collect suggestions for improvement.
Tips for slrn macro programmers
Introduction
slrn macros let you expand slrn's functionality using S-Lang, a
relatively high-level language.
What follows are a set of tips to let you write macros that operate
without side effects, run quickly, and operate correctly. Naturally
this collection of tips are opinionated -- I find these to be
worthwhile goals for a slrn macro.
You'll need to know something about S-Lang and it helps to have some
idea of what you want your slrn macro to do. You can look up slrn's
S-Lang API as you go.
slrn state
In slrn all user-modifiable settings affect the entire slrn session,
the state is not set per-newsgroup. In other words, slrn settings you
modify (whether manually, by setting in a .slrnrc, or by macro) stay
modified until that state is modified again.
Therefore you'll probably want to store the state you're about to
change, do the main work of your macro, and then restore the stored
state before your code ends. The more effectively your macro can do
this, the more your macro can preserve the illusion that the macro has
no side effects; your macro does nothing but what it needs to do.
A lack of side effects should instill more confidence in non-technical
users that your macro is better written. Your macros should be
distributed as free software, of course, so technical users can come
to trust the code by using the freedoms of free software.
Don't let a user's prefix argument throw off your macro
If you call() any intrinsic functions you'll want to make sure that
prefix arguments don't make your intrinsic function calls behave
differently than you expect. And you probably don't want to memorize
or look up which intrinsic functions use prefix arguments. Therefore I
recommend saving, clearing, and restoring the prefix argument any time
you use an intrinsic function:
1. Define a variable to hold the prefix argument and define an
EXIT_BLOCK to handle restoring state just before the function ends.
variable prefix_argument = get_prefix_arg();
reset_prefix_arg();
EXIT_BLOCK {
if (prefix_argument != -1) set_prefix_argument (prefix_argument);
}
2. Then do whatever your macro needs to do. Just before the function
returns, the EXIT_BLOCK will run restoring the saved prefix argument.
Consider maintaining the cursor position, thread display, header
flags, read/unread articles, etc.
In article mode, maintaining the display of collapsed/uncollapsed
threads, header flags, and so on helps complete the illusion that your
macro did only the work needed to accomplish some task. Almost as if
your macro operated on some underlying structure describing the
articles.
In group mode, storing where the cursor was and restoring that
position also helps complete the same illusion.
I prefer macros that don't create side effects. An example of a macro
that has side effects is a macro that uncollapses every thread, does
something to each article in the current group, and perhaps collapses
the thread display again. This means that by the time slrn returns
control to me, the thread display is either fully uncollapsed or
collapsed; it looks different than what the display showed when I
initiated the macro which is most likely not how I left the list of
threads when I launched the macro.
In article mode we can store the position of the cursor and set up the
code to restore the cursor position later:
variable
starting_article = extract_article_header("Message-ID"),
t = 1, % thread size (integer)
i = 1, % current article in the thread (integer)
thread_was_collapsed = 0; % was the thread collapsed? (1 bit)
EXIT_BLOCK {
() = locate_header_by_msgid (starting_article, 0);
}
call("header_bob");
do
{
t = thread_size();
thread_was_collapsed = is_thread_collapsed();
uncollapse_thread();
for (i = 1; i <= t; i++)
{
%
% .
% .
% Do something with each article here. You can
% break out of the loop if you need to, the
% next statement will recollapse a thread if needed.
% .
% .
%
% Go to the next article if you're not at the last
% article, otherwise consider whether to collapse
% the thread (while you're still in the thread).
if (i != t) () = header_down (1);
else if (thread_was_collapsed) collapse_thread();
}
if (thread_was_collapsed) collapse_thread();
}
while (header_down (1)); % Go to the next thread.
This code will do your work while saving and restoring the thread
collapse status of each thread. The outer do-while() loop advances the
cursor through the threads, and the inner for() loop advances the
cursor within the thread. If the current sorting mode is unthreaded,
the inner loop will run once per article because in unthreaded sorting
modes thread_size() always returns 1, is_thread_collapsed() always
returns 0, and uncollapse_thread()/collapse_thread() each do nothing.
Bonus points/exercise for the reader: if you need to change slrn state
to do something (like setting query_read_group_cutoff to 0 in order to
read articles in a group without being prompted), figure out what it
would take to record that state and restore that state in your macro's
EXIT_BLOCK so the user's state remains undisturbed and slrn continues
to function as the user expects.
The article mode code is relatively simple because no call()s are
involved. If no intrinsic functions are being call()ed anywhere else
in the function, the above code should suffice to move the cursor back
to the article at which we started or not move the cursor if that
article is unavailable (see slrn's documentation for
locate_header_by_msgid() for more information).
In group mode the code to work on each group is a bit simpler because
the groups are not hierarchical:
variable
starting_group = current_newsgroup(),
prefix_argument = get_prefix_arg();
reset_prefix_arg();
EXIT_BLOCK {
set_input_string (starting_group);
call ("add_group");
if (prefix_argument != -1) set_prefix_argument (prefix_argument);
}
call ("bob");
do
{
%
% .
% .
% Do something with each group here. You can
% break out of the loop if you need to.
% .
% .
%
}
while (group_down_n(1)); % Go to the next group, if possible.
[TODO: Is it faster to loop over the groups with group_down_n() or get
an array of groups and jump to each group with add_group intrinsic?
This should recommend the faster approach since both should do the
same overall job.]
To run faster, or to make your macro scale up better, use intrinsic
functions.
slrn's intrinsic functions are faster than anything you can write in
S-Lang. This means intrinsic functions can process more
groups/articles than an equivalent function in S-Lang. So to speed up
your macros, use intrinsic functions whenever you can.
Be careful: some intrinsic functions have side effects you might need
to account for. For example:
* get_parent_header will collapse all threads except for the current thread.
* expunge collapses all threads.
you might know of other intrinsic functions that cause something to
happen without good reason (not necessarily needlessly changing the
thread collapse status).
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