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