2019-02-26 10:41:59 +01:00
|
|
|
|
;;; oni-eshell.el --- Eshell configuration -*- lexical-binding: t; -*-
|
|
|
|
|
|
|
|
|
|
;; Copyright (C) 2019 Tom Willemse
|
|
|
|
|
|
|
|
|
|
;; Author: Tom Willemse <tom@ryuslash.org>
|
|
|
|
|
;; Keywords: local
|
2023-04-16 08:10:50 +02:00
|
|
|
|
;; Version: 2023.0414.233822
|
2020-09-08 09:31:06 +02:00
|
|
|
|
;; Package-Requires: (eshell-fringe-status esh-autosuggest xterm-color eshell-syntax-highlighting)
|
2019-02-26 10:41:59 +01:00
|
|
|
|
|
|
|
|
|
;; 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)
|
2019-09-20 02:33:38 +02:00
|
|
|
|
|
2020-10-26 21:36:09 +01:00
|
|
|
|
(require 'em-dirs)
|
2019-02-26 10:41:59 +01:00
|
|
|
|
(require 'em-prompt)
|
2019-05-31 16:48:35 +02:00
|
|
|
|
(require 'esh-autosuggest)
|
2019-09-20 02:33:38 +02:00
|
|
|
|
(require 'xterm-color)
|
2019-02-26 10:41:59 +01:00
|
|
|
|
|
|
|
|
|
(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))
|
|
|
|
|
|
2019-09-20 18:34:43 +02:00
|
|
|
|
(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)))
|
|
|
|
|
|
2020-11-07 01:40:57 +01:00
|
|
|
|
(defun oni-eshell--expand-keymap ()
|
2019-02-26 10:41:59 +01:00
|
|
|
|
"Set `C-d' to quit eshell if used at end of prompt."
|
2020-11-07 01:40:57 +01:00
|
|
|
|
(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))
|
2019-02-26 10:41:59 +01:00
|
|
|
|
|
2019-09-20 02:33:38 +02:00
|
|
|
|
(defun oni-eshell--set-xterm-variables ()
|
|
|
|
|
"Set ‘xterm-color-preserve-properties’ to t."
|
|
|
|
|
(setq xterm-color-preserve-properties t))
|
|
|
|
|
|
2020-06-15 19:32:45 +02:00
|
|
|
|
(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))
|
|
|
|
|
|
2020-10-26 21:36:09 +01:00
|
|
|
|
(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))
|
2020-12-11 06:30:37 +01:00
|
|
|
|
|
2021-02-28 02:28:16 +01:00
|
|
|
|
(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))
|
|
|
|
|
|
2023-04-16 08:10:50 +02:00
|
|
|
|
(defun oni-eshell-change-font ()
|
|
|
|
|
"Remap the default font to the one I use for terminals."
|
|
|
|
|
(face-remap-add-relative 'default :family "Classic Console Neue"))
|
|
|
|
|
|
|
|
|
|
(defun oni-eshell-set-page-delimiter ()
|
|
|
|
|
"Change the page delimiter so that it matches the prompt for easy navigation."
|
|
|
|
|
(setq-local page-delimiter eshell-prompt-regexp))
|
|
|
|
|
|
2020-09-08 09:31:06 +02:00
|
|
|
|
(add-hook 'eshell-before-prompt-hook #'oni-eshell--set-xterm-variables)
|
2020-11-07 01:40:57 +01:00
|
|
|
|
(add-hook 'eshell-first-time-mode-hook #'oni-eshell--expand-keymap)
|
2020-09-08 09:31:06 +02:00
|
|
|
|
(add-hook 'eshell-load-hook #'oni-eshell--disable-ansi-color-handling)
|
2019-02-26 10:41:59 +01:00
|
|
|
|
(add-hook 'eshell-load-hook #'oni-eshell--enable-truncating-buffers)
|
2019-09-20 18:34:43 +02:00
|
|
|
|
(add-hook 'eshell-load-hook #'oni-eshell--enable-xterm-filter)
|
2023-04-16 08:10:50 +02:00
|
|
|
|
(add-hook 'eshell-mode-hook #'oni-eshell-change-font)
|
2021-02-28 02:28:16 +01:00
|
|
|
|
(add-hook 'eshell-mode-hook #'oni-eshell-disable-beacon-on-scroll)
|
2023-04-16 08:10:50 +02:00
|
|
|
|
(add-hook 'eshell-mode-hook #'oni-eshell-set-page-delimiter)
|
2019-05-31 16:48:35 +02:00
|
|
|
|
(add-hook 'eshell-mode-hook 'esh-autosuggest-mode)
|
2020-09-08 09:31:06 +02:00
|
|
|
|
(add-hook 'eshell-mode-hook 'eshell-syntax-highlighting-mode)
|
2019-02-26 10:41:59 +01:00
|
|
|
|
(add-hook 'eshell-mode-hook 'goto-address-mode)
|
|
|
|
|
|
2020-06-15 19:32:45 +02:00
|
|
|
|
(add-hook 'esh-autosuggest-mode-hook #'oni-eshell-fix-esh-autosuggest-active-keymap)
|
|
|
|
|
|
2019-05-31 16:49:43 +02:00
|
|
|
|
(when (display-graphic-p)
|
|
|
|
|
(add-hook 'eshell-mode-hook 'eshell-fringe-status-mode))
|
2019-02-26 11:19:53 +01:00
|
|
|
|
|
2019-09-20 02:33:38 +02:00
|
|
|
|
(setenv "TERM" "xterm-256color")
|
|
|
|
|
|
2019-09-09 01:53:11 +02:00
|
|
|
|
(add-to-list 'display-buffer-alist
|
2022-12-04 16:36:54 +01:00
|
|
|
|
'("\\`\\*eshell" display-buffer-at-bottom
|
2019-09-09 01:53:11 +02:00
|
|
|
|
(side . bottom)
|
|
|
|
|
(slot . 0)
|
|
|
|
|
(window-height . 0.33)))
|
|
|
|
|
|
2023-04-16 08:10:50 +02:00
|
|
|
|
;;; Eshell prompt
|
|
|
|
|
|
|
|
|
|
(defun oni-eshell-shortest-unique-directory (current-path directory)
|
|
|
|
|
"Find the shortest unique substring of DIRECTORY.
|
|
|
|
|
DIRECTORY should be a directory that exists within CURRENT-PATH."
|
|
|
|
|
(catch 'result
|
|
|
|
|
(dotimes (i (length directory))
|
|
|
|
|
(let* ((current-directory (substring directory 0 (1+ i)))
|
|
|
|
|
(dir-rx (rx string-start
|
|
|
|
|
(literal current-directory)
|
|
|
|
|
(zero-or-more any)))
|
|
|
|
|
(matches (directory-files current-path nil dir-rx)))
|
|
|
|
|
(when (= (length matches) 1)
|
|
|
|
|
(throw 'result current-directory))))))
|
|
|
|
|
|
|
|
|
|
(defun oni-eshell-shorten-directory (directory)
|
|
|
|
|
"Shorten DIRECTORY to the shortest unique names of each directory."
|
|
|
|
|
(let ((current-path "/")
|
|
|
|
|
(current-short-path "/")
|
|
|
|
|
(home (concat (getenv "HOME") "/"))
|
|
|
|
|
(components (string-split (expand-file-name directory) "/" t)))
|
|
|
|
|
(dolist (dir components current-short-path)
|
|
|
|
|
(let* ((shortened (propertize (oni-eshell-shortest-unique-directory current-path dir)
|
|
|
|
|
'help-echo dir))
|
|
|
|
|
(new-path (format "%s%s/" current-path dir))
|
|
|
|
|
(new-short-path (if (string= new-path home)
|
|
|
|
|
"~/"
|
|
|
|
|
(format "%s%s/" current-short-path shortened))))
|
|
|
|
|
(setq current-path new-path
|
|
|
|
|
current-short-path new-short-path)))))
|
|
|
|
|
|
|
|
|
|
(defun oni-eshell-show-perforce-info-p ()
|
|
|
|
|
"Predicate to indicate whether or not powershell info should be shown."
|
|
|
|
|
(locate-dominating-file "." ".p4config"))
|
|
|
|
|
|
|
|
|
|
(defun oni-eshell-perforce-workspace ()
|
|
|
|
|
"Function returning the current Perforce workspace."
|
|
|
|
|
(car (map-elt (mapcar (lambda (str) (split-string str ": "))
|
|
|
|
|
(split-string (shell-command-to-string "p4 info -s") "\n"))
|
|
|
|
|
"Client name")))
|
|
|
|
|
|
|
|
|
|
(defun oni-eshell-perforce-root ()
|
|
|
|
|
"Function returning the root directory of the current Perforce workspace."
|
|
|
|
|
(string-replace
|
|
|
|
|
"\\"
|
|
|
|
|
"/"
|
|
|
|
|
(car (map-elt (mapcar (lambda (str) (split-string str ": "))
|
|
|
|
|
(split-string (shell-command-to-string "p4 info") "\n"))
|
|
|
|
|
"Client root"))))
|
|
|
|
|
|
|
|
|
|
(defun oni-eshell-perforce-stream ()
|
|
|
|
|
"Function returning the current Perforce stream."
|
|
|
|
|
(string-trim-right (shell-command-to-string "p4 switch")))
|
|
|
|
|
|
|
|
|
|
(defun oni-eshell-prompt-function ()
|
|
|
|
|
"Construct a prompt string for Eshell."
|
|
|
|
|
(let* ((perforcep (oni-eshell-show-perforce-info-p))
|
|
|
|
|
(pwd (eshell/pwd))
|
|
|
|
|
(dir (if perforcep
|
|
|
|
|
(concat (propertize (oni-eshell-perforce-workspace)
|
|
|
|
|
'face '((foreground-color . "#ca3cad90828e")))
|
|
|
|
|
":"
|
|
|
|
|
(let ((relative-path (string-remove-prefix (oni-eshell-perforce-root) pwd)))
|
|
|
|
|
(if (string= relative-path "") "/" relative-path)))
|
|
|
|
|
(oni-eshell-shorten-directory pwd)))
|
|
|
|
|
(stream (if perforcep (concat " ("
|
|
|
|
|
(propertize (oni-eshell-perforce-stream)
|
|
|
|
|
'face '((foreground-color . "#90e4ca3c828e")))
|
|
|
|
|
")")
|
|
|
|
|
"")))
|
|
|
|
|
(concat dir
|
|
|
|
|
stream
|
|
|
|
|
(if (= (user-uid) 0) " # " " $ "))))
|
|
|
|
|
|
|
|
|
|
(setq eshell-prompt-function #'oni-eshell-prompt-function)
|
|
|
|
|
|
2019-02-26 10:41:59 +01:00
|
|
|
|
(provide 'oni-eshell)
|
|
|
|
|
;;; oni-eshell.el ends here
|