Browse Source

New commands `org-next-block' and `org-previous-block'

* ob-core.el (org-babel-next-src-block)
(org-babel-previous-src-block): Rewrite using
`org-next-block'.

* org.el (org-next-block, org-previous-block): New navigation
commands.
(org-mode-map): Bind the new commands to C-c C-F and C-c C-B
respectively.

Thanks to Bill White for suggesting this.
Bastien Guerry 12 years ago
parent
commit
eac6af707c
2 changed files with 28 additions and 11 deletions
  1. 4 11
      lisp/ob-core.el
  2. 24 0
      lisp/org.el

+ 4 - 11
lisp/ob-core.el

@@ -1639,22 +1639,15 @@ buffer or nil if no such result exists."
 (defun org-babel-next-src-block (&optional arg)
 (defun org-babel-next-src-block (&optional arg)
   "Jump to the next source block.
   "Jump to the next source block.
 With optional prefix argument ARG, jump forward ARG many source blocks."
 With optional prefix argument ARG, jump forward ARG many source blocks."
-  (interactive "P")
-  (when (looking-at org-babel-src-block-regexp) (forward-char 1))
-  (condition-case nil
-      (re-search-forward org-babel-src-block-regexp nil nil (or arg 1))
-    (error (error "No further code blocks")))
-  (goto-char (match-beginning 0)) (org-show-context))
+  (interactive "p")
+  (org-next-block arg nil org-babel-src-block-regexp))
 
 
 ;;;###autoload
 ;;;###autoload
 (defun org-babel-previous-src-block (&optional arg)
 (defun org-babel-previous-src-block (&optional arg)
   "Jump to the previous source block.
   "Jump to the previous source block.
 With optional prefix argument ARG, jump backward ARG many source blocks."
 With optional prefix argument ARG, jump backward ARG many source blocks."
-  (interactive "P")
-  (condition-case nil
-      (re-search-backward org-babel-src-block-regexp nil nil (or arg 1))
-    (error (error "No previous code blocks")))
-  (goto-char (match-beginning 0)) (org-show-context))
+  (interactive "p")
+  (org-previous-block arg org-babel-src-block-regexp))
 
 
 (defvar org-babel-load-languages)
 (defvar org-babel-load-languages)
 
 

+ 24 - 0
lisp/org.el

@@ -18463,6 +18463,8 @@ BEG and END default to the buffer boundaries."
 (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
 (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
+(org-defkey org-mode-map "\C-c\C-F" 'org-next-block)
+(org-defkey org-mode-map "\C-c\C-B" 'org-previous-block)
 (org-defkey org-mode-map "\C-c$"    'org-archive-subtree)
 (org-defkey org-mode-map "\C-c$"    'org-archive-subtree)
 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
@@ -22659,6 +22661,28 @@ Stop at the first and last subheadings of a superior heading."
 	  (if (< l level) (setq arg 1)))
 	  (if (< l level) (setq arg 1)))
 	(setq arg (1- arg))))))
 	(setq arg (1- arg))))))
 
 
+(defun org-next-block (arg &optional backward block-regexp)
+  "Jump to the next block.
+With a prefix argument ARG, jump forward ARG many source blocks.
+When BACKWARD is non-nil, jump to the previous block.
+When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
+  (interactive "p")
+  (let ((re (or block-regexp org-babel-src-block-regexp))
+	(re-search-fn (or (and backward 're-search-backward)
+			   're-search-forward)))
+    (if (looking-at re) (forward-char 1))
+    (condition-case nil
+	(funcall re-search-fn re nil nil arg)
+      (error (error "No %s code blocks" (if backward "previous" "further" ))))
+    (goto-char (match-beginning 0)) (org-show-context)))
+
+(defun org-previous-block (arg &optional block-regexp)
+  "Jump to the previous block.
+With a prefix argument ARG, jump backward ARG many source blocks.
+When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
+  (interactive "p")
+  (org-next-block arg t block-regexp))
+
 (defun org-forward-element ()
 (defun org-forward-element ()
   "Move forward by one element.
   "Move forward by one element.
 Move to the next element at the same level, when possible."
 Move to the next element at the same level, when possible."