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 ;; Prefer `replace-regexp-in-string' (present in Emacs, XEmacs 21.5,
64 ;; SXEmacs 22.1.4) over `replace-in-string'. The later leads to inf-loops
66 ;; (replace-in-string "foo" "/*$" "/")
67 ;; (replace-in-string "xe" "\\(x\\)?" "")
68 ((fboundp 'replace-regexp-in-string)
69 (defun gnus-replace-in-string (string regexp newtext &optional literal)
70 "Replace all matches for REGEXP with NEWTEXT in STRING.
71 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
72 string containing the replacements.
74 This is a compatibility function for different Emacsen."
75 (replace-regexp-in-string regexp newtext string nil literal)))
76 ((fboundp 'replace-in-string)
77 (defalias 'gnus-replace-in-string 'replace-in-string))))
79 (defun gnus-boundp (variable)
80 "Return non-nil if VARIABLE is bound and non-nil."
81 (and (boundp variable)
82 (symbol-value variable)))
84 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
85 "Pop to BUFFER, evaluate FORMS, and then return to the original window."
86 (let ((tempvar (make-symbol "GnusStartBufferWindow"))
88 (buf (make-symbol "buf")))
89 `(let* ((,tempvar (selected-window))
91 (,w (gnus-get-buffer-window ,buf 'visible)))
97 (set-buffer (window-buffer ,w)))
100 (select-window ,tempvar)))))
102 (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1)
103 (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body))
105 (defmacro gnus-intern-safe (string hashtable)
106 "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
107 `(let ((symbol (intern ,string ,hashtable)))
112 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
113 ;; to limit the length of a string. This function is necessary since
114 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
115 ;; Fixme: Why not `truncate-string-to-width'?
116 (defsubst gnus-limit-string (str width)
117 (if (> (length str) width)
118 (substring str 0 width)
121 (defsubst gnus-goto-char (point)
122 (and point (goto-char point)))
124 (defmacro gnus-buffer-exists-p (buffer)
125 `(let ((buffer ,buffer))
127 (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
130 ;; The LOCAL arg to `add-hook' is interpreted differently in Emacs and
131 ;; XEmacs. In Emacs we don't need to call `make-local-hook' first.
132 ;; It's harmless, though, so the main purpose of this alias is to shut
133 ;; up the byte compiler.
134 (defalias 'gnus-make-local-hook
135 (if (eq (get 'make-local-hook 'byte-compile)
136 'byte-compile-obsolete)
138 'make-local-hook)) ; XEmacs
140 (defun gnus-delete-first (elt list)
141 "Delete by side effect the first occurrence of ELT as a member of LIST."
142 (if (equal (car list) elt)
145 (while (and (cdr list)
146 (not (equal (cadr list) elt)))
147 (setq list (cdr list)))
149 (setcdr list (cddr list)))
152 ;; Delete the current line (and the next N lines).
153 (defmacro gnus-delete-line (&optional n)
154 `(delete-region (point-at-bol)
155 (progn (forward-line ,(or n 1)) (point))))
157 (defun gnus-byte-code (func)
158 "Return a form that can be `eval'ed based on FUNC."
159 (let ((fval (indirect-function func)))
160 (if (byte-code-function-p fval)
161 (let ((flist (append fval nil)))
162 (setcar flist 'byte-code)
164 (cons 'progn (cddr fval)))))
166 (defun gnus-extract-address-components (from)
167 "Extract address components from a From header.
168 Given an RFC-822 address FROM, extract full name and canonical address.
169 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS). Much more simple
170 solution than `mail-extract-address-components', which works much better, but
173 ;; First find the address - the thing with the @ in it. This may
174 ;; not be accurate in mail addresses, but does the trick most of
175 ;; the time in news messages.
176 (when (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
177 (setq address (substring from (match-beginning 0) (match-end 0))))
178 ;; Then we check whether the "name <address>" format is used.<