e737159e3750ea213f1ae0386a8e91014c3f2b52
[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 'ring)
39
40 (defgroup riece-history nil
41   "Channel history"
42   :tag "History"
43   :prefix "riece-"
44   :group 'riece)
45
46 (defcustom riece-channel-history-length 3
47   "Length of riece-channel-history."
48   :type 'integer
49   :group 'riece-history)
50
51 (defface riece-channel-list-history-face
52   '((((class color)
53       (background dark))
54      (:foreground "PaleTurquoise"))
55     (((class color)
56       (background light))
57      (:foreground "SeaGreen3"))
58     (t
59      (:bold t)))
60   "Face used for displaying history channels."
61   :group 'riece-highlight-faces)
62 (defvar riece-channel-list-history-face 'riece-channel-list-history-face)
63
64 (defvar riece-channel-history nil)
65
66 (defvar riece-history-enabled nil)
67
68 (defconst riece-history-description
69   "Keep track channel history")
70
71 (defun riece-guess-channel-from-history ()
72   (let ((length (ring-length riece-channel-history))
73         (index 0)
74         result)
75     (while (< index length)
76       (setq result (cons (ring-ref riece-channel-history index) result)
77             index (1+ index)))
78     (nreverse result)))
79
80 (defun riece-history-format-identity-for-channel-list-buffer (index identity)
81   (if (and riece-history-enabled
82            (not (ring-empty-p riece-channel-history))
83            (riece-identity-equal identity (ring-ref riece-channel-history 0)))
84       (concat (format "%2d:+" index)
85               (riece-format-identity identity))))
86
87 (defun riece-history-format-identity-for-channel-list-indicator (index
88                                                                  identity)
89   (if (and riece-history-enabled
90            (not (ring-empty-p riece-channel-history))
91            (riece-identity-equal identity (ring-ref riece-channel-history 0)))
92       (let ((string (riece-format-identity identity))
93             (start 0))
94         ;; Escape % -> %%.
95         (while (string-match "%" string start)
96           (setq start (1+ (match-end 0))
97                 string (replace-match "%%" nil nil string)))
98         (list (format "%d:" index)
99               (riece-propertize-modeline-string
100                string 'face 'riece-channel-list-history-face)))))
101
102 ;;; (defun riece-history-requires ()
103 ;;;   (if (memq 'riece-guess riece-addons)
104 ;;;       '(riece-guess)))
105
106 (defun riece-history-insinuate ()
107   (add-hook 'riece-after-switch-to-channel-functions
108             (lambda (last)
109               (if (and riece-history-enabled last
110                        (not (riece-identity-equal last riece-current-channel)))
111                   (ring-insert riece-channel-history last))))
112   (add-hook 'riece-format-identity-for-channel-list-buffer-functions
113             'riece-history-format-identity-for-channel-list-buffer)
114   (add-hook 'riece-format-identity-for-channel-list-indicator-functions
115             'riece-history-format-identity-for-channel-list-indicator)
116   (if (memq 'riece-highlight riece-addons)
117       (setq riece-channel-list-mark-face-alist
118             (cons '(?+ . riece-channel-list-history-face)
119                   riece-channel-list-mark-face-alist)))
120 ;;;  (if (memq 'riece-guess riece-addons)
121 ;;;      (add-hook 'riece-guess-channel-try-functions
122 ;;;             'riece-guess-channel-from-history))
123   )
124
125 (defun riece-history-enable ()
126   (setq riece-channel-history
127         (make-ring riece-channel-history-length))
128   (setq riece-history-enabled t)
129   (riece-emit-signal 'channel-list-changed))
130
131 (defun riece-history-disable ()
132   (setq riece-channel-history nil
133         riece-history-enabled nil)
134   (riece-emit-signal 'channel-list-changed))
135
136 (provide 'riece-history)
137
138 ;;; riece-history.el ends here