1 ;;; gnus-spec.el --- format spec functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
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 (featurep 'xemacs)
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)
77 (defvar gnus-tmp-header)
78 (defvar gnus-tmp-from)
80 (defun gnus-summary-line-format-spec ()
81 (insert gnus-tmp-unread gnus-tmp-replied
82 gnus-tmp-score-char gnus-tmp-indentation)
83 (gnus-put-text-property
87 (format "%c%4s: %-23s%c" gnus-tmp-opening-bracket gnus-tmp-lines
90 (gnus-summary-from-or-to-or-newsgroups
91 gnus-tmp-header gnus-tmp-from))))
92 (if (> (length val) 23)
95 gnus-tmp-closing-bracket))
97 gnus-mouse-face-prop gnus-mouse-face)
98 (insert " " gnus-tmp-subject-or-nil "\n"))
100 (defvar gnus-summary-line-format-spec
101 (gnus-byte-code 'gnus-summary-line-format-spec))
103 (defun gnus-summary-dummy-line-format-spec ()
105 (gnus-put-text-property
110 gnus-mouse-face-prop gnus-mouse-face)
111 (insert " " gnus-tmp-subject "\n"))
113 (defvar gnus-summary-dummy-line-format-spec
114 (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
116 (defun gnus-group-line-format-spec ()
117 (insert gnus-tmp-marked-mark gnus-tmp-subscribed
118 gnus-tmp-process-marked
119 gnus-group-indentation
120 (format "%5s: " gnus-tmp-number-of-unread))
121 (gnus-put-text-property
124 (insert gnus-tmp-group "\n")
126 gnus-mouse-face-prop gnus-mouse-face))
127 (defvar gnus-group-line-format-spec
128 (gnus-byte-code 'gnus-group-line-format-spec))
130 (defvar gnus-format-specs
131 `((version . ,emacs-version)
132 (gnus-version . ,(gnus-continuum-version))
133 (group "%M\%S\%p\%P\%5y: %(%g%)%l\n" ,gnus-group-line-format-spec)
134 (summary-dummy "* %(: :%) %S\n"
135 ,gnus-summary-dummy-line-format-spec)
136 (summary "%U%R%z%I%(%[%4L: %-23,23f%]%) %s\n"
137 ,gnus-summary-line-format-spec))
138 "Alist of format specs.")
140 (defvar gnus-default-format-specs gnus-format-specs)
142 (defvar gnus-article-mode-line-format-spec nil)
143 (defvar gnus-summary-mode-line-format-spec nil)
144 (defvar gnus-group-mode-line-format-spec nil)
146 ;;; Phew. All that gruft is over with, fortunately.
149 (defun gnus-update-format (var)
150 "Update the format specification near point."
155 ;; Find the end of the current word.
156 (re-search-forward "[ \t\n]" nil t)
158 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
160 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
161 (match-string 1 var))))
162 (entry (assq type gnus-format-specs))
165 (setq gnus-format-specs (delq entry gnus-format-specs)))
167 (intern (format "%s-spec" var))
168 (gnus-parse-format (setq value (symbol-value (intern var)))
169 (symbol-value (intern (format "%s-alist" var)))
170 (not (string-match "mode" var))))
171 (setq spec (symbol-value (intern (format "%s-spec" var))))
172 (push (list type value spec) gnus-format-specs)
174 (pop-to-buffer "*Gnus Format*")
176 (lisp-interaction-mode)
177 (insert (pp-to-string spec))))
179 (defun gnus-update-format-specifications (&optional force &rest types)
180 "Update all (necessary) format specifications."
181 ;; Make the indentation array.
182 ;; See whether all the stored info needs to be flushed.
184 (not gnus-newsrc-file-version)
185 (not (equal (gnus-continuum-version)
186 (gnus-continuum-version gnus-newsrc-file-version)))
187 (not (equal emacs-version
188 (cdr (assq 'version gnus-format-specs)))))
189 (setq gnus-format-specs nil))
191 ;; Go through all the formats and see whether they need updating.
192 (let (new-format entry type val)
193 (while (setq type (pop types))
194 ;; Jump to the proper buffer to find out the value of the
195 ;; variable, if possible. (It may be buffer-local.)
197 (let ((buffer (intern (format "gnus-%s-buffer" type)))
199 (when (and (boundp buffer)
200 (setq val (symbol-value buffer))
201 (gnus-buffer-exists-p val))
203 (setq new-format (symbol-value
204 (intern (format "gnus-%s-line-format" type)))))
205 (setq entry (cdr (assq type gnus-format-specs)))
207 (equal (car entry) new-format))
208 ;; Use the old format.
209 (set (intern (format "gnus-%s-line-format-spec" type))
211 ;; This is a new format.
213 (if (not (stringp new-format))
214 ;; This is a function call or something.
216 ;; This is a "real" format.
220 (intern (format "gnus-%s-line-format-alist" type)))
221 (not (string-match "mode$" (symbol-name type))))))
222 ;; Enter the new format spec into the list.
225 (setcar (cdr entry) val)
226 (setcar entry new-format))
227 (push (list type new-format val) gnus-format-specs))
228 (set (intern (format "gnus-%s-line-format-spec" type)) val)))))
230 (unless (assq 'version gnus-format-specs)
231 (push (cons 'version emacs-version) gnus-format-specs)))
233 (defvar gnus-mouse-face-0 'highlight)
234 (defvar gnus-mouse-face-1 'highlight)
235 (defvar gnus-mouse-face-2 'highlight)
236 (defvar gnus-mouse-face-3 'highlight)
237 (defvar gnus-mouse-face-4 'highlight)
239 (defun gnus-mouse-face-function (form type)
240 `(gnus-put-text-property
241 (point) (progn ,@form (point))
245 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
247 (defvar gnus-face-0 'bold)
248 (defvar gnus-face-1 'italic)
249 (defvar gnus-face-2 'bold-italic)
250 (defvar gnus-face-3 'bold)
251 (defvar gnus-face-4 'bold)
253 (defun gnus-face-face-function (form type)
254 `(gnus-add-text-properties
255 (point) (progn ,@form (point))
256 '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
258 (defun gnus-balloon-face-function (form type)
259 `(gnus-put-text-property
260 (point) (progn ,@form (point))
262 ,(intern (format "gnus-balloon-face-%d" type))))
264 (defun gnus-spec-tab (column)
266 `(insert (make-string (max (- ,column (current-column)) 0) ? ))
267 (let ((column (abs column)))
268 (if gnus-use-correct-string-widths
270 (if (> (current-column) ,column)
272 (delete-backward-char 1)
273 (> (current-column) ,column))))
274 (insert (make-string (max (- ,column (current-column)) 0) ? )))
276 (if (> (current-column) ,column)
277 (delete-region (point)
278 (- (point) (- (current-column) ,column)))
279 (insert (make-string (max (- ,column (current-column)) 0)
282 (defun gnus-correct-length (string)
283 "Return the correct width of STRING."
285 (mapcar (lambda (char) (incf length (gnus-char-width char))) string)
288 (defun gnus-correct-substring (string start &optional end)
293 (length (length string))
294 (string (concat string "\0")))
295 ;; Find the start position.
296 (while (and (< seek length)
298 (incf wseek (gnus-char-width (aref string seek)))
301 ;; Find the end position.
302 (while (and (<= seek length)
305 (incf wseek (gnus-char-width (aref string seek)))
308 (substring string wstart (1- wend))))
310 (defun gnus-string-width-function ()
312 (gnus-use-correct-string-widths
313 'gnus-correct-length)
314 ((fboundp 'string-width)
319 (defun gnus-substring-function ()
321 (gnus-use-correct-string-widths
322 'gnus-correct-substring)
323 ((fboundp 'string-width)
324 'gnus-correct-substring)
328 (defun gnus-tilde-max-form (el max-width)
329 "Return a form that limits EL to MAX-WIDTH."
330 (let ((max (abs max-width))
331 (length-fun (gnus-string-width-function))
332 (substring-fun (gnus-substring-function)))
334 `(if (> (,length-fun ,el) ,max)
336 `(,substring-fun ,el (- (,length-fun ,el) ,max))
337 `(,substring-fun ,el 0 ,max))
339 `(let ((val (eval ,el)))
340 (if (> (,length-fun val) ,max)
342 `(,substring-fun val (- (,length-fun val) ,max))
343 `(,substring-fun val 0 ,max))
346 (defun gnus-tilde-cut-form (el cut-width)
347 "Return a form that cuts CUT-WIDTH off of EL."
348 (let ((cut (abs cut-width))
349 (length-fun (gnus-string-width-function))
350 (substring-fun (gnus-substring-function)))
352 `(if (> (,length-fun ,el) ,cut)
354 `(,substring-fun ,el 0 (- (,length-fun ,el) ,cut))
355 `(,substring-fun ,el ,cut))
357 `(let ((val (eval ,el)))
358 (if (> (,length-fun val) ,cut)
360 `(,substring-fun val 0 (- (,length-fun val) ,cut))
361 `(,substring-fun val ,cut))
364 (defun gnus-tilde-ignore-form (el ignore-value)
365 "Return a form that is blank when EL is IGNORE-VALUE."
367 `(if (equal ,el ,ignore-value)
369 `(let ((val (eval ,el)))
370 (if (equal val ,ignore-value)
373 (defun gnus-pad-form (el pad-width)
374 "Return a form that pads EL to PAD-WIDTH accounting for multi-column
375 characters correctly. This is because `format' may pad to columns or to
376 characters when given a pad value."
377 (let ((pad (abs pad-width))
378 (side (< 0 pad-width)))
380 `(let ((need (- ,pad (,(if gnus-use-correct-string-widths
385 (concat ,(when side '(make-string need ?\ ))
387 ,(when (not side) '(make-string need ?\ )))
389 `(let* ((val (eval ,el))
390 (need (- ,pad (,(if gnus-use-correct-string-widths
394 (concat ,(when side '(make-string need ?\ ))
396 ,(when (not side) '(make-string need ?\ )))
399 (defun gnus-parse-format (format spec-alist &optional insert)
400 ;; This function parses the FORMAT string with the help of the
401 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
402 ;; string. If the FORMAT string contains the specifiers %( and %)
403 ;; the text between them will have the mouse-face text property.
404 ;; If the FORMAT string contains the specifiers %[ and %], the text between
405 ;; them will have the balloon-help text property.
406 (let ((case-fold-search nil))
408 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'\\|%[-0-9]*="
410 (gnus-parse-complex-format format spec-alist)
411 ;; This is a simple format.
412 (gnus-parse-simple-format format spec-alist insert))))
414 (defun gnus-parse-complex-format (format spec-alist)
417 (gnus-set-work-buffer)
419 (goto-char (point-min))
420 (while (re-search-forward "\"" nil t)
421 (replace-match "\\\"" nil t))
422 (goto-char (point-min))
424 ;; Convert all font specs into font spec lists.
425 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
426 (let ((number (if (match-beginning 1)
427 (match-string 1) "0"))
428 (delim (aref (match-string 2) 0)))
429 (if (or (= delim ?\()
432 (replace-match (concat "\"("
433 (cond ((= delim ?\() "mouse")
434 ((= delim ?\{) "face")
438 (replace-match "\")\""))))
439 (goto-char (point-max))
441 ;; Convert point position commands.
442 (goto-char (point-min))
443 (let ((case-fold-search nil))
444 (while (re-search-forward "%\\([-0-9]+\\)?C" nil t)
445 (replace-match "\"(point)\"" t t)
447 ;; Convert TAB commands.
448 (goto-char (point-min))
449 (while (re-search-forward "%\\([-0-9]+\\)=" nil t)
450 (replace-match (format "\"(tab %s)\"" (match-string 1)) t t))
451 ;; Convert the buffer into the spec.
452 (goto-char (point-min))
453 (let ((form (read (current-buffer))))
455 `(let (gnus-position)
456 ,@(gnus-complex-form-to-spec form spec-alist)
458 (gnus-put-text-property gnus-position (1+ gnus-position)
461 ,@(gnus-complex-form-to-spec form spec-alist)))))))
463 (defun gnus-complex-form-to-spec (form spec-alist)
469 (gnus-parse-simple-format sform spec-alist t))
470 ((eq (car sform) 'point)
471 '(setq gnus-position (point)))
472 ((eq (car sform) 'tab)
473 (gnus-spec-tab (cadr sform)))
475 (funcall (intern (format "gnus-%s-face-function" (car sform)))
476 (gnus-complex-form-to-spec (cddr sform) spec-alist)
480 (defun gnus-parse-simple-format (format spec-alist &optional insert)
481 ;; This function parses the FORMAT string with the help of the
482 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
485 spec flist fstring elem result dontinsert user-defined
486 type value pad-width spec-beg cut-width ignore-value
487 tilde-form tilde elem-type extended-spec)
489 (gnus-set-work-buffer)
491 (goto-char (point-min))
492 (while (re-search-forward "%" nil t)
493 (setq user-defined nil
501 (setq spec-beg (1- (point)))
503 ;; Parse this spec fully.
506 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
507 (setq pad-width (string-to-number (match-string 1)))
508 (when (match-beginning 2)
509 (setq max-width (string-to-number (buffer-substring
510 (1+ (match-beginning 2))
512 (goto-char (match-end 0)))
515 (setq tilde (read (current-buffer))
519 ((memq type '(pad pad-left))
520 (setq pad-width value))
521 ((eq type 'pad-right)
522 (setq pad-width (- value)))
523 ((memq type '(max-right max))
524 (setq max-width value))
526 (setq max-width (- value)))
527 ((memq type '(cut cut-left))
528 (setq cut-width value))
529 ((eq type 'cut-right)
530 (setq cut-width (- value)))
533 (if (stringp value) value (format "%s" value))))
535 (setq tilde-form value))
537 (error "Unknown tilde type: %s" tilde)))
542 ;; User-defined spec -- find the spec name.
543 ((eq (setq spec (char-after)) ?u)
545 (when (and (eq (setq user-defined (char-after)) ?&)
546 (looking-at "&\\([^;]+\\);"))
547 (setq user-defined (match-string 1))
548 (goto-char (match-end 1))))
550 ((and (eq spec ?&) (looking-at "&\\([^;]+\\);"))
551 (setq extended-spec (intern (match-string 1)))
552 (goto-char (match-end 1))))
554 (delete-region spec-beg (point))
556 ;; Now we have all the relevant data on this spec, so
557 ;; we start doing stuff.
560 ;; "%%" just results in a "%".
565 (setq elem (list tilde-form ?s)))
566 ;; Treat user defined format specifiers specially.
570 (list (intern (format
571 (if (stringp user-defined)
572 "gnus-user-format-function-%s"
573 "gnus-user-format-function-%c")
577 ;; Find the specification from `spec-alist'.
578 ((setq elem (cdr (assq (or extended-spec spec) spec-alist))))
580 (setq elem '("*" ?s))))
581 (setq elem-type (cadr elem))
582 ;; Insert the new format elements.
584 (not (and (featurep 'xemacs)
585 gnus-use-correct-string-widths)))
586 (insert (number-to-string pad-width)))
587 ;; Create the form to be evaled.
588 (if (or max-width cut-width ignore-value
589 (and (featurep 'xemacs)
590 gnus-use-correct-string-widths))
593 (let ((el (car elem)))
594 (cond ((= (cadr elem) ?c)
595 (setq el (list 'char-to-string el)))
597 (setq el (list 'int-to-string el))))
599 (setq el (gnus-tilde-ignore-form el ignore-value)))
601 (setq el (gnus-tilde-cut-form el cut-width)))
603 (setq el (gnus-tilde-max-form el max-width)))
605 (setq el (gnus-pad-form el pad-width)))
608 (push (car elem) flist))))
609 (setq fstring (buffer-substring-no-properties (point-min) (point-max))))
611 ;; Do some postprocessing to increase efficiency.
616 ((string= fstring "")
618 ;; Not a format string.
619 ((not (string-match "%" fstring))
621 ;; A format string with just a single string spec.
622 ((string= fstring "%s")
624 ;; A single character.
625 ((string= fstring "%c")
628 ((string= fstring "%d")
631 (list `(princ ,(car flist)))
632 (list `(int-to-string ,(car flist)))))
633 ;; Just lots of chars and strings.
634 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
636 ;; A single string spec at the beginning of the spec.
637 ((string-match "\\`%[sc][^%]+\\'" fstring)
638 (list (car flist) (substring fstring 2)))
639 ;; A single string spec in the middle of the spec.
640 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
641 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
642 ;; A single string spec in the end of the spec.
643 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
644 (list (match-string 1 fstring) (car flist)))
645 ;; A more complex spec.
647 (list (cons 'format (cons fstring (nreverse flist)))))))
653 (cons 'insert result)))
654 (cond ((stringp result)
657 (cons 'concat result))
660 (defun gnus-eval-format (format &optional alist props)
661 "Eval the format variable FORMAT, using ALIST.
662 If PROPS, insert the result."
663 (let ((form (gnus-parse-format format alist props)))
665 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
668 (defun gnus-compile ()
669 "Byte-compile the user-defined format specs."
672 (let ((entries gnus-format-specs)
673 (byte-compile-warnings '(unresolved callargs redefine))
676 (gnus-message 7 "Compiling format specs...")
679 (setq entry (pop entries))
680 (if (memq (car entry) '(gnus-version version))
681 (setq gnus-format-specs (delq entry gnus-format-specs))
682 (let ((form (caddr entry)))
683 (when (and (listp form)
684 ;; Under GNU Emacs, it's (byte-code ...)
685 (not (eq 'byte-code (car form)))
686 ;; Under XEmacs, it's (funcall #<compiled-function ...>)
687 (not (and (eq 'funcall (car form))
688 (byte-code-function-p (cadr form)))))
689 (defalias 'gnus-tmp-func `(lambda () ,form))
690 (byte-compile 'gnus-tmp-func)
691 (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func))))))
693 (push (cons 'version emacs-version) gnus-format-specs)
694 ;; Mark the .newsrc.eld file as "dirty".
696 (gnus-message 7 "Compiling user specs...done"))))
698 (defun gnus-set-format (type &optional insertable)
699 (set (intern (format "gnus-%s-line-format-spec" type))
701 (symbol-value (intern (format "gnus-%s-line-format" type)))
702 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
708 ;; coding: iso-8859-1
711 ;;; gnus-spec.el ends here