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