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

80 lines
2.5 KiB
EmacsLisp
Raw Normal View History

2012-07-29 13:56:20 +02:00
;;; ein-iexec.el --- Instant execution mode for notebook
2012-07-22 15:40:41 +02:00
;; Copyright (C) 2012 Takafumi Arakaki
;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
;; This file is NOT part of GNU Emacs.
2012-07-29 13:56:20 +02:00
;; ein-iexec.el is free software: you can redistribute it and/or modify
2012-07-22 15:40:41 +02:00
;; 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.
2012-07-29 13:56:20 +02:00
;; ein-iexec.el is distributed in the hope that it will be useful,
2012-07-22 15:40:41 +02:00
;; 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
2012-07-29 13:56:20 +02:00
;; along with ein-iexec.el. If not, see <http://www.gnu.org/licenses/>.
2012-07-22 15:40:41 +02:00
;;; Commentary:
;;
;;; Code:
2012-08-20 04:17:59 +02:00
(require 'ein-worksheet)
2012-07-22 15:40:41 +02:00
2012-07-29 13:56:20 +02:00
(defcustom ein:iexec-delay 0.3
2012-07-22 15:53:00 +02:00
"Delay before executing cell after change in second."
:type 'number
:group 'ein)
2012-07-29 13:56:20 +02:00
(defvar ein:iexec-timer nil)
2012-07-22 15:53:00 +02:00
2012-07-29 13:56:20 +02:00
(defun ein:iexec-execute-cell (cell)
"Call `ein:notebook-execute-cell' after `ein:iexec-delay' second.
2012-07-22 15:53:00 +02:00
If the previous execution timer is not fired yet, cancel the timer."
2012-07-29 13:56:20 +02:00
(when ein:iexec-timer
(cancel-timer ein:iexec-timer))
(setq ein:iexec-timer
(run-with-idle-timer ein:iexec-delay nil
2012-08-20 04:17:59 +02:00
#'ein:worksheet-execute-cell
ein:%worksheet% cell)))
2012-07-22 15:53:00 +02:00
2012-07-29 13:56:20 +02:00
(defun ein:iexec-should-execute-p (cell beg end)
2012-07-22 17:40:35 +02:00
"Return non-`nil' if CELL should be executed by the change within
BEG and END."
(and (ein:codecell-p cell)
this-command
2019-11-24 00:17:52 -05:00
(aif (ein:cell-input-pos-min cell) (<= it beg))
(aif (ein:cell-input-pos-max cell) (>= it end))))
2012-07-22 17:40:35 +02:00
2012-07-29 13:56:20 +02:00
(defun ein:iexec-after-change (beg end -ignore-len-)
2012-07-22 15:40:41 +02:00
"Called via `after-change-functions' hook."
2012-08-20 04:17:59 +02:00
(let ((cell (ein:worksheet-get-current-cell :pos beg)))
2012-07-29 13:56:20 +02:00
(when (ein:iexec-should-execute-p cell beg end)
(ein:iexec-execute-cell cell))))
2012-07-22 15:40:41 +02:00
2012-09-01 20:51:55 +02:00
;;;###autoload
2012-07-29 13:56:20 +02:00
(define-minor-mode ein:iexec-mode
"Instant cell execution minor mode.
2012-07-22 16:02:14 +02:00
Code cell at point will be automatically executed after any
change in its input area."
2012-07-29 17:11:30 +02:00
:lighter " ein:i"
2012-07-22 15:40:41 +02:00
:group 'ein
2012-07-29 13:56:20 +02:00
(if ein:iexec-mode
(add-hook 'after-change-functions 'ein:iexec-after-change nil t)
(remove-hook 'after-change-functions 'ein:iexec-after-change t)))
2012-07-22 15:40:41 +02:00
2012-07-29 13:56:20 +02:00
;; To avoid MuMaMo to discard `ein:iexec-after-change', make it
2012-07-22 15:40:41 +02:00
;; permanent local.
2012-07-29 13:56:20 +02:00
(put 'ein:iexec-after-change 'permanent-local-hook t)
(put 'ein:iexec-mode 'permanent-local t)
2012-07-22 15:40:41 +02:00
2012-07-29 13:56:20 +02:00
(provide 'ein-iexec)
2012-07-22 15:40:41 +02:00
2012-07-29 13:56:20 +02:00
;;; ein-iexec.el ends here