(gnus-compile): Modify the actual format specs as well;
[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   `((version . ,emacs-version)
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.
133 Each element should be the form (TYPE . BYTECODE).")
134
135 (defvar gnus-article-mode-line-format-spec nil)
136 (defvar gnus-summary-mode-line-format-spec nil)
137 (defvar gnus-group-mode-line-format-spec nil)
138
139 ;;; Phew.  All that gruft is over, fortunately.
140
141 ;;;###autoload
142 (defun gnus-update-format (var)
143   "Update the format specification near point."
144   (interactive
145    (list
146     (save-excursion
147       (eval-defun nil)
148       ;; Find the end of the current word.
149       (re-search-forward "[ \t\n]" nil t)
150       ;; Search backward.
151       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
152         (match-string 1)))))
153   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
154                               (match-string 1 var))))
155          (entry (assq type gnus-format-specs))
156          value spec)
157     (when entry
158       (setq gnus-format-specs (delq entry gnus-format-specs)))
159     (set
160      (intern (format "%s-spec" var))
161      (gnus-parse-format (setq value (symbol-value (intern var)))
162                         (symbol-value (intern (format "%s-alist" var)))
163                         (not (string-match "mode" var))))
164     (setq spec (symbol-value (intern (format "%s-spec" var))))
165     (push (list type value spec) gnus-format-specs)
166
167     (pop-to-buffer "*Gnus Format*")
168     (erase-buffer)
169     (lisp-interaction-mode)
170     (insert (pp-to-string spec))))
171
172 (defun gnus-update-format-specification-1 (type val &optional new)
173   (if gnus-compile-user-specs
174       (let ((bytecode (if new
175                           nil
176                         (cdr (assq type gnus-format-specs-compiled)))))
177         (unless bytecode
178           (fset 'gnus-tmp-func `(lambda () ,val))
179           (require 'bytecomp)
180           (let (byte-compile-warnings)
181             (byte-compile 'gnus-tmp-func))
182           (setq bytecode (gnus-byte-code 'gnus-tmp-func))
183           (when (get-buffer "*Compile-Log*")
184             (bury-buffer "*Compile-Log*"))
185           (when (get-buffer "*Compile-Log-Show*")
186             (bury-buffer "*Compile-Log-Show*")))
187         (set (intern (format "gnus-%s-line-format-spec" type)) bytecode)
188         (set-alist 'gnus-format-specs-compiled type bytecode))
189     (set (intern (format "gnus-%s-line-format-spec" type)) val)))
190
191 (defun gnus-update-format-specifications (&optional force &rest types)
192   "Update all (necessary) format specifications."
193   ;; Make the indentation array.
194   ;; See whether all the stored info needs to be flushed.
195   (when (or force
196             (not (equal emacs-version
197                         (cdr (assq 'version gnus-format-specs))))
198             (not (equal gnus-version gnus-newsrc-file-version)))
199     (message "%s" "Force update format specs.")
200     (setq gnus-format-specs nil
201           gnus-newsrc-file-version gnus-version))
202
203   ;; Go through all the formats and see whether they need updating.
204   (let (new-format entry type val)
205     (while (setq type (pop types))
206       ;; Jump to the proper buffer to find out the value of
207       ;; the variable, if possible.  (It may be buffer-local.)
208       (save-excursion
209         (let ((buffer (intern (format "gnus-%s-buffer" type)))
210               val)
211           (when (and (boundp buffer)
212                      (setq val (symbol-value buffer))
213                      (gnus-buffer-exists-p val))
214             (set-buffer val))
215           (setq new-format (symbol-value
216                             (intern (format "gnus-%s-line-format" type)))))
217         (setq entry (cdr (assq type gnus-format-specs)))
218         (if (and (car entry)
219                  (equal (car entry) new-format))
220             ;; Use the old format.
221             (gnus-update-format-specification-1 type (cadr entry))
222           ;; This is a new format.
223           (setq val
224                 (if (not (stringp new-format))
225                     ;; This is a function call or something.
226                     new-format
227                   ;; This is a "real" format.
228                   (gnus-parse-format
229                    new-format
230                    (symbol-value
231                     (intern (format "gnus-%s-line-format-alist" type)))
232                    (not (string-match "mode$" (symbol-name type))))))
233           ;; Enter the new format spec into the list.
234           (if entry
235               (progn
236                 (setcar (cdr entry) val)
237                 (setcar entry new-format))
238             (push (list type new-format val) gnus-format-specs))
239           (gnus-update-format-specification-1 type val 'new)))))
240
241   (unless (assq 'version gnus-format-specs)
242     (push (cons 'version emacs-version) gnus-format-specs)))
243
244 (defvar gnus-mouse-face-0 'highlight)
245 (defvar gnus-mouse-face-1 'highlight)
246 (defvar gnus-mouse-face-2 'highlight)
247 (defvar gnus-mouse-face-3 'highlight)
248 (defvar gnus-mouse-face-4 'highlight)
249
250 (defun gnus-mouse-face-function (form type)
251   `(gnus-put-text-property
252     (point) (progn ,@form (point))
253     gnus-mouse-face-prop
254     ,(if (equal type 0)
255          'gnus-mouse-face
256        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
257
258 (defvar gnus-face-0 'bold)
259 (defvar gnus-face-1 'italic)
260 (defvar gnus-face-2 'bold-italic)
261 (defvar gnus-face-3 'bold)
262 (defvar gnus-face-4 'bold)
263
264 (defun gnus-face-face-function (form type)
265   `(gnus-add-text-properties
266     (point) (progn ,@form (point))
267     '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
268
269 ;;; Avoid byte-compile warning.
270 (defun gnus-tilde-pad-form (el pad-width)
271   "Dummy function except for XEmacs-mule. It will be redefined
272 by `gnus-xmas-redefine'."
273   (let ((val (if (symbolp el) (eval el) el)))
274     (` (, val))))
275
276 (defun gnus-balloon-face-function (form type)
277   `(gnus-put-text-property
278     (point) (progn ,@form (point))
279     'balloon-help
280     ,(intern (format "gnus-balloon-face-%d" type))))
281
282 (defun gnus-tilde-max-form (el max-width)
283   "Return a form that limits EL to MAX-WIDTH."
284   (let ((max (abs max-width)))
285     (if (symbolp el)
286         `(if (> (length ,el) ,max)
287              ,(if (< max-width 0)
288                   `(substring ,el (- (length el) ,max))
289                 `(substring ,el 0 ,max))
290            ,el)
291       `(let ((val (eval ,el)))
292          (if (> (length val) ,max)
293              ,(if (< max-width 0)
294                   `(substring val (- (length val) ,max))
295                 `(substring val 0 ,max))
296            val)))))
297
298 (defun gnus-tilde-cut-form (el cut-width)
299   "Return a form that cuts CUT-WIDTH off of EL."
300   (let ((cut (abs cut-width)))
301     (if (symbolp el)
302         `(if (> (length ,el) ,cut)
303              ,(if (< cut-width 0)
304                   `(substring ,el 0 (- (length el) ,cut))
305                 `(substring ,el ,cut))
306            ,el)
307       `(let ((val (eval ,el)))
308          (if (> (length val) ,cut)
309              ,(if (< cut-width 0)
310                   `(substring val 0 (- (length val) ,cut))
311                 `(substring val ,cut))
312            val)))))
313
314 (defun gnus-tilde-ignore-form (el ignore-value)
315   "Return a form that is blank when EL is IGNORE-VALUE."
316   (if (symbolp el)
317       `(if (equal ,el ,ignore-value)
318            "" ,el)
319     `(let ((val (eval ,el)))
320        (if (equal val ,ignore-value)
321            "" val))))
322
323 (defun gnus-parse-format (format spec-alist &optional insert)
324   ;; This function parses the FORMAT string with the help of the
325   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
326   ;; string.  If the FORMAT string contains the specifiers %( and %)
327   ;; the text between them will have the mouse-face text property.
328   ;; If the FORMAT string contains the specifiers %[ and %], the text between
329   ;; them will have the balloon-help text property.
330   (if (string-match
331        "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'"
332        format)
333       (gnus-parse-complex-format format spec-alist)
334     ;; This is a simple format.
335     (gnus-parse-simple-format format spec-alist insert)))
336
337 (defun gnus-parse-complex-format (format spec-alist)
338   (save-excursion
339     (gnus-set-work-buffer)
340     (insert format)
341     (goto-char (point-min))
342     (while (re-search-forward "\"" nil t)
343       (replace-match "\\\"" nil t))
344     (goto-char (point-min))
345     (insert "(\"")
346     (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
347       (let ((number (if (match-beginning 1)
348                         (match-string 1) "0"))
349             (delim (aref (match-string 2) 0)))
350         (if (or (= delim ?\()
351                 (= delim ?\{)
352                 (= delim ?\«))
353             (replace-match (concat "\"("
354                                    (cond ((= delim ?\() "mouse")
355                                          ((= delim ?\{) "face")
356                                          (t "balloon"))
357                                    " " number " \""))
358           (replace-match "\")\""))))
359     (goto-char (point-max))
360     (insert "\")")
361     (goto-char (point-min))
362     (let ((form (read (current-buffer))))
363       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
364
365 (defun gnus-complex-form-to-spec (form spec-alist)
366   (delq nil
367         (mapcar
368          (lambda (sform)
369            (if (stringp sform)
370                (gnus-parse-simple-format sform spec-alist t)
371              (funcall (intern (format "gnus-%s-face-function" (car sform)))
372                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
373                       (nth 1 sform))))
374          form)))
375
376 (defun gnus-parse-simple-format (format spec-alist &optional insert)
377   ;; This function parses the FORMAT string with the help of the
378   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
379   ;; string.
380   (let ((xemacs-mule-p (and gnus-xemacs (featurep 'mule)))
381         max-width
382         spec flist fstring elem result dontinsert user-defined
383         type value pad-width spec-beg cut-width ignore-value
384         tilde-form tilde elem-type)
385     (save-excursion
386       (gnus-set-work-buffer)
387       (insert format)
388       (goto-char (point-min))
389       (while (search-forward "%" nil t)
390         (setq user-defined nil
391               spec-beg nil
392               pad-width nil
393               max-width nil
394               cut-width nil
395               ignore-value nil
396               tilde-form nil)
397         (setq spec-beg (1- (point)))
398
399         ;; Parse this spec fully.
400         (while
401             (cond
402              ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
403               (setq pad-width (string-to-number (match-string 1)))
404               (when (match-beginning 2)
405                 (setq max-width (string-to-number (buffer-substring
406                                                    (1+ (match-beginning 2))
407                                                    (match-end 2)))))
408               (goto-char (match-end 0)))
409              ((looking-at "~")
410               (forward-char 1)
411               (setq tilde (read (current-buffer))
412                     type (car tilde)
413                     value (cadr tilde))
414               (cond
415                ((memq type '(pad pad-left))
416                 (setq pad-width value))
417                ((eq type 'pad-right)
418                 (setq pad-width (- value)))
419                ((memq type '(max-right max))
420                 (setq max-width value))
421                ((eq type 'max-left)
422                 (setq max-width (- value)))
423                ((memq type '(cut cut-left))
424                 (setq cut-width value))
425                ((eq type 'cut-right)
426                 (setq cut-width (- value)))
427                ((eq type 'ignore)
428                 (setq ignore-value
429                       (if (stringp value) value (format "%s" value))))
430                ((eq type 'form)
431                 (setq tilde-form value))
432                (t
433                 (error "Unknown tilde type: %s" tilde)))
434               t)
435              (t
436               nil)))
437         ;; User-defined spec -- find the spec name.
438         (when (eq (setq spec (char-after)) ?u)
439           (forward-char 1)
440           (setq user-defined (char-after)))
441         (forward-char 1)
442         (delete-region spec-beg (point))
443
444         ;; Now we have all the relevant data on this spec, so
445         ;; we start doing stuff.
446         (insert "%")
447         (if (eq spec ?%)
448             ;; "%%" just results in a "%".
449             (insert "%")
450           (cond
451            ;; Do tilde forms.
452            ((eq spec ?@)
453             (setq elem (list tilde-form ?s)))
454            ;; Treat user defined format specifiers specially.
455            (user-defined
456             (setq elem
457                   (list
458                    (list (intern (format "gnus-user-format-function-%c"
459                                          user-defined))
460                          'gnus-tmp-header)
461                    ?s)))
462            ;; Find the specification from `spec-alist'.
463            ((setq elem (cdr (assq spec spec-alist))))
464            (t
465             (setq elem '("*" ?s))))
466           (setq elem-type (cadr elem))
467           ;; Insert the new format elements.
468           (and pad-width (not xemacs-mule-p)
469                (insert (number-to-string pad-width)))
470           ;; Create the form to be evaled.
471           (if (or max-width cut-width ignore-value
472                   (and pad-width xemacs-mule-p))
473               (progn
474                 (insert ?s)
475                 (let ((el (car elem)))
476                   (cond ((= (cadr elem) ?c)
477                          (setq el (list 'char-to-string el)))
478                         ((= (cadr elem) ?d)
479                          (setq el (list 'int-to-string el))))
480                   (when ignore-value
481                     (setq el (gnus-tilde-ignore-form el ignore-value)))
482                   (when cut-width
483                     (setq el (gnus-tilde-cut-form el cut-width)))
484                   (when max-width
485                     (setq el (gnus-tilde-max-form el max-width)))
486                   (and pad-width xemacs-mule-p
487                        (setq el (gnus-tilde-pad-form el pad-width)))
488                   (push el flist)))
489             (insert elem-type)
490             (push (car elem) flist))))
491       (setq fstring (buffer-string)))
492
493     ;; Do some postprocessing to increase efficiency.
494     (setq
495      result
496      (cond
497       ;; Emptyness.
498       ((string= fstring "")
499        nil)
500       ;; Not a format string.
501       ((not (string-match "%" fstring))
502        (list fstring))
503       ;; A format string with just a single string spec.
504       ((string= fstring "%s")
505        (list (car flist)))
506       ;; A single character.
507       ((string= fstring "%c")
508        (list (car flist)))
509       ;; A single number.
510       ((string= fstring "%d")
511        (setq dontinsert)
512        (if insert
513            (list `(princ ,(car flist)))
514          (list `(int-to-string ,(car flist)))))
515       ;; Just lots of chars and strings.
516       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
517        (nreverse flist))
518       ;; A single string spec at the beginning of the spec.
519       ((string-match "\\`%[sc][^%]+\\'" fstring)
520        (list (car flist) (substring fstring 2)))
521       ;; A single string spec in the middle of the spec.
522       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
523        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
524       ;; A single string spec in the end of the spec.
525       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
526        (list (match-string 1 fstring) (car flist)))
527       ;; A more complex spec.
528       (t
529        (list (cons 'format (cons fstring (nreverse flist)))))))
530
531     (if insert
532         (when result
533           (if dontinsert
534               result
535             (cons 'insert result)))
536       (cond ((stringp result)
537              result)
538             ((consp result)
539              (cons 'concat result))
540             (t "")))))
541
542 (defun gnus-eval-format (format &optional alist props)
543   "Eval the format variable FORMAT, using ALIST.
544 If PROPS, insert the result."
545   (let ((form (gnus-parse-format format alist props)))
546     (if props
547         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
548       (eval form))))
549
550 (defun gnus-compile ()
551   "Byte-compile the user-defined format specs."
552   (interactive)
553   (require 'bytecomp)
554   (let ((entries gnus-format-specs)
555         (byte-compile-warnings '(unresolved callargs redefine))
556         entry type bytecode)
557     (save-excursion
558       (gnus-message 7 "Compiling format specs...")
559
560       (while entries
561         (setq entry (pop entries)
562               type (car entry))
563         (if (memq type '(version gnus-version))
564             (setq gnus-format-specs (delq entry gnus-format-specs))
565           (let ((form (caddr entry)))
566             (when (and (listp form)
567                        ;; Under GNU Emacs, it's (byte-code ...)
568                        (not (eq 'byte-code (car form)))
569                        ;; Under XEmacs, it's (funcall #<compiled-function ...>)
570                        (not (and (eq 'funcall (car form))
571                                  (byte-code-function-p (cadr form)))))
572               (fset 'gnus-tmp-func `(lambda () ,form))
573               (byte-compile 'gnus-tmp-func)
574               (setq bytecode (gnus-byte-code 'gnus-tmp-func))
575               (set (intern (format "gnus-%s-line-format-spec" type)) bytecode)
576               (set-alist 'gnus-format-specs-compiled type bytecode)))))
577
578       (push (cons 'version emacs-version) gnus-format-specs)
579       (gnus-message 7 "Compiling user specs...done"))))
580
581 (defun gnus-set-format (type &optional insertable)
582   (set (intern (format "gnus-%s-line-format-spec" type))
583        (gnus-parse-format
584         (symbol-value (intern (format "gnus-%s-line-format" type)))
585         (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
586         insertable)))
587
588 (gnus-ems-redefine)
589
590 (provide 'gnus-spec)
591
592 ;; Local Variables:
593 ;; coding: iso-8859-1
594 ;; End:
595
596 ;;; gnus-spec.el ends here