Fixed.
[riece] / lisp / riece-unread.el
1 ;;; riece-unread.el --- "unread message mark" 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 ;; This add-on marks channels where new messages are arrived.
27
28 ;; You can check the unread 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-unread)
33
34 ;;; Code:
35
36 (require 'riece-message)
37 (require 'riece-commands)
38 (require 'riece-signal)
39
40 (eval-when-compile (require 'riece-highlight))
41
42 (defgroup riece-unread nil
43   "Mark unread channels"
44   :tag "Unread"
45   :prefix "riece-"
46   :group 'riece)
47
48 (defface riece-channel-list-unread-face
49   '((((class color)
50       (background dark))
51      (:foreground "orange"))
52     (((class color)
53       (background light))
54      (:foreground "firebrick"))
55     (t
56      (:bold t)))
57   "Face used for displaying unread channels."
58   :group 'riece-highlight-faces)
59 (defvar riece-channel-list-unread-face 'riece-channel-list-unread-face)
60
61 (unless (riece-facep 'riece-modeline-unread-face)
62   (make-face 'riece-modeline-unread-face
63              "Face used for displaying unread channels in modeline.")
64   (if (featurep 'xemacs)
65       (set-face-parent 'riece-modeline-unread-face 'modeline))
66   (set-face-foreground 'riece-modeline-unread-face
67                        (face-foreground 'riece-channel-list-unread-face)))
68
69 (defvar riece-unread-channels nil)
70
71 (defvar riece-unread-enabled nil)
72
73 (defconst riece-unread-description
74   "Mark channels where \"unread\" messages arrived")
75
76 (defun riece-unread-after-display-message-function (message)
77   (if riece-unread-enabled
78       (let ((target (if (riece-message-private-p message)
79                         (riece-message-speaker message)
80                       (riece-message-target message))))
81         (unless (or (riece-message-own-p message)
82                     (riece-message-type message)
83                     (riece-identity-equal target riece-current-channel)
84                     (riece-identity-member target riece-unread-channels))
85           (setq riece-unread-channels (cons target riece-unread-channels))
86           (riece-emit-signal 'channel-list-changed)))))
87
88 (defun riece-unread-after-switch-to-channel-function (last)
89   (if riece-unread-enabled
90       (setq riece-unread-channels
91             (delete riece-current-channel
92                     riece-unread-channels))))
93
94 (defun riece-unread-format-identity-for-channel-list-buffer (index identity)
95   (if (and riece-unread-enabled
96            (riece-identity-member identity riece-unread-channels))
97       (concat (format "%2d:!" index)
98               (riece-format-identity identity))))
99
100 (defun riece-unread-format-identity-for-channel-list-indicator (index identity)
101   (if (and riece-unread-enabled
102            (riece-identity-member identity riece-unread-channels))
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-unread-face)))))
112
113 (defun riece-unread-switch-to-channel ()
114   (interactive)
115   (if riece-unread-channels
116       (let ((channel (car riece-unread-channels)))
117         (if (riece-identity-member channel riece-current-channels)
118             (riece-command-switch-to-channel channel)
119           (setq riece-unread-channels
120                 (delete channel riece-unread-channels))
121           (riece-unread-switch-to-channel)))
122     (error "No unread channel!")))
123
124 (defun riece-guess-channel-from-unread ()
125   riece-unread-channels)
126
127 (defun riece-unread-requires ()
128   (let (requires)
129     (if (memq 'riece-highlight riece-addons)
130         (setq requires (cons 'riece-highlight requires)))
131 ;;;    (if (memq 'riece-guess riece-addons)
132 ;;;     (setq requires (cons 'riece-guess requires)))
133     requires))
134
135 (defun riece-unread-insinuate ()
136   (add-hook 'riece-after-display-message-functions
137             'riece-unread-after-display-message-function)
138   (add-hook 'riece-after-switch-to-channel-functions
139             'riece-unread-after-switch-to-channel-function)
140   (add-hook 'riece-format-identity-for-channel-list-buffer-functions
141             'riece-unread-format-identity-for-channel-list-buffer)
142   (add-hook 'riece-format-identity-for-channel-list-indicator-functions
143             'riece-unread-format-identity-for-channel-list-indicator)
144   (if (memq 'riece-highlight riece-addons)
145       (setq riece-channel-list-mark-face-alist
146             (cons '(?! . riece-channel-list-unread-face)
147                   riece-channel-list-mark-face-alist)))
148 ;;;  (if (memq 'riece-guess riece-addons)
149 ;;;      (add-hook 'riece-guess-channel-try-functions
150 ;;;             'riece-guess-channel-from-unread))
151   )
152
153 (defvar riece-command-mode-map)
154 (defvar riece-dialogue-mode-map)
155 (defvar riece-channel-list-mode-map)
156 (defun riece-unread-enable ()
157   (define-key riece-command-mode-map
158     "\C-c\C-u" 'riece-unread-switch-to-channel)
159   (define-key riece-dialogue-mode-map
160     "u" 'riece-unread-switch-to-channel)
161   (define-key riece-channel-list-mode-map
162     "u" 'riece-unread-switch-to-channel)
163   (setq riece-unread-enabled t)
164   (riece-emit-signal 'channel-list-changed))
165
166 (defun riece-unread-disable ()
167   (define-key riece-command-mode-map
168     "\C-c\C-u" nil)
169   (define-key riece-dialogue-mode-map
170     "u" nil)
171   (define-key riece-channel-list-mode-map
172     "u" nil)
173   (setq riece-unread-channels nil
174         riece-unread-enabled nil)
175   (riece-emit-signal 'channel-list-changed))
176
177 (provide 'riece-unread)
178
179 ;;; riece-unread.el ends here