1 ;;; gnus-spec.el --- format spec functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, 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."
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."
46 ;;; Internal variables.
48 (defvar gnus-summary-mark-positions nil)
49 (defvar gnus-group-mark-positions nil)
50 (defvar gnus-group-indentation "")
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.
58 ;; First we have lots of dummy defvars to let the compiler know these
59 ;; are really dynamic variables.
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)
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
95 (format "%c%4s: %-23s%c" gnus-tmp-opening-bracket gnus-tmp-lines
98 (gnus-summary-from-or-to-or-newsgroups
99 gnus-tmp-header gnus-tmp-from))))
100 (if (> (length val) 23)
103 gnus-tmp-closing-bracket))
105 gnus-mouse-face-prop gnus-mouse-face)
106 (insert " " gnus-tmp-subject-or-nil "\n"))
108 (defvar gnus-summary-line-format-spec
109 (gnus-byte-code 'gnus-summary-line-format-spec))
111 (defun gnus-summary-dummy-line-format-spec ()
113 (gnus-put-text-property
118 gnus-mouse-face-prop gnus-mouse-face)
119 (insert " " gnus-tmp-subject "\n"))
121 (defvar gnus-summary-dummy-line-format-spec
122 (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
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
132 (insert gnus-tmp-group "\n")
134 gnus-mouse-face-prop gnus-mouse-face))
135 (defvar gnus-group-line-format-spec
136 (gnus-byte-code 'gnus-group-line-format-spec))
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.")
148 (defvar gnus-default-format-specs gnus-format-specs)
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)
154 ;;; Phew. All that gruft is over with, fortunately.
157 (defun gnus-update-format (var)
158 "Update the format specification near point."
163 ;; Find the end of the current word.
164 (re-search-forward "[ \t\n]" nil t)
166 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
168 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
169 (match-string 1 var))))
170 (entry (assq type gnus-format-specs))
173 (setq gnus-format-specs (delq entry gnus-format-specs)))
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)
182 (pop-to-buffer "*Gnus Format*")
184 (lisp-interaction-mode)
185 (insert (gnus-pp-to-string spec))))
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.
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 ;; Flush the group format spec cache if it doesn't support decoded
201 (when (memq 'group types)
202 (let ((spec (assq 'group gnus-format-specs)))
203 (unless (string-match " gnus-tmp-decoded-group[ )]"
204 (gnus-prin1-to-string (nth 2 spec)))
205 (setq gnus-format-specs (delq spec gnus-format-specs)))))
207 ;; Go through all the formats and see whether they need updating.
208 (let (new-format entry type val updated)
209 (while (setq type (pop types))
210 ;; Jump to the proper buffer to find out the value of the
211 ;; variable, if possible. (It may be buffer-local.)
213 (let ((buffer (intern (format "gnus-%s-buffer" type))))
214 (when (and (boundp buffer)
215 (setq val (symbol-value buffer))
216 (gnus-buffer-exists-p val))
218 (setq new-format (symbol-value
219 (intern (format "gnus-%s-line-format" type)))))
220 (setq entry (cdr (assq type gnus-format-specs)))
222 (equal (car entry) new-format))
223 ;; Use the old format.
224 (set (intern (format "gnus-%s-line-format-spec" type))
226 ;; This is a new format.
228 (if (not (stringp new-format))
229 ;; This is a function call or something.
231 ;; This is a "real" format.
235 (intern (format "gnus-%s-line-format-alist" type)))
236 (not (string-match "mode$" (symbol-name type))))))
237 ;; Enter the new format spec into the list.
240 (setcar (cdr entry) val)
241 (setcar entry new-format))
242 (push (list type new-format val) gnus-format-specs))
243 (set (intern (format "gnus-%s-line-format-spec" type)) val)
244 (push type updated))))
246 (unless (assq 'version gnus-format-specs)
247 (push (cons 'version emacs-version) gnus-format-specs))
250 (defvar gnus-mouse-face-0 'highlight)
251 (defvar gnus-mouse-face-1 'highlight)
252 (defvar gnus-mouse-face-2 'highlight)
253 (defvar gnus-mouse-face-3 'highlight)
254 (defvar gnus-mouse-face-4 'highlight)
256 (defun gnus-mouse-face-function (form type)
257 `(gnus-put-text-property
258 (point) (progn ,@form (point))
262 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
264 (defvar gnus-face-0 'bold)
265 (defvar gnus-face-1 'italic)
266 (defvar gnus-face-2 'bold-italic)
267 (defvar gnus-face-3 'bold)
268 (defvar gnus-face-4 'bold)
270 (defun gnus-face-face-function (form type)
271 `(gnus-add-text-properties
272 (point) (progn ,@form (point))
273 '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
275 (defun gnus-balloon-face-function (form type)
276 `(gnus-put-text-property
277 (point) (progn ,@form (point))
278 ,(if (fboundp 'balloon-help-mode)
281 ,(intern (format "gnus-balloon-face-%d" type))))
283 (defun gnus-spec-tab (column)
285 `(insert-char ? (max (- ,column (current-column)) 0))
286 (let ((column (abs column)))
287 `(if (> (current-column) ,column)
289 (if (= (move-to-column ,column) ,column)
290 (delete-region (point) end)
291 (delete-region (1- (point)) end)
293 (insert-char ? (max (- ,column (current-column)) 0))))))
295 (defun gnus-correct-length (string)
296 "Return the correct width of STRING."
298 (mapcar (lambda (char) (incf length (gnus-char-width char))) string)
301 (defun gnus-correct-substring (string start &optional end)
306 (length (length string))
307 (string (concat string "\0")))
308 ;; Find the start position.
309 (while (and (< seek length)
311 (incf wseek (gnus-char-width (aref string seek)))
314 ;; Find the end position.
315 (while (and (<= seek length)
318 (incf wseek (gnus-char-width (aref string seek)))
321 (substring string wstart (1- wend))))
323 (defun gnus-string-width-function ()
325 (gnus-use-correct-string-widths
326 'gnus-correct-length)
327 ((fboundp 'string-width)
332 (defun gnus-substring-function ()
334 (gnus-use-correct-string-widths
335 'gnus-correct-substring)
336 ((fboundp 'string-width)
337 'gnus-correct-substring)
341 (defun gnus-tilde-max-form (el max-width)
342 "Return a form that limits EL to MAX-WIDTH."
343 (let ((max (abs max-width))
344 (length-fun (gnus-string-width-function))
345 (substring-fun (gnus-substring-function)))
347 `(if (> (,length-fun ,el) ,max)
349 `(,substring-fun ,el (- (,length-fun ,el) ,max))
350 `(,substring-fun ,el 0 ,max))
352 `(let ((val (eval ,el)))
353 (if (> (,length-fun val) ,max)
355 `(,substring-fun val (- (,length-fun val) ,max))
356 `(,substring-fun val 0 ,max))
359 (defun gnus-tilde-cut-form (el cut-width)
360 "Return a form that cuts CUT-WIDTH off of EL."
361 (let ((cut (abs cut-width))
362 (length-fun (gnus-string-width-function))
363 (substring-fun (gnus-substring-function)))
365 `(if (> (,length-fun ,el) ,cut)
367 `(,substring-fun ,el 0 (- (,length-fun ,el) ,cut))
368 `(,substring-fun ,el ,cut))
370 `(let ((val (eval ,el)))
371 (if (> (,length-fun val) ,cut)
373 `(,substring-fun val 0 (- (,length-fun val) ,cut))
374 `(,substring-fun val ,cut))
377 (defun gnus-tilde-ignore-form (el ignore-value)
378 "Return a form that is blank when EL is IGNORE-VALUE."
380 `(if (equal ,el ,ignore-value)
382 `(let ((val (eval ,el)))
383 (if (equal val ,ignore-value)
386 (defun gnus-pad-form (el pad-width)
387 "Return a form that pads EL to PAD-WIDTH accounting for multi-column
388 characters correctly. This is because `format' may pad to columns or to
389 characters when given a pad value."
390 (let ((pad (abs pad-width))
391 (side (< 0 pad-width))
392 (length-fun (gnus-string-width-function)))
394 `(let ((need (- ,pad (,length-fun ,el))))
396 (concat ,(when side '(make-string need ?\ ))
398 ,(when (not side) '(make-string need ?\ )))
400 `(let* ((val (eval ,el))
401 (need (- ,pad (,length-fun val))))
403 (concat ,(when side '(make-string need ?\ ))
405 ,(when (not side) '(make-string need ?\ )))
408 (defun gnus-parse-format (format spec-alist &optional insert)
409 ;; This function parses the FORMAT string with the help of the
410 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
411 ;; string. If the FORMAT string contains the specifiers %( and %)
412 ;; the text between them will have the mouse-face text property.
413 ;; If the FORMAT string contains the specifiers %[ and %], the text between
414 ;; them will have the balloon-help text property.
415 (let ((case-fold-search nil))
417 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'\\|%[-0-9]*=\\|%[-0-9]*\\*"
419 (gnus-parse-complex-format format spec-alist)
420 ;; This is a simple format.
421 (gnus-parse-simple-format format spec-alist insert))))
423 (defun gnus-parse-complex-format (format spec-alist)
424 (let ((cursor-spec nil))
426 (gnus-set-work-buffer)
428 (goto-char (point-min))
429 (while (re-search-forward "\"" nil t)
430 (replace-match "\\\"" nil t))
431 (goto-char (point-min))
433 ;; Convert all font specs into font spec lists.
434 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
435 (let ((number (if (match-beginning 1)
436 (match-string 1) "0"))
437 (delim (aref (match-string 2) 0)))
438 (if (or (= delim ?\()
441 (replace-match (concat "\"("
442 (cond ((= delim ?\() "mouse")
443 ((= delim ?\{) "face")
447 (replace-match "\")\""))))
448 (goto-char (point-max))
450 ;; Convert point position commands.
451 (goto-char (point-min))
452 (let ((case-fold-search nil))
453 (while (re-search-forward "%\\([-0-9]+\\)?\\*" nil t)
454 (replace-match "\"(point)\"" t t)
455 (setq cursor-spec t)))
456 ;; Convert TAB commands.
457 (goto-char (point-min))
458 (while (re-search-forward "%\\([-0-9]+\\)=" nil t)
459 (replace-match (format "\"(tab %s)\"" (match-string 1)) t t))
460 ;; Convert the buffer into the spec.
461 (goto-char (point-min))
462 (let ((form (read (current-buffer))))
464 `(let (gnus-position)
465 ,@(gnus-complex-form-to-spec form spec-alist)
467 (gnus-put-text-property gnus-position (1+ gnus-position)
470 ,@(gnus-complex-form-to-spec form spec-alist)))))))
472 (defun gnus-complex-form-to-spec (form spec-alist)
478 (gnus-parse-simple-format sform spec-alist t))
479 ((eq (car sform) 'point)
480 '(setq gnus-position (point)))
481 ((eq (car sform) 'tab)
482 (gnus-spec-tab (cadr sform)))
484 (funcall (intern (format "gnus-%s-face-function" (car sform)))
485 (gnus-complex-form-to-spec (cddr sform) spec-alist)
490 (defun gnus-xmas-format (fstring &rest args)
491 "A version of `format' which preserves text properties.
493 Required for XEmacs, where the built in `format' function strips all text
494 properties from both the format string and any inserted strings.
496 Only supports the format sequence %s, and %% for inserting
497 literal % characters. A pad width and an optional - (to right pad)
498 are supported for %s."
499 (let ((re "%%\\|%\\(-\\)?\\([1-9][0-9]*\\)?s")
503 (goto-char (point-min))
504 (while (re-search-forward re nil t)
505 (goto-char (match-end 0))
507 ((string= (match-string 0) "%%")
511 (error 'wrong-number-of-arguments #'my-format n fstring))
512 (let* ((minlen (string-to-number (or (match-string 2) "")))
514 (str (if (stringp arg) arg (format "%s" arg)))
515 (lpad (null (match-string 1)))
516 (padlen (max 0 (- minlen (length str)))))
518 (if lpad (insert-char ?\ padlen))
520 (unless lpad (insert-char ?\ padlen))
521 (setq args (cdr args))))))
524 (defun gnus-parse-simple-format (format spec-alist &optional insert)
525 ;; This function parses the FORMAT string with the help of the
526 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
529 spec flist fstring elem result dontinsert user-defined
530 type value pad-width spec-beg cut-width ignore-value
531 tilde-form tilde elem-type extended-spec)
533 (gnus-set-work-buffer)
535 (goto-char (point-min))
536 (while (re-search-forward "%" nil t)
537 (setq user-defined nil
545 (setq spec-beg (1- (point)))
547 ;; Parse this spec fully.
550 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
551 (setq pad-width (string-to-number (match-string 1)))
552 (when (match-beginning 2)
553 (setq max-width (string-to-number (buffer-substring
554 (1+ (match-beginning 2))
556 (goto-char (match-end 0)))
559 (setq tilde (read (current-buffer))
563 ((memq type '(pad pad-left))
564 (setq pad-width value))
565 ((eq type 'pad-right)
566 (setq pad-width (- value)))
567 ((memq type '(max-right max))
568 (setq max-width value))
570 (setq max-width (- value)))
571 ((memq type '(cut cut-left))
572 (setq cut-width value))
573 ((eq type 'cut-right)
574 (setq cut-width (- value)))
577 (if (stringp value) value (format "%s" value))))
579 (setq tilde-form value))
581 (error "Unknown tilde type: %s" tilde)))
586 ;; User-defined spec -- find the spec name.
587 ((eq (setq spec (char-after)) ?u)
589 (when (and (eq (setq user-defined (char-after)) ?&)
590 (looking-at "&\\([^;]+\\);"))
591 (setq user-defined (match-string 1))
592 (goto-char (match-end 1))))
594 ((and (eq spec ?&) (looking-at "&\\([^;]+\\);"))
595 (setq extended-spec (intern (match-string 1)))
596 (goto-char (match-end 1))))
598 (delete-region spec-beg (point))
600 ;; Now we have all the relevant data on this spec, so
601 ;; we start doing stuff.
604 ;; "%%" just results in a "%".
609 (setq elem (list tilde-form ?s)))
610 ;; Treat user defined format specifiers specially.
614 (list (intern (format
615 (if (stringp user-defined)
616 "gnus-user-format-function-%s"
617 "gnus-user-format-function-%c")
621 ;; Find the specification from `spec-alist'.
622 ((setq elem (cdr (assq (or extended-spec spec) spec-alist))))
624 (setq elem '("*" ?s))))
625 (setq elem-type (cadr elem))
626 ;; Insert the new format elements.
628 (not (and (featurep 'xemacs)
629 gnus-use-correct-string-widths)))
630 (insert (number-to-string pad-width)))
631 ;; Create the form to be evaled.
632 (if (or max-width cut-width ignore-value
633 (and (featurep 'xemacs)
634 gnus-use-correct-string-widths))
637 (let ((el (car elem)))
638 (cond ((= (cadr elem) ?c)
639 (setq el (list 'char-to-string el)))
641 (setq el (list 'int-to-string el))))
643 (setq el (gnus-tilde-ignore-form el ignore-value)))
645 (setq el (gnus-tilde-cut-form el cut-width)))
647 (setq el (gnus-tilde-max-form el max-width)))
649 (setq el (gnus-pad-form el pad-width)))
652 (push (car elem) flist))))
653 (setq fstring (buffer-substring-no-properties (point-min) (point-max))))
655 ;; Do some postprocessing to increase efficiency.
660 ((string= fstring "")
662 ;; Not a format string.
663 ((not (string-match "%" fstring))
665 ;; A format string with just a single string spec.
666 ((string= fstring "%s")
668 ;; A single character.
669 ((string= fstring "%c")
672 ((string= fstring "%d")
675 (list `(princ ,(car flist)))
676 (list `(int-to-string ,(car flist)))))
677 ;; Just lots of chars and strings.
678 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
680 ;; A single string spec at the beginning of the spec.
681 ((string-match "\\`%[sc][^%]+\\'" fstring)
682 (list (car flist) (substring fstring 2)))
683 ;; A single string spec in the middle of the spec.
684 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
685 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
686 ;; A single string spec in the end of the spec.
687 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
688 (list (match-string 1 fstring) (car flist)))
689 ;; Only string (and %) specs (XEmacs only!)
690 ((and (featurep 'xemacs)
691 gnus-make-format-preserve-properties
693 "\\`\\([^%]*\\(%%\\|%-?\\([1-9][0-9]*\\)?s\\)\\)*[^%]*\\'"
695 (list (cons 'gnus-xmas-format (cons fstring (nreverse flist)))))
696 ;; A more complex spec.
698 (list (cons 'format (cons fstring (nreverse flist)))))))
704 (cons 'insert result)))
705 (cond ((stringp result)
708 (cons 'concat result))
711 (defun gnus-eval-format (format &optional alist props)
712 "Eval the format variable FORMAT, using ALIST.
713 If PROPS, insert the result."
714 (let ((form (gnus-parse-format format alist props)))
716 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
719 (defun gnus-compile ()
720 "Byte-compile the user-defined format specs."
723 (let ((entries gnus-format-specs)
724 (byte-compile-warnings '(unresolved callargs redefine))
727 (gnus-message 7 "Compiling format specs...")
730 (setq entry (pop entries))
731 (if (memq (car entry) '(gnus-version version))
732 (setq gnus-format-specs (delq entry gnus-format-specs))
733 (let ((form (caddr entry)))
734 (when (and (listp form)
735 ;; Under GNU Emacs, it's (byte-code ...)
736 (not (eq 'byte-code (car form)))
737 ;; Under XEmacs, it's (funcall #<compiled-function ...>)
738 (not (and (eq 'funcall (car form))
739 (byte-code-function-p (cadr form)))))
740 (defalias 'gnus-tmp-func `(lambda () ,form))
741 (byte-compile 'gnus-tmp-func)
742 (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func))))))
744 (push (cons 'version emacs-version) gnus-format-specs)
745 ;; Mark the .newsrc.eld file as "dirty".
747 (gnus-message 7 "Compiling user specs...done"))))
749 (defun gnus-set-format (type &optional insertable)
750 (set (intern (format "gnus-%s-line-format-spec" type))
752 (symbol-value (intern (format "gnus-%s-line-format" type)))
753 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
759 ;; coding: iso-8859-1
762 ;;; arch-tag: a4328fa1-1f84-4b09-97ad-4b5767cfd50f
763 ;;; gnus-spec.el ends here