]> git.notmuchmail.org Git - notmuch/blobdiff - emacs/notmuch.el
emacs: Use --format-version for search, show, and reply
[notmuch] / emacs / notmuch.el
index 60e8bf957ca45da525e78b3e485ed8d2ace3fba9..63387a2dfe28c85fce445dc0a1f74d63498f3d0f 100644 (file)
@@ -536,19 +536,13 @@ If BARE is set then do not prefix with \"thread:\""
 (defun notmuch-call-notmuch-process (&rest args)
   "Synchronously invoke \"notmuch\" with the given list of arguments.
 
-Output from the process will be presented to the user as an error
-and will also appear in a buffer named \"*Notmuch errors*\"."
-  (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
-    (with-current-buffer error-buffer
-       (erase-buffer))
-    (if (eq (apply 'call-process notmuch-command nil error-buffer nil args) 0)
-       (point)
-      (progn
-       (with-current-buffer error-buffer
-         (let ((beg (point-min))
-               (end (- (point-max) 1)))
-           (error (buffer-substring beg end))
-           ))))))
+If notmuch exits with a non-zero status, output from the process
+will appear in a buffer named \"*Notmuch errors*\" and an error
+will be signaled."
+  (with-temp-buffer
+    (let ((status (apply #'call-process notmuch-command nil t nil args)))
+      (notmuch-check-exit-status status (cons notmuch-command args)
+                                (buffer-string)))))
 
 (defun notmuch-search-set-tags (tags &optional pos)
   (let ((new-result (plist-put (notmuch-search-get-result pos) :tags tags)))
@@ -644,6 +638,7 @@ of the result."
        (exit-status (process-exit-status proc))
        (never-found-target-thread nil))
     (when (memq status '(exit signal))
+      (catch 'return
        (kill-buffer (process-get proc 'parse-buf))
        (if (buffer-live-p buffer)
            (with-current-buffer buffer
@@ -654,17 +649,23 @@ of the result."
                  (if (eq status 'signal)
                      (insert "Incomplete search results (search process was killed).\n"))
                  (when (eq status 'exit)
-                   (insert "End of search results.")
-                   (unless (= exit-status 0)
-                     (insert (format " (process returned %d)" exit-status)))
-                   (insert "\n")
+                   (insert "End of search results.\n")
+                   ;; For version mismatch, there's no point in
+                   ;; showing the search buffer
+                   (when (or (= exit-status 20) (= exit-status 21))
+                     (kill-buffer))
+                   (condition-case nil
+                       (notmuch-check-async-exit-status proc msg)
+                     ;; Suppress the error signal since strange
+                     ;; things happen if a sentinel signals.
+                     (error (throw 'return nil)))
                    (if (and atbob
                             (not (string= notmuch-search-target-thread "found")))
                        (set 'never-found-target-thread t)))))
              (when (and never-found-target-thread
                       notmuch-search-target-line)
                  (goto-char (point-min))
-                 (forward-line (1- notmuch-search-target-line))))))))
+                 (forward-line (1- notmuch-search-target-line)))))))))
 
 (defcustom notmuch-search-line-faces '(("unread" :weight bold)
                                       ("flagged" :foreground "blue"))
@@ -820,12 +821,6 @@ non-authors is found, assume that all of the authors match."
     (insert (apply #'format string objects))
     (insert "\n")))
 
-(defvar notmuch-search-process-state nil
-  "Parsing state of the search process filter.")
-
-(defvar notmuch-search-json-parser nil
-  "Incremental JSON parser for the search process filter.")
-
 (defun notmuch-search-process-filter (proc string)
   "Process and filter the output of \"notmuch search\""
   (let ((results-buf (process-buffer proc))
@@ -838,41 +833,10 @@ non-authors is found, assume that all of the authors match."
        ;; Insert new data
        (save-excursion
          (goto-char (point-max))
-         (insert string)))
-      (with-current-buffer results-buf
-       (while (not done)
-         (condition-case nil
-             (case notmuch-search-process-state
-               ((begin)
-                ;; Enter the results list
-                (if (eq (notmuch-json-begin-compound
-                         notmuch-search-json-parser) 'retry)
-                    (setq done t)
-                  (setq notmuch-search-process-state 'result)))
-               ((result)
-                ;; Parse a result
-                (let ((result (notmuch-json-read notmuch-search-json-parser)))
-                  (case result
-                    ((retry) (setq done t))
-                    ((end) (setq notmuch-search-process-state 'end))
-                    (otherwise (notmuch-search-show-result result)))))
-               ((end)
-                ;; Any trailing data is unexpected
-                (notmuch-json-eof notmuch-search-json-parser)
-                (setq done t)))
-           (json-error
-            ;; Do our best to resynchronize and ensure forward
-            ;; progress
-            (notmuch-search-show-error
-             "%s"
-             (with-current-buffer parse-buf
-               (let ((bad (buffer-substring (line-beginning-position)
-                                            (line-end-position))))
-                 (forward-line)
-                 bad))))))
-       ;; Clear out what we've parsed
-       (with-current-buffer parse-buf
-         (delete-region (point-min) (point)))))))
+         (insert string))
+       (notmuch-json-parse-partial-list 'notmuch-search-show-result
+                                        'notmuch-search-show-error
+                                        results-buf)))))
 
 (defun notmuch-search-tag-all (&optional tag-changes)
   "Add/remove tags from all messages in current search buffer.
@@ -975,7 +939,7 @@ Other optional parameters are used as follows:
        (let ((proc (start-process
                     "notmuch-search" buffer
                     notmuch-command "search"
-                    "--format=json"
+                    "--format=json" "--format-version=1"
                     (if oldest-first
                         "--sort=oldest-first"
                       "--sort=newest-first")
@@ -984,9 +948,6 @@ Other optional parameters are used as follows:
              ;; This buffer will be killed by the sentinel, which
              ;; should be called no matter how the process dies.
              (parse-buf (generate-new-buffer " *notmuch search parse*")))
-         (set (make-local-variable 'notmuch-search-process-state) 'begin)
-         (set (make-local-variable 'notmuch-search-json-parser)
-              (notmuch-json-create-parser parse-buf))
          (process-put proc 'parse-buf parse-buf)
          (set-process-sentinel proc 'notmuch-search-process-sentinel)
          (set-process-filter proc 'notmuch-search-process-filter)