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