diff --git a/doc/source/conf.el b/doc/source/conf.el index 093bbd2..f9c500f 100644 --- a/doc/source/conf.el +++ b/doc/source/conf.el @@ -8,6 +8,7 @@ (require 'ein-mumamo) (require 'ein-ac) (require 'ein-connect) +(require 'ein-autoexec) ;; Load `wid-edit'. Otherwise the following error will be raised: ;; Symbol's function definition is void: widget-button-press diff --git a/doc/source/index.rst b/doc/source/index.rst index 41e9ece..281de86 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -220,6 +220,7 @@ The following keybinds are available in notebook buffers. .. el:function:: ein:notebook-delete-cell-command .. el:function:: ein:notebook-rename-to-scratch-command .. el:function:: ein:notebook-kill-all-buffers +.. el:function:: ein:autoexec-mode Connected buffer ^^^^^^^^^^^^^^^^ @@ -304,6 +305,7 @@ Notebook .. el:variable:: ein:cell-traceback-level .. el:variable:: ein:cell-max-num-outputs .. el:variable:: ein:scratch-notebook-name-template +.. el:variable:: ein:autoexec-delay Connect ^^^^^^^ @@ -474,6 +476,7 @@ v0.1.1 * Add :el:symbol:`ein:pytools-pandas-to-ses`. * Add Imenu support. * Better heading cell faces. +* Add :el:symbol:`ein:autoexec-mode` v0.1 diff --git a/lisp/ein-autoexec.el b/lisp/ein-autoexec.el new file mode 100644 index 0000000..28a09a3 --- /dev/null +++ b/lisp/ein-autoexec.el @@ -0,0 +1,73 @@ +;;; ein-autoexec.el --- Automatic cell execution + +;; Copyright (C) 2012 Takafumi Arakaki + +;; Author: Takafumi Arakaki + +;; This file is NOT part of GNU Emacs. + +;; ein-autoexec.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-autoexec.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-autoexec.el. If not, see . + +;;; Commentary: + +;; + +;;; Code: + +(require 'ein-notebook) + +(defcustom ein:autoexec-delay 0.3 + "Delay before executing cell after change in second." + :type 'number + :group 'ein) + +(defvar ein:autoexec-timer nil) + +(defun ein:autoexec-execute-cell (cell) + "Call `ein:notebook-execute-cell' after `ein:autoexec-delay' second. +If the previous execution timer is not fired yet, cancel the timer." + (when ein:autoexec-timer + (cancel-timer ein:autoexec-timer)) + (setq ein:autoexec-timer + (run-with-idle-timer ein:autoexec-delay nil + #'ein:notebook-execute-cell + ein:notebook cell))) + +(defun ein:autoexec-after-change (beg end -ignore-len-) + "Called via `after-change-functions' hook." + (let ((cell (ein:notebook-get-current-cell beg))) + (when (and (ein:codecell-p cell) + this-command + (<= (ein:cell-input-pos-min cell) beg) + (>= (ein:cell-input-pos-max cell) end)) + (ein:autoexec-execute-cell cell)))) + +(define-minor-mode ein:autoexec-mode + "Automatic cell execution minor mode. +Code cell at point will be automatically executed after any +change in its input area." + :lighter " ein:au" + :group 'ein + (if ein:autoexec-mode + (add-hook 'after-change-functions 'ein:autoexec-after-change nil t) + (remove-hook 'after-change-functions 'ein:autoexec-after-change t))) + +;; To avoid MuMaMo to discard `ein:autoexec-after-change', make it +;; permanent local. +(put 'ein:autoexec-after-change 'permanent-local-hook t) +(put 'ein:autoexec-mode 'permanent-local t) + +(provide 'ein-autoexec) + +;;; ein-autoexec.el ends here diff --git a/lisp/ein.el b/lisp/ein.el index 48750e9..771f40f 100644 --- a/lisp/ein.el +++ b/lisp/ein.el @@ -43,6 +43,9 @@ (autoload 'ein:connect-to-notebook "ein-connect" "Connect any buffer to notebook and its kernel." t) +(autoload 'ein:autoexec-mode "ein-autoexec" + "Automatic cell execution minor mode." t) + (autoload 'ein:dev-insert-mode-map "ein-dev") (autoload 'ein:dev-start-debug "ein-dev" "Enable debugging support." t) (autoload 'ein:dev-stop-debug "ein-dev" "Disable debugging support." t)