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