1 ;;; gnus-spec.el --- format spec functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
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)
79 (defvar gnus-tmp-header)
80 (defvar gnus-tmp-from)
82 (defun gnus-summary-line-format-spec ()
83 (insert gnus-tmp-unread gnus-tmp-replied
84 gnus-tmp-score-char gnus-tmp-indentation)
85 (gnus-put-text-property
89 (format "%c%4s: %-23s%c" gnus-tmp-opening-bracket gnus-tmp-lines
92 (gnus-summary-from-or-to-or-newsgroups
93 gnus-tmp-header gnus-tmp-from))))
94 (if (> (length val) 23)
97 gnus-tmp-closing-bracket))
99 gnus-mouse-face-prop gnus-mouse-face)
100 (insert " " gnus-tmp-subject-or-nil "\n"))
102 (defvar gnus-summary-line-format-spec
103 (gnus-byte-code 'gnus-summary-line-format-spec))
105 (defun gnus-summary-dummy-line-format-spec ()
107 (gnus-put-text-property
112 gnus-mouse-face-prop gnus-mouse-face)
113 (insert " " gnus-tmp-subject "\n"))
115 (defvar gnus-summary-dummy-line-format-spec
116 (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
118 (defun gnus-group-line-format-spec ()
119 (insert gnus-tmp-marked-mark gnus-tmp-subscribed
120 gnus-tmp-process-marked
121 gnus-group-indentation
122 (format "%5s: " gnus-tmp-number-of-unread))
123 (gnus-put-text-property
126 (insert gnus-tmp-group "\n")
128 gnus-mouse-face-prop gnus-mouse-face))
129 (defvar gnus-group-line-format-spec
130 (gnus-byte-code 'gnus-group-line-format-spec))
132 (defvar gnus-format-specs
133 `((group ("%M\%S\%p\%P\%5y: %(%g%)%l\n" ,gnus-group-line-format-spec))
134 (summary-dummy ("* %(: :%) %S\n"
135 ,gnus-summary-dummy-line-format-spec))
136 (summary ("%U%R%z%I%(%[%4L: %-23,23f%]%) %s\n"
137 ,gnus-summary-line-format-spec)))
138 "Alist of format specs.")
140 (defvar gnus-default-format-specs gnus-format-specs)
142 (defvar gnus-format-specs-compiled nil
143 "Alist of compiled format specs. Each element should be the form:
144 \(TYPE (FORMAT-STRING-1 . COMPILED-FUNCTION-1)
146 (FORMAT-STRING-n . COMPILED-FUNCTION-n)).")
148 (defvar gnus-article-mode-line-format-spec nil)
149 (defvar gnus-summary-mode-line-format-spec nil)
150 (defvar gnus-group-mode-line-format-spec nil)
152 ;;; Phew. All that gruft is over with, fortunately.
155 (defun gnus-update-format (var)
156 "Update the format specification near point."
161 ;; Find the end of the current word.
162 (re-search-forward "[ \t\n]" nil t)
164 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
166 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
167 (match-string 1 var))))
168 (value (symbol-value (intern var)))
170 (intern (format "%s-spec" var))
172 value (symbol-value (intern (format "%s-alist" var)))
173 (not (string-match "mode" var)))))
174 (entry (assq type gnus-format-specs)))
176 (let ((elem (assoc value entry)))
179 (setcdr entry (cons (cons value elem) (cdr entry)))))
180 (push (list type (cons value spec)) gnus-format-specs))
181 (gnus-product-variable-touch 'gnus-format-specs)
183 (pop-to-buffer "*Gnus Format*")
185 (lisp-interaction-mode)
186 (insert (pp-to-string spec))))
188 (put 'gnus-search-or-regist-spec 'lisp-indent-function 1)
189 (defmacro gnus-search-or-regist-spec (mspec &rest body)
190 (let ((specs (nth 0 mspec)) (type (nth 1 mspec)) (format (nth 2 mspec))
191 (spec (nth 3 mspec)) (entry (nth 4 mspec)) (elem (nth 5 mspec)))
192 `(let* ((,entry (assq ,type ,specs))
193 (,elem (assoc ,format (cdr ,entry))))
199 (setcdr ,entry (cons (cons ,format ,spec) (cdr ,entry))))
200 (push (list ,type (cons ,format ,spec)) ,specs))
201 (gnus-product-variable-touch (quote ,specs)))
204 (defun gnus-update-format-specification-1 (type format val)
205 (set (intern (format "gnus-%s-line-format-spec" type))
206 (gnus-search-or-regist-spec (gnus-format-specs-compiled
207 type format val entry elem)
208 (when (and gnus-compile-user-specs val)
211 (fset 'gnus-tmp-func `(lambda () ,val))
213 (let (byte-compile-warnings)
214 (byte-compile 'gnus-tmp-func))
215 (gnus-byte-code 'gnus-tmp-func))
216 (when (get-buffer "*Compile-Log*")
217 (bury-buffer "*Compile-Log*"))
218 (when (get-buffer "*Compile-Log-Show*")
219 (bury-buffer "*Compile-Log-Show*"))))))))
221 (defun gnus-update-format-specifications (&optional force &rest types)
222 "Update all (necessary) format specifications."
223 ;; Make the indentation array.
224 ;; See whether all the stored info needs to be flushed.
226 (message "%s" "Force update format specs.")
227 (setq gnus-format-specs nil
228 gnus-format-specs-compiled nil)
229 (gnus-product-variable-touch 'gnus-format-specs
230 'gnus-format-specs-compiled))
232 ;; Go through all the formats and see whether they need updating.
235 (while (setq type (pop types))
236 ;; Jump to the proper buffer to find out the value of the
237 ;; variable, if possible. (It may be buffer-local.)
239 (let ((buffer (intern (format "gnus-%s-buffer" type))))
240 (when (and (boundp buffer)
241 (setq val (symbol-value buffer))
242 (gnus-buffer-exists-p val))
245 (intern (format "gnus-%s-line-format" type))))))
246 (or (gnus-update-format-specification-1 type new-format nil)
247 ;; This is a new format.
248 (gnus-update-format-specification-1
250 (gnus-search-or-regist-spec (gnus-format-specs
251 type new-format val entry elem)
252 (setq val (if (stringp new-format)
253 ;; This is a "real" format.
257 (intern (format "gnus-%s-line-format-alist"
259 (not (string-match "mode$"
260 (symbol-name type))))
261 ;; This is a function call or something.
264 (defvar gnus-mouse-face-0 'highlight)
265 (defvar gnus-mouse-face-1 'highlight)
266 (defvar gnus-mouse-face-2 'highlight)
267 (defvar gnus-mouse-face-3 'highlight)
268 (defvar gnus-mouse-face-4 'highlight)
270 (defun gnus-mouse-face-function (form type)
271 `(gnus-put-text-property
272 (point) (progn ,@form (point))
276 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
278 (defvar gnus-face-0 'bold)
279 (defvar gnus-face-1 'italic)
280 (defvar gnus-face-2 'bold-italic)
281 (defvar gnus-face-3 'bold)
282 (defvar gnus-face-4 'bold)
284 (defun gnus-face-face-function (form type)
285 `(gnus-add-text-properties
286 (point) (progn ,@form (point))
287 '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
289 (defun gnus-balloon-face-function (form type)
290 `(gnus-put-text-property
291 (point) (progn ,@form (point))
293 ,(intern (format "gnus-balloon-face-%d" type))))
295 (defun gnus-spec-tab (column)
297 `(insert (make-string (max (- ,column (current-column)) 0) ? ))
298 (let ((column (abs column)))
299 (if gnus-use-correct-string-widths
301 (if (> (current-column) ,column)
303 (delete-backward-char 1)
304 (> (current-column) ,column))))
305 (insert (make-string (max (- ,column (current-column)) 0) ? )))
307 (if (> (current-column) ,column)
308 (delete-region (point)
309 (- (point) (- (current-column) ,column)))
310 (insert (make-string (max (- ,column (current-column)) 0)
313 (defun gnus-correct-length (string)
314 "Return the correct width of STRING."
316 (mapcar (lambda (char) (incf length (gnus-char-width char))) string)
319 (defun gnus-correct-substring (string start &optional end)
324 (length (length string))
325 (string (concat string "\0")))
326 ;; Find the start position.
327 (while (and (< seek length)
329 (incf wseek (gnus-char-width (aref string seek)))
332 ;; Find the end position.
333 (while (and (<= seek length)
336 (incf wseek (gnus-char-width (aref string seek)))
339 (substring string wstart (1- wend))))
341 (defun gnus-string-width-function ()
343 (gnus-use-correct-string-widths
344 'gnus-correct-length)
345 ((fboundp 'string-width)
350 (defun gnus-substring-function ()
352 (gnus-use-correct-string-widths
353 'gnus-correct-substring)
354 ((fboundp 'string-width)
355 'gnus-correct-substring)
359 (defun gnus-tilde-max-form (el max-width)
360 "Return a form that limits EL to MAX-WIDTH."
361 (let ((max (abs max-width))
362 (length-fun (gnus-string-width-function))
363 (substring-fun (gnus-substring-function)))
365 `(if (> (,length-fun ,el) ,max)
367 `(,substring-fun ,el (- (,length-fun ,el) ,max))
368 `(,substring-fun ,el 0 ,max))
370 `(let ((val (eval ,el)))
371 (if (> (,length-fun val) ,max)
373 `(,substring-fun val (- (,length-fun val) ,max))
374 `(,substring-fun val 0 ,max))
377 (defun gnus-tilde-cut-form (el cut-width)
378 "Return a form that cuts CUT-WIDTH off of EL."
379 (let ((cut (abs cut-width))
380 (length-fun (gnus-string-width-function))
381 (substring-fun (gnus-substring-function)))
383 `(if (> (,length-fun ,el) ,cut)
385 `(,substring-fun ,el 0 (- (,length-fun ,el) ,cut))
386 `(,substring-fun ,el ,cut))
388 `(let ((val (eval ,el)))
389 (if (> (,length-fun val) ,cut)
391 `(,substring-fun val 0 (- (,length-fun val) ,cut))
392 `(,substring-fun val ,cut))
395 (defun gnus-tilde-ignore-form (el ignore-value)
396 "Return a form that is blank when EL is IGNORE-VALUE."
398 `(if (equal ,el ,ignore-value)
400 `(let ((val (eval ,el)))
401 (if (equal val ,ignore-value)
404 (defun gnus-pad-form (el pad-width)
405 "Return a form that pads EL to PAD-WIDTH accounting for multi-column
406 characters correctly. This is because `format' may pad to columns or to
407 characters when given a pad value."
408 (let ((pad (abs pad-width))
409 (side (< 0 pad-width)))
411 `(let ((need (- ,pad (,(if gnus-use-correct-string-widths
416 (concat ,(when side '(make-string need ?\ ))
418 ,(when (not side) '(make-string need ?\ )))
420 `(let* ((val (eval ,el))
421 (need (- ,pad (,(if gnus-use-correct-string-widths
425 (concat ,(when side '(make-string need ?\ ))
427 ,(when (not side) '(make-string need ?\ )))
430 (defun gnus-parse-format (format spec-alist &optional insert)
431 ;; This function parses the FORMAT string with the help of the
432 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
433 ;; string. If the FORMAT string contains the specifiers %( and %)
434 ;; the text between them will have the mouse-face text property.
435 ;; If the FORMAT string contains the specifiers %[ and %], the text between
436 ;; them will have the balloon-help text property.
437 (let ((case-fold-search nil))
439 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'\\|%[-0-9]*="
441 (gnus-parse-complex-format format spec-alist)
442 ;; This is a simple format.
443 (gnus-parse-simple-format format spec-alist insert))))
445 (defun gnus-parse-complex-format (format spec-alist)
448 (gnus-set-work-buffer)
450 (goto-char (point-min))
451 (while (re-search-forward "\"" nil t)
452 (replace-match "\\\"" nil t))
453 (goto-char (point-min))
455 ;; Convert all font specs into font spec lists.
456 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
457 (let ((number (if (match-beginning 1)
458 (match-string 1) "0"))
459 (delim (aref (match-string 2) 0)))
460 (if (or (= delim ?\()
463 (replace-match (concat "\"("
464 (cond ((= delim ?\() "mouse")
465 ((= delim ?\{) "face")
469 (replace-match "\")\""))))
470 (goto-char (point-max))
472 ;; Convert point position commands.
473 (goto-char (point-min))
474 (let ((case-fold-search nil))
475 (while (re-search-forward "%\\([-0-9]+\\)?C" nil t)
476 (replace-match "\"(point)\"" t t)
478 ;; Convert TAB commands.
479 (goto-char (point-min))
480 (while (re-search-forward "%\\([-0-9]+\\)=" nil t)
481 (replace-match (format "\"(tab %s)\"" (match-string 1)) t t))
482 ;; Convert the buffer into the spec.
483 (goto-char (point-min))
484 (let ((form (read (current-buffer))))
486 `(let (gnus-position)
487 ,@(gnus-complex-form-to-spec form spec-alist)
489 (gnus-put-text-property gnus-position (1+ gnus-position)
492 ,@(gnus-complex-form-to-spec form spec-alist)))))))
494 (defun gnus-complex-form-to-spec (form spec-alist)
500 (gnus-parse-simple-format sform spec-alist t))
501 ((eq (car sform) 'point)
502 '(setq gnus-position (point)))
503 ((eq (car sform) 'tab)
504 (gnus-spec-tab (cadr sform)))
506 (funcall (intern (format "gnus-%s-face-function" (car sform)))
507 (gnus-complex-form-to-spec (cddr sform) spec-alist)
511 (defun gnus-parse-simple-format (format spec-alist &optional insert)
512 ;; This function parses the FORMAT string with the help of the
513 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
516 spec flist fstring elem result dontinsert user-defined
517 type value pad-width spec-beg cut-width ignore-value
518 tilde-form tilde elem-type extended-spec)
520 (gnus-set-work-buffer)
522 (goto-char (point-min))
523 (while (search-forward "%" nil t)
524 (setq user-defined nil
532 (setq spec-beg (1- (point)))
534 ;; Parse this spec fully.
537 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
538 (setq pad-width (string-to-number (match-string 1)))
539 (when (match-beginning 2)
540 (setq max-width (string-to-number (buffer-substring
541 (1+ (match-beginning 2))
543 (goto-char (match-end 0)))
546 (setq tilde (read (current-buffer))
550 ((memq type '(pad pad-left))
551 (setq pad-width value))
552 ((eq type 'pad-right)
553 (setq pad-width (- value)))
554 ((memq type '(max-right max))
555 (setq max-width value))
557 (setq max-width (- value)))
558 ((memq type '(cut cut-left))
559 (setq cut-width value))
560 ((eq type 'cut-right)
561 (setq cut-width (- value)))
564 (if (stringp value) value (format "%s" value))))
566 (setq tilde-form value))
568 (error "Unknown tilde type: %s" tilde)))
573 ;; User-defined spec -- find the spec name.
574 ((eq (setq spec (char-after)) ?u)
576 (when (and (eq (setq user-defined (char-after)) ?&)
577 (looking-at "&\\([^;]+\\);"))
578 (setq user-defined (match-string 1))
579 (goto-char (match-end 1))))
581 ((and (eq spec ?&) (looking-at "&\\([^;]+\\);"))
582 (setq extended-spec (intern (match-string 1)))
583 (goto-char (match-end 1))))
585 (delete-region spec-beg (point))
587 ;; Now we have all the relevant data on this spec, so
588 ;; we start doing stuff.
591 ;; "%%" just results in a "%".
596 (setq elem (list tilde-form ?s)))
597 ;; Treat user defined format specifiers specially.
601 (list (intern (format
602 (if (stringp user-defined)
603 "gnus-user-format-function-%s"
604 "gnus-user-format-function-%c")
608 ;; Find the specification from `spec-alist'.
609 ((setq elem (cdr (assq (or extended-spec spec) spec-alist))))
611 (setq elem '("*" ?s))))
612 (setq elem-type (cadr elem))
613 ;; Insert the new format elements.
615 (not (and (featurep 'xemacs)
616 gnus-use-correct-string-widths)))
617 (insert (number-to-string pad-width)))
618 ;; Create the form to be evaled.
619 (if (or max-width cut-width ignore-value
620 (and (featurep 'xemacs)
621 gnus-use-correct-string-widths))
624 (let ((el (car elem)))
625 (cond ((= (cadr elem) ?c)
626 (setq el (list 'char-to-string el)))
628 (setq el (list 'int-to-string el))))
630 (setq el (gnus-tilde-ignore-form el ignore-value)))
632 (setq el (gnus-tilde-cut-form el cut-width)))
634 (setq el (gnus-tilde-max-form el max-width)))
636 (setq el (gnus-pad-form el pad-width)))
639 (push (car elem) flist))))
640 (setq fstring (buffer-substring-no-properties (point-min) (point-max))))
642 ;; Do some postprocessing to increase efficiency.
647 ((string= fstring "")
649 ;; Not a format string.
650 ((not (string-match "%" fstring))
652 ;; A format string with just a single string spec.
653 ((string= fstring "%s")
655 ;; A single character.
656 ((string= fstring "%c")
659 ((string= fstring "%d")
662 (list `(princ ,(car flist)))
663 (list `(int-to-string ,(car flist)))))
664 ;; Just lots of chars and strings.
665 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
667 ;; A single string spec at the beginning of the spec.
668 ((string-match "\\`%[sc][^%]+\\'" fstring)
669 (list (car flist) (substring fstring 2)))
670 ;; A single string spec in the middle of the spec.
671 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
672 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
673 ;; A single string spec in the end of the spec.
674 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
675 (list (match-string 1 fstring) (car flist)))
676 ;; A more complex spec.
678 (list (cons 'format (cons fstring (nreverse flist)))))))
684 (cons 'insert result)))
685 (cond ((stringp result)
688 (cons 'concat result))
691 (defun gnus-eval-format (format &optional alist props)
692 "Eval the format variable FORMAT, using ALIST.
693 If PROPS, insert the result."
694 (let ((form (gnus-parse-format format alist props)))
696 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
699 (defun gnus-compile ()
700 "Byte-compile the user-defined format specs."
703 (let ((entries gnus-format-specs)
704 (byte-compile-warnings '(unresolved callargs redefine))
705 entry type compiled-function)
707 (gnus-message 7 "Compiling format specs...")
710 (setq entry (pop entries)
712 (if (memq type '(gnus-version version))
713 (setq gnus-format-specs (delq entry gnus-format-specs))
714 (let ((form (caddr entry)))
715 (when (and (listp form)
716 ;; Under GNU Emacs, it's (byte-code ...)
717 (not (eq 'byte-code (car form)))
718 ;; Under XEmacs, it's (funcall #<compiled-function ...>)
719 (not (and (eq 'funcall (car form))
720 (byte-code-function-p (cadr form)))))
721 (defalias 'gnus-tmp-func `(lambda () ,form))
722 (byte-compile 'gnus-tmp-func)
723 (setq compiled-function (gnus-byte-code 'gnus-tmp-func))
724 (set (intern (format "gnus-%s-line-format-spec" type))
726 (let ((elem (cdr (assq type gnus-format-specs-compiled))))
728 (set-alist 'elem (cadr entry) compiled-function)
729 (setq elem (list (cadr entry) compiled-function)))
730 (set-alist 'gnus-format-specs-compiled type elem))))))
732 (push (cons 'version emacs-version) gnus-format-specs)
733 (gnus-message 7 "Compiling user specs...done"))))
735 (defun gnus-set-format (type &optional insertable)
736 (set (intern (format "gnus-%s-line-format-spec" type))
738 (symbol-value (intern (format "gnus-%s-line-format" type)))
739 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
747 ;; coding: iso-8859-1
750 ;;; gnus-spec.el ends here