emacs - commit all stale emacs config
[dotfiles.git] / emacs.d / config / init-eshell.el
1
2 ;;(add-hook 'eshell-mode-hook
3 ;;        (lambda ()
4 ;;          (linum-mode -1)
5 ;;          (highlight-current-line-on nil)))
6
7 (setenv "PATH"
8         (concat
9          "/usr/local/bin:/usr/local/sbin:"
10          (getenv "PATH")))
11
12 (setq eshell-scroll-to-bottom-on-input t)
13
14
15 ;; copied from howardabrams's config
16 (defun eshell/gst (&rest args)
17     (magit-status (pop args) nil)
18     (eshell/echo))  
19
20
21
22 (add-hook 'eshell-mode-hook
23    (lambda ()
24       (add-to-list 'eshell-visual-commands "ssh")
25       (add-to-list 'eshell-visual-commands "tail")))
26
27 ; copied from howard's github
28 (defun curr-dir-git-branch-string (pwd)
29   "Returns current git branch as a string, or the empty string if
30 PWD is not in a git repo (or the git command is not found)."
31   (interactive)
32   (when (and (eshell-search-path "git")
33              (locate-dominating-file pwd ".git"))
34     (let ((git-output (shell-command-to-string (concat "cd " pwd " && git branch | grep '\\*' | sed -e 's/^\\* //'"))))
35       (if (> (length git-output) 0)
36           (concat " :" (substring git-output 0 -1))
37         "(no branch)"))))
38
39
40
41
42 (defun pwd-replace-home (pwd)
43   "Replace home in PWD with tilde (~) character."
44   (interactive)
45   (let* ((home (expand-file-name (getenv "HOME")))
46          (home-len (length home)))
47     (if (and
48          (>= (length pwd) home-len)
49          (equal home (substring pwd 0 home-len)))
50         (concat "~" (substring pwd home-len))
51       pwd)))
52
53
54
55
56 (defun pwd-shorten-dirs (pwd)
57   "Shorten all directory names in PWD except the last two."
58   (let ((p-lst (split-string pwd "/")))
59     (if (> (length p-lst) 2)
60         (concat
61          (mapconcat (lambda (elm) (if (zerop (length elm)) ""
62                                (substring elm 0 1)))
63                     (butlast p-lst 2)
64                     "/")
65          "/"
66          (mapconcat (lambda (elm) elm)
67                     (last p-lst 2)
68                     "/"))
69       pwd  ;; Otherwise, we just return the PWD
70       )))
71
72 ;; Turn off the default prompt.
73 (setq eshell-highlight-prompt nil)
74
75
76
77
78 (defun split-directory-prompt (directory)
79   (if (string-match-p ".*/.*" directory)
80       (list (file-name-directory directory) (file-name-base directory))
81     (list "" directory)))
82
83
84 (setq eshell-prompt-function
85       (lambda ()
86         (let* ((directory (split-directory-prompt (pwd-shorten-dirs (pwd-replace-home (eshell/pwd)))))
87                (parent (car directory))
88                (name (cadr directory))
89                (branch (or (curr-dir-git-branch-string (eshell/pwd)) "")))
90
91           (if (eq 'dark (frame-parameter nil 'background-mode))
92               (concat   ;; Prompt for Dark Themes
93                (propertize parent 'face `(:foreground "#8888FF"))
94                (propertize name   'face `(:foreground "#8888FF" :weight bold))
95                (propertize branch 'face `(:foreground "green"))
96                (propertize " $"   'face `(:weight ultra-bold))
97                (propertize " "    'face `(:weight bold)))
98
99             (concat    ;; Prompt for Light Themes
100              (propertize parent 'face `(:foreground "blue"))
101              (propertize name   'face `(:foreground "blue" :weight bold))
102              (propertize branch 'face `(:foreground "dark green"))
103              (propertize " $"   'face `(:weight ultra-bold))
104              (propertize " "    'face `(:weight bold)))))))
105
106
107
108 (setq eshell-highlight-prompt nil)
109
110
111
112 (when (require 'esh-buf-stack nil t)
113   (setup-eshell-buf-stack)
114   (add-hook 'eshell-mode-hook
115             (lambda () (local-set-key (kbd "M-q") 'eshell-push-command))))
116
117 (defun eshell/x ()
118   "Closes the EShell session and gets rid of the EShell window."
119   (kill-buffer)
120   (delete-window))
121
122
123 (defun eshell-here ()
124   "Opens up a new shell in the directory associated with the
125 current buffer's file. The eshell is renamed to match that
126 directory to make multiple eshell windows easier."
127   (interactive)
128   (let* ((parent (if (buffer-file-name)
129                      (file-name-directory (buffer-file-name))
130                    default-directory))
131          (height (/ (window-total-height) 3))
132          (name   (car (last (split-string parent "/" t)))))
133     (split-window-vertically (- height))
134     (other-window 1)
135     (eshell "new")
136     (rename-buffer (concat "*eshell: " name "*"))
137
138     (insert (concat "ls"))
139     (eshell-send-input)))
140
141 (global-set-key (kbd "C-!") 'eshell-here)
142
143
144
145 (add-hook 'eshell-mode-hook
146      (lambda ()
147        (local-set-key (kbd "M-P") 'eshell-previous-prompt)
148        (local-set-key (kbd "M-N") 'eshell-next-prompt)
149        (local-set-key (kbd "M-R") 'eshell-list-history)
150        (local-set-key (kbd "M-r")
151               (lambda ()
152                 (interactive)
153                 (insert
154                  (ido-completing-read "Eshell history: "
155                                       (delete-dups
156                                        (ring-elements eshell-history-ring))))))))
157
158
159
160
161 (require 'em-smart)
162 (setq eshell-where-to-jump 'begin)
163 (setq eshell-review-quick-commands nil)
164 (setq eshell-smart-space-goes-to-end t)
165
166
167
168 (defun execute-command-on-file-buffer (cmd)
169   (interactive "sCommand to execute: ")
170   (let* ((file-name (buffer-file-name))
171          (full-cmd (concat cmd " " file-name)))
172     (shell-command full-cmd)))
173
174 (defun execute-command-on-file-directory (cmd)
175   (interactive "sCommand to execute: ")
176   (let* ((dir-name (file-name-directory (buffer-file-name)))
177          (full-cmd (concat "cd " dir-name "; " cmd)))
178     (shell-command full-cmd)))
179
180 (global-set-key (kbd "A-1") 'execute-command-on-file-buffer)
181 (global-set-key (kbd "A-!") 'execute-command-on-file-directory)
182
183
184
185
186
187 ;; alias
188
189 (defalias 'e 'find-file)
190 (defalias 'ee 'find-file-other-window)
191
192
193
194 (provide 'init-eshell)