Fix last change.
[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 displays unread mark ("!") for channels which have
27 ;; "unread messages".
28
29 ;; To use, add the following line to your ~/.riece/init.el:
30 ;; (add-to-list 'riece-addons 'riece-unread t)
31
32 ;;; Code:
33
34 (eval-when-compile (require 'riece-message))
35
36 (defvar riece-unread-channels nil)
37
38 (defun riece-unread-display-message-function (message)
39   (unless (or (riece-message-own-p message)
40               (and (equal (riece-message-target message) riece-current-channel)
41                    (eq (window-buffer (selected-window))
42                        (get-buffer riece-command-buffer))))
43     (setq riece-unread-channels
44           (delete (riece-message-target message) riece-unread-channels))
45     (add-to-list 'riece-unread-channels
46                  (riece-message-target message))
47     (riece-unread-update-channel-list-buffer)))
48
49 (defun riece-unread-channel-switch-hook ()
50   (setq riece-unread-channels
51         (delete riece-current-channel
52                 riece-unread-channels))
53   (riece-unread-update-channel-list-buffer))
54
55 (defun riece-unread-update-channel-list-buffer ()
56   (if riece-channel-list-buffer-mode
57       (save-excursion
58         (set-buffer riece-channel-list-buffer)
59         (let ((inhibit-read-only t)
60               buffer-read-only)
61           (goto-char (point-min))
62           (while (not (eobp))
63             (if (looking-at "\\( ?[0-9]+:\\)\\([ !]\\)\\(.+\\)")
64                 (let ((channel (match-string 3)))
65                   (replace-match
66                    (concat "\\1"
67                            (if (member channel riece-unread-channels)
68                                "!"
69                              " ")
70                            "\\3"))))
71             (forward-line))))))
72       
73 (defun riece-unread-switch-to-channel ()
74   (interactive)
75   (if (car riece-unread-channels)
76       (riece-command-switch-to-channel (car riece-unread-channels))
77     (error "No unread channel!")))
78
79 (defvar riece-command-mode-map)
80 (defvar riece-dialogue-mode-map)
81 (defvar riece-channel-list-mode-map)
82
83 (defun riece-unread-insinuate ()
84   (add-hook 'riece-after-display-message-functions
85             'riece-unread-display-message-function)
86   (add-hook 'riece-channel-switch-hook
87             'riece-unread-channel-switch-hook)
88   (add-hook 'riece-update-buffers-hook
89             'riece-unread-update-channel-list-buffer)
90   (define-key riece-command-mode-map
91     "\C-c\C-u" 'riece-unread-switch-to-channel)
92   (define-key riece-dialogue-mode-map
93     "u" 'riece-unread-switch-to-channel)
94   (define-key riece-channel-list-mode-map
95     "u" 'riece-unread-switch-to-channel))
96
97 (provide 'riece-unread)
98
99 ;;; riece-unread.el ends here