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>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
28 (eval-when-compile (require 'cl))
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 evaled 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-tmp-article-number)
69 (defvar gnus-mouse-face)
70 (defvar gnus-mouse-face-prop)
72 (defun gnus-summary-line-format-spec ()
73 (insert gnus-tmp-unread gnus-tmp-replied
74 gnus-tmp-score-char gnus-tmp-indentation)
75 (gnus-put-text-property
79 gnus-tmp-opening-bracket
82 (if (> (length gnus-tmp-name) 20)
83 (substring gnus-tmp-name 0 20)
85 gnus-tmp-closing-bracket)
87 gnus-mouse-face-prop gnus-mouse-face)
88 (insert " " gnus-tmp-subject-or-nil "\n"))
90 (defvar gnus-summary-line-format-spec
91 (gnus-byte-code 'gnus-summary-line-format-spec))
93 (defun gnus-summary-dummy-line-format-spec ()
95 (gnus-put-text-property
100 gnus-mouse-face-prop gnus-mouse-face)
101 (insert " " gnus-tmp-subject "\n"))
103 (defvar gnus-summary-dummy-line-format-spec
104 (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
106 (defun gnus-group-line-format-spec ()
107 (insert gnus-tmp-marked-mark gnus-tmp-subscribed
108 gnus-tmp-process-marked
109 gnus-group-indentation
110 (format "%5s: " gnus-tmp-number-of-unread))
111 (gnus-put-text-property
114 (insert gnus-tmp-group "\n")
116 gnus-mouse-face-prop gnus-mouse-face))
117 (defvar gnus-group-line-format-spec
118 (gnus-byte-code 'gnus-group-line-format-spec))
120 (defvar gnus-format-specs
121 `((version . ,emacs-version)
122 (group "%M\%S\%p\%P\%5y: %(%g%)%l\n" ,gnus-group-line-format-spec)
123 (summary-dummy "* %(: :%) %S\n"
124 ,gnus-summary-dummy-line-format-spec)
125 (summary "%U\%R\%z\%I\%(%[%4L: %-20,20n%]%) %s\n"
126 ,gnus-summary-line-format-spec))
127 "Alist of format specs.")
129 (defvar gnus-article-mode-line-format-spec nil)
130 (defvar gnus-summary-mode-line-format-spec nil)
131 (defvar gnus-group-mode-line-format-spec nil)
133 ;;; Phew. All that gruft is over, fortunately.
136 (defun gnus-update-format (var)
137 "Update the format specification near point."
142 ;; Find the end of the current word.
143 (re-search-forward "[ \t\n]" nil t)
145 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
147 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
148 (match-string 1 var))))
149 (entry (assq type gnus-format-specs))
152 (setq gnus-format-specs (delq entry gnus-format-specs)))
154 (intern (format "%s-spec" var))
155 (gnus-parse-format (setq value (symbol-value (intern var)))
156 (symbol-value (intern (format "%s-alist" var)))
157 (not (string-match "mode" var))))
158 (setq spec (symbol-value (intern (format "%s-spec" var))))
159 (push (list type value spec) gnus-format-specs)
161 (pop-to-buffer "*Gnus Format*")
163 (lisp-interaction-mode)
164 (insert (pp-to-string spec))))
166 (defun gnus-update-format-specifications (&optional force &rest types)
167 "Update all (necessary) format specifications."
168 ;; Make the indentation array.
169 ;; See whether all the stored info needs to be flushed.
171 (not (equal emacs-version
172 (cdr (assq 'version gnus-format-specs))))
173 (not (equal gnus-version gnus-newsrc-file-version)))
174 (message "%s" "Force update format specs.")
175 (setq gnus-format-specs nil))
177 ;; Go through all the formats and see whether they need updating.
178 (let (new-format entry type val)
179 (while (setq type (pop types))
180 ;; Jump to the proper buffer to find out the value of
181 ;; the variable, if possible. (It may be buffer-local.)
183 (let ((buffer (intern (format "gnus-%s-buffer" type)))
185 (when (and (boundp buffer)
186 (setq val (symbol-value buffer))
187 (gnus-buffer-exists-p val))
189 (setq new-format (symbol-value
190 (intern (format "gnus-%s-line-format" type)))))
191 (setq entry (cdr (assq type gnus-format-specs)))
193 (equal (car entry) new-format))
194 ;; Use the old format.
195 (set (intern (format "gnus-%s-line-format-spec" type))
197 ;; This is a new format.
199 (if (not (stringp new-format))
200 ;; This is a function call or something.
202 ;; This is a "real" format.
206 (intern (format "gnus-%s-line-format-alist" type)))
207 (not (string-match "mode$" (symbol-name type))))))
208 ;; Enter the new format spec into the list.
211 (setcar (cdr entry) val)
212 (setcar entry new-format))
213 (push (list type new-format val) gnus-format-specs))
214 (set (intern (format "gnus-%s-line-format-spec" type)) val)))))
216 (unless (assq 'version gnus-format-specs)
217 (push (cons 'version emacs-version) gnus-format-specs)))
219 (defvar gnus-mouse-face-0 'highlight)
220 (defvar gnus-mouse-face-1 'highlight)
221 (defvar gnus-mouse-face-2 'highlight)
222 (defvar gnus-mouse-face-3 'highlight)
223 (defvar gnus-mouse-face-4 'highlight)
225 (defun gnus-mouse-face-function (form type)
226 `(gnus-put-text-property
227 (point) (progn ,@form (point))
231 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
233 (defvar gnus-face-0 'bold)
234 (defvar gnus-face-1 'italic)
235 (defvar gnus-face-2 'bold-italic)
236 (defvar gnus-face-3 'bold)
237 (defvar gnus-face-4 'bold)
239 (defun gnus-face-face-function (form type)
240 `(gnus-add-text-properties
241 (point) (progn ,@form (point))
242 '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
244 (defun gnus-balloon-face-function (form type)
245 `(gnus-put-text-property
246 (point) (progn ,@form (point))
248 ,(intern (format "gnus-balloon-face-%d" type))))
250 (defun gnus-tilde-max-form (el max-width)
251 "Return a form that limits EL to MAX-WIDTH."
252 (let ((max (abs max-width)))
254 `(if (> (length ,el) ,max)
256 `(substring ,el (- (length el) ,max))
257 `(substring ,el 0 ,max))
259 `(let ((val (eval ,el)))
260 (if (> (length val) ,max)
262 `(substring val (- (length val) ,max))
263 `(substring val 0 ,max))
266 (defun gnus-tilde-cut-form (el cut-width)
267 "Return a form that cuts CUT-WIDTH off of EL."
268 (let ((cut (abs cut-width)))
270 `(if (> (length ,el) ,cut)
272 `(substring ,el 0 (- (length el) ,cut))
273 `(substring ,el ,cut))
275 `(let ((val (eval ,el)))
276 (if (> (length val) ,cut)
278 `(substring val 0 (- (length val) ,cut))
279 `(substring val ,cut))
282 (defun gnus-tilde-ignore-form (el ignore-value)
283 "Return a form that is blank when EL is IGNORE-VALUE."
285 `(if (equal ,el ,ignore-value)
287 `(let ((val (eval ,el)))
288 (if (equal val ,ignore-value)
291 (defun gnus-parse-format (format spec-alist &optional insert)
292 ;; This function parses the FORMAT string with the help of the
293 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
294 ;; string. If the FORMAT string contains the specifiers %( and %)
295 ;; the text between them will have the mouse-face text property.
296 ;; If the FORMAT string contains the specifiers %[ and %], the text between
297 ;; them will have the balloon-help text property.
299 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'"
301 (gnus-parse-complex-format format spec-alist)
302 ;; This is a simple format.
303 (gnus-parse-simple-format format spec-alist insert)))
305 (defun gnus-parse-complex-format (format spec-alist)
307 (gnus-set-work-buffer)
309 (goto-char (point-min))
310 (while (re-search-forward "\"" nil t)
311 (replace-match "\\\"" nil t))
312 (goto-char (point-min))
314 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
315 (let ((number (if (match-beginning 1)
316 (match-string 1) "0"))
317 (delim (aref (match-string 2) 0)))
318 (if (or (= delim ?\()
321 (replace-match (concat "\"("
322 (cond ((= delim ?\() "mouse")
323 ((= delim ?\{) "face")
326 (replace-match "\")\""))))
327 (goto-char (point-max))
329 (goto-char (point-min))
330 (let ((form (read (current-buffer))))
331 (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
333 (defun gnus-complex-form-to-spec (form spec-alist)
338 (gnus-parse-simple-format sform spec-alist t)
339 (funcall (intern (format "gnus-%s-face-function" (car sform)))
340 (gnus-complex-form-to-spec (cddr sform) spec-alist)
344 (defun gnus-parse-simple-format (format spec-alist &optional insert)
345 ;; This function parses the FORMAT string with the help of the
346 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
349 spec flist fstring elem result dontinsert user-defined
350 type value pad-width spec-beg cut-width ignore-value
351 tilde-form tilde elem-type)
353 (gnus-set-work-buffer)
355 (goto-char (point-min))
356 (while (re-search-forward "%" nil t)
357 (setq user-defined nil
364 (setq spec-beg (1- (point)))
366 ;; Parse this spec fully.
369 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
370 (setq pad-width (string-to-number (match-string 1)))
371 (when (match-beginning 2)
372 (setq max-width (string-to-number (buffer-substring
373 (1+ (match-beginning 2))
375 (goto-char (match-end 0)))
378 (setq tilde (read (current-buffer))
382 ((memq type '(pad pad-left))
383 (setq pad-width value))
384 ((eq type 'pad-right)
385 (setq pad-width (- value)))
386 ((memq type '(max-right max))
387 (setq max-width value))
389 (setq max-width (- value)))
390 ((memq type '(cut cut-left))
391 (setq cut-width value))
392 ((eq type 'cut-right)
393 (setq cut-width (- value)))
396 (if (stringp value) value (format "%s" value))))
398 (setq tilde-form value))
400 (error "Unknown tilde type: %s" tilde)))
404 ;; User-defined spec -- find the spec name.
405 (when (eq (setq spec (char-after)) ?u)
407 (setq user-defined (char-after)))
409 (delete-region spec-beg (point))
411 ;; Now we have all the relevant data on this spec, so
412 ;; we start doing stuff.
415 ;; "%%" just results in a "%".
420 (setq elem (list tilde-form ?s)))
421 ;; Treat user defined format specifiers specially.
425 (list 'condition-case 'err
426 (list (intern (format "gnus-user-format-function-%c"
432 "Error occured in `gnus-user-format-function-%c: %%s"
436 ;; Find the specification from `spec-alist'.
437 ((setq elem (cdr (assq spec spec-alist))))
439 (setq elem '("*" ?s))))
440 (setq elem-type (cadr elem))
441 ;; Insert the new format elements.
443 (insert (number-to-string pad-width)))
444 ;; Create the form to be evaled.
445 (if (or max-width cut-width ignore-value)
448 (let ((el (car elem)))
449 (cond ((= (cadr elem) ?c)
450 (setq el (list 'char-to-string el)))
452 (setq el (list 'int-to-string el))))
454 (setq el (gnus-tilde-ignore-form el ignore-value)))
456 (setq el (gnus-tilde-cut-form el cut-width)))
458 (setq el (gnus-tilde-max-form el max-width)))
461 (push (car elem) flist))))
462 (setq fstring (buffer-string)))
464 ;; Do some postprocessing to increase efficiency.
469 ((string= fstring "")
471 ;; Not a format string.
472 ((not (string-match "%" fstring))
474 ;; A format string with just a single string spec.
475 ((string= fstring "%s")
477 ;; A single character.
478 ((string= fstring "%c")
481 ((string= fstring "%d")
484 (list `(princ ,(car flist)))
485 (list `(int-to-string ,(car flist)))))
486 ;; Just lots of chars and strings.
487 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
489 ;; A single string spec at the beginning of the spec.
490 ((string-match "\\`%[sc][^%]+\\'" fstring)
491 (list (car flist) (substring fstring 2)))
492 ;; A single string spec in the middle of the spec.
493 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
494 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
495 ;; A single string spec in the end of the spec.
496 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
497 (list (match-string 1 fstring) (car flist)))
498 ;; A more complex spec.
500 (list (cons 'format (cons fstring (nreverse flist)))))))
506 (cons 'insert result)))
507 (cond ((stringp result)
510 (cons 'concat result))
513 (defun gnus-eval-format (format &optional alist props)
514 "Eval the format variable FORMAT, using ALIST.
515 If PROPS, insert the result."
516 (let ((form (gnus-parse-format format alist props)))
518 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
521 (defun gnus-compile ()
522 "Byte-compile the user-defined format specs."
525 (let ((entries gnus-format-specs)
526 (byte-compile-warnings '(unresolved callargs redefine))
529 (gnus-message 7 "Compiling format specs...")
532 (setq entry (pop entries))
533 (if (memq (car entry) '(version gnus-version))
534 (setq gnus-format-specs (delq entry gnus-format-specs))
535 (let ((form (caddr entry)))
536 (when (and (listp form)
537 ;; Under GNU Emacs, it's (byte-code ...)
538 (not (eq 'byte-code (car form)))
539 ;; Under XEmacs, it's (funcall #<compiled-function ...>)
540 (not (and (eq 'funcall (car form))
541 (byte-code-function-p (cadr form)))))
542 (fset 'gnus-tmp-func `(lambda () ,form))
543 (byte-compile 'gnus-tmp-func)
544 (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func))))))
546 (push (cons 'version emacs-version) gnus-format-specs)
547 ;; Mark the .newsrc.eld file as "dirty".
549 (gnus-message 7 "Compiling user specs...done"))))
551 (defun gnus-set-format (type &optional insertable)
552 (set (intern (format "gnus-%s-line-format-spec" type))
554 (symbol-value (intern (format "gnus-%s-line-format" type)))
555 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
561 ;; coding: iso-8859-1
564 ;;; gnus-spec.el ends here