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