(mm-inline-text-html-render-with-w3m): Revert last change.
[gnus] / lisp / gnus-spec.el
1 ;;; gnus-spec.el --- format spec functions for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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
13 ;; the Free Software Foundation; either version 2, or (at your option)
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
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31 (defvar gnus-newsrc-file-version)
32
33 (require 'gnus)
34
35 (defcustom gnus-use-correct-string-widths (featurep 'xemacs)
36   "*If non-nil, use correct functions for dealing with wide characters."
37   :version "22.1"
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
43 text properties. This is only needed on XEmacs, as FSF Emacs does this anyway."
44   :version "22.1"
45   :group 'gnus-format
46   :type 'boolean)
47
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)
87 (defvar gnus-tmp-header)
88 (defvar gnus-tmp-from)
89
90 (defun gnus-summary-line-format-spec ()
91   (insert gnus-tmp-unread gnus-tmp-replied
92           gnus-tmp-score-char gnus-tmp-indentation)
93   (gnus-put-text-property
94    (point)
95    (progn
96      (insert
97       (format "%c%4s: %-23s%c" gnus-tmp-opening-bracket gnus-tmp-lines
98               (let ((val
99                      (inline
100                        (gnus-summary-from-or-to-or-newsgroups
101                         gnus-tmp-header gnus-tmp-from))))
102                 (if (> (length val) 23)
103                     (substring val 0 23)
104                   val))
105               gnus-tmp-closing-bracket))
106      (point))
107    gnus-mouse-face-prop gnus-mouse-face)
108   (insert " " gnus-tmp-subject-or-nil "\n"))
109
110 (defvar gnus-summary-line-format-spec
111   (gnus-byte-code 'gnus-summary-line-format-spec))
112
113 (defun gnus-summary-dummy-line-format-spec ()
114   (insert "*  ")
115   (gnus-put-text-property
116    (point)
117    (progn
118      (insert ":                          :")
119      (point))
120    gnus-mouse-face-prop gnus-mouse-face)
121   (insert " " gnus-tmp-subject "\n"))
122
123 (defvar gnus-summary-dummy-line-format-spec
124   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
125
126 (defun gnus-group-line-format-spec ()
127   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
128           gnus-tmp-process-marked
129           gnus-group-indentation
130           (format "%5s: " gnus-tmp-number-of-unread))
131   (gnus-put-text-property
132    (point)
133    (progn
134      (insert gnus-tmp-group "\n")
135      (1- (point)))
136    gnus-mouse-face-prop gnus-mouse-face))
137 (defvar gnus-group-line-format-spec
138   (gnus-byte-code 'gnus-group-line-format-spec))
139
140 (defvar gnus-format-specs
141   `((version . ,emacs-version)
142     (gnus-version . ,(gnus-continuum-version))
143     (group "%M\%S\%p\%P\%5y: %(%g%)\n" ,gnus-group-line-format-spec)
144     (summary-dummy "*  %(:                          :%) %S\n"
145                    ,gnus-summary-dummy-line-format-spec)
146     (summary "%U%R%z%I%(%[%4L: %-23,23f%]%) %s\n"
147              ,gnus-summary-line-format-spec))
148   "Alist of format specs.")
149
150 (defvar gnus-default-format-specs gnus-format-specs)
151
152 (defvar gnus-article-mode-line-format-spec nil)
153 (defvar gnus-summary-mode-line-format-spec nil)
154 (defvar gnus-group-mode-line-format-spec nil)
155
156 ;;; Phew.  All that gruft is over with, fortunately.
157
158 ;;;###autoload
159 (defun gnus-update-format (var)
160   "Update the format specification near point."
161   (interactive
162    (list
163     (save-excursion
164       (eval-defun nil)
165       ;; Find the end of the current word.
166       (re-search-forward "[ \t\n]" nil t)
167       ;; Search backward.
168       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
169         (match-string 1)))))
170   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
171                               (match-string 1 var))))
172          (entry (assq type gnus-format-specs))
173          value spec)
174     (when entry
175       (setq gnus-format-specs (delq entry gnus-format-specs)))
176     (set
177      (intern (format "%s-spec" var))
178      (gnus-parse-format (setq value (symbol-value (intern var)))
179                         (symbol-value (intern (format "%s-alist" var)))
180                         (not (string-match "mode" var))))
181     (setq spec (symbol-value (intern (format "%s-spec" var))))
182     (push (list type value spec) gnus-format-specs)
183
184     (pop-to-buffer "*Gnus Format*")
185     (erase-buffer)
186     (lisp-interaction-mode)
187     (insert (gnus-pp-to-string spec))))
188
189 (defun gnus-update-format-specifications (&optional force &rest types)
190   "Update all (necessary) format specifications.
191 Return a list of updated types."
192   ;; Make the indentation array.
193   ;; See whether all the stored info needs to be flushed.
194   (when (or force
195             (not gnus-newsrc-file-version)
196             (not (equal (gnus-continuum-version)
197                         (gnus-continuum-version gnus-newsrc-file-version)))
198             (not (equal emacs-version
199                         (cdr (assq 'version gnus-format-specs)))))
200     (setq gnus-format-specs nil))
201   ;; Flush the group format spec cache if there's the grouplens stuff
202   ;; or it doesn't support decoded group names.
203   (when (memq 'group types)
204     (let* ((spec (assq 'group gnus-format-specs))
205            (sspec (gnus-prin1-to-string (nth 2 spec))))
206       (when (or (string-match " gnus-tmp-grouplens[ )]" sspec)
207                 (not (string-match " gnus-tmp-decoded-group[ )]" sspec)))
208         (setq gnus-format-specs (delq spec gnus-format-specs)))))
209
210   ;; Go through all the formats and see whether they need updating.
211   (let (new-format entry type val updated)
212     (while (setq type (pop types))
213       ;; Jump to the proper buffer to find out the value of the
214       ;; variable, if possible.  (It may be buffer-local.)
215       (save-excursion
216         (let ((buffer (intern (format "gnus-%s-buffer" type))))
217           (when (and (boundp buffer)
218                      (setq val (symbol-value buffer))
219                      (gnus-buffer-exists-p val))
220             (set-buffer val))
221           (setq new-format (symbol-value
222                             (intern (format "gnus-%s-line-format" type)))))
223         (setq entry (cdr (assq type gnus-format-specs)))
224         (if (and (car entry)
225                  (equal (car entry) new-format))
226             ;; Use the old format.
227             (set (intern (format "gnus-%s-line-format-spec" type))
228                  (cadr entry))
229           ;; This is a new format.
230           (setq val
231                 (if (not (stringp new-format))
232                     ;; This is a function call or something.
233                     new-format
234                   ;; This is a "real" format.
235                   (gnus-parse-format
236                    new-format
237                    (symbol-value
238                     (intern (format "gnus-%s-line-format-alist" type)))
239                    (not (string-match "mode$" (symbol-name type))))))
240           ;; Enter the new format spec into the list.
241           (if entry
242               (progn
243                 (setcar (cdr entry) val)
244                 (setcar entry new-format))
245             (push (list type new-format val) gnus-format-specs))
246           (set (intern (format "gnus-%s-line-format-spec" type)) val)
247           (push type updated))))
248
249     (unless (assq 'version gnus-format-specs)
250       (push (cons 'version emacs-version) gnus-format-specs))
251     updated))
252
253 (defvar gnus-mouse-face-0 'highlight)
254 (defvar gnus-mouse-face-1 'highlight)
255 (defvar gnus-mouse-face-2 'highlight)
256 (defvar gnus-mouse-face-3 'highlight)
257 (defvar gnus-mouse-face-4 'highlight)
258
259 (defun gnus-mouse-face-function (form type)
260   `(gnus-put-text-property
261     (point) (progn ,@form (point))
262     gnus-mouse-face-prop
263     ,(if (equal type 0)
264          'gnus-mouse-face
265        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
266
267 (defvar gnus-face-0 'bold)
268 (defvar gnus-face-1 'italic)
269 (defvar gnus-face-2 'bold-italic)
270 (defvar gnus-face-3 'bold)
271 (defvar gnus-face-4 'bold)
272
273 (defun gnus-face-face-function (form type)
274   `(gnus-add-text-properties
275     (point) (progn ,@form (point))
276     '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
277
278 (defun gnus-balloon-face-function (form type)
279   `(gnus-put-text-property
280     (point) (progn ,@form (point))
281     ,(if (fboundp 'balloon-help-mode)
282          ''balloon-help
283        ''help-echo)
284     ,(intern (format "gnus-balloon-face-%d" type))))
285
286 (defun gnus-spec-tab (column)
287   (if (> column 0)
288       `(insert-char ?  (max (- ,column (current-column)) 0))
289     (let ((column (abs column)))
290       `(if (> (current-column) ,column)
291            (let ((end (point)))
292              (if (= (move-to-column ,column) ,column)
293                  (delete-region (point) end)
294                (delete-region (1- (point)) end)
295                (insert " ")))
296          (insert-char ?  (max (- ,column (current-column)) 0))))))
297
298 (defun gnus-correct-length (string)
299   "Return the correct width of STRING."
300   (let ((length 0))
301     (mapcar (lambda (char) (incf length (char-width char))) string)
302     length))
303
304 (defun gnus-correct-substring (string start &optional end)
305   (let ((wstart 0)
306         (wend 0)
307         (wseek 0)
308         (seek 0)
309         (length (length string))
310         (string (concat string "\0")))
311     ;; Find the start position.
312     (while (and (< seek length)
313                 (< wseek start))
314       (incf wseek (char-width (aref string seek)))
315       (incf seek))
316     (setq wstart seek)
317     ;; Find the end position.
318     (while (and (<= seek length)
319                 (or (not end)
320                     (<= wseek end)))
321       (incf wseek (char-width (aref string seek)))
322       (incf seek))
323     (setq wend seek)
324     (substring string wstart (1- wend))))
325
326 (defun gnus-string-width-function ()
327   (cond
328    (gnus-use-correct-string-widths
329     'gnus-correct-length)
330    ((fboundp 'string-width)
331     'string-width)
332    (t
333     'length)))
334
335 (defun gnus-substring-function ()
336   (cond
337    (gnus-use-correct-string-widths
338     'gnus-correct-substring)
339    ((fboundp 'string-width)
340     'gnus-correct-substring)
341    (t
342     'substring)))
343
344 (defun gnus-tilde-max-form (el max-width)
345   "Return a form that limits EL to MAX-WIDTH."
346   (let ((max (abs max-width))
347         (length-fun (gnus-string-width-function))
348         (substring-fun (gnus-substring-function)))
349     (if (symbolp el)
350         `(if (> (,length-fun ,el) ,max)
351              ,(if (< max-width 0)
352                   `(,substring-fun ,el (- (,length-fun ,el) ,max))
353                 `(,substring-fun ,el 0 ,max))
354            ,el)
355       `(let ((val (eval ,el)))
356          (if (> (,length-fun val) ,max)
357              ,(if (< max-width 0)
358                   `(,substring-fun val (- (,length-fun val) ,max))
359                 `(,substring-fun val 0 ,max))
360            val)))))
361
362 (defun gnus-tilde-cut-form (el cut-width)
363   "Return a form that cuts CUT-WIDTH off of EL."
364   (let ((cut (abs cut-width))
365         (length-fun (gnus-string-width-function))
366         (substring-fun (gnus-substring-function)))
367     (if (symbolp el)
368         `(if (> (,length-fun ,el) ,cut)
369              ,(if (< cut-width 0)
370                   `(,substring-fun ,el 0 (- (,length-fun ,el) ,cut))
371                 `(,substring-fun ,el ,cut))
372            ,el)
373       `(let ((val (eval ,el)))
374          (if (> (,length-fun val) ,cut)
375              ,(if (< cut-width 0)
376                   `(,substring-fun val 0 (- (,length-fun val) ,cut))
377                 `(,substring-fun val ,cut))
378            val)))))
379
380 (defun gnus-tilde-ignore-form (el ignore-value)
381   "Return a form that is blank when EL is IGNORE-VALUE."
382   (if (symbolp el)
383       `(if (equal ,el ,ignore-value)
384            "" ,el)
385     `(let ((val (eval ,el)))
386        (if (equal val ,ignore-value)
387            "" val))))
388
389 (defun gnus-pad-form (el pad-width)
390   "Return a form that pads EL to PAD-WIDTH accounting for multi-column
391 characters correctly. This is because `format' may pad to columns or to
392 characters when given a pad value."
393   (let ((pad (abs pad-width))
394         (side (< 0 pad-width))
395         (length-fun (gnus-string-width-function)))
396     (if (symbolp el)
397         `(let ((need (- ,pad (,length-fun ,el))))
398            (if (> need 0)
399                (concat ,(when side '(make-string need ?\ ))
400                        ,el
401                        ,(when (not side) '(make-string need ?\ )))
402              ,el))
403       `(let* ((val (eval ,el))
404               (need (- ,pad (,length-fun val))))
405          (if (> need 0)
406              (concat ,(when side '(make-string need ?\ ))
407                      val
408                      ,(when (not side) '(make-string need ?\ )))
409            val)))))
410
411 (defun gnus-parse-format (format spec-alist &optional insert)
412   ;; This function parses the FORMAT string with the help of the
413   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
414   ;; string.  If the FORMAT string contains the specifiers %( and %)
415   ;; the text between them will have the mouse-face text property.
416   ;; If the FORMAT string contains the specifiers %[ and %], the text between
417   ;; them will have the balloon-help text property.
418   (let ((case-fold-search nil))
419     (if (string-match
420          "\\`\\(.*\\)%[0-9]?[{(«]\\(.*\\)%[0-9]?[»})]\\(.*\n?\\)\\'\\|%[-0-9]*=\\|%[-0-9]*\\*"
421          format)
422         (gnus-parse-complex-format format spec-alist)
423       ;; This is a simple format.
424       (gnus-parse-simple-format format spec-alist insert))))
425
426 (defun gnus-parse-complex-format (format spec-alist)
427   (let ((cursor-spec nil))
428     (save-excursion
429       (gnus-set-work-buffer)
430       (insert format)
431       (goto-char (point-min))
432       (while (re-search-forward "\"" nil t)
433         (replace-match "\\\"" nil t))
434       (goto-char (point-min))
435       (insert "(\"")
436       ;; Convert all font specs into font spec lists.
437       (while (re-search-forward "%\\([0-9]+\\)?\\([«»{}()]\\)" nil t)
438         (let ((number (if (match-beginning 1)
439                           (match-string 1) "0"))
440               (delim (aref (match-string 2) 0)))
441           (if (or (= delim ?\()
442                   (= delim ?\{)
443                   (= delim ?\«))
444               (replace-match (concat "\"("
445                                      (cond ((= delim ?\() "mouse")
446                                            ((= delim ?\{) "face")
447                                            (t "balloon"))
448                                      " " number " \"")
449                              t t)
450             (replace-match "\")\""))))
451       (goto-char (point-max))
452       (insert "\")")
453       ;; Convert point position commands.
454       (goto-char (point-min))
455       (let ((case-fold-search nil))
456         (while (re-search-forward "%\\([-0-9]+\\)?\\*" nil t)
457           (replace-match "\"(point)\"" t t)
458           (setq cursor-spec t)))
459       ;; Convert TAB commands.
460       (goto-char (point-min))
461       (while (re-search-forward "%\\([-0-9]+\\)=" nil t)
462         (replace-match (format "\"(tab %s)\"" (match-string 1)) t t))
463       ;; Convert the buffer into the spec.
464       (goto-char (point-min))
465       (let ((form (read (current-buffer))))
466         (if cursor-spec
467             `(let (gnus-position)
468                ,@(gnus-complex-form-to-spec form spec-alist)
469                (if gnus-position
470                    (gnus-put-text-property gnus-position (1+ gnus-position)
471                                            'gnus-position t)))
472           `(progn
473              ,@(gnus-complex-form-to-spec form spec-alist)))))))
474
475 (defun gnus-complex-form-to-spec (form spec-alist)
476   (delq nil
477         (mapcar
478          (lambda (sform)
479            (cond
480             ((stringp sform)
481              (gnus-parse-simple-format sform spec-alist t))
482             ((eq (car sform) 'point)
483              '(setq gnus-position (point)))
484             ((eq (car sform) 'tab)
485              (gnus-spec-tab (cadr sform)))
486             (t
487              (funcall (intern (format "gnus-%s-face-function" (car sform)))
488                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
489                       (nth 1 sform)))))
490          form)))
491
492
493 (defun gnus-xmas-format (fstring &rest args)
494   "A version of `format' which preserves text properties.
495
496 Required for XEmacs, where the built in `format' function strips all text
497 properties from both the format string and any inserted strings.
498