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 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)
288 (incf wend (gnus-char-width (aref string seek)))
291 (substring string wstart (1- wend))))
293 (defun gnus-tilde-max-form (el max-width)
294 "Return a form that limits EL to MAX-WIDTH."
295 (let ((max (abs max-width)))
297 `(if (> (,(if gnus-use-correct-string-widths
302 `(,(if gnus-use-correct-string-widths
303 'gnus-correct-substring
305 ,el (- (,(if gnus-use-correct-string-widths
309 `(,(if gnus-use-correct-string-widths
310 'gnus-correct-substring
314 `(let ((val (eval ,el)))
315 (if (> (,(if gnus-use-correct-string-widths
319 `(,(if gnus-use-correct-string-widths
320 'gnus-correct-substring
322 val (- (,(if gnus-use-correct-string-widths
325 `(,(if gnus-use-correct-string-widths
326 'gnus-correct-substring
331 (defun gnus-tilde-cut-form (el cut-width)
332 "Return a form that cuts CUT-WIDTH off of EL."
333 (let ((cut (abs cut-width)))
335 `(if (> (,(if gnus-use-correct-string-widths
339 `(,(if gnus-use-correct-string-widths
340 'gnus-correct-substring
342 (- (,(if gnus-use-correct-string-widths
345 `(,(if gnus-use-correct-string-widths
346 'gnus-correct-substring
347 'substring) ,el ,cut))
349 `(let ((val (eval ,el)))
350 (if (> (,(if gnus-use-correct-string-widths
354 `(,(if gnus-use-correct-string-widths
355 'gnus-correct-substring
357 (- (,(if gnus-use-correct-string-widths
360 `(,(if gnus-use-correct-string-widths
361 'gnus-correct-substring
362 'substring) val ,cut))
365 (defun gnus-tilde-ignore-form (el ignore-value)
366 "Return a form that is blank when EL is IGNORE-VALUE."
368 `(if (equal ,el ,ignore-value)
370 `(let ((val (eval ,el)))
371 (if (equal val ,ignore-value)
374 (defun gnus-parse-format (format spec-alist &optional insert)
375 ;; This function parses the FORMAT string with the help of the
376 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
377 ;; string. If the FORMAT string contains the specifiers %( and %)
378 ;; the text between them will have the mouse-face text property.
379 ;; If the FORMAT string contains the specifiers %[ and %], the text between
380 ;; them will have the balloon-help text property.
382 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'"
384 (gnus-parse-complex-format format spec-alist)
385 ;; This is a simple format.
386 (gnus-parse-simple-format format spec-alist insert)))
388 (defun gnus-parse-complex-format (format spec-alist)
390 (gnus-set-work-buffer)
392 (goto-char (point-min))
393 (while (re-search-forward "\"" nil t)
394 (replace-match "\\\"" nil t))
395 (goto-char (point-min))
397 ;; Convert all font specs into font spec lists.
398 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
399 (let ((number (if (match-beginning 1)
400 (match-string 1) "0"))
401 (delim (aref (match-string 2) 0)))
402 (if (or (= delim ?\()
405 (replace-match (concat "\"("
406 (cond ((= delim ?\() "mouse")
407 ((= delim ?\{) "face")
411 (replace-match "\")\""))))
412 (goto-char (point-max))
414 ;; Convert point position commands.
415 (goto-char (point-min))
416 (while (re-search-forward "%\\([-0-9]+\\)?C" nil t)
417 (replace-match "\"(point)\"" t t))
418 ;; Convert TAB commands.
419 (goto-char (point-min))
420 (while (re-search-forward "%\\([-0-9]+\\)=" nil t)
421 (replace-match (format "\"(tab %s)\"" (match-string 1)) t t))
422 ;; Convert the buffer into the spec.
423 (goto-char (point-min))
424 (let ((form (read (current-buffer))))
425 ;; If the first element is '(point), we just remove it.
426 (when (equalp (car form) '(point))
428 (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
430 (defun gnus-complex-form-to-spec (form spec-alist)
436 (gnus-parse-simple-format sform spec-alist t))
437 ((eq (car sform) 'point)
438 `(gnus-put-text-property (1- (point)) (point) 'gnus-position t))
439 ((eq (car sform) 'tab)
440 (gnus-spec-tab (cadr sform)))
442 (funcall (intern (format "gnus-%s-face-function" (car sform)))
443 (gnus-complex-form-to-spec (cddr sform) spec-alist)
447 (defun gnus-parse-simple-format (format spec-alist &optional insert)
448 ;; This function parses the FORMAT string with the help of the
449 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
452 spec flist fstring elem result dontinsert user-defined
453 type value pad-width spec-beg cut-width ignore-value
454 tilde-form tilde elem-type)
456 (gnus-set-work-buffer)
458 (goto-char (point-min))
459 (while (re-search-forward "%" nil t)
460 (setq user-defined nil
467 (setq spec-beg (1- (point)))
469 ;; Parse this spec fully.
472 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
473 (setq pad-width (string-to-number (match-string 1)))
474 (when (match-beginning 2)
475 (setq max-width (string-to-number (buffer-substring
476 (1+ (match-beginning 2))
478 (goto-char (match-end 0)))
481 (setq tilde (read (current-buffer))
485 ((memq type '(pad pad-left))
486 (setq pad-width value))
487 ((eq type 'pad-right)
488 (setq pad-width (- value)))
489 ((memq type '(max-right max))
490 (setq max-width value))
492 (setq max-width (- value)))
493 ((memq type '(cut cut-left))
494 (setq cut-width value))
495 ((eq type 'cut-right)
496 (setq cut-width (- value)))
499 (if (stringp value) value (format "%s" value))))
501 (setq tilde-form value))
503 (error "Unknown tilde type: %s" tilde)))
507 ;; User-defined spec -- find the spec name.
508 (when (eq (setq spec (char-after)) ?u)
510 (setq user-defined (char-after)))
512 (delete-region spec-beg (point))
514 ;; Now we have all the relevant data on this spec, so
515 ;; we start doing stuff.
518 ;; "%%" just results in a "%".
523 (setq elem (list tilde-form ?s)))
524 ;; Treat user defined format specifiers specially.
528 (list (intern (format "gnus-user-format-function-%c"
532 ;; Find the specification from `spec-alist'.
533 ((setq elem (cdr (assq spec spec-alist))))
535 (setq elem '("*" ?s))))
536 (setq elem-type (cadr elem))
537 ;; Insert the new format elements.
539 (insert (number-to-string pad-width)))
540 ;; Create the form to be evaled.
541 (if (or max-width cut-width ignore-value)
544 (let ((el (car elem)))
545 (cond ((= (cadr elem) ?c)
546 (setq el (list 'char-to-string el)))
548 (setq el (list 'int-to-string el))))
550 (setq el (gnus-tilde-ignore-form el ignore-value)))
552 (setq el (gnus-tilde-cut-form el cut-width)))
554 (setq el (gnus-tilde-max-form el max-width)))
557 (push (car elem) flist))))
558 (setq fstring (buffer-string)))
560 ;; Do some postprocessing to increase efficiency.
565 ((string= fstring "")
567 ;; Not a format string.
568 ((not (string-match "%" fstring))
570 ;; A format string with just a single string spec.
571 ((string= fstring "%s")
573 ;; A single character.
574 ((string= fstring "%c")
577 ((string= fstring "%d")
580 (list `(princ ,(car flist)))
581 (list `(int-to-string ,(car flist)))))
582 ;; Just lots of chars and strings.
583 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
585 ;; A single string spec at the beginning of the spec.
586 ((string-match "\\`%[sc][^%]+\\'" fstring)
587 (list (car flist) (substring fstring 2)))
588 ;; A single string spec in the middle of the spec.
589 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
590 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
591 ;; A single string spec in the end of the spec.
592 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
593 (list (match-string 1 fstring) (car flist)))
594 ;; A more complex spec.
596 (list (cons 'format (cons fstring (nreverse flist)))))))
602 (cons 'insert result)))
603 (cond ((stringp result)
606 (cons 'concat result))
609 (defun gnus-eval-format (format &optional alist props)
610 "Eval the format variable FORMAT, using ALIST.
611 If PROPS, insert the result."
612 (let ((form (gnus-parse-format format alist props)))
614 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
617 (defun gnus-compile ()
618 "Byte-compile the user-defined format specs."
621 (let ((entries gnus-format-specs)
622 (byte-compile-warnings '(unresolved callargs redefine))
625 (gnus-message 7 "Compiling format specs...")
628 (setq entry (pop entries))
629 (if (eq (car entry) 'version)
630 (setq gnus-format-specs (delq entry gnus-format-specs))
631 (let ((form (caddr entry)))
632 (when (and (listp form)
633 ;; Under GNU Emacs, it's (byte-code ...)
634 (not (eq 'byte-code (car form)))
635 ;; Under XEmacs, it's (funcall #<compiled-function ...>)
636 (not (and (eq 'funcall (car form))
637 (byte-code-function-p (cadr form)))))
638 (defalias 'gnus-tmp-func `(lambda () ,form))
639 (byte-compile 'gnus-tmp-func)
640 (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func))))))
642 (push (cons 'version emacs-version) gnus-format-specs)
643 ;; Mark the .newsrc.eld file as "dirty".
645 (gnus-message 7 "Compiling user specs...done"))))
647 (defun gnus-set-format (type &optional insertable)
648 (set (intern (format "gnus-%s-line-format-spec" type))
650 (symbol-value (intern (format "gnus-%s-line-format" type)))
651 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
657 ;; coding: iso-8859-1
660 ;;; gnus-spec.el ends here