1 ;;; gnus-spec.el --- format spec functions for Gnus
2 ;; Copyright (C) 1996,97,98 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
174 (cdr (assq 'gnus-version gnus-format-specs)))))
175 (message "%s" "Force update format specs.")
176 (setq gnus-format-specs nil))
178 ;; Go through all the formats and see whether they need updating.
179 (let (new-format entry type val)
180 (while (setq type (pop types))
181 ;; Jump to the proper buffer to find out the value of
182 ;; the variable, if possible. (It may be buffer-local.)
184 (let ((buffer (intern (format "gnus-%s-buffer" type)))
186 (when (and (boundp buffer)
187 (setq val (symbol-value buffer))
188 (gnus-buffer-exists-p val))
190 (setq new-format (symbol-value
191 (intern (format "gnus-%s-line-format" type)))))
192 (setq entry (cdr (assq type gnus-format-specs)))
194 (equal (car entry) new-format))
195 ;; Use the old format.
196 (set (intern (format "gnus-%s-line-format-spec" type))
198 ;; This is a new format.
200 (if (not (stringp new-format))
201 ;; This is a function call or something.
203 ;; This is a "real" format.
207 (intern (format "gnus-%s-line-format-alist"
208 (if (eq type 'article-mode)
209 'summary-mode type))))
210 (not (string-match "mode$" (symbol-name type))))))
211 ;; Enter the new format spec into the list.
214 (setcar (cdr entry) val)
215 (setcar entry new-format))
216 (push (list type new-format val) gnus-format-specs))
217 (set (intern (format "gnus-%s-line-format-spec" type)) val)))))
219 (unless (assq 'version gnus-format-specs)
220 (push (cons 'version emacs-version) gnus-format-specs))
221 (unless (assq 'gnus-version gnus-format-specs)
222 (push (cons 'gnus-version gnus-version) gnus-format-specs)))
224 (defvar gnus-mouse-face-0 'highlight)
225 (defvar gnus-mouse-face-1 'highlight)
226 (defvar gnus-mouse-face-2 'highlight)
227 (defvar gnus-mouse-face-3 'highlight)
228 (defvar gnus-mouse-face-4 'highlight)
230 (defun gnus-mouse-face-function (form type)
231 `(gnus-put-text-property
232 (point) (progn ,@form (point))
236 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
238 (defvar gnus-face-0 'bold)
239 (defvar gnus-face-1 'italic)
240 (defvar gnus-face-2 'bold-italic)
241 (defvar gnus-face-3 'bold)
242 (defvar gnus-face-4 'bold)
244 (defun gnus-face-face-function (form type)
245 `(gnus-add-text-properties
246 (point) (progn ,@form (point))
247 '(gnus-face t face ,(symbol-value (intern (format "gnus-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.
296 "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
298 (gnus-parse-complex-format format spec-alist)
299 ;; This is a simple format.
300 (gnus-parse-simple-format format spec-alist insert)))
302 (defun gnus-parse-complex-format (format spec-alist)
304 (gnus-set-work-buffer)
306 (goto-char (point-min))
307 (while (re-search-forward "\"" nil t)
308 (replace-match "\\\"" nil t))
309 (goto-char (point-min))
311 (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
312 (let ((number (if (match-beginning 1)
313 (match-string 1) "0"))
314 (delim (aref (match-string 2) 0)))
315 (if (or (= delim ?\()
317 (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
319 (replace-match "\")\""))))
320 (goto-char (point-max))
322 (goto-char (point-min))
323 (let ((form (read (current-buffer))))
324 (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
326 (defun gnus-complex-form-to-spec (form spec-alist)
331 (gnus-parse-simple-format sform spec-alist t)
332 (funcall (intern (format "gnus-%s-face-function" (car sform)))
333 (gnus-complex-form-to-spec (cddr sform) spec-alist)
337 (defun gnus-parse-simple-format (format spec-alist &optional insert)
338 ;; This function parses the FORMAT string with the help of the
339 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
342 spec flist fstring elem result dontinsert user-defined
343 type value pad-width spec-beg cut-width ignore-value
344 tilde-form tilde elem-type
345 (xemacs-mule-p (and gnus-xemacs (featurep 'mule))))
347 (gnus-set-work-buffer)
349 (goto-char (point-min))
350 (while (search-forward "%" nil t)
351 (setq user-defined nil
358 (setq spec-beg (1- (point)))
360 ;; Parse this spec fully.
363 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
364 (setq pad-width (string-to-number (match-string 1)))
365 (when (match-beginning 2)
366 (setq max-width (string-to-number (buffer-substring
367 (1+ (match-beginning 2))
369 (goto-char (match-end 0)))
372 (setq tilde (read (current-buffer))
376 ((memq type '(pad pad-left))
377 (setq pad-width value))
378 ((eq type 'pad-right)
379 (setq pad-width (- value)))
380 ((memq type '(max-right max))
381 (setq max-width value))
383 (setq max-width (- value)))
384 ((memq type '(cut cut-left))
385 (setq cut-width value))
386 ((eq type 'cut-right)
387 (setq cut-width (- value)))
390 (if (stringp value) value (format "%s" value))))
392 (setq tilde-form value))
394 (error "Unknown tilde type: %s" tilde)))
398 ;; User-defined spec -- find the spec name.
399 (when (eq (setq spec (char-after)) ?u)
401 (setq user-defined (char-after)))
403 (delete-region spec-beg (point))
405 ;; Now we have all the relevant data on this spec, so
406 ;; we start doing stuff.
409 ;; "%%" just results in a "%".
414 (setq elem (list tilde-form ?s)))
415 ;; Treat user defined format specifiers specially.
419 (list (intern (format "gnus-user-format-function-%c"
423 ;; Find the specification from `spec-alist'.
424 ((setq elem (cdr (assq spec spec-alist))))
426 (setq elem '("*" ?s))))
427 (setq elem-type (cadr elem))
428 ;; Insert the new format elements.
429 (and pad-width (not xemacs-mule-p)
430 (insert (number-to-string pad-width)))
431 ;; Create the form to be evaled.
432 (if (or max-width cut-width ignore-value
433 (and pad-width xemacs-mule-p))
436 (let ((el (car elem)))
437 (cond ((= (cadr elem) ?c)
438 (setq el (list 'char-to-string el)))
440 (setq el (list 'int-to-string el))))
442 (setq el (gnus-tilde-ignore-form el ignore-value)))
444 (setq el (gnus-tilde-cut-form el cut-width)))
446 (setq el (gnus-tilde-max-form el max-width)))
447 (and pad-width xemacs-mule-p
448 (setq el (gnus-tilde-pad-form el pad-width)))
451 (push (car elem) flist))))
452 (setq fstring (buffer-string)))
454 ;; Do some postprocessing to increase efficiency.
459 ((string= fstring "")
461 ;; Not a format string.
462 ((not (string-match "%" fstring))
464 ;; A format string with just a single string spec.
465 ((string= fstring "%s")
467 ;; A single character.
468 ((string= fstring "%c")
471 ((string= fstring "%d")
474 (list `(princ ,(car flist)))
475 (list `(int-to-string ,(car flist)))))
476 ;; Just lots of chars and strings.
477 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
479 ;; A single string spec at the beginning of the spec.
480 ((string-match "\\`%[sc][^%]+\\'" fstring)
481 (list (car flist) (substring fstring 2)))
482 ;; A single string spec in the middle of the spec.
483 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
484 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
485 ;; A single string spec in the end of the spec.
486 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
487 (list (match-string 1 fstring) (car flist)))
488 ;; A more complex spec.
490 (list (cons 'format (cons fstring (nreverse flist)))))))
496 (cons 'insert result)))
497 (cond ((stringp result)
500 (cons 'concat result))
503 (defun gnus-eval-format (format &optional alist props)
504 "Eval the format variable FORMAT, using ALIST.
505 If PROPS, insert the result."
506 (let ((form (gnus-parse-format format alist props)))
508 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
511 (defun gnus-compile ()
512 "Byte-compile the user-defined format specs."
515 (let ((entries gnus-format-specs)
516 (byte-compile-warnings '(unresolved callargs redefine))
519 (gnus-message 7 "Compiling format specs...")
522 (setq entry (pop entries))
523 (if (eq (car entry) 'version)
524 (setq gnus-format-specs (delq entry gnus-format-specs))
525 (let ((form (caddr entry)))
526 (when (and (listp form)
527 ;; Under GNU Emacs, it's (byte-code ...)
528 (not (eq 'byte-code (car form)))
529 ;; Under XEmacs, it's (funcall #<compiled-function ...>)
530 (not (and (eq 'funcall (car form))
531 (compiled-function-p (cadr form)))))
532 (fset 'gnus-tmp-func `(lambda () ,form))
533 (byte-compile 'gnus-tmp-func)
534 (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func))))))
536 (push (cons 'version emacs-version) gnus-format-specs)
537 ;; Mark the .newsrc.eld file as "dirty".
539 (gnus-message 7 "Compiling user specs...done"))))
541 (defun gnus-set-format (type &optional insertable)
542 (set (intern (format "gnus-%s-line-format-spec" type))
544 (symbol-value (intern (format "gnus-%s-line-format" type)))
545 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
551 ;;; gnus-spec.el ends here