1 ;;; gnus-spec.el --- format spec functions for Gnus -*- coding: iso-latin-1 -*-
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Katsumi Yamaoka <yamaoka@jpl.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
30 (eval-when-compile (require 'cl))
35 (defcustom gnus-use-correct-string-widths t
36 "*If non-nil, use correct functions for dealing with wide characters."
40 ;;; Internal variables.
42 (defvar gnus-summary-mark-positions nil)
43 (defvar gnus-group-mark-positions nil)
44 (defvar gnus-group-indentation "")
46 ;; Format specs. The chunks below are the machine-generated forms
47 ;; that are to be evaled as the result of the default format strings.
48 ;; We write them in here to get them byte-compiled. That way the
49 ;; default actions will be quite fast, while still retaining the full
50 ;; flexibility of the user-defined format specs.
52 ;; First we have lots of dummy defvars to let the compiler know these
53 ;; are really dynamic variables.
55 (defvar gnus-tmp-unread)
56 (defvar gnus-tmp-replied)
57 (defvar gnus-tmp-score-char)
58 (defvar gnus-tmp-indentation)
59 (defvar gnus-tmp-opening-bracket)
60 (defvar gnus-tmp-lines)
61 (defvar gnus-tmp-name)
62 (defvar gnus-tmp-closing-bracket)
63 (defvar gnus-tmp-subject-or-nil)
64 (defvar gnus-tmp-subject)
65 (defvar gnus-tmp-marked)
66 (defvar gnus-tmp-marked-mark)
67 (defvar gnus-tmp-subscribed)
68 (defvar gnus-tmp-process-marked)
69 (defvar gnus-tmp-number-of-unread)
70 (defvar gnus-tmp-group-name)
71 (defvar gnus-tmp-group)
72 (defvar gnus-tmp-article-number)
73 (defvar gnus-tmp-unread-and-unselected)
74 (defvar gnus-tmp-news-method)
75 (defvar gnus-tmp-news-server)
76 (defvar gnus-tmp-article-number)
77 (defvar gnus-mouse-face)
78 (defvar gnus-mouse-face-prop)
80 (defun gnus-summary-line-format-spec ()
81 (insert gnus-tmp-unread gnus-tmp-replied
82 gnus-tmp-score-char gnus-tmp-indentation)
83 (gnus-put-text-property
87 gnus-tmp-opening-bracket
90 (if (> (length gnus-tmp-name) 20)
91 (substring gnus-tmp-name 0 20)
93 gnus-tmp-closing-bracket)
95 gnus-mouse-face-prop gnus-mouse-face)
96 (insert " " gnus-tmp-subject-or-nil "\n"))
98 (defvar gnus-summary-line-format-spec
99 (gnus-byte-code 'gnus-summary-line-format-spec))
101 (defun gnus-summary-dummy-line-format-spec ()
103 (gnus-put-text-property
108 gnus-mouse-face-prop gnus-mouse-face)
109 (insert " " gnus-tmp-subject "\n"))
111 (defvar gnus-summary-dummy-line-format-spec
112 (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
114 (defun gnus-group-line-format-spec ()
115 (insert gnus-tmp-marked-mark gnus-tmp-subscribed
116 gnus-tmp-process-marked
117 gnus-group-indentation
118 (format "%5s: " gnus-tmp-number-of-unread))
119 (gnus-put-text-property
122 (insert gnus-tmp-group "\n")
124 gnus-mouse-face-prop gnus-mouse-face))
125 (defvar gnus-group-line-format-spec
126 (gnus-byte-code 'gnus-group-line-format-spec))
128 (defvar gnus-format-specs
129 `((group ("%M\%S\%p\%P\%5y: %(%g%)%l\n" ,gnus-group-line-format-spec))
130 (summary-dummy ("* %(: :%) %S\n"
131 ,gnus-summary-dummy-line-format-spec))
132 (summary ("%U\%R\%z\%I\%(%[%4L: %-23,23n%]%) %s\n"
133 ,gnus-summary-line-format-spec)))
134 "Alist of format specs.")
136 (defvar gnus-format-specs-compiled nil
137 "Alist of compiled format specs. Each element should be the form:
138 \(TYPE (FORMAT-STRING-1 . COMPILED-FUNCTION-1)
140 (FORMAT-STRING-n . COMPILED-FUNCTION-n)).")
142 (defvar gnus-article-mode-line-format-spec nil)
143 (defvar gnus-summary-mode-line-format-spec nil)
144 (defvar gnus-group-mode-line-format-spec nil)
146 ;;; Phew. All that gruft is over with, fortunately.
149 (defun gnus-update-format (var)
150 "Update the format specification near point."
155 ;; Find the end of the current word.
156 (re-search-forward "[ \t\n]" nil t)
158 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
160 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
161 (match-string 1 var))))
162 (value (symbol-value (intern var)))
164 (intern (format "%s-spec" var))
166 value (symbol-value (intern (format "%s-alist" var)))
167 (not (string-match "mode" var)))))
168 (entry (assq type gnus-format-specs)))
170 (let ((elem (assoc value entry)))
173 (setcdr entry (cons (cons value elem) (cdr entry)))))
174 (push (list type (cons value spec)) gnus-format-specs))
175 (gnus-product-variable-touch 'gnus-format-specs)
177 (pop-to-buffer "*Gnus Format*")
179 (lisp-interaction-mode)
180 (insert (pp-to-string spec))))
182 (put 'gnus-search-or-regist-spec 'lisp-indent-function 1)
183 (defmacro gnus-search-or-regist-spec (mspec &rest body)
184 (let ((specs (nth 0 mspec)) (type (nth 1 mspec)) (format (nth 2 mspec))
185 (spec (nth 3 mspec)) (entry (nth 4 mspec)) (elem (nth 5 mspec)))
186 `(let* ((,entry (assq ,type ,specs))
187 (,elem (assoc ,format (cdr ,entry))))
193 (setcdr ,entry (cons (cons ,format ,spec) (cdr ,entry))))
194 (push (list ,type (cons ,format ,spec)) ,specs))
195 (gnus-product-variable-touch (quote ,specs)))
198 (defun gnus-update-format-specification-1 (type format val)
199 (set (intern (format "gnus-%s-line-format-spec" type))
200 (gnus-search-or-regist-spec (gnus-format-specs-compiled
201 type format val entry elem)
202 (when (and gnus-compile-user-specs val)
205 (fset 'gnus-tmp-func `(lambda () ,val))
207 (let (byte-compile-warnings)
208 (byte-compile 'gnus-tmp-func))
209 (gnus-byte-code 'gnus-tmp-func))
210 (when (get-buffer "*Compile-Log*")
211 (bury-buffer "*Compile-Log*"))
212 (when (get-buffer "*Compile-Log-Show*")
213 (bury-buffer "*Compile-Log-Show*"))))))))
215 (defun gnus-update-format-specifications (&optional force &rest types)
216 "Update all (necessary) format specifications."
217 ;; Make the indentation array.
218 ;; See whether all the stored info needs to be flushed.
220 (message "%s" "Force update format specs.")
221 (setq gnus-format-specs nil
222 gnus-format-specs-compiled nil)
223 (gnus-product-variable-touch 'gnus-format-specs
224 'gnus-format-specs-compiled))
226 ;; Go through all the formats and see whether they need updating.
229 (while (setq type (pop types))
230 ;; Jump to the proper buffer to find out the value of the
231 ;; variable, if possible. (It may be buffer-local.)
233 (let ((buffer (intern (format "gnus-%s-buffer" type))))
234 (when (and (boundp buffer)
235 (setq val (symbol-value buffer))
236 (gnus-buffer-exists-p val))
239 (intern (format "gnus-%s-line-format" type))))))
240 (or (gnus-update-format-specification-1 type new-format nil)
241 ;; This is a new format.
242 (gnus-update-format-specification-1
244 (gnus-search-or-regist-spec (gnus-format-specs
245 type new-format val entry elem)
246 (setq val (if (stringp new-format)
247 ;; This is a "real" format.
251 (intern (format "gnus-%s-line-format-alist"
253 (not (string-match "mode$"
254 (symbol-name type))))
255 ;; This is a function call or something.
258 (defvar gnus-mouse-face-0 'highlight)
259 (defvar gnus-mouse-face-1 'highlight)
260 (defvar gnus-mouse-face-2 'highlight)
261 (defvar gnus-mouse-face-3 'highlight)
262 (defvar gnus-mouse-face-4 'highlight)
264 (defun gnus-mouse-face-function (form type)
265 `(gnus-put-text-property
266 (point) (progn ,@form (point))
270 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
272 (defvar gnus-face-0 'bold)
273 (defvar gnus-face-1 'italic)
274 (defvar gnus-face-2 'bold-italic)
275 (defvar gnus-face-3 'bold)
276 (defvar gnus-face-4 'bold)
278 (defun gnus-face-face-function (form type)
279 `(gnus-add-text-properties
280 (point) (progn ,@form (point))
281 '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
283 (defun gnus-balloon-face-function (form type)
284 `(gnus-put-text-property
285 (point) (progn ,@form (point))
287 ,(intern (format "gnus-balloon-face-%d" type))))
289 (defun gnus-spec-tab (column)
291 `(insert (make-string (max (- ,column (current-column)) 0) ? ))
293 (if (> (current-column) ,(abs column))
294 (delete-region (point)
295 (- (point) (- (current-column) ,(abs column))))
296 (insert (make-string (max (- ,(abs column) (current-column)) 0)
299 (defun gnus-correct-length (string)
300 "Return the correct width of STRING."
302 (mapcar (lambda (char) (incf length (gnus-char-width char))) string)
305 (defun gnus-correct-substring (string start &optional end)
309 (length (length string)))
310 ;; Find the start position.
311 (while (and (< seek length)
313 (incf wstart (gnus-char-width (aref string seek)))
317 ;; Find the end position.
318 (while (and (< seek length)
321 (incf wend (gnus-char-width (aref string seek)))
324 (substring string wstart (1- wend))))
326 (defun gnus-tilde-max-form (el max-width)
327 "Return a form that limits EL to MAX-WIDTH."
328 (let ((max (abs max-width)))
330 `(if (> (,(if gnus-use-correct-string-widths
335 `(,(if gnus-use-correct-string-widths
336 'gnus-correct-substring
338 ,el (- (,(if gnus-use-correct-string-widths
342 `(,(if gnus-use-correct-string-widths
343 'gnus-correct-substring
347 `(let ((val (eval ,el)))
348 (if (> (,(if gnus-use-correct-string-widths
352 `(,(if gnus-use-correct-string-widths
353 'gnus-correct-substring
355 val (- (,(if gnus-use-correct-string-widths
358 `(,(if gnus-use-correct-string-widths
359 'gnus-correct-substring
364 (defun gnus-tilde-cut-form (el cut-width)
365 "Return a form that cuts CUT-WIDTH off of EL."
366 (let ((cut (abs cut-width)))
368 `(if (> (,(if gnus-use-correct-string-widths
372 `(,(if gnus-use-correct-string-widths
373 'gnus-correct-substring
375 (- (,(if gnus-use-correct-string-widths
378 `(,(if gnus-use-correct-string-widths
379 'gnus-correct-substring
380 'substring) ,el ,cut))
382 `(let ((val (eval ,el)))
383 (if (> (,(if gnus-use-correct-string-widths
387 `(,(if gnus-use-correct-string-widths
388 'gnus-correct-substring
390 (- (,(if gnus-use-correct-string-widths
393 `(,(if gnus-use-correct-string-widths
394 'gnus-correct-substring
395 'substring) val ,cut))
398 (defun gnus-tilde-ignore-form (el ignore-value)
399 "Return a form that is blank when EL is IGNORE-VALUE."
401 `(if (equal ,el ,ignore-value)
403 `(let ((val (eval ,el)))
404 (if (equal val ,ignore-value)
407 (defun gnus-correct-pad-form (el pad-width)
408 "Return a form that pads EL to PAD-WIDTH accounting for multi-column
409 characters correctly. This is because `format' may pad to columns or to
410 characters when given a pad value."
411 (let ((pad (abs pad-width)))
413 `(let ((need (- ,pad (gnus-correct-length ,el))))
415 (concat ,el (make-string need ?\ ))
417 `(let* ((val (eval ,el))
418 (need (- ,pad (gnus-correct-length ,el))))
420 (concat ,el (make-string need ?\ ))
423 (defun gnus-parse-format (format spec-alist &optional insert)
424 ;; This function parses the FORMAT string with the help of the
425 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
426 ;; string. If the FORMAT string contains the specifiers %( and %)
427 ;; the text between them will have the mouse-face text property.
428 ;; If the FORMAT string contains the specifiers %[ and %], the text between
429 ;; them will have the balloon-help text property.
431 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'"
433 (gnus-parse-complex-format format spec-alist)
434 ;; This is a simple format.
435 (gnus-parse-simple-format format spec-alist insert)))
437 (defun gnus-parse-complex-format (format spec-alist)
439 (gnus-set-work-buffer)
441 (goto-char (point-min))
442 (while (re-search-forward "\"" nil t)
443 (replace-match "\\\"" nil t))
444 (goto-char (point-min))
446 ;; Convert all font specs into font spec lists.
447 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
448 (let ((number (if (match-beginning 1)
449 (match-string 1) "0"))
450 (delim (aref (match-string 2) 0)))
451 (if (or (= delim ?\()
454 (replace-match (concat "\"("
455 (cond ((= delim ?\() "mouse")
456 ((= delim ?\{) "face")
460 (replace-match "\")\""))))
461 (goto-char (point-max))
463 ;; Convert point position commands.
464 (goto-char (point-min))
465 (while (re-search-forward "%\\([-0-9]+\\)?C" nil t)
466 (replace-match "\"(point)\"" t t))
467 ;; Convert TAB commands.
468 (goto-char (point-min))
469 (while (re-search-forward "%\\([-0-9]+\\)=" nil t)
470 (replace-match (format "\"(tab %s)\"" (match-string 1)) t t))
471 ;; Convert the buffer into the spec.
472 (goto-char (point-min))
473 (let ((form (read (current-buffer))))
474 ;; If the first element is '(point), we just remove it.
475 (when (equal (car form) '(point))
477 (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
479 (defun gnus-complex-form-to-spec (form spec-alist)
485 (gnus-parse-simple-format sform spec-alist t))
486 ((eq (car sform) 'point)
487 `(gnus-put-text-property (1- (point)) (point) 'gnus-position t))
488 ((eq (car sform) 'tab)
489 (gnus-spec-tab (cadr sform)))
491 (funcall (intern (format "gnus-%s-face-function" (car sform)))
492 (gnus-complex-form-to-spec (cddr sform) spec-alist)
496 (defun gnus-parse-simple-format (format spec-alist &optional insert)
497 ;; This function parses the FORMAT string with the help of the
498 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
501 spec flist fstring elem result dontinsert user-defined
502 type value pad-width spec-beg cut-width ignore-value
503 tilde-form tilde elem-type)
505 (gnus-set-work-buffer)
507 (goto-char (point-min))
508 (while (search-forward "%" nil t)
509 (setq user-defined nil
516 (setq spec-beg (1- (point)))
518 ;; Parse this spec fully.
521 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
522 (setq pad-width (string-to-number (match-string 1)))
523 (when (match-beginning 2)
524 (setq max-width (string-to-number (buffer-substring
525 (1+ (match-beginning 2))
527 (goto-char (match-end 0)))
530 (setq tilde (read (current-buffer))
534 ((memq type '(pad pad-left))
535 (setq pad-width value))
536 ((eq type 'pad-right)
537 (setq pad-width (- value)))
538 ((memq type '(max-right max))
539 (setq max-width value))
541 (setq max-width (- value)))
542 ((memq type '(cut cut-left))
543 (setq cut-width value))
544 ((eq type 'cut-right)
545 (setq cut-width (- value)))
548 (if (stringp value) value (format "%s" value))))
550 (setq tilde-form value))
552 (error "Unknown tilde type: %s" tilde)))
556 ;; User-defined spec -- find the spec name.
557 (when (eq (setq spec (char-after)) ?u)
559 (setq user-defined (char-after)))
561 (delete-region spec-beg (point))
563 ;; Now we have all the relevant data on this spec, so
564 ;; we start doing stuff.
567 ;; "%%" just results in a "%".
572 (setq elem (list tilde-form ?s)))
573 ;; Treat user defined format specifiers specially.
577 (list (intern (format "gnus-user-format-function-%c"
581 ;; Find the specification from `spec-alist'.
582 ((setq elem (cdr (assq spec spec-alist))))
584 (setq elem '("*" ?s))))
585 (setq elem-type (cadr elem))
586 ;; Insert the new format elements.
588 (not (and (featurep 'xemacs)
589 gnus-use-correct-string-widths)))
590 (insert (number-to-string pad-width)))
591 ;; Create the form to be evaled.
592 (if (or max-width cut-width ignore-value
593 (and (featurep 'xemacs)
594 gnus-use-correct-string-widths))
597 (let ((el (car elem)))
598 (cond ((= (cadr elem) ?c)
599 (setq el (list 'char-to-string el)))
601 (setq el (list 'int-to-string el))))
603 (setq el (gnus-tilde-ignore-form el ignore-value)))
605 (setq el (gnus-tilde-cut-form el cut-width)))
607 (setq el (gnus-tilde-max-form el max-width)))
609 (setq el (gnus-correct-pad-form el pad-width)))
612 (push (car elem) flist))))
613 (setq fstring (buffer-string)))
615 ;; Do some postprocessing to increase efficiency.
620 ((string= fstring "")
622 ;; Not a format string.
623 ((not (string-match "%" fstring))
625 ;; A format string with just a single string spec.
626 ((string= fstring "%s")
628 ;; A single character.
629 ((string= fstring "%c")
632 ((string= fstring "%d")
635 (list `(princ ,(car flist)))
636 (list `(int-to-string ,(car flist)))))
637 ;; Just lots of chars and strings.
638 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
640 ;; A single string spec at the beginning of the spec.
641 ((string-match "\\`%[sc][^%]+\\'" fstring)
642 (list (car flist) (substring fstring 2)))
643 ;; A single string spec in the middle of the spec.
644 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
645 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
646 ;; A single string spec in the end of the spec.
647 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
648 (list (match-string 1 fstring) (car flist)))
649 ;; A more complex spec.
651 (list (cons 'format (cons fstring (nreverse flist)))))))
657 (cons 'insert result)))
658 (cond ((stringp result)
661 (cons 'concat result))
664 (defun gnus-eval-format (format &optional alist props)
665 "Eval the format variable FORMAT, using ALIST.
666 If PROPS, insert the result."
667 (let ((form (gnus-parse-format format alist props)))
669 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
672 (defun gnus-compile ()
673 "Byte-compile the user-defined format specs."
676 (let ((entries gnus-format-specs)
677 (byte-compile-warnings '(unresolved callargs redefine))
678 entry type compiled-function)
680 (gnus-message 7 "Compiling format specs...")
683 (setq entry (pop entries)
685 (if (memq type '(gnus-version version))
686 (setq gnus-format-specs (delq entry gnus-format-specs))
687 (let ((form (caddr entry)))
688 (when (and (listp form)
689 ;; Under GNU Emacs, it's (byte-code ...)
690 (not (eq 'byte-code (car form)))
691 ;; Under XEmacs, it's (funcall #<compiled-function ...>)
692 (not (and (eq 'funcall (car form))
693 (byte-code-function-p (cadr form)))))
694 (defalias 'gnus-tmp-func `(lambda () ,form))
695 (byte-compile 'gnus-tmp-func)
696 (setq compiled-function (gnus-byte-code 'gnus-tmp-func))
697 (set (intern (format "gnus-%s-line-format-spec" type))
699 (let ((elem (cdr (assq type gnus-format-specs-compiled))))
701 (set-alist 'elem (cadr entry) compiled-function)
702 (setq elem (list (cadr entry) compiled-function)))
703 (set-alist 'gnus-format-specs-compiled type elem))))))
705 (push (cons 'version emacs-version) gnus-format-specs)
706 (gnus-message 7 "Compiling user specs...done"))))
708 (defun gnus-set-format (type &optional insertable)
709 (set (intern (format "gnus-%s-line-format-spec" type))
711 (symbol-value (intern (format "gnus-%s-line-format" type)))
712 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
720 ;; coding: iso-8859-1
723 ;;; gnus-spec.el ends here