* riece-xface.el: Support enable/disable.
[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 (defvar riece-unread-channels nil)
62
63 (defvar riece-unread-enabled nil)
64
65 (defconst riece-unread-description
66   "Mark channels where \"unread\" messages arrived")
67
68 (defun riece-unread-after-display-message-function (message)
69   (if riece-unread-enabled
70       (let ((target (if (riece-message-private-p message)
71                         (riece-message-speaker message)
72                       (riece-message-target message))))
73         (unless (or (riece-message-own-p message)
74                     (riece-message-type message)
75                     (riece-identity-equal target riece-current-channel)
76                     (riece-identity-member target riece-unread-channels))
77           (setq riece-unread-channels (cons target riece-unread-channels))
78           (riece-emit-signal 'channel-list-changed)))))
79
80 (defun riece-unread-after-switch-to-channel-function (last)
81   (if riece-unread-enabled
82       (setq riece-unread-channels
83             (delete riece-current-channel
84                     riece-unread-channels))))
85
86 (defun riece-unread-format-identity-for-channel-list-buffer (index identity)
87   (if (and riece-unread-enabled
88            (riece-identity-member identity riece-unread-channels))
89       (concat (format "%2d:!" index)
90               (riece-format-identity identity))))
91
92 (defun riece-unread-format-identity-for-channel-list-indicator (index identity)
93   (if (and riece-unread-enabled
94            (riece-identity-member identity riece-unread-channels))
95       (let ((string (riece-format-identity identity))
96             (start 0))
97         ;; Escape % -> %%.
98         (while (string-match "%" string start)
99           (setq start (1+ (match-end 0))
100                 string (replace-match "%%" nil nil string)))
101         (list (format "%d:" index)
102               (riece-propertize-modeline-string
103                string 'face 'riece-channel-list-unread-face)))))
104
105 (defun riece-unread-switch-to-channel ()
106   (interactive)
107   (if riece-unread-channels
108       (let ((channel (car riece-unread-channels)))
109         (if (riece-identity-member channel riece-current-channels)
110             (riece-command-switch-to-channel channel)
111           (setq riece-unread-channels
112                 (delete channel riece-unread-channels))
113           (riece-unread-switch-to-channel)))
114     (error "No unread channel!")))
115
116 (defun riece-guess-channel-from-unread ()
117   riece-unread-channels)
118
119 (defun riece-unread-requires ()
120   (let (requires)
121     (if (memq 'riece-highlight riece-addons)
122         (setq requires (cons 'riece-highlight requires)))
123 ;;;    (if (memq 'riece-guess riece-addons)
124 ;;;     (setq requires (cons 'riece-guess requires)))
125     requires))
126
127 (defun riece-unread-insinuate ()
128   (add-hook 'riece-after-display-message-functions
129             'riece-unread-after-display-message-function)
130   (add-hook 'riece-after-switch-to-channel-functions
131             'riece-unread-after-switch-to-channel-function)
132   (add-hook 'riece-format-identity-for-channel-list-buffer-functions
133             'riece-unread-format-identity-for-channel-list-buffer)
134   (add-hook 'riece-format-identity-for-channel-list-indicator-functions
135             'riece-unread-format-identity-for-channel-list-indicator)
136   (if (memq 'riece-highlight riece-addons)
137       (setq riece-channel-list-mark-face-alist
138             (cons '(?! . riece-channel-list-unread-face)
139                   riece-channel-list-mark-face-alist)))
140 ;;;  (if (memq 'riece-guess riece-addons)
141 ;;;      (add-hook 'riece-guess-channel-try-functions
142 ;;;             'riece-guess-channel-from-unread))
143   )
144
145 (defvar riece-command-mode-map)
146 (defvar riece-dialogue-mode-map)
147 (defvar riece-channel-list-mode-map)
148 (defun riece-unread-enable ()
149   (define-key riece-command-mode-map
150     "\C-c\C-u" 'riece-unread-switch-to-channel)
151   (define-key riece-dialogue-mode-map
152     "u" 'riece-unread-switch-to-channel)
153   (define-key riece-channel-list-mode-map
154     "u" 'riece-unread-switch-to-channel)
155   (setq riece-unread-enabled t)
156   (riece-emit-signal 'channel-list-changed))
157
158 (defun riece-unread-disable ()
159   (define-key riece-command-mode-map
160     "\C-c\C-u" nil)
161   (define-key riece-dialogue-mode-map
162     "u" nil)
163   (define-key riece-channel-list-mode-map
164     "u" nil)
165   (setq riece-unread-enabled nil)
166   (riece-emit-signal 'channel-list-changed))
167
168 (provide 'riece-unread)
169
170 ;;; riece-unread.el ends here