Why does re-search-backward give different result in python-mode?

心已入冬 提交于 2019-12-07 11:54:52

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!