Added Riece pkg subtree
[packages] / xemacs-packages / riece / lisp / riece-highlight.el
1 ;;; riece-highlight.el --- highlight IRC buffers -*- lexical-binding: t -*-
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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;; NOTE: This is an add-on module for Riece.
28
29 ;;; Code:
30
31 (require 'riece-globals)
32 (require 'riece-options)                ;riece-channel-list-buffer-mode
33 (require 'riece-identity)               ;riece-format-identity
34 (require 'riece-misc)
35 (require 'font-lock)
36 (require 'derived)
37
38 (defgroup riece-highlight nil
39   "Decorate IRC buffers with faces and fonts."
40   :tag "Highlight"
41   :prefix "riece-"
42   :group 'riece)
43
44 (defgroup riece-highlight-faces nil
45   "Faces for highlight IRC buffers."
46   :tag "Faces"
47   :prefix "riece-highlight-"
48   :group 'riece-highlight)
49
50 (defface riece-dialogue-change-face
51   '((((class color)
52       (background dark))
53      (:foreground "cyan" :bold t))
54     (((class color)
55       (background light))
56      (:foreground "RoyalBlue" :bold t))
57     (t
58      (:bold t)))
59   "Face used for displaying \"*** Change:\" line"
60   :group 'riece-highlight-faces)
61 (defvar riece-dialogue-change-face 'riece-dialogue-change-face)
62
63 (defface riece-dialogue-notice-face
64   '((((class color)
65       (background dark))
66      (:foreground "green2" :bold t))
67     (((class color)
68       (background light))
69      (:foreground "MidnightBlue" :bold t))
70     (t
71      (:bold t)))
72   "Face used for displaying \"*** Notice:\" line"
73   :group 'riece-highlight-faces)
74 (defvar riece-dialogue-notice-face 'riece-dialogue-notice-face)
75
76 (defface riece-dialogue-wallops-face
77   '((((class color)
78       (background dark))
79      (:foreground "yellow" :bold t))
80     (((class color)
81       (background light))
82      (:foreground "blue4" :bold t))
83     (t
84      (:bold t)))
85   "Face used for displaying \"*** Wallops:\" line"
86   :group 'riece-highlight-faces)
87 (defvar riece-dialogue-wallops-face 'riece-dialogue-wallops-face)
88
89 (defface riece-dialogue-error-face
90   '((((class color)
91       (background dark))
92      (:foreground "cornflower blue" :bold t))
93     (((class color)
94       (background light))
95      (:foreground "DarkGreen"))
96     (t
97      (:bold t)))
98   "Face used for displaying \"*** Error:\" line"
99   :group 'riece-highlight-faces)
100 (defvar riece-dialogue-error-face 'riece-dialogue-error-face)
101
102 (defface riece-dialogue-info-face
103   '((((class color)
104       (background dark))
105      (:foreground "PaleTurquoise" :bold t))
106     (((class color)
107       (background light))
108      (:foreground "RoyalBlue"))
109     (t
110      (:bold t)))
111   "Face used for displaying \"*** Info:\" line"
112   :group 'riece-highlight-faces)
113 (defvar riece-dialogue-info-face 'riece-dialogue-info-face)
114
115 (defface riece-dialogue-server-face
116   '((((class color)
117       (background dark))
118      (:foreground "Gray70"))
119     (((class color)
120       (background light))
121      (:foreground "DimGray"))
122     (t
123      (:bold t)))
124   "Face used for displaying \"(from server)\" extent."
125   :group 'riece-highlight-faces)
126 (defvar riece-dialogue-server-face 'riece-dialogue-server-face)
127
128 (defface riece-dialogue-prefix-face
129   '((((class color)
130       (background dark))
131      (:foreground "moccasin"))
132     (((class color)
133       (background light))
134      (:foreground "firebrick"))
135     (t
136      (:bold nil)))
137   "Face used for displaying \"<nick>\" extent"
138   :group 'riece-highlight-faces)
139 (defvar riece-dialogue-prefix-face 'riece-dialogue-prefix-face)
140
141 (defcustom riece-dialogue-font-lock-keywords
142   (append
143    (list (list (concat "^" riece-time-prefix-regexp
144                        "\\(<[^>]+>\\|>[^<]+<\\|([^)]+)\\|{[^}]+}\\|=[^=]+=\\)")
145                '(1 riece-dialogue-prefix-face append t)))
146    ;; set property to the whole line
147    (mapcar
148     (lambda (line)
149       (cons
150        (concat
151         "^" riece-time-prefix-regexp "\\("
152         (regexp-quote
153          (symbol-value (intern (format "riece-%s-prefix" line))))
154         ".*\\)$")
155        (list 1 (intern (format "riece-dialogue-%s-face" line)) t t)))
156     '(change notice wallops error info))
157    '((riece-highlight-server-match 0 riece-dialogue-server-face t)))
158   "Default expressions to highlight in riece-dialogue-mode."
159   :type '(repeat (list string))
160   :group 'riece-highlight)
161
162 (defface riece-channel-list-default-face
163   '((t ()))
164   "Face used for displaying channels."
165   :group 'riece-highlight-faces)
166 (defvar riece-channel-list-default-face 'riece-channel-list-default-face)
167
168 (defface riece-channel-list-current-face
169   '((((class color)
170       (background dark))
171      (:foreground "turquoise" :underline t))
172     (((class color)
173       (background light))
174      (:foreground "SeaGreen" :underline t))
175     (t
176      ()))
177   "Face used for displaying the current channel."
178   :group 'riece-highlight-faces)
179 (defvar riece-channel-list-current-face 'riece-channel-list-current-face)
180
181 (defcustom riece-channel-list-mark-face-alist
182   '((?* . riece-channel-list-current-face))
183   "An alist mapping marks on riece-channel-list-buffer to faces."
184   :type '(repeat (cons character symbol))
185   :group 'riece-highlight)
186
187 (defcustom riece-channel-list-font-lock-keywords
188   '(("^[ 0-9][0-9]:\\(.\\)\\(.*\\)"
189      (2 (or (cdr (assq (aref (match-string 1) 0)
190                        riece-channel-list-mark-face-alist))
191             riece-channel-list-default-face))))
192   "Default expressions to highlight in riece-channel-list-mode."
193   :type '(repeat (list string))
194   :group 'riece-highlight)
195
196 (unless (riece-facep 'riece-modeline-current-face)
197   ;; In Emacs, set-face-doc-string is an alias to
198   ;; set-face-documentation, but we use the former since it is
199   ;; available in both Emacs and XEmacs.
200   (make-face 'riece-modeline-current-face)
201   (set-face-doc-string
202    'riece-modeline-current-face
203    "Face used for displaying the current channel in modeline.")
204   (if (featurep 'xemacs)
205       (set-face-parent 'riece-modeline-current-face 'modeline))
206   (set-face-foreground 'riece-modeline-current-face
207                        (face-foreground 'riece-channel-list-current-face)))
208
209 (defconst riece-highlight-description
210   "Highlight IRC buffers.")
211
212 (defun riece-highlight-server-match (limit)
213   (and (re-search-forward "(from [^)]+)$" limit t)
214        (get-text-property (match-beginning 0) 'riece-server-name)))
215
216 (defun riece-highlight-setup-dialogue ()
217   (make-local-variable 'font-lock-defaults)
218   (setq font-lock-defaults '(riece-dialogue-font-lock-keywords t))
219   ;; In XEmacs, auto-initialization of font-lock is not affective
220   ;; when buffer-file-name is not set.
221   (font-lock-set-defaults)
222   (riece-make-local-hook 'after-change-functions)
223   (add-hook 'after-change-functions
224             'riece-highlight-hide-prefix nil t)
225   (if (get 'riece-highlight 'riece-addon-enabled)
226       (font-lock-mode 1)))
227
228 (defun riece-highlight-setup-channel-list ()
229   (make-local-variable 'font-lock-defaults)
230   (setq font-lock-defaults '(riece-channel-list-font-lock-keywords t))
231   ;; In XEmacs, auto-initialization of font-lock is not affective
232   ;; when buffer-file-name is not set.
233   (font-lock-set-defaults)
234   (if (get 'riece-highlight 'riece-addon-enabled)
235       (font-lock-mode 1)))
236
237 (defun riece-highlight-hide-prefix (start _end _length)
238   (save-excursion
239     (goto-char start)
240     (if (looking-at riece-prefix-regexp)
241         (put-text-property (match-beginning 1) (match-end 1) 'invisible t))))
242
243 (defun riece-highlight-put-overlay-faces (start end)
244   (if (get 'riece-highlight 'riece-addon-enabled)
245       (riece-scan-property-region
246        'riece-overlay-face
247        start end
248        (lambda (start end)
249          (riece-overlay-put (riece-make-overlay start end)
250                             'face
251                             (get-text-property start 'riece-overlay-face))))))
252
253 (defun riece-highlight-format-identity-for-channel-list-indicator (index
254                                                                    identity)
255   (if (and (get 'riece-highlight 'riece-addon-enabled)
256            (riece-identity-equal identity riece-current-channel))
257       (let ((string (riece-format-identity identity))
258             (start 0))
259         ;; Escape % -> %%.
260         (while (string-match "%" string start)
261           (setq start (1+ (match-end 0))
262                 string (replace-match "%%" nil nil string)))
263         (list (format "%d:" index)
264               (riece-propertize-modeline-string
265                string 'face 'riece-modeline-current-face)))))
266
267 (defun riece-highlight-insinuate ()
268   (put 'riece-channel-mode 'font-lock-defaults
269        '(riece-dialogue-font-lock-keywords t))
270   (put 'riece-others-mode 'font-lock-defaults
271        '(riece-dialogue-font-lock-keywords t))
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-uninstall ()
286   (let ((buffers riece-buffer-list))
287     (save-excursion
288       (while buffers
289         (set-buffer (car buffers))
290         (if (riece-derived-mode-p 'riece-dialogue-mode)
291             (remove-hook 'after-change-functions
292                          'riece-highlight-hide-prefix t))
293         (setq buffers (cdr buffers)))))
294   (riece-remprop 'riece-channel-mode 'font-lock-defaults)
295   (riece-remprop 'riece-others-mode 'font-lock-defaults)
296   (riece-remprop 'riece-dialogue-mode 'font-lock-defaults)
297   (remove-hook 'riece-dialogue-mode-hook
298                'riece-highlight-setup-dialogue)
299   (riece-remprop 'riece-channel-list-mode 'font-lock-defaults)
300   (remove-hook 'riece-channel-list-mode-hook
301                'riece-highlight-setup-channel-list)
302   (remove-hook 'riece-format-identity-for-channel-list-indicator-functions
303                'riece-highlight-format-identity-for-channel-list-indicator)
304   (remove-hook 'riece-after-insert-functions
305                'riece-highlight-put-overlay-faces))
306
307 (defun riece-highlight-enable ()
308   (let ((buffers riece-buffer-list))
309     (while buffers
310       (if (with-current-buffer (car buffers)
311             (riece-derived-mode-p 'riece-dialogue-mode
312                                   'riece-channel-list-mode))
313           (with-current-buffer (car buffers)
314             (font-lock-mode 1)))
315       (setq buffers (cdr buffers)))))
316
317 (defun riece-highlight-disable ()
318   (let ((buffers riece-buffer-list))
319     (while buffers
320       (if (with-current-buffer (car buffers)
321             (riece-derived-mode-p 'riece-dialogue-mode
322                                   'riece-channel-list-mode))
323           (with-current-buffer (car buffers)
324             (font-lock-mode 0)))
325       (setq buffers (cdr buffers)))))
326
327 (provide 'riece-highlight)
328
329 ;;; riece-highlight.el ends here