Simplified.
[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             (delq (car (riece-identity-member riece-current-channel
92                                               riece-unread-channels))
93                   riece-unread-channels))))
94
95 (defun riece-unread-format-identity-for-channel-list-buffer (index identity)
96   (if (and riece-unread-enabled
97            (riece-identity-member identity riece-unread-channels))
98       (concat (format "%2d:!" index)
99               (riece-format-identity identity))))
100
101 (defun riece-unread-format-identity-for-channel-list-indicator (index identity)
102   (if (and riece-unread-enabled
103            (riece-identity-member identity riece-unread-channels))
104       (let ((string (riece-format-identity identity))
105             (start 0))
106         ;; Escape % -> %%.
107         (while (string-match "%" string start)
108           (setq start (1+ (match-end 0))
109                 string (replace-match "%%" nil nil string)))
110         (list (format "%d:" index)
111               (riece-propertize-modeline-string
112                string 'face 'riece-modeline-unread-face)))))
113
114 (defun riece-unread-switch-to-channel ()
115   (interactive)
116   (if riece-unread-channels
117       (let ((channel (car riece-unread-channels)))
118         (if (riece-identity-member channel riece-current-channels)
119             (riece-command-switch-to-channel channel)
120           (setq riece-unread-channels
121                 (delete channel riece-unread-channels))
122           (riece-unread-switch-to-channel)))
123     (error "No unread channel!")))
124
125 (defun riece-guess-channel-from-unread ()
126   riece-unread-channels)
127
128 (defun riece-unread-requires ()
129   (let (requires)
130     (if (memq 'riece-highlight riece-addons)
131         (setq requires (cons 'riece-highlight requires)))
132 ;;;    (if (memq 'riece-guess riece-addons)
133 ;;;     (setq requires (cons 'riece-guess requires)))
134     requires))
135
136 (defun riece-unread-insinuate ()
137   (add-hook 'riece-after-display-message-functions
138             'riece-unread-after-display-message-function)
139   (add-hook 'riece-after-switch-to-channel-functions
140             'riece-unread-after-switch-to-channel-function)
141   (add-hook 'riece-format-identity-for-channel-list-buffer-functions
142             'riece-unread-format-identity-for-channel-list-buffer)
143   (add-hook 'riece-format-identity-for-channel-list-indicator-functions
144             'riece-unread-format-identity-for-channel-list-indicator)
145   (if (memq 'riece-highlight riece-addons)
146       (setq riece-channel-list-mark-face-alist
147             (cons '(?! . riece-channel-list-unread-face)
148                   riece-channel-list-mark-face-alist)))
149 ;;;  (if (memq 'riece-guess riece-addons)
150 ;;;      (add-hook 'riece-guess-channel-try-functions
151 ;;;             'riece-guess-channel-from-unread))
152   )
153
154 (defvar riece-command-mode-map)
155 (defvar riece-dialogue-mode-map)
156 (defvar riece-channel-list-mode-map)
157 (defun riece-unread-enable ()
158   (define-key riece-command-mode-map
159     "\C-c\C-u" 'riece-unread-switch-to-channel)
160   (define-key riece-dialogue-mode-map
161     "u" 'riece-unread-switch-to-channel)
162   (define-key riece-channel-list-mode-map
163     "u" 'riece-unread-switch-to-channel)
164   (setq riece-unread-enabled t)
165   (riece-emit-signal 'channel-list-changed))
166
167 (defun riece-unread-disable ()
168   (define-key riece-command-mode-map
169     "\C-c\C-u" nil)
170   (define-key riece-dialogue-mode-map
171     "u" nil)
172   (define-key riece-channel-list-mode-map
173     "u" nil)
174   (setq riece-unread-channels nil
175         riece-unread-enabled nil)
176   (riece-emit-signal 'channel-list-changed))
177
178 (provide 'riece-unread)
179
180 ;;; riece-unread.el ends here