emacs-ipython-notebook/lisp/ein-helm.el

144 lines
4.3 KiB
EmacsLisp
Raw Normal View History

2012-08-10 21:13:59 +02:00
;;; ein-helm.el --- Helm/anything commands
;; Copyright (C) 2012 Takafumi Arakaki
;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
;; This file is NOT part of GNU Emacs.
;; ein-helm.el is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; ein-helm.el is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with ein-helm.el. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(eval-when-compile (require 'cl))
(declare-function anything-other-buffer "anything")
(declare-function helm-other-buffer "helm")
2012-09-05 22:47:29 +02:00
(require 'ein-kernel)
2012-09-23 15:18:08 +02:00
;;; Macros
(defmacro ein:helm-export-source (name)
(let* ((orig-source (intern (format "ein:helm-source-%s" name)))
(any-source (intern (format "anything-c-source-ein-%s" name)))
(helm-source (intern (format "helm-c-source-ein-%s" name)))
(docstring (format "Alias to `%s'" orig-source)))
`(progn
(defvaralias ',helm-source ',orig-source ,docstring)
(defvaralias ',any-source ',orig-source ,docstring))))
2012-09-05 22:47:29 +02:00
;;; Dynamic Variables
(defvar ein:helm-pattern 'helm-pattern
"Dynamically bound to one of `helm-pattern' or `anything-pattern'.")
(defvar ein:helm-kernel nil
"Dynamically bound to a kernel object.")
;;; History search
(defcustom ein:helm-kernel-history-search-auto-pattern t
"Automatically construct search pattern when non-`nil'.
1. Single space is converted to \"*\".
2. A backslash followed by a space is converted to a single space.
3. A \"*\" is added at the end of the pattern.
This variable applies to both `helm-ein-kernel-history' and
`anything-ein-kernel-history'."
:group 'ein)
(defun ein:helm-kernel-history-search-construct-pattern (pattern)
(when ein:helm-kernel-history-search-auto-pattern
(setq pattern
(replace-regexp-in-string "[^\\\\ ]\\( \\)[^\\\\ ]"
"*" pattern nil nil 1))
(setq pattern
(replace-regexp-in-string "\\\\ " " " pattern))
(setq pattern (concat pattern "*")))
pattern)
2012-09-05 23:34:22 +02:00
(defvar ein:helm-source-kernel-history
2012-09-05 22:47:29 +02:00
'((name . "IPython history")
(candidates . (lambda ()
(ein:kernel-history-search-synchronously
ein:helm-kernel
(ein:helm-kernel-history-search-construct-pattern
(eval ein:helm-pattern)))))
(requires-pattern . 3)
2012-09-06 00:17:22 +02:00
;; There is no need to filter out candidates:
(match . (identity))
2012-09-05 22:47:29 +02:00
(volatile)
(action . insert)
(delayed)
(multiline))
"Helm/anything source for searching kernel history.")
2012-09-05 22:51:21 +02:00
;;;###autoload
2012-09-05 22:47:29 +02:00
(defun anything-ein-kernel-history ()
"Search kernel execution history then insert the selected one."
(interactive)
(let ((ein:helm-pattern 'anything-pattern)
(ein:helm-kernel (ein:get-kernel-or-error)))
2012-09-05 23:34:22 +02:00
(anything-other-buffer ein:helm-source-kernel-history "*anything ein*")))
2012-09-05 22:47:29 +02:00
2012-09-05 22:51:21 +02:00
;;;###autoload
2012-09-05 22:47:29 +02:00
(defun helm-ein-kernel-history ()
"Search kernel execution history then insert the selected one."
(interactive)
(let ((ein:helm-pattern 'helm-pattern)
(ein:helm-kernel (ein:get-kernel-or-error)))
2012-09-05 23:34:22 +02:00
(helm-other-buffer ein:helm-source-kernel-history "*helm ein*")))
2012-09-05 22:47:29 +02:00
2012-09-05 22:51:04 +02:00
;;; Notebook buffers
2012-08-10 21:13:59 +02:00
(defvar ein:helm-source-notebook-buffers
'((name . "IPython notebook buffers")
(candidates . ein:notebook-opened-buffer-names)
2012-08-10 21:13:59 +02:00
(type . buffer))
"Helm/anything source for notebook buffers.")
;;; "Export" sources to `helm/anything-c-source-*'
2012-09-23 15:18:08 +02:00
(ein:helm-export-source notebook-buffers)
2012-08-10 21:13:59 +02:00
;;; Helm/anything commands
2012-09-01 20:51:55 +02:00
;;;###autoload
2012-08-10 21:13:59 +02:00
(defun anything-ein-notebook-buffers ()
"Choose opened notebook using anything.el interface."
2012-08-10 21:13:59 +02:00
(interactive)
(anything-other-buffer ein:helm-source-notebook-buffers "*anything ein*"))
2012-09-01 20:51:55 +02:00
;;;###autoload
2012-08-10 21:13:59 +02:00
(defun helm-ein-notebook-buffers ()
"Choose opened notebook using helm interface."
2012-08-10 21:13:59 +02:00
(interactive)
(helm-other-buffer ein:helm-source-notebook-buffers "*helm ein*"))
(provide 'ein-helm)
;;; ein-helm.el ends here