emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/code-cells 83cb4d0095 24/36: Add code-cells-eval


From: ELPA Syncer
Subject: [elpa] externals/code-cells 83cb4d0095 24/36: Add code-cells-eval
Date: Mon, 28 Feb 2022 15:57:43 -0500 (EST)

branch: externals/code-cells
commit 83cb4d0095eb3fa513c964e1b90e3343fe2d65dc
Author: Augusto Stoffel <arstoffel@gmail.com>
Commit: Augusto Stoffel <arstoffel@gmail.com>

    Add code-cells-eval
---
 code-cells.el | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/code-cells.el b/code-cells.el
index f6961edf5b..19dc2faccf 100644
--- a/code-cells.el
+++ b/code-cells.el
@@ -179,6 +179,49 @@ COMMAND."
                              (looking-at (code-cells-boundary-regexp)))
                     d))))
 
+;;; Code evaluation
+
+(defcustom code-cells-eval-region-commands
+  '((jupyter-repl-interaction-mode . jupyter-eval-region)
+    (python-mode . python-shell-send-region)
+    (emacs-lisp-mode . eval-region)
+    (lisp-interaction-mode . eval-region))
+  "Alist of commands to evaluate a region.
+The keys are major or minor modes and the values are functions
+taking region bounds as argument."
+  :type '(alist :key-type symbol :value-type symbol))
+
+;;;###autoload
+(defun code-cells-eval (start end)
+  "Evaluate code according to current modes.
+The first suitable function from `code-cells-eval-region-commands'
+is used to do the job.
+
+Interactively, evaluate the region, if active, otherwise the
+current code cell.  With a numeric prefix, evaluate that many
+code cells.
+
+Called from Lisp, evaluate region between START and END."
+  (interactive (code-cells--bounds (prefix-numeric-value current-prefix-arg) 
t))
+  (funcall
+   (or (seq-some (pcase-lambda (`(,mode . ,fun))
+                   (when (or (and (boundp mode) mode)
+                             (derived-mode-p mode))
+                     fun))
+                 code-cells-eval-region-commands)
+       (user-error
+        "No entry for the current modes in 
`code-cells-eval-region-commands'."))
+   start end)
+  (pulse-momentary-highlight-region start end))
+
+;;;###autoload
+(defun code-cells-eval-above (arg)
+  "Evaluate this and all above cells."
+  (interactive "p")
+  (code-cells-eval (point-min) (save-excursion
+                                 (code-cells-forward-cell arg)
+                                 (point))))
+
 ;;; Minor mode
 
 (defvar-local code-cells--saved-vars nil
@@ -237,6 +280,14 @@ level."
     (font-lock-remove-keywords nil (code-cells--font-lock-keywords)))
   (font-lock-flush))
 
+(let ((map (make-sparse-keymap)))
+  (define-key code-cells-mode-map "\C-c%" map)
+  (define-key map ";" 'code-cells-comment-or-uncomment)
+  (define-key map "@" 'code-cells-mark-cell)
+  (define-key map "b" 'code-cells-backward-cell)
+  (define-key map "f" 'code-cells-forward-cell)
+  (define-key map "e" 'code-cells-eval))
+
 ;;; Jupyter notebook conversion
 
 (defcustom code-cells-convert-ipynb-style



reply via email to

[Prev in Thread] Current Thread [Next in Thread]