You apparently want to search only within certain contexts, defined by those two Org labels,
#+BEGIN_SRC python and
You can use Icicles search to do that, defining the search contexts as text that matches a regexp, which you can input at the prompt:
But as a shortcut (and easier on the eyes) you can use the toggle key
C-M-. to make typing just
. have the effect of typing
\(.\|[\n]\) (to match any char, including a newline), so just:
0 (accept the default), to match the whole regexp, (subgroup 0).
That searches all text from your start to your finish text. If you don't want to include the delimiters then you can use this instead, and enter
1 for the subgroup:
Then, to search within the contexts, just type whatever pattern (e.g. a regexp) you want to match into the minibuffer and hit
S-TAB to see the matches as completions in buffer
*Completions*. The matches update as you change the pattern in the minibuffer. You can navigate among contexts by cycling (e.g.
C-<down>). You can narrow the candidates further with
S-SPC and another search pattern. Etc.
But that context-defining regexp is far too simple. As you noted in a comment, it matches the text between the first
#+BEGIN_SRC and the last
#+END_SRC, producing only one context, which is useless.
Defining the contexts you really want is trickier. For that, I looked in the Org code for a suitable regexp and found
org-babel-src-block-regexp, which matches the text of any source block (not just
python), including matching the delimiters
Here's the value of
"^\\([ ]*\\)#\\+begin_src[ ]+\\([^ \f \n
That's the string form. To use
icicle-search interactively you would need to enter this at the prompt to define the regexp, and then enter
3 as the subgroup of the regexp to use as the context:
^\([ ]*\)#[+]BEGIN_SRC[ ]+\([^
With an extra
\) added around the part between the two delimiters.
As a value that you would input in the minibuffer as a regexp. E.g., for
\n you would use
C-q C-j (which inserts a newline char).
Clearly you wouldn't want to be entering a regexp like that interactively!
Fortunately, you can also use function
icicle-search to define your own search command.
Here's an Icicles-search command that incorporates
org-babel-src-block-regexp, but with an extra
\\) added around the part between the two delimiters:
(defvar org-python-section-regexp "^\\([ ]*\\)#\\+BEGIN_SRC[ ]+python[
"Regexp to match the content of Org-bable Python source sections.")
(defun icy-search-org-python-sections (beg end)
"`icicle-search' with Org-babel python sections as contexts.
Type a regexp to match within each context."
(let ((icicle-search-context-level 2)
(icicle-transform-function (and (not (interactive-p))
(progn (face-remap-set-base 'icicle-search-main-regexp-others nil)
(icicle-search beg end org-python-section-regexp t))
(when icicle-search-cleanup-flag (icicle-search-highlight-cleanup))
This is the part of that regexp that matches the text between the delimiters:
That's thanks to the Org code -- glad I didn't have to come up with it!
All I did was take the value of
python for the more general pattern
\\([^ \f \n
]+\\), and then wrap the between-delimiters part in
\\) so I could tell Icicles search to pick up all of that stuff.
Then I bound
2, which is the regexp subgroup number to use for the context.
(See also the Isearch answer below. There are advantages to each: Icicles search and Isearch.)