这是一个解决方案:
(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))))
经测试:
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."
工作正常。是否有理由不使用缩进级别而不是注释作为锚点?