1 ;;; gnus-util.el --- utility functions for Gnus
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
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
38 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
39 "Pop to BUFFER, evaluate FORMS, and then return to the original window."
40 (let ((tempvar (make-symbol "GnusStartBufferWindow"))
42 (buf (make-symbol "buf")))
43 `(let* ((,tempvar (selected-window))
45 (,w (get-buffer-window ,buf 'visible)))
52 (select-window ,tempvar)))))
54 (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1)
55 (put 'gnus-eval-in-buffer-window 'lisp-indent-hook 1)
56 (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body))
58 (defmacro gnus-intern-safe (string hashtable)
59 "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
60 `(let ((symbol (intern ,string ,hashtable)))
65 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
66 ;; function `substring' might cut on a middle of multi-octet
68 (defun gnus-truncate-string (str width)
69 (substring str 0 width))
71 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
72 ;; to limit the length of a string. This function is necessary since
73 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
74 (defsubst gnus-limit-string (str width)
75 (if (> (length str) width)
76 (substring str 0 width)
79 (defsubst gnus-functionp (form)
80 "Return non-nil if FORM is funcallable."
81 (or (and (symbolp form) (fboundp form))
82 (and (listp form) (eq (car form) 'lambda))))
84 (defsubst gnus-goto-char (point)
85 (and point (goto-char point)))
87 (defmacro gnus-buffer-exists-p (buffer)
88 `(let ((buffer ,buffer))
90 (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
93 (defmacro gnus-kill-buffer (buffer)
95 (if (gnus-buffer-exists-p buf)
98 (defsubst gnus-point-at-bol ()
99 "Return point at the beginning of the line."
106 (defsubst gnus-point-at-eol ()
107 "Return point at the end of the line."
114 (defun gnus-delete-first (elt list)
115 "Delete by side effect the first occurrence of ELT as a member of LIST."
116 (if (equal (car list) elt)
119 (while (and (cdr list)
120 (not (equal (cadr list) elt)))
121 (setq list (cdr list)))
123 (setcdr list (cddr list)))
126 ;; Delete the current line (and the next N lines).
127 (defmacro gnus-delete-line (&optional n)
128 `(delete-region (progn (beginning-of-line) (point))
129 (progn (forward-line ,(or n 1)) (point))))
131 (defun gnus-byte-code (func)
132 "Return a form that can be `eval'ed based on FUNC."
133 (let ((fval (symbol-function func)))
134 (if (byte-code-function-p fval)
135 (let ((flist (append fval nil)))
136 (setcar flist 'byte-code)
138 (cons 'progn (cddr fval)))))
140 (defun gnus-extract-address-components (from)
142 ;; First find the address - the thing with the @ in it. This may
143 ;; not be accurate in mail addresses, but does the trick most of
144 ;; the time in news messages.
145 (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
146 (setq address (substring from (match-beginning 0) (match-end 0))))
147 ;; Then we check whether the "name <address>" format is used.
149 ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
150 ;; Linear white space is not required.
151 (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
152 (and (setq name (substring from 0 (match-beginning 0)))
153 ;; Strip any quotes from the name.
154 (string-match "\".*\"" name)
155 (setq name (substring name 1 (1- (match-end 0))))))
156 ;; If not, then "address (name)" is used.
158 (and (string-match "(.+)" from)
159 (setq name (substring from (1+ (match-beginning 0))
160 (1- (match-end 0)))))
161 (and (string-match "()" from)
163 ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
164 ;; XOVER might not support folded From headers.
165 (and (string-match "(.*" from)
166 (setq name (substring from (1+ (match-beginning 0))
168 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
169 (list (or name from) (or address from))))
171 (defun gnus-fetch-field (field)
172 "Return the value of the header FIELD of current article."
175 (let ((case-fold-search t)
176 (inhibit-point-motion-hooks t))
177 (nnheader-narrow-to-headers)
178 (message-fetch-field field)))))
180 (defun gnus-goto-colon ()
182 (search-forward ":" (gnus-point-at-eol) t))
184 (defun gnus-remove-text-with-property (prop)
185 "Delete all text in the current buffer with text property PROP."
187 (goto-char (point-min))
189 (while (get-text-property (point) prop)
191 (goto-char (next-single-property-change (point) prop nil (point-max))))))
193 (defun gnus-newsgroup-directory-form (newsgroup)
194 "Make hierarchical directory name from NEWSGROUP name."
195 (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
196 (len (length newsgroup))
198 ;; If this is a foreign group, we don't want to translate the
200 (if (setq idx (string-match ":" newsgroup))
201 (aset newsgroup idx ?/)
203 ;; Replace all occurrences of `.' with `/'.
205 (if (= (aref newsgroup idx) ?.)
206 (aset newsgroup idx ?/))
210 (defun gnus-newsgroup-savable-name (group)
211 ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
213 (nnheader-replace-chars-in-string group ?/ ?.))
215 (defun gnus-string> (s1 s2)
216 (not (or (string< s1 s2)
221 (defun gnus-days-between (date1 date2)
222 ;; Return the number of days between date1 and date2.
223 (- (gnus-day-number date1) (gnus-day-number date2)))
225 (defun gnus-day-number (date)
226 (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
227 (timezone-parse-date date))))
228 (timezone-absolute-from-gregorian
229 (nth 1 dat) (nth 2 dat) (car dat))))
231 (defun gnus-encode-date (date)
232 "Convert DATE to internal time."
233 (let* ((parse (timezone-parse-date date))
234 (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
235 (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
236 (encode-time (caddr time) (cadr time) (car time)
237 (caddr date) (cadr date) (car date) (nth 4 date))))
239 (defun gnus-time-minus (t1 t2)
240 "Subtract two internal times."
241 (let ((borrow (< (cadr t1) (cadr t2))))
242 (list (- (car t1) (car t2) (if borrow 1 0))
243 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
245 (defun gnus-time-less (t1 t2)
246 "Say whether time T1 is less than time T2."
247 (or (< (car t1) (car t2))
248 (and (= (car t1) (car t2))
249 (< (nth 1 t1) (nth 1 t2)))))
251 (defun gnus-file-newer-than (file date)
252 (let ((fdate (nth 5 (file-attributes file))))
253 (or (> (car fdate) (car date))
254 (and (= (car fdate) (car date))
255 (> (nth 1 fdate) (nth 1 date))))))
259 (defmacro gnus-local-set-keys (&rest plist)
260 "Set the keys in PLIST in the current keymap."
261 `(gnus-define-keys-1 (current-local-map) ',plist))
263 (defmacro gnus-define-keys (keymap &rest plist)
264 "Define all keys in PLIST in KEYMAP."
265 `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
267 (put 'gnus-define-keys 'lisp-indent-function 1)
268 (put 'gnus-define-keys 'lisp-indent-hook 1)
269 (put 'gnus-define-keymap 'lisp-indent-function 1)
270 (put 'gnus-define-keymap 'lisp-indent-hook 1)
272 (defmacro gnus-define-keymap (keymap &rest plist)
273 "Define all keys in PLIST in KEYMAP."
274 `(gnus-define-keys-1 ,keymap (quote ,plist)))
276 (defun gnus-define-keys-1 (keymap plist)
278 (error "Can't set keys in a null keymap"))
279 (cond ((symbolp keymap)
280 (setq keymap (symbol-value keymap)))
283 (set (car keymap) nil)
284 (define-prefix-command (car keymap))
285 (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
286 (setq keymap (symbol-value (car keymap)))))
289 (when (symbolp (setq key (pop plist)))
290 (setq key (symbol-value key)))
291 (define-key keymap key (pop plist)))))
293 (defun gnus-completing-read (default prompt &rest args)
294 ;; Like `completing-read', except that DEFAULT is the default argument.
295 (let* ((prompt (if default
296 (concat prompt " (default " default ") ")
297 (concat prompt " ")))
298 (answer (apply 'completing-read prompt args)))
299 (if (or (null answer) (zerop (length answer)))
303 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
305 (defun gnus-y-or-n-p (prompt)
310 (defun gnus-yes-or-no-p (prompt)
315 ;; I suspect there's a better way, but I haven't taken the time to do
316 ;; it yet. -erik selberg@cs.washington.edu
317 (defun gnus-dd-mmm (messy-date)
318 "Return a string like DD-MMM from a big messy string"
319 (let ((datevec (condition-case () (timezone-parse-date messy-date)
325 ;; Make sure leading zeroes are stripped.
326 (number-to-string (string-to-number (aref datevec 2)))
330 (nth (1- (string-to-number (aref datevec 1)))
331 timezone-months-assoc))
334 (defun gnus-mode-string-quote (string)
335 "Quote all \"%\" in STRING."
337 (gnus-set-work-buffer)
339 (goto-char (point-min))
340 (while (search-forward "%" nil t)
344 ;; Make a hash table (default and minimum size is 255).
345 ;; Optional argument HASHSIZE specifies the table size.
346 (defun gnus-make-hashtable (&optional hashsize)
347 (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
349 ;; Make a number that is suitable for hashing; bigger than MIN and one
351 (defun gnus-create-hash-size (min)
357 (defvar gnus-verbose 7
358 "*Integer that says how verbose Gnus should be.
359 The higher the number, the more messages Gnus will flash to say what
360 it's doing. At zero, Gnus will be totally mute; at five, Gnus will
361 display most important messages; and at ten, Gnus will keep on
362 jabbering all the time.")
364 ;; Show message if message has a lower level than `gnus-verbose'.
365 ;; Guideline for numbers:
366 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
367 ;; for things that take a long time, 7 - not very important messages
368 ;; on stuff, 9 - messages inside loops.
369 (defun gnus-message (level &rest args)
370 (if (<= level gnus-verbose)
371 (apply 'message args)
372 ;; We have to do this format thingy here even if the result isn't
373 ;; shown - the return value has to be the same as the return value
375 (apply 'format args)))
377 (defun gnus-error (level &rest args)
378 "Beep an error if LEVEL is equal to or less than `gnus-verbose'."
379 (when (<= (floor level) gnus-verbose)
380 (apply 'message args)
383 (when (and (floatp level)
384 (not (zerop (setq duration (* 10 (- level (floor level)))))))
385 (sit-for duration))))
388 (defun gnus-parent-id (references)
389 "Return the last Message-ID in REFERENCES."
390 (when (and references
391 (string-match "\\(<[^\n<>]+>\\)[ \t\n]*\\'" references))
392 (substring references (match-beginning 1) (match-end 1))))
394 (defun gnus-split-references (references)
395 "Return a list of Message-IDs in REFERENCES."
398 (while (string-match "<[^>]+>" references beg)
399 (push (substring references (match-beginning 0) (setq beg (match-end 0)))
403 (defun gnus-buffer-live-p (buffer)
404 "Say whether BUFFER is alive or not."
407 (buffer-name (get-buffer buffer))))
409 (defun gnus-horizontal-recenter ()
410 "Recenter the current buffer horizontally."
411 (if (< (current-column) (/ (window-width) 2))
412 (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
413 (let* ((orig (point))
414 (end (window-end (get-buffer-window (current-buffer) t)))
416 ;; Find the longest line currently displayed in the window.
417 (goto-char (window-start))
418 (while (and (not (eobp))
421 (setq max (max max (current-column)))
424 ;; Scroll horizontally to center (sort of) the point.
425 (if (> max (window-width))
427 (get-buffer-window (current-buffer) t)
428 (min (- (current-column) (/ (window-width) 3))
429 (+ 2 (- max (window-width)))))
430 (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
433 (defun gnus-read-event-char ()
434 "Get the next event."
435 (let ((event (read-event)))
436 (cons (and (numberp event) event) event)))
438 (defun gnus-sortable-date (date)
439 "Make sortable string by string-lessp from DATE.
440 Timezone package is used."
443 (setq date (inline (timezone-fix-time
445 (aref (inline (timezone-parse-date date)) 4))))
447 (timezone-make-sortable-date
448 (aref date 0) (aref date 1) (aref date 2)
450 (timezone-make-time-string
451 (aref date 3) (aref date 4) (aref date 5))))))
454 (defun gnus-copy-file (file &optional to)
457 (list (read-file-name "Copy file: " default-directory)
458 (read-file-name "Copy file to: " default-directory)))
459 (or to (setq to (read-file-name "Copy file to: " default-directory)))
460 (and (file-directory-p to)
461 (setq to (concat (file-name-as-directory to)
462 (file-name-nondirectory file))))
465 (defun gnus-kill-all-overlays ()
466 "Delete all overlays in the current buffer."
467 (when (fboundp 'overlay-lists)
468 (let* ((overlayss (overlay-lists))
469 (buffer-read-only nil)
470 (overlays (nconc (car overlayss) (cdr overlayss))))
472 (delete-overlay (pop overlays))))))
474 (defvar gnus-work-buffer " *gnus work*")
476 (defun gnus-set-work-buffer ()
477 "Put point in the empty Gnus work buffer."
478 (if (get-buffer gnus-work-buffer)
480 (set-buffer gnus-work-buffer)
482 (set-buffer (get-buffer-create gnus-work-buffer))
483 (kill-all-local-variables)
484 (buffer-disable-undo (current-buffer))))
486 (defmacro gnus-group-real-name (group)
487 "Find the real name of a foreign newsgroup."
488 `(let ((gname ,group))
489 (if (string-match ":[^:]+$" gname)
490 (substring gname (1+ (match-beginning 0)))
493 (defun gnus-make-sort-function (funs)
494 "Return a composite sort condition based on the functions in FUNC."
496 `(or (,(car funs) t1 t2)
497 (and (not (,(car funs) t2 t1))
498 ,(gnus-make-sort-function (cdr funs))))
499 `(,(car funs) t1 t2)))
503 ;;; gnus-util.el ends here