21a877195cbd82aea691374f4c3c5711cbe9b04f
[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)
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               (equal (riece-message-target message) riece-current-channel))
41     (setq riece-unread-channels
42           (delete (riece-message-target message) riece-unread-channels))
43     (add-to-list 'riece-unread-channels
44                  (riece-message-target message))
45     (riece-unread-update-channel-list-buffer)))
46
47 (defun riece-unread-channel-switch-hook ()
48   (setq riece-unread-channels
49         (delete riece-current-channel
50                 riece-unread-channels))
51   (riece-unread-update-channel-list-buffer))
52
53 (defun riece-unread-update-channel-list-buffer ()
54   (if riece-channel-list-buffer-mode
55       (save-excursion
56         (set-buffer riece-channel-list-buffer)
57         (let ((inhibit-read-only t)
58               buffer-read-only)
59           (goto-char (point-min))
60           (while (not (eobp))
61             (if (looking-at "\\( ?[0-9]+:\\)\\([ !]\\)\\(.+\\)")
62                 (let ((channel (save-match-data
63                                  (riece-parse-identity (match-string 3)))))
64                   (replace-match
65                    (concat "\\1"
66                            (if (member channel riece-unread-channels)
67                                "!"
68                              " ")
69                            "\\3"))))
70             (forward-line))))))
71       
72 (defun riece-unread-switch-to-channel ()
73   (interactive)
74   (if (car riece-unread-channels)
75       (riece-command-switch-to-channel (car riece-unread-channels))
76     (error "No unread channel!")))
77
78 (defvar riece-command-mode-map)
79 (defvar riece-dialogue-mode-map)
80 (defvar riece-channel-list-mode-map)
81
82 (defun riece-unread-insinuate ()
83   (add-hook 'riece-after-display-message-functions
84             'riece-unread-display-message-function)
85   (add-hook 'riece-channel-switch-hook
86             'riece-unread-channel-switch-hook)
87   (add-hook 'riece-update-buffer-functions
88             'riece-unread-update-channel-list-buffer t)
89   (define-key riece-command-mode-map
90     "\C-c\C-u" 'riece-unread-switch-to-channel)
91   (define-key riece-dialogue-mode-map
92     "u" 'riece-unread-switch-to-channel)
93   (define-key riece-channel-list-mode-map
94     "u" 'riece-unread-switch-to-channel))
95
96 (provide 'riece-unread)
97
98 ;;; riece-unread.el ends here