*** empty log message ***
[gnus] / lisp / gnus-spec.el
1 ;;; gnus-spec.el --- format spec functions for Gnus
2 ;; Copyright (C) 1996,97 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
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 (require 'gnus)
29
30 ;;; Internal variables.
31
32 (defvar gnus-summary-mark-positions nil)
33 (defvar gnus-group-mark-positions nil)
34 (defvar gnus-group-indentation "")
35
36 ;; Format specs.  The chunks below are the machine-generated forms
37 ;; that are to be evaled as the result of the default format strings.
38 ;; We write them in here to get them byte-compiled.  That way the
39 ;; default actions will be quite fast, while still retaining the full
40 ;; flexibility of the user-defined format specs.
41
42 ;; First we have lots of dummy defvars to let the compiler know these
43 ;; are really dynamic variables.
44
45 (defvar gnus-tmp-unread)
46 (defvar gnus-tmp-replied)
47 (defvar gnus-tmp-score-char)
48 (defvar gnus-tmp-indentation)
49 (defvar gnus-tmp-opening-bracket)
50 (defvar gnus-tmp-lines)
51 (defvar gnus-tmp-name)
52 (defvar gnus-tmp-closing-bracket)
53 (defvar gnus-tmp-subject-or-nil)
54 (defvar gnus-tmp-subject)
55 (defvar gnus-tmp-marked)
56 (defvar gnus-tmp-marked-mark)
57 (defvar gnus-tmp-subscribed)
58 (defvar gnus-tmp-process-marked)
59 (defvar gnus-tmp-number-of-unread)
60 (defvar gnus-tmp-group-name)
61 (defvar gnus-tmp-group)
62 (defvar gnus-tmp-article-number)
63 (defvar gnus-tmp-unread-and-unselected)
64 (defvar gnus-tmp-news-method)
65 (defvar gnus-tmp-news-server)
66 (defvar gnus-tmp-article-number)
67 (defvar gnus-mouse-face)
68 (defvar gnus-mouse-face-prop)
69
70 (defun gnus-summary-line-format-spec ()
71   (insert gnus-tmp-unread gnus-tmp-replied
72           gnus-tmp-score-char gnus-tmp-indentation)
73   (gnus-put-text-property
74    (point)
75    (progn
76      (insert
77       gnus-tmp-opening-bracket
78       (format "%4d: %-20s"
79               gnus-tmp-lines
80               (if (> (length gnus-tmp-name) 20)
81                   (substring gnus-tmp-name 0 20)
82                 gnus-tmp-name))
83       gnus-tmp-closing-bracket)
84      (point))
85    gnus-mouse-face-prop gnus-mouse-face)
86   (insert " " gnus-tmp-subject-or-nil "\n"))
87
88 (defvar gnus-summary-line-format-spec
89   (gnus-byte-code 'gnus-summary-line-format-spec))
90
91 (defun gnus-summary-dummy-line-format-spec ()
92   (insert "*  ")
93   (gnus-put-text-property
94    (point)
95    (progn
96      (insert ":                          :")
97      (point))
98    gnus-mouse-face-prop gnus-mouse-face)
99   (insert " " gnus-tmp-subject "\n"))
100
101 (defvar gnus-summary-dummy-line-format-spec
102   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
103
104 (defun gnus-group-line-format-spec ()
105   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
106           gnus-tmp-process-marked
107           gnus-group-indentation
108           (format "%5s: " gnus-tmp-number-of-unread))
109   (gnus-put-text-property
110    (point)
111    (progn
112      (insert gnus-tmp-group "\n")
113      (1- (point)))
114    gnus-mouse-face-prop gnus-mouse-face))
115 (defvar gnus-group-line-format-spec
116   (gnus-byte-code 'gnus-group-line-format-spec))
117
118 (defvar gnus-format-specs 
119   `((version . ,emacs-version)
120     (group "%M\%S\%p\%P\%5y: %(%g%)%l\n" ,gnus-group-line-format-spec)
121     (summary-dummy "*  %(:                          :%) %S\n"
122                    ,gnus-summary-dummy-line-format-spec)
123     (summary "%U\%R\%z\%I\%(%[%4L: %-20,20n%]%) %s\n" 
124              ,gnus-summary-line-format-spec))
125   "Alist of format specs.")
126
127 (defvar gnus-article-mode-line-format-spec nil)
128 (defvar gnus-summary-mode-line-format-spec nil)
129 (defvar gnus-group-mode-line-format-spec nil)
130
131 ;;; Phew.  All that gruft is over, fortunately.
132
133 ;;;###autoload
134 (defun gnus-update-format (var)
135   "Update the format specification near point."
136   (interactive
137    (list
138     (save-excursion
139       (eval-defun nil)
140       ;; Find the end of the current word.
141       (re-search-forward "[ \t\n]" nil t)
142       ;; Search backward.
143       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
144         (match-string 1)))))
145   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
146                               (match-string 1 var))))
147          (entry (assq type gnus-format-specs))
148          value spec)
149     (when entry
150       (setq gnus-format-specs (delq entry gnus-format-specs)))
151     (set
152      (intern (format "%s-spec" var))
153      (gnus-parse-format (setq value (symbol-value (intern var)))
154                         (symbol-value (intern (format "%s-alist" var)))
155                         (not (string-match "mode" var))))
156     (setq spec (symbol-value (intern (format "%s-spec" var))))
157     (push (list type value spec) gnus-format-specs)
158
159     (pop-to-buffer "*Gnus Format*")
160     (erase-buffer)
161     (lisp-interaction-mode)
162     (insert (pp-to-string spec))))
163
164 (defun gnus-update-format-specifications (&optional force &rest types)
165   "Update all (necessary) format specifications."
166   ;; Make the indentation array.
167   ;; See whether all the stored info needs to be flushed.
168   (when (or force
169             (not (equal emacs-version
170                         (cdr (assq 'version gnus-format-specs)))))
171     (setq gnus-format-specs nil))
172
173   ;; Go through all the formats and see whether they need updating.
174   (let (new-format entry type val)
175     (while (setq type (pop types))
176       ;; Jump to the proper buffer to find out the value of
177       ;; the variable, if possible.  (It may be buffer-local.)
178       (save-excursion
179         (let ((buffer (intern (format "gnus-%s-buffer" type)))
180               val)
181           (when (and (boundp buffer)
182                      (setq val (symbol-value buffer))
183                      (get-buffer val)
184                      (buffer-name (get-buffer val)))
185             (set-buffer (get-buffer val)))
186           (setq new-format (symbol-value
187                             (intern (format "gnus-%s-line-format" type)))))
188         (setq entry (cdr (assq type gnus-format-specs)))
189         (if (and (car entry)
190                  (equal (car entry) new-format))
191             ;; Use the old format.
192             (set (intern (format "gnus-%s-line-format-spec" type))
193                  (cadr entry))
194           ;; This is a new format.
195           (setq val
196                 (if (not (stringp new-format))
197                     ;; This is a function call or something.
198                     new-format
199                   ;; This is a "real" format.
200                   (gnus-parse-format
201                    new-format
202                    (symbol-value
203                     (intern (format "gnus-%s-line-format-alist"
204                                     (if (eq type 'article-mode)
205                                         'summary-mode type))))
206                    (not (string-match "mode$" (symbol-name type))))))
207           ;; Enter the new format spec into the list.
208           (if entry
209               (progn
210                 (setcar (cdr entry) val)
211                 (setcar entry new-format))
212             (push (list type new-format val) gnus-format-specs))
213           (set (intern (format "gnus-%s-line-format-spec" type)) val)))))
214
215   (unless (assq 'version gnus-format-specs)
216     (push (cons 'version emacs-version) gnus-format-specs)))
217
218 (defvar gnus-mouse-face-0 'highlight)
219 (defvar gnus-mouse-face-1 'highlight)
220 (defvar gnus-mouse-face-2 'highlight)
221 (defvar gnus-mouse-face-3 'highlight)
222 (defvar gnus-mouse-face-4 'highlight)
223
224 (defun gnus-mouse-face-function (form type)
225   `(gnus-put-text-property
226     (point) (progn ,@form (point))
227     gnus-mouse-face-prop
228     ,(if (equal type 0)
229          'gnus-mouse-face
230        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
231
232 (defvar gnus-face-0 'bold)
233 (defvar gnus-face-1 'italic)
234 (defvar gnus-face-2 'bold-italic)
235 (defvar gnus-face-3 'bold)
236 (defvar gnus-face-4 'bold)
237
238 (defun gnus-face-face-function (form type)
239   `(gnus-put-text-property
240     (point) (progn ,@form (point))
241     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
242
243 (defun gnus-tilde-max-form (el max-width)
244   "Return a form that limits EL to MAX-WIDTH."
245   (let ((max (abs max-width)))
246     (if (symbolp el)
247         `(if (> (length ,el) ,max)
248              ,(if (< max-width 0)
249                   `(substring ,el (- (length el) ,max))
250                 `(substring ,el 0 ,max))
251            ,el)
252       `(let ((val (eval ,el)))
253          (if (> (length val) ,max)
254              ,(if (< max-width 0)
255                   `(substring val (- (length val) ,max))
256                 `(substring val 0 ,max))
257            val)))))
258
259 (defun gnus-tilde-cut-form (el cut-width)
260   "Return a form that cuts CUT-WIDTH off of EL."
261   (let ((cut (abs cut-width)))
262     (if (symbolp el)
263         `(if (> (length ,el) ,cut)
264              ,(if (< cut-width 0)
265                   `(substring ,el 0 (- (length el) ,cut))
266                 `(substring ,el ,cut))
267            ,el)
268       `(let ((val (eval ,el)))
269          (if (> (length val) ,cut)
270              ,(if (< cut-width 0)
271                   `(substring val 0 (- (length val) ,cut))
272                 `(substring val ,cut))
273            val)))))
274
275 (defun gnus-tilde-ignore-form (el ignore-value)
276   "Return a form that is blank when EL is IGNORE-VALUE."
277   (if (symbolp el)
278       `(if (equal ,el ,ignore-value)
279            "" ,el)
280     `(let ((val (eval ,el)))
281        (if (equal val ,ignore-value)
282            "" val))))
283
284 (defun gnus-parse-format (format spec-alist &optional insert)
285   ;; This function parses the FORMAT string with the help of the
286   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
287   ;; string.  If the FORMAT string contains the specifiers %( and %)
288   ;; the text between them will have the mouse-face text property.
289   (if (string-match
290        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
291        format)
292       (gnus-parse-complex-format format spec-alist)
293     ;; This is a simple format.
294     (gnus-parse-simple-format format spec-alist insert)))
295
296 (defun gnus-parse-complex-format (format spec-alist)
297   (save-excursion
298     (gnus-set-work-buffer)
299     (insert format)
300     (goto-char (point-min))
301     (while (re-search-forward "\"" nil t)
302       (replace-match "\\\"" nil t))
303     (goto-char (point-min))
304     (insert "(\"")
305     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
306       (let ((number (if (match-beginning 1)
307                         (match-string 1) "0"))
308             (delim (aref (match-string 2) 0)))
309         (if (or (= delim ?\() (= delim ?\{))
310             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
311                                    " " number " \""))
312           (replace-match "\")\""))))
313     (goto-char (point-max))
314     (insert "\")")
315     (goto-char (point-min))
316     (let ((form (read (current-buffer))))
317       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
318
319 (defun gnus-complex-form-to-spec (form spec-alist)
320   (delq nil
321         (mapcar
322          (lambda (sform)
323            (if (stringp sform)
324                (gnus-parse-simple-format sform spec-alist t)
325              (funcall (intern (format "gnus-%s-face-function" (car sform)))
326                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
327                       (nth 1 sform))))
328          form)))
329
330 (defun gnus-parse-simple-format (format spec-alist &optional insert)
331   ;; This function parses the FORMAT string with the help of the
332   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
333   ;; string.
334   (let ((max-width 0)
335         spec flist fstring elem result dontinsert user-defined
336         type value pad-width spec-beg cut-width ignore-value
337         tilde-form tilde elem-type)
338     (save-excursion
339       (gnus-set-work-buffer)
340       (insert format)
341       (goto-char (point-min))
342       (while (re-search-forward "%" nil t)
343         (setq user-defined nil
344               spec-beg nil
345               pad-width nil
346               max-width nil
347               cut-width nil
348               ignore-value nil
349               tilde-form nil)
350         (setq spec-beg (1- (point)))
351
352         ;; Parse this spec fully.
353         (while
354             (cond 
355              ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
356               (setq pad-width (string-to-number (match-string 1)))
357               (when (match-beginning 2)
358                 (setq max-width (string-to-number (buffer-substring
359                                                    (1+ (match-beginning 2))
360                                                    (match-end 2)))))
361               (goto-char (match-end 0)))
362              ((looking-at "~")
363               (forward-char 1)
364               (setq tilde (read (current-buffer))
365                     type (car tilde)
366                     value (cadr tilde))
367               (cond
368                ((memq type '(pad pad-left))
369                 (setq pad-width value))
370                ((eq type 'pad-right)
371                 (setq pad-width (- value)))
372                ((memq type '(max-right max))
373                 (setq max-width value))
374                ((eq type 'max-left)
375                 (setq max-width (- value)))
376                ((memq type '(cut cut-left))
377                 (setq cut-width value))
378                ((eq type 'cut-right)
379                 (setq cut-width (- value)))
380                ((eq type 'ignore)
381                 (setq ignore-value
382                       (if (stringp value) value (format "%s" value))))
383                ((eq type 'form)
384                 (setq tilde-form value))
385                (t
386                 (error "Unknown tilde type: %s" tilde)))
387               t)
388              (t
389               nil)))
390         ;; User-defined spec -- find the spec name.
391         (when (= (setq spec (following-char)) ?u)
392           (forward-char 1)
393           (setq user-defined (following-char)))
394         (forward-char 1)
395         (delete-region spec-beg (point))
396
397         ;; Now we have all the relevant data on this spec, so
398         ;; we start doing stuff.
399         (insert "%")
400         (if (eq spec ?%)
401             ;; "%%" just results in a "%".
402             (insert "%")
403           (cond
404            ;; Do tilde forms.
405            ((eq spec ?@)
406             (setq elem (list tilde-form ?s)))
407            ;; Treat user defined format specifiers specially.
408            (user-defined
409             (setq elem
410                   (list
411                    (list (intern (format "gnus-user-format-function-%c"
412                                          user-defined))
413                          'gnus-tmp-header)
414                    ?s)))
415            ;; Find the specification from `spec-alist'.
416            ((setq elem (cdr (assq spec spec-alist))))
417            (t
418             (setq elem '("*" ?s))))
419           (setq elem-type (cadr elem))
420           ;; Insert the new format elements.
421           (when pad-width
422             (insert (number-to-string pad-width)))
423           ;; Create the form to be evaled.
424           (if (or max-width cut-width ignore-value)
425               (progn
426                 (insert ?s)
427                 (let ((el (car elem)))
428                   (cond ((= (cadr elem) ?c)
429                          (setq el (list 'char-to-string el)))
430                         ((= (cadr elem) ?d)
431                          (setq el (list 'int-to-string el))))
432                   (when ignore-value
433                     (setq el (gnus-tilde-ignore-form el ignore-value)))
434                   (when cut-width
435                     (setq el (gnus-tilde-cut-form el cut-width)))
436                   (when max-width
437                     (setq el (gnus-tilde-max-form el max-width)))
438                   (push el flist)))
439             (insert elem-type)
440             (push (car elem) flist))))
441       (setq fstring (buffer-string)))
442     
443     ;; Do some postprocessing to increase efficiency.
444     (setq
445      result
446      (cond
447       ;; Emptyness.
448       ((string= fstring "")
449        nil)
450       ;; Not a format string.
451       ((not (string-match "%" fstring))
452        (list fstring))
453       ;; A format string with just a single string spec.
454       ((string= fstring "%s")
455        (list (car flist)))
456       ;; A single character.
457       ((string= fstring "%c")
458        (list (car flist)))
459       ;; A single number.
460       ((string= fstring "%d")
461        (setq dontinsert)
462        (if insert
463            (list `(princ ,(car flist)))
464          (list `(int-to-string ,(car flist)))))
465       ;; Just lots of chars and strings.
466       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
467        (nreverse flist))
468       ;; A single string spec at the beginning of the spec.
469       ((string-match "\\`%[sc][^%]+\\'" fstring)
470        (list (car flist) (substring fstring 2)))
471       ;; A single string spec in the middle of the spec.
472       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
473        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
474       ;; A single string spec in the end of the spec.
475       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
476        (list (match-string 1 fstring) (car flist)))
477       ;; A more complex spec.
478       (t
479        (list (cons 'format (cons fstring (nreverse flist)))))))
480
481     (if insert
482         (when result
483           (if dontinsert
484               result
485             (cons 'insert result)))
486       (cond ((stringp result)
487              result)
488             ((consp result)
489              (cons 'concat result))
490             (t "")))))
491
492 (defun gnus-eval-format (format &optional alist props)
493   "Eval the format variable FORMAT, using ALIST.
494 If PROPS, insert the result."
495   (let ((form (gnus-parse-format format alist props)))
496     (if props
497         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
498       (eval form))))
499
500 (defun gnus-compile ()
501   "Byte-compile the user-defined format specs."
502   (interactive)
503   (let ((entries gnus-format-specs)
504         (byte-compile-warnings '(unresolved callargs redefine))
505         entry gnus-tmp-func)
506     (save-excursion
507       (gnus-message 7 "Compiling format specs...")
508
509       (while entries
510         (setq entry (pop entries))
511         (if (eq (car entry) 'version)
512             (setq gnus-format-specs (delq entry gnus-format-specs))
513           (when (and (listp (caddr entry))
514                      (not (eq 'byte-code (caaddr entry))))
515             (fset 'gnus-tmp-func `(lambda () ,(caddr entry)))
516             (byte-compile 'gnus-tmp-func)
517             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
518
519       (push (cons 'version emacs-version) gnus-format-specs)
520       ;; Mark the .newsrc.eld file as "dirty".
521       (gnus-dribble-enter " ")
522       (gnus-message 7 "Compiling user specs...done"))))
523
524 (provide 'gnus-spec)
525
526 ;;; gnus-spec.el ends here