Fix bug #9221 with memory leak in bidi display.
[emacs.git] / lisp / gnus / gnus-spec.el
blob4682f512476c448a123272a0458d6cf51f3fae9d
1 ;;; gnus-spec.el --- format spec functions for Gnus
3 ;; Copyright (C) 1996-2011 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 evaled as the result of the default format strings.
56 ;; We write them in here to get them byte-compiled. That way the
57 ;; default actions will be quite fast, while still retaining the full
58 ;; flexibility of the user-defined format specs.
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 (defun gnus-summary-line-format-spec ()
94 (insert gnus-tmp-unread gnus-tmp-replied
95 gnus-tmp-score-char gnus-tmp-indentation)
96 (gnus-put-text-property
97 (point)
98 (progn
99 (insert
100 (format "%c%4s: %-23s%c" gnus-tmp-opening-bracket gnus-tmp-lines
101 (let ((val
102 (inline
103 (gnus-summary-from-or-to-or-newsgroups
104 gnus-tmp-header gnus-tmp-from))))
105 (if (> (length val) 23)
106 (substring val 0 23)
107 val))
108 gnus-tmp-closing-bracket))
109 (point))
110 gnus-mouse-face-prop gnus-mouse-face)
111 (insert " " gnus-tmp-subject-or-nil "\n"))
113 (defvar gnus-summary-line-format-spec
114 (gnus-byte-code 'gnus-summary-line-format-spec))
116 (defun gnus-summary-dummy-line-format-spec ()
117 (insert "* ")
118 (gnus-put-text-property
119 (point)
120 (progn
121 (insert ": :")
122 (point))
123 gnus-mouse-face-prop gnus-mouse-face)
124 (insert " " gnus-tmp-subject "\n"))
126 (defvar gnus-summary-dummy-line-format-spec
127 (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
129 (defun gnus-group-line-format-spec ()
130 (insert gnus-tmp-marked-mark gnus-tmp-subscribed
131 gnus-tmp-process-marked
132 gnus-group-indentation
133 (format "%5s: " gnus-tmp-number-of-unread))
134 (gnus-put-text-property
135 (point)
136 (progn
137 (insert gnus-tmp-group "\n")
138 (1- (point)))
139 gnus-mouse-face-prop gnus-mouse-face))
140 (defvar gnus-group-line-format-spec
141 (gnus-byte-code 'gnus-group-line-format-spec))
143 (defvar gnus-format-specs
144 `((version . ,emacs-version)
145 (gnus-version . ,(gnus-continuum-version))
146 (group "%M\%S\%p\%P\%5y: %(%g%)\n" ,gnus-group-line-format-spec)
147 (summary-dummy "* %(: :%) %S\n"
148 ,gnus-summary-dummy-line-format-spec)
149 (summary "%U%R%z%I%(%[%4L: %-23,23f%]%) %s\n"
150 ,gnus-summary-line-format-spec))
151 "Alist of format specs.")
153 (defvar gnus-default-format-specs gnus-format-specs)
155 (defvar gnus-article-mode-line-format-spec nil)
156 (defvar gnus-summary-mode-line-format-spec nil)
157 (defvar gnus-group-mode-line-format-spec nil)
159 ;;; Phew. All that gruft is over with, fortunately.
161 ;;;###autoload
162 (defun gnus-update-format (var)
163 "Update the format specification near point."
164 (interactive
165 (list
166 (save-excursion
167 (eval-defun nil)
168 ;; Find the end of the current word.
169 (re-search-forward "[ \t\n]" nil t)
170 ;; Search backward.
171 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
172 (match-string 1)))))
173 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
174 (match-string 1 var))))
175 (entry (assq type gnus-format-specs))
176 value spec)
177 (when entry
178 (setq gnus-format-specs (delq entry gnus-format-specs)))
179 (set
180 (intern (format "%s-spec" var))
181 (gnus-parse-format (setq value (symbol-value (intern var)))
182 (symbol-value (intern (format "%s-alist" var)))
183 (not (string-match "mode" var))))
184 (setq spec (symbol-value (intern (format "%s-spec" var))))
185 (push (list type value spec) gnus-format-specs)
187 (pop-to-buffer "*Gnus Format*")
188 (erase-buffer)
189 (lisp-interaction-mode)
190 (insert (gnus-pp-to-string spec))))
192 (defun gnus-update-format-specifications (&optional force &rest types)
193 "Update all (necessary) format specifications.
194 Return a list of updated types."
195 ;; Make the indentation array.
196 ;; See whether all the stored info needs to be flushed.
197 (when (or force
198 (not gnus-newsrc-file-version)
199 (not (equal (gnus-continuum-version)
200 (gnus-continuum-version gnus-newsrc-file-version)))
201 (not (equal emacs-version
202 (cdr (assq 'version gnus-format-specs)))))
203 (setq gnus-format-specs nil))
204 ;; Flush the group format spec cache if there's the grouplens stuff
205 ;; or it doesn't support decoded group names.
206 (when (memq 'group types)
207 (let* ((spec (assq 'group gnus-format-specs))
208 (sspec (gnus-prin1-to-string (nth 2 spec))))
209 (when (or (string-match " gnus-tmp-grouplens[ )]" sspec)
210 (not (string-match " gnus-tmp-decoded-group[ )]" sspec)))
211 (setq gnus-format-specs (delq spec gnus-format-specs)))))
213 ;; Go through all the formats and see whether they need updating.
214 (let (new-format entry type val updated)
215 (while (setq type (pop types))
216 ;; Jump to the proper buffer to find out the value of the
217 ;; variable, if possible. (It may be buffer-local.)
218 (save-excursion
219 (let ((buffer (intern (format "gnus-%s-buffer" type))))
220 (when (and (boundp buffer)
221 (setq val (symbol-value buffer))
222 (gnus-buffer-exists-p val))
223 (set-buffer val))
224 (setq new-format (symbol-value
225 (intern (format "gnus-%s-line-format" type)))))
226 (setq entry (cdr (assq type gnus-format-specs)))
227 (if (and (car entry)
228 (equal (car entry) new-format))
229 ;; Use the old format.
230 (set (intern (format "gnus-%s-line-format-spec" type))
231 (cadr entry))
232 ;; This is a new format.
233 (setq val
234 (if (not (stringp new-format))
235 ;; This is a function call or something.
236 new-format
237 ;; This is a "real" format.
238 (gnus-parse-format
239 new-format
240 (symbol-value
241 (intern (format "gnus-%s-line-format-alist" type)))
242 (not (string-match "mode$" (symbol-name type))))))
243 ;; Enter the new format spec into the list.
244 (if entry
245 (progn
246 (setcar (cdr entry) val)
247 (setcar entry new-format))
248 (push (list type new-format val) gnus-format-specs))
249 (set (intern (format "gnus-%s-line-format-spec" type)) val)
250 (push type updated))))
252 (unless (assq 'version gnus-format-specs)
253 (push (cons 'version emacs-version) gnus-format-specs))
254 updated))
256 (defvar gnus-mouse-face-0 'highlight)
257 (defvar gnus-mouse-face-1 'highlight)
258 (defvar gnus-mouse-face-2 'highlight)
259 (defvar gnus-mouse-face-3 'highlight)
260 (defvar gnus-mouse-face-4 'highlight)
262 (defun gnus-mouse-face-function (form type)
263 `(gnus-put-text-property
264 (point) (progn ,@form (point))
265 gnus-mouse-face-prop
266 ,(if (equal type 0)
267 'gnus-mouse-face
268 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
270 (defvar gnus-face-0 'bold)
271 (defvar gnus-face-1 'italic)
272 (defvar gnus-face-2 'bold-italic)
273 (defvar gnus-face-3 'bold)
274 (defvar gnus-face-4 'bold)
276 (defun gnus-face-face-function (form type)
277 `(gnus-add-text-properties
278 (point) (progn ,@form (point))
279 '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
281 (defun gnus-balloon-face-function (form type)
282 `(gnus-put-text-property
283 (point) (progn ,@form (point))
284 ,(if (fboundp 'balloon-help-mode)
285 ''balloon-help
286 ''help-echo)
287 ,(intern (format "gnus-balloon-face-%d" type))))
289 (defun gnus-spec-tab (column)
290 (if (> column 0)
291 `(insert-char ? (max (- ,column (current-column)) 0))
292 (let ((column (abs column)))
293 `(if (> (current-column) ,column)
294 (let ((end (point)))
295 (if (= (move-to-column ,column) ,column)
296 (delete-region (point) end)
297 (delete-region (1- (point)) end)
298 (insert " ")))
299 (insert-char ? (max (- ,column (current-column)) 0))))))
301 (defun gnus-correct-length (string)
302 "Return the correct width of STRING."
303 (apply #'+ (mapcar #'char-width string)))
305 (defun gnus-correct-substring (string start &optional end)
306 (let ((wstart 0)
307 (wend 0)
308 (wseek 0)
309 (seek 0)
310 (length (length string))
311 (string (concat string "\0")))
312 ;; Find the start position.
313 (while (and (< seek length)
314 (< wseek start))
315 (incf wseek (char-width (aref string seek)))
316 (incf seek))
317 (setq wstart seek)
318 ;; Find the end position.
319 (while (and (<= seek length)
320 (or (not end)
321 (<= wseek end)))
322 (incf wseek (char-width (aref string seek)))
323 (incf seek))
324 (setq wend seek)
325 (substring string wstart (1- wend))))
327 (defun gnus-string-width-function ()
328 (cond
329 (gnus-use-correct-string-widths
330 'gnus-correct-length)
331 ((fboundp 'string-width)
332 'string-width)
334 'length)))
336 (defun gnus-substring-function ()
337 (cond
338 (gnus-use-correct-string-widths
339 'gnus-correct-substring)
340 ((fboundp 'string-width)
341 'gnus-correct-substring)
343 'substring)))
345 (defun gnus-tilde-max-form (el max-width)
346 "Return a form that limits EL to MAX-WIDTH."
347 (let ((max (abs max-width))
348 (length-fun (gnus-string-width-function))
349 (substring-fun (gnus-substring-function)))
350 (if (symbolp el)
351 `(if (> (,length-fun ,el) ,max)
352 ,(if (< max-width 0)
353 `(,substring-fun ,el (- (,length-fun ,el) ,max))
354 `(,substring-fun ,el 0 ,max))
355 ,el)
356 `(let ((val (eval ,el)))
357 (if (> (,length-fun val) ,max)
358 ,(if (< max-width 0)
359 `(,substring-fun val (- (,length-fun val) ,max))
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 ?\«))
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 evaled.
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-compile ()
727 "Byte-compile the user-defined format specs."
728 (interactive)
729 (require 'bytecomp)
730 (let ((entries gnus-format-specs)
731 (byte-compile-warnings '(unresolved callargs redefine))
732 entry gnus-tmp-func)
733 (save-excursion
734 (gnus-message 7 "Compiling format specs...")
736 (while entries
737 (setq entry (pop entries))
738 (if (memq (car entry) '(gnus-version version))
739 (setq gnus-format-specs (delq entry gnus-format-specs))
740 (let ((form (caddr entry)))
741 (when (and (listp form)
742 ;; Under GNU Emacs, it's (byte-code ...)
743 (not (eq 'byte-code (car form)))
744 ;; Under XEmacs, it's (funcall #<compiled-function ...>)
745 (not (and (eq 'funcall (car form))
746 (byte-code-function-p (cadr form)))))
747 (defalias 'gnus-tmp-func `(lambda () ,form))
748 (byte-compile 'gnus-tmp-func)
749 (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func))))))
751 (push (cons 'version emacs-version) gnus-format-specs)
752 ;; Mark the .newsrc.eld file as "dirty".
753 (gnus-dribble-touch)
754 (gnus-message 7 "Compiling user specs...done"))))
756 (defun gnus-set-format (type &optional insertable)
757 (set (intern (format "gnus-%s-line-format-spec" type))
758 (gnus-parse-format
759 (symbol-value (intern (format "gnus-%s-line-format" type)))
760 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
761 insertable)))
763 (provide 'gnus-spec)
765 ;; Local Variables:
766 ;; coding: iso-8859-1
767 ;; End:
769 ;;; gnus-spec.el ends here