summary refs log tree commit diff
path: root/emacs
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2021-10-17 11:50:57 -0700
committerFranck Cuny <franck@fcuny.net>2021-10-17 11:50:57 -0700
commit8ea5ace578d5fa7c4673c28079291339370634b7 (patch)
tree58d9d9454e843019420aec7d9c34732903f83a5d /emacs
parentemacs: set `eshell-cd-on-directory` to true (diff)
downloademacs.d-8ea5ace578d5fa7c4673c28079291339370634b7.tar.gz
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
Diffstat (limited to 'emacs')
-rw-r--r--emacs/custom/fcuny-eshell.el33
1 files changed, 32 insertions, 1 deletions
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)