branch: externals/code-cells
commit 7bad8f1cf647e9cfa7fcc36b61ba9eb20d426229
Author: Augusto Stoffel <[email protected]>
Commit: Augusto Stoffel <[email protected]>

    Speed keys
---
 README.md | 23 +++++++++++++++++++++++
 cells.el  |  9 +++++++++
 2 files changed, 32 insertions(+)

diff --git a/README.md b/README.md
index e0de5e3668..2921b10a68 100644
--- a/README.md
+++ b/README.md
@@ -175,6 +175,29 @@ Note that since `defhydra` is a macro and wraps the 
definition of a
 key in an interactive lambda when it is a sexp, we need to use
 `cells-do` instead of `cells-command` above.
 
+### Speed keys
+
+Similarly to org-mode's [speed 
keys](https://orgmode.org/manual/Speed-Keys.html),
+the `cells-speed-key` function returns a key definition that only acts
+when the point is at the beginning of a cell boundary.  Since this is
+usually not an interesting place to insert text, you can assign short
+keybindings there.  A sample configuration is as follows:
+
+``` elisp
+(require 'cells)
+(let ((map cells-mode-map))
+  (define-key map "n" (cells-speed-key 'cells-forward-cell))
+  (define-key map "p" (cells-speed-key 'cells-backward-cell))
+  (define-key map "e" (cells-speed-key (cells-command 
'your-favorite-eval-region)))
+  (define-key map (kbd "TAB") (cells-speed-key (lambda ()
+                                                 "Show/hide current cell"
+                                                 (interactive)
+                                                 (outline-minor-mode)
+                                                 (if (outline-invisible-p 
(line-end-position))
+                                                     (outline-show-subtree)
+                                                   (outline-hide-subtree))))))
+```
+
 ### Tweaking the ipynb conversion
 
 If relegating markdown cells to comment blocks offends your literate
diff --git a/cells.el b/cells.el
index e650996ba4..69b47696cc 100644
--- a/cells.el
+++ b/cells.el
@@ -139,6 +139,15 @@ via `pulse-momentary-highlight-region'."
                        '(pulse-momentary-highlight-region start end))
                     (funcall ',fun start end)))))
 
+(defun cells-speed-key (command)
+  "Return a speed key definition, suitable for passing to `define-key'.
+The resulting keybinding will only have any effect when the point
+is at the beginning of a cell heading, in which case it executes
+COMMAND."
+  (list 'menu-item nil command
+        :filter (lambda (d)
+                  (if (and (bolp) (looking-at (cells-boundary-regexp))) d))))
+
 ;;* Minor mode
 
 (defvar-local cells--saved-vars nil

Reply via email to