为什么re-search-backward在python模式下给出不同的结果? [英] Why does re-search-backward give different result in python-mode?
问题描述
M-b
运动。如果有一个名为 test_varialbe
的变量,我将光标放在此变量的末尾,在python模式 Mb
将使光标指向 t
,而在其他模式下,它将转到 v
。 所以我研究了子字模式的来源,发现了以下功能:
(defun subword-backward-internal()
/ pre>
(if(save-excursion
(let((case-fold-search nil))
(re-search-backward
(concat
\\(\\(\\W\\ | [[:lower:] [:digit:]] \\)\\([[:upper :]] + \\W * \\)
\\ | \\W\\\w + \\)
nil t) )
(goto-char
(cond
((和(match-end 3))
(< 1( - (match-end 3) )))
(not(eq(point)(match-end 3))))
(1-(match-end 3 ))
(t
(1+(match-beginning 0)))))
(反向字1)))
进行一些测试后,我发现
re-search-backward
在不同的模式下给出不同的结果。如果我在python-mode中的eval-expression
(let ...)
表达式,光标将跳转到test_varialbe
之前的空格,而在其他模式中,它将跳转到-
。
为什么是这样?导致
re-search-backward
的行为不同?解决方案原因是_的语法表定义有差异。
在Python模式中,_具有单词的语法定义,而在其他情况下它被定义为符号。看看 Elisp手册:语法表
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 namedtest_varialbe
and I put the cursor at the end of this variable, in python-modeM-b
will make the cursor point tot
while in other modes it will go tov
.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 Ieval-expression
the(let ...)
expression in python-mode, the cursor will jump to the space beforetest_varialbe
, and in other modes it will jump to-
.Why is this? What has caused
re-search-backward
to behave differently?解决方案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
这篇关于为什么re-search-backward在python模式下给出不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!