[alt.sources] Move back to previous indentation level

mcgrath%paris.Berkeley.EDU@GINGER.BERKELEY.EDU (Roland McGrath) (12/09/89)

Original-posting-by: mcgrath%paris.Berkeley.EDU@GINGER.BERKELEY.EDU (Roland McGrath)
Reposted-by: emv@math.lsa.umich.edu (Edward Vielmetti)
Posting-id: 891208.2044
Posting-number: Volume TEST, Number TEST
Archive-name: back-indent.el, reverse of indent-relative for gnu emacs

[This is an experimental alt.sources re-posting from the
newsgroup(s) gnu.emacs.bug.
No attempt has been made to edit, clean, modify, or otherwise
change the contents of the original posting, or to contact the
author.  Please consider cross-posting all sources postings to
alt.sources as a matter of course.]

[Comments on this service to emv@math.lsa.umich.edu (Edward Vielmetti)]

Here is back-indent.el, which implements the function back-indent-relative.
This is the reverse of indent-relative (TAB in indented-text-mode).  It moves
back one indentation level, where an indentation level is defined by how far
the preceding lines are indented.

For example, in indented-text-mode, if you have:

* foo
    * bar
        * ack
    

Then point will be under the `*' in "* ack".  If you then do
back-indent-relative, point will move back to being under the `*' in "* bar",
deleting the spaces as if you had hit DEL four times.  If you do it again,
point will move to the beginning of the line.

I bind this to ESC TAB in indented-text-mode.


;;; Copyright (C) 1989 Roland McGrath
;;;
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 1, or (at your option)
;;; any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; A copy of the GNU General Public License can be obtained from this
;;; program's author (send electronic mail to roland@ai.mit.edu) or from
;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
;;; 02139, USA.
;;;
;;; Send bug reports to roland@ai.mit.edu.

;; To use this, byte-compile this file, and then put the following line
;; in your .emacs (or site-init.el, etc.):
;;  (autoload 'back-indent-relative "back-indent" "Reverse-indent." t)


(defun back-indent-relative ()
  "Move back an indentation level, deleting spaces and tabs.
Indentation levels are defined by the amount of leading whitespace on
the preceding lines.  This is essentially the reverse of \\[indent-relative]."
  (interactive)
  (let* ((start-column (current-indentation))
	 (indent start-column))
    (save-excursion
      (while (and (>= indent start-column)
		  (progn (beginning-of-line)
			 (re-search-backward "^[^\n]" nil t)))
	(setq indent (current-indentation))))
    (backward-delete-char-untabify (- start-column indent) nil)))