1 ;;; gnus-spec.el --- format spec functions for Gnus
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 (eval-when-compile (require 'cl))
34 (defcustom gnus-use-correct-string-widths (featurep 'xemacs)
35 "*If non-nil, use correct functions for dealing with wide characters."
40 (defcustom gnus-make-format-preserve-properties (featurep 'xemacs)
41 "*If non-nil, use a replacement `format' function which preserves
42 text properties. This is only needed on XEmacs, as FSF Emacs does this anyway."
47 ;;; Internal variables.
49 (defvar gnus-summary-mark-positions nil)
50 (defvar gnus-group-mark-positions nil)
51 (defvar gnus-group-indentation "")
53 ;; Format specs. The chunks below are the machine-generated forms
54 ;; that are to be evaled as the result of the default format strings.
55 ;; We write them in here to get them byte-compiled. That way the
56 ;; default actions will be quite fast, while still retaining the full
57 ;; flexibility of the user-defined format specs.
59 ;; First we have lots of dummy defvars to let the compiler know these
60 ;; are really dynamic variables.
62 (defvar gnus-tmp-unread)
63 (defvar gnus-tmp-replied)
64 (defvar gnus-tmp-score-char)
65 (defvar gnus-tmp-indentation)
66 (defvar gnus-tmp-opening-bracket)
67 (defvar gnus-tmp-lines)
68 (defvar gnus-tmp-name)
69 (defvar gnus-tmp-closing-bracket)
70 (defvar gnus-tmp-subject-or-nil)
71 (defvar gnus-tmp-subject)
72 (defvar gnus-tmp-marked)
73 (defvar gnus-tmp-marked-mark)
74 (defvar gnus-tmp-subscribed)
75 (defvar gnus-tmp-process-marked)
76 (defvar gnus-tmp-number-of-unread)
77 (defvar gnus-tmp-group-name)
78 (defvar gnus-tmp-group)
79 (defvar gnus-tmp-article-number)
80 (defvar gnus-tmp-unread-and-unselected)
81 (defvar gnus-tmp-news-method)
82 (defvar gnus-tmp-news-server)
83 (defvar gnus-tmp-article-number)
84 (defvar gnus-mouse-face)
85 (defvar gnus-mouse-face-prop)
86 (defvar gnus-tmp-header)
87 (defvar gnus-tmp-from)
89 (defun gnus-summary-line-format-spec ()
90 (insert gnus-tmp-unread gnus-tmp-replied
91 gnus-tmp-score-char gnus-tmp-indentation)
92 (gnus-put-text-property
96 (format "%c%4s: %-23s%c" gnus-tmp-opening-bracket gnus-tmp-lines
99 (gnus-summary-from-or-to-or-newsgroups
100 gnus-tmp-header gnus-tmp-from))))
101 (if (> (length val) 23)
104 gnus-tmp-closing-bracket))
106 gnus-mouse-face-prop gnus-mouse-face)
107 (insert " " gnus-tmp-subject-or-nil "\n"))
109 (defvar gnus-summary-line-format-spec
110 (gnus-byte-code 'gnus-summary-line-format-spec))
112 (defun gnus-summary-dummy-line-format-spec ()
114 (gnus-put-text-property
119 gnus-mouse-face-prop gnus-mouse-face)
120 (insert " " gnus-tmp-subject "\n"))
122 (defvar gnus-summary-dummy-line-format-spec
123 (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
125 (defun gnus-group-line-format-spec ()
126 (insert gnus-tmp-marked-mark gnus-tmp-subscribed
127 gnus-tmp-process-marked
128 gnus-group-indentation
129 (format "%5s: " gnus-tmp-number-of-unread))
130 (gnus-put-text-property
133 (insert gnus-tmp-group "\n")
135 gnus-mouse-face-prop gnus-mouse-face))
136 (defvar gnus-group-line-format-spec
137 (gnus-byte-code 'gnus-group-line-format-spec))
139 (defvar gnus-format-specs
140 `((version . ,emacs-version)
141 (gnus-version . ,(gnus-continuum-version))
142 (group "%M\%S\%p\%P\%5y: %(%g%)%l\n" ,gnus-group-line-format-spec)
143 (summary-dummy "* %(: :%) %S\n"
144 ,gnus-summary-dummy-line-format-spec)
145 (summary "%U%R%z%I%(%[%4L: %-23,23f%]%) %s\n"
146 ,gnus-summary-line-format-spec))
147 "Alist of format specs.")
149 (defvar gnus-default-format-specs gnus-format-specs)
151 (defvar gnus-article-mode-line-format-spec nil)
152 (defvar gnus-summary-mode-line-format-spec nil)
153 (defvar gnus-group-mode-line-format-spec nil)
155 ;;; Phew. All that gruft is over with, fortunately.
158 (defun gnus-update-format (var)
159 "Update the format specification near point."
164 ;; Find the end of the current word.
165 (re-search-forward "[ \t\n]" nil t)
167 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
169 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
170 (match-string 1 var))))
171 (entry (assq type gnus-format-specs))
174 (setq gnus-format-specs (delq entry gnus-format-specs)))
176 (intern (format "%s-spec" var))
177 (gnus-parse-format (setq value (symbol-value (intern var)))
178 (symbol-value (intern (format "%s-alist" var)))
179 (not (string-match "mode" var))))
180 (setq spec (symbol-value (intern (format "%s-spec" var))))
181 (push (list type value spec) gnus-format-specs)
183 (pop-to-buffer "*Gnus Format*")
185 (lisp-interaction-mode)
186 (insert (gnus-pp-to-string spec))))
188 (defun gnus-update-format-specifications (&optional force &rest types)
189 "Update all (necessary) format specifications.
190 Return a list of updated types."
191 ;; Make the indentation array.
192 ;; See whether all the stored info needs to be flushed.
194 (not gnus-newsrc-file-version)
195 (not (equal (gnus-continuum-version)
196 (gnus-continuum-version gnus-newsrc-file-version)))
197 (not (equal emacs-version
198 (cdr (assq 'version gnus-format-specs)))))
199 (setq gnus-format-specs nil))
200 ;; Flush the group format spec cache if it doesn't support decoded
202 (when (memq 'group types)
203 (let ((spec (assq 'group gnus-format-specs)))
204 (unless (string-match " gnus-tmp-decoded-group[ )]"
205 (gnus-prin1-to-string (nth 2 spec)))
206 (setq gnus-format-specs (delq spec gnus-format-specs)))))
208 ;; Go through all the formats and see whether they need updating.
209 (let (new-format entry type val updated)
210 (while (setq type (pop types))
211 ;; Jump to the proper buffer to find out the value of the
212 ;; variable, if possible. (It may be buffer-local.)
214 (let ((buffer (intern (format "gnus-%s-buffer" type))))
215 (when (and (boundp buffer)
216 (setq val (symbol-value buffer))
217 (gnus-buffer-exists-p val))
219 (setq new-format (symbol-value
220 (intern (format "gnus-%s-line-format" type)))))
221 (setq entry (cdr (assq type gnus-format-specs)))
223 (equal (car entry) new-format))
224 ;; Use the old format.
225 (set (intern (format "gnus-%s-line-format-spec" type))
227 ;; This is a new format.
229 (if (not (stringp new-format))
230 ;; This is a function call or something.
232 ;; This is a "real" format.
236 (intern (format "gnus-%s-line-format-alist" type)))
237 (not (string-match "mode$" (symbol-name type))))))
238 ;; Enter the new format spec into the list.
241 (setcar (cdr entry) val)
242 (setcar entry new-format))
243 (push (list type new-format val) gnus-format-specs))
244 (set (intern (format "gnus-%s-line-format-spec" type)) val)
245 (push type updated))))
247 (unless (assq 'version gnus-format-specs)
248 (push (cons 'version emacs-version) gnus-format-specs))
251 (defvar gnus-mouse-face-0 'highlight)
252 (defvar gnus-mouse-face-1 'highlight)
253 (defvar gnus-mouse-face-2 'highlight)
254 (defvar gnus-mouse-face-3 'highlight)
255 (defvar gnus-mouse-face-4 'highlight)
257 (defun gnus-mouse-face-function (form type)
258 `(gnus-put-text-property
259 (point) (progn ,@form (point))
263 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
265 (defvar gnus-face-0 'bold)
266 (defvar gnus-face-1 'italic)
267 (defvar gnus-face-2 'bold-italic)
268 (defvar gnus-face-3 'bold)
269 (defvar gnus-face-4 'bold)
271 (defun gnus-face-face-function (form type)
272 `(gnus-add-text-properties
273 (point) (progn ,@form (point))
274 '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
276 (defun gnus-balloon-face-function (form type)
277 `(gnus-put-text-property
278 (point) (progn ,@form (point))
279 ,(if (fboundp 'balloon-help-mode)
282 ,(intern (format "gnus-balloon-face-%d" type))))
284 (defun gnus-spec-tab (column)
286 `(insert-char ? (max (- ,column (current-column)) 0))
287 (let ((column (abs column)))
288 `(if (> (current-column) ,column)
290 (if (= (move-to-column ,column) ,column)
291 (delete-region (point) end)
292 (delete-region (1- (point)) end)
294 (insert-char ? (max (- ,column (current-column)) 0))))))
296 (defun gnus-correct-length (string)
297 "Return the correct width of STRING."
299 (mapcar (lambda (char) (incf length (gnus-char-width char))) string)
302 (defun gnus-correct-substring (string start &optional end)
307 (length (length string))
308 (string (concat string "\0")))
309 ;; Find the start position.
310 (while (and (< seek length)
312 (incf wseek (gnus-char-width (aref string seek)))
315 ;; Find the end position.
316 (while (and (<= seek length)
319 (incf wseek (gnus-char-width (aref string seek)))
322 (substring string wstart (1- wend))))
324 (defun gnus-string-width-function ()
326 (gnus-use-correct-string-widths
327 'gnus-correct-length)
328 ((fboundp 'string-width)
333 (defun gnus-substring-function ()
335 (gnus-use-correct-string-widths
336 'gnus-correct-substring)
337 ((fboundp 'string-width)
338 'gnus-correct-substring)
342 (defun gnus-tilde-max-form (el max-width)
343 "Return a form that limits EL to MAX-WIDTH."
344 (let ((max (abs max-width))
345 (length-fun (gnus-string-width-function))
346 (substring-fun (gnus-substring-function)))
348 `(if (> (,length-fun ,el) ,max)
350 `(,substring-fun ,el (- (,length-fun ,el) ,max))
351 `(,substring-fun ,el 0 ,max))
353 `(let ((val (eval ,el)))
354 (if (> (,length-fun val) ,max)
356 `(,substring-fun val (- (,length-fun val) ,max))
357 `(,substring-fun val 0 ,max))
360 (defun gnus-tilde-cut-form (el cut-width)
361 "Return a form that cuts CUT-WIDTH off of EL."
362 (let ((cut (abs cut-width))
363 (length-fun (gnus-string-width-function))
364 (substring-fun (gnus-substring-function)))
366 `(if (> (,length-fun ,el) ,cut)
368 `(,substring-fun ,el 0 (- (,length-fun ,el) ,cut))
369 `(,substring-fun ,el ,cut))
371 `(let ((val (eval ,el)))
372 (if (> (,length-fun val) ,cut)
374 `(,substring-fun val 0 (- (,length-fun val) ,cut))
375 `(,substring-fun val ,cut))
378 (defun gnus-tilde-ignore-form (el ignore-value)
379 "Return a form that is blank when EL is IGNORE-VALUE."
381 `(if (equal ,el ,ignore-value)
383 `(let ((val (eval ,el)))
384 (if (equal val ,ignore-value)
387 (defun gnus-pad-form (el pad-width)
388 "Return a form that pads EL to PAD-WIDTH accounting for multi-column
389 characters correctly. This is because `format' may pad to columns or to
390 characters when given a pad value."
391 (let ((pad (abs pad-width))
392 (side (< 0 pad-width))
393 (length-fun (gnus-string-width-function)))
395 `(let ((need (- ,pad (,length-fun ,el))))
397 (concat ,(when side '(make-string need ?\ ))
399 ,(when (not side) '(make-string need ?\ )))
401 `(let* ((val (eval ,el))
402 (need (- ,pad (,length-fun val))))
404 (concat ,(when side '(make-string need ?\ ))
406 ,(when (not side) '(make-string need ?\ )))
409 (defun gnus-parse-format (format spec-alist &optional insert)
410 ;; This function parses the FORMAT string with the help of the
411 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
412 ;; string. If the FORMAT string contains the specifiers %( and %)
413 ;; the text between them will have the mouse-face text property.
414 ;; If the FORMAT string contains the specifiers %[ and %], the text between
415 ;; them will have the balloon-help text property.
416 (let ((case-fold-search nil))
418 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'\\|%[-0-9]*=\\|%[-0-9]*\\*"
420 (gnus-parse-complex-format format spec-alist)
421 ;; This is a simple format.
422 (gnus-parse-simple-format format spec-alist insert))))
424 (defun gnus-parse-complex-format (format spec-alist)
425 (let ((cursor-spec nil))
427 (gnus-set-work-buffer)
429 (goto-char (point-min))
430 (while (re-search-forward "\"" nil t)
431 (replace-match "\\\"" nil t))
432 (goto-char (point-min))
434 ;; Convert all font specs into font spec lists.
435 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
436 (let ((number (if (match-beginning 1)
437 (match-string 1) "0"))
438 (delim (aref (match-string 2) 0)))
439 (if (or (= delim ?\()
442 (replace-match (concat "\"("
443 (cond ((= delim ?\() "mouse")
444 ((= delim ?\{) "face")
448 (replace-match "\")\""))))
449 (goto-char (point-max))
451 ;; Convert point position commands.
452 (goto-char (point-min))
453 (let ((case-fold-search nil))
454 (while (re-search-forward "%\\([-0-9]+\\)?\\*" nil t)
455 (replace-match "\"(point)\"" t t)
456 (setq cursor-spec t)))
457 ;; Convert TAB commands.
458 (goto-char (point-min))
459 (while (re-search-forward "%\\([-0-9]+\\)=" nil t)
460 (replace-match (format "\"(tab %s)\"" (match-string 1)) t t))
461 ;; Convert the buffer into the spec.
462 (goto-char (point-min))
463 (let ((form (read (current-buffer))))
465 `(let (gnus-position)
466 ,@(gnus-complex-form-to-spec form spec-alist)
468 (gnus-put-text-property gnus-position (1+ gnus-position)
471 ,@(gnus-complex-form-to-spec form spec-alist)))))))
473 (defun gnus-complex-form-to-spec (form spec-alist)
479 (gnus-parse-simple-format sform spec-alist t))
480 ((eq (car sform) 'point)
481 '(setq gnus-position (point)))
482 ((eq (car sform) 'tab)
483 (gnus-spec-tab (cadr sform)))
485 (funcall (intern (format "gnus-%s-face-function" (car sform)))
486 (gnus-complex-form-to-spec (cddr sform) spec-alist)
491 (defun gnus-xmas-format (fstring &rest args)
492 "A version of `format' which preserves text properties.
494 Required for XEmacs, where the built in `format' function strips all text
495 properties from both the format string and any inserted strings.
497 Only supports the format sequence %s, and %% for inserting
498 literal % characters. A pad width and an optional - (to right pad)
499 are supported for %s."
500 (let ((re "%%\\|%\\(-\\)?\\([1-9][0-9]*\\)?s")
504 (goto-char (point-min))
505 (while (re-search-forward re nil t)
506 (goto-char (match-end 0))
508 ((string= (match-string 0) "%%")
512 (error 'wrong-number-of-arguments #'my-format n fstring))
513 (let* ((minlen (string-to-number (or (match-string 2) "")))
515 (str (if (stringp arg) arg (format "%s" arg)))
516 (lpad (null (match-string 1)))
517 (padlen (max 0 (- minlen (length str)))))
519 (if lpad (insert-char ?\ padlen))
521 (unless lpad (insert-char ?\ padlen))
522 (setq args (cdr args))))))
525 (defun gnus-parse-simple-format (format spec-alist &optional insert)
526 ;; This function parses the FORMAT string with the help of the
527 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
530 spec flist fstring elem result dontinsert user-defined
531 type value pad-width spec-beg cut-width ignore-value
532 tilde-form tilde elem-type extended-spec)
534 (gnus-set-work-buffer)
536 (goto-char (point-min))
537 (while (re-search-forward "%" nil t)
538 (setq user-defined nil
546 (setq spec-beg (1- (point)))
548 ;; Parse this spec fully.
551 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
552 (setq pad-width (string-to-number (match-string 1)))
553 (when (match-beginning 2)
554 (setq max-width (string-to-number (buffer-substring
555 (1+ (match-beginning 2))
557 (goto-char (match-end 0)))
560 (setq tilde (read (current-buffer))
564 ((memq type '(pad pad-left))
565 (setq pad-width value))
566 ((eq type 'pad-right)
567 (setq pad-width (- value)))
568 ((memq type '(max-right max))
569 (setq max-width value))
571 (setq max-width (- value)))
572 ((memq type '(cut cut-left))
573 (setq cut-width value))
574 ((eq type 'cut-right)
575 (setq cut-width (- value)))
578 (if (stringp value) value (format "%s" value))))
580 (setq tilde-form value))
582 (error "Unknown tilde type: %s" tilde)))
587 ;; User-defined spec -- find the spec name.
588 ((eq (setq spec (char-after)) ?u)
590 (when (and (eq (setq user-defined (char-after)) ?&)
591 (looking-at "&\\([^;]+\\);"))
592 (setq user-defined (match-string 1))
593 (goto-char (match-end 1))))
595 ((and (eq spec ?&) (looking-at "&\\([^;]+\\);"))
596 (setq extended-spec (intern (match-string 1)))
597 (goto-char (match-end 1))))
599 (delete-region spec-beg (point))
601 ;; Now we have all the relevant data on this spec, so
602 ;; we start doing stuff.
605 ;; "%%" just results in a "%".
610 (setq elem (list tilde-form ?s)))
611 ;; Treat user defined format specifiers specially.
615 (list (intern (format
616 (if (stringp user-defined)
617 "gnus-user-format-function-%s"
618 "gnus-user-format-function-%c")
622 ;; Find the specification from `spec-alist'.
623 ((setq elem (cdr (assq (or extended-spec spec) spec-alist))))
625 (setq elem '("*" ?s))))
626 (setq elem-type (cadr elem))
627 ;; Insert the new format elements.
629 (not (and (featurep 'xemacs)
630 gnus-use-correct-string-widths)))
631 (insert (number-to-string pad-width)))
632 ;; Create the form to be evaled.
633 (if (or max-width cut-width ignore-value
634 (and (featurep 'xemacs)
635 gnus-use-correct-string-widths))
638 (let ((el (car elem)))
639 (cond ((= (cadr elem) ?c)
640 (setq el (list 'char-to-string el)))
642 (setq el (list 'int-to-string el))))
644 (setq el (gnus-tilde-ignore-form el ignore-value)))
646 (setq el (gnus-tilde-cut-form el cut-width)))
648 (setq el (gnus-tilde-max-form el max-width)))
650 (setq el (gnus-pad-form el pad-width)))
653 (push (car elem) flist))))
654 (setq fstring (buffer-substring-no-properties (point-min) (point-max))))
656 ;; Do some postprocessing to increase efficiency.
661 ((string= fstring "")
663 ;; Not a format string.
664 ((not (string-match "%" fstring))
666 ;; A format string with just a single string spec.
667 ((string= fstring "%s")
669 ;; A single character.
670 ((string= fstring "%c")
673 ((string= fstring "%d")
676 (list `(princ ,(car flist)))
677 (list `(int-to-string ,(car flist)))))
678 ;; Just lots of chars and strings.
679 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
681 ;; A single string spec at the beginning of the spec.
682 ((string-match "\\`%[sc][^%]+\\'" fstring)
683 (list (car flist) (substring fstring 2)))
684 ;; A single string spec in the middle of the spec.
685 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
686 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
687 ;; A single string spec in the end of the spec.
688 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
689 (list (match-string 1 fstring) (car flist)))
690 ;; Only string (and %) specs (XEmacs only!)
691 ((and (featurep 'xemacs)
692 gnus-make-format-preserve-properties
694 "\\`\\([^%]*\\(%%\\|%-?\\([1-9][0-9]*\\)?s\\)\\)*[^%]*\\'"
696 (list (cons 'gnus-xmas-format (cons fstring (nreverse flist)))))
697 ;; A more complex spec.
699 (list (cons 'format (cons fstring (nreverse flist)))))))
705 (cons 'insert result)))
706 (cond ((stringp result)
709 (cons 'concat result))
712 (defun gnus-eval-format (format &optional alist props)
713 "Eval the format variable FORMAT, using ALIST.
714 If PROPS, insert the result."
715 (let ((form (gnus-parse-format format alist props)))
717 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
720 (defun gnus-compile ()
721 "Byte-compile the user-defined format specs."
724 (let ((entries gnus-format-specs)
725 (byte-compile-warnings '(unresolved callargs redefine))
728 (gnus-message 7 "Compiling format specs...")
731 (setq entry (pop entries))
732 (if (memq (car entry) '(gnus-version version))
733 (setq gnus-format-specs (delq entry gnus-format-specs))
734 (let ((form (caddr entry)))
735 (when (and (listp form)
736 ;; Under GNU Emacs, it's (byte-code ...)
737 (not (eq 'byte-code (car form)))
738 ;; Under XEmacs, it's (funcall #<compiled-function ...>)
739 (not (and (eq 'funcall (car form))
740 (byte-code-function-p (cadr form)))))
741 (defalias 'gnus-tmp-func `(lambda () ,form))
742 (byte-compile 'gnus-tmp-func)
743 (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func))))))
745 (push (cons 'version emacs-version) gnus-format-specs)
746 ;; Mark the .newsrc.eld file as "dirty".
748 (gnus-message 7 "Compiling user specs...done"))))
750 (defun gnus-set-format (type &optional insertable)
751 (set (intern (format "gnus-%s-line-format-spec" type))
753 (symbol-value (intern (format "gnus-%s-line-format" type)))
754 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
760 ;; coding: iso-8859-1
763 ;;; arch-tag: a4328fa1-1f84-4b09-97ad-4b5767cfd50f
764 ;;; gnus-spec.el ends here