Use define-derived-mode (and derived-mode-p).
[emacs.git] / lisp / gnus / gnus-spec.el
blob0ff8ec89ac1a88dffb5ad6fac7d0935b6f1e4a27
1 ;;; gnus-spec.el --- format spec functions for Gnus
3 ;; Copyright (C) 1996-2013 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-tmp-article-number)
85 (defvar gnus-mouse-face)
86 (defvar gnus-mouse-face-prop)
87 (defvar gnus-tmp-header)
88 (defvar gnus-tmp-from)
90 (declare-function gnus-summary-from-or-to-or-newsgroups "gnus-sum"
91 (header gnus-tmp-from))
93 (defmacro gnus-lrm-string-p (string)
94 (if (fboundp 'bidi-string-mark-left-to-right)
95 ;; LRM, RLM, PDF characters as integers to avoid breaking Emacs
96 ;; 23.
97 `(memq (aref ,string (1- (length ,string))) '(8206 8207 8236))
98 nil))
100 (defvar gnus-lrm-string (if (ignore-errors (string 8206))
101 (propertize (string 8206) 'invisible t)
102 ""))
104 (defvar gnus-summary-line-format-spec nil)
105 (defvar gnus-summary-dummy-line-format-spec nil)
106 (defvar gnus-group-line-format-spec nil)
108 (defvar gnus-format-specs
109 `((version . ,emacs-version)
110 (gnus-version . ,(gnus-continuum-version)))
111 "Alist of format specs.")
113 (defvar gnus-default-format-specs gnus-format-specs)
115 (defvar gnus-article-mode-line-format-spec nil)
116 (defvar gnus-summary-mode-line-format-spec nil)
117 (defvar gnus-group-mode-line-format-spec nil)
119 ;;; Phew. All that gruft is over with, fortunately.
121 ;;;###autoload
122 (defun gnus-update-format (var)
123 "Update the format specification near point."
124 (interactive
125 (list
126 (save-excursion
127 (eval-defun nil)
128 ;; Find the end of the current word.
129 (re-search-forward "[ \t\n]" nil t)
130 ;; Search backward.
131 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
132 (match-string 1)))))
133 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
134 (match-string 1 var))))
135 (entry (assq type gnus-format-specs))
136 value spec)
137 (when entry
138 (setq gnus-format-specs (delq entry gnus-format-specs)))
139 (set
140 (intern (format "%s-spec" var))
141 (gnus-parse-format (setq value (symbol-value (intern var)))
142 (symbol-value (intern (format "%s-alist" var)))
143 (not (string-match "mode" var))))
144 (setq spec (symbol-value (intern (format "%s-spec" var))))
145 (push (list type value spec) gnus-format-specs)
147 (pop-to-buffer "*Gnus Format*")
148 (erase-buffer)
149 (lisp-interaction-mode)
150 (insert (gnus-pp-to-string spec))))
152 (defun gnus-update-format-specifications (&optional force &rest types)
153 "Update all (necessary) format specifications.
154 Return a list of updated types."
155 ;; Make the indentation array.
156 ;; See whether all the stored info needs to be flushed.
157 (when (or force
158 (not gnus-newsrc-file-version)
159 (not (equal (gnus-continuum-version)
160 (gnus-continuum-version gnus-newsrc-file-version)))
161 (not (equal emacs-version
162 (cdr (assq 'version gnus-format-specs)))))
163 (setq gnus-format-specs nil))
164 ;; Go through all the formats and see whether they need updating.
165 (let (new-format entry type val updated)
166 (while (setq type (pop types))
167 ;; Jump to the proper buffer to find out the value of the
168 ;; variable, if possible. (It may be buffer-local.)
169 (save-excursion
170 (let ((buffer (intern (format "gnus-%s-buffer" type))))
171 (when (and (boundp buffer)
172 (setq val (symbol-value buffer))
173 (gnus-buffer-exists-p val))
174 (set-buffer val))
175 (setq new-format (symbol-value
176 (intern (format "gnus-%s-line-format" type)))))
177 (setq entry (cdr (assq type gnus-format-specs)))
178 (if (and (car entry)
179 (equal (car entry) new-format))
180 ;; Use the old format.
181 (set (intern (format "gnus-%s-line-format-spec" type))
182 (cadr entry))
183 ;; This is a new format.
184 (setq val
185 (if (not (stringp new-format))
186 ;; This is a function call or something.
187 new-format
188 ;; This is a "real" format.
189 (gnus-parse-format
190 new-format
191 (symbol-value
192 (intern (format "gnus-%s-line-format-alist" type)))
193 (not (string-match "mode$" (symbol-name type))))))
194 ;; Enter the new format spec into the list.
195 (if entry
196 (progn
197 (setcar (cdr entry) val)
198 (setcar entry new-format))
199 (push (list type new-format val) gnus-format-specs))
200 (set (intern (format "gnus-%s-line-format-spec" type)) val)
201 (push type updated))))
203 (unless (assq 'version gnus-format-specs)
204 (push (cons 'version emacs-version) gnus-format-specs))
205 updated))
207 (defcustom gnus-mouse-face-0 'highlight
208 "The \"%(hello%)\" face."
209 :group 'gnus-format
210 :type 'face)
212 (defcustom gnus-mouse-face-1 'highlight
213 "The \"%1(hello%)\" face."
214 :group 'gnus-format
215 :type 'face)
217 (defcustom gnus-mouse-face-2 'highlight
218 "The \"%2(hello%)\" face."
219 :group 'gnus-format
220 :type 'face)
222 (defcustom gnus-mouse-face-3 'highlight
223 "The \"%3(hello%)\" face."
224 :group 'gnus-format
225 :type 'face)
227 (defcustom gnus-mouse-face-4 'highlight
228 "The \"%4(hello%)\" face."
229 :group 'gnus-format
230 :type 'face)
232 (defun gnus-mouse-face-function (form type)
233 `(gnus-put-text-property
234 (point) (progn ,@form (point))
235 gnus-mouse-face-prop
236 ,(if (equal type 0)
237 'gnus-mouse-face
238 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
240 (defcustom gnus-face-0 'bold
241 "The \"%{hello%}\" face."
242 :group 'gnus-format
243 :type 'face)
245 (defcustom gnus-face-1 'italic
246 "The \"%1{hello%}\" face."
247 :group 'gnus-format
248 :type 'face)
250 (defcustom gnus-face-2 'bold-italic
251 "The \"%2{hello%}\" face."
252 :group 'gnus-format
253 :type 'face)
255 (defcustom gnus-face-3 'bold
256 "The \"%3{hello%}\" face."
257 :group 'gnus-format
258 :type 'face)
260 (defcustom gnus-face-4 'bold
261 "The \"%4{hello%}\" face."
262 :group 'gnus-format
263 :type 'face)
265 (defun gnus-face-face-function (form type)
266 `(gnus-add-text-properties
267 (point) (progn ,@form (point))
268 (cons 'face
269 (cons
270 ;; Delay consing the value of the `face' property until
271 ;; `gnus-add-text-properties' runs, since it will be modified
272 ;; by `gnus-put-text-property-excluding-characters-with-faces'.
273 (list ',(symbol-value (intern (format "gnus-face-%d" type))) 'default)
274 ;; Redundant now, but still convenient.
275 '(gnus-face t)))))
277 (defun gnus-balloon-face-function (form type)
278 `(gnus-put-text-property
279 (point) (progn ,@form (point))
280 ,(if (fboundp 'balloon-help-mode)
281 ''balloon-help
282 ''help-echo)
283 ,(intern (format "gnus-balloon-face-%d" type))))
285 (defun gnus-spec-tab (column)
286 (if (> column 0)
287 `(insert-char ? (max (- ,column (current-column)) 0))
288 (let ((column (abs column)))
289 `(if (> (current-column) ,column)
290 (let ((end (point)))
291 (if (= (move-to-column ,column) ,column)
292 (delete-region (point) end)
293 (delete-region (1- (point)) end)
294 (insert " ")))
295 (insert-char ? (max (- ,column (current-column)) 0))))))
297 (defun gnus-correct-length (string)
298 "Return the correct width of STRING."
299 (apply #'+ (mapcar #'char-width string)))
301 (defun gnus-correct-substring (string start &optional end)
302 (let ((wstart 0)
303 (wend 0)
304 (wseek 0)
305 (seek 0)
306 (length (length string))
307 (string (concat string "\0")))
308 ;; Find the start position.
309 (while (and (< seek length)
310 (< wseek start))
311 (incf wseek (char-width (aref string seek)))
312 (incf seek))
313 (setq wstart seek)
314 ;; Find the end position.
315 (while (and (<= seek length)
316 (or (not end)
317 (<= wseek end)))
318 (incf wseek (char-width (aref string seek)))
319 (incf seek))
320 (setq wend seek)
321 (substring string wstart (1- wend))))
323 (defun gnus-string-width-function ()
324 (cond
325 (gnus-use-correct-string-widths
326 'gnus-correct-length)
327 ((fboundp 'string-width)
328 'string-width)
330 'length)))
332 (defun gnus-substring-function ()
333 (cond
334 (gnus-use-correct-string-widths
335 'gnus-correct-substring)
336 ((fboundp 'string-width)
337 'gnus-correct-substring)
339 'substring)))
341 (defun gnus-tilde-max-form (el max-width)
342 "Return a form that limits EL to MAX-WIDTH."
343 (let ((max (abs max-width))
344 (length-fun (gnus-string-width-function))
345 (substring-fun (gnus-substring-function)))
346 (if (symbolp el)
347 `(if (> (,length-fun ,el) ,max)
348 ,(if (< max-width 0)
349 `(,substring-fun ,el (- (,length-fun ,el) ,max))
350 `(if (gnus-lrm-string-p ,el)
351 (concat (,substring-fun ,el 0 ,max) ,gnus-lrm-string)
352 (,substring-fun ,el 0 ,max)))
353 ,el)
354 `(let ((val (eval ,el)))
355 (if (> (,length-fun val) ,max)
356 ,(if (< max-width 0)
357 `(,substring-fun val (- (,length-fun val) ,max))
358 `(if (gnus-lrm-string-p val)
359 (concat (,substring-fun val 0 ,max) ,gnus-lrm-string)
360 (,substring-fun val 0 ,max)))
361 val)))))
363 (defun gnus-tilde-cut-form (el cut-width)
364 "Return a form that cuts CUT-WIDTH off of EL."
365 (let ((cut (abs cut-width))
366 (length-fun (gnus-string-width-function))
367 (substring-fun (gnus-substring-function)))
368 (if (symbolp el)
369 `(if (> (,length-fun ,el) ,cut)
370 ,(if (< cut-width 0)
371 `(,substring-fun ,el 0 (- (,length-fun ,el) ,cut))
372 `(,substring-fun ,el ,cut))
373 ,el)
374 `(let ((val (eval ,el)))
375 (if (> (,length-fun val) ,cut)
376 ,(if (< cut-width 0)
377 `(,substring-fun val 0 (- (,length-fun val) ,cut))
378 `(,substring-fun val ,cut))
379 val)))))
381 (defun gnus-tilde-ignore-form (el ignore-value)
382 "Return a form that is blank when EL is IGNORE-VALUE."
383 (if (symbolp el)
384 `(if (equal ,el ,ignore-value)
385 "" ,el)
386 `(let ((val (eval ,el)))
387 (if (equal val ,ignore-value)
388 "" val))))
390 (defun gnus-pad-form (el pad-width)
391 "Return a form that pads EL to PAD-WIDTH accounting for multi-column
392 characters correctly. This is because `format' may pad to columns or to
393 characters when given a pad value."
394 (let ((pad (abs pad-width))
395 (side (< 0 pad-width))
396 (length-fun (gnus-string-width-function)))
397 (if (symbolp el)
398 `(let ((need (- ,pad (,length-fun ,el))))
399 (if (> need 0)
400 (concat ,(when side '(make-string need ?\ ))
402 ,(when (not side) '(make-string need ?\ )))
403 ,el))
404 `(let* ((val (eval ,el))
405 (need (- ,pad (,length-fun val))))
406 (if (> need 0)
407 (concat ,(when side '(make-string need ?\ ))
409 ,(when (not side) '(make-string need ?\ )))
410 val)))))
412 (defun gnus-parse-format (format spec-alist &optional insert)
413 ;; This function parses the FORMAT string with the help of the
414 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
415 ;; string. If the FORMAT string contains the specifiers %( and %)
416 ;; the text between them will have the mouse-face text property.
417 ;; If the FORMAT string contains the specifiers %[ and %], the text between
418 ;; them will have the balloon-help text property.
419 (let ((case-fold-search nil))
420 (if (string-match
421 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'\\|%[-0-9]*=\\|%[-0-9]*\\*"
422 format)
423 (gnus-parse-complex-format format spec-alist)
424 ;; This is a simple format.
425 (gnus-parse-simple-format format spec-alist insert))))
427 (defun gnus-parse-complex-format (format spec-alist)
428 (let ((cursor-spec nil))
429 (save-excursion
430 (gnus-set-work-buffer)
431 (insert format)
432 (goto-char (point-min))
433 (while (re-search-forward "\"" nil t)
434 (replace-match "\\\"" nil t))
435 (goto-char (point-min))
436 (insert "(\"")
437 ;; Convert all font specs into font spec lists.
438 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
439 (let ((number (if (match-beginning 1)
440 (match-string 1) "0"))
441 (delim (aref (match-string 2) 0)))
442 (if (or (= delim ?\()
443 (= delim ?\{)
444 (= delim 171)) ; «
445 (replace-match (concat "\"("
446 (cond ((= delim ?\() "mouse")
447 ((= delim ?\{) "face")
448 (t "balloon"))
449 " " number " \"")
450 t t)
451 (replace-match "\")\""))))
452 (goto-char (point-max))
453 (insert "\")")
454 ;; Convert point position commands.
455 (goto-char (point-min))
456 (let ((case-fold-search nil))
457 (while (re-search-forward "%\\([-0-9]+\\)?\\*" nil t)
458 (replace-match "\"(point)\"" t t)
459 (setq cursor-spec t)))
460 ;; Convert TAB commands.
461 (goto-char (point-min))
462 (while (re-search-forward "%\\([-0-9]+\\)=" nil t)
463 (replace-match (format "\"(tab %s)\"" (match-string 1)) t t))
464 ;; Convert the buffer into the spec.
465 (goto-char (point-min))
466 (let ((form (read (current-buffer))))
467 (if cursor-spec
468 `(let (gnus-position)
469 ,@(gnus-complex-form-to-spec form spec-alist)
470 (if gnus-position
471 (gnus-put-text-property gnus-position (1+ gnus-position)
472 'gnus-position t)))
473 `(progn
474 ,@(gnus-complex-form-to-spec form spec-alist)))))))
476 (defun gnus-complex-form-to-spec (form spec-alist)
477 (delq nil
478 (mapcar
479 (lambda (sform)
480 (cond
481 ((stringp sform)
482 (gnus-parse-simple-format sform spec-alist t))
483 ((eq (car sform) 'point)
484 '(setq gnus-position (point)))
485 ((eq (car sform) 'tab)
486 (gnus-spec-tab (cadr sform)))
488 (funcall (intern (format "gnus-%s-face-function" (car sform)))
489 (gnus-complex-form-to-spec (cddr sform) spec-alist)
490 (nth 1 sform)))))
491 form)))
494 (defun gnus-xmas-format (fstring &rest args)
495 "A version of `format' which preserves text properties.
497 Required for XEmacs, where the built in `format' function strips all text
498 properties from both the format string and any inserted strings.
500 Only supports the format sequence %s, and %% for inserting
501 literal % characters. A pad width and an optional - (to right pad)
502 are supported for %s."
503 (let ((re "%%\\|%\\(-\\)?\\([1-9][0-9]*\\)?s")
504 (n (length args)))
505 (with-temp-buffer
506 (insert fstring)
507 (goto-char (point-min))
508 (while (re-search-forward re nil t)
509 (goto-char (match-end 0))
510 (cond
511 ((string= (match-string 0) "%%")
512 (delete-char -1))
514 (if (null args)
515 (error 'wrong-number-of-arguments #'my-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