From 8ea5ace578d5fa7c4673c28079291339370634b7 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Sun, 17 Oct 2021 11:50:57 -0700 Subject: emacs: add function to export eshell output Add a new function (and a new binding) to export the latest output from eshell to a buffer. The code comes from https://gitlab.com/protesilaos/dotfiles/-/blob/master/emacs/.emacs.d/prot-lisp/prot-eshell.el#L114 --- emacs/custom/fcuny-eshell.el | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'emacs') diff --git a/emacs/custom/fcuny-eshell.el b/emacs/custom/fcuny-eshell.el index 6d3b1c1..a3cf178 100644 --- a/emacs/custom/fcuny-eshell.el +++ b/emacs/custom/fcuny-eshell.el @@ -67,6 +67,36 @@ multiple eshell windows easier." (insert (concat "ls")) (eshell-send-input))) +(defvar-local fcuny/eshell-output-buffer "*Exported eshell output*" + "Name of buffer with the last output of Eshell command.") + +(defvar-local fcuny/eshell-output-delimiter "---" + "Delimiter for successive `prot-eshell-export' outputs.") + +(defun fcuny/eshell--command-prompt-output () + "Capture last command prompt and its output." + (let ((beg (save-excursion + (goto-char (eshell-beginning-of-input)) + (goto-char (point-at-bol))))) + (when (derived-mode-p 'eshell-mode) + (buffer-substring-no-properties beg (eshell-end-of-output))))) + +;; https://gitlab.com/protesilaos/dotfiles/-/blob/master/emacs/.emacs.d/prot-lisp/prot-eshell.el#L114 +(defun fcuny/eshell-export () + "Produce a buffer with output of the last Eshell command. +If `fcuny/eshell-output-buffer' does not exist, create it. Else +append to it, while separating multiple outputs with +`fcuny/eshell-output-delimiter'." + (interactive) + (let ((eshell-output (fcuny/eshell--command-prompt-output))) + (with-current-buffer (get-buffer-create fcuny/eshell-output-buffer) + (goto-char (point-max)) + (unless (eq (point-min) (point-max)) + (insert (format "\n%s\n\n" fcuny/eshell-output-delimiter))) + (goto-char (point-at-bol)) + (insert eshell-output) + (switch-to-buffer-other-window (current-buffer))))) + ;; eshell time and notification (defvar-local eshell-current-command-start-time nil) @@ -93,7 +123,8 @@ multiple eshell windows easier." (eshell-mode . fcuny/eshell-current-command-time-track) (eshell-mode . eshell-smart-initialize)) :commands (eshell eshell-command) - :bind (("C-c e h" . fcuny/eshell-here)) + :bind (("C-c e h" . fcuny/eshell-here) + ("C-c e e" . fcuny/eshell-export)) :custom (eshell-scroll-to-bottom-on-input 'all) (eshell-error-if-no-glob t) -- cgit 1.4.1