2013-04-16 60 views
1

我想替换Emacs的默认前向词和后向词以更像Visual Studio的操作 - 我发现它更适合Perl编程。我第一次尝试黑客的语法表,没有达到我想要的效果。然后,我想出了以下内容:开始选择替换Emacs的前向词

(defconst perl-movement-stop-chars "[email protected]%_0-9'") 
(defconst perl-movement-stop-pattern (concat "[" perl-movement-stop-chars "]")) 
(defconst non-perl-movement-stop-pattern (concat "[^" perl-movement-stop-chars "]")) 

(defun perl-forward-word() 
    (interactive) 
    (if (looking-at perl-movement-stop-pattern) 
     (progn 
     (if (re-search-forward non-perl-movement-stop-pattern nil t) 
      (backward-char))) 
    (if (re-search-forward perl-movement-stop-pattern nil t) 
     (backward-char)))) 

(defun perl-backward-word() 
    (interactive) 
    (backward-char) 
    (if (looking-at perl-movement-stop-pattern) 
     (progn 
     (if (re-search-backward non-perl-movement-stop-pattern nil t) 
      (forward-char))) 
    (if (re-search-backward perl-movement-stop-pattern nil t) 
     (forward-char)))) 

(add-hook 'cperl-mode-hook 
      (lambda() 
      (local-set-key [C-right] 'perl-forward-word) 
      (local-set-key [C-left] 'perl-backward-word) 
      linum-mode)) 

此我想要做什么 - 近:我仍然有在缓冲区中的第一个字里面向后移动时,处理此案。但那不是我的问题。

这样做的问题是,当我键入C-S-右键时,选择不会开始,因为它没有安装(或在其他模式下)。如果我开始选择(例如通过击打第一个S右),我的功能确实会扩展它。

我对elisp编程知之甚少,我只是在这里猜测我的方式。我希望有一点帮助。谢谢...

+2

你能描述你正在努力实现(而不是说“操作更像是Visual Studio的”)和现在正在发生的事情是什么。 – Lindydancer

+0

当点处于“停止”字符[a-zA-Z $ @%_ 0-9']时,我想向前(或向后)移动,直到它不再存在。当它不是停止角色时,我想移动到它停止。换句话说,我想从一个“单词”内部轻松地移动到它的结尾,然后到下一个“单词”的开始,然后是刚刚结束下一个“单词”等 - 我的“单词”的定义是有点不标准虽然。 – yorel

回答

2

要得到shift-select-mode的工作,你需要使用(interactive "^")。尝试C-h f interactive RET

顺便说一句,你可以相当简化你的代码:向前移动,只需(re-search-forward ".\\(\\_<\\|\\_>\\)" nil t)并向后移动,使用(re-search-backward "\\(\\_<\\|\\_>\\)." nil t)

+0

谢谢。 BTW的建议没有给出我想要的结果,我想是因为它依赖于语法表的正常定义(也许我应该修改语法表)。然而,(互动“^”)建议正是我所需要的。 – yorel

+0

特别地,它跳过第二个“使用”: 使用严格; 使用警告; – yorel

+0

我无法重现您的特定问题,但我怀疑问题是'.'应该由两个正则表达式中的\\(。\\ | \ n \\)'替换。 – Stefan

1

我不确定你想要什么,但我想我会提供这个。我有一个包裹,我叫syntax-subword(可用于Melpa)。它使得单词运动更加细致,达到几乎不会被字符移动的程度,并使用其他解决方案(如isearch)来移动更远的距离。

从评论:

;; This package provides `syntax-subword' minor mode, which extends 
;; `subword-mode' to make word editing and motion more fine-grained. 
;; Basically, it makes syntax changes, CamelCaseWords, and the normal 
;; word boundaries the boundaries for word operations. Here's an 
;; example of where the cursor stops using `forward-word' in 
;; `emacs-lisp-mode': 
;; 
;; (defun FooBar (arg) "doc string" 
;; |  |  | |  |  | standard 
;; |  | | | |  |  | subword-mode 
;; || || | ||| |||| ||  || syntax-subword-mode 
;; ||  |  || | || |  | vim