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