EMACS重新绑定C-spc禁用所选区域的突出显示

Mar*_*zzo 2 emacs elisp

EMACS 24.1.我需要重新绑定Ctrl-space到一个自定义函数:

  • 走到尽头
  • 删除尾随空格(如果有)
  • 设置标记(正常Ctrl-space)

这是我的代码无效:

(define-key global-map [?\C- ] 'my-set-mark-command)
(defun my-set-mark-command()
   (interactive)
   (end-of-line)
   (delete-char (* -1 (skip-chars-backward "\t\s")));;delete trailing spaces
   (set-mark-command nil))
Run Code Online (Sandbox Code Playgroud)

当没有尾随空格时,它正常工作:开始选择并突出显示该区域.尾随空格:删除尾随空格,停在行尾,设置标记但不突出显示该区域.

如果我删除最后一个命令(set-mark-command)并且我手动运行它可以M-xset-mark-command工作.请有人帮助我使这个功能正常工作??

Die*_*lla 5

修改命令会导致变量deactivate-mark重置,从而导致标记丢失.在save-excursion它的文档中说明如何通过绑定deactivate-mark变量来禁用该行为let.然后,您可以将代码更改为:

(defun my-set-mark-command ()
   (interactive)
   (end-of-line)
   (let (deactivate-mark)
     (delete-char (* -1 (skip-chars-backward "\t\s"))))  ;;delete trailing spaces
   (set-mark-command nil))
Run Code Online (Sandbox Code Playgroud)

甚至包括整个let内部save-excursion.

见手册:

http://www.gnu.org/software/emacs/manual/html_node/elisp/The-Mark.html#index-deactivate_002dmark-2801