home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / vm / vm-misc.el < prev    next >
Encoding:
Text File  |  1995-07-28  |  15.8 KB  |  491 lines

  1. ;;; Miscellaneous functions for VM
  2. ;;; Copyright (C) 1989, 1990, 1991, 1993, 1994 Kyle E. Jones
  3. ;;;
  4. ;;; This program is free software; you can redistribute it and/or modify
  5. ;;; it under the terms of the GNU General Public License as published by
  6. ;;; the Free Software Foundation; either version 1, or (at your option)
  7. ;;; any later version.
  8. ;;;
  9. ;;; This program is distributed in the hope that it will be useful,
  10. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ;;; GNU General Public License for more details.
  13. ;;;
  14. ;;; You should have received a copy of the GNU General Public License
  15. ;;; along with this program; if not, write to the Free Software
  16. ;;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. (provide 'vm-misc)
  19.  
  20. (defun vm-delete-non-matching-strings (regexp list &optional destructively)
  21.   "Delete strings matching REGEXP from LIST.
  22. Optional third arg non-nil means to destructively alter LIST, instead of
  23. working on a copy.
  24.  
  25. The new version of the list, minus the deleted strings, is returned."
  26.   (or destructively (setq list (copy-sequence list)))
  27.   (let ((curr list) (prev nil))
  28.     (while curr
  29.       (if (string-match regexp (car curr))
  30.       (setq prev curr
  31.         curr (cdr curr))
  32.     (if (null prev)
  33.         (setq list (cdr list)
  34.           curr list)
  35.       (setcdr prev (cdr curr))
  36.       (setq curr (cdr curr)))))
  37.     list ))
  38.  
  39. (defun vm-parse (string regexp &optional matchn)
  40.   (or matchn (setq matchn 1))
  41.   (let (list)
  42.     (store-match-data nil)
  43.     (while (string-match regexp string (match-end 0))
  44.       (setq list (cons (substring string (match-beginning matchn)
  45.                   (match-end matchn)) list)))
  46.     (nreverse list)))
  47.  
  48. (defun vm-parse-addresses (string)
  49.   (if (null string)
  50.       ()
  51.     (let (work-buffer)
  52.       (save-excursion
  53.        (unwind-protect
  54.        (let (list start s char)
  55.          (setq work-buffer (generate-new-buffer "*vm-work*"))
  56.          (set-buffer work-buffer)
  57.          (insert string)
  58.          (goto-char (point-min))
  59.          (skip-chars-forward "\t\f\n\r ")
  60.          (setq start (point))
  61.          (while (not (eobp))
  62.            (skip-chars-forward "^\"\\,(")
  63.            (setq char (following-char))
  64.            (cond ((= char ?\\)
  65.               (forward-char 1)
  66.               (if (not (eobp))
  67.               (forward-char 1)))
  68.              ((= char ?,)
  69.               (setq s (buffer-substring start (point)))
  70.               (if (or (null (string-match "^[\t\f\n\r ]+$" s))
  71.                   (not (string= s "")))
  72.               (setq list (cons s list)))
  73.               (forward-char 1)
  74.               (skip-chars-forward "\t\f\n\r ")
  75.               (setq start (point)))
  76.              ((= char ?\")
  77.               (forward-char 1)
  78.               (re-search-forward "[^\\]\"" nil 0))
  79.              ((= char ?\()
  80.               (let ((parens 1))
  81.             (forward-char 1)
  82.             (while (and (not (eobp)) (not (zerop parens)))
  83.               (re-search-forward "[^\\][()]" nil 0)
  84.               (cond ((eobp))
  85.                 ((= (preceding-char) ?\()
  86.                  (setq parens (1+ parens)))
  87.                 ((= (preceding-char) ?\))
  88.                  (setq parens (1- parens)))))))))
  89.          (setq s (buffer-substring start (point)))
  90.          (if (and (null (string-match "^[\t\f\n\r ]+$" s))
  91.               (not (string= s "")))
  92.          (setq list (cons s list)))
  93.          (nreverse list)) ; jwz: fixed order
  94.     (and work-buffer (kill-buffer work-buffer)))))))
  95.  
  96. (defun vm-write-string (where string)
  97.   (if (bufferp where)
  98.       (vm-save-buffer-excursion
  99.     (set-buffer where)
  100.     (goto-char (point-max))
  101.     (insert string))
  102.     (let ((temp-buffer nil))
  103.       (unwind-protect
  104.       (save-excursion
  105.         (setq temp-buffer (generate-new-buffer "*vm-work*"))
  106.         (set-buffer temp-buffer)
  107.         (insert string)
  108.         (write-region (point-min) (point-max) where t 'quiet))
  109.     (and temp-buffer (kill-buffer temp-buffer))))))
  110.  
  111. (defmacro vm-marker (pos &optional buffer)
  112.   (list 'set-marker '(make-marker) pos buffer))
  113.  
  114. (defmacro vm-increment (variable)
  115.   (list 'setq variable (list '1+ variable)))
  116.  
  117. (defmacro vm-decrement (variable)
  118.   (list 'setq variable (list '1- variable)))
  119.  
  120. (defmacro vm-select-folder-buffer ()
  121.   '(and vm-mail-buffer
  122.     (or (buffer-name vm-mail-buffer)
  123.         (error "Folder buffer has been killed."))
  124.     (set-buffer vm-mail-buffer)))
  125.  
  126. (defun vm-check-for-killed-summary ()
  127.   (and (bufferp vm-summary-buffer) (null (buffer-name vm-summary-buffer))
  128.        (let ((mp vm-message-list))
  129.      (setq vm-summary-buffer nil)
  130.      (while mp
  131.        (vm-set-su-start-of (car mp) nil)
  132.        (vm-set-su-end-of (car mp) nil)
  133.        (setq mp (cdr mp))))))
  134.  
  135. (defun vm-check-for-killed-folder ()
  136.   (and (bufferp vm-mail-buffer) (null (buffer-name vm-mail-buffer))
  137.        (setq vm-mail-buffer nil)))
  138.  
  139. (defmacro vm-error-if-folder-read-only ()
  140.   '(while vm-folder-read-only
  141.      (signal 'folder-read-only (list (current-buffer)))))
  142.  
  143. (put 'folder-read-only 'error-conditions '(folder-read-only error))
  144. (put 'folder-read-only 'error-message "Folder is read-only")
  145.  
  146. (defmacro vm-error-if-virtual-folder ()
  147.   '(and (eq major-mode 'vm-virtual-mode)
  148.     (error "%s cannot be applied to virtual folders." this-command)))
  149.  
  150. (defmacro vm-build-threads-if-unbuilt ()
  151.   '(if (null vm-thread-obarray)
  152.        (vm-build-threads nil)))
  153.  
  154. (defun vm-abs (n) (if (< n 0) (- n) n))
  155.  
  156. ;; save-restriction flubs restoring the clipping region if you
  157. ;; (widen) and modify text outside the old region.
  158. ;; This should do it right.
  159. (defmacro vm-save-restriction (&rest forms)
  160.   (let ((vm-sr-clip (make-symbol "vm-sr-clip"))
  161.     (vm-sr-min (make-symbol "vm-sr-min"))
  162.     (vm-sr-max (make-symbol "vm-sr-max")))
  163.     (list 'let (list (list vm-sr-clip '(> (buffer-size)
  164.                       (- (point-max) (point-min))))
  165.              ;; this shouldn't be necessary but the
  166.              ;; byte-compiler turns these into interned symbols
  167.              ;; which utterly defeats the purpose of the
  168.              ;; make-symbol calls above.  Soooo, until the compiler
  169.              ;; is fixed, these must be made into (let ...)
  170.              ;; temporaries so that nested calls to this macros
  171.              ;; won't misbehave.
  172.              vm-sr-min vm-sr-max)
  173.       (list 'and vm-sr-clip
  174.         (list 'setq vm-sr-min '(set-marker (make-marker) (point-min)))
  175.         (list 'setq vm-sr-max '(set-marker (make-marker) (point-max))))
  176.       (list 'unwind-protect (cons 'progn forms)
  177.         '(widen)
  178.         (list 'and vm-sr-clip
  179.               (list 'progn
  180.                 (list 'narrow-to-region vm-sr-min vm-sr-max)
  181.                 (list 'set-marker vm-sr-min nil)
  182.                 (list 'set-marker vm-sr-max nil)))))))
  183.  
  184. (defmacro vm-save-buffer-excursion (&rest forms)
  185.   (list 'let '((vm-sbe-buffer (current-buffer)))
  186.     (list 'unwind-protect
  187.           (cons 'progn forms)
  188.           '(and (not (eq vm-sbe-buffer (current-buffer)))
  189.             (buffer-name vm-sbe-buffer)
  190.             (set-buffer vm-sbe-buffer)))))
  191.  
  192. (defun vm-last (list) (while (cdr-safe list) (setq list (cdr list))) list)
  193.  
  194. (defun vm-vector-to-list (vector)
  195.   (let ((i (1- (length vector)))
  196.     list)
  197.     (while (>= i 0)
  198.       (setq list (cons (aref vector i) list))
  199.       (vm-decrement i))
  200.     list ))
  201.  
  202. (defun vm-extend-vector (vector length &optional fill)
  203.   (let ((vlength (length vector)))
  204.     (if (< vlength length)
  205.     (apply 'vector (nconc (vm-vector-to-list vector)
  206.                   (make-list (- length vlength) fill)))
  207.       vector )))
  208.  
  209. (defun vm-obarray-to-string-list (obarray)
  210.   (let ((list nil))
  211.     (mapatoms (function (lambda (s) (setq list (cons (symbol-name s) list))))
  212.           obarray)
  213.     list ))
  214.  
  215. (defun vm-mapcar (function &rest lists)
  216.   (let (arglist result)
  217.     (while (car lists)
  218.       (setq arglist (mapcar 'car lists))
  219.       (setq result (cons (apply function arglist) result))
  220.       (setq lists (mapcar 'cdr lists)))
  221.     (nreverse result)))
  222.  
  223. (defun vm-mapc (function &rest lists)
  224.   (let (arglist)
  225.     (while (car lists)
  226.       (setq arglist (mapcar 'car lists))
  227.       (apply function arglist)
  228.       (setq lists (mapcar 'cdr lists)))))
  229.  
  230. (defun vm-delete (predicate list &optional reverse)
  231.   (let ((p list) (reverse (if reverse 'not 'identity)) prev)
  232.     (while p
  233.       (if (funcall reverse (funcall predicate (car p)))
  234.       (if (null prev)
  235.           (setq list (cdr list) p list)
  236.         (setcdr prev (cdr p))
  237.         (setq p (cdr p)))
  238.     (setq prev p p (cdr p))))
  239.     list ))
  240.  
  241. (defun vm-delete-duplicates (list &optional all hack-addresses)
  242.   "Delete duplicate equivalent strings from the list.
  243. If ALL is t, then if there is more than one occurrence of a string in the list,
  244.  then all occurrences of it are removed instead of just the subsequent ones.
  245. If HACK-ADDRESSES is t, then the strings are considered to be mail addresses,
  246.  and only the address part is compared (so that \"Name <foo>\" and \"foo\"
  247.  would be considered to be equivalent.)"
  248.   (let ((hashtable vm-delete-duplicates-obarray)
  249.     (new-list nil)
  250.     sym-string sym)
  251.     (fillarray hashtable 0)
  252.     (while list
  253.       (setq sym-string
  254.         (if hack-addresses
  255.         (nth 1 (funcall vm-chop-full-name-function (car list)))
  256.           (car list))
  257.         sym (intern sym-string hashtable))
  258.       (if (boundp sym)
  259.       (and all (setcar (symbol-value sym) nil))
  260.     (setq new-list (cons (car list) new-list))
  261.     (set sym new-list))
  262.       (setq list (cdr list)))
  263.     (delq nil (nreverse new-list))))
  264.  
  265. (defun vm-member-0 (thing list)
  266.   (catch 'done
  267.     (while list
  268.       (and (equal (car list) thing)
  269.        (throw 'done list))
  270.       (setq list (cdr list)))
  271.     nil ))
  272.  
  273. (fset 'vm-member (symbol-function (if (fboundp 'member) 'member 'vm-member-0)))
  274.  
  275. (defun vm-delqual (ob list)
  276.   (let ((prev nil)
  277.     (curr list))
  278.     (while curr
  279.       (if (not (equal ob (car curr)))
  280.       (setq prev curr
  281.         curr (cdr curr))
  282.     (if (null prev)
  283.         (setq list (cdr list)
  284.           curr list)
  285.       (setq curr (cdr curr))
  286.       (setcdr prev curr))))
  287.     list ))
  288.  
  289. (defun vm-copy-local-variables (buffer &rest variables)
  290.   (let ((values (mapcar 'symbol-value variables)))
  291.     (save-excursion
  292.       (set-buffer buffer)
  293.       (vm-mapc 'set variables values))))
  294.  
  295. (put 'folder-empty 'error-conditions '(folder-empty error))
  296. (put 'folder-empty 'error-message "Folder is empty")
  297. (put 'unrecognized-folder-type 'error-conditions
  298.      '(unrecognized-folder-type error))
  299. (put 'unrecognized-folder-type 'error-message "Unrecognized folder type")
  300.  
  301. (defun vm-error-if-folder-empty ()
  302.   (while (null vm-message-list)
  303.     (if vm-folder-type
  304.     (signal 'unrecognized-folder-type nil)
  305.       (signal 'folder-empty nil))))
  306.  
  307. (defun vm-copy (object)
  308.   (cond ((consp object)
  309.      (let (return-value cons)
  310.        (setq return-value (cons (vm-copy (car object)) nil)
  311.          cons return-value
  312.          object (cdr object))
  313.        (while (consp object)
  314.          (setcdr cons (cons (vm-copy (car object)) nil))
  315.          (setq cons (cdr cons)
  316.            object (cdr object)))
  317.        (setcdr cons object)
  318.        return-value ))
  319.     ((vectorp object) (apply 'vector (mapcar 'vm-copy object)))
  320.     ((stringp object) (copy-sequence object))
  321.     (t object)))
  322.  
  323. (defun vm-xemacs-p ()
  324.   (let ((case-fold-search nil))
  325.     (string-match "XEmacs" emacs-version)))
  326.  
  327. (defun vm-fsfemacs-19-p ()
  328.   (and (string-match "^19" emacs-version)
  329.        (not (string-match "XEmacs\\|Lucid" emacs-version))))
  330.  
  331. ;; make-frame might be defined and still not work.  This would
  332. ;; be true since the user could be running on a tty and using
  333. ;; XEmacs 19.12, or using FSF Emacs 19.28 (or prior FSF Emacs versions).
  334. ;;
  335. ;; make-frame works on ttys in FSF Emacs 19.29, but other than
  336. ;; looking at the version number I don't know a sane way to
  337. ;; test for it without just running make-frame.  I'll just
  338. ;; let it not work for now... someone will complain eventually
  339. ;; and I'll think of something.
  340. (defun vm-multiple-frames-possible-p ()
  341.   (or (and (boundp 'window-system) (not (eq window-system nil)))
  342.       (and (fboundp 'device-type) (eq (device-type) 'x))))
  343.  
  344. (defun vm-mouse-support-possible-p ()
  345.   (vm-multiple-frames-possible-p))
  346.  
  347. (defun vm-menu-support-possible-p ()
  348.   (vm-multiple-frames-possible-p))
  349.  
  350. (defun vm-toolbar-support-possible-p ()
  351.   (and (vm-xemacs-p)
  352.        (vm-multiple-frames-possible-p)
  353.        (featurep 'toolbar)))
  354.  
  355. (defun vm-run-message-hook (message &optional hook-variable)
  356.   (save-excursion
  357.     (set-buffer (vm-buffer-of message))
  358.     (vm-save-restriction
  359.       (widen)
  360.       (save-excursion
  361.     (narrow-to-region (vm-headers-of message) (vm-text-end-of message))
  362.     (run-hooks hook-variable)))))
  363.  
  364. (defun vm-error-free-call (function &rest args)
  365.   (condition-case nil
  366.       (apply function args)
  367.     (error nil)))
  368.  
  369. (put 'beginning-of-folder 'error-conditions '(beginning-of-folder error))
  370. (put 'beginning-of-folder 'error-message "Beginning of folder")
  371. (put 'end-of-folder 'error-conditions '(end-of-folder error))
  372. (put 'end-of-folder 'error-message "End of folder")
  373.  
  374. (defun vm-trace (&rest args)
  375.   (save-excursion
  376.     (set-buffer (get-buffer-create "*vm-trace*"))
  377.     (apply 'insert args)))
  378.  
  379. (defun vm-timezone-make-date-sortable (string)
  380.   (or (cdr (assq string vm-sortable-date-alist))
  381.       (let ((vect (vm-parse-date string))
  382.         (date (vm-parse (current-time-string) " *\\([^ ]+\\)")))
  383.     ;; if specified date is incomplete fill in the holes
  384.     ;; with useful information, defaulting to the current
  385.     ;; date and timezone for everything except hh:mm:ss which
  386.     ;; defaults to midnight.
  387.     (if (equal (aref vect 1) "")
  388.         (aset vect 1 (nth 2 date)))
  389.     (if (equal (aref vect 2) "")
  390.         (aset vect 2 (nth 1 date)))
  391.     (if (equal (aref vect 3) "")
  392.         (aset vect 3 (nth 4 date)))
  393.     (if (equal (aref vect 4) "")
  394.         (aset vect 4 "00:00:00"))
  395.     (if (equal (aref vect 5) "")
  396.         (aset vect 5 (vm-current-time-zone)))
  397.     ;; save this work so we won't have to do it again
  398.     (setq vm-sortable-date-alist
  399.           (cons (cons string
  400.               (timezone-make-date-sortable
  401.                (format "%s %s %s %s %s"
  402.                    (aref vect 1)
  403.                    (aref vect 2)
  404.                    (aref vect 3)
  405.                    (aref vect 4)
  406.                    (aref vect 5))))
  407.             vm-sortable-date-alist))
  408.     ;; return result
  409.     (cdr (car vm-sortable-date-alist)))))
  410.  
  411. (defun vm-current-time-zone ()
  412.   (or (condition-case nil
  413.       (let* ((zone (car (current-time-zone)))
  414.          (absmin (/ (vm-abs zone) 60)))
  415.         (format "%c%02d%02d" (if (< zone 0) ?- ?+)
  416.             (/ absmin 60) (% absmin 60)))
  417.     (error nil))
  418.       (let ((temp-buffer nil))
  419.     (condition-case nil
  420.         (unwind-protect
  421.         (save-excursion
  422.           (setq temp-buffer (generate-new-buffer "*vm-work*"))
  423.           (set-buffer temp-buffer)
  424.           (call-process "date" nil temp-buffer nil)
  425.           (nth 4 (vm-parse (vm-buffer-string-no-properties)
  426.                    " *\\([^ ]+\\)")))
  427.           (and temp-buffer (kill-buffer temp-buffer)))
  428.       (error nil)))
  429.       ""))
  430.  
  431. (defun vm-should-generate-summary ()
  432.   (cond ((eq vm-startup-with-summary t) t)
  433.     ((integerp vm-startup-with-summary)
  434.      (let ((n vm-startup-with-summary))
  435.        (cond ((< n 0) (null (nth (vm-abs n) vm-message-list)))
  436.          (t (nth (1- n) vm-message-list)))))
  437.     (vm-startup-with-summary t)
  438.     (t nil)))
  439.  
  440. (defun vm-find-composition-buffer (&optional not-picky)
  441.   (let ((b-list (buffer-list)) choice alternate)
  442.     (save-excursion
  443.      (while b-list
  444.        (set-buffer (car b-list))
  445.        (if (eq major-mode 'mail-mode)
  446.        (if (buffer-modified-p)
  447.            (setq choice (current-buffer)
  448.              b-list nil)
  449.          (and not-picky (null alternate)
  450.           (setq alternate (current-buffer)))
  451.          (setq b-list (cdr b-list)))
  452.      (setq b-list (cdr b-list))))
  453.     (or choice alternate))))
  454.  
  455. (defun vm-get-file-buffer (file)
  456.   "Like get-file-buffer, but also checks buffers against FILE's truename"
  457.   (or (get-file-buffer file)
  458.       (and (fboundp 'file-truename)
  459.        (get-file-buffer (file-truename file)))))
  460.  
  461. (defun vm-set-region-face (start end face)
  462.   (cond ((fboundp 'make-overlay)
  463.      (let ((o (make-overlay start end)))
  464.        (overlay-put o 'face face)))
  465.     ((fboundp 'make-extent)
  466.      (let ((o (make-extent start end)))
  467.        (set-extent-property o 'face face)))))
  468.  
  469. (defun vm-unsaved-message (&rest args)
  470.   (let ((message-log-max nil))
  471.     (apply (function message) args)))
  472.  
  473. (defun vm-default-buffer-substring-no-properties (beg end &optional buffer)
  474.   (let ((s (if buffer
  475.            (save-excursion
  476.          (set-buffer buffer)
  477.          (buffer-substring beg end))
  478.          (buffer-substring beg end))))
  479.     (set-text-properties 0 (length s) nil s)
  480.     (copy-sequence s)))
  481.  
  482. (fset 'vm-buffer-substring-no-properties
  483.   (cond ((fboundp 'buffer-substring-no-properties)
  484.      (function buffer-substring-no-properties))
  485.     ((vm-xemacs-p)
  486.      (function buffer-substring))
  487.     (t (function vm-default-buffer-substring-no-properties))))
  488.  
  489. (defun vm-buffer-string-no-properties ()
  490.   (vm-buffer-substring-no-properties (point-min) (point-max)))
  491.