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