Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / gnus / gnus-spec.el
blob54714d503bc75c2fea555f008d2e74fca5a42339
1 ;;; gnus-spec.el --- format spec functions for Gnus
3 ;; Copyright (C) 1996-2014 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
23 ;;; Commentary:
25 ;;; Code:
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)
33 (require 'gnus)
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)
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)
48 ;;; Internal variables.
50 (defvar gnus-summary-mark-positions nil)
51 (defvar gnus-group-mark-positions nil)
52 (defvar gnus-group-indentation "")
54 ;; Format specs. The chunks below are the machine-generated forms
55 ;; that are to be evalled as the result of the default format strings.
56 ;; We write them in here to get them byte-compiled. That way the
57 ;; default actions will be quite fast, while still retaining the full
58 ;; flexibility of the user-defined format specs.
60 ;; First we have lots of dummy defvars to let the compiler know these
61 ;; are really dynamic variables.
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-mouse-face)
85 (defvar gnus-mouse-face-prop)
86 (defvar gnus-tmp-header)
87 (defvar gnus-tmp-from)
89 (declare-function gnus-summary-from-or-to-or-newsgroups "gnus-sum"
90 (header gnus-tmp-from))
92 (defmacro gnus-lrm-string-p (string)
93 (if (fboundp 'bidi-string-mark-left-to-right)
94 ;; LRM, RLM, PDF characters as integers to avoid breaking Emacs
95 ;; 23.
96 `(memq (aref ,string (1- (length ,string))) '(8206 8207 8236))
97 nil))
99 (defvar gnus-lrm-string (if (ignore-errors (string 8206))
100 (propertize (string 8206) 'invisible t)
101 ""))
103 (defvar gnus-summary-line-format-spec nil)
104 (defvar gnus-summary-dummy-line-format-spec nil)
105 (defvar gnus-group-line-format-spec nil)
107 (defvar gnus-format-specs
108 `((version . ,emacs-version)
109 (gnus-version . ,(gnus-continuum-version)))
110 "Alist of format specs.")
112 (defvar gnus-default-format-specs gnus-format-specs)
114 (defvar gnus-article-mode-line-format-spec nil)
115 (defvar gnus-summary-mode-line-format-spec nil)
116 (defvar gnus-group-mode-line-format-spec nil)
118 ;;; Phew. All that gruft is over with, fortunately.
120 ;;;###autoload
121 (defun gnus-update-format (var)
122 "Update the format specification near point."
123 (interactive
124 (list
125 (save-excursion
126 (eval-defun nil)
127 ;; Find the end of the current word.
128 (re-search-forward "[ \t\n]" nil t)
129 ;; Search backward.
130 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
131 (match-string 1)))))
132 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
133 (match-string 1 var))))
134 (entry (assq type gnus-format-specs))
135 value spec)
136 (when entry
137 (setq gnus-format-specs (delq entry gnus-format-specs)))
138 (set
139 (intern (format "%s-spec" var))
140 (gnus-parse-format (setq value (symbol-value (intern var)))
141 (symbol-value (intern (format "%s-alist" var)))
142 (not (string-match "mode" var))))
143 (setq spec (symbol-value (intern (format "%s-spec" var))))
144 (push (list type value spec) gnus-format-specs)
146 (pop-to-buffer "*Gnus Format*")
147 (erase-buffer)
148 (lisp-interaction-mode)
149 (insert (gnus-pp-to-string spec))))
151 (defun gnus-update-format-specifications (&optional force &rest types)
152 "Update all (necessary) format specifications.
153 Return a list of updated types."
154 ;; Make the indentation array.
155 ;; See whether all the stored info needs to be flushed.
156 (when (or force
157 (not gnus-newsrc-file-version)
158 (not (equal (gnus-continuum-version)
159 (gnus-continuum-version gnus-newsrc-file-version)))
160 (not (equal emacs-version
161 (cdr (assq 'version gnus-format-specs)))))
162 (setq gnus-format-specs nil))
163 ;; Go through all the formats and see whether they need updating.
164 (let (new-format entry type val updated)
165 (while (setq type (pop types))
166 ;; Jump to the proper buffer to find out the value of the
167 ;; variable, if possible. (It may be buffer-local.)
168 (save-excursion
169 (let ((buffer (intern (format "gnus-%s-buffer" type))))
170 (when (and (boundp buffer)
171 (setq val (symbol-value buffer))
172 (gnus-buffer-exists-p val))
173 (set-buffer val))
174 (setq new-format (symbol-value
175 (intern (format "gnus-%s-line-format" type)))))
176 (setq entry (cdr (assq type gnus-format-specs)))
177 (if (and (car entry)
178 (equal (car entry) new-format))
179 ;; Use the old format.
180 (set (intern (format "gnus-%s-line-format-spec" type))
181 (cadr entry))
182 ;; This is a new format.
183 (setq val
184 (if (not (stringp new-format))
185 ;; This is a function call or something.
186 new-format
187 ;; This is a "real" format.
188 (gnus-parse-format
189 new-format
190 (symbol-value
191 (intern (format "gnus-%s-line-format-alist" type)))
192 (not (string-match "mode$" (symbol-name type))))))
193 ;; Enter the new format spec into the list.
194 (if entry
195 (progn
196 (setcar (cdr entry) val)
197 (setcar entry new-format))
198 (push (list type new-format val) gnus-format-specs))
199 (set (intern (format "gnus-%s-line-format-spec" type)) val)
200 (push type updated))))
202 (unless (assq 'version gnus-format-specs)
203 (push (cons 'version emacs-version) gnus-format-specs))
204 updated))
206 (defcustom gnus-mouse-face-0 'highlight
207 "The \"%(hello%)\" face."
208 :group 'gnus-format
209 :type 'face)
211 (defcustom gnus-mouse-face-1 'highlight
212 "The \"%1(hello%)\" face."
213 :group 'gnus-format
214 :type 'face)
216 (defcustom gnus-mouse-face-2 'highlight
217 "The \"%2(hello%)\" face."
218 :group 'gnus-format
219 :type 'face)
221 (defcustom gnus-mouse-face-3 'highlight
222 "The \"%3(hello%)\" face."
223 :group 'gnus-format
224 :type 'face)
226 (defcustom gnus-mouse-face-4 'highlight
227 "The \"%4(hello%)\" face."
228 :group 'gnus-format
229 :type 'face)
231 (defun gnus-mouse-face-function (form type)
232 `(gnus-put-text-property
233 (point) (progn ,@form (point))
234 gnus-mouse-face-prop
235 ,(if (equal type 0)
236 'gnus-mouse-face
237 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
239 (defcustom gnus-face-0 'bold
240 "The \"%{hello%}\" face."
241 :group 'gnus-format
242 :type 'face)
244 (defcustom gnus-face-1 'italic
245 "The \"%1{hello%}\" face."
246 :group 'gnus-format
247 :type 'face)
249 (defcustom gnus-face-2 'bold-italic
250 "The \"%2{hello%}\" face."
251 :group 'gnus-format
252 :type 'face)
254 (defcustom gnus-face-3 'bold
255 "The \"%3{hello%}\" face."
256 :group 'gnus-format
257 :type 'face)
259 (defcustom gnus-face-4 'bold
260 "The \"%4{hello%}\" face."
261 :group 'gnus-format
262 :type 'face)
264 (defun gnus-face-face-function (form type)
265 `(gnus-add-text-properties
266 (point) (progn ,@form (point))
267 (cons 'face
268 (cons
269 ;; Delay consing the value of the `face' property until
270 ;; `gnus-add-text-properties' runs, since it will be modified
271 ;; by `gnus-put-text-property-excluding-characters-with-faces'.
272 (list ',(symbol-value (intern (format "gnus-face-%d" type))) 'default)
273 ;; Redundant now, but still convenient.
274 '(gnus-face t)))))
276 (defun gnus-balloon-face-function (form type)
277 `(gnus-put-text-property
278 (point) (progn ,@form (point))
279 ,(if (fboundp 'balloon-help-mode)
280 ''balloon-help
281 ''help-echo)
282 ,(intern (format "gnus-balloon-face-%d" type))))
284 (defun gnus-spec-tab (column)
285 (if (> column 0)
286 `(insert-char ? (max (- ,column (current-column)) 0))
287 (let ((column (abs column)))
288 `(if (> (current-column) ,column)
289 (let ((end (point)))
290 (if (= (move-to-column ,column) ,column)
291 (delete-region (point) end)
292 (delete-region (1- (point)) end)
293 (insert " ")))
294 (insert-char ? (max (- ,column (current-column)) 0))))))
296 (defun gnus-correct-length (string)
297 "Return the correct width of STRING."
298 (apply #'+ (mapcar #'char-width string)))
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))))
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)
329 'length)))
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)
338 'substring)))
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 `(if (gnus-lrm-string-p ,el)
350 (concat (,substring-fun ,el 0 ,max) ,gnus-lrm-string)
351 (,substring-fun ,el 0 ,max)))
352 ,el)
353 `(let ((val (eval ,el)))
354 (if (> (,length-fun val) ,max)
355 ,(if (< max-width 0)
356 `(,substring-fun val (- (,length-fun val) ,max))
357 `(if (gnus-lrm-string-p val)
358 (concat (,substring-fun val 0 ,max) ,gnus-lrm-string)
359 (,substring-fun val 0 ,max)))
360 val)))))
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)))))
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))))
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 ?\ ))
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 ?\ ))
408 ,(when (not side) '(make-string need ?\ )))
409 val)))))
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))))
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 171)) ; «
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)))))))
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)))
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)))
493 (defun gnus-xmas-format (fstring &rest args)
494 "A version of `format' which preserves text properties.
496 Required for XEmacs, where the built in `format' function strips all text
497 properties from both the format string and any inserted strings.
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))
513 (if (null args)
514 (signal 'wrong-number-of-arguments
515 (list #'gnus-xmas-format n fstring)))
516 (let* ((minlen (string-to-number (or (match-string 2) "")))
517 (arg (car args))
518 (str (if (stringp arg) arg (format "%s" arg)))
519 (lpad (null (match-string 1)))
520 (padlen (max 0 (- minlen (length str)))))
521 (replace-match "")
522 (if lpad (insert-char ?\ padlen))
523 (insert str)
524 (unless lpad (insert-char ?\ padlen))
525 (setq args (cdr args))))))
526 (buffer-string))))
528 (defun gnus-parse-simple-format (format spec-alist &optional insert)
529 ;; This function parses the FORMAT string with the help of the
530 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
531 ;; string.
532 (let ((max-width 0)
533 spec flist fstring elem result dontinsert user-defined
534 type value pad-width spec-beg cut-width ignore-value
535 tilde-form tilde elem-type extended-spec)
536 (save-excursion
537 (gnus-set-work-buffer)
538 (insert format)
539 (goto-char (point-min))
540 (while (re-search-forward "%" nil t)
541 (setq user-defined nil
542 spec-beg nil
543 pad-width nil
544 max-width nil
545 cut-width nil
546 ignore-value nil
547 tilde-form nil
548 extended-spec nil)
549 (setq spec-beg (1- (point)))
551 ;; Parse this spec fully.
552 (while
553 (cond
554 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
555 (setq pad-width (string-to-number (match-string 1)))
556 (when (match-beginning 2)
557 (setq max-width (string-to-number (buffer-substring
558 (1+ (match-beginning 2))
559 (match-end 2)))))
560 (goto-char (match-end 0)))
561 ((looking-at "~")
562 (forward-char 1)
563 (setq tilde (read (current-buffer))
564 type (car tilde)
565 value (cadr tilde))
566 (cond
567 ((memq type '(pad pad-left))
568 (setq pad-width value))
569 ((eq type 'pad-right)
570 (setq pad-width (- value)))
571 ((memq type '(max-right max))
572 (setq max-width value))
573 ((eq type 'max-left)
574 (setq max-width (- value)))
575 ((memq type '(cut cut-left))
576 (setq cut-width value))
577 ((eq type 'cut-right)
578 (setq cut-width (- value)))
579 ((eq type 'ignore)
580 (setq ignore-value
581 (if (stringp value) value (format "%s" value))))
582 ((eq type 'form)
583 (setq tilde-form value))
585 (error "Unknown tilde type: %s" tilde)))
588 nil)))
589 (cond
590 ;; User-defined spec -- find the spec name.
591 ((eq (setq spec (char-after)) ?u)
592 (forward-char 1)
593 (when (and (eq (setq user-defined (char-after)) ?&)
594 (looking-at "&\\([^;]+\\);"))
595 (setq user-defined (match-string 1))
596 (goto-char (match-end 1))))
597 ;; extended spec
598 ((and (eq spec ?&) (looking-at "&\\([^;]+\\);"))
599 (setq extended-spec (intern (match-string 1)))
600 (goto-char (match-end 1))))
601 (forward-char 1)
602 (delete-region spec-beg (point))
604 ;; Now we have all the relevant data on this spec, so
605 ;; we start doing stuff.
606 (insert "%")
607 (if (eq spec ?%)
608 ;; "%%" just results in a "%".
609 (insert "%")
610 (cond
611 ;; Do tilde forms.
612 ((eq spec ?@)
613 (setq elem (list tilde-form ?s)))
614 ;; Treat user defined format specifiers specially.
615 (user-defined
616 (setq elem
617 (list
618 (list (intern (format
619 (if (stringp user-defined)
620 "gnus-user-format-function-%s"
621 "gnus-user-format-function-%c")
622 user-defined))
623 'gnus-tmp-header)
624 ?s)))
625 ;; Find the specification from `spec-alist'.
626 ((setq elem (cdr (assq (or extended-spec spec) spec-alist))))
627 ;; We used to use "%l" for displaying the grouplens score.
628 ((eq spec ?l)
629 (setq elem '("" ?s)))
631 (setq elem '("*" ?s))))
632 (setq elem-type (cadr elem))
633 ;; Insert the new format elements.
634 (when (and pad-width
635 (not (and (featurep 'xemacs)
636 gnus-use-correct-string-widths)))
637 (insert (number-to-string pad-width)))
638 ;; Create the form to be evalled.
639 (if (or max-width cut-width ignore-value
640 (and (featurep 'xemacs)
641 gnus-use-correct-string-widths))
642 (progn
643 (insert ?s)
644 (let ((el (car elem)))
645 (cond ((= (cadr elem) ?c)
646 (setq el (list 'char-to-string el)))
647 ((= (cadr elem) ?d)
648 (setq el (list 'int-to-string el))))
649 (when ignore-value
650 (setq el (gnus-tilde-ignore-form el ignore-value)))
651 (when cut-width
652 (setq el (gnus-tilde-cut-form el cut-width)))
653 (when max-width
654 (setq el (gnus-tilde-max-form el max-width)))
655 (when pad-width
656 (setq el (gnus-pad-form el pad-width)))
657 (push el flist)))
658 (insert elem-type)
659 (push (car elem) flist))))
660 (setq fstring (buffer-substring-no-properties (point-min) (point-max))))
662 ;; Do some postprocessing to increase efficiency.
663 (setq
664 result
665 (cond
666 ;; Emptiness.
667 ((string= fstring "")
668 nil)
669 ;; Not a format string.
670 ((not (string-match "%" fstring))
671 (list fstring))
672 ;; A format string with just a single string spec.
673 ((string= fstring "%s")
674 (list (car flist)))
675 ;; A single character.
676 ((string= fstring "%c")
677 (list (car flist)))
678 ;; A single number.
679 ((string= fstring "%d")
680 (setq dontinsert t)
681 (if insert
682 `(insert (int-to-string ,(car flist)))
683 (list `(int-to-string ,(car flist)))))
684 ;; Just lots of chars and strings.
685 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
686 (nreverse flist))
687 ;; A single string spec at the beginning of the spec.
688 ((string-match "\\`%[sc][^%]+\\'" fstring)
689 (list (car flist) (substring fstring 2)))
690 ;; A single string spec in the middle of the spec.
691 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
692 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
693 ;; A single string spec in the end of the spec.
694 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
695 (list (match-string 1 fstring) (car flist)))
696 ;; Only string (and %) specs (XEmacs only!)
697 ((and (featurep 'xemacs)
698 gnus-make-format-preserve-properties
699 (string-match
700 "\\`\\([^%]*\\(%%\\|%-?\\([1-9][0-9]*\\)?s\\)\\)*[^%]*\\'"
701 fstring))
702 (list (cons 'gnus-xmas-format (cons fstring (nreverse flist)))))
703 ;; A more complex spec.
705 (list (cons 'format (cons fstring (nreverse flist)))))))
707 (if insert
708 (when result
709 (if dontinsert
710 result
711 (cons 'insert result)))
712 (cond ((stringp result)
713 result)
714 ((consp result)
715 (cons 'concat result))
716 (t "")))))
718 (defun gnus-eval-format (format &optional alist props)
719 "Eval the format variable FORMAT, using ALIST.
720 If PROPS, insert the result."
721 (let ((form (gnus-parse-format format alist props)))
722 (if props
723 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
724 (eval form))))
726 (defun gnus-set-format (type &optional insertable)
727 (set (intern (format "gnus-%s-line-format-spec" type))
728 (gnus-parse-format
729 (symbol-value (intern (format "gnus-%s-line-format" type)))
730 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
731 insertable)))
733 (provide 'gnus-spec)
735 ;; Local Variables:
736 ;; coding: utf-8
737 ;; End:
739 ;;; gnus-spec.el ends here