1 ;;; gnus-spec.el --- format spec functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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 2, or (at your option)
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; 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.
29 (eval-when-compile (require 'cl))
33 (defcustom gnus-use-correct-string-widths (featurep 'xemacs)
34 "*If non-nil, use correct functions for dealing with wide characters."
38 (defcustom gnus-make-format-preserve-properties (featurep 'xemacs)
39 "*If non-nil, use a replacement `format' function which preserves
40 text properties. This is only needed on XEmacs, as FSF Emacs does this anyway."
44 ;;; Internal variables.
46 (defvar gnus-summary-mark-positions nil)
47 (defvar gnus-group-mark-positions nil)
48 (defvar gnus-group-indentation "")
50 ;; Format specs. The chunks below are the machine-generated forms
51 ;; that are to be evaled as the result of the default format strings.
52 ;; We write them in here to get them byte-compiled. That way the
53 ;; default actions will be quite fast, while still retaining the full
54 ;; flexibility of the user-defined format specs.
56 ;; First we have lots of dummy defvars to let the compiler know these
57 ;; are really dynamic variables.
59 (defvar gnus-tmp-unread)
60 (defvar gnus-tmp-replied)
61 (defvar gnus-tmp-score-char)
62 (defvar gnus-tmp-indentation)
63 (defvar gnus-tmp-opening-bracket)
64 (defvar gnus-tmp-lines)
65 (defvar gnus-tmp-name)
66 (defvar gnus-tmp-closing-bracket)
67 (defvar gnus-tmp-subject-or-nil)
68 (defvar gnus-tmp-subject)
69 (defvar gnus-tmp-marked)
70 (defvar gnus-tmp-marked-mark)
71 (defvar gnus-tmp-subscribed)
72 (defvar gnus-tmp-process-marked)
73 (defvar gnus-tmp-number-of-unread)
74 (defvar gnus-tmp-group-name)
75 (defvar gnus-tmp-group)
76 (defvar gnus-tmp-article-number)
77 (defvar gnus-tmp-unread-and-unselected)
78 (defvar gnus-tmp-news-method)
79 (defvar gnus-tmp-news-server)
80 (defvar gnus-tmp-article-number)
81 (defvar gnus-mouse-face)
82 (defvar gnus-mouse-face-prop)
83 (defvar gnus-tmp-header)
84 (defvar gnus-tmp-from)
86 (defun gnus-summary-line-format-spec ()
87 (insert gnus-tmp-unread gnus-tmp-replied
88 gnus-tmp-score-char gnus-tmp-indentation)
89 (gnus-put-text-property
93 (format "%c%4s: %-23s%c" gnus-tmp-opening-bracket gnus-tmp-lines
96 (gnus-summary-from-or-to-or-newsgroups
97 gnus-tmp-header gnus-tmp-from))))
98 (if (> (length val) 23)
101 gnus-tmp-closing-bracket))
103 gnus-mouse-face-prop gnus-mouse-face)
104 (insert " " gnus-tmp-subject-or-nil "\n"))
106 (defvar gnus-summary-line-format-spec
107 (gnus-byte-code 'gnus-summary-line-format-spec))
109 (defun gnus-summary-dummy-line-format-spec ()
111 (gnus-put-text-property
116 gnus-mouse-face-prop gnus-mouse-face)
117 (insert " " gnus-tmp-subject "\n"))
119 (defvar gnus-summary-dummy-line-format-spec
120 (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
122 (defun gnus-group-line-format-spec ()
123 (insert gnus-tmp-marked-mark gnus-tmp-subscribed
124 gnus-tmp-process-marked
125 gnus-group-indentation
126 (format "%5s: " gnus-tmp-number-of-unread))
127 (gnus-put-text-property
130 (insert gnus-tmp-group "\n")
132 gnus-mouse-face-prop gnus-mouse-face))
133 (defvar gnus-group-line-format-spec
134 (gnus-byte-code 'gnus-group-line-format-spec))
136 (defvar gnus-format-specs
137 `((version . ,emacs-version)
138 (gnus-version . ,(gnus-continuum-version))
139 (group "%M\%S\%p\%P\%5y: %(%g%)%l\n" ,gnus-group-line-format-spec)
140 (summary-dummy "* %(: :%) %S\n"
141 ,gnus-summary-dummy-line-format-spec)
142 (summary "%U%R%z%I%(%[%4L: %-23,23f%]%) %s\n"
143 ,gnus-summary-line-format-spec))
144 "Alist of format specs.")
146 (defvar gnus-default-format-specs gnus-format-specs)
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 (entry (assq type gnus-format-specs))
171 (setq gnus-format-specs (delq entry gnus-format-specs)))
173 (intern (format "%s-spec" var))
174 (gnus-parse-format (setq value (symbol-value (intern var)))
175 (symbol-value (intern (format "%s-alist" var)))
176 (not (string-match "mode" var))))
177 (setq spec (symbol-value (intern (format "%s-spec" var))))
178 (push (list type value spec) gnus-format-specs)
180 (pop-to-buffer "*Gnus Format*")
182 (lisp-interaction-mode)
183 (insert (pp-to-string spec))))
185 (defun gnus-update-format-specifications (&optional force &rest types)
186 "Update all (necessary) format specifications."
187 ;; Make the indentation array.
188 ;; See whether all the stored info needs to be flushed.
190 (not gnus-newsrc-file-version)
191 (not (equal (gnus-continuum-version)
192 (gnus-continuum-version gnus-newsrc-file-version)))
193 (not (equal emacs-version
194 (cdr (assq 'version gnus-format-specs)))))
195 (setq gnus-format-specs nil))
197 ;; Go through all the formats and see whether they need updating.
198 (let (new-format entry type val)
199 (while (setq type (pop types))
200 ;; Jump to the proper buffer to find out the value of the
201 ;; variable, if possible. (It may be buffer-local.)
203 (let ((buffer (intern (format "gnus-%s-buffer" type)))
205 (when (and (boundp buffer)
206 (setq val (symbol-value buffer))
207 (gnus-buffer-exists-p val))
209 (setq new-format (symbol-value
210 (intern (format "gnus-%s-line-format" type)))))
211 (setq entry (cdr (assq type gnus-format-specs)))
213 (equal (car entry) new-format))
214 ;; Use the old format.
215 (set (intern (format "gnus-%s-line-format-spec" type))
217 ;; This is a new format.
219 (if (not (stringp new-format))
220 ;; This is a function call or something.
222 ;; This is a "real" format.
226 (intern (format "gnus-%s-line-format-alist" type)))
227 (not (string-match "mode$" (symbol-name type))))))
228 ;; Enter the new format spec into the list.
231 (setcar (cdr entry) val)
232 (setcar entry new-format))
233 (push (list type new-format val) gnus-format-specs))
234 (set (intern (format "gnus-%s-line-format-spec" type)) val)))))
236 (unless (assq 'version gnus-format-specs)
237 (push (cons 'version emacs-version) gnus-format-specs)))
239 (defvar gnus-mouse-face-0 'highlight)
240 (defvar gnus-mouse-face-1 'highlight)
241 (defvar gnus-mouse-face-2 'highlight)
242 (defvar gnus-mouse-face-3 'highlight)
243 (defvar gnus-mouse-face-4 'highlight)
245 (defun gnus-mouse-face-function (form type)
246 `(gnus-put-text-property
247 (point) (progn ,@form (point))
251 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
253 (defvar gnus-face-0 'bold)
254 (defvar gnus-face-1 'italic)
255 (defvar gnus-face-2 'bold-italic)
256 (defvar gnus-face-3 'bold)
257 (defvar gnus-face-4 'bold)
259 (defun gnus-face-face-function (form type)
260 `(gnus-add-text-properties
261 (point) (progn ,@form (point))
262 '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
264 (defun gnus-balloon-face-function (form type)
265 `(gnus-put-text-property
266 (point) (progn ,@form (point))
267 ,(if (fboundp 'balloon-help-mode)
270 ,(intern (format "gnus-balloon-face-%d" type))))
272 (defun gnus-spec-tab (column)
274 `(insert (make-string (max (- ,column (current-column)) 0) ? ))
275 (let ((column (abs column)))
276 (if gnus-use-correct-string-widths
278 (if (> (current-column) ,column)
280 (delete-backward-char 1)
281 (> (current-column) ,column))))
282 (insert (make-string (max (- ,column (current-column)) 0) ? )))
284 (if (> (current-column) ,column)
285 (delete-region (point)
286 (- (point) (- (current-column) ,column)))
287 (insert (make-string (max (- ,column (current-column)) 0)
290 (defun gnus-correct-length (string)
291 "Return the correct width of STRING."
293 (mapcar (lambda (char) (incf length (gnus-char-width char))) string)
296 (defun gnus-correct-substring (string start &optional end)
301 (length (length string))
302 (string (concat string "\0")))
303 ;; Find the start position.
304 (while (and (< seek length)
306 (incf wseek (gnus-char-width (aref string seek)))
309 ;; Find the end position.
310 (while (and (<= seek length)
313 (incf wseek (gnus-char-width (aref string seek)))
316 (substring string wstart (1- wend))))
318 (defun gnus-string-width-function ()
320 (gnus-use-correct-string-widths
321 'gnus-correct-length)
322 ((fboundp 'string-width)
327 (defun gnus-substring-function ()
329 (gnus-use-correct-string-widths
330 'gnus-correct-substring)
331 ((fboundp 'string-width)
332 'gnus-correct-substring)
336 (defun gnus-tilde-max-form (el max-width)
337 "Return a form that limits EL to MAX-WIDTH."
338 (let ((max (abs max-width))
339 (length-fun (gnus-string-width-function))
340 (substring-fun (gnus-substring-function)))
342 `(if (> (,length-fun ,el) ,max)
344 `(,substring-fun ,el (- (,length-fun ,el) ,max))
345 `(,substring-fun ,el 0 ,max))
347 `(let ((val (eval ,el)))
348 (if (> (,length-fun val) ,max)
350 `(,substring-fun val (- (,length-fun val) ,max))
351 `(,substring-fun val 0 ,max))
354 (defun gnus-tilde-cut-form (el cut-width)
355 "Return a form that cuts CUT-WIDTH off of EL."
356 (let ((cut (abs cut-width))
357 (length-fun (gnus-string-width-function))
358 (substring-fun (gnus-substring-function)))
360 `(if (> (,length-fun ,el) ,cut)
362 `(,substring-fun ,el 0 (- (,length-fun ,el) ,cut))
363 `(,substring-fun ,el ,cut))
365 `(let ((val (eval ,el)))
366 (if (> (,length-fun val) ,cut)
368 `(,substring-fun val 0 (- (,length-fun val) ,cut))
369 `(,substring-fun val ,cut))
372 (defun gnus-tilde-ignore-form (el ignore-value)
373 "Return a form that is blank when EL is IGNORE-VALUE."
375 `(if (equal ,el ,ignore-value)
377 `(let ((val (eval ,el)))
378 (if (equal val ,ignore-value)
381 (defun gnus-pad-form (el pad-width)
382 "Return a form that pads EL to PAD-WIDTH accounting for multi-column
383 characters correctly. This is because `format' may pad to columns or to
384 characters when given a pad value."
385 (let ((pad (abs pad-width))
386 (side (< 0 pad-width))
387 (length-fun (gnus-string-width-function)))
389 `(let ((need (- ,pad (,length-fun ,el))))
391 (concat ,(when side '(make-string need ?\ ))
393 ,(when (not side) '(make-string need ?\ )))
395 `(let* ((val (eval ,el))
396 (need (- ,pad (,length-fun val))))
398 (concat ,(when side '(make-string need ?\ ))
400 ,(when (not side) '(make-string need ?\ )))
403 (defun gnus-parse-format (format spec-alist &optional insert)
404 ;; This function parses the FORMAT string with the help of the
405 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
406 ;; string. If the FORMAT string contains the specifiers %( and %)
407 ;; the text between them will have the mouse-face text property.
408 ;; If the FORMAT string contains the specifiers %[ and %], the text between
409 ;; them will have the balloon-help text property.
410 (let ((case-fold-search nil))
412 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'\\|%[-0-9]*=\\|%[-0-9]*\\*"
414 (gnus-parse-complex-format format spec-alist)
415 ;; This is a simple format.
416 (gnus-parse-simple-format format spec-alist insert))))
418 (defun gnus-parse-complex-format (format spec-alist)
419 (let ((cursor-spec nil))
421 (gnus-set-work-buffer)
423 (goto-char (point-min))
424 (while (re-search-forward "\"" nil t)
425 (replace-match "\\\"" nil t))
426 (goto-char (point-min))
428 ;; Convert all font specs into font spec lists.
429 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
430 (let ((number (if (match-beginning 1)
431 (match-string 1) "0"))
432 (delim (aref (match-string 2) 0)))
433 (if (or (= delim ?\()
436 (replace-match (concat "\"("
437 (cond ((= delim ?\() "mouse")
438 ((= delim ?\{) "face")
442 (replace-match "\")\""))))
443 (goto-char (point-max))
445 ;; Convert point position commands.
446 (goto-char (point-min))
447 (let ((case-fold-search nil))
448 (while (re-search-forward "%\\([-0-9]+\\)?\\*" nil t)
449 (replace-match "\"(point)\"" t t)
450 (setq cursor-spec t)))
451 ;; Convert TAB commands.
452 (goto-char (point-min))
453 (while (re-search-forward "%\\([-0-9]+\\)=" nil t)
454 (replace-match (format "\"(tab %s)\"" (match-string 1)) t t))
455 ;; Convert the buffer into the spec.
456 (goto-char (point-min))
457 (let ((form (read (current-buffer))))
459 `(let (gnus-position)
460 ,@(gnus-complex-form-to-spec form spec-alist)
462 (gnus-put-text-property gnus-position (1+ gnus-position)
465 ,@(gnus-complex-form-to-spec form spec-alist)))))))
467 (defun gnus-complex-form-to-spec (form spec-alist)
473 (gnus-parse-simple-format sform spec-alist t))
474 ((eq (car sform) 'point)
475 '(setq gnus-position (point)))
476 ((eq (car sform) 'tab)
477 (gnus-spec-tab (cadr sform)))
479 (funcall (intern (format "gnus-%s-face-function" (car sform)))
480 (gnus-complex-form-to-spec (cddr sform) spec-alist)
485 (defun gnus-xmas-format (fstring &rest args)
486 "A version of `format' which preserves text properties.
488 Required for XEmacs, where the built in `format' function strips all text
489 properties from both the format string and any inserted strings.
491 Only supports the format sequence %s, and %% for inserting
492 literal % characters. A pad width and an optional - (to right pad)
493 are supported for %s."
494 (let ((re "%%\\|%\\(-\\)?\\([1-9][0-9]*\\)?s")
498 (goto-char (point-min))
499 (while (re-search-forward re nil t)
500 (goto-char (match-end 0))
502 ((string= (match-string 0) "%%")
506 (error 'wrong-number-of-arguments #'my-format n fstring))
507 (let* ((minlen (string-to-int (or (match-string 2) "")))
509 (str (if (stringp arg) arg (format "%s" arg)))
510 (lpad (null (match-string 1)))
511 (padlen (max 0 (- minlen (length str)))))
513 (if lpad (insert-char ?\ padlen))
515 (unless lpad (insert-char ?\ padlen))
516 (setq args (cdr args))))))
519 (defun gnus-parse-simple-format (format spec-alist &optional insert)
520 ;; This function parses the FORMAT string with the help of the
521 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
524 spec flist fstring elem result dontinsert user-defined
525 type value pad-width spec-beg cut-width ignore-value
526 tilde-form tilde elem-type extended-spec)
528 (gnus-set-work-buffer)
530 (goto-char (point-min))
531 (while (re-search-forward "%" nil t)
532 (setq user-defined nil
540 (setq spec-beg (1- (point)))
542 ;; Parse this spec fully.
545 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
546 (setq pad-width (string-to-number (match-string 1)))
547 (when (match-beginning 2)
548 (setq max-width (string-to-number (buffer-substring
549 (1+ (match-beginning 2))
551 (goto-char (match-end 0)))
554 (setq tilde (read (current-buffer))
558 ((memq type '(pad pad-left))
559 (setq pad-width value))
560 ((eq type 'pad-right)
561 (setq pad-width (- value)))
562 ((memq type '(max-right max))
563 (setq max-width value))
565 (setq max-width (- value)))
566 ((memq type '(cut cut-left))
567 (setq cut-width value))
568 ((eq type 'cut-right)
569 (setq cut-width (- value)))
572 (if (stringp value) value (format "%s" value))))
574 (setq tilde-form value))
576 (error "Unknown tilde type: %s" tilde)))
581 ;; User-defined spec -- find the spec name.
582 ((eq (setq spec (char-after)) ?u)
584 (when (and (eq (setq user-defined (char-after)) ?&)
585 (looking-at "&\\([^;]+\\);"))
586 (setq user-defined (match-string 1))
587 (goto-char (match-end 1))))
589 ((and (eq spec ?&) (looking-at "&\\([^;]+\\);"))
590 (setq extended-spec (intern (match-string 1)))
591 (goto-char (match-end 1))))
593 (delete-region spec-beg (point))
595 ;; Now we have all the relevant data on this spec, so
596 ;; we start doing stuff.
599 ;; "%%" just results in a "%".
604 (setq elem (list tilde-form ?s)))
605 ;; Treat user defined format specifiers specially.
609 (list (intern (format
610 (if (stringp user-defined)
611 "gnus-user-format-function-%s"
612 "gnus-user-format-function-%c")
616 ;; Find the specification from `spec-alist'.
617 ((setq elem (cdr (assq (or extended-spec spec) spec-alist))))
619 (setq elem '("*" ?s))))
620 (setq elem-type (cadr elem))
621 ;; Insert the new format elements.
623 (not (and (featurep 'xemacs)
624 gnus-use-correct-string-widths)))
625 (insert (number-to-string pad-width)))
626 ;; Create the form to be evaled.
627 (if (or max-width cut-width ignore-value
628 (and (featurep 'xemacs)
629 gnus-use-correct-string-widths))
632 (let ((el (car elem)))
633 (cond ((= (cadr elem) ?c)
634 (setq el (list 'char-to-string el)))
636 (setq el (list 'int-to-string el))))
638 (setq el (gnus-tilde-ignore-form el ignore-value)))
640 (setq el (gnus-tilde-cut-form el cut-width)))
642 (setq el (gnus-tilde-max-form el max-width)))
644 (setq el (gnus-pad-form el pad-width)))
647 (push (car elem) flist))))
648 (setq fstring (buffer-substring-no-properties (point-min) (point-max))))
650 ;; Do some postprocessing to increase efficiency.
655 ((string= fstring "")
657 ;; Not a format string.
658 ((not (string-match "%" fstring))
660 ;; A format string with just a single string spec.
661 ((string= fstring "%s")
663 ;; A single character.
664 ((string= fstring "%c")
667 ((string= fstring "%d")
670 (list `(princ ,(car flist)))
671 (list `(int-to-string ,(car flist)))))
672 ;; Just lots of chars and strings.
673 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
675 ;; A single string spec at the beginning of the spec.
676 ((string-match "\\`%[sc][^%]+\\'" fstring)
677 (list (car flist) (substring fstring 2)))
678 ;; A single string spec in the middle of the spec.
679 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
680 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
681 ;; A single string spec in the end of the spec.
682 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
683 (list (match-string 1 fstring) (car flist)))
684 ;; Only string (and %) specs (XEmacs only!)
685 ((and (featurep 'xemacs)
686 gnus-make-format-preserve-properties
688 "\\`\\([^%]*\\(%%\\|%-?\\([1-9][0-9]*\\)?s\\)\\)*[^%]*\\'"
690 (list (cons 'gnus-xmas-format (cons fstring (nreverse flist)))))
691 ;; A more complex spec.
693 (list (cons 'format (cons fstring (nreverse flist)))))))
699 (cons 'insert result)))
700 (cond ((stringp result)
703 (cons 'concat result))
706 (defun gnus-eval-format (format &optional alist props)
707 "Eval the format variable FORMAT, using ALIST.
708 If PROPS, insert the result."
709 (let ((form (gnus-parse-format format alist props)))
711 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
714 (defun gnus-compile ()
715 "Byte-compile the user-defined format specs."
718 (let ((entries gnus-format-specs)
719 (byte-compile-warnings '(unresolved callargs redefine))
722 (gnus-message 7 "Compiling format specs...")
725 (setq entry (pop entries))
726 (if (memq (car entry) '(gnus-version version))
727 (setq gnus-format-specs (delq entry gnus-format-specs))
728 (let ((form (caddr entry)))
729 (when (and (listp form)
730 ;; Under GNU Emacs, it's (byte-code ...)
731 (not (eq 'byte-code (car form)))
732 ;; Under XEmacs, it's (funcall #<compiled-function ...>)
733 (not (and (eq 'funcall (car form))
734 (byte-code-function-p (cadr form)))))
735 (defalias 'gnus-tmp-func `(lambda () ,form))
736 (byte-compile 'gnus-tmp-func)
737 (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func))))))
739 (push (cons 'version emacs-version) gnus-format-specs)
740 ;; Mark the .newsrc.eld file as "dirty".
742 (gnus-message 7 "Compiling user specs...done"))))
744 (defun gnus-set-format (type &optional insertable)
745 (set (intern (format "gnus-%s-line-format-spec" type))
747 (symbol-value (intern (format "gnus-%s-line-format" type)))
748 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
754 ;; coding: iso-8859-1
757 ;;; gnus-spec.el ends here