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