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