summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--init.org56
-rw-r--r--lib/eos/jao-eshell-here.el99
2 files changed, 106 insertions, 49 deletions
diff --git a/init.org b/init.org
index 6d984fe..025bd53 100644
--- a/init.org
+++ b/init.org
@@ -1949,55 +1949,13 @@
#+END_SRC
***** Toggle
#+begin_src emacs-lisp
- (defun jao-eshell-here--find-window (b)
- (when-let (w (seq-find (lambda (w) (eq (window-buffer w) b))
- (window-list)))
- (select-window w)))
-
- (defun jao-eshell-frame-buffer (&optional b)
- (let ((p 'jao-eshell-buffer))
- (if b (set-frame-parameter nil p b) (frame-parameter nil p))))
-
- (defun jao-eshell--cd-here (&optional dir)
- (eshell-kill-input)
- (eshell/cd (or dir default-directory))
- (insert "\n")
- (eshell-send-input))
-
- (defun jao-eshell-here (&optional stay)
- (interactive "p")
- (message "%s" stay)
- (let ((b (jao-eshell-frame-buffer))
- (dir (cond ((> stay 4) default-directory)
- ((> stay 1)
- (project-root
- (or (project-current)
- `(transient . ,default-directory)))))))
- (jao-with-attached-buffer "^\\*eshell" 30
- (if (buffer-live-p b)
- (progn (pop-to-buffer b nil t)
- (eshell-save-some-history)
- (when dir (jao-eshell--cd-here dir)))
- (let ((default-directory (or dir default-directory)))
- (eshell))
- (jao-eshell-frame-buffer (current-buffer))))))
-
- (defun eshell/x ()
- (when (derived-mode-p 'eshell-mode)
- (when (fboundp 'eshell-autojump-save)
- (eshell-autojump-save))
- (eshell-save-some-history)
- (if (> (frame-height) (window-height))
- (delete-window)
- (bury-buffer))))
-
- (defun jao-eshell-here-toggle (stay)
- (interactive "p")
- (if (eq (current-buffer) (jao-eshell-frame-buffer))
- (eshell/x)
- (jao-eshell-here stay)))
-
- (global-set-key [(f1)] #'jao-eshell-here-toggle)
+
+ (use-package jao-eshell-here
+ :demand t
+ :config (jao-define-attached-buffer "^\\*eshell" 0.5)
+ :bind (("<f1>" . jao-eshell-here-toggle)
+ ("C-<f1>" . jao-eshell-here-toggle-new)))
+
#+end_src
***** Workarounds
#+begin_src emacs-lisp
diff --git a/lib/eos/jao-eshell-here.el b/lib/eos/jao-eshell-here.el
new file mode 100644
index 0000000..cf29e31
--- /dev/null
+++ b/lib/eos/jao-eshell-here.el
@@ -0,0 +1,99 @@
+;;; jao-eshell-here.el --- Easy opening of eshell buffers -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2021 jao
+
+;; Author: jao <mail@jao.io>
+;; Keywords: eshell
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Commands for toggling eshell buffers below the current one.
+
+;;; Code:
+
+(require 'eshell)
+
+(defvar jao-eshell-here-display-buffer-alist
+ '(("^\\*eshell"
+ (display-buffer-reuse-window display-buffer-below-selected)
+ (window-height . 0.5)))
+ "Value to use for `display-buffer-alist' when displaying the eshell buffer.")
+
+(defun jao-eshell-here--find-window (b)
+ (when-let (w (seq-find (lambda (w) (eq (window-buffer w) b))
+ (window-list)))
+ (select-window w)))
+
+(defun jao-eshell-here--frame-buffer (&optional b)
+ (let ((p 'jao-eshell-buffer))
+ (if b (set-frame-parameter nil p b) (frame-parameter nil p))))
+
+(defun jao-eshell--cd-here (&optional dir)
+ (eshell-kill-input)
+ (eshell/cd (or dir default-directory))
+ (insert "\n")
+ (eshell-send-input))
+
+(defun jao-eshell-here (stay &optional force-new)
+ "Open an eshell buffer below current buffer.
+Buffers are reused (per frame) unless FORCE-NEW is t. With prefix
+STAY (C-u), open at project's root, and with double prefix (C-u
+C-u) open in the current's buffer default dir."
+ (interactive "p")
+ (let ((b (unless force-new (jao-eshell-here--frame-buffer)))
+ (dir (cond ((> stay 4) default-directory)
+ ((> stay 1)
+ (project-root
+ (or (project-current)
+ `(transient . ,default-directory))))))
+ (display-buffer-alist
+ (append display-buffer-alist jao-eshell-here-display-buffer-alist)))
+ (jao-with-attached-buffer "^\\*eshell" 0.5
+ (if (buffer-live-p b)
+ (progn (pop-to-buffer b nil t)
+ (eshell-save-some-history)
+ (when dir (jao-eshell--cd-here dir)))
+ (let ((default-directory (or dir default-directory)))
+ (eshell (when force-new 4)))
+ (unless force-new (jao-eshell-here--frame-buffer (current-buffer)))))))
+
+;;;###autoload
+(defun eshell/x ()
+ (when (derived-mode-p 'eshell-mode)
+ (when (fboundp 'eshell-autojump-save)
+ (eshell-autojump-save))
+ (eshell-save-some-history)
+ (if (> (frame-height) (window-height))
+ (delete-window)
+ (bury-buffer))))
+
+;;;###autoload
+(defun jao-eshell-here-toggle (stay &optional force-new)
+ "Open or close an eshell buffer, using `jao-eshell-here'."
+ (interactive "p")
+ (if (or (and force-new (derived-mode-p 'eshell-mode))
+ (eq (current-buffer) (jao-eshell-here--frame-buffer)))
+ (eshell/x)
+ (jao-eshell-here stay force-new)))
+
+;;;###autoload
+(defun jao-eshell-here-toggle-new (stay)
+ "Open or close a new eshell buffer, using `jao-eshell-here'."
+ (interactive "p")
+ (jao-eshell-here-toggle stay t))
+
+(provide 'jao-eshell-here)
+;;; jao-eshell-here.el ends here