1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2025-01-04 11:40:22 +00:00
emacs/lisp/mh-e/mh-pick.el

240 lines
8.3 KiB
EmacsLisp
Raw Normal View History

2002-11-29 18:15:21 +00:00
;;; mh-pick.el --- make a search pattern and search for a message in MH-E
1994-03-15 06:16:30 +00:00
2002-10-01 20:27:23 +00:00
;; Copyright (C) 1993, 1995, 2001 Free Software Foundation, Inc.
;; Author: Bill Wohler <wohler@newt.com>
;; Maintainer: Bill Wohler <wohler@newt.com>
;; Keywords: mail
;; See: mh-e.el
1994-03-15 06:16:30 +00:00
;; This file is part of GNU Emacs.
1994-03-15 06:16:30 +00:00
1995-04-10 00:20:07 +00:00
;; GNU Emacs is free software; you can redistribute it and/or modify
1994-03-15 06:16:30 +00:00
;; 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.
1995-04-10 00:20:07 +00:00
;; GNU Emacs is distributed in the hope that it will be useful,
1994-03-15 06:16:30 +00:00
;; 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
1996-01-14 07:34:30 +00:00
;; 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.
1994-03-15 06:16:30 +00:00
;;; Commentary:
2002-11-29 18:15:21 +00:00
;; Internal support for MH-E package.
1994-03-15 06:16:30 +00:00
1995-04-09 22:31:08 +00:00
;;; Change Log:
;; $Id: mh-pick.el,v 1.10 2003/01/08 23:21:16 wohler Exp $
1995-04-09 22:31:08 +00:00
1994-03-15 06:16:30 +00:00
;;; Code:
(require 'mh-e)
2002-10-01 20:27:23 +00:00
(require 'easymenu)
(require 'gnus-util)
1994-03-15 06:16:30 +00:00
1995-04-09 22:31:08 +00:00
;;; Internal variables:
(defvar mh-pick-mode-map (make-sparse-keymap)
"Keymap for searching folder.")
2003-01-08 23:21:16 +00:00
(defvar mh-searching-folder nil) ;Folder this pick is searching.
1994-03-15 06:16:30 +00:00
2003-01-08 23:21:16 +00:00
;;;###mh-autoload
1994-03-15 06:16:30 +00:00
(defun mh-search-folder (folder)
1995-04-09 22:31:08 +00:00
"Search FOLDER for messages matching a pattern.
2002-11-29 18:15:21 +00:00
This function uses the MH command `pick' to do the work.
1995-04-09 22:31:08 +00:00
Add the messages found to the sequence named `search'."
1994-03-15 06:16:30 +00:00
(interactive (list (mh-prompt-for-folder "Search"
2003-01-08 23:21:16 +00:00
mh-current-folder
t)))
1994-03-15 06:16:30 +00:00
(switch-to-buffer-other-window "pick-pattern")
(if (or (zerop (buffer-size))
2003-01-08 23:21:16 +00:00
(not (y-or-n-p "Reuse pattern? ")))
1994-03-15 06:16:30 +00:00
(mh-make-pick-template)
(message ""))
2002-11-29 18:15:21 +00:00
(setq mh-searching-folder folder)
(message "%s" (substitute-command-keys
2003-01-08 23:21:16 +00:00
(concat "Type \\[mh-do-pick-search] to search messages, "
"\\[mh-help] for help."))))
1994-03-15 06:16:30 +00:00
(defun mh-make-pick-template ()
2002-11-29 18:15:21 +00:00
"Initialize the current buffer with a template for a pick pattern."
1994-03-15 06:16:30 +00:00
(erase-buffer)
(insert "From: \n"
2003-01-08 23:21:16 +00:00
"To: \n"
"Cc: \n"
"Date: \n"
"Subject: \n"
"---------\n")
1994-03-15 06:16:30 +00:00
(mh-pick-mode)
(goto-char (point-min))
(end-of-line))
2002-11-29 18:15:21 +00:00
;;; Menu extracted from mh-menubar.el V1.1 (31 July 2001)
(easy-menu-define
mh-pick-menu mh-pick-mode-map "Menu for MH-E pick-mode"
'("Pick"
["Execute the Search" mh-do-pick-search t]))
;;; Help Messages
;;; Group messages logically, more or less.
(defvar mh-pick-mode-help-messages
'((nil
"Search messages: \\[mh-do-pick-search]\n"
"Move to a field by typing C-c C-f C-<field>\n"
"where <field> is the first letter of the desired field."))
"Key binding cheat sheet.
This is an associative array which is used to show the most common commands.
The key is a prefix char. The value is one or more strings which are
concatenated together and displayed in the minibuffer if ? is pressed after
the prefix character. The special key nil is used to display the
non-prefixed commands.
The substitutions described in `substitute-command-keys' are performed as
well.")
1994-03-15 06:16:30 +00:00
(put 'mh-pick-mode 'mode-class 'special)
2002-10-01 20:27:23 +00:00
(define-derived-mode mh-pick-mode fundamental-mode "MH-Pick"
2002-11-29 18:15:21 +00:00
"Mode for creating search templates in MH-E.\\<mh-pick-mode-map>
2002-10-01 20:27:23 +00:00
1995-04-09 22:31:08 +00:00
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.
1994-03-15 06:16:30 +00:00
When you have finished, type \\[mh-do-pick-search] to do the search.
2002-10-01 20:27:23 +00:00
2002-11-29 18:15:21 +00:00
The value of `mh-pick-mode-hook' is a list of functions to be called,
with no arguments, upon entry to this mode.
2002-10-01 20:27:23 +00:00
\\{mh-pick-mode-map}"
1994-03-15 06:16:30 +00:00
(make-local-variable 'mh-searching-folder)
2002-11-29 18:15:21 +00:00
(easy-menu-add mh-pick-menu)
(make-local-variable 'mh-help-messages)
(setq mh-help-messages mh-pick-mode-help-messages)
(run-hooks 'mh-pick-mode-hook))
1994-03-15 06:16:30 +00:00
2003-01-08 23:21:16 +00:00
;;;###mh-autoload
1994-03-15 06:16:30 +00:00
(defun mh-do-pick-search ()
"Find messages that match the qualifications in the current pattern buffer.
2002-10-01 20:27:23 +00:00
Messages are searched for in the folder named in `mh-searching-folder'.
1995-04-09 22:31:08 +00:00
Add the messages found to the sequence named `search'."
1994-03-15 06:16:30 +00:00
(interactive)
(let ((pattern-buffer (buffer-name))
2003-01-08 23:21:16 +00:00
(searching-buffer mh-searching-folder)
range
msgs
(pattern nil)
(new-buffer nil))
1994-03-15 06:16:30 +00:00
(save-excursion
(cond ((get-buffer searching-buffer)
2003-01-08 23:21:16 +00:00
(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))))
1994-03-15 06:16:30 +00:00
(message "Searching...")
(goto-char (point-min))
1995-04-09 22:31:08 +00:00
(while (and range
2003-01-08 23:21:16 +00:00
(setq pattern (mh-next-pick-field pattern-buffer)))
1994-03-15 06:16:30 +00:00
(setq msgs (mh-seq-from-command searching-buffer
2003-01-08 23:21:16 +00:00
'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
1994-03-15 06:16:30 +00:00
(message "Searching...done")
(if new-buffer
2003-01-08 23:21:16 +00:00
(mh-scan-folder searching-buffer msgs)
(switch-to-buffer searching-buffer))
1995-04-09 22:31:08 +00:00
(mh-add-msgs-to-seq msgs 'search)
(delete-other-windows)))
1994-03-15 06:16:30 +00:00
2002-11-29 18:15:21 +00:00
(defun mh-seq-from-command (folder 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."
1994-03-15 06:16:30 +00:00
(let ((msg)
2003-01-08 23:21:16 +00:00
(msgs ())
(case-fold-search t))
1994-03-15 06:16:30 +00:00
(save-excursion
(save-window-excursion
2003-01-08 23:21:16 +00:00
(if (eq 0 (apply 'mh-exec-cmd-quiet nil command))
;; "pick" outputs one number per line
(while (setq msg (car (mh-read-msg-list)))
(setq msgs (cons msg msgs))
(forward-line 1))))
1994-03-15 06:16:30 +00:00
(set-buffer folder)
2003-01-08 23:21:16 +00:00
(setq msgs (nreverse msgs)) ;put in ascending order
1994-03-15 06:16:30 +00:00
msgs)))
(defun mh-next-pick-field (buffer)
2002-11-29 18:15:21 +00:00
"Return the next piece of a pick argument extracted from BUFFER.
Return a list like (\"--fieldname\" \"pattern\") or (\"-search\" \"bodypat\")
or nil if no pieces remain."
1994-03-15 06:16:30 +00:00
(set-buffer buffer)
(let ((case-fold-search t))
(cond ((eobp)
2003-01-08 23:21:16 +00:00
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))))
1994-03-15 06:16:30 +00:00
2002-11-29 18:15:21 +00:00
1994-03-15 06:16:30 +00:00
;;; Build the pick-mode keymap:
2002-11-29 18:15:21 +00:00
;;; If this changes, modify mh-pick-mode-help-messages accordingly, above.
2002-10-01 20:27:23 +00:00
(gnus-define-keys mh-pick-mode-map
2003-01-08 23:21:16 +00:00
"\C-c?" mh-help
"\C-c\C-c" mh-do-pick-search
"\C-c\C-f\C-b" mh-to-field
"\C-c\C-f\C-c" mh-to-field
"\C-c\C-f\C-d" mh-to-field
"\C-c\C-f\C-f" mh-to-field
"\C-c\C-f\C-r" mh-to-field
"\C-c\C-f\C-s" mh-to-field
"\C-c\C-f\C-t" mh-to-field
"\C-c\C-fb" mh-to-field
"\C-c\C-fc" mh-to-field
"\C-c\C-fd" mh-to-field
"\C-c\C-ff" mh-to-field
"\C-c\C-fr" mh-to-field
"\C-c\C-fs" mh-to-field
"\C-c\C-ft" mh-to-field)
2002-10-01 20:27:23 +00:00
2002-11-29 18:15:21 +00:00
(provide 'mh-pick)
;;; Local Variables:
2003-01-08 23:21:16 +00:00
;;; indent-tabs-mode: nil
2002-11-29 18:15:21 +00:00
;;; sentence-end-double-space: nil
;;; End:
;;; mh-pick.el ends here