emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Proposal for DEL to delete the active region


From: Chong Yidong
Subject: Proposal for DEL to delete the active region
Date: Fri, 30 Apr 2010 23:23:22 -0400

Here is a proposal for DEL to delete the region when transient-mark-mode
is enabled.  DEL already does this in delete-selection-mode; the idea
here is to move this behavior into transient-mark-mode proper.

This involves getting rid of `mouse-region-delete-keys', moving
`delete-backward-char' from C to Lisp, and introducing a new variable
`delete-backward-char-delete-region'.  Its default value, t, means that
`delete-backward-char' will delete the active region.

Conceptually, this puts `delete-backward-char-delete-region' on the same
footing as other user commands that "act on the region" under Transient
Mark mode.  The additional benefit is getting rid of the
`mouse-region-delete-keys' hack, so that DEL does the same thing for all
active regions.

Something similar would be done with delete-char (not shown in this
patch), so that [delete] deletes active regions too.

Comments, and suggestions for different approaches, welcome.


*** lisp/simple.el      2010-04-28 15:18:37 +0000
--- lisp/simple.el      2010-05-01 03:12:28 +0000
***************
*** 837,842 ****
--- 837,891 ----
         (overlay-recenter (point))
         (recenter -3))))
  
+ (defcustom delete-backward-char-delete-region t
+   "If non-nil, `delete-backward-char' deletes active regions.
+ This has an effect only when Transient Mark mode is enabled.
+ If the value is the symbol `kill', `delete-backward-char' kills
+ the active region instead of deleting it."
+   :type '(choice (const :tag "Delete region" t)
+                  (const :tag "Kill region" kill)
+                  (const :tag "Do not delete region" nil))
+   :group 'editing
+   :version "24.1")
+ 
+ (defun delete-backward-char (n killflag)
+   "Delete the previous N characters (following if N is negative).
+ KILLFLAG, if non-nil, means kill instead (save in kill ring).
+ Interactively, N is the prefix arg, and KILLFLAG is set if N is
+ explicitly specified.
+ 
+ If Transient Mark mode is enabled, the mark is active, and N is 1,
+ delete the text in the region and deactivate the mark instead.
+ To disable this, set `delete-backward-char-delete-region' to nil."
+   (interactive "p\nP")
+   (unless (integerp n)
+     (signal 'wrong-type-argument (list 'integerp n)))
+   (cond ((and (use-region-p)
+             delete-backward-char-delete-region
+             (= n 1))
+        (if (eq delete-backward-char-delete-region 'kill)
+            (kill-region (region-beginning) (region-end))
+          (delete-region (region-beginning) (region-end))))
+       ((and overwrite-mode
+             (> n 0)
+             (null (memq (char-before) '(?\t ?\n)))
+             (null (eobp))
+             (null (eq (char-after) ?\n)))
+        ;; In overwrite mode, back over columns while clearing them
+        ;; out, unless at end of line.
+        (let* ((ocol (current-column))
+               (val (delete-char (- n) killflag)))
+          (save-excursion
+            (insert-char ?\s (- ocol (current-column)) nil))))
+       (killflag
+        (kill-backward-chars n))
+       ((< (min (point) (- (point) n)) (point-min))
+        (signal 'beginning-of-buffer nil))
+       ((> (max (point) (- (point) n)) (point-max))
+        (signal 'beginning-of-buffer nil))
+       (t
+        (delete-region (- (point) n) (point)))))
+ 
  (defun mark-whole-buffer ()
    "Put point at beginning and mark at end of buffer.
  You probably should not use this function in Lisp programs;
*** src/cmds.c  2010-03-31 04:14:08 +0000
--- src/cmds.c  2010-05-01 00:06:40 +0000
***************
*** 265,322 ****
    return Qnil;
  }
  
- DEFUN ("delete-backward-char", Fdelete_backward_char, Sdelete_backward_char,
-        1, 2, "p\nP",
-        doc: /* Delete the previous N characters (following if N is negative).
- Optional second arg KILLFLAG non-nil means kill instead (save in kill ring).
- Interactively, N is the prefix arg, and KILLFLAG is set if
- N was explicitly specified.  */)
-      (n, killflag)
-      Lisp_Object n, killflag;
- {
-   Lisp_Object value;
-   int deleted_special = 0;
-   int pos, pos_byte, i;
- 
-   CHECK_NUMBER (n);
- 
-   /* See if we are about to delete a tab or newline backwards.  */
-   pos = PT;
-   pos_byte = PT_BYTE;
-   for (i = 0; i < XINT (n) && pos_byte > BEGV_BYTE; i++)
-     {
-       int c;
- 
-       DEC_BOTH (pos, pos_byte);
-       c = FETCH_BYTE (pos_byte);
-       if (c == '\t' || c == '\n')
-       {
-         deleted_special = 1;
-         break;
-       }
-     }
- 
-   /* In overwrite mode, back over columns while clearing them out,
-      unless at end of line.  */
-   if (XINT (n) > 0
-       && ! NILP (current_buffer->overwrite_mode)
-       && ! deleted_special
-       && ! (PT == ZV || FETCH_BYTE (PT_BYTE) == '\n'))
-     {
-       int column = (int) current_column (); /* iftc */
- 
-       value = Fdelete_char (make_number (-XINT (n)), killflag);
-       i = column - (int) current_column (); /* iftc */
-       Finsert_char (make_number (' '), make_number (i), Qnil);
-       /* Whitespace chars are ASCII chars, so we can simply subtract.  */
-       SET_PT_BOTH (PT - i, PT_BYTE - i);
-     }
-   else
-     value = Fdelete_char (make_number (-XINT (n)), killflag);
- 
-   return value;
- }
- 
  static int nonundocount;
  
  /* Note that there's code in command_loop_1 which typically avoids
--- 265,270 ----
***************
*** 625,631 ****
    defsubr (&Send_of_line);
  
    defsubr (&Sdelete_char);
-   defsubr (&Sdelete_backward_char);
  
    defsubr (&Sself_insert_command);
  }
--- 573,578 ----
*** lisp/mouse.el       2010-01-13 08:35:10 +0000
--- lisp/mouse.el       2010-04-30 23:30:48 +0000
***************
*** 1263,1273 ****
  
  ;; Momentarily show where the mark is, if highlighting doesn't show it.
  
- (defcustom mouse-region-delete-keys '([delete] [deletechar] [backspace])
-   "List of keys that should cause the mouse region to be deleted."
-   :group 'mouse
-   :type '(repeat key-sequence))
- 
  (defun mouse-show-mark ()
    (let ((inhibit-quit t)
        (echo-keystrokes 0)
--- 1263,1268 ----
***************
*** 1297,1304 ****
                                 'vertical-scroll-bar))
                        (and (memq 'down (event-modifiers event))
                             (not (key-binding key))
!                            (not (mouse-undouble-last-event events))
!                            (not (member key mouse-region-delete-keys)))))
        (and (consp event)
             (or (eq (car event) 'switch-frame)
                 (eq (posn-point (event-end event))
--- 1292,1298 ----
                                 'vertical-scroll-bar))
                        (and (memq 'down (event-modifiers event))
                             (not (key-binding key))
!                            (not (mouse-undouble-last-event events)))))
        (and (consp event)
             (or (eq (car event) 'switch-frame)
                 (eq (posn-point (event-end event))
***************
*** 1311,1332 ****
                      (setq events nil)))))))
      ;; If we lost the selection, just turn off the highlighting.
      (unless ignore
!       ;; For certain special keys, delete the region.
!       (if (member key mouse-region-delete-keys)
!         (progn
!           ;; Since notionally this is a separate command,
!           ;; run all the hooks that would be run if it were
!           ;; executed separately.
!           (run-hooks 'post-command-hook)
!           (setq last-command this-command)
!           (setq this-original-command 'delete-region)
!           (setq this-command (or (command-remapping this-original-command)
!                                  this-original-command))
!           (run-hooks 'pre-command-hook)
!           (call-interactively this-command))
!       ;; Otherwise, unread the key so it gets executed normally.
!       (setq unread-command-events
!             (nconc events unread-command-events))))
      (setq quit-flag nil)
      (unless transient-mark-mode
        (delete-overlay mouse-drag-overlay))))
--- 1305,1313 ----
                      (setq events nil)))))))
      ;; If we lost the selection, just turn off the highlighting.
      (unless ignore
!       ;; Unread the key so it gets executed normally.
!       (setq unread-command-events
!           (nconc events unread-command-events)))
      (setq quit-flag nil)
      (unless transient-mark-mode
        (delete-overlay mouse-drag-overlay))))




reply via email to

[Prev in Thread] Current Thread [Next in Thread]