cd8a36fff02c45c90cdf7ac35013249d22ebb120
[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
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., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, 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 "21.4"
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 "21.4"
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   (let ((spec (assq 'group gnus-format-specs)))
201     (when (and (memq 'group types)
202                (string-match " gnus-tmp-grouplens[ )]"
203                              (gnus-prin1-to-string (cdr spec))))
204       (setq gnus-format-specs (delq spec gnus-format-specs))))
205
206   ;; Go through all the formats and see whether they need updating.
207   (let (new-format entry type val updated)
208     (while (setq type (pop types))
209       ;; Jump to the proper buffer to find out the value of the
210       ;; variable, if possible.  (It may be buffer-local.)
211       (save-excursion
212         (let ((buffer (intern (format "gnus-%s-buffer" type))))
213           (when (and (boundp buffer)
214                      (setq val (symbol-value buffer))
215                      (gnus-buffer-exists-p val))
216             (set-buffer val))
217           (setq new-format (symbol-value
218                             (intern (format "gnus-%s-line-format" type)))))
219         (setq entry (cdr (assq type gnus-format-specs)))
220         (if (and (car entry)
221                  (equal (car entry) new-format))
222             ;; Use the old format.
223             (set (intern (format "gnus-%s-line-format-spec" type))
224                  (cadr entry))
225           ;; This is a new format.
226           (setq val
227                 (if (not (stringp new-format))
228                     ;; This is a function call or something.
229                     new-format
230                   ;; This is a "real" format.
231                   (gnus-parse-format
232                    new-format
233                    (symbol-value
234                     (intern (format "gnus-%s-line-format-alist" type)))
235                    (not (string-match "mode$" (symbol-name type))))))
236           ;; Enter the new format spec into the list.
237           (if entry
238               (progn
239                 (setcar (cdr entry) val)
240                 (setcar entry new-format))
241             (push (list type new-format val) gnus-format-specs))
242           (set (intern (format "gnus-%s-line-format-spec" type)) val)
243           (push type updated))))
244
245     (unless (assq 'version gnus-format-specs)
246       (push (cons 'version emacs-version) gnus-format-specs))
247     updated))
248
249 (defvar gnus-mouse-face-0 'highlight)
250 (defvar gnus-mouse-face-1 'highlight)
251 (defvar gnus-mouse-face-2 'highlight)
252 (defvar gnus-mouse-face-3 'highlight)
253 (defvar gnus-mouse-face-4 'highlight)
254
255 (defun gnus-mouse-face-function (form type)
256   `(gnus-put-text-property
257     (point) (progn ,@form (point))
258     gnus-mouse-face-prop
259     ,(if (equal type 0)
260          'gnus-mouse-face
261        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
262
263 (defvar gnus-face-0 'bold)
264 (defvar gnus-face-1 'italic)
265 (defvar gnus-face-2 'bold-italic)
266 (defvar gnus-face-3 'bold)
267 (defvar gnus-face-4 'bold)
268
269 (defun gnus-face-face-function (form type)
270   `(gnus-add-text-properties
271     (point) (progn ,@form (point))
272     '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
273
274 (defun gnus-balloon-face-function (form type)
275   `(gnus-put-text-property
276     (point) (progn ,@form (point))
277     ,(if (fboundp 'balloon-help-mode)
278          ''balloon-help
279        ''help-echo)
280     ,(intern (format "gnus-balloon-face-%d" type))))
281
282 (defun gnus-spec-tab (column)
283   (if (> column 0)
284       `(insert (make-string (max (- ,column (current-column)) 0) ? ))
285     (let ((column (abs column)))
286       (if gnus-use-correct-string-widths
287           `(progn
288              (if (> (current-column) ,column)
289                  (while (progn
290                           (delete-backward-char 1)
291                           (> (current-column) ,column))))
292              (insert (make-string (max (- ,column (current-column)) 0) ? )))
293         `(progn
294            (if (> (current-column) ,column)
295                (delete-region (point)
296                               (- (point) (- (current-column) ,column)))
297              (insert (make-string (max (- ,column (current-column)) 0)
298                                   ? ))))))))
299
300 (defun gnus-correct-length (string)
301   "Return the correct width of STRING."
302   (let ((length 0))
303     (mapcar (lambda (char) (incf length (char-width char))) string)
304     length))
305
306 (defun gnus-correct-substring (string start &optional end)
307   (let ((wstart 0)
308         (wend 0)
309         (wseek 0)
310         (seek 0)
311         (length (length string))
312         (string (concat string "\0")))
313     ;; Find the start position.
314     (while (and (< seek length)
315                 (< wseek start))
316       (incf wseek (char-width (aref string seek)))
317       (incf seek))
318     (setq wstart seek)
319     ;; Find the end position.
320     (while (and (<= seek length)
321                 (or (not end)
322                     (<= wseek end)))
323       (incf wseek (char-width (aref string seek)))
324       (incf seek))
325     (setq wend seek)
326     (substring string wstart (1- wend))))
327
328 (defun gnus-string-width-function ()
329   (cond
330    (gnus-use-correct-string-widths
331     'gnus-correct-length)
332    ((fboundp 'string-width)
333     'string-width)
334    (t
335     'length)))
336
337 (defun gnus-substring-function ()
338   (cond
339    (gnus-use-correct-string-widths
340     'gnus-correct-substring)
341    ((fboundp 'string-width)
342     'gnus-correct-substring)
343    (t
344     'substring)))
345
346 (defun gnus-tilde-max-form (el max-width)
347   "Return a form that limits EL to MAX-WIDTH."
348   (let ((max (abs max-width))
349         (length-fun (gnus-string-width-function))
350         (substring-fun (gnus-substring-function)))
351     (if (symbolp el)
352         `(if (> (,length-fun ,el) ,max)
353              ,(if (< max-width 0)
354                   `(,substring-fun ,el (- (,length-fun ,el) ,max))
355                 `(,substring-fun ,el 0 ,max))
356            ,el)
357       `(let ((val (eval ,el)))
358          (if (> (,length-fun val) ,max)
359              ,(if (< max-width 0)
360                   `(,substring-fun val (- (,length-fun val) ,max))
361                 `(,substring-fun val 0 ,max))
362            val)))))
363
364 (defun gnus-tilde-cut-form (el cut-width)
365   "Return a form that cuts CUT-WIDTH off of EL."
366   (let ((cut (abs cut-width))
367         (length-fun (gnus-string-width-function))
368         (substring-fun (gnus-substring-function)))
369     (if (symbolp el)
370         `(if (> (,length-fun ,el) ,cut)
371              ,(if (< cut-width 0)
372                   `(,substring-fun ,el 0 (- (,length-fun ,el) ,cut))
373                 `(,substring-fun ,el ,cut))
374            ,el)
375       `(let ((val (eval ,el)))
376          (if (> (,length-fun val) ,cut)
377              ,(if (< cut-width 0)
378                   `(,substring-fun val 0 (- (,length-fun val) ,cut))
379                 `(,substring-fun val ,cut))
380            val)))))
381
382 (defun gnus-tilde-ignore-form (el ignore-value)
383   "Return a form that is blank when EL is IGNORE-VALUE."
384   (if (symbolp el)
385       `(if (equal ,el ,ignore-value)
386            "" ,el)
387     `(let ((val (eval ,el)))
388        (if (equal val ,ignore-value)
389            "" val))))
390
391 (defun gnus-pad-form (el pad-width)
392   "Return a form that pads EL to PAD-WIDTH accounting for multi-column
393 characters correctly. This is because `format' may pad to columns or to
394 characters when given a pad value."
395   (let ((pad (abs pad-width))
396         (side (< 0 pad-width))
397         (length-fun (gnus-string-width-function)))
398     (if (symbolp el)
399         `(let ((need (- ,pad (,length-fun ,el))))
400            (if (> need 0)
401                (concat ,(when side '(make-string need ?\ ))
402                        ,el
403                        ,(when (not side) '(make-string need ?\ )))
404              ,el))
405       `(let* ((val (eval ,el))
406               (need (- ,pad (,length-fun val))))
407          (if (> need 0)
408              (concat ,(when side '(make-string need ?\ ))
409                      val
410                      ,(when (not side) '(make-string need ?\ )))
411            val)))))
412
413 (defun gnus-parse-format (format spec-alist &optional insert)
414   ;; This function parses the FORMAT string with the help of the
415   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
416   ;; string.  If the FORMAT string contains the specifiers %( and %)
417   ;; the text between them will have the mouse-face text property.
418   ;; If the FORMAT string contains the specifiers %[ and %], the text between
419   ;; them will have the balloon-help text property.
420   (let ((case-fold-search nil))
421     (if (string-match
422          "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'\\|%[-0-9]*=\\|%[-0-9]*\\*"
423          format)
424         (gnus-parse-complex-format format spec-alist)
425       ;; This is a simple format.
426       (gnus-parse-simple-format format spec-alist insert))))
427
428 (defun gnus-parse-complex-format (format spec-alist)
429   (let ((cursor-spec nil))
430     (save-excursion
431       (gnus-set-work-buffer)
432       (insert format)
433       (goto-char (point-min))
434       (while (re-search-forward "\"" nil t)
435         (replace-match "\\\"" nil t))
436       (goto-char (point-min))
437       (insert "(\"")
438       ;; Convert all font specs into font spec lists.
439       (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
440         (let ((number (if (match-beginning 1)
441                           (match-string 1) "0"))
442               (delim (aref (match-string 2) 0)))
443           (if (or (= delim ?\()
444                   (= delim ?\{)
445                   (= delim ?\«))
446               (replace-match (concat "\"("
447                                      (cond ((= delim ?\() "mouse")
448                                            ((= delim ?\{) "face")
449                                            (t "balloon"))
450                                      " " number " \"")
451                              t t)
452             (replace-match "\")\""))))
453       (goto-char (point-max))
454       (insert "\")")
455       ;; Convert point position commands.
456       (goto-char (point-min))
457       (let ((case-fold-search nil))
458         (while (re-search-forward "%\\([-0-9]+\\)?\\*" nil t)
459           (replace-match "\"(point)\"" t t)
460           (setq cursor-spec t)))
461       ;; Convert TAB commands.
462       (goto-char (point-min))
463       (while (re-search-forward "%\\([-0-9]+\\)=" nil t)
464         (replace-match (format "\"(tab %s)\"" (match-string 1)) t t))
465       ;; Convert the buffer into the spec.
466       (goto-char (point-min))
467       (let ((form (read (current-buffer))))
468         (if cursor-spec
469             `(let (gnus-position)
470                ,@(gnus-complex-form-to-spec form spec-alist)
471                (if gnus-position
472                    (gnus-put-text-property gnus-position (1+ gnus-position)
473                                            'gnus-position t)))
474           `(progn
475              ,@(gnus-complex-form-to-spec form spec-alist)))))))
476
477 (defun gnus-complex-form-to-spec (form spec-alist)
478   (delq nil
479         (mapcar
480          (lambda (sform)
481            (cond
482             ((stringp sform)
483              (gnus-parse-simple-format sform spec-alist t))
484             ((eq (car sform) 'point)
485              '(setq gnus-position (point)))
486             ((eq (car sform) 'tab)
487              (gnus-spec-tab (cadr sform)))
488             (t
489              (funcall (intern (format "gnus-%s-face-function" (car sform)))
490                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
491                       (nth 1 sform)))))
492          form)))
493
494
495 (defun gnus-xmas-format (fstring &rest args)
496   "A version of `format' which preserves text properties.
497
498 Required for XEmacs, where the built in `format' function strips all text
499 properties from both the format string and any inserted strings.
500
501 Only supports the format sequence %s, and %% for inserting
502 literal % characters. A pad width and an optional - (to right pad)
503 are supported for %s."
504   (let ((re "%%\\|%\\(-\\)?\\([1-9][0-9]*\\)?s")
505         (n (length args)))
506     (with-temp-buffer
507       (insert fstring)
508       (goto-char (point-min))
509       (while (re-search-forward re nil t)
510         (goto-char (match-end 0))
511         (cond
512          ((string= (match-string 0) "%%")
513           (delete-char -1))
514          (t
515           (if (null args)
516               (error 'wrong-number-of-arguments #'my-format n fstring))
517           (let* ((minlen (string-to-int (or (match-string 2) "")))
518                  (arg (car args))
519                  (str (if (stringp arg) arg (format "%s" arg)))
520                  (lpad (null (match-string 1)))
521                  (padlen (max 0 (- minlen (length str)))))
522             (replace-match "")
523             (if lpad (insert-char ?\  padlen))
524             (insert str)
525             (unless lpad (insert-char ?\  padlen))
526             (setq args (cdr args))))))
527       (buffer-string))))
528
529 (defun gnus-parse-simple-format (format spec-alist &optional insert)
530   ;; This function parses the FORMAT string with the help of the
531   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
532   ;; string.
533   (let ((max-width 0)
534         spec flist fstring elem result dontinsert user-defined
535         type value pad-width spec-beg cut-width ignore-value
536         tilde-form tilde elem-type extended-spec)
537     (save-excursion
538       (gnus-set-work-buffer)
539       (insert format)
540       (goto-char (point-min))
541       (while (re-search-forward "%" nil t)
542         (setq user-defined nil
543               spec-beg nil
544               pad-width nil
545               max-width nil
546               cut-width nil
547               ignore-value nil
548               tilde-form nil
549               extended-spec nil)
550         (setq spec-beg (1- (point)))
551
552         ;; Parse this spec fully.
553         (while
554             (cond
555              ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
556               (setq pad-width (string-to-number (match-string 1)))
557               (when (match-beginning 2)
558                 (setq max-width (string-to-number (buffer-substring
559                                                    (1+ (match-beginning 2))
560                                                    (match-end 2)))))
561               (goto-char (match-end 0)))
562              ((looking-at "~")
563               (forward-char 1)
564               (setq tilde (read (current-buffer))
565                     type (car tilde)
566                     value (cadr tilde))
567               (cond
568                ((memq type '(pad pad-left))
569                 (setq pad-width value))
570                ((eq type 'pad-right)
571                 (setq pad-width (- value)))
572                ((memq type '(max-right max))
573                 (setq max-width value))
574                ((eq type 'max-left)
575                 (setq max-width (- value)))
576                ((memq type '(cut cut-left))
577                 (setq cut-width value))
578                ((eq type 'cut-right)
579                 (setq cut-width (- value)))
580                ((eq type 'ignore)
581                 (setq ignore-value
582                       (if (stringp value) value (format "%s" value))))
583                ((eq type 'form)
584                 (setq tilde-form value))
585                (t
586                 (error "Unknown tilde type: %s" tilde)))
587               t)
588              (t
589               nil)))
590         (cond
591          ;; User-defined spec -- find the spec name.
592          ((eq (setq spec (char-after)) ?u)
593           (forward-char 1)
594           (when (and (eq (setq user-defined (char-after)) ?&)
595                      (looking-at "&\\([^;]+\\);"))
596             (setq user-defined (match-string 1))
597             (goto-char (match-end 1))))
598          ;; extended spec
599          ((and (eq spec ?&) (looking-at "&\\([^;]+\\);"))
600           (setq extended-spec (intern (match-string 1)))
601           (goto-char (match-end 1))))
602         (forward-char 1)
603         (delete-region spec-beg (point))
604
605         ;; Now we have all the relevant data on this spec, so
606         ;; we start doing stuff.
607         (insert "%")
608         (if (eq spec ?%)
609             ;; "%%" just results in a "%".
610             (insert "%")
611           (cond
612            ;; Do tilde forms.
613            ((eq spec ?@)
614             (setq elem (list tilde-form ?s)))
615            ;; Treat user defined format specifiers specially.
616            (user-defined
617             (setq elem
618                   (list
619                    (list (intern (format
620                                   (if (stringp user-defined)
621                                       "gnus-user-format-function-%s"
622                                     "gnus-user-format-function-%c")
623                                   user-defined))
624                          'gnus-tmp-header)
625                    ?s)))
626            ;; Find the specification from `spec-alist'.
627            ((setq elem (cdr (assq (or extended-spec spec) spec-alist))))
628            ;; We used to use "%l" for displaying the grouplens score.
629            ((eq spec ?l)
630             (setq elem '("" ?s)))
631            (t
632             (setq elem '("*" ?s))))
633           (setq elem-type (cadr elem))
634           ;; Insert the new format elements.
635           (when (and pad-width
636                      (not (and (featurep 'xemacs)
637                                gnus-use-correct-string-widths)))
638             (insert (number-to-string pad-width)))
639           ;; Create the form to be evaled.
640           (if (or max-width cut-width ignore-value
641                   (and (featurep 'xemacs)
642                        gnus-use-correct-string-widths))
643               (progn
644                 (insert ?s)
645                 (let ((el (car elem)))
646                   (cond ((= (cadr elem) ?c)
647                          (setq el (list 'char-to-string el)))
648                         ((= (cadr elem) ?d)
649                          (setq el (list 'int-to-string el))))
650                   (when ignore-value
651                     (setq el (gnus-tilde-ignore-form el ignore-value)))
652                   (when cut-width
653                     (setq el (gnus-tilde-cut-form el cut-width)))
654                   (when max-width
655                     (setq el (gnus-tilde-max-form el max-width)))
656                   (when pad-width
657                     (setq el (gnus-pad-form el pad-width)))
658                   (push el flist)))
659             (insert elem-type)
660             (push (car elem) flist))))
661       (setq fstring (buffer-substring-no-properties (point-min) (point-max))))
662
663     ;; Do some postprocessing to increase efficiency.
664     (setq
665      result
666      (cond
667       ;; Emptiness.
668       ((string= fstring "")
669        nil)
670       ;; Not a format string.
671       ((not (string-match "%" fstring))
672        (list fstring))
673       ;; A format string with just a single string spec.
674       ((string= fstring "%s")
675        (list (car flist)))
676       ;; A single character.
677       ((string= fstring "%c")
678        (list (car flist)))
679       ;; A single number.
680       ((string= fstring "%d")
681        (setq dontinsert t)
682        (if insert
683            (list `(princ ,(car flist)))
684          (list `(int-to-string ,(car flist)))))
685       ;; Just lots of chars and strings.
686       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
687        (nreverse flist))
688       ;; A single string spec at the beginning of the spec.
689       ((string-match "\\`%[sc][^%]+\\'" fstring)
690        (list (car flist) (substring fstring 2)))
691       ;; A single string spec in the middle of the spec.
692       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
693        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
694       ;; A single string spec in the end of the spec.
695       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
696        (list (match-string 1 fstring) (car flist)))
697       ;; Only string (and %) specs (XEmacs only!)
698       ((and (featurep 'xemacs)
699             gnus-make-format-preserve-properties
700             (string-match
701              "\\`\\([^%]*\\(%%\\|%-?\\([1-9][0-9]*\\)?s\\)\\)*[^%]*\\'"
702              fstring))
703        (list (cons 'gnus-xmas-format (cons fstring (nreverse flist)))))
704       ;; A more complex spec.
705       (t
706        (list (cons 'format (cons fstring (nreverse flist)))))))
707
708     (if insert
709         (when result
710           (if dontinsert
711               result
712             (cons 'insert result)))
713       (cond ((stringp result)
714              result)
715             ((consp result)
716              (cons 'concat result))
717             (t "")))))
718
719 (defun gnus-eval-format (format &optional alist props)
720   "Eval the format variable FORMAT, using ALIST.
721 If PROPS, insert the result."
722   (let ((form (gnus-parse-format format alist props)))
723     (if props
724         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
725       (eval form))))
726
727 (defun gnus-compile ()
728   "Byte-compile the user-defined format specs."
729   (interactive)
730   (require 'bytecomp)
731   (let ((entries gnus-format-specs)
732         (byte-compile-warnings '(unresolved callargs redefine))
733         entry gnus-tmp-func)
734     (save-excursion
735       (gnus-message 7 "Compiling format specs...")
736
737       (while entries
738         (setq entry (pop entries))
739         (if (memq (car entry) '(gnus-version version))
740             (setq gnus-format-specs (delq entry gnus-format-specs))
741           (let ((form (caddr entry)))
742             (when (and (listp form)
743                        ;; Under GNU Emacs, it's (byte-code ...)
744                        (not (eq 'byte-code (car form)))
745                        ;; Under XEmacs, it's (funcall #<compiled-function ...>)
746                        (not (and (eq 'funcall (car form))
747                                  (byte-code-function-p (cadr form)))))
748               (defalias 'gnus-tmp-func `(lambda () ,form))
749               (byte-compile 'gnus-tmp-func)
750               (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func))))))
751
752       (push (cons 'version emacs-version) gnus-format-specs)
753       ;; Mark the .newsrc.eld file as "dirty".
754       (gnus-dribble-touch)
755       (gnus-message 7 "Compiling user specs...done"))))
756
757 (defun gnus-set-format (type &optional insertable)
758   (set (intern (format "gnus-%s-line-format-spec" type))
759        (gnus-parse-format
760         (symbol-value (intern (format "gnus-%s-line-format" type)))
761         (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
762         insertable)))
763
764 (provide 'gnus-spec)
765
766 ;; Local Variables:
767 ;; coding: iso-8859-1
768 ;; End:
769
770 ;;; arch-tag: a4328fa1-1f84-4b09-97ad-4b5767cfd50f
771 ;;; gnus-spec.el ends here