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