问题
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?
回答1:
The reason is that there are differences in the Syntax table definition of '_'.
In Python mode '_' has a syntax definition of "word" whereas in other cases it is defined as "symbol". Look at Elisp manual: Syntax tables
回答2:
in addition:
Grasping identifiers, basic commands like `forward-word' make more sense with "_" on word syntax. AFAIK Emacs doesn't provide the respective commands WRT with symbols.
来源:https://stackoverflow.com/questions/13354199/why-does-re-search-backward-give-different-result-in-python-mode