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