1 ;;; gnus-spec.el --- format spec functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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 (defcustom gnus-make-format-preserve-properties (featurep 'xemacs)
39 "*If non-nil, use a replacement `format' function which preserves
40 text properties. This is only needed on XEmacs, as FSF Emacs does this anyway."
44 ;;; Internal variables.
46 (defvar gnus-summary-mark-positions nil)
47 (defvar gnus-group-mark-positions nil)
48 (defvar gnus-group-indentation "")
50 ;; Format specs. The chunks below are the machine-generated forms
51 ;; that are to be evaled as the result of the default format strings.
52 ;; We write them in here to get them byte-compiled. That way the
53 ;; default actions will be quite fast, while still retaining the full
54 ;; flexibility of the user-defined format specs.
56 ;; First we have lots of dummy defvars to let the compiler know these
57 ;; are really dynamic variables.
59 (defvar gnus-tmp-unread)
60 (defvar gnus-tmp-replied)
61 (defvar gnus-tmp-score-char)
62 (defvar gnus-tmp-indentation)
63 (defvar gnus-tmp-opening-bracket)
64 (defvar gnus-tmp-lines)
65 (defvar gnus-tmp-name)
66 (defvar gnus-tmp-closing-bracket)
67 (defvar gnus-tmp-subject-or-nil)
68 (defvar gnus-tmp-subject)
69 (defvar gnus-tmp-marked)
70 (defvar gnus-tmp-marked-mark)
71 (defvar gnus-tmp-subscribed)
72 (defvar gnus-tmp-process-marked)
73 (defvar gnus-tmp-number-of-unread)
74 (defvar gnus-tmp-group-name)
75 (defvar gnus-tmp-group)
76 (defvar gnus-tmp-article-number)
77 (defvar gnus-tmp-unread-and-unselected)
78 (defvar gnus-tmp-news-method)
79 (defvar gnus-tmp-news-server)
80 (defvar gnus-tmp-article-number)
81 (defvar gnus-mouse-face)
82 (defvar gnus-mouse-face-prop)
83 (defvar gnus-tmp-header)
84 (defvar gnus-tmp-from)
86 (defun gnus-summary-line-format-spec ()
87 (insert gnus-tmp-unread gnus-tmp-replied
88 gnus-tmp-score-char gnus-tmp-indentation)
89 (gnus-put-text-property
93 (format "%c%4s: %-23s%c" gnus-tmp-opening-bracket gnus-tmp-lines
96 (gnus-summary-from-or-to-or-newsgroups
97 gnus-tmp-header gnus-tmp-from))))
98 (if (> (length val) 23)
101 gnus-tmp-closing-bracket))
103 gnus-mouse-face-prop gnus-mouse-face)
104 (insert " " gnus-tmp-subject-or-nil "\n"))
106 (defvar gnus-summary-line-format-spec
107 (gnus-byte-code 'gnus-summary-line-format-spec))
109 (defun gnus-summary-dummy-line-format-spec ()
111 (gnus-put-text-property
116 gnus-mouse-face-prop gnus-mouse-face)
117 (insert " " gnus-tmp-subject "\n"))
119 (defvar gnus-summary-dummy-line-format-spec
120 (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
122 (defun gnus-group-line-format-spec ()
123 (insert gnus-tmp-marked-mark gnus-tmp-subscribed
124 gnus-tmp-process-marked
125 gnus-group-indentation
126 (format "%5s: " gnus-tmp-number-of-unread))
127 (gnus-put-text-property
130 (insert gnus-tmp-group "\n")
132 gnus-mouse-face-prop gnus-mouse-face))
133 (defvar gnus-group-line-format-spec
134 (gnus-byte-code 'gnus-group-line-format-spec))
136 (defvar gnus-format-specs
137 `((version . ,emacs-version)
138 (gnus-version . ,(gnus-continuum-version))
139 (group "%M\%S\%p\%P\%5y: %(%g%)%l\n" ,gnus-group-line-format-spec)
140 (summary-dummy "* %(: :%) %S\n"
141 ,gnus-summary-dummy-line-format-spec)
142 (summary "%U%R%z%I%(%[%4L: %-23,23f%]%) %s\n"
143 ,gnus-summary-line-format-spec))
144 "Alist of format specs.")
146 (defvar gnus-default-format-specs gnus-format-specs)
148 (defvar gnus-article-mode-line-format-spec nil)
149 (defvar gnus-summary-mode-line-format-spec nil)
150 (defvar gnus-group-mode-line-format-spec nil)
152 ;;; Phew. All that gruft is over with, fortunately.
155 (defun gnus-update-format (var)
156 "Update the format specification near point."
161 ;; Find the end of the current word.
162 (re-search-forward "[ \t\n]" nil t)
164 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
166 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
167 (match-string 1 var))))
168 (entry (assq type gnus-format-specs))
171 (setq gnus-format-specs (delq entry gnus-format-specs)))
173 (intern (format "%s-spec" var))
174 (gnus-parse-format (setq value (symbol-value (intern var)))
175 (symbol-value (intern (format "%s-alist" var)))
176 (not (string-match "mode" var))))
177 (setq spec (symbol-value (intern (format "%s-spec" var))))
178 (push (list type value spec) gnus-format-specs)
180 (pop-to-buffer "*Gnus Format*")
182 (lisp-interaction-mode)
183 (insert (pp-to-string spec))))
185 (defun gnus-update-format-specifications (&optional force &rest types)
186 "Update all (necessary) format specifications."
187 ;; Make the indentation array.
188 ;; See whether all the stored info needs to be flushed.
190 (not gnus-newsrc-file-version)
191 (not (equal (gnus-continuum-version)
192 (gnus-continuum-version gnus-newsrc-file-version)))
193 (not (equal emacs-version
194 (cdr (assq 'version gnus-format-specs)))))
195 (setq gnus-format-specs nil))
197 ;; Go through all the formats and see whether they need updating.
198 (let (new-format entry type val)
199 (while (setq type (pop types))
200 ;; Jump to the proper buffer to find out the value of the
201 ;; variable, if possible. (It may be buffer-local.)
203 (let ((buffer (intern (format "gnus-%s-buffer" type)))
205 (when (and (boundp buffer)
206 (setq val (symbol-value buffer))
207 (gnus-buffer-exists-p val))
209 (setq new-format (symbol-value
210 (intern (format "gnus-%s-line-format" type)))))
211 (setq entry (cdr (assq type gnus-format-specs)))
213 (equal (car entry) new-format))
214 ;; Use the old format.
215 (set (intern (format "gnus-%s-line-format-spec" type))
217 ;; This is a new format.
219 (if (not (stringp new-format))
220 ;; This is a function call or something.
222 ;; This is a "real" format.
226 (intern (format "gnus-%s-line-format-alist" type)))
227 (not (string-match "mode$" (symbol-name type))))))
228 ;; Enter the new format spec into the list.
231 (setcar (cdr entry) val)
232 (setcar entry new-format))
233 (push (list type new-format val) gnus-format-specs))
234 (set (intern (format "gnus-%s-line-format-spec" type)) val)))))
236 (unless (assq 'version gnus-format-specs)
237 (push (cons 'version emacs-version) gnus-format-specs)))
239 (defvar gnus-mouse-face-0 'highlight)
240 (defvar gnus-mouse-face-1 'highlight)
241 (defvar gnus-mouse-face-2 'highlight)
242 (defvar gnus-mouse-face-3 'highlight)
243 (defvar gnus-mouse-face-4 'highlight)
245 (defun gnus-mouse-face-function (form type)
246 `(gnus-put-text-property
247 (point) (progn ,@form (point))
251 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
253 (defvar gnus-face-0 'bold)
254 (defvar gnus-face-1 'italic)
255 (defvar gnus-face-2 'bold-italic)
256 (defvar gnus-face-3 'bold)
257 (defvar gnus-face-4 'bold)
259 (defun gnus-face-face-function (form type)
260 `(gnus-add-text-properties
261 (point) (progn ,@form (point))
262 '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
264 (defun gnus-balloon-face-function (form type)
265 `(gnus-put-text-property
266 (point) (progn ,@form (point))
267 ,(if (fboundp 'balloon-help-mode)
270 ,(intern (format "gnus-balloon-face-%d" type))))
272 (defun gnus-spec-tab (column)
274 `(insert (make-string (max (- ,column (current-column)) 0) ? ))
275 (let ((column (abs column)))
276 (if gnus-use-correct-string-widths
278 (if (> (current-column) ,column)
280 (delete-backward-char 1)
281 (> (current-column) ,column))))
282 (insert (make-string (max (- ,column (current-column)) 0) ? )))
284 (if (> (current-column) ,column)
285 (delete-region (point)
286 (- (point) (- (current-column) ,column)))
287 (insert (make-string (max (- ,column (current-column)) 0)
290 (defun gnus-correct-length (string)
291 "Return the correct width of STRING."
293 (mapcar (lambda (char) (incf length (char-width char))) string)
296 (defun gnus-correct-substring (string start &optional end)
301 (length (length string))
302 (string (concat string "\0")))
303 ;; Find the start position.
304 (while (and (< seek length)
306 (incf wseek (char-width (aref string seek)))
309 ;; Find the end position.
310 (while (and (<= seek length)
313 (incf wseek (char-width (aref string seek)))
316 (substring string wstart (1- wend))))
318 (defun gnus-string-width-function ()