Merge from emacs-24; up to 2012-05-08T14:11:47Z!monnier@iro.umontreal.ca
[bpt/emacs.git] / lisp / gnus / gnus-spec.el
CommitLineData
23f87bed 1;;; gnus-spec.el --- format spec functions for Gnus
e84b4b86 2
acaf905b 3;; Copyright (C) 1996-2012 Free Software Foundation, Inc.
eec82323 4
6748645f 5;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
eec82323
LMI
6;; Keywords: news
7
8;; This file is part of GNU Emacs.
9
5e809f55 10;; GNU Emacs is free software: you can redistribute it and/or modify
eec82323 11;; it under the terms of the GNU General Public License as published by
5e809f55
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
eec82323
LMI
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
5e809f55 17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
eec82323
LMI
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
5e809f55 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
eec82323
LMI
22
23;;; Commentary:
24
25;;; Code:
26
f0b7f5a8 27;; For Emacs <22.2 and XEmacs.
2d1974c9
GM
28(eval-and-compile
29 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
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
0985c8f6 43text properties. This is only needed on XEmacs, as 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
99d99081 55;; that are to be evalled as the result of the default format strings.
eec82323
LMI
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 89
2d1974c9
GM
90(declare-function gnus-summary-from-or-to-or-newsgroups "gnus-sum"
91 (header gnus-tmp-from))
92
c7b98a1c 93(defmacro gnus-lrm-string-p (string)
e21bac42
G
94 (if (fboundp 'bidi-string-mark-left-to-right)
95 ;; LRM, RLM, PDF characters as integers to avoid breaking Emacs
96 ;; 23.
97 `(memq (aref ,string (1- (length ,string))) '(8206 8207 8236))
c7b98a1c
G
98 nil))
99
100(defvar gnus-lrm-string (if (ignore-errors (string 8206))
101 (propertize (string 8206) 'invisible t)
102 ""))
103
89b163db
G
104(defvar gnus-summary-line-format-spec nil)
105(defvar gnus-summary-dummy-line-format-spec nil)
106(defvar gnus-group-line-format-spec nil)
eec82323
LMI
107
108(defvar gnus-format-specs
109 `((version . ,emacs-version)
89b163db 110 (gnus-version . ,(gnus-continuum-version)))
eec82323
LMI
111 "Alist of format specs.")
112
23f87bed
MB
113(defvar gnus-default-format-specs gnus-format-specs)
114
eec82323
LMI
115(defvar gnus-article-mode-line-format-spec nil)
116(defvar gnus-summary-mode-line-format-spec nil)
117(defvar gnus-group-mode-line-format-spec nil)
118
23f87bed 119;;; Phew. All that gruft is over with, fortunately.
eec82323
LMI
120
121;;;###autoload
122(defun gnus-update-format (var)
123 "Update the format specification near point."
124 (interactive
125 (list
126 (save-excursion
127 (eval-defun nil)
128 ;; Find the end of the current word.
129 (re-search-forward "[ \t\n]" nil t)
130 ;; Search backward.
131 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
132 (match-string 1)))))
133 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
134 (match-string 1 var))))
135 (entry (assq type gnus-format-specs))
136 value spec)
137 (when entry
138 (setq gnus-format-specs (delq entry gnus-format-specs)))
139 (set
140 (intern (format "%s-spec" var))
141 (gnus-parse-format (setq value (symbol-value (intern var)))
142 (symbol-value (intern (format "%s-alist" var)))
143 (not (string-match "mode" var))))
144 (setq spec (symbol-value (intern (format "%s-spec" var))))
145 (push (list type value spec) gnus-format-specs)
146
147 (pop-to-buffer "*Gnus Format*")
148 (erase-buffer)
149 (lisp-interaction-mode)
23f87bed 150 (insert (gnus-pp-to-string spec))))
eec82323
LMI
151
152(defun gnus-update-format-specifications (&optional force &rest types)
5153a47a
MB
153 "Update all (necessary) format specifications.
154Return a list of updated types."
eec82323
LMI
155 ;; Make the indentation array.
156 ;; See whether all the stored info needs to be flushed.
157 (when (or force
23f87bed
MB
158 (not gnus-newsrc-file-version)
159 (not (equal (gnus-continuum-version)
160 (gnus-continuum-version gnus-newsrc-file-version)))
eec82323
LMI
161 (not (equal emacs-version
162 (cdr (assq 'version gnus-format-specs)))))
163 (setq gnus-format-specs nil))
eec82323 164 ;; Go through all the formats and see whether they need updating.
5153a47a 165 (let (new-format entry type val updated)
eec82323 166 (while (setq type (pop types))
23f87bed
MB
167 ;; Jump to the proper buffer to find out the value of the
168 ;; variable, if possible. (It may be buffer-local.)
eec82323 169 (save-excursion
5153a47a 170 (let ((buffer (intern (format "gnus-%s-buffer" type))))
eec82323
LMI
171 (when (and (boundp buffer)
172 (setq val (symbol-value buffer))
6748645f
LMI
173 (gnus-buffer-exists-p val))
174 (set-buffer val))
eec82323
LMI
175 (setq new-format (symbol-value
176 (intern (format "gnus-%s-line-format" type)))))
177 (setq entry (cdr (assq type gnus-format-specs)))
178 (if (and (car entry)
179 (equal (car entry) new-format))
180 ;; Use the old format.
181 (set (intern (format "gnus-%s-line-format-spec" type))
182 (cadr entry))
183 ;; This is a new format.
184 (setq val
185 (if (not (stringp new-format))
186 ;; This is a function call or something.
187 new-format
188 ;; This is a "real" format.
189 (gnus-parse-format
190 new-format
191 (symbol-value
16409b0b 192 (intern (format "gnus-%s-line-format-alist" type)))
eec82323
LMI
193 (not (string-match "mode$" (symbol-name type))))))
194 ;; Enter the new format spec into the list.
195 (if entry
196 (progn
197 (setcar (cdr entry) val)
198 (setcar entry new-format))
199 (push (list type new-format val) gnus-format-specs))
5153a47a
MB
200 (set (intern (format "gnus-%s-line-format-spec" type)) val)
201 (push type updated))))
eec82323 202
5153a47a
MB
203 (unless (assq 'version gnus-format-specs)
204 (push (cons 'version emacs-version) gnus-format-specs))
205 updated))
eec82323 206
052bd38a
LMI
207(defcustom gnus-mouse-face-0 'highlight
208 "The \"%(hello%)\" face."
209 :group 'gnus-format
210 :type 'face)
211
212(defcustom gnus-mouse-face-1 'highlight
213 "The \"%1(hello%)\" face."
214 :group 'gnus-format
215 :type 'face)
216
217(defcustom gnus-mouse-face-2 'highlight
218 "The \"%2(hello%)\" face."
219 :group 'gnus-format
220 :type 'face)
221
222(defcustom gnus-mouse-face-3 'highlight
223 "The \"%3(hello%)\" face."
224 :group 'gnus-format
225 :type 'face)
226
227(defcustom gnus-mouse-face-4 'highlight
228 "The \"%4(hello%)\" face."
229 :group 'gnus-format
230 :type 'face)
eec82323
LMI
231
232(defun gnus-mouse-face-function (form type)
233 `(gnus-put-text-property
234 (point) (progn ,@form (point))
235 gnus-mouse-face-prop
236 ,(if (equal type 0)
237 'gnus-mouse-face
238 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
239
052bd38a
LMI
240(defcustom gnus-face-0 'bold
241 "The \"%{hello%}\" face."
242 :group 'gnus-format
243 :type 'face)
244
245(defcustom gnus-face-1 'italic
246 "The \"%1{hello%}\" face."
247 :group 'gnus-format
248 :type 'face)
249
250(defcustom gnus-face-2 'bold-italic
251 "The \"%2{hello%}\" face."
252 :group 'gnus-format
253 :type 'face)
254
255(defcustom gnus-face-3 'bold
256 "The \"%3{hello%}\" face."
257 :group 'gnus-format
258 :type 'face)
259
260(defcustom gnus-face-4 'bold
261 "The \"%4{hello%}\" face."
262 :group 'gnus-format
263 :type 'face)
eec82323
LMI
264
265(defun gnus-face-face-function (form type)
6748645f 266 `(gnus-add-text-properties
eec82323 267 (point) (progn ,@form (point))
6748645f 268 '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
eec82323 269
16409b0b
GM
270(defun gnus-balloon-face-function (form type)
271 `(gnus-put-text-property
272 (point) (progn ,@form (point))
23f87bed
MB
273 ,(if (fboundp 'balloon-help-mode)
274 ''balloon-help
275 ''help-echo)
16409b0b
GM
276 ,(intern (format "gnus-balloon-face-%d" type))))
277
23f87bed
MB
278(defun gnus-spec-tab (column)
279 (if (> column 0)
aa0a8561 280 `(insert-char ? (max (- ,column (current-column)) 0))
23f87bed 281 (let ((column (abs column)))
aa0a8561
MB
282 `(if (> (current-column) ,column)
283 (let ((end (point)))
284 (if (= (move-to-column ,column) ,column)
285 (delete-region (point) end)
286 (delete-region (1- (point)) end)
287 (insert " ")))
288 (insert-char ? (max (- ,column (current-column)) 0))))))
23f87bed
MB
289
290(defun gnus-correct-length (string)
291 "Return the correct width of STRING."
01c52d31 292 (apply #'+ (mapcar #'char-width string)))
23f87bed
MB
293
294(defun gnus-correct-substring (string start &optional end)
295 (let ((wstart 0)
296 (wend 0)
297 (wseek 0)
298 (seek 0)
299 (length (length string))
300 (string (concat string "\0")))
301 ;; Find the start position.
302 (while (and (< seek length)
303 (< wseek start))
01c52d31 304 (incf wseek (char-width (aref string seek)))
23f87bed
MB
305 (incf seek))
306 (setq wstart seek)
307 ;; Find the end position.
308 (while (and (<= seek length)
309 (or (not end)
310 (<= wseek end)))
01c52d31 311 (incf wseek (char-width (aref string seek)))
23f87bed
MB
312 (incf seek))
313 (setq wend seek)
314 (substring string wstart (1- wend))))
315
316(defun gnus-string-width-function ()
317 (cond
318 (gnus-use-correct-string-widths
319 'gnus-correct-length)
320 ((fboundp 'string-width)
321 'string-width)
322 (t
323 'length)))
324
325(defun gnus-substring-function ()
326 (cond
327 (gnus-use-correct-string-widths
328 'gnus-correct-substring)
329 ((fboundp 'string-width)
330 'gnus-correct-substring)
331 (t
332 'substring)))
333
eec82323
LMI
334(defun gnus-tilde-max-form (el max-width)
335 "Return a form that limits EL to MAX-WIDTH."
23f87bed
MB
336 (let ((max (abs max-width))
337 (length-fun (gnus-string-width-function))
338 (substring-fun (gnus-substring-function)))
eec82323 339 (if (symbolp el)
23f87bed 340 `(if (> (,length-fun ,el) ,max)
eec82323 341 ,(if (< max-width 0)
23f87bed 342 `(,substring-fun ,el (- (,length-fun ,el) ,max))
c7b98a1c
G
343 `(if (gnus-lrm-string-p ,el)
344 (concat (,substring-fun ,el 0 ,max) ,gnus-lrm-string)
345 (,substring-fun ,el 0 ,max)))
eec82323
LMI
346 ,el)
347 `(let ((val (eval ,el)))
23f87bed 348 (if (> (,length-fun val) ,max)
eec82323 349 ,(if (< max-width 0)
23f87bed 350 `(,substring-fun val (- (,length-fun val) ,max))
c7b98a1c
G
351 `(if (gnus-lrm-string-p val)
352 (concat (,substring-fun val 0 ,max) ,gnus-lrm-string)
353 (,substring-fun val 0 ,max)))
eec82323
LMI
354 val)))))
355
356(defun gnus-tilde-cut-form (el cut-width)
357 "Return a form that cuts CUT-WIDTH off of EL."
23f87bed
MB
358 (let ((cut (abs cut-width))
359 (length-fun (gnus-string-width-function))
360 (substring-fun (gnus-substring-function)))
eec82323 361 (if (symbolp el)
23f87bed 362 `(if (> (,length-fun ,el) ,cut)
eec82323 363 ,(if (< cut-width 0)
23f87bed
MB
364 `(,substring-fun ,el 0 (- (,length-fun ,el) ,cut))
365 `(,substring-fun ,el ,cut))
eec82323
LMI
366 ,el)
367 `(let ((val (eval ,el)))
23f87bed 368 (if (> (,length-fun val) ,cut)
eec82323 369 ,(if (< cut-width 0)
23f87bed
MB
370 `(,substring-fun val 0 (- (,length-fun val) ,cut))
371 `(,substring-fun val ,cut))
eec82323
LMI
372 val)))))
373
374(defun gnus-tilde-ignore-form (el ignore-value)
375 "Return a form that is blank when EL is IGNORE-VALUE."
376 (if (symbolp el)
377 `(if (equal ,el ,ignore-value)
378 "" ,el)
379 `(let ((val (eval ,el)))
380 (if (equal val ,ignore-value)
381 "" val))))
382
23f87bed
MB
383(defun gnus-pad-form (el pad-width)
384 "Return a form that pads EL to PAD-WIDTH accounting for multi-column
385characters correctly. This is because `format' may pad to columns or to
386characters when given a pad value."
387 (let ((pad (abs pad-width))
388 (side (< 0 pad-width))
389 (length-fun (gnus-string-width-function)))
390 (if (symbolp el)
391 `(let ((need (- ,pad (,length-fun ,el))))
392 (if (> need 0)
393 (concat ,(when side '(make-string need ?\ ))
394 ,el
395 ,(when (not side) '(make-string need ?\ )))
396 ,el))
397 `(let* ((val (eval ,el))
398 (need (- ,pad (,length-fun val))))
399 (if (> need 0)
400 (concat ,(when side '(make-string need ?\ ))
401 val
402 ,(when (not side) '(make-string need ?\ )))
403 val)))))
404
eec82323
LMI
405(defun gnus-parse-format (format spec-alist &optional insert)
406 ;; This function parses the FORMAT string with the help of the
407 ;; SPEC-ALIST and returns a list that can be eval'ed to return the
408 ;; string. If the FORMAT string contains the specifiers %( and %)
409 ;; the text between them will have the mouse-face text property.
16409b0b
GM
410 ;; If the FORMAT string contains the specifiers %[ and %], the text between
411 ;; them will have the balloon-help text property.
23f87bed
MB
412 (let ((case-fold-search nil))
413 (if (string-match
414