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