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