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-format-specs-compiled nil
131 "Alist of compiled format specs.")
133 (defvar gnus-article-mode-line-format-spec nil)
134 (defvar gnus-summary-mode-line-format-spec nil)
135 (defvar gnus-group-mode-line-format-spec nil)
137 ;;; Phew. All that gruft is over, fortunately.
140 (defun gnus-update-format (var)
141 "Update the format specification near point."
146 ;; Find the end of the current word.
147 (re-search-forward "[ \t\n]" nil t)
149 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
151 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
152 (match-string 1 var))))
153 (entry (assq type gnus-format-specs))
156 (setq gnus-format-specs (delq entry gnus-format-specs)))
158 (intern (format "%s-spec" var))
159 (gnus-parse-format (setq value (symbol-value (intern var)))
160 (symbol-value (intern (format "%s-alist" var)))
161 (not (string-match "mode" var))))
162 (setq spec (symbol-value (intern (format "%s-spec" var))))
163 (push (list type value spec) gnus-format-specs)
165 (pop-to-buffer "*Gnus Format*")
167 (lisp-interaction-mode)
168 (insert (pp-to-string spec))))
170 (defun gnus-update-format-specifications-1 (force types)
171 "Update all (necessary) format specifications."
172 ;; Make the indentation array.
173 ;; See whether all the stored info needs to be flushed.
175 (not (equal emacs-version
176 (cdr (assq 'version gnus-format-specs))))
177 (not (equal gnus-version gnus-newsrc-file-version)))
178 (message "%s" "Force update format specs.")
179 (setq gnus-format-specs nil
180 gnus-newsrc-file-version gnus-version))
182 ;; Go through all the formats and see whether they need updating.
183 (let (new-format entry type val)
184 (while (setq type (pop types))
185 ;; Jump to the proper buffer to find out the value of
186 ;; the variable, if possible. (It may be buffer-local.)
188 (let ((buffer (intern (format "gnus-%s-buffer" type)))
190 (when (and (boundp buffer)
191 (setq val (symbol-value buffer))
192 (gnus-buffer-exists-p val))
194 (setq new-format (symbol-value
195 (intern (format "gnus-%s-line-format" type)))))
196 (setq entry (cdr (assq type gnus-format-specs)))
198 (equal (car entry) new-format))
199 ;; Use the old format.
200 (set (intern (format "gnus-%s-line-format-spec" type))
202 ;; This is a new format.
204 (if (not (stringp new-format))
205 ;; This is a function call or something.
207 ;; This is a "real" format.
211 (intern (format "gnus-%s-line-format-alist" type)))
212 (not (string-match "mode$" (symbol-name type))))))
213 ;; Enter the new format spec into the list.
216 (setcar (cdr entry) val)
217 (setcar entry new-format))
218 (push (list type new-format val) gnus-format-specs))
219 (set (intern (format "gnus-%s-line-format-spec" type)) val)))))
221 (unless (assq 'version gnus-format-specs)
222 (push (cons 'version emacs-version) gnus-format-specs)))
224 (defun gnus-update-format-specifications (&optional force &rest types)
225 "Update all (necessary) format specifications."
226 (if gnus-format-specs-compiled
227 (let ((gnus-format-specs gnus-format-specs-compiled))
228 (gnus-update-format-specifications-1 force types))
229 (gnus-update-format-specifications-1 force types)))
231 (defvar gnus-mouse-face-0 'highlight)
232 (defvar gnus-mouse-face-1 'highlight)
233 (defvar gnus-mouse-face-2 'highlight)
234 (defvar gnus-mouse-face-3 'highlight)
235 (defvar gnus-mouse-face-4 'highlight)
237 (defun gnus-mouse-face-function (form type)
238 `(gnus-put-text-property
239 (point) (progn ,@form (point))
243 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
245 (defvar gnus-face-0 'bold)
246 (defvar gnus-face-1 'italic)
247 (defvar gnus-face-2 'bold-italic)
248 (defvar gnus-face-3 'bold)
249 (defvar gnus-face-4 'bold)
251 (defun gnus-face-face-function (form type)
252 `(gnus-add-text-properties
253 (point) (progn ,@form (point))
254 '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
256 ;;; Avoid byte-compile warning.
257 (defun gnus-tilde-pad-form (el pad-width)
258 "Dummy function except for XEmacs-mule. It will be redefined
259 by `gnus-xmas-redefine'."
260 (let ((val (if (symbolp el) (eval el) el)))
263 (defun gnus-balloon-face-function (form type)
264 `(gnus-put-text-property
265 (point) (progn ,@form (point))
267 ,(intern (format "gnus-balloon-face-%d" type))))
269 (defun gnus-tilde-max-form (el max-width)
270 "Return a form that limits EL to MAX-WIDTH."
271 (let ((max (abs max-width)))
273 `(if (> (length ,el) ,max)
275 `(substring ,el (- (length el) ,max))
276 `(substring ,el 0 ,max))
278 `(let ((val (eval ,el)))
279 (if (> (length val) ,max)
281 `(substring val (- (length val) ,max))
282 `(substring val 0 ,max))
285 (defun gnus-tilde-cut-form (el cut-width)
286 "Return a form that cuts CUT-WIDTH off of EL."
287 (let ((cut (abs cut-width)))
289 `(if (> (length ,el) ,cut)
291 `(substring ,el 0 (- (length el) ,cut))
292 `(substring ,el ,cut))
294 `(let ((val (eval ,el)))
295 (if (> (length val) ,cut)
297 `(substring val 0 (- (length val) ,cut))
298 `(substring val ,cut))
301 (defun gnus-tilde-ignore-form (el ignore-value)
302 "Return a form that is blank when EL is IGNORE-VALUE."
304 `(if (equal ,el ,ignore-value)
306 `(let ((val (eval ,el)))
307 (if (equal val ,ignore-value)
310 (defun gnus-parse-format (format spec-alist &optional insert)
311 ;; This function parses the FORMAT string with the help of the
312 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
313 ;; string. If the FORMAT string contains the specifiers %( and %)
314 ;; the text between them will have the mouse-face text property.
315 ;; If the FORMAT string contains the specifiers %[ and %], the text between
316 ;; them will have the balloon-help text property.
318 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'"
320 (gnus-parse-complex-format format spec-alist)
321 ;; This is a simple format.
322 (gnus-parse-simple-format format spec-alist insert)))
324 (defun gnus-parse-complex-format (format spec-alist)
326 (gnus-set-work-buffer)
328 (goto-char (point-min))
329 (while (re-search-forward "\"" nil t)
330 (replace-match "\\\"" nil t))
331 (goto-char (point-min))
333 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
334 (let ((number (if (match-beginning 1)
335 (match-string 1) "0"))
336 (delim (aref (match-string 2) 0)))
337 (if (or (= delim ?\()
340 (replace-match (concat "\"("
341 (cond ((= delim ?\() "mouse")
342 ((= delim ?\{) "face")
345 (replace-match "\")\""))))
346 (goto-char (point-max))
348 (goto-char (point-min))
349 (let ((form (read (current-buffer))))
350 (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
352 (defun gnus-complex-form-to-spec (form spec-alist)
357 (gnus-parse-simple-format sform spec-alist t)
358 (funcall (intern (format "gnus-%s-face-function" (car sform)))
359 (gnus-complex-form-to-spec (cddr sform) spec-alist)
363 (defun gnus-parse-simple-format (format spec-alist &optional insert)
364 ;; This function parses the FORMAT string with the help of the
365 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
367 (let ((xemacs-mule-p (and gnus-xemacs (featurep 'mule)))
369 spec flist fstring elem result dontinsert user-defined
370 type value pad-width spec-beg cut-width ignore-value
371 tilde-form tilde elem-type)
373 (gnus-set-work-buffer)
375 (goto-char (point-min))
376 (while (search-forward "%" nil t)
377 (setq user-defined nil
384 (setq spec-beg (1- (point)))
386 ;; Parse this spec fully.
389 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
390 (setq pad-width (string-to-number (match-string 1)))
391 (when (match-beginning 2)
392 (setq max-width (string-to-number (buffer-substring
393 (1+ (match-beginning 2))
395 (goto-char (match-end 0)))
398 (setq tilde (read (current-buffer))
402 ((memq type '(pad pad-left))
403 (setq pad-width value))
404 ((eq type 'pad-right)
405 (setq pad-width (- value)))
406 ((memq type '(max-right max))
407 (setq max-width value))
409 (setq max-width (- value)))
410 ((memq type '(cut cut-left))
411 (setq cut-width value))
412 ((eq type 'cut-right)
413 (setq cut-width (- value)))
416 (if (stringp value) value (format "%s" value))))
418 (setq tilde-form value))
420 (error "Unknown tilde type: %s" tilde)))
424 ;; User-defined spec -- find the spec name.
425 (when (eq (setq spec (char-after)) ?u)
427 (setq user-defined (char-after)))
429 (delete-region spec-beg (point))
431 ;; Now we have all the relevant data on this spec, so
432 ;; we start doing stuff.
435 ;; "%%" just results in a "%".
440 (setq elem (list tilde-form ?s)))
441 ;; Treat user defined format specifiers specially.
445 (list (intern (format "gnus-user-format-function-%c"
449 ;; Find the specification from `spec-alist'.
450 ((setq elem (cdr (assq spec spec-alist))))
452 (setq elem '("*" ?s))))
453 (setq elem-type (cadr elem))
454 ;; Insert the new format elements.
455 (and pad-width (not xemacs-mule-p)
456 (insert (number-to-string pad-width)))
457 ;; Create the form to be evaled.
458 (if (or max-width cut-width ignore-value
459 (and pad-width xemacs-mule-p))
462 (let ((el (car elem)))
463 (cond ((= (cadr elem) ?c)
464 (setq el (list 'char-to-string el)))
466 (setq el (list 'int-to-string el))))
468 (setq el (gnus-tilde-ignore-form el ignore-value)))
470 (setq el (gnus-tilde-cut-form el cut-width)))
472 (setq el (gnus-tilde-max-form el max-width)))
473 (and pad-width xemacs-mule-p
474 (setq el (gnus-tilde-pad-form el pad-width)))
477 (push (car elem) flist))))
478 (setq fstring (buffer-string)))
480 ;; Do some postprocessing to increase efficiency.
485 ((string= fstring "")
487 ;; Not a format string.
488 ((not (string-match "%" fstring))
490 ;; A format string with just a single string spec.
491 ((string= fstring "%s")
493 ;; A single character.
494 ((string= fstring "%c")
497 ((string= fstring "%d")
500 (list `(princ ,(car flist)))
501 (list `(int-to-string ,(car flist)))))
502 ;; Just lots of chars and strings.
503 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
505 ;; A single string spec at the beginning of the spec.
506 ((string-match "\\`%[sc][^%]+\\'" fstring)
507 (list (car flist) (substring fstring 2)))
508 ;; A single string spec in the middle of the spec.
509 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
510 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
511 ;; A single string spec in the end of the spec.
512 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
513 (list (match-string 1 fstring) (car flist)))
514 ;; A more complex spec.
516 (list (cons 'format (cons fstring (nreverse flist)))))))
522 (cons 'insert result)))
523 (cond ((stringp result)
526 (cons 'concat result))
529 (defun gnus-eval-format (format &optional alist props)
530 "Eval the format variable FORMAT, using ALIST.
531 If PROPS, insert the result."
532 (let ((form (gnus-parse-format format alist props)))
534 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
537 (defun gnus-compile ()
538 "Byte-compile the user-defined format specs."
541 (let ((entries gnus-format-specs)
542 (byte-compile-warnings '(unresolved callargs redefine))
545 (gnus-message 7 "Compiling format specs...")
547 (setq gnus-format-specs-compiled nil)
549 (setq entry (pop entries))
550 (if (memq (car entry) '(version gnus-version))
551 (setq gnus-format-specs (delq entry gnus-format-specs))
552 (let ((form (caddr entry)))
553 (when (and (listp form)
554 ;; Under GNU Emacs, it's (byte-code ...)
555 (not (eq 'byte-code (car form)))
556 ;; Under XEmacs, it's (funcall #<compiled-function ...>)
557 (not (and (eq 'funcall (car form))
558 (byte-code-function-p (cadr form)))))
559 (fset 'gnus-tmp-func `(lambda () ,form))
560 (byte-compile 'gnus-tmp-func)
561 (push (nconc (butlast entry)
562 (list (gnus-byte-code 'gnus-tmp-func)))
563 gnus-format-specs-compiled)))))
565 (push (cons 'version emacs-version) gnus-format-specs)
566 (push (cons 'version emacs-version) gnus-format-specs-compiled)
567 (gnus-message 7 "Compiling user specs...done"))))
569 (defun gnus-set-format (type &optional insertable)
570 (set (intern (format "gnus-%s-line-format-spec" type))
572 (symbol-value (intern (format "gnus-%s-line-format" type)))
573 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
581 ;; coding: iso-8859-1
584 ;;; gnus-spec.el ends here