1 ;;; gnus-spec.el --- format spec functions for Gnus
3 ;; Copyright (C) 1996-2011 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 3 of the License, or
13 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
27 ;; For Emacs <22.2 and XEmacs.
29 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30 (eval-when-compile (require 'cl))
31 (defvar gnus-newsrc-file-version)
35 (defcustom gnus-use-correct-string-widths (featurep 'xemacs)
36 "*If non-nil, use correct functions for dealing with wide characters."
41 (defcustom gnus-make-format-preserve-properties (featurep 'xemacs)
42 "*If non-nil, use a replacement `format' function which preserves
43 text properties. This is only needed on XEmacs, as Emacs does this anyway."
48 ;;; Internal variables.
50 (defvar gnus-summary-mark-positions nil)
51 (defvar gnus-group-mark-positions nil)
52 (defvar gnus-group-indentation "")
54 ;; Format specs. The chunks below are the machine-generated forms
55 ;; that are to be evaled as the result of the default format strings.
56 ;; We write them in here to get them byte-compiled. That way the
57 ;; default actions will be quite fast, while still retaining the full
58 ;; flexibility of the user-defined format specs.
60 ;; First we have lots of dummy defvars to let the compiler know these
61 ;; are really dynamic variables.
63 (defvar gnus-tmp-unread)
64 (defvar gnus-tmp-replied)
65 (defvar gnus-tmp-score-char)
66 (defvar gnus-tmp-indentation)
67 (defvar gnus-tmp-opening-bracket)
68 (defvar gnus-tmp-lines)
69 (defvar gnus-tmp-name)
70 (defvar gnus-tmp-closing-bracket)
71 (defvar gnus-tmp-subject-or-nil)
72 (defvar gnus-tmp-subject)
73 (defvar gnus-tmp-marked)
74 (defvar gnus-tmp-marked-mark)
75 (defvar gnus-tmp-subscribed)
76 (defvar gnus-tmp-process-marked)
77 (defvar gnus-tmp-number-of-unread)
78 (defvar gnus-tmp-group-name)
79 (defvar gnus-tmp-group)
80 (defvar gnus-tmp-article-number)
81 (defvar gnus-tmp-unread-and-unselected)
82 (defvar gnus-tmp-news-method)
83 (defvar gnus-tmp-news-server)
84 (defvar gnus-tmp-article-number)
85 (defvar gnus-mouse-face)
86 (defvar gnus-mouse-face-prop)
87 (defvar gnus-tmp-header)
88 (defvar gnus-tmp-from)
90 (declare-function gnus-summary-from-or-to-or-newsgroups "gnus-sum"
91 (header gnus-tmp-from))
93 (defun gnus-summary-line-format-spec ()
94 (insert gnus-tmp-unread gnus-tmp-replied
95 gnus-tmp-score-char gnus-tmp-indentation)
96 (gnus-put-text-property
100 (format "%c%4s: %-23s%c" gnus-tmp-opening-bracket gnus-tmp-lines
103 (gnus-summary-from-or-to-or-newsgroups
104 gnus-tmp-header gnus-tmp-from))))
105 (if (> (length val) 23)
108 gnus-tmp-closing-bracket))
110 gnus-mouse-face-prop gnus-mouse-face)
111 (insert " " gnus-tmp-subject-or-nil "\n"))
113 (defvar gnus-summary-line-format-spec
114 (gnus-byte-code 'gnus-summary-line-format-spec))
116 (defun gnus-summary-dummy-line-format-spec ()
118 (gnus-put-text-property
123 gnus-mouse-face-prop gnus-mouse-face)
124 (insert " " gnus-tmp-subject "\n"))
126 (defvar gnus-summary-dummy-line-format-spec
127 (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
129 (defun gnus-group-line-format-spec ()
130 (insert gnus-tmp-marked-mark gnus-tmp-subscribed
131 gnus-tmp-process-marked
132 gnus-group-indentation
133 (format "%5s: " gnus-tmp-number-of-unread))
134 (gnus-put-text-property
137 (insert gnus-tmp-group "\n")
139 gnus-mouse-face-prop gnus-mouse-face))
140 (defvar gnus-group-line-format-spec
141 (gnus-byte-code 'gnus-group-line-format-spec))
143 (defvar gnus-format-specs
144 `((version . ,emacs-version)
145 (gnus-version . ,(gnus-continuum-version))
146 (group "%M\%S\%p\%P\%5y: %(%g%)\n" ,gnus-group-line-format-spec)
147 (summary-dummy "* %(: :%) %S\n"
148 ,gnus-summary-dummy-line-format-spec)
149 (summary "%U%R%z%I%(%[%4L: %-23,23f%]%) %s\n"
150 ,gnus-summary-line-format-spec))
151 "Alist of format specs.")
153 (defvar gnus-default-format-specs gnus-format-specs)
155 (defvar gnus-article-mode-line-format-spec nil)
156 (defvar gnus-summary-mode-line-format-spec nil)
157 (defvar gnus-group-mode-line-format-spec nil)
159 ;;; Phew. All that gruft is over with, fortunately.
162 (defun gnus-update-format (var)
163 "Update the format specification near point."
168 ;; Find the end of the current word.
169 (re-search-forward "[ \t\n]" nil t)
171 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
173 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
174 (match-string 1 var))))
175 (entry (assq type gnus-format-specs))
178 (setq gnus-format-specs (delq entry gnus-format-specs)))
180 (intern (format "%s-spec" var))
181 (gnus-parse-format (setq value (symbol-value (intern var)))
182 (symbol-value (intern (format "%s-alist" var)))
183 (not (string-match "mode" var))))
184 (setq spec (symbol-value (intern (format "%s-spec" var))))
185 (push (list type value spec) gnus-format-specs)
187 (pop-to-buffer "*Gnus Format*")
189 (lisp-interaction-mode)
190 (insert (gnus-pp-to-string spec))))
192 (defun gnus-update-format-specifications (&optional force &rest types)
193 "Update all (necessary) format specifications.
194 Return a list of updated types."
195 ;; Make the indentation array.
196 ;; See whether all the stored info needs to be flushed.
198 (not gnus-newsrc-file-version)
199 (not (equal (gnus-continuum-version)
200 (gnus-continuum-version gnus-newsrc-file-version)))
201 (not (equal emacs-version
202 (cdr (assq 'version gnus-format-specs)))))
203 (setq gnus-format-specs nil))
204 ;; Flush the group format spec cache if there's the grouplens stuff
205 ;; or it doesn't support decoded group names.
206 (when (memq 'group types)
207 (let* ((spec (assq 'group gnus-format-specs))
208 (sspec (gnus-prin1-to-string (nth 2 spec))))
209 (when (or (string-match " gnus-tmp-grouplens[ )]" sspec)
210 (not (string-match " gnus-tmp-decoded-group[ )]" sspec)))
211 (setq gnus-format-specs (delq spec gnus-format-specs)))))
213 ;; Go through all the formats and see whether they need updating.
214 (let (new-format entry type val updated)
215 (while (setq type (pop types))
216 ;; Jump to the proper buffer to find out the value of the
217 ;; variable, if possible. (It may be buffer-local.)
219 (let ((buffer (intern (format "gnus-%s-buffer" type))))
220 (when (and (boundp buffer)
221 (setq val (symbol-value buffer))
222 (gnus-buffer-exists-p val))
224 (setq new-format (symbol-value
225 (intern (format "gnus-%s-line-format" type)))))
226 (setq entry (cdr (assq type gnus-format-specs)))
228 (equal (car entry) new-format))
229 ;; Use the old format.
230 (set (intern (format "gnus-%s-line-format-spec" type))
232 ;; This is a new format.
234 (if (not (stringp new-format))
235 ;; This is a function call or something.
237 ;; This is a "real" format.
241 (intern (format "gnus-%s-line-format-alist" type)))
242 (not (string-match "mode$" (symbol-name type))))))
243 ;; Enter the new format spec into the list.
246 (setcar (cdr entry) val)
247 (setcar entry new-format))
248 (push (list type new-format val) gnus-format-specs))
249 (set (intern (format "gnus-%s-line-format-spec" type)) val)
250 (push type updated))))
252 (unless (assq 'version gnus-format-specs)
253 (push (cons 'version emacs-version) gnus-format-specs))
256 (defvar gnus-mouse-face-0 'highlight)
257 (defvar gnus-mouse-face-1 'highlight)
258 (defvar gnus-mouse-face-2 'highlight)
259 (defvar gnus-mouse-face-3 'highlight)
260 (defvar gnus-mouse-face-4 'highlight)
262 (defun gnus-mouse-face-function (form type)
263 `(gnus-put-text-property
264 (point) (progn ,@form (point))
268 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
270 (defvar gnus-face-0 'bold)
271 (defvar gnus-face-1 'italic)
272 (defvar gnus-face-2 'bold-italic)
273 (defvar gnus-face-3 'bold)
274 (defvar gnus-face-4 'bold)
276 (defun gnus-face-face-function (form type)
277 `(gnus-add-text-properties
278 (point) (progn ,@form (point))
279 '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
281 (defun gnus-balloon-face-function (form type)
282 `(gnus-put-text-property
283 (point) (progn ,@form (point))
284 ,(if (fboundp 'balloon-help-mode)
287 ,(intern (format "gnus-balloon-face-%d" type))))
289 (defun gnus-spec-tab (column)
291 `(insert-char ? (max (- ,column (current-column)) 0))
292 (let ((column (abs column)))
293 `(if (> (current-column) ,column)
295 (if (= (move-to-column ,column) ,column)
296 (delete-region (point) end)
297 (delete-region (1- (point)) end)
299 (insert-char ? (max (- ,column (current-column)) 0))))))
301 (defun gnus-correct-length (string)
302 "Return the correct width of STRING."
303 (apply #'+ (mapcar #'char-width string)))
305 (defun gnus-correct-substring (string start &optional end)
310 (length (length string))
311 (string (concat string "\0")))
312 ;; Find the start position.
313 (while (and (< seek length)
315 (incf wseek (char-width (aref string seek)))
318 ;; Find the end position.
319 (while (and (<= seek length)
322 (incf wseek (char-width (aref string seek)))
325 (substring string wstart (1- wend))))
327 (defun gnus-string-width-function ()
329 (gnus-use-correct-string-widths
330 'gnus-correct-length)
331 ((fboundp 'string-width)
336 (defun gnus-substring-function ()
338 (gnus-use-correct-string-widths
339 'gnus-correct-substring)
340 ((fboundp 'string-width)
341 'gnus-correct-substring)
345 (defun gnus-tilde-max-form (el max-width)
346 "Return a form that limits EL to MAX-WIDTH."
347 (let ((max (abs max-width))
348 (length-fun (gnus-string-width-function))
349 (substring-fun (gnus-substring-function)))
351 `(if (> (,length-fun ,el) ,max)
353 `(,substring-fun ,el (- (,length-fun ,el) ,max))
354 `(,substring-fun ,el 0 ,max))
356 `(let ((val (eval ,el)))
357 (if (> (,length-fun val) ,max)
359 `(,substring-fun val (- (,length-fun val) ,max))
360 `(,substring-fun val 0 ,max))
363 (defun gnus-tilde-cut-form (el cut-width)
364 "Return a form that cuts CUT-WIDTH off of EL."
365 (let ((cut (abs cut-width))
366 (length-fun (gnus-string-width-function))
367 (substring-fun (gnus-substring-function)))
369 `(if (> (,length-fun ,el) ,cut)
371 `(,substring-fun ,el 0 (- (,length-fun ,el) ,cut))
372 `(,substring-fun ,el ,cut))
374 `(let ((val (eval ,el)))
375 (if (> (,length-fun val) ,cut)
377 `(,substring-fun val 0 (- (,length-fun val) ,cut))
378 `(,substring-fun val ,cut))
381 (defun gnus-tilde-ignore-form (el ignore-value)
382 "Return a form that is blank when EL is IGNORE-VALUE."
384 `(if (equal ,el ,ignore-value)
386 `(let ((val (eval ,el)))
387 (if (equal val ,ignore-value)
390 (defun gnus-pad-form (el pad-width)
391 "Return a form that pads EL to PAD-WIDTH accounting for multi-column
392 characters correctly. This is because `format' may pad to columns or to
393 characters when given a pad value."
394 (let ((pad (abs pad-width))
395 (side (< 0 pad-width))
396 (length-fun (gnus-string-width-function)))
398 `(let ((need (- ,pad (,length-fun ,el))))
400 (concat ,(when side '(make-string need ?\ ))
402 ,(when (not side) '(make-string need ?\ )))
404 `(let* ((val (eval ,el))
405 (need (- ,pad (,length-fun val))))
407 (concat ,(when side '(make-string need ?\ ))
409 ,(when (not side) '(make-string need ?\ )))
412 (defun gnus-parse-format (format spec-alist &optional insert)
413 ;; This function parses the FORMAT string with the help of the
414 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
415 ;; string. If the FORMAT string contains the specifiers %( and %)
416 ;; the text between them will have the mouse-face text property.
417 ;; If the FORMAT string contains the specifiers %[ and %], the text between
418 ;; them will have the balloon-help text property.
419 (let ((case-fold-search nil))
421 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'\\|%[-0-9]*=\\|%[-0-9]*\\*"
423 (gnus-parse-complex-format format spec-alist)
424 ;; This is a simple format.
425 (gnus-parse-simple-format format spec-alist insert))))
427 (defun gnus-parse-complex-format (format spec-alist)
428 (let ((cursor-spec nil))
430 (gnus-set-work-buffer)
432 (goto-char (point-min))
433 (while (re-search-forward "\"" nil t)
434 (replace-match "\\\"" nil t))
435 (goto-char (point-min))
437 ;; Convert all font specs into font spec lists.
438 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
439 (let ((number (if (match-beginning 1)
440 (match-string 1) "0"))
441 (delim (aref (match-string 2) 0)))
442 (if (or (= delim ?\()
445 (replace-match (concat "\"("
446 (cond ((= delim ?\() "mouse")
447 ((= delim ?\{) "face")
451 (replace-match "\")\""))))
452 (goto-char (point-max))
454 ;; Convert point position commands.
455 (goto-char (point-min))
456 (let ((case-fold-search nil))
457 (while (re-search-forward "%\\([-0-9]+\\)?\\*" nil t)
458 (replace-match "\"(point)\"" t t)
459 (setq cursor-spec t)))
460 ;; Convert TAB commands.
461 (goto-char (point-min))
462 (while (re-search-forward "%\\([-0-9]+\\)=" nil t)
463 (replace-match (format "\"(tab %s)\"" (match-string 1)) t t))
464 ;; Convert the buffer into the spec.
465 (goto-char (point-min))
466 (let ((form (read (current-buffer))))
468 `(let (gnus-position)
469 ,@(gnus-complex-form-to-spec form spec-alist)
471 (gnus-put-text-property gnus-position (1+ gnus-position)
474 ,@(gnus-complex-form-to-spec form spec-alist)))))))
476 (defun gnus-complex-form-to-spec (form spec-alist)
482 (gnus-parse-simple-format sform spec-alist t))
483 ((eq (car sform) 'point)
484 '(setq gnus-position (point)))
485 ((eq (car sform) 'tab)
486 (gnus-spec-tab (cadr sform)))
488 (funcall (intern (format "gnus-%s-face-function" (car sform)))
489 (gnus-complex-form-to-spec (cddr sform) spec-alist)
494 (defun gnus-xmas-format (fstring &rest args)
495 "A version of `format' which preserves text properties.
497 Required for XEmacs, where the built in `format' function strips all text
498 properties from both the format string and any inserted strings.
500 Only supports the format sequence %s, and %% for inserting
501 literal % characters. A pad width and an optional - (to right pad)
502 are supported for %s."
503 (let ((re "%%\\|%\\(-\\)?\\([1-9][0-9]*\\)?s")
507 (goto-char (point-min))
508 (while (re-search-forward re nil t)
509 (goto-char (match-end 0))
511 ((string= (match-string 0) "%%")
515 (error 'wrong-number-of-arguments #'my-format n fstring))
516 (let* ((minlen (string-to-number (or (match-string 2) "")))
518 (str (if (stringp arg) arg (format "%s" arg)))
519 (lpad (null (match-string 1)))
520 (padlen (max 0 (- minlen (length str)))))
522 (if lpad (insert-char ?\ padlen))
524 (unless lpad (insert-char ?\ padlen))
525 (setq args (cdr args))))))
528 (defun gnus-parse-simple-format (format spec-alist &optional insert)
529 ;; This function parses the FORMAT string with the help of the
530 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
533 spec flist fstring elem result dontinsert user-defined
534 type value pad-width spec-beg cut-width ignore-value
535 tilde-form tilde elem-type extended-spec)
537 (gnus-set-work-buffer)
539 (goto-char (point-min))
540 (while (re-search-forward "%" nil t)
541 (setq user-defined nil
549 (setq spec-beg (1- (point)))
551 ;; Parse this spec fully.
554 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
555 (setq pad-width (string-to-number (match-string 1)))
556 (when (match-beginning 2)
557 (setq max-width (string-to-number (buffer-substring
558 (1+ (match-beginning 2))
560 (goto-char (match-end 0)))
563 (setq tilde (read (current-buffer))
567 ((memq type '(pad pad-left))
568 (setq pad-width value))
569 ((eq type 'pad-right)
570 (setq pad-width (- value)))
571 ((memq type '(max-right max))
572 (setq max-width value))
574 (setq max-width (- value)))
575 ((memq type '(cut cut-left))
576 (setq cut-width value))
577 ((eq type 'cut-right)
578 (setq cut-width (- value)))
581 (if (stringp value) value (format "%s" value))))
583 (setq tilde-form value))
585 (error "Unknown tilde type: %s" tilde)))
590 ;; User-defined spec -- find the spec name.
591 ((eq (setq spec (char-after)) ?u)
593 (when (and (eq (setq user-defined (char-after)) ?&)
594 (looking-at "&\\([^;]+\\);"))
595 (setq user-defined (match-string 1))
596 (goto-char (match-end 1))))
598 ((and (eq spec ?&) (looking-at "&\\([^;]+\\);"))
599 (setq extended-spec (intern (match-string 1)))
600 (goto-char (match-end 1))))
602 (delete-region spec-beg (point))
604 ;; Now we have all the relevant data on this spec, so
605 ;; we start doing stuff.
608 ;; "%%" just results in a "%".
613 (setq elem (list tilde-form ?s)))
614 ;; Treat user defined format specifiers specially.
618 (list (intern (format
619 (if (stringp user-defined)
620 "gnus-user-format-function-%s"
621 "gnus-user-format-function-%c")
625 ;; Find the specification from `spec-alist'.
626 ((setq elem (cdr (assq (or extended-spec spec) spec-alist))))
627 ;; We used to use "%l" for displaying the grouplens score.
629 (setq elem '("" ?s)))
631 (setq elem '("*" ?s))))
632 (setq elem-type (cadr elem))
633 ;; Insert the new format elements.
635 (not (and (featurep 'xemacs)
636 gnus-use-correct-string-widths)))
637 (insert (number-to-string pad-width)))
638 ;; Create the form to be evaled.
639 (if (or max-width cut-width ignore-value
640 (and (featurep 'xemacs)
641 gnus-use-correct-string-widths))
644 (let ((el (car elem)))
645 (cond ((= (cadr elem) ?c)
646 (setq el (list 'char-to-string el)))
648 (setq el (list 'int-to-string el))))
650 (setq el (gnus-tilde-ignore-form el ignore-value)))
652 (setq el (gnus-tilde-cut-form el cut-width)))
654 (setq el (gnus-tilde-max-form el max-width)))
656 (setq el (gnus-pad-form el pad-width)))
659 (push (car elem) flist))))
660 (setq fstring (buffer-substring-no-properties (point-min) (point-max))))
662 ;; Do some postprocessing to increase efficiency.
667 ((string= fstring "")
669 ;; Not a format string.
670 ((not (string-match "%" fstring))
672 ;; A format string with just a single string spec.
673 ((string= fstring "%s")
675 ;; A single character.
676 ((string= fstring "%c")
679 ((string= fstring "%d")
682 `(insert (int-to-string ,(car flist)))
683 (list `(int-to-string ,(car flist)))))
684 ;; Just lots of chars and strings.
685 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
687 ;; A single string spec at the beginning of the spec.
688 ((string-match "\\`%[sc][^%]+\\'" fstring)
689 (list (car flist) (substring fstring 2)))
690 ;; A single string spec in the middle of the spec.
691 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
692 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
693 ;; A single string spec in the end of the spec.
694 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
695 (list (match-string 1 fstring) (car flist)))
696 ;; Only string (and %) specs (XEmacs only!)
697 ((and (featurep 'xemacs)
698 gnus-make-format-preserve-properties
700 "\\`\\([^%]*\\(%%\\|%-?\\([1-9][0-9]*\\)?s\\)\\)*[^%]*\\'"
702 (list (cons 'gnus-xmas-format (cons fstring (nreverse flist)))))
703 ;; A more complex spec.
705 (list (cons 'format (cons fstring (nreverse flist)))))))
711 (cons 'insert result)))
712 (cond ((stringp result)
715 (cons 'concat result))
718 (defun gnus-eval-format (format &optional alist props)
719 "Eval the format variable FORMAT, using ALIST.
720 If PROPS, insert the result."
721 (let ((form (gnus-parse-format format alist props)))
723 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
726 (defun gnus-compile ()
727 "Byte-compile the user-defined format specs."
730 (let ((entries gnus-format-specs)
731 (byte-compile-warnings '(unresolved callargs redefine))
734 (gnus-message 7 "Compiling format specs...")
737 (setq entry (pop entries))
738 (if (memq (car entry) '(gnus-version version))
739 (setq gnus-format-specs (delq entry gnus-format-specs))
740 (let ((form (caddr entry)))
741 (when (and (listp form)
742 ;; Under GNU Emacs, it's (byte-code ...)
743 (not (eq 'byte-code (car form)))
744 ;; Under XEmacs, it's (funcall #<compiled-function ...>)
745 (not (and (eq 'funcall (car form))
746 (byte-code-function-p (cadr form)))))
747 (defalias 'gnus-tmp-func `(lambda () ,form))
748 (byte-compile 'gnus-tmp-func)
749 (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func))))))
751 (push (cons 'version emacs-version) gnus-format-specs)
752 ;; Mark the .newsrc.eld file as "dirty".
754 (gnus-message 7 "Compiling user specs...done"))))
756 (defun gnus-set-format (type &optional insertable)
757 (set (intern (format "gnus-%s-line-format-spec" type))
759 (symbol-value (intern (format "gnus-%s-line-format" type)))
760 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
766 ;; coding: iso-8859-1
769 ;;; gnus-spec.el ends here