ol-eshell.el 2.2 KB

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