summary refs log tree commit diff stats
path: root/lisp/acdw-shell.el
blob: ce63bdcb810661906bfea3abffa19d51eaf13f9b (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
;;; acdw-shell.el ---Shell config -*- lexical-binding: t; -*-

;;; Code:

(defvar eshell-buffer-format "*eshell:%s*"
  "Format for eshell buffer names.")

(defun eshell-rename-pwd ()
  (rename-buffer (format eshell-buffer-format default-directory) t))

(defun eshell-last-dir ()
  (goto-char (point-max))
  (insert "cd -")
  (eshell-send-input))

(defun eshellp (buffer-or-name)
  (with-current-buffer buffer-or-name
    (derived-mode-p 'eshell-mode)))

(defun +eshell (&optional new)
  (interactive "P")
  (let ((dir default-directory)
        (bname (format eshell-buffer-format default-directory))
        (display-comint-buffer-action 'pop-to-buffer))
    (if-let ((buf (and (not new)
                       (or (get-buffer bname)
                           (seq-find #'eshellp
                                     (reverse (buffer-list)))))))
        (pop-to-buffer buf)
      (eshell new))
    (eshell-rename-pwd)
    (unless (equal default-directory dir)
      (eshell/cd dir)
      (eshell-send-input)
      (goto-char (point-max)))))

(defun +eshell-quit (&optional choose)
  (interactive "P")
  (if choose
      (let* ((bufs (mapcar #'buffer-name
                           (seq-filter #'eshellp
                                       (buffer-list))))
             (buf (get-buffer
                   (completing-read "Eshell: "
                                    bufs nil t nil nil (car bufs)))))
        (quit-window)
        (pop-to-buffer buf))
    (quit-window)))

(defun acdw/eshell-prompt ()
  "My custom eshell prompt."
  (concat (if (= 0 eshell-last-command-status)
              "^_^ "
            ";_; ")
          (abbreviate-file-name (eshell/pwd))
          (if (= (user-uid) 0) " # " " $ ")))


;;; Packages

(use-package eshell
  :init
  (add-hook 'eshell-post-command-hook #'eshell-rename-pwd)
  (setopt eshell-modules-list
          '(eshell-alias
            eshell-basic
            eshell-cmpl
            eshell-dirs
            eshell-elecslash
            eshell-hist
            eshell-ls
            eshell-prompt
            eshell-smart
            eshell-extpipe
            eshell-glob
            eshell-hist
            eshell-ls
            eshell-pred
            eshell-prompt
            eshell-script
            eshell-term
            eshell-unix))
  :commands eshell
  :bind (("C-z" . +eshell)
         :map eshell-mode-map
         ("C-z" . +eshell-quit)
         ("C-o" . eshell-last-dir))
  :config
  (require 'esh-module)
  (require 'em-smart)
  (require 'em-tramp)
  (setq eshell-destroy-buffer-when-process-dies t
        eshell-error-if-no-glob t
        eshell-hist-ignoredups t
        eshell-kill-on-exit t
        eshell-prefer-lisp-functions t
        eshell-prefer-lisp-variables t
        eshell-scroll-to-bottom-on-input 'this
        eshell-banner-message ""
        eshell-hist-ignoredups 'erase
        eshell-history-size 512
        eshell-input-filter (lambda (input)
                              (or (eshell-input-filter-default input)
                                  (eshell-input-filter-initial-space input)))
        eshell-prompt-function #'acdw/eshell-prompt)
  (add-hook 'eshell-mode-hook
            (defun eshell-setup ()
              (hungry-delete-mode -1)
              (setq-local outline-regexp eshell-prompt-regexp
                          page-delimiter eshell-prompt-regexp
                          imenu-generic-expression
                          '(("Prompt" " $ \\(.*\\)" 1))
                          truncate-lines t)
              (setenv "PAGER" "cat"))))

(use-package eat
  :ensure t
  :hook (eshell-load-hook . eat-eshell-mode))

(use-package exec-path-from-shell
  :when (eq system-type 'gnu/linux)
  :ensure t
  :config
  (add-to-list 'exec-path-from-shell-variables "SSH_AUTH_SOCK")
  (add-to-list 'exec-path-from-shell-variables "SSH_AGENT_PID")
  (add-to-list 'exec-path-from-shell-variables "GPG_AGENT_INFO")
  (add-to-list 'exec-path-from-shell-variables "LANG")
  (add-to-list 'exec-path-from-shell-variables "LC_CTYPE")
  (add-to-list 'exec-path-from-shell-variables "XDG_CONFIG_HOME")
  (add-to-list 'exec-path-from-shell-variables "XDG_CONFIG_DIRS")
  (add-to-list 'exec-path-from-shell-variables "XDG_DATA_HOME")
  (add-to-list 'exec-path-from-shell-variables "XDG_DATA_DIRS")
  (add-to-list 'exec-path-from-shell-variables "XDG_CACHE_HOME")
  (exec-path-from-shell-initialize))

(use-package eshell-bookmark
  :ensure t
  :hook (eshell-mode-hook . eshell-bookmark-setup))

(provide 'acdw-shell)
;;; acdw-shell.el ends here