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