org-eshell.el 2.2 KB

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