1 ;;; gnus-util.el --- utility functions for Gnus
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 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 3 of the License, or
14 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
26 ;; Nothing in this file depends on any other parts of Gnus -- all
27 ;; functions and macros in this file are utility functions that are
28 ;; used by Gnus and may be used by any other package without loading
31 ;; [Unfortunately, it does depend on other parts of Gnus, e.g. the
32 ;; autoloads and defvars below...]
38 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
43 (unless (fboundp 'with-no-warnings)
44 (defmacro with-no-warnings (&rest body)
47 ;; Fixme: this should be a gnus variable, not nnmail-.
48 (defvar nnmail-pathname-coding-system)
49 (defvar nnmail-active-file-coding-system)
51 ;; Inappropriate references to other parts of Gnus.
52 (defvar gnus-emphasize-whitespace-regexp)
53 (defvar gnus-original-article-buffer)
54 (defvar gnus-user-agent)
56 (autoload 'gnus-get-buffer-window "gnus-win")
57 (autoload 'nnheader-narrow-to-headers "nnheader")
58 (autoload 'nnheader-replace-chars-in-string "nnheader")
59 (autoload 'mail-header-remove-comments "mail-parse")
63 ;; Prefer `replace-regexp-in-string' (present in Emacs, XEmacs 21.5,
64 ;; SXEmacs 22.1.4) over `replace-in-string'. The latter 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 "Get hash value. Arguments are STRING and HASHTABLE."
107 `(let ((symbol (intern ,string ,hashtable)))
112 (defsubst gnus-goto-char (point)
113 (and point (goto-char point)))
115 (defmacro gnus-buffer-exists-p (buffer)
116 `(let ((buffer ,buffer))
118 (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
121 ;; The LOCAL arg to `add-hook' is interpreted differently in Emacs and
122 ;; XEmacs. In Emacs we don't need to call `make-local-hook' first.
123 ;; It's harmless, though, so the main purpose of this alias is to shut
124 ;; up the byte compiler.
125 (defalias 'gnus-make-local-hook
126 (if (eq (get 'make-local-hook 'byte-compile)
127 'byte-compile-obsolete)
129 'make-local-hook)) ; XEmacs
131 (defun gnus-delete-first (elt list)
132 "Delete by side effect the first occurrence of ELT as a member of LIST."
133 (if (equal (car list) elt)
136 (while (and (cdr list)
137 (not (equal (cadr list) elt)))
138 (setq list (cdr list)))
140 (setcdr list (cddr list)))
143 ;; Delete the current line (and the next N lines).
144 (defmacro gnus-delete-line (&optional n)
145 `(delete-region (point-at-bol)
146 (progn (forward-line ,(or n 1)) (point))))
148 (defun gnus-byte-code (func)
149 "Return a form that can be `eval'ed based on FUNC."