ol-eshell.el (2552B)
1 ;;; ol-eshell.el --- Links to Working Directories in Eshell -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2011-2024 Free Software Foundation, Inc. 4 5 ;; Author: Konrad Hinsen <konrad.hinsen AT fastmail.net> 6 7 ;; This file is part of GNU Emacs. 8 9 ;; GNU Emacs is free software: you can redistribute it and/or modify 10 ;; it under the terms of the GNU General Public License as published by 11 ;; the Free Software Foundation, either version 3 of the License, or 12 ;; (at your option) any later version. 13 14 ;; GNU Emacs is distributed in the hope that it will be useful, 15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 ;; GNU General Public License for more details. 18 19 ;; You should have received a copy of the GNU General Public License 20 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. 21 22 ;;; Commentary: 23 24 ;;; Code: 25 26 (require 'org-macs) 27 (org-assert-version) 28 29 (require 'eshell) 30 (require 'esh-mode) 31 (require 'ol) 32 33 (declare-function eshell/pwd "em-dirs.el" (&rest args)) 34 35 (org-link-set-parameters "eshell" 36 :follow #'org-eshell-open 37 :store #'org-eshell-store-link) 38 39 (defun org-eshell-open (link _) 40 "Switch to an eshell buffer and execute a command line for LINK. 41 The LINK can be just a command line (executed in the default 42 eshell buffer) or a command line prefixed by a buffer name 43 followed by a colon." 44 (let* ((buffer-and-command 45 (if (string-match "\\([A-Za-z0-9+*-]+\\):\\(.*\\)" link) 46 (list (match-string 1 link) 47 (match-string 2 link)) 48 (list eshell-buffer-name link))) 49 (eshell-buffer-name (car buffer-and-command)) 50 (command (cadr buffer-and-command))) 51 (if (get-buffer eshell-buffer-name) 52 (pop-to-buffer 53 eshell-buffer-name 54 (if (boundp 'display-comint-buffer-action) ; Emacs >= 29 55 display-comint-buffer-action 56 '(display-buffer-same-window (inhibit-same-window)))) 57 (eshell)) 58 (goto-char (point-max)) 59 (eshell-kill-input) 60 (insert command) 61 (eshell-send-input))) 62 63 (defun org-eshell-store-link (&optional _interactive?) 64 "Store eshell link. 65 When opened, the link switches back to the current eshell buffer and 66 the current working directory." 67 (when (eq major-mode 'eshell-mode) 68 (let* ((command (concat "cd " (eshell/pwd))) 69 (link (concat (buffer-name) ":" command))) 70 (org-link-store-props 71 :link (concat "eshell:" link) 72 :description command)))) 73 74 (provide 'ol-eshell) 75 76 ;;; ol-eshell.el ends here