ein-org.el 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ;;; ein-org.el --- Org-mode link support for EIN
  2. ;; Copyright (C) 2012 Takafumi Arakaki
  3. ;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
  4. ;; This file is NOT part of GNU Emacs.
  5. ;; ein-org.el 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. ;; ein-org.el 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 ein-org.el.
  15. ;; If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;;; Code:
  19. (require 'org)
  20. (require 'ein-notebooklist)
  21. ;; FIXME: Separate org-unrelated cores from the following code and
  22. ;; expose them as API in ein-link.el.
  23. (defun* ein:org-goto-link (notebook created
  24. &key
  25. worksheet-index
  26. search
  27. &allow-other-keys)
  28. (if created
  29. (ein:log 'info "Linked notebook did not exist. Created a new one.")
  30. (if worksheet-index
  31. (ein:notebook-worksheet-open-ith notebook worksheet-index
  32. #'pop-to-buffer)
  33. (pop-to-buffer (ein:notebook-buffer notebook)))
  34. (when search
  35. (goto-char (point-min))
  36. (search-forward search nil t))
  37. ;; More to come here:
  38. ))
  39. ;;;###autoload
  40. (defun ein:org-open (link-path)
  41. "Open IPython notebook specified by LINK-PATH.
  42. This function is to be used for FOLLOW function of
  43. `org-add-link-type'."
  44. (let ((link (read link-path)))
  45. (destructuring-bind (&key url-or-port name &allow-other-keys)
  46. link
  47. (ein:notebooklist-open-notebook-by-name name url-or-port
  48. #'ein:org-goto-link link))))
  49. ;;;###autoload
  50. (defun ein:org-store-link ()
  51. "Call `org-store-link-props' when in notebook buffer.
  52. This function is to be used for `org-store-link-functions'.
  53. Examples::
  54. ipynb:(:url-or-port 8888 :name \"My_Notebook\")
  55. ipynb:(:url-or-port \"http://notebook-server\" :name \"My_Notebook\")
  56. Note that spaces will be escaped in org files.
  57. As how IPython development team supports multiple directory in
  58. IPython notebook server is unclear, it is not easy to decide the
  59. format for notebook links. Current approach is to use
  60. S-expression based (rather verbose) serialization, so that
  61. extending link spec without loosing backward compatibility is
  62. easier. For the examples of link format in general, see Info
  63. node `(org) External links' and Info node `(org) Search options'"
  64. (ein:and-let* (((ein:worksheet-buffer-p))
  65. (notebook (ein:get-notebook))
  66. (name (ein:notebook-name notebook))
  67. (link (list :url-or-port (ein:get-url-or-port)
  68. :name name))
  69. (description name))
  70. (ein:aif (ein:notebook-worksheet-index notebook)
  71. (unless (= it 0)
  72. (plist-put link :worksheet-index it))
  73. (error "[ein] Cannot link to scratch sheet!"))
  74. (when (region-active-p)
  75. ;; FIXME: It does not work when selecting muli-line.
  76. (plist-put link :search (buffer-substring-no-properties
  77. (region-beginning) (region-end))))
  78. (org-store-link-props
  79. :type "ipynb"
  80. :link (let ((print-length nil)
  81. (print-level nil))
  82. (format "ipynb:%S" link))
  83. :description description)))
  84. ;;;###autoload
  85. (eval-after-load "org"
  86. '(progn
  87. (org-add-link-type "ipynb" 'ein:org-open)
  88. (add-hook 'org-store-link-functions 'ein:org-store-link)))
  89. ;; The above expression is evaluated via loaddef file. At the moment,
  90. ;; org.el nor ein-org.el need not be loaded. When org-mode is used,
  91. ;; the above `progn' is executed but still ein-org.el is not loaded.
  92. ;; When `ein:org-open' or `ein:org-store-link' is called for opening
  93. ;; or storing ipynb link, ein-org.el is loaded finally. (When
  94. ;; ein-org.el is loaded the above expression is evaluated again, but
  95. ;; that's OK as the expression is idempotent.)
  96. (provide 'ein-org)
  97. ;;; ein-org.el ends here