ein-helm.el 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. ;;; ein-helm.el --- Helm/anything commands
  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-helm.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-helm.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-helm.el. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;;; Code:
  18. (eval-when-compile (require 'cl))
  19. (declare-function anything-other-buffer "anything")
  20. (declare-function helm-other-buffer "helm")
  21. (require 'ein-kernel)
  22. ;;; Macros
  23. (defmacro ein:helm-export-source (name)
  24. (let* ((orig-source (intern (format "ein:helm-source-%s" name)))
  25. (any-source (intern (format "anything-c-source-ein-%s" name)))
  26. (helm-source (intern (format "helm-c-source-ein-%s" name)))
  27. (docstring (format "Alias to `%s'" orig-source)))
  28. `(progn
  29. (defvaralias ',helm-source ',orig-source ,docstring)
  30. (defvaralias ',any-source ',orig-source ,docstring))))
  31. ;;; Dynamic Variables
  32. (defvar ein:helm-pattern 'helm-pattern
  33. "Dynamically bound to one of `helm-pattern' or `anything-pattern'.")
  34. (defvar ein:helm-kernel nil
  35. "Dynamically bound to a kernel object.")
  36. ;;; History search
  37. (defcustom ein:helm-kernel-history-search-auto-pattern t
  38. "Automatically construct search pattern when non-`nil'.
  39. 1. Single space is converted to \"*\".
  40. 2. A backslash followed by a space is converted to a single space.
  41. 3. A \"*\" is added at the beginning and end of the pattern.
  42. This variable applies to both `helm-ein-kernel-history' and
  43. `anything-ein-kernel-history'."
  44. :group 'ein)
  45. (defun ein:helm-kernel-history-search-construct-pattern (pattern)
  46. (when ein:helm-kernel-history-search-auto-pattern
  47. (setq pattern
  48. (replace-regexp-in-string "[^\\\\ ]\\( \\)[^\\\\ ]"
  49. "*" pattern nil nil 1))
  50. (setq pattern
  51. (replace-regexp-in-string "\\\\ " " " pattern))
  52. (setq pattern (concat "*" pattern "*")))
  53. pattern)
  54. (defun ein:helm-kernel-history-search-get-candidates ()
  55. "Retrieve search result from kernel.
  56. It requires the following dynamical variables:
  57. * `ein:helm-pattern'
  58. * `ein:helm-kernel'"
  59. (let* ((pattern (ein:helm-kernel-history-search-construct-pattern
  60. (eval ein:helm-pattern)))
  61. (candidates (ein:kernel-history-search-synchronously
  62. ein:helm-kernel pattern :unique t)))
  63. ;; Most recent history first:
  64. (nreverse candidates)))
  65. (defvar ein:helm-source-kernel-history
  66. '((name . "IPython history")
  67. (candidates . ein:helm-kernel-history-search-get-candidates)
  68. (requires-pattern . 3)
  69. ;; There is no need to filter out candidates:
  70. (match . (identity))
  71. (volatile)
  72. (action . insert)
  73. (delayed)
  74. (multiline))
  75. "Helm/anything source for searching kernel history.")
  76. ;;;###autoload
  77. (defun anything-ein-kernel-history ()
  78. "Search kernel execution history then insert the selected one."
  79. (interactive)
  80. (let ((ein:helm-pattern 'anything-pattern)
  81. (ein:helm-kernel (ein:get-kernel-or-error)))
  82. (anything-other-buffer ein:helm-source-kernel-history "*anything ein*")))
  83. ;;;###autoload
  84. (defun helm-ein-kernel-history ()
  85. "Search kernel execution history then insert the selected one."
  86. (interactive)
  87. (let ((ein:helm-pattern 'helm-pattern)
  88. (ein:helm-kernel (ein:get-kernel-or-error)))
  89. (helm-other-buffer ein:helm-source-kernel-history "*helm ein*")))
  90. ;;; Notebook buffers
  91. (defvar ein:helm-source-notebook-buffers
  92. '((name . "All IPython notebook buffers")
  93. (candidates . ein:notebook-opened-buffer-names)
  94. (type . buffer))
  95. "Helm/anything source for all opened notebook buffers.")
  96. (defvar ein:helm-source-modified-notebook-buffers
  97. '((name . "Modified IPython notebook buffers")
  98. (candidates
  99. . (lambda ()
  100. (ein:notebook-opened-buffer-names #'ein:notebook-modified-p)))
  101. (type . buffer))
  102. "Helm/anything source for modified notebook buffers.")
  103. (defvar ein:helm-source-saved-notebook-buffers
  104. '((name . "Saved IPython notebook buffers")
  105. (candidates
  106. . (lambda ()
  107. (ein:notebook-opened-buffer-names
  108. (lambda (nb) (not (ein:notebook-modified-p nb))))))
  109. (type . buffer))
  110. "Helm/anything source for saved notebook buffers.")
  111. ;;; "Export" sources to `helm/anything-c-source-*'
  112. (ein:helm-export-source notebook-buffers)
  113. (ein:helm-export-source modified-notebook-buffers)
  114. (ein:helm-export-source saved-notebook-buffers)
  115. ;;; Helm/anything commands
  116. (defvar ein:helm-notebook-buffer-sources
  117. '(ein:helm-source-modified-notebook-buffers
  118. ein:helm-source-saved-notebook-buffers))
  119. ;;;###autoload
  120. (defun anything-ein-notebook-buffers ()
  121. "Choose opened notebook using anything.el interface."
  122. (interactive)
  123. (anything-other-buffer ein:helm-notebook-buffer-sources "*anything ein*"))
  124. ;;;###autoload
  125. (defun helm-ein-notebook-buffers ()
  126. "Choose opened notebook using helm interface."
  127. (interactive)
  128. (helm-other-buffer ein:helm-notebook-buffer-sources "*helm ein*"))
  129. (provide 'ein-helm)
  130. ;;; ein-helm.el ends here