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 `((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-format-specs-compiled nil
131 "Alist of compiled format specs. Each element should be the form:
132 \(TYPE (FORMAT-STRING-1 . COMPILED-FUNCTION-1)
134 (FORMAT-STRING-n . COMPILED-FUNCTION-n)).")
136 (defvar gnus-article-mode-line-format-spec nil)
137 (defvar gnus-summary-mode-line-format-spec nil)
138 (defvar gnus-group-mode-line-format-spec nil)
140 ;;; Phew. All that gruft is over, fortunately.
143 (defun gnus-update-format (var)
144 "Update the format specification near point."
149 ;; Find the end of the current word.
150 (re-search-forward "[ \t\n]" nil t)
152 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
154 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
155 (match-string 1 var))))
156 (value (symbol-value (intern var)))
158 (intern (format "%s-spec" var))
160 value (symbol-value (intern (format "%s-alist" var)))
161 (not (string-match "mode" var)))))
162 (entry (assq type gnus-format-specs)))
164 (let ((elem (assoc value entry)))
167 (setcdr entry (cons (cons value elem) (cdr entry)))))
168 (push (list type (cons value spec)) gnus-format-specs))
169 (gnus-product-variable-touch 'gnus-format-specs)
171 (pop-to-buffer "*Gnus Format*")
173 (lisp-interaction-mode)
174 (insert (pp-to-string spec))))
176 (put 'gnus-search-or-regist-spec 'lisp-indent-function 1)
177 (defmacro gnus-search-or-regist-spec (mspec &rest body)
178 (let ((specs (nth 0 mspec)) (type (nth 1 mspec)) (format (nth 2 mspec))
179 (spec (nth 3 mspec)) (entry (nth 4 mspec)) (elem (nth 5 mspec)))
180 `(let* ((,entry (assq ,type ,specs))
181 (,elem (assoc ,format (cdr ,entry))))
187 (setcdr ,entry (cons (cons ,format ,spec) (cdr ,entry))))
188 (push (list ,type (cons ,format ,spec)) ,specs))
189 (gnus-product-variable-touch (quote ,specs)))
192 (defun gnus-update-format-specification-1 (type format val)
193 (set (intern (format "gnus-%s-line-format-spec" type))
194 (gnus-search-or-regist-spec (gnus-format-specs-compiled
195 type format val entry elem)
196 (when (and gnus-compile-user-specs val)
199 (fset 'gnus-tmp-func `(lambda () ,val))
201 (let (byte-compile-warnings)
202 (byte-compile 'gnus-tmp-func))
203 (gnus-byte-code 'gnus-tmp-func))
204 (when (get-buffer "*Compile-Log*")
205 (bury-buffer "*Compile-Log*"))
206 (when (get-buffer "*Compile-Log-Show*")
207 (bury-buffer "*Compile-Log-Show*"))))))))
209 (defun gnus-update-format-specifications (&optional force &rest types)
210 "Update all (necessary) format specifications."
211 ;; Make the indentation array.
212 ;; See whether all the stored info needs to be flushed.
214 (message "%s" "Force update format specs.")
215 (setq gnus-format-specs nil
216 gnus-format-specs-compiled nil)
217 (gnus-product-variable-touch 'gnus-format-specs
218 'gnus-format-specs-compiled))
220 ;; Go through all the formats and see whether they need updating.
223 (while (setq type (pop types))
224 ;; Jump to the proper buffer to find out the value of
225 ;; the variable, if possible. (It may be buffer-local.)
227 (let ((buffer (intern (format "gnus-%s-buffer" type))))
228 (when (and (boundp buffer)
229 (setq val (symbol-value buffer))
230 (gnus-buffer-exists-p val))
233 (intern (format "gnus-%s-line-format" type))))))
234 (or (gnus-update-format-specification-1 type new-format nil)
235 ;; This is a new format.
236 (gnus-update-format-specification-1
238 (gnus-search-or-regist-spec (gnus-format-specs
239 type new-format val entry elem)
240 (setq val (if (stringp new-format)
241 ;; This is a "real" format.
245 (intern (format "gnus-%s-line-format-alist"
247 (not (string-match "mode$"
248 (symbol-name type))))
249 ;; This is a function call or something.
252 (defvar gnus-mouse-face-0 'highlight)
253 (defvar gnus-mouse-face-1 'highlight)
254 (defvar gnus-mouse-face-2 'highlight)
255 (defvar gnus-mouse-face-3 'highlight)
256 (defvar gnus-mouse-face-4 'highlight)
258 (defun gnus-mouse-face-function (form type)
259 `(gnus-put-text-property
260 (point) (progn ,@form (point))
264 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
266 (defvar gnus-face-0 'bold)
267 (defvar gnus-face-1 'italic)
268 (defvar gnus-face-2 'bold-italic)
269 (defvar gnus-face-3 'bold)
270 (defvar gnus-face-4 'bold)
272 (defun gnus-face-face-function (form type)
273 `(gnus-add-text-properties
274 (point) (progn ,@form (point))
275 '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
277 ;;; Avoid byte-compile warning.
278 (defun gnus-tilde-pad-form (el pad-width)
279 "Dummy function except for XEmacs-mule. It will be redefined
280 by `gnus-xmas-redefine'."
281 (let ((val (if (symbolp el) (eval el) el)))
284 (defun gnus-balloon-face-function (form type)
285 `(gnus-put-text-property
286 (point) (progn ,@form (point))
288 ,(intern (format "gnus-balloon-face-%d" type))))
290 (defun gnus-tilde-max-form (el max-width)
291 "Return a form that limits EL to MAX-WIDTH."
292 (let ((max (abs max-width)))
294 `(if (> (length ,el) ,max)
296 `(substring ,el (- (length el) ,max))
297 `(substring ,el 0 ,max))
299 `(let ((val (eval ,el)))
300 (if (> (length val) ,max)
302 `(substring val (- (length val) ,max))
303 `(substring val 0 ,max))
306 (defun gnus-tilde-cut-form (el cut-width)
307 "Return a form that cuts CUT-WIDTH off of EL."
308 (let ((cut (abs cut-width)))
310 `(if (> (length ,el) ,cut)
312 `(substring ,el 0 (- (length el) ,cut))
313 `(substring ,el ,cut))
315 `(let ((val (eval ,el)))
316 (if (> (length val) ,cut)
318 `(substring val 0 (- (length val) ,cut))
319 `(substring val ,cut))
322 (defun gnus-tilde-ignore-form (el ignore-value)
323 "Return a form that is blank when EL is IGNORE-VALUE."
325 `(if (equal ,el ,ignore-value)
327 `(let ((val (eval ,el)))
328 (if (equal val ,ignore-value)
331 (defun gnus-parse-format (format spec-alist &optional insert)
332 ;; This function parses the FORMAT string with the help of the
333 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
334 ;; string. If the FORMAT string contains the specifiers %( and %)
335 ;; the text between them will have the mouse-face text property.
336 ;; If the FORMAT string contains the specifiers %[ and %], the text between
337 ;; them will have the balloon-help text property.
339 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'"
341 (gnus-parse-complex-format format spec-alist)
342 ;; This is a simple format.
343 (gnus-parse-simple-format format spec-alist insert)))
345 (defun gnus-parse-complex-format (format spec-alist)
347 (gnus-set-work-buffer)
349 (goto-char (point-min))
350 (while (re-search-forward "\"" nil t)
351 (replace-match "\\\"" nil t))
352 (goto-char (point-min))
354 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
355 (let ((number (if (match-beginning 1)
356 (match-string 1) "0"))
357 (delim (aref (match-string 2) 0)))
358 (if (or (= delim ?\()
361 (replace-match (concat "\"("
362 (cond ((= delim ?\() "mouse")
363 ((= delim ?\{) "face")
366 (replace-match "\")\""))))
367 (goto-char (point-max))
369 (goto-char (point-min))
370 (let ((form (read (current-buffer))))
371 (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
373 (defun gnus-complex-form-to-spec (form spec-alist)
378 (gnus-parse-simple-format sform spec-alist t)
379 (funcall (intern (format "gnus-%s-face-function" (car sform)))
380 (gnus-complex-form-to-spec (cddr sform) spec-alist)
384 (defun gnus-parse-simple-format (format spec-alist &optional insert)
385 ;; This function parses the FORMAT string with the help of the
386 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
388 (let ((xemacs-mule-p (and gnus-xemacs (featurep 'mule)))
390 spec flist fstring elem result dontinsert user-defined
391 type value pad-width spec-beg cut-width ignore-value
392 tilde-form tilde elem-type)
394 (gnus-set-work-buffer)
396 (goto-char (point-min))
397 (while (search-forward "%" nil t)
398 (setq user-defined nil
405 (setq spec-beg (1- (point)))
407 ;; Parse this spec fully.
410 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
411 (setq pad-width (string-to-number (match-string 1)))
412 (when (match-beginning 2)
413 (setq max-width (string-to-number (buffer-substring
414 (1+ (match-beginning 2))
416 (goto-char (match-end 0)))
419 (setq tilde (read (current-buffer))
423 ((memq type '(pad pad-left))
424 (setq pad-width value))
425 ((eq type 'pad-right)
426 (setq pad-width (- value)))
427 ((memq type '(max-right max))
428 (setq max-width value))
430 (setq max-width (- value)))
431 ((memq type '(cut cut-left))
432 (setq cut-width value))
433 ((eq type 'cut-right)
434 (setq cut-width (- value)))
437 (if (stringp value) value (format "%s" value))))
439 (setq tilde-form value))
441 (error "Unknown tilde type: %s" tilde)))
445 ;; User-defined spec -- find the spec name.
446 (when (eq (setq spec (char-after)) ?u)
448 (setq user-defined (char-after)))
450 (delete-region spec-beg (point))
452 ;; Now we have all the relevant data on this spec, so
453 ;; we start doing stuff.
456 ;; "%%" just results in a "%".
461 (setq elem (list tilde-form ?s)))
462 ;; Treat user defined format specifiers specially.
466 (list (intern (format "gnus-user-format-function-%c"
470 ;; Find the specification from `spec-alist'.
471 ((setq elem (cdr (assq spec spec-alist))))
473 (setq elem '("*" ?s))))
474 (setq elem-type (cadr elem))
475 ;; Insert the new format elements.
476 (and pad-width (not xemacs-mule-p)
477 (insert (number-to-string pad-width)))
478 ;; Create the form to be evaled.
479 (if (or max-width cut-width ignore-value
480 (and pad-width xemacs-mule-p))
483 (let ((el (car elem)))
484 (cond ((= (cadr elem) ?c)
485 (setq el (list 'char-to-string el)))
487 (setq el (list 'int-to-string el))))
489 (setq el (gnus-tilde-ignore-form el ignore-value)))
491 (setq el (gnus-tilde-cut-form el cut-width)))
493 (setq el (gnus-tilde-max-form el max-width)))
494 (and pad-width xemacs-mule-p
495 (setq el (gnus-tilde-pad-form el pad-width)))
498 (push (car elem) flist))))
499 (setq fstring (buffer-string)))
501 ;; Do some postprocessing to increase efficiency.
506 ((string= fstring "")
508 ;; Not a format string.
509 ((not (string-match "%" fstring))
511 ;; A format string with just a single string spec.
512 ((string= fstring "%s")
514 ;; A single character.
515 ((string= fstring "%c")
518 ((string= fstring "%d")
521 (list `(princ ,(car flist)))
522 (list `(int-to-string ,(car flist)))))
523 ;; Just lots of chars and strings.
524 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
526 ;; A single string spec at the beginning of the spec.
527 ((string-match "\\`%[sc][^%]+\\'" fstring)
528 (list (car flist) (substring fstring 2)))
529 ;; A single string spec in the middle of the spec.
530 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
531 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
532 ;; A single string spec in the end of the spec.
533 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
534 (list (match-string 1 fstring) (car flist)))
535 ;; A more complex spec.
537 (list (cons 'format (cons fstring (nreverse flist)))))))
543 (cons 'insert result)))
544 (cond ((stringp result)
547 (cons 'concat result))
550 (defun gnus-eval-format (format &optional alist props)
551 "Eval the format variable FORMAT, using ALIST.
552 If PROPS, insert the result."
553 (let ((form (gnus-parse-format format alist props)))
555 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
558 (defun gnus-compile ()
559 "Byte-compile the user-defined format specs."
562 (let ((entries gnus-format-specs)
563 (byte-compile-warnings '(unresolved callargs redefine))
564 entry type compiled-function)
566 (gnus-message 7 "Compiling format specs...")
569 (setq entry (pop entries)
571 (if (memq type '(version gnus-version))
572 (setq gnus-format-specs (delq entry gnus-format-specs))
573 (let ((form (caddr entry)))
574 (when (and (listp form)
575 ;; Under GNU Emacs, it's (byte-code ...)
576 (not (eq 'byte-code (car form)))
577 ;; Under XEmacs, it's (funcall #<compiled-function ...>)
578 (not (and (eq 'funcall (car form))
579 (byte-code-function-p (cadr form)))))
580 (fset 'gnus-tmp-func `(lambda () ,form))
581 (byte-compile 'gnus-tmp-func)
582 (setq compiled-function (gnus-byte-code 'gnus-tmp-func))
583 (set (intern (format "gnus-%s-line-format-spec" type))
585 (let ((elem (cdr (assq type gnus-format-specs-compiled))))
587 (set-alist 'elem (cadr entry) compiled-function)
588 (setq elem (list (cadr entry) compiled-function)))
589 (set-alist 'gnus-format-specs-compiled type elem))))))
591 (push (cons 'version emacs-version) gnus-format-specs)
592 (gnus-message 7 "Compiling user specs...done"))))
594 (defun gnus-set-format (type &optional insertable)
595 (set (intern (format "gnus-%s-line-format-spec" type))
597 (symbol-value (intern (format "gnus-%s-line-format" type)))
598 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
606 ;; coding: iso-8859-1
609 ;;; gnus-spec.el ends here