ein-iexec.el 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. ;;; ein-iexec.el --- Instant execution mode for notebook
  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-iexec.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-iexec.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-iexec.el. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;;; Code:
  18. (require 'ein-worksheet)
  19. (defcustom ein:iexec-delay 0.3
  20. "Delay before executing cell after change in second."
  21. :type 'number
  22. :group 'ein)
  23. (defvar ein:iexec-timer nil)
  24. (defun ein:iexec-execute-cell (cell)
  25. "Call `ein:notebook-execute-cell' after `ein:iexec-delay' second.
  26. If the previous execution timer is not fired yet, cancel the timer."
  27. (when ein:iexec-timer
  28. (cancel-timer ein:iexec-timer))
  29. (setq ein:iexec-timer
  30. (run-with-idle-timer ein:iexec-delay nil
  31. #'ein:worksheet-execute-cell
  32. ein:%worksheet% cell)))
  33. (defun ein:iexec-should-execute-p (cell beg end)
  34. "Return non-`nil' if CELL should be executed by the change within
  35. BEG and END."
  36. (and (ein:codecell-p cell)
  37. this-command
  38. (ein:aif (ein:cell-input-pos-min cell) (<= it beg))
  39. (ein:aif (ein:cell-input-pos-max cell) (>= it end))))
  40. (defun ein:iexec-after-change (beg end -ignore-len-)
  41. "Called via `after-change-functions' hook."
  42. (let ((cell (ein:worksheet-get-current-cell :pos beg)))
  43. (when (ein:iexec-should-execute-p cell beg end)
  44. (ein:iexec-execute-cell cell))))
  45. ;;;###autoload
  46. (define-minor-mode ein:iexec-mode
  47. "Instant cell execution minor mode.
  48. Code cell at point will be automatically executed after any
  49. change in its input area."
  50. :lighter " ein:i"
  51. :group 'ein
  52. (if ein:iexec-mode
  53. (add-hook 'after-change-functions 'ein:iexec-after-change nil t)
  54. (remove-hook 'after-change-functions 'ein:iexec-after-change t)))
  55. ;; To avoid MuMaMo to discard `ein:iexec-after-change', make it
  56. ;; permanent local.
  57. (put 'ein:iexec-after-change 'permanent-local-hook t)
  58. (put 'ein:iexec-mode 'permanent-local t)
  59. (provide 'ein-iexec)
  60. ;;; ein-iexec.el ends here