Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

backward-delete-char-untabify-method

11 views
Skip to first unread message

Sam Halliday

unread,
Jun 8, 2003, 2:05:40 PM6/8/03
to
hi there,

the docs mention that the only options available to
`backward-delete-char-untabify-method' are:

`untabify' -- turn a tab to many spaces, then delete one space;
`hungry' -- delete all whitespace, both tabs and spaces;
`all' -- delete all whitespace, including tabs, spaces and newlines;
nil -- just delete one character.

but none of these do _exactly_ what i am after. what i would like is
similar to `hungry', but not the same. if i have some indented text:

line 1
line 2
line 3
A

when the cursor is at `A', if i press BACKSPACE, the cursor will go to
the beginning of the line, but i would prefer it to go to the same
indentation as "line 3", another BACKSPACE bringing me to "line 2" and
requiring a third to bring me to the beginning of the line. i.e. I would
prefer a `delete back one level of indentation' option.

could somebody please tell me if is this possible? and how to set it up
(or where to look)?

my ~/.emacs (for 21.3.x) is at
http://www.ma.hw.ac.uk/~samuel/emacs
but to save the trip, the relevant part is:

(setq backward-delete-char-untabify-method 'hungry)
(global-set-key "\C-?" 'backward-delete-char-untabify)

cheers,
Sam

Kai Großjohann

unread,
Jun 8, 2003, 3:54:59 PM6/8/03
to
Sam Halliday <dev...@example.com> writes:

> when the cursor is at `A', if i press BACKSPACE, the cursor will go to
> the beginning of the line, but i would prefer it to go to the same
> indentation as "line 3", another BACKSPACE bringing me to "line 2" and
> requiring a third to bring me to the beginning of the line. i.e. I would
> prefer a `delete back one level of indentation' option.
>
> could somebody please tell me if is this possible? and how to set it up
> (or where to look)?

I'm afraid you'll have to do it yourself :-/ It would require some
moving about in the buffer to find the right spots to go to, as well.
--
This line is not blank.

Stefan Monnier

unread,
Jun 8, 2003, 4:46:15 PM6/8/03
to
> line 1
> line 2
> line 3
> A

> when the cursor is at `A', if i press BACKSPACE, the cursor will go to
> the beginning of the line, but i would prefer it to go to the same
> indentation as "line 3", another BACKSPACE bringing me to "line 2" and
> requiring a third to bring me to the beginning of the line. i.e. I would
> prefer a `delete back one level of indentation' option.

Sounds like a cool idea.

> could somebody please tell me if is this possible?

Is that a troll ?

> and how to set it up (or where to look)?

I think you'll have to make it from scratch (and a few other ingredients).

(defun sam-backspace ()
"Delete space backward to prev level of indentation."
(interactive)
(if (or (bolp) (save-excursion (skip-chars-backward " \t") (not (bolp))))
;; If we're not inside indentation, behave as usual.
(call-interactively 'backward-delete-char-untabify)
;; We're inside indentation.
(let* ((col (current-column))
(destcol
(save-excursion
;; Skip previous lines that are more indented than us.
(while (and (not (bobp))
(zerop (forward-line -1))
(skip-chars-forward " \t")
(>= (current-column) col)))
(current-column))))
(delete-region (point) (progn (move-to-column destcol) (point))))))


-- Stefan

Sam Halliday

unread,
Jun 8, 2003, 5:22:13 PM6/8/03
to
Stefan Monnier wrote:

> Sam Halliday wrote:
> > line 1
> > line 2
> > line 3
> > A
> > when the cursor is at `A', if i press BACKSPACE, the cursor will go
> > to the beginning of the line, but i would prefer it to go to the
> > same indentation as "line 3", another BACKSPACE bringing me to "line
> > 2" and requiring a third to bring me to the beginning of the line.
> > i.e. I would prefer a `delete back one level of indentation' option.
> Sounds like a cool idea.

> > could somebody please tell me if is this possible?
> Is that a troll ?

:-D

> > and how to set it up (or where to look)?
> I think you'll have to make it from scratch (and a few other

> ingredients).(defun sam-backspace ()


> "Delete space backward to prev level of indentation."
> (interactive)
> (if (or (bolp) (save-excursion (skip-chars-backward " \t") (not
> (bolp))))
> ;; If we're not inside indentation, behave as usual.
> (call-interactively 'backward-delete-char-untabify)
> ;; We're inside indentation.
> (let* ((col (current-column))
> (destcol
> (save-excursion
> ;; Skip previous lines that are more indented than us.
> (while (and (not (bobp))
> (zerop (forward-line -1))
> (skip-chars-forward " \t")
> (>= (current-column) col)))
> (current-column))))
> (delete-region (point) (progn (move-to-column destcol)
> (point))))))

wooah woah; im very new to elisp (and by exposure only) so you'll have
to be patient here... but from checking this out very quickly (one
indenting example)... it seems to do exactly what i am after!! thank you
very much Stefan :-D

i of course renamed the function to something more appropriate; i
figured `backward-delete-char-tablevel' was best.

thank you again!! i recommend this function to go into Emacs... its only
a small function and i am surprised nobody has ever requested it before.

cheers,
Sam

Sam Halliday

unread,
Jun 8, 2003, 5:37:12 PM6/8/03
to
Sam Halliday wrote:

> Stefan Monnier wrote:
> > I think you'll have to make it from scratch (and a few other
> > (defun sam-backspace ()
<snip>

> wooah woah; im very new to elisp (and by exposure only) so you'll have
> to be patient here... but from checking this out very quickly (one
> indenting example)... it seems to do exactly what i am after!! thank
> you very much Stefan :-D

ok, this does not seem to be perfect just yet; it doesnt seem to work in
elisp mode for example, but is doing as expected in LaTeX mode;
especially if i keep my untabify-method=hungry.

you can take as an example the function itself... go to the 13th line
which begins "(zerop..." and type BACKSPACE there, i would expect it to
delete back to just below "(while...", but instead
backward-delete-char-untabify is being called.

cheers,
Sam

Stefan Monnier

unread,
Jun 8, 2003, 6:24:25 PM6/8/03
to
> ok, this does not seem to be perfect just yet; it doesnt seem to work in
> elisp mode for example, but is doing as expected in LaTeX mode;
> especially if i keep my untabify-method=hungry.

I suspect a "pilot error on your part".
DEL is rebound to backward-delete-char-untabify in elisp mode, so
when you hit backspace, it probably doesn't run the new code at all.
You can also call it a misfeature of elisp-mode (and C mode as well,
IIRC). Maybe a bug-report/feature-request about it is in order.


Stefan

Sam Halliday

unread,
Jun 8, 2003, 7:16:14 PM6/8/03
to

yes you are correct about c-mode as well; damn, i will be using that
mode. can you think of a workaround? this might also help me with my
flyspell-hogging-M-TAB problems as well, if i can work out how to force
a keybinding.

should i make the report, or will you?

Sam

Kai Großjohann

unread,
Jun 9, 2003, 5:34:50 AM6/9/03
to
Sam Halliday <dev...@example.com> writes:

> yes you are correct about c-mode as well; damn, i will be using that
> mode. can you think of a workaround?

You mean, bind DEL to your command in those modes?

(defun sam-keys ()
(local-set-key (kbd "DEL") 'sam-backspace))
(add-hook 'c-mode-hook 'sam-keys)
(add-hook 'emacs-lisp-mode-hook 'sam-keys)

Kai Großjohann

unread,
Jun 9, 2003, 5:35:43 AM6/9/03
to
Sam Halliday <dev...@example.com> writes:

> i of course renamed the function to something more appropriate; i
> figured `backward-delete-char-tablevel' was best.

IMHO it would be useful if you kept a special prefix, say sam or sh.
Otherwise, you might be surprised by a new Emacs with its own
definition of the function backward-delete-char-tablevel.

Sam Halliday

unread,
Jun 9, 2003, 5:50:02 AM6/9/03
to
Kai Großjohann wrote:

> Sam Halliday writes:
> > yes you are correct about c-mode as well; damn, i will be using that
> > mode. can you think of a workaround?
>
> You mean, bind DEL to your command in those modes?
>
> (defun sam-keys ()
> (local-set-key (kbd "DEL") 'sam-backspace))
> (add-hook 'c-mode-hook 'sam-keys)
> (add-hook 'emacs-lisp-mode-hook 'sam-keys)

thank you very much Kai and Stefan, your help is greatly appreciated!

Sam

0 new messages