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??
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
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