view unread-threads.sl @ 0:4ec1eb102837

Initial revision
author Guido Berhoerster <guido+slrn@berhoerster.name>
date Sat, 14 Mar 2015 11:46:41 +0100
parents
children e50b2ba42af7
line wrap: on
line source

% unread-threads.sl - display complete threads if they contain unread articles
%
% Copyright (C) 2009 Guido Berhoerster <guido+slrn@berhoerster.name>
%
% Permission is hereby granted, free of charge, to any person obtaining
% a copy of this software and associated documentation files (the
% "Software"), to deal in the Software without restriction, including
% without limitation the rights to use, copy, modify, merge, publish,
% distribute, sublicense, and/or sell copies of the Software, and to
% permit persons to whom the Software is furnished to do so, subject to
% the following conditions:
%
% The above copyright notice and this permission notice shall be included
% in all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
% IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
% CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
% TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
% SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%

%open_log_file(make_home_filename("slrn-debug.log"));
%_traceback = 1;

implements("UnreadThreads");

define select_group_unread_threads()
{
    variable msgid = NULL;
    variable flags;
    variable saved_header_flags = Assoc_Type[];
    variable saved_show_article = get_variable_value("show_article");

    set_integer_variable("show_article", 0);
    set_prefix_argument(4);
    if (select_group() == -1)
        return;
    reset_prefix_arg();

    uncollapse_threads();
    while (((msgid == NULL) && (not get_header_flags() & HEADER_READ)) ||
            header_next_unread())
    {
        msgid = extract_article_header("Message-ID");
        ifnot (strlen(msgid))
            continue;

        % move on to next unread article if this thread has already been
        % processed
        if (assoc_key_exists(saved_header_flags, msgid))
            continue;

        % move to first article of the current thread
        while (has_parent())
            () = header_up(1);

        % save the flags of each article in this thread in saved_header_flags
        % and mark it unread
        loop (thread_size())
        {
            msgid = extract_article_header("Message-ID");
            saved_header_flags[msgid] = get_header_flags();
            call("undelete");
        }

        % move one article up since "undelete" except for the very last message
        % moves one article down
        ifnot (msgid == extract_article_header("Message-ID"))
            () = header_up(1);
    }

    % return if there are no unread articles
    ifnot (length(saved_header_flags))
    {
        collapse_threads();

        % clear message area
        message("");

        return;
    }

    % remove all read articles and restore flags for the remaining articles
    call("expunge");
    foreach msgid, flags(saved_header_flags) using("keys", "values")
    {
        () = locate_header_by_msgid(msgid, 0);
        set_header_flags(flags);
    }

    % place cursor on first unread article
    collapse_threads();
    call("header_bob");
    if (get_header_flags() & HEADER_READ)
        header_next_unread();

    % obey show_article setting
    if (saved_show_article)
        call("hide_article");
    set_integer_variable("show_article", saved_show_article);

    % clear message area
    message("");

    return;
}