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