view lisp/mail/mh-pick.el @ 47641:172cf7391545

(calc-bug-address, calc-scan-for-dels, calc-stack) (calc-stack-top, calc-always-load-extensions) (calc-line-numbering, calc-line-breaking, calc-display-just) (calc-display-origin, calc-number-radix, calc-leading-zeros) (calc-group-digits, calc-group-char, calc-point-char) (calc-frac-format, calc-prefer-frac, calc-hms-format) (calc-date-format, calc-float-format, calc-complex-format) (calc-full-float-format, calc-complex-mode, calc-infinite-mode) (calc-display-strings, calc-matrix-just, calc-break-vectors) (calc-full-vectors, calc-full-trail-vectors, calc-vector-commas) (calc-vector-brackets, calc-matrix-brackets, calc-language) (calc-language-option, calc-function-open, calc-function-open) (calc-function-close, calc-language-output-filter) (calc-language-input-filter, calc-radix-formatter) (calc-left-label, calc-right-label, calc-word-size) (calc-previous-modulo, calc-simplify-mode, calc-auto-recompute) (calc-display-raw, calc-internal-prec, calc-inverse-flag) (calc-hyperbolic-flag, calc-keep-args-flag, calc-angle-mode) (calc-algebraic-mode, calc-incomplete-algebraic-mode) (calc-symbolic-mode, calc-matrix-mode, calc-shift-prefix) (calc-window-height, calc-display-trail, calc-show-selections) (calc-use-selections, calc-assoc-selections) (calc-display-working-message, calc-auto-why, calc-timing) (calc-display-sci-high, calc-display-sci-low, calc-other-modes) (calc-other-modes, calc-Y-help-msgs, calc-loaded-settings-file): Make into real defvars. (calc-mode-var-list): Delete. (calc-mode-save-mode, calc-standard-date-formats) (calc-autorange-units, calc-was-keypad-mode, calc-full-mode) (calc-user-parse-tables, calc-gnuplot-default-device) (calc-gnuplot-default-output, calc-gnuplot-print-device) (calc-gnuplot-print-output, calc-gnuplot-geometry) (calc-graph-default-resolution, calc-graph-default-resolution-3d) (calc-invocation-macro, calc-show-banner): Make into defvars, taken from `calc-mode-var-list'. (calc-emacs-type-epoch, calc-emacs-type-19) (calc-emacs-type-lucid, calc-emacs-type-gnu19): Make into defvars. (calc-version, calc-version-date, calc-trail-pointer) (calc-trail-overlay, calc-undo-list, calc-redo-list) (calc-main-buffer, calc-trail-buffer, calc-why, calc-next-why) (calc-inverse-flag, calc-hyperbolic-flag, calc-keep-args-flag) (calc-last-kill, calc-previous-alg-entry, calc-dollar-values) (calc-dollar-used, calc-hashes-used, calc-quick-prev-results) (calc-said-hello, calc-executing-macro, calc-any-selections) (calc-help-phase, calc-full-help-flag, calc-refresh-count) (calc-display-dirty, calc-prepared-composition) (calc-selection-cache-default-entry, calc-embedded-info) (calc-embedded-active, calc-standalone-flag, var-EvalRules) (math-eval-rules-cache-tag, math-radix-explicit-format) (math-expr-function-mapping, math-expr-variable-mapping) (math-read-expr-quotes, math-working-step, math-working-step-2) (var-i, var-pi, var-e, var-phi, var-gamma, var-Modes): Make into defvars, from toplevel setq. (calc-mode-map): Set up keymap in more modern fashion. (calc-dispatch-map): Ditto. (calc-command-flags, calc-final-point-line) (calc-final-point-column): Defvar. (calc-do): Use `save-current-buffer' instead of `save-excursion'. (sel-mode): Defvar. (calc-any-evaltos): Ditto. (calc-buffer, calc-prev-char, calc-prev-prev-char) (calc-digit-value): Ditto. (math-eval-rules-cache, math-eval-rules-cache-other): Ditto. (math-sub-bignum): Bind `diff'. (calc-selection-cache-entry): Defvar. (calc-count-lines): Reference `pos' instead of `newpos'.
author Colin Walters <walters@gnu.org>
date Fri, 27 Sep 2002 04:55:03 +0000
parents 0f4506820432
children 2568d5a27317
line wrap: on
line source

;;; mh-pick.el --- make a search pattern and search for a message in mh-e
;; Time-stamp: <2001-12-20 18:55:31 pavel>

;; Copyright (C) 1993, 1995 Free Software Foundation, Inc.

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;; Internal support for mh-e package.

;;; Change Log:

;; $Id: mh-pick.el,v 1.6 2001/07/15 19:53:53 pj Exp $

;;; Code:

(provide 'mh-pick)
(require 'mh-e)

(defvar mh-pick-mode-hook nil
  "Invoked in `mh-pick-mode' on a new pattern.")

;;; Internal variables:

(defvar mh-pick-mode-map (make-sparse-keymap)
  "Keymap for searching folder.")

(defvar mh-searching-folder nil)	;Folder this pick is searching.

(defun mh-search-folder (folder)
  "Search FOLDER for messages matching a pattern.
Add the messages found to the sequence named `search'."
  (interactive (list (mh-prompt-for-folder "Search"
					   mh-current-folder
					   t)))
  (switch-to-buffer-other-window "pick-pattern")
  (if (or (zerop (buffer-size))
	  (not (y-or-n-p "Reuse pattern? ")))
      (mh-make-pick-template)
    (message ""))
  (setq mh-searching-folder folder))

(defun mh-make-pick-template ()
  ;; Initialize the current buffer with a template for a pick pattern.
  (erase-buffer)
  (insert "From: \n"
	  "To: \n"
	  "Cc: \n"
	  "Date: \n"
	  "Subject: \n"
	  "---------\n")
  (mh-pick-mode)
  (goto-char (point-min))
  (end-of-line))

(put 'mh-pick-mode 'mode-class 'special)

(defun mh-pick-mode ()
  "Mode for creating search templates in mh-e.\\<mh-pick-mode-map>
After each field name, enter the pattern to search for.  If a field's
value does not matter for the search, leave it empty.  To search the
entire message, supply the pattern in the \"body\" of the template.
Each non-empty field must be matched for a message to be selected.
To effect a logical \"or\", use \\[mh-search-folder] multiple times.
When you have finished, type  \\[mh-do-pick-search]  to do the search.
\\{mh-pick-mode-map}
Turning on mh-pick-mode calls the value of the variable mh-pick-mode-hook
if that value is non-nil."
  (interactive)
  (kill-all-local-variables)
  (make-local-variable 'mh-searching-folder)
  (use-local-map mh-pick-mode-map)
  (setq major-mode 'mh-pick-mode)
  (mh-set-mode-name "MH-Pick")
  (run-hooks 'mh-pick-mode-hook))


(defun mh-do-pick-search ()
  "Find messages that match the qualifications in the current pattern buffer.
Messages are searched for in the folder named in mh-searching-folder.
Add the messages found to the sequence named `search'."
  (interactive)
  (let ((pattern-buffer (buffer-name))
	(searching-buffer mh-searching-folder)
	range
	msgs
	(finding-messages t)
	(pattern nil)
	(new-buffer nil))
    (save-excursion
      (cond ((get-buffer searching-buffer)
	     (set-buffer searching-buffer)
	     (setq range (list (format "%d-%d"
				       mh-first-msg-num mh-last-msg-num))))
	    (t
	     (mh-make-folder searching-buffer)
	     (setq range '("all"))
	     (setq new-buffer t))))
    (message "Searching...")
    (goto-char (point-min))
    (while (and range
		(setq pattern (mh-next-pick-field pattern-buffer)))
      (setq msgs (mh-seq-from-command searching-buffer
				      'search
				      (mh-list-to-string
				       (list "pick" pattern searching-buffer
					     "-list"
					     (mh-coalesce-msg-list range)))))
      (setq range msgs))		;restrict the pick range for next pass
    (message "Searching...done")
    (if new-buffer
	(mh-scan-folder searching-buffer msgs)
	(switch-to-buffer searching-buffer))
    (mh-add-msgs-to-seq msgs 'search)
    (delete-other-windows)))


(defun mh-seq-from-command (folder seq seq-command)
  ;; In FOLDER, make a sequence named SEQ by executing COMMAND.
  ;; COMMAND is a list.  The first element is a program name
  ;; and the subsequent elements are its arguments, all strings.
  (let ((msg)
	(msgs ())
	(case-fold-search t))
    (save-excursion
      (save-window-excursion
	(if (eq 0 (apply 'mh-exec-cmd-quiet nil seq-command))
	    ;; "pick" outputs one number per line
	    (while (setq msg (car (mh-read-msg-list)))
	      (setq msgs (cons msg msgs))
	      (forward-line 1))))
      (set-buffer folder)
      (setq msgs (nreverse msgs))	;put in ascending order
      msgs)))


(defun mh-next-pick-field (buffer)
  ;; Return the next piece of a pick argument that can be extracted from the
  ;; BUFFER.
  ;; Return a list like ("--fieldname" "pattern") or ("-search" "bodypat")
  ;; or nil if no pieces remain.
  (set-buffer buffer)
  (let ((case-fold-search t))
    (cond ((eobp)
	   nil)
	  ((re-search-forward "^\\([a-z][^: \t\n]*\\):[ \t]*\\([a-z0-9].*\\)$" nil t)
	   (let* ((component
		   (format "--%s"
			   (downcase (buffer-substring (match-beginning 1)
						       (match-end 1)))))
		  (pat (buffer-substring (match-beginning 2) (match-end 2))))
	       (forward-line 1)
	       (list component pat)))
	  ((re-search-forward "^-*$" nil t)
	   (forward-char 1)
	   (let ((body (buffer-substring (point) (point-max))))
	     (if (and (> (length body) 0) (not (equal body "\n")))
		 (list "-search" body)
		 nil)))
	  (t
	   nil))))

;;; Build the pick-mode keymap:

(define-key mh-pick-mode-map "\C-c\C-c" 'mh-do-pick-search)
(define-key mh-pick-mode-map "\C-c\C-f\C-b" 'mh-to-field)
(define-key mh-pick-mode-map "\C-c\C-f\C-c" 'mh-to-field)
(define-key mh-pick-mode-map "\C-c\C-f\C-d" 'mh-to-field)
(define-key mh-pick-mode-map "\C-c\C-f\C-f" 'mh-to-field)
(define-key mh-pick-mode-map "\C-c\C-f\C-r" 'mh-to-field)
(define-key mh-pick-mode-map "\C-c\C-f\C-s" 'mh-to-field)
(define-key mh-pick-mode-map "\C-c\C-f\C-t" 'mh-to-field)
(define-key mh-pick-mode-map "\C-c\C-fb" 'mh-to-field)
(define-key mh-pick-mode-map "\C-c\C-fc" 'mh-to-field)
(define-key mh-pick-mode-map "\C-c\C-fd" 'mh-to-field)
(define-key mh-pick-mode-map "\C-c\C-ff" 'mh-to-field)
(define-key mh-pick-mode-map "\C-c\C-fr" 'mh-to-field)
(define-key mh-pick-mode-map "\C-c\C-fs" 'mh-to-field)
(define-key mh-pick-mode-map "\C-c\C-ft" 'mh-to-field)

;;; mh-pick.el ends here