Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
5.4 KiB

  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. (declare-function anything-other-buffer "anything")
  19. (declare-function helm-other-buffer "helm")
  20. (require 'ein-kernel)
  21. ;;; Macros
  22. (defmacro ein:helm-export-source (name)
  23. (let* ((orig-source (intern (format "ein:helm-source-%s" name)))
  24. (any-source (intern (format "anything-c-source-ein-%s" name)))
  25. (helm-source (intern (format "helm-c-source-ein-%s" name)))
  26. (docstring (format "Alias to `%s'" orig-source)))
  27. `(progn
  28. (defvaralias ',helm-source ',orig-source ,docstring)
  29. (defvaralias ',any-source ',orig-source ,docstring))))
  30. ;;; Dynamic Variables
  31. (defvar ein:helm-pattern 'helm-pattern
  32. "Dynamically bound to one of `helm-pattern' or `anything-pattern'.")
  33. (defvar ein:helm-kernel nil
  34. "Dynamically bound to a kernel object.")
  35. ;;; History search
  36. (defcustom ein:helm-kernel-history-search-auto-pattern t
  37. "Automatically construct search pattern when non-`nil'.
  38. 1. Single space is converted to \"*\".
  39. 2. A backslash followed by a space is converted to a single space.
  40. 3. A \"*\" is added at the beginning and end of the pattern.
  41. This variable applies to both `helm-ein-kernel-history' and
  42. `anything-ein-kernel-history'."
  43. :type 'boolean
  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