Add the following comment to add-on modules.
[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., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, 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
42 (eval-when-compile (require 'riece-highlight))
43
44 (defgroup riece-unread nil
45   "Mark unread channels."
46   :tag "Unread"
47   :prefix "riece-"
48   :group 'riece)
49
50 (defface riece-channel-list-unread-face
51   '((((class color)
52       (background dark))
53      (:foreground "orange"))
54     (((class color)
55       (background light))
56      (:foreground "firebrick"))
57     (t
58      (:bold t)))
59   "Face used for displaying unread channels."
60   :group 'riece-highlight-faces)
61 (defvar riece-channel-list-unread-face 'riece-channel-list-unread-face)
62
63 (unless (riece-facep 'riece-modeline-unread-face)
64   (make-face 'riece-modeline-unread-face
65              "Face used for displaying unread channels in modeline.")
66   (if (featurep 'xemacs)
67       (set-face-parent 'riece-modeline-unread-face 'modeline))
68   (set-face-foreground 'riece-modeline-unread-face
69                        (face-foreground 'riece-channel-list-unread-face)))
70
71 (defvar riece-unread-channels nil)
72
73 (defvar riece-unread-enabled nil)
74
75 (defconst riece-unread-description
76   "Mark channels where new messages arrived.")
77
78 (defun riece-unread-after-display-message-function (message)
79   (if riece-unread-enabled
80       (let ((target (if (riece-message-private-p message)
81                         (riece-message-speaker message)
82                       (riece-message-target message))))
83         (unless (or (riece-message-own-p message)
84                     (riece-message-type message)
85                     (riece-identity-equal target riece-current-channel)
86                     (riece-identity-member target riece-unread-channels))
87           (setq riece-unread-channels (cons target riece-unread-channels))
88           (riece-emit-signal 'channel-list-changed)))))
89
90 (defun riece-unread-after-switch-to-channel-function (last)
91   (if riece-unread-enabled
92       (setq riece-unread-channels
93             (delq (car (riece-identity-member riece-current-channel
94                                               riece-unread-channels))
95                   riece-unread-channels))))
96
97 (defun riece-unread-format-identity-for-channel-list-buffer (index identity)
98   (if (and riece-unread-enabled
99            (riece-identity-member identity riece-unread-channels))
100       (concat (format "%2d:!" index)
101               (riece-format-identity identity))))
102
103 (defun riece-unread-format-identity-for-channel-list-indicator (index identity)
104   (if (and riece-unread-enabled
105            (riece-identity-member identity riece-unread-channels))
106       (let ((string (riece-format-identity identity))
107             (start 0))
108         ;; Escape % -> %%.
109         (while (string-match "%" string start)
110           (setq start (1+ (match-end 0))
111                 string (replace-match "%%" nil nil string)))
112         (list (format "%d:" index)
113               (riece-propertize-modeline-string
114                string 'face 'riece-modeline-unread-face)))))
115
116 (defun riece-unread-switch-to-channel ()
117   (interactive)
118   (if riece-unread-channels
119       (let ((channel (car riece-unread-channels)))
120         (if (riece-identity-member channel riece-current-channels)
121             (riece-command-switch-to-channel channel)
122           (setq riece-unread-channels
123                 (delete channel riece-unread-channels))
124           (riece-unread-switch-to-channel)))
125     (error "No unread channel!")))
126
127 (defun riece-guess-channel-from-unread ()
128   (reverse riece-unread-channels))
129
130 (defun riece-unread-requires ()
131   (let (requires)
132     (if (memq 'riece-highlight riece-addons)
133         (setq requires (cons 'riece-highlight 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 (defvar riece-command-mode-map)
157 (defvar riece-dialogue-mode-map)
158 (defvar riece-channel-list-mode-map)
159 (defun riece-unread-enable ()
160   (define-key riece-command-mode-map
161     "\C-c\C-u" 'riece-unread-switch-to-channel)
162   (define-key riece-dialogue-mode-map
163     "u" 'riece-unread-switch-to-channel)
164   (define-key riece-channel-list-mode-map
165     "u" 'riece-unread-switch-to-channel)
166   (setq riece-unread-enabled t)
167   (riece-emit-signal 'channel-list-changed))
168
169 (defun riece-unread-disable ()
170   (define-key riece-command-mode-map
171     "\C-c\C-u" nil)
172   (define-key riece-dialogue-mode-map
173     "u" nil)
174   (define-key riece-channel-list-mode-map
175     "u" nil)
176   (setq riece-unread-channels nil
177         riece-unread-enabled nil)
178   (riece-emit-signal 'channel-list-changed))
179
180 (provide 'riece-unread)
181
182 ;;; riece-unread.el ends here