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