emacs: avoid unnecessary let-bindings

To some extend this is a personal preference, but the preference is
strongly dependent on whether one is used to a language that makes it
necessary to use variables like this.

This makes it perfectly clear that we are first getting and then using
a "foo":

  (use-foo (get-foo))

Sure this has to be read "inside out", but that's something one better
gets used to quickly when dealing with lisp.  I don't understand why
one would want to write this instead:

  (let ((the-foo (get-foo)))
    (use-foo the-foo))

Both `get-foo' and `use-foo' are named in a way that make it very
clear that we are dealing with a "foo".  Storing the value in an
additional variable `the-foo' does not make this any more clear.

On the contrary I makes the reader wonder why the author choose to
use a variable.  Is the value used more than once?  Is the value
being retrieved in one context and then used in another (e.g. when
the current buffer changes)?
This commit is contained in:
Jonas Bernoulli 2021-01-10 15:01:06 +01:00 committed by David Bremner
parent 25a8873c68
commit f47e3333b5
7 changed files with 48 additions and 51 deletions

View file

@ -260,8 +260,8 @@ requiring external commands."
;;; Harvest ;;; Harvest
(defun notmuch-address-harvest-addr (result) (defun notmuch-address-harvest-addr (result)
(let ((name-addr (plist-get result :name-addr))) (puthash (plist-get result :name-addr)
(puthash name-addr t notmuch-address-completions))) t notmuch-address-completions))
(defun notmuch-address-harvest-filter (proc string) (defun notmuch-address-harvest-filter (proc string)
(when (buffer-live-p (process-buffer proc)) (when (buffer-live-p (process-buffer proc))

View file

@ -416,9 +416,9 @@ A command that supports a prefix argument can explicitly document
its prefixed behavior by setting the 'notmuch-prefix-doc property its prefixed behavior by setting the 'notmuch-prefix-doc property
of its command symbol." of its command symbol."
(interactive) (interactive)
(let* ((mode major-mode) (let ((doc (substitute-command-keys
(doc (substitute-command-keys (notmuch-substitute-command-keys
(notmuch-substitute-command-keys (documentation mode t))))) (documentation major-mode t)))))
(with-current-buffer (generate-new-buffer "*notmuch-help*") (with-current-buffer (generate-new-buffer "*notmuch-help*")
(insert doc) (insert doc)
(goto-char (point-min)) (goto-char (point-min))

View file

@ -207,11 +207,11 @@ 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 (and create (list "--create-folder")) (apply 'notmuch-call-notmuch-process
(list (concat "--folder=" folder)) :stdin-string (buffer-string) "insert"
tags))) (append (and create (list "--create-folder"))
(apply 'notmuch-call-notmuch-process (list (concat "--folder=" folder))
:stdin-string (buffer-string) "insert" args))) tags)))
(defun notmuch-maildir-fcc-with-notmuch-insert (fcc-header &optional create) (defun notmuch-maildir-fcc-with-notmuch-insert (fcc-header &optional create)
"Store message with notmuch insert. "Store message with notmuch insert.

View file

@ -1666,13 +1666,13 @@ It gets property PROP from PROPS or, if PROPS is nil, the current
message in either tree or show. This means that several utility message in either tree or show. This means that several utility
functions in notmuch-show can be used directly by notmuch-tree as functions in notmuch-show can be used directly by notmuch-tree as
they just need the correct message properties." they just need the correct message properties."
(let ((props (or props (plist-get (or props
(cond ((eq major-mode 'notmuch-show-mode) (cond ((eq major-mode 'notmuch-show-mode)
(notmuch-show-get-message-properties)) (notmuch-show-get-message-properties))
((eq major-mode 'notmuch-tree-mode) ((eq major-mode 'notmuch-tree-mode)
(notmuch-tree-get-message-properties)) (notmuch-tree-get-message-properties))
(t nil))))) (t nil)))
(plist-get props prop))) prop))
(defun notmuch-show-get-message-id (&optional bare) (defun notmuch-show-get-message-id (&optional bare)
"Return an id: query for the Message-Id of the current message. "Return an id: query for the Message-Id of the current message.

View file

@ -406,8 +406,9 @@ Return all tags if no search terms are given."
"\n+" t)) "\n+" t))
(defun notmuch-select-tag-with-completion (prompt &rest search-terms) (defun notmuch-select-tag-with-completion (prompt &rest search-terms)
(let ((tag-list (apply #'notmuch-tag-completions search-terms))) (completing-read prompt
(completing-read prompt tag-list nil nil nil 'notmuch-select-tag-history))) (apply #'notmuch-tag-completions search-terms)
nil nil nil 'notmuch-select-tag-history))
(defun notmuch-read-tag-changes (current-tags &optional prompt initial-input) (defun notmuch-read-tag-changes (current-tags &optional prompt initial-input)
"Prompt for tag changes in the minibuffer. "Prompt for tag changes in the minibuffer.
@ -455,10 +456,9 @@ present or a \"-\" to indicate that the tag should be removed
from TAGS if present." from TAGS if present."
(let ((result-tags (copy-sequence tags))) (let ((result-tags (copy-sequence tags)))
(dolist (tag-change tag-changes) (dolist (tag-change tag-changes)
(let ((op (aref tag-change 0)) (let ((tag (and (not (string= tag-change ""))
(tag (and (not (string= tag-change ""))
(substring tag-change 1)))) (substring tag-change 1))))
(cl-case op (cl-case (aref tag-change 0)
(?+ (unless (member tag result-tags) (?+ (unless (member tag result-tags)
(push tag result-tags))) (push tag result-tags)))
(?- (setq result-tags (delete tag result-tags))) (?- (setq result-tags (delete tag result-tags)))

View file

@ -401,9 +401,8 @@ Some useful entries are:
(notmuch-tree-set-message-properties props))) (notmuch-tree-set-message-properties props)))
(defun notmuch-tree-get-prop (prop &optional props) (defun notmuch-tree-get-prop (prop &optional props)
(let ((props (or props (plist-get (or props (notmuch-tree-get-message-properties))
(notmuch-tree-get-message-properties)))) prop))
(plist-get props prop)))
(defun notmuch-tree-set-tags (tags) (defun notmuch-tree-set-tags (tags)
"Set the tags of the current message." "Set the tags of the current message."

View file

@ -521,17 +521,16 @@ With a prefix argument, invert the default value of
`notmuch-show-only-matching-messages' when displaying the `notmuch-show-only-matching-messages' when displaying the
thread." thread."
(interactive "P") (interactive "P")
(let ((thread-id (notmuch-search-find-thread-id)) (let ((thread-id (notmuch-search-find-thread-id)))
(subject (notmuch-search-find-subject))) (if thread-id
(if (> (length thread-id) 0)
(notmuch-show thread-id (notmuch-show thread-id
elide-toggle elide-toggle
(current-buffer) (current-buffer)
notmuch-search-query-string notmuch-search-query-string
;; Name the buffer based on the subject. ;; Name the buffer based on the subject.
(concat "*" (format "*%s*" (truncate-string-to-width
(truncate-string-to-width subject 30 nil nil t) (notmuch-search-find-subject)
"*")) 30 nil nil t)))
(message "End of search results.")))) (message "End of search results."))))
(defun notmuch-tree-from-search-current-query () (defun notmuch-tree-from-search-current-query ()
@ -556,20 +555,21 @@ thread."
(defun notmuch-search-reply-to-thread (&optional prompt-for-sender) (defun notmuch-search-reply-to-thread (&optional prompt-for-sender)
"Begin composing a reply-all to the entire current thread in a new buffer." "Begin composing a reply-all to the entire current thread in a new buffer."
(interactive "P") (interactive "P")
(let ((message-id (notmuch-search-find-thread-id))) (notmuch-mua-new-reply (notmuch-search-find-thread-id)
(notmuch-mua-new-reply message-id prompt-for-sender t))) prompt-for-sender t))
(defun notmuch-search-reply-to-thread-sender (&optional prompt-for-sender) (defun notmuch-search-reply-to-thread-sender (&optional prompt-for-sender)
"Begin composing a reply to the entire current thread in a new buffer." "Begin composing a reply to the entire current thread in a new buffer."
(interactive "P") (interactive "P")
(let ((message-id (notmuch-search-find-thread-id))) (notmuch-mua-new-reply (notmuch-search-find-thread-id)
(notmuch-mua-new-reply message-id prompt-for-sender nil))) prompt-for-sender nil))
;;; Tags ;;; Tags
(defun notmuch-search-set-tags (tags &optional pos) (defun notmuch-search-set-tags (tags &optional pos)
(let ((new-result (plist-put (notmuch-search-get-result pos) :tags tags))) (notmuch-search-update-result
(notmuch-search-update-result new-result pos))) (plist-put (notmuch-search-get-result pos) :tags tags)
pos))
(defun notmuch-search-get-tags (&optional pos) (defun notmuch-search-get-tags (&optional pos)
(plist-get (notmuch-search-get-result pos) :tags)) (plist-get (notmuch-search-get-result pos) :tags))
@ -1013,10 +1013,9 @@ the configured default sort order."
(setq notmuch-search-target-thread target-thread) (setq notmuch-search-target-thread target-thread)
(setq notmuch-search-target-line target-line) (setq notmuch-search-target-line target-line)
(notmuch-tag-clear-cache) (notmuch-tag-clear-cache)
(let ((proc (get-buffer-process (current-buffer))) (when (get-buffer-process buffer)
(inhibit-read-only t)) (error "notmuch search process already running for query `%s'" query))
(when proc (let ((inhibit-read-only t))
(error "notmuch search process already running for query `%s'" query))
(erase-buffer) (erase-buffer)
(goto-char (point-min)) (goto-char (point-min))
(save-excursion (save-excursion
@ -1045,13 +1044,12 @@ the new search results, then point will be placed on the same
thread. Otherwise, point will be moved to attempt to be in the thread. Otherwise, point will be moved to attempt to be in the
same relative position within the new buffer." same relative position within the new buffer."
(interactive) (interactive)
(let ((target-line (line-number-at-pos)) (notmuch-search notmuch-search-query-string
(oldest-first notmuch-search-oldest-first) notmuch-search-oldest-first
(target-thread (notmuch-search-find-thread-id 'bare)) (notmuch-search-find-thread-id 'bare)
(query notmuch-search-query-string)) (line-number-at-pos)
;; notmuch-search erases the current buffer. t)
(notmuch-search query oldest-first target-thread target-line t) (goto-char (point-min)))
(goto-char (point-min))))
(defun notmuch-search-toggle-order () (defun notmuch-search-toggle-order ()
"Toggle the current search order. "Toggle the current search order.
@ -1160,9 +1158,9 @@ Used as`imenu-prev-index-position-function' in notmuch buffers."
"Return imenu name for line at point. "Return imenu name for line at point.
Used as `imenu-extract-index-name-function' in notmuch buffers. Used as `imenu-extract-index-name-function' in notmuch buffers.
Point should be at the beginning of the line." Point should be at the beginning of the line."
(let ((subject (notmuch-search-find-subject)) (format "%s (%s)"
(author (notmuch-search-find-authors))) (notmuch-search-find-subject)
(format "%s (%s)" subject author))) (notmuch-search-find-authors)))
;;; _ ;;; _