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