1 ;;; elmo-util.el -- Utilities for Elmo.
3 ;; Copyright 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 (defmacro elmo-set-work-buf (&rest body)
89 "Execute BODY on work buffer. Work buffer remains."
91 (set-buffer (get-buffer-create elmo-work-buf-name))
92 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
96 (defmacro elmo-match-substring (pos string from)
97 "Substring of POSth matched string of STRING. "
98 (` (substring (, string)
99 (+ (match-beginning (, pos)) (, from))
100 (match-end (, pos)))))
102 (defmacro elmo-match-string (pos string)
103 "Substring POSth matched string."
104 (` (substring (, string) (match-beginning (, pos)) (match-end (, pos)))))
106 (defmacro elmo-match-buffer (pos)
107 "Substring POSth matched from the current buffer."
108 (` (buffer-substring-no-properties
109 (match-beginning (, pos)) (match-end (, pos)))))
111 (defmacro elmo-bind-directory (dir &rest body)
112 "Set current directory DIR and execute BODY."
113 (` (let ((default-directory (file-name-as-directory (, dir))))
116 (defmacro elmo-folder-get-type (folder)
117 "Get type of FOLDER."
118 (` (and (stringp (, folder))
119 (cdr (assoc (string-to-char (, folder)) elmo-spec-alist)))))
121 (defun elmo-object-load (filename &optional mime-charset no-err)
122 "Load OBJECT from the file specified by FILENAME.
123 File content is decoded with MIME-CHARSET."
124 (if (not (file-readable-p filename))
127 (as-binary-input-file
128 (insert-file-contents filename))
130 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
131 (decode-mime-charset-region (point-min) (point-max) mime-charset))
133 (read (current-buffer))
134 (error (unless no-err
135 (message "Warning: Loading object from %s failed."
137 (elmo-object-save filename nil))
140 (defsubst elmo-save-buffer (filename &optional mime-charset)
141 "Save current buffer to the file specified by FILENAME.
142 Directory of the file is created if it doesn't exist.
143 File content is encoded with MIME-CHARSET."
144 (let ((dir (directory-file-name (file-name-directory filename))))
145 (if (file-directory-p dir)
147 (unless (file-exists-p dir)
148 (elmo-make-directory dir)))
149 (if (file-writable-p filename)
152 ;;(elmo-set-buffer-multibyte default-enable-multibyte-characters)
153 (encode-mime-charset-region (point-min) (point-max) mime-charset))
154 (as-binary-output-file
155 (write-region (point-min) (point-max) filename nil 'no-msg)))
156 (message (format "%s is not writable." filename)))))
158 (defun elmo-object-save (filename object &optional mime-charset)
159 "Save OBJECT to the file specified by FILENAME.
160 Directory of the file is created if it doesn't exist.
161 File content is encoded with MIME-CHARSET."
163 (prin1 object (current-buffer))
164 ;;(princ "\n" (current-buffer))
165 (elmo-save-buffer filename mime-charset)))
167 (defsubst elmo-imap4-decode-folder-string (string)
168 (if elmo-imap4-use-modified-utf7
169 (utf7-decode-string string 'imap)
172 (defsubst elmo-imap4-encode-folder-string (string)
173 (if elmo-imap4-use-modified-utf7
174 (utf7-encode-string string 'imap)
177 (defun elmo-get-network-stream-type (stream-type stream-type-alist)
179 (while stream-type-alist
180 (if (eq (nth 1 (car stream-type-alist)) stream-type)
181 (throw 'found (car stream-type-alist)))
182 (setq stream-type-alist (cdr stream-type-alist)))))
184 (defun elmo-network-get-spec (folder server port stream-type stream-type-alist)
185 (if (string-match "\\(@[^@:/!]+\\)?\\(:[0-9]+\\)?\\(!.*\\)?$" folder)
187 (if (match-beginning 1)
188 (setq server (elmo-match-substring 1 folder 1)))
189 (if (match-beginning 2)
190 (setq port (string-to-int (elmo-match-substring 2 folder 1))))
191 (if (match-beginning 3)
192 (setq stream-type (assoc (elmo-match-string 3 folder)
194 (setq folder (substring folder 0 (match-beginning 0))))
195 (setq stream-type-alist (elmo-get-network-stream-type
196 stream-type stream-type-alist)))
197 (cons folder (list server port stream-type)))
199 (defun elmo-imap4-get-spec (folder)
200 (let ((default-user elmo-default-imap4-user)
201 (default-server elmo-default-imap4-server)
202 (default-port elmo-default-imap4-port)
203 (default-stream-type elmo-default-imap4-stream-type)
204 (stream-type-alist elmo-network-stream-type-alist)
205 spec mailbox user auth)
206 (when (string-match "\\(.*\\)@\\(.*\\)" default-server)
207 ;; case: default-imap4-server is specified like
208 ;; "hoge%imap.server@gateway".
209 (setq default-user (elmo-match-string 1 default-server))
210 (setq default-server (elmo-match-string 2 default-server)))
211 (if elmo-imap4-stream-type-alist
212 (setq stream-type-alist
213 (append elmo-imap4-stream-type-alist stream-type-alist)))
214 (setq spec (elmo-network-get-spec
215 folder default-server default-port default-stream-type
217 (setq folder (car spec))
219 "^\\(%\\)\\([^:@!]*\\)\\(:[^/!]+\\)?\\(/[^/:@!]+\\)?"
222 (setq mailbox (if (match-beginning 2)
223 (elmo-match-string 2 folder)
224 elmo-default-imap4-mailbox))
225 (setq user (if (match-beginning 3)
226 (elmo-match-substring 3 folder 1)
228 (setq auth (if (match-beginning 4)
229 (intern (elmo-match-substring 4 folder 1))
230 elmo-default-imap4-authenticate-type))
232 (elmo-imap4-encode-folder-string mailbox)
236 (defsubst elmo-imap4-spec-mailbox (spec)
239 (defsubst elmo-imap4-spec-username (spec)
242 (defsubst elmo-imap4-spec-auth (spec)
245 (defsubst elmo-imap4-spec-hostname (spec)
248 (defsubst elmo-imap4-spec-port (spec)
251 (defsubst elmo-imap4-spec-stream-type (spec)
254 (defalias 'elmo-imap4-spec-folder 'elmo-imap4-spec-mailbox)
255 (make-obsolete 'elmo-imap4-spec-folder 'elmo-imap4-spec-mailbox)
257 (defsubst elmo-imap4-connection-get-process (conn)
260 (defsubst elmo-imap4-connection-get-buffer (conn)
263 (defsubst elmo-imap4-connection-get-cwf (conn)
266 (defun elmo-nntp-get-spec (folder)
267 (let ((stream-type-alist elmo-network-stream-type-alist)
269 (if elmo-nntp-stream-type-alist
270 (setq stream-type-alist
271 (append elmo-nntp-stream-type-alist stream-type-alist)))
272 (setq spec (elmo-network-get-spec folder
273 elmo-default-nntp-server
274 elmo-default-nntp-port
275 elmo-default-nntp-stream-type
277 (setq folder (car spec))
279 "^\\(-\\)\\([^:@!]*\\)\\(:[^/!]+\\)?\\(/[^/:@!]+\\)?"
282 (if (match-beginning 2)
283 (elmo-match-string 2 folder)))
285 (if (match-beginning 3)
286 (elmo-match-substring 3 folder 1)
287 elmo-default-nntp-user))
288 (append (list 'nntp group user)
291 (defsubst elmo-nntp-spec-group (spec)
294 (defsubst elmo-nntp-spec-username (spec)
298 ;; (defsubst elmo-nntp-spec-auth (spec))
300 (defsubst elmo-nntp-spec-hostname (spec)
303 (defsubst elmo-nntp-spec-port (spec)
306 (defsubst elmo-nntp-spec-stream-type (spec)
309 (defun elmo-localdir-get-spec (folder)
312 "^\\(\\+\\)\\(.*\\)$"
314 (if (eq (length (setq fld-name
315 (elmo-match-string 2 folder))) 0)
318 (if (file-name-absolute-p fld-name)
319 (setq path (expand-file-name fld-name))
320 (setq path fld-name))
321 ;(setq path (expand-file-name fld-name
322 ;elmo-localdir-folder-path)))
323 (list (if (elmo-folder-maildir-p folder)
327 (defun elmo-maildir-get-spec (folder)
330 "^\\(\\.\\)\\(.*\\)$"
332 (if (eq (length (setq fld-name
333 (elmo-match-string 2 folder))) 0)
335 (if (file-name-absolute-p fld-name)
336 (setq path (expand-file-name fld-name))
337 (setq path fld-name))
338 (list 'maildir path))))
340 (defun elmo-folder-maildir-p (folder)
342 (let ((li elmo-maildir-list))
344 (if (string-match (car li) folder)
346 (setq li (cdr li))))))
348 (defun elmo-localnews-get-spec (folder)
353 (if (eq (length (setq fld-name
354 (elmo-match-string 2 folder))) 0)
358 (elmo-replace-in-string fld-name "\\." "/")))))
360 (defun elmo-cache-get-spec (folder)
365 (if (eq (length (setq fld-name
366 (elmo-match-string 2 folder))) 0)
370 (elmo-replace-in-string fld-name "\\." "/")))))
372 ;; Archive interface by OKUNISHI Fujikazu <fuji0924@mbox.kyoto-inet.or.jp>
373 (defun elmo-archive-get-spec (folder)
374 (require 'elmo-archive)
375 (let (fld-name type prefix)
377 "^\\(\\$\\)\\([^;]*\\);?\\([^;]*\\);?\\([^;]*\\)$"
379 ;; Drive letter is OK!
380 (if (eq (length (setq fld-name
381 (elmo-match-string 2 folder))) 0)
384 (if (eq (length (setq type
385 (elmo-match-string 3 folder))) 0)
386 (setq type (symbol-name elmo-archive-default-type)))
387 (if (eq (length (setq prefix
388 (elmo-match-string 4 folder))) 0)
390 (list 'archive fld-name (intern-soft type) prefix))))
392 (defun elmo-pop3-get-spec (folder)
393 (let ((stream-type-alist elmo-network-stream-type-alist)
395 (if elmo-pop3-stream-type-alist
396 (setq stream-type-alist
397 (append elmo-pop3-stream-type-alist stream-type-alist)))
398 (setq spec (elmo-network-get-spec folder
399 elmo-default-pop3-server
400 elmo-default-pop3-port
401 elmo-default-pop3-stream-type
403 (setq folder (car spec))
405 "^\\(&\\)\\([^:/!]*\\)\\(/[^/:@!]+\\)?"
407 (setq user (if (match-beginning 2)
408 (elmo-match-string 2 folder)))
409 (if (eq (length user) 0)
410 (setq user elmo-default-pop3-user))
411 (setq auth (if (match-beginning 3)
412 (intern (elmo-match-substring 3 folder 1))
413 elmo-default-pop3-authenticate-type))
414 (append (list 'pop3 user auth)
417 (defsubst elmo-pop3-spec-username (spec)
420 (defsubst elmo-pop3-spec-auth (spec)
423 (defsubst elmo-pop3-spec-hostname (spec)
426 (defsubst elmo-pop3-spec-port (spec)
429 (defsubst elmo-pop3-spec-stream-type (spec)
432 (defun elmo-internal-get-spec (folder)
433 (if (string-match "\\('\\)\\([^/]*\\)/?\\(.*\\)$" folder)
434 (let* ((item (downcase (elmo-match-string 2 folder)))
435 (sym (and (> (length item) 0) (intern item))))
436 (cond ((or (null sym)
438 (list 'internal sym (elmo-match-string 3 folder)))
440 (list 'cache (elmo-match-string 3 folder)))
441 (t (error "Invalid internal folder spec"))))))
443 (defun elmo-multi-get-spec (folder)
446 "^\\(\\*\\)\\(.*\\)$"
448 (append (list 'multi)
450 (elmo-match-string 2 folder)
453 (defun elmo-filter-get-spec (folder)
454 (when (string-match "^\\(/\\)\\(.*\\)$" folder)
455 (let ((folder (elmo-match-string 2 folder))
457 (setq pair (elmo-parse-search-condition folder))
458 (if (string-match "^ */\\(.*\\)$" (cdr pair))
459 (list 'filter (car pair) (elmo-match-string 1 (cdr pair)))
460 (error "Folder syntax error `%s'" folder)))))
462 (defun elmo-pipe-get-spec (folder)
463 (when (string-match "^\\(|\\)\\([^|]*\\)|\\(.*\\)$" folder)
465 (elmo-match-string 2 folder)
466 (elmo-match-string 3 folder))))
468 (defun elmo-folder-get-spec (folder)
469 "return spec of folder"
470 (let ((type (elmo-folder-get-type folder)))
472 (funcall (intern (concat "elmo-" (symbol-name type) "-get-spec"))
474 (error "%s is not supported folder type" folder))))
477 (defun elmo-read-search-condition (default)
478 "Read search condition string interactively"
479 (elmo-read-search-condition-internal "Search by" default))
481 (defun elmo-read-search-condition-internal (prompt default)
482 (let* ((completion-ignore-case t)
483 (field (completing-read
484 (format "%s (%s): " prompt default)
488 "From" "Subject" "To" "Cc" "Body"
489 "Since" "Before" "ToCc"
490 "!From" "!Subject" "!To" "!Cc" "!Body"
491 "!Since" "!Before" "!ToCc")
492 elmo-msgdb-extra-fields)))))
493 (setq field (if (string= field "")
497 ((or (string= field "AND") (string= field "OR"))
499 (elmo-read-search-condition-internal
500 (concat field "(1) Search by") default)
501 (if (string= field "AND") "&" "|")
502 (elmo-read-search-condition-internal
503 (concat field "(2) Search by") default)
505 ((string-match "Since\\|Before" field)
508 (completing-read (format "Value for '%s': " field)
511 (list (format "%s" (car x)))))
512 elmo-date-descriptions))))
517 (read-from-minibuffer
518 (format "Value for '%s': " field))))))))
520 (defsubst elmo-condition-parse-error ()
521 (error "Syntax error in '%s'" (buffer-string)))
523 (defun elmo-parse-search-condition (condition)
525 Return value is a cons cell of (STRUCTURE . REST)"
528 (goto-char (point-min))
529 (cons (elmo-condition-parse) (buffer-substring (point) (point-max)))))
531 ;; condition ::= or-expr
532 (defun elmo-condition-parse ()
533 (or (elmo-condition-parse-or-expr)
534 (elmo-condition-parse-error)))
536 ;; or-expr ::= and-expr /
537 ;; and-expr "|" or-expr
538 (defun elmo-condition-parse-or-expr ()
539 (let ((left (elmo-condition-parse-and-expr)))
540 (if (looking-at "| *")
542 (goto-char (match-end 0))
543 (list 'or left (elmo-condition-parse-or-expr)))
546 ;; and-expr ::= primitive /
547 ;; primitive "&" and-expr
548 (defun elmo-condition-parse-and-expr ()
549 (let ((left (elmo-condition-parse-primitive)))
550 (if (looking-at "& *")
552 (goto-char (match-end 0))
553 (list 'and left (elmo-condition-parse-and-expr)))
556 ;; primitive ::= "(" expr ")" /
557 ;; ["!"] search-key SPACE* ":" SPACE* search-value
558 (defun elmo-condition-parse-primitive ()
561 (goto-char (match-end 0))
562 (prog1 (elmo-condition-parse)
563 (unless (looking-at ") *")
564 (elmo-condition-parse-error))
565 (goto-char (match-end 0))))
566 ;; search-key ::= [A-Za-z-]+
567 ;; ;; "since" / "before" / "last" / "first" /
568 ;; ;; "body" / field-name
569 ((looking-at "\\(!\\)? *\\([A-Za-z-]+\\) *: *")
570 (goto-char (match-end 0))
571 (let ((search-key (vector
572 (if (match-beginning 1) 'unmatch 'match)
573 (elmo-match-buffer 2)
574 (elmo-condition-parse-search-value))))
576 (if (string= (aref search-key 1) "tocc")
577 (if (eq (aref search-key 0) 'match)
579 (vector 'match "to" (aref search-key 2))
580 (vector 'match "cc" (aref search-key 2)))
582 (vector 'unmatch "to" (aref search-key 2))
583 (vector 'unmatch "cc" (aref search-key 2))))
586 ;; search-value ::= quoted / time / number / atom
587 ;; quoted ::= <elisp string expression>
588 ;; time ::= "yesterday" / "lastweek" / "lastmonth" / "lastyear" /
589 ;; number SPACE* "daysago" /
590 ;; number "-" month "-" number ; ex. 10-May-2000
592 ;; month ::= "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" /
593 ;; "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
594 ;; atom ::= ATOM_CHARS*
595 ;; SPACE ::= <ascii space character, 0x20>
596 ;; ATOM_CHARS ::= <any character except specials>
597 ;; specials ::= SPACE / <"> / </> / <)> / <|> / <&>
598 ;; ;; These characters should be quoted.
599 (defun elmo-condition-parse-search-value ()
602 (read (current-buffer)))
603 ((or (looking-at "yesterday") (looking-at "lastweek")
604 (looking-at "lastmonth") (looking-at "lastyear")
605 (looking-at "[0-9]+ *daysago")
606 (looking-at "[0-9]+-[A-Za-z]+-[0-9]+")
607 (looking-at "[0-9]+")
608 (looking-at "[^/ \")|&]*") ; atom* (except quoted specials).
610 (prog1 (elmo-match-buffer 0)
611 (goto-char (match-end 0))))
612 (t (error "Syntax error '%s'" (buffer-string)))))
615 (defun elmo-multi-get-real-folder-number (folder number)
616 (let* ((spec (elmo-folder-get-spec folder))
619 (fld (nth (- (/ num elmo-multi-divide-number) 1) flds)))
620 (cons fld (% num elmo-multi-divide-number))))
622 (defsubst elmo-buffer-replace (regexp &optional newtext)
623 (goto-char (point-min))
624 (while (re-search-forward regexp nil t)
625 (replace-match (or newtext ""))))
627 (defsubst elmo-delete-char (char string &optional unibyte)
630 (let ((coding-system-for-read 'no-conversion)
631 (coding-system-for-write 'no-conversion))
632 (if unibyte (elmo-set-buffer-multibyte nil))
634 (goto-char (point-min))
635 (while (search-forward (char-to-string char) nil t)
639 (defsubst elmo-delete-cr-get-content-type ()
641 (goto-char (point-min))
642 (while (search-forward "\r\n" nil t)
643 (replace-match "\n"))
644 (goto-char (point-min))
645 (or (std11-field-body "content-type")
648 (defun elmo-delete-cr (string)
652 (goto-char (point-min))
653 (while (search-forward "\r\n" nil t)
654 (replace-match "\n"))
657 (defun elmo-uniq-list (lst)
658 "Distractively uniqfy elements of LST."
660 (while tmp (setq tmp (setcdr tmp (and (cdr tmp) (delete (car tmp) (cdr tmp)))))))
663 (defun elmo-string-partial-p (string)
664 (and (stringp string) (string-match "message/partial" string)))
666 (defun elmo-get-file-string (filename &optional remove-final-newline)
668 (let (insert-file-contents-pre-hook ; To avoid autoconv-xmas...
669 insert-file-contents-post-hook)
670 (when (file-exists-p filename)
672 (as-binary-input-file (insert-file-contents filename)))
673 (when (and remove-final-newline
675 (= (char-after (1- (point-max))) ?\n))
676 (goto-char (point-max))
677 (delete-backward-char 1))
680 (defun elmo-save-string (string filename)
683 (as-binary-output-file
685 (write-region (point-min) (point-max)
686 filename nil 'no-msg))
689 (defun elmo-max-of-list (nlist)
693 (if (< max-num (car l))
694 (setq max-num (car l)))
698 (defun elmo-concat-path (path filename)
699 (if (not (string= path ""))
700 (if (string= elmo-path-sep (substring path (- (length path) 1)))
701 (concat path filename)
702 (concat path elmo-path-sep filename))
705 (defvar elmo-passwd-alist nil)
707 (defun elmo-passwd-alist-load ()
709 (let ((filename (expand-file-name elmo-passwd-alist-file-name
711 (tmp-buffer (get-buffer-create " *elmo-passwd-alist-tmp*"))
712 insert-file-contents-pre-hook ; To avoid autoconv-xmas...
713 insert-file-contents-post-hook
715 (if (not (file-readable-p filename))
717 (set-buffer tmp-buffer)
718 (insert-file-contents filename)
721 (read (current-buffer))
723 (kill-buffer tmp-buffer)
726 (defun elmo-passwd-alist-clear ()
727 "Clear password cache."
729 (setq elmo-passwd-alist nil))
731 (defun elmo-passwd-alist-save ()
732 "Save password into file."
735 (let ((filename (expand-file-name elmo-passwd-alist-file-name
737 (tmp-buffer (get-buffer-create " *elmo-passwd-alist-tmp*")))
738 (set-buffer tmp-buffer)
740 (prin1 elmo-passwd-alist tmp-buffer)
741 (princ "\n" tmp-buffer)
742 ; (if (and (file-exists-p filename)
743 ; (not (equal 384 (file-modes filename))))
744 ; (error "%s is not safe.chmod 600 %s!" filename filename))
745 (if (file-writable-p filename)
747 (write-region (point-min) (point-max)
748 filename nil 'no-msg)
749 (set-file-modes filename 384))
750 (message (format "%s is not writable." filename)))
751 (kill-buffer tmp-buffer))))
753 (defun elmo-get-passwd (key)
754 "Get password from password pool."
756 (if (not elmo-passwd-alist)
757 (setq elmo-passwd-alist (elmo-passwd-alist-load)))
758 (setq pair (assoc key elmo-passwd-alist))
760 (elmo-base64-decode-string (cdr pair))
761 (setq pass (elmo-read-passwd (format "Password for %s: "
763 (setq elmo-passwd-alist
764 (append elmo-passwd-alist
766 (elmo-base64-encode-string pass)))))
767 (if elmo-passwd-life-time
768 (run-with-timer elmo-passwd-life-time nil
769 (` (lambda () (elmo-remove-passwd (, key))))))
772 (defun elmo-remove-passwd (key)
773 "Remove password from password pool (for failure)."
775 (if (setq pass-cons (assoc key elmo-passwd-alist))
778 (fillarray (cdr pass-cons) 0))
779 (setq elmo-passwd-alist
780 (delete pass-cons elmo-passwd-alist))))))
782 (defmacro elmo-read-char-exclusive ()
783 (cond ((featurep 'xemacs)
784 '(let ((table (quote ((backspace . ?\C-h) (delete . ?\C-?)
789 (key-press-event-p (setq event (next-command-event)))
790 (setq key (or (event-to-character event)
791 (cdr (assq (event-key event) table)))))))
793 ((fboundp 'read-char-exclusive)
794 '(read-char-exclusive))
798 (defun elmo-read-passwd (prompt &optional stars)
799 "Read a single line of text from user without echoing, and return it."
803 (cursor-in-echo-area t)
804 (log-message-max-size 0)
805 message-log-max done msg truncate)
807 (if (or (not stars) (string= "" ans))
809 (setq msg (concat prompt (make-string (length ans) ?.)))
811 (1+ (- (length msg) (window-width (minibuffer-window)))))
813 (setq msg (concat "$" (substring msg (1+ truncate))))))
815 (setq c (elmo-read-char-exclusive))
819 ((or (= c ?\r) (= c ?\n) (= c ?\e))
823 ((and (/= c ?\b) (/= c ?\177))
824 (setq ans (concat ans (char-to-string c))))
826 (setq ans (substring ans 0 -1)))))
836 (defun elmo-replace-in-string (str regexp newtext &optional literal)
837 "Replaces all matches in STR for REGEXP with NEWTEXT string,
838 and returns the new string.
839 Optional LITERAL non-nil means do a literal replacement.
840 Otherwise treat \\ in NEWTEXT string as special:
841 \\& means substitute original matched text,
842 \\N means substitute match for \(...\) number N,
843 \\\\ means insert one \\."
848 (while (setq match (string-match regexp str start))
849 (setq prev-start start
854 (substring str prev-start match)
855 (cond (literal newtext)
862 (cond ((eq c ?\\) "\\")
864 (elmo-match-string 0 str))
865 ((and (>= c ?0) (<= c ?9))
866 (if (> c (+ ?0 (length
869 (error "Invalid match num: %c" c)
871 (elmo-match-string c str)))
872 (t (char-to-string c))))
873 (if (eq c ?\\) (progn (setq special t) nil)
874 (char-to-string c)))))
876 (concat rtn-str (substring str start))))
878 (defun elmo-string-to-list (string)
881 (goto-char (point-min))
883 (goto-char (point-max))
885 (goto-char (point-min))
886 (read (current-buffer))))
888 (defun elmo-plug-on-by-servers (alist &optional servers)
889 (let ((server-list (or servers elmo-plug-on-servers)))
892 (if (elmo-plugged-p (car server-list))
894 (setq server-list (cdr server-list))))))
896 (defun elmo-plug-on-by-exclude-servers (alist &optional servers)
897 (let ((server-list (or servers elmo-plug-on-exclude-servers))
898 server other-servers)
900 (when (and (not (member (setq server (caaar alist)) server-list))
901 (not (member server other-servers)))
902 (push server other-servers))
903 (setq alist (cdr alist)))
904 (elmo-plug-on-by-servers alist other-servers)))
906 (defun elmo-plugged-p (&optional server port alist label-exp)
907 (let ((alist (or alist elmo-plugged-alist))
909 (cond ((and (not port) (not server))
910 (cond ((eq elmo-plugged-condition 'one)
913 (if (nth 2 (car alist))
915 (setq alist (cdr alist)))))
916 ((eq elmo-plugged-condition 'all)
919 (if (not (nth 2 (car alist)))
920 (throw 'plugged nil))
921 (setq alist (cdr alist)))
923 ((functionp elmo-plugged-condition)
924 (funcall elmo-plugged-condition alist))
927 ((not port) ;; server
930 (when (string= server (caaar alist))
931 (if (nth 2 (car alist))
933 (setq alist (cdr alist)))))
935 (setq plugged-info (assoc (cons server port) alist))
936 (if (not plugged-info)
937 ;; add elmo-plugged-alist automatically
939 (elmo-set-plugged elmo-plugged server port nil nil label-exp)
941 (if (and elmo-auto-change-plugged
942 (> elmo-auto-change-plugged 0)
943 (nth 3 plugged-info) ;; time
944 (elmo-time-expire (nth 3 plugged-info)
945 elmo-auto-change-plugged))
947 (nth 2 plugged-info)))))))
949 (defun elmo-set-plugged (plugged &optional server port time
951 (let ((alist (or alist elmo-plugged-alist))
953 (cond ((and (not port) (not server))
954 (setq elmo-plugged plugged)
955 ;; set plugged all element of elmo-plugged-alist.
957 (setcdr (cdar alist) (list plugged time))
958 (setq alist (cdr alist))))
960 ;; set plugged all port of server
962 (when (string= server (caaar alist))
963 (setcdr (cdar alist) (list plugged time)))
964 (setq alist (cdr alist))))
966 ;; set plugged one port of server
967 (setq plugged-info (assoc (cons server port) alist))
968 (setq label (if label-exp
970 (nth 1 plugged-info)))
972 ;; if add is non-nil, don't reset plug state.
974 (setcdr plugged-info (list label plugged time)))
976 (setq elmo-plugged-alist
977 (nconc elmo-plugged-alist
979 (list (cons server port) label plugged time))))))))
982 (defun elmo-delete-plugged (&optional server port alist)
983 (let* ((alist (or alist elmo-plugged-alist))
985 (cond ((and (not port) (not server))
988 ;; delete plugged all port of server
990 (when (string= server (caaar alist2))
991 (setq alist (delete (car alist2) alist)))
992 (setq alist2 (cdr alist2))))
994 ;; delete plugged one port of server
996 (delete (assoc (cons server port) alist) alist))))
999 (defun elmo-disk-usage (path)
1000 "Get disk usage (bytes) in PATH."
1002 (condition-case () (file-attributes path) (error nil))))
1004 (if (nth 0 file-attr) ; directory
1005 (let ((files (condition-case ()
1006 (directory-files path t "^[^\\.]")
1009 ;; (result (nth 7 file-attr))) ... directory size
1011 (setq result (+ result (or (elmo-disk-usage (car files)) 0)))
1012 (setq files (cdr files)))
1014 (float (nth 7 file-attr))))))
1016 (defun elmo-get-last-accessed-time (path &optional dir)
1017 "Returns last accessed time."
1018 (let ((last-accessed (nth 4 (file-attributes (or (and dir
1023 (setq last-accessed (+ (* (nth 0 last-accessed)
1024 (float 65536)) (nth 1 last-accessed)))
1027 (defun elmo-get-last-modification-time (path &optional dir)
1028 "Returns last accessed time."
1029 (let ((last-modified (nth 5 (file-attributes (or (and dir
1033 (setq last-modified (+ (* (nth 0 last-modified)
1034 (float 65536)) (nth 1 last-modified)))))
1036 (defun elmo-make-directory (path)
1037 "create directory recursively."
1038 (let ((parent (directory-file-name (file-name-directory path))))
1039 (if (null (file-directory-p parent))
1040 (elmo-make-directory parent))
1041 (make-directory path)
1042 (if (string= path (expand-file-name elmo-msgdb-dir))
1043 (set-file-modes path 448) ; 700
1046 (defun elmo-delete-directory (path &optional no-hierarchy)
1047 "delete directory recursively."
1048 (let ((dirent (directory-files path))
1049 relpath abspath hierarchy)
1051 (setq relpath (car dirent)
1053 abspath (expand-file-name relpath path))
1054 (when (not (string-match "^\\.\\.?$" relpath))
1055 (if (eq (nth 0 (file-attributes abspath)) t)
1058 (elmo-delete-directory abspath no-hierarchy))
1059 (delete-file abspath))))
1061 (delete-directory path))))
1063 (defun elmo-list-filter (l1 l2)
1066 ;; t means filter all.
1069 (elmo-delete-if (lambda (x) (not (memq x l1))) l2)
1073 (defun elmo-folder-local-p (folder)
1074 "Return whether FOLDER is a local folder or not."
1075 (let ((type (elmo-folder-get-type folder)))
1076 (memq type '(localdir localnews archive maildir internal cache))))
1078 (defun elmo-folder-writable-p (folder)
1079 (let ((type (elmo-folder-get-type folder)))
1080 (memq type '(imap4 localdir archive))))
1082 (defun elmo-multi-get-intlist-list (numlist &optional as-is)
1083 (let ((numbers (sort numlist '<))
1085 one-list int-list-list)
1087 (setq cur-number (+ cur-number 1))
1092 (* elmo-multi-divide-number cur-number))
1093 elmo-multi-divide-number)))
1094 (setq one-list (nconc
1100 (* elmo-multi-divide-number cur-number))))))
1101 (setq numbers (cdr numbers)))
1102 (setq int-list-list (nconc int-list-list (list one-list))))
1105 (defsubst elmo-list-delete-if-smaller (list number)
1106 (let ((ret-val (copy-sequence list)))
1108 (if (< (car list) number)
1109 (setq ret-val (delq (car list) ret-val)))
1110 (setq list (cdr list)))
1113 (defun elmo-list-diff (list1 list2 &optional mes)
1116 (let ((clist1 (copy-sequence list1))
1117 (clist2 (copy-sequence list2)))
1119 (setq clist1 (delq (car list2) clist1))
1120 (setq list2 (cdr list2)))
1122 (setq clist2 (delq (car list1) clist2))
1123 (setq list1 (cdr list1)))
1125 (message (concat mes "done.")))
1126 (list clist1 clist2)))
1128 (defun elmo-list-bigger-diff (list1 list2 &optional mes)
1129 "Returns a list (- +). + is bigger than max of LIST1, in LIST2"
1134 (max-of-l2 (or (nth (max 0 (1- (length l2))) l2) 0))
1138 (setq num (+ (length l1)))
1140 (if (memq (car l1) l2)
1141 (if (eq (car l1) (car l2))
1144 (if (> (car l1) max-of-l2)
1145 (setq diff1 (nconc diff1 (list (car l1))))))
1149 (setq percent (/ (* i 100) num))
1150 (if (eq (% percent 5) 0)
1151 (elmo-display-progress
1152 'elmo-list-bigger-diff "%s%d%%" percent mes))))
1154 (cons diff1 (list l2)))))
1156 (defun elmo-multi-list-bigger-diff (list1 list2 &optional mes)
1157 (let ((list1-list (elmo-multi-get-intlist-list list1 t))
1158 (list2-list (elmo-multi-get-intlist-list list2 t))
1161 (while (or list1-list list2-list)
1162 (setq result (elmo-list-bigger-diff (car list1-list) (car list2-list)
1164 (setq dels (append dels (car result)))
1165 (setq news (append news (cadr result)))
1166 (setq list1-list (cdr list1-list))
1167 (setq list2-list (cdr list2-list)))
1168 (cons dels (list news))))
1170 (defvar elmo-imap4-name-space-regexp-list nil)
1171 (defun elmo-imap4-identical-name-space-p (fld1 fld2)
1173 (if (or (eq (string-to-char fld1) ?#)
1174 (eq (string-to-char fld2) ?#))
1175 (string= (car (split-string fld1 "/"))
1176 (car (split-string fld2 "/")))
1179 (defun elmo-folder-identical-system-p (folder1 folder2)
1180 "folder1 and folder2 should be real folder (not virtual)."
1181 (cond ((eq (elmo-folder-get-type folder1) 'imap4)
1182 (let ((spec1 (elmo-folder-get-spec folder1))
1183 (spec2 (elmo-folder-get-spec folder2)))
1184 (and ;(elmo-imap4-identical-name-space-p ;; No use.
1185 ; (nth 1 spec1) (nth 1 spec2))
1186 (string= (elmo-imap4-spec-hostname spec1)
1187 (elmo-imap4-spec-hostname spec2)) ; hostname
1188 (string= (elmo-imap4-spec-username spec1)
1189 (elmo-imap4-spec-username spec2))))) ; username
1191 (elmo-folder-direct-copy-p folder1 folder2))))
1193 (defconst elmo-folder-direct-copy-alist
1194 '((localdir . (localdir localnews archive))
1195 (maildir . (maildir localdir localnews archive))
1196 (localnews . (localdir localnews archive))
1197 (archive . (localdir localnews archive))
1198 (cache . (localdir localnews archive))))
1200 (defun elmo-folder-direct-copy-p (src-folder dst-folder)
1201 (let ((src-type (car (elmo-folder-get-spec src-folder)))
1202 (dst-type (car (elmo-folder-get-spec dst-folder)))
1204 (and (setq dst-copy-type
1205 (cdr (assq src-type elmo-folder-direct-copy-alist)))
1206 (memq dst-type dst-copy-type))))
1208 (defmacro elmo-filter-type (filter)
1209 (` (aref (, filter) 0)))
1211 (defmacro elmo-filter-key (filter)
1212 (` (aref (, filter) 1)))
1214 (defmacro elmo-filter-value (filter)
1215 (` (aref (, filter) 2)))
1217 (defsubst elmo-buffer-field-primitive-condition-match (condition
1221 (goto-char (point-min))
1223 ((string= (elmo-filter-key condition) "last")
1224 (setq result (<= (length (memq number number-list))
1225 (string-to-int (elmo-filter-value condition)))))
1226 ((string= (elmo-filter-key condition) "first")
1227 (setq result (< (- (length number-list)
1228 (length (memq number number-list)))
1229 (string-to-int (elmo-filter-value condition)))))
1230 ((string= (elmo-filter-key condition) "since")
1231 (let ((date (elmo-date-get-datevec (elmo-filter-value condition))))
1234 (timezone-make-sortable-date (aref date 0)
1237 (timezone-make-time-string
1241 (timezone-make-date-sortable (std11-field-body "date"))))))
1242 ((string= (elmo-filter-key condition) "before")
1243 (let ((date (elmo-date-get-datevec (elmo-filter-value condition))))
1246 (timezone-make-date-sortable (std11-field-body "date"))
1247 (timezone-make-sortable-date (aref date 0)
1250 (timezone-make-time-string
1254 ((string= (elmo-filter-key condition) "body")
1255 (and (re-search-forward "^$" nil t) ; goto body
1256 (setq result (search-forward (elmo-filter-value condition)
1259 (let ((fval (std11-field-body (elmo-filter-key condition))))
1260 (if (eq (length fval) 0) (setq fval nil))
1261 (if fval (setq fval (eword-decode-string fval)))
1262 (setq result (and fval (string-match
1263 (elmo-filter-value condition) fval))))))
1264 (if (eq (elmo-filter-type condition) 'unmatch)
1265 (setq result (not result)))
1268 (defun elmo-condition-find-key-internal (condition key)
1270 ((vectorp condition)
1271 (if (string= (elmo-filter-key condition) key)
1273 ((or (eq (car condition) 'and)
1274 (eq (car condition) 'or))
1275 (elmo-condition-find-key-internal (nth 1 condition) key)
1276 (elmo-condition-find-key-internal (nth 2 condition) key))))
1278 (defun elmo-condition-find-key (condition key)
1280 (elmo-condition-find-key-internal condition key)))
1282 (defun elmo-buffer-field-condition-match (condition number number-list)
1284 ((vectorp condition)
1285 (elmo-buffer-field-primitive-condition-match
1286 condition number number-list))
1287 ((eq (car condition) 'and)
1288 (and (elmo-buffer-field-condition-match
1289 (nth 1 condition) number number-list)
1290 (elmo-buffer-field-condition-match
1291 (nth 2 condition) number number-list)))
1292 ((eq (car condition) 'or)
1293 (or (elmo-buffer-field-condition-match
1294 (nth 1 condition) number number-list)
1295 (elmo-buffer-field-condition-match
1296 (nth 2 condition) number number-list)))))
1298 (defsubst elmo-file-field-condition-match (file condition number number-list)
1300 (as-binary-input-file (insert-file-contents file))
1301 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
1302 ;; Should consider charset?
1303 (decode-mime-charset-region (point-min)(point-max) elmo-mime-charset)
1304 (elmo-buffer-field-condition-match condition number number-list)))
1306 (defmacro elmo-get-hash-val (string hashtable)
1307 (let ((sym (list 'intern-soft string hashtable)))
1308 (list 'if (list 'boundp sym)
1309 (list 'symbol-value sym))))
1311 (defmacro elmo-set-hash-val (string value hashtable)
1312 (list 'set (list 'intern string hashtable) value))
1314 (defmacro elmo-clear-hash-val (string hashtable)
1315 (static-if (fboundp 'unintern)
1316 (list 'unintern string hashtable)
1317 (list 'makunbound (list 'intern string hashtable))))
1319 ;; Make a hash table (default and minimum size is 1024).
1320 (defun elmo-make-hash (&optional hashsize)
1322 (if hashsize (max (min (elmo-create-hash-size hashsize)
1323 elmo-hash-maximum-size) 1024) 1024) 0))
1325 (defsubst elmo-mime-string (string)
1326 "Normalize MIME encoded string."
1330 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
1331 (setq str (eword-decode-string
1332 (decode-mime-charset-string string elmo-mime-charset)))
1333 (setq str (encode-mime-charset-string str elmo-mime-charset))
1334 (elmo-set-buffer-multibyte nil)
1337 (defsubst elmo-collect-field (beg end downcase-field-name)
1340 (narrow-to-region beg end)
1341 (goto-char (point-min))
1342 (let ((regexp (concat "\\(" std11-field-head-regexp "\\)[ \t]*"))
1344 (while (re-search-forward regexp nil t)
1345 (setq name (buffer-substring-no-properties
1346 (match-beginning 1)(1- (match-end 1))))
1347 (if downcase-field-name
1348 (setq name (downcase name)))
1349 (setq body (buffer-substring-no-properties
1350 (match-end 0) (std11-field-end)))
1351 (or (assoc name dest)
1352 (setq dest (cons (cons name body) dest))))
1355 (defsubst elmo-collect-field-from-string (string downcase-field-name)
1358 (goto-char (point-min))
1359 (let ((regexp (concat "\\(" std11-field-head-regexp "\\)[ \t]*"))
1361 (while (re-search-forward regexp nil t)
1362 (setq name (buffer-substring-no-properties
1363 (match-beginning 1)(1- (match-end 1))))
1364 (if downcase-field-name
1365 (setq name (downcase name)))
1366 (setq body (buffer-substring-no-properties
1367 (match-end 0) (std11-field-end)))
1368 (or (assoc name dest)
1369 (setq dest (cons (cons name body) dest))))
1372 (defun elmo-create-hash-size (min)
1378 (defun elmo-safe-filename (folder)
1379 (elmo-replace-in-string
1380 (elmo-replace-in-string
1381 (elmo-replace-in-string folder "/" " ")
1385 (defvar elmo-msgid-replace-chars nil)
1387 (defsubst elmo-replace-msgid-as-filename (msgid)
1388 "Replace message-id string as filename."
1389 (setq msgid (elmo-replace-in-string msgid " " " "))
1390 (if (null elmo-msgid-replace-chars)
1391 (setq elmo-msgid-replace-chars
1392 (regexp-quote (mapconcat
1393 'car elmo-msgid-replace-string-alist ""))))
1394 (while (string-match (concat "[" elmo-msgid-replace-chars "]")
1397 (substring msgid 0 (match-beginning 0))
1400 (match-beginning 0) (match-end 0))
1401 elmo-msgid-replace-string-alist))
1402 (substring msgid (match-end 0)))))
1405 (defsubst elmo-recover-msgid-from-filename (filename)
1406 "Recover Message-ID from filename."
1408 (while (string-match " " filename)
1409 (setq tmp (substring filename
1411 (+ (match-end 0) 1)))
1412 (if (string= tmp " ")
1414 (setq tmp (car (rassoc tmp
1415 elmo-msgid-replace-string-alist))))
1418 (substring filename 0 (match-beginning 0))
1420 (setq filename (substring filename (+ (match-end 0) 1))))
1421 (concat result filename)))
1423 (defsubst elmo-copy-file (src dst)
1425 (elmo-add-name-to-file src dst t)
1426 (error (copy-file src dst t)
1427 (error "copy file failed"))))
1429 (defsubst elmo-buffer-exists-p (buffer)
1430 (if (bufferp buffer)
1431 (buffer-live-p buffer)
1432 (get-buffer buffer)))
1434 (defsubst elmo-kill-buffer (buffer)
1435 (when (elmo-buffer-exists-p buffer)
1436 (kill-buffer buffer)))
1438 (defun elmo-delete-if (pred lst)
1439 "Returns new list contains items which don't satisfy PRED in LST."
1442 (unless (funcall pred (car lst))
1443 (setq result (nconc result (list (car lst)))))
1444 (setq lst (cdr lst)))
1447 (defun elmo-list-delete (list1 list2)
1448 "Delete by side effect any occurrences equal to elements of LIST1 from LIST2.
1449 Return the modified LIST2. Deletion is done with `delete'.
1450 Write `(setq foo (elmo-list-delete bar foo))' to be sure of changing
1451 the value of `foo'."
1453 (setq list2 (delete (car list1) list2))
1454 (setq list1 (cdr list1)))
1457 (defun elmo-list-member (list1 list2)
1458 "If any element of list1 is member of list2, returns t."
1461 (if (member (car list1) list2)
1463 (setq list1 (cdr list1)))))
1465 (defun elmo-count-matches (regexp beg end)
1469 (while (re-search-forward regexp end t)
1470 (setq count (1+ count)))
1473 (if (fboundp 'display-error)
1474 (defalias 'elmo-display-error 'display-error)
1475 (defun elmo-display-error (error-object stream)
1476 "a tiny function to display error-object to the stream."
1478 (errobj error-object)
1481 (setq err-mes (concat err-mes (format
1482 (if (stringp (car errobj))
1484 (if (boundp 'nemacs-version)
1486 "%S")) (car errobj))))
1487 (setq errobj (cdr errobj))
1488 (if errobj (setq err-mes (concat err-mes (if first ": " ", "))))
1490 (princ err-mes stream))))
1492 (if (fboundp 'define-error)
1493 (defalias 'elmo-define-error 'define-error)
1494 (defun elmo-define-error (error doc &optional parents)
1496 (setq parents 'error))
1497 (let ((conds (get parents 'error-conditions)))
1499 (error "Not an error symbol: %s" error))
1501 (list 'error-message doc
1502 'error-conditions (cons error conds))))))
1504 (cond ((fboundp 'lprogress-display)
1505 (defalias 'elmo-display-progress 'lprogress-display))
1506 ((fboundp 'progress-feedback-with-label)
1507 (defalias 'elmo-display-progress 'progress-feedback-with-label))
1509 (defun elmo-display-progress (label format &optional value &rest args)
1510 "Print a progress message."
1511 (if (and (null format) (null args))
1513 (apply (function message) (concat format " %d%%")
1514 (nconc args (list value)))))))
1516 (defun elmo-time-expire (before-time diff-time)
1517 (let* ((current (current-time))
1518 (rest (when (< (nth 1 current) (nth 1 before-time))
1522 (list (- (+ (car current) (if rest -1 0)) (car before-time))
1523 (- (+ (or rest 0) (nth 1 current)) (nth 1 before-time))))
1524 (and (eq (car diff) 0)
1525 (< diff-time (nth 1 diff)))))
1527 (if (fboundp 'std11-fetch-field)
1528 (defalias 'elmo-field-body 'std11-fetch-field) ;;no narrow-to-region
1529 (defalias 'elmo-field-body 'std11-field-body))
1531 (defmacro elmo-string (string)
1532 "String without text property"
1533 (` (let ((obj (copy-sequence (, string))))
1534 (set-text-properties 0 (length obj) nil obj)
1537 (defun elmo-y-or-n-p (prompt &optional auto default)
1538 "Same as `y-or-n-p'.
1539 But if optional argument AUTO is non-nil, DEFAULT is returned."
1544 (defun elmo-string-member (string slist)
1545 "Returns t if STRING is a member of the SLIST."
1548 (if (and (stringp (car slist))
1549 (string= string (car slist)))
1551 (setq slist (cdr slist)))))
1553 (defun elmo-string-match-member (str list &optional case-ignore)
1554 (let ((case-fold-search case-ignore))
1557 (if (string-match (car list) str)
1558 (throw 'member (car list)))
1559 (setq list (cdr list))))))
1561 (defsubst elmo-string-delete-match (string pos)
1562 (concat (substring string
1563 0 (match-beginning pos))
1568 (defun elmo-string-match-assoc (key alist &optional case-ignore)
1569 (let ((case-fold-search case-ignore)
1573 (setq a (car alist))
1576 (string-match key (car a)))
1578 (setq alist (cdr alist))))))
1580 (defun elmo-string-matched-assoc (key alist &optional case-ignore)
1581 (let ((case-fold-search case-ignore)
1585 (setq a (car alist))
1588 (string-match (car a) key))
1590 (setq alist (cdr alist))))))
1592 (defun elmo-string-assoc (key alist)
1596 (setq a (car alist))
1599 (string= key (car a)))
1601 (setq alist (cdr alist))))))
1603 (defun elmo-string-rassoc (key alist)
1607 (setq a (car alist))
1610 (string= key (cdr a)))
1612 (setq alist (cdr alist))))))
1614 ;;; Number set defined by OKAZAKI Tetsurou <okazaki@be.to>
1616 ;; number ::= [0-9]+
1619 ;; number-range ::= "(" beg " . " end ")" ;; cons cell
1620 ;; number-set-elem ::= number / number-range
1621 ;; number-set ::= "(" *number-set-elem ")" ;; list
1623 (defun elmo-number-set-member (number number-set)
1624 "Returns non-nil if NUMBER is an element of NUMBER-SET.
1625 The value is actuall the tail of SET-LIST whose car contains NUMBER."
1626 (or (memq number number-set)
1628 (while (and number-set (not found))
1629 (if (and (consp (car number-set))
1630 (and (<= (car (car number-set)) number)
1631 (<= number (cdr (car number-set)))))
1633 (setq number-set (cdr number-set))))
1636 (defun elmo-number-set-append-list (number-set list)
1637 "Append LIST of numbers to the NUMBER-SET.
1638 NUMBER-SET is altered."
1639 (let ((appended number-set))
1641 (setq appended (elmo-number-set-append appended (car list)))
1642 (setq list (cdr list)))
1645 (defun elmo-number-set-append (number-set number)
1646 "Append NUMBER to the NUMBER-SET.
1647 NUMBER-SET is altered."
1648 (let ((number-set-1 number-set)
1650 (while (and number-set (not found))
1651 (setq elem (car number-set))
1654 (eq (+ 1 (cdr elem)) number))
1655 (setcdr elem number)
1657 ((and (integerp elem)
1658 (eq (+ 1 elem) number))
1659 (setcar number-set (cons elem number))
1661 ((or (and (integerp elem) (eq elem number))
1663 (<= (car elem) number)
1664 (<= number (cdr elem))))
1666 (setq number-set (cdr number-set)))
1668 (setq number-set-1 (nconc number-set-1 (list number))))
1671 (provide 'elmo-util)
1673 ;;; elmo-util.el ends here