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