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