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 (group "%M\%S\%p\%P\%5y: %(%g%)%l\n" ,gnus-group-line-format-spec)
129 (summary-dummy "* %(: :%) %S\n"
130 ,gnus-summary-dummy-line-format-spec)
131 (summary "%U\%R\%z\%I\%(%[%4L: %-23,23n%]%) %s\n"
132 ,gnus-summary-line-format-spec))
133 "Alist of format specs.")
135 (defvar gnus-article-mode-line-format-spec nil)
136 (defvar gnus-summary-mode-line-format-spec nil)
137 (defvar gnus-group-mode-line-format-spec nil)
139 ;;; Phew. All that gruft is over with, fortunately.
142 (defun gnus-update-format (var)
143 "Update the format specification near point."
148 ;; Find the end of the current word.
149 (re-search-forward "[ \t\n]" nil t)
151 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
153 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
154 (match-string 1 var))))
155 (entry (assq type gnus-format-specs))
158 (setq gnus-format-specs (delq entry gnus-format-specs)))
160 (intern (format "%s-spec" var))
161 (gnus-parse-format (setq value (symbol-value (intern var)))
162 (symbol-value (intern (format "%s-alist" var)))
163 (not (string-match "mode" var))))
164 (setq spec (symbol-value (intern (format "%s-spec" var))))
165 (push (list type value spec) gnus-format-specs)
167 (pop-to-buffer "*Gnus Format*")
169 (lisp-interaction-mode)
170 (insert (pp-to-string spec))))
172 (defun gnus-update-format-specifications (&optional force &rest types)
173 "Update all (necessary) format specifications."
174 ;; Make the indentation array.
175 ;; See whether all the stored info needs to be flushed.
177 (not (equal emacs-version
178 (cdr (assq 'version gnus-format-specs)))))
179 (setq gnus-format-specs nil))
181 ;; Go through all the formats and see whether they need updating.
182 (let (new-format entry type val)
183 (while (setq type (pop types))
184 ;; Jump to the proper buffer to find out the value of
185 ;; the variable, if possible. (It may be buffer-local.)
187 (let ((buffer (intern (format "gnus-%s-buffer" type)))
189 (when (and (boundp buffer)
190 (setq val (symbol-value buffer))
191 (gnus-buffer-exists-p val))
193 (setq new-format (symbol-value
194 (intern (format "gnus-%s-line-format" type)))))
195 (setq entry (cdr (assq type gnus-format-specs)))
197 (equal (car entry) new-format))
198 ;; Use the old format.
199 (set (intern (format "gnus-%s-line-format-spec" type))
201 ;; This is a new format.
203 (if (not (stringp new-format))
204 ;; This is a function call or something.
206 ;; This is a "real" format.
210 (intern (format "gnus-%s-line-format-alist" type)))
211 (not (string-match "mode$" (symbol-name type))))))
212 ;; Enter the new format spec into the list.
215 (setcar (cdr entry) val)
216 (setcar entry new-format))
217 (push (list type new-format val) gnus-format-specs))
218 (set (intern (format "gnus-%s-line-format-spec" type)) val)))))
220 (unless (assq 'version gnus-format-specs)
221 (push (cons 'version emacs-version) gnus-format-specs)))
223 (defvar gnus-mouse-face-0 'highlight)
224 (defvar gnus-mouse-face-1 'highlight)
225 (defvar gnus-mouse-face-2 'highlight)
226 (defvar gnus-mouse-face-3 'highlight)
227 (defvar gnus-mouse-face-4 'highlight)
229 (defun gnus-mouse-face-function (form type)
230 `(gnus-put-text-property
231 (point) (progn ,@form (point))
235 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
237 (defvar gnus-face-0 'bold)
238 (defvar gnus-face-1 'italic)
239 (defvar gnus-face-2 'bold-italic)
240 (defvar gnus-face-3 'bold)
241 (defvar gnus-face-4 'bold)
243 (defun gnus-face-face-function (form type)
244 `(gnus-add-text-properties
245 (point) (progn ,@form (point))
246 '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
248 (defun gnus-balloon-face-function (form type)
249 `(gnus-put-text-property
250 (point) (progn ,@form (point))
252 ,(intern (format "gnus-balloon-face-%d" type))))
254 (defun gnus-spec-tab (column)
256 `(insert (make-string (max (- ,column (current-column)) 0) ? ))
258 (if (> (current-column) ,(abs column))
259 (delete-region (point)
260 (- (point) (- (current-column) ,(abs column))))
261 (insert (make-string (max (- ,(abs column) (current-column)) 0)
264 (defun gnus-correct-length (string)
265 "Return the correct width of STRING."
267 (mapcar (lambda (char) (incf length (gnus-char-width char))) string)
270 (defun gnus-correct-substring (string start end)
274 (length (length string)))
275 ;; Find the start position.
276 (while (and (< seek length)
278 (incf wstart (gnus-char-width (aref string seek)))
282 ;; Find the end position.
283 (while (and (< seek length)
285 (incf wend (gnus-char-width (aref string seek)))
288 (substring string wstart (1- wend))))
290 (defun gnus-tilde-max-form (el max-width)
291 "Return a form that limits EL to MAX-WIDTH."
292 (let ((max (abs max-width)))
294 `(if (> (,(if gnus-use-correct-string-widths
299 `(,(if gnus-use-correct-string-widths
300 'gnus-correct-substring
302 ,el (- (,(if gnus-use-correct-string-widths
306 `(,(if gnus-use-correct-string-widths
307 'gnus-correct-substring
311 `(let ((val (eval ,el)))
312 (if (> (,(if gnus-use-correct-string-widths
316 `(,(if gnus-use-correct-string-widths
317 'gnus-correct-substring
319 val (- (,(if gnus-use-correct-string-widths
322 `(,(if gnus-use-correct-string-widths
323 'gnus-correct-substring
328 (defun gnus-tilde-cut-form (el cut-width)
329 "Return a form that cuts CUT-WIDTH off of EL."
330 (let ((cut (abs cut-width)))
332 `(if (> (,(if gnus-use-correct-string-widths
336 `(,(if gnus-use-correct-string-widths
337 'gnus-correct-substring
339 (- (,(if gnus-use-correct-string-widths
342 `(,(if gnus-use-correct-string-widths
343 'gnus-correct-substring
344 'substring) ,el ,cut))
346 `(let ((val (eval ,el)))
347 (if (> (,(if gnus-use-correct-string-widths
351 `(,(if gnus-use-correct-string-widths
352 'gnus-correct-substring
354 (- (,(if gnus-use-correct-string-widths
357 `(,(if gnus-use-correct-string-widths
358 'gnus-correct-substring
359 'substring) val ,cut))
362 (defun gnus-tilde-ignore-form (el ignore-value)
363 "Return a form that is blank when EL is IGNORE-VALUE."
365 `(if (equal ,el ,ignore-value)
367 `(let ((val (eval ,el)))
368 (if (equal val ,ignore-value)
371 (defun gnus-parse-format (format spec-alist &optional insert)
372 ;; This function parses the FORMAT string with the help of the
373 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
374 ;; string. If the FORMAT string contains the specifiers %( and %)
375 ;; the text between them will have the mouse-face text property.
376 ;; If the FORMAT string contains the specifiers %[ and %], the text between
377 ;; them will have the balloon-help text property.
379 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'"
381 (gnus-parse-complex-format format spec-alist)
382 ;; This is a simple format.
383 (gnus-parse-simple-format format spec-alist insert)))
385 (defun gnus-parse-complex-format (format spec-alist)
387 (gnus-set-work-buffer)
389 (goto-char (point-min))
390 (while (re-search-forward "\"" nil t)
391 (replace-match "\\\"" nil t))
392 (goto-char (point-min))
394 ;; Convert all font specs into font spec lists.
395 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
396 (let ((number (if (match-beginning 1)
397 (match-string 1) "0"))
398 (delim (aref (match-string 2) 0)))
399 (if (or (= delim ?\()
402 (replace-match (concat "\"("
403 (cond ((= delim ?\() "mouse")
404 ((= delim ?\{) "face")
408 (replace-match "\")\""))))
409 (goto-char (point-max))
411 ;; Convert point position commands.
412 (goto-char (point-min))
413 (while (re-search-forward "%\\([-0-9]+\\)?C" nil t)
414 (replace-match "\"(point)\"" t t))
415 ;; Convert TAB commands.
416 (goto-char (point-min))
417 (while (re-search-forward "%\\([-0-9]+\\)=" nil t)
418 (replace-match (format "\"(tab %s)\"" (match-string 1)) t t))
419 ;; Convert the buffer into the spec.
420 (goto-char (point-min))
421 (let ((form (read (current-buffer))))
422 (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
424 (defun gnus-complex-form-to-spec (form spec-alist)
430 (gnus-parse-simple-format sform spec-alist t))
431 ((eq (car sform) 'point)
432 `(gnus-put-text-property (1- (point)) (point) 'gnus-position t))
433 ((eq (car sform) 'tab)
434 (gnus-spec-tab (cadr sform)))
436 (funcall (intern (format "gnus-%s-face-function" (car sform)))
437 (gnus-complex-form-to-spec (cddr sform) spec-alist)
441 (defun gnus-parse-simple-format (format spec-alist &optional insert)
442 ;; This function parses the FORMAT string with the help of the
443 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
446 spec flist fstring elem result dontinsert user-defined
447 type value pad-width spec-beg cut-width ignore-value
448 tilde-form tilde elem-type)
450 (gnus-set-work-buffer)
452 (goto-char (point-min))
453 (while (re-search-forward "%" nil t)
454 (setq user-defined nil
461 (setq spec-beg (1- (point)))
463 ;; Parse this spec fully.
466 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
467 (setq pad-width (string-to-number (match-string 1)))
468 (when (match-beginning 2)
469 (setq max-width (string-to-number (buffer-substring
470 (1+ (match-beginning 2))
472 (goto-char (match-end 0)))
475 (setq tilde (read (current-buffer))
479 ((memq type '(pad pad-left))
480 (setq pad-width value))
481 ((eq type 'pad-right)
482 (setq pad-width (- value)))
483 ((memq type '(max-right max))
484 (setq max-width value))
486 (setq max-width (- value)))
487 ((memq type '(cut cut-left))
488 (setq cut-width value))
489 ((eq type 'cut-right)
490 (setq cut-width (- value)))
493 (if (stringp value) value (format "%s" value))))
495 (setq tilde-form value))
497 (error "Unknown tilde type: %s" tilde)))
501 ;; User-defined spec -- find the spec name.
502 (when (eq (setq spec (char-after)) ?u)
504 (setq user-defined (char-after)))
506 (delete-region spec-beg (point))
508 ;; Now we have all the relevant data on this spec, so
509 ;; we start doing stuff.
512 ;; "%%" just results in a "%".
517 (setq elem (list tilde-form ?s)))
518 ;; Treat user defined format specifiers specially.
522 (list (intern (format "gnus-user-format-function-%c"
526 ;; Find the specification from `spec-alist'.
527 ((setq elem (cdr (assq spec spec-alist))))
529 (setq elem '("*" ?s))))
530 (setq elem-type (cadr elem))
531 ;; Insert the new format elements.
533 (insert (number-to-string pad-width)))
534 ;; Create the form to be evaled.
535 (if (or max-width cut-width ignore-value)
538 (let ((el (car elem)))
539 (cond ((= (cadr elem) ?c)
540 (setq el (list 'char-to-string el)))
542 (setq el (list 'int-to-string el))))
544 (setq el (gnus-tilde-ignore-form el ignore-value)))
546 (setq el (gnus-tilde-cut-form el cut-width)))
548 (setq el (gnus-tilde-max-form el max-width)))
551 (push (car elem) flist))))
552 (setq fstring (buffer-string)))
554 ;; Do some postprocessing to increase efficiency.
559 ((string= fstring "")
561 ;; Not a format string.
562 ((not (string-match "%" fstring))
564 ;; A format string with just a single string spec.
565 ((string= fstring "%s")
567 ;; A single character.
568 ((string= fstring "%c")
571 ((string= fstring "%d")
574 (list `(princ ,(car flist)))
575 (list `(int-to-string ,(car flist)))))
576 ;; Just lots of chars and strings.
577 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
579 ;; A single string spec at the beginning of the spec.
580 ((string-match "\\`%[sc][^%]+\\'" fstring)
581 (list (car flist) (substring fstring 2)))
582 ;; A single string spec in the middle of the spec.
583 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
584 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
585 ;; A single string spec in the end of the spec.
586 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
587 (list (match-string 1 fstring) (car flist)))
588 ;; A more complex spec.
590 (list (cons 'format (cons fstring (nreverse flist)))))))
596 (cons 'insert result)))
597 (cond ((stringp result)
600 (cons 'concat result))
603 (defun gnus-eval-format (format &optional alist props)
604 "Eval the format variable FORMAT, using ALIST.
605 If PROPS, insert the result."
606 (let ((form (gnus-parse-format format alist props)))
608 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
611 (defun gnus-compile ()
612 "Byte-compile the user-defined format specs."
615 (let ((entries gnus-format-specs)
616 (byte-compile-warnings '(unresolved callargs redefine))
619 (gnus-message 7 "Compiling format specs...")
622 (setq entry (pop entries))
623 (if (eq (car entry) 'version)
624 (setq gnus-format-specs (delq entry gnus-format-specs))
625 (let ((form (caddr entry)))
626 (when (and (listp form)
627 ;; Under GNU Emacs, it's (byte-code ...)
628 (not (eq 'byte-code (car form)))
629 ;; Under XEmacs, it's (funcall #<compiled-function ...>)
630 (not (and (eq 'funcall (car form))
631 (byte-code-function-p (cadr form)))))
632 (defalias 'gnus-tmp-func `(lambda () ,form))
633 (byte-compile 'gnus-tmp-func)
634 (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func))))))
636 (push (cons 'version emacs-version) gnus-format-specs)
637 ;; Mark the .newsrc.eld file as "dirty".
639 (gnus-message 7 "Compiling user specs...done"))))
641 (defun gnus-set-format (type &optional insertable)
642 (set (intern (format "gnus-%s-line-format-spec" type))
644 (symbol-value (intern (format "gnus-%s-line-format" type)))
645 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
651 ;; coding: iso-8859-1
654 ;;; gnus-spec.el ends here