Mercurial > emacs
annotate lisp/gnus/gnus-spec.el @ 20347:d8e5f3c1618b
Include "intervals.h" for prototypes.
author | Andreas Schwab <schwab@suse.de> |
---|---|
date | Tue, 25 Nov 1997 13:28:06 +0000 |
parents | 6f6cf9184e93 |
children | 15fc6acbae7a |
rev | line source |
---|---|
17493 | 1 ;;; gnus-spec.el --- format spec functions for Gnus |
2 ;; Copyright (C) 1996,97 Free Software Foundation, Inc. | |
3 | |
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no> | |
5 ;; Keywords: news | |
6 | |
7 ;; This file is part of GNU Emacs. | |
8 | |
9 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
10 ;; it under the terms of the GNU General Public License as published by | |
11 ;; the Free Software Foundation; either version 2, or (at your option) | |
12 ;; any later version. | |
13 | |
14 ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 ;; GNU General Public License for more details. | |
18 | |
19 ;; You should have received a copy of the GNU General Public License | |
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
22 ;; Boston, MA 02111-1307, USA. | |
23 | |
24 ;;; Commentary: | |
25 | |
26 ;;; Code: | |
27 | |
19521
6f6cf9184e93
Require cl at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
17493
diff
changeset
|
28 (eval-when-compile (require 'cl)) |
6f6cf9184e93
Require cl at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
17493
diff
changeset
|
29 |
17493 | 30 (require 'gnus) |
31 | |
32 ;;; Internal variables. | |
33 | |
34 (defvar gnus-summary-mark-positions nil) | |
35 (defvar gnus-group-mark-positions nil) | |
36 (defvar gnus-group-indentation "") | |
37 | |
38 ;; Format specs. The chunks below are the machine-generated forms | |
39 ;; that are to be evaled as the result of the default format strings. | |
40 ;; We write them in here to get them byte-compiled. That way the | |
41 ;; default actions will be quite fast, while still retaining the full | |
42 ;; flexibility of the user-defined format specs. | |
43 | |
44 ;; First we have lots of dummy defvars to let the compiler know these | |
45 ;; are really dynamic variables. | |
46 | |
47 (defvar gnus-tmp-unread) | |
48 (defvar gnus-tmp-replied) | |
49 (defvar gnus-tmp-score-char) | |
50 (defvar gnus-tmp-indentation) | |
51 (defvar gnus-tmp-opening-bracket) | |
52 (defvar gnus-tmp-lines) | |
53 (defvar gnus-tmp-name) | |
54 (defvar gnus-tmp-closing-bracket) | |
55 (defvar gnus-tmp-subject-or-nil) | |
56 (defvar gnus-tmp-subject) | |
57 (defvar gnus-tmp-marked) | |
58 (defvar gnus-tmp-marked-mark) | |
59 (defvar gnus-tmp-subscribed) | |
60 (defvar gnus-tmp-process-marked) | |
61 (defvar gnus-tmp-number-of-unread) | |
62 (defvar gnus-tmp-group-name) | |
63 (defvar gnus-tmp-group) | |
64 (defvar gnus-tmp-article-number) | |
65 (defvar gnus-tmp-unread-and-unselected) | |
66 (defvar gnus-tmp-news-method) | |
67 (defvar gnus-tmp-news-server) | |
68 (defvar gnus-tmp-article-number) | |
69 (defvar gnus-mouse-face) | |
70 (defvar gnus-mouse-face-prop) | |
71 | |
72 (defun gnus-summary-line-format-spec () | |
73 (insert gnus-tmp-unread gnus-tmp-replied | |
74 gnus-tmp-score-char gnus-tmp-indentation) | |
75 (gnus-put-text-property | |
76 (point) | |
77 (progn | |
78 (insert | |
79 gnus-tmp-opening-bracket | |
80 (format "%4d: %-20s" | |
81 gnus-tmp-lines | |
82 (if (> (length gnus-tmp-name) 20) | |
83 (substring gnus-tmp-name 0 20) | |
84 gnus-tmp-name)) | |
85 gnus-tmp-closing-bracket) | |
86 (point)) | |
87 gnus-mouse-face-prop gnus-mouse-face) | |
88 (insert " " gnus-tmp-subject-or-nil "\n")) | |
89 | |
90 (defvar gnus-summary-line-format-spec | |
91 (gnus-byte-code 'gnus-summary-line-format-spec)) | |
92 | |
93 (defun gnus-summary-dummy-line-format-spec () | |
94 (insert "* ") | |
95 (gnus-put-text-property | |
96 (point) | |
97 (progn | |
98 (insert ": :") | |
99 (point)) | |
100 gnus-mouse-face-prop gnus-mouse-face) | |
101 (insert " " gnus-tmp-subject "\n")) | |
102 | |
103 (defvar gnus-summary-dummy-line-format-spec | |
104 (gnus-byte-code 'gnus-summary-dummy-line-format-spec)) | |
105 | |
106 (defun gnus-group-line-format-spec () | |
107 (insert gnus-tmp-marked-mark gnus-tmp-subscribed | |
108 gnus-tmp-process-marked | |
109 gnus-group-indentation | |
110 (format "%5s: " gnus-tmp-number-of-unread)) | |
111 (gnus-put-text-property | |
112 (point) | |
113 (progn | |
114 (insert gnus-tmp-group "\n") | |
115 (1- (point))) | |
116 gnus-mouse-face-prop gnus-mouse-face)) | |
117 (defvar gnus-group-line-format-spec | |
118 (gnus-byte-code 'gnus-group-line-format-spec)) | |
119 | |
120 (defvar gnus-format-specs | |
121 `((version . ,emacs-version) | |
122 (group "%M\%S\%p\%P\%5y: %(%g%)%l\n" ,gnus-group-line-format-spec) | |
123 (summary-dummy "* %(: :%) %S\n" | |
124 ,gnus-summary-dummy-line-format-spec) | |
125 (summary "%U\%R\%z\%I\%(%[%4L: %-20,20n%]%) %s\n" | |
126 ,gnus-summary-line-format-spec)) | |
127 "Alist of format specs.") | |
128 | |
129 (defvar gnus-article-mode-line-format-spec nil) | |
130 (defvar gnus-summary-mode-line-format-spec nil) | |
131 (defvar gnus-group-mode-line-format-spec nil) | |
132 | |
133 ;;; Phew. All that gruft is over, fortunately. | |
134 | |
135 ;;;###autoload | |
136 (defun gnus-update-format (var) | |
137 "Update the format specification near point." | |
138 (interactive | |
139 (list | |
140 (save-excursion | |
141 (eval-defun nil) | |
142 ;; Find the end of the current word. | |
143 (re-search-forward "[ \t\n]" nil t) | |
144 ;; Search backward. | |
145 (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t) | |
146 (match-string 1))))) | |
147 (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var) | |
148 (match-string 1 var)))) | |
149 (entry (assq type gnus-format-specs)) | |
150 value spec) | |
151 (when entry | |
152 (setq gnus-format-specs (delq entry gnus-format-specs))) | |
153 (set | |
154 (intern (format "%s-spec" var)) | |
155 (gnus-parse-format (setq value (symbol-value (intern var))) | |
156 (symbol-value (intern (format "%s-alist" var))) | |
157 (not (string-match "mode" var)))) | |
158 (setq spec (symbol-value (intern (format "%s-spec" var)))) | |
159 (push (list type value spec) gnus-format-specs) | |
160 | |
161 (pop-to-buffer "*Gnus Format*") | |
162 (erase-buffer) | |
163 (lisp-interaction-mode) | |
164 (insert (pp-to-string spec)))) | |
165 | |
166 (defun gnus-update-format-specifications (&optional force &rest types) | |
167 "Update all (necessary) format specifications." | |
168 ;; Make the indentation array. | |
169 ;; See whether all the stored info needs to be flushed. | |
170 (when (or force | |
171 (not (equal emacs-version | |
172 (cdr (assq 'version gnus-format-specs))))) | |
173 (setq gnus-format-specs nil)) | |
174 | |
175 ;; Go through all the formats and see whether they need updating. | |
176 (let (new-format entry type val) | |
177 (while (setq type (pop types)) | |
178 ;; Jump to the proper buffer to find out the value of | |
179 ;; the variable, if possible. (It may be buffer-local.) | |
180 (save-excursion | |
181 (let ((buffer (intern (format "gnus-%s-buffer" type))) | |
182 val) | |
183 (when (and (boundp buffer) | |
184 (setq val (symbol-value buffer)) | |
185 (get-buffer val) | |
186 (buffer-name (get-buffer val))) | |
187 (set-buffer (get-buffer val))) | |
188 (setq new-format (symbol-value | |
189 (intern (format "gnus-%s-line-format" type))))) | |
190 (setq entry (cdr (assq type gnus-format-specs))) | |
191 (if (and (car entry) | |
192 (equal (car entry) new-format)) | |
193 ;; Use the old format. | |
194 (set (intern (format "gnus-%s-line-format-spec" type)) | |
195 (cadr entry)) | |
196 ;; This is a new format. | |
197 (setq val | |
198 (if (not (stringp new-format)) | |
199 ;; This is a function call or something. | |
200 new-format | |
201 ;; This is a "real" format. | |
202 (gnus-parse-format | |
203 new-format | |
204 (symbol-value | |
205 (intern (format "gnus-%s-line-format-alist" | |
206 (if (eq type 'article-mode) | |
207 'summary-mode type)))) | |
208 (not (string-match "mode$" (symbol-name type)))))) | |
209 ;; Enter the new format spec into the list. | |
210 (if entry | |
211 (progn | |
212 (setcar (cdr entry) val) | |
213 (setcar entry new-format)) | |
214 (push (list type new-format val) gnus-format-specs)) | |
215 (set (intern (format "gnus-%s-line-format-spec" type)) val))))) | |
216 | |
217 (unless (assq 'version gnus-format-specs) | |
218 (push (cons 'version emacs-version) gnus-format-specs))) | |
219 | |
220 (defvar gnus-mouse-face-0 'highlight) | |
221 (defvar gnus-mouse-face-1 'highlight) | |
222 (defvar gnus-mouse-face-2 'highlight) | |
223 (defvar gnus-mouse-face-3 'highlight) | |
224 (defvar gnus-mouse-face-4 'highlight) | |
225 | |
226 (defun gnus-mouse-face-function (form type) | |
227 `(gnus-put-text-property | |
228 (point) (progn ,@form (point)) | |
229 gnus-mouse-face-prop | |
230 ,(if (equal type 0) | |
231 'gnus-mouse-face | |
232 `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type))))))) | |
233 | |
234 (defvar gnus-face-0 'bold) | |
235 (defvar gnus-face-1 'italic) | |
236 (defvar gnus-face-2 'bold-italic) | |
237 (defvar gnus-face-3 'bold) | |
238 (defvar gnus-face-4 'bold) | |
239 | |
240 (defun gnus-face-face-function (form type) | |
241 `(gnus-put-text-property | |
242 (point) (progn ,@form (point)) | |
243 'face ',(symbol-value (intern (format "gnus-face-%d" type))))) | |
244 | |
245 (defun gnus-tilde-max-form (el max-width) | |
246 "Return a form that limits EL to MAX-WIDTH." | |
247 (let ((max (abs max-width))) | |
248 (if (symbolp el) | |
249 `(if (> (length ,el) ,max) | |
250 ,(if (< max-width 0) | |
251 `(substring ,el (- (length el) ,max)) | |
252 `(substring ,el 0 ,max)) | |
253 ,el) | |
254 `(let ((val (eval ,el))) | |
255 (if (> (length val) ,max) | |
256 ,(if (< max-width 0) | |
257 `(substring val (- (length val) ,max)) | |
258 `(substring val 0 ,max)) | |
259 val))))) | |
260 | |
261 (defun gnus-tilde-cut-form (el cut-width) | |
262 "Return a form that cuts CUT-WIDTH off of EL." | |
263 (let ((cut (abs cut-width))) | |
264 (if (symbolp el) | |
265 `(if (> (length ,el) ,cut) | |
266 ,(if (< cut-width 0) | |
267 `(substring ,el 0 (- (length el) ,cut)) | |
268 `(substring ,el ,cut)) | |
269 ,el) | |
270 `(let ((val (eval ,el))) | |
271 (if (> (length val) ,cut) | |
272 ,(if (< cut-width 0) | |
273 `(substring val 0 (- (length val) ,cut)) | |
274 `(substring val ,cut)) | |
275 val))))) | |
276 | |
277 (defun gnus-tilde-ignore-form (el ignore-value) | |
278 "Return a form that is blank when EL is IGNORE-VALUE." | |
279 (if (symbolp el) | |
280 `(if (equal ,el ,ignore-value) | |
281 "" ,el) | |
282 `(let ((val (eval ,el))) | |
283 (if (equal val ,ignore-value) | |
284 "" val)))) | |
285 | |
286 (defun gnus-parse-format (format spec-alist &optional insert) | |
287 ;; This function parses the FORMAT string with the help of the | |
288 ;; SPEC-ALIST and returns a list that can be eval'ed to return the | |
289 ;; string. If the FORMAT string contains the specifiers %( and %) | |
290 ;; the text between them will have the mouse-face text property. | |
291 (if (string-match | |
292 "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'" | |
293 format) | |
294 (gnus-parse-complex-format format spec-alist) | |
295 ;; This is a simple format. | |
296 (gnus-parse-simple-format format spec-alist insert))) | |
297 | |
298 (defun gnus-parse-complex-format (format spec-alist) | |
299 (save-excursion | |
300 (gnus-set-work-buffer) | |
301 (insert format) | |
302 (goto-char (point-min)) | |
303 (while (re-search-forward "\"" nil t) | |
304 (replace-match "\\\"" nil t)) | |
305 (goto-char (point-min)) | |
306 (insert "(\"") | |
307 (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t) | |
308 (let ((number (if (match-beginning 1) | |
309 (match-string 1) "0")) | |
310 (delim (aref (match-string 2) 0))) | |
311 (if (or (= delim ?\() (= delim ?\{)) | |
312 (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face") | |
313 " " number " \"")) | |
314 (replace-match "\")\"")))) | |
315 (goto-char (point-max)) | |
316 (insert "\")") | |
317 (goto-char (point-min)) | |
318 (let ((form (read (current-buffer)))) | |
319 (cons 'progn (gnus-complex-form-to-spec form spec-alist))))) | |
320 | |
321 (defun gnus-complex-form-to-spec (form spec-alist) | |
322 (delq nil | |
323 (mapcar | |
324 (lambda (sform) | |
325 (if (stringp sform) | |
326 (gnus-parse-simple-format sform spec-alist t) | |
327 (funcall (intern (format "gnus-%s-face-function" (car sform))) | |
328 (gnus-complex-form-to-spec (cddr sform) spec-alist) | |
329 (nth 1 sform)))) | |
330 form))) | |
331 | |
332 (defun gnus-parse-simple-format (format spec-alist &optional insert) | |
333 ;; This function parses the FORMAT string with the help of the | |
334 ;; SPEC-ALIST and returns a list that can be eval'ed to return a | |
335 ;; string. | |
336 (let ((max-width 0) | |
337 spec flist fstring elem result dontinsert user-defined | |
338 type value pad-width spec-beg cut-width ignore-value | |
339 tilde-form tilde elem-type) | |
340 (save-excursion | |
341 (gnus-set-work-buffer) | |
342 (insert format) | |
343 (goto-char (point-min)) | |
344 (while (re-search-forward "%" nil t) | |
345 (setq user-defined nil | |
346 spec-beg nil | |
347 pad-width nil | |
348 max-width nil | |
349 cut-width nil | |
350 ignore-value nil | |
351 tilde-form nil) | |
352 (setq spec-beg (1- (point))) | |
353 | |
354 ;; Parse this spec fully. | |
355 (while | |
356 (cond | |
357 ((looking-at "\\([-.0-9]+\\)\\(,[-0-9]+\\)?") | |
358 (setq pad-width (string-to-number (match-string 1))) | |
359 (when (match-beginning 2) | |
360 (setq max-width (string-to-number (buffer-substring | |
361 (1+ (match-beginning 2)) | |
362 (match-end 2))))) | |
363 (goto-char (match-end 0))) | |
364 ((looking-at "~") | |
365 (forward-char 1) | |
366 (setq tilde (read (current-buffer)) | |
367 type (car tilde) | |
368 value (cadr tilde)) | |
369 (cond | |
370 ((memq type '(pad pad-left)) | |
371 (setq pad-width value)) | |
372 ((eq type 'pad-right) | |
373 (setq pad-width (- value))) | |
374 ((memq type '(max-right max)) | |
375 (setq max-width value)) | |
376 ((eq type 'max-left) | |
377 (setq max-width (- value))) | |
378 ((memq type '(cut cut-left)) | |
379 (setq cut-width value)) | |
380 ((eq type 'cut-right) | |
381 (setq cut-width (- value))) | |
382 ((eq type 'ignore) | |
383 (setq ignore-value | |
384 (if (stringp value) value (format "%s" value)))) | |
385 ((eq type 'form) | |
386 (setq tilde-form value)) | |
387 (t | |
388 (error "Unknown tilde type: %s" tilde))) | |
389 t) | |
390 (t | |
391 nil))) | |
392 ;; User-defined spec -- find the spec name. | |
393 (when (= (setq spec (following-char)) ?u) | |
394 (forward-char 1) | |
395 (setq user-defined (following-char))) | |
396 (forward-char 1) | |
397 (delete-region spec-beg (point)) | |
398 | |
399 ;; Now we have all the relevant data on this spec, so | |
400 ;; we start doing stuff. | |
401 (insert "%") | |
402 (if (eq spec ?%) | |
403 ;; "%%" just results in a "%". | |
404 (insert "%") | |
405 (cond | |
406 ;; Do tilde forms. | |
407 ((eq spec ?@) | |
408 (setq elem (list tilde-form ?s))) | |
409 ;; Treat user defined format specifiers specially. | |
410 (user-defined | |
411 (setq elem | |
412 (list | |
413 (list (intern (format "gnus-user-format-function-%c" | |
414 user-defined)) | |
415 'gnus-tmp-header) | |
416 ?s))) | |
417 ;; Find the specification from `spec-alist'. | |
418 ((setq elem (cdr (assq spec spec-alist)))) | |
419 (t | |
420 (setq elem '("*" ?s)))) | |
421 (setq elem-type (cadr elem)) | |
422 ;; Insert the new format elements. | |
423 (when pad-width | |
424 (insert (number-to-string pad-width))) | |
425 ;; Create the form to be evaled. | |
426 (if (or max-width cut-width ignore-value) | |
427 (progn | |
428 (insert ?s) | |
429 (let ((el (car elem))) | |
430 (cond ((= (cadr elem) ?c) | |
431 (setq el (list 'char-to-string el))) | |
432 ((= (cadr elem) ?d) | |
433 (setq el (list 'int-to-string el)))) | |
434 (when ignore-value | |
435 (setq el (gnus-tilde-ignore-form el ignore-value))) | |
436 (when cut-width | |
437 (setq el (gnus-tilde-cut-form el cut-width))) | |
438 (when max-width | |
439 (setq el (gnus-tilde-max-form el max-width))) | |
440 (push el flist))) | |
441 (insert elem-type) | |
442 (push (car elem) flist)))) | |
443 (setq fstring (buffer-string))) | |
444 | |
445 ;; Do some postprocessing to increase efficiency. | |
446 (setq | |
447 result | |
448 (cond | |
449 ;; Emptyness. | |
450 ((string= fstring "") | |
451 nil) | |
452 ;; Not a format string. | |
453 ((not (string-match "%" fstring)) | |
454 (list fstring)) | |
455 ;; A format string with just a single string spec. | |
456 ((string= fstring "%s") | |
457 (list (car flist))) | |
458 ;; A single character. | |
459 ((string= fstring "%c") | |
460 (list (car flist))) | |
461 ;; A single number. | |
462 ((string= fstring "%d") | |
463 (setq dontinsert) | |
464 (if insert | |
465 (list `(princ ,(car flist))) | |
466 (list `(int-to-string ,(car flist))))) | |
467 ;; Just lots of chars and strings. | |
468 ((string-match "\\`\\(%[cs]\\)+\\'" fstring) | |
469 (nreverse flist)) | |
470 ;; A single string spec at the beginning of the spec. | |
471 ((string-match "\\`%[sc][^%]+\\'" fstring) | |
472 (list (car flist) (substring fstring 2))) | |
473 ;; A single string spec in the middle of the spec. | |
474 ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring) | |
475 (list (match-string 1 fstring) (car flist) (match-string 2 fstring))) | |
476 ;; A single string spec in the end of the spec. | |
477 ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring) | |
478 (list (match-string 1 fstring) (car flist))) | |
479 ;; A more complex spec. | |
480 (t | |
481 (list (cons 'format (cons fstring (nreverse flist))))))) | |
482 | |
483 (if insert | |
484 (when result | |
485 (if dontinsert | |
486 result | |
487 (cons 'insert result))) | |
488 (cond ((stringp result) | |
489 result) | |
490 ((consp result) | |
491 (cons 'concat result)) | |
492 (t ""))))) | |
493 | |
494 (defun gnus-eval-format (format &optional alist props) | |
495 "Eval the format variable FORMAT, using ALIST. | |
496 If PROPS, insert the result." | |
497 (let ((form (gnus-parse-format format alist props))) | |
498 (if props | |
499 (gnus-add-text-properties (point) (progn (eval form) (point)) props) | |
500 (eval form)))) | |
501 | |
502 (defun gnus-compile () | |
503 "Byte-compile the user-defined format specs." | |
504 (interactive) | |
505 (when gnus-xemacs | |
506 (error "Can't compile specs under XEmacs")) | |
507 (let ((entries gnus-format-specs) | |
508 (byte-compile-warnings '(unresolved callargs redefine)) | |
509 entry gnus-tmp-func) | |
510 (save-excursion | |
511 (gnus-message 7 "Compiling format specs...") | |
512 | |
513 (while entries | |
514 (setq entry (pop entries)) | |
515 (if (eq (car entry) 'version) | |
516 (setq gnus-format-specs (delq entry gnus-format-specs)) | |
517 (when (and (listp (caddr entry)) | |
518 (not (eq 'byte-code (caaddr entry)))) | |
519 (fset 'gnus-tmp-func `(lambda () ,(caddr entry))) | |
520 (byte-compile 'gnus-tmp-func) | |
521 (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func))))) | |
522 | |
523 (push (cons 'version emacs-version) gnus-format-specs) | |
524 ;; Mark the .newsrc.eld file as "dirty". | |
525 (gnus-dribble-enter " ") | |
526 (gnus-message 7 "Compiling user specs...done")))) | |
527 | |
528 (provide 'gnus-spec) | |
529 | |
530 ;;; gnus-spec.el ends here |