* riece-xemacs.el (riece-run-at-time): Accept nil as the current-time.
[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    '((riece-highlight-server-match 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 (unless (riece-facep 'riece-modeline-current-face)
193   (make-face 'riece-modeline-current-face
194              "Face used for displaying the current channel in modeline.")
195   (if (featurep 'xemacs)
196       (set-face-parent 'riece-modeline-current-face 'modeline))
197   (set-face-foreground 'riece-modeline-current-face
198                        (face-foreground 'riece-channel-list-current-face)))
199
200 (defvar riece-highlight-enabled nil)
201
202 (defconst riece-highlight-description
203   "Highlight IRC buffers")
204
205 (defun riece-highlight-server-match (limit)
206   (and (re-search-forward "(from [^)]+)$" limit t)
207        (get-text-property (match-beginning 0) 'riece-server-name)))
208
209 (defun riece-highlight-setup-dialogue ()
210   (make-local-variable 'font-lock-defaults)
211   (setq font-lock-defaults '(riece-dialogue-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   (make-local-hook 'after-change-functions)
216   (add-hook 'after-change-functions
217             'riece-highlight-hide-prefix nil 'local)
218   (if riece-highlight-enabled
219       (font-lock-mode 1)))
220
221 (defun riece-highlight-setup-channel-list ()
222   (make-local-variable 'font-lock-defaults)
223   (setq font-lock-defaults '(riece-channel-list-font-lock-keywords t))
224   ;; In XEmacs, auto-initialization of font-lock is not affective
225   ;; when buffer-file-name is not set.
226   (font-lock-set-defaults)
227   (if riece-highlight-enabled
228       (font-lock-mode 1)))
229
230 (defun riece-highlight-hide-prefix (start end length)
231   (save-excursion
232     (goto-char start)
233     (if (looking-at riece-prefix-regexp)
234         (put-text-property (match-beginning 1) (match-end 1) 'invisible t))))
235
236 (defun riece-highlight-put-overlay-faces (start end)
237   (if riece-highlight-enabled
238       (riece-scan-property-region
239        'riece-overlay-face
240        start end
241        (lambda (start end)
242          (riece-overlay-put (riece-make-overlay start end)
243                             'face
244                             (get-text-property start 'riece-overlay-face))))))
245
246 (defun riece-highlight-format-identity-for-channel-list-indicator (index
247                                                                    identity)
248   (if (and riece-highlight-enabled
249            (riece-identity-equal identity riece-current-channel))
250       (let ((string (riece-format-identity identity))
251             (start 0))
252         ;; Escape % -> %%.
253         (while (string-match "%" string start)
254           (setq start (1+ (match-end 0))
255                 string (replace-match "%%" nil nil string)))
256         (list (format "%d:" index)
257               (riece-propertize-modeline-string
258                string 'face 'riece-modeline-current-face)))))
259
260 (defun riece-highlight-insinuate ()
261   (put 'riece-channel-mode 'font-lock-defaults
262        '(riece-dialogue-font-lock-keywords t))
263   (add-hook 'riece-channel-mode-hook
264             'riece-highlight-setup-dialogue)
265   (put 'riece-others-mode 'font-lock-defaults
266        '(riece-dialogue-font-lock-keywords t))
267   (add-hook 'riece-others-mode-hook
268             'riece-highlight-setup-dialogue)
269   (put 'riece-dialogue-mode 'font-lock-defaults
270        '(riece-dialogue-font-lock-keywords t))
271   (add-hook 'riece-dialogue-mode-hook
272             'riece-highlight-setup-dialogue)
273   (put 'riece-channel-list-mode 'font-lock-defaults
274        '(riece-channel-list-font-lock-keywords t))
275   (add-hook 'riece-channel-list-mode-hook
276             'riece-highlight-setup-channel-list)
277   (add-hook 'riece-format-identity-for-channel-list-indicator-functions
278             'riece-highlight-format-identity-for-channel-list-indicator)
279   (add-hook 'riece-after-insert-functions
280             'riece-highlight-put-overlay-faces))
281
282 (defun riece-highlight-enable ()
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 1)))
291       (setq buffers (cdr buffers))))
292   (setq riece-highlight-enabled t))
293
294 (defun riece-highlight-disable ()
295   (let ((buffers riece-buffer-list))
296     (while buffers
297       (if (memq (derived-mode-class
298                (with-current-buffer (car buffers)
299                  major-mode))
300                 '(riece-dialogue-mode riece-channel-list-mode))
301           (with-current-buffer (car buffers)
302             (font-lock-mode 0)))
303       (setq buffers (cdr buffers))))
304   (setq riece-highlight-enabled nil))
305
306 (provide 'riece-highlight)
307
308 ;;; riece-highlight.el ends here