1 ;;; gnus-spec.el --- format spec functions for Gnus
3 ;; Copyright (C) 1996-2015 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 (eval-when-compile (require 'cl))
28 (defvar gnus-newsrc-file-version)
32 (defcustom gnus-use-correct-string-widths (featurep 'xemacs)
33 "*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 Emacs does this anyway."
45 ;;; Internal variables.
47 (defvar gnus-summary-mark-positions nil)
48 (defvar gnus-group-mark-positions nil)
49 (defvar gnus-group-indentation "")
51 ;; Format specs. The chunks below are the machine-generated forms
52 ;; that are to be evalled as the result of the default format strings.
53 ;; We write them in here to get them byte-compiled. That way the
54 ;; default actions will be quite fast, while still retaining the full
55 ;; flexibility of the user-defined format specs.
57 ;; First we have lots of dummy defvars to let the compiler know these
58 ;; are really dynamic variables.
60 (defvar gnus-tmp-unread)
61 (defvar gnus-tmp-replied)
62 (defvar gnus-tmp-score-char)
63 (defvar gnus-tmp-indentation)
64 (defvar gnus-tmp-opening-bracket)
65 (defvar gnus-tmp-lines)
66 (defvar gnus-tmp-name)
67 (defvar gnus-tmp-closing-bracket)
68 (defvar gnus-tmp-subject-or-nil)
69 (defvar gnus-tmp-subject)
70 (defvar gnus-tmp-marked)
71 (defvar gnus-tmp-marked-mark)
72 (defvar gnus-tmp-subscribed)
73 (defvar gnus-tmp-process-marked)
74 (defvar gnus-tmp-number-of-unread)
75 (defvar gnus-tmp-group-name)
76 (defvar gnus-tmp-group)
77 (defvar gnus-tmp-article-number)
78 (defvar gnus-tmp-unread-and-unselected)
79 (defvar gnus-tmp-news-method)
80 (defvar gnus-tmp-news-server)
81 (defvar gnus-mouse-face)
82 (defvar gnus-mouse-face-prop)
83 (defvar gnus-tmp-header)
84 (defvar gnus-tmp-from)
86 (declare-function gnus-summary-from-or-to-or-newsgroups "gnus-sum"
87 (header gnus-tmp-from))
89 (defmacro gnus-lrm-string-p (string)
90 (if (fboundp 'bidi-string-mark-left-to-right)
91 ;; LRM, RLM, PDF characters as integers to avoid breaking Emacs
93 `(memq (aref ,string (1- (length ,string))) '(8206 8207 8236))
96 (defvar gnus-lrm-string (if (ignore-errors (string 8206))
97 (propertize (string 8206) 'invisible t)
100 (defvar gnus-summary-line-format-spec nil)
101 (defvar gnus-summary-dummy-line-format-spec nil)
102 (defvar gnus-group-line-format-spec nil)
104 (defvar gnus-format-specs
105 `((version . ,emacs-version)
106 (gnus-version . ,(gnus-continuum-version)))
107 "Alist of format specs.")
109 (defvar gnus-default-format-specs gnus-format-specs)
111 (defvar gnus-article-mode-line-format-spec nil)
112 (defvar gnus-summary-mode-line-format-spec nil)
113 (defvar gnus-group-mode-line-format-spec nil)
115 ;;; Phew. All that gruft is over with, fortunately.
118 (defun gnus-update-format (var)
119 "Update the format specification near point."
124 ;; Find the end of the current word.
125 (re-search-forward "[ \t\n]" nil t)
127 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
129 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
130 (match-string 1 var))))
131 (entry (assq type gnus-format-specs))
134 (setq gnus-format-specs (delq entry gnus-format-specs)))
136 (intern (format "%s-spec" var))
137 (gnus-parse-format (setq value (symbol-value (intern var)))
138 (symbol-value (intern (format "%s-alist" var)))
139 (not (string-match "mode" var))))
140 (setq spec (symbol-value (intern (format "%s-spec" var))))
141 (push (list type value spec) gnus-format-specs)
143 (pop-to-buffer "*Gnus Format*")
145 (lisp-interaction-mode)
146 (insert (gnus-pp-to-string spec))))
148 (defun gnus-update-format-specifications (&optional force &rest types)
149 "Update all (necessary) format specifications.
150 Return a list of updated types."
151 ;; Make the indentation array.
152 ;; See whether all the stored info needs to be flushed.
154 (not gnus-newsrc-file-version)
155 (not (equal (gnus-continuum-version)
156 (gnus-continuum-version gnus-newsrc-file-version)))
157 (not (equal emacs-version
158 (cdr (assq 'version gnus-format-specs)))))
159 (setq gnus-format-specs nil))
160 ;; Go through all the formats and see whether they need updating.
161 (let (new-format entry type val updated)
162 (while (setq type (pop types))
163 ;; Jump to the proper buffer to find out the value of the
164 ;; variable, if possible. (It may be buffer-local.)
166 (let ((buffer (intern (format "gnus-%s-buffer" type))))
167 (when (and (boundp buffer)
168 (setq val (symbol-value buffer))
169 (gnus-buffer-exists-p val))
171 (setq new-format (symbol-value
172 (intern (format "gnus-%s-line-format" type)))))
173 (setq entry (cdr (assq type gnus-format-specs)))
175 (equal (car entry) new-format))
176 ;; Use the old format.
177 (set (intern (format "gnus-%s-line-format-spec" type))
179 ;; This is a new format.
181 (if (not (stringp new-format))
182 ;; This is a function call or something.
184 ;; This is a "real" format.
188 (intern (format "gnus-%s-line-format-alist" type)))
189 (not (string-match "mode$" (symbol-name type))))))
190 ;; Enter the new format spec into the list.
193 (setcar (cdr entry) val)
194 (setcar entry new-format))
195 (push (list type new-format val) gnus-format-specs))
196 (set (intern (format "gnus-%s-line-format-spec" type)) val)
197 (push type updated))))
199 (unless (assq 'version gnus-format-specs)
200 (push (cons 'version emacs-version) gnus-format-specs))
203 (defcustom gnus-mouse-face-0 'highlight
204 "The \"%(hello%)\" face."
208 (defcustom gnus-mouse-face-1 'highlight
209 "The \"%1(hello%)\" face."
213 (defcustom gnus-mouse-face-2 'highlight
214 "The \"%2(hello%)\" face."
218 (defcustom gnus-mouse-face-3 'highlight
219 "The \"%3(hello%)\" face."
223 (defcustom gnus-mouse-face-4 'highlight
224 "The \"%4(hello%)\" face."
228 (defun gnus-mouse-face-function (form type)
229 `(gnus-put-text-property
230 (point) (progn ,@form (point))
234 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
236 (defcustom gnus-face-0 'bold
237 "The \"%{hello%}\" face."
241 (defcustom gnus-face-1 'italic
242 "The \"%1{hello%}\" face."
246 (defcustom gnus-face-2 'bold-italic
247 "The \"%2{hello%}\" face."
251 (defcustom gnus-face-3 'bold
252 "The \"%3{hello%}\" face."
256 (defcustom gnus-face-4 'bold
257 "The \"%4{hello%}\" face."
261 (defun gnus-face-face-function (form type)
262 `(gnus-add-text-properties
263 (point) (progn ,@form (point))
266 ;; Delay consing the value of the `face' property until
267 ;; `gnus-add-text-properties' runs, since it will be modified
268 ;; by `gnus-put-text-property-excluding-characters-with-faces'.
269 (list ',(symbol-value (intern (format "gnus-face-%d" type))) 'default)
270 ;; Redundant now, but still convenient.
273 (defun gnus-balloon-face-function (form type)
274 `(gnus-put-text-property
275 (point) (progn ,@form (point))
276 ,(if (fboundp 'balloon-help-mode)
279 ,(intern (format "gnus-balloon-face-%d" type))))
281 (defun gnus-spec-tab (column)
283 `(insert-char ? (max (- ,column (current-column)) 0))
284 (let ((column (abs column)))
285 `(if (> (current-column) ,column)
287 (if (= (move-to-column ,column) ,column)
288 (delete-region (point) end)
289 (delete-region (1- (point)) end)
291 (insert-char ? (max (- ,column (current-column)) 0))))))
293 (defun gnus-correct-length (string)
294 "Return the correct width of STRING."
295 (apply #'+ (mapcar #'char-width string)))
297 (defun gnus-correct-substring (string start &optional end)
302 (length (length string))
303 (string (concat string "\0")))
304 ;; Find the start position.
305 (while (and (< seek length)
307 (incf wseek (char-width (aref string seek)))
310 ;; Find the end position.
311 (while (and (<= seek length)
314 (incf wseek (char-width (aref string seek)))
317 (substring string wstart (1- wend))))
319 (defun gnus-string-width-function ()
321 (gnus-use-correct-string-widths
322 'gnus-correct-length)
323 ((fboundp 'string-width)
328 (defun gnus-substring-function ()
330 (gnus-use-correct-string-widths
331 'gnus-correct-substring)
332 ((fboundp 'string-width)
333 'gnus-correct-substring)
337 (defun gnus-tilde-max-form (el max-width)
338 "Return a form that limits EL to MAX-WIDTH."
339 (let ((max (abs max-width))
340 (length-fun (gnus-string-width-function))
341 (substring-fun (gnus-substring-function)))
343 `(if (> (,length-fun ,el) ,max)
345 `(,substring-fun ,el (- (,length-fun ,el) ,max))
346 `(if (gnus-lrm-string-p ,el)
347 (concat (,substring-fun ,el 0 ,max) ,gnus-lrm-string)
348 (,substring-fun ,el 0 ,max)))
350 `(let ((val (eval ,el)))
351 (if (> (,length-fun val) ,max)
353 `(,substring-fun val (- (,length-fun val) ,max))
354 `(if (gnus-lrm-string-p val)
355 (concat (,substring-fun val 0 ,max) ,gnus-lrm-string)
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 (signal 'wrong-number-of-arguments
512 (list #'gnus-xmas-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))))
624 ;; We used to use "%l" for displaying the grouplens score.
626 (setq elem '("" ?s)))
628 (setq elem '("*" ?s))))
629 (setq elem-type (cadr elem))
630 ;; Insert the new format elements.
632 (not (and (featurep 'xemacs)
633 gnus-use-correct-string-widths)))
634 (insert (number-to-string pad-width)))
635 ;; Create the form to be evalled.
636 (if (or max-width cut-width ignore-value
637 (and (featurep 'xemacs)
638 gnus-use-correct-string-widths))
641 (let ((el (car elem)))
642 (cond ((= (cadr elem) ?c)
643 (setq el (list 'char-to-string el)))
645 (setq el (list 'int-to-string el))))
647 (setq el (gnus-tilde-ignore-form el ignore-value)))
649 (setq el (gnus-tilde-cut-form el cut-width)))
651 (setq el (gnus-tilde-max-form el max-width)))
653 (setq el (gnus-pad-form el pad-width)))
656 (push (car elem) flist))))
657 (setq fstring (buffer-substring-no-properties (point-min) (point-max))))
659 ;; Do some postprocessing to increase efficiency.
664 ((string= fstring "")
666 ;; Not a format string.
667 ((not (string-match "%" fstring))
669 ;; A format string with just a single string spec.
670 ((string= fstring "%s")
672 ;; A single character.
673 ((string= fstring "%c")
676 ((string= fstring "%d")
679 `(insert (int-to-string ,(car flist)))
680 (list `(int-to-string ,(car flist)))))
681 ;; Just lots of chars and strings.
682 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
684 ;; A single string spec at the beginning of the spec.
685 ((string-match "\\`%[sc][^%]+\\'" fstring)
686 (list (car flist) (substring fstring 2)))
687 ;; A single string spec in the middle of the spec.
688 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
689 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
690 ;; A single string spec in the end of the spec.
691 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
692 (list (match-string 1 fstring) (car flist)))
693 ;; Only string (and %) specs (XEmacs only!)
694 ((and (featurep 'xemacs)
695 gnus-make-format-preserve-properties
697 "\\`\\([^%]*\\(%%\\|%-?\\([1-9][0-9]*\\)?s\\)\\)*[^%]*\\'"
699 (list (cons 'gnus-xmas-format (cons fstring (nreverse flist)))))
700 ;; A more complex spec.
702 (list (cons 'format (cons fstring (nreverse flist)))))))
708 (cons 'insert result)))
709 (cond ((stringp result)
712 (cons 'concat result))
715 (defun gnus-eval-format (format &optional alist props)
716 "Eval the format variable FORMAT, using ALIST.
717 If PROPS, insert the result."
718 (let ((form (gnus-parse-format format alist props)))
720 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
723 (defun gnus-set-format (type &optional insertable)
724 (set (intern (format "gnus-%s-line-format-spec" type))
726 (symbol-value (intern (format "gnus-%s-line-format" type)))
727 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
736 ;;; gnus-spec.el ends here