Synch to No Gnus 200411282151.
[elisp/gnus.git-] / lisp / gnus-spec.el
1 ;;; gnus-spec.el --- format spec functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;      Katsumi Yamaoka <yamaoka@jpl.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'alist)
33 (require 'gnus)
34
35 (defcustom gnus-use-correct-string-widths t
36   "*If non-nil, use correct functions for dealing with wide characters."
37   :version "21.4"
38   :group 'gnus-format
39   :type 'boolean)
40
41 (defcustom gnus-make-format-preserve-properties (featurep 'xemacs)
42   "*If non-nil, use a replacement `format' function which preserves
43 text properties. This is only needed on XEmacs, as FSF Emacs does this anyway."
44   :version "21.4"
45   :group 'gnus-format
46   :type 'boolean)
47
48 ;;; Internal variables.
49
50 (defvar gnus-summary-mark-positions nil)
51 (defvar gnus-group-mark-positions nil)
52 (defvar gnus-group-indentation "")
53
54 ;; Format specs.  The chunks below are the machine-generated forms
55 ;; that are to be evaled as the result of the default format strings.
56 ;; We write them in here to get them byte-compiled.  That way the
57 ;; default actions will be quite fast, while still retaining the full
58 ;; flexibility of the user-defined format specs.
59
60 ;; First we have lots of dummy defvars to let the compiler know these
61 ;; are really dynamic variables.
62
63 (defvar gnus-tmp-unread)
64 (defvar gnus-tmp-replied)
65 (defvar gnus-tmp-score-char)
66 (defvar gnus-tmp-indentation)
67 (defvar gnus-tmp-opening-bracket)
68 (defvar gnus-tmp-lines)
69 (defvar gnus-tmp-name)
70 (defvar gnus-tmp-closing-bracket)
71 (defvar gnus-tmp-subject-or-nil)
72 (defvar gnus-tmp-subject)
73 (defvar gnus-tmp-marked)
74 (defvar gnus-tmp-marked-mark)
75 (defvar gnus-tmp-subscribed)
76 (defvar gnus-tmp-process-marked)
77 (defvar gnus-tmp-number-of-unread)
78 (defvar gnus-tmp-group-name)
79 (defvar gnus-tmp-group)
80 (defvar gnus-tmp-article-number)
81 (defvar gnus-tmp-unread-and-unselected)
82 (defvar gnus-tmp-news-method)
83 (defvar gnus-tmp-news-server)
84 (defvar gnus-tmp-article-number)
85 (defvar gnus-mouse-face)
86 (defvar gnus-mouse-face-prop)
87 (defvar gnus-tmp-header)
88 (defvar gnus-tmp-from)
89
90 (defun gnus-summary-line-format-spec ()
91   (insert gnus-tmp-unread gnus-tmp-replied
92           gnus-tmp-score-char gnus-tmp-indentation)
93   (gnus-put-text-property
94    (point)
95    (progn
96      (insert
97       (format "%c%4s: %-23s%c" gnus-tmp-opening-bracket gnus-tmp-lines
98               (let ((val
99                      (inline
100                        (gnus-summary-from-or-to-or-newsgroups
101                         gnus-tmp-header gnus-tmp-from))))
102                 (if (> (length val) 23)
103                     (substring val 0 23)
104                   val))
105               gnus-tmp-closing-bracket))
106      (point))
107    gnus-mouse-face-prop gnus-mouse-face)
108   (insert " " gnus-tmp-subject-or-nil "\n"))
109
110 (defvar gnus-summary-line-format-spec
111   (gnus-byte-code 'gnus-summary-line-format-spec))
112
113 (defun gnus-summary-dummy-line-format-spec ()
114   (insert "*  ")
115   (gnus-put-text-property
116    (point)
117    (progn
118      (insert ":                          :")
119      (point))
120    gnus-mouse-face-prop gnus-mouse-face)
121   (insert " " gnus-tmp-subject "\n"))
122
123 (defvar gnus-summary-dummy-line-format-spec
124   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
125
126 (defun gnus-group-line-format-spec ()
127   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
128           gnus-tmp-process-marked
129           gnus-group-indentation
130           (format "%5s: " gnus-tmp-number-of-unread))
131   (gnus-put-text-property
132    (point)
133    (progn
134      (insert gnus-tmp-group "\n")
135      (1- (point)))
136    gnus-mouse-face-prop gnus-mouse-face))
137 (defvar gnus-group-line-format-spec
138   (gnus-byte-code 'gnus-group-line-format-spec))
139
140 (defvar gnus-format-specs
141   `((group ("%M\%S\%p\%P\%5y: %(%g%)\n" ,gnus-group-line-format-spec))
142     (summary-dummy ("*  %(:                          :%) %S\n"
143                     ,gnus-summary-dummy-line-format-spec))
144     (summary ("%U%R%z%I%(%[%4L: %-23,23f%]%) %s\n"
145               ,gnus-summary-line-format-spec)))
146   "Alist of format specs.")
147
148 (defvar gnus-default-format-specs gnus-format-specs)
149
150 (defvar gnus-format-specs-compiled nil
151   "Alist of compiled format specs.  Each element should be the form:
152 \(TYPE (FORMAT-STRING-1 . COMPILED-FUNCTION-1)
153                  :
154        (FORMAT-STRING-n . COMPILED-FUNCTION-n)).")
155
156 (defvar gnus-article-mode-line-format-spec nil)
157 (defvar gnus-summary-mode-line-format-spec nil)
158 (defvar gnus-group-mode-line-format-spec nil)
159
160 ;;; Phew.  All that gruft is over with, fortunately.
161
162 ;;;###autoload
163 (defun gnus-update-format (var)
164   "Update the format specification near point."
165   (interactive
166    (list
167     (save-excursion
168       (eval-defun nil)
169       ;; Find the end of the current word.
170       (re-search-forward "[ \t\n]" nil t)
171       ;; Search backward.
172       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
173         (match-string 1)))))
174   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
175                               (match-string 1 var))))
176          (value (symbol-value (intern var)))
177          (spec (set
178                 (intern (format "%s-spec" var))
179                 (gnus-parse-format
180                  value (symbol-value (intern (format "%s-alist" var)))
181                  (not (string-match "mode" var)))))
182          (entry (assq type gnus-format-specs)))
183     (if entry
184         (let ((elem (assoc value entry)))
185           (if elem
186               (setcdr elem spec)
187             (setcdr entry (cons (cons value elem) (cdr entry)))))
188       (push (list type (cons value spec)) gnus-format-specs))
189     (gnus-product-variable-touch 'gnus-format-specs)
190
191     (pop-to-buffer "*Gnus Format*")
192     (erase-buffer)
193     (lisp-interaction-mode)
194     (insert (gnus-pp-to-string spec))))
195
196 (eval-when-compile (defvar unchanged))
197
198 (put 'gnus-search-or-regist-spec 'lisp-indent-function 4)
199 (defmacro gnus-search-or-regist-spec (specs type format val &rest body)
200   `(let* ((entry (assq ,type ,specs))
201           (elem (assoc ,format (cdr entry))))
202      ;; That `(cdr elem)' returns non-nil means the spec for `type'
203      ;; doesn't need to be updated.
204      (or (cdr elem)
205          ;; This variable is set beforehand.
206          (setq unchanged nil)
207          ;; Update the spec.  Where `body' will modify `val'.  This
208          ;; section will be skipped if compiling the spec is disabled.
209          (when (progn ,@body)
210            (if entry
211                (if elem
212                    (setcdr elem ,val)
213                  (setcdr entry (cons (cons ,format ,val) (cdr entry))))
214              (push (list ,type (cons ,format ,val)) ,specs))
215            (gnus-product-variable-touch (quote ,specs)))
216          ;; Return the new spec without compiling.
217          ,val)))
218
219 (defun gnus-update-format-specification-1 (type format val)
220   (set (intern (format "gnus-%s-line-format-spec" type))
221        (gnus-search-or-regist-spec
222            gnus-format-specs-compiled type format val
223          (when (and gnus-compile-user-specs val)
224            (setq val (prog1
225                          (progn
226                            (fset 'gnus-tmp-func `(lambda () ,val))
227                            (require 'bytecomp)
228                            (let (byte-compile-warnings)
229                              (byte-compile 'gnus-tmp-func))
230                            (gnus-byte-code 'gnus-tmp-func))
231                        (when (get-buffer "*Compile-Log*")
232                          (bury-buffer "*Compile-Log*"))
233                        (when (get-buffer "*Compile-Log-Show*")
234                          (bury-buffer "*Compile-Log-Show*"))))))))
235
236 (defun gnus-update-format-specifications (&optional force &rest types)
237   "Update all (necessary) format specifications.
238 Return a list of updated types."
239   ;; Make the indentation array.
240   ;; See whether all the stored info needs to be flushed.
241   (when force
242     (message "%s" "Force update format specs.")
243     (setq gnus-format-specs nil
244           gnus-format-specs-compiled nil)
245     (gnus-product-variable-touch 'gnus-format-specs
246                                  'gnus-format-specs-compiled))
247   ;; Flush the group format spec cache if there's the grouplens stuff.
248   (let ((spec (assq 'group gnus-format-specs)))
249     (when (and (memq 'group types)
250                (string-match " gnus-tmp-grouplens[ )]"
251                              (gnus-prin1-to-string (cdr spec))))
252       (setq gnus-format-specs (delq spec gnus-format-specs)
253             spec (assq 'group gnus-format-specs-compiled)
254             gnus-format-specs-compiled (delq spec gnus-format-specs-compiled))))
255
256   ;; Go through all the formats and see whether they need updating.
257   (let (new-format type val unchanged updated)
258     (save-excursion
259       (while (setq type (pop types))
260         ;; Jump to the proper buffer to find out the value of the
261         ;; variable, if possible.  (It may be buffer-local.)
262         (let ((buffer (intern (format "gnus-%s-buffer" type))))
263           (when (and (boundp buffer)
264                      (setq val (symbol-value buffer))
265                      (gnus-buffer-exists-p val))
266             (set-buffer val))
267           (setq new-format (symbol-value
268                             (intern (format "gnus-%s-line-format" type)))))
269         (setq unchanged t)
270         (or (gnus-update-format-specification-1 type new-format nil)
271             ;; This is a new format.
272             (gnus-update-format-specification-1
273              type new-format
274              (gnus-search-or-regist-spec
275                  gnus-format-specs type new-format val
276                (setq val (if (stringp new-format)
277                              ;; This is a "real" format.
278                              (gnus-parse-format
279                               new-format
280                               (symbol-value
281                                (intern (format "gnus-%s-line-format-alist"
282                                                type)))
283                               (not (string-match "mode$"
284                                                  (symbol-name type))))
285                            ;; This is a function call or something.
286                            new-format)))))
287         (unless unchanged
288           (push type updated))))
289     updated))
290
291 (defvar gnus-mouse-face-0 'highlight)
292 (defvar gnus-mouse-face-1 'highlight)
293 (defvar gnus-mouse-face-2 'highlight)
294 (defvar gnus-mouse-face-3 'highlight)
295 (defvar gnus-mouse-face-4 'highlight)
296
297 (defun gnus-mouse-face-function (form type)
298   `(gnus-put-text-property
299     (point) (progn ,@form (point))
300     gnus-mouse-face-prop
301     ,(if (equal type 0)
302          'gnus-mouse-face
303        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
304
305 (defvar gnus-face-0 'bold)
306 (defvar gnus-face-1 'italic)
307 (defvar gnus-face-2 'bold-italic)
308 (defvar gnus-face-3 'bold)
309 (defvar gnus-face-4 'bold)
310
311 (defun gnus-face-face-function (form type)
312   `(gnus-add-text-properties
313     (point) (progn ,@form (point))
314     '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
315
316 (defun gnus-balloon-face-function (form type)
317   `(gnus-put-text-property
318     (point) (progn ,@form (point))
319     ,(if (fboundp 'balloon-help-mode)
320          ''balloon-help
321        ''help-echo)
322     ,(intern (format "gnus-balloon-face-%d" type))))
323
324 (defun gnus-spec-tab (column)
325   (if (> column 0)
326       `(insert (make-string (max (- ,column (current-column)) 0) ? ))
327     (let ((column (abs column)))
328       (if gnus-use-correct-string-widths
329           `(progn
330              (if (> (current-column) ,column)
331                  (while (progn
332                           (delete-backward-char 1)
333                           (> (current-column) ,column))))
334              (insert (make-string (max (- ,column (current-column)) 0) ? )))
335         `(progn
336            (if (> (current-column) ,column)
337                (delete-region (point)
338                               (- (point) (- (current-column) ,column)))
339              (insert (make-string (max (- ,column (current-column)) 0)
340                                   ? ))))))))
341
342 (defun gnus-correct-length (string)
343   "Return the correct width of STRING."
344   (let ((length 0))
345     (mapcar (lambda (char) (incf length (char-width char))) string)
346     length))
347
348 (defun gnus-correct-substring (string start &optional end)
349   (let ((wstart 0)
350         (wend 0)
351         (wseek 0)
352         (seek 0)
353         (length (length string))
354         (string (concat string "\0")))
355     ;; Find the start position.
356     (while (and (< seek length)
357                 (< wseek start))
358       (incf wseek (char-width (aref string seek)))
359       (incf seek))
360     (setq wstart seek)
361     ;; Find the end position.
362     (while (and (<= seek length)
363                 (or (not end)
364                     (<= wseek end)))
365       (incf wseek (char-width (aref string seek)))
366       (incf seek))
367     (setq wend seek)
368     (substring string wstart (1- wend))))
369
370 (defun gnus-string-width-function ()
371   (cond
372    (gnus-use-correct-string-widths
373     'gnus-correct-length)
374    ((fboundp 'string-width)
375     'string-width)
376    (t
377     'length)))
378
379 (defun gnus-substring-function ()
380   (cond
381    (gnus-use-correct-string-widths
382     'gnus-correct-substring)
383    ((fboundp 'string-width)
384     'gnus-correct-substring)
385    (t
386     'substring)))
387
388 (defun gnus-tilde-max-form (el max-width)
389   "Return a form that limits EL to MAX-WIDTH."
390   (let ((max (abs max-width))
391         (length-fun (gnus-string-width-function))
392         (substring-fun (gnus-substring-function)))
393     (if (symbolp el)
394         `(if (> (,length-fun ,el) ,max)
395              ,(if (< max-width 0)
396                   `(,substring-fun ,el (- (,length-fun ,el) ,max))
397                 `(,substring-fun ,el 0 ,max))
398            ,el)
399       `(let ((val (eval ,el)))
400          (if (> (,length-fun val) ,max)
401              ,(if (< max-width 0)
402                   `(,substring-fun val (- (,length-fun val) ,max))
403                 `(,substring-fun val 0 ,max))
404            val)))))
405
406 (defun gnus-tilde-cut-form (el cut-width)
407   "Return a form that cuts CUT-WIDTH off of EL."
408   (let ((cut (abs cut-width))
409         (length-fun (gnus-string-width-function))
410         (substring-fun (gnus-substring-function)))
411     (if (symbolp el)
412         `(if (> (,length-fun ,el) ,cut)
413              ,(if (< cut-width 0)
414                   `(,substring-fun ,el 0 (- (,length-fun ,el) ,cut))
415                 `(,substring-fun ,el ,cut))
416            ,el)
417       `(let ((val (eval ,el)))
418          (if (> (,length-fun val) ,cut)
419              ,(if (< cut-width 0)
420                   `(,substring-fun val 0 (- (,length-fun val) ,cut))
421                 `(,substring-fun val ,cut))
422            val)))))
423
424 (defun gnus-tilde-ignore-form (el ignore-value)
425   "Return a form that is blank when EL is IGNORE-VALUE."
426   (if (symbolp el)
427       `(if (equal ,el ,ignore-value)
428            "" ,el)
429     `(let ((val (eval ,el)))
430        (if (equal val ,ignore-value)
431            "" val))))
432
433 (defun gnus-pad-form (el pad-width)
434   "Return a form that pads EL to PAD-WIDTH accounting for multi-column
435 characters correctly. This is because `format' may pad to columns or to
436 characters when given a pad value."
437   (let ((pad (abs pad-width))
438         (side (< 0 pad-width))
439         (length-fun (gnus-string-width-function)))
440     (if (symbolp el)
441         `(let ((need (- ,pad (,length-fun ,el))))
442            (if (> need 0)
443                (concat ,(when side '(make-string need ?\ ))
444                        ,el
445                        ,(when (not side) '(make-string need ?\ )))
446              ,el))
447       `(let* ((val (eval ,el))
448               (need (- ,pad (,length-fun val))))
449          (if (> need 0)
450              (concat ,(when side '(make-string need ?\ ))
451                      val
452                      ,(when (not side) '(make-string need ?\ )))
453            val)))))
454
455 (defun gnus-parse-format (format spec-alist &optional insert)
456   ;; This function parses the FORMAT string with the help of the
457   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
458   ;; string.  If the FORMAT string contains the specifiers %( and %)
459   ;; the text between them will have the mouse-face text property.
460   ;; If the FORMAT string contains the specifiers %[ and %], the text between
461   ;; them will have the balloon-help text property.
462   (let ((case-fold-search nil))
463     (if (string-match
464          "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'\\|%[-0-9]*=\\|%[-0-9]*\\*"
465          format)
466         (gnus-parse-complex-format format spec-alist)
467       ;; This is a simple format.
468       (gnus-parse-simple-format format spec-alist insert))))
469
470 (defun gnus-parse-complex-format (format spec-alist)
471   (let ((cursor-spec nil))
472     (save-excursion
473       (gnus-set-work-buffer)
474       (insert format)
475       (goto-char (point-min))
476       (while (re-search-forward "\"" nil t)
477         (replace-match "\\\"" nil t))
478       (goto-char (point-min))
479       (insert "(\"")
480       ;; Convert all font specs into font spec lists.
481       (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
482         (let ((number (if (match-beginning 1)
483                           (match-string 1) "0"))
484               (delim (aref (match-string 2) 0)))
485           (if (or (= delim ?\()
486                   (= delim ?\{)
487                   (= delim ?\«))
488               (replace-match (concat "\"("
489                                      (cond ((= delim ?\() "mouse")
490                                            ((= delim ?\{) "face")
491                                            (t "balloon"))
492                                      " " number " \"")
493                              t t)
494             (replace-match "\")\""))))
495       (goto-char (point-max))
496       (insert "\")")
497       ;; Convert point position commands.
498       (goto-char (point-min))
499       (let ((case-fold-search nil))
500         (while (re-search-forward "%\\([-0-9]+\\)?\\*" nil t)
501           (replace-match "\"(point)\"" t t)
502           (setq cursor-spec t)))
503       ;; Convert TAB commands.
504       (goto-char (point-min))
505       (while (re-search-forward "%\\([-0-9]+\\)=" nil t)
506         (replace-match (format "\"(tab %s)\"" (match-string 1)) t t))
507       ;; Convert the buffer into the spec.
508       (goto-char (point-min))
509       (let ((form (read (current-buffer))))
510         (if cursor-spec
511             `(let (gnus-position)
512                ,@(gnus-complex-form-to-spec form spec-alist)
513                (if gnus-position
514                    (gnus-put-text-property gnus-position (1+ gnus-position)
515                                            'gnus-position t)))
516           `(progn
517              ,@(gnus-complex-form-to-spec form spec-alist)))))))
518
519 (defun gnus-complex-form-to-spec (form spec-alist)
520   (delq nil
521         (mapcar
522          (lambda (sform)
523            (cond
524             ((stringp sform)
525              (gnus-parse-simple-format sform spec-alist t))
526             ((eq (car sform) 'point)
527              '(setq gnus-position (point)))
528             ((eq (car sform) 'tab)
529              (gnus-spec-tab (cadr sform)))
530             (t
531              (funcall (intern (format "gnus-%s-face-function" (car sform)))
532                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
533                       (nth 1 sform)))))
534          form)))
535
536
537 (defun gnus-xmas-format (fstring &rest args)
538   "A version of `format' which preserves text properties.
539
540 Required for XEmacs, where the built in `format' function strips all text
541 properties from both the format string and any inserted strings.
542
543 Only supports the format sequence %s, and %% for inserting
544 literal % characters. A pad width and an optional - (to right pad)
545 are supported for %s."
546   (let ((re "%%\\|%\\(-\\)?\\([1-9][0-9]*\\)?s")
547         (n (length args)))
548     (with-temp-buffer
549       (insert fstring)
550       (goto-char (point-min))
551       (while (re-search-forward re nil t)
552         (goto-char (match-end 0))
553         (cond
554          ((string= (match-string 0) "%%")
555           (delete-char -1))
556          (t
557           (if (null args)
558               (error 'wrong-number-of-arguments #'my-format n fstring))
559           (let* ((minlen (string-to-int (or (match-string 2) "")))
560                  (arg (car args))
561                  (str (if (stringp arg) arg (format "%s" arg)))
562                  (lpad (null (match-string 1)))
563                  (padlen (max 0 (- minlen (length str)))))
564             (replace-match "")
565             (if lpad (insert-char ?\  padlen))
566             (insert str)
567             (unless lpad (insert-char ?\  padlen))
568             (setq args (cdr args))))))
569       (buffer-string))))
570
571 (defun gnus-parse-simple-format (format spec-alist &optional insert)
572   ;; This function parses the FORMAT string with the help of the
573   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
574   ;; string.
575   (let (max-width
576         spec flist fstring elem result dontinsert user-defined
577         type value pad-width spec-beg cut-width ignore-value
578         tilde-form tilde elem-type extended-spec)
579     (save-excursion
580       (gnus-set-work-buffer)
581       (insert format)
582       (goto-char (point-min))
583       (while (search-forward "%" nil t)
584         (setq user-defined nil
585               spec-beg nil
586               pad-width nil
587               max-width nil
588               cut-width nil
589               ignore-value nil
590               tilde-form nil
591               extended-spec nil)
592         (setq spec-beg (1- (point)))
593
594         ;; Parse this spec fully.
595         (while
596             (cond
597              ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
598               (setq pad-width (string-to-number (match-string 1)))
599               (when (match-beginning 2)
600                 (setq max-width (string-to-number (buffer-substring
601                                                    (1+ (match-beginning 2))
602                                                    (match-end 2)))))
603               (goto-char (match-end 0)))
604              ((looking-at "~")
605               (forward-char 1)
606               (setq tilde (read (current-buffer))
607                     type (car tilde)
608                     value (cadr tilde))
609               (cond
610                ((memq type '(pad pad-left))
611                 (setq pad-width value))
612                ((eq type 'pad-right)
613                 (setq pad-width (- value)))
614                ((memq type '(max-right max))
615                 (setq max-width value))
616                ((eq type 'max-left)
617                 (setq max-width (- value)))
618                ((memq type '(cut cut-left))
619                 (setq cut-width value))
620                ((eq type 'cut-right)
621                 (setq cut-width (- value)))
622                ((eq type 'ignore)
623                 (setq ignore-value
624                       (if (stringp value) value (format "%s" value))))
625                ((eq type 'form)
626                 (setq tilde-form value))
627                (t
628                 (error "Unknown tilde type: %s" tilde)))
629               t)
630              (t
631               nil)))
632         (cond
633          ;; User-defined spec -- find the spec name.
634          ((eq (setq spec (char-after)) ?u)
635           (forward-char 1)
636           (when (and (eq (setq user-defined (char-after)) ?&)
637                      (looking-at "&\\([^;]+\\);"))
638             (setq user-defined (match-string 1))
639             (goto-char (match-end 1))))
640          ;; extended spec
641          ((and (eq spec ?&) (looking-at "&\\([^;]+\\);"))
642           (setq extended-spec (intern (match-string 1)))
643           (goto-char (match-end 1))))
644         (forward-char 1)
645         (delete-region spec-beg (point))
646
647         ;; Now we have all the relevant data on this spec, so
648         ;; we start doing stuff.
649         (insert "%")
650         (if (eq spec ?%)
651             ;; "%%" just results in a "%".
652             (insert "%")
653           (cond
654            ;; Do tilde forms.
655            ((eq spec ?@)
656             (setq elem (list tilde-form ?s)))
657            ;; Treat user defined format specifiers specially.
658            (user-defined
659             (setq elem
660                   (list
661                    (list (intern (format
662                                   (if (stringp user-defined)
663                                       "gnus-user-format-function-%s"
664                                     "gnus-user-format-function-%c")
665                                   user-defined))
666                          'gnus-tmp-header)
667                    ?s)))
668            ;; Find the specification from `spec-alist'.
669            ((setq elem (cdr (assq (or extended-spec spec) spec-alist))))
670            ;; We used to use "%l" for displaying the grouplens score.
671            ((eq spec ?l)
672             (setq elem '("" ?s)))
673            (t
674             (setq elem '("*" ?s))))
675           (setq elem-type (cadr elem))
676           ;; Insert the new format elements.
677           (when (and pad-width
678                      (not (and (featurep 'xemacs)
679                                gnus-use-correct-string-widths)))
680             (insert (number-to-string pad-width)))
681           ;; Create the form to be evaled.
682           (if (or max-width cut-width ignore-value
683                   (and (featurep 'xemacs)
684                        gnus-use-correct-string-widths))
685               (progn
686                 (insert ?s)
687                 (let ((el (car elem)))
688                   (cond ((= (cadr elem) ?c)
689                          (setq el (list 'char-to-string el)))
690                         ((= (cadr elem) ?d)
691                          (setq el (list 'int-to-string el))))
692                   (when ignore-value
693                     (setq el (gnus-tilde-ignore-form el ignore-value)))
694                   (when cut-width
695                     (setq el (gnus-tilde-cut-form el cut-width)))
696                   (when max-width
697                     (setq el (gnus-tilde-max-form el max-width)))
698                   (when pad-width
699                     (setq el (gnus-pad-form el pad-width)))
700                   (push el flist)))
701             (insert elem-type)
702             (push (car elem) flist))))
703       (setq fstring (buffer-substring-no-properties (point-min) (point-max))))
704
705     ;; Do some postprocessing to increase efficiency.
706     (setq
707      result
708      (cond
709       ;; Emptiness.
710       ((string= fstring "")
711        nil)
712       ;; Not a format string.
713       ((not (string-match "%" fstring))
714        (list fstring))
715       ;; A format string with just a single string spec.
716       ((string= fstring "%s")
717        (list (car flist)))
718       ;; A single character.
719       ((string= fstring "%c")
720        (list (car flist)))
721       ;; A single number.
722       ((string= fstring "%d")
723        (setq dontinsert t)
724        (if insert
725            (list `(princ ,(car flist)))
726          (list `(int-to-string ,(car flist)))))
727       ;; Just lots of chars and strings.
728       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
729        (nreverse flist))
730       ;; A single string spec at the beginning of the spec.
731       ((string-match "\\`%[sc][^%]+\\'" fstring)
732        (list (car flist) (substring fstring 2)))
733       ;; A single string spec in the middle of the spec.
734       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
735        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
736       ;; A single string spec in the end of the spec.
737       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
738        (list (match-string 1 fstring) (car flist)))
739       ;; Only string (and %) specs (XEmacs only!)
740       ((and (featurep 'xemacs)
741             gnus-make-format-preserve-properties
742             (string-match
743              "\\`\\([^%]*\\(%%\\|%-?\\([1-9][0-9]*\\)?s\\)\\)*[^%]*\\'"
744              fstring))
745        (list (cons 'gnus-xmas-format (cons fstring (nreverse flist)))))
746       ;; A more complex spec.
747       (t
748        (list (cons 'format (cons fstring (nreverse flist)))))))
749
750     (if insert
751         (when result
752           (if dontinsert
753               result
754             (cons 'insert result)))
755       (cond ((stringp result)
756              result)
757             ((consp result)
758              (cons 'concat result))
759             (t "")))))
760
761 (defun gnus-eval-format (format &optional alist props)
762   "Eval the format variable FORMAT, using ALIST.
763 If PROPS, insert the result."
764   (let ((form (gnus-parse-format format alist props)))
765     (if props
766         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
767       (eval form))))
768
769 (defun gnus-compile ()
770   "Byte-compile the user-defined format specs."
771   (interactive)
772   (require 'bytecomp)
773   (let ((entries gnus-format-specs)
774         (byte-compile-warnings '(unresolved callargs redefine))
775         entry type compiled-function)
776     (save-excursion
777       (gnus-message 7 "Compiling format specs...")
778
779       (while entries
780         (setq entry (pop entries)
781               type (car entry))
782         (if (memq type '(gnus-version version))
783             (setq gnus-format-specs (delq entry gnus-format-specs))
784           (let ((form (caddr entry)))
785             (when (and (listp form)
786                        ;; Under GNU Emacs, it's (byte-code ...)
787                        (not (eq 'byte-code (car form)))
788                        ;; Under XEmacs, it's (funcall #<compiled-function ...>)
789                        (not (and (eq 'funcall (car form))
790                                  (byte-code-function-p (cadr form)))))
791               (defalias 'gnus-tmp-func `(lambda () ,form))
792               (byte-compile 'gnus-tmp-func)
793               (setq compiled-function (gnus-byte-code 'gnus-tmp-func))
794               (set (intern (format "gnus-%s-line-format-spec" type))
795                    compiled-function)
796               (let ((elem (cdr (assq type gnus-format-specs-compiled))))
797                 (if elem
798                     (set-alist 'elem (cadr entry) compiled-function)
799                   (setq elem (list (cadr entry) compiled-function)))
800                 (set-alist 'gnus-format-specs-compiled type elem))))))
801
802       (push (cons 'version emacs-version) gnus-format-specs)
803       (gnus-message 7 "Compiling user specs...done"))))
804
805 (defun gnus-set-format (type &optional insertable)
806   (set (intern (format "gnus-%s-line-format-spec" type))
807        (gnus-parse-format
808         (symbol-value (intern (format "gnus-%s-line-format" type)))
809         (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
810         insertable)))
811
812 (gnus-ems-redefine)
813
814 (provide 'gnus-spec)
815
816 ;; Local Variables:
817 ;; coding: iso-8859-1
818 ;; End:
819
820 ;;; gnus-spec.el ends here