Say I have a buffer with code (in this case Python) organized as follows:
.. cell 1 ..
##
.. cell 2 ..
# this is a comment
### this is also a comment
.. still cell 2 ..
##
.. cell 3 (code that is indented)
The sequence of characters ##
is meant to delimit cells
(code regions/blocks) in the buffer. The caracter #
starts a comment in Python, so ##
is treated as a comment by the language. Similar constructions could be built in e.g. Elisp with ;;
or other programming languages.
I would like to define an Emacs command that when invoked, it defines the current cell
(i.e. the cell
on which point/cursor currently sits.) to be the Emacs region
(i.e. it highlights the cell).
How can I do this in Emacs?
For reference:
Here's a solution:
(defun python-inside-comment-p ()
(save-excursion
(beginning-of-line 1)
(looking-at "^#")))
(defun python-select-cell ()
(interactive)
(goto-char
(if (re-search-backward "^\\s-*##[^#]" nil t)
(match-end 0)
(point-min)))
(while (and (python-inside-comment-p)
(eq 0 (forward-line 1)))
nil)
(set-mark (point))
(goto-char
(if (re-search-forward "^\\s-*\\(##[^#]\\)" nil t)
(- (match-beginning 1) 2)
(point-max))))
Tested with:
print "Beautiful is better than ugly."
##
print "Explicit is better than implicit."
print "Simple is better than complex."
print "Complex is better than complicated."
# this is a comment
print "Flat is better than nested."
### this is also a comment
print "Sparse is better than dense."
##
print "Readability counts."
print "Special cases aren't special enough to break the rules."
print "Although practicality beats purity."
print "Errors should never pass silently."
print "Unless explicitly silenced."
Works OK. Is there a reason not to use indentation levels rather than comments as anchors?