From 7b9c2d5f1e976f0a0de582bc9abaee8464fd7bd7 Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Sun, 4 May 2008 13:03:06 -0700 Subject: [PATCH] [Emacs] Have haml-electric-backspace optionally back-dent the entire nested block. --- extra/haml-mode.el | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/extra/haml-mode.el b/extra/haml-mode.el index 4c9da337..9a3e3c98 100644 --- a/extra/haml-mode.el +++ b/extra/haml-mode.el @@ -33,6 +33,13 @@ :type 'integer :group 'haml) +(defcustom haml-backspace-backdents-nesting t + "Non-nil to have `haml-electric-backspace' re-indent all code +nested beneath the backspaced line be re-indented along with the +line itself." + :type 'boolean + :group 'haml) + (defface haml-tab-face '((((class color)) (:background "hotpink")) (t (:reverse-video t))) @@ -265,17 +272,33 @@ back-dent the line by `haml-indent-offset' spaces. On reaching column (defun haml-electric-backspace (arg) "Delete characters or back-dent the current line. -If invoked following only whitespace on a line, will back-dent to the -immediately previous multiple of `haml-indent-offset' spaces." +If invoked following only whitespace on a line, will back-dent +the line and all nested lines to the immediately previous +multiple of `haml-indent-offset' spaces. + +Set `haml-backspace-backdents-nesting' to nil to just back-dent +the current line." (interactive "*p") - (if (or (/= (current-indentation) (current-column)) (bolp)) + (if (or (/= (current-indentation) (current-column)) + (bolp) + (looking-at "^[ \t]+$")) (backward-delete-char arg) (let ((ci (current-column))) (beginning-of-line) - (delete-horizontal-space) - (indent-to (* (/ (- ci (* arg haml-indent-offset)) - haml-indent-offset) - haml-indent-offset))))) + (if (not haml-backspace-backdents-nesting) + (set-mark (save-excursion (end-of-line) (point))) + (mark-sexp) + (set-mark + (save-excursion + (goto-char (mark)) + (beginning-of-line) + (point)))) + (save-excursion + (replace-regexp (concat "^" (make-string ci ? )) + (make-string (max 0 (- ci (* arg haml-indent-offset))) ? ) + nil (point) (mark))) + (back-to-indentation) + (pop-mark)))) ;; Setup/Activation