Synch with Oort Gnus.
[elisp/gnus.git-] / lisp / gnus-spec.el
1 ;;; gnus-spec.el --- format spec functions for Gnus  -*- coding: iso-latin-1 -*-
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;      Katsumi Yamaoka <yamaoka@jpl.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'alist)
33 (require 'gnus)
34
35 ;;; Internal variables.
36
37 (defvar gnus-summary-mark-positions nil)
38 (defvar gnus-group-mark-positions nil)
39 (defvar gnus-group-indentation "")
40
41 ;; Format specs.  The chunks below are the machine-generated forms
42 ;; that are to be evaled as the result of the default format strings.
43 ;; We write them in here to get them byte-compiled.  That way the
44 ;; default actions will be quite fast, while still retaining the full
45 ;; flexibility of the user-defined format specs.
46
47 ;; First we have lots of dummy defvars to let the compiler know these
48 ;; are really dynamic variables.
49
50 (defvar gnus-tmp-unread)
51 (defvar gnus-tmp-replied)
52 (defvar gnus-tmp-score-char)
53 (defvar gnus-tmp-indentation)
54 (defvar gnus-tmp-opening-bracket)
55 (defvar gnus-tmp-lines)
56 (defvar gnus-tmp-name)
57 (defvar gnus-tmp-closing-bracket)
58 (defvar gnus-tmp-subject-or-nil)
59 (defvar gnus-tmp-subject)
60 (defvar gnus-tmp-marked)
61 (defvar gnus-tmp-marked-mark)
62 (defvar gnus-tmp-subscribed)
63 (defvar gnus-tmp-process-marked)
64 (defvar gnus-tmp-number-of-unread)
65 (defvar gnus-tmp-group-name)
66 (defvar gnus-tmp-group)
67 (defvar gnus-tmp-article-number)
68 (defvar gnus-tmp-unread-and-unselected)
69 (defvar gnus-tmp-news-method)
70 (defvar gnus-tmp-news-server)
71 (defvar gnus-tmp-article-number)
72 (defvar gnus-mouse-face)
73 (defvar gnus-mouse-face-prop)
74
75 (defun gnus-summary-line-format-spec ()
76   (insert gnus-tmp-unread gnus-tmp-replied
77           gnus-tmp-score-char gnus-tmp-indentation)
78   (gnus-put-text-property
79    (point)
80    (progn
81      (insert
82       gnus-tmp-opening-bracket
83       (format "%4d: %-20s"
84               gnus-tmp-lines
85               (if (> (length gnus-tmp-name) 20)
86                   (substring gnus-tmp-name 0 20)
87                 gnus-tmp-name))
88       gnus-tmp-closing-bracket)
89      (point))
90    gnus-mouse-face-prop gnus-mouse-face)
91   (insert " " gnus-tmp-subject-or-nil "\n"))
92
93 (defvar gnus-summary-line-format-spec
94   (gnus-byte-code 'gnus-summary-line-format-spec))
95
96 (defun gnus-summary-dummy-line-format-spec ()
97   (insert "*  ")
98   (gnus-put-text-property
99    (point)
100    (progn
101      (insert ":                          :")
102      (point))
103    gnus-mouse-face-prop gnus-mouse-face)
104   (insert " " gnus-tmp-subject "\n"))
105
106 (defvar gnus-summary-dummy-line-format-spec
107   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
108
109 (defun gnus-group-line-format-spec ()
110   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
111           gnus-tmp-process-marked
112           gnus-group-indentation
113           (format "%5s: " gnus-tmp-number-of-unread))
114   (gnus-put-text-property
115    (point)
116    (progn
117      (insert gnus-tmp-group "\n")
118      (1- (point)))
119    gnus-mouse-face-prop gnus-mouse-face))
120 (defvar gnus-group-line-format-spec
121   (gnus-byte-code 'gnus-group-line-format-spec))
122
123 (defvar gnus-format-specs
124   `((group ("%M\%S\%p\%P\%5y: %(%g%)%l\n" ,gnus-group-line-format-spec))
125     (summary-dummy ("*  %(:                          :%) %S\n"
126                     ,gnus-summary-dummy-line-format-spec))
127     (summary ("%U\%R\%z\%I\%(%[%4L: %-20,20n%]%) %s\n"
128               ,gnus-summary-line-format-spec)))
129   "Alist of format specs.")
130
131 (defvar gnus-format-specs-compiled nil
132   "Alist of compiled format specs.  Each element should be the form:
133 \(TYPE (FORMAT-STRING-1 . COMPILED-FUNCTION-1)
134                  :
135        (FORMAT-STRING-n . COMPILED-FUNCTION-n)).")
136
137 (defvar gnus-article-mode-line-format-spec nil)
138 (defvar gnus-summary-mode-line-format-spec nil)
139 (defvar gnus-group-mode-line-format-spec nil)
140
141 ;;; Phew.  All that gruft is over, fortunately.
142
143 ;;;###autoload
144 (defun gnus-update-format (var)
145   "Update the format specification near point."
146   (interactive
147    (list
148     (save-excursion
149       (eval-defun nil)
150       ;; Find the end of the current word.
151       (re-search-forward "[ \t\n]" nil t)
152       ;; Search backward.
153       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
154         (match-string 1)))))
155   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
156                               (match-string 1 var))))
157          (value (symbol-value (intern var)))
158          (spec (set
159                 (intern (format "%s-spec" var))
160                 (gnus-parse-format
161                  value (symbol-value (intern (format "%s-alist" var)))
162                  (not (string-match "mode" var)))))
163          (entry (assq type gnus-format-specs)))
164     (if entry
165         (let ((elem (assoc value entry)))
166           (if elem
167               (setcdr elem spec)
168             (setcdr entry (cons (cons value elem) (cdr entry)))))
169       (push (list type (cons value spec)) gnus-format-specs))
170     (gnus-product-variable-touch 'gnus-format-specs)
171
172     (pop-to-buffer "*Gnus Format*")
173     (erase-buffer)
174     (lisp-interaction-mode)
175     (insert (pp-to-string spec))))
176
177 (put 'gnus-search-or-regist-spec 'lisp-indent-function 1)
178 (defmacro gnus-search-or-regist-spec (mspec &rest body)
179   (let ((specs (nth 0 mspec)) (type (nth 1 mspec)) (format (nth 2 mspec))
180         (spec (nth 3 mspec)) (entry (nth 4 mspec)) (elem (nth 5 mspec)))
181     `(let* ((,entry (assq ,type ,specs))
182             (,elem (assoc ,format (cdr ,entry))))
183        (or (cdr ,elem)
184            (when (progn ,@body)
185              (if ,entry
186                  (if ,elem
187                      (setcdr ,elem ,spec)
188                    (setcdr ,entry (cons (cons ,format ,spec) (cdr ,entry))))
189                (push (list ,type (cons ,format ,spec)) ,specs))
190              (gnus-product-variable-touch (quote ,specs)))
191            ,spec))))
192
193 (defun gnus-update-format-specification-1 (type format val)
194   (set (intern (format "gnus-%s-line-format-spec" type))
195        (gnus-search-or-regist-spec (gnus-format-specs-compiled
196                                     type format val entry elem)
197          (when (and gnus-compile-user-specs val)
198            (setq val (prog1
199                          (progn
200                            (fset 'gnus-tmp-func `(lambda () ,val))
201                            (require 'bytecomp)
202                            (let (byte-compile-warnings)
203                              (byte-compile 'gnus-tmp-func))
204                            (gnus-byte-code 'gnus-tmp-func))
205                        (when (get-buffer "*Compile-Log*")
206                          (bury-buffer "*Compile-Log*"))
207                        (when (get-buffer "*Compile-Log-Show*")
208                          (bury-buffer "*Compile-Log-Show*"))))))))
209
210 (defun gnus-update-format-specifications (&optional force &rest types)
211   "Update all (necessary) format specifications."
212   ;; Make the indentation array.
213   ;; See whether all the stored info needs to be flushed.
214   (when force
215     (message "%s" "Force update format specs.")
216     (setq gnus-format-specs nil
217           gnus-format-specs-compiled nil)
218     (gnus-product-variable-touch 'gnus-format-specs
219                                  'gnus-format-specs-compiled))
220
221   ;; Go through all the formats and see whether they need updating.
222   (let (type val)
223     (save-excursion
224       (while (setq type (pop types))
225         ;; Jump to the proper buffer to find out the value of
226         ;; the variable, if possible.  (It may be buffer-local.)
227         (let* ((new-format
228                 (let ((buffer (intern (format "gnus-%s-buffer" type))))
229                   (when (and (boundp buffer)
230                              (setq val (symbol-value buffer))
231                              (gnus-buffer-exists-p val))
232                     (set-buffer val))
233                   (symbol-value
234                    (intern (format "gnus-%s-line-format" type))))))
235           (or (gnus-update-format-specification-1 type new-format nil)
236               ;; This is a new format.
237               (gnus-update-format-specification-1
238                type new-format
239                (gnus-search-or-regist-spec (gnus-format-specs
240                                             type new-format val entry elem)
241                  (setq val (if (stringp new-format)
242                                ;; This is a "real" format.
243                                (gnus-parse-format
244                                 new-format
245                                 (symbol-value
246                                  (intern (format "gnus-%s-line-format-alist"
247                                                  type)))
248                                 (not (string-match "mode$"
249                                                    (symbol-name type))))
250                              ;; This is a function call or something.
251                              new-format))))))))))
252
253 (defvar gnus-mouse-face-0 'highlight)
254 (defvar gnus-mouse-face-1 'highlight)
255 (defvar gnus-mouse-face-2 'highlight)
256 (defvar gnus-mouse-face-3 'highlight)
257 (defvar gnus-mouse-face-4 'highlight)
258
259 (defun gnus-mouse-face-function (form type)
260   `(gnus-put-text-property
261     (point) (progn ,@form (point))
262     gnus-mouse-face-prop
263     ,(if (equal type 0)
264          'gnus-mouse-face
265        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
266
267 (defvar gnus-face-0 'bold)
268 (defvar gnus-face-1 'italic)
269 (defvar gnus-face-2 'bold-italic)
270 (defvar gnus-face-3 'bold)
271 (defvar gnus-face-4 'bold)
272
273 (defun gnus-face-face-function (form type)
274   `(gnus-add-text-properties
275     (point) (progn ,@form (point))
276     '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
277
278 ;;; Avoid byte-compile warning.
279 (defun gnus-tilde-pad-form (el pad-width)
280   "Dummy function except for XEmacs-mule. It will be redefined
281 by `gnus-xmas-redefine'."
282   (let ((val (if (symbolp el) (eval el) el)))
283     (` (, val))))
284
285 (defun gnus-balloon-face-function (form type)
286   `(gnus-put-text-property
287     (point) (progn ,@form (point))
288     'balloon-help
289     ,(intern (format "gnus-balloon-face-%d" type))))
290
291 (defun gnus-tilde-max-form (el max-width)
292   "Return a form that limits EL to MAX-WIDTH."
293   (let ((max (abs max-width)))
294     (if (symbolp el)
295         `(if (> (length ,el) ,max)
296              ,(if (< max-width 0)
297                   `(substring ,el (- (length el) ,max))
298                 `(substring ,el 0 ,max))
299            ,el)
300       `(let ((val (eval ,el)))
301          (if (> (length val) ,max)
302              ,(if (< max-width 0)
303                   `(substring val (- (length val) ,max))
304                 `(substring val 0 ,max))
305            val)))))
306
307 (defun gnus-tilde-cut-form (el cut-width)
308   "Return a form that cuts CUT-WIDTH off of EL."
309   (let ((cut (abs cut-width)))
310     (if (symbolp el)
311         `(if (> (length ,el) ,cut)
312              ,(if (< cut-width 0)
313                   `(substring ,el 0 (- (length el) ,cut))
314                 `(substring ,el ,cut))
315            ,el)
316       `(let ((val (eval ,el)))
317          (if (> (length val) ,cut)
318              ,(if (< cut-width 0)
319                   `(substring val 0 (- (length val) ,cut))
320                 `(substring val ,cut))
321            val)))))
322
323 (defun gnus-tilde-ignore-form (el ignore-value)
324   "Return a form that is blank when EL is IGNORE-VALUE."
325   (if (symbolp el)
326       `(if (equal ,el ,ignore-value)
327            "" ,el)
328     `(let ((val (eval ,el)))
329        (if (equal val ,ignore-value)
330            "" val))))
331
332 (defun gnus-parse-format (format spec-alist &optional insert)
333   ;; This function parses the FORMAT string with the help of the
334   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
335   ;; string.  If the FORMAT string contains the specifiers %( and %)
336   ;; the text between them will have the mouse-face text property.
337   ;; If the FORMAT string contains the specifiers %[ and %], the text between
338   ;; them will have the balloon-help text property.
339   (if (string-match
340        "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'"
341        format)
342       (gnus-parse-complex-format format spec-alist)
343     ;; This is a simple format.
344     (gnus-parse-simple-format format spec-alist insert)))
345
346 (defun gnus-parse-complex-format (format spec-alist)
347   (save-excursion
348     (gnus-set-work-buffer)
349     (insert format)
350     (goto-char (point-min))
351     (while (re-search-forward "\"" nil t)
352       (replace-match "\\\"" nil t))
353     (goto-char (point-min))
354     (insert "(\"")
355     (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
356       (let ((number (if (match-beginning 1)
357                         (match-string 1) "0"))
358             (delim (aref (match-string 2) 0)))
359         (if (or (= delim ?\()
360                 (= delim ?\{)
361                 (= delim ?\«))
362             (replace-match (concat "\"("
363                                    (cond ((= delim ?\() "mouse")
364                                          ((= delim ?\{) "face")
365                                          (t "balloon"))
366                                    " " number " \""))
367           (replace-match "\")\""))))
368     (goto-char (point-max))
369     (insert "\")")
370     (goto-char (point-min))
371     (let ((form (read (current-buffer))))
372       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
373
374 (defun gnus-complex-form-to-spec (form spec-alist)
375   (delq nil
376         (mapcar
377          (lambda (sform)
378            (if (stringp sform)
379                (gnus-parse-simple-format sform spec-alist t)
380              (funcall (intern (format "gnus-%s-face-function" (car sform)))
381                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
382                       (nth 1 sform))))
383          form)))
384
385 (defun gnus-parse-simple-format (format spec-alist &optional insert)
386   ;; This function parses the FORMAT string with the help of the
387   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
388   ;; string.
389   (let ((xemacs-mule-p (and (featurep 'xemacs) (featurep 'mule)))
390         max-width
391         spec flist fstring elem result dontinsert user-defined
392         type value pad-width spec-beg cut-width ignore-value
393         tilde-form tilde elem-type)
394     (save-excursion
395       (gnus-set-work-buffer)
396       (insert format)
397       (goto-char (point-min))
398       (while (search-forward "%" nil t)
399         (setq user-defined nil
400               spec-beg nil
401               pad-width nil
402               max-width nil
403               cut-width nil
404               ignore-value nil
405               tilde-form nil)
406         (setq spec-beg (1- (point)))
407
408         ;; Parse this spec fully.
409         (while
410             (cond
411              ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
412               (setq pad-width (string-to-number (match-string 1)))
413               (when (match-beginning 2)
414                 (setq max-width (string-to-number (buffer-substring
415                                                    (1+ (match-beginning 2))
416                                                    (match-end 2)))))
417               (goto-char (match-end 0)))
418              ((looking-at "~")
419               (forward-char 1)
420               (setq tilde (read (current-buffer))
421                     type (car tilde)
422                     value (cadr tilde))
423               (cond
424                ((memq type '(pad pad-left))
425                 (setq pad-width value))
426                ((eq type 'pad-right)
427                 (setq pad-width (- value)))
428                ((memq type '(max-right max))
429                 (setq max-width value))
430                ((eq type 'max-left)
431                 (setq max-width (- value)))
432                ((memq type '(cut cut-left))
433                 (setq cut-width value))
434                ((eq type 'cut-right)
435                 (setq cut-width (- value)))
436                ((eq type 'ignore)
437                 (setq ignore-value
438                       (if (stringp value) value (format "%s" value))))
439                ((eq type 'form)
440                 (setq tilde-form value))
441                (t
442                 (error "Unknown tilde type: %s" tilde)))
443               t)
444              (t
445               nil)))
446         ;; User-defined spec -- find the spec name.
447         (when (eq (setq spec (char-after)) ?u)
448           (forward-char 1)
449           (setq user-defined (char-after)))
450         (forward-char 1)
451         (delete-region spec-beg (point))
452
453         ;; Now we have all the relevant data on this spec, so
454         ;; we start doing stuff.
455         (insert "%")
456         (if (eq spec ?%)
457             ;; "%%" just results in a "%".
458             (insert "%")
459           (cond
460            ;; Do tilde forms.
461            ((eq spec ?@)
462             (setq elem (list tilde-form ?s)))
463            ;; Treat user defined format specifiers specially.
464            (user-defined
465             (setq elem
466                   (list
467                    (list (intern (format "gnus-user-format-function-%c"
468                                          user-defined))
469                          'gnus-tmp-header)
470                    ?s)))
471            ;; Find the specification from `spec-alist'.
472            ((setq elem (cdr (assq spec spec-alist))))
473            (t
474             (setq elem '("*" ?s))))
475           (setq elem-type (cadr elem))
476           ;; Insert the new format elements.
477           (and pad-width (not xemacs-mule-p)
478                (insert (number-to-string pad-width)))
479           ;; Create the form to be evaled.
480           (if (or max-width cut-width ignore-value
481                   (and pad-width xemacs-mule-p))
482               (progn
483                 (insert ?s)
484                 (let ((el (car elem)))
485                   (cond ((= (cadr elem) ?c)
486                          (setq el (list 'char-to-string el)))
487                         ((= (cadr elem) ?d)
488                          (setq el (list 'int-to-string el))))
489                   (when ignore-value
490                     (setq el (gnus-tilde-ignore-form el ignore-value)))
491                   (when cut-width
492                     (setq el (gnus-tilde-cut-form el cut-width)))
493                   (when max-width
494                     (setq el (gnus-tilde-max-form el max-width)))
495                   (and pad-width xemacs-mule-p
496                        (setq el (gnus-tilde-pad-form el pad-width)))
497                   (push el flist)))
498             (insert elem-type)
499             (push (car elem) flist))))
500       (setq fstring (buffer-string)))
501
502     ;; Do some postprocessing to increase efficiency.
503     (setq
504      result
505      (cond
506       ;; Emptyness.
507       ((string= fstring "")
508        nil)
509       ;; Not a format string.
510       ((not (string-match "%" fstring))
511        (list fstring))
512       ;; A format string with just a single string spec.
513       ((string= fstring "%s")
514        (list (car flist)))
515       ;; A single character.
516       ((string= fstring "%c")
517        (list (car flist)))
518       ;; A single number.
519       ((string= fstring "%d")
520        (setq dontinsert)
521        (if insert
522            (list `(princ ,(car flist)))
523          (list `(int-to-string ,(car flist)))))
524       ;; Just lots of chars and strings.
525       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
526        (nreverse flist))
527       ;; A single string spec at the beginning of the spec.
528       ((string-match "\\`%[sc][^%]+\\'" fstring)
529        (list (car flist) (substring fstring 2)))
530       ;; A single string spec in the middle of the spec.
531       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
532        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
533       ;; A single string spec in the end of the spec.
534       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
535        (list (match-string 1 fstring) (car flist)))
536       ;; A more complex spec.
537       (t
538        (list (cons 'format (cons fstring (nreverse flist)))))))
539
540     (if insert
541         (when result
542           (if dontinsert
543               result
544             (cons 'insert result)))
545       (cond ((stringp result)
546              result)
547             ((consp result)
548              (cons 'concat result))
549             (t "")))))
550
551 (defun gnus-eval-format (format &optional alist props)
552   "Eval the format variable FORMAT, using ALIST.
553 If PROPS, insert the result."
554   (let ((form (gnus-parse-format format alist props)))
555     (if props
556         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
557       (eval form))))
558
559 (defun gnus-compile ()
560   "Byte-compile the user-defined format specs."
561   (interactive)
562   (require 'bytecomp)
563   (let ((entries gnus-format-specs)
564         (byte-compile-warnings '(unresolved callargs redefine))
565         entry type compiled-function)
566     (save-excursion
567       (gnus-message 7 "Compiling format specs...")
568
569       (while entries
570         (setq entry (pop entries)
571               type (car entry))
572         (if (memq type '(version gnus-version))
573             (setq gnus-format-specs (delq entry gnus-format-specs))
574           (let ((form (caddr entry)))
575             (when (and (listp form)
576                        ;; Under GNU Emacs, it's (byte-code ...)
577                        (not (eq 'byte-code (car form)))
578                        ;; Under XEmacs, it's (funcall #<compiled-function ...>)
579                        (not (and (eq 'funcall (car form))
580                                  (byte-code-function-p (cadr form)))))
581               (defalias 'gnus-tmp-func `(lambda () ,form))
582               (byte-compile 'gnus-tmp-func)
583               (setq compiled-function (gnus-byte-code 'gnus-tmp-func))
584               (set (intern (format "gnus-%s-line-format-spec" type))
585                    compiled-function)
586               (let ((elem (cdr (assq type gnus-format-specs-compiled))))
587                 (if elem
588                     (set-alist 'elem (cadr entry) compiled-function)
589                   (setq elem (list (cadr entry) compiled-function)))
590                 (set-alist 'gnus-format-specs-compiled type elem))))))
591
592       (push (cons 'version emacs-version) gnus-format-specs)
593       (gnus-message 7 "Compiling user specs...done"))))
594
595 (defun gnus-set-format (type &optional insertable)
596   (set (intern (format "gnus-%s-line-format-spec" type))
597        (gnus-parse-format
598         (symbol-value (intern (format "gnus-%s-line-format" type)))
599         (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
600         insertable)))
601
602 (gnus-ems-redefine)
603
604 (provide 'gnus-spec)
605
606 ;; Local Variables:
607 ;; coding: iso-8859-1
608 ;; End:
609
610 ;;; gnus-spec.el ends here