From 708af8997ebc69b6b86abc26b05b6ec09dcf77fb Mon Sep 17 00:00:00 2001 From: Mark Walters Date: Mon, 3 Jun 2013 10:42:06 +0100 Subject: [PATCH] [RFC PATCH] emacs: search: allow command line args as part of query --- 08/6d054330fd488a65a656062da3e93f705bd394 | 175 ++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 08/6d054330fd488a65a656062da3e93f705bd394 diff --git a/08/6d054330fd488a65a656062da3e93f705bd394 b/08/6d054330fd488a65a656062da3e93f705bd394 new file mode 100644 index 000000000..148466419 --- /dev/null +++ b/08/6d054330fd488a65a656062da3e93f705bd394 @@ -0,0 +1,175 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 1D632421191 + for ; Mon, 3 Jun 2013 02:42:24 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: 0.201 +X-Spam-Level: +X-Spam-Status: No, score=0.201 tagged_above=-999 required=5 + tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, + FREEMAIL_ENVFROM_END_DIGIT=1, FREEMAIL_FROM=0.001, + RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id vk6-ZVVLGxw3 for ; + Mon, 3 Jun 2013 02:42:13 -0700 (PDT) +Received: from mail-wg0-f41.google.com (mail-wg0-f41.google.com + [74.125.82.41]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client + certificate requested) by olra.theworths.org (Postfix) with ESMTPS id + B04AB421180 for ; Mon, 3 Jun 2013 02:42:13 -0700 + (PDT) +Received: by mail-wg0-f41.google.com with SMTP id k13so2493258wgh.2 + for ; Mon, 03 Jun 2013 02:42:11 -0700 (PDT) +DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; + h=from:to:cc:subject:date:message-id:x-mailer; + bh=fFPkO/0noVmfeJAYHc4MG/29zRFgaNZHr45CbY0/beo=; + b=uoxGFYhzOCxCVqDZvqS9K3fH0KboIhbJuEHGqsQQm6kQkEMtlnTtXpzScRkTVDTgMT + NKgGaFu8lWcXIWSmS+ml+qPeKuoEK9GivzD6OUipttW0xd3Nhc64F5gYY2JdIl79LccY + X/LdkTP3tE6a67E7u/i9Bh5xNyRr/XM5s7EkFeANmLq+2HTxUGxqE6KN1xLW0iNKbqwQ + PeSG1NSTXX4RjuBF7Ox56ZoOEhFMO5LjrLDm909RKES1ECM1ug538XoqBOd0FyC5Q8RN + cL9vnm/53lBK1p8FQ05YR81d0m6p8uPZvMVAxCK4sIzoLPHStJhCbQtmndHBY9sDp/Rs + sF+w== +X-Received: by 10.194.249.231 with SMTP id yx7mr7432993wjc.13.1370252531175; + Mon, 03 Jun 2013 02:42:11 -0700 (PDT) +Received: from localhost (93-97-24-31.zone5.bethere.co.uk. [93.97.24.31]) + by mx.google.com with ESMTPSA id + fu14sm22205396wic.0.2013.06.03.02.42.09 for + (version=TLSv1.2 cipher=RC4-SHA bits=128/128); + Mon, 03 Jun 2013 02:42:10 -0700 (PDT) +From: Mark Walters +To: notmuch@notmuchmail.org, + jani@nikula.org +Subject: [RFC PATCH] emacs: search: allow command line args as part of query +Date: Mon, 3 Jun 2013 10:42:06 +0100 +Message-Id: <1370252526-18003-1-git-send-email-markwalters1009@gmail.com> +X-Mailer: git-send-email 1.7.9.1 +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Mon, 03 Jun 2013 09:42:24 -0000 + +This allows command line arguments for notmuch-search to be part of +the query-string. The string must be of the form +[:blank:]*--cli-arguments -- query. I hope this doesn't clash with +xapian: I believe that queries shouldn't start with a "-". + +Correctly parsed example queries are +--sort=oldest-first -- tag:inbox +--exclude=false -- from:fred + +Some options (currently only sort-order) we parse in emacs, the rest +we just pass to the cli. In light testing it seems to work. + +A full custom parser would be nicer but at least here we are only parsing +the non-query part of a string which is relatively simple: indeed we +already do that in the c code. + +We could just implement the option for sort-order, but I thought for +interface consistency making all the options (sort-order exclude limit +and offset) work was worth the small extra hassle. +--- + +This is an attempt to achieve the same as +id:1349209083-7170-1-git-send-email-jani@nikula.org : that is allow a +saved search to specify the query sort order. However, this is a +rather more complete solution (note as this might be viewed a hack +minimal may be *better* than complete): it works for any search (saved +or from "s") and lets other options (exclude etc) work. What do people +think? + +Best wishes + +Mark + + emacs/notmuch.el | 31 ++++++++++++++++++++++++++----- + 1 files changed, 26 insertions(+), 5 deletions(-) + +diff --git a/emacs/notmuch.el b/emacs/notmuch.el +index 7994d74..26ba7e7 100644 +--- a/emacs/notmuch.el ++++ b/emacs/notmuch.el +@@ -255,6 +255,7 @@ For a mouse binding, return nil." + (notmuch-common-do-stash (notmuch-search-find-thread-id))) + + (defvar notmuch-search-query-string) ++(defvar notmuch-search-full-query) + (defvar notmuch-search-target-thread) + (defvar notmuch-search-target-line) + (defvar notmuch-search-continuation) +@@ -409,6 +410,7 @@ Complete list of currently available key bindings: + (interactive) + (kill-all-local-variables) + (make-local-variable 'notmuch-search-query-string) ++ (make-local-variable 'notmuch-search-full-query) + (make-local-variable 'notmuch-search-oldest-first) + (make-local-variable 'notmuch-search-target-thread) + (make-local-variable 'notmuch-search-target-line) +@@ -896,6 +898,26 @@ PROMPT is the string to prompt with." + (read-from-minibuffer prompt nil keymap nil + 'notmuch-search-history nil nil))))) + ++(defun notmuch-search-parse-query (query oldest-first) ++ (setq notmuch-search-oldest-first oldest-first) ++ (if (string-match "^[:blank:]*--.*? -- " query) ++ (let ((actual-query (substring query (match-end 0))) ++ (args (split-string (match-string 0 query) " " t))) ++ (setq notmuch-search-query-string actual-query) ++ (dolist (arg args nil) ++ (when (equal arg "--sort=oldest-first") ++ (setq notmuch-search-oldest-first t) ++ (message "oldest first found")) ++ (when (equal arg "--sort=newest-first") ++ (setq notmuch-search-oldest-first nil) ++ (message "newest first found"))) ++ (setq args (delete "--sort=oldest-first" args)) ++ (setq args (delete "--sort=newest-first" args)) ++ (setq notmuch-search-full-query (append args (list actual-query)))) ++ ;; no special arguments ++ (setq notmuch-search-full-query (list query)) ++ (setq notmuch-search-query-string query))) ++ + ;;;###autoload + (defun notmuch-search (&optional query oldest-first target-thread target-line continuation) + "Run \"notmuch search\" with the given `query' and display results. +@@ -915,8 +937,7 @@ Other optional parameters are used as follows: + (notmuch-search-mode) + ;; Don't track undo information for this buffer + (set 'buffer-undo-list t) +- (set 'notmuch-search-query-string query) +- (set 'notmuch-search-oldest-first oldest-first) ++ (notmuch-search-parse-query query oldest-first) + (set 'notmuch-search-target-thread target-thread) + (set 'notmuch-search-target-line target-line) + (set 'notmuch-search-continuation continuation) +@@ -928,13 +949,13 @@ Other optional parameters are used as follows: + (erase-buffer) + (goto-char (point-min)) + (save-excursion +- (let ((proc (notmuch-start-notmuch ++ (let ((proc (apply #'notmuch-start-notmuch + "notmuch-search" buffer #'notmuch-search-process-sentinel + "search" "--format=sexp" "--format-version=1" +- (if oldest-first ++ (if notmuch-search-oldest-first + "--sort=oldest-first" + "--sort=newest-first") +- query)) ++ notmuch-search-full-query)) + ;; Use a scratch buffer to accumulate partial output. + ;; This buffer will be killed by the sentinel, which + ;; should be called no matter how the process dies. +-- +1.7.9.1 + -- 2.26.2