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
175 (cdr (assq 'gnus-version gnus-format-specs)))))
176 (message "%s" "Force update format specs.")
177 (setq gnus-format-specs nil))
179 ;; Go through all the formats and see whether they need updating.
180 (let (new-format entry type val)
181 (while (setq type (pop types))
182 ;; Jump to the proper buffer to find out the value of
183 ;; the variable, if possible. (It may be buffer-local.)
185 (let ((buffer (intern (format "gnus-%s-buffer" type)))
187 (when (and (boundp buffer)
188 (setq val (symbol-value buffer))
189 (gnus-buffer-exists-p val))
191 (setq new-format (symbol-value
192 (intern (format "gnus-%s-line-format" type)))))
193 (setq entry (cdr (assq type gnus-format-specs)))
195 (equal (car entry) new-format))
196 ;; Use the old format.
197 (set (intern (format "gnus-%s-line-format-spec" type))
199 ;; This is a new format.
201 (if (not (stringp new-format))
202 ;; This is a function call or something.
204 ;; This is a "real" format.
208 (intern (format "gnus-%s-line-format-alist" type)))
209 (not (string-match "mode$" (symbol-name type))))))
210 ;; Enter the new format spec into the list.
213 (setcar (cdr entry) val)
214 (setcar entry new-format))
215 (push (list type new-format val) gnus-format-specs))
216 (set (intern (format "gnus-%s-line-format-spec" type)) val)))))
218 (unless (assq 'version gnus-format-specs)
219 (push (cons 'version emacs-version) gnus-format-specs))
220 (unless (assq 'gnus-version gnus-format-specs)
221 (push (cons 'gnus-version gnus-version) gnus-format-specs)))
223 (defvar gnus-mouse-face-0 'highlight)
224 (defvar gnus-mouse-face-1 'highlight)
225 (defvar gnus-mouse-face-2 'highlight)
226 (defvar gnus-mouse-face-3 'highlight)
227 (defvar gnus-mouse-face-4 'highlight)
229 (defun gnus-mouse-face-function (form type)
230 `(gnus-put-text-property
231 (point) (progn ,@form (point))
235 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
237 (defvar gnus-face-0 'bold)
238 (defvar gnus-face-1 'italic)
239 (defvar gnus-face-2 'bold-italic)
240 (defvar gnus-face-3 'bold)
241 (defvar gnus-face-4 'bold)
243 (defun gnus-face-face-function (form type)
244 `(gnus-add-text-properties
245 (point) (progn ,@form (point))
246 '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
248 ;;; Avoid byte-compile warning.
249 (defun gnus-tilde-pad-form (el pad-width)
250 "Dummy function except for XEmacs-mule. It will be redefined
251 by `gnus-xmas-redefine'."
252 (let ((val (if (symbolp el) (eval el) el)))
255 (defun gnus-balloon-face-function (form type)
256 `(gnus-put-text-property
257 (point) (progn ,@form (point))
259 ,(intern (format "gnus-balloon-face-%d" type))))
261 (defun gnus-tilde-max-form (el max-width)
262 "Return a form that limits EL to MAX-WIDTH."
263 (let ((max (abs max-width)))
265 `(if (> (length ,el) ,max)
267 `(substring ,el (- (length el) ,max))
268 `(substring ,el 0 ,max))
270 `(let ((val (eval ,el)))
271 (if (> (length val) ,max)
273 `(substring val (- (length val) ,max))
274 `(substring val 0 ,max))
277 (defun gnus-tilde-cut-form (el cut-width)
278 "Return a form that cuts CUT-WIDTH off of EL."
279 (let ((cut (abs cut-width)))
281 `(if (> (length ,el) ,cut)
283 `(substring ,el 0 (- (length el) ,cut))
284 `(substring ,el ,cut))
286 `(let ((val (eval ,el)))
287 (if (> (length val) ,cut)
289 `(substring val 0 (- (length val) ,cut))
290 `(substring val ,cut))
293 (defun gnus-tilde-ignore-form (el ignore-value)
294 "Return a form that is blank when EL is IGNORE-VALUE."
296 `(if (equal ,el ,ignore-value)
298 `(let ((val (eval ,el)))
299 (if (equal val ,ignore-value)
302 (defun gnus-parse-format (format spec-alist &optional insert)
303 ;; This function parses the FORMAT string with the help of the
304 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
305 ;; string. If the FORMAT string contains the specifiers %( and %)
306 ;; the text between them will have the mouse-face text property.
307 ;; If the FORMAT string contains the specifiers %< and %>, the text between
308 ;; them will have the balloon-help text property.
310 "\\`\\(.*\\)%[0-9]?[{(<]\\(.*\\)%[0-9]?[})>]\\(.*\n?\\)\\'"
312 (gnus-parse-complex-format format spec-alist)
313 ;; This is a simple format.
314 (gnus-parse-simple-format format spec-alist insert)))
316 (defun gnus-parse-complex-format (format spec-alist)
318 (gnus-set-work-buffer)
320 (goto-char (point-min))
321 (while (re-search-forward "\"" nil t)
322 (replace-match "\\\"" nil t))
323 (goto-char (point-min))
325 (while (re-search-forward "%\\([0-9]+\\)?\\([{}()<>]\\)" nil t)
326 (let ((number (if (match-beginning 1)
327 (match-string 1) "0"))
328 (delim (aref (match-string 2) 0)))
329 (if (or (= delim ?\()
332 (replace-match (concat "\"("
333 (cond ((= delim ?\() "mouse")
334 ((= delim ?\{) "face")
337 (replace-match "\")\""))))
338 (goto-char (point-max))
340 (goto-char (point-min))
341 (let ((form (read (current-buffer))))
342 (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
344 (defun gnus-complex-form-to-spec (form spec-alist)
349 (gnus-parse-simple-format sform spec-alist t)
350 (funcall (intern (format "gnus-%s-face-function" (car sform)))
351 (gnus-complex-form-to-spec (cddr sform) spec-alist)
355 (defun gnus-parse-simple-format (format spec-alist &optional insert)
356 ;; This function parses the FORMAT string with the help of the
357 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
359 (let ((xemacs-mule-p (and gnus-xemacs (featurep 'mule)))
361 spec flist fstring elem result dontinsert user-defined
362 type value pad-width spec-beg cut-width ignore-value
363 tilde-form tilde elem-type)
365 (gnus-set-work-buffer)
367 (goto-char (point-min))
368 (while (search-forward "%" nil t)
369 (setq user-defined nil
376 (setq spec-beg (1- (point)))
378 ;; Parse this spec fully.
381 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
382 (setq pad-width (string-to-number (match-string 1)))
383 (when (match-beginning 2)
384 (setq max-width (string-to-number (buffer-substring
385 (1+ (match-beginning 2))
387 (goto-char (match-end 0)))
390 (setq tilde (read (current-buffer))
394 ((memq type '(pad pad-left))
395 (setq pad-width value))
396 ((eq type 'pad-right)
397 (setq pad-width (- value)))
398 ((memq type '(max-right max))
399 (setq max-width value))
401 (setq max-width (- value)))
402 ((memq type '(cut cut-left))
403 (setq cut-width value))
404 ((eq type 'cut-right)
405 (setq cut-width (- value)))
408 (if (stringp value) value (format "%s" value))))
410 (setq tilde-form value))
412 (error "Unknown tilde type: %s" tilde)))
416 ;; User-defined spec -- find the spec name.
417 (when (eq (setq spec (char-after)) ?u)
419 (setq user-defined (char-after)))
421 (delete-region spec-beg (point))
423 ;; Now we have all the relevant data on this spec, so
424 ;; we start doing stuff.
427 ;; "%%" just results in a "%".
432 (setq elem (list tilde-form ?s)))
433 ;; Treat user defined format specifiers specially.
437 (list (intern (format "gnus-user-format-function-%c"
441 ;; Find the specification from `spec-alist'.
442 ((setq elem (cdr (assq spec spec-alist))))
444 (setq elem '("*" ?s))))
445 (setq elem-type (cadr elem))
446 ;; Insert the new format elements.
447 (and pad-width (not xemacs-mule-p)
448 (insert (number-to-string pad-width)))
449 ;; Create the form to be evaled.
450 (if (or max-width cut-width ignore-value
451 (and pad-width xemacs-mule-p))
454 (let ((el (car elem)))
455 (cond ((= (cadr elem) ?c)
456 (setq el (list 'char-to-string el)))
458 (setq el (list 'int-to-string el))))
460 (setq el (gnus-tilde-ignore-form el ignore-value)))
462 (setq el (gnus-tilde-cut-form el cut-width)))
464 (setq el (gnus-tilde-max-form el max-width)))
465 (and pad-width xemacs-mule-p
466 (setq el (gnus-tilde-pad-form el pad-width)))
469 (push (car elem) flist))))
470 (setq fstring (buffer-string)))
472 ;; Do some postprocessing to increase efficiency.
477 ((string= fstring "")
479 ;; Not a format string.
480 ((not (string-match "%" fstring))
482 ;; A format string with just a single string spec.
483 ((string= fstring "%s")
485 ;; A single character.
486 ((string= fstring "%c")
489 ((string= fstring "%d")
492 (list `(princ ,(car flist)))
493 (list `(int-to-string ,(car flist)))))
494 ;; Just lots of chars and strings.
495 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
497 ;; A single string spec at the beginning of the spec.
498 ((string-match "\\`%[sc][^%]+\\'" fstring)
499 (list (car flist) (substring fstring 2)))
500 ;; A single string spec in the middle of the spec.
501 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
502 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
503 ;; A single string spec in the end of the spec.
504 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
505 (list (match-string 1 fstring) (car flist)))
506 ;; A more complex spec.
508 (list (cons 'format (cons fstring (nreverse flist)))))))
514 (cons 'insert result)))
515 (cond ((stringp result)
518 (cons 'concat result))
521 (defun gnus-eval-format (format &optional alist props)
522 "Eval the format variable FORMAT, using ALIST.
523 If PROPS, insert the result."
524 (let ((form (gnus-parse-format format alist props)))
526 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
529 (defun gnus-compile ()
530 "Byte-compile the user-defined format specs."
533 (let ((entries gnus-format-specs)
534 (byte-compile-warnings '(unresolved callargs redefine))
537 (gnus-message 7 "Compiling format specs...")
540 (setq entry (pop entries))
541 (if (eq (car entry) 'version)
542 (setq gnus-format-specs (delq entry gnus-format-specs))
543 (let ((form (caddr entry)))
544 (when (and (listp form)
545 ;; Under GNU Emacs, it's (byte-code ...)
546 (not (eq 'byte-code (car form)))
547 ;; Under XEmacs, it's (funcall #<compiled-function ...>)
548 (not (and (eq 'funcall (car form))
549 (byte-code-function-p (cadr form)))))
550 (fset 'gnus-tmp-func `(lambda () ,form))
551 (byte-compile 'gnus-tmp-func)
552 (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func))))))
554 (push (cons 'version emacs-version) gnus-format-specs)
555 ;; Mark the .newsrc.eld file as "dirty".
557 (gnus-message 7 "Compiling user specs...done"))))
559 (defun gnus-set-format (type &optional insertable)
560 (set (intern (format "gnus-%s-line-format-spec" type))
562 (symbol-value (intern (format "gnus-%s-line-format" type)))
563 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
569 ;;; gnus-spec.el ends here