*** empty log message ***
[gnus] / lisp / gnus-spec.el
1 ;;; gnus-spec.el --- format spec functions for Gnus
2 ;; Copyright (C) 1996 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-load)
29 (require 'gnus)
30
31 ;;; Internal variables.
32
33 (defvar gnus-summary-mark-positions nil)
34 (defvar gnus-group-mark-positions nil)
35 (defvar gnus-group-indentation "")
36
37 ;; Format specs.  The chunks below are the machine-generated forms
38 ;; that are to be evaled as the result of the default format strings.
39 ;; We write them in here to get them byte-compiled.  That way the
40 ;; default actions will be quite fast, while still retaining the full
41 ;; flexibility of the user-defined format specs.
42
43 ;; First we have lots of dummy defvars to let the compiler know these
44 ;; are really dynamic variables.
45
46 (defvar gnus-tmp-unread)
47 (defvar gnus-tmp-replied)
48 (defvar gnus-tmp-score-char)
49 (defvar gnus-tmp-indentation)
50 (defvar gnus-tmp-opening-bracket)
51 (defvar gnus-tmp-lines)
52 (defvar gnus-tmp-name)
53 (defvar gnus-tmp-closing-bracket)
54 (defvar gnus-tmp-subject-or-nil)
55 (defvar gnus-tmp-subject)
56 (defvar gnus-tmp-marked)
57 (defvar gnus-tmp-marked-mark)
58 (defvar gnus-tmp-subscribed)
59 (defvar gnus-tmp-process-marked)
60 (defvar gnus-tmp-number-of-unread)
61 (defvar gnus-tmp-group-name)
62 (defvar gnus-tmp-group)
63 (defvar gnus-tmp-article-number)
64 (defvar gnus-tmp-unread-and-unselected)
65 (defvar gnus-tmp-news-method)
66 (defvar gnus-tmp-news-server)
67 (defvar gnus-tmp-article-number)
68 (defvar gnus-mouse-face)
69 (defvar gnus-mouse-face-prop)
70
71 (defun gnus-summary-line-format-spec ()
72   (insert gnus-tmp-unread gnus-tmp-replied
73           gnus-tmp-score-char gnus-tmp-indentation)
74   (gnus-put-text-property
75    (point)
76    (progn
77      (insert
78       gnus-tmp-opening-bracket
79       (format "%4d: %-20s"
80               gnus-tmp-lines
81               (if (> (length gnus-tmp-name) 20)
82                   (substring gnus-tmp-name 0 20)
83                 gnus-tmp-name))
84       gnus-tmp-closing-bracket)
85      (point))
86    gnus-mouse-face-prop gnus-mouse-face)
87   (insert " " gnus-tmp-subject-or-nil "\n"))
88
89 (defvar gnus-summary-line-format-spec
90   (gnus-byte-code 'gnus-summary-line-format-spec))
91
92 (defun gnus-summary-dummy-line-format-spec ()
93   (insert "*  ")
94   (gnus-put-text-property
95    (point)
96    (progn
97      (insert ":                          :")
98      (point))
99    gnus-mouse-face-prop gnus-mouse-face)
100   (insert " " gnus-tmp-subject "\n"))
101
102 (defvar gnus-summary-dummy-line-format-spec
103   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
104
105 (defun gnus-group-line-format-spec ()
106   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
107           gnus-tmp-process-marked
108           gnus-group-indentation
109           (format "%5s: " gnus-tmp-number-of-unread))
110   (gnus-put-text-property
111    (point)
112    (progn
113      (insert gnus-tmp-group "\n")
114      (1- (point)))
115    gnus-mouse-face-prop gnus-mouse-face))
116 (defvar gnus-group-line-format-spec
117   (gnus-byte-code 'gnus-group-line-format-spec))
118
119 (defvar gnus-format-specs 
120   `((version . ,emacs-version)
121     (group "%M\%S\%p\%P\%5y: %(%g%)%l\n" ,gnus-group-line-format-spec)
122     (summary-dummy "*  %(:                          :%) %S\n"
123                    ,gnus-summary-dummy-line-format-spec)
124     (summary "%U\%R\%z\%I\%(%[%4L: %-20,20n%]%) %s\n" 
125              ,gnus-summary-line-format-spec))
126   "Alist of format specs.")
127
128 (defvar gnus-article-mode-line-format-spec nil)
129 (defvar gnus-summary-mode-line-format-spec nil)
130 (defvar gnus-group-mode-line-format-spec nil)
131
132 ;;; Phew.  All that gruft is over, fortunately.
133
134 ;;;###autoload
135 (defun gnus-update-format (var)
136   "Update the format specification near point."
137   (interactive
138    (list
139     (save-excursion
140       (eval-defun nil)
141       ;; Find the end of the current word.
142       (re-search-forward "[ \t\n]" nil t)
143       ;; Search backward.
144       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
145         (match-string 1)))))
146   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
147                               (match-string 1 var))))
148          (entry (assq type gnus-format-specs))
149          value spec)
150     (when entry
151       (setq gnus-format-specs (delq entry gnus-format-specs)))
152     (set
153      (intern (format "%s-spec" var))
154      (gnus-parse-format (setq value (symbol-value (intern var)))
155                         (symbol-value (intern (format "%s-alist" var)))
156                         (not (string-match "mode" var))))
157     (setq spec (symbol-value (intern (format "%s-spec" var))))
158     (push (list type value spec) gnus-format-specs)
159
160     (pop-to-buffer "*Gnus Format*")
161     (erase-buffer)
162     (lisp-interaction-mode)
163     (insert (pp-to-string spec))))
164
165 (defun gnus-update-format-specifications (&optional force &rest types)
166   "Update all (necessary) format specifications."
167   ;; Make the indentation array.
168   ;; See whether all the stored info needs to be flushed.
169   (when (or force
170             (not (equal emacs-version
171                         (cdr (assq 'version gnus-format-specs)))))
172     (setq gnus-format-specs nil))
173
174   ;; Go through all the formats and see whether they need updating.
175   (let (new-format entry type val)
176     (while (setq type (pop types))
177       ;; Jump to the proper buffer to find out the value of
178       ;; the variable, if possible.  (It may be buffer-local.)
179       (save-excursion
180         (let ((buffer (intern (format "gnus-%s-buffer" type)))
181               val)
182           (when (and (boundp buffer)
183                      (setq val (symbol-value buffer))
184                      (get-buffer val)
185                      (buffer-name (get-buffer val)))
186             (set-buffer (get-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"
205                                   (if (eq type 'article-mode)
206                                       'summary-mode type))))
207                  (not (string-match "mode$" (symbol-name type))))))
208         ;; Enter the new format spec into the list.
209         (if entry
210             (progn
211               (setcar (cdr entry) val)
212               (setcar entry new-format))
213           (push (list type new-format val) gnus-format-specs))
214         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
215
216   (unless (assq 'version gnus-format-specs)
217     (push (cons 'version emacs-version) gnus-format-specs)))
218
219 (defvar gnus-mouse-face-0 'highlight)
220 (defvar gnus-mouse-face-1 'highlight)
221 (defvar gnus-mouse-face-2 'highlight)
222 (defvar gnus-mouse-face-3 'highlight)
223 (defvar gnus-mouse-face-4 'highlight)
224
225 (defun gnus-mouse-face-function (form type)
226   `(gnus-put-text-property
227     (point) (progn ,@form (point))
228     gnus-mouse-face-prop
229     ,(if (equal type 0)
230          'gnus-mouse-face
231        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
232
233 (defvar gnus-face-0 'bold)
234 (defvar gnus-face-1 'italic)
235 (defvar gnus-face-2 'bold-italic)
236 (defvar gnus-face-3 'bold)
237 (defvar gnus-face-4 'bold)
238
239 (defun gnus-face-face-function (form type)
240   `(gnus-put-text-property
241     (point) (progn ,@form (point))
242     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
243
244 (defun gnus-tilde-max-form (el max-width)
245   "Return a form that limits EL to MAX-WIDTH."
246   (let ((max (abs max-width)))
247     (if (symbolp el)
248         `(if (> (length ,el) ,max)
249              ,(if (< max-width 0)
250                   `(substring ,el (- (length el) ,max))
251                 `(substring ,el 0 ,max))
252            ,el)
253       `(let ((val (eval ,el)))
254          (if (> (length val) ,max)
255              ,(if (< max-width 0)
256                   `(substring val (- (length val) ,max))
257                 `(substring val 0 ,max))
258            val)))))
259
260 (defun gnus-tilde-cut-form (el cut-width)
261   "Return a form that cuts CUT-WIDTH off of EL."
262   (let ((cut (abs cut-width)))
263     (if (symbolp el)
264         `(if (> (length ,el) ,cut)
265              ,(if (< cut-width 0)
266                   `(substring ,el 0 (- (length el) ,cut))
267                 `(substring ,el ,cut))
268            ,el)
269       `(let ((val (eval ,el)))
270          (if (> (length val) ,cut)
271              ,(if (< cut-width 0)
272                   `(substring val 0 (- (length val) ,cut))
273                 `(substring val ,cut))
274            val)))))
275
276 (defun gnus-tilde-ignore-form (el ignore-value)
277   "Return a form that is blank when EL is IGNORE-VALUE."
278   (if (symbolp el)
279       `(if (equal ,el ,ignore-value)
280            "" ,el)
281     `(let ((val (eval ,el)))
282        (if (equal val ,ignore-value)
283            "" val))))
284
285 (defun gnus-parse-format (format spec-alist &optional insert)
286   ;; This function parses the FORMAT string with the help of the
287   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
288   ;; string.  If the FORMAT string contains the specifiers %( and %)
289   ;; the text between them will have the mouse-face text property.
290   (if (string-match
291        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
292        format)
293       (gnus-parse-complex-format format spec-alist)
294     ;; This is a simple format.
295     (gnus-parse-simple-format format spec-alist insert)))
296
297 (defun gnus-parse-complex-format (format spec-alist)
298   (save-excursion
299     (gnus-set-work-buffer)
300     (insert format)
301     (goto-char (point-min))
302     (while (re-search-forward "\"" nil t)
303       (replace-match "\\\"" nil t))
304     (goto-char (point-min))
305     (insert "(\"")
306     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
307       (let ((number (if (match-beginning 1)
308                         (match-string 1) "0"))
309             (delim (aref (match-string 2) 0)))
310         (if (or (= delim ?\() (= delim ?\{))
311             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
312                                    " " number " \""))
313           (replace-match "\")\""))))
314     (goto-char (point-max))
315     (insert "\")")
316     (goto-char (point-min))
317     (let ((form (read (current-buffer))))
318       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
319
320 (defun gnus-complex-form-to-spec (form spec-alist)
321   (delq nil
322         (mapcar
323          (lambda (sform)
324            (if (stringp sform)
325                (gnus-parse-simple-format sform spec-alist t)
326              (funcall (intern (format "gnus-%s-face-function" (car sform)))
327                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
328                       (nth 1 sform))))
329          form)))
330
331 (defun gnus-parse-simple-format (format spec-alist &optional insert)
332   ;; This function parses the FORMAT string with the help of the
333   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
334   ;; string.
335   (let ((max-width 0)
336         spec flist fstring elem result dontinsert user-defined
337         type value pad-width spec-beg cut-width ignore-value
338         tilde-form tilde elem-type)
339     (save-excursion
340       (gnus-set-work-buffer)
341       (insert format)
342       (goto-char (point-min))
343       (while (re-search-forward "%" nil t)
344         (setq user-defined nil
345               spec-beg nil
346               pad-width nil
347               max-width nil
348               cut-width nil
349               ignore-value nil
350               tilde-form nil)
351         (setq spec-beg (1- (point)))
352
353         ;; Parse this spec fully.
354         (while
355             (cond 
356              ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
357               (setq pad-width (string-to-number (match-string 1)))
358               (when (match-beginning 2)
359                 (setq max-width (string-to-number (buffer-substring
360                                                    (1+ (match-beginning 2))
361                                                    (match-end 2)))))
362               (goto-char (match-end 0)))
363              ((looking-at "~")
364               (forward-char 1)
365               (setq tilde (read (current-buffer))
366                     type (car tilde)
367                     value (cadr tilde))
368               (cond
369                ((memq type '(pad pad-left))
370                 (setq pad-width value))
371                ((eq type 'pad-right)
372                 (setq pad-width (- value)))
373                ((memq type '(max-right max))
374                 (setq max-width value))
375                ((eq type 'max-left)
376                 (setq max-width (- value)))
377                ((memq type '(cut cut-left))
378                 (setq cut-width value))
379                ((eq type 'cut-right)
380                 (setq cut-width (- value)))
381                ((eq type 'ignore)
382                 (setq ignore-value
383                       (if (stringp value) value (format "%s" value))))
384                ((eq type 'form)
385                 (setq tilde-form value))
386                (t
387                 (error "Unknown tilde type: %s" tilde)))
388               t)
389              (t
390               nil)))
391         ;; User-defined spec -- find the spec name.
392         (when (= (setq spec (following-char)) ?u)
393           (forward-char 1)
394           (setq user-defined (following-char)))
395         (forward-char 1)
396         (delete-region spec-beg (point))
397
398         ;; Now we have all the relevant data on this spec, so
399         ;; we start doing stuff.
400         (insert "%")
401         (if (eq spec ?%)
402             ;; "%%" just results in a "%".
403             (insert "%")
404           (cond
405            ;; Do tilde forms.
406            ((eq spec ?@)
407             (setq elem (list tilde-form ?s)))
408            ;; Treat user defined format specifiers specially.
409            (user-defined
410             (setq elem
411                   (list
412                    (list (intern (format "gnus-user-format-function-%c"
413                                          user-defined))
414                          'gnus-tmp-header) ?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