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