org-eshell.el 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. ;;; org-eshell.el - Support for links to working directories in eshell
  2. ;; Copyright (C) 2011 Free Software Foundation, Inc.
  3. ;;
  4. ;; Author: Konrad Hinsen <konrad.hinsen AT fastmail.net>
  5. ;; Version: 0.1
  6. ;;
  7. ;; This file is not part of GNU Emacs.
  8. ;;
  9. ;; Emacs is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 3, or (at your option)
  12. ;; any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  19. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  20. ;; Boston, MA 02110-1301, USA.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. (require 'org)
  23. (require 'eshell)
  24. (require 'esh-mode)
  25. (org-add-link-type "eshell" 'org-eshell-open)
  26. (add-hook 'org-store-link-functions 'org-eshell-store-link)
  27. (defun org-eshell-open (link)
  28. "Switch to am eshell buffer and execute a command line.
  29. The link can be just a command line (executed in the default
  30. eshell buffer) or a command line prefixed by a buffer name
  31. followed by a colon."
  32. (let* ((buffer-and-command
  33. (if (string-match "\\([A-Za-z0-9-+*]+\\):\\(.*\\)" link)
  34. (list (match-string 1 link)
  35. (match-string 2 link))
  36. (list eshell-buffer-name link)))
  37. (eshell-buffer-name (car buffer-and-command))
  38. (command (cadr buffer-and-command)))
  39. (if (get-buffer eshell-buffer-name)
  40. (org-pop-to-buffer-same-window eshell-buffer-name)
  41. (eshell))
  42. (end-of-buffer)
  43. (eshell-kill-input)
  44. (insert command)
  45. (eshell-send-input)))
  46. (defun org-eshell-store-link ()
  47. "Store a link that, when opened, switches back to the current eshell buffer
  48. and the current working directory."
  49. (when (eq major-mode 'eshell-mode)
  50. (let* ((command (concat "cd " dired-directory))
  51. (link (concat (buffer-name) ":" command)))
  52. (org-store-link-props
  53. :link (org-make-link "eshell:" link)
  54. :description command))))
  55. (provide 'org-eshell)
  56. ;;; org-eshell.el ends here