Fixed defgroup description.
[riece] / lisp / riece-history.el
1 ;;; riece-history.el --- channel history management add-on
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 ;; You can check recently visited channels via `C-c g' in the commands
27 ;; buffer, by adding the following lines to ~/.riece/init.el:
28
29 ;;   (add-hook 'riece-guess-channel-try-functions
30 ;;             'riece-guess-channel-from-history)
31
32 ;;; Code:
33
34 (require 'riece-options)
35 (require 'riece-globals)
36 (require 'riece-highlight)
37 (require 'riece-identity)
38 (require 'riece-signal)
39 (require 'ring)
40
41 (defgroup riece-history nil
42   "Channel switch history."
43   :tag "History"
44   :prefix "riece-"
45   :group 'riece)
46
47 (defcustom riece-channel-history-length 3
48   "Length of riece-channel-history."
49   :type 'integer
50   :group 'riece-history)
51
52 (defface riece-channel-list-history-face
53   '((((class color)
54       (background dark))
55      (:foreground "PaleTurquoise"))
56     (((class color)
57       (background light))
58      (:foreground "SeaGreen3"))
59     (t
60      (:bold t)))
61   "Face used for displaying history channels."
62   :group 'riece-highlight-faces)
63 (defvar riece-channel-list-history-face 'riece-channel-list-history-face)
64
65 (unless (riece-facep 'riece-modeline-history-face)
66   (make-face 'riece-modeline-history-face
67              "Face used for displaying history channels in modeline.")
68   (if (featurep 'xemacs)
69       (set-face-parent 'riece-modeline-history-face 'modeline))
70   (set-face-foreground 'riece-modeline-history-face
71                        (face-foreground 'riece-channel-list-history-face)))
72
73 (defvar riece-modeline-history-face 'riece-modeline-history-face)
74
75 (defvar riece-channel-history nil)
76
77 (defvar riece-history-enabled nil)
78
79 (defconst riece-history-description
80   "Keep track channel history")
81
82 (defun riece-guess-channel-from-history ()
83   (let ((length (ring-length riece-channel-history))
84         (index 0)
85         result)
86     (while (< index length)
87       (setq result (cons (ring-ref riece-channel-history index) result)
88             index (1+ index)))
89     (nreverse result)))
90
91 (defun riece-history-format-identity-for-channel-list-buffer (index identity)
92   (if (and riece-history-enabled
93            (not (ring-empty-p riece-channel-history))
94            (riece-identity-equal identity (ring-ref riece-channel-history 0)))
95       (concat (format "%2d:+" index)
96               (riece-format-identity identity))))
97
98 (defun riece-history-format-identity-for-channel-list-indicator (index
99                                                                  identity)
100   (if (and riece-history-enabled
101            (not (ring-empty-p riece-channel-history))
102            (riece-identity-equal identity (ring-ref riece-channel-history 0)))
103       (let ((string (riece-format-identity identity))
104             (start 0))
105         ;; Escape % -> %%.
106         (while (string-match "%" string start)
107           (setq start (1+ (match-end 0))
108                 string (replace-match "%%" nil nil string)))
109         (list (format "%d:" index)
110               (riece-propertize-modeline-string
111                string 'face 'riece-modeline-history-face)))))
112
113 ;;; (defun riece-history-requires ()
114 ;;;   (if (memq 'riece-guess riece-addons)
115 ;;;       '(riece-guess)))
116
117 (defun riece-history-insinuate ()
118   (add-hook 'riece-after-switch-to-channel-functions
119             (lambda (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   (add-hook 'riece-format-identity-for-channel-list-buffer-functions
124             'riece-history-format-identity-for-channel-list-buffer)
125   (add-hook 'riece-format-identity-for-channel-list-indicator-functions
126             'riece-history-format-identity-for-channel-list-indicator)
127   (if (memq 'riece-highlight riece-addons)
128       (setq riece-channel-list-mark-face-alist
129             (cons '(?+ . riece-channel-list-history-face)
130                   riece-channel-list-mark-face-alist)))
131 ;;;  (if (memq 'riece-guess riece-addons)
132 ;;;      (add-hook 'riece-guess-channel-try-functions
133 ;;;             'riece-guess-channel-from-history))
134   )
135
136 (defun riece-history-enable ()
137   (setq riece-channel-history
138         (make-ring riece-channel-history-length))
139   (setq riece-history-enabled t)
140   (riece-emit-signal 'channel-list-changed))
141
142 (defun riece-history-disable ()
143   (setq riece-channel-history nil
144         riece-history-enabled nil)
145   (riece-emit-signal 'channel-list-changed))
146
147 (provide 'riece-history)
148
149 ;;; riece-history.el ends here