1 ;;; gnus-spec.el --- format spec functions for Gnus
3 ;; Copyright (C) 1996-2013 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-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 (defmacro gnus-lrm-string-p (string)
94 (if (fboundp 'bidi-string-mark-left-to-right)
95 ;; LRM, RLM, PDF characters as integers to avoid breaking Emacs
97 `(memq (aref ,string (1- (length ,string))) '(8206 8207 8236))
100 (defvar gnus-lrm-string (if (ignore-errors (string 8206))
101 (propertize (string 8206) 'invisible t)
104 (defvar gnus-summary-line-format-spec nil)
105 (defvar gnus-summary-dummy-line-format-spec nil)
106 (defvar gnus-group-line-format-spec nil)
108 (defvar gnus-format-specs
109 `((version . ,emacs-version)
110 (gnus-version . ,(gnus-continuum-version)))
111 "Alist of format specs.")
113 (defvar gnus-default-format-specs gnus-format-specs)
115 (defvar gnus-article-mode-line-format-spec nil)
116 (defvar gnus-summary-mode-line-format-spec nil)
117 (defvar gnus-group-mode-line-format-spec nil)
119 ;;; Phew. All that gruft is over with, fortunately.
122 (defun gnus-update-format (var)
123 "Update the format specification near point."
128 ;; Find the end of the current word.
129 (re-search-forward "[ \t\n]" nil t)
131 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
133 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
134 (match-string 1 var))))
135 (entry (assq type gnus-format-specs))
138 (setq gnus-format-specs (delq entry gnus-format-specs)))
140 (intern (format "%s-spec" var))
141 (gnus-parse-format (setq value (symbol-value (intern var)))
142 (symbol-value (intern (format "%s-alist" var)))
143 (not (string-match "mode" var))))
144 (setq spec (symbol-value (intern (format "%s-spec" var))))
145 (push (list type value spec) gnus-format-specs)
147 (pop-to-buffer "*Gnus Format*")
149 (lisp-interaction-mode)
150 (insert (gnus-pp-to-string spec))))
152 (defun gnus-update-format-specifications (&optional force &rest types)
153 "Update all (necessary) format specifications.
154 Return a list of updated types."
155 ;; Make the indentation array.
156 ;; See whether all the stored info needs to be flushed.
158 (not gnus-newsrc-file-version)
159 (not (equal (gnus-continuum-version)
160 (gnus-continuum-version gnus-newsrc-file-version)))
161 (not (equal emacs-version
162 (cdr (assq 'version gnus-format-specs)))))
163 (setq gnus-format-specs nil))
164 ;; Go through all the formats and see whether they need updating.
165 (let (new-format entry type val updated)
166 (while (setq type (pop types))
167 ;; Jump to the proper buffer to find out the value of the
168 ;; variable, if possible. (It may be buffer-local.)
170 (let ((buffer (intern (format "gnus-%s-buffer" type))))
171 (when (and (boundp buffer)
172 (setq val (symbol-value buffer))
173 (gnus-buffer-exists-p val))
175 (setq new-format (symbol-value
176 (intern (format "gnus-%s-line-format" type)))))
177 (setq entry (cdr (assq type gnus-format-specs)))
179 (equal (car entry) new-format))
180 ;; Use the old format.
181 (set (intern (format "gnus-%s-line-format-spec" type))
183 ;; This is a new format.
185 (if (not (stringp new-format))
186 ;; This is a function call or something.
188 ;; This is a "real" format.
192 (intern (format "gnus-%s-line-format-alist" type)))
193 (not (string-match "mode$" (symbol-name type))))))
194 ;; Enter the new format spec into the list.
197 (setcar (cdr entry) val)
198 (setcar entry new-format))
199 (push (list type new-format val) gnus-format-specs))
200 (set (intern (format "gnus-%s-line-format-spec" type)) val)
201 (push type updated))))
203 (unless (assq 'version gnus-format-specs)
204 (push (cons 'version emacs-version) gnus-format-specs))
207 (defcustom gnus-mouse-face-0 'highlight
208 "The \"%(hello%)\" face."
212 (defcustom gnus-mouse-face-1 'highlight
213 "The \"%1(hello%)\" face."
217 (defcustom gnus-mouse-face-2 'highlight
218 "The \"%2(hello%)\" face."
222 (defcustom gnus-mouse-face-3 'highlight
223 "The \"%3(hello%)\" face."
227 (defcustom gnus-mouse-face-4 'highlight
228 "The \"%4(hello%)\" face."
232 (defun gnus-mouse-face-function (form type)
233 `(gnus-put-text-property
234 (point) (progn ,@form (point))
238 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
240 (defcustom gnus-face-0 'bold
241 "The \"%{hello%}\" face."
245 (defcustom gnus-face-1 'italic
246 "The \"%1{hello%}\" face."
250 (defcustom gnus-face-2 'bold-italic
251 "The \"%2{hello%}\" face."
255 (defcustom gnus-face-3 'bold
256 "The \"%3{hello%}\" face."
260 (defcustom gnus-face-4 'bold
261 "The \"%4{hello%}\" face."
265 (defun gnus-face-face-function (form type)
266 `(gnus-add-text-properties
267 (point) (progn ,@form (point))
270 ;; Delay consing the value of the `face' property until
271 ;; `gnus-add-text-properties' runs, since it will be modified
272 ;; by `gnus-put-text-property-excluding-characters-with-faces'.
273 (list ',(symbol-value (intern (format "gnus-face-%d" type))) 'default)
274 ;; Redundant now, but still convenient.
277 (defun gnus-balloon-face-function (form type)
278 `(gnus-put-text-property
279 (point) (progn ,@form (point))
280 ,(if (fboundp 'balloon-help-mode)
283 ,(intern (format "gnus-balloon-face-%d" type))))
285 (defun gnus-spec-tab (column)
287 `(insert-char ? (max (- ,column (current-column)) 0))
288 (let ((column (abs column)))
289 `(if (> (current-column) ,column)
291 (if (= (move-to-column ,column) ,column)
292 (delete-region (point) end)
293 (delete-region (1- (point)) end)
295 (insert-char ? (max (- ,column (current-column)) 0))))))
297 (defun gnus-correct-length (string)
298 "Return the correct width of STRING."
299 (apply #'+ (mapcar #'char-width 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 (char-width (aref string seek)))
314 ;; Find the end position.
315 (while (and (<= seek length)
318 (incf wseek (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 `(if (gnus-lrm-string-p ,el)
351 (concat (,substring-fun ,el 0 ,max) ,gnus-lrm-string)
352 (,substring-fun ,el 0 ,max)))
354 `(let ((val (eval ,el)))
355 (if (> (,length-fun val) ,max)
357 `(,substring-fun val (- (,length-fun val) ,max))
358 `(if (gnus-lrm-string-p val)
359 (concat (,substring-fun val 0 ,max) ,gnus-lrm-string)
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 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