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