ol-eshell.el 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. ;;; ol-eshell.el --- Links to Working Directories in Eshell -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2011-2022 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 'org-macs)
  18. (org-assert-version)
  19. (require 'eshell)
  20. (require 'esh-mode)
  21. (require 'ol)
  22. (declare-function eshell/pwd "em-dirs.el" (&rest args))
  23. (org-link-set-parameters "eshell"
  24. :follow #'org-eshell-open
  25. :store #'org-eshell-store-link)
  26. (defun org-eshell-open (link _)
  27. "Switch to an eshell buffer and execute a command line.
  28. The link can be just a command line (executed in the default
  29. eshell buffer) or a command line prefixed by a buffer name
  30. followed by a colon."
  31. (let* ((buffer-and-command
  32. (if (string-match "\\([A-Za-z0-9+*-]+\\):\\(.*\\)" link)
  33. (list (match-string 1 link)
  34. (match-string 2 link))
  35. (list eshell-buffer-name link)))
  36. (eshell-buffer-name (car buffer-and-command))
  37. (command (cadr buffer-and-command)))
  38. (if (get-buffer eshell-buffer-name)
  39. (pop-to-buffer
  40. eshell-buffer-name
  41. (if (boundp 'display-comint-buffer-action) ; Emacs >= 29
  42. display-comint-buffer-action
  43. '(display-buffer-same-window (inhibit-same-window))))
  44. (eshell))
  45. (goto-char (point-max))
  46. (eshell-kill-input)
  47. (insert command)
  48. (eshell-send-input)))
  49. (defun org-eshell-store-link ()
  50. "Store a link that, when opened, switches back to the current eshell buffer
  51. and the current working directory."
  52. (when (eq major-mode 'eshell-mode)
  53. (let* ((command (concat "cd " (eshell/pwd)))
  54. (link (concat (buffer-name) ":" command)))
  55. (org-link-store-props
  56. :link (concat "eshell:" link)
  57. :description command))))
  58. (provide 'ol-eshell)
  59. ;;; ol-eshell.el ends here