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