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