tags:

views:

89

answers:

4

I cannot find the setting that prevents deletion of an entire region when you have it highlighted and you hit backspace... (I just want it to delete one character even if a region is highlighted.) I already have

(delete-selection-mode 0)

and in custom-set-variables (I have cua-mode enabled for its rectangle functions),

'(cua-delete-selection nil)

but that is the behavior I get. Am I missing something?

+2  A: 

In ELisp, 0 is considered as true.

Try by setting

(setq delete-selection-mode nil)
ring0
I was not aware that delete-select-mode was also a variable... but your suggestion did not work, unforunately...
Stephen
how odd too - about the zero - thanks for that tip.
Stephen
From your current buffer, try `Control ESC ESC` and enter the Lisp above `(setq delete-selection-mode nil)` (Enter). It should change the local behavior. Then try by setting the line in `.emacs` (may work for all buffers). Finally you can use the `M-x` (Alt-x) `describe-variable` and enter (completion) `delete-selection-mode`. The menu will offer you first `customization`, in which you can save the value permanently.
ring0
Nope... actually I don't know about `Control ESC ESC` (doesn't work) but same as `M-:`, which is `(eval-expression)`? I'm not entirely new to emacs so I did try changing the variable with both `setq` and in `custom-set-variables` in my .emacs file and restarted emacs - but did not work. Strange...
Stephen
Sorry it was `Control-x ESC ESC` to quick-edit the mini buffer and eval the `setq`. You can do also Alt-x `eval-expression`, 'Alt' being Meta - depending on your keyboard. It should work at least locally. This is `M-x` (Alt-x) and not `M-:`, to use `describe-variable`.
ring0
Hm, `C-x ESC ESC` is bound to `repeat-complex-command`. Are you sure it's not `M-:` for `eval-expression`? But in any case, problem solved! I think...
Stephen
+3  A: 

Emacs has different behavior depending on whether the highlighting was done with mouse or the keyboard.

Even in transient-mark-mode, if you set the mark and move the point, using backspace will not delete the region. delete-selection-mode is a minor mode that changes this behavior.

When using the mouse to highlight a region, regardless of delete-selection-mode, using backspace will delete the region that was highlighted with the mouse. From Section 25.1.1 of the manual:

"While the region remains active, typing or deletes the text in that region and deactivates the mark; this behavior follows a convention established by other graphical programs, and it does not apply when you set the region any other way, including shift-selection (*note Shift Selection::)."

Based on this, it sounds like you're selecting the region using the mouse. Is that correct? Does the same behavior arise when using shift-select-mode or simply using mark and point?

R. P. Dillon
Hi, actually I am only using the keyboard, with point and mark (not shift)... shift-selection-mode is nil.
Stephen
Well, then something is askew. I'm using GNU Emacs 23.2.1 on OS X and when I use the keyboard in any fashion to highlight a region (`transient-mark-mode` or not), and I backspace while the region is highlighted, I don't get this behavior. I'll post again if I can figure out some setting that might be causing the problem you're having.
R. P. Dillon
Thank you. Yes, very strange. I now found that `delete-forward-char` is also "broken" in the same way!
Stephen
I just got the nightly build (Emacs 24) - even if I start that with "-Q", same behavior! Maybe I'm just screwed...
Stephen
You know what, I applied a patch `http://citizen428.net/archives/432` to get the full-screen mode to operate. I wonder if this is causing the problems in my 23 and 24 (which includes the patch).
Stephen
My /usr/bin/emacs runs fine so it's something with these GNU Emacs builds...
Stephen
Of interest. When you rebind `<delete>` to something (say, `(insert "a")`), that command is NOT called when you press `<delete>` when the region is selected with the mouse....
Trey Jackson
Yes- I tried before-advising `delete-backward-char` so the mark would become inactive, but that did not work either. @Trey: It was your blog by they way that led me to try cua-mode for its rectangles in the first place ;). Cheers!
Stephen
+1  A: 

Found it:

(setq delete-active-region nil)

is the answer. Thanks to all for toughing it out with me!

Stephen
For this I had to turn cua-mode off also, since when the mark is active the `backspace` and `C-d` is bound to `cua-delete-region`. Unexpected, since `cua-enable-cua-keys` is set to `nil`, but I guess I'll do without cua-mode...
Stephen
That doesn't solve the problem in vanilla Emacs (`emacs -q`).
Trey Jackson
Interesting! I don't even have that variable...a quick Google search makes it seem like it only exists in delsel.el, which is included in GNU Emacs but apparently has to be `require`d before use. I never knew about it; sorry I couldn't have been more help!
R. P. Dillon
works for me with `emacs -q`... very odd, very odd... indeed. @rpdillon - thanks for your help!
Stephen
+1  A: 

As has been noted, cua-mode doesn't handle this nicely. The simplest way to retain that mode without this problem would seem to be to redefine the cua keymap (see cua--init-keymaps) after initialising the mode, to unbind the various delete keys from cua-delete-region:

(add-hook 'cua-mode-hook 'my-cua-mode-hook)
(defun my-cua-mode-hook ()
  (define-key cua--region-keymap [remap delete-backward-char] 'delete-backward-char)
  (define-key cua--region-keymap [remap backward-delete-char] 'backward-delete-char)
  (define-key cua--region-keymap [remap backward-delete-char-untabify] 'backward-delete-char-untabify)
  (define-key cua--region-keymap [remap delete-char] 'delete-char))
phils