Merge from emacs--rel--22
[bpt/emacs.git] / lisp / gnus / gnus-spec.el
CommitLineData
23f87bed 1;;; gnus-spec.el --- format spec functions for Gnus
e84b4b86
TTN
2
3;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
d7a0267c 4;; 2005, 2006, 2007 Free Software Foundation, Inc.
eec82323 5
6748645f 6;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
eec82323
LMI
7;; Keywords: news
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
eec82323
LMI
25
26;;; Commentary:
27
28;;; Code:
29
5ab7173c 30(eval-when-compile (require 'cl))
139bbb9c 31(defvar gnus-newsrc-file-version)
5ab7173c 32
eec82323
LMI
33(require 'gnus)
34
23f87bed
MB
35(defcustom gnus-use-correct-string-widths (featurep 'xemacs)
36 "*If non-nil, use correct functions for dealing with wide characters."
bf247b6e 37 :version "22.1"
23f87bed
MB
38 :group 'gnus-format
39 :type 'boolean)
40
41(defcustom gnus-make-format-preserve-properties (featurep 'xemacs)
42 "*If non-nil, use a replacement `format' function which preserves
43text properties. This is only needed on XEmacs, as FSF Emacs does this anyway."
bf247b6e 44 :version "22.1"
23f87bed
MB
45 :group 'gnus-format
46 :type 'boolean)
47
eec82323
LMI
48;;; Internal variables.
49
50(defvar gnus-summary-mark-positions nil)
51(defvar gnus-group-mark-positions nil)
52(defvar gnus-group-indentation "")
53
54;; Format specs. The chunks below are the machine-generated forms
55;; that are to be evaled as the result of the default format strings.
56;; We write them in here to get them byte-compiled. That way the
57;; default actions will be quite fast, while still retaining the full
58;; flexibility of the user-defined format specs.
59
60;; First we have lots of dummy defvars to let the compiler know these
61;; are really dynamic variables.
62
63(defvar gnus-tmp-unread)
64(defvar gnus-tmp-replied)
65(defvar gnus-tmp-score-char)
66(defvar gnus-tmp-indentation)
67(defvar gnus-tmp-opening-bracket)
68(defvar gnus-tmp-lines)
69(defvar gnus-tmp-name)
70(defvar gnus-tmp-closing-bracket)
71(defvar gnus-tmp-subject-or-nil)
72(defvar gnus-tmp-subject)
73(defvar gnus-tmp-marked)
74(defvar gnus-tmp-marked-mark)
75(defvar gnus-tmp-subscribed)
76(defvar gnus-tmp-process-marked)
77(defvar gnus-tmp-number-of-unread)
78(defvar gnus-tmp-group-name)
79(defvar gnus-tmp-group)
80(defvar gnus-tmp-article-number)
81(defvar gnus-tmp-unread-and-unselected)
82(defvar gnus-tmp-news-method)
83(defvar gnus-tmp-news-server)
84(defvar gnus-tmp-article-number)
85(defvar gnus-mouse-face)
86(defvar gnus-mouse-face-prop)
23f87bed
MB
87(defvar gnus-tmp-header)
88(defvar gnus-tmp-from)
eec82323
LMI
89
90(defun gnus-summary-line-format-spec ()
91 (insert gnus-tmp-unread gnus-tmp-replied
92 gnus-tmp-score-char gnus-tmp-indentation)
93 (gnus-put-text-property
94 (point)
95 (progn
96 (insert
23f87bed
MB
97 (format "%c%4s: %-23s%c" gnus-tmp-opening-bracket gnus-tmp-lines
98 (let ((val
99 (inline
100 (gnus-summary-from-or-to-or-newsgroups
101 gnus-tmp-header gnus-tmp-from))))
102 (if (> (length val) 23)
103 (substring val 0 23)
104 val))
105 gnus-tmp-closing-bracket))
eec82323
LMI
106 (point))
107 gnus-mouse-face-prop gnus-mouse-face)
108 (insert " " gnus-tmp-subject-or-nil "\n"))
109
110(defvar gnus-summary-line-format-spec
111 (gnus-byte-code 'gnus-summary-line-format-spec))
112
113(defun gnus-summary-dummy-line-format-spec ()
114 (insert "* ")
115 (gnus-put-text-property
116 (point)
117 (progn
118 (insert ": :")
119 (point))
120 gnus-mouse-face-prop gnus-mouse-face)
121 (insert " " gnus-tmp-subject "\n"))
122
123(defvar gnus-summary-dummy-line-format-spec
124 (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
125
126(defun gnus-group-line-format-spec ()
127 (insert gnus-tmp-marked-mark gnus-tmp-subscribed
128 gnus-tmp-process-marked
129 gnus-group-indentation
130 (format "%5s: " gnus-tmp-number-of-unread))
131 (gnus-put-text-property
132 (point)
133 (progn
134 (insert gnus-tmp-group "\n")
135 (1- (point)))
136 gnus-mouse-face-prop gnus-mouse-face))
137(defvar gnus-group-line-format-spec
138 (gnus-byte-code 'gnus-group-line-format-spec))
139
140(defvar gnus-format-specs
141 `((version . ,emacs-version)
23f87bed 142 (gnus-version . ,(gnus-continuum-version))
eec82323
LMI
143 (group "%M\%S\%p\%P\%5y: %(%g%)%l\n" ,gnus-group-line-format-spec)
144 (summary-dummy "* %(: :%) %S\n"
145 ,gnus-summary-dummy-line-format-spec)
23f87bed 146 (summary "%U%R%z%I%(%[%4L: %-23,23f%]%) %s\n"
eec82323
LMI
147 ,gnus-summary-line-format-spec))
148 "Alist of format specs.")
149
23f87bed
MB
150(defvar gnus-default-format-specs gnus-format-specs)
151
eec82323
LMI
152(defvar gnus-article-mode-line-format-spec nil)
153(defvar gnus-summary-mode-line-format-spec nil)
154(defvar gnus-group-mode-line-format-spec nil)
155
23f87bed 156;;; Phew. All that gruft is over with, fortunately.
eec82323
LMI
157
158;;;###autoload
159(defun gnus-update-format (var)
160 "Update the format specification near point."
161 (interactive
162 (list
163 (save-excursion
164 (eval-defun nil)
165 ;; Find the end of the current word.
166 (re-search-forward "[ \t\n]" nil t)
167 ;; Search backward.
168 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
169 (match-string 1)))))
170 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
171 (match-string 1 var))))
172 (entry (assq type gnus-format-specs))
173 value spec)
174 (when entry
175 (setq gnus-format-specs (delq entry gnus-format-specs)))
176 (set
177 (intern (format "%s-spec" var))
178 (gnus-parse-format (setq value (symbol-value (intern var)))
179 (symbol-value (intern (format "%s-alist" var)))
180 (not (string-match "mode" var))))
181 (setq spec (symbol-value (intern (format "%s-spec" var))))
182 (push (list type value spec) gnus-format-specs)
183
184 (pop-to-buffer "*Gnus Format*")
185 (erase-buffer)
186 (lisp-interaction-mode)
23f87bed 187 (insert (gnus-pp-to-string spec))))
eec82323
LMI
188
189(defun gnus-update-format-specifications (&optional force &rest types)
5153a47a
MB
190 "Update all (necessary) format specifications.
191Return a list of updated types."
eec82323
LMI
192 ;; Make the indentation array.
193 ;; See whether all the stored info needs to be flushed.
194 (when (or force
23f87bed
MB
195 (not gnus-newsrc-file-version)
196 (not (equal (gnus-continuum-version)
197 (gnus-continuum-version gnus-newsrc-file-version)))
eec82323
LMI
198 (not (equal emacs-version
199 (cdr (assq 'version gnus-format-specs)))))
200 (setq gnus-format-specs nil))
91472578
MB
201 ;; Flush the group format spec cache if it doesn't support decoded
202 ;; group names.
203 (when (memq 'group types)
204 (let ((spec (assq 'group gnus-format-specs)))
205 (unless (string-match " gnus-tmp-decoded-group[ )]"
206 (gnus-prin1-to-string (nth 2 spec)))
207 (setq gnus-format-specs (delq spec gnus-format-specs)))))
eec82323
LMI
208
209 ;; Go through all the formats and see whether they need updating.
5153a47a 210 (let (new-format entry type val updated)
eec82323 211 (while (setq type (pop types))
23f87bed
MB
212 ;; Jump to the proper buffer to find out the value of the
213 ;; variable, if possible. (It may be buffer-local.)
eec82323 214 (save-excursion
5153a47a 215 (let ((buffer (intern (format "gnus-%s-buffer" type))))
eec82323
LMI
216 (when (and (boundp buffer)
217 (setq val (symbol-value buffer))
6748645f
LMI
218 (gnus-buffer-exists-p val))
219 (set-buffer val))
eec82323
LMI
220 (setq new-format (symbol-value
221 (intern (format "gnus-%s-line-format" type)))))
222 (setq entry (cdr (assq type gnus-format-specs)))
223 (if (and (car entry)
224 (equal (car entry) new-format))
225 ;; Use the old format.
226 (set (intern (format "gnus-%s-line-format-spec" type))
227 (cadr entry))
228 ;; This is a new format.
229 (setq val
230 (if (not (stringp new-format))
231 ;; This is a function call or something.
232 new-format
233 ;; This is a "real" format.
234 (gnus-parse-format
235 new-format
236 (symbol-value
16409b0b 237 (intern (format "gnus-%s-line-format-alist" type)))
eec82323
LMI
238 (not (string-match "mode$" (symbol-name type))))))
239 ;; Enter the new format spec into the list.
240 (if entry
241 (progn
242 (setcar (cdr entry) val)
243 (setcar entry new-format))
244 (push (list type new-format val) gnus-format-specs))
5153a47a
MB
245 (set (intern (format "gnus-%s-line-format-spec" type)) val)
246 (push type updated))))
eec82323 247
5153a47a
MB
248 (unless (assq 'version gnus-format-specs)
249 (push (cons 'version emacs-version) gnus-format-specs))
250 updated))
eec82323
LMI
251
252(defvar gnus-mouse-face-0 'highlight)
253(defvar gnus-mouse-face-1 'highlight)
254(defvar gnus-mouse-face-2 'highlight)
255(defvar gnus-mouse-face-3 'highlight)
256(defvar gnus-mouse-face-4 'highlight)
257
258(defun gnus-mouse-face-function (form type)
259 `(gnus-put-text-property
260 (point) (progn ,@form (point))
261 gnus-mouse-face-prop
262 ,(if (equal type 0)
263 'gnus-mouse-face
264 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
265
266(defvar gnus-face-0 'bold)
267(defvar gnus-face-1 'italic)
268(defvar gnus-face-2 'bold-italic)
269(defvar gnus-face-3 'bold)
270(defvar gnus-face-4 'bold)
271
272(defun gnus-face-face-function (form type)
6748645f 273 `(gnus-add-text-properties
eec82323 274 (point) (progn ,@form (point))
6748645f 275 '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
eec82323 276
16409b0b
GM
277(defun gnus-balloon-face-function (form type)
278 `(gnus-put-text-property
279 (point) (progn ,@form (point))
23f87bed
MB
280 ,(if (fboundp 'balloon-help-mode)
281 ''balloon-help
282 ''help-echo)
16409b0b
GM
283 ,(intern (format "gnus-balloon-face-%d" type))))
284
23f87bed
MB
285(defun gnus-spec-tab (column)
286 (if (> column 0)
aa0a8561 287 `(insert-char ? (max (- ,column (current-column)) 0))
23f87bed 288 (let ((column (abs column)))
aa0a8561
MB
289 `(if (> (current-column) ,column)
290 (let ((end (point)))
291 (if (= (move-to-column ,column) ,column)
292 (delete-region (point) end)
293 (delete-region (1- (point)) end)
294 (insert " ")))
295 (insert-char ? (max (- ,column (current-column)) 0))))))
23f87bed
MB
296
297(defun gnus-correct-length (string)
298 "Return the correct width of STRING."
299 (let ((length 0))
300 (mapcar (lambda (char) (incf length (gnus-char-width char))) string)
301 length))
302
303(defun gnus-correct-substring (string start &optional end)
304 (let ((wstart 0)
305 (wend 0)
306 (wseek 0)
307 (seek 0)
308 (length (length string))
309 (string (concat string "\0")))
310 ;; Find the start position.
311 (while (and (< seek length)
312 (< wseek start))
313 (incf wseek (gnus-char-width (aref string seek)))
314 (incf seek))
315 (setq wstart seek)
316 ;; Find the end position.
317 (while (and (<= seek length)
318 (or (not end)
319 (<= wseek end)))
320 (incf wseek (gnus-char-width (aref string seek)))
321 (incf seek))
322 (setq wend seek)
323 (substring string wstart (1- wend))))
324
325(defun gnus-string-width-function ()
326 (cond
327 (gnus-use-correct-string-widths
328 'gnus-correct-length)
329 ((fboundp 'string-width)
330 'string-width)
331 (t
332 'length)))
333
334(defun gnus-substring-function ()
335 (cond
336 (gnus-use-correct-string-widths
337 'gnus-correct-substring)
338 ((fboundp 'string-width)
339 'gnus-correct-substring)
340 (t
341 'substring)))
342
eec82323
LMI
343(defun gnus-tilde-max-form (el max-width)
344 "Return a form that limits EL to MAX-WIDTH."
23f87bed
MB
345 (let ((max (abs max-width))
346 (length-fun (gnus-string-width-function))
347 (substring-fun (gnus-substring-function)))
eec82323 348 (if (symbolp el)
23f87bed 349 `(if (> (,length-fun ,el) ,max)
eec82323 350 ,(if (< max-width 0)
23f87bed
MB
351 `(,substring-fun ,el (- (,length-fun ,el) ,max))
352 `(,substring-fun ,el 0 ,max))
eec82323
LMI
353 ,el)
354 `(let ((val (eval ,el)))
23f87bed 355 (if (> (,length-fun val) ,max)
eec82323 356 ,(if (< max-width 0)
23f87bed
MB
357 `(,substring-fun val (- (,length-fun val) ,max))
358 `(,substring-fun val 0 ,max))
eec82323
LMI
359 val)))))
360
361(defun gnus-tilde-cut-form (el cut-width)
362 "Return a form that cuts CUT-WIDTH off of EL."
23f87bed
MB
363 (let ((cut (abs cut-width))
364 (length-fun (gnus-string-width-function))
365 (substring-fun (gnus-substring-function)))
eec82323 366 (if (symbolp el)
23f87bed 367 `(if (> (,length-fun ,el) ,cut)
eec82323 368 ,(if (< cut-width 0)
23f87bed
MB
369 `(,substring-fun ,el 0 (- (,length-fun ,el) ,cut))
370 `(,substring-fun ,el ,cut))
eec82323
LMI
371 ,el)
372 `(let ((val (eval ,el)))
23f87bed 373 (if (> (,length-fun val) ,cut)
eec82323 374 ,(if (< cut-width 0)
23f87bed
MB
375 `(,substring-fun val 0 (- (,length-fun val) ,cut))
376 `(,substring-fun val ,cut))
eec82323
LMI
377 val)))))
378
379(defun gnus-tilde-ignore-form (el ignore-value)
380 "Return a form that is blank when EL is IGNORE-VALUE."
381 (if (symbolp el)
382 `(if (equal ,el ,ignore-value)
383 "" ,el)
384 `(let ((val (eval ,el)))
385 (if (equal val ,ignore-value)
386 "" val))))
387
23f87bed
MB
388(defun gnus-pad-form (el pad-width)
389 "Return a form that pads EL to PAD-WIDTH accounting for multi-column
390characters correctly. This is because `format' may pad to columns or to
391characters when given a pad value."
392 (let ((pad (abs pad-width))
393 (side (< 0 pad-width))
394 (length-fun (gnus-string-width-function)))
395 (if (symbolp el)
396 `(let ((need (- ,pad (,length-fun ,el))))
397 (if (> need 0)
398 (concat ,(when side '(make-string need ?\ ))
399 ,el
400 ,(when (not side) '(make-string need ?\ )))
401 ,el))
402 `(let* ((val (eval ,el))
403 (need (- ,pad (,length-fun val))))
404 (if (> need 0)
405 (concat ,(when side '(make-string need ?\ ))
406 val
407 ,(when (not side) '(make-string need ?\ )))
408 val)))))
409
eec82323
LMI
410(defun gnus-parse-format (format spec-alist &optional insert)
411 ;; This function parses the FORMAT string with the help of the
412 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
413 ;; string. If the FORMAT string contains the specifiers %( and %)
414 ;; the text between them will have the mouse-face text property.
16409b0b
GM
415 ;; If the FORMAT string contains the specifiers %[ and %], the text between
416 ;; them will have the balloon-help text property.
23f87bed
MB
417 (let ((case-fold-search nil))
418 (if (string-match
419