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>
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 (defcustom gnus-use-correct-string-widths t
34 "*If non-nil, use correct functions for dealing with wide characters."
38 ;;; Internal variables.
40 (defvar gnus-summary-mark-positions nil)
41 (defvar gnus-group-mark-positions nil)
42 (defvar gnus-group-indentation "")
44 ;; Format specs. The chunks below are the machine-generated forms
45 ;; that are to be evaled as the result of the default format strings.
46 ;; We write them in here to get them byte-compiled. That way the
47 ;; default actions will be quite fast, while still retaining the full
48 ;; flexibility of the user-defined format specs.
50 ;; First we have lots of dummy defvars to let the compiler know these
51 ;; are really dynamic variables.
53 (defvar gnus-tmp-unread)
54 (defvar gnus-tmp-replied)
55 (defvar gnus-tmp-score-char)
56 (defvar gnus-tmp-indentation)
57 (defvar gnus-tmp-opening-bracket)
58 (defvar gnus-tmp-lines)
59 (defvar gnus-tmp-name)
60 (defvar gnus-tmp-closing-bracket)
61 (defvar gnus-tmp-subject-or-nil)
62 (defvar gnus-tmp-subject)
63 (defvar gnus-tmp-marked)
64 (defvar gnus-tmp-marked-mark)
65 (defvar gnus-tmp-subscribed)
66 (defvar gnus-tmp-process-marked)
67 (defvar gnus-tmp-number-of-unread)
68 (defvar gnus-tmp-group-name)
69 (defvar gnus-tmp-group)
70 (defvar gnus-tmp-article-number)
71 (defvar gnus-tmp-unread-and-unselected)
72 (defvar gnus-tmp-news-method)
73 (defvar gnus-tmp-news-server)
74 (defvar gnus-tmp-article-number)
75 (defvar gnus-mouse-face)
76 (defvar gnus-mouse-face-prop)
78 (defun gnus-summary-line-format-spec ()
79 (insert gnus-tmp-unread gnus-tmp-replied
80 gnus-tmp-score-char gnus-tmp-indentation)
81 (gnus-put-text-property
85 gnus-tmp-opening-bracket
88 (if (> (length gnus-tmp-name) 20)
89 (substring gnus-tmp-name 0 20)
91 gnus-tmp-closing-bracket)
93 gnus-mouse-face-prop gnus-mouse-face)
94 (insert " " gnus-tmp-subject-or-nil "\n"))
96 (defvar gnus-summary-line-format-spec
97 (gnus-byte-code 'gnus-summary-line-format-spec))
99 (defun gnus-summary-dummy-line-format-spec ()
101 (gnus-put-text-property
106 gnus-mouse-face-prop gnus-mouse-face)
107 (insert " " gnus-tmp-subject "\n"))
109 (defvar gnus-summary-dummy-line-format-spec
110 (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
112 (defun gnus-group-line-format-spec ()
113 (insert gnus-tmp-marked-mark gnus-tmp-subscribed
114 gnus-tmp-process-marked
115 gnus-group-indentation
116 (format "%5s: " gnus-tmp-number-of-unread))
117 (gnus-put-text-property
120 (insert gnus-tmp-group "\n")
122 gnus-mouse-face-prop gnus-mouse-face))
123 (defvar gnus-group-line-format-spec
124 (gnus-byte-code 'gnus-group-line-format-spec))
126 (defvar gnus-format-specs
127 `((version . ,emacs-version)
128 (gnus-version . ,(gnus-continuum-version))
129 (group "%M\%S\%p\%P\%5y: %(%g%)%l\n" ,gnus-group-line-format-spec)
130 (summary-dummy "* %(: :%) %S\n"
131 ,gnus-summary-dummy-line-format-spec)
132 (summary "%U\%R\%z\%I\%(%[%4L: %-23,23n%]%) %s\n"
133 ,gnus-summary-line-format-spec))
134 "Alist of format specs.")
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 with, 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 (entry (assq type gnus-format-specs))
159 (setq gnus-format-specs (delq entry gnus-format-specs)))
161 (intern (format "%s-spec" var))
162 (gnus-parse-format (setq value (symbol-value (intern var)))
163 (symbol-value (intern (format "%s-alist" var)))
164 (not (string-match "mode" var))))
165 (setq spec (symbol-value (intern (format "%s-spec" var))))
166 (push (list type value spec) gnus-format-specs)
168 (pop-to-buffer "*Gnus Format*")
170 (lisp-interaction-mode)
171 (insert (pp-to-string spec))))
173 (defun gnus-update-format-specifications (&optional force &rest types)
174 "Update all (necessary) format specifications."
175 ;; Make the indentation array.
176 ;; See whether all the stored info needs to be flushed.
178 (not (equal (gnus-continuum-version)
179 (cdr (assq 'gnus-version gnus-format-specs))))
180 (not (equal emacs-version
181 (cdr (assq 'version gnus-format-specs)))))
182 (setq gnus-format-specs nil))
184 ;; Go through all the formats and see whether they need updating.
185 (let (new-format entry type val)
186 (while (setq type (pop types))
187 ;; Jump to the proper buffer to find out the value of the
188 ;; variable, if possible. (It may be buffer-local.)
190 (let ((buffer (intern (format "gnus-%s-buffer" type)))
192 (when (and (boundp buffer)
193 (setq val (symbol-value buffer))
194 (gnus-buffer-exists-p val))
196 (setq new-format (symbol-value
197 (intern (format "gnus-%s-line-format" type)))))
198 (setq entry (cdr (assq type gnus-format-specs)))
200 (equal (car entry) new-format))
201 ;; Use the old format.
202 (set (intern (format "gnus-%s-line-format-spec" type))
204 ;; This is a new format.
206 (if (not (stringp new-format))
207 ;; This is a function call or something.
209 ;; This is a "real" format.
213 (intern (format "gnus-%s-line-format-alist" type)))
214 (not (string-match "mode$" (symbol-name type))))))
215 ;; Enter the new format spec into the list.
218 (setcar (cdr entry) val)
219 (setcar entry new-format))
220 (push (list type new-format val) gnus-format-specs))
221 (set (intern (format "gnus-%s-line-format-spec" type)) val)))))
223 (unless (assq 'version gnus-format-specs)
224 (push (cons 'version emacs-version) gnus-format-specs)))
226 (defvar gnus-mouse-face-0 'highlight)
227 (defvar gnus-mouse-face-1 'highlight)
228 (defvar gnus-mouse-face-2 'highlight)
229 (defvar gnus-mouse-face-3 'highlight)
230 (defvar gnus-mouse-face-4 'highlight)
232 (defun gnus-mouse-face-function (form type)
233 `(gnus-put-text-property
234 (point) (progn ,@form (point))
238 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
240 (defvar gnus-face-0 'bold)
241 (defvar gnus-face-1 'italic)
242 (defvar gnus-face-2 'bold-italic)
243 (defvar gnus-face-3 'bold)
244 (defvar gnus-face-4 'bold)
246 (defun gnus-face-face-function (form type)
247 `(gnus-add-text-properties
248 (point) (progn ,@form (point))
249 '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
251 (defun gnus-balloon-face-function (form type)
252 `(gnus-put-text-property
253 (point) (progn ,@form (point))
255 ,(intern (format "gnus-balloon-face-%d" type))))
257 (defun gnus-spec-tab (column)
259 `(insert (make-string (max (- ,column (current-column)) 0) ? ))
261 (if (> (current-column) ,(abs column))
262 (delete-region (point)
263 (- (point) (- (current-column) ,(abs column))))
264 (insert (make-string (max (- ,(abs column) (current-column)) 0)
267 (defun gnus-correct-length (string)
268 "Return the correct width of STRING."
270 (mapcar (lambda (char) (incf length (gnus-char-width char))) string)
273 (defun gnus-correct-substring (string start &optional end)
277 (length (length string)))
278 ;; Find the start position.
279 (while (and (< seek length)
281 (incf wstart (gnus-char-width (aref string seek)))
285 ;; Find the end position.
286 (while (and (< seek length)
289 (incf wend (gnus-char-width (aref string seek)))
292 (substring string wstart (1- wend))))
294 (defun gnus-tilde-max-form (el max-width)
295 "Return a form that limits EL to MAX-WIDTH."
296 (let ((max (abs max-width)))
298 `(if (> (,(if gnus-use-correct-string-widths
303 `(,(if gnus-use-correct-string-widths
304 'gnus-correct-substring
306 ,el (- (,(if gnus-use-correct-string-widths
310 `(,(if gnus-use-correct-string-widths
311 'gnus-correct-substring
315 `(let ((val (eval ,el)))
316 (if (> (,(if gnus-use-correct-string-widths
320 `(,(if gnus-use-correct-string-widths
321 'gnus-correct-substring
323 val (- (,(if gnus-use-correct-string-widths
326 `(,(if gnus-use-correct-string-widths
327 'gnus-correct-substring
332 (defun gnus-tilde-cut-form (el cut-width)
333 "Return a form that cuts CUT-WIDTH off of EL."
334 (let ((cut (abs cut-width)))
336 `(if (> (,(if gnus-use-correct-string-widths
340 `(,(if gnus-use-correct-string-widths
341 'gnus-correct-substring
343 (- (,(if gnus-use-correct-string-widths
346 `(,(if gnus-use-correct-string-widths
347 'gnus-correct-substring
348 'substring) ,el ,cut))
350 `(let ((val (eval ,el)))
351 (if (> (,(if gnus-use-correct-string-widths
355 `(,(if gnus-use-correct-string-widths
356 'gnus-correct-substring
358 (- (,(if gnus-use-correct-string-widths
361 `(,(if gnus-use-correct-string-widths
362 'gnus-correct-substring
363 'substring) val ,cut))
366 (defun gnus-tilde-ignore-form (el ignore-value)
367 "Return a form that is blank when EL is IGNORE-VALUE."
369 `(if (equal ,el ,ignore-value)
371 `(let ((val (eval ,el)))
372 (if (equal val ,ignore-value)
375 (defun gnus-correct-pad-form (el pad-width)
376 "Return a form that pads EL to PAD-WIDTH accounting for multi-column
377 characters correctly. This is because `format' may pad to columns or to
378 characters when given a pad value."
379 (let ((pad (abs pad-width))
380 (side (< 0 pad-width)))
382 `(let ((need (- ,pad (gnus-correct-length ,el))))
384 (concat ,(when side '(make-string need ?\ ))
386 ,(when (not side) '(make-string need ?\ )))
388 `(let* ((val (eval ,el))
389 (need (- ,pad (gnus-correct-length ,el))))
391 (concat ,(when side '(make-string need ?\ ))
393 ,(when (not side) '(make-string need ?\ )))
396 (defun gnus-parse-format (format spec-alist &optional insert)
397 ;; This function parses the FORMAT string with the help of the
398 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
399 ;; string. If the FORMAT string contains the specifiers %( and %)
400 ;; the text between them will have the mouse-face text property.
401 ;; If the FORMAT string contains the specifiers %[ and %], the text between
402 ;; them will have the balloon-help text property.
404 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'"
406 (gnus-parse-complex-format format spec-alist)
407 ;; This is a simple format.
408 (gnus-parse-simple-format format spec-alist insert)))
410 (defun gnus-parse-complex-format (format spec-alist)
412 (gnus-set-work-buffer)
414 (goto-char (point-min))
415 (while (re-search-forward "\"" nil t)
416 (replace-match "\\\"" nil t))
417 (goto-char (point-min))
419 ;; Convert all font specs into font spec lists.
420 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
421 (let ((number (if (match-beginning 1)
422 (match-string 1) "0"))
423 (delim (aref (match-string 2) 0)))
424 (if (or (= delim ?\()
427 (replace-match (concat "\"("
428 (cond ((= delim ?\() "mouse")
429 ((= delim ?\{) "face")
433 (replace-match "\")\""))))
434 (goto-char (point-max))
436 ;; Convert point position commands.
437 (goto-char (point-min))
438 (while (re-search-forward "%\\([-0-9]+\\)?C" nil t)
439 (replace-match "\"(point)\"" t t))
440 ;; Convert TAB commands.
441 (goto-char (point-min))
442 (while (re-search-forward "%\\([-0-9]+\\)=" nil t)
443 (replace-match (format "\"(tab %s)\"" (match-string 1)) t t))
444 ;; Convert the buffer into the spec.
445 (goto-char (point-min))
446 (let ((form (read (current-buffer))))
447 ;; If the first element is '(point), we just remove it.
448 (when (equal (car form) '(point))
450 (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
452 (defun gnus-complex-form-to-spec (form spec-alist)
458 (gnus-parse-simple-format sform spec-alist t))
459 ((eq (car sform) 'point)
460 `(gnus-put-text-property (1- (point)) (point) 'gnus-position t))
461 ((eq (car sform) 'tab)
462 (gnus-spec-tab (cadr sform)))
464 (funcall (intern (format "gnus-%s-face-function" (car sform)))
465 (gnus-complex-form-to-spec (cddr sform) spec-alist)
469 (defun gnus-parse-simple-format (format spec-alist &optional insert)
470 ;; This function parses the FORMAT string with the help of the
471 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
474 spec flist fstring elem result dontinsert user-defined
475 type value pad-width spec-beg cut-width ignore-value
476 tilde-form tilde elem-type)
478 (gnus-set-work-buffer)
480 (goto-char (point-min))
481 (while (re-search-forward "%" nil t)
482 (setq user-defined nil
489 (setq spec-beg (1- (point)))
491 ;; Parse this spec fully.
494 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
495 (setq pad-width (string-to-number (match-string 1)))
496 (when (match-beginning 2)
497 (setq max-width (string-to-number (buffer-substring
498 (1+ (match-beginning 2))
500 (goto-char (match-end 0)))
503 (setq tilde (read (current-buffer))
507 ((memq type '(pad pad-left))
508 (setq pad-width value))
509 ((eq type 'pad-right)
510 (setq pad-width (- value)))
511 ((memq type '(max-right max))
512 (setq max-width value))
514 (setq max-width (- value)))
515 ((memq type '(cut cut-left))
516 (setq cut-width value))
517 ((eq type 'cut-right)
518 (setq cut-width (- value)))
521 (if (stringp value) value (format "%s" value))))
523 (setq tilde-form value))
525 (error "Unknown tilde type: %s" tilde)))
529 ;; User-defined spec -- find the spec name.
530 (when (eq (setq spec (char-after)) ?u)
532 (setq user-defined (char-after)))
534 (delete-region spec-beg (point))
536 ;; Now we have all the relevant data on this spec, so
537 ;; we start doing stuff.
540 ;; "%%" just results in a "%".
545 (setq elem (list tilde-form ?s)))
546 ;; Treat user defined format specifiers specially.
550 (list (intern (format "gnus-user-format-function-%c"
554 ;; Find the specification from `spec-alist'.
555 ((setq elem (cdr (assq spec spec-alist))))
557 (setq elem '("*" ?s))))
558 (setq elem-type (cadr elem))
559 ;; Insert the new format elements.
561 (not (and (featurep 'xemacs)
562 gnus-use-correct-string-widths)))
563 (insert (number-to-string pad-width)))
564 ;; Create the form to be evaled.
565 (if (or max-width cut-width ignore-value
566 (and (featurep 'xemacs)
567 gnus-use-correct-string-widths))
570 (let ((el (car elem)))
571 (cond ((= (cadr elem) ?c)
572 (setq el (list 'char-to-string el)))
574 (setq el (list 'int-to-string el))))
576 (setq el (gnus-tilde-ignore-form el ignore-value)))
578 (setq el (gnus-tilde-cut-form el cut-width)))
580 (setq el (gnus-tilde-max-form el max-width)))
582 (setq el (gnus-correct-pad-form el pad-width)))
585 (push (car elem) flist))))
586 (setq fstring (buffer-string)))
588 ;; Do some postprocessing to increase efficiency.
593 ((string= fstring "")
595 ;; Not a format string.
596 ((not (string-match "%" fstring))
598 ;; A format string with just a single string spec.
599 ((string= fstring "%s")
601 ;; A single character.
602 ((string= fstring "%c")
605 ((string= fstring "%d")
608 (list `(princ ,(car flist)))
609 (list `(int-to-string ,(car flist)))))
610 ;; Just lots of chars and strings.
611 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
613 ;; A single string spec at the beginning of the spec.
614 ((string-match "\\`%[sc][^%]+\\'" fstring)
615 (list (car flist) (substring fstring 2)))
616 ;; A single string spec in the middle of the spec.
617 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
618 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
619 ;; A single string spec in the end of the spec.
620 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
621 (list (match-string 1 fstring) (car flist)))
622 ;; A more complex spec.
624 (list (cons 'format (cons fstring (nreverse flist)))))))
630 (cons 'insert result)))
631 (cond ((stringp result)
634 (cons 'concat result))
637 (defun gnus-eval-format (format &optional alist props)
638 "Eval the format variable FORMAT, using ALIST.
639 If PROPS, insert the result."
640 (let ((form (gnus-parse-format format alist props)))
642 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
645 (defun gnus-compile ()
646 "Byte-compile the user-defined format specs."
649 (let ((entries gnus-format-specs)
650 (byte-compile-warnings '(unresolved callargs redefine))
653 (gnus-message 7 "Compiling format specs...")
656 (setq entry (pop entries))
657 (if (memq (car entry) '(gnus-version version))
658 (setq gnus-format-specs (delq entry gnus-format-specs))
659 (let ((form (caddr entry)))
660 (when (and (listp form)
661 ;; Under GNU Emacs, it's (byte-code ...)
662 (not (eq 'byte-code (car form)))
663 ;; Under XEmacs, it's (funcall #<compiled-function ...>)
664 (not (and (eq 'funcall (car form))
665 (byte-code-function-p (cadr form)))))
666 (defalias 'gnus-tmp-func `(lambda () ,form))
667 (byte-compile 'gnus-tmp-func)
668 (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func))))))
670 (push (cons 'version emacs-version) gnus-format-specs)
671 ;; Mark the .newsrc.eld file as "dirty".
673 (gnus-message 7 "Compiling user specs...done"))))
675 (defun gnus-set-format (type &optional insertable)
676 (set (intern (format "gnus-%s-line-format-spec" type))
678 (symbol-value (intern (format "gnus-%s-line-format" type)))
679 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
685 ;; coding: iso-8859-1
688 ;;; gnus-spec.el ends here