1 ;;; gnus-spec.el --- format spec functions for Gnus
3 ;; Copyright (C) 1996-2012 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))
268 '(gnus-face t face
,(symbol-value (intern (format "gnus-face-%d" type
))))))
270 (defun gnus-balloon-face-function (form type
)
271 `(gnus-put-text-property
272 (point) (progn ,@form
(point))
273 ,(if (fboundp 'balloon-help-mode
)
276 ,(intern (format "gnus-balloon-face-%d" type
))))
278 (defun gnus-spec-tab (column)
280 `(insert-char ?
(max (- ,column
(current-column)) 0))
281 (let ((column (abs column
)))
282 `(if (> (current-column) ,column
)
284 (if (= (move-to-column ,column
) ,column
)
285 (delete-region (point) end
)
286 (delete-region (1- (point)) end
)
288 (insert-char ?
(max (- ,column
(current-column)) 0))))))
290 (defun gnus-correct-length (string)
291 "Return the correct width of STRING."
292 (apply #'+ (mapcar #'char-width string
)))
294 (defun gnus-correct-substring (string start
&optional end
)
299 (length (length string
))
300 (string (concat string
"\0")))
301 ;; Find the start position.
302 (while (and (< seek length
)
304 (incf wseek
(char-width (aref string seek
)))
307 ;; Find the end position.
308 (while (and (<= seek length
)
311 (incf wseek
(char-width (aref string seek
)))
314 (substring string wstart
(1- wend
))))
316 (defun gnus-string-width-function ()
318 (gnus-use-correct-string-widths
319 'gnus-correct-length
)
320 ((fboundp 'string-width
)
325 (defun gnus-substring-function ()
327 (gnus-use-correct-string-widths
328 'gnus-correct-substring
)
329 ((fboundp 'string-width
)
330 'gnus-correct-substring
)
334 (defun gnus-tilde-max-form (el max-width
)
335 "Return a form that limits EL to MAX-WIDTH."
336 (let ((max (abs max-width
))
337 (length-fun (gnus-string-width-function))
338 (substring-fun (gnus-substring-function)))
340 `(if (> (,length-fun
,el
) ,max
)
342 `(,substring-fun
,el
(- (,length-fun
,el
) ,max
))
343 `(if (gnus-lrm-string-p ,el
)
344 (concat (,substring-fun
,el
0 ,max
) ,gnus-lrm-string
)
345 (,substring-fun
,el
0 ,max
)))
347 `(let ((val (eval ,el
)))
348 (if (> (,length-fun val
) ,max
)
350 `(,substring-fun val
(- (,length-fun val
) ,max
))
351 `(if (gnus-lrm-string-p val
)
352 (concat (,substring-fun val
0 ,max
) ,gnus-lrm-string
)
353 (,substring-fun val
0 ,max
)))
356 (defun gnus-tilde-cut-form (el cut-width
)
357 "Return a form that cuts CUT-WIDTH off of EL."
358 (let ((cut (abs cut-width
))
359 (length-fun (gnus-string-width-function))
360 (substring-fun (gnus-substring-function)))
362 `(if (> (,length-fun
,el
) ,cut
)
364 `(,substring-fun
,el
0 (- (,length-fun
,el
) ,cut
))
365 `(,substring-fun
,el
,cut
))
367 `(let ((val (eval ,el
)))
368 (if (> (,length-fun val
) ,cut
)
370 `(,substring-fun val
0 (- (,length-fun val
) ,cut
))
371 `(,substring-fun val
,cut
))
374 (defun gnus-tilde-ignore-form (el ignore-value
)
375 "Return a form that is blank when EL is IGNORE-VALUE."
377 `(if (equal ,el
,ignore-value
)
379 `(let ((val (eval ,el
)))
380 (if (equal val
,ignore-value
)
383 (defun gnus-pad-form (el pad-width
)
384 "Return a form that pads EL to PAD-WIDTH accounting for multi-column
385 characters correctly. This is because `format' may pad to columns or to
386 characters when given a pad value."
387 (let ((pad (abs pad-width
))
388 (side (< 0 pad-width
))
389 (length-fun (gnus-string-width-function)))
391 `(let ((need (- ,pad
(,length-fun
,el
))))
393 (concat ,(when side
'(make-string need ?\
))
395 ,(when (not side
) '(make-string need ?\
)))
397 `(let* ((val (eval ,el
))
398 (need (- ,pad
(,length-fun val
))))
400 (concat ,(when side
'(make-string need ?\
))
402 ,(when (not side
) '(make-string need ?\
)))
405 (defun gnus-parse-format (format spec-alist
&optional insert
)
406 ;; This function parses the FORMAT string with the help of the
407 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
408 ;; string. If the FORMAT string contains the specifiers %( and %)
409 ;; the text between them will have the mouse-face text property.
410 ;; If the FORMAT string contains the specifiers %[ and %], the text between
411 ;; them will have the balloon-help text property.
412 (let ((case-fold-search nil
))
414 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'\\|%[-0-9]*=\\|%[-0-9]*\\*"
416 (gnus-parse-complex-format format spec-alist
)
417 ;; This is a simple format.
418 (gnus-parse-simple-format format spec-alist insert
))))
420 (defun gnus-parse-complex-format (format spec-alist
)
421 (let ((cursor-spec nil
))
423 (gnus-set-work-buffer)
425 (goto-char (point-min))
426 (while (re-search-forward "\"" nil t
)
427 (replace-match "\\\"" nil t
))
428 (goto-char (point-min))
430 ;; Convert all font specs into font spec lists.
431 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t
)
432 (let ((number (if (match-beginning 1)
433 (match-string 1) "0"))
434 (delim (aref (match-string 2) 0)))
435 (if (or (= delim ?\
()
438 (replace-match (concat "\"("
439 (cond ((= delim ?\
() "mouse")
440 ((= delim ?\
{) "face")
444 (replace-match "\")\""))))
445 (goto-char (point-max))
447 ;; Convert point position commands.
448 (goto-char (point-min))
449 (let ((case-fold-search nil
))
450 (while (re-search-forward "%\\([-0-9]+\\)?\\*" nil t
)
451 (replace-match "\"(point)\"" t t
)
452 (setq cursor-spec t
)))
453 ;; Convert TAB commands.
454 (goto-char (point-min))
455 (while (re-search-forward "%\\([-0-9]+\\)=" nil t
)
456 (replace-match (format "\"(tab %s)\"" (match-string 1)) t t
))
457 ;; Convert the buffer into the spec.
458 (goto-char (point-min))
459 (let ((form (read (current-buffer))))
461 `(let (gnus-position)
462 ,@(gnus-complex-form-to-spec form spec-alist
)
464 (gnus-put-text-property gnus-position
(1+ gnus-position
)
467 ,@(gnus-complex-form-to-spec form spec-alist
)))))))
469 (defun gnus-complex-form-to-spec (form spec-alist
)
475 (gnus-parse-simple-format sform spec-alist t
))
476 ((eq (car sform
) 'point
)
477 '(setq gnus-position
(point)))
478 ((eq (car sform
) 'tab
)
479 (gnus-spec-tab (cadr sform
)))
481 (funcall (intern (format "gnus-%s-face-function" (car sform
)))
482 (gnus-complex-form-to-spec (cddr sform
) spec-alist
)
487 (defun gnus-xmas-format (fstring &rest args
)
488 "A version of `format' which preserves text properties.
490 Required for XEmacs, where the built in `format' function strips all text
491 properties from both the format string and any inserted strings.
493 Only supports the format sequence %s, and %% for inserting
494 literal % characters. A pad width and an optional - (to right pad)
495 are supported for %s."
496 (let ((re "%%\\|%\\(-\\)?\\([1-9][0-9]*\\)?s")
500 (goto-char (point-min))
501 (while (re-search-forward re nil t
)
502 (goto-char (match-end 0))
504 ((string= (match-string 0) "%%")
508 (error 'wrong-number-of-arguments
#'my-format n fstring
))
509 (let* ((minlen (string-to-number (or (match-string 2) "")))
511 (str (if (stringp arg
) arg
(format "%s" arg
)))
512 (lpad (null (match-string 1)))
513 (padlen (max 0 (- minlen
(length str
)))))
515 (if lpad
(insert-char ?\ padlen
))
517 (unless lpad
(insert-char ?\ padlen
))
518 (setq args
(cdr args
))))))
521 (defun gnus-parse-simple-format (format spec-alist
&optional insert
)
522 ;; This function parses the FORMAT string with the help of the
523 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
526 spec flist fstring elem result dontinsert user-defined
527 type value pad-width spec-beg cut-width ignore-value
528 tilde-form tilde elem-type extended-spec
)
530 (gnus-set-work-buffer)
532 (goto-char (point-min))
533 (while (re-search-forward "%" nil t
)
534 (setq user-defined nil
542 (setq spec-beg
(1- (point)))
544 ;; Parse this spec fully.
547 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
548 (setq pad-width
(string-to-number (match-string 1)))
549 (when (match-beginning 2)
550 (setq max-width
(string-to-number (buffer-substring
551 (1+ (match-beginning 2))
553 (goto-char (match-end 0)))
556 (setq tilde
(read (current-buffer))
560 ((memq type
'(pad pad-left
))
561 (setq pad-width value
))
562 ((eq type
'pad-right
)
563 (setq pad-width
(- value
)))
564 ((memq type
'(max-right max
))
565 (setq max-width value
))
567 (setq max-width
(- value
)))
568 ((memq type
'(cut cut-left
))
569 (setq cut-width value
))
570 ((eq type
'cut-right
)
571 (setq cut-width
(- value
)))
574 (if (stringp value
) value
(format "%s" value
))))
576 (setq tilde-form value
))
578 (error "Unknown tilde type: %s" tilde
)))
583 ;; User-defined spec -- find the spec name.
584 ((eq (setq spec
(char-after)) ?u
)
586 (when (and (eq (setq user-defined
(char-after)) ?
&)
587 (looking-at "&\\([^;]+\\);"))
588 (setq user-defined
(match-string 1))
589 (goto-char (match-end 1))))
591 ((and (eq spec ?
&) (looking-at "&\\([^;]+\\);"))
592 (setq extended-spec
(intern (match-string 1)))
593 (goto-char (match-end 1))))
595 (delete-region spec-beg
(point))
597 ;; Now we have all the relevant data on this spec, so
598 ;; we start doing stuff.
601 ;; "%%" just results in a "%".
606 (setq elem
(list tilde-form ?s
)))
607 ;; Treat user defined format specifiers specially.
611 (list (intern (format
612 (if (stringp user-defined
)
613 "gnus-user-format-function-%s"
614 "gnus-user-format-function-%c")
618 ;; Find the specification from `spec-alist'.
619 ((setq elem
(cdr (assq (or extended-spec spec
) spec-alist
))))
620 ;; We used to use "%l" for displaying the grouplens score.
622 (setq elem
'("" ?s
)))
624 (setq elem
'("*" ?s
))))
625 (setq elem-type
(cadr elem
))
626 ;; Insert the new format elements.
628 (not (and (featurep 'xemacs
)
629 gnus-use-correct-string-widths
)))
630 (insert (number-to-string pad-width
)))
631 ;; Create the form to be evalled.
632 (if (or max-width cut-width ignore-value
633 (and (featurep 'xemacs
)
634 gnus-use-correct-string-widths
))
637 (let ((el (car elem
)))
638 (cond ((= (cadr elem
) ?c
)
639 (setq el
(list 'char-to-string el
)))
641 (setq el
(list 'int-to-string el
))))
643 (setq el
(gnus-tilde-ignore-form el ignore-value
)))
645 (setq el
(gnus-tilde-cut-form el cut-width
)))
647 (setq el
(gnus-tilde-max-form el max-width
)))
649 (setq el
(gnus-pad-form el pad-width
)))
652 (push (car elem
) flist
))))
653 (setq fstring
(buffer-substring-no-properties (point-min) (point-max))))
655 ;; Do some postprocessing to increase efficiency.
660 ((string= fstring
"")
662 ;; Not a format string.
663 ((not (string-match "%" fstring
))
665 ;; A format string with just a single string spec.
666 ((string= fstring
"%s")
668 ;; A single character.
669 ((string= fstring
"%c")
672 ((string= fstring
"%d")
675 `(insert (int-to-string ,(car flist
)))
676 (list `(int-to-string ,(car flist
)))))
677 ;; Just lots of chars and strings.
678 ((string-match "\\`\\(%[cs]\\)+\\'" fstring
)
680 ;; A single string spec at the beginning of the spec.
681 ((string-match "\\`%[sc][^%]+\\'" fstring
)
682 (list (car flist
) (substring fstring
2)))
683 ;; A single string spec in the middle of the spec.
684 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring
)
685 (list (match-string 1 fstring
) (car flist
) (match-string 2 fstring
)))
686 ;; A single string spec in the end of the spec.
687 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring
)
688 (list (match-string 1 fstring
) (car flist
)))
689 ;; Only string (and %) specs (XEmacs only!)
690 ((and (featurep 'xemacs
)
691 gnus-make-format-preserve-properties
693 "\\`\\([^%]*\\(%%\\|%-?\\([1-9][0-9]*\\)?s\\)\\)*[^%]*\\'"
695 (list (cons 'gnus-xmas-format
(cons fstring
(nreverse flist
)))))
696 ;; A more complex spec.
698 (list (cons 'format
(cons fstring
(nreverse flist
)))))))
704 (cons 'insert result
)))
705 (cond ((stringp result
)
708 (cons 'concat result
))
711 (defun gnus-eval-format (format &optional alist props
)
712 "Eval the format variable FORMAT, using ALIST.
713 If PROPS, insert the result."
714 (let ((form (gnus-parse-format format alist props
)))
716 (gnus-add-text-properties (point) (progn (eval form
) (point)) props
)
719 (defun gnus-set-format (type &optional insertable
)
720 (set (intern (format "gnus-%s-line-format-spec" type
))
722 (symbol-value (intern (format "gnus-%s-line-format" type
)))
723 (symbol-value (intern (format "gnus-%s-line-format-alist" type
)))
729 ;; coding: iso-8859-1
732 ;;; gnus-spec.el ends here