aboutsummaryrefslogtreecommitdiffstats
path: root/oni-eshell.el
blob: bfbd3cd079075caf3d97c1eafb0bacbb85ee3db0 (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
;;; oni-eshell.el --- Eshell configuration           -*- lexical-binding: t; -*-

;; Copyright (C) 2019  Tom Willemse

;; Author: Tom Willemse <tom@ryuslash.org>
;; Keywords: local
;; Version: 2021.1123.002814
;; Package-Requires: (eshell-fringe-status esh-autosuggest xterm-color eshell-syntax-highlighting)

;; 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:

;; Eshell configuration.

;;; Code:

(require 'eshell)

(require 'em-dirs)
(require 'em-prompt)
(require 'esh-autosuggest)
(require 'xterm-color)

(defun oni-eshell--C-d ()
  "Call `delete-char' or close the buffer if it fails."
  (interactive)
  (condition-case err
      (call-interactively #'delete-char)
    (error (if (and (eq (car err) 'end-of-buffer)
                    (looking-back eshell-prompt-regexp nil))
               (kill-buffer)
             (signal (car err) (cdr err))))))

(defun oni-eshell--enable-truncating-buffers ()
  "Add `eshell-truncate-buffer' to `eshell-output-filter-functions'."
  (add-to-list 'eshell-output-filter-functions 'eshell-truncate-buffer))

(defun oni-eshell--enable-xterm-filter ()
  "Add ‘xterm-color-filter’ to ‘eshell-preoutput-filter-functions’."
  (add-to-list 'eshell-preoutput-filter-functions 'xterm-color-filter))

(defun oni-eshell--disable-ansi-color-handling ()
  "Remove ‘eshell-handle-ansi-color’ from ‘eshell-output-filter-functions’."
  (setq eshell-output-filter-functions
        (remove 'eshell-handle-ansi-color eshell-output-filter-functions)))

(defun oni-eshell--expand-keymap ()
  "Set `C-d' to quit eshell if used at end of prompt."
  (define-key eshell-mode-map (kbd "C-d") #'oni-eshell--C-d)
  (define-key eshell-mode-map (kbd "C-c b") #'oni-eshell-goto-buffer-directory))

(defun oni-eshell--set-xterm-variables ()
  "Set ‘xterm-color-preserve-properties’ to t."
  (setq xterm-color-preserve-properties t))

(defun oni-eshell-fix-esh-autosuggest-active-keymap ()
  "Set ‘company-posframe-active-map’ to ‘esh-autosuggest-active-map’."
  (setq-local company-posframe-active-map esh-autosuggest-active-map))

(defun oni-eshell-goto-buffer-directory (buffer-name)
  "Change the current directory to the given BUFFER-NAME’s directory."
  (interactive
   (list (read-buffer "Switch to buffer’s directory: "
                      nil t (lambda (buf)
                              (let ((b (if (consp buf)
                                           (cdr buf)
                                         (get-buffer buf))))
                                (buffer-file-name b))))))
  (eshell/cd (file-name-directory (buffer-file-name (get-buffer buffer-name))))
  (eshell-reset))

(defun oni-eshell-shorten-directories (path)
  "Shorten PATH to a list of unique shortened directory names."
  (let* ((directories (split-string path "/" t))
         (new-path
          (string-join
           (mapcar (lambda (d)
                     (let ((name (substring d 0 1))
                           (length 1))
                       (while (cl-find-if (lambda (s) (and (not (string= s d))
                                                           (string-prefix-p name s)))
                                          directories)
                         (setq name (substring d 0 (cl-incf length))))
                       name))
                   directories) "/")))
    (if (string-prefix-p "~" new-path)
        new-path
      (concat "/" new-path))))

(defun oni-eshell-disable-beacon-on-scroll ()
  "Disable ‘beacon-blink-when-window-scrolls’ in the current buffer."
  (setq-local beacon-blink-when-window-scrolls nil))

(add-hook 'eshell-before-prompt-hook #'oni-eshell--set-xterm-variables)
(add-hook 'eshell-first-time-mode-hook #'oni-eshell--expand-keymap)
(add-hook 'eshell-load-hook #'oni-eshell--disable-ansi-color-handling)
(add-hook 'eshell-load-hook #'oni-eshell--enable-truncating-buffers)
(add-hook 'eshell-load-hook #'oni-eshell--enable-xterm-filter)
(add-hook 'eshell-mode-hook #'oni-eshell-disable-beacon-on-scroll)
(add-hook 'eshell-mode-hook 'esh-autosuggest-mode)
(add-hook 'eshell-mode-hook 'eshell-syntax-highlighting-mode)
(add-hook 'eshell-mode-hook 'goto-address-mode)

(add-hook 'esh-autosuggest-mode-hook #'oni-eshell-fix-esh-autosuggest-active-keymap)

(when (display-graphic-p)
  (add-hook 'eshell-mode-hook 'eshell-fringe-status-mode))

(setenv "TERM" "xterm-256color")

(add-to-list 'display-buffer-alist
             '("\\`\\*eshell" display-buffer-in-side-window
               (side . bottom)
               (slot . 0)
               (window-height . 0.33)))

(provide 'oni-eshell)
;;; oni-eshell.el ends here