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