ed3a057949838717070bd1aeeddcf311006ab9ab
[riece] / lisp / riece-history.el
1 ;;; riece-history.el --- manage history of channel shifting
2 ;; Copyright (C) 1998-2003 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: IRC, riece
6
7 ;; This file is part of Riece.
8
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; NOTE: This is an add-on module for Riece.
27
28 ;; You can check recently visited channels via `C-c g' in the commands
29 ;; buffer, by adding the following lines to ~/.riece/init.el:
30
31 ;;   (add-hook 'riece-guess-channel-try-functions
32 ;;             'riece-guess-channel-from-history)
33
34 ;;; Code:
35
36 (require 'riece-options)
37 (require 'riece-globals)
38 (require 'riece-identity)
39 (require 'riece-signal)
40 (require 'ring)
41 (eval-when-compile (require 'riece-highlight))
42
43 (defgroup riece-history nil
44   "Manage history of channel shifting."
45   :tag "History"
46   :prefix "riece-"
47   :group 'riece)
48
49 (defcustom riece-channel-history-length 3
50   "Length of riece-channel-history."
51   :type 'integer
52   :group 'riece-history)
53
54 (defface riece-channel-list-history-face
55   '((((class color)
56       (background dark))
57      (:foreground "PaleTurquoise"))
58     (((class color)
59       (background light))
60      (:foreground "SeaGreen3"))
61     (t
62      (:bold t)))
63   "Face used for displaying history channels."
64   :group 'riece-highlight-faces)
65 (defvar riece-channel-list-history-face 'riece-channel-list-history-face)
66
67 (unless (riece-facep 'riece-modeline-history-face)
68   (make-face 'riece-modeline-history-face
69              "Face used for displaying history channels in modeline.")
70   (if (featurep 'xemacs)
71       (set-face-parent 'riece-modeline-history-face 'modeline))
72   (set-face-foreground 'riece-modeline-history-face
73                        (face-foreground 'riece-channel-list-history-face)))
74
75 (defvar riece-modeline-history-face 'riece-modeline-history-face)
76
77 (defvar riece-channel-history nil)
78
79 (defvar riece-history-enabled nil)
80
81 (defconst riece-history-description
82   "Manage history of channel shifting.")
83
84 (defun riece-guess-channel-from-history ()
85   (let ((length (ring-length riece-channel-history))
86         (index 0)
87         result)
88     (while (< index length)
89       (setq result (cons (ring-ref riece-channel-history index) result)
90             index (1+ index)))
91     (nreverse result)))
92
93 (defun riece-history-format-identity-for-channel-list-buffer (index identity)
94   (if (and riece-history-enabled
95            (not (ring-empty-p riece-channel-history))
96            (riece-identity-equal identity (ring-ref riece-channel-history 0)))
97       (concat (format "%2d:+" index)
98               (riece-format-identity identity))))
99
100 (defun riece-history-format-identity-for-channel-list-indicator (index
101                                                                  identity)
102   (if (and riece-history-enabled
103            (not (ring-empty-p riece-channel-history))
104            (riece-identity-equal identity (ring-ref riece-channel-history 0)))
105       (let ((string (riece-format-identity identity))
106             (start 0))
107         ;; Escape % -> %%.
108         (while (string-match "%" string start)
109           (setq start (1+ (match-end 0))
110                 string (replace-match "%%" nil nil string)))
111         (list (format "%d:" index)
112               (riece-propertize-modeline-string
113                string 'face 'riece-modeline-history-face)))))
114
115 ;;; (defun riece-history-requires ()
116 ;;;   (if (memq 'riece-guess riece-addons)
117 ;;;       '(riece-guess)))
118
119 (defun riece-history-after-switch-to-channel-functions (last)
120   (if (and riece-history-enabled last
121            (not (riece-identity-equal last riece-current-channel)))
122       (ring-insert riece-channel-history last)))
123
124 (defun riece-history-requires ()
125   (if (memq 'riece-highlight riece-addons)
126       '(riece-highlight)))
127
128 (defun riece-history-insinuate ()
129   (add-hook 'riece-after-switch-to-channel-functions
130             'riece-history-after-switch-to-channel-functions)
131   (add-hook 'riece-format-identity-for-channel-list-buffer-functions
132             'riece-history-format-identity-for-channel-list-buffer)
133   (add-hook 'riece-format-identity-for-channel-list-indicator-functions
134             'riece-history-format-identity-for-channel-list-indicator)
135   (if (memq 'riece-highlight riece-addons)
136       (setq riece-channel-list-mark-face-alist
137             (cons '(?+ . riece-channel-list-history-face)
138                   riece-channel-list-mark-face-alist)))
139 ;;;  (if (memq 'riece-guess riece-addons)
140 ;;;      (add-hook 'riece-guess-channel-try-functions
141 ;;;             'riece-guess-channel-from-history))
142   )
143
144 (defun riece-history-uninstall ()
145   (remove-hook 'riece-after-switch-to-channel-functions
146                'riece-history-after-switch-to-channel-functions)
147   (remove-hook 'riece-format-identity-for-channel-list-buffer-functions
148                'riece-history-format-identity-for-channel-list-buffer)
149   (remove-hook 'riece-format-identity-for-channel-list-indicator-functions
150                'riece-history-format-identity-for-channel-list-indicator)
151   (setq riece-channel-list-mark-face-alist
152         (delq (assq ?+ riece-channel-list-mark-face-alist)
153               riece-channel-list-mark-face-alist)))
154
155 (defun riece-history-enable ()
156   (setq riece-channel-history
157         (make-ring riece-channel-history-length))
158   (setq riece-history-enabled t)
159   (riece-emit-signal 'channel-list-changed))
160
161 (defun riece-history-disable ()
162   (setq riece-channel-history nil
163         riece-history-enabled nil)
164   (riece-emit-signal 'channel-list-changed))
165
166 (provide 'riece-history)
167
168 ;;; riece-history.el ends here