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)
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) ? ))
260 (let ((column (abs column)))
261 (if gnus-use-correct-string-widths
263 (if (> (current-column) ,column)
265 (delete-backward-char 1)
266 (> (current-column) ,column))))
267 (insert (make-string (max (- ,column (current-column)) 0) ? )))
269 (if (> (current-column) ,column)
270 (delete-region (point)
271 (- (point) (- (current-column) ,column)))
272 (insert (make-string (max (- ,column (current-column)) 0)
275 (defun gnus-correct-length (string)
276 "Return the correct width of STRING."
278 (mapcar (lambda (char) (incf length (gnus-char-width char))) string)
281 (defun gnus-correct-substring (string start &optional end)
286 (length (length string))
287 (string (concat string "\0")))
288 ;; Find the start position.
289 (while (and (< seek length)
291 (incf wseek (gnus-char-width (aref string seek)))
294 ;; Find the end position.
295 (while (and (<= seek length)
298 (incf wseek (gnus-char-width (aref string seek)))
301 (substring string wstart (1- wend))))
303 (defun gnus-tilde-max-form (el max-width)
304 "Return a form that limits EL to MAX-WIDTH."
305 (let ((max (abs max-width))
306 (length-fun (if gnus-use-correct-string-widths
309 (substring-fun (if gnus-use-correct-string-widths
310 'gnus-correct-substring
313 `(if (> (,length-fun ,el) ,max)
315 `(,substring-fun ,el (- (,length-fun ,el) ,max))
316 `(,substring-fun ,el 0 ,max))
318 `(let ((val (eval ,el)))
319 (if (> (,length-fun val) ,max)
321 `(,substring-fun val (- (,length-fun val) ,max))
322 `(,substring-fun val 0 ,max))
325 (defun gnus-tilde-cut-form (el cut-width)
326 "Return a form that cuts CUT-WIDTH off of EL."
327 (let ((cut (abs cut-width))
328 (length-fun (if gnus-use-correct-string-widths
331 (substring-fun (if gnus-use-correct-string-widths
332 'gnus-correct-substring
335 `(if (> (,length-fun ,el) ,cut)
337 `(,substring-fun ,el 0 (- (,length-fun ,el) ,cut))
338 `(,substring-fun ,el ,cut))
340 `(let ((val (eval ,el)))
341 (if (> (,length-fun val) ,cut)
343 `(,substring-fun val 0 (- (,length-fun val) ,cut))
344 `(,substring-fun val ,cut))
347 (defun gnus-tilde-ignore-form (el ignore-value)
348 "Return a form that is blank when EL is IGNORE-VALUE."
350 `(if (equal ,el ,ignore-value)
352 `(let ((val (eval ,el)))
353 (if (equal val ,ignore-value)
356 (defun gnus-pad-form (el pad-width)
357 "Return a form that pads EL to PAD-WIDTH accounting for multi-column
358 characters correctly. This is because `format' may pad to columns or to
359 characters when given a pad value."
360 (let ((pad (abs pad-width))
361 (side (< 0 pad-width)))
363 `(let ((need (- ,pad (,(if gnus-use-correct-string-widths
368 (concat ,(when side '(make-string need ?\ ))
370 ,(when (not side) '(make-string need ?\ )))
372 `(let* ((val (eval ,el))
373 (need (- ,pad (,(if gnus-use-correct-string-widths
377 (concat ,(when side '(make-string need ?\ ))
379 ,(when (not side) '(make-string need ?\ )))
382 (defun gnus-parse-format (format spec-alist &optional insert)
383 ;; This function parses the FORMAT string with the help of the
384 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
385 ;; string. If the FORMAT string contains the specifiers %( and %)
386 ;; the text between them will have the mouse-face text property.
387 ;; If the FORMAT string contains the specifiers %[ and %], the text between
388 ;; them will have the balloon-help text property.
389 (let ((case-fold-search nil))
391 "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'\\|%[-0-9]*="
393 (gnus-parse-complex-format format spec-alist)
394 ;; This is a simple format.
395 (gnus-parse-simple-format format spec-alist insert))))
397 (defun gnus-parse-complex-format (format spec-alist)
400 (gnus-set-work-buffer)
402 (goto-char (point-min))
403 (while (re-search-forward "\"" nil t)
404 (replace-match "\\\"" nil t))
405 (goto-char (point-min))
407 ;; Convert all font specs into font spec lists.
408 (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
409 (let ((number (if (match-beginning 1)
410 (match-string 1) "0"))
411 (delim (aref (match-string 2) 0)))
412 (if (or (= delim ?\()
415 (replace-match (concat "\"("
416 (cond ((= delim ?\() "mouse")
417 ((= delim ?\{) "face")
421 (replace-match "\")\""))))
422 (goto-char (point-max))
424 ;; Convert point position commands.
425 (goto-char (point-min))
426 (let ((case-fold-search nil))
427 (while (re-search-forward "%\\([-0-9]+\\)?C" nil t)
428 (replace-match "\"(point)\"" t t)
430 ;; Convert TAB commands.
431 (goto-char (point-min))
432 (while (re-search-forward "%\\([-0-9]+\\)=" nil t)
433 (replace-match (format "\"(tab %s)\"" (match-string 1)) t t))
434 ;; Convert the buffer into the spec.
435 (goto-char (point-min))
436 (let ((form (read (current-buffer))))
438 `(let (gnus-position)
439 ,@(gnus-complex-form-to-spec form spec-alist)
441 (gnus-put-text-property gnus-position (1+ gnus-position)
444 ,@(gnus-complex-form-to-spec form spec-alist)))))))
446 (defun gnus-complex-form-to-spec (form spec-alist)
452 (gnus-parse-simple-format sform spec-alist t))
453 ((eq (car sform) 'point)
454 '(setq gnus-position (point)))
455 ((eq (car sform) 'tab)
456 (gnus-spec-tab (cadr sform)))
458 (funcall (intern (format "gnus-%s-face-function" (car sform)))
459 (gnus-complex-form-to-spec (cddr sform) spec-alist)
463 (defun gnus-parse-simple-format (format spec-alist &optional insert)
464 ;; This function parses the FORMAT string with the help of the
465 ;; SPEC-ALIST and returns a list that can be eval'ed to return a
468 spec flist fstring elem result dontinsert user-defined
469 type value pad-width spec-beg cut-width ignore-value
470 tilde-form tilde elem-type extended-spec)
472 (gnus-set-work-buffer)
474 (goto-char (point-min))
475 (while (re-search-forward "%" nil t)
476 (setq user-defined nil
484 (setq spec-beg (1- (point)))
486 ;; Parse this spec fully.
489 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?")
490 (setq pad-width (string-to-number (match-string 1)))
491 (when (match-beginning 2)
492 (setq max-width (string-to-number (buffer-substring
493 (1+ (match-beginning 2))
495 (goto-char (match-end 0)))
498 (setq tilde (read (current-buffer))
502 ((memq type '(pad pad-left))
503 (setq pad-width value))
504 ((eq type 'pad-right)
505 (setq pad-width (- value)))
506 ((memq type '(max-right max))
507 (setq max-width value))
509 (setq max-width (- value)))
510 ((memq type '(cut cut-left))
511 (setq cut-width value))
512 ((eq type 'cut-right)
513 (setq cut-width (- value)))
516 (if (stringp value) value (format "%s" value))))
518 (setq tilde-form value))
520 (error "Unknown tilde type: %s" tilde)))
525 ;; User-defined spec -- find the spec name.
526 ((eq (setq spec (char-after)) ?u)
528 (when (and (eq (setq user-defined (char-after)) ?&)
529 (looking-at "&\\([^;]+\\);"))
530 (setq user-defined (match-string 1))
531 (goto-char (match-end 1))))
533 ((and (eq spec ?&) (looking-at "&\\([^;]+\\);"))
534 (setq extended-spec (intern (match-string 1)))
535 (goto-char (match-end 1))))
537 (delete-region spec-beg (point))
539 ;; Now we have all the relevant data on this spec, so
540 ;; we start doing stuff.
543 ;; "%%" just results in a "%".
548 (setq elem (list tilde-form ?s)))
549 ;; Treat user defined format specifiers specially.
553 (list (intern (format
554 (if (stringp user-defined)
555 "gnus-user-format-function-%s"
556 "gnus-user-format-function-%c")
560 ;; Find the specification from `spec-alist'.
561 ((setq elem (cdr (assq (or extended-spec spec) spec-alist))))
563 (setq elem '("*" ?s))))
564 (setq elem-type (cadr elem))
565 ;; Insert the new format elements.
567 (not (and (featurep 'xemacs)
568 gnus-use-correct-string-widths)))
569 (insert (number-to-string pad-width)))
570 ;; Create the form to be evaled.
571 (if (or max-width cut-width ignore-value
572 (and (featurep 'xemacs)
573 gnus-use-correct-string-widths))
576 (let ((el (car elem)))
577 (cond ((= (cadr elem) ?c)
578 (setq el (list 'char-to-string el)))
580 (setq el (list 'int-to-string el))))
582 (setq el (gnus-tilde-ignore-form el ignore-value)))
584 (setq el (gnus-tilde-cut-form el cut-width)))
586 (setq el (gnus-tilde-max-form el max-width)))
588 (setq el (gnus-pad-form el pad-width)))
591 (push (car elem) flist))))
592 (setq fstring (buffer-substring-no-properties (point-min) (point-max))))
594 ;; Do some postprocessing to increase efficiency.
599 ((string= fstring "")
601 ;; Not a format string.
602 ((not (string-match "%" fstring))
604 ;; A format string with just a single string spec.
605 ((string= fstring "%s")
607 ;; A single character.
608 ((string= fstring "%c")
611 ((string= fstring "%d")
614 (list `(princ ,(car flist)))
615 (list `(int-to-string ,(car flist)))))
616 ;; Just lots of chars and strings.
617 ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
619 ;; A single string spec at the beginning of the spec.
620 ((string-match "\\`%[sc][^%]+\\'" fstring)
621 (list (car flist) (substring fstring 2)))
622 ;; A single string spec in the middle of the spec.
623 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
624 (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
625 ;; A single string spec in the end of the spec.
626 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
627 (list (match-string 1 fstring) (car flist)))
628 ;; A more complex spec.
630 (list (cons 'format (cons fstring (nreverse flist)))))))
636 (cons 'insert result)))
637 (cond ((stringp result)
640 (cons 'concat result))
643 (defun gnus-eval-format (format &optional alist props)
644 "Eval the format variable FORMAT, using ALIST.
645 If PROPS, insert the result."
646 (let ((form (gnus-parse-format format alist props)))
648 (gnus-add-text-properties (point) (progn (eval form) (point)) props)
651 (defun gnus-compile ()
652 "Byte-compile the user-defined format specs."
655 (let ((entries gnus-format-specs)
656 (byte-compile-warnings '(unresolved callargs redefine))
659 (gnus-message 7 "Compiling format specs...")
662 (setq entry (pop entries))
663 (if (memq (car entry) '(gnus-version version))
664 (setq gnus-format-specs (delq entry gnus-format-specs))
665 (let ((form (caddr entry)))
666 (when (and (listp form)
667 ;; Under GNU Emacs, it's (byte-code ...)
668 (not (eq 'byte-code (car form)))
669 ;; Under XEmacs, it's (funcall #<compiled-function ...>)
670 (not (and (eq 'funcall (car form))
671 (byte-code-function-p (cadr form)))))
672 (defalias 'gnus-tmp-func `(lambda () ,form))
673 (byte-compile 'gnus-tmp-func)
674 (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func))))))
676 (push (cons 'version emacs-version) gnus-format-specs)
677 ;; Mark the .newsrc.eld file as "dirty".
679 (gnus-message 7 "Compiling user specs...done"))))
681 (defun gnus-set-format (type &optional insertable)
682 (set (intern (format "gnus-%s-line-format-spec" type))
684 (symbol-value (intern (format "gnus-%s-line-format" type)))
685 (symbol-value (intern (format "gnus-%s-line-format-alist" type)))
691 ;; coding: iso-8859-1
694 ;;; gnus-spec.el ends here