/* * window_list.c * vim: expandtab:ts=4:sts=4:sw=4 * * Copyright (C) 2012 - 2019 James Booth * Copyright (C) 2019 - 2020 Michael Vetter * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Profanity is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Profanity. If not, see . * * In addition, as a special exception, the copyright holders give permission to * link the code of portions of this program with the OpenSSL library under * certain conditions as described in each individual source file, and * distribute linked combinations including the two. * * You must obey the GNU General Public License in all respects for all of the * code used other than OpenSSL. If you modify file(s) with this exception, you * may extend this exception to your version of the file(s), but you are not * obligated to do so. If you do not wish to do so, delete this exception * statement from your version. If you delete this exception statement from all * source files in the program, then also delete it here. * */ #include "config.h" #include #include #include #include #include "common.h" #include "config/preferences.h" #include "config/theme.h" #include "plugins/plugins.h" #include "ui/ui.h" #include "ui/window_list.h" #include "xmpp/xmpp.h" #include "xmpp/roster_list.h" static GHashTable *windows; static int current; static Autocomplete wins_ac; static Autocomplete wins_close_ac; static int _wins_cmp_num(gconstpointer a, gconstpointer b); static int _wins_get_next_available_num(GList *used); void wins_init(void) { windows = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)win_free); ProfWin *console = win_create_console(); g_hash_table_insert(windows, GINT_TO_POINTER(1), console); current = 1; wins_ac = autocomplete_new(); autocomplete_add(wins_ac, "console"); wins_close_ac = autocomplete_new(); autocomplete_add(wins_close_ac, "all"); autocomplete_add(wins_close_ac, "read"); } ProfWin* wins_get_console(void) { return g_hash_table_lookup(windows, GINT_TO_POINTER(1)); } gboolean wins_chat_exists(const char *const barejid) { ProfChatWin *chatwin = wins_get_chat(barejid); return (chatwin != NULL); } ProfChatWin* wins_get_chat(const char *const barejid) { GList *values = g_hash_table_get_values(windows); GList *curr = values; while (curr) { ProfWin *window = curr->data; if (window->type == WIN_CHAT) { ProfChatWin *chatwin = (ProfChatWin*)window; if (g_strcmp0(chatwin->barejid, barejid) == 0) { g_list_free(values); return chatwin; } } curr = g_list_next(curr); } g_list_free(values); return NULL; } static gint _cmp_unsubscribed_wins(ProfChatWin *a, ProfChatWin *b) { return g_strcmp0(a->barejid, b->barejid); } GList* wins_get_chat_unsubscribed(void) { GList *result = NULL; GList *values = g_hash_table_get_values(windows); GList *curr = values; while (curr) { ProfWin *window = curr->data; if (window->type == WIN_CHAT) { ProfChatWin *chatwin = (ProfChatWin*)window; PContact contact = roster_get_contact(chatwin->barejid); if (contact == NULL) { result = g_list_insert_sorted(result, chatwin, (GCompareFunc)_cmp_unsubscribed_wins); } } curr = g_list_next(curr); } g_list_free(values); return result; } ProfConfWin* wins_get_conf(const char *const roomjid) { GList *values = g_hash_table_get_values(windows); GList *curr = values; while (curr) { ProfWin *window = curr->data; if (window->type == WIN_CONFIG) { ProfConfWin *confwin = (ProfConfWin*)window; if (g_strcmp0(confwin->roomjid, roomji
;;; init-kill.el --- Kill Ring Configuration File -*- lexical-binding: t -*-
;;; Commentary:
;; Contains code copied from prelude-editor.el
;;; Code:

(use-package browse-kill-ring
  :config
  (browse-kill-ring-default-keybindings))

(use-package emacs
  :config
  (defadvice exchange-point-and-mark (before deactivate-mark activate compile)
    "When called with no active region, do not activate mark."
    (interactive
     (list (not (region-active-p)))))

  (defun yank-advised-indent-function (beg end)
    "Do indentation, as long as the region isn't too large."
    (if (<= (- end beg) 1000)
        (indent-region beg end nil)))

  (defmacro advise-commands (advice-name commands class &rest body)
    "Apply advice named ADVICE-NAME to multiple COMMANDS.

The body of the advice is in BODY."
    `(progn
       ,@(mapcar (lambda (command)
                   `(defadvice ,command (,class ,(intern (concat (symbol-name command) "-" advice-name)) activate)
                      ,@body))
                 commands)))

  (advise-commands "indent" (yank yank-pop) after
                   (if (and (not (ad-get-arg 0))
                            (not (member major-mode '(conf-mode coffee-mode haml-mode python-mode slim-mode yaml-mode)))
                            (or (derived-mode-p 'prog-mode)
                                (member major-mode '(LaTeX-mode TeX-mode))))
                       (let ((transient-mark-mode nil))
                         (yank-advised-indent-function (region-beginning) (region-end))))))

(provide 'init-kill)
;;; init-kill.el ends here
orm *form, ProfConfWinCallback submit, ProfConfWinCallback cancel, const void *userdata) { GList *keys = g_hash_table_get_keys(windows); int result = _wins_get_next_available_num(keys); g_list_free(keys); ProfWin *newwin = win_create_config(roomjid, form, submit, cancel, userdata); g_hash_table_insert(windows, GINT_TO_POINTER(result), newwin); return newwin; } ProfWin* wins_new_private(const char *const fulljid) { GList *keys = g_hash_table_get_keys(windows); int result = _wins_get_next_available_num(keys); g_list_free(keys); ProfWin *newwin = win_create_private(fulljid); g_hash_table_insert(windows, GINT_TO_POINTER(result), newwin); autocomplete_add(wins_ac, fulljid); autocomplete_add(wins_close_ac, fulljid); return newwin; } ProfWin * wins_new_plugin(const char *const plugin_name, const char * const tag) { GList *keys = g_hash_table_get_keys(windows); int result = _wins_get_next_available_num(keys); g_list_free(keys); ProfWin *newwin = win_create_plugin(plugin_name, tag); g_hash_table_insert(windows, GINT_TO_POINTER(result), newwin); autocomplete_add(wins_ac, tag); autocomplete_add(wins_close_ac, tag); return newwin; } gboolean wins_do_notify_remind(void) { GList *values = g_hash_table_get_values(windows); GList *curr = values; while (curr) { ProfWin *window = curr->data; if (win_notify_remind(window)) { g_list_free(values); return TRUE; } curr = g_list_next(curr); } g_list_free(values); return FALSE; } int wins_get_total_unread(void) { int result = 0; GList *values = g_hash_table_get_values(windows); GList *curr = values; while (curr) { ProfWin *window = curr->data; result += win_unread(window); curr = g_list_next(curr); } g_list_free(values); return result; } void wins_resize_all(void) { GList *values = g_hash_table_get_values(windows); GList *curr = values; while (curr) { ProfWin *window = curr->data; win_resize(window); curr = g_list_next(curr); } g_list_free(values); ProfWin *current_win = wins_get_current(); win_update_virtual(current_win); } void wins_hide_subwin(ProfWin *window) { win_hide_subwin(window); ProfWin *current_win = wins_get_current(); win_refresh_without_subwin(current_win); } void wins_show_subwin(ProfWin *window) { win_show_subwin(window); ProfWin *current_win = wins_get_current(); win_refresh_with_subwin(current_win); } ProfXMLWin* wins_get_xmlconsole(void) { GList *values = g_hash_table_get_values(windows); GList *curr = values; while (curr) { ProfWin *window = curr->data; if (window->type == WIN_XML) { ProfXMLWin *xmlwin = (ProfXMLWin*)window; assert(xmlwin->memcheck == PROFXMLWIN_MEMCHECK); g_list_free(values); return xmlwin; } curr = g_list_next(curr); } g_list_free(values); return NULL; } GSList* wins_get_chat_recipients(void) { GSList *result = NULL; GList *values = g_hash_table_get_values(windows); GList *curr = values; while (curr) { ProfWin *window = curr->data; if (window->type == WIN_CHAT) { ProfChatWin *chatwin = (ProfChatWin*)window; result = g_slist_append(result, chatwin->barejid); } curr = g_list_next(curr); } g_list_free(values); return result; } GSList* wins_get_prune_wins(void) { GSList *result = NULL; GList *values = g_hash_table_get_values(windows); GList *curr = values; while (curr) { ProfWin *window = curr->data; if (win_unread(window) == 0 && window->type != WIN_MUC && window->type != WIN_CONFIG && window->type != WIN_XML && window->type != WIN_CONSOLE) { result = g_slist_append(result, window); } curr = g_list_next(curr); } g_list_free(values); return result; } void wins_lost_connection(void) { GList *values = g_hash_table_get_values(windows); GList *curr = values; while (curr) { ProfWin *window = curr->data; if (window->type != WIN_CONSOLE) { win_println(window, THEME_ERROR, "-", "Lost connection."); // if current win, set current_win_dirty if (wins_is_current(window)) { win_update_virtual(window); } } curr = g_list_next(curr); } g_list_free(values); } void wins_reestablished_connection(void) { GList *values = g_hash_table_get_values(windows); GList *curr = values; while (curr) { ProfWin *window = curr->data; if (window->type != WIN_CONSOLE) { win_println(window, THEME_TEXT, "-", "Connection re-established."); // if current win, set current_win_dirty if (wins_is_current(window)) { win_update_virtual(window); } } curr = g_list_next(curr); } g_list_free(values); } void wins_swap(int source_win, int target_win) { ProfWin *source = g_hash_table_lookup(windows, GINT_TO_POINTER(source_win)); ProfWin *console = wins_get_console(); if (source) { ProfWin *target = g_hash_table_lookup(windows, GINT_TO_POINTER(target_win)); // target window empty if (target == NULL) { g_hash_table_steal(windows, GINT_TO_POINTER(source_win)); g_hash_table_insert(windows, GINT_TO_POINTER(target_win), source); status_bar_inactive(source_win); char *identifier = win_get_tab_identifier(source); if (win_unread(source) > 0) { status_bar_new(target_win, source->type, identifier); } else { status_bar_active(target_win, source->type, identifier); } free(identifier); if (wins_get_current_num() == source_win) { wins_set_current_by_num(target_win); ui_focus_win(console); } // target window occupied } else { g_hash_table_steal(windows, GINT_TO_POINTER(source_win)); g_hash_table_steal(windows, GINT_TO_POINTER(target_win)); g_hash_table_insert(windows, GINT_TO_POINTER(source_win), target); g_hash_table_insert(windows, GINT_TO_POINTER(target_win), source); char *source_identifier = win_get_tab_identifier(source); char *target_identifier = win_get_tab_identifier(target); if (win_unread(source) > 0) { status_bar_new(target_win, source->type, source_identifier); } else { status_bar_active(target_win, source->type, source_identifier); } if (win_unread(target) > 0) { status_bar_new(source_win, target->type, target_identifier); } else { status_bar_active(source_win, target->type, target_identifier); } free(source_identifier); free(target_identifier); if ((wins_get_current_num() == source_win) || (wins_get_current_num() == target_win)) { ui_focus_win(console); } } } } static int _wins_cmp_num(gconstpointer a, gconstpointer b) { int real_a = GPOINTER_TO_INT(a); int real_b = GPOINTER_TO_INT(b); if (real_a == 0) { real_a = 10; } if (real_b == 0) { real_b = 10; } if (real_a < real_b) { return -1; } else if (real_a == real_b) { return 0; } else { return 1; } } static int _wins_get_next_available_num(GList *used) { // only console used if (g_list_length(used) == 1) { return 2; } else { GList *sorted = NULL; GList *curr = used; while (curr) { sorted = g_list_insert_sorted(sorted, curr->data, _wins_cmp_num); curr = g_list_next(curr); } int result = 0; int last_num = 1; curr = sorted; // skip console curr = g_list_next(curr); while (curr) { int curr_num = GPOINTER_TO_INT(curr->data); if (((last_num != 9) && ((last_num + 1) != curr_num)) || ((last_num == 9) && (curr_num != 0))) { result = last_num + 1; if (result == 10) { result = 0; } g_list_free(sorted); return (result); } else { last_num = curr_num; if (last_num == 0) { last_num = 10; } } curr = g_list_next(curr); } result = last_num + 1; if (result == 10) { result = 0; } g_list_free(sorted); return result; } } gboolean wins_tidy(void) { gboolean tidy_required = FALSE; // check for gaps GList *keys = g_hash_table_get_keys(windows); keys = g_list_sort(keys, _wins_cmp_num); // get last used GList *last = g_list_last(keys); int last_num = GPOINTER_TO_INT(last->data); // find first free num TODO - Will sort again int next_available = _wins_get_next_available_num(keys); // found gap (next available before last window) if (_wins_cmp_num(GINT_TO_POINTER(next_available), GINT_TO_POINTER(last_num)) < 0) { tidy_required = TRUE; } if (tidy_required) { status_bar_set_all_inactive(); GHashTable *new_windows = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)win_free); int num = 1; GList *curr = keys; while (curr) { ProfWin *window = g_hash_table_lookup(windows, curr->data); char *identifier = win_get_tab_identifier(window); g_hash_table_steal(windows, curr->data); if (num == 10) { g_hash_table_insert(new_windows, GINT_TO_POINTER(0), window); if (win_unread(window) > 0) { status_bar_new(0, window->type, identifier); } else { status_bar_active(0, window->type, identifier); } } else { g_hash_table_insert(new_windows, GINT_TO_POINTER(num), window); if (win_unread(window) > 0) { status_bar_new(num, window->type, identifier); } else { status_bar_active(num, window->type, identifier); } } free(identifier); num++; curr = g_list_next(curr); } g_hash_table_destroy(windows); windows = new_windows; current = 1; ProfWin *console = wins_get_console(); ui_focus_win(console); g_list_free(keys); return TRUE; } else { g_list_free(keys); return FALSE; } } GSList* wins_create_summary(gboolean unread) { if (unread && wins_get_total_unread() == 0) { return NULL; } GSList *result = NULL; GList *keys = g_hash_table_get_keys(windows); keys = g_list_sort(keys, _wins_cmp_num); GList *curr = keys; while (curr) { ProfWin *window = g_hash_table_lookup(windows, curr->data); if (!unread || (unread && win_unread(window) > 0)) { GString *line = g_string_new(""); int ui_index = GPOINTER_TO_INT(curr->data); char *winstring = win_to_string(window); if (!winstring) { g_string_free(line, TRUE); continue; } g_string_append_printf(line, "%d: %s", ui_index, winstring); free(winstring); result = g_slist_append(result, strdup(line->str)); g_string_free(line, TRUE); } curr = g_list_next(curr); } g_list_free(keys); return result; } char* win_autocomplete(const char *const search_str, gboolean previous, void *context) { return autocomplete_complete(wins_ac, search_str, TRUE, previous); } char* win_close_autocomplete(const char *const search_str, gboolean previous, void *context) { return autocomplete_complete(wins_close_ac, search_str, TRUE, previous); } void win_reset_search_attempts(void) { autocomplete_reset(wins_ac); } void win_close_reset_search_attempts(void) { autocomplete_reset(wins_close_ac); } void wins_destroy(void) { g_hash_table_destroy(windows); autocomplete_free(wins_ac); autocomplete_free(wins_close_ac); } ProfWin* wins_get_next_unread(void) { // get and sort win nums GList *values = g_hash_table_get_values(windows); values = g_list_sort(values, _wins_cmp_num); GList *curr = values; while (curr) { if (current == GPOINTER_TO_INT(curr->data)) { break; } ProfWin *window = curr->data; if (win_unread(window) > 0) { g_list_free(values); return window; } curr = g_list_next(curr); } g_list_free(values); return NULL; }