branch: externals/dtache commit b38560b002b26bcbfd85fa4100df2202a16ee9ac Author: Niklas Eklund <niklas.ekl...@posteo.net> Commit: Niklas Eklund <niklas.ekl...@posteo.net>
Remove dtache's need of setup functions The package required the user to run different setup functions, which forced users to load dtache at start-up. This restructuring update the code as well as the README to guide users on how they can lazy load dtache. --- README.md | 38 +++++++++++++++++++++----------------- dtache-compile.el | 31 +++++++++++++------------------ dtache-eshell.el | 9 ++------- dtache-org.el | 6 ------ dtache-shell.el | 9 +-------- dtache.el | 55 +++++++++++++++++++++++++++++-------------------------- 6 files changed, 66 insertions(+), 82 deletions(-) diff --git a/README.md b/README.md index 70333f515c..090a639d02 100644 --- a/README.md +++ b/README.md @@ -63,8 +63,10 @@ A minimal configuration for `dtache`. ``` emacs-lisp (use-package dtache - :hook (after-init . dtache-setup) - :bind (([remap async-shell-command] . dtache-shell-command))) + :defer t + :hook ((shell-mode . dtache-shell-mode)) + :bind (([remap async-shell-command] . dtache-shell-command)) + :custom ((dtache-show-output-on-attach t))) ``` # Commands @@ -127,10 +129,11 @@ A `use-package` configuration of the `dtache-shell` extension, which provides th ``` emacs-lisp (use-package dtache-shell - :after dtache - :config - (dtache-shell-setup) - (setq dtache-shell-history-file "~/.bash_history")) + :after shell + :init + (advice-add 'shell :around #'dtache-shell-override-history) + (add-hook 'shell-mode-hook #'dtache-shell-save-history-on-kill) + :custom (dtache-shell-history-file "~/.bash_history")) ``` A minor mode named `dtache-shell-mode` is provided, and will be enabled in `shell`. The commands that are implemented are: @@ -147,9 +150,8 @@ A `use-package` configuration of the `dtache-eshell` extension, which provides t ``` emacs-lisp (use-package dtache-eshell - :after (eshell dtache) - :config - (dtache-eshell-setup)) + :defer t + :hook ((eshell-mode . dtache-eshell-mode))) ``` A minor mode named `dtache-eshell-mode` is provided, and will be enabled in `eshell`. The commands that are implemented are: @@ -167,10 +169,12 @@ In this [blog post](https://niklaseklund.gitlab.io/blog/posts/dtache_eshell/) th A `use-package` configuration of the `dtache-compile` extension, which provides the integration with `compile`. ``` emacs-lisp - (use-package dtache-compile - :hook (after-init . dtache-compile-setup) - :bind (([remap compile] . dtache-compile) - ([remap recompile] . dtache-compile-recompile))) +(use-package dtache-compile + :defer t + :bind (([remap compile] . dtache-compile) + ([remap recompile] . dtache-compile-recompile)) + :hook ((compilation-start . dtache-compile-start) + (compilation-shell-minor-mode . dtache-shell-mode))) ``` The package implements the commands `dtache-compile` and `dtache-compile-recompile`, which are thin wrappers around the original `compile` and `recompile` commands. The users should be able to use the former as replacements for the latter without noticing any difference except from the possibility to `detach`. @@ -182,14 +186,14 @@ A `use-package` configuration of the `dtache-org` extension, which provides the ``` emacs-lisp (use-package dtache-org - :after (dtache org) - :config - (dtache-org-setup)) + :after ob + :init + (advice-add #'org-babel-sh-evaluate :around #'dtache-org-babel-sh)) ``` The package implements an additional header argument for `ob-shell`. The header argument is `:dtache t`. When provided it will enable the code inside a src block to be run with `dtache`. Since org is not providing any live updates on the output the session is created with `dtache-sesion-mode` set to `create`. This means that if you want to access the output of the session you do that the same way you would for any other type of session. The `dtache-org` works both with and without the `: [...] -``` emacs-lisp +``` #+begin_src sh :dtache t cd ~/code ls -la diff --git a/dtache-compile.el b/dtache-compile.el index 713805225e..6ed9f3060e 100644 --- a/dtache-compile.el +++ b/dtache-compile.el @@ -82,6 +82,7 @@ Optionally EDIT-COMMAND." (dtache--current-session session)) (compilation-start (dtache--session-command session))))) +;;;###autoload (defun dtache-compile-open (session) "Open SESSION with `dtache-compile'." (when (dtache-valid-session session) @@ -89,13 +90,15 @@ Optionally EDIT-COMMAND." (dtache-compile-attach session) (dtache-compile-session session)))) -;;;###autoload -(defun dtache-compile-setup () - "Setup `dtache-compile'." - (dtache-setup) - (advice-add #'compilation-start :around #'dtache-compile--compilation-start) - (add-hook 'compilation-start-hook #'dtache-compile--start) - (add-hook 'compilation-shell-minor-mode-hook #'dtache-shell-mode)) +(defun dtache-compile-start (_) + "Run in `compilation-start-hook' if `dtache-enabled'." + (when dtache-enabled + (setq dtache--buffer-session dtache--current-session) + (dtache-compile--replace-modesetter) + (when dtache-filter-ansi-sequences + (add-hook 'compilation-filter-hook #'ansi-color-compilation-filter 0 t)) + (add-hook 'comint-preoutput-filter-functions #'dtache--dtache-env-message-filter 0 t) + (add-hook 'comint-preoutput-filter-functions #'dtache--dtach-eof-message-filter 0 t))) ;;;;; Support functions @@ -116,16 +119,6 @@ Optionally EDIT-COMMAND." ,highlight-regexp))))) (apply compilation-start args))) -(defun dtache-compile--start (_) - "Run in `compilation-start-hook' if `dtache-enabled'." - (when dtache-enabled - (setq dtache--buffer-session dtache--current-session) - (dtache-compile--replace-modesetter) - (when dtache-filter-ansi-sequences - (add-hook 'compilation-filter-hook #'ansi-color-compilation-filter 0 t)) - (add-hook 'comint-preoutput-filter-functions #'dtache--dtache-env-message-filter 0 t) - (add-hook 'comint-preoutput-filter-functions #'dtache--dtach-eof-message-filter 0 t))) - (defun dtache-compile--replace-modesetter () "Replace the modsetter inserted by `compilation-start'." (save-excursion @@ -164,10 +157,12 @@ Optionally EDIT-COMMAND." ;;;###autoload (define-derived-mode dtache-compilation-mode compilation-mode "Dtache Compilation" - "Major mode for tailing dtache logs." + "Major mode for tailing `dtache' logs." (add-hook 'compilation-filter-hook #'dtache-compile--compilation-eof-filter 0 t) (add-hook 'compilation-filter-hook #'dtache-compile--compilation-dtache-filter 0 t)) +(advice-add #'compilation-start :around #'dtache-compile--compilation-start) + (provide 'dtache-compile) ;;; dtache-compile.el ends here diff --git a/dtache-eshell.el b/dtache-eshell.el index a7edd69d18..f6756ee3b1 100644 --- a/dtache-eshell.el +++ b/dtache-eshell.el @@ -59,13 +59,6 @@ (and (string= (process-name process) "dtach") process))) -;;;###autoload -(defun dtache-eshell-setup () - "Setup `dtache-eshell'." - (dtache-setup) - (add-hook 'eshell-mode-hook #'dtache-eshell-mode) - (advice-add #'eshell-external-command :around #'dtache-eshell--external-command)) - ;;;; Commands ;;;###autoload @@ -146,6 +139,8 @@ If prefix-argument directly DETACH from the session." (remove-hook 'eshell-preoutput-filter-functions #'dtache--dtache-env-message-filter) (remove-hook 'eshell-preoutput-filter-functions #'dtache--dtach-eof-message-filter))) +(advice-add #'eshell-external-command :around #'dtache-eshell--external-command) + (provide 'dtache-eshell) ;;; dtache-eshell.el ends here diff --git a/dtache-org.el b/dtache-org.el index 07e9054ae8..dab1a33f93 100644 --- a/dtache-org.el +++ b/dtache-org.el @@ -41,12 +41,6 @@ ;;;; Functions -;;;###autoload -(defun dtache-org-setup () - "Setup `dtache-org'." - (dtache-setup) - (advice-add #'org-babel-sh-evaluate :around #'dtache-org-babel-sh)) - (defun dtache-org-babel-sh (org-babel-sh-evaluate-fun &rest args) "Modify ARGS before calling ORG-BABEL-SH-EVALUATE-FUN. diff --git a/dtache-shell.el b/dtache-shell.el index b2d40598a3..27299236a7 100644 --- a/dtache-shell.el +++ b/dtache-shell.el @@ -44,13 +44,6 @@ ;;;; Functions -;;;###autoload -(defun dtache-shell-setup () - "Setup `dtache-shell'." - (dtache-setup) - (add-hook 'shell-mode-hook #'dtache-shell--save-history-on-kill) - (advice-add 'shell :around #'dtache-shell-override-history)) - (defun dtache-shell-select-session () "Return selected session." (let* ((host-name (car (dtache--host))) @@ -137,7 +130,7 @@ This function also makes sure that the HISTFILE is disabled for local shells." (advice-add 'comint-read-input-ring :around #'dtache-shell--comint-read-input-ring-advice) (apply orig-fun args))) -(defun dtache-shell--save-history-on-kill () +(defun dtache-shell-save-history-on-kill () "Add hook to save history when killing `shell' buffer." (add-hook 'kill-buffer-hook #'dtache-shell--save-history 0 t)) diff --git a/dtache.el b/dtache.el index 93befbca6a..0153a53f3d 100644 --- a/dtache.el +++ b/dtache.el @@ -162,6 +162,13 @@ If set to a non nil value the latest entry to :type 'hook :group 'dtache) +(defcustom dtache-shell-mode-filter-functions + '(dtache--dtache-env-message-filter + dtache--dtach-eof-message-filter) + "A list of filter functions that are run in `dtache-shell-mode'." + :type 'list + :group 'dtache) + ;;;;; Public (defvar dtache-enabled nil) @@ -673,22 +680,6 @@ Optionally SUPPRESS-OUTPUT." )))) ""))) -(defun dtache--annotation-widths (sessions annotation-format) - "Return widths for ANNOTATION-FORMAT based on SESSIONS." - (seq-map (lambda (it) (dtache--annotation-width sessions it)) annotation-format)) - -(defun dtache--annotation-width (sessions annotation) - "Determine width for ANNOTATION based on SESSIONS." - (let ((annotation-fun (plist-get annotation ':function)) - (width (plist-get annotation ':width))) - `(,annotation-fun . - ,(thread-last sessions - (seq-map annotation-fun) - (seq-map #'length) - (seq-max) - (min width))))) - -;;;###autoload (defun dtache-setup () "Initialize `dtache'." @@ -726,10 +717,7 @@ Optionally SUPPRESS-OUTPUT." (seq-filter (lambda (it) (eq 'active (dtache--session-state it)))) (seq-map #'dtache--session-directory) (seq-uniq) - (seq-do #'dtache--watch-session-directory)) - - ;; Other - (add-hook 'shell-mode-hook #'dtache-shell-mode))) + (seq-do #'dtache--watch-session-directory)))) (defun dtache-valid-session (session) "Ensure that SESSION is valid. @@ -796,7 +784,8 @@ This function uses the `notifications' library." (t (message "Dtache session is in an unexpected state."))))) (defun dtache-get-sessions () - "Return validitated sessions." + "Return validated sessions." + (dtache-setup) (dtache--validate-unknown-sessions) (dtache--db-get-sessions)) @@ -1272,6 +1261,21 @@ If event is cased by an update to the `dtache' database, re-initialize (when database-updated) (dtache--db-initialize))) +(defun dtache--annotation-widths (sessions annotation-format) + "Return widths for ANNOTATION-FORMAT based on SESSIONS." + (seq-map (lambda (it) (dtache--annotation-width sessions it)) annotation-format)) + +(defun dtache--annotation-width (sessions annotation) + "Determine width for ANNOTATION based on SESSIONS." + (let ((annotation-fun (plist-get annotation ':function)) + (width (plist-get annotation ':width))) + `(,annotation-fun . + ,(thread-last sessions + (seq-map annotation-fun) + (seq-map #'length) + (seq-max) + (min width))))) + ;;;;; UI (defun dtache--command-str (session max-length) @@ -1359,11 +1363,10 @@ If event is cased by an update to the `dtache' database, re-initialize :keymap (let ((map (make-sparse-keymap))) map) (if dtache-shell-mode - (progn - (add-hook 'comint-preoutput-filter-functions #'dtache--dtache-env-message-filter 0 t) - (add-hook 'comint-preoutput-filter-functions #'dtache--dtach-eof-message-filter 0 t)) - (remove-hook 'comint-preoutput-filter-functions #'dtache--dtache-env-message-filter t) - (remove-hook 'comint-preoutput-filter-functions #'dtache--dtach-eof-message-filter t))) + (dolist (filter dtache-shell-mode-filter-functions) + (add-hook 'comint-preoutput-filter-functions filter 0 t)) + (dolist (filter dtache-shell-mode-filter-functions) + (remove-hook 'comint-preoutput-filter-functions filter t)))) ;;;; Major modes