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