org-eshell.el 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. ;;; org-eshell.el - Support for Links to Working Directories in Eshell -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2011-2018 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)
  18. (require 'eshell)
  19. (require 'esh-mode)
  20. (org-link-set-parameters "eshell"
  21. :follow #'org-eshell-open
  22. :store #'org-eshell-store-link)
  23. (defun org-eshell-open (link)
  24. "Switch to am eshell buffer and execute a command line.
  25. The link can be just a command line (executed in the default
  26. eshell buffer) or a command line prefixed by a buffer name
  27. followed by a colon."
  28. (let* ((buffer-and-command
  29. (if (string-match "\\([A-Za-z0-9-+*]+\\):\\(.*\\)" link)
  30. (list (match-string 1 link)
  31. (match-string 2 link))
  32. (list eshell-buffer-name link)))
  33. (eshell-buffer-name (car buffer-and-command))
  34. (command (cadr buffer-and-command)))
  35. (if (get-buffer eshell-buffer-name)
  36. (pop-to-buffer-same-window eshell-buffer-name)
  37. (eshell))
  38. (goto-char (point-max))
  39. (eshell-kill-input)
  40. (insert command)
  41. (eshell-send-input)))
  42. (defun org-eshell-store-link ()
  43. "Store a link that, when opened, switches back to the current eshell buffer
  44. and the current working directory."
  45. (when (eq major-mode 'eshell-mode)
  46. (let* ((command (concat "cd " dired-directory))
  47. (link (concat (buffer-name) ":" command)))
  48. (org-store-link-props
  49. :link (concat "eshell:" link)
  50. :description command))))
  51. (provide 'org-eshell)
  52. ;;; org-eshell.el ends here