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