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