* riece-highlight.el: Require 'derived.
[riece] / lisp / riece-highlight.el
1 ;;; riece-highlight.el --- highlight IRC buffers
2 ;; Copyright (C) 1998-2003 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
6 ;; Keywords: IRC, riece
7
8 ;; This file is part of Riece.
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (require 'riece-globals)
28 (require 'riece-options)                ;riece-channel-list-buffer-mode
29 (require 'riece-identity)               ;riece-format-identity
30 (require 'font-lock)
31 (require 'derived)
32
33 (defgroup riece-highlight nil
34   "Highlight IRC buffers"
35   :tag "Highlight"
36   :prefix "riece-"
37   :group 'riece)
38
39 (defgroup riece-highlight-faces nil
40   "Faces for highlight IRC buffers"
41   :tag "Faces"
42   :prefix "riece-highlight-"
43   :group 'riece-highlight)
44
45 (defface riece-dialogue-change-face
46   '((((class color)
47       (background dark))
48      (:foreground "cyan" :bold t))
49     (((class color)
50       (background light))
51      (:foreground "RoyalBlue" :bold t))
52     (t
53      (:bold t)))
54   "Face used for displaying \"*** Change:\" line"
55   :group 'riece-highlight-faces)
56 (defvar riece-dialogue-change-face 'riece-dialogue-change-face)
57
58 (defface riece-dialogue-notice-face
59   '((((class color)
60       (background dark))
61      (:foreground "green2" :bold t))
62     (((class color)
63       (background light))
64      (:foreground "MidnightBlue" :bold t))
65     (t
66      (:bold t)))
67   "Face used for displaying \"*** Notice:\" line"
68   :group 'riece-highlight-faces)
69 (defvar riece-dialogue-notice-face 'riece-dialogue-notice-face)
70
71 (defface riece-dialogue-wallops-face
72   '((((class color)
73       (background dark))
74      (:foreground "yellow" :bold t))
75     (((class color)
76       (background light))
77      (:foreground "blue4" :bold t))
78     (t
79      (:bold t)))
80   "Face used for displaying \"*** Wallops:\" line"
81   :group 'riece-highlight-faces)
82 (defvar riece-dialogue-wallops-face 'riece-dialogue-wallops-face)
83
84 (defface riece-dialogue-error-face
85   '((((class color)
86       (background dark))
87      (:foreground "cornflower blue" :bold t))
88     (((class color)
89       (background light))
90      (:foreground "DarkGreen"))
91     (t
92      (:bold t)))
93   "Face used for displaying \"*** Error:\" line"
94   :group 'riece-highlight-faces)
95 (defvar riece-dialogue-error-face 'riece-dialogue-error-face)
96
97 (defface riece-dialogue-info-face
98   '((((class color)
99       (background dark))
100      (:foreground "PaleTurquoise" :bold t))
101     (((class color)
102       (background light))
103      (:foreground "RoyalBlue"))
104     (t
105      (:bold t)))
106   "Face used for displaying \"*** Info:\" line"
107   :group 'riece-highlight-faces)
108 (defvar riece-dialogue-info-face 'riece-dialogue-info-face)
109
110 (defface riece-dialogue-server-face
111   '((((class color)
112       (background dark))
113      (:foreground "Gray70"))
114     (((class color)
115       (background light))
116      (:foreground "DimGray"))
117     (t
118      (:bold t)))
119   "Face used for displaying \"(from server)\" extent."
120   :group 'riece-highlight-faces)
121 (defvar riece-dialogue-server-face 'riece-dialogue-server-face)
122
123 (defface riece-dialogue-prefix-face
124   '((((class color)
125       (background dark))
126      (:foreground "moccasin"))
127     (((class color)
128       (background light))
129      (:foreground "firebrick"))
130     (t
131      (:bold nil)))
132   "Face used for displaying \"<nick>\" extent"
133   :group 'riece-highlight-faces)
134 (defvar riece-dialogue-prefix-face 'riece-dialogue-prefix-face)
135
136 (defcustom riece-dialogue-font-lock-keywords
137   (append
138    (list (list (concat "^" riece-time-prefix-regexp
139                        "\\(<[^>]+>\\|>[^<]+<\\|([^)]+)\\|{[^}]+}\\|=[^=]+=\\)")
140                '(1 riece-dialogue-prefix-face append t)))
141    ;; set property to the whole line
142    (mapcar
143     (lambda (line)
144       (cons
145        (concat
146         "^" riece-time-prefix-regexp "\\("
147         (regexp-quote
148          (symbol-value (intern (format "riece-%s-prefix" line))))
149         ".*\\)$")
150        (list 1 (intern (format "riece-dialogue-%s-face" line)) t t)))
151     '(change notice wallops error info))
152    (list (list "(from [^)]+)$" 0 riece-dialogue-server-face t)))
153   "Default expressions to highlight in riece-dialogue-mode."
154   :type '(repeat (list string))
155   :group 'riece-highlight)
156
157 (defface riece-channel-list-default-face
158   '((t ()))
159   "Face used for displaying channels."
160   :group 'riece-highlight-faces)
161 (defvar riece-channel-list-default-face 'riece-channel-list-default-face)
162
163 (defface riece-channel-list-current-face
164   '((((class color)
165       (background dark))
166      (:foreground "turquoise" :underline t))
167     (((class color)
168       (background light))
169      (:foreground "SeaGreen" :underline t))
170     (t
171      ()))
172   "Face used for displaying the current channel."
173   :group 'riece-highlight-faces)
174 (defvar riece-channel-list-current-face 'riece-channel-list-current-face)
175
176 (defcustom riece-channel-list-mark-face-alist
177   '((?* . riece-channel-list-current-face))
178   "An alist mapping marks on riece-channel-list-buffer to faces."
179   :type 'list
180   :group 'riece-highlight)
181
182 (defcustom riece-channel-list-font-lock-keywords
183   '(("^[ 0-9][0-9]:\\(.\\)\\(.*\\)"
184      (2 (or (cdr (assq (aref (match-string 1) 0)
185                        riece-channel-list-mark-face-alist))
186             riece-channel-list-default-face))))
187   "Default expressions to highlight in riece-channel-list-mode."
188   :type '(repeat (list string))
189   :group 'riece-highlight)
190
191 (defvar riece-highlight-enabled nil)
192
193 (defconst riece-highlight-description
194   "Highlight IRC buffers")
195
196 (defun riece-highlight-setup-dialogue ()
197   (make-local-variable 'font-lock-defaults)
198   (setq font-lock-defaults '(riece-dialogue-font-lock-keywords t))
199   ;; In XEmacs, auto-initialization of font-lock is not affective
200   ;; when buffer-file-name is not set.
201   (font-lock-set-defaults)
202   (make-local-hook 'after-change-functions)
203   (add-hook 'after-change-functions
204             'riece-highlight-hide-prefix nil 'local)
205   (if riece-highlight-enabled
206       (font-lock-mode 1)))
207
208 (defun riece-highlight-setup-channel-list ()
209   (make-local-variable 'font-lock-defaults)
210   (setq font-lock-defaults '(riece-channel-list-font-lock-keywords t))
211   ;; In XEmacs, auto-initialization of font-lock is not affective
212   ;; when buffer-file-name is not set.
213   (font-lock-set-defaults)
214   (if riece-highlight-enabled
215       (font-lock-mode 1)))
216
217 (defun riece-highlight-hide-prefix (start end length)
218   (save-excursion
219     (goto-char start)
220     (if (looking-at riece-prefix-regexp)
221         (put-text-property (match-beginning 1) (match-end 1) 'invisible t))))
222
223 (defun riece-highlight-put-overlay-faces (start end)
224   (if riece-highlight-enabled
225       (riece-scan-property-region
226        'riece-overlay-face
227        start end
228        (lambda (start end)
229          (riece-overlay-put (riece-make-overlay start end)
230                             'face
231                             (get-text-property start 'riece-overlay-face))))))
232
233 (defun riece-highlight-format-identity-for-channel-list-indicator (index
234                                                                    identity)
235   (if (and riece-highlight-enabled
236            (riece-identity-equal identity riece-current-channel))
237       (let ((string (riece-format-identity identity))
238             (start 0))
239         ;; Escape % -> %%.
240         (while (string-match "%" string start)
241           (setq start (1+ (match-end 0))
242                 string (replace-match "%%" nil nil string)))
243         (list (format "%d:" index)
244               (riece-propertize-modeline-string
245                string 'face 'riece-channel-list-current-face)))))
246
247 (defun riece-highlight-insinuate ()
248   (put 'riece-channel-mode 'font-lock-defaults
249        '(riece-dialogue-font-lock-keywords t))
250   (add-hook 'riece-channel-mode-hook
251             'riece-highlight-setup-dialogue)
252   (put 'riece-others-mode 'font-lock-defaults
253        '(riece-dialogue-font-lock-keywords t))
254   (add-hook 'riece-others-mode-hook
255             'riece-highlight-setup-dialogue)
256   (put 'riece-dialogue-mode 'font-lock-defaults
257        '(riece-dialogue-font-lock-keywords t))
258   (add-hook 'riece-dialogue-mode-hook
259             'riece-highlight-setup-dialogue)
260   (put 'riece-channel-list-mode 'font-lock-defaults
261        '(riece-channel-list-font-lock-keywords t))
262   (add-hook 'riece-channel-list-mode-hook
263             'riece-highlight-setup-channel-list)
264   (add-hook 'riece-format-identity-for-channel-list-indicator-functions
265             'riece-highlight-format-identity-for-channel-list-indicator)
266   (add-hook 'riece-after-insert-functions
267             'riece-highlight-put-overlay-faces))
268
269 (defun riece-highlight-enable ()
270   (let ((buffers riece-buffer-list))
271     (while buffers
272       (if (memq (derived-mode-class
273                (with-current-buffer (car buffers)
274                  major-mode))
275                 '(riece-dialogue-mode riece-channel-list-mode))
276           (with-current-buffer (car buffers)
277             (font-lock-mode 1)))
278       (setq buffers (cdr buffers))))
279   (setq riece-highlight-enabled t))
280
281 (defun riece-highlight-disable ()
282   (let ((buffers riece-buffer-list))
283     (while buffers
284       (if (memq (derived-mode-class
285                (with-current-buffer (car buffers)
286                  major-mode))
287                 '(riece-dialogue-mode riece-channel-list-mode))
288           (with-current-buffer (car buffers)
289             (font-lock-mode 0)))
290       (setq buffers (cdr buffers))))
291   (setq riece-highlight-enabled nil))
292
293 (provide 'riece-highlight)
294
295 ;;; riece-highlight.el ends here