Synch to No Gnus 200412220328.
[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-char ?  (max (- ,column (current-column)) 0))
327     (let ((column (abs column)))
328       `(if (> (current-column) ,column)
329            (let ((end (point)))
330              (if (= (move-to-column ,column) ,column)
331                  (delete-region (point) end)
332                (delete-region (1- (point)) end)
333                (insert " ")))
334          (insert-char ?  (max (- ,column (current-column)) 0))))))
335
336 (defun gnus-correct-length (string)
337   "Return the correct width of STRING."
338   (let ((length 0))
339     (mapcar (lambda (char) (incf length (char-width char))) string)
340     length))
341
342 (defun gnus-correct-substring (string start &optional end)
343   (let ((wstart 0)
344         (wend 0)
345         (wseek 0)
346         (seek 0)
347         (length (length string))
348         (string (concat string "\0")))
349     ;; Find the start position.
350     (while (and (< seek length)
351                 (< wseek start))
352       (incf wseek (char-width (aref string seek)))
353       (incf seek))
354     (setq wstart seek)
355     ;; Find the end position.
356     (while (and (<= seek length)
357                 (or (not end)
358                     (<= wseek end)))
359       (incf wseek (char-width (aref string seek)))
360       (incf seek))
361     (setq wend seek)
362     (substring string wstart (1- wend))))
363
364 (defun gnus-string-width-function ()
365   (cond
366    (gnus-use-correct-string-widths
367     'gnus-correct-length)
368    ((fboundp 'string-width)
369     'string-width)
370    (t
371     'length)))
372
373 (defun gnus-substring-function ()
374   (cond
375    (gnus-use-correct-string-widths
376     'gnus-correct-substring)
377    ((fboundp 'string-width)
378     'gnus-correct-substring)
379    (t
380     'substring)))
381
382 (defun gnus-tilde-max-form (el max-width)
383   "Return a form that limits EL to MAX-WIDTH."
384   (let ((max (abs max-width))
385         (length-fun (gnus-string-width-function))
386         (substring-fun (gnus-substring-function)))
387     (if (symbolp el)
388         `(if (> (,length-fun ,el) ,max)
389              ,(if (< max-width 0)
390                   `(,substring-fun ,el (- (,length-fun ,el) ,max))
391                 `(,substring-fun ,el 0 ,max))
392            ,el)
393       `(let ((val (eval ,el)))
394          (if (> (,length-fun val) ,max)
395              ,(if (< max-width 0)
396                   `(,substring-fun val (- (,length-fun val) ,max))
397                 `(,substring-fun val 0 ,max))
398            val)))))
399
400 (defun gnus-tilde-cut-form (el cut-width)
401   "Return a form that cuts CUT-WIDTH off of EL."
402   (let ((cut (abs cut-width))
403         (length-fun (gnus-string-width-function))
404         (substring-fun (gnus-substring-function)))
405     (if (symbolp el)
406         `(if (> (,length-fun ,el) ,cut)
407              ,(if (< cut-width 0)
408                   `(,substring-fun ,el 0 (- (,length-fun ,el) ,cut))
409                 `(,substring-fun ,el ,cut))
410            ,el)
411       `(let ((val (eval ,el)))
412          (if (> (,length-fun val) ,cut)
413              ,(if (< cut-width 0)
414                   `(,substring-fun val 0 (- (,length-fun val) ,cut))
415                 `(,substring-fun val ,cut))
416            val)))))
417
418 (defun gnus-tilde-ignore-form (el ignore-value)
419   "Return a form that is blank when EL is IGNORE-VALUE."
420   (if (symbolp el)
421       `(if (equal ,el ,ignore-value)
422            "" ,el)
423     `(let ((val (eval ,el)))
424        (if (equal val ,ignore-value)
425            "" val))))
426
427 (defun gnus-pad-form (el pad-width)
428   "Return a form that pads EL to PAD-WIDTH accounting for multi-column
429 characters correctly. This is because `format' may pad to columns or to
430 characters when given a pad value."
431   (let ((pad (abs pad-width))
432         (side (< 0 pad-width))
433         (length-fun (gnus-string-width-function)))
434     (if (symbolp el)
435         `(let ((need (- ,pad (,length-fun ,el))))
436            (if (> need 0)
437                (concat ,(when side '(make-string need ?\ ))
438                        ,el
439                        ,(when (not side) '(make-string need ?\ )))
440              ,el))
441       `(let* ((val (eval ,el))
442               (need (- ,pad (,length-fun val))))
443          (if (> need 0)
444              (concat ,(when side '(make-string need ?\ ))
445                      val
446                      ,(when (not side) '(make-string need ?\ )))
447            val)))))
448
449 (defun gnus-parse-format (format spec-alist &optional insert)
450   ;; This function parses the FORMAT string with the help of the
451   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
452   ;; string.  If the FORMAT string contains the specifiers %( and %)
453   ;; the text between them will have the mouse-face text property.
454   ;; If the FORMAT string contains the specifiers %[ and %], the text between
455   ;; them will have the balloon-help text property.
456   (let ((case-fold-search nil))
457     (if (string-match
458          "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'\\|%[-0-9]*=\\|%[-0-9]*\\*"
459          format)
460         (gnus-parse-complex-format format spec-alist)
461       ;; This is a simple format.
462       (gnus-parse-simple-format format spec-alist insert))))
463
464 (defun gnus-parse-complex-format (format spec-alist)
465   (let ((cursor-spec nil))
466     (save-excursion
467       (gnus-set-work-buffer)
468       (insert format)
469       (goto-char (point-min))
470       (while (re-search-forward "\"" nil t)
471         (replace-match "\\\"" nil t))
472       (goto-char (point-min))
473       (insert "(\"")
474       ;; Convert all font specs into font spec lists.
475       (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
476         (let ((number (if (match-beginning 1)
477                           (match-string 1) "0"))
478               (delim (aref (match-string 2) 0)))
479           (if (or (= delim ?\()
480                   (= delim ?\{)
481                   (= delim ?\«))
482               (replace-match (concat "\"("
483                                      (cond ((= delim ?\() "mouse")
484                                            ((= delim ?\{) "face")
485                                            (t "balloon"))
486                                      " " number " \"")
487                              t t)
488             (replace-match "\")\""))))
489       (goto-char (point-max))
490       (insert "\")")
491       ;; Convert point position commands.
492       (goto-char (point-min))
493       (let ((case-fold-search nil))
494         (while (re-search-forward "%\\([-0-9]+\\)?\\*" nil t)
495           (replace-match "\"(point)\"" t t)
496           (setq cursor-spec t)))
497       ;; Convert TAB commands.
498       (goto-char (point-min))
499       (while (re-search-forward "%\\([-0-9]+\\)=" nil t)
500         (replace-match (format "\"(tab %s)\"" (match-string 1)) t t))
501       ;; Convert the buffer into the spec.
502       (goto-char (point-min))
503       (let ((form (read (current-buffer))))
504         (if cursor-spec
505             `(let (gnus-position)
506                ,@(gnus-complex-form-to-spec form spec-alist)
507                (if gnus-position
508                    (gnus-put-text-property gnus-position (1+ gnus-position)
509                                            'gnus-position t)))
510           `(progn
511              ,@(gnus-complex-form-to-spec form spec-alist)))))))
512
513 (defun gnus-complex-form-to-spec (form spec-alist)
514   (delq nil
515         (mapcar
516          (lambda (sform)
517            (cond
518             ((stringp sform)
519              (gnus-parse-simple-format sform spec-alist t))
520             ((eq (car sform) 'point)
521              '(setq gnus-position (point)))
522             ((eq (car sform) 'tab)
523              (gnus-spec-tab (cadr sform)))
524             (t
525              (funcall (intern (format "gnus-%s-face-function" (car sform)))
526                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
527                       (nth 1 sform)))))
528          form)))
529
530
531 (defun gnus-xmas-format (fstring &rest args)
532   "A version of `format' which preserves text properties.
533
534 Required for XEmacs, where the built in `format' function strips all text
535 properties from both the format string and any inserted strings.
536
537 Only supports the format sequence %s, and %% for inserting
538 literal % characters. A pad width and an optional - (to right pad)
539 are supported for %s."
540   (let ((re "%%\\|%\\(-\\)?\\([1-9][0-9]*\\)?s")
541         (n (length args)))
542     (with-temp-buffer
543       (insert fstring)
544       (goto-char (point-min))
545       (while (re-search-forward re nil t)
546         (goto-char (match-end 0))
547         (cond
548          ((string= (match-string 0) "%%")
549           (delete-char -1))
550          (t
551           (if (null args)
552               (error 'wrong-number-of-arguments #'my-format n fstring))
553           (let* ((minlen (string-to-int (or (match-string 2) "")))
554                  (arg (car args))
555                  (str (if (stringp arg) arg (format "%s" arg)))
556                  (lpad (null (match-string 1)))
557                  (padlen (max 0 (- minlen (length str)))))
558             (replace-match "")
559             (if lpad (insert-char ?\  padlen))
560             (insert str)
561             (unless lpad (insert-char ?\  padlen))
562             (setq args (cdr args))))))
563       (buffer-string))))
564
565 (defun gnus-parse-simple-format (format spec-alist &optional insert)
566   ;; This function parses the FORMAT string with the help of the
567   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
568   ;; string.
569   (let (max-width
570         spec flist fstring elem result dontinsert user-defined
571         type value pad-width spec-beg cut-width ignore-value
572         tilde-form tilde elem-type extended-spec)
573     (save-excursion
574       (gnus-set-work-buffer)
575       (insert format)
576       (goto-char (point-min))
577       (while (search-forward "%" nil t)
578         (setq user-defined nil
579               spec-beg nil
580               pad-width nil
581               max-width nil
582               cut-width nil
583               ignore-value nil
584               tilde-form nil
585               extended-spec nil)
586         (setq spec-beg (1- (point)))
587
588         ;; Parse this spec fully.
589         (while
590             (cond
591              ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
592               (setq pad-width (string-to-number (match-string 1)))
593               (when (match-beginning 2)
594                 (setq max-width (string-to-number (buffer-substring
595                                                    (1+ (match-beginning 2))
596                                                    (match-end 2)))))
597               (goto-char (match-end 0)))
598              ((looking-at "~")
599               (forward-char 1)
600               (setq tilde (read (current-buffer))
601                     type (car tilde)
602                     value (cadr tilde))
603               (cond
604                ((memq type '(pad pad-left))
605                 (setq pad-width value))
606                ((eq type 'pad-right)
607                 (setq pad-width (- value)))
608                ((memq type '(max-right max))
609                 (setq max-width value))
610                ((eq type 'max-left)
611                 (setq max-width (- value)))
612                ((memq type '(cut cut-left))
613                 (setq cut-width value))
614                ((eq type 'cut-right)
615                 (setq cut-width (- value)))
616                ((eq type 'ignore)
617                 (setq ignore-value
618                       (if (stringp value) value (format "%s" value))))
619                ((eq type 'form)
620                 (setq tilde-form value))
621                (t
622                 (error "Unknown tilde type: %s" tilde)))
623               t)
624              (t
625               nil)))
626         (cond
627          ;; User-defined spec -- find the spec name.
628          ((eq (setq spec (char-after)) ?u)
629           (forward-char 1)
630           (when (and (eq (setq user-defined (char-after)) ?&)
631                      (looking-at "&\\([^;]+\\);"))
632             (setq user-defined (match-string 1))
633             (goto-char (match-end 1))))
634          ;; extended spec
635          ((and (eq spec ?&) (looking-at "&\\([^;]+\\);"))
636           (setq extended-spec (intern (match-string 1)))
637           (goto-char (match-end 1))))
638         (forward-char 1)
639         (delete-region spec-beg (point))
640
641         ;; Now we have all the relevant data on this spec, so
642         ;; we start doing stuff.
643         (insert "%")
644         (if (eq spec ?%)
645             ;; "%%" just results in a "%".
646             (insert "%")
647           (cond
648            ;; Do tilde forms.
649            ((eq spec ?@)
650             (setq elem (list tilde-form ?s)))
651            ;; Treat user defined format specifiers specially.
652            (user-defined
653             (setq elem
654                   (list
655                    (list (intern (format
656                                   (if (stringp user-defined)
657                                       "gnus-user-format-function-%s"
658                                     "gnus-user-format-function-%c")
659                                   user-defined))
660                          'gnus-tmp-header)
661                    ?s)))
662            ;; Find the specification from `spec-alist'.
663            ((setq elem (cdr (assq (or extended-spec spec) spec-alist))))
664            ;; We used to use "%l" for displaying the grouplens score.
665            ((eq spec ?l)
666             (setq elem '("" ?s)))
667            (t
668             (setq elem '("*" ?s))))
669           (setq elem-type (cadr elem))
670           ;; Insert the new format elements.
671           (when (and pad-width
672                      (not (and (featurep 'xemacs)
673                                gnus-use-correct-string-widths)))
674             (insert (number-to-string pad-width)))
675           ;; Create the form to be evaled.
676           (if (or max-width cut-width ignore-value
677                   (and (featurep 'xemacs)
678                        gnus-use-correct-string-widths))
679               (progn
680                 (insert ?s)
681                 (let ((el (car elem)))
682                   (cond ((= (cadr elem) ?c)
683                          (setq el (list 'char-to-string el)))
684                         ((= (cadr elem) ?d)
685                          (setq el (list 'int-to-string el))))
686                   (when ignore-value
687                     (setq el (gnus-tilde-ignore-form el ignore-value)))
688                   (when cut-width
689                     (setq el (gnus-tilde-cut-form el cut-width)))
690                   (when max-width
691                     (setq el (gnus-tilde-max-form el max-width)))
692                   (when pad-width
693                     (setq el (gnus-pad-form el pad-width)))
694                   (push el flist)))
695             (insert elem-type)
696             (push (car elem) flist))))
697       (setq fstring (buffer-substring-no-properties (point-min) (point-max))))
698
699     ;; Do some postprocessing to increase efficiency.
700     (setq
701      result
702      (cond
703       ;; Emptiness.
704       ((string= fstring "")
705        nil)
706       ;; Not a format string.
707       ((not (string-match "%" fstring))
708        (list fstring))
709       ;; A format string with just a single string spec.
710       ((string= fstring "%s")
711        (list (car flist)))
712       ;; A single character.
713       ((string= fstring "%c")
714        (list (car flist)))
715       ;; A single number.
716       ((string= fstring "%d")
717        (setq dontinsert t)
718        (if insert
719            (list `(princ ,(car flist)))
720          (list `(int-to-string ,(car flist)))))
721       ;; Just lots of chars and strings.
722       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
723        (nreverse flist))
724       ;; A single string spec at the beginning of the spec.
725       ((string-match "\\`%[sc][^%]+\\'" fstring)
726        (list (car flist) (substring fstring 2)))
727       ;; A single string spec in the middle of the spec.
728       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
729        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
730       ;; A single string spec in the end of the spec.
731       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
732        (list (match-string 1 fstring) (car flist)))
733       ;; Only string (and %) specs (XEmacs only!)
734       ((and (featurep 'xemacs)
735             gnus-make-format-preserve-properties
736             (string-match
737              "\\`\\([^%]*\\(%%\\|%-?\\([1-9][0-9]*\\)?s\\)\\)*[^%]*\\'"
738              fstring))
739        (list (cons 'gnus-xmas-format (cons fstring (nreverse flist)))))
740       ;; A more complex spec.
741       (t
742        (list (cons 'format (cons fstring (nreverse flist)))))))
743
744     (if insert
745         (when result
746           (if dontinsert
747               result
748             (cons 'insert result)))
749       (cond ((stringp result)
750              result)
751             ((consp result)
752              (cons 'concat result))
753             (t "")))))
754
755 (defun gnus-eval-format (format &optional alist props)
756   "Eval the format variable FORMAT, using ALIST.
757 If PROPS, insert the result."
758   (let ((form (gnus-parse-format format alist props)))
759     (if props
760         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
761       (eval form))))
762
763 (defun gnus-compile ()
764   "Byte-compile the user-defined format specs."
765   (interactive)
766   (require 'bytecomp)
767   (let ((entries gnus-format-specs)
768         (byte-compile-warnings '(unresolved callargs redefine))
769         entry type compiled-function)
770     (save-excursion
771       (gnus-message 7 "Compiling format specs...")
772
773       (while entries
774         (setq entry (pop entries)
775               type (car entry))
776         (if (memq type '(gnus-version version))
777             (setq gnus-format-specs (delq entry gnus-format-specs))
778           (let ((form (caddr entry)))
779             (when (and (listp form)
780                        ;; Under GNU Emacs, it's (byte-code ...)
781                        (not (eq 'byte-code (car form)))
782                        ;; Under XEmacs, it's (funcall #<compiled-function ...>)
783                        (not (and (eq 'funcall (car form))
784                                  (byte-code-function-p (cadr form)))))
785               (defalias 'gnus-tmp-func `(lambda () ,form))
786               (byte-compile 'gnus-tmp-func)
787               (setq compiled-function (gnus-byte-code 'gnus-tmp-func))
788               (set (intern (format "gnus-%s-line-format-spec" type))
789                    compiled-function)
790               (let ((elem (cdr (assq type gnus-format-specs-compiled))))
791                 (if elem
792                     (set-alist 'elem (cadr entry) compiled-function)
793                   (setq elem (list (cadr entry) compiled-function)))
794                 (set-alist 'gnus-format-specs-compiled type elem))))))
795
796       (push (cons 'version emacs-version) gnus-format-specs)
797       (gnus-message 7 "Compiling user specs...done"))))
798
799 (defun gnus-set-format (type &optional insertable)
800   (set (intern (format "gnus-%s-line-format-spec" type))
801        (gnus-parse-format
802         (symbol-value (intern (format "gnus-%s-line-format" type)))
803         (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
804         insertable)))
805
806 (gnus-ems-redefine)
807
808 (provide 'gnus-spec)
809
810 ;; Local Variables:
811 ;; coding: iso-8859-1
812 ;; End:
813
814 ;;; gnus-spec.el ends here