Added SILC Thread Queue API
[runtime.git] / apps / irssi / src / fe-text / lastlog.c
index 75e68da3188fc83aa9a0bec11f7da87c915062d7..3919be7ef66eee5c2acae3d15b405a2c3769b55d 100644 (file)
 #include "gui-windows.h"
 #include "gui-printtext.h"
 
+#define DEFAULT_LASTLOG_BEFORE 3
+#define DEFAULT_LASTLOG_AFTER 3
 #define MAX_LINES_WITHOUT_FORCE 1000
 
 static void window_lastlog_clear(WINDOW_REC *window)
 {
-        TEXT_BUFFER_VIEW_REC *view;
-       GList *tmp, *next;
+       TEXT_BUFFER_VIEW_REC *view;
+        LINE_REC *line, *next;
 
-        screen_refresh_freeze();
+        term_refresh_freeze();
        view = WINDOW_GUI(window)->view;
-       for (tmp = textbuffer_view_get_lines(view); tmp != NULL; tmp = next) {
-               LINE_REC *line = tmp->data;
+       line = textbuffer_view_get_lines(view);
 
-                next = tmp->next;
-                if (line->info.level & MSGLEVEL_LASTLOG)
+       while (line != NULL) {
+                next = line->next;
+
+               if (line->info.level & MSGLEVEL_LASTLOG)
                        textbuffer_view_remove_line(view, line);
+                line = next;
        }
         textbuffer_view_redraw(view);
-        screen_refresh_thaw();
+        term_refresh_thaw();
 }
 
 /* Only unknown keys in `optlist' should be levels.
@@ -98,7 +102,7 @@ static void show_lastlog(const char *searchtext, GHashTable *optlist,
        GList *list, *tmp;
        GString *line;
         char *str;
-       int level, len;
+       int level, before, after, len;
 
         level = cmd_options_get_level("lastlog", optlist);
        if (level == -1) return; /* error in options */
@@ -131,14 +135,26 @@ static void show_lastlog(const char *searchtext, GHashTable *optlist,
        else
                startline = NULL;
 
-       if (startline == NULL) {
-                list = textbuffer_view_get_lines(WINDOW_GUI(window)->view);
-               startline = list == NULL ? NULL : list->data;
+       if (startline == NULL)
+                startline = textbuffer_view_get_lines(WINDOW_GUI(window)->view);
+
+       str = g_hash_table_lookup(optlist, "#");
+       if (str != NULL) {
+               before = after = atoi(str);
+       } else {
+               str = g_hash_table_lookup(optlist, "before");
+               before = str == NULL ? 0 : *str != '\0' ?
+                       atoi(str) : DEFAULT_LASTLOG_BEFORE;
+
+               str = g_hash_table_lookup(optlist, "after");
+               if (str == NULL) str = g_hash_table_lookup(optlist, "a");
+               after = str == NULL ? 0 : *str != '\0' ?
+                       atoi(str) : DEFAULT_LASTLOG_AFTER;
        }
 
        list = textbuffer_find_text(WINDOW_GUI(window)->view->buffer, startline,
                                    level, MSGLEVEL_LASTLOG,
-                                   searchtext,
+                                   searchtext, before, after,
                                    g_hash_table_lookup(optlist, "regexp") != NULL,
                                    g_hash_table_lookup(optlist, "word") != NULL,
                                    g_hash_table_lookup(optlist, "case") != NULL);
@@ -147,18 +163,24 @@ static void show_lastlog(const char *searchtext, GHashTable *optlist,
        if (count <= 0)
                tmp = list;
        else {
-               int pos = len-count;
-
+               int pos = len-count-start;
                if (pos < 0) pos = 0;
-               pos += start;
 
                tmp = pos > len ? NULL : g_list_nth(list, pos);
                len = g_list_length(tmp);
        }
 
+       if (g_hash_table_lookup(optlist, "count") != NULL) {
+               printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
+                                  TXT_LASTLOG_COUNT, len);
+               g_list_free(list);
+               return;
+       }
+
        if (len > MAX_LINES_WITHOUT_FORCE && fhandle == -1 &&
            g_hash_table_lookup(optlist, "force") == NULL) {
-               printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
+               printformat_window(active_win,
+                                  MSGLEVEL_CLIENTNOTICE|MSGLEVEL_LASTLOG,
                                   TXT_LASTLOG_TOO_LONG, len);
                g_list_free(list);
                return;
@@ -171,6 +193,20 @@ static void show_lastlog(const char *searchtext, GHashTable *optlist,
         while (tmp != NULL && (count < 0 || count > 0)) {
                LINE_REC *rec = tmp->data;
 
+               if (rec == NULL) {
+                       if (tmp->next == NULL)
+                                break;
+                       if (fhandle != -1) {
+                               write(fhandle, "--\n", 3);
+                       } else {
+                               printformat_window(active_win,
+                                                  MSGLEVEL_LASTLOG,
+                                                  TXT_LASTLOG_SEPARATOR);
+                       }
+                        tmp = tmp->next;
+                       continue;
+               }
+
                 /* get the line text */
                textbuffer_line2text(rec, fhandle == -1, line);
                if (!settings_get_bool("timestamps")) {
@@ -207,9 +243,10 @@ static void show_lastlog(const char *searchtext, GHashTable *optlist,
        g_list_free(list);
 }
 
-/* SYNTAX: LASTLOG [-] [-file <filename>] [-clear] [-<level> -<level...>]
-                  [-new | -away] [-regexp | -word] [-case]
-                  [-window <ref#|name>] [<pattern>] [<count> [<start>]] */
+/* SYNTAX: LASTLOG [-] [-file <filename>] [-window <ref#|name>] [-new | -away]
+                  [-<level> -<level...>] [-clear] [-count] [-case]
+                  [-regexp | -word] [-before [<#>]] [-after [<#>]]
+                  [-<# before+after>] [<pattern>] [<count> [<start>]] */
 static void cmd_lastlog(const char *data)
 {
        GHashTable *optlist;
@@ -219,13 +256,14 @@ static void cmd_lastlog(const char *data)
 
        g_return_if_fail(data != NULL);
 
-       if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_OPTIONS | PARAM_FLAG_UNKNOWN_OPTIONS,
-                           "lastlog", &optlist, &text, &countstr, &start))
+       if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_OPTIONS |
+                           PARAM_FLAG_UNKNOWN_OPTIONS, "lastlog", &optlist,
+                           &text, &countstr, &start))
                return;
 
-       if (*start == '\0' && is_numeric(text, 0)) {
-               if (is_numeric(countstr, 0))
-                       start = countstr;
+       if (*start == '\0' && is_numeric(text, 0) && *text != '0' &&
+           (*countstr == '\0' || is_numeric(countstr, 0))) {
+               start = countstr;
                countstr = text;
                text = "";
        }
@@ -258,7 +296,7 @@ void lastlog_init(void)
 {
        command_bind("lastlog", NULL, (SIGNAL_FUNC) cmd_lastlog);
 
-       command_set_options("lastlog", "!- force clear -file -window new away word regexp case");
+       command_set_options("lastlog", "!- # force clear -file -window new away word regexp case count @a @after @before");
 }
 
 void lastlog_deinit(void)