I have been using python-mode for a long time. And I always use subword-mode. But subword-mode behave strangely in python-mode. For example, the M-b
movement. If there is a variable named test_varialbe
and I put the cursor at the end of this variable, in python-mode M-b
will make the cursor point to t
while in other modes it will go to v
.
So I looked into the source of subword-mode and found the following function:
(defun subword-backward-internal ()
(if (save-excursion
(let ((case-fold-search nil))
(re-search-backward
(concat
"\\(\\(\\W\\|[[:lower:][:digit:]]\\)\\([[:upper:]]+\\W*\\)"
"\\|\\W\\w+\\)")
nil t)))
(goto-char
(cond
((and (match-end 3)
(< 1 (- (match-end 3) (match-beginning 3)))
(not (eq (point) (match-end 3))))
(1- (match-end 3)))
(t
(1+ (match-beginning 0)))))
(backward-word 1)))
After making some tests, I found re-search-backward
is giving different result in different modes. If I eval-expression
the (let ...)
expression in python-mode, the cursor will jump to the space before test_varialbe
, and in other modes it will jump to -
.
Why is this? What has caused re-search-backward
to behave differently?