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