org-eshell.el 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. ;;; org-eshell.el - Support for links to working directories in eshell
  2. ;; Copyright (C) 2011-2012 Free Software Foundation, Inc.
  3. ;; Author: Konrad Hinsen <konrad.hinsen AT fastmail.net>
  4. ;; Version: 0.1
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (require 'org)
  19. (require 'eshell)
  20. (require 'esh-mode)
  21. (org-add-link-type "eshell" 'org-eshell-open)
  22. (add-hook 'org-store-link-functions '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. (org-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 (org-make-link "eshell:" link)
  50. :description command))))
  51. (provide 'org-eshell)
  52. ;;; org-eshell.el ends here