1 ;;; gnus-spec.el --- format spec functions for Gnus
3 ;; Copyright (C) 1996-2014 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 evalled 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-mouse-face)
85 (defvar gnus-mouse-face-prop)
86 (defvar gnus-tmp-header)
87 (defvar gnus-tmp-from)
89 (declare-function gnus-summary-from-or-to-or-newsgroups "gnus-sum"
90 (header gnus-tmp-from))
92 (defmacro gnus-lrm-string-p (string)
93 (if (fboundp 'bidi-string-mark-left-to-right)
94 ;; LRM, RLM, PDF characters as integers to avoid breaking Emacs
96 `(memq (aref ,string (1- (length ,string))) '(8206 8207 8236))
99 (defvar gnus-lrm-string (if (ignore-errors (string 8206))
100 (propertize (string 8206) 'invisible t)
103 (defvar gnus-summary-line-format-spec nil)
104 (defvar gnus-summary-dummy-line-format-spec nil)
105 (defvar gnus-group-line-format-spec nil)
107 (defvar gnus-format-specs
108 `((version . ,emacs-version)
109 (gnus-version . ,(gnus-continuum-version)))
110 "Alist of format specs.")
112 (defvar gnus-default-format-specs gnus-format-specs)
114 (defvar gnus-article-mode-line-format-spec nil)
115 (defvar gnus-summary-mode-line-format-spec nil)
116 (defvar gnus-group-mode-line-format-spec nil)
118 ;;; Phew. All that gruft is over with, fortunately.
121 (defun gnus-update-format (var)
122 "Update the format specification near point."
127 ;; Find the end of the current word.
128 (re-search-forward "[ \t\n]" nil t)
130 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
132 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
133 (match-string 1 var))))
134 (entry (assq type gnus-format-specs))
137 (setq gnus-format-specs (delq entry gnus-format-specs)))
139 (intern (format "%s-spec" var))
140 (gnus-parse-format (setq value (symbol-value (intern var)))
141 (symbol-value (intern (format "%s-alist" var)))
142 (not (string-match "mode" var))))
143 (setq spec (symbol-value (intern (format "%s-spec" var))))
144 (push (list type value spec) gnus-format-specs)
146 (pop-to-buffer "*Gnus Format*")
148 (lisp-interaction-mode)
149 (insert (gnus-pp-to-string spec))))
151 (defun gnus-update-format-specifications (&optional force &rest types)
152 "Update all (necessary) format specifications.
153 Return a list of updated types."
154 ;; Make the indentation array.
155 ;; See whether all the stored info needs to be flushed.
157 (not gnus-newsrc-file-version)
158 (not (equal (gnus-continuum-version)
159 (gnus-continuum-version gnus-newsrc-file-version)))
160 (not (equal emacs-version
161 (cdr (assq 'version gnus-format-specs)))))
162 (setq gnus-format-specs nil))
163 ;; Go through all the formats and see whether they need updating.
164 (let (new-format entry type val updated)
165 (while (setq type (pop types))
166 ;; Jump to the proper buffer to find out the value of the
167 ;; variable, if possible. (It may be buffer-local.)
169 (let ((buffer (intern (format "gnus-%s-buffer" type))))
170 (when (and (boundp buffer)
171 (setq val (symbol-value buffer))
172 (gnus-buffer-exists-p val))
174 (setq new-format (symbol-value
175 (intern (format "gnus-%s-line-format" type)))))
176 (setq entry (cdr (assq type gnus-format-specs)))
178 (equal (car entry) new-format))
179 ;; Use the old format.
180 (set (intern (format "gnus-%s-line-format-spec" type))
182 ;; This is a new format.
184 (if (not (stringp new-format))
185 ;; This is a function call or something.
187 ;; This is a "real" format.
191 (intern (format "gnus-%s-line-format-alist" type)))
192 (not (string-match "mode$" (symbol-name type))))))
193 ;; Enter the new format spec into the list.
196 (setcar (cdr entry) val)
197 (setcar entry new-format))
198 (push (list type new-format val) gnus-format-specs))
199 (set (intern (format "gnus-%s-line-format-spec" type)) val)
200 (push type updated))))
202 (unless (assq 'version gnus-format-specs)
203 (push (cons 'version emacs-version) gnus-format-specs))
206 (defcustom gnus-mouse-face-0 'highlight
207 "The \"%(hello%)\" face."
211 (defcustom gnus-mouse-face-1 'highlight
212 "The \"%1(hello%)\" face."
216 (defcustom gnus-mouse-face-2 'highlight
217 "The \"%2(hello%)\" face."
221 (defcustom gnus-mouse-face-3 'highlight
222 "The \"%3(hello%)\" face."
226 (defcustom gnus-mouse-face-4 'highlight
227 "The \"%4(hello%)\" face."
231 (defun gnus-mouse-face-function (form type)
232 `(gnus-put-text-property
233 (point) (progn ,@form (point))
237 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
239 (defcustom gnus-face-0 'bold
240 "The \"%{hello%}\" face."
244 (defcustom gnus-face-1 'italic
245 "The \"%1{hello%}\" face."
249 (defcustom gnus-face-2 'bold-italic
250 "The \"%2{hello%}\" face."
254 (defcustom gnus-face-3 'bold
255 "The \"%3{hello%}\" face."
259 (defcustom gnus-face-4 'bold
260 "The \"%4{hello%}\" face."
264 (defun gnus-face-face-function (form type)
265 `(gnus-add-text-properties
266 (point) (progn ,@form (point))
269 ;; Delay consing the value of the `face' property until
270 ;; `gnus-add-text-properties' runs, since it will be modified
271 ;; by `gnus-put-text-property-excluding-characters-with-faces'.
272 (list ',(symbol-value (intern (format "gnus-face-%d" type))) 'default)
273 ;; Redundant now, but still convenient.
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."
298 (apply #'+ (mapcar #'char-width string)))
300 (defun gnus-correct-substring (string start &optional end)
305 (length (length string))
306 (string (concat string "\0")))
307 ;; Find the start position.
308 (while (and (< seek length)
310 (incf wseek (char-width (aref string seek)))
313 ;; Find the end position.
314 (while (and (<= seek length)
317 (incf wseek (char-width (aref string seek)))
320 (substring string wstart (1- wend))))
322 (defun gnus-string-width-function ()
324 (gnus-use-correct-string-widths
325 'gnus-correct-length)
326 ((fboundp 'string-width)
331 (defun gnus-substring-function ()
333 (gnus-use-correct-string-widths
334 'gnus-correct-substring)
335 ((fboundp 'string-width)
336 'gnus-correct-substring)
340 (defun gnus-tilde-max-form (el max-width)
341 "Return a form that limits EL to MAX-WIDTH."
342 (let ((max (abs max-width))
343 (length-fun (gnus-string-width-function))
344 (substring-fun (gnus-substring-function)))
346 `(if (> (,length-fun ,el) ,max)
348 `(,substring-fun ,el (- (,length-fun ,el) ,max))
349 `(if (gnus-lrm-string-p ,el)
350 (concat (,substring-fun ,el 0 ,max) ,gnus-lrm-string)
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 `(if (gnus-lrm-string-p val)
358 (concat (,substring-fun val 0 ,max) ,gnus-lrm-string)
359 (,substring-fun val 0 ,max)))
362 (defun gnus-tilde-cut-form (el cut-width)
363 "Return a form that cuts CUT-WIDTH off of EL."
364 (let ((cut (abs cut-width))
365 (length-fun (gnus-string-width-function))
366 (substring-fun (gnus-substring-function)))
368 `(if (> (,length-fun ,el) ,cut)
370 `(,substring-fun ,el 0 (- (,length-fun ,el) ,cut))
371 `(,substring-fun ,el ,cut))
373 `(let ((val (eval ,el)))
374 (if (> (,length-fun val) ,cut)
376 `(,substring-fun val 0 (- (,length-fun val) ,cut))
377 `(,substring-fun val ,cut))
380 (defun gnus-tilde-ignore-form (el ignore-value)
381 "Return a form that is blank when EL is IGNORE-VALUE."
383 `(if (equal ,el ,ignore-value)
385 `(let ((val (eval ,el)))
386 (if (equal val ,ignore-value)
389 (defun gnus-pad-form (el pad-width)
390 "Return a form that pads EL to PAD-WIDTH accounting for multi-column
391 characters correctly. This is because `format' may pad to columns or to
392 characters when given a pad value."
393 (let ((pad (abs pad-width))
394 (side (< 0 pad-width))
395 (length-fun (gnus-string-width-function)))
397 `(let ((need (- ,pad (,length-fun ,el))))
399 (concat ,(when side '(make-string need ?\ ))
401 ,(when (not side) '(make-string need ?\ )))
403 `(let* ((val (eval ,el))
404 (need (- ,pad (,length-fun val))))
406 (concat ,(when side '(make-string need ?\ ))
408 ,(when (not side) '(make-string need ?\ )))
411 (defun gnus-parse-format (format spec-alist &optional insert)
412 ;; This function parses the FORMAT string with the help of the
413 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
414 ;; string. If the FORMAT string contains the specifiers %( and %)
415 ;; the text between them will have the mouse-face text property.
416 ;; If the FORMAT string contains the specifiers %[ and %], the text between
417 ;; them will have the balloon-help text property.
418 (let ((case-fold-search nil))
420 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'\\|%[-0-9]*=\\|%[-0-9]*\\*"
422 (gnus-parse-complex-format format spec-alist)
423 ;; This is a simple format.
424 (gnus-parse-simple-format format spec-alist insert))))
426 (defun gnus-parse-complex-format (format spec-alist)
427 (let ((cursor-spec nil))
429 (gnus-set-work-buffer)
431 (goto-char (point-min))
432 (while (re-search-forward "\"" nil t)
433 (replace-match "\\\"" nil t))
434 (goto-char (point-min))
436 ;; Convert all font specs into font spec lists.
437 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
438 (let ((number (if (match-beginning 1)
439 (match-string 1) "0"))
440 (delim (aref (match-string 2) 0)))
441 (if (or (= delim ?\()
444 (replace-match (concat "\"("
445 (cond ((= delim ?\() "mouse")
446 ((= delim ?\{) "face")
450 (replace-match "\")\""))))
451 (goto-char (point-max))
453 ;; Convert point position commands.
454 (goto-char (point-min))
455 (let ((case-fold-search nil))
456 (while (re-search-forward "%\\([-0-9]+\\)?\\*" nil t)
457 (replace-match "\"(point)\"" t t)
458 (setq cursor-spec t)))
459 ;; Convert TAB commands.
460 (goto-char (point-min))
461 (while (re-search-forward "%\\([-0-9]+\\)=" nil t)
462 (replace-match (format "\"(tab %s)\"" (match-string 1)) t t))
463 ;; Convert the buffer into the spec.
464 (goto-char (point-min))
465 (let ((form (read (current-buffer))))
467 `(let (gnus-position)
468 ,@(gnus-complex-form-to-spec form spec-alist)
470 (gnus-put-text-property gnus-position (1+ gnus-position)
473 ,@(gnus-complex-form-to-spec form spec-alist)))))))
475 (defun gnus-complex-form-to-spec (form spec-alist)
481 (gnus-parse-simple-format sform spec-alist t))
482 ((eq (car sform) 'point)
483 '(setq gnus-position (point)))
484 ((eq (car sform) 'tab)
485 (gnus-spec-tab (cadr sform)))
487 (funcall (intern (format "gnus-%s-face-function" (car sform)))
488 (gnus-complex-form-to-spec (cddr sform) spec-alist)
493 (defun gnus-xmas-format (fstring &rest args)
494 "A version of `format' which preserves text properties.
496 Required for XEmacs, where the built in `format' function strips all text
497 properties from both the format string and any inserted strings.
499 Only supports the format sequence %s, and %% for inserting
500 literal % characters. A pad width and an optional - (to right pad)
501 are supported for %s."
502 (let ((re "%%\\|%\\(-\\)?\\([1-9][0-9]*\\)?s")
506 (goto-char (point-min))
507 (while (re-search-forward re nil t)
508 (goto-char (match-end 0))
510 ((string= (match-string 0) "%%")
514 (signal 'wrong-number-of-arguments
515 (list #'gnus-xmas-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 evalled.
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-set-format (type &optional insertable)
727 (set (intern (format "gnus-%s-line-format-spec" type))
729 (symbol-value (intern (format "gnus-%s-line-format" type)))
730 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
739 ;;; gnus-spec.el ends here