summary refs log tree commit diff
path: root/emacs/custom/fcuny-eshell.el
blob: 4b39d6f99000c088ed523f0cd8d683185ec7ee4d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(defun fcuny/eshell-mode-setup ()
  (eshell/alias "e" "find-file $1")
  (eshell/alias "emacs" "find-file $1")
  (eshell/alias "ee" "find-file-other-window $1")

  (eshell/alias "gs" "magit-status")
  (eshell/alias "gd" "magit-diff-unstaged")
  (eshell/alias "gds" "magit-diff-staged")
  (eshell/alias "d" "dired $1"))

(defun eshell/gst (&rest args)
  (magit-status (pop args) nil)
  (eshell/echo))   ;; The echo command suppresses output

(defun fcuny/eshell-here ()
  "Opens up a new shell in the directory associated with the current
buffer's file. The eshell is renamed to match that directory to make
multiple eshell windows easier."
  (interactive)
  (let* ((parent (if (buffer-file-name)
                     (file-name-directory (buffer-file-name))
                   default-directory))
         (height (/ (window-total-height) 3))
         (name (car (last (split-string parent "/" t)))))
    (split-window-vertically (- height))
    (other-window 1)
    (eshell "new")
    (rename-buffer (concat "*eshell: " name "*"))
    (insert (concat "ls"))
    (eshell-send-input)))

;; eshell time and notification
(defvar-local eshell-current-command-start-time nil)

;; https://www.birkey.co/2021-06-20-why-eshell-part-1.html
(defun fcuny/eshell-current-command-start ()
  (setq eshell-current-command-start-time (current-time)))

(defun fcuny/eshell-current-command-stop ()
  (when eshell-current-command-start-time
    (let ((elapsed-time (float-time
			             (time-subtract (current-time)
					                    eshell-current-command-start-time))))
      (if (> elapsed-time 5)
          (eshell-interactive-print
	       (format "Finished in: %.0fs\n" elapsed-time)))))
    (setq eshell-current-command-start-time nil))

(defun fcuny/eshell-current-command-time-track ()
  (add-hook 'eshell-pre-command-hook #'fcuny/eshell-current-command-start nil t)
  (add-hook 'eshell-post-command-hook #'fcuny/eshell-current-command-stop nil t))

(use-package eshell
  :hook ((eshell-mode . fcuny/eshell-mode-setup)
         (eshell-mode . fcuny/eshell-current-command-time-track)
         (eshell-mode . eshell-smart-initialize))
  :bind (("C-c e h" . fcuny/eshell-here))
  :init
  (require 'em-smart)
  (require 'em-hist)
  (require 'em-tramp)
  :custom
  (eshell-scroll-to-bottom-on-input 'all)
  (eshell-error-if-no-glob t)
  (eshell-hist-ignoredups t)
  (eshell-save-history-on-exit t)
  (eshell-prefer-lisp-functions nil)
  (eshell-where-to-jump 'begin)
  (eshell-review-quick-commands nil)
  (eshell-smart-space-goes-to-end t)
  (eshell-destroy-buffer-when-process-dies t))

(provide 'fcuny-eshell)