1 ;;; gnus-util.el --- utility functions for Gnus
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
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)
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.
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.
28 ;; Nothing in this file depends on any other parts of Gnus -- all
29 ;; functions and macros in this file are utility functions that are
30 ;; used by Gnus and may be used by any other package without loading
33 ;; [Unfortunately, it does depend on other parts of Gnus, e.g. the
34 ;; autoloads and defvars below...]
40 ;; Fixme: this should be a gnus variable, not nnmail-.
41 (defvar nnmail-pathname-coding-system)
42 (defvar nnmail-active-file-coding-system)
44 ;; Inappropriate references to other parts of Gnus.
45 (defvar gnus-emphasize-whitespace-regexp)
46 (defvar gnus-original-article-buffer)
47 (defvar gnus-user-agent)
53 (autoload 'message-fetch-field "message")
54 (autoload 'gnus-get-buffer-window "gnus-win")
55 (autoload 'rmail-insert-rmail-file-header "rmail")
56 (autoload 'rmail-count-new-messages "rmail")
57 (autoload 'rmail-show-message "rmail")
58 (autoload 'nnheader-narrow-to-headers "nnheader")
59 (autoload 'nnheader-replace-chars-in-string "nnheader"))
63 ((fboundp 'replace-in-string)
64 (defalias 'gnus-replace-in-string 'replace-in-string))
65 ((fboundp 'replace-regexp-in-string)
66 (defun gnus-replace-in-string (string regexp newtext &optional literal)
67 "Replace all matches for REGEXP with NEWTEXT in STRING.
68 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
69 string containing the replacements.
71 This is a compatibility function for different Emacsen."
72 (replace-regexp-in-string regexp newtext string nil literal)))))
74 (defun gnus-boundp (variable)
75 "Return non-nil if VARIABLE is bound and non-nil."
76 (and (boundp variable)
77 (symbol-value variable)))
79 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
80 "Pop to BUFFER, evaluate FORMS, and then return to the original window."
81 (let ((tempvar (make-symbol "GnusStartBufferWindow"))
83 (buf (make-symbol "buf")))
84 `(let* ((,tempvar (selected-window))
86 (,w (gnus-get-buffer-window ,buf 'visible)))
92 (set-buffer (window-buffer ,w)))
95 (select-window ,tempvar)))))
97 (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1)
98 (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body))
100 (defmacro gnus-intern-safe (string hashtable)
101 "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
102 `(let ((symbol (intern ,string ,hashtable)))
107 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
108 ;; to limit the length of a string. This function is necessary since
109 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
110 ;; Fixme: Why not `truncate-string-to-width'?
111 (defsubst gnus-limit-string (str width)
112 (if (> (length str) width)
113 (substring str 0 width)
116 (defsubst gnus-goto-char (point)
117 (and point (goto-char point)))
119 (defmacro gnus-buffer-exists-p (buffer)
120 `(let ((buffer ,buffer))
122 (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
125 ;; The LOCAL arg to `add-hook' is interpreted differently in Emacs and
126 ;; XEmacs. In Emacs we don't need to call `make-local-hook' first.
127 ;; It's harmless, though, so the main purpose of this alias is to shut
128 ;; up the byte compiler.
129 (defalias 'gnus-make-local-hook
130 (if (eq (get 'make-local-hook 'byte-compile)
131 'byte-compile-obsolete)
133 'make-local-hook)) ; XEmacs
135 (defun gnus-delete-first (elt list)
136 "Delete by side effect the first occurrence of ELT as a member of LIST."
137 (if (equal (car list) elt)
140 (while (and (cdr list)
141 (not (equal (cadr list) elt)))
142 (setq list (cdr list)))
144 (setcdr list (cddr list)))
147 ;; Delete the current line (and the next N lines).
148 (defmacro gnus-delete-line (&optional n)
149 `(delete-region (point-at-bol)
150 (progn (forward-line ,(or n 1)) (point))))
152 (defun gnus-byte-code (func)
153 "Return a form that can be `eval'ed based on FUNC."
154 (let ((fval (indirect-function func)))
155 (if (byte-code-function-p fval)
156 (let ((flist (append fval nil)))
157 (setcar flist 'byte-code)
159 (cons 'progn (cddr fval)))))
161 (defun gnus-extract-address-components (from)
162 "Extract address components from a From header.
163 Given an RFC-822 address FROM, extract full name and canonical address.
164 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS). Much more simple
165 solution than `mail-extract-address-components', which works much better, but
168 ;; First find the address - the thing with the @ in it. This may
169 ;; not be accurate in mail addresses, but does the trick most of
170 ;; the time in news messages.
171 (when (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
172 (setq address (substring from (match-beginning 0) (match-end 0))))
173 ;; Then we check whether the "name <address>" format is used.
175 ;; Linear white space is not required.
176 (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
177 (and (setq name (substring from 0 (match-beginning 0)))
178 ;; Strip any quotes from the name.
179 (string-match "^\".*\"$" name)
180 (setq name (substring name 1 (1- (match-end 0))))))
181 ;; If not, then "address (name)" is used.
183 (and (string-match "(.+)" from)
184 (setq name (substring from (1+ (match-beginning 0))
185 (1- (match-end 0)))))
186 (and (string-match "()" from)
188 ;; XOVER might not support folded From headers.
189 (and (string-match "(.*" from)
190 (setq name (substring from (1+ (match-beginning 0))
192 (list (if (string= name "") nil name) (or address from))))
195 (defun gnus-fetch-field (field)
196 "Return the value of the header FIELD of current article."
199 (let ((inhibit-point-motion-hooks t))
200 (nnheader-narrow-to-headers)
201 (message-fetch-field field)))))
203 (defun gnus-fetch-original-field (field)
204 "Fetch FIELD from the original version of the current article."
205 (with-current-buffer gnus-original-article-buffer