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