Debian Codegraph @

contrib/mew-fancy-summary.el

raw ◀ ▶
1 Debian patch touch this file
•1 ;;; mew-fancy-summary.el --- Fontify Mew summary buffer
2 ;;
3 ;; Author: Shun-ichi TAHARA <[email protected]>
4 ;; Hideyuki SHIRAI <[email protected]>
5 ;;
6 ;; Time-stamp: <03/13/2002 20:45 shirai>
7
8 ;;; Commentary:
9 ;;
10 ;; HOW TO USE
11 ;;
12 ;; 1. Set some customize variables and faces as you like.
13 ;;
14 ;; 2. Require this file in your init file of Emacsen.
15 ;;
16 ;; (add-hook 'mew-init-hook (lambda () (require 'mew-fancy-summary)))
17 ;;
18 ;; 3. BE SURE TO USE jit-lock, lazy-shot or lazy-lock.
19 ;;
20 ;; ;;; common setting
21 ;; (require 'font-lock)
22 ;; ;; (global-font-lock-mode t) ;; as you like.
23 ;;
24 ;; ;;; jit-lock-mode (for Emacs-21)
25 ;; (setq font-lock-support-mode 'jit-lock-mode) ;; system default value.
26 ;;
27 ;; ;;; lazy-shot-mode (for XEmacs)
28 ;; (require 'font-lock)
29 ;; (setq font-lock-support-mode 'lazy-shot-mode)
30 ;; (setq lazy-shot-verbose nil)
31 ;; (setq lazy-shot-stealth-verbose nil)
32 ;;
33 ;; ;;; lazy-lock-mode (for All Emacsen)
34 ;; (require 'font-lock)
35 ;; (setq font-lock-support-mode 'lazy-lock-mode)
36 ;; (when (featurep 'xemacs)
37 ;; (setq lazy-lock-minimum-size 0))
38 ;;
39 ;; 4. If you use 'jit-lock-mode' and feel 'scan is too slow'
40 ;; , please put bellow code to your init file.
41 ;;
42 ;; (add-hook 'mew-init-hook
43 ;; (lambda ()
44 ;; (defvar mew-fancy-disable nil)
45 ;; (make-variable-buffer-local 'mew-fancy-disable)
46 ;; (defadvice mew-scan (before fancy-disable activate)
47 ;; (setq mew-fancy-disable t))
48 ;; (defadvice mew-scan-filter (before fancy-disable activate)
49 ;; (when (and mew-fancy-disable font-lock-mode)
50 ;; (setq mew-fancy-disable nil)
51 ;; (font-lock-mode -1)))
52 ;; ))
53 ;;
54 ;; BUGS
55 ;;
56 ;; 1. If you choice 'lazy-lock-mode', mew-summary-cook-folders() becomes
57 ;; too slow.
58 ;;
59
60 ;;; Code:
61
62 (eval-when-compile (require 'mew))
63 (defconst mew-fancy-summary-version "mew-fancy-summary 0.23")
64
65 ;;; User-customizable variables
66
67 (defcustom mew-fancy-summary-face-spec
68 '((num . mew-fancy-summary-face-marginal)
69 (size . mew-fancy-summary-face-marginal)
70 (type . mew-fancy-summary-face-type)
71 (from . "From:")
72 (subj . "Subject:")
73 (date . "Date:")
74 (year . "Date:")
75 (time . "Date:"))
76 "*Alist of face specs for fancy-summary mode, corresponding to
77 scan-form entries. Each entry consists of a pair of symbol or string
78 and face, header name or function.
79
80 The symbol is what can be set in scan-form, including user-extended
81 entry.
82
83 If you want to highlight the raw string in 'mew-scan-form', put the
84 string and the corresponding face (or another possible) in this alist.
85 Also define the face, if needed.
86
87 The face and the function are both set as a symbol in cdr of each
88 entry. As a special case, you can specify 'nil' not to highlight the
89 field.
90
91 If the symbol is a function name, the function is called with an
92 integer argument indicating the width of the field (or 0 means
93 unlimited) and with the pointer pointing the top of the field to be
94 highlighted. In the function, you can fontify it as you like, and no
95 return values are needed (even the cursor position need not be cared).
96
97 The string in cdr of the entry indicates a header name, and a face is
98 decided by 'mew-field-spec' with the header name."
99 :group 'mew-highlight
100 :type '(repeat (cons (choice symbol string) (choice face string function))))
101
102 (defcustom mew-fancy-summary-extended-face-spec
103 '((truncated . mew-fancy-summary-face-truncated)
104 (thread . mew-fancy-summary-face-marginal)
105 (special . mew-fancy-summary-face-special)
106 (ml . mew-fancy-summary-face-tag)
107 (attach . mew-fancy-summary-face-marginal)
108 (to . "To:"))
109 "*Alist of face specs for fancy-summary mode, not corresponding to
110 scan-form entries. Each entry consists of a pair of symbol and face,
111 header name or function.
112
113 Symbol is one of those: 'truncated' for 'T' mark in the 'type' field
114 in scan-form, 'thread' for a thread indent, 'special' for the special
115 person (see also 'mew-fancy-summary-special-*'), 'ml' for a subject
116 prefix of ML (see also 'mew-fancy-summary-ml-regex'), and 'attach' for
117 a attachment line.
118
119 For cdr of each element, see 'mew-fancy-summary-face-spec'."
120 :group 'mew-highlight
121 :type '(repeat (cons (choice symbol string) (choice face string function))))
122
123 (defcustom mew-fancy-summary-special-persons nil
124 "*List of the 'special' persons.
125
126 If the From: header matches a member of this list, 'special' entry of
127 'mew-fancy-summary-extended-face-spec' is used for highlighting 'from'
128 part of scan-form."
129 :group 'mew-highlight
130 :type '(repeat string))
131
132 (defcustom mew-fancy-summary-special-addrbook nil
133 "*If non-nil, nicknames in the addrbook are treated as 'special'
134 person."
135 :group 'mew-highlight
136 :type 'boolean)
137
138 (defcustom mew-fancy-summary-special-to t
139 "*If non-nil, 'from' part of scan-form about an entry of the mail
140 destinated to 'special' person (then displayed To: header) are also
141 highlighted specially."
142 :group 'mew-highlight
143 :type 'boolean)
144
145 (defcustom mew-fancy-summary-ml-regex "[\[(][^])|\n\r]*[\])|]"
146 "*Regex string as a prefix of the subject of mails posted to ML."
147 :group 'mew-highlight
148 :type 'regexp)
149
150 (defcustom mew-fancy-summary-external-highlighting-hook nil
151 "*Hook called to highlight an abnormal line.
152
153 If the function in the hook highlighted the line successfully, it must
154 return t, otherwise nil."
155 :group 'mew-highlight
156 :type 'hook)
157
158 ;;; Faces
159
160 (defface mew-fancy-summary-face-marginal nil
161 "*Face to highlight the marginal part of Summary buffer."
162 :group 'mew-highlight)
163
164 (defface mew-fancy-summary-face-type nil
165 "*Face to highlight 'type' part of Summary buffer (except 'T' mark)."
166 :group 'mew-highlight)
167
168 (defface mew-fancy-summary-face-truncated nil
169 "*Face to highlight 'T' mark at 'type' part of Summary buffer."
170 :group 'mew-highlight)
171
172 (defface mew-fancy-summary-face-special nil
173 "*Face to highlight the special person in 'from' part of Summary buffer."
174 :group 'mew-highlight)
175
176 (defface mew-fancy-summary-face-tag nil
177 "*Face to highlight the tag part of Summary buffer."
178 :group 'mew-highlight)
179
180 ;;; End of user-customizable stuffs
181
182 ;;; Internal variables
183
184 (defvar mew-fancy-summary-special-list nil)
185 (defvar mew-fancy-summary-special-alist nil)
186
187 (defvar mew-fancy-summary-scan-form nil)
188 (make-variable-buffer-local 'mew-fancy-summary-scan-form)
189
190 (defvar mew-fancy-summary-thread-column nil)
191 (make-variable-buffer-local 'mew-fancy-summary-thread-column)
192
193 ;;; Initialization
194
195 (add-hook 'mew-summary-mode-hook 'mew-fancy-summary-enable)
196 (add-hook 'mew-virtual-mode-hook 'mew-fancy-summary-enable)
197 (add-hook 'mew-thread-display-hook 'mew-fancy-summary-thread-enable)
198 (add-hook 'mew-pop-sentinel-hook 'mew-fancy-summary-block)
199 (add-hook 'mew-scan-sentinel-hook 'mew-fancy-summary-block)
200
201 (setq mew-use-highlight-mark nil) ; Hilight mark by itself, not by Mew
202
203 (defalias 'mew-summary-cook-region 'mew-fancy-summary-make-invisible-region)
204 (defalias 'mew-highlight-mark-line 'mew-fancy-summary-mark-line)
205 (defalias 'mew-highlight-unmark-line 'mew-fancy-summary-mark-line)
206
207 ;;; Setup
208
209 (defadvice mew-addrbook-setup (after hl-setup activate)
210 "Setup fancy-summary when starting up Mew or executing \"Z\"."
211 (mew-fancy-summary-special-setup))
212
213 (defun mew-fancy-summary-special-setup ()
214 (setq mew-fancy-summary-special-list mew-fancy-summary-special-persons)
215 (when mew-fancy-summary-special-addrbook
216 (let ((addrbook mew-addrbook-alist) nickname)
217 (while addrbook
218 (setq nickname (nth 2 (car addrbook)))
219 (when (and (stringp nickname)
220 (not (member nickname mew-fancy-summary-special-list)))
221 (setq mew-fancy-summary-special-list
222 (cons nickname mew-fancy-summary-special-list)))
223 (setq addrbook (cdr addrbook))))))
224
225 ;;; Workarounds about highlighting in Mew
226
227 (defun mew-fancy-summary-remove-invisible ()
228 "Remove the invisible hook in Mew."
229 (jit-lock-unregister 'mew-summary-cook-region)
230 (remove-hook 'window-scroll-functions 'mew-summary-cook-window 'local)
231 (remove-hook 'pre-idle-hook 'mew-summary-cook-window))
232
233 (defun mew-fancy-summary-make-invisible-region (beg end &optional interrupt)
234 "Faster invisible function for summary with many text properties."
235 (when (and (memq major-mode
236 '(mew-summary-mode mew-virtual-mode mew-refile-view-mode))
237 mew-summary-buffer-raw)
238 (let ((inhibit-point-motion-hooks t) ret start)
239 (catch 'loop
240 (mew-elet
241 (save-excursion
242 (goto-char beg)
243 (while (and (< (point) end)
244 (search-forward "\r" end t))
245 (setq start (match-beginning 0))
246 (unless (get-text-property start 'invisible)
247 (put-text-property start (line-end-position) 'invisible t))
248 (forward-line)
249 (when (and interrupt (input-pending-p))
250 (throw 'loop (setq ret t)))))))
251 (set-buffer-modified-p nil)
252 ret)))
253
254 (defun mew-fancy-summary-mark-line (&rest args)
255 "Highlighting mark and unmark line is done by mew-fancy-summary."
256 (save-excursion
257 (mew-fancy-summary-region (point) (point) nil 'strip)))
258
259 ;;; Activating fancy summary
260
261 (defun mew-fancy-summary-enable ()
262 "Activate mew-fancy-summary in summary or virtual buffer."
263 (mew-fancy-summary-remove-invisible)
264 (unless (mew-thread-p)
265 (set (make-local-variable 'font-lock-fontify-buffer-function)
266 'mew-fancy-summary-buffer)
267 (when (boundp 'font-lock-function)
268 (set (make-local-variable 'font-lock-function)
269 'mew-fancy-summary-font-lock-function))
270 (set (make-local-variable 'font-lock-fontify-region-function)
271 'mew-fancy-summary-region)
272 (setq mew-fancy-summary-scan-form
273 (mew-get-summary-form (mew-summary-folder-name 'ext)))
274 (setq mew-fancy-summary-thread-column -1)
275 (font-lock-mode 1)))
276
277 (defun mew-fancy-summary-font-lock-function (font-lock-mode)
278 (when font-lock-mode
279 (add-hook 'after-change-functions 'font-lock-after-change-function t t)
280 (font-lock-turn-on-thing-lock))
281 (unless font-lock-mode
282 (remove-hook 'after-change-functions 'font-lock-after-change-function t)
283 (font-lock-unfontify-buffer)
284 (font-lock-turn-off-thing-lock)))
285
286 (defun mew-fancy-summary-thread-enable ()
287 "Activate mew-fancy-summary in thread buffer."
288 (when (mew-thread-p)
289 (mew-fancy-summary-remove-invisible)
290 (setq mew-fancy-summary-scan-form
291 (mew-get-summary-form (substring (mew-summary-folder-name 'ext) 1)))
292 (setq mew-fancy-summary-thread-column (mew-vinfo-get-column))
293 (when (boundp 'font-lock-function)
294 (set (make-local-variable 'font-lock-function)
295 'mew-fancy-summary-font-lock-function))
296 (set (make-local-variable 'font-lock-fontify-buffer-function)
297 'mew-fancy-summary-buffer)
298 (set (make-local-variable 'font-lock-fontify-region-function)
299 'mew-fancy-summary-region)
300 (font-lock-mode 1)))
301
302 ;;; macros
303
304 (defmacro mew-fancy-summary-get-face (key extended)
305 `(let ((face (cdr (assoc ,key (if ,extended
306 mew-fancy-summary-extended-face-spec
307 mew-fancy-summary-face-spec)))))
308 (if (stringp face)
309 (or (mew-nspec-valface (mew-nspec-by-key face))
310 'mew-face-header-marginal)
311 face)))
312
313 (defsubst mew-fancy-summary-special-p (key len)
314 (when (and mew-fancy-summary-special-list (> (length key) 0))
315 (member key (or (cdr (assq len mew-fancy-summary-special-alist))
316 (let ((special mew-fancy-summary-special-list)
317 lst str)
318 (while special
319 (setq str (truncate-string-to-width (car special) len))
320 (when (string-match "^.+\\( +\\)$" str)
321 (setq str (substring str 0 (match-beginning 1))))
322 (setq lst (cons str lst))
323 (setq special (cdr special)))
324 (setq mew-fancy-summary-special-alist
325 (cons (cons len lst) mew-fancy-summary-special-alist))
326 lst)))))
327
328 ;;; Main
329
330 (defun mew-fancy-summary-block ()
331 (font-lock-mode 1))
332
333 (defun mew-fancy-summary-buffer ()
334 "Highlight summary buffer with font-lock-mode."
335 (interactive)
336 (mew-fancy-summary-region (point-min) (point-max)))
337
338 (defun mew-fancy-summary-region (beg end &optional loudly strip)
339 "Highlight the region of summary buffer with font-lock-mode."
340 (interactive "r")
341 (let ((pos (point)) linebeg lineend col numend)
342 (when (memq major-mode
343 '(mew-summary-mode mew-virtual-mode mew-refile-view-mode))
344 (mew-elet
345 (setq end (progn (goto-char end)
346 (forward-line)
347 (point)))
348 (setq beg (progn (goto-char beg)
349 (beginning-of-line)
350 (point)))
351 (when strip
352 (put-text-property beg end 'face nil))
353 (mew-fancy-summary-make-invisible-region beg end)
354 ;; Highlight each line
355 (while (< (point) end)
356 (setq linebeg (point))
357 (setq col 0)
358 (cond
359 ;; unread line
360 ((and (not (mew-in-decode-syntax-p))
361 (setq numend (point-min))
362 (eq (char-after) mew-mark-unread))
363 (let ((form mew-fancy-summary-scan-form) entry)
364 (while form
365 (setq entry (car form))
366 (when (= col mew-fancy-summary-thread-column)
367 (setq col (+ col (mew-fancy-summary-do-highlight 'thread t))))
368 (setq col (+ col (mew-fancy-summary-do-highlight entry nil numend)))
369 (setq form (cdr form))))
370 (looking-at "[^\r\n]*")
371 (setq lineend (match-end 0))
372 (put-text-property linebeg (1+ linebeg) 'face 'mew-face-mark-unread))
373 ;; Normal line
374 ((and (not (mew-in-decode-syntax-p))
375 (setq numend (point-min))
376 (not (looking-at mew-regex-mark)))
377 (let ((form mew-fancy-summary-scan-form) entry)
378 (while form
379 (setq entry (car form))
380 (when (= col mew-fancy-summary-thread-column)
381 (setq col (+ col (mew-fancy-summary-do-highlight 'thread t))))
382 (setq col (+ col (mew-fancy-summary-do-highlight entry nil numend)))
383 (setq form (cdr form))))
384 (looking-at "[^\r\n]*")
385 (setq lineend (match-end 0)))
386 ;; Marked line
387 ((and (not (mew-in-decode-syntax-p))
388 (looking-at mew-regex-mark))
389 (let ((mark (string-to-char (match-string 0))))
390 (looking-at "[^\r\n]*")
391 (setq lineend (match-end 0))
392 (put-text-property linebeg lineend
393 'face (mew-highlight-mark-get-face mark))))
394 ;; Others (usually, expanded multi-part)
395 (t
396 (unless (run-hook-with-args-until-success
397 ;; Highlight thread separator or folder of refile-view
398 ;; by your custom function
399 'mew-fancy-summary-external-highlighting-hook nil)
400 (setq lineend (progn (end-of-line) (point)))
401 (put-text-property linebeg lineend
402 'face (mew-fancy-summary-get-face 'attach t))
403 (when mew-use-highlight-mouse-line
404 (remove-text-properties linebeg (min (1+ lineend) (point-max))
405 '(mouse-face))))))
406 (when mew-use-highlight-mouse-line
407 (put-text-property linebeg lineend
408 'mouse-face mew-highlight-mouse-line-face))
409 (forward-line)))
410 (set-buffer-modified-p nil)
411 (goto-char pos))))
412
413 ;;;
414 (defun mew-fancy-summary-do-highlight (entry &optional extended numend)
415 (let ((l 0) (col (current-column)) beg end range face
416 (spec (mew-fancy-summary-get-spec entry extended)))
417 (while spec
418 (setq range (car (car spec)))
419 (setq face (cdr (car spec)))
420 (cond
421 ((eq range 'num)
422 (if (fboundp face)
423 (funcall face (- numend (point)))
424 (put-text-property (point) numend 'face face))
425 (setq l (+ l (- numend (point))))
426 (goto-char numend)
427 (setq col (current-column)))
428 ((stringp range)
429 (when (looking-at range)
430 (setq beg (match-beginning 0))
431 (setq end (match-end 0))
432 (if (fboundp face)
433 (funcall face range)
434 (put-text-property beg end 'face face))
435 (goto-char end)
436 (setq l (+ l (- (current-column) col)))
437 (setq col (current-column))))
438 ((> range 0)
439 (setq col (+ col (- range l)))
440 (setq beg (point))
441 (if (fboundp face)
442 (funcall face (- range l))
443 (move-to-column col)
444 (put-text-property beg (point) 'face face))
445 (setq l range)))
446 (setq spec (cdr spec)))
447 l))
448
449 (defun mew-fancy-summary-get-spec (entry extended)
450 (let ((range (cond
451 ((stringp entry) (string-width entry))
452 ((listp entry) (abs (car entry)))
453 (t 1)))
454 (elem (cond
455 ((listp entry) (nth 1 entry))
456 (t entry)))
457 face)
458 (if (= range 0) ; To the tail of line
459 (setq range "[^\r\n]*[^ \r\n]"))
460 (setq face (mew-fancy-summary-get-face elem extended))
461 (cond
462 ((eq elem t) ;; thread indent
463 nil)
464 ((eq elem 'num)
465 (list (cons elem face)))
466 ((eq elem 'type)
467 (cond
468 ((looking-at "T")
469 (list (cons range (mew-fancy-summary-get-face 'truncated t))))
470 ((looking-at "[^ ]")
471 (list (cons range face)))
472 (t
473 (list (cons range nil)))))
474 ((eq elem 'thread)
475 (let ((indent (get-text-property (point) 'mew-thread-indent)))
476 (if (or (null indent) (= indent 0))
477 nil
478 (list (cons (string-width
479 (mew-buffer-substring
480 (point)
481 (next-single-property-change (point) 'mew-thread-indent)))
482 face)))))
483 ((eq elem 'from)
484 (let* ((beg (point))
485 (entry (mew-buffer-substring
486 beg
487 (prog2
488 (move-to-column (+ (current-column) range))
489 (point)
490 (goto-char beg))))
491 (prelen (and (stringp mew-summary-form-from-me-prefix)
492 (string-width mew-summary-form-from-me-prefix)))
493 elen)
494 (if (string-match "[^\r\n]*[^ \r\n]" entry)
495 (setq entry (substring entry (match-beginning 0) (match-end 0)))
496 (setq entry ""))
497 (setq elen (string-width entry))
498 (if (not (and prelen (> prelen 0)
499 (eq (string-match mew-summary-form-from-me-prefix entry) 0)))
500 ;; Mail to me
501 (if (mew-fancy-summary-special-p entry range)
502 (list (cons elen (mew-fancy-summary-get-face 'special t))
503 (cons range nil))
504 (list (cons elen face) (cons range nil)))
505 ;; Mail from me
506 (setq face (mew-fancy-summary-get-face 'to t))
507 (if (and prelen mew-fancy-summary-special-to
508 (mew-fancy-summary-special-p (substring entry prelen)
509 (- range prelen)))
510 (list (cons prelen face)
511 (cons elen (mew-fancy-summary-get-face 'special t))
512 (cons range nil))
513 (list (cons elen face) (cons range nil))))))
514 ((eq elem 'subj)
515 (list (cons mew-fancy-summary-ml-regex
516 (mew-fancy-summary-get-face 'ml t))
517 (cons
518 (if (> mew-fancy-summary-thread-column 0)
519 (save-excursion
520 (move-to-column mew-fancy-summary-thread-column)
521 (- range
522 (* (mew-thread-get-property (point))
523 mew-thread-indent-width)))
524 range)
525 face)))
526 (t
527 (list (cons range face))))))
528
529 ;;;
530
531 (provide 'mew-fancy-summary)
532
533 ;;; Copyright Notice:
534
535 ;; Copyright (C) 2001 Shun-ichi TAHARA <[email protected]>
536 ;; Copyright (C) 1999-2001 Hideyuki SHIRAI <[email protected]>
537 ;; Copyright (C) 1994-2001 Mew developing team.
538 ;; All rights reserved.
539
540 ;; Redistribution and use in source and binary forms, with or without
541 ;; modification, are permitted provided that the following conditions
542 ;; are met:
543 ;;
544 ;; 1. Redistributions of source code must retain the above copyright
545 ;; notice, this list of conditions and the following disclaimer.
546 ;; 2. Redistributions in binary form must reproduce the above copyright
547 ;; notice, this list of conditions and the following disclaimer in the
548 ;; documentation and/or other materials provided with the distribution.
549 ;; 3. Neither the name of the team nor the names of its contributors
550 ;; may be used to endorse or promote products derived from this software
551 ;; without specific prior written permission.
552 ;;
553 ;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
554 ;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
555 ;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
556 ;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
557 ;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
558 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
559 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
560 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
561 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
562 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
563 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
564
565 ;;; mew-fancy-summary.el ends here