1 ;;; gnus-spec.el --- format spec functions for Gnus
2 ;; Copyright (C) 1996,97 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
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.
30 ;;; Internal variables.
32 (defvar gnus-summary-mark-positions nil
)
33 (defvar gnus-group-mark-positions nil
)
34 (defvar gnus-group-indentation
"")
36 ;; Format specs. The chunks below are the machine-generated forms
37 ;; that are to be evaled as the result of the default format strings.
38 ;; We write them in here to get them byte-compiled. That way the
39 ;; default actions will be quite fast, while still retaining the full
40 ;; flexibility of the user-defined format specs.
42 ;; First we have lots of dummy defvars to let the compiler know these
43 ;; are really dynamic variables.
45 (defvar gnus-tmp-unread
)
46 (defvar gnus-tmp-replied
)
47 (defvar gnus-tmp-score-char
)
48 (defvar gnus-tmp-indentation
)
49 (defvar gnus-tmp-opening-bracket
)
50 (defvar gnus-tmp-lines
)
51 (defvar gnus-tmp-name
)
52 (defvar gnus-tmp-closing-bracket
)
53 (defvar gnus-tmp-subject-or-nil
)
54 (defvar gnus-tmp-subject
)
55 (defvar gnus-tmp-marked
)
56 (defvar gnus-tmp-marked-mark
)
57 (defvar gnus-tmp-subscribed
)
58 (defvar gnus-tmp-process-marked
)
59 (defvar gnus-tmp-number-of-unread
)
60 (defvar gnus-tmp-group-name
)
61 (defvar gnus-tmp-group
)
62 (defvar gnus-tmp-article-number
)
63 (defvar gnus-tmp-unread-and-unselected
)
64 (defvar gnus-tmp-news-method
)
65 (defvar gnus-tmp-news-server
)
66 (defvar gnus-tmp-article-number
)
67 (defvar gnus-mouse-face
)
68 (defvar gnus-mouse-face-prop
)
70 (defun gnus-summary-line-format-spec ()
71 (insert gnus-tmp-unread gnus-tmp-replied
72 gnus-tmp-score-char gnus-tmp-indentation
)
73 (gnus-put-text-property
77 gnus-tmp-opening-bracket
80 (if (> (length gnus-tmp-name
) 20)
81 (substring gnus-tmp-name
0 20)
83 gnus-tmp-closing-bracket
)
85 gnus-mouse-face-prop gnus-mouse-face
)
86 (insert " " gnus-tmp-subject-or-nil
"\n"))
88 (defvar gnus-summary-line-format-spec
89 (gnus-byte-code 'gnus-summary-line-format-spec
))
91 (defun gnus-summary-dummy-line-format-spec ()
93 (gnus-put-text-property
98 gnus-mouse-face-prop gnus-mouse-face
)
99 (insert " " gnus-tmp-subject
"\n"))
101 (defvar gnus-summary-dummy-line-format-spec
102 (gnus-byte-code 'gnus-summary-dummy-line-format-spec
))
104 (defun gnus-group-line-format-spec ()
105 (insert gnus-tmp-marked-mark gnus-tmp-subscribed
106 gnus-tmp-process-marked
107 gnus-group-indentation
108 (format "%5s: " gnus-tmp-number-of-unread
))
109 (gnus-put-text-property
112 (insert gnus-tmp-group
"\n")
114 gnus-mouse-face-prop gnus-mouse-face
))
115 (defvar gnus-group-line-format-spec
116 (gnus-byte-code 'gnus-group-line-format-spec
))
118 (defvar gnus-format-specs
119 `((version .
,emacs-version
)
120 (group "%M\%S\%p\%P\%5y: %(%g%)%l\n" ,gnus-group-line-format-spec
)
121 (summary-dummy "* %(: :%) %S\n"
122 ,gnus-summary-dummy-line-format-spec
)
123 (summary "%U\%R\%z\%I\%(%[%4L: %-20,20n%]%) %s\n"
124 ,gnus-summary-line-format-spec
))
125 "Alist of format specs.")
127 (defvar gnus-article-mode-line-format-spec nil
)
128 (defvar gnus-summary-mode-line-format-spec nil
)
129 (defvar gnus-group-mode-line-format-spec nil
)
131 ;;; Phew. All that gruft is over, fortunately.
134 (defun gnus-update-format (var)
135 "Update the format specification near point."
140 ;; Find the end of the current word.
141 (re-search-forward "[ \t\n]" nil t
)
143 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t
)
145 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var
)
146 (match-string 1 var
))))
147 (entry (assq type gnus-format-specs
))
150 (setq gnus-format-specs
(delq entry gnus-format-specs
)))
152 (intern (format "%s-spec" var
))
153 (gnus-parse-format (setq value
(symbol-value (intern var
)))
154 (symbol-value (intern (format "%s-alist" var
)))
155 (not (string-match "mode" var
))))
156 (setq spec
(symbol-value (intern (format "%s-spec" var
))))
157 (push (list type value spec
) gnus-format-specs
)
159 (pop-to-buffer "*Gnus Format*")
161 (lisp-interaction-mode)
162 (insert (pp-to-string spec
))))
164 (defun gnus-update-format-specifications (&optional force
&rest types
)
165 "Update all (necessary) format specifications."
166 ;; Make the indentation array.
167 ;; See whether all the stored info needs to be flushed.
169 (not (equal emacs-version
170 (cdr (assq 'version gnus-format-specs
)))))
171 (setq gnus-format-specs nil
))
173 ;; Go through all the formats and see whether they need updating.
174 (let (new-format entry type val
)
175 (while (setq type
(pop types
))
176 ;; Jump to the proper buffer to find out the value of
177 ;; the variable, if possible. (It may be buffer-local.)
179 (let ((buffer (intern (format "gnus-%s-buffer" type
)))
181 (when (and (boundp buffer
)
182 (setq val
(symbol-value buffer
))
184 (buffer-name (get-buffer val
)))
185 (set-buffer (get-buffer val
)))
186 (setq new-format
(symbol-value
187 (intern (format "gnus-%s-line-format" type
)))))
188 (setq entry
(cdr (assq type gnus-format-specs
)))
190 (equal (car entry
) new-format
))
191 ;; Use the old format.
192 (set (intern (format "gnus-%s-line-format-spec" type
))
194 ;; This is a new format.
196 (if (not (stringp new-format
))
197 ;; This is a function call or something.
199 ;; This is a "real" format.
203 (intern (format "gnus-%s-line-format-alist"
204 (if (eq type
'article-mode
)
205 'summary-mode type
))))
206 (not (string-match "mode$" (symbol-name type
))))))
207 ;; Enter the new format spec into the list.
210 (setcar (cdr entry
) val
)
211 (setcar entry new-format
))
212 (push (list type new-format val
) gnus-format-specs
))
213 (set (intern (format "gnus-%s-line-format-spec" type
)) val
)))))
215 (unless (assq 'version gnus-format-specs
)
216 (push (cons 'version emacs-version
) gnus-format-specs
)))
218 (defvar gnus-mouse-face-0
'highlight
)
219 (defvar gnus-mouse-face-1
'highlight
)
220 (defvar gnus-mouse-face-2
'highlight
)
221 (defvar gnus-mouse-face-3
'highlight
)
222 (defvar gnus-mouse-face-4
'highlight
)
224 (defun gnus-mouse-face-function (form type
)
225 `(gnus-put-text-property
226 (point) (progn ,@form
(point))
230 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type
)))))))
232 (defvar gnus-face-0
'bold
)
233 (defvar gnus-face-1
'italic
)
234 (defvar gnus-face-2
'bold-italic
)
235 (defvar gnus-face-3
'bold
)
236 (defvar gnus-face-4
'bold
)
238 (defun gnus-face-face-function (form type
)
239 `(gnus-put-text-property
240 (point) (progn ,@form
(point))
241 'face
',(symbol-value (intern (format "gnus-face-%d" type
)))))
243 (defun gnus-tilde-max-form (el max-width
)
244 "Return a form that limits EL to MAX-WIDTH."
245 (let ((max (abs max-width
)))
247 `(if (> (length ,el
) ,max
)
249 `(substring ,el
(- (length el
) ,max
))
250 `(substring ,el
0 ,max
))
252 `(let ((val (eval ,el
)))
253 (if (> (length val
) ,max
)
255 `(substring val
(- (length val
) ,max
))
256 `(substring val
0 ,max
))
259 (defun gnus-tilde-cut-form (el cut-width
)
260 "Return a form that cuts CUT-WIDTH off of EL."
261 (let ((cut (abs cut-width
)))
263 `(if (> (length ,el
) ,cut
)
265 `(substring ,el
0 (- (length el
) ,cut
))
266 `(substring ,el
,cut
))
268 `(let ((val (eval ,el
)))
269 (if (> (length val
) ,cut
)
271 `(substring val
0 (- (length val
) ,cut
))
272 `(substring val
,cut
))
275 (defun gnus-tilde-ignore-form (el ignore-value
)
276 "Return a form that is blank when EL is IGNORE-VALUE."
278 `(if (equal ,el
,ignore-value
)
280 `(let ((val (eval ,el
)))
281 (if (equal val
,ignore-value
)
284 (defun gnus-parse-format (format spec-alist
&optional insert
)
285 ;; This function parses the FORMAT string with the help of the
286 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
287 ;; string. If the FORMAT string contains the specifiers %( and %)
288 ;; the text between them will have the mouse-face text property.
290 "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
292 (gnus-parse-complex-format format spec-alist
)
293 ;; This is a simple format.
294 (gnus-parse-simple-format format spec-alist insert
)))
296 (defun gnus-parse-complex-format (format spec-alist
)
298 (gnus-set-work-buffer)
300 (goto-char (point-min))
301 (while (re-search-forward "\"" nil t
)
302 (replace-match "\\\"" nil t
))
303 (goto-char (point-min))
305 (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t
)
306 (let ((number (if (match-beginning 1)
307 (match-string 1) "0"))
308 (delim (aref (match-string 2) 0)))
309 (if (or (= delim ?\
() (= delim ?\
{))
310 (replace-match (concat "\"(" (if (= delim ?\
() "mouse" "face")
312 (replace-match "\")\""))))
313 (goto-char (point-max))
315 (goto-char (point-min))
316 (let ((form (read (current-buffer))))
317 (cons 'progn
(gnus-complex-form-to-spec form spec-alist
)))))
319 (defun gnus-complex-form-to-spec (form spec-alist
)
324 (gnus-parse-simple-format sform spec-alist t
)
325 (funcall (intern (format "gnus-%s-face-function" (car sform
)))
326 (gnus-complex-form-to-spec (cddr sform
) spec-alist
)
330 (defun gnus-parse-simple-format (format spec-alist
&optional insert
)
331 ;; This function parses the FORMAT string with the help of the
332 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
335 spec flist fstring elem result dontinsert user-defined
336 type value pad-width spec-beg cut-width ignore-value
337 tilde-form tilde elem-type
)
339 (gnus-set-work-buffer)
341 (goto-char (point-min))
342 (while (re-search-forward "%" nil t
)
343 (setq user-defined nil
350 (setq spec-beg
(1- (point)))
352 ;; Parse this spec fully.
355 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
356 (setq pad-width
(string-to-number (match-string 1)))
357 (when (match-beginning 2)
358 (setq max-width
(string-to-number (buffer-substring
359 (1+ (match-beginning 2))
361 (goto-char (match-end 0)))
364 (setq tilde
(read (current-buffer))
368 ((memq type
'(pad pad-left
))
369 (setq pad-width value
))
370 ((eq type
'pad-right
)
371 (setq pad-width
(- value
)))
372 ((memq type
'(max-right max
))
373 (setq max-width value
))
375 (setq max-width
(- value
)))
376 ((memq type
'(cut cut-left
))
377 (setq cut-width value
))
378 ((eq type
'cut-right
)
379 (setq cut-width
(- value
)))
382 (if (stringp value
) value
(format "%s" value
))))
384 (setq tilde-form value
))
386 (error "Unknown tilde type: %s" tilde
)))
390 ;; User-defined spec -- find the spec name.
391 (when (= (setq spec
(following-char)) ?u
)
393 (setq user-defined
(following-char)))
395 (delete-region spec-beg
(point))
397 ;; Now we have all the relevant data on this spec, so
398 ;; we start doing stuff.
401 ;; "%%" just results in a "%".
406 (setq elem
(list tilde-form ?s
)))
407 ;; Treat user defined format specifiers specially.
411 (list (intern (format "gnus-user-format-function-%c"
415 ;; Find the specification from `spec-alist'.
416 ((setq elem
(cdr (assq spec spec-alist
))))
418 (setq elem
'("*" ?s
))))
419 (setq elem-type
(cadr elem
))
420 ;; Insert the new format elements.
422 (insert (number-to-string pad-width
)))
423 ;; Create the form to be evaled.
424 (if (or max-width cut-width ignore-value
)
427 (let ((el (car elem
)))
428 (cond ((= (cadr elem
) ?c
)
429 (setq el
(list 'char-to-string el
)))
431 (setq el
(list 'int-to-string el
))))
433 (setq el
(gnus-tilde-ignore-form el ignore-value
)))
435 (setq el
(gnus-tilde-cut-form el cut-width
)))
437 (setq el
(gnus-tilde-max-form el max-width
)))
440 (push (car elem
) flist
))))
441 (setq fstring
(buffer-string)))
443 ;; Do some postprocessing to increase efficiency.
448 ((string= fstring
"")
450 ;; Not a format string.
451 ((not (string-match "%" fstring
))
453 ;; A format string with just a single string spec.
454 ((string= fstring
"%s")
456 ;; A single character.
457 ((string= fstring
"%c")
460 ((string= fstring
"%d")
463 (list `(princ ,(car flist
)))
464 (list `(int-to-string ,(car flist
)))))
465 ;; Just lots of chars and strings.
466 ((string-match "\\`\\(%[cs]\\)+\\'" fstring
)
468 ;; A single string spec at the beginning of the spec.
469 ((string-match "\\`%[sc][^%]+\\'" fstring
)
470 (list (car flist
) (substring fstring
2)))
471 ;; A single string spec in the middle of the spec.
472 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring
)
473 (list (match-string 1 fstring
) (car flist
) (match-string 2 fstring
)))
474 ;; A single string spec in the end of the spec.
475 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring
)
476 (list (match-string 1 fstring
) (car flist
)))
477 ;; A more complex spec.
479 (list (cons 'format
(cons fstring
(nreverse flist
)))))))
485 (cons 'insert result
)))
486 (cond ((stringp result
)
489 (cons 'concat result
))
492 (defun gnus-eval-format (format &optional alist props
)
493 "Eval the format variable FORMAT, using ALIST.
494 If PROPS, insert the result."
495 (let ((form (gnus-parse-format format alist props
)))
497 (gnus-add-text-properties (point) (progn (eval form
) (point)) props
)
500 (defun gnus-compile ()
501 "Byte-compile the user-defined format specs."
504 (error "Can't compile specs under XEmacs"))
505 (let ((entries gnus-format-specs
)
506 (byte-compile-warnings '(unresolved callargs redefine
))
509 (gnus-message 7 "Compiling format specs...")
512 (setq entry
(pop entries
))
513 (if (eq (car entry
) 'version
)
514 (setq gnus-format-specs
(delq entry gnus-format-specs
))
515 (when (and (listp (caddr entry
))
516 (not (eq 'byte-code
(caaddr entry
))))
517 (fset 'gnus-tmp-func
`(lambda () ,(caddr entry
)))
518 (byte-compile 'gnus-tmp-func
)
519 (setcar (cddr entry
) (gnus-byte-code 'gnus-tmp-func
)))))
521 (push (cons 'version emacs-version
) gnus-format-specs
)
522 ;; Mark the .newsrc.eld file as "dirty".
523 (gnus-dribble-enter " ")
524 (gnus-message 7 "Compiling user specs...done"))))
528 ;;; gnus-spec.el ends here