gnus-spec.el (gnus-xmas-format): Fix weird error call
[gnus] / lisp / gnus-spec.el
1 ;;; gnus-spec.el --- format spec functions for Gnus
2
3 ;; Copyright (C) 1996-2013 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30 (eval-when-compile (require 'cl))
31 (defvar gnus-newsrc-file-version)
32
33 (require 'gnus)
34
35 (defcustom gnus-use-correct-string-widths (featurep 'xemacs)
36   "*If non-nil, use correct functions for dealing with wide characters."
37   :version "22.1"
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 Emacs does this anyway."
44   :version "22.1"
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 evalled 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 (declare-function gnus-summary-from-or-to-or-newsgroups "gnus-sum"
91                   (header gnus-tmp-from))
92
93 (defmacro gnus-lrm-string-p (string)
94   (if (fboundp 'bidi-string-mark-left-to-right)
95       ;; LRM, RLM, PDF characters as integers to avoid breaking Emacs
96       ;; 23.
97       `(memq (aref ,string (1- (length ,string))) '(8206 8207 8236))
98     nil))
99
100 (defvar gnus-lrm-string (if (ignore-errors (string 8206))
101                             (propertize (string 8206) 'invisible t)
102                           ""))
103
104 (defvar gnus-summary-line-format-spec nil)
105 (defvar gnus-summary-dummy-line-format-spec nil)
106 (defvar gnus-group-line-format-spec nil)
107
108 (defvar gnus-format-specs
109   `((version . ,emacs-version)
110     (gnus-version . ,(gnus-continuum-version)))
111   "Alist of format specs.")
112
113 (defvar gnus-default-format-specs gnus-format-specs)
114
115 (defvar gnus-article-mode-line-format-spec nil)
116 (defvar gnus-summary-mode-line-format-spec nil)
117 (defvar gnus-group-mode-line-format-spec nil)
118
119 ;;; Phew.  All that gruft is over with, fortunately.
120
121 ;;;###autoload
122 (defun gnus-update-format (var)
123   "Update the format specification near point."
124   (interactive
125    (list
126     (save-excursion
127       (eval-defun nil)
128       ;; Find the end of the current word.
129       (re-search-forward "[ \t\n]" nil t)
130       ;; Search backward.
131       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
132         (match-string 1)))))
133   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
134                               (match-string 1 var))))
135          (entry (assq type gnus-format-specs))
136          value spec)
137     (when entry
138       (setq gnus-format-specs (delq entry gnus-format-specs)))
139     (set
140      (intern (format "%s-spec" var))
141      (gnus-parse-format (setq value (symbol-value (intern var)))
142                         (symbol-value (intern (format "%s-alist" var)))
143                         (not (string-match "mode" var))))
144     (setq spec (symbol-value (intern (format "%s-spec" var))))
145     (push (list type value spec) gnus-format-specs)
146
147     (pop-to-buffer "*Gnus Format*")
148     (erase-buffer)
149     (lisp-interaction-mode)
150     (insert (gnus-pp-to-string spec))))
151
152 (defun gnus-update-format-specifications (&optional force &rest types)
153   "Update all (necessary) format specifications.
154 Return a list of updated types."
155   ;; Make the indentation array.
156   ;; See whether all the stored info needs to be flushed.
157   (when (or force
158             (not gnus-newsrc-file-version)
159             (not (equal (gnus-continuum-version)
160                         (gnus-continuum-version gnus-newsrc-file-version)))
161             (not (equal emacs-version
162                         (cdr (assq 'version gnus-format-specs)))))
163     (setq gnus-format-specs nil))
164   ;; Go through all the formats and see whether they need updating.
165   (let (new-format entry type val updated)
166     (while (setq type (pop types))
167       ;; Jump to the proper buffer to find out the value of the
168       ;; variable, if possible.  (It may be buffer-local.)
169       (save-excursion
170         (let ((buffer (intern (format "gnus-%s-buffer" type))))
171           (when (and (boundp buffer)
172                      (setq val (symbol-value buffer))
173                      (gnus-buffer-exists-p val))
174             (set-buffer val))
175           (setq new-format (symbol-value
176                             (intern (format "gnus-%s-line-format" type)))))
177         (setq entry (cdr (assq type gnus-format-specs)))
178         (if (and (car entry)
179                  (equal (car entry) new-format))
180             ;; Use the old format.
181             (set (intern (format "gnus-%s-line-format-spec" type))
182                  (cadr entry))
183           ;; This is a new format.
184           (setq val
185                 (if (not (stringp new-format))
186                     ;; This is a function call or something.
187                     new-format
188                   ;; This is a "real" format.
189                   (gnus-parse-format
190                    new-format
191                    (symbol-value
192                     (intern (format "gnus-%s-line-format-alist" type)))
193                    (not (string-match "mode$" (symbol-name type))))))
194           ;; Enter the new format spec into the list.
195           (if entry
196               (progn
197                 (setcar (cdr entry) val)
198                 (setcar entry new-format))
199             (push (list type new-format val) gnus-format-specs))
200           (set (intern (format "gnus-%s-line-format-spec" type)) val)
201           (push type updated))))
202
203     (unless (assq 'version gnus-format-specs)
204       (push (cons 'version emacs-version) gnus-format-specs))
205     updated))
206
207 (defcustom gnus-mouse-face-0 'highlight
208   "The \"%(hello%)\" face."
209   :group 'gnus-format
210   :type 'face)
211
212 (defcustom gnus-mouse-face-1 'highlight
213   "The \"%1(hello%)\" face."
214   :group 'gnus-format
215   :type 'face)
216
217 (defcustom gnus-mouse-face-2 'highlight
218   "The \"%2(hello%)\" face."
219   :group 'gnus-format
220   :type 'face)
221
222 (defcustom gnus-mouse-face-3 'highlight
223   "The \"%3(hello%)\" face."
224   :group 'gnus-format
225   :type 'face)
226
227 (defcustom gnus-mouse-face-4 'highlight
228   "The \"%4(hello%)\" face."
229   :group 'gnus-format
230   :type 'face)
231
232 (defun gnus-mouse-face-function (form type)
233   `(gnus-put-text-property
234     (point) (progn ,@form (point))
235     gnus-mouse-face-prop
236     ,(if (equal type 0)
237          'gnus-mouse-face
238        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
239
240 (defcustom gnus-face-0 'bold
241   "The \"%{hello%}\" face."
242   :group 'gnus-format
243   :type 'face)
244
245 (defcustom gnus-face-1 'italic
246   "The \"%1{hello%}\" face."
247   :group 'gnus-format
248   :type 'face)
249
250 (defcustom gnus-face-2 'bold-italic
251   "The \"%2{hello%}\" face."
252   :group 'gnus-format
253   :type 'face)
254
255 (defcustom gnus-face-3 'bold
256   "The \"%3{hello%}\" face."
257   :group 'gnus-format
258   :type 'face)
259
260 (defcustom gnus-face-4 'bold
261   "The \"%4{hello%}\" face."
262   :group 'gnus-format
263   :type 'face)
264
265 (defun gnus-face-face-function (form type)
266   `(gnus-add-text-properties
267     (point) (progn ,@form (point))
268     (cons 'face
269           (cons
270            ;; Delay consing the value of the `face' property until
271            ;; `gnus-add-text-properties' runs, since it will be modified
272            ;; by `gnus-put-text-property-excluding-characters-with-faces'.
273            (list ',(symbol-value (intern (format "gnus-face-%d" type))) 'default)
274            ;; Redundant now, but still convenient.
275            '(gnus-face t)))))
276
277 (defun gnus-balloon-face-function (form type)
278   `(gnus-put-text-property
279     (point) (progn ,@form (point))
280     ,(if (fboundp 'balloon-help-mode)
281          ''balloon-help
282        ''help-echo)
283     ,(intern (format "gnus-balloon-face-%d" type))))
284
285 (defun gnus-spec-tab (column)
286   (if (> column 0)
287       `(insert-char ?  (max (- ,column (current-column)) 0))
288     (let ((column (abs column)))
289       `(if (> (current-column) ,column)
290            (let ((end (point)))
291              (if (= (move-to-column ,column) ,column)
292                  (delete-region (point) end)
293                (delete-region (1- (point)) end)
294                (insert " ")))
295          (insert-char ?  (max (- ,column (current-column)) 0))))))
296
297 (defun gnus-correct-length (string)
298   "Return the correct width of STRING."
299   (apply #'+ (mapcar #'char-width string)))
300
301 (defun gnus-correct-substring (string start &optional end)
302   (let ((wstart 0)
303         (wend 0)
304         (wseek 0)
305         (seek 0)
306         (length (length string))
307         (string (concat string "\0")))
308     ;; Find the start position.
309     (while (and (< seek length)
310                 (< wseek start))
311       (incf wseek (char-width (aref string seek)))
312       (incf seek))
313     (setq wstart seek)
314     ;; Find the end position.
315     (while (and (<= seek length)
316                 (or (not end)
317                     (<= wseek end)))
318       (incf wseek (char-width (aref string seek)))
319       (incf seek))
320     (setq wend seek)
321     (substring string wstart (1- wend))))
322
323 (defun gnus-string-width-function ()
324   (cond
325    (gnus-use-correct-string-widths
326     'gnus-correct-length)
327    ((fboundp 'string-width)
328     'string-width)
329    (t
330     'length)))
331
332 (defun gnus-substring-function ()
333   (cond
334    (gnus-use-correct-string-widths
335     'gnus-correct-substring)
336    ((fboundp 'string-width)
337     'gnus-correct-substring)
338    (t
339     'substring)))
340
341 (defun gnus-tilde-max-form (el max-width)
342   "Return a form that limits EL to MAX-WIDTH."
343   (let ((max (abs max-width))
344         (length-fun (gnus-string-width-function))
345         (substring-fun (gnus-substring-function)))
346     (if (symbolp el)
347         `(if (> (,length-fun ,el) ,max)
348              ,(if (< max-width 0)
349                   `(,substring-fun ,el (- (,length-fun ,el) ,max))
350                 `(if (gnus-lrm-string-p ,el)
351                      (concat (,substring-fun ,el 0 ,max) ,gnus-lrm-string)
352                    (,substring-fun ,el 0 ,max)))
353            ,el)
354       `(let ((val (eval ,el)))
355          (if (> (,length-fun val) ,max)
356              ,(if (< max-width 0)
357                   `(,substring-fun val (- (,length-fun val) ,max))
358                 `(if (gnus-lrm-string-p val)
359                      (concat (,substring-fun val 0 ,max) ,gnus-lrm-string)
360                    (,substring-fun val 0 ,max)))
361            val)))))
362
363 (defun gnus-tilde-cut-form (el cut-width)
364   "Return a form that cuts CUT-WIDTH off of EL."
365   (let ((cut (abs cut-width))
366         (length-fun (gnus-string-width-function))
367         (substring-fun (gnus-substring-function)))
368     (if (symbolp el)
369         `(if (> (,length-fun ,el) ,cut)
370              ,(if (< cut-width 0)
371                   `(,substring-fun ,el 0 (- (,length-fun ,el) ,cut))
372                 `(,substring-fun ,el ,cut))
373            ,el)
374       `(let ((val (eval ,el)))
375          (if (> (,length-fun val) ,cut)
376              ,(if (< cut-width 0)
377                   `(,substring-fun val 0 (- (,length-fun val) ,cut))
378                 `(,substring-fun val ,cut))
379            val)))))
380
381 (defun gnus-tilde-ignore-form (el ignore-value)
382   "Return a form that is blank when EL is IGNORE-VALUE."
383   (if (symbolp el)
384       `(if (equal ,el ,ignore-value)
385            "" ,el)
386     `(let ((val (eval ,el)))
387        (if (equal val ,ignore-value)
388            "" val))))
389
390 (defun gnus-pad-form (el pad-width)
391   "Return a form that pads EL to PAD-WIDTH accounting for multi-column
392 characters correctly. This is because `format' may pad to columns or to
393 characters when given a pad value."
394   (let ((pad (abs pad-width))
395         (side (< 0 pad-width))
396         (length-fun (gnus-string-width-function)))
397     (if (symbolp el)
398         `(let ((need (- ,pad (,length-fun ,el))))
399            (if (> need 0)
400                (concat ,(when side '(make-string need ?\ ))
401                        ,el
402                        ,(when (not side) '(make-string need ?\ )))
403              ,el))
404       `(let* ((val (eval ,el))
405               (need (- ,pad (,length-fun val))))
406          (if (> need 0)
407              (concat ,(when side '(make-string need ?\ ))
408                      val
409                      ,(when (not side) '(make-string need ?\ )))
410            val)))))
411
412 (defun gnus-parse-format (format spec-alist &optional insert)
413   ;; This function parses the FORMAT string with the help of the
414   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
415   ;; string.  If the FORMAT string contains the specifiers %( and %)
416   ;; the text between them will have the mouse-face text property.
417   ;; If the FORMAT string contains the specifiers %[ and %], the text between
418   ;; them will have the balloon-help text property.
419   (let ((case-fold-search nil))
420     (if (string-match
421          "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'\\|%[-0-9]*=\\|%[-0-9]*\\*"
422          format)
423         (gnus-parse-complex-format format spec-alist)
424       ;; This is a simple format.
425       (gnus-parse-simple-format format spec-alist insert))))
426
427 (defun gnus-parse-complex-format (format spec-alist)
428   (let ((cursor-spec nil))
429     (save-excursion
430       (gnus-set-work-buffer)
431       (insert format)
432       (goto-char (point-min))
433       (while (re-search-forward "\"" nil t)
434         (replace-match "\\\"" nil t))
435       (goto-char (point-min))
436       (insert "(\"")
437       ;; Convert all font specs into font spec lists.
438       (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
439         (let ((number (if (match-beginning 1)
440                           (match-string 1) "0"))
441               (delim (aref (match-string 2) 0)))
442           (if (or (= delim ?\()
443                   (= delim ?\{)
444                   (= delim 171)) ; Â«
445               (replace-match (concat "\"("
446                                      (cond ((= delim ?\() "mouse")
447                                            ((= delim ?\{) "face")
448                                            (t "balloon"))
449                                      " " number " \"")
450                              t t)
451             (replace-match "\")\""))))
452       (goto-char (point-max))
453       (insert "\")")
454       ;; Convert point position commands.
455       (goto-char (point-min))
456       (let ((case-fold-search nil))
457         (while (re-search-forward "%\\([-0-9]+\\)?\\*" nil t)
458           (replace-match "\"(point)\"" t t)
459           (setq cursor-spec t)))
460       ;; Convert TAB commands.
461       (goto-char (point-min))
462       (while (re-search-forward "%\\([-0-9]+\\)=" nil t)
463         (replace-match (format "\"(tab %s)\"" (match-string 1)) t t))
464       ;; Convert the buffer into the spec.
465       (goto-char (point-min))
466       (let ((form (read (current-buffer))))
467         (if cursor-spec
468             `(let (gnus-position)
469                ,@(gnus-complex-form-to-spec form spec-alist)
470                (if gnus-position
471                    (gnus-put-text-property gnus-position (1+ gnus-position)
472                                            'gnus-position t)))
473           `(progn
474              ,@(gnus-complex-form-to-spec form spec-alist)))))))
475
476 (defun gnus-complex-form-to-spec (form spec-alist)
477   (delq nil
478         (mapcar
479          (lambda (sform)
480            (cond
481             ((stringp sform)
482              (gnus-parse-simple-format sform spec-alist t))
483             ((eq (car sform) 'point)
484              '(setq gnus-position (point)))
485             ((eq (car sform) 'tab)
486              (gnus-spec-tab (cadr sform)))
487             (t
488              (funcall (intern (format "gnus-%s-face-function" (car sform)))
489                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
490                       (nth 1 sform)))))
491          form)))
492
493
494 (defun gnus-xmas-format (fstring &rest args)
495   "A version of `format' which preserves text properties.
496
497 Required for XEmacs, where the built in `format' function strips all text
498 properties from both the format string and any inserted strings.
499
500 Only supports the format sequence %s, and %% for inserting
501 literal % characters. A pad width and an optional - (to right pad)
502 are supported for %s."
503   (let ((re "%%\\|%\\(-\\)?\\([1-9][0-9]*\\)?s")
504         (n (length args)))
505     (with-temp-buffer
506       (insert fstring)
507       (goto-char (point-min))
508       (while (re-search-forward re nil t)
509         (goto-char (match-end 0))
510         (cond
511          ((string= (match-string 0) "%%")
512           (delete-char -1))
513          (t
514           (if (null args)
515               (signal 'wrong-number-of-arguments
516                       (list #'gnus-xmas-format n fstring)))
517           (let* ((minlen (string-to-number (or (match-string 2) "")))
518                  (arg (car args))
519                  (str (if (stringp arg) arg (format "%s" arg)))
520                  (lpad (null (match-string 1)))
521                  (padlen (max 0 (- minlen (length str)))))
522             (replace-match "")
523             (if lpad (insert-char ?\  padlen))
524             (insert str)
525             (unless lpad (insert-char ?\  padlen))
526             (setq args (cdr args))))))
527       (buffer-string))))
528
529 (defun gnus-parse-simple-format (format spec-alist &optional insert)
530   ;; This function parses the FORMAT string with the help of the
531   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
532   ;; string.
533   (let ((max-width 0)
534         spec flist fstring elem result dontinsert user-defined
535         type value pad-width spec-beg cut-width ignore-value
536         tilde-form tilde elem-type extended-spec)
537     (save-excursion
538       (gnus-set-work-buffer)
539       (insert format)
540       (goto-char (point-min))
541       (while (re-search-forward "%" nil t)
542         (setq user-defined nil
543               spec-beg nil
544               pad-width nil
545               max-width nil
546               cut-width nil
547               ignore-value nil
548               tilde-form nil
549               extended-spec nil)
550         (setq spec-beg (1- (point)))
551
552         ;; Parse this spec fully.
553         (while
554             (cond
555              ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
556               (setq pad-width (string-to-number (match-string 1)))
557               (when (match-beginning 2)
558                 (setq max-width (string-to-number (buffer-substring
559                                                    (1+ (match-beginning 2))
560                                                    (match-end 2)))))
561               (goto-char (match-end 0)))
562              ((looking-at "~")
563               (forward-char 1)
564               (setq tilde (read (current-buffer))
565                     type (car tilde)
566                     value (cadr tilde))
567               (cond
568                ((memq type '(pad pad-left))
569                 (setq pad-width value))
570                ((eq type 'pad-right)
571                 (setq pad-width (- value)))
572                ((memq type '(max-right max))
573                 (setq max-width value))
574                ((eq type 'max-left)
575                 (setq max-width (- value)))
576                ((memq type '(cut cut-left))
577                 (setq cut-width value))
578                ((eq type 'cut-right)
579                 (setq cut-width (- value)))
580                ((eq type 'ignore)
581                 (setq ignore-value
582                       (if (stringp value) value (format "%s" value))))
583                ((eq type 'form)
584                 (setq tilde-form value))
585                (t
586                 (error "Unknown tilde type: %s" tilde)))
587               t)
588              (t
589               nil)))
590         (cond
591          ;; User-defined spec -- find the spec name.
592          ((eq (setq spec (char-after)) ?u)
593           (forward-char 1)
594           (when (and (eq (setq user-defined (char-after)) ?&)
595                      (looking-at "&\\([^;]+\\);"))
596             (setq user-defined (match-string 1))
597             (goto-char (match-end 1))))
598          ;; extended spec
599          ((and (eq spec ?&) (looking-at "&\\([^;]+\\);"))
600           (setq extended-spec (intern (match-string 1)))
601           (goto-char (match-end 1))))
602         (forward-char 1)
603         (delete-region spec-beg (point))
604
605         ;; Now we have all the relevant data on this spec, so
606         ;; we start doing stuff.
607         (insert "%")
608         (if (eq spec ?%)
609             ;; "%%" just results in a "%".
610             (insert "%")
611           (cond
612            ;; Do tilde forms.
613            ((eq spec ?@)
614             (setq elem (list tilde-form ?s)))
615            ;; Treat user defined format specifiers specially.
616            (user-defined
617             (setq elem
618                   (list
619                    (list (intern (format
620                                   (if (stringp user-defined)
621                                       "gnus-user-format-function-%s"
622                                     "gnus-user-format-function-%c")
623                                   user-defined))
624                          'gnus-tmp-header)
625                    ?s)))
626            ;; Find the specification from `spec-alist'.
627            ((setq elem (cdr (assq (or extended-spec spec) spec-alist))))
628            ;; We used to use "%l" for displaying the grouplens score.
629            ((eq spec ?l)
630             (setq elem '("" ?s)))
631            (t
632             (setq elem '("*" ?s))))
633           (setq elem-type (cadr elem))
634           ;; Insert the new format elements.
635           (when (and pad-width
636                      (not (and (featurep 'xemacs)
637                                gnus-use-correct-string-widths)))
638             (insert (number-to-string pad-width)))
639           ;; Create the form to be evalled.
640           (if (or max-width cut-width ignore-value
641                   (and (featurep 'xemacs)
642                        gnus-use-correct-string-widths))
643               (progn
644                 (insert ?s)
645                 (let ((el (car elem)))
646                   (cond ((= (cadr elem) ?c)
647                          (setq el (list 'char-to-string el)))
648                         ((= (cadr elem) ?d)
649                          (setq el (list 'int-to-string el))))
650                   (when ignore-value
651                     (setq el (gnus-tilde-ignore-form el ignore-value)))
652                   (when cut-width
653                     (setq el (gnus-tilde-cut-form el cut-width)))
654                   (when max-width
655                     (setq el (gnus-tilde-max-form el max-width)))
656                   (when pad-width
657                     (setq el (gnus-pad-form el pad-width)))
658                   (push el flist)))
659             (insert elem-type)
660             (push (car elem) flist))))
661       (setq fstring (buffer-substring-no-properties (point-min) (point-max))))
662
663     ;; Do some postprocessing to increase efficiency.
664     (setq
665      result
666      (cond
667       ;; Emptiness.
668       ((string= fstring "")
669        nil)
670       ;; Not a format string.
671       ((not (string-match "%" fstring))
672        (list fstring))
673       ;; A format string with just a single string spec.
674       ((string= fstring "%s")
675        (list (car flist)))
676       ;; A single character.
677       ((string= fstring "%c")
678        (list (car flist)))
679       ;; A single number.
680       ((string= fstring "%d")
681        (setq dontinsert t)
682        (if insert
683            `(insert (int-to-string ,(car flist)))
684          (list `(int-to-string ,(car flist)))))
685       ;; Just lots of chars and strings.
686       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
687        (nreverse flist))
688       ;; A single string spec at the beginning of the spec.
689       ((string-match "\\`%[sc][^%]+\\'" fstring)
690        (list (car flist) (substring fstring 2)))
691       ;; A single string spec in the middle of the spec.
692       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
693        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
694       ;; A single string spec in the end of the spec.
695       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
696        (list (match-string 1 fstring) (car flist)))
697       ;; Only string (and %) specs (XEmacs only!)
698       ((and (featurep 'xemacs)
699             gnus-make-format-preserve-properties
700             (string-match
701              "\\`\\([^%]*\\(%%\\|%-?\\([1-9][0-9]*\\)?s\\)\\)*[^%]*\\'"
702              fstring))
703        (list (cons 'gnus-xmas-format (cons fstring (nreverse flist)))))
704       ;; A more complex spec.
705       (t
706        (list (cons 'format (cons fstring (nreverse flist)))))))
707
708     (if insert
709         (when result
710           (if dontinsert
711               result
712             (cons 'insert result)))
713       (cond ((stringp result)
714              result)
715             ((consp result)
716              (cons 'concat result))
717             (t "")))))
718
719 (defun gnus-eval-format (format &optional alist props)
720   "Eval the format variable FORMAT, using ALIST.
721 If PROPS, insert the result."
722   (let ((form (gnus-parse-format format alist props)))
723     (if props
724         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
725       (eval form))))
726
727 (defun gnus-set-format (type &optional insertable)
728   (set (intern (format "gnus-%s-line-format-spec" type))
729        (gnus-parse-format
730         (symbol-value (intern (format "gnus-%s-line-format" type)))
731         (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
732         insertable)))
733
734 (provide 'gnus-spec)
735
736 ;; Local Variables:
737 ;; coding: utf-8
738 ;; End:
739
740 ;;; gnus-spec.el ends here