eshell-buttonize.el (2244B)
1 ;;; eshell-buttonize.el --- Create useful buttons in eshell -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2020 Jamie Beardslee 4 5 ;; Author: Jamie Beardslee <jdb@jamzattack.xyz> 6 ;; Keywords: unix, convenience, eshell, buttons 7 8 ;; This program is free software; you can redistribute it and/or modify 9 ;; it under the terms of the GNU General Public License as published by 10 ;; the Free Software Foundation, either version 3 of the License, or 11 ;; (at your option) any later version. 12 13 ;; This program is distributed in the hope that it will be useful, 14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 ;; GNU General Public License for more details. 17 18 ;; You should have received a copy of the GNU General Public License 19 ;; along with this program. If not, see <https://www.gnu.org/licenses/>. 20 21 ;;; Commentary: 22 23 ;; 24 25 ;;; Code: 26 27 (defvar eshell-last-output-start) 28 (defvar eshell-last-output-end) 29 30 (defun eshell-buttonize:buttonize-file-names () 31 (let ((case-fold-search nil)) 32 (save-excursion 33 (dolist (file (directory-files default-directory nil nil :nosort)) 34 (goto-char eshell-last-output-start) 35 (while (search-forward file eshell-last-output-end :noerror) 36 ;; Don't create button if the match is read-only (prompt) 37 (unless (get-text-property (match-beginning 0) 'read-only) 38 (make-text-button 39 (match-beginning 0) (match-end 0) 40 'keymap (let ((map (make-sparse-keymap)) 41 (function `(lambda () 42 (interactive) 43 ,(let ((file (expand-file-name file))) 44 (if (file-directory-p file) 45 `(progn 46 (eshell/cd ,file) 47 (eshell-reset)) 48 `(find-file-other-window ,file)))))) 49 (define-key map (kbd "RET") function) 50 (define-key map [mouse-1] function) 51 map) 52 'category nil 53 'mouse-face 'highlight))))))) 54 55 (define-minor-mode eshell-buttonize-mode 56 "Add useful buttons to eshell." 57 :global t 58 (if eshell-buttonize-mode 59 (add-hook 'eshell-output-filter-functions #'eshell-buttonize:buttonize-file-names) 60 (remove-hook 'eshell-output-filter-functions #'eshell-buttonize:buttonize-file-names))) 61 62 (provide 'eshell-buttonize) 63 ;;; eshell-buttonize.el ends here