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