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