1 ;;; gnus-spec.el --- format spec functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;; 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 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 (setq gnus-format-specs nil))
176 ;; Go through all the formats and see whether they need updating.
177 (let (new-format entry type val)
178 (while (setq type (pop types))
179 ;; Jump to the proper buffer to find out the value of
180 ;; the variable, if possible. (It may be buffer-local.)
182 (let ((buffer (intern (format "gnus-%s-buffer" type)))
184 (when (and (boundp buffer)
185 (setq val (symbol-value buffer))
186 (gnus-buffer-exists-p val))
188 (setq new-format (symbol-value
189 (intern (format "gnus-%s-line-format" type)))))
190 (setq entry (cdr (assq type gnus-format-specs)))
192 (equal (car entry) new-format))
193 ;; Use the old format.
194 (set (intern (format "gnus-%s-line-format-spec" type))
196 ;; This is a new format.
198 (if (not (stringp new-format))
199 ;; This is a function call or something.
201 ;; This is a "real" format.
205 (intern (format "gnus-%s-line-format-alist" type)))
206 (not (string-match "mode$" (symbol-name type))))))
207 ;; Enter the new format spec into the list.
210 (setcar (cdr entry) val)
211 (setcar entry new-format))
212 (push (list type new-format val) gnus-format-specs))
213 (set (intern (format "gnus-%s-line-format-spec" type)) val)))))
215 (unless (assq 'version gnus-format-specs)
216 (push (cons 'version emacs-version) gnus-format-specs)))
218 (defvar gnus-mouse-face-0 'highlight)
219 (defvar gnus-mouse-face-1 'highlight)
220 (defvar gnus-mouse-face-2 'highlight)
221 (defvar gnus-mouse-face-3 'highlight)
222 (defvar gnus-mouse-face-4 'highlight)
224 (defun gnus-mouse-face-function (form type)
225 `(gnus-put-text-property
226 (point) (progn ,@form (point))
230 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
232 (defvar gnus-face-0 'bold)
233 (defvar gnus-face-1 'italic)
234 (defvar gnus-face-2 'bold-italic)
235 (defvar gnus-face-3 'bold)
236 (defvar gnus-face-4 'bold)
238 (defun gnus-face-face-function (form type)
239 `(gnus-add-text-properties
240 (point) (progn ,@form (point))
241 '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
243 (defun gnus-balloon-face-function (form type)
244 `(gnus-put-text-property
245 (point) (progn ,@form (point))
247 ,(intern (format "gnus-balloon-face-%d" type))))
249 (defun gnus-tilde-max-form (el max-width)
250 "Return a form that limits EL to MAX-WIDTH."
251 (let ((max (abs max-width)))
253 `(if (> (length ,el) ,max)
255 `(substring ,el (- (length el) ,max))
256 `(substring ,el 0 ,max))
258 `(let ((val (eval ,el)))
259 (if (> (length val) ,max)
261 `(substring val (- (length val) ,max))
262 `(substring val 0 ,max))
265 (defun gnus-tilde-cut-form (el cut-width)
266 "Return a form that cuts CUT-WIDTH off of EL."
267 (let ((cut (abs cut-width)))
269 `(if (> (length ,el) ,cut)
271 `(substring ,el 0 (- (length el) ,cut))
272 `(substring ,el ,cut))
274 `(let ((val (eval ,el)))
275 (if (> (length val) ,cut)
277 `(substring val 0 (- (length val) ,cut))
278 `(substring val ,cut))
281 (defun gnus-tilde-ignore-form (el ignore-value)
282 "Return a form that is blank when EL is IGNORE-VALUE."
284 `(if (equal ,el ,ignore-value)
286 `(let ((val (eval ,el)))
287 (if (equal val ,ignore-value)
290 (defun gnus-parse-format (format spec-alist &optional insert)
291 ;; This function parses the FORMAT string with the help of the
292 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
293 ;; string. If the FORMAT string contains the specifiers %( and %)
294 ;; the text between them will have the mouse-face text property.
295 ;; If the FORMAT string contains the specifiers %[ and %], the text between
296 ;; them will have the balloon-help text property.
298 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'"
300 (gnus-parse-complex-format format spec-alist)
301 ;; This is a simple format.
302 (gnus-parse-simple-format format spec-alist insert)))
304 (defun gnus-parse-complex-format (format spec-alist)
306 (gnus-set-work-buffer)
308 (goto-char (point-min))
309 (while (re-search-forward "\"" nil t)
310 (replace-match "\\\"" nil t))
311 (goto-char (point-min))
313 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
314 (let ((number (if (match-beginning 1)
315 (match-string 1) "0"))
316 (delim (aref (match-string 2) 0)))
317 (if (or (= delim ?\()
320 (replace-match (concat "\"("
321 (cond ((= delim ?\() "mouse")
322 ((= delim ?\{) "face")
325 (replace-match "\")\""))))
326 (goto-char (point-max))
328 (goto-char (point-min))
329 (let ((form (read (current-buffer))))
330 (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
332 (defun gnus-complex-form-to-spec (form spec-alist)
337 (gnus-parse-simple-format sform spec-alist t)
338 (funcall (intern (format "gnus-%s-face-function" (car sform)))
339 (gnus-complex-form-to-spec (cddr sform) spec-alist)
343 (defun gnus-parse-simple-format (format spec-alist &optional insert)
344 ;; This function parses the FORMAT string with the help of the
345 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
348 spec flist fstring elem result dontinsert user-defined
349 type value pad-width spec-beg cut-width ignore-value
350 tilde-form tilde elem-type)
352 (gnus-set-work-buffer)
354 (goto-char (point-min))
355 (while (re-search-forward "%" nil t)
356 (setq user-defined nil
363 (setq spec-beg (1- (point)))
365 ;; Parse this spec fully.
368 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
369 (setq pad-width (string-to-number (match-string 1)))
370 (when (match-beginning 2)
371 (setq max-width (string-to-number (buffer-substring
372 (1+ (match-beginning 2))
374 (goto-char (match-end 0)))
377 (setq tilde (read (current-buffer))
381 ((memq type '(pad pad-left))
382 (setq pad-width value))
383 ((eq type 'pad-right)
384 (setq pad-width (- value)))
385 ((memq type '(max-right max))
386 (setq max-width value))
388 (setq max-width (- value)))
389 ((memq type '(cut cut-left))
390 (setq cut-width value))
391 ((eq type 'cut-right)
392 (setq cut-width (- value)))
395 (if (stringp value) value (format "%s" value))))
397 (setq tilde-form value))
399 (error "Unknown tilde type: %s" tilde)))
403 ;; User-defined spec -- find the spec name.
404 (when (eq (setq spec (char-after)) ?u)
406 (setq user-defined (char-after)))
408 (delete-region spec-beg (point))
410 ;; Now we have all the relevant data on this spec, so
411 ;; we start doing stuff.
414 ;; "%%" just results in a "%".
419 (setq elem (list tilde-form ?s)))
420 ;; Treat user defined format specifiers specially.
424 (list (intern (format "gnus-user-format-function-%c"
428 ;; Find the specification from `spec-alist'.
429 ((setq elem (cdr (assq spec spec-alist))))
431 (setq elem '("*" ?s))))
432 (setq elem-type (cadr elem))
433 ;; Insert the new format elements.
435 (insert (number-to-string pad-width)))
436 ;; Create the form to be evaled.
437 (if (or max-width cut-width ignore-value)
440 (let ((el (car elem)))
441 (cond ((= (cadr elem) ?c)
442 (setq el (list 'char-to-string el)))
444 (setq el (list 'int-to-string el))))
446 (setq el (gnus-tilde-ignore-form el ignore-value)))
448 (setq el (gnus-tilde-cut-form el cut-width)))
450 (setq el (gnus-tilde-max-form el max-width)))
453 (push (car elem) flist))))
454 (setq fstring (buffer-string)))
456 ;; Do some postprocessing to increase efficiency.
461 ((string= fstring "")
463 ;; Not a format string.
464 ((not (string-match "%" fstring))
466 ;; A format string with just a single string spec.
467 ((string= fstring "%s")
469 ;; A single character.
470 ((string= fstring "%c")
473 ((string= fstring "%d")
476 (list `(princ ,(car flist)))
477 (list `(int-to-string ,(car flist)))))
478 ;; Just lots of chars and strings.
479 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
481 ;; A single string spec at the beginning of the spec.
482 ((string-match "\\`%[sc][^%]+\\'" fstring)
483 (list (car flist) (substring fstring 2)))
484 ;; A single string spec in the middle of the spec.
485 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
486 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
487 ;; A single string spec in the end of the spec.
488 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
489 (list (match-string 1 fstring) (car flist)))
490 ;; A more complex spec.
492 (list (cons 'format (cons fstring (nreverse flist)))))))
498 (cons 'insert result)))
499 (cond ((stringp result)
502 (cons 'concat result))
505 (defun gnus-eval-format (format &optional alist props)
506 "Eval the format variable FORMAT, using ALIST.
507 If PROPS, insert the result."
508 (let ((form (gnus-parse-format format alist props)))
510 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
513 (defun gnus-compile ()
514 "Byte-compile the user-defined format specs."
517 (let ((entries gnus-format-specs)
518 (byte-compile-warnings '(unresolved callargs redefine))
521 (gnus-message 7 "Compiling format specs...")
524 (setq entry (pop entries))
525 (if (eq (car entry) 'version)
526 (setq gnus-format-specs (delq entry gnus-format-specs))
527 (let ((form (caddr entry)))
528 (when (and (listp form)
529 ;; Under GNU Emacs, it's (byte-code ...)
530 (not (eq 'byte-code (car form)))
531 ;; Under XEmacs, it's (funcall #<compiled-function ...>)
532 (not (and (eq 'funcall (car form))
533 (byte-code-function-p (cadr form)))))
534 (fset 'gnus-tmp-func `(lambda () ,form))
535 (byte-compile 'gnus-tmp-func)
536 (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func))))))
538 (push (cons 'version emacs-version) gnus-format-specs)
539 ;; Mark the .newsrc.eld file as "dirty".
541 (gnus-message 7 "Compiling user specs...done"))))
543 (defun gnus-set-format (type &optional insertable)
544 (set (intern (format "gnus-%s-line-format-spec" type))
546 (symbol-value (intern (format "gnus-%s-line-format" type)))
547 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
553 ;; coding: iso-8859-1
556 ;;; gnus-spec.el ends here