Rename `autoselect-window' to `mouse-autoselect-window'.
[emacs.git] / lisp / gnus / gnus-spec.el
blob57c9997d26bc4aba866a13f9e5f384c6e1d20bb4
1 ;;; gnus-spec.el --- format spec functions for Gnus -*- coding: iso-latin-1 -*-
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
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)
13 ;; any later version.
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.
25 ;;; Commentary:
27 ;;; Code:
29 (eval-when-compile (require 'cl))
31 (require 'gnus)
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
77 (point)
78 (progn
79 (insert
80 gnus-tmp-opening-bracket
81 (format "%4d: %-20s"
82 gnus-tmp-lines
83 (if (> (length gnus-tmp-name) 20)
84 (substring gnus-tmp-name 0 20)
85 gnus-tmp-name))
86 gnus-tmp-closing-bracket)
87 (point))
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 ()
95 (insert "* ")
96 (gnus-put-text-property
97 (point)
98 (progn
99 (insert ": :")
100 (point))
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
113 (point)
114 (progn
115 (insert gnus-tmp-group "\n")
116 (1- (point)))
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-article-mode-line-format-spec nil)
131 (defvar gnus-summary-mode-line-format-spec nil)
132 (defvar gnus-group-mode-line-format-spec nil)
134 ;;; Phew. All that gruft is over, fortunately.
136 ;;;###autoload
137 (defun gnus-update-format (var)
138 "Update the format specification near point."
139 (interactive
140 (list
141 (save-excursion
142 (eval-defun nil)
143 ;; Find the end of the current word.
144 (re-search-forward "[ \t\n]" nil t)
145 ;; Search backward.
146 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
147 (match-string 1)))))
148 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
149 (match-string 1 var))))
150 (entry (assq type gnus-format-specs))
151 value spec)
152 (when entry
153 (setq gnus-format-specs (delq entry gnus-format-specs)))
154 (set
155 (intern (format "%s-spec" var))
156 (gnus-parse-format (setq value (symbol-value (intern var)))
157 (symbol-value (intern (format "%s-alist" var)))
158 (not (string-match "mode" var))))
159 (setq spec (symbol-value (intern (format "%s-spec" var))))
160 (push (list type value spec) gnus-format-specs)
162 (pop-to-buffer "*Gnus Format*")
163 (erase-buffer)
164 (lisp-interaction-mode)
165 (insert (pp-to-string spec))))
167 (defun gnus-update-format-specifications (&optional force &rest types)
168 "Update all (necessary) format specifications."
169 ;; Make the indentation array.
170 ;; See whether all the stored info needs to be flushed.
171 (when (or force
172 (not (equal emacs-version
173 (cdr (assq 'version gnus-format-specs)))))
174 (setq gnus-format-specs nil))
176 ;; Go through all the formats and see whether they need updating.
177 (let (new-format entry type val)
178 (while (setq type (pop types))
179 ;; Jump to the proper buffer to find out the value of
180 ;; the variable, if possible. (It may be buffer-local.)
181 (save-excursion
182 (let ((buffer (intern (format "gnus-%s-buffer" type)))
183 val)
184 (when (and (boundp buffer)
185 (setq val (symbol-value buffer))
186 (gnus-buffer-exists-p val))
187 (set-buffer val))
188 (setq new-format (symbol-value
189 (intern (format "gnus-%s-line-format" type)))))
190 (setq entry (cdr (assq type gnus-format-specs)))
191 (if (and (car entry)
192 (equal (car entry) new-format))
193 ;; Use the old format.
194 (set (intern (format "gnus-%s-line-format-spec" type))
195 (cadr entry))
196 ;; This is a new format.
197 (setq val
198 (if (not (stringp new-format))
199 ;; This is a function call or something.
200 new-format
201 ;; This is a "real" format.
202 (gnus-parse-format
203 new-format
204 (symbol-value
205 (intern (format "gnus-%s-line-format-alist" type)))
206 (not (string-match "mode$" (symbol-name type))))))
207 ;; Enter the new format spec into the list.
208 (if entry
209 (progn
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))
227 gnus-mouse-face-prop
228 ,(if (equal type 0)
229 'gnus-mouse-face
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-add-text-properties
240 (point) (progn ,@form (point))
241 '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
243 (defun gnus-balloon-face-function (form type)
244 `(gnus-put-text-property
245 (point) (progn ,@form (point))
246 'balloon-help
247 ,(intern (format "gnus-balloon-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)))
252 (if (symbolp el)
253 `(if (> (length ,el) ,max)
254 ,(if (< max-width 0)
255 `(substring ,el (- (length el) ,max))
256 `(substring ,el 0 ,max))
257 ,el)
258 `(let ((val (eval ,el)))
259 (if (> (length val) ,max)
260 ,(if (< max-width 0)
261 `(substring val (- (length val) ,max))
262 `(substring val 0 ,max))
263 val)))))
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)))
268 (if (symbolp el)
269 `(if (> (length ,el) ,cut)
270 ,(if (< cut-width 0)
271 `(substring ,el 0 (- (length el) ,cut))
272 `(substring ,el ,cut))
273 ,el)
274 `(let ((val (eval ,el)))
275 (if (> (length val) ,cut)
276 ,(if (< cut-width 0)
277 `(substring val 0 (- (length val) ,cut))
278 `(substring val ,cut))
279 val)))))
281 (defun gnus-tilde-ignore-form (el ignore-value)
282 "Return a form that is blank when EL is IGNORE-VALUE."
283 (if (symbolp el)
284 `(if (equal ,el ,ignore-value)
285 "" ,el)
286 `(let ((val (eval ,el)))
287 (if (equal val ,ignore-value)
288 "" val))))
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.
295 ;; If the FORMAT string contains the specifiers %[ and %], the text between
296 ;; them will have the balloon-help text property.
297 (if (string-match
298 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'"
299 format)
300 (gnus-parse-complex-format format spec-alist)
301 ;; This is a simple format.
302 (gnus-parse-simple-format format spec-alist insert)))
304 (defun gnus-parse-complex-format (format spec-alist)
305 (save-excursion
306 (gnus-set-work-buffer)
307 (insert format)
308 (goto-char (point-min))
309 (while (re-search-forward "\"" nil t)
310 (replace-match "\\\"" nil t))
311 (goto-char (point-min))
312 (insert "(\"")
313 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
314 (let ((number (if (match-beginning 1)
315 (match-string 1) "0"))
316 (delim (aref (match-string 2) 0)))
317 (if (or (= delim ?\()
318 (= delim ?\{)
319 (= delim ?\«))
320 (replace-match (concat "\"("
321 (cond ((= delim ?\() "mouse")
322 ((= delim ?\{) "face")
323 (t "balloon"))
324 " " number " \""))
325 (replace-match "\")\""))))
326 (goto-char (point-max))
327 (insert "\")")
328 (goto-char (point-min))
329 (let ((form (read (current-buffer))))
330 (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
332 (defun gnus-complex-form-to-spec (form spec-alist)
333 (delq nil
334 (mapcar
335 (lambda (sform)
336 (if (stringp sform)
337 (gnus-parse-simple-format sform spec-alist t)
338 (funcall (intern (format "gnus-%s-face-function" (car sform)))
339 (gnus-complex-form-to-spec (cddr sform) spec-alist)
340 (nth 1 sform))))
341 form)))
343 (defun gnus-parse-simple-format (format spec-alist &optional insert)
344 ;; This function parses the FORMAT string with the help of the
345 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
346 ;; string.
347 (let ((max-width 0)
348 spec flist fstring elem result dontinsert user-defined
349 type value pad-width spec-beg cut-width ignore-value
350 tilde-form tilde elem-type)
351 (save-excursion
352 (gnus-set-work-buffer)
353 (insert format)
354 (goto-char (point-min))
355 (while (re-search-forward "%" nil t)
356 (setq user-defined nil
357 spec-beg nil
358 pad-width nil
359 max-width nil
360 cut-width nil
361 ignore-value nil
362 tilde-form nil)
363 (setq spec-beg (1- (point)))
365 ;; Parse this spec fully.
366 (while
367 (cond
368 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
369 (setq pad-width (string-to-number (match-string 1)))
370 (when (match-beginning 2)
371 (setq max-width (string-to-number (buffer-substring
372 (1+ (match-beginning 2))
373 (match-end 2)))))
374 (goto-char (match-end 0)))
375 ((looking-at "~")
376 (forward-char 1)
377 (setq tilde (read (current-buffer))
378 type (car tilde)
379 value (cadr tilde))
380 (cond
381 ((memq type '(pad pad-left))
382 (setq pad-width value))
383 ((eq type 'pad-right)
384 (setq pad-width (- value)))
385 ((memq type '(max-right max))
386 (setq max-width value))
387 ((eq type 'max-left)
388 (setq max-width (- value)))
389 ((memq type '(cut cut-left))
390 (setq cut-width value))
391 ((eq type 'cut-right)
392 (setq cut-width (- value)))
393 ((eq type 'ignore)
394 (setq ignore-value
395 (if (stringp value) value (format "%s" value))))
396 ((eq type 'form)
397 (setq tilde-form value))
399 (error "Unknown tilde type: %s" tilde)))
402 nil)))
403 ;; User-defined spec -- find the spec name.
404 (when (eq (setq spec (char-after)) ?u)
405 (forward-char 1)
406 (setq user-defined (char-after)))
407 (forward-char 1)
408 (delete-region spec-beg (point))
410 ;; Now we have all the relevant data on this spec, so
411 ;; we start doing stuff.
412 (insert "%")
413 (if (eq spec ?%)
414 ;; "%%" just results in a "%".
415 (insert "%")
416 (cond
417 ;; Do tilde forms.
418 ((eq spec ?@)
419 (setq elem (list tilde-form ?s)))
420 ;; Treat user defined format specifiers specially.
421 (user-defined
422 (setq elem
423 (list
424 (list (intern (format "gnus-user-format-function-%c"
425 user-defined))
426 'gnus-tmp-header)
427 ?s)))
428 ;; Find the specification from `spec-alist'.
429 ((setq elem (cdr (assq spec spec-alist))))
431 (setq elem '("*" ?s))))
432 (setq elem-type (cadr elem))
433 ;; Insert the new format elements.
434 (when pad-width
435 (insert (number-to-string pad-width)))
436 ;; Create the form to be evaled.
437 (if (or max-width cut-width ignore-value)
438 (progn
439 (insert ?s)
440 (let ((el (car elem)))
441 (cond ((= (cadr elem) ?c)
442 (setq el (list 'char-to-string el)))
443 ((= (cadr elem) ?d)
444 (setq el (list 'int-to-string el))))
445 (when ignore-value
446 (setq el (gnus-tilde-ignore-form el ignore-value)))
447 (when cut-width
448 (setq el (gnus-tilde-cut-form el cut-width)))
449 (when max-width
450 (setq el (gnus-tilde-max-form el max-width)))
451 (push el flist)))
452 (insert elem-type)
453 (push (car elem) flist))))
454 (setq fstring (buffer-string)))
456 ;; Do some postprocessing to increase efficiency.
457 (setq
458 result
459 (cond
460 ;; Emptyness.
461 ((string= fstring "")
462 nil)
463 ;; Not a format string.
464 ((not (string-match "%" fstring))
465 (list fstring))
466 ;; A format string with just a single string spec.
467 ((string= fstring "%s")
468 (list (car flist)))
469 ;; A single character.
470 ((string= fstring "%c")
471 (list (car flist)))
472 ;; A single number.
473 ((string= fstring "%d")
474 (setq dontinsert)
475 (if insert
476 (list `(princ ,(car flist)))
477 (list `(int-to-string ,(car flist)))))
478 ;; Just lots of chars and strings.
479 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
480 (nreverse flist))
481 ;; A single string spec at the beginning of the spec.
482 ((string-match "\\`%[sc][^%]+\\'" fstring)
483 (list (car flist) (substring fstring 2)))
484 ;; A single string spec in the middle of the spec.
485 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
486 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
487 ;; A single string spec in the end of the spec.
488 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
489 (list (match-string 1 fstring) (car flist)))
490 ;; A more complex spec.
492 (list (cons 'format (cons fstring (nreverse flist)))))))
494 (if insert
495 (when result
496 (if dontinsert
497 result
498 (cons 'insert result)))
499 (cond ((stringp result)
500 result)
501 ((consp result)
502 (cons 'concat result))
503 (t "")))))
505 (defun gnus-eval-format (format &optional alist props)
506 "Eval the format variable FORMAT, using ALIST.
507 If PROPS, insert the result."
508 (let ((form (gnus-parse-format format alist props)))
509 (if props
510 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
511 (eval form))))
513 (defun gnus-compile ()
514 "Byte-compile the user-defined format specs."
515 (interactive)
516 (require 'bytecomp)
517 (let ((entries gnus-format-specs)
518 (byte-compile-warnings '(unresolved callargs redefine))
519 entry gnus-tmp-func)
520 (save-excursion
521 (gnus-message 7 "Compiling format specs...")
523 (while entries
524 (setq entry (pop entries))
525 (if (eq (car entry) 'version)
526 (setq gnus-format-specs (delq entry gnus-format-specs))
527 (let ((form (caddr entry)))
528 (when (and (listp form)
529 ;; Under GNU Emacs, it's (byte-code ...)
530 (not (eq 'byte-code (car form)))
531 ;; Under XEmacs, it's (funcall #<compiled-function ...>)
532 (not (and (eq 'funcall (car form))
533 (byte-code-function-p (cadr form)))))
534 (fset 'gnus-tmp-func `(lambda () ,form))
535 (byte-compile 'gnus-tmp-func)
536 (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func))))))
538 (push (cons 'version emacs-version) gnus-format-specs)
539 ;; Mark the .newsrc.eld file as "dirty".
540 (gnus-dribble-touch)
541 (gnus-message 7 "Compiling user specs...done"))))
543 (defun gnus-set-format (type &optional insertable)
544 (set (intern (format "gnus-%s-line-format-spec" type))
545 (gnus-parse-format
546 (symbol-value (intern (format "gnus-%s-line-format" type)))
547 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
548 insertable)))
550 (provide 'gnus-spec)
552 ;; Local Variables:
553 ;; coding: iso-8859-1
554 ;; End:
556 ;;; gnus-spec.el ends here