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