* riece-server.el: Move autoload setting for 'riece-exit before
[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 'riece-misc)
31 (require 'font-lock)
32 (require 'derived)
33
34 (defgroup riece-highlight nil
35   "Highlight IRC buffers"
36   :tag "Highlight"
37   :prefix "riece-"
38   :group 'riece)
39
40 (defgroup riece-highlight-faces nil
41   "Faces for highlight IRC buffers"
42   :tag "Faces"
43   :prefix "riece-highlight-"
44   :group 'riece-highlight)
45
46 (defface riece-dialogue-change-face
47   '((((class color)
48       (background dark))
49      (:foreground "cyan" :bold t))
50     (((class color)
51       (background light))
52      (:foreground "RoyalBlue" :bold t))
53     (t
54      (:bold t)))
55   "Face used for displaying \"*** Change:\" line"
56   :group 'riece-highlight-faces)
57 (defvar riece-dialogue-change-face 'riece-dialogue-change-face)
58
59 (defface riece-dialogue-notice-face
60   '((((class color)
61       (background dark))
62      (:foreground "green2" :bold t))
63     (((class color)
64       (background light))
65      (:foreground "MidnightBlue" :bold t))
66     (t
67      (:bold t)))
68   "Face used for displaying \"*** Notice:\" line"
69   :group 'riece-highlight-faces)
70 (defvar riece-dialogue-notice-face 'riece-dialogue-notice-face)
71
72 (defface riece-dialogue-wallops-face
73   '((((class color)
74       (background dark))
75      (:foreground "yellow" :bold t))
76     (((class color)
77       (background light))
78      (:foreground "blue4" :bold t))
79     (t
80      (:bold t)))
81   "Face used for displaying \"*** Wallops:\" line"
82   :group 'riece-highlight-faces)
83 (defvar riece-dialogue-wallops-face 'riece-dialogue-wallops-face)
84
85 (defface riece-dialogue-error-face
86   '((((class color)
87       (background dark))
88      (:foreground "cornflower blue" :bold t))
89     (((class color)
90       (background light))
91      (:foreground "DarkGreen"))
92     (t
93      (:bold t)))
94   "Face used for displaying \"*** Error:\" line"
95   :group 'riece-highlight-faces)
96 (defvar riece-dialogue-error-face 'riece-dialogue-error-face)
97
98 (defface riece-dialogue-info-face
99   '((((class color)
100       (background dark))
101      (:foreground "PaleTurquoise" :bold t))
102     (((class color)
103       (background light))
104      (:foreground "RoyalBlue"))
105     (t
106      (:bold t)))
107   "Face used for displaying \"*** Info:\" line"
108   :group 'riece-highlight-faces)
109 (defvar riece-dialogue-info-face 'riece-dialogue-info-face)
110
111 (defface riece-dialogue-server-face
112   '((((class color)
113       (background dark))
114      (:foreground "Gray70"))
115     (((class color)
116       (background light))
117      (:foreground "DimGray"))
118     (t
119      (:bold t)))
120   "Face used for displaying \"(from server)\" extent."
121   :group 'riece-highlight-faces)
122 (defvar riece-dialogue-server-face 'riece-dialogue-server-face)
123
124 (defface riece-dialogue-prefix-face
125   '((((class color)
126       (background dark))
127      (:foreground "moccasin"))
128     (((class color)
129       (background light))
130      (:foreground "firebrick"))
131     (t
132      (:bold nil)))
133   "Face used for displaying \"<nick>\" extent"
134   :group 'riece-highlight-faces)
135 (defvar riece-dialogue-prefix-face 'riece-dialogue-prefix-face)
136
137 (defcustom riece-dialogue-font-lock-keywords
138   (append
139    (list (list (concat "^" riece-time-prefix-regexp
140                        "\\(<[^>]+>\\|>[^<]+<\\|([^)]+)\\|{[^}]+}\\|=[^=]+=\\)")
141                '(1 riece-dialogue-prefix-face append t)))
142    ;; set property to the whole line
143    (mapcar
144     (lambda (line)
145       (cons
146        (concat
147         "^" riece-time-prefix-regexp "\\("
148         (regexp-quote
149          (symbol-value (intern (format "riece-%s-prefix" line))))
150         ".*\\)$")
151        (list 1 (intern (format "riece-dialogue-%s-face" line)) t t)))
152     '(change notice wallops error info))
153    (list (list "(from [^)]+)$" 0 riece-dialogue-server-face t)))
154   "Default expressions to highlight in riece-dialogue-mode."
155   :type '(repeat (list string))
156   :group 'riece-highlight)
157
158 (defface riece-channel-list-default-face
159   '((t ()))
160   "Face used for displaying channels."
161   :group 'riece-highlight-faces)
162 (defvar riece-channel-list-default-face 'riece-channel-list-default-face)
163
164 (defface riece-channel-list-current-face
165   '((((class color)
166       (background dark))
167      (:foreground "turquoise" :underline t))
168     (((class color)
169       (background light))
170      (:foreground "SeaGreen" :underline t))
171     (t
172      ()))
173   "Face used for displaying the current channel."
174   :group 'riece-highlight-faces)
175 (defvar riece-channel-list-current-face 'riece-channel-list-current-face)
176
177 (defcustom riece-channel-list-mark-face-alist
178   '((?* . riece-channel-list-current-face))
179   "An alist mapping marks on riece-channel-list-buffer to faces."
180   :type 'list
181   :group 'riece-highlight)
182
183 (defcustom riece-channel-list-font-lock-keywords
184   '(("^[ 0-9][0-9]:\\(.\\)\\(.*\\)"
185      (2 (or (cdr (assq (aref (match-string 1) 0)
186                        riece-channel-list-mark-face-alist))
187             riece-channel-list-default-face))))
188   "Default expressions to highlight in riece-channel-list-mode."
189   :type '(repeat (list string))
190   :group 'riece-highlight)
191
192 (defvar riece-highlight-enabled nil)
193
194 (defconst riece-highlight-description
195   "Highlight IRC buffers")
196
197 (defun riece-highlight-setup-dialogue ()
198   (make-local-variable 'font-lock-defaults)
199   (setq font-lock-defaults '(riece-dialogue-font-lock-keywords t))
200   ;; In XEmacs, auto-initialization of font-lock is not affective
201   ;; when buffer-file-name is not set.
202   (font-lock-set-defaults)
203   (make-local-hook 'after-change-functions)
204   (add-hook 'after-change-functions
205             'riece-highlight-hide-prefix nil 'local)
206   (if riece-highlight-enabled
207       (font-lock-mode 1)))
208
209 (defun riece-highlight-setup-channel-list ()
210   (make-local-variable 'font-lock-defaults)
211   (setq font-lock-defaults '(riece-channel-list-font-lock-keywords t))
212   ;; In XEmacs, auto-initialization of font-lock is not affective
213   ;; when buffer-file-name is not set.
214   (font-lock-set-defaults)
215   (if riece-highlight-enabled
216       (font-lock-mode 1)))
217
218 (defun riece-highlight-hide-prefix (start end length)
219   (save-excursion
220     (goto-char start)
221     (if (looking-at riece-prefix-regexp)
222         (put-text-property (match-beginning 1) (match-end 1) 'invisible t))))
223
224 (defun riece-highlight-put-overlay-faces (start end)
225   (if riece-highlight-enabled
226       (riece-scan-property-region
227        'riece-overlay-face
228        start end
229        (lambda (start end)
230          (riece-overlay-put (riece-make-overlay start end)
231                             'face
232                             (get-text-property start 'riece-overlay-face))))))
233
234 (defun riece-highlight-format-identity-for-channel-list-indicator (index
235                                                                    identity)
236   (if (and riece-highlight-enabled
237            (riece-identity-equal identity riece-current-channel))
238       (let ((string (riece-format-identity identity))
239             (start 0))
240         ;; Escape % -> %%.
241         (while (string-match "%" string start)
242           (setq start (1+ (match-end 0))
243                 string (replace-match "%%" nil nil string)))
244         (list (format "%d:" index)
245               (riece-propertize-modeline-string
246                string 'face 'riece-channel-list-current-face)))))
247
248 (defun riece-highlight-insinuate ()
249   (put 'riece-channel-mode 'font-lock-defaults
250        '(riece-dialogue-font-lock-keywords t))
251   (add-hook 'riece-channel-mode-hook
252             'riece-highlight-setup-dialogue)
253   (put 'riece-others-mode 'font-lock-defaults
254        '(riece-dialogue-font-lock-keywords t))
255   (add-hook 'riece-others-mode-hook
256             'riece-highlight-setup-dialogue)
257   (put 'riece-dialogue-mode 'font-lock-defaults
258        '(riece-dialogue-font-lock-keywords t))
259   (add-hook 'riece-dialogue-mode-hook
260             'riece-highlight-setup-dialogue)
261   (put 'riece-channel-list-mode 'font-lock-defaults
262        '(riece-channel-list-font-lock-keywords t))
263   (add-hook 'riece-channel-list-mode-hook
264             'riece-highlight-setup-channel-list)
265   (add-hook 'riece-format-identity-for-channel-list-indicator-functions
266             'riece-highlight-format-identity-for-channel-list-indicator)
267   (add-hook 'riece-after-insert-functions
268             'riece-highlight-put-overlay-faces))
269
270 (defun riece-highlight-enable ()
271   (let ((buffers riece-buffer-list))
272     (while buffers
273       (if (memq (derived-mode-class
274                (with-current-buffer (car buffers)
275                  major-mode))
276                 '(riece-dialogue-mode riece-channel-list-mode))
277           (with-current-buffer (car buffers)
278             (font-lock-mode 1)))
279       (setq buffers (cdr buffers))))
280   (setq riece-highlight-enabled t))
281
282 (defun riece-highlight-disable ()
283   (let ((buffers riece-buffer-list))
284     (while buffers
285       (if (memq (derived-mode-class
286                (with-current-buffer (car buffers)
287                  major-mode))
288                 '(riece-dialogue-mode riece-channel-list-mode))
289           (with-current-buffer (car buffers)
290             (font-lock-mode 0)))
291       (setq buffers (cdr buffers))))
292   (setq riece-highlight-enabled nil))
293
294 (provide 'riece-highlight)
295
296 ;;; riece-highlight.el ends here