emacs: Use 'and' instead of 'when' when the return value matters

Also do so for some 'if' forms that lack an ELSE part.
Even go as far as using 'and' and 'not' instead of 'unless'.
This commit is contained in:
Jonas Bernoulli 2020-08-08 13:49:41 +02:00 committed by David Bremner
parent 18d289c863
commit dfb1b8eb89
11 changed files with 114 additions and 113 deletions

View file

@ -107,12 +107,12 @@ not need to be wrapped, move point to the next line and return t."
If the line should not be broken, return nil; point remains on the If the line should not be broken, return nil; point remains on the
line." line."
(move-to-column fill-column) (move-to-column fill-column)
(if (and (re-search-forward "[^ ]" (line-end-position) 1) (and (re-search-forward "[^ ]" (line-end-position) 1)
(> (current-column) fill-column)) (> (current-column) fill-column)
;; This line is too long. Can we break it? ;; This line is too long. Can we break it?
(or (coolj-find-break-backward prefix) (or (coolj-find-break-backward prefix)
(progn (move-to-column fill-column) (progn (move-to-column fill-column)
(coolj-find-break-forward))))) (coolj-find-break-forward)))))
(defun coolj-find-break-backward (prefix) (defun coolj-find-break-backward (prefix)
"Move point backward to the first available breakpoint and return t. "Move point backward to the first available breakpoint and return t.

View file

@ -252,20 +252,20 @@ external commands."
(defun notmuch-address-locate-command (command) (defun notmuch-address-locate-command (command)
"Return non-nil if `command' is an executable either on "Return non-nil if `command' is an executable either on
`exec-path' or an absolute pathname." `exec-path' or an absolute pathname."
(when (stringp command) (and (stringp command)
(if (and (file-name-absolute-p command) (if (and (file-name-absolute-p command)
(file-executable-p command)) (file-executable-p command))
command command
(setq command (file-name-nondirectory command)) (setq command (file-name-nondirectory command))
(catch 'found-command (catch 'found-command
(let (bin) (let (bin)
(dolist (dir exec-path) (dolist (dir exec-path)
(setq bin (expand-file-name command dir)) (setq bin (expand-file-name command dir))
(when (or (and (file-executable-p bin) (when (or (and (file-executable-p bin)
(not (file-directory-p bin))) (not (file-directory-p bin)))
(and (file-executable-p (setq bin (concat bin ".exe"))) (and (file-executable-p (setq bin (concat bin ".exe")))
(not (file-directory-p bin)))) (not (file-directory-p bin))))
(throw 'found-command bin)))))))) (throw 'found-command bin))))))))
(defun notmuch-address-harvest-addr (result) (defun notmuch-address-harvest-addr (result)
(let ((name-addr (plist-get result :name-addr))) (let ((name-addr (plist-get result :name-addr)))
@ -304,18 +304,20 @@ asynchronously unless SYNCHRONOUS is t. In case of asynchronous
execution, CALLBACK is called when harvesting finishes." execution, CALLBACK is called when harvesting finishes."
(let* ((sent (eq (car notmuch-address-internal-completion) 'sent)) (let* ((sent (eq (car notmuch-address-internal-completion) 'sent))
(config-query (cadr notmuch-address-internal-completion)) (config-query (cadr notmuch-address-internal-completion))
(prefix-query (when addr-prefix (prefix-query (and addr-prefix
(format "%s:%s*" (if sent "to" "from") addr-prefix))) (format "%s:%s*"
(if sent "to" "from")
addr-prefix)))
(from-or-to-me-query (from-or-to-me-query
(mapconcat (lambda (x) (mapconcat (lambda (x)
(concat (if sent "from:" "to:") x)) (concat (if sent "from:" "to:") x))
(notmuch-user-emails) " or ")) (notmuch-user-emails) " or "))
(query (if (or prefix-query config-query) (query (if (or prefix-query config-query)
(concat (format "(%s)" from-or-to-me-query) (concat (format "(%s)" from-or-to-me-query)
(when prefix-query (and prefix-query
(format " and (%s)" prefix-query)) (format " and (%s)" prefix-query))
(when config-query (and config-query
(format " and (%s)" config-query))) (format " and (%s)" config-query)))
from-or-to-me-query)) from-or-to-me-query))
(args `("address" "--format=sexp" "--format-version=4" (args `("address" "--format=sexp" "--format-version=4"
,(if sent "--output=recipients" "--output=sender") ,(if sent "--output=recipients" "--output=sender")
@ -354,21 +356,21 @@ execution, CALLBACK is called when harvesting finishes."
Returns nil if the save file does not exist, or it does not seem Returns nil if the save file does not exist, or it does not seem
to be a saved address hash." to be a saved address hash."
(when notmuch-address-save-filename (and notmuch-address-save-filename
(condition-case nil (condition-case nil
(with-temp-buffer (with-temp-buffer
(insert-file-contents notmuch-address-save-filename) (insert-file-contents notmuch-address-save-filename)
(let ((name (read (current-buffer))) (let ((name (read (current-buffer)))
(plist (read (current-buffer)))) (plist (read (current-buffer))))
;; We do two simple sanity checks on the loaded file. We just ;; We do two simple sanity checks on the loaded file.
;; check a version is specified, not that it is the current ;; We just check a version is specified, not that
;; version, as we are allowed to over-write and a save-file with ;; it is the current version, as we are allowed to
;; an older version. ;; over-write and a save-file with an older version.
(when (and (string= name "notmuch-address-hash") (and (string= name "notmuch-address-hash")
(plist-get plist :version)) (plist-get plist :version)
plist))) plist)))
;; The error case catches any of the reads failing. ;; The error case catches any of the reads failing.
(error nil)))) (error nil))))
(defun notmuch-address--load-address-hash () (defun notmuch-address--load-address-hash ()
"Read the saved address hash and set the corresponding variables." "Read the saved address hash and set the corresponding variables."

View file

@ -253,7 +253,7 @@ corresponding key when the status button is pressed."
"Decryption error") "Decryption error")
(t (t
(concat "Unknown encryption status" (concat "Unknown encryption status"
(if status (concat ": " status)))))) (and status (concat ": " status))))))
" ]") " ]")
:type 'notmuch-crypto-status-button-type :type 'notmuch-crypto-status-button-type
'face 'notmuch-crypto-decryption 'face 'notmuch-crypto-decryption

View file

@ -263,7 +263,7 @@ applied to newly inserted messages)."
;; If the resumed message was a draft then set the draft ;; 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-id so that we can delete the current saved draft if the
;; message is resaved or sent. ;; 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) (add-hook 'message-send-hook 'notmuch-draft--mark-deleted)

View file

@ -608,7 +608,7 @@ the given type."
(set-buffer-multibyte nil)) (set-buffer-multibyte nil))
(let ((args `("show" "--format=raw" (let ((args `("show" "--format=raw"
,(format "--part=%s" (plist-get part :id)) ,(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)))) ,(notmuch-id-to-query (plist-get msg :id))))
(coding-system-for-read (coding-system-for-read
(if binaryp 'no-conversion (if binaryp 'no-conversion
@ -781,8 +781,8 @@ signaled error. This function does not return."
(insert extra) (insert extra)
(unless (bolp) (unless (bolp)
(newline))))) (newline)))))
(error "%s" (concat msg (when extra (error "%s"
" (see *Notmuch errors* for more details)")))) (concat msg (and extra " (see *Notmuch errors* for more details)"))))
(defun notmuch-check-async-exit-status (proc msg &optional command err) (defun notmuch-check-async-exit-status (proc msg &optional command err)
"If PROC exited abnormally, pop up an error buffer and signal an error. "If PROC exited abnormally, pop up an error buffer and signal an error.
@ -836,10 +836,8 @@ You may need to restart Emacs or upgrade your notmuch package."))
(if (integerp exit-status) (if (integerp exit-status)
(format "exit status: %s\n" exit-status) (format "exit status: %s\n" exit-status)
(format "exit signal: %s\n" exit-status)) (format "exit signal: %s\n" exit-status))
(when err (and err (concat "stderr:\n" err))
(concat "stderr:\n" err)) (and output (concat "stdout:\n" output)))))
(when output
(concat "stdout:\n" output)))))
(if err (if err
;; We have an error message straight from the CLI. ;; We have an error message straight from the CLI.
(notmuch-logged-error (notmuch-logged-error
@ -968,8 +966,8 @@ status."
(let* ((err-file (process-get proc 'err-file)) (let* ((err-file (process-get proc 'err-file))
(err-buffer (or (process-get proc 'err-buffer) (err-buffer (or (process-get proc 'err-buffer)
(find-file-noselect err-file))) (find-file-noselect err-file)))
(err (when (not (zerop (buffer-size err-buffer))) (err (and (not (zerop (buffer-size err-buffer)))
(with-current-buffer err-buffer (buffer-string)))) (with-current-buffer err-buffer (buffer-string))))
(sub-sentinel (process-get proc 'sub-sentinel)) (sub-sentinel (process-get proc 'sub-sentinel))
(real-command (process-get proc 'real-command))) (real-command (process-get proc 'real-command)))
(condition-case err (condition-case err
@ -987,16 +985,17 @@ status."
;; If that didn't signal an error, then any error output was ;; If that didn't signal an error, then any error output was
;; really warning output. Show warnings, if any. ;; really warning output. Show warnings, if any.
(let ((warnings (let ((warnings
(when err (and err
(with-current-buffer err-buffer (with-current-buffer err-buffer
(goto-char (point-min)) (goto-char (point-min))
(end-of-line) (end-of-line)
;; Show first line; stuff remaining lines in the ;; Show first line; stuff remaining lines in the
;; errors buffer. ;; errors buffer.
(let ((l1 (buffer-substring (point-min) (point)))) (let ((l1 (buffer-substring (point-min) (point))))
(skip-chars-forward "\n") (skip-chars-forward "\n")
(cons l1 (unless (eobp) (cons l1 (and (not (eobp))
(buffer-substring (point) (point-max))))))))) (buffer-substring (point)
(point-max)))))))))
(when warnings (when warnings
(notmuch-logged-error (car warnings) (cdr warnings))))) (notmuch-logged-error (car warnings) (cdr warnings)))))
(error (error

View file

@ -215,7 +215,7 @@ This inserts the current buffer as a message into the notmuch
database in folder FOLDER. If CREATE is non-nil it will supply database in folder FOLDER. If CREATE is non-nil it will supply
the --create-folder flag to create the folder if necessary. TAGS the --create-folder flag to create the folder if necessary. TAGS
should be a list of tag changes to apply to the inserted message." 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)) (list (concat "--folder=" folder))
tags))) tags)))
(apply 'notmuch-call-notmuch-process (apply 'notmuch-call-notmuch-process
@ -315,7 +315,7 @@ if successful, nil if not."
(defun notmuch-maildir-fcc-move-tmp-to-cur (destdir msg-id &optional mark-seen) (defun notmuch-maildir-fcc-move-tmp-to-cur (destdir msg-id &optional mark-seen)
(add-name-to-file (add-name-to-file
(concat destdir "/tmp/" msg-id) (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) (defun notmuch-maildir-fcc-file-fcc (fcc-header)
"Write the message to the file specified by FCC-HEADER. "Write the message to the file specified by FCC-HEADER.

View file

@ -460,8 +460,8 @@ If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
the From: address first." the From: address first."
(interactive "P") (interactive "P")
(let ((other-headers (let ((other-headers
(when (or prompt-for-sender notmuch-always-prompt-for-sender) (and (or prompt-for-sender notmuch-always-prompt-for-sender)
(list (cons 'From (notmuch-mua-prompt-for-sender)))))) (list (cons 'From (notmuch-mua-prompt-for-sender))))))
(notmuch-mua-mail nil nil other-headers nil (notmuch-mua-get-switch-function)))) (notmuch-mua-mail nil nil other-headers nil (notmuch-mua-get-switch-function))))
(defun notmuch-mua-new-forward-messages (messages &optional prompt-for-sender) (defun notmuch-mua-new-forward-messages (messages &optional prompt-for-sender)
@ -470,8 +470,8 @@ the From: address first."
If PROMPT-FOR-SENDER is non-nil, the user will be prompteed for If PROMPT-FOR-SENDER is non-nil, the user will be prompteed for
the From: address." the From: address."
(let* ((other-headers (let* ((other-headers
(when (or prompt-for-sender notmuch-always-prompt-for-sender) (and (or prompt-for-sender notmuch-always-prompt-for-sender)
(list (cons 'From (notmuch-mua-prompt-for-sender))))) (list (cons 'From (notmuch-mua-prompt-for-sender)))))
;; Comes from the first message and is applied later. ;; Comes from the first message and is applied later.
forward-subject forward-subject
;; List of accumulated message-references of forwarded messages. ;; List of accumulated message-references of forwarded messages.
@ -542,9 +542,8 @@ will be addressed to all recipients of the source message."
;; primary selection was previously in a non-emacs window but not if ;; 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 ;; it was in an emacs window. To avoid the problem in the latter case
;; we deactivate mark. ;; we deactivate mark.
(let ((sender (let ((sender (and prompt-for-sender
(when prompt-for-sender (notmuch-mua-prompt-for-sender)))
(notmuch-mua-prompt-for-sender)))
(select-active-regions nil)) (select-active-regions nil))
(notmuch-mua-reply query-string sender reply-all) (notmuch-mua-reply query-string sender reply-all)
(deactivate-mark))) (deactivate-mark)))

View file

@ -506,10 +506,10 @@ message at DEPTH in the current thread."
(defun notmuch-show-insert-part-header (nth content-type declared-type (defun notmuch-show-insert-part-header (nth content-type declared-type
&optional name comment) &optional name comment)
(let ((button) (let ((button)
(base-label (concat (when name (concat name ": ")) (base-label (concat (and name (concat name ": "))
declared-type declared-type
(unless (string-equal declared-type content-type) (and (not (string-equal declared-type content-type))
(concat " (as " content-type ")")) (concat " (as " content-type ")"))
comment))) comment)))
(setq button (setq button
(insert-button (insert-button
@ -787,18 +787,15 @@ will return nil if the CID is unknown or cannot be retrieved."
(defun notmuch-show-get-mime-type-of-application/octet-stream (part) (defun notmuch-show-get-mime-type-of-application/octet-stream (part)
;; If we can deduce a MIME type from the filename of the attachment, ;; If we can deduce a MIME type from the filename of the attachment,
;; we return that. ;; we return that.
(if (plist-get part :filename) (and (plist-get part :filename)
(let ((extension (file-name-extension (plist-get part :filename))) (let ((extension (file-name-extension (plist-get part :filename))))
mime-type) (and extension
(if extension (progn
(progn (mailcap-parse-mimetypes)
(mailcap-parse-mimetypes) (let ((mime-type (mailcap-extension-to-mime extension)))
(setq mime-type (mailcap-extension-to-mime extension)) (and mime-type
(if (and mime-type (not (string-equal mime-type "application/octet-stream"))
(not (string-equal mime-type "application/octet-stream"))) mime-type)))))))
mime-type
nil))
nil))))
(defun notmuch-show-insert-part-text/html (msg part content-type nth depth button) (defun notmuch-show-insert-part-text/html (msg part content-type nth depth button)
(if (eq mm-text-html-renderer 'shr) (if (eq mm-text-html-renderer 'shr)
@ -997,9 +994,10 @@ is t, hide the part initially and show the button."
(beg (point)) (beg (point))
;; This default header-p function omits the part button for ;; This default header-p function omits the part button for
;; the first (or only) part if this is text/plain. ;; the first (or only) part if this is text/plain.
(button (when (funcall notmuch-show-insert-header-p-function part hide) (button (and (funcall notmuch-show-insert-header-p-function part hide)
(notmuch-show-insert-part-header nth mime-type content-type (notmuch-show-insert-part-header
(plist-get part :filename)))) nth mime-type content-type
(plist-get part :filename))))
;; Hide the part initially if HIDE is t, or if it is too long ;; Hide the part initially if HIDE is t, or if it is too long
;; and we have a button to allow toggling. ;; and we have a button to allow toggling.
(show-part (not (or (equal hide t) (show-part (not (or (equal hide t)
@ -1054,9 +1052,8 @@ is t, hide the part initially and show the button."
(bare-subject (notmuch-show-strip-re (plist-get headers :Subject)))) (bare-subject (notmuch-show-strip-re (plist-get headers :Subject))))
(setq message-start (point-marker)) (setq message-start (point-marker))
(notmuch-show-insert-headerline headers (notmuch-show-insert-headerline headers
(or (if notmuch-show-relative-dates (or (and notmuch-show-relative-dates
(plist-get msg :date_relative) (plist-get msg :date_relative))
nil)
(plist-get headers :Date)) (plist-get headers :Date))
(plist-get msg :tags) depth) (plist-get msg :tags) depth)
(setq content-start (point-marker)) (setq content-start (point-marker))
@ -1303,8 +1300,8 @@ first relevant message.
If no messages match the query return NIL." If no messages match the query return NIL."
(let* ((cli-args (cons "--exclude=false" (let* ((cli-args (cons "--exclude=false"
(when notmuch-show-elide-non-matching-messages (and notmuch-show-elide-non-matching-messages
(list "--entire-thread=false")))) (list "--entire-thread=false"))))
(queries (notmuch-show--build-queries (queries (notmuch-show--build-queries
notmuch-show-thread-id notmuch-show-query-context)) notmuch-show-thread-id notmuch-show-query-context))
(forest nil) (forest nil)
@ -2412,7 +2409,7 @@ MIME-TYPE is given then set the handle's mime-type to MIME-TYPE."
(buf (notmuch-show-generate-part-buffer msg part)) (buf (notmuch-show-generate-part-buffer msg part))
(computed-type (or mime-type (plist-get part :computed-type))) (computed-type (or mime-type (plist-get part :computed-type)))
(filename (plist-get part :filename)) (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))) (mm-make-handle buf (list computed-type) nil nil disposition)))
(defun notmuch-show-apply-to-current-part-handle (fn &optional mime-type) (defun notmuch-show-apply-to-current-part-handle (fn &optional mime-type)

View file

@ -1020,8 +1020,8 @@ the same as for the function notmuch-tree."
(erase-buffer) (erase-buffer)
(goto-char (point-min)) (goto-char (point-min))
(let* ((search-args (concat basic-query (let* ((search-args (concat basic-query
(if query-context (and query-context
(concat " and (" query-context ")")))) (concat " and (" query-context ")"))))
(message-arg (if unthreaded "--unthreaded" "--entire-thread"))) (message-arg (if unthreaded "--unthreaded" "--entire-thread")))
(if (equal (car (process-lines notmuch-command "count" search-args)) "0") (if (equal (car (process-lines notmuch-command "count" search-args)) "0")
(setq search-args basic-query)) (setq search-args basic-query))

View file

@ -370,10 +370,10 @@ filename, before trimming any trailing . and - characters."
Return the patch sequence number N from the last \"[PATCH N/M]\" 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." style prefix in SUBJECT, or nil if such a prefix can't be found."
(when (string-match (and (string-match
"^ *\\(\\[[^]]*\\] *\\)*\\[[^]]*?\\([0-9]+\\)/[0-9]+[^]]*\\].*" "^ *\\(\\[[^]]*\\] *\\)*\\[[^]]*?\\([0-9]+\\)/[0-9]+[^]]*\\].*"
subject) subject)
(string-to-number (substring subject (match-beginning 2) (match-end 2))))) (string-to-number (substring subject (match-beginning 2) (match-end 2)))))
(defun notmuch-wash-subject-to-patch-filename (subject) (defun notmuch-wash-subject-to-patch-filename (subject)
"Convert a patch mail SUBJECT into a filename. "Convert a patch mail SUBJECT into a filename.

View file

@ -264,7 +264,8 @@ there will be called at other points of notmuch execution."
(goto-char (point-max)) (goto-char (point-max))
(forward-line -2) (forward-line -2)
(let ((beg (notmuch-search-result-beginning))) (let ((beg (notmuch-search-result-beginning)))
(when beg (goto-char beg)))) (when beg
(goto-char beg))))
(defun notmuch-search-first-thread () (defun notmuch-search-first-thread ()
"Select the first thread in the search results." "Select the first thread in the search results."
@ -406,11 +407,12 @@ If there is no thread at POS (or point), returns nil."
"Return the point at the beginning of the thread at POS (or point). "Return the point at the beginning of the thread at POS (or point).
If there is no thread at POS (or point), returns nil." If there is no thread at POS (or point), returns nil."
(when (notmuch-search-get-result pos) (and (notmuch-search-get-result pos)
;; We pass 1+point because previous-single-property-change starts ;; We pass 1+point because previous-single-property-change starts
;; searching one before the position we give it. ;; searching one before the position we give it.
(previous-single-property-change (1+ (or pos (point))) (previous-single-property-change (1+ (or pos (point)))
'notmuch-search-result nil (point-min)))) 'notmuch-search-result nil
(point-min))))
(defun notmuch-search-result-end (&optional pos) (defun notmuch-search-result-end (&optional pos)
"Return the point at the end of the thread at POS (or point). "Return the point at the end of the thread at POS (or point).
@ -418,9 +420,10 @@ If there is no thread at POS (or point), returns nil."
The returned point will be just after the newline character that The returned point will be just after the newline character that
ends the result line. If there is no thread at POS (or point), ends the result line. If there is no thread at POS (or point),
returns nil." returns nil."
(when (notmuch-search-get-result pos) (and (notmuch-search-get-result pos)
(next-single-property-change (or pos (point)) 'notmuch-search-result (next-single-property-change (or pos (point))
nil (point-max)))) 'notmuch-search-result nil
(point-max))))
(defun notmuch-search-foreach-result (beg end fn) (defun notmuch-search-foreach-result (beg end fn)
"Invoke FN for each result between BEG and END. "Invoke FN for each result between BEG and END.
@ -461,7 +464,8 @@ BEG."
If BARE is set then do not prefix with \"thread:\"." If BARE is set then do not prefix with \"thread:\"."
(let ((thread (plist-get (notmuch-search-get-result) :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 () (defun notmuch-search-find-stable-query ()
"Return the stable queries for the current thread. "Return the stable queries for the current thread.
@ -482,8 +486,8 @@ no messages in the region then return nil."
(push (car queries) query-list)) (push (car queries) query-list))
(when (and all (cadr queries)) (when (and all (cadr queries))
(push (cadr queries) query-list))) (push (cadr queries) query-list)))
(when query-list (and query-list
(concat "(" (mapconcat 'identity query-list ") or (") ")")))) (concat "(" (mapconcat 'identity query-list ") or (") ")"))))
(defun notmuch-search-find-authors () (defun notmuch-search-find-authors ()
"Return the authors for the current thread." "Return the authors for the current thread."