(riece-read-variables-files): Re-evaluate custom settings.
[riece] / lisp / riece-unread.el
1 ;;; riece-unread.el --- mark channels where new messages arrived
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., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
23
24 ;;; Commentary:
25
26 ;; NOTE: This is an add-on module for Riece.
27
28 ;; This add-on marks channels where new messages arrived.
29
30 ;; You can check the unread channels via `C-c g' in the commands
31 ;; buffer, by adding the following lines to ~/.riece/init.el:
32
33 ;;   (add-hook 'riece-guess-channel-try-functions
34 ;;             'riece-guess-channel-from-unread)
35
36 ;;; Code:
37
38 (require 'riece-message)
39 (require 'riece-commands)
40 (require 'riece-signal)
41 (require 'riece-highlight)
42
43 (defgroup riece-unread nil
44   "Mark unread channels."
45   :tag "Unread"
46   :prefix "riece-"
47   :group 'riece)
48
49 (defface riece-channel-list-unread-face
50   '((((class color)
51       (background dark))
52      (:foreground "orange"))
53     (((class color)
54       (background light))
55      (:foreground "firebrick"))
56     (t
57      (:bold t)))
58   "Face used for displaying unread channels."
59   :group 'riece-highlight-faces)
60 (defvar riece-channel-list-unread-face 'riece-channel-list-unread-face)
61
62 (unless (riece-facep 'riece-modeline-unread-face)
63   (make-face 'riece-modeline-unread-face
64              "Face used for displaying unread channels in modeline.")
65   (if (featurep 'xemacs)
66       (set-face-parent 'riece-modeline-unread-face 'modeline))
67   (set-face-foreground 'riece-modeline-unread-face
68                        (face-foreground 'riece-channel-list-unread-face)))
69
70 (defvar riece-unread-channels nil)
71
72 (defconst riece-unread-description
73   "Mark channels where new messages arrived.")
74
75 (defun riece-unread-after-display-message-function (message)
76   (if (get 'riece-unread 'riece-addon-enabled)
77       (let ((target (if (riece-message-private-p message)
78                         (riece-message-speaker message)
79                       (riece-message-target message))))
80         (unless (or (riece-message-own-p message)
81                     (riece-message-type message)
82                     (riece-identity-equal target riece-current-channel)
83                     (riece-identity-member target riece-unread-channels))
84           (setq riece-unread-channels (cons target riece-unread-channels))
85           (riece-emit-signal 'channel-list-changed)))))
86
87 (defun riece-unread-after-switch-to-channel-function (last)
88   (if (get 'riece-unread 'riece-addon-enabled)
89       (setq riece-unread-channels
90             (delq (car (riece-identity-member riece-current-channel
91                                               riece-unread-channels))
92                   riece-unread-channels))))
93
94 (defun riece-unread-format-identity-for-channel-list-buffer (index identity)
95   (if (and (get 'riece-unread 'riece-addon-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 (get 'riece-unread 'riece-addon-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   (reverse 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     ;; To override riece-history's channel mark in the channel list buffer.
132     (if (memq 'riece-history riece-addons)
133         (setq requires (cons 'riece-history requires)))
134 ;;;    (if (memq 'riece-guess riece-addons)
135 ;;;     (setq requires (cons 'riece-guess requires)))
136     requires))
137
138 (defun riece-unread-insinuate ()
139   (add-hook 'riece-after-display-message-functions
140             'riece-unread-after-display-message-function)
141   (add-hook 'riece-after-switch-to-channel-functions
142             'riece-unread-after-switch-to-channel-function)
143   (add-hook 'riece-format-identity-for-channel-list-buffer-functions
144             'riece-unread-format-identity-for-channel-list-buffer)
145   (add-hook 'riece-format-identity-for-channel-list-indicator-functions
146             'riece-unread-format-identity-for-channel-list-indicator)
147   (if (memq 'riece-highlight riece-addons)
148       (setq riece-channel-list-mark-face-alist
149             (cons '(?! . riece-channel-list-unread-face)
150                   riece-channel-list-mark-face-alist)))
151 ;;;  (if (memq 'riece-guess riece-addons)
152 ;;;      (add-hook 'riece-guess-channel-try-functions
153 ;;;             'riece-guess-channel-from-unread))
154   )
155
156 (defun riece-unread-uninstall ()
157   (remove-hook 'riece-after-display-message-functions
158                'riece-unread-after-display-message-function)
159   (remove-hook 'riece-after-switch-to-channel-functions
160                'riece-unread-after-switch-to-channel-function)
161   (remove-hook 'riece-format-identity-for-channel-list-buffer-functions
162                'riece-unread-format-identity-for-channel-list-buffer)
163   (remove-hook 'riece-format-identity-for-channel-list-indicator-functions
164                'riece-unread-format-identity-for-channel-list-indicator)
165   (setq riece-channel-list-mark-face-alist
166         (delq (assq ?! riece-channel-list-mark-face-alist)
167               riece-channel-list-mark-face-alist))
168 ;;;  (if (memq 'riece-guess riece-addons)
169 ;;;      (add-hook 'riece-guess-channel-try-functions
170 ;;;             'riece-guess-channel-from-unread))
171   )
172
173 (defvar riece-command-mode-map)
174 (defvar riece-dialogue-mode-map)
175 (defvar riece-channel-list-mode-map)
176 (defun riece-unread-enable ()
177   (define-key riece-command-mode-map
178     "\C-c\C-u" 'riece-unread-switch-to-channel)
179   (define-key riece-dialogue-mode-map
180     "u" 'riece-unread-switch-to-channel)
181   (define-key riece-channel-list-mode-map
182     "u" 'riece-unread-switch-to-channel)  
183   (riece-emit-signal 'channel-list-changed))
184
185 (defun riece-unread-disable ()
186   (define-key riece-command-mode-map
187     "\C-c\C-u" nil)
188   (define-key riece-dialogue-mode-map
189     "u" nil)
190   (define-key riece-channel-list-mode-map
191     "u" nil)
192   (setq riece-unread-channels nil)
193   (riece-emit-signal 'channel-list-changed))
194
195 (provide 'riece-unread)
196
197 ;;; riece-unread.el ends here