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 (defcustom gnus-completing-read-function 'gnus-emacs-completing-read
48 "Function use to do completing read."
51 :type `(radio (function-item
52 :doc "Use Emacs standard `completing-read' function."
53 gnus-emacs-completing-read)
54 ;; iswitchb.el is very old and ido.el is unavailable
55 ;; in XEmacs, so we exclude those function items.
56 ,@(unless (featurep 'xemacs)
58 :doc "Use `ido-completing-read' function."
59 gnus-ido-completing-read)
61 :doc "Use iswitchb based completing-read function."
62 gnus-iswitchb-completing-read)))))
64 (defcustom gnus-completion-styles
65 (if (and (boundp 'completion-styles-alist)
66 (boundp 'completion-styles))
67 (append (when (and (assq 'substring completion-styles-alist)
68 (not (memq 'substring completion-styles)))
72 "Value of `completion-styles' to use when completing."
77 ;; Fixme: this should be a gnus variable, not nnmail-.
78 (defvar nnmail-pathname-coding-system)
79 (defvar nnmail-active-file-coding-system)
81 ;; Inappropriate references to other parts of Gnus.
82 (defvar gnus-emphasize-whitespace-regexp)
83 (defvar gnus-original-article-buffer)
84 (defvar gnus-user-agent)
86 (autoload 'gnus-get-buffer-window "gnus-win")
87 (autoload 'nnheader-narrow-to-headers "nnheader")
88 (autoload 'nnheader-replace-chars-in-string "nnheader")
89 (autoload 'mail-header-remove-comments "mail-parse")
93 ;; Prefer `replace-regexp-in-string' (present in Emacs, XEmacs 21.5,
94 ;; SXEmacs 22.1.4) over `replace-in-string'. The latter leads to inf-loops
96 ;; (replace-in-string "foo" "/*$" "/")
97 ;; (replace-in-string "xe" "\\(x\\)?" "")
98 ((fboundp 'replace-regexp-in-string)
99 (defun gnus-replace-in-string (string regexp newtext &optional literal)
100 "Replace all matches for REGEXP with NEWTEXT in STRING.
101 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
102 string containing the replacements.
104 This is a compatibility function for different Emacsen."
105 (replace-regexp-in-string regexp newtext string nil literal)))
106 ((fboundp 'replace-in-string)
107 (defalias 'gnus-replace-in-string 'replace-in-string))))
109 (defun gnus-boundp (variable)
110 "Return non-nil if VARIABLE is bound and non-nil."
111 (and (boundp variable)
112 (symbol-value variable)))
114 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
115 "Pop to BUFFER, evaluate FORMS, and then return to the original window."
116 (let ((tempvar (make-symbol "GnusStartBufferWindow"))
117 (w (make-symbol "w"))
118 (buf (make-symbol "buf")))
119 `(let* ((,tempvar (selected-window))
121 (,w (gnus-get-buffer-window ,buf 'visible)))
127 (set-buffer (window-buffer ,w)))
128 (pop-to-buffer ,buf))
130 (select-window ,tempvar)))))
132 (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1)
133 (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body))
135 (defmacro gnus-intern-safe (string hashtable)
136 "Get hash value. Arguments are STRING and HASHTABLE."
137 `(let ((symbol (intern ,string ,hashtable)))
142 (defsubst gnus-goto-char (point)
143 (and point (goto-char point)))
145 (defmacro gnus-buffer-exists-p (buffer)
146 `(let ((buffer ,buffer))
148 (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
151 ;; The LOCAL arg to `add-hook' is interpreted differently in Emacs and
152 ;; XEmacs. In Emacs we don't need to call `make-local-hook' first.
153 ;; It's harmless, though, so the main purpose of this alias is to shut
154 ;; up the byte compiler.
155 (defalias 'gnus-make-local-hook (if (featurep 'xemacs)
159 (defun gnus-delete-first (elt list)
160 "Delete by side effect the first occurrence of ELT as a member of LIST."
161 (if (equal (car list) elt)
164 (while (and (cdr list)
165 (not (equal (cadr list) elt)))
166 (setq list (cdr list)))
168 (setcdr list (cddr list)))
171 ;; Delete the current line (and the next N lines).
172 (defmacro gnus-delete-line (&optional n)
173 `(delete-region (point-at-bol)
174 (progn (forward-line ,(or n 1)) (point))))
176 (defun gnus-byte-code (func)
177 "Return a form that can be `eval'ed based on FUNC."
178 (let ((fval (indirect-function func)))
179 (if (byte-code-function-p fval)
180 (let ((flist (append fval nil)))
181 (setcar flist 'byte-code)
183 (cons 'progn (cddr fval)))))
185 (defun gnus-extract-address-components (from)
186 "Extract address components from a From header.
187 Given an RFC-822 address FROM, extract full name and canonical address.
188 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS). Much more simple
189 solution than `mail-extract-address-components', which works much better, but
192 ;; First find the address - the thing with the @ in it. This may
193 ;; not be accurate in mail addresses, but does the trick most of
194 ;; the time in news messages.
195 (cond (;; Check ``<foo@bar>'' first in order to handle the quite common
196 ;; form ``"abc@xyz" <foo@bar>'' (i.e. ``@'' as part of a comment)
198 (string-match "<\\([^@ \t<>]+[!@][^@ \t<>]+\\)>" from)
199 (setq address (substring from (match-beginning 1) (match-end 1))))
200 ((string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
201 (setq address (substring from (match-beginning 0) (match-end 0)))))
202 ;; Then we check whether the "name <address>" format is used.
204 ;; Linear white space is not required.
205 (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
206 (and (setq name (substring from 0 (match-beginning 0)))
207 ;; Strip any quotes from the name.
208 (string-match "^\".*\"$" name)
209 (setq name (substring name 1 (1- (match-end 0))))))
210 ;; If not, then "address (name)" is used.
212 (and (string-match "(.+)" from)
213 (setq name (substring from (1+ (match-beginning 0))
214 (1- (match-end 0)))))
215 (and (string-match "()" from)
217 ;; XOVER might not support folded From headers.
218 (and (string-match "(.*" from)
219 (setq name (substring from (1+ (match-beginning 0))
221 (list (if (string= name "") nil name) (or address from))))
223 (defun gnus-extract-address-component-name (from)
224 "Extract name from a From header.
225 Uses `gnus-extract-address-components'."
226 (nth 0 (gnus-extract-address-components from)))
228 (defun gnus-extract-address-component-email (from)
229 "Extract e-mail address from a From header.
230 Uses `gnus-extract-address-components'."
231 (nth 1 (gnus-extract-address-components from)))
233 (declare-function message-fetch-field "message" (header &optional not-all))
235 (defun gnus-fetch-field (field)
236 "Return the value of the header FIELD of current article."
240 (let ((inhibit-point-motion-hooks t))
241 (nnheader-narrow-to-headers)
242 (message-fetch-field field)))))
244 (defun gnus-fetch-original-field (field)
245 "Fetch FIELD from the original version of the current article."
246 (with-current-buffer gnus-original-article-buffer
247 (gnus-fetch-field field)))
250 (defun gnus-goto-colon ()
252 (let ((eol (point-at-eol)))
253 (goto-char (or (text-property-any (point) eol 'gnus-position t)
254 (search-forward ":" eol t)
257 (declare-function gnus-find-method-for-group "gnus" (group &optional info))
258 (declare-function gnus-group-name-decode "gnus-group" (string charset))
259 (declare-function gnus-group-name-charset "gnus-group" (method group))
260 ;; gnus-group requires gnus-int which requires message.
261 (declare-function message-tokenize-header "message"
262 (header &optional separator))
264 (defun gnus-decode-newsgroups (newsgroups group &optional method)
265 (require 'gnus-group)
266 (let ((method (or method (gnus-find-method-for-group group))))
267 (mapconcat (lambda (group)
268 (gnus-group-name-decode group (gnus-group-name-charset
270 (message-tokenize-header newsgroups)
273 (defun gnus-remove-text-with-property (prop)
274 "Delete all text in the current buffer with text property PROP."
275 (let ((start (point-min))
277 (unless (get-text-property start prop)
278 (setq start (next-single-property-change start prop)))
280 (setq end (text-property-any start (point-max) prop nil))
281 (delete-region start (or end (point-max)))
282 (setq start (when end
283 (next-single-property-change start prop))))))
285 (defun gnus-newsgroup-directory-form (newsgroup)
286 "Make hierarchical directory name from NEWSGROUP name."
287 (let* ((newsgroup (gnus-newsgroup-savable-name newsgroup))
288 (idx (string-match ":" newsgroup)))
290 (if idx (substring newsgroup 0 idx))
292 (nnheader-replace-chars-in-string
293 (if idx (substring newsgroup (1+ idx)) newsgroup)
296 (defun gnus-newsgroup-savable-name (group)
297 ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
299 (nnheader-replace-chars-in-string group ?/ ?.))
301 (defun gnus-string> (s1 s2)
302 (not (or (string< s1 s2)
305 (defun gnus-string< (s1 s2)
306 "Return t if first arg string is less than second in lexicographic order.
307 Case is significant if and only if `case-fold-search' is nil.
308 Symbols are also allowed; their print names are used instead."
310 (string-lessp (downcase (if (symbolp s1) (symbol-name s1) s1))
311 (downcase (if (symbolp s2) (symbol-name s2) s2)))
312 (string-lessp s1 s2)))
316 (defun gnus-file-newer-than (file date)
317 (let ((fdate (nth 5 (file-attributes file))))
318 (or (> (car fdate) (car date))
319 (and (= (car fdate) (car date))
320 (> (nth 1 fdate) (nth 1 date))))))
323 (if (and (fboundp 'float-time)
324 (subrp (symbol-function 'float-time)))
325 (defalias 'gnus-float-time 'float-time)
326 (defun gnus-float-time (&optional time)
327 "Convert time value TIME to a floating point number.
328 TIME defaults to the current time."
329 (with-no-warnings (time-to-seconds (or time (current-time)))))))
333 (defmacro gnus-local-set-keys (&rest plist)
334 "Set the keys in PLIST in the current keymap."
335 `(gnus-define-keys-1 (current-local-map) ',plist))
337 (defmacro gnus-define-keys (keymap &rest plist)
338 "Define all keys in PLIST in KEYMAP."
339 `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
341 (defmacro gnus-define-keys-safe (keymap &rest plist)
342 "Define all keys in PLIST in KEYMAP without overwriting previous definitions."
343 `(gnus-define-keys-1 (quote ,keymap) (quote ,plist) t))
345 (put 'gnus-define-keys 'lisp-indent-function 1)
346 (put 'gnus-define-keys-safe 'lisp-indent-function 1)
347 (put 'gnus-local-set-keys 'lisp-indent-function 1)
349 (defmacro gnus-define-keymap (keymap &rest plist)
350 "Define all keys in PLIST in KEYMAP."
351 `(gnus-define-keys-1 ,keymap (quote ,plist)))
353 (put 'gnus-define-keymap 'lisp-indent-function 1)
355 (defun gnus-define-keys-1 (keymap plist &optional safe)
357 (error "Can't set keys in a null keymap"))
358 (cond ((symbolp keymap)
359 (setq keymap (symbol-value keymap)))
362 (set (car keymap) nil)
363 (define-prefix-command (car keymap))
364 (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
365 (setq keymap (symbol-value (car keymap)))))
368 (when (symbolp (setq key (pop plist)))
369 (setq key (symbol-value key)))
371 (eq (lookup-key keymap key) 'undefined))
372 (define-key keymap key (pop plist))
375 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
378 ;; Do we really need these functions? Workarounds for bugs in the corresponding
379 ;; Emacs functions? Maybe these bugs are no longer present in any supported
380 ;; (X)Emacs version? Alias them to the original functions and see if anyone
381 ;; reports a problem. If not, replace with original functions. --rsteib,
384 ;; All supported Emacsen clear the echo area after `yes-or-no-p', so we can
385 ;; remove `yes-or-no-p'. RMS says that not clearing after `y-or-n-p' is
386 ;; intentional (see below), so we could remove `gnus-y-or-n-p' too.
387 ;; Objections? --rsteib, 2008-02-16
389 ;; ,----[ http://thread.gmane.org/gmane.emacs.gnus.general/65099/focus=66070 ]
390 ;; | From: Richard Stallman
391 ;; | Subject: Re: Do we need gnus-yes-or-no-p and gnus-y-or-n-p?
392 ;; | To: Katsumi Yamaoka [...]
393 ;; | Cc: emacs-devel@[...], xemacs-beta@[...], ding@[...]
394 ;; | Date: Mon, 07 Jan 2008 12:16:05 -0500
395 ;; | Message-ID: <E1JBva1-000528-VY@fencepost.gnu.org>
397 ;; | The behavior of `y-or-n-p' that it doesn't clear the question
398 ;; | and the answer is not serious of course, but I feel it is not
401 ;; | It is intentional.
403 ;; | Currently, it is commented out in the trunk by Reiner Steib. He
404 ;; | also wrote the benefit of leaving the question and the answer in
405 ;; | the echo area as follows:
407 ;; | (http://article.gmane.org/gmane.emacs.gnus.general/66061)
408 ;; | > In contrast to yes-or-no-p it is much easier to type y, n,
409 ;; | > SPC, DEL, etc accidentally, so it might be useful for the user
410 ;; | > to see what he has typed.
412 ;; | Yes, that is the reason.
415 ;; (defun gnus-y-or-n-p (prompt)
419 ;; (defun gnus-yes-or-no-p (prompt)
421 ;; (yes-or-no-p prompt)
424 (defalias 'gnus-y-or-n-p 'y-or-n-p)
425 (defalias 'gnus-yes-or-no-p 'yes-or-no-p)
427 ;; By Frank Schmitt <ich@Frank-Schmitt.net>. Allows to have
428 ;; age-depending date representations. (e.g. just the time if it's
429 ;; from today, the day of the week if it's within the last 7 days and
430 ;; the full date if it's older)
432 (defun gnus-seconds-today ()
433 "Return the number of seconds passed today."
434 (let ((now (decode-time (current-time))))
435 (+ (car now) (* (car (cdr now)) 60) (* (car (nthcdr 2 now)) 3600))))
437 (defun gnus-seconds-month ()
438 "Return the number of seconds passed this month."
439 (let ((now (decode-time (current-time))))
440 (+ (car now) (* (car (cdr now)) 60) (* (car (nthcdr 2 now)) 3600)
441 (* (- (car (nthcdr 3 now)) 1) 3600 24))))
443 (defun gnus-seconds-year ()
444 "Return the number of seconds passed this year."
445 (let ((now (decode-time (current-time)))
446 (days (format-time-string "%j" (current-time))))
447 (+ (car now) (* (car (cdr now)) 60) (* (car (nthcdr 2 now)) 3600)
448 (* (- (string-to-number days) 1) 3600 24))))
450 (defmacro gnus-date-get-time (date)
451 "Convert DATE string to Emacs time.
452 Cache the result as a text property stored in DATE."
453 ;; Either return the cached value...
457 (or (get-text-property 0 'gnus-time d)
458 ;; or compute the value...
459 (let ((time (safe-date-to-time d)))
460 ;; and store it back in the string.
461 (put-text-property 0 1 'gnus-time time d)
464 (defvar gnus-user-date-format-alist
465 '(((gnus-seconds-today) . "%k:%M")
466 (604800 . "%a %k:%M") ;;that's one week
467 ((gnus-seconds-month) . "%a %d")
468 ((gnus-seconds-year) . "%b %d")
469 (t . "%b %d '%y")) ;;this one is used when no
471 "Specifies date format depending on age of article.
472 This is an alist of items (AGE . FORMAT). AGE can be a number (of
473 seconds) or a Lisp expression evaluating to a number. When the age of
474 the article is less than this number, then use `format-time-string'
475 with the corresponding FORMAT for displaying the date of the article.
476 If AGE is not a number or a Lisp expression evaluating to a
477 non-number, then the corresponding FORMAT is used as a default value.
479 Note that the list is processed from the beginning, so it should be
480 sorted by ascending AGE. Also note that items following the first
481 non-number AGE will be ignored.
483 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
484 and `gnus-seconds-year' in the AGE spec. They return the number of
485 seconds passed since the start of today, of this month, of this year,
488 (defun gnus-user-date (messy-date)
489 "Format the messy-date according to gnus-user-date-format-alist.
490 Returns \" ? \" if there's bad input or if another error occurs.
491 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
493 (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date)))
494 (now (gnus-float-time))
495 ;;If we don't find something suitable we'll use this one
496 (my-format "%b %d '%y"))
497 (let* ((difference (- now messy-date))
498 (templist gnus-user-date-format-alist)
499 (top (eval (caar templist))))
500 (while (if (numberp top) (< top difference) (not top))
502 (setq templist (cdr templist))
503 (setq top (eval (caar templist)))))
504 (if (stringp (cdr (car templist)))
505 (setq my-format (cdr (car templist)))))
506 (format-time-string (eval my-format) (seconds-to-time messy-date)))
509 (defun gnus-dd-mmm (messy-date)
510 "Return a string like DD-MMM from a big messy string."
512 (format-time-string "%d-%b" (gnus-date-get-time messy-date))
515 (defsubst gnus-time-iso8601 (time)
516 "Return a string of TIME in YYYYMMDDTHHMMSS format."
517 (format-time-string "%Y%m%dT%H%M%S" time))
519 (defun gnus-date-iso8601 (date)
520 "Convert the DATE to YYYYMMDDTHHMMSS."
522 (gnus-time-iso8601 (gnus-date-get-time date))
525 (defun gnus-mode-string-quote (string)
526 "Quote all \"%\"'s in STRING."
527 (gnus-replace-in-string string "%" "%%"))