Use functions instead of lambdas in add-hook calls

This commit is contained in:
Thierry Volpiatto
2020-11-13 11:38:50 +01:00
parent 861194f9a1
commit 0109172ad4
7 changed files with 84 additions and 55 deletions

View File

@ -354,25 +354,32 @@ Just after saving we restore it; thus, the separator should never
appear on disk. Also update the Date and ensure we have a appear on disk. Also update the Date and ensure we have a
Message-ID." Message-ID."
(add-hook 'before-save-hook (add-hook 'before-save-hook
(lambda() #'mu4e~compose-before-save-hook-fn
;; replace the date nil t)
(save-excursion
(message-remove-header "Date")
(message-generate-headers '(Date Message-ID))
(save-match-data
(mu4e~draft-remove-mail-header-separator)))) nil t)
(add-hook 'after-save-hook (add-hook 'after-save-hook
(lambda () #'mu4e~compose-after-save-hook-fn
(save-match-data nil t))
(mu4e~compose-set-friendly-buffer-name)
(mu4e~draft-insert-mail-header-separator) (defun mu4e~compose-before-save-hook-fn ()
;; hide some headers again ;; replace the date
(widen) (save-excursion
(mu4e~compose-hide-headers) (message-remove-header "Date")
(set-buffer-modified-p nil) (message-generate-headers '(Date Message-ID))
(mu4e-message "Saved (%d lines)" (count-lines (point-min) (point-max))) (save-match-data
;; update the file on disk -- ie., without the separator (mu4e~draft-remove-mail-header-separator))))
(mu4e~proc-add (buffer-file-name)))) nil t))
(defun mu4e~compose-after-save-hook-fn ()
(save-match-data
(mu4e~compose-set-friendly-buffer-name)
(mu4e~draft-insert-mail-header-separator)
;; hide some headers again
(widen)
(mu4e~compose-hide-headers)
(set-buffer-modified-p nil)
(mu4e-message "Saved (%d lines)" (count-lines (point-min) (point-max)))
;; update the file on disk -- ie., without the separator
(mu4e~proc-add (buffer-file-name))))
;;; address completion ;;; address completion
@ -563,26 +570,34 @@ buffers; lets remap its faces so it uses the ones for mu4e."
;; setup the fcc-stuff, if needed ;; setup the fcc-stuff, if needed
(add-hook 'message-send-hook (add-hook 'message-send-hook
(lambda () ;; mu4e~compose-save-before-sending #'mu4e~setup-fcc-message-sent-hook-fn
;; when in-reply-to was removed, remove references as well. nil t)
(when (eq mu4e-compose-type 'reply)
(mu4e~remove-refs-maybe))
(when use-hard-newlines
(mu4e-send-harden-newlines))
;; for safety, always save the draft before sending
(set-buffer-modified-p t)
(save-buffer)
(mu4e~compose-setup-fcc-maybe)
(widen)) nil t)
;; when the message has been sent. ;; when the message has been sent.
(add-hook 'message-sent-hook (add-hook 'message-sent-hook
(lambda () ;; mu4e~compose-mark-after-sending #'mu4e~set-sent-handler-message-sent-hook-fn
(setq mu4e-sent-func 'mu4e-sent-handler) nil t))
(mu4e~proc-sent (buffer-file-name))) nil t))
;; mark these two hooks as permanent-local, so they'll survive mode-changes ;; mark these two hooks as permanent-local, so they'll survive mode-changes
;; (put 'mu4e~compose-save-before-sending 'permanent-local-hook t) ;; (put 'mu4e~compose-save-before-sending 'permanent-local-hook t)
(put 'mu4e~compose-mark-after-sending 'permanent-local-hook t)) (put 'mu4e~compose-mark-after-sending 'permanent-local-hook t))
(defun mu4e~setup-fcc-message-sent-hook-fn ()
;; mu4e~compose-save-before-sending
;; when in-reply-to was removed, remove references as well.
(when (eq mu4e-compose-type 'reply)
(mu4e~remove-refs-maybe))
(when use-hard-newlines
(mu4e-send-harden-newlines))
;; for safety, always save the draft before sending
(set-buffer-modified-p t)
(save-buffer)
(mu4e~compose-setup-fcc-maybe)
(widen))
(defun mu4e~set-sent-handler-message-sent-hook-fn ()
;; mu4e~compose-mark-after-sending
(setq mu4e-sent-func 'mu4e-sent-handler)
(mu4e~proc-sent (buffer-file-name)))
(defun mu4e-send-harden-newlines () (defun mu4e-send-harden-newlines ()
"Set the hard property to all newlines." "Set the hard property to all newlines."
(save-excursion (save-excursion

View File

@ -59,16 +59,18 @@
;; ;;
;; Allow bookmarking a mu4e buffer in regular emacs bookmarks. ;; Allow bookmarking a mu4e buffer in regular emacs bookmarks.
(defun mu4e~view-set-bookmark-make-record-fn ()
(set (make-local-variable 'bookmark-make-record-function)
'mu4e-view-bookmark-make-record))
(defun mu4e~headers-set-bookmark-make-record-fn ()
(set (make-local-variable 'bookmark-make-record-function)
'mu4e-view-bookmark-make-record))
;; Probably this can be moved to mu4e-view.el. ;; Probably this can be moved to mu4e-view.el.
(add-hook 'mu4e-view-mode-hook (add-hook 'mu4e-view-mode-hook #'mu4e~view-set-bookmark-make-record-fn)
(lambda ()
(set (make-local-variable 'bookmark-make-record-function)
'mu4e-view-bookmark-make-record)))
;; And this can be moved to mu4e-headers.el. ;; And this can be moved to mu4e-headers.el.
(add-hook 'mu4e-headers-mode-hook (add-hook 'mu4e-headers-mode-hook #'mu4e~headers-set-bookmark-make-record-fn)
(lambda ()
(set (make-local-variable 'bookmark-make-record-function)
'mu4e-view-bookmark-make-record)))
(defun mu4e-view-bookmark-make-record () (defun mu4e-view-bookmark-make-record ()
"Make a bookmark entry for a mu4e buffer. Note that this is an "Make a bookmark entry for a mu4e buffer. Note that this is an

View File

@ -1136,7 +1136,8 @@ no user-interaction ongoing."
;; maybe update the current headers upon indexing changes ;; maybe update the current headers upon indexing changes
(add-hook 'mu4e-index-updated-hook 'mu4e~headers-maybe-auto-update) (add-hook 'mu4e-index-updated-hook 'mu4e~headers-maybe-auto-update)
(add-hook 'mu4e-index-updated-hook (add-hook 'mu4e-index-updated-hook
(lambda() (run-hooks 'mu4e-message-changed-hook)) t) #'mu4e~headers-index-updated-hook-fn
t)
(setq (setq
truncate-lines t truncate-lines t
buffer-undo-list t ;; don't record undo information buffer-undo-list t ;; don't record undo information
@ -1146,6 +1147,9 @@ no user-interaction ongoing."
(mu4e~mark-initialize) ;; initialize the marking subsystem (mu4e~mark-initialize) ;; initialize the marking subsystem
(hl-line-mode 1)) (hl-line-mode 1))
(defun mu4e~headers-index-updated-hook-fn ()
(run-hooks 'mu4e-message-changed-hook))
;;; Highlighting ;;; Highlighting
(defvar mu4e~highlighted-docid nil (defvar mu4e~highlighted-docid nil

View File

@ -163,10 +163,12 @@ response in icalendar format."
;; also trash the message (thus must be appended to hooks). ;; also trash the message (thus must be appended to hooks).
(add-hook (add-hook
'message-sent-hook 'message-sent-hook
(lambda () (setq mu4e-sent-func #'mu4e~icalendar-setup-sent-hook-fn
(mu4e~icalendar-trash-message original-msg)))
t t)))) t t))))
(defun mu4e~icalendar-setup-sent-hook-fn ()
(setq mu4e-sent-func
(mu4e~icalendar-trash-message original-msg)))
(defun mu4e~icalendar-insert-diary (event reply-status filename) (defun mu4e~icalendar-insert-diary (event reply-status filename)
"Insert a diary entry for the EVENT in file named FILENAME. "Insert a diary entry for the EVENT in file named FILENAME.

View File

@ -55,11 +55,7 @@
(defun mu4e-speedbar-install-variables () (defun mu4e-speedbar-install-variables ()
"Install those variables used by speedbar to enhance mu4e." "Install those variables used by speedbar to enhance mu4e."
(add-hook 'mu4e-context-changed-hook (add-hook 'mu4e-context-changed-hook
(lambda() #'mu4e~speedbar-context-changed-hook-fn)
(when (buffer-live-p speedbar-buffer)
(with-current-buffer speedbar-buffer
(let ((inhibit-read-only t))
(mu4e-speedbar-buttons))))))
(dolist (keymap (dolist (keymap
'( mu4e-main-speedbar-key-map '( mu4e-main-speedbar-key-map
mu4e-headers-speedbar-key-map mu4e-headers-speedbar-key-map
@ -69,6 +65,12 @@
(define-key keymap "RET" 'speedbar-edit-line) (define-key keymap "RET" 'speedbar-edit-line)
(define-key keymap "e" 'speedbar-edit-line)))) (define-key keymap "e" 'speedbar-edit-line))))
(defun mu4e~speedbar-context-changed-hook-fn ()
(when (buffer-live-p speedbar-buffer)
(with-current-buffer speedbar-buffer
(let ((inhibit-read-only t))
(mu4e-speedbar-buttons)))))
;; Make sure our special speedbar major mode is loaded ;; Make sure our special speedbar major mode is loaded
(if (featurep 'speedbar) (if (featurep 'speedbar)
(mu4e-speedbar-install-variables) (mu4e-speedbar-install-variables)

View File

@ -407,13 +407,15 @@ article-mode."
(mu4e-view-mode) (mu4e-view-mode)
(setq gnus-article-decoded-p gnus-article-decode-hook) (setq gnus-article-decoded-p gnus-article-decode-hook)
(set-buffer-modified-p nil) (set-buffer-modified-p nil)
(add-hook 'kill-buffer-hook (add-hook 'kill-buffer-hook #'mu4e~view-kill-buffer-hook-fn)
(lambda() ;; cleanup the mm-* buffers that the view spawns
(when mu4e~gnus-article-mime-handles
(mm-destroy-parts mu4e~gnus-article-mime-handles)
(setq mu4e~gnus-article-mime-handles nil))))
(read-only-mode))) (read-only-mode)))
(defun mu4e~view-kill-buffer-hook-fn ()
;; cleanup the mm-* buffers that the view spawns
(when mu4e~gnus-article-mime-handles
(mm-destroy-parts mu4e~gnus-article-mime-handles)
(setq mu4e~gnus-article-mime-handles nil)))
(defun mu4e~view-gnus-display-mime (msg) (defun mu4e~view-gnus-display-mime (msg)
"Same as `gnus-display-mime' but add a mu4e headers to MSG." "Same as `gnus-display-mime' but add a mu4e headers to MSG."
(lambda (&optional ihandles) (lambda (&optional ihandles)

View File

@ -185,8 +185,7 @@ or org-mode (when in the body)."
((and (> (point) sepapoint) (eq major-mode 'mu4e-compose-mode)) ((and (> (point) sepapoint) (eq major-mode 'mu4e-compose-mode))
(org-mode) (org-mode)
(add-hook 'before-save-hook (add-hook 'before-save-hook
(lambda () #'org~mu4e-error-before-save-hook-fn
(mu4e-error "Switch to mu4e-compose-mode (M-m) before saving"))
nil t) nil t)
(org~mu4e-mime-decorate-headers) (org~mu4e-mime-decorate-headers)
(local-set-key (kbd "M-m") (local-set-key (kbd "M-m")
@ -203,6 +202,9 @@ or org-mode (when in the body)."
;; and add the hook ;; and add the hook
(add-hook 'post-command-hook 'org~mu4e-mime-switch-headers-or-body t t)))) (add-hook 'post-command-hook 'org~mu4e-mime-switch-headers-or-body t t))))
(defun org~mu4e-error-before-save-hook-fn ()
(mu4e-error "Switch to mu4e-compose-mode (M-m) before saving"))
(defun org-mu4e-compose-org-mode () (defun org-mu4e-compose-org-mode ()
"Defines a pseudo-minor mode for mu4e-compose-mode. "Defines a pseudo-minor mode for mu4e-compose-mode.
Edit the message body using org mode. DEPRECATED." Edit the message body using org mode. DEPRECATED."