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))
34 ;;; Internal variables.
36 (defvar gnus-summary-mark-positions nil)
37 (defvar gnus-group-mark-positions nil)
38 (defvar gnus-group-indentation "")
40 ;; Format specs. The chunks below are the machine-generated forms
41 ;; that are to be evaled as the result of the default format strings.
42 ;; We write them in here to get them byte-compiled. That way the
43 ;; default actions will be quite fast, while still retaining the full
44 ;; flexibility of the user-defined format specs.
46 ;; First we have lots of dummy defvars to let the compiler know these
47 ;; are really dynamic variables.
49 (defvar gnus-tmp-unread)
50 (defvar gnus-tmp-replied)
51 (defvar gnus-tmp-score-char)
52 (defvar gnus-tmp-indentation)
53 (defvar gnus-tmp-opening-bracket)
54 (defvar gnus-tmp-lines)
55 (defvar gnus-tmp-name)
56 (defvar gnus-tmp-closing-bracket)
57 (defvar gnus-tmp-subject-or-nil)
58 (defvar gnus-tmp-subject)
59 (defvar gnus-tmp-marked)
60 (defvar gnus-tmp-marked-mark)
61 (defvar gnus-tmp-subscribed)
62 (defvar gnus-tmp-process-marked)
63 (defvar gnus-tmp-number-of-unread)
64 (defvar gnus-tmp-group-name)
65 (defvar gnus-tmp-group)
66 (defvar gnus-tmp-article-number)
67 (defvar gnus-tmp-unread-and-unselected)
68 (defvar gnus-tmp-news-method)
69 (defvar gnus-tmp-news-server)
70 (defvar gnus-tmp-article-number)
71 (defvar gnus-mouse-face)
72 (defvar gnus-mouse-face-prop)
74 (defun gnus-summary-line-format-spec ()
75 (insert gnus-tmp-unread gnus-tmp-replied
76 gnus-tmp-score-char gnus-tmp-indentation)
77 (gnus-put-text-property
81 gnus-tmp-opening-bracket
84 (if (> (length gnus-tmp-name) 20)
85 (substring gnus-tmp-name 0 20)
87 gnus-tmp-closing-bracket)
89 gnus-mouse-face-prop gnus-mouse-face)
90 (insert " " gnus-tmp-subject-or-nil "\n"))
92 (defvar gnus-summary-line-format-spec
93 (gnus-byte-code 'gnus-summary-line-format-spec))
95 (defun gnus-summary-dummy-line-format-spec ()
97 (gnus-put-text-property
102 gnus-mouse-face-prop gnus-mouse-face)
103 (insert " " gnus-tmp-subject "\n"))
105 (defvar gnus-summary-dummy-line-format-spec
106 (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
108 (defun gnus-group-line-format-spec ()
109 (insert gnus-tmp-marked-mark gnus-tmp-subscribed
110 gnus-tmp-process-marked
111 gnus-group-indentation
112 (format "%5s: " gnus-tmp-number-of-unread))
113 (gnus-put-text-property
116 (insert gnus-tmp-group "\n")
118 gnus-mouse-face-prop gnus-mouse-face))
119 (defvar gnus-group-line-format-spec
120 (gnus-byte-code 'gnus-group-line-format-spec))
122 (defvar gnus-format-specs
123 `((version . ,emacs-version)
124 (group "%M\%S\%p\%P\%5y: %(%g%)%l\n" ,gnus-group-line-format-spec)
125 (summary-dummy "* %(: :%) %S\n"
126 ,gnus-summary-dummy-line-format-spec)
127 (summary "%U\%R\%z\%I\%(%[%4L: %-20,20n%]%) %s\n"
128 ,gnus-summary-line-format-spec))
129 "Alist of format specs.")
131 (defvar gnus-format-specs-compiled nil
132 "Alist of compiled format specs.
133 Each element should be the form (TYPE . BYTECODE).")
135 (defvar gnus-article-mode-line-format-spec nil)
136 (defvar gnus-summary-mode-line-format-spec nil)
137 (defvar gnus-group-mode-line-format-spec nil)
139 ;;; Phew. All that gruft is over, fortunately.
142 (defun gnus-update-format (var)
143 "Update the format specification near point."
148 ;; Find the end of the current word.
149 (re-search-forward "[ \t\n]" nil t)
151 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
153 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
154 (match-string 1 var))))
155 (entry (assq type gnus-format-specs))
158 (setq gnus-format-specs (delq entry gnus-format-specs)))
160 (intern (format "%s-spec" var))
161 (gnus-parse-format (setq value (symbol-value (intern var)))
162 (symbol-value (intern (format "%s-alist" var)))
163 (not (string-match "mode" var))))
164 (setq spec (symbol-value (intern (format "%s-spec" var))))
165 (push (list type value spec) gnus-format-specs)
167 (pop-to-buffer "*Gnus Format*")
169 (lisp-interaction-mode)
170 (insert (pp-to-string spec))))
172 (defun gnus-update-format-specification-1 (type val &optional new)
173 (if gnus-compile-user-specs
174 (let ((bytecode (if new
176 (cdr (assq type gnus-format-specs-compiled)))))
178 (fset 'gnus-tmp-func `(lambda () ,val))
180 (let (byte-compile-warnings)
181 (byte-compile 'gnus-tmp-func))
182 (setq bytecode (gnus-byte-code 'gnus-tmp-func))
183 (when (get-buffer "*Compile-Log*")
184 (bury-buffer "*Compile-Log*"))
185 (when (get-buffer "*Compile-Log-Show*")
186 (bury-buffer "*Compile-Log-Show*")))
187 (set (intern (format "gnus-%s-line-format-spec" type)) bytecode)
188 (set-alist 'gnus-format-specs-compiled type bytecode))
189 (set (intern (format "gnus-%s-line-format-spec" type)) val)))
191 (defun gnus-update-format-specifications (&optional force &rest types)
192 "Update all (necessary) format specifications."
193 ;; Make the indentation array.
194 ;; See whether all the stored info needs to be flushed.
196 (not (equal emacs-version
197 (cdr (assq 'version gnus-format-specs))))
198 (not (equal gnus-version gnus-newsrc-file-version)))
199 (message "%s" "Force update format specs.")
200 (setq gnus-format-specs nil
201 gnus-newsrc-file-version gnus-version))
203 ;; Go through all the formats and see whether they need updating.
204 (let (new-format entry type val)
205 (while (setq type (pop types))
206 ;; Jump to the proper buffer to find out the value of
207 ;; the variable, if possible. (It may be buffer-local.)
209 (let ((buffer (intern (format "gnus-%s-buffer" type)))
211 (when (and (boundp buffer)
212 (setq val (symbol-value buffer))
213 (gnus-buffer-exists-p val))
215 (setq new-format (symbol-value
216 (intern (format "gnus-%s-line-format" type)))))
217 (setq entry (cdr (assq type gnus-format-specs)))
219 (equal (car entry) new-format))
220 ;; Use the old format.
221 (gnus-update-format-specification-1 type (cadr entry))
222 ;; This is a new format.
224 (if (not (stringp new-format))
225 ;; This is a function call or something.
227 ;; This is a "real" format.
231 (intern (format "gnus-%s-line-format-alist" type)))
232 (not (string-match "mode$" (symbol-name type))))))
233 ;; Enter the new format spec into the list.
236 (setcar (cdr entry) val)
237 (setcar entry new-format))
238 (push (list type new-format val) gnus-format-specs))
239 (gnus-update-format-specification-1 type val 'new)))))
241 (unless (assq 'version gnus-format-specs)
242 (push (cons 'version emacs-version) gnus-format-specs)))
244 (defvar gnus-mouse-face-0 'highlight)
245 (defvar gnus-mouse-face-1 'highlight)
246 (defvar gnus-mouse-face-2 'highlight)
247 (defvar gnus-mouse-face-3 'highlight)
248 (defvar gnus-mouse-face-4 'highlight)
250 (defun gnus-mouse-face-function (form type)
251 `(gnus-put-text-property
252 (point) (progn ,@form (point))
256 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
258 (defvar gnus-face-0 'bold)
259 (defvar gnus-face-1 'italic)
260 (defvar gnus-face-2 'bold-italic)
261 (defvar gnus-face-3 'bold)
262 (defvar gnus-face-4 'bold)
264 (defun gnus-face-face-function (form type)
265 `(gnus-add-text-properties
266 (point) (progn ,@form (point))
267 '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
269 ;;; Avoid byte-compile warning.
270 (defun gnus-tilde-pad-form (el pad-width)
271 "Dummy function except for XEmacs-mule. It will be redefined
272 by `gnus-xmas-redefine'."
273 (let ((val (if (symbolp el) (eval el) el)))
276 (defun gnus-balloon-face-function (form type)
277 `(gnus-put-text-property
278 (point) (progn ,@form (point))
280 ,(intern (format "gnus-balloon-face-%d" type))))
282 (defun gnus-tilde-max-form (el max-width)
283 "Return a form that limits EL to MAX-WIDTH."
284 (let ((max (abs max-width)))
286 `(if (> (length ,el) ,max)
288 `(substring ,el (- (length el) ,max))
289 `(substring ,el 0 ,max))
291 `(let ((val (eval ,el)))
292 (if (> (length val) ,max)
294 `(substring val (- (length val) ,max))
295 `(substring val 0 ,max))
298 (defun gnus-tilde-cut-form (el cut-width)
299 "Return a form that cuts CUT-WIDTH off of EL."
300 (let ((cut (abs cut-width)))
302 `(if (> (length ,el) ,cut)
304 `(substring ,el 0 (- (length el) ,cut))
305 `(substring ,el ,cut))
307 `(let ((val (eval ,el)))
308 (if (> (length val) ,cut)
310 `(substring val 0 (- (length val) ,cut))
311 `(substring val ,cut))
314 (defun gnus-tilde-ignore-form (el ignore-value)
315 "Return a form that is blank when EL is IGNORE-VALUE."
317 `(if (equal ,el ,ignore-value)
319 `(let ((val (eval ,el)))
320 (if (equal val ,ignore-value)
323 (defun gnus-parse-format (format spec-alist &optional insert)
324 ;; This function parses the FORMAT string with the help of the
325 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
326 ;; string. If the FORMAT string contains the specifiers %( and %)
327 ;; the text between them will have the mouse-face text property.
328 ;; If the FORMAT string contains the specifiers %[ and %], the text between
329 ;; them will have the balloon-help text property.
331 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'"
333 (gnus-parse-complex-format format spec-alist)
334 ;; This is a simple format.
335 (gnus-parse-simple-format format spec-alist insert)))
337 (defun gnus-parse-complex-format (format spec-alist)
339 (gnus-set-work-buffer)
341 (goto-char (point-min))
342 (while (re-search-forward "\"" nil t)
343 (replace-match "\\\"" nil t))
344 (goto-char (point-min))
346 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
347 (let ((number (if (match-beginning 1)
348 (match-string 1) "0"))
349 (delim (aref (match-string 2) 0)))
350 (if (or (= delim ?\()
353 (replace-match (concat "\"("
354 (cond ((= delim ?\() "mouse")
355 ((= delim ?\{) "face")
358 (replace-match "\")\""))))
359 (goto-char (point-max))
361 (goto-char (point-min))
362 (let ((form (read (current-buffer))))
363 (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
365 (defun gnus-complex-form-to-spec (form spec-alist)
370 (gnus-parse-simple-format sform spec-alist t)
371 (funcall (intern (format "gnus-%s-face-function" (car sform)))
372 (gnus-complex-form-to-spec (cddr sform) spec-alist)
376 (defun gnus-parse-simple-format (format spec-alist &optional insert)
377 ;; This function parses the FORMAT string with the help of the
378 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
380 (let ((xemacs-mule-p (and gnus-xemacs (featurep 'mule)))
382 spec flist fstring elem result dontinsert user-defined
383 type value pad-width spec-beg cut-width ignore-value
384 tilde-form tilde elem-type)
386 (gnus-set-work-buffer)
388 (goto-char (point-min))
389 (while (search-forward "%" nil t)
390 (setq user-defined nil
397 (setq spec-beg (1- (point)))
399 ;; Parse this spec fully.
402 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
403 (setq pad-width (string-to-number (match-string 1)))
404 (when (match-beginning 2)
405 (setq max-width (string-to-number (buffer-substring
406 (1+ (match-beginning 2))
408 (goto-char (match-end 0)))
411 (setq tilde (read (current-buffer))
415 ((memq type '(pad pad-left))
416 (setq pad-width value))
417 ((eq type 'pad-right)
418 (setq pad-width (- value)))
419 ((memq type '(max-right max))
420 (setq max-width value))
422 (setq max-width (- value)))
423 ((memq type '(cut cut-left))
424 (setq cut-width value))
425 ((eq type 'cut-right)
426 (setq cut-width (- value)))
429 (if (stringp value) value (format "%s" value))))
431 (setq tilde-form value))
433 (error "Unknown tilde type: %s" tilde)))
437 ;; User-defined spec -- find the spec name.
438 (when (eq (setq spec (char-after)) ?u)
440 (setq user-defined (char-after)))
442 (delete-region spec-beg (point))
444 ;; Now we have all the relevant data on this spec, so
445 ;; we start doing stuff.
448 ;; "%%" just results in a "%".
453 (setq elem (list tilde-form ?s)))
454 ;; Treat user defined format specifiers specially.
458 (list (intern (format "gnus-user-format-function-%c"
462 ;; Find the specification from `spec-alist'.
463 ((setq elem (cdr (assq spec spec-alist))))
465 (setq elem '("*" ?s))))
466 (setq elem-type (cadr elem))
467 ;; Insert the new format elements.
468 (and pad-width (not xemacs-mule-p)
469 (insert (number-to-string pad-width)))
470 ;; Create the form to be evaled.
471 (if (or max-width cut-width ignore-value
472 (and pad-width xemacs-mule-p))
475 (let ((el (car elem)))
476 (cond ((= (cadr elem) ?c)
477 (setq el (list 'char-to-string el)))
479 (setq el (list 'int-to-string el))))
481 (setq el (gnus-tilde-ignore-form el ignore-value)))
483 (setq el (gnus-tilde-cut-form el cut-width)))
485 (setq el (gnus-tilde-max-form el max-width)))
486 (and pad-width xemacs-mule-p
487 (setq el (gnus-tilde-pad-form el pad-width)))
490 (push (car elem) flist))))
491 (setq fstring (buffer-string)))
493 ;; Do some postprocessing to increase efficiency.
498 ((string= fstring "")
500 ;; Not a format string.
501 ((not (string-match "%" fstring))
503 ;; A format string with just a single string spec.
504 ((string= fstring "%s")
506 ;; A single character.
507 ((string= fstring "%c")
510 ((string= fstring "%d")
513 (list `(princ ,(car flist)))
514 (list `(int-to-string ,(car flist)))))
515 ;; Just lots of chars and strings.
516 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
518 ;; A single string spec at the beginning of the spec.
519 ((string-match "\\`%[sc][^%]+\\'" fstring)
520 (list (car flist) (substring fstring 2)))
521 ;; A single string spec in the middle of the spec.
522 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
523 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
524 ;; A single string spec in the end of the spec.
525 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
526 (list (match-string 1 fstring) (car flist)))
527 ;; A more complex spec.
529 (list (cons 'format (cons fstring (nreverse flist)))))))
535 (cons 'insert result)))
536 (cond ((stringp result)
539 (cons 'concat result))
542 (defun gnus-eval-format (format &optional alist props)
543 "Eval the format variable FORMAT, using ALIST.
544 If PROPS, insert the result."
545 (let ((form (gnus-parse-format format alist props)))
547 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
550 (defun gnus-compile ()
551 "Byte-compile the user-defined format specs."
554 (let ((entries gnus-format-specs)
555 (byte-compile-warnings '(unresolved callargs redefine))
558 (gnus-message 7 "Compiling format specs...")
561 (setq entry (pop entries)
563 (if (memq type '(version gnus-version))
564 (setq gnus-format-specs (delq entry gnus-format-specs))
565 (let ((form (caddr entry)))
566 (when (and (listp form)
567 ;; Under GNU Emacs, it's (byte-code ...)
568 (not (eq 'byte-code (car form)))
569 ;; Under XEmacs, it's (funcall #<compiled-function ...>)
570 (not (and (eq 'funcall (car form))
571 (byte-code-function-p (cadr form)))))
572 (fset 'gnus-tmp-func `(lambda () ,form))
573 (byte-compile 'gnus-tmp-func)
574 (setq bytecode (gnus-byte-code 'gnus-tmp-func))
575 (set (intern (format "gnus-%s-line-format-spec" type)) bytecode)
576 (set-alist 'gnus-format-specs-compiled type bytecode)))))
578 (push (cons 'version emacs-version) gnus-format-specs)
579 (gnus-message 7 "Compiling user specs...done"))))
581 (defun gnus-set-format (type &optional insertable)
582 (set (intern (format "gnus-%s-line-format-spec" type))
584 (symbol-value (intern (format "gnus-%s-line-format" type)))
585 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
593 ;; coding: iso-8859-1
596 ;;; gnus-spec.el ends here