1 ;;; gnus-spec.el --- format spec functions for Gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Katsumi Yamaoka <yamaoka@jpl.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 2, or (at your option)
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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
29 (eval-when-compile (require 'cl))
33 ;;; Internal variables.
35 (defvar gnus-summary-mark-positions nil)
36 (defvar gnus-group-mark-positions nil)
37 (defvar gnus-group-indentation "")
39 ;; Format specs. The chunks below are the machine-generated forms
40 ;; that are to be evaled as the result of the default format strings.
41 ;; We write them in here to get them byte-compiled. That way the
42 ;; default actions will be quite fast, while still retaining the full
43 ;; flexibility of the user-defined format specs.
45 ;; First we have lots of dummy defvars to let the compiler know these
46 ;; are really dynamic variables.
48 (defvar gnus-tmp-unread)
49 (defvar gnus-tmp-replied)
50 (defvar gnus-tmp-score-char)
51 (defvar gnus-tmp-indentation)
52 (defvar gnus-tmp-opening-bracket)
53 (defvar gnus-tmp-lines)
54 (defvar gnus-tmp-name)
55 (defvar gnus-tmp-closing-bracket)
56 (defvar gnus-tmp-subject-or-nil)
57 (defvar gnus-tmp-subject)
58 (defvar gnus-tmp-marked)
59 (defvar gnus-tmp-marked-mark)
60 (defvar gnus-tmp-subscribed)
61 (defvar gnus-tmp-process-marked)
62 (defvar gnus-tmp-number-of-unread)
63 (defvar gnus-tmp-group-name)
64 (defvar gnus-tmp-group)
65 (defvar gnus-tmp-article-number)
66 (defvar gnus-tmp-unread-and-unselected)
67 (defvar gnus-tmp-news-method)
68 (defvar gnus-tmp-news-server)
69 (defvar gnus-tmp-article-number)
70 (defvar gnus-mouse-face)
71 (defvar gnus-mouse-face-prop)
73 (defun gnus-summary-line-format-spec ()
74 (insert gnus-tmp-unread gnus-tmp-replied
75 gnus-tmp-score-char gnus-tmp-indentation)
76 (gnus-put-text-property
80 gnus-tmp-opening-bracket
83 (if (> (length gnus-tmp-name) 20)
84 (substring gnus-tmp-name 0 20)
86 gnus-tmp-closing-bracket)
88 gnus-mouse-face-prop gnus-mouse-face)
89 (insert " " gnus-tmp-subject-or-nil "\n"))
91 (defvar gnus-summary-line-format-spec
92 (gnus-byte-code 'gnus-summary-line-format-spec))
94 (defun gnus-summary-dummy-line-format-spec ()
96 (gnus-put-text-property
101 gnus-mouse-face-prop gnus-mouse-face)
102 (insert " " gnus-tmp-subject "\n"))
104 (defvar gnus-summary-dummy-line-format-spec
105 (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
107 (defun gnus-group-line-format-spec ()
108 (insert gnus-tmp-marked-mark gnus-tmp-subscribed
109 gnus-tmp-process-marked
110 gnus-group-indentation
111 (format "%5s: " gnus-tmp-number-of-unread))
112 (gnus-put-text-property
115 (insert gnus-tmp-group "\n")
117 gnus-mouse-face-prop gnus-mouse-face))
118 (defvar gnus-group-line-format-spec
119 (gnus-byte-code 'gnus-group-line-format-spec))
121 (defvar gnus-format-specs
122 `((version . ,emacs-version)
123 (group "%M\%S\%p\%P\%5y: %(%g%)%l\n" ,gnus-group-line-format-spec)
124 (summary-dummy "* %(: :%) %S\n"
125 ,gnus-summary-dummy-line-format-spec)
126 (summary "%U\%R\%z\%I\%(%[%4L: %-20,20n%]%) %s\n"
127 ,gnus-summary-line-format-spec))
128 "Alist of format specs.")
130 (defvar gnus-article-mode-line-format-spec nil)
131 (defvar gnus-summary-mode-line-format-spec nil)
132 (defvar gnus-group-mode-line-format-spec nil)
134 ;;; Phew. All that gruft is over, fortunately.
137 (defun gnus-update-format (var)
138 "Update the format specification near point."
143 ;; Find the end of the current word.
144 (re-search-forward "[ \t\n]" nil t)
146 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
148 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
149 (match-string 1 var))))
150 (entry (assq type gnus-format-specs))
153 (setq gnus-format-specs (delq entry gnus-format-specs)))
155 (intern (format "%s-spec" var))
156 (gnus-parse-format (setq value (symbol-value (intern var)))
157 (symbol-value (intern (format "%s-alist" var)))
158 (not (string-match "mode" var))))
159 (setq spec (symbol-value (intern (format "%s-spec" var))))
160 (push (list type value spec) gnus-format-specs)
162 (pop-to-buffer "*Gnus Format*")
164 (lisp-interaction-mode)
165 (insert (pp-to-string spec))))
167 (defun gnus-update-format-specifications (&optional force &rest types)
168 "Update all (necessary) format specifications."
169 ;; Make the indentation array.
170 ;; See whether all the stored info needs to be flushed.
172 (not (equal emacs-version
173 (cdr (assq 'version gnus-format-specs))))
174 (not (equal gnus-version gnus-newsrc-file-version)))
175 (message "%s" "Force update format specs.")
176 (setq gnus-format-specs nil))
178 ;; Go through all the formats and see whether they need updating.
179 (let (new-format entry type val)
180 (while (setq type (pop types))
181 ;; Jump to the proper buffer to find out the value of
182 ;; the variable, if possible. (It may be buffer-local.)
184 (let ((buffer (intern (format "gnus-%s-buffer" type)))
186 (when (and (boundp buffer)
187 (setq val (symbol-value buffer))
188 (gnus-buffer-exists-p val))
190 (setq new-format (symbol-value
191 (intern (format "gnus-%s-line-format" type)))))
192 (setq entry (cdr (assq type gnus-format-specs)))
194 (equal (car entry) new-format))
195 ;; Use the old format.
196 (set (intern (format "gnus-%s-line-format-spec" type))
198 ;; This is a new format.
200 (if (not (stringp new-format))
201 ;; This is a function call or something.
203 ;; This is a "real" format.
207 (intern (format "gnus-%s-line-format-alist" type)))
208 (not (string-match "mode$" (symbol-name type))))))
209 ;; Enter the new format spec into the list.
212 (setcar (cdr entry) val)
213 (setcar entry new-format))
214 (push (list type new-format val) gnus-format-specs))
215 (set (intern (format "gnus-%s-line-format-spec" type)) val)))))
217 (unless (assq 'version gnus-format-specs)
218 (push (cons 'version emacs-version) gnus-format-specs)))
220 (defvar gnus-mouse-face-0 'highlight)
221 (defvar gnus-mouse-face-1 'highlight)
222 (defvar gnus-mouse-face-2 'highlight)
223 (defvar gnus-mouse-face-3 'highlight)
224 (defvar gnus-mouse-face-4 'highlight)
226 (defun gnus-mouse-face-function (form type)
227 `(gnus-put-text-property
228 (point) (progn ,@form (point))
232 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
234 (defvar gnus-face-0 'bold)
235 (defvar gnus-face-1 'italic)
236 (defvar gnus-face-2 'bold-italic)
237 (defvar gnus-face-3 'bold)
238 (defvar gnus-face-4 'bold)
240 (defun gnus-face-face-function (form type)
241 `(gnus-add-text-properties
242 (point) (progn ,@form (point))
243 '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
245 ;;; Avoid byte-compile warning.
246 (defun gnus-tilde-pad-form (el pad-width)
247 "Dummy function except for XEmacs-mule. It will be redefined
248 by `gnus-xmas-redefine'."
249 (let ((val (if (symbolp el) (eval el) el)))
252 (defun gnus-balloon-face-function (form type)
253 `(gnus-put-text-property
254 (point) (progn ,@form (point))
256 ,(intern (format "gnus-balloon-face-%d" type))))
258 (defun gnus-tilde-max-form (el max-width)
259 "Return a form that limits EL to MAX-WIDTH."
260 (let ((max (abs max-width)))
262 `(if (> (length ,el) ,max)
264 `(substring ,el (- (length el) ,max))
265 `(substring ,el 0 ,max))
267 `(let ((val (eval ,el)))
268 (if (> (length val) ,max)
270 `(substring val (- (length val) ,max))
271 `(substring val 0 ,max))
274 (defun gnus-tilde-cut-form (el cut-width)
275 "Return a form that cuts CUT-WIDTH off of EL."
276 (let ((cut (abs cut-width)))
278 `(if (> (length ,el) ,cut)
280 `(substring ,el 0 (- (length el) ,cut))
281 `(substring ,el ,cut))
283 `(let ((val (eval ,el)))
284 (if (> (length val) ,cut)
286 `(substring val 0 (- (length val) ,cut))
287 `(substring val ,cut))
290 (defun gnus-tilde-ignore-form (el ignore-value)
291 "Return a form that is blank when EL is IGNORE-VALUE."
293 `(if (equal ,el ,ignore-value)
295 `(let ((val (eval ,el)))
296 (if (equal val ,ignore-value)
299 (defun gnus-parse-format (format spec-alist &optional insert)
300 ;; This function parses the FORMAT string with the help of the
301 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
302 ;; string. If the FORMAT string contains the specifiers %( and %)
303 ;; the text between them will have the mouse-face text property.
304 ;; If the FORMAT string contains the specifiers %[ and %], the text between
305 ;; them will have the balloon-help text property.
307 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'"
309 (gnus-parse-complex-format format spec-alist)
310 ;; This is a simple format.
311 (gnus-parse-simple-format format spec-alist insert)))
313 (defun gnus-parse-complex-format (format spec-alist)
315 (gnus-set-work-buffer)
317 (goto-char (point-min))
318 (while (re-search-forward "\"" nil t)
319 (replace-match "\\\"" nil t))
320 (goto-char (point-min))
322 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
323 (let ((number (if (match-beginning 1)
324 (match-string 1) "0"))
325 (delim (aref (match-string 2) 0)))
326 (if (or (= delim ?\()
329 (replace-match (concat "\"("
330 (cond ((= delim ?\() "mouse")
331 ((= delim ?\{) "face")
334 (replace-match "\")\""))))
335 (goto-char (point-max))
337 (goto-char (point-min))
338 (let ((form (read (current-buffer))))
339 (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
341 (defun gnus-complex-form-to-spec (form spec-alist)
346 (gnus-parse-simple-format sform spec-alist t)
347 (funcall (intern (format "gnus-%s-face-function" (car sform)))
348 (gnus-complex-form-to-spec (cddr sform) spec-alist)
352 (defun gnus-parse-simple-format (format spec-alist &optional insert)
353 ;; This function parses the FORMAT string with the help of the
354 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
356 (let ((xemacs-mule-p (and gnus-xemacs (featurep 'mule)))
358 spec flist fstring elem result dontinsert user-defined
359 type value pad-width spec-beg cut-width ignore-value
360 tilde-form tilde elem-type)
362 (gnus-set-work-buffer)
364 (goto-char (point-min))
365 (while (search-forward "%" nil t)
366 (setq user-defined nil
373 (setq spec-beg (1- (point)))
375 ;; Parse this spec fully.
378 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
379 (setq pad-width (string-to-number (match-string 1)))
380 (when (match-beginning 2)
381 (setq max-width (string-to-number (buffer-substring
382 (1+ (match-beginning 2))
384 (goto-char (match-end 0)))
387 (setq tilde (read (current-buffer))
391 ((memq type '(pad pad-left))
392 (setq pad-width value))
393 ((eq type 'pad-right)
394 (setq pad-width (- value)))
395 ((memq type '(max-right max))
396 (setq max-width value))
398 (setq max-width (- value)))
399 ((memq type '(cut cut-left))
400 (setq cut-width value))
401 ((eq type 'cut-right)
402 (setq cut-width (- value)))
405 (if (stringp value) value (format "%s" value))))
407 (setq tilde-form value))
409 (error "Unknown tilde type: %s" tilde)))
413 ;; User-defined spec -- find the spec name.
414 (when (eq (setq spec (char-after)) ?u)
416 (setq user-defined (char-after)))
418 (delete-region spec-beg (point))
420 ;; Now we have all the relevant data on this spec, so
421 ;; we start doing stuff.
424 ;; "%%" just results in a "%".
429 (setq elem (list tilde-form ?s)))
430 ;; Treat user defined format specifiers specially.
434 (list (intern (format "gnus-user-format-function-%c"
438 ;; Find the specification from `spec-alist'.
439 ((setq elem (cdr (assq spec spec-alist))))
441 (setq elem '("*" ?s))))
442 (setq elem-type (cadr elem))
443 ;; Insert the new format elements.
444 (and pad-width (not xemacs-mule-p)
445 (insert (number-to-string pad-width)))
446 ;; Create the form to be evaled.
447 (if (or max-width cut-width ignore-value
448 (and pad-width xemacs-mule-p))
451 (let ((el (car elem)))
452 (cond ((= (cadr elem) ?c)
453 (setq el (list 'char-to-string el)))
455 (setq el (list 'int-to-string el))))
457 (setq el (gnus-tilde-ignore-form el ignore-value)))
459 (setq el (gnus-tilde-cut-form el cut-width)))
461 (setq el (gnus-tilde-max-form el max-width)))
462 (and pad-width xemacs-mule-p
463 (setq el (gnus-tilde-pad-form el pad-width)))
466 (push (car elem) flist))))
467 (setq fstring (buffer-string)))
469 ;; Do some postprocessing to increase efficiency.
474 ((string= fstring "")
476 ;; Not a format string.
477 ((not (string-match "%" fstring))
479 ;; A format string with just a single string spec.
480 ((string= fstring "%s")
482 ;; A single character.
483 ((string= fstring "%c")
486 ((string= fstring "%d")
489 (list `(princ ,(car flist)))
490 (list `(int-to-string ,(car flist)))))
491 ;; Just lots of chars and strings.
492 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
494 ;; A single string spec at the beginning of the spec.
495 ((string-match "\\`%[sc][^%]+\\'" fstring)
496 (list (car flist) (substring fstring 2)))
497 ;; A single string spec in the middle of the spec.
498 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
499 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
500 ;; A single string spec in the end of the spec.
501 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
502 (list (match-string 1 fstring) (car flist)))
503 ;; A more complex spec.
505 (list (cons 'format (cons fstring (nreverse flist)))))))
511 (cons 'insert result)))
512 (cond ((stringp result)
515 (cons 'concat result))
518 (defun gnus-eval-format (format &optional alist props)
519 "Eval the format variable FORMAT, using ALIST.
520 If PROPS, insert the result."
521 (let ((form (gnus-parse-format format alist props)))
523 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
526 (defun gnus-compile ()
527 "Byte-compile the user-defined format specs."
530 (let ((entries gnus-format-specs)
531 (byte-compile-warnings '(unresolved callargs redefine))
534 (gnus-message 7 "Compiling format specs...")
537 (setq entry (pop entries))
538 (if (memq (car entry) '(version gnus-version))
539 (setq gnus-format-specs (delq entry gnus-format-specs))
540 (let ((form (caddr entry)))
541 (when (and (listp form)
542 ;; Under GNU Emacs, it's (byte-code ...)
543 (not (eq 'byte-code (car form)))
544 ;; Under XEmacs, it's (funcall #<compiled-function ...>)
545 (not (and (eq 'funcall (car form))
546 (byte-code-function-p (cadr form)))))
547 (fset 'gnus-tmp-func `(lambda () ,form))
548 (byte-compile 'gnus-tmp-func)
549 (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func))))))
551 (push (cons 'version emacs-version) gnus-format-specs)
552 ;; Mark the .newsrc.eld file as "dirty".
554 (gnus-message 7 "Compiling user specs...done"))))
556 (defun gnus-set-format (type &optional insertable)
557 (set (intern (format "gnus-%s-line-format-spec" type))
559 (symbol-value (intern (format "gnus-%s-line-format" type)))
560 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
568 ;; coding: iso-8859-1
571 ;;; gnus-spec.el ends here