* riece-options.el (riece-addons): Add riece-button.
[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
39 (eval-when-compile (require 'riece-highlight))
40
41 (defgroup riece-unread nil
42   "Mark unread channels"
43   :tag "Unread"
44   :prefix "riece-"
45   :group 'riece)
46
47 (defface riece-channel-list-unread-face
48   '((((class color)
49       (background dark))
50      (:foreground "orange"))
51     (((class color)
52       (background light))
53      (:foreground "firebrick"))
54     (t
55      (:bold t)))
56   "Face used for displaying unread channels."
57   :group 'riece-highlight-faces)
58 (defvar riece-channel-list-unread-face 'riece-channel-list-unread-face)
59
60 (defvar riece-unread-channels nil)
61
62 (defun riece-unread-after-display-message-function (message)
63   (unless (or (riece-message-own-p message)
64               (equal (riece-message-target message) riece-current-channel))
65     (setq riece-unread-channels
66           (cons (riece-message-target message)
67                 (delete (riece-message-target message) riece-unread-channels)))
68     (riece-unread-update-channel-list-buffer)))
69
70 (defun riece-unread-after-switch-to-channel-function (last)
71   (setq riece-unread-channels
72         (delete riece-current-channel
73                 riece-unread-channels))
74   (riece-unread-update-channel-list-buffer))
75
76 (defun riece-unread-update-channel-list-buffer ()
77   (if riece-channel-list-buffer-mode
78       (save-excursion
79         (set-buffer riece-channel-list-buffer)
80         (let ((inhibit-read-only t)
81               buffer-read-only)
82           (goto-char (point-min))
83           (while (not (eobp))
84             (if (looking-at "\\( ?[0-9]+:\\)\\(.\\)")
85                 (let ((channel (get-text-property (match-end 0)
86                                                   'riece-identity)))
87                   (replace-match
88                    (concat "\\1"
89                            (if (riece-identity-member channel
90                                                       riece-unread-channels)
91                                "!"
92                              "\\2")))))
93             (forward-line))))))
94       
95 (defun riece-unread-switch-to-channel ()
96   (interactive)
97   (if (car riece-unread-channels)
98       (riece-command-switch-to-channel (car riece-unread-channels))
99     (error "No unread channel!")))
100
101 (defun riece-guess-channel-from-unread ()
102   riece-unread-channels)
103
104 (defvar riece-command-mode-map)
105 (defvar riece-dialogue-mode-map)
106 (defvar riece-channel-list-mode-map)
107
108 (defun riece-unread-requires ()
109   (let (requires)
110     (if (memq 'riece-highlight riece-addons)
111         (setq requires (cons 'riece-highlight requires)))
112 ;;;    (if (memq 'riece-guess riece-addons)
113 ;;;     (setq requires (cons 'riece-guess requires)))
114     requires))
115
116 (defun riece-unread-insinuate ()
117   (add-hook 'riece-after-display-message-functions
118             'riece-unread-after-display-message-function)
119   (add-hook 'riece-after-switch-to-channel-functions
120             'riece-unread-after-switch-to-channel-function)
121   (add-hook 'riece-channel-list-mode-hook
122             (lambda ()
123               (add-hook 'riece-update-buffer-functions
124                         'riece-unread-update-channel-list-buffer t)))
125   (define-key riece-command-mode-map
126     "\C-c\C-u" 'riece-unread-switch-to-channel)
127   (define-key riece-dialogue-mode-map
128     "u" 'riece-unread-switch-to-channel)
129   (define-key riece-channel-list-mode-map
130     "u" 'riece-unread-switch-to-channel)
131   (if (memq 'riece-highlight riece-addons)
132       (setq riece-channel-list-mark-face-alist
133             (cons '(?! . riece-channel-list-unread-face)
134                   riece-channel-list-mark-face-alist)))
135 ;;;  (if (memq 'riece-guess riece-addons)
136 ;;;      (add-hook 'riece-guess-channel-try-functions
137 ;;;             'riece-guess-channel-from-unread))
138   )
139
140 (provide 'riece-unread)
141
142 ;;; riece-unread.el ends here