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