[PATCH 07/23] emacs: Use 'and' instead of 'when' when the return value matters

Subject: [PATCH 07/23] emacs: Use 'and' instead of 'when' when the return value matters

Date: Sun, 26 Jul 2020 18:58:02 +0200

To: notmuch@notmuchmail.org

Cc:

From: Jonas Bernoulli


Also do so for some 'if' forms that lack an ELSE part.
Even go as far as using 'and' and 'not' instead of 'unless'.
---
 emacs/coolj.el               | 12 +++---
 emacs/notmuch-address.el     | 72 ++++++++++++++++++------------------
 emacs/notmuch-crypto.el      |  2 +-
 emacs/notmuch-draft.el       |  2 +-
 emacs/notmuch-lib.el         | 37 +++++++++---------
 emacs/notmuch-maildir-fcc.el |  4 +-
 emacs/notmuch-mua.el         | 13 +++----
 emacs/notmuch-show.el        | 45 +++++++++++-----------
 emacs/notmuch-tree.el        |  4 +-
 emacs/notmuch-wash.el        |  8 ++--
 emacs/notmuch.el             | 28 ++++++++------
 11 files changed, 114 insertions(+), 113 deletions(-)

diff --git a/emacs/coolj.el b/emacs/coolj.el
index 961db606..39a8de2b 100644
--- a/emacs/coolj.el
+++ b/emacs/coolj.el
@@ -107,12 +107,12 @@ (defun coolj-set-breakpoint (prefix)
 If the line should not be broken, return nil; point remains on the
 line."
   (move-to-column fill-column)
-  (if (and (re-search-forward "[^ ]" (line-end-position) 1)
-	   (> (current-column) fill-column))
-      ;; This line is too long.  Can we break it?
-      (or (coolj-find-break-backward prefix)
-	  (progn (move-to-column fill-column)
-		 (coolj-find-break-forward)))))
+  (and (re-search-forward "[^ ]" (line-end-position) 1)
+       (> (current-column) fill-column)
+       ;; This line is too long.  Can we break it?
+       (or (coolj-find-break-backward prefix)
+	   (progn (move-to-column fill-column)
+		  (coolj-find-break-forward)))))
 
 (defun coolj-find-break-backward (prefix)
   "Move point backward to the first available breakpoint and return t.
diff --git a/emacs/notmuch-address.el b/emacs/notmuch-address.el
index 4db7096c..2dd08661 100644
--- a/emacs/notmuch-address.el
+++ b/emacs/notmuch-address.el
@@ -252,20 +252,20 @@ (defun notmuch-address-expand-name ()
 (defun notmuch-address-locate-command (command)
   "Return non-nil if `command' is an executable either on
 `exec-path' or an absolute pathname."
-  (when (stringp command)
-    (if (and (file-name-absolute-p command)
-	     (file-executable-p command))
-	command
-      (setq command (file-name-nondirectory command))
-      (catch 'found-command
-	(let (bin)
-	  (dolist (dir exec-path)
-	    (setq bin (expand-file-name command dir))
-	    (when (or (and (file-executable-p bin)
-			   (not (file-directory-p bin)))
-		      (and (file-executable-p (setq bin (concat bin ".exe")))
-			   (not (file-directory-p bin))))
-	      (throw 'found-command bin))))))))
+  (and (stringp command)
+       (if (and (file-name-absolute-p command)
+		(file-executable-p command))
+	   command
+	 (setq command (file-name-nondirectory command))
+	 (catch 'found-command
+	   (let (bin)
+	     (dolist (dir exec-path)
+	       (setq bin (expand-file-name command dir))
+	       (when (or (and (file-executable-p bin)
+			      (not (file-directory-p bin)))
+			 (and (file-executable-p (setq bin (concat bin ".exe")))
+			      (not (file-directory-p bin))))
+		 (throw 'found-command bin))))))))
 
 (defun notmuch-address-harvest-addr (result)
   (let ((name-addr (plist-get result :name-addr)))
@@ -304,18 +304,20 @@ (defun notmuch-address-harvest (&optional addr-prefix synchronous callback)
 execution, CALLBACK is called when harvesting finishes."
   (let* ((sent (eq (car notmuch-address-internal-completion) 'sent))
 	 (config-query (cadr notmuch-address-internal-completion))
-	 (prefix-query (when addr-prefix
-			 (format "%s:%s*" (if sent "to" "from") addr-prefix)))
+	 (prefix-query (and addr-prefix
+			    (format "%s:%s*"
+				    (if sent "to" "from")
+				    addr-prefix)))
 	 (from-or-to-me-query
 	  (mapconcat (lambda (x)
 		       (concat (if sent "from:" "to:") x))
 		     (notmuch-user-emails) " or "))
 	 (query (if (or prefix-query config-query)
 		    (concat (format "(%s)" from-or-to-me-query)
-			    (when prefix-query
-			      (format " and (%s)" prefix-query))
-			    (when config-query
-			      (format " and (%s)" config-query)))
+			    (and prefix-query
+				 (format " and (%s)" prefix-query))
+			    (and config-query
+				 (format " and (%s)" config-query)))
 		  from-or-to-me-query))
 	 (args `("address" "--format=sexp" "--format-version=4"
 		 ,(if sent "--output=recipients" "--output=sender")
@@ -354,21 +356,21 @@ (defun notmuch-address--get-address-hash ()
 
 Returns nil if the save file does not exist, or it does not seem
 to be a saved address hash."
-  (when notmuch-address-save-filename
-    (condition-case nil
-	(with-temp-buffer
-	  (insert-file-contents notmuch-address-save-filename)
-	  (let ((name (read (current-buffer)))
-		(plist (read (current-buffer))))
-	    ;; We do two simple sanity checks on the loaded file. We just
-	    ;; check a version is specified, not that it is the current
-	    ;; version, as we are allowed to over-write and a save-file with
-	    ;; an older version.
-	    (when (and (string= name "notmuch-address-hash")
-		       (plist-get plist :version))
-	      plist)))
-      ;; The error case catches any of the reads failing.
-      (error nil))))
+  (and notmuch-address-save-filename
+       (condition-case nil
+	   (with-temp-buffer
+	     (insert-file-contents notmuch-address-save-filename)
+	     (let ((name (read (current-buffer)))
+		   (plist (read (current-buffer))))
+	       ;; We do two simple sanity checks on the loaded file.
+	       ;; We just check a version is specified, not that
+	       ;; it is the current version, as we are allowed to
+	       ;; over-write and a save-file with an older version.
+	       (and (string= name "notmuch-address-hash")
+		    (plist-get plist :version)
+		    plist)))
+	 ;; The error case catches any of the reads failing.
+	 (error nil))))
 
 (defun notmuch-address--load-address-hash ()
   "Read the saved address hash and set the corresponding variables."
diff --git a/emacs/notmuch-crypto.el b/emacs/notmuch-crypto.el
index 420b008f..e6bf8339 100644
--- a/emacs/notmuch-crypto.el
+++ b/emacs/notmuch-crypto.el
@@ -253,7 +253,7 @@ (defun notmuch-crypto-insert-encstatus-button (encstatus)
 	       "Decryption error")
 	      (t
 	       (concat "Unknown encryption status"
-		       (if status (concat ": " status))))))
+		       (and status (concat ": " status))))))
 	   " ]")
    :type 'notmuch-crypto-status-button-type
    'face 'notmuch-crypto-decryption
diff --git a/emacs/notmuch-draft.el b/emacs/notmuch-draft.el
index ea995379..759e6c9e 100644
--- a/emacs/notmuch-draft.el
+++ b/emacs/notmuch-draft.el
@@ -263,7 +263,7 @@ (defun notmuch-draft-resume (id)
       ;; If the resumed message was a draft then set the draft
       ;; message-id so that we can delete the current saved draft if the
       ;; message is resaved or sent.
-      (setq notmuch-draft-id (when draft id)))))
+      (setq notmuch-draft-id (and draft id)))))
 
 
 (add-hook 'message-send-hook 'notmuch-draft--mark-deleted)
diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el
index 5d0c373a..9b02cd66 100644
--- a/emacs/notmuch-lib.el
+++ b/emacs/notmuch-lib.el
@@ -608,7 +608,7 @@ (defun notmuch--get-bodypart-raw (msg part process-crypto binaryp cache)
 		       (set-buffer-multibyte nil))
 		     (let ((args `("show" "--format=raw"
 				   ,(format "--part=%s" (plist-get part :id))
-				   ,@(when process-crypto '("--decrypt=true"))
+				   ,@(and process-crypto '("--decrypt=true"))
 				   ,(notmuch-id-to-query (plist-get msg :id))))
 			   (coding-system-for-read
 			    (if binaryp 'no-conversion
@@ -781,8 +781,8 @@ (defun notmuch-logged-error (msg &optional extra)
 	(insert extra)
 	(unless (bolp)
 	  (newline)))))
-  (error "%s" (concat msg (when extra
-			    " (see *Notmuch errors* for more details)"))))
+  (error "%s"
+	 (concat msg (and extra " (see *Notmuch errors* for more details)"))))
 
 (defun notmuch-check-async-exit-status (proc msg &optional command err)
   "If PROC exited abnormally, pop up an error buffer and signal an error.
@@ -836,10 +836,8 @@ (defun notmuch-check-exit-status (exit-status command &optional output err)
 		    (if (integerp exit-status)
 			(format "exit status: %s\n" exit-status)
 		      (format "exit signal: %s\n" exit-status))
-		    (when err
-		      (concat "stderr:\n" err))
-		    (when output
-		      (concat "stdout:\n" output)))))
+		    (and err    (concat "stderr:\n" err))
+		    (and output (concat "stdout:\n" output)))))
       (if err
 	  ;; We have an error message straight from the CLI.
 	  (notmuch-logged-error
@@ -968,8 +966,8 @@ (defun notmuch-start-notmuch-sentinel (proc event)
   (let* ((err-file (process-get proc 'err-file))
 	 (err-buffer (or (process-get proc 'err-buffer)
 			 (find-file-noselect err-file)))
-	 (err (when (not (zerop (buffer-size err-buffer)))
-		(with-current-buffer err-buffer (buffer-string))))
+	 (err (and (not (zerop (buffer-size err-buffer)))
+		   (with-current-buffer err-buffer (buffer-string))))
 	 (sub-sentinel (process-get proc 'sub-sentinel))
 	 (real-command (process-get proc 'real-command)))
     (condition-case err
@@ -987,16 +985,17 @@ (defun notmuch-start-notmuch-sentinel (proc event)
 	  ;; If that didn't signal an error, then any error output was
 	  ;; really warning output.  Show warnings, if any.
 	  (let ((warnings
-		 (when err
-		   (with-current-buffer err-buffer
-		     (goto-char (point-min))
-		     (end-of-line)
-		     ;; Show first line; stuff remaining lines in the
-		     ;; errors buffer.
-		     (let ((l1 (buffer-substring (point-min) (point))))
-		       (skip-chars-forward "\n")
-		       (cons l1 (unless (eobp)
-				  (buffer-substring (point) (point-max)))))))))
+		 (and err
+		      (with-current-buffer err-buffer
+			(goto-char (point-min))
+			(end-of-line)
+			;; Show first line; stuff remaining lines in the
+			;; errors buffer.
+			(let ((l1 (buffer-substring (point-min) (point))))
+			  (skip-chars-forward "\n")
+			  (cons l1 (and (not (eobp))
+					(buffer-substring (point)
+							  (point-max)))))))))
 	    (when warnings
 	      (notmuch-logged-error (car warnings) (cdr warnings)))))
       (error
diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index 7d001b2d..1027e1a7 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -215,7 +215,7 @@ (defun notmuch-maildir-notmuch-insert-current-buffer (folder &optional create ta
 database in folder FOLDER. If CREATE is non-nil it will supply
 the --create-folder flag to create the folder if necessary. TAGS
 should be a list of tag changes to apply to the inserted message."
-  (let* ((args (append (when create (list "--create-folder"))
+  (let* ((args (append (and create (list "--create-folder"))
 		       (list (concat "--folder=" folder))
 		       tags)))
     (apply 'notmuch-call-notmuch-process
@@ -315,7 +315,7 @@ (defun notmuch-maildir-fcc-move-tmp-to-new (destdir msg-id)
 (defun notmuch-maildir-fcc-move-tmp-to-cur (destdir msg-id &optional mark-seen)
   (add-name-to-file
    (concat destdir "/tmp/" msg-id)
-   (concat destdir "/cur/" msg-id ":2," (when mark-seen "S"))))
+   (concat destdir "/cur/" msg-id ":2," (and mark-seen "S"))))
 
 (defun notmuch-maildir-fcc-file-fcc (fcc-header)
   "Write the message to the file specified by FCC-HEADER.
diff --git a/emacs/notmuch-mua.el b/emacs/notmuch-mua.el
index 90b02e34..3fe513ab 100644
--- a/emacs/notmuch-mua.el
+++ b/emacs/notmuch-mua.el
@@ -460,8 +460,8 @@ (defun notmuch-mua-new-mail (&optional prompt-for-sender)
 the From: address first."
   (interactive "P")
   (let ((other-headers
-	 (when (or prompt-for-sender notmuch-always-prompt-for-sender)
-	   (list (cons 'From (notmuch-mua-prompt-for-sender))))))
+	 (and (or prompt-for-sender notmuch-always-prompt-for-sender)
+	      (list (cons 'From (notmuch-mua-prompt-for-sender))))))
     (notmuch-mua-mail nil nil other-headers nil (notmuch-mua-get-switch-function))))
 
 (defun notmuch-mua-new-forward-messages (messages &optional prompt-for-sender)
@@ -470,8 +470,8 @@ (defun notmuch-mua-new-forward-messages (messages &optional prompt-for-sender)
 If PROMPT-FOR-SENDER is non-nil, the user will be prompteed for
 the From: address."
   (let* ((other-headers
-	  (when (or prompt-for-sender notmuch-always-prompt-for-sender)
-	    (list (cons 'From (notmuch-mua-prompt-for-sender)))))
+	  (and (or prompt-for-sender notmuch-always-prompt-for-sender)
+	       (list (cons 'From (notmuch-mua-prompt-for-sender)))))
 	 forward-subject  ;; Comes from the first message and is
 	 ;; applied later.
 	 forward-references ;; List of accumulated message-references of forwarded messages
@@ -540,9 +540,8 @@ (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all
   ;; primary selection was previously in a non-emacs window but not if
   ;; it was in an emacs window. To avoid the problem in the latter case
   ;; we deactivate mark.
-  (let ((sender
-	 (when prompt-for-sender
-	   (notmuch-mua-prompt-for-sender)))
+  (let ((sender (and prompt-for-sender
+		     (notmuch-mua-prompt-for-sender)))
 	(select-active-regions nil))
     (notmuch-mua-reply query-string sender reply-all)
     (deactivate-mark)))
diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el
index a0a6c09d..211be091 100644
--- a/emacs/notmuch-show.el
+++ b/emacs/notmuch-show.el
@@ -500,10 +500,10 @@ (define-button-type 'notmuch-show-part-button-type
 (defun notmuch-show-insert-part-header (nth content-type declared-type
 					    &optional name comment)
   (let ((button)
-	(base-label (concat (when name (concat name ": "))
+	(base-label (concat (and name (concat name ": "))
 			    declared-type
-			    (unless (string-equal declared-type content-type)
-			      (concat " (as " content-type ")"))
+			    (and (not (string-equal declared-type content-type))
+				 (concat " (as " content-type ")"))
 			    comment)))
     (setq button
 	  (insert-button
@@ -781,18 +781,15 @@ (if (version< emacs-version "25.3")
 (defun notmuch-show-get-mime-type-of-application/octet-stream (part)
   ;; If we can deduce a MIME type from the filename of the attachment,
   ;; we return that.
-  (if (plist-get part :filename)
-      (let ((extension (file-name-extension (plist-get part :filename)))
-	    mime-type)
-	(if extension
-	    (progn
-	      (mailcap-parse-mimetypes)
-	      (setq mime-type (mailcap-extension-to-mime extension))
-	      (if (and mime-type
-		       (not (string-equal mime-type "application/octet-stream")))
-		  mime-type
-		nil))
-	  nil))))
+  (and (plist-get part :filename)
+       (let ((extension (file-name-extension (plist-get part :filename))))
+	 (and extension
+	      (progn
+		(mailcap-parse-mimetypes)
+		(let ((mime-type (mailcap-extension-to-mime extension)))
+		  (and mime-type
+		       (not (string-equal mime-type "application/octet-stream"))
+		       mime-type)))))))
 
 (defun notmuch-show-insert-part-text/html (msg part content-type nth depth button)
   (if (eq mm-text-html-renderer 'shr)
@@ -991,9 +988,10 @@ (defun notmuch-show-insert-bodypart (msg part depth &optional hide)
 	 (beg (point))
 	 ;; This default header-p function omits the part button for
 	 ;; the first (or only) part if this is text/plain.
-	 (button (when (funcall notmuch-show-insert-header-p-function part hide)
-		   (notmuch-show-insert-part-header nth mime-type content-type
-						    (plist-get part :filename))))
+	 (button (and (funcall notmuch-show-insert-header-p-function part hide)
+		      (notmuch-show-insert-part-header
+		       nth mime-type content-type
+		       (plist-get part :filename))))
 	 ;; Hide the part initially if HIDE is t, or if it is too long
 	 ;; and we have a button to allow toggling.
 	 (show-part (not (or (equal hide t)
@@ -1048,9 +1046,8 @@ (defun notmuch-show-insert-msg (msg depth)
 	 (bare-subject (notmuch-show-strip-re (plist-get headers :Subject))))
     (setq message-start (point-marker))
     (notmuch-show-insert-headerline headers
-				    (or (if notmuch-show-relative-dates
-					    (plist-get msg :date_relative)
-					  nil)
+				    (or (and notmuch-show-relative-dates
+					     (plist-get msg :date_relative))
 					(plist-get headers :Date))
 				    (plist-get msg :tags) depth)
     (setq content-start (point-marker))
@@ -1297,8 +1294,8 @@ (defun notmuch-show--build-buffer (&optional state)
 
 If no messages match the query return NIL."
   (let* ((cli-args (cons "--exclude=false"
-			 (when notmuch-show-elide-non-matching-messages
-			   (list "--entire-thread=false"))))
+			 (and notmuch-show-elide-non-matching-messages
+			      (list "--entire-thread=false"))))
 	 (queries (notmuch-show--build-queries
 		   notmuch-show-thread-id notmuch-show-query-context))
 	 (forest nil)
@@ -2406,7 +2403,7 @@ (defun notmuch-show-current-part-handle (&optional mime-type)
 	 (buf (notmuch-show-generate-part-buffer msg part))
 	 (computed-type (or mime-type (plist-get part :computed-type)))
 	 (filename (plist-get part :filename))
-	 (disposition (if filename `(attachment (filename . ,filename)))))
+	 (disposition (and filename `(attachment (filename . ,filename)))))
     (mm-make-handle buf (list computed-type) nil nil disposition)))
 
 (defun notmuch-show-apply-to-current-part-handle (fn &optional mime-type)
diff --git a/emacs/notmuch-tree.el b/emacs/notmuch-tree.el
index 827acebf..37a5d1c8 100644
--- a/emacs/notmuch-tree.el
+++ b/emacs/notmuch-tree.el
@@ -1020,8 +1020,8 @@ (defun notmuch-tree-worker (basic-query &optional query-context target open-targ
   (erase-buffer)
   (goto-char (point-min))
   (let* ((search-args (concat basic-query
-			      (if query-context
-				  (concat " and (" query-context ")"))))
+			      (and query-context
+				   (concat " and (" query-context ")"))))
 	 (message-arg (if unthreaded "--unthreaded" "--entire-thread")))
     (if (equal (car (process-lines notmuch-command "count" search-args)) "0")
 	(setq search-args basic-query))
diff --git a/emacs/notmuch-wash.el b/emacs/notmuch-wash.el
index 00ac45b6..31fda61f 100644
--- a/emacs/notmuch-wash.el
+++ b/emacs/notmuch-wash.el
@@ -370,10 +370,10 @@ (defun notmuch-wash-subject-to-patch-sequence-number (subject)
 
 Return the patch sequence number N from the last \"[PATCH N/M]\"
 style prefix in SUBJECT, or nil if such a prefix can't be found."
-  (when (string-match
-	 "^ *\\(\\[[^]]*\\] *\\)*\\[[^]]*?\\([0-9]+\\)/[0-9]+[^]]*\\].*"
-	 subject)
-    (string-to-number (substring subject (match-beginning 2) (match-end 2)))))
+  (and (string-match
+	"^ *\\(\\[[^]]*\\] *\\)*\\[[^]]*?\\([0-9]+\\)/[0-9]+[^]]*\\].*"
+	subject)
+       (string-to-number (substring subject (match-beginning 2) (match-end 2)))))
 
 (defun notmuch-wash-subject-to-patch-filename (subject)
   "Convert a patch mail SUBJECT into a filename.
diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index 6ce2b1f9..41207643 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -264,7 +264,8 @@ (defun notmuch-search-last-thread ()
   (goto-char (point-max))
   (forward-line -2)
   (let ((beg (notmuch-search-result-beginning)))
-    (when beg (goto-char beg))))
+    (when beg
+      (goto-char beg))))
 
 (defun notmuch-search-first-thread ()
   "Select the first thread in the search results."
@@ -406,11 +407,12 @@ (defun notmuch-search-result-beginning (&optional pos)
   "Return the point at the beginning of the thread at POS (or point).
 
 If there is no thread at POS (or point), returns nil."
-  (when (notmuch-search-get-result pos)
-    ;; We pass 1+point because previous-single-property-change starts
-    ;; searching one before the position we give it.
-    (previous-single-property-change (1+ (or pos (point)))
-				     'notmuch-search-result nil (point-min))))
+  (and (notmuch-search-get-result pos)
+       ;; We pass 1+point because previous-single-property-change starts
+       ;; searching one before the position we give it.
+       (previous-single-property-change (1+ (or pos (point)))
+					'notmuch-search-result nil
+					(point-min))))
 
 (defun notmuch-search-result-end (&optional pos)
   "Return the point at the end of the thread at POS (or point).
@@ -418,9 +420,10 @@ (defun notmuch-search-result-end (&optional pos)
 The returned point will be just after the newline character that
 ends the result line.  If there is no thread at POS (or point),
 returns nil."
-  (when (notmuch-search-get-result pos)
-    (next-single-property-change (or pos (point)) 'notmuch-search-result
-				 nil (point-max))))
+  (and (notmuch-search-get-result pos)
+       (next-single-property-change (or pos (point))
+				    'notmuch-search-result nil
+				    (point-max))))
 
 (defun notmuch-search-foreach-result (beg end fn)
   "Invoke FN for each result between BEG and END.
@@ -461,7 +464,8 @@ (defun notmuch-search-find-thread-id (&optional bare)
 
 If BARE is set then do not prefix with \"thread:\"."
   (let ((thread (plist-get (notmuch-search-get-result) :thread)))
-    (when thread (concat (unless bare "thread:") thread))))
+    (when thread
+      (concat (and (not bare) "thread:") thread))))
 
 (defun notmuch-search-find-stable-query ()
   "Return the stable queries for the current thread.
@@ -482,8 +486,8 @@ (defun notmuch-search-find-stable-query-region (beg end &optional only-matched)
 	(push (car queries) query-list))
       (when (and all (cadr queries))
 	(push (cadr queries) query-list)))
-    (when query-list
-      (concat "(" (mapconcat 'identity query-list ") or (") ")"))))
+    (and query-list
+	 (concat "(" (mapconcat 'identity query-list ") or (") ")"))))
 
 (defun notmuch-search-find-authors ()
   "Return the authors for the current thread."
-- 
2.26.0
_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: