* gnus-spec.el (gnus-correct-length, gnus-correct-substring):
[gnus] / lisp / gnus-spec.el
1 ;;; gnus-spec.el --- format spec functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32
33 (defcustom gnus-use-correct-string-widths (featurep 'xemacs)
34   "*If non-nil, use correct functions for dealing with wide characters."
35   :group 'gnus-format
36   :type 'boolean)
37
38 (defcustom gnus-make-format-preserve-properties (featurep 'xemacs)
39   "*If non-nil, use a replacement `format' function which preserves
40 text properties. This is only needed on XEmacs, as FSF Emacs does this anyway."
41   :group 'gnus-format
42   :type 'boolean)
43
44 ;;; Internal variables.
45
46 (defvar gnus-summary-mark-positions nil)
47 (defvar gnus-group-mark-positions nil)
48 (defvar gnus-group-indentation "")
49
50 ;; Format specs.  The chunks below are the machine-generated forms
51 ;; that are to be evaled as the result of the default format strings.
52 ;; We write them in here to get them byte-compiled.  That way the
53 ;; default actions will be quite fast, while still retaining the full
54 ;; flexibility of the user-defined format specs.
55
56 ;; First we have lots of dummy defvars to let the compiler know these
57 ;; are really dynamic variables.
58
59 (defvar gnus-tmp-unread)
60 (defvar gnus-tmp-replied)
61 (defvar gnus-tmp-score-char)
62 (defvar gnus-tmp-indentation)
63 (defvar gnus-tmp-opening-bracket)
64 (defvar gnus-tmp-lines)
65 (defvar gnus-tmp-name)
66 (defvar gnus-tmp-closing-bracket)
67 (defvar gnus-tmp-subject-or-nil)
68 (defvar gnus-tmp-subject)
69 (defvar gnus-tmp-marked)
70 (defvar gnus-tmp-marked-mark)
71 (defvar gnus-tmp-subscribed)
72 (defvar gnus-tmp-process-marked)
73 (defvar gnus-tmp-number-of-unread)
74 (defvar gnus-tmp-group-name)
75 (defvar gnus-tmp-group)
76 (defvar gnus-tmp-article-number)
77 (defvar gnus-tmp-unread-and-unselected)
78 (defvar gnus-tmp-news-method)
79 (defvar gnus-tmp-news-server)
80 (defvar gnus-tmp-article-number)
81 (defvar gnus-mouse-face)
82 (defvar gnus-mouse-face-prop)
83 (defvar gnus-tmp-header)
84 (defvar gnus-tmp-from)
85
86 (defun gnus-summary-line-format-spec ()
87   (insert gnus-tmp-unread gnus-tmp-replied
88           gnus-tmp-score-char gnus-tmp-indentation)
89   (gnus-put-text-property
90    (point)
91    (progn
92      (insert
93       (format "%c%4s: %-23s%c" gnus-tmp-opening-bracket gnus-tmp-lines
94               (let ((val
95                      (inline
96                        (gnus-summary-from-or-to-or-newsgroups
97                         gnus-tmp-header gnus-tmp-from))))
98                 (if (> (length val) 23)
99                     (substring val 0 23)
100                   val))
101               gnus-tmp-closing-bracket))
102      (point))
103    gnus-mouse-face-prop gnus-mouse-face)
104   (insert " " gnus-tmp-subject-or-nil "\n"))
105
106 (defvar gnus-summary-line-format-spec
107   (gnus-byte-code 'gnus-summary-line-format-spec))
108
109 (defun gnus-summary-dummy-line-format-spec ()
110   (insert "*  ")
111   (gnus-put-text-property
112    (point)
113    (progn
114      (insert ":                          :")
115      (point))
116    gnus-mouse-face-prop gnus-mouse-face)
117   (insert " " gnus-tmp-subject "\n"))
118
119 (defvar gnus-summary-dummy-line-format-spec
120   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
121
122 (defun gnus-group-line-format-spec ()
123   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
124           gnus-tmp-process-marked
125           gnus-group-indentation
126           (format "%5s: " gnus-tmp-number-of-unread))
127   (gnus-put-text-property
128    (point)
129    (progn
130      (insert gnus-tmp-group "\n")
131      (1- (point)))
132    gnus-mouse-face-prop gnus-mouse-face))
133 (defvar gnus-group-line-format-spec
134   (gnus-byte-code 'gnus-group-line-format-spec))
135
136 (defvar gnus-format-specs
137   `((version . ,emacs-version)
138     (gnus-version . ,(gnus-continuum-version))
139     (group "%M\%S\%p\%P\%5y: %(%g%)%l\n" ,gnus-group-line-format-spec)
140     (summary-dummy "*  %(:                          :%) %S\n"
141                    ,gnus-summary-dummy-line-format-spec)
142     (summary "%U%R%z%I%(%[%4L: %-23,23f%]%) %s\n"
143              ,gnus-summary-line-format-spec))
144   "Alist of format specs.")
145
146 (defvar gnus-default-format-specs gnus-format-specs)
147
148 (defvar gnus-article-mode-line-format-spec nil)
149 (defvar gnus-summary-mode-line-format-spec nil)
150 (defvar gnus-group-mode-line-format-spec nil)
151
152 ;;; Phew.  All that gruft is over with, fortunately.
153
154 ;;;###autoload
155 (defun gnus-update-format (var)
156   "Update the format specification near point."
157   (interactive
158    (list
159     (save-excursion
160       (eval-defun nil)
161       ;; Find the end of the current word.
162       (re-search-forward "[ \t\n]" nil t)
163       ;; Search backward.
164       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
165         (match-string 1)))))
166   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
167                               (match-string 1 var))))
168          (entry (assq type gnus-format-specs))
169          value spec)
170     (when entry
171       (setq gnus-format-specs (delq entry gnus-format-specs)))
172     (set
173      (intern (format "%s-spec" var))
174      (gnus-parse-format (setq value (symbol-value (intern var)))
175                         (symbol-value (intern (format "%s-alist" var)))
176                         (not (string-match "mode" var))))
177     (setq spec (symbol-value (intern (format "%s-spec" var))))
178     (push (list type value spec) gnus-format-specs)
179
180     (pop-to-buffer "*Gnus Format*")
181     (erase-buffer)
182     (lisp-interaction-mode)
183     (insert (pp-to-string spec))))
184
185 (defun gnus-update-format-specifications (&optional force &rest types)
186   "Update all (necessary) format specifications."
187   ;; Make the indentation array.
188   ;; See whether all the stored info needs to be flushed.
189   (when (or force
190             (not gnus-newsrc-file-version)
191             (not (equal (gnus-continuum-version)
192                         (gnus-continuum-version gnus-newsrc-file-version)))
193             (not (equal emacs-version
194                         (cdr (assq 'version gnus-format-specs)))))
195     (setq gnus-format-specs nil))
196
197   ;; Go through all the formats and see whether they need updating.
198   (let (new-format entry type val)
199     (while (setq type (pop types))
200       ;; Jump to the proper buffer to find out the value of the
201       ;; variable, if possible.  (It may be buffer-local.)
202       (save-excursion
203         (let ((buffer (intern (format "gnus-%s-buffer" type)))
204               val)
205           (when (and (boundp buffer)
206                      (setq val (symbol-value buffer))
207                      (gnus-buffer-exists-p val))
208             (set-buffer val))
209           (setq new-format (symbol-value
210                             (intern (format "gnus-%s-line-format" type)))))
211         (setq entry (cdr (assq type gnus-format-specs)))
212         (if (and (car entry)
213                  (equal (car entry) new-format))
214             ;; Use the old format.
215             (set (intern (format "gnus-%s-line-format-spec" type))
216                  (cadr entry))
217           ;; This is a new format.
218           (setq val
219                 (if (not (stringp new-format))
220                     ;; This is a function call or something.
221                     new-format
222                   ;; This is a "real" format.
223                   (gnus-parse-format
224                    new-format
225                    (symbol-value
226                     (intern (format "gnus-%s-line-format-alist" type)))
227                    (not (string-match "mode$" (symbol-name type))))))
228           ;; Enter the new format spec into the list.
229           (if entry
230               (progn
231                 (setcar (cdr entry) val)
232                 (setcar entry new-format))
233             (push (list type new-format val) gnus-format-specs))
234           (set (intern (format "gnus-%s-line-format-spec" type)) val)))))
235
236   (unless (assq 'version gnus-format-specs)
237     (push (cons 'version emacs-version) gnus-format-specs)))
238
239 (defvar gnus-mouse-face-0 'highlight)
240 (defvar gnus-mouse-face-1 'highlight)
241 (defvar gnus-mouse-face-2 'highlight)
242 (defvar gnus-mouse-face-3 'highlight)
243 (defvar gnus-mouse-face-4 'highlight)
244
245 (defun gnus-mouse-face-function (form type)
246   `(gnus-put-text-property
247     (point) (progn ,@form (point))
248     gnus-mouse-face-prop
249     ,(if (equal type 0)
250          'gnus-mouse-face
251        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
252
253 (defvar gnus-face-0 'bold)
254 (defvar gnus-face-1 'italic)
255 (defvar gnus-face-2 'bold-italic)
256 (defvar gnus-face-3 'bold)
257 (defvar gnus-face-4 'bold)
258
259 (defun gnus-face-face-function (form type)
260   `(gnus-add-text-properties
261     (point) (progn ,@form (point))
262     '(gnus-face t face ,(symbol-value (intern (format "gnus-face-%d" type))))))
263
264 (defun gnus-balloon-face-function (form type)
265   `(gnus-put-text-property
266     (point) (progn ,@form (point))
267     ,(if (fboundp 'balloon-help-mode)
268          ''balloon-help
269        ''help-echo)
270     ,(intern (format "gnus-balloon-face-%d" type))))
271
272 (defun gnus-spec-tab (column)
273   (if (> column 0)
274       `(insert (make-string (max (- ,column (current-column)) 0) ? ))
275     (let ((column (abs column)))
276       (if gnus-use-correct-string-widths
277           `(progn
278              (if (> (current-column) ,column)
279                  (while (progn
280                           (delete-backward-char 1)
281                           (> (current-column) ,column))))
282              (insert (make-string (max (- ,column (current-column)) 0) ? )))
283         `(progn
284            (if (> (current-column) ,column)
285                (delete-region (point)
286                               (- (point) (- (current-column) ,column)))
287              (insert (make-string (max (- ,column (current-column)) 0)
288                                   ? ))))))))
289
290 (defun gnus-correct-length (string)
291   "Return the correct width of STRING."
292   (let ((length 0))
293     (mapcar (lambda (char) (incf length (char-width char))) string)
294     length))
295
296 (defun gnus-correct-substring (string start &optional end)
297   (let ((wstart 0)
298         (wend 0)
299         (wseek 0)
300         (seek 0)
301         (length (length string))
302         (string (concat string "\0")))
303     ;; Find the start position.
304     (while (and (< seek length)
305                 (< wseek start))
306       (incf wseek (char-width (aref string seek)))
307       (incf seek))
308     (setq wstart seek)
309     ;; Find the end position.
310     (while (and (<= seek length)
311                 (or (not end)
312                     (<= wseek end)))
313       (incf wseek (char-width (aref string seek)))
314       (incf seek))
315     (setq wend seek)
316     (substring string wstart (1- wend))))
317
318 (defun gnus-string-width-function ()
319   (cond