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