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