EMACS rebinding C-spc disable highlight of selected region

时光毁灭记忆、已成空白 提交于 2019-12-11 03:14:39

问题


EMACS 24.1. I need to Rebind Ctrl-space to a custom function that:

  • go to end of line
  • delete trailing spaces if any
  • set the Mark (as normal Ctrl-space)

This is my code not working:

(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))

When no trailing spaces it works normally: start to select and highlight the region. When trailing spaces: it deletes trailing spaces, stop at end of line, set Mark but don't highlight the region.

If I remove the last command (set-mark-command) and I run it manually M-xset-mark-command it works. Please can somebody help me to make this function works properly??


回答1:


Modification commands cause the variable deactivate-mark to be reset, causing the mark to be lost. In the documentation for save-excursion it says how to disable that behavior, by binding the deactivate-mark variable with let. You can then change the code to:

(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))

or even include the whole let inside a save-excursion.

See manual:

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




回答2:


Diego's explained what you want to know. I just want to note that remapping C-SPC is not a good idea. C-SPC manipulates the mark ring, which is extremely useful. See manual, in particular see C-u C-SPC



来源:https://stackoverflow.com/questions/11080242/emacs-rebinding-c-spc-disable-highlight-of-selected-region

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