gmm-utils.el (gmm-flet, gmm-labels): New macros.
[gnus] / lisp / gnus-spec.el
1 ;;; gnus-spec.el --- format spec functions for Gnus
2
3 ;; Copyright (C) 1996-2012 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     '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
269
270 (defun gnus-balloon-face-function (form type)
271   `(gnus-put-text-property
272     (point) (progn ,@form (point))
273     ,(if (fboundp 'balloon-help-mode)
274          ''balloon-help
275        ''help-echo)
276     ,(intern (format "gnus-balloon-face-%d" type))))
277
278 (defun gnus-spec-tab (column)
279   (if (> column 0)
280       `(insert-char ?  (max (- ,column (current-column)) 0))
281     (let ((column (abs column)))
282       `(if (> (current-column) ,column)
283            (let ((end (point)))
284              (if (= (move-to-column ,column) ,column)
285                  (delete-region (point) end)
286                (delete-region (1- (point)) end)
287                (insert " ")))
288          (insert-char ?  (max (- ,column (current-column)) 0))))))
289
290 (defun gnus-correct-length (string)
291   "Return the correct width of STRING."
292   (apply #'+ (mapcar #'char-width string)))
293
294 (defun gnus-correct-substring (string start &optional end)
295   (let ((wstart 0)
296         (wend 0)
297         (wseek 0)
298         (seek 0)
299         (length (length string))
300         (string (concat string "\0")))
301     ;; Find the start position.
302     (while (and (< seek length)
303                 (< wseek start))
304       (incf wseek (char-width (aref string seek)))
305       (incf seek))
306     (setq wstart seek)
307     ;; Find the end position.
308     (while (and (<= seek length)
309                 (or (not end)
310                     (<= wseek end)))
311       (incf wseek (char-width (aref string seek)))
312       (incf seek))
313     (setq wend seek)
314     (substring string wstart (1- wend))))
315
316 (defun gnus-string-width-function ()
317   (cond
318    (gnus-use-correct-string-widths
319     'gnus-correct-length)
320    ((fboundp 'string-width)
321     'string-width)
322    (t
323     'length)))
324
325 (defun gnus-substring-function ()
326   (cond
327    (gnus-use-correct-string-widths
328     'gnus-correct-substring)
329    ((fboundp 'string-width)
330     'gnus-correct-substring)
331    (t
332     'substring)))
333
334 (defun gnus-tilde-max-form (el max-width)
335   "Return a form that limits EL to MAX-WIDTH."
336   (let ((max (abs max-width))
337         (length-fun (gnus-string-width-function))
338         (substring-fun (gnus-substring-function)))
339     (if (symbolp el)
340         `(if (> (,length-fun ,el) ,max)
341              ,(if (< max-width 0)
342                   `(,substring-fun ,el (- (,length-fun ,el) ,max))
343                 `(if (gnus-lrm-string-p ,el)
344                      (concat (,substring-fun ,el 0 ,max) ,gnus-lrm-string)
345                    (,substring-fun ,el 0 ,max)))
346            ,el)
347       `(let ((val (eval ,el)))
348          (if (> (,length-fun val) ,max)
349              ,(if (< max-width 0)
350                   `(,substring-fun val (- (,length-fun val) ,max))
351                 `(if (gnus-lrm-string-p val)
352                      (concat (,substring-fun val 0 ,max) ,gnus-lrm-string)
353                    (,substring-fun val 0 ,max)))
354            val)))))
355
356 (defun gnus-tilde-cut-form (el cut-width)
357   "Return a form that cuts CUT-WIDTH off of EL."
358   (let ((cut (abs cut-width))
359         (length-fun (gnus-string-width-function))
360         (substring-fun (gnus-substring-function)))
361     (if (symbolp el)
362         `(if (> (,length-fun ,el) ,cut)
363              ,(if (< cut-width 0)
364                   `(,substring-fun ,el 0 (- (,length-fun ,el) ,cut))
365                 `(,substring-fun ,el ,cut))
366            ,el)
367       `(let ((val (eval ,el)))
368          (if (> (,length-fun val) ,cut)
369              ,(if (< cut-width 0)
370                   `(,substring-fun val 0 (- (,length-fun val) ,cut))
371                 `(,substring-fun val ,cut))
372            val)))))
373
374 (defun gnus-tilde-ignore-form (el ignore-value)
375   "Return a form that is blank when EL is IGNORE-VALUE."
376   (if (symbolp el)
377       `(if (equal ,el ,ignore-value)
378            "" ,el)
379     `(let ((val (eval ,el)))
380        (if (equal val ,ignore-value)
381            "" val))))
382
383 (defun gnus-pad-form (el pad-width)
384   "Return a form that pads EL to PAD-WIDTH accounting for multi-column
385 characters correctly. This is because `format' may pad to columns or to
386 characters when given a pad value."
387   (let ((pad (abs pad-width))
388         (side (< 0 pad-width))
389         (length-fun (gnus-string-width-function)))
390     (if (symbolp el)
391         `(let ((need (- ,pad (,length-fun ,el))))
392            (if (> need 0)
393                (concat ,(when side '(make-string need ?\ ))
394                        ,el
395                        ,(when (not side) '(make-string need ?\ )))
396              ,el))
397       `(let* ((val (eval ,el))
398               (need (- ,pad (,length-fun val))))
399          (if (> need 0)
400              (concat ,(when side '(make-string need ?\ ))
401                      val
402                      ,(when (not side) '(make-string need ?\ )))
403            val)))))
404
405 (defun gnus-parse-format (format spec-alist &optional insert)
406   ;; This function parses the FORMAT string with the help of the
407   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
408   ;; string.  If the FORMAT string contains the specifiers %( and %)
409   ;; the text between them will have the mouse-face text property.
410   ;; If the FORMAT string contains the specifiers %[ and %], the text between
411   ;; them will have the balloon-help text property.
412   (let ((case-fold-search nil))
413     (if (string-match
414          "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'\\|%[-0-9]*=\\|%[-0-9]*\\*"
415          format)
416         (gnus-parse-complex-format format spec-alist)
417       ;; This is a simple format.
418       (gnus-parse-simple-format format spec-alist insert))))
419
420 (defun gnus-parse-complex-format (format spec-alist)
421   (let ((cursor-spec nil))
422     (save-excursion
423       (gnus-set-work-buffer)
424       (insert format)
425       (goto-char (point-min))
426       (while (re-search-forward "\"" nil t)
427         (replace-match "\\\"" nil t))
428       (goto-char (point-min))
429       (insert "(\"")
430       ;; Convert all font specs into font spec lists.
431       (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
432         (let ((number (if (match-beginning 1)
433                           (match-string 1) "0"))
434               (delim (aref (match-string 2) 0)))
435           (if (or (= delim ?\()
436                   (= delim ?\{)
437                   (= delim ?\«))
438               (replace-match (concat "\"("
439                                      (cond ((= delim ?\() "mouse")
440                                            ((= delim ?\{) "face")
441                                            (t "balloon"))
442                                      " " number " \"")
443                              t t)
444             (replace-match "\")\""))))
445       (goto-char (point-max))
446       (insert "\")")
447       ;; Convert point position commands.
448       (goto-char (point-min))
449       (let ((case-fold-search nil))
450         (while (re-search-forward "%\\([-0-9]+\\)?\\*" nil t)
451           (replace-match "\"(point)\"" t t)
452           (setq cursor-spec t)))
453       ;; Convert TAB commands.
454       (goto-char (point-min))
455       (while (re-search-forward "%\\([-0-9]+\\)=" nil t)
456         (replace-match (format "\"(tab %s)\"" (match-string 1)) t t))
457       ;; Convert the buffer into the spec.
458       (goto-char (point-min))
459       (let ((form (read (current-buffer))))
460         (if cursor-spec
461             `(let (gnus-position)
462                ,@(gnus-complex-form-to-spec form spec-alist)
463                (if gnus-position
464                    (gnus-put-text-property gnus-position (1+ gnus-position)
465                                            'gnus-position t)))
466           `(progn
467              ,@(gnus-complex-form-to-spec form spec-alist)))))))
468
469 (defun gnus-complex-form-to-spec (form spec-alist)
470   (delq nil
471         (mapcar
472          (lambda (sform)
473            (cond
474             ((stringp sform)
475              (gnus-parse-simple-format sform spec-alist t))
476             ((eq (car sform) 'point)
477              '(setq gnus-position (point)))
478             ((eq (car sform) 'tab)
479              (gnus-spec-tab (cadr sform)))
480             (t
481              (funcall (intern (format "gnus-%s-face-function" (car sform)))
482                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
483                       (nth 1 sform)))))
484          form)))
485
486
487 (defun gnus-xmas-format (fstring &rest args)
488   "A version of `format' which preserves text properties.
489
490 Required for XEmacs, where the built in `format' function strips all text
491 properties from both the format string and any inserted strings.
492
493 Only supports the format sequence %s, and %% for inserting
494 literal % characters. A pad width and an optional - (to right pad)
495 are supported for %s."
496   (let ((re "%%\\|%\\(-\\)?\\([1-9][0-9]*\\)?s")
497         (n (length args)))
498     (with-temp-buffer
499       (insert fstring)
500       (goto-char (point-min))
501       (while (re-search-forward re nil t)
502         (goto-char (match-end 0))
503         (cond
504          ((string= (match-string 0) "%%")
505           (delete-char -1))
506          (t
507           (if (null args)
508               (error 'wrong-number-of-arguments #'my-format n fstring))
509           (let* ((minlen (string-to-number (or (match-string 2) "")))
510                  (arg (car args))
511                  (str (if (stringp arg) arg (format "%s" arg)))
512                  (lpad (null (match-string 1)))
513                  (padlen (max 0 (- minlen (length str)))))
514             (replace-match "")
515             (if lpad (insert-char ?\  padlen))
516             (insert str)
517             (unless lpad (insert-char ?\  padlen))
518             (setq args (cdr args))))))
519       (buffer-string))))
520
521 (defun gnus-parse-simple-format (format spec-alist &optional insert)
522   ;; This function parses the FORMAT string with the help of the
523   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
524   ;; string.
525   (let ((max-width 0)
526         spec flist fstring elem result dontinsert user-defined
527         type value pad-width spec-beg cut-width ignore-value
528         tilde-form tilde elem-type extended-spec)
529     (save-excursion
530       (gnus-set-work-buffer)
531       (insert format)
532       (goto-char (point-min))
533       (while (re-search-forward "%" nil t)
534         (setq user-defined nil
535               spec-beg nil
536               pad-width nil
537               max-width nil
538               cut-width nil
539               ignore-value nil
540               tilde-form nil
541               extended-spec nil)
542         (setq spec-beg (1- (point)))
543
544         ;; Parse this spec fully.
545         (while
546             (cond
547              ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
548               (setq pad-width (string-to-number (match-string 1)))
549               (when (match-beginning 2)
550                 (setq max-width (string-to-number (buffer-substring
551                                                    (1+ (match-beginning 2))
552                                                    (match-end 2)))))
553               (goto-char (match-end 0)))
554              ((looking-at "~")
555               (forward-char 1)
556               (setq tilde (read (current-buffer))
557                     type (car tilde)
558                     value (cadr tilde))
559               (cond
560                ((memq type '(pad pad-left))
561                 (setq pad-width value))
562                ((eq type 'pad-right)
563                 (setq pad-width (- value)))
564                ((memq type '(max-right max))
565                 (setq max-width value))
566                ((eq type 'max-left)
567                 (setq max-width (- value)))
568                ((memq type '(cut cut-left))
569                 (setq cut-width value))
570                ((eq type 'cut-right)
571                 (setq cut-width (- value)))
572                ((eq type 'ignore)
573                 (setq ignore-value
574                       (if (stringp value) value (format "%s" value))))
575                ((eq type 'form)
576                 (setq tilde-form value))
577                (t
578                 (error "Unknown tilde type: %s" tilde)))
579               t)
580              (t
581               nil)))
582         (cond
583          ;; User-defined spec -- find the spec name.
584          ((eq (setq spec (char-after)) ?u)
585           (forward-char 1)
586           (when (and (eq (setq user-defined (char-after)) ?&)
587                      (looking-at "&\\([^;]+\\);"))
588             (setq user-defined (match-string 1))
589             (goto-char (match-end 1))))
590          ;; extended spec
591          ((and (eq spec ?&) (looking-at "&\\([^;]+\\);"))
592           (setq extended-spec (intern (match-string 1)))
593           (goto-char (match-end 1))))
594         (forward-char 1)
595         (delete-region spec-beg (point))
596
597         ;; Now we have all the relevant data on this spec, so
598         ;; we start doing stuff.
599         (insert "%")
600         (if (eq spec ?%)
601             ;; "%%" just results in a "%".
602             (insert "%")
603           (cond
604            ;; Do tilde forms.
605            ((eq spec ?@)
606             (setq elem (list tilde-form ?s)))
607            ;; Treat user defined format specifiers specially.
608            (user-defined
609             (setq elem
610                   (list
611                    (list (intern (format
612                                   (if (stringp user-defined)
613                                       "gnus-user-format-function-%s"
614                                     "gnus-user-format-function-%c")
615                                   user-defined))
616                          'gnus-tmp-header)
617                    ?s)))
618            ;; Find the specification from `spec-alist'.
619            ((setq elem (cdr (assq (or extended-spec spec) spec-alist))))
620            ;; We used to use "%l" for displaying the grouplens score.
621            ((eq spec ?l)
622             (setq elem '("" ?s)))
623            (t
624             (setq elem '("*" ?s))))
625           (setq elem-type (cadr elem))
626           ;; Insert the new format elements.
627           (when (and pad-width
628                      (not (and (featurep 'xemacs)
629                                gnus-use-correct-string-widths)))
630             (insert (number-to-string pad-width)))
631           ;; Create the form to be evalled.
632           (if (or max-width cut-width ignore-value
633                   (and (featurep 'xemacs)
634                        gnus-use-correct-string-widths))
635               (progn
636                 (insert ?s)
637                 (let ((el (car elem)))
638                   (cond ((= (cadr elem) ?c)
639                          (setq el (list 'char-to-string el)))
640                         ((= (cadr elem) ?d)
641                          (setq el (list 'int-to-string el))))
642                   (when ignore-value
643                     (setq el (gnus-tilde-ignore-form el ignore-value)))
644                   (when cut-width
645                     (setq el (gnus-tilde-cut-form el cut-width)))
646                   (when max-width
647                     (setq el (gnus-tilde-max-form el max-width)))
648                   (when pad-width
649                     (setq el (gnus-pad-form el pad-width)))
650                   (push el flist)))
651             (insert elem-type)
652             (push (car elem) flist))))
653       (setq fstring (buffer-substring-no-properties (point-min) (point-max))))
654
655     ;; Do some postprocessing to increase efficiency.
656     (setq
657      result
658      (cond
659       ;; Emptiness.
660       ((string= fstring "")
661        nil)
662       ;; Not a format string.
663       ((not (string-match "%" fstring))
664        (list fstring))
665       ;; A format string with just a single string spec.
666       ((string= fstring "%s")
667        (list (car flist)))
668       ;; A single character.
669       ((string= fstring "%c")
670        (list (car flist)))
671       ;; A single number.
672       ((string= fstring "%d")
673        (setq dontinsert t)
674        (if insert
675            `(insert (int-to-string ,(car flist)))
676          (list `(int-to-string ,(car flist)))))
677       ;; Just lots of chars and strings.
678       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
679        (nreverse flist))
680       ;; A single string spec at the beginning of the spec.
681       ((string-match "\\`%[sc][^%]+\\'" fstring)
682        (list (car flist) (substring fstring 2)))
683       ;; A single string spec in the middle of the spec.
684       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
685        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
686       ;; A single string spec in the end of the spec.
687       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
688        (list (match-string 1 fstring) (car flist)))
689       ;; Only string (and %) specs (XEmacs only!)
690       ((and (featurep 'xemacs)
691             gnus-make-format-preserve-properties
692             (string-match
693              "\\`\\([^%]*\\(%%\\|%-?\\([1-9][0-9]*\\)?s\\)\\)*[^%]*\\'"
694              fstring))
695        (list (cons 'gnus-xmas-format (cons fstring (nreverse flist)))))
696       ;; A more complex spec.
697       (t
698        (list (cons 'format (cons fstring (nreverse flist)))))))
699
700     (if insert
701         (when result
702           (if dontinsert
703               result
704             (cons 'insert result)))
705       (cond ((stringp result)
706              result)
707             ((consp result)
708              (cons 'concat result))
709             (t "")))))
710
711 (defun gnus-eval-format (format &optional alist props)
712   "Eval the format variable FORMAT, using ALIST.
713 If PROPS, insert the result."
714   (let ((form (gnus-parse-format format alist props)))
715     (if props
716         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
717       (eval form))))
718
719 (defun gnus-set-format (type &optional insertable)
720   (set (intern (format "gnus-%s-line-format-spec" type))
721        (gnus-parse-format
722         (symbol-value (intern (format "gnus-%s-line-format" type)))
723         (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
724         insertable)))
725
726 (provide 'gnus-spec)
727
728 ;; Local Variables:
729 ;; coding: iso-8859-1
730 ;; End:
731
732 ;;; gnus-spec.el ends here