org-eshell.el 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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: 7.8.03
  5. ;;
  6. ;; This file is part of GNU Emacs.
  7. ;;
  8. ;; Emacs is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  18. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19. ;; Boston, MA 02110-1301, USA.
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. (require 'org)
  22. (require 'eshell)
  23. (require 'esh-mode)
  24. (org-add-link-type "eshell" 'org-eshell-open)
  25. (add-hook 'org-store-link-functions 'org-eshell-store-link)
  26. (defun org-eshell-open (link)
  27. "Switch to am 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. (org-pop-to-buffer-same-window eshell-buffer-name)
  40. (eshell))
  41. (end-of-buffer)
  42. (eshell-kill-input)
  43. (insert command)
  44. (eshell-send-input)))
  45. (defun org-eshell-store-link ()
  46. "Store a link that, when opened, switches back to the current eshell buffer
  47. and the current working directory."
  48. (when (eq major-mode 'eshell-mode)
  49. (let* ((command (concat "cd " dired-directory))
  50. (link (concat (buffer-name) ":" command)))
  51. (org-store-link-props
  52. :link (org-make-link "eshell:" link)
  53. :description command))))
  54. (provide 'org-eshell)
  55. ;;; org-eshell.el ends here