1 ;;; gnus-spec.el --- format spec functions for Gnus
3 ;; Copyright (C) 1996-2018 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 <https://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'cl
))
28 (defvar gnus-newsrc-file-version
)
32 ;;; Internal variables.
34 (defvar gnus-summary-mark-positions nil
)
35 (defvar gnus-group-mark-positions nil
)
36 (defvar gnus-group-indentation
"")
38 ;; Format specs. The chunks below are the machine-generated forms
39 ;; that are to be evalled as the result of the default format strings.
40 ;; We write them in here to get them byte-compiled. That way the
41 ;; default actions will be quite fast, while still retaining the full
42 ;; flexibility of the user-defined format specs.
44 ;; First we have lots of dummy defvars to let the compiler know these
45 ;; are really dynamic variables.
47 (defvar gnus-tmp-unread
)
48 (defvar gnus-tmp-replied
)
49 (defvar gnus-tmp-score-char
)
50 (defvar gnus-tmp-indentation
)
51 (defvar gnus-tmp-opening-bracket
)
52 (defvar gnus-tmp-lines
)
53 (defvar gnus-tmp-name
)
54 (defvar gnus-tmp-closing-bracket
)
55 (defvar gnus-tmp-subject-or-nil
)
56 (defvar gnus-tmp-subject
)
57 (defvar gnus-tmp-marked
)
58 (defvar gnus-tmp-marked-mark
)
59 (defvar gnus-tmp-subscribed
)
60 (defvar gnus-tmp-process-marked
)
61 (defvar gnus-tmp-number-of-unread
)
62 (defvar gnus-tmp-group-name
)
63 (defvar gnus-tmp-group
)
64 (defvar gnus-tmp-article-number
)
65 (defvar gnus-tmp-unread-and-unselected
)
66 (defvar gnus-tmp-news-method
)
67 (defvar gnus-tmp-news-server
)
68 (defvar gnus-mouse-face
)
69 (defvar gnus-tmp-header
)
70 (defvar gnus-tmp-from
)
72 (declare-function gnus-summary-from-or-to-or-newsgroups
"gnus-sum"
73 (header gnus-tmp-from
))
75 (defmacro gnus-lrm-string-p
(string)
76 ;; LRM, RLM, PDF characters as integers to avoid breaking Emacs
78 `(memq (aref ,string
(1- (length ,string
))) '(8206 8207 8236)))
80 (defvar gnus-lrm-string
(if (ignore-errors (string 8206))
81 (propertize (string 8206) 'invisible t
)
84 (defvar gnus-summary-line-format-spec nil
)
85 (defvar gnus-summary-dummy-line-format-spec nil
)
86 (defvar gnus-group-line-format-spec nil
)
88 (defvar gnus-format-specs
89 `((version .
,emacs-version
)
90 (gnus-version .
,(gnus-continuum-version)))
91 "Alist of format specs.")
93 (defvar gnus-default-format-specs gnus-format-specs
)
95 (defvar gnus-article-mode-line-format-spec nil
)
96 (defvar gnus-summary-mode-line-format-spec nil
)
97 (defvar gnus-group-mode-line-format-spec nil
)
99 ;;; Phew. All that gruft is over with, fortunately.
102 (defun gnus-update-format (var)
103 "Update the format specification near point."
108 ;; Find the end of the current word.
109 (re-search-forward "[ \t\n]" nil t
)
111 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t
)
113 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var
)
114 (match-string 1 var
))))
115 (entry (assq type gnus-format-specs
))
118 (setq gnus-format-specs
(delq entry gnus-format-specs
)))
120 (intern (format "%s-spec" var
))
121 (gnus-parse-format (setq value
(symbol-value (intern var
)))
122 (symbol-value (intern (format "%s-alist" var
)))
123 (not (string-match "mode" var
))))
124 (setq spec
(symbol-value (intern (format "%s-spec" var
))))
125 (push (list type value spec
) gnus-format-specs
)
127 (pop-to-buffer "*Gnus Format*")
129 (lisp-interaction-mode)
130 (insert (gnus-pp-to-string spec
))))
132 (defun gnus-update-format-specifications (&optional force
&rest types
)
133 "Update all (necessary) format specifications.
134 Return a list of updated types."
135 ;; Make the indentation array.
136 ;; See whether all the stored info needs to be flushed.
138 (not gnus-newsrc-file-version
)
139 (not (equal (gnus-continuum-version)
140 (gnus-continuum-version gnus-newsrc-file-version
)))
141 (not (equal emacs-version
142 (cdr (assq 'version gnus-format-specs
)))))
143 (setq gnus-format-specs nil
))
144 ;; Go through all the formats and see whether they need updating.
145 (let (new-format entry type val updated
)
146 (while (setq type
(pop types
))
147 ;; Jump to the proper buffer to find out the value of the
148 ;; variable, if possible. (It may be buffer-local.)
150 (let ((buffer (intern (format "gnus-%s-buffer" type
))))
151 (when (and (boundp buffer
)
152 (setq val
(symbol-value buffer
))
153 (gnus-buffer-exists-p val
))
155 (setq new-format
(symbol-value
156 (intern (format "gnus-%s-line-format" type
)))))
157 (setq entry
(cdr (assq type gnus-format-specs
)))
159 (equal (car entry
) new-format
))
160 ;; Use the old format.
161 (set (intern (format "gnus-%s-line-format-spec" type
))
163 ;; This is a new format.
165 (if (not (stringp new-format
))
166 ;; This is a function call or something.
168 ;; This is a "real" format.
172 (intern (format "gnus-%s-line-format-alist" type
)))
173 (not (string-match "mode$" (symbol-name type
))))))
174 ;; Enter the new format spec into the list.
177 (setcar (cdr entry
) val
)
178 (setcar entry new-format
))
179 (push (list type new-format val
) gnus-format-specs
))
180 (set (intern (format "gnus-%s-line-format-spec" type
)) val
)
181 (push type updated
))))
183 (unless (assq 'version gnus-format-specs
)
184 (push (cons 'version emacs-version
) gnus-format-specs
))
187 (defcustom gnus-mouse-face-0
'highlight
188 "The \"%(hello%)\" face."
192 (defcustom gnus-mouse-face-1
'highlight
193 "The \"%1(hello%)\" face."
197 (defcustom gnus-mouse-face-2
'highlight
198 "The \"%2(hello%)\" face."
202 (defcustom gnus-mouse-face-3
'highlight
203 "The \"%3(hello%)\" face."
207 (defcustom gnus-mouse-face-4
'highlight
208 "The \"%4(hello%)\" face."
212 (defun gnus-mouse-face-function (form type
)
214 (point) (progn ,@form
(point))
218 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type
)))))))
220 (defcustom gnus-face-0
'bold
221 "The \"%{hello%}\" face."
225 (defcustom gnus-face-1
'italic
226 "The \"%1{hello%}\" face."
230 (defcustom gnus-face-2
'bold-italic
231 "The \"%2{hello%}\" face."
235 (defcustom gnus-face-3
'bold
236 "The \"%3{hello%}\" face."
240 (defcustom gnus-face-4
'bold
241 "The \"%4{hello%}\" face."
245 (defun gnus-face-face-function (form type
)
246 `(add-text-properties
247 (point) (progn ,@form
(point))
250 ;; Delay consing the value of the `face' property until
251 ;; `add-text-properties' runs, since it will be modified
252 ;; by `put-text-property-excluding-characters-with-faces'.
253 (list ',(symbol-value (intern (format "gnus-face-%d" type
))) 'default
)
254 ;; Redundant now, but still convenient.
257 (defun gnus-balloon-face-function (form type
)
259 (point) (progn ,@form
(point)) 'help-echo
260 ,(intern (format "gnus-balloon-face-%d" type
))))
262 (defun gnus-spec-tab (column)
264 `(insert-char ?
(max (- ,column
(current-column)) 0))
265 (let ((column (abs column
)))
266 `(if (> (current-column) ,column
)
268 (if (= (move-to-column ,column
) ,column
)
269 (delete-region (point) end
)
270 (delete-region (1- (point)) end
)
272 (insert-char ?
(max (- ,column
(current-column)) 0))))))
274 (defun gnus-correct-length (string)
275 "Return the correct width of STRING."
276 (apply #'+ (mapcar #'char-width string
)))
278 (defun gnus-correct-substring (string start
&optional end
)
283 (length (length string
))
284 (string (concat string
"\0")))
285 ;; Find the start position.
286 (while (and (< seek length
)
288 (incf wseek
(char-width (aref string seek
)))
291 ;; Find the end position.
292 (while (and (<= seek length
)
295 (incf wseek
(char-width (aref string seek
)))
298 (substring string wstart
(1- wend
))))
300 (defun gnus-tilde-max-form (el max-width
)
301 "Return a form that limits EL to MAX-WIDTH."
302 (let ((max (abs max-width
)))
304 `(if (> (string-width ,el
) ,max
)
306 `(gnus-correct-substring ,el
(- (string-width ,el
) ,max
))
307 `(if (gnus-lrm-string-p ,el
)
308 (concat (gnus-correct-substring ,el
0 ,max
)
310 (gnus-correct-substring ,el
0 ,max
)))
312 `(let ((val (eval ,el
)))
313 (if (> (string-width val
) ,max
)
315 `(gnus-correct-substring val
(- (string-width val
) ,max
))
316 `(if (gnus-lrm-string-p val
)
317 (concat (gnus-correct-substring val
0 ,max
)
319 (gnus-correct-substring val
0 ,max
)))
322 (defun gnus-tilde-cut-form (el cut-width
)
323 "Return a form that cuts CUT-WIDTH off of EL."
324 (let ((cut (abs cut-width
)))
326 `(if (> (string-width ,el
) ,cut
)
328 `(gnus-correct-substring ,el
0 (- (string-width ,el
) ,cut
))
329 `(gnus-correct-substring ,el
,cut
))
331 `(let ((val (eval ,el
)))
332 (if (> (string-width val
) ,cut
)
334 `(gnus-correct-substring val
0 (- (string-width val
) ,cut
))
335 `(gnus-correct-substring val
,cut
))
338 (defun gnus-tilde-ignore-form (el ignore-value
)
339 "Return a form that is blank when EL is IGNORE-VALUE."
341 `(if (equal ,el
,ignore-value
)
343 `(let ((val (eval ,el
)))
344 (if (equal val
,ignore-value
)
347 (defun gnus-pad-form (el pad-width
)
348 "Return a form that pads EL to PAD-WIDTH accounting for multi-column
349 characters correctly. This is because `format' may pad to columns or to
350 characters when given a pad value."
351 (let ((pad (abs pad-width
))
352 (side (< 0 pad-width
)))
354 `(let ((need (- ,pad
(string-width ,el
))))
356 (concat ,(when side
'(make-string need ?\
))
358 ,(when (not side
) '(make-string need ?\
)))
360 `(let* ((val (eval ,el
))
361 (need (- ,pad
(string-width val
))))
363 (concat ,(when side
'(make-string need ?\
))
365 ,(when (not side
) '(make-string need ?\
)))
368 (defun gnus-parse-format (format spec-alist
&optional insert
)
369 ;; This function parses the FORMAT string with the help of the
370 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
371 ;; string. If the FORMAT string contains the specifiers %( and %)
372 ;; the text between them will have the mouse-face text property.
373 ;; If the FORMAT string contains the specifiers %[ and %], the text between
374 ;; them will have the balloon-help text property.
375 (let ((case-fold-search nil
))
377 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'\\|%[-0-9]*=\\|%[-0-9]*\\*"
379 (gnus-parse-complex-format format spec-alist
)
380 ;; This is a simple format.
381 (gnus-parse-simple-format format spec-alist insert
))))
383 (defun gnus-parse-complex-format (format spec-alist
)
384 (let ((cursor-spec nil
))
386 (gnus-set-work-buffer)
388 (goto-char (point-min))
389 (while (re-search-forward "\"" nil t
)
390 (replace-match "\\\"" nil t
))
391 (goto-char (point-min))
393 ;; Convert all font specs into font spec lists.
394 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t
)
395 (let ((number (if (match-beginning 1)
396 (match-string 1) "0"))
397 (delim (aref (match-string 2) 0)))
398 (if (or (= delim ?\
()
401 (replace-match (concat "\"("
402 (cond ((= delim ?\
() "mouse")
403 ((= delim ?\
{) "face")
407 (replace-match "\")\""))))
408 (goto-char (point-max))
410 ;; Convert point position commands.
411 (goto-char (point-min))
412 (let ((case-fold-search nil
))
413 (while (re-search-forward "%\\([-0-9]+\\)?\\*" nil t
)
414 (replace-match "\"(point)\"" t t
)
415 (setq cursor-spec t
)))
416 ;; Convert TAB commands.
417 (goto-char (point-min))
418 (while (re-search-forward "%\\([-0-9]+\\)=" nil t
)
419 (replace-match (format "\"(tab %s)\"" (match-string 1)) t t
))
420 ;; Convert the buffer into the spec.
421 (goto-char (point-min))
422 (let ((form (read (current-buffer))))
424 `(let (gnus-position)
425 ,@(gnus-complex-form-to-spec form spec-alist
)
427 (put-text-property gnus-position
(1+ gnus-position
)
430 ,@(gnus-complex-form-to-spec form spec-alist
)))))))
432 (defun gnus-complex-form-to-spec (form spec-alist
)
438 (gnus-parse-simple-format sform spec-alist t
))
439 ((eq (car sform
) 'point
)
440 '(setq gnus-position
(point)))
441 ((eq (car sform
) 'tab
)
442 (gnus-spec-tab (cadr sform
)))
444 (funcall (intern (format "gnus-%s-face-function" (car sform
)))
445 (gnus-complex-form-to-spec (cddr sform
) spec-alist
)
449 (defun gnus-parse-simple-format (format spec-alist
&optional insert
)
450 ;; This function parses the FORMAT string with the help of the
451 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
454 spec flist fstring elem result dontinsert user-defined
455 type value pad-width spec-beg cut-width ignore-value
456 tilde-form tilde elem-type extended-spec
)
458 (gnus-set-work-buffer)
460 (goto-char (point-min))
461 (while (re-search-forward "%" nil t
)
462 (setq user-defined nil
470 (setq spec-beg
(1- (point)))
472 ;; Parse this spec fully.
475 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
476 (setq pad-width
(string-to-number (match-string 1)))
477 (when (match-beginning 2)
478 (setq max-width
(string-to-number (buffer-substring
479 (1+ (match-beginning 2))
481 (goto-char (match-end 0)))
484 (setq tilde
(read (current-buffer))
488 ((memq type
'(pad pad-left
))
489 (setq pad-width value
))
490 ((eq type
'pad-right
)
491 (setq pad-width
(- value
)))
492 ((memq type
'(max-right max
))
493 (setq max-width value
))
495 (setq max-width
(- value
)))
496 ((memq type
'(cut cut-left
))
497 (setq cut-width value
))
498 ((eq type
'cut-right
)
499 (setq cut-width
(- value
)))
502 (if (stringp value
) value
(format "%s" value
))))
504 (setq tilde-form value
))
506 (error "Unknown tilde type: %s" tilde
)))
511 ;; User-defined spec -- find the spec name.
512 ((eq (setq spec
(char-after)) ?u
)
514 (when (and (eq (setq user-defined
(char-after)) ?
&)
515 (looking-at "&\\([^;]+\\);"))
516 (setq user-defined
(match-string 1))
517 (goto-char (match-end 1))))
519 ((and (eq spec ?
&) (looking-at "&\\([^;]+\\);"))
520 (setq extended-spec
(intern (match-string 1)))
521 (goto-char (match-end 1))))
523 (delete-region spec-beg
(point))
525 ;; Now we have all the relevant data on this spec, so
526 ;; we start doing stuff.
529 ;; "%%" just results in a "%".
534 (setq elem
(list tilde-form ?s
)))
535 ;; Treat user defined format specifiers specially.
539 (list (intern (format
540 (if (stringp user-defined
)
541 "gnus-user-format-function-%s"
542 "gnus-user-format-function-%c")
546 ;; Find the specification from `spec-alist'.
547 ((setq elem
(cdr (assq (or extended-spec spec
) spec-alist
))))
548 ;; We used to use "%l" for displaying the grouplens score.
550 (setq elem
'("" ?s
)))
552 (setq elem
'("*" ?s
))))
553 (setq elem-type
(cadr elem
))
554 ;; Insert the new format elements.
556 (insert (number-to-string pad-width
)))
557 ;; Create the form to be evalled.
558 (if (or max-width cut-width ignore-value
)
561 (let ((el (car elem
)))
562 (cond ((= (cadr elem
) ?c
)
563 (setq el
(list 'char-to-string el
)))
565 (setq el
(list 'int-to-string el
))))
567 (setq el
(gnus-tilde-ignore-form el ignore-value
)))
569 (setq el
(gnus-tilde-cut-form el cut-width
)))
571 (setq el
(gnus-tilde-max-form el max-width
)))
573 (setq el
(gnus-pad-form el pad-width
)))
576 (push (car elem
) flist
))))
577 (setq fstring
(buffer-substring-no-properties (point-min) (point-max))))
579 ;; Do some postprocessing to increase efficiency.
584 ((string= fstring
"")
586 ;; Not a format string.
587 ((not (string-match "%" fstring
))
589 ;; A format string with just a single string spec.
590 ((string= fstring
"%s")
592 ;; A single character.
593 ((string= fstring
"%c")
596 ((string= fstring
"%d")
599 `(insert (int-to-string ,(car flist
)))
600 (list `(int-to-string ,(car flist
)))))
601 ;; Just lots of chars and strings.
602 ((string-match "\\`\\(%[cs]\\)+\\'" fstring
)
604 ;; A single string spec at the beginning of the spec.
605 ((string-match "\\`%[sc][^%]+\\'" fstring
)
606 (list (car flist
) (substring fstring
2)))
607 ;; A single string spec in the middle of the spec.
608 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring
)
609 (list (match-string 1 fstring
) (car flist
) (match-string 2 fstring
)))
610 ;; A single string spec in the end of the spec.
611 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring
)
612 (list (match-string 1 fstring
) (car flist
)))
613 ;; A more complex spec.
615 (list (cons 'format
(cons fstring
(nreverse flist
)))))))
621 (cons 'insert result
)))
622 (cond ((stringp result
)
625 (cons 'concat result
))
628 (defun gnus-eval-format (format &optional alist props
)
629 "Eval the format variable FORMAT, using ALIST.
630 If PROPS, insert the result."
631 (let ((form (gnus-parse-format format alist props
)))
633 (add-text-properties (point) (progn (eval form
) (point)) props
)
636 (defun gnus-set-format (type &optional insertable
)
637 (set (intern (format "gnus-%s-line-format-spec" type
))
639 (symbol-value (intern (format "gnus-%s-line-format" type
)))
640 (symbol-value (intern (format "gnus-%s-line-format-alist" type
)))
644 (defun gnus-summary-line-format-spec ()
645 (insert gnus-tmp-unread gnus-tmp-replied
646 gnus-tmp-score-char gnus-tmp-indentation
)
651 gnus-tmp-opening-bracket
654 (if (> (length gnus-tmp-name
) 20)
655 (truncate-string-to-width gnus-tmp-name
20)
657 gnus-tmp-closing-bracket
)
659 'mouse-face gnus-mouse-face
)
660 (insert " " gnus-tmp-subject-or-nil
"\n"))
668 ;;; gnus-spec.el ends here