1 ;;; elmo-util.el -- Utilities for Elmo.
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
8 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
34 (eval-when-compile (require 'cl))
36 (require 'eword-decode)
39 (defmacro elmo-set-buffer-multibyte (flag)
40 "Set the multibyte flag of the current buffer to FLAG."
42 (list 'setq 'mc-flag flag))
45 ((and (boundp 'emacs-major-version) (>= emacs-major-version 20))
46 (list 'set-buffer-multibyte flag))
50 (defvar elmo-work-buf-name " *elmo work*")
51 (defvar elmo-temp-buf-name " *elmo temp*")
53 (or (boundp 'default-enable-multibyte-characters)
54 (defvar default-enable-multibyte-characters (featurep 'mule)
55 "The mock variable except for Emacs 20."))
57 (defun elmo-base64-encode-string (string &optional no-line-break))
58 (defun elmo-base64-decode-string (string))
60 ;; base64 encoding/decoding
62 (fset 'elmo-base64-encode-string
63 (mel-find-function 'mime-encode-string "base64"))
64 (fset 'elmo-base64-decode-string
65 (mel-find-function 'mime-decode-string "base64"))
67 ;; Any Emacsen may have add-name-to-file(), because loadup.el requires it. :-p
68 ;; Check make-symbolic-link() instead. -- 981002 by Fuji
69 (if (fboundp 'make-symbolic-link) ;; xxx
70 (defalias 'elmo-add-name-to-file 'add-name-to-file)
71 (defun elmo-add-name-to-file
72 (filename newname &optional ok-if-already-exists)
73 (copy-file filename newname ok-if-already-exists t)))
75 (defsubst elmo-call-func (folder func-name &rest args)
76 (let* ((spec (if (stringp folder)
77 (elmo-folder-get-spec folder)
79 (type (symbol-name (car spec)))
80 (backend-str (concat "elmo-" type))
81 (backend-sym (intern backend-str)))
82 (unless (featurep backend-sym)
83 (require backend-sym))
84 (apply (intern (format "%s-%s" backend-str func-name))
88 ;; Nemacs's `read' is different.
89 (static-if (fboundp 'nemacs-version)
90 (defun elmo-read (obj)
93 (or (bobp) (forward-char -1)))))
94 (defalias 'elmo-read 'read))
96 (defmacro elmo-set-work-buf (&rest body)
97 "Execute BODY on work buffer. Work buffer remains."
99 (set-buffer (get-buffer-create elmo-work-buf-name))
100 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
104 (defmacro elmo-match-substring (pos string from)
105 "Substring of POSth matched string of STRING."
106 (` (substring (, string)
107 (+ (match-beginning (, pos)) (, from))
108 (match-end (, pos)))))
110 (defmacro elmo-match-string (pos string)
111 "Substring POSth matched STRING."
112 (` (substring (, string) (match-beginning (, pos)) (match-end (, pos)))))
114 (defmacro elmo-match-buffer (pos)
115 "Substring POSth matched from the current buffer."
116 (` (buffer-substring-no-properties
117 (match-beginning (, pos)) (match-end (, pos)))))
119 (defmacro elmo-bind-directory (dir &rest body)
120 "Set current directory DIR and execute BODY."
121 (` (let ((default-directory (file-name-as-directory (, dir))))
124 (defmacro elmo-folder-get-type (folder)
125 "Get type of FOLDER."
126 (` (and (stringp (, folder))
127 (cdr (assoc (string-to-char (, folder)) elmo-spec-alist)))))
129 (defun elmo-object-load (filename &optional mime-charset no-err)
130 "Load OBJECT from the file specified by FILENAME.
131 File content is decoded with MIME-CHARSET."
132 (if (not (file-readable-p filename))
135 (as-binary-input-file
136 (insert-file-contents filename))
138 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
139 (decode-mime-charset-region (point-min) (point-max) mime-charset))
141 (read (current-buffer))
142 (error (unless no-err
143 (message "Warning: Loading object from %s failed."
145 (elmo-object-save filename nil))
148 (defsubst elmo-save-buffer (filename &optional mime-charset)
149 "Save current buffer to the file specified by FILENAME.
150 Directory of the file is created if it doesn't exist.
151 File content is encoded with MIME-CHARSET."
152 (let ((dir (directory-file-name (file-name-directory filename))))
153 (if (file-directory-p dir)
155 (unless (file-exists-p dir)
156 (elmo-make-directory dir)))
157 (if (file-writable-p filename)
160 ;;; (elmo-set-buffer-multibyte default-enable-multibyte-characters)
161 (encode-mime-charset-region (point-min) (point-max) mime-charset))
162 (as-binary-output-file
163 (write-region (point-min) (point-max) filename nil 'no-msg)))
164 (message (format "%s is not writable." filename)))))
166 (defun elmo-object-save (filename object &optional mime-charset)
167 "Save OBJECT to the file specified by FILENAME.
168 Directory of the file is created if it doesn't exist.
169 File content is encoded with MIME-CHARSET."
171 (prin1 object (current-buffer))
172 ;;;(princ "\n" (current-buffer))
173 (elmo-save-buffer filename mime-charset)))
175 (defsubst elmo-imap4-decode-folder-string (string)
176 (if elmo-imap4-use-modified-utf7
177 (utf7-decode-string string 'imap)
180 (defsubst elmo-imap4-encode-folder-string (string)
181 (if elmo-imap4-use-modified-utf7
182 (utf7-encode-string string 'imap)
185 (defun elmo-get-network-stream-type (stream-type stream-type-alist)
187 (while stream-type-alist
188 (if (eq (nth 1 (car stream-type-alist)) stream-type)
189 (throw 'found (car stream-type-alist)))
190 (setq stream-type-alist (cdr stream-type-alist)))))
192 (defun elmo-network-get-spec (folder server port stream-type stream-type-alist)
193 (setq stream-type (elmo-get-network-stream-type
194 stream-type stream-type-alist))
195 (when (string-match "\\(@[^@:/!]+\\)?\\(:[0-9]+\\)?\\(!.*\\)?$" folder)
196 (if (match-beginning 1)
197 (setq server (elmo-match-substring 1 folder 1)))
198 (if (match-beginning 2)
199 (setq port (string-to-int (elmo-match-substring 2 folder 1))))
200 (if (match-beginning 3)
201 (setq stream-type (assoc (elmo-match-string 3 folder)
203 (setq folder (substring folder 0 (match-beginning 0))))
204 (cons folder (list server port stream-type)))
206 (defun elmo-imap4-get-spec (folder)
207 (let ((default-user elmo-default-imap4-user)
208 (default-server elmo-default-imap4-server)
209 (default-port elmo-default-imap4-port)
210 (default-stream-type elmo-default-imap4-stream-type)
211 (stream-type-alist elmo-network-stream-type-alist)
212 spec mailbox user auth)
213 (when (string-match "\\(.*\\)@\\(.*\\)" default-server)
214 ;; case: default-imap4-server is specified like
215 ;; "hoge%imap.server@gateway".
216 (setq default-user (elmo-match-string 1 default-server))
217 (setq default-server (elmo-match-string 2 default-server)))
218 (if elmo-imap4-stream-type-alist
219 (setq stream-type-alist
220 (append elmo-imap4-stream-type-alist stream-type-alist)))
221 (setq spec (elmo-network-get-spec
222 folder default-server default-port default-stream-type
224 (setq folder (car spec))
226 "^\\(%\\)\\([^:@!]*\\)\\(:[^/!]+\\)?\\(/[^/:@!]+\\)?"
229 (setq mailbox (if (match-beginning 2)
230 (elmo-match-string 2 folder)
231 elmo-default-imap4-mailbox))
232 (setq user (if (match-beginning 3)
233 (elmo-match-substring 3 folder 1)
235 (setq auth (if (match-beginning 4)
236 (intern (elmo-match-substring 4 folder 1))
237 elmo-default-imap4-authenticate-type))
238 (setq auth (or auth 'clear))
240 (elmo-imap4-encode-folder-string mailbox)
244 (defsubst elmo-imap4-spec-mailbox (spec)
247 (defsubst elmo-imap4-spec-username (spec)
250 (defsubst elmo-imap4-spec-auth (spec)
253 (defsubst elmo-imap4-spec-hostname (spec)
256 (defsubst elmo-imap4-spec-port (spec)
259 (defsubst elmo-imap4-spec-stream-type (spec)
262 (defalias 'elmo-imap4-spec-folder 'elmo-imap4-spec-mailbox)
263 (make-obsolete 'elmo-imap4-spec-folder 'elmo-imap4-spec-mailbox)
265 (defsubst elmo-imap4-connection-get-process (conn)
268 (defsubst elmo-imap4-connection-get-buffer (conn)
271 (defsubst elmo-imap4-connection-get-cwf (conn)
274 (defun elmo-nntp-get-spec (folder)
275 (let ((stream-type-alist elmo-network-stream-type-alist)
277 (if elmo-nntp-stream-type-alist
278 (setq stream-type-alist
279 (append elmo-nntp-stream-type-alist stream-type-alist)))
280 (setq spec (elmo-network-get-spec folder
281 elmo-default-nntp-server
282 elmo-default-nntp-port
283 elmo-default-nntp-stream-type
285 (setq folder (car spec))
287 "^\\(-\\)\\([^:@!]*\\)\\(:[^/!]+\\)?\\(/[^/:@!]+\\)?"
290 (if (match-beginning 2)
291 (elmo-match-string 2 folder)))
293 (if (match-beginning 3)
294 (elmo-match-substring 3 folder 1)
295 elmo-default-nntp-user))
296 (append (list 'nntp group user)
299 (defsubst elmo-nntp-spec-group (spec)
302 (defsubst elmo-nntp-spec-username (spec)
306 ;; (defsubst elmo-nntp-spec-auth (spec))
308 (defsubst elmo-nntp-spec-hostname (spec)
311 (defsubst elmo-nntp-spec-port (spec)
314 (defsubst elmo-nntp-spec-stream-type (spec)
317 (defun elmo-localdir-get-spec (folder)
320 "^\\(\\+\\)\\(.*\\)$"
322 (if (eq (length (setq fld-name
323 (elmo-match-string 2 folder))) 0)
326 (if (file-name-absolute-p fld-name)
327 (setq path (expand-file-name fld-name))
328 ;;; (setq path (expand-file-name fld-name
329 ;;; elmo-localdir-folder-path))
330 (setq path fld-name))
331 (list (if (elmo-folder-maildir-p folder)
335 (defun elmo-maildir-get-spec (folder)
338 "^\\(\\.\\)\\(.*\\)$"
340 (if (eq (length (setq fld-name
341 (elmo-match-string 2 folder))) 0)
343 (if (file-name-absolute-p fld-name)
344 (setq path (expand-file-name fld-name))
345 (setq path fld-name))
346 (list 'maildir path))))
348 (defun elmo-folder-maildir-p (folder)
350 (let ((li elmo-maildir-list))
352 (if (string-match (car li) folder)
354 (setq li (cdr li))))))
356 (defun elmo-localnews-get-spec (folder)
361 (if (eq (length (setq fld-name
362 (elmo-match-string 2 folder))) 0)
366 (elmo-replace-in-string fld-name "\\." "/")))))
368 (defun elmo-cache-get-spec (folder)
373 (if (eq (length (setq fld-name
374 (elmo-match-string 2 folder))) 0)
378 (elmo-replace-in-string fld-name "\\." "/")))))
380 ;; Archive interface by OKUNISHI Fujikazu <fuji0924@mbox.kyoto-inet.or.jp>
381 (defun elmo-archive-get-spec (folder)
382 (require 'elmo-archive)
383 (let (fld-name type prefix)
385 "^\\(\\$\\)\\([^;]*\\);?\\([^;]*\\);?\\([^;]*\\)$"
387 ;; Drive letter is OK!
388 (if (eq (length (setq fld-name
389 (elmo-match-string 2 folder))) 0)
392 (if (eq (length (setq type
393 (elmo-match-string 3 folder))) 0)
394 (setq type (symbol-name elmo-archive-default-type)))
395 (if (eq (length (setq prefix
396 (elmo-match-string 4 folder))) 0)
398 (list 'archive fld-name (intern-soft type) prefix))))
400 (defun elmo-pop3-get-spec (folder)
401 (let ((stream-type-alist elmo-network-stream-type-alist)
403 (if elmo-pop3-stream-type-alist
404 (setq stream-type-alist
405 (append elmo-pop3-stream-type-alist stream-type-alist)))
406 (setq spec (elmo-network-get-spec folder
407 elmo-default-pop3-server
408 elmo-default-pop3-port
409 elmo-default-pop3-stream-type
411 (setq folder (car spec))
413 "^\\(&\\)\\([^:/!]*\\)\\(/[^/:@!]+\\)?"
415 (setq user (if (match-beginning 2)
416 (elmo-match-string 2 folder)))
417 (if (eq (length user) 0)
418 (setq user elmo-default-pop3-user))
419 (setq auth (if (match-beginning 3)
420 (intern (elmo-match-substring 3 folder 1))
421 elmo-default-pop3-authenticate-type))
422 (setq auth (or auth 'user))
423 (append (list 'pop3 user auth)
426 (defsubst elmo-pop3-spec-username (spec)
429 (defsubst elmo-pop3-spec-auth (spec)
432 (defsubst elmo-pop3-spec-hostname (spec)
435 (defsubst elmo-pop3-spec-port (spec)
438 (defsubst elmo-pop3-spec-stream-type (spec)
441 (defun elmo-internal-get-spec (folder)
442 (if (string-match "\\('\\)\\([^/]*\\)/?\\(.*\\)$" folder)
443 (let* ((item (downcase (elmo-match-string 2 folder)))
444 (sym (and (> (length item) 0) (intern item))))
445 (cond ((or (null sym)
447 (list 'internal sym (elmo-match-string 3 folder)))
449 (list 'cache (elmo-match-string 3 folder)))
450 (t (error "Invalid internal folder spec"))))))
452 (defun elmo-multi-get-spec (folder)
455 "^\\(\\*\\)\\(.*\\)$"
457 (append (list 'multi)
459 (elmo-match-string 2 folder)
462 (defun elmo-filter-get-spec (folder)
463 (when (string-match "^\\(/\\)\\(.*\\)$" folder)
464 (let ((folder (elmo-match-string 2 folder))
466 (setq pair (elmo-parse-search-condition folder))
467 (if (string-match "^ */\\(.*\\)$" (cdr pair))
468 (list 'filter (car pair) (elmo-match-string 1 (cdr pair)))
469 (error "Folder syntax error `%s'" folder)))))
471 (defun elmo-pipe-get-spec (folder)
472 (when (string-match "^\\(|\\)\\([^|]*\\)|\\(.*\\)$" folder)
474 (elmo-match-string 2 folder)
475 (elmo-match-string 3 folder))))
477 (defsubst elmo-pipe-spec-src (spec)
480 (defsubst elmo-pipe-spec-dst (spec)
483 (defun elmo-folder-get-spec (folder)
484 "Return spec of FOLDER."
485 (let ((type (elmo-folder-get-type folder)))
488 (funcall (intern (concat "elmo-" (symbol-name type) "-get-spec"))
490 (error "%s is not supported folder type" folder))))
494 (defconst elmo-condition-atom-regexp "[^/ \")|&]*")
496 (defun elmo-read-search-condition (default)
497 "Read search condition string interactively."
498 (elmo-read-search-condition-internal "Search by" default))
500 (defun elmo-read-search-condition-internal (prompt default)
501 (let* ((completion-ignore-case t)
502 (field (completing-read
503 (format "%s (%s): " prompt default)
507 "From" "Subject" "To" "Cc" "Body"
508 "Since" "Before" "ToCc"
509 "!From" "!Subject" "!To" "!Cc" "!Body"
510 "!Since" "!Before" "!ToCc")
511 elmo-msgdb-extra-fields)) nil t))
513 (setq field (if (string= field "")
517 ((or (string= field "AND") (string= field "OR"))
519 (elmo-read-search-condition-internal
520 (concat field "(1) Search by") default)
521 (if (string= field "AND") "&" "|")
522 (elmo-read-search-condition-internal
523 (concat field "(2) Search by") default)
525 ((string-match "Since\\|Before" field)
526 (concat (downcase field) ":"
527 (completing-read (format "Value for '%s': " field)
530 (list (format "%s" (car x)))))
531 elmo-date-descriptions))))
533 (setq value (read-from-minibuffer (format "Value for '%s': " field)))
534 (unless (string-match (concat "^" elmo-condition-atom-regexp "$")
536 (setq value (prin1-to-string value)))
537 (concat (downcase field) ":" value)))))
539 (defsubst elmo-condition-parse-error ()
540 (error "Syntax error in '%s'" (buffer-string)))
542 (defun elmo-parse-search-condition (condition)
544 Return value is a cons cell of (STRUCTURE . REST)"
547 (goto-char (point-min))
548 (cons (elmo-condition-parse) (buffer-substring (point) (point-max)))))
550 ;; condition ::= or-expr
551 (defun elmo-condition-parse ()
552 (or (elmo-condition-parse-or-expr)
553 (elmo-condition-parse-error)))
555 ;; or-expr ::= and-expr /
556 ;; and-expr "|" or-expr
557 (defun elmo-condition-parse-or-expr ()
558 (let ((left (elmo-condition-parse-and-expr)))
559 (if (looking-at "| *")
561 (goto-char (match-end 0))
562 (list 'or left (elmo-condition-parse-or-expr)))
565 ;; and-expr ::= primitive /
566 ;; primitive "&" and-expr
567 (defun elmo-condition-parse-and-expr ()
568 (let ((left (elmo-condition-parse-primitive)))
569 (if (looking-at "& *")
571 (goto-char (match-end 0))
572 (list 'and left (elmo-condition-parse-and-expr)))
575 ;; primitive ::= "(" expr ")" /
576 ;; ["!"] search-key SPACE* ":" SPACE* search-value
577 (defun elmo-condition-parse-primitive ()
580 (goto-char (match-end 0))
581 (prog1 (elmo-condition-parse)
582 (unless (looking-at ") *")
583 (elmo-condition-parse-error))
584 (goto-char (match-end 0))))
585 ;; search-key ::= [A-Za-z-]+
586 ;; ;; "since" / "before" / "last" / "first" /
587 ;; ;; "body" / field-name
588 ((looking-at "\\(!\\)? *\\([A-Za-z-]+\\) *: *")
589 (goto-char (match-end 0))
590 (let ((search-key (vector
591 (if (match-beginning 1) 'unmatch 'match)
592 (elmo-match-buffer 2)
593 (elmo-condition-parse-search-value))))
595 (if (string= (aref search-key 1) "tocc")
596 (if (eq (aref search-key 0) 'match)
598 (vector 'match "to" (aref search-key 2))
599 (vector 'match "cc" (aref search-key 2)))
601 (vector 'unmatch "to" (aref search-key 2))
602 (vector 'unmatch "cc" (aref search-key 2))))
605 ;; search-value ::= quoted / time / number / atom
606 ;; quoted ::= <elisp string expression>
607 ;; time ::= "yesterday" / "lastweek" / "lastmonth" / "lastyear" /
608 ;; number SPACE* "daysago" /
609 ;; number "-" month "-" number ; ex. 10-May-2000
611 ;; month ::= "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" /
612 ;; "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
613 ;; atom ::= ATOM_CHARS*
614 ;; SPACE ::= <ascii space character, 0x20>
615 ;; ATOM_CHARS ::= <any character except specials>
616 ;; specials ::= SPACE / <"> / </> / <)> / <|> / <&>
617 ;; ;; These characters should be quoted.
618 (defun elmo-condition-parse-search-value ()
621 (elmo-read (current-buffer)))
622 ((or (looking-at "yesterday") (looking-at "lastweek")
623 (looking-at "lastmonth") (looking-at "lastyear")
624 (looking-at "[0-9]+ *daysago")
625 (looking-at "[0-9]+-[A-Za-z]+-[0-9]+")
626 (looking-at "[0-9]+")
627 (looking-at elmo-condition-atom-regexp))
628 (prog1 (elmo-match-buffer 0)
629 (goto-char (match-end 0))))
630 (t (error "Syntax error '%s'" (buffer-string)))))
633 (defun elmo-multi-get-real-folder-number (folder number)
634 (let* ((spec (elmo-folder-get-spec folder))
637 (fld (nth (- (/ num elmo-multi-divide-number) 1) flds)))
638 (cons fld (% num elmo-multi-divide-number))))
640 (defsubst elmo-buffer-replace (regexp &optional newtext)
641 (goto-char (point-min))
642 (while (re-search-forward regexp nil t)
643 (replace-match (or newtext ""))))
645 (defsubst elmo-delete-char (char string &optional unibyte)
648 (let ((coding-system-for-read 'no-conversion)
649 (coding-system-for-write 'no-conversion))
650 (if unibyte (elmo-set-buffer-multibyte nil))
652 (goto-char (point-min))
653 (while (search-forward (char-to-string char) nil t)
657 (defsubst elmo-delete-cr-get-content-type ()
659 (goto-char (point-min))
660 (while (search-forward "\r\n" nil t)
661 (replace-match "\n"))
662 (goto-char (point-min))
663 (or (std11-field-body "content-type")
666 (defun elmo-delete-cr (string)
670 (goto-char (point-min))
671 (while (search-forward "\r\n" nil t)
672 (replace-match "\n"))
675 (defun elmo-uniq-list (lst)
676 "Distractively uniqfy elements of LST."
685 (defun elmo-list-insert (list element after)
686 "Insert an ELEMENT to the LIST, just after AFTER."
691 (if (eq (car li) after)
692 (setq p li pn curn li nil)
696 (setcdr (nthcdr pn list) (cons element (cdr p)))
697 (nconc list (list element)))))
699 (defun elmo-string-partial-p (string)
700 (and (stringp string) (string-match "message/partial" string)))
702 (defun elmo-get-file-string (filename &optional remove-final-newline)
704 (let (insert-file-contents-pre-hook ; To avoid autoconv-xmas...
705 insert-file-contents-post-hook)
706 (when (file-exists-p filename)
708 (as-binary-input-file (insert-file-contents filename)))
709 (when (and remove-final-newline
711 (= (char-after (1- (point-max))) ?\n))
712 (goto-char (point-max))
713 (delete-backward-char 1))
716 (defun elmo-save-string (string filename)
719 (as-binary-output-file
721 (write-region (point-min) (point-max)
722 filename nil 'no-msg))
725 (defun elmo-max-of-list (nlist)
729 (if (< max-num (car l))
730 (setq max-num (car l)))
734 (defun elmo-concat-path (path filename)
735 (if (not (string= path ""))
736 (if (string= elmo-path-sep (substring path (- (length path) 1)))
737 (concat path filename)
738 (concat path elmo-path-sep filename))
741 (defvar elmo-passwd-alist nil)
743 (defun elmo-passwd-alist-load ()
745 (let ((filename (expand-file-name elmo-passwd-alist-file-name
747 (tmp-buffer (get-buffer-create " *elmo-passwd-alist-tmp*"))
748 insert-file-contents-pre-hook ; To avoid autoconv-xmas...
749 insert-file-contents-post-hook
751 (if (not (file-readable-p filename))
753 (set-buffer tmp-buffer)
754 (insert-file-contents filename)
757 (read (current-buffer))
759 (kill-buffer tmp-buffer)
762 (defun elmo-passwd-alist-clear ()
763 "Clear password cache."
765 (setq elmo-passwd-alist nil))
767 (defun elmo-passwd-alist-save ()
768 "Save password into file."
771 (let ((filename (expand-file-name elmo-passwd-alist-file-name
773 (tmp-buffer (get-buffer-create " *elmo-passwd-alist-tmp*")))
774 (set-buffer tmp-buffer)
776 (prin1 elmo-passwd-alist tmp-buffer)
777 (princ "\n" tmp-buffer)
778 ;;; (if (and (file-exists-p filename)
779 ;;; (not (equal 384 (file-modes filename))))
780 ;;; (error "%s is not safe.chmod 600 %s!" filename filename))
781 (if (file-writable-p filename)
783 (write-region (point-min) (point-max)
784 filename nil 'no-msg)
785 (set-file-modes filename 384))
786 (message (format "%s is not writable." filename)))
787 (kill-buffer tmp-buffer))))
789 (defun elmo-get-passwd (key)
790 "Get password from password pool."
792 (if (not elmo-passwd-alist)
793 (setq elmo-passwd-alist (elmo-passwd-alist-load)))
794 (setq pair (assoc key elmo-passwd-alist))
796 (elmo-base64-decode-string (cdr pair))
797 (setq pass (elmo-read-passwd (format "Password for %s: "
799 (setq elmo-passwd-alist
800 (append elmo-passwd-alist
802 (elmo-base64-encode-string pass)))))
803 (if elmo-passwd-life-time
804 (run-with-timer elmo-passwd-life-time nil
805 (` (lambda () (elmo-remove-passwd (, key))))))
808 (defun elmo-remove-passwd (key)
809 "Remove password from password pool (for failure)."
811 (if (setq pass-cons (assoc key elmo-passwd-alist))
814 (fillarray (cdr pass-cons) 0))
815 (setq elmo-passwd-alist
816 (delete pass-cons elmo-passwd-alist))))))
818 (defmacro elmo-read-char-exclusive ()
819 (cond ((featurep 'xemacs)
820 '(let ((table (quote ((backspace . ?\C-h) (delete . ?\C-?)
825 (key-press-event-p (setq event (next-command-event)))
826 (setq key (or (event-to-character event)
827 (cdr (assq (event-key event) table)))))))
829 ((fboundp 'read-char-exclusive)
830 '(read-char-exclusive))
834 (defun elmo-read-passwd (prompt &optional stars)
835 "Read a single line of text from user without echoing, and return it."
839 (cursor-in-echo-area t)
840 (log-message-max-size 0)
841 message-log-max done msg truncate)
843 (if (or (not stars) (string= "" ans))
845 (setq msg (concat prompt (make-string (length ans) ?.)))
847 (1+ (- (length msg) (window-width (minibuffer-window)))))
849 (setq msg (concat "$" (substring msg (1+ truncate))))))
851 (setq c (elmo-read-char-exclusive))
855 ((or (= c ?\r) (= c ?\n) (= c ?\e))
859 ((and (/= c ?\b) (/= c ?\177))
860 (setq ans (concat ans (char-to-string c))))
862 (setq ans (substring ans 0 -1)))))
872 (defun elmo-replace-in-string (str regexp newtext &optional literal)
873 "Replace all matches in STR for REGEXP with NEWTEXT string.
874 And returns the new string.
875 Optional LITERAL non-nil means do a literal replacement.
876 Otherwise treat \\ in NEWTEXT string as special:
877 \\& means substitute original matched text,
878 \\N means substitute match for \(...\) number N,
879 \\\\ means insert one \\."
884 (while (setq match (string-match regexp str start))
885 (setq prev-start start
890 (substring str prev-start match)
891 (cond (literal newtext)
898 (cond ((eq c ?\\) "\\")
900 (elmo-match-string 0 str))
901 ((and (>= c ?0) (<= c ?9))
902 (if (> c (+ ?0 (length
905 (error "Invalid match num: %c" c)
907 (elmo-match-string c str)))
908 (t (char-to-string c))))
909 (if (eq c ?\\) (progn (setq special t) nil)
910 (char-to-string c)))))
912 (concat rtn-str (substring str start))))
914 (defun elmo-string-to-list (string)
917 (goto-char (point-min))
919 (goto-char (point-max))
921 (goto-char (point-min))
922 (read (current-buffer))))
924 (defun elmo-list-to-string (list)
933 (if (symbolp (car tlist))
934 (symbol-name (car tlist))
939 (setq tlist (cdr tlist)))
949 (defun elmo-plug-on-by-servers (alist &optional servers)
950 (let ((server-list (or servers elmo-plug-on-servers)))
953 (if (elmo-plugged-p (car server-list))
955 (setq server-list (cdr server-list))))))
957 (defun elmo-plug-on-by-exclude-servers (alist &optional servers)
958 (let ((server-list (or servers elmo-plug-on-exclude-servers))
959 server other-servers)
961 (when (and (not (member (setq server (caaar alist)) server-list))
962 (not (member server other-servers)))
963 (push server other-servers))
964 (setq alist (cdr alist)))
965 (elmo-plug-on-by-servers alist other-servers)))
967 (defun elmo-plugged-p (&optional server port alist label-exp)
968 (let ((alist (or alist elmo-plugged-alist))
970 (cond ((and (not port) (not server))
971 (cond ((eq elmo-plugged-condition 'one)
974 (if (nth 2 (car alist))
976 (setq alist (cdr alist)))))
977 ((eq elmo-plugged-condition 'all)
980 (if (not (nth 2 (car alist)))
981 (throw 'plugged nil))
982 (setq alist (cdr alist)))
984 ((functionp elmo-plugged-condition)
985 (funcall elmo-plugged-condition alist))
988 ((not port) ;; server
991 (when (string= server (caaar alist))
992 (if (nth 2 (car alist))
994 (setq alist (cdr alist)))))
996 (setq plugged-info (assoc (cons server port) alist))
997 (if (not plugged-info)
998 ;; add elmo-plugged-alist automatically
1000 (elmo-set-plugged elmo-plugged server port nil nil label-exp)
1002 (if (and elmo-auto-change-plugged
1003 (> elmo-auto-change-plugged 0)
1004 (nth 3 plugged-info) ;; time
1005 (elmo-time-expire (nth 3 plugged-info)
1006 elmo-auto-change-plugged))
1008 (nth 2 plugged-info)))))))
1010 (defun elmo-set-plugged (plugged &optional server port time
1011 alist label-exp add)
1012 (let ((alist (or alist elmo-plugged-alist))
1014 (cond ((and (not port) (not server))
1015 (setq elmo-plugged plugged)
1016 ;; set plugged all element of elmo-plugged-alist.
1018 (setcdr (cdar alist) (list plugged time))
1019 (setq alist (cdr alist))))
1021 ;; set plugged all port of server
1023 (when (string= server (caaar alist))
1024 (setcdr (cdar alist) (list plugged time)))
1025 (setq alist (cdr alist))))
1027 ;; set plugged one port of server
1028 (setq plugged-info (assoc (cons server port) alist))
1029 (setq label (if label-exp
1031 (nth 1 plugged-info)))
1033 ;; if add is non-nil, don't reset plug state.
1035 (setcdr plugged-info (list label plugged time)))
1037 (setq elmo-plugged-alist
1038 (nconc elmo-plugged-alist
1040 (list (cons server port) label plugged time))))))))
1043 (defun elmo-delete-plugged (&optional server port alist)
1044 (let* ((alist (or alist elmo-plugged-alist))
1046 (cond ((and (not port) (not server))
1049 ;; delete plugged all port of server
1051 (when (string= server (caaar alist2))
1052 (setq alist (delete (car alist2) alist)))
1053 (setq alist2 (cdr alist2))))
1055 ;; delete plugged one port of server
1057 (delete (assoc (cons server port) alist) alist))))
1060 (defun elmo-disk-usage (path)
1061 "Get disk usage (bytes) in PATH."
1063 (condition-case () (file-attributes path) (error nil))))
1065 (if (nth 0 file-attr) ; directory
1066 (let ((files (condition-case ()
1067 (directory-files path t "^[^\\.]")
1070 ;; (result (nth 7 file-attr))) ... directory size
1072 (setq result (+ result (or (elmo-disk-usage (car files)) 0)))
1073 (setq files (cdr files)))
1075 (float (nth 7 file-attr))))))
1077 (defun elmo-get-last-accessed-time (path &optional dir)
1078 "Return the last accessed time of PATH."
1079 (let ((last-accessed (nth 4 (file-attributes (or (and dir
1084 (setq last-accessed (+ (* (nth 0 last-accessed)
1085 (float 65536)) (nth 1 last-accessed)))
1088 (defun elmo-get-last-modification-time (path &optional dir)
1089 "Return the last accessed time of PATH."
1090 (let ((last-modified (nth 5 (file-attributes (or (and dir
1094 (setq last-modified (+ (* (nth 0 last-modified)
1095 (float 65536)) (nth 1 last-modified)))))
1097 (defun elmo-make-directory (path)
1098 "Create directory recursively."
1099 (let ((parent (directory-file-name (file-name-directory path))))
1100 (if (null (file-directory-p parent))
1101 (elmo-make-directory parent))
1102 (make-directory path)
1103 (if (string= path (expand-file-name elmo-msgdb-dir))
1104 (set-file-modes path (+ (* 64 7) (* 8 0) 0))))) ; chmod 0700
1106 (defun elmo-delete-directory (path &optional no-hierarchy)
1107 "Delete directory recursively."
1108 (let ((dirent (directory-files path))
1109 relpath abspath hierarchy)
1111 (setq relpath (car dirent)
1113 abspath (expand-file-name relpath path))
1114 (when (not (string-match "^\\.\\.?$" relpath))
1115 (if (eq (nth 0 (file-attributes abspath)) t)
1118 (elmo-delete-directory abspath no-hierarchy))
1119 (delete-file abspath))))
1121 (delete-directory path))))
1123 (defun elmo-list-filter (l1 l2)
1126 ;; t means filter all.
1129 (elmo-delete-if (lambda (x) (not (memq x l1))) l2)
1133 (defun elmo-folder-local-p (folder)
1134 "Return whether FOLDER is a local folder or not."
1135 (let ((spec (elmo-folder-get-spec folder)))
1137 (filter (elmo-folder-local-p (nth 2 spec)))
1138 (pipe (elmo-folder-local-p (elmo-pipe-spec-dst spec)))
1140 '(localdir localnews archive maildir internal cache))))))
1142 (defun elmo-folder-writable-p (folder)
1143 (let ((type (elmo-folder-get-type folder)))
1144 (memq type '(imap4 localdir archive))))
1146 (defun elmo-multi-get-intlist-list (numlist &optional as-is)
1147 (let ((numbers (sort numlist '<))
1149 one-list int-list-list)
1151 (setq cur-number (+ cur-number 1))
1156 (* elmo-multi-divide-number cur-number))
1157 elmo-multi-divide-number)))
1158 (setq one-list (nconc
1164 (* elmo-multi-divide-number cur-number))))))
1165 (setq numbers (cdr numbers)))
1166 (setq int-list-list (nconc int-list-list (list one-list))))
1169 (defsubst elmo-list-delete-if-smaller (list number)
1170 (let ((ret-val (copy-sequence list)))
1172 (if (< (car list) number)
1173 (setq ret-val (delq (car list) ret-val)))
1174 (setq list (cdr list)))
1177 (defun elmo-list-diff (list1 list2 &optional mes)
1180 (let ((clist1 (copy-sequence list1))
1181 (clist2 (copy-sequence list2)))
1183 (setq clist1 (delq (car list2) clist1))
1184 (setq list2 (cdr list2)))
1186 (setq clist2 (delq (car list1) clist2))
1187 (setq list1 (cdr list1)))
1189 (message (concat mes "done.")))
1190 (list clist1 clist2)))
1192 (defun elmo-list-bigger-diff (list1 list2 &optional mes)
1193 "Returns a list (- +). + is bigger than max of LIST1, in LIST2."
1198 (max-of-l2 (or (nth (max 0 (1- (length l2))) l2) 0))
1202 (setq num (+ (length l1)))
1204 (if (memq (car l1) l2)
1205 (if (eq (car l1) (car l2))
1208 (if (> (car l1) max-of-l2)
1209 (setq diff1 (nconc diff1 (list (car l1))))))
1213 (setq percent (/ (* i 100) num))
1214 (if (eq (% percent 5) 0)
1215 (elmo-display-progress
1216 'elmo-list-bigger-diff "%s%d%%" percent mes))))
1218 (cons diff1 (list l2)))))
1220 (defun elmo-multi-list-bigger-diff (list1 list2 &optional mes)
1221 (let ((list1-list (elmo-multi-get-intlist-list list1 t))
1222 (list2-list (elmo-multi-get-intlist-list list2 t))
1225 (while (or list1-list list2-list)
1226 (setq result (elmo-list-bigger-diff (car list1-list) (car list2-list)
1228 (setq dels (append dels (car result)))
1229 (setq news (append news (cadr result)))
1230 (setq list1-list (cdr list1-list))
1231 (setq list2-list (cdr list2-list)))
1232 (cons dels (list news))))
1234 (defvar elmo-imap4-name-space-regexp-list nil)
1235 (defun elmo-imap4-identical-name-space-p (fld1 fld2)
1237 (if (or (eq (string-to-char fld1) ?#)
1238 (eq (string-to-char fld2) ?#))
1239 (string= (car (split-string fld1 "/"))
1240 (car (split-string fld2 "/")))
1243 (defun elmo-folder-identical-system-p (folder1 folder2)
1244 "FOLDER1 and FOLDER2 should be real folder (not virtual)."
1245 (cond ((eq (elmo-folder-get-type folder1) 'imap4)
1246 (let ((spec1 (elmo-folder-get-spec folder1))
1247 (spec2 (elmo-folder-get-spec folder2)))
1250 ;;; (elmo-imap4-identical-name-space-p
1251 ;;; (nth 1 spec1) (nth 1 spec2))
1252 (string= (elmo-imap4-spec-hostname spec1)
1253 (elmo-imap4-spec-hostname spec2)) ; hostname
1254 (string= (elmo-imap4-spec-username spec1)
1255 (elmo-imap4-spec-username spec2))))) ; username
1257 (elmo-folder-direct-copy-p folder1 folder2))))
1259 (defun elmo-folder-get-store-type (folder)
1260 (let ((spec (elmo-folder-get-spec folder)))
1262 (filter (elmo-folder-get-store-type (nth 2 spec)))
1263 (pipe (elmo-folder-get-store-type (elmo-pipe-spec-dst spec)))
1264 (multi (elmo-folder-get-store-type (nth 1 spec)))
1267 (defconst elmo-folder-direct-copy-alist
1268 '((localdir . (localdir localnews archive))
1269 (maildir . (maildir localdir localnews archive))
1270 (localnews . (localdir localnews archive))
1271 (archive . (localdir localnews archive))
1272 (cache . (localdir localnews archive))))
1274 (defun elmo-folder-direct-copy-p (src-folder dst-folder)
1275 (let ((src-type (elmo-folder-get-store-type src-folder))
1276 (dst-type (elmo-folder-get-store-type dst-folder))
1278 (and (setq dst-copy-type
1279 (cdr (assq src-type elmo-folder-direct-copy-alist)))
1280 (memq dst-type dst-copy-type))))
1282 (defmacro elmo-filter-type (filter)
1283 (` (aref (, filter) 0)))
1285 (defmacro elmo-filter-key (filter)
1286 (` (aref (, filter) 1)))
1288 (defmacro elmo-filter-value (filter)
1289 (` (aref (, filter) 2)))
1291 (defsubst elmo-buffer-field-primitive-condition-match (condition
1295 (goto-char (point-min))
1297 ((string= (elmo-filter-key condition) "last")
1298 (setq result (<= (length (memq number number-list))
1299 (string-to-int (elmo-filter-value condition)))))
1300 ((string= (elmo-filter-key condition) "first")
1301 (setq result (< (- (length number-list)
1302 (length (memq number number-list)))
1303 (string-to-int (elmo-filter-value condition)))))
1304 ((string= (elmo-filter-key condition) "since")
1305 (let ((date (elmo-date-get-datevec (elmo-filter-value condition))))
1308 (timezone-make-sortable-date (aref date 0)
1311 (timezone-make-time-string
1315 (timezone-make-date-sortable (std11-field-body "date"))))))
1316 ((string= (elmo-filter-key condition) "before")
1317 (let ((date (elmo-date-get-datevec (elmo-filter-value condition))))
1320 (timezone-make-date-sortable (std11-field-body "date"))
1321 (timezone-make-sortable-date (aref date 0)
1324 (timezone-make-time-string
1328 ((string= (elmo-filter-key condition) "body")
1329 (and (re-search-forward "^$" nil t) ; goto body
1330 (setq result (search-forward (elmo-filter-value condition)
1333 (let ((fval (std11-field-body (elmo-filter-key condition))))
1334 (if (eq (length fval) 0) (setq fval nil))
1335 (if fval (setq fval (eword-decode-string fval)))
1336 (setq result (and fval (string-match
1337 (elmo-filter-value condition) fval))))))
1338 (if (eq (elmo-filter-type condition) 'unmatch)
1339 (setq result (not result)))
1342 (defun elmo-condition-find-key-internal (condition key)
1344 ((vectorp condition)
1345 (if (string= (elmo-filter-key condition) key)
1347 ((or (eq (car condition) 'and)
1348 (eq (car condition) 'or))
1349 (elmo-condition-find-key-internal (nth 1 condition) key)
1350 (elmo-condition-find-key-internal (nth 2 condition) key))))
1352 (defun elmo-condition-find-key (condition key)
1354 (elmo-condition-find-key-internal condition key)))
1356 (defun elmo-buffer-field-condition-match (condition number number-list)
1358 ((vectorp condition)
1359 (elmo-buffer-field-primitive-condition-match
1360 condition number number-list))
1361 ((eq (car condition) 'and)
1362 (and (elmo-buffer-field-condition-match
1363 (nth 1 condition) number number-list)
1364 (elmo-buffer-field-condition-match
1365 (nth 2 condition) number number-list)))
1366 ((eq (car condition) 'or)
1367 (or (elmo-buffer-field-condition-match
1368 (nth 1 condition) number number-list)
1369 (elmo-buffer-field-condition-match
1370 (nth 2 condition) number number-list)))))
1372 (defsubst elmo-file-field-condition-match (file condition number number-list)
1374 (as-binary-input-file (insert-file-contents file))
1375 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
1376 ;; Should consider charset?
1377 (decode-mime-charset-region (point-min)(point-max) elmo-mime-charset)
1378 (elmo-buffer-field-condition-match condition number number-list)))
1380 (defmacro elmo-get-hash-val (string hashtable)
1381 (let ((sym (list 'intern-soft string hashtable)))
1382 (list 'if (list 'boundp sym)
1383 (list 'symbol-value sym))))
1385 (defmacro elmo-set-hash-val (string value hashtable)
1386 (list 'set (list 'intern string hashtable) value))
1388 (defmacro elmo-clear-hash-val (string hashtable)
1389 (static-if (fboundp 'unintern)
1390 (list 'unintern string hashtable)
1391 (list 'makunbound (list 'intern string hashtable))))
1393 (defmacro elmo-unintern (string)
1394 "`unintern' symbol named STRING, When can use `unintern'.
1395 Emacs 19.28 or earlier does not have `unintern'."
1396 (static-if (fboundp 'unintern)
1397 (list 'unintern string)))
1399 ;; Make a hash table (default and minimum size is 1024).
1400 (defun elmo-make-hash (&optional hashsize)
1402 (if hashsize (max (min (elmo-create-hash-size hashsize)
1403 elmo-hash-maximum-size) 1024) 1024) 0))
1405 (defsubst elmo-mime-string (string)
1406 "Normalize MIME encoded STRING."
1410 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
1411 (setq str (eword-decode-string
1412 (decode-mime-charset-string string elmo-mime-charset)))
1413 (setq str (encode-mime-charset-string str elmo-mime-charset))
1414 (elmo-set-buffer-multibyte nil)
1417 (defsubst elmo-collect-field (beg end downcase-field-name)
1420 (narrow-to-region beg end)
1421 (goto-char (point-min))
1422 (let ((regexp (concat "\\(" std11-field-head-regexp "\\)[ \t]*"))
1424 (while (re-search-forward regexp nil t)
1425 (setq name (buffer-substring-no-properties
1426 (match-beginning 1)(1- (match-end 1))))
1427 (if downcase-field-name
1428 (setq name (downcase name)))
1429 (setq body (buffer-substring-no-properties
1430 (match-end 0) (std11-field-end)))
1431 (or (assoc name dest)
1432 (setq dest (cons (cons name body) dest))))
1435 (defsubst elmo-collect-field-from-string (string downcase-field-name)
1438 (goto-char (point-min))
1439 (let ((regexp (concat "\\(" std11-field-head-regexp "\\)[ \t]*"))
1441 (while (re-search-forward regexp nil t)
1442 (setq name (buffer-substring-no-properties
1443 (match-beginning 1)(1- (match-end 1))))
1444 (if downcase-field-name
1445 (setq name (downcase name)))
1446 (setq body (buffer-substring-no-properties
1447 (match-end 0) (std11-field-end)))
1448 (or (assoc name dest)
1449 (setq dest (cons (cons name body) dest))))
1452 (defun elmo-create-hash-size (min)
1458 (defun elmo-safe-filename (folder)
1459 (elmo-replace-in-string
1460 (elmo-replace-in-string
1461 (elmo-replace-in-string folder "/" " ")
1465 (defvar elmo-msgid-replace-chars nil)
1467 (defsubst elmo-replace-msgid-as-filename (msgid)
1468 "Replace Message-ID string (MSGID) as filename."
1469 (setq msgid (elmo-replace-in-string msgid " " " "))
1470 (if (null elmo-msgid-replace-chars)
1471 (setq elmo-msgid-replace-chars
1472 (regexp-quote (mapconcat
1473 'car elmo-msgid-replace-string-alist ""))))
1474 (while (string-match (concat "[" elmo-msgid-replace-chars "]")
1477 (substring msgid 0 (match-beginning 0))
1480 (match-beginning 0) (match-end 0))
1481 elmo-msgid-replace-string-alist))
1482 (substring msgid (match-end 0)))))
1485 (defsubst elmo-recover-msgid-from-filename (filename)
1486 "Recover Message-ID from FILENAME."
1488 (while (string-match " " filename)
1489 (setq tmp (substring filename
1491 (+ (match-end 0) 1)))
1492 (if (string= tmp " ")
1494 (setq tmp (car (rassoc tmp
1495 elmo-msgid-replace-string-alist))))
1498 (substring filename 0 (match-beginning 0))
1500 (setq filename (substring filename (+ (match-end 0) 1))))
1501 (concat result filename)))
1503 (defsubst elmo-copy-file (src dst)
1505 (elmo-add-name-to-file src dst t)
1506 (error (copy-file src dst t))))
1508 (defsubst elmo-buffer-exists-p (buffer)
1509 (if (bufferp buffer)
1510 (buffer-live-p buffer)
1511 (get-buffer buffer)))
1513 (defsubst elmo-kill-buffer (buffer)
1514 (when (elmo-buffer-exists-p buffer)
1515 (kill-buffer buffer)))
1517 (defun elmo-delete-if (pred lst)
1518 "Return new list contain items which don't satisfy PRED in LST."
1521 (unless (funcall pred (car lst))
1522 (setq result (nconc result (list (car lst)))))
1523 (setq lst (cdr lst)))
1526 (defun elmo-list-delete (list1 list2)
1527 "Delete by side effect any occurrences equal to elements of LIST1 from LIST2.
1528 Return the modified LIST2. Deletion is done with `delete'.
1529 Write `(setq foo (elmo-list-delete bar foo))' to be sure of changing
1530 the value of `foo'."
1532 (setq list2 (delete (car list1) list2))
1533 (setq list1 (cdr list1)))
1536 (defun elmo-list-member (list1 list2)
1537 "If any element of LIST1 is member of LIST2, return t."
1540 (if (member (car list1) list2)
1542 (setq list1 (cdr list1)))))
1544 (defun elmo-count-matches (regexp beg end)
1548 (while (re-search-forward regexp end t)
1549 (setq count (1+ count)))
1552 (if (fboundp 'display-error)
1553 (defalias 'elmo-display-error 'display-error)
1554 (defun elmo-display-error (error-object stream)
1555 "A tiny function to display ERROR-OBJECT to the STREAM."
1557 (errobj error-object)
1560 (setq err-mes (concat err-mes (format
1561 (if (stringp (car errobj))
1563 (if (boundp 'nemacs-version)
1565 "%S")) (car errobj))))
1566 (setq errobj (cdr errobj))
1567 (if errobj (setq err-mes (concat err-mes (if first ": " ", "))))
1569 (princ err-mes stream))))
1571 (if (fboundp 'define-error)
1572 (defalias 'elmo-define-error 'define-error)
1573 (defun elmo-define-error (error doc &optional parents)
1575 (setq parents 'error))
1576 (let ((conds (get parents 'error-conditions)))
1578 (error "Not an error symbol: %s" error))
1580 (list 'error-message doc
1581 'error-conditions (cons error conds))))))
1583 (cond ((fboundp 'progress-feedback-with-label)
1584 (defalias 'elmo-display-progress 'progress-feedback-with-label))
1585 ((fboundp 'lprogress-display)
1586 (defalias 'elmo-display-progress 'lprogress-display))
1588 (defun elmo-display-progress (label format &optional value &rest args)
1589 "Print a progress message."
1590 (if (and (null format) (null args))
1592 (apply (function message) (concat format " %d%%")
1593 (nconc args (list value)))))))
1595 (defun elmo-time-expire (before-time diff-time)
1596 (let* ((current (current-time))
1597 (rest (when (< (nth 1 current) (nth 1 before-time))
1601 (list (- (+ (car current) (if rest -1 0)) (car before-time))
1602 (- (+ (or rest 0) (nth 1 current)) (nth 1 before-time))))
1603 (and (eq (car diff) 0)
1604 (< diff-time (nth 1 diff)))))
1606 (if (fboundp 'std11-fetch-field)
1607 (defalias 'elmo-field-body 'std11-fetch-field) ;;no narrow-to-region
1608 (defalias 'elmo-field-body 'std11-field-body))
1610 (defmacro elmo-string (string)
1611 "STRING without text property."
1612 (` (let ((obj (copy-sequence (, string))))
1613 (set-text-properties 0 (length obj) nil obj)
1616 (defun elmo-flatten (list-of-list)
1617 "Flatten LIST-OF-LIST."
1618 (unless (null list-of-list)
1619 (append (if (and (car list-of-list)
1620 (listp (car list-of-list)))
1622 (list (car list-of-list)))
1623 (elmo-flatten (cdr list-of-list)))))
1625 (defun elmo-y-or-n-p (prompt &optional auto default)
1626 "Same as `y-or-n-p'.
1627 But if optional argument AUTO is non-nil, DEFAULT is returned."
1632 (defun elmo-string-member (string slist)
1633 "Return t if STRING is a member of the SLIST."
1636 (if (and (stringp (car slist))
1637 (string= string (car slist)))
1639 (setq slist (cdr slist)))))
1641 (defun elmo-string-match-member (str list &optional case-ignore)
1642 (let ((case-fold-search case-ignore))
1645 (if (string-match (car list) str)
1646 (throw 'member (car list)))
1647 (setq list (cdr list))))))
1649 (defun elmo-string-matched-member (str list &optional case-ignore)
1650 (let ((case-fold-search case-ignore))
1653 (if (string-match str (car list))
1654 (throw 'member (car list)))
1655 (setq list (cdr list))))))
1657 (defsubst elmo-string-delete-match (string pos)
1658 (concat (substring string
1659 0 (match-beginning pos))
1664 (defun elmo-string-match-assoc (key alist &optional case-ignore)
1665 (let ((case-fold-search case-ignore)
1669 (setq a (car alist))
1672 (string-match key (car a)))
1674 (setq alist (cdr alist))))))
1676 (defun elmo-string-matched-assoc (key alist &optional case-ignore)
1677 (let ((case-fold-search case-ignore)
1681 (setq a (car alist))
1684 (string-match (car a) key))
1686 (setq alist (cdr alist))))))
1688 (defun elmo-string-assoc (key alist)
1692 (setq a (car alist))
1695 (string= key (car a)))
1697 (setq alist (cdr alist))))))
1699 (defun elmo-string-rassoc (key alist)
1703 (setq a (car alist))
1706 (string= key (cdr a)))
1708 (setq alist (cdr alist))))))
1710 (defun elmo-string-rassoc-all (key alist)
1713 (if (string= key (cdr (car alist)))
1717 (setq alist (cdr alist)))
1720 ;;; Number set defined by OKAZAKI Tetsurou <okazaki@be.to>
1722 ;; number ::= [0-9]+
1725 ;; number-range ::= "(" beg " . " end ")" ;; cons cell
1726 ;; number-set-elem ::= number / number-range
1727 ;; number-set ::= "(" *number-set-elem ")" ;; list
1729 (defun elmo-number-set-member (number number-set)
1730 "Return non-nil if NUMBER is an element of NUMBER-SET.
1731 The value is actually the tail of NUMBER-RANGE whose car contains NUMBER."
1732 (or (memq number number-set)
1734 (while (and number-set (not found))
1735 (if (and (consp (car number-set))
1736 (and (<= (car (car number-set)) number)
1737 (<= number (cdr (car number-set)))))
1739 (setq number-set (cdr number-set))))
1742 (defun elmo-number-set-append-list (number-set list)
1743 "Append LIST of numbers to the NUMBER-SET.
1744 NUMBER-SET is altered."
1745 (let ((appended number-set))
1747 (setq appended (elmo-number-set-append appended (car list)))
1748 (setq list (cdr list)))
1751 (defun elmo-number-set-append (number-set number)
1752 "Append NUMBER to the NUMBER-SET.
1753 NUMBER-SET is altered."
1754 (let ((number-set-1 number-set)
1756 (while (and number-set (not found))
1757 (setq elem (car number-set))
1760 (eq (+ 1 (cdr elem)) number))
1761 (setcdr elem number)
1763 ((and (integerp elem)
1764 (eq (+ 1 elem) number))
1765 (setcar number-set (cons elem number))
1767 ((or (and (integerp elem) (eq elem number))
1769 (<= (car elem) number)
1770 (<= number (cdr elem))))
1772 (setq number-set (cdr number-set)))
1774 (setq number-set-1 (nconc number-set-1 (list number))))
1778 (product-provide (provide 'elmo-util) (require 'elmo-version))
1780 ;;; elmo-util.el ends here