]> code.delx.au - gnu-emacs-elpa/blobdiff - aggressive-indent.el
Exclude TeX-output-mode
[gnu-emacs-elpa] / aggressive-indent.el
index e2ed5d80391004cd434c85db554c7b7834295a26..b83775408e1752eff4323af3db1c4e2296494768 100644 (file)
@@ -4,7 +4,7 @@
 
 ;; Author: Artur Malabarba <bruce.connor.am@gmail.com>
 ;; URL: http://github.com/Bruce-Connor/aggressive-indent-mode
-;; Version: 0.3.1
+;; Version: 0.3.4
 ;; Package-Requires: ((emacs "24.1") (names "0.5") (cl-lib "0.5"))
 ;; Keywords: indent lisp maint tools
 ;; Prefix: aggressive-indent
 ;;
 
 ;;; Change Log:
-;; 0.3 - 2014/10/23 - Implement a smarter engine for non-lisp modes.
-;; 0.2 - 2014/10/20 - Reactivate `electric-indent-mode'.
-;; 0.2 - 2014/10/19 - Add variable `aggressive-indent-dont-indent-if', so the user can prevent indentation.
-;; 0.1 - 2014/10/15 - Release.
+;; 0.3.1 - 2014/10/30 - Define new delete-backward bound to backspace.
+;; 0.3   - 2014/10/23 - Implement a smarter engine for non-lisp modes.
+;; 0.2   - 2014/10/20 - Reactivate `electric-indent-mode'.
+;; 0.2   - 2014/10/19 - Add variable `aggressive-indent-dont-indent-if', so the user can prevent indentation.
+;; 0.1   - 2014/10/15 - Release.
 ;;; Code:
 
 (require 'cl-lib)
 (require 'names)
 
+(defmacro aggressive-indent--do-softly (&rest body)
+  "Execute body unobstrusively.
+This means:
+ 1. Do nothing in several situations, specified by
+    `aggressive-indent-dont-indent-if' and
+    `aggressive-indent--internal-dont-indent-if'.
+ 2. Silence all messages.
+ 3. Never throw errors.
+Meant for use in functions which go in hooks."
+  (declare (debug t))
+  `(unless (or (run-hook-wrapped
+                'aggressive-indent--internal-dont-indent-if
+                #'eval)
+               (aggressive-indent--run-user-hooks))
+     (cl-letf (((symbol-function 'message) #'ignore))
+       (ignore-errors ,@body))))
+
 ;;;###autoload
 (define-namespace aggressive-indent- :group indent
 
-(defconst version "0.3.1" "Version of the aggressive-indent.el package.")
+(defconst version "0.3.2" "Version of the aggressive-indent.el package.")
 (defun bug-report ()
   "Opens github issues page in a web browser. Please send any bugs you find.
 Please include your emacs and aggressive-indent versions."
@@ -104,16 +122,47 @@ Please include this in your report!"
 
 \f
 ;;; Start of actual Code:
+(defcustom dont-electric-modes '(ruby-mode)
+  "List of major-modes where `electric-indent-mode' should be disabled."
+  :type '(choice
+          (const :tag "Never use `electric-indent-mode'." t)
+          (repeat :tag "List of major-modes to avoid `electric-indent-mode'." symbol))
+  :package-version '(aggressive-indent . "0.3.1"))
+
 (defcustom excluded-modes
-  '(text-mode tabulated-list-mode special-mode
-              minibuffer-inactive-mode
-              yaml-mode jabber-chat-mode)
+  '(
+    bibtex-mode
+    coffee-mode
+    conf-mode
+    Custom-mode
+    diff-mode
+    dos-mode
+    erc-mode
+    jabber-chat-mode
+    haml-mode
+    haskell-mode
+    makefile-mode
+    minibuffer-inactive-mode
+    netcmd-mode
+    python-mode
+    sass-mode
+    slim-mode
+    special-mode
+    shell-mode
+    snippet-mode
+    eshell-mode
+    tabulated-list-mode
+    term-mode
+    TeX-output-mode
+    text-mode
+    yaml-mode
+    )
   "Modes in which `aggressive-indent-mode' should not be activated.
 This variable is only used if `global-aggressive-indent-mode' is
 active. If the minor mode is turned on with the local command,
 `aggressive-indent-mode', this variable is ignored."
   :type '(repeat symbol)
-  :package-version '(aggressive-indent . "0.2"))
+  :package-version '(aggressive-indent . "0.3.1"))
 
 (defcustom protected-commands '(undo undo-tree-undo undo-tree-redo)
   "Commands after which indentation will NOT be performed.
@@ -133,9 +182,11 @@ commands will NOT be followed by a re-indent."
     (region-active-p)
     buffer-read-only
     (null (buffer-modified-p))
-    (string-match "\\`[[:blank:]]*\n?\\'" (thing-at-point 'line))
+    (and (boundp 'smerge-mode) smerge-mode)
+    (string-match "\\`[[:blank:]]*\n?\\'" (or (thing-at-point 'line) ""))
     (and (not aggressive-indent-comments-too)
-         (aggressive-indent--in-comment-p)))
+         (aggressive-indent--in-comment-p))
+    (aggressive-indent--in-string-p))
   "List of forms which prevent indentation when they evaluate to non-nil.
 This is for internal use only. For user customization, use
 `aggressive-indent-dont-indent-if' instead.")
@@ -152,7 +203,6 @@ If current major mode is derived from one of these,
 it will call `aggressive-indent-indent-region-and-on' after every
 buffer change."
   :type '(repeat symbol)
-  :group 'aggressive-indent
   :package-version '(aggressive-indent . "0.3"))
 
 (eval-after-load 'yasnippet
@@ -171,12 +221,6 @@ buffer change."
      (add-to-list 'aggressive-indent--internal-dont-indent-if
                   'ac-completing)))
 
-(eval-after-load 'css-mode
-  '(add-hook
-    'css-mode-hook
-    (lambda () (unless defun-prompt-regexp
-            (setq-local defun-prompt-regexp "^[^[:blank:]].*")))))
-
 (defcustom dont-indent-if '()
   "List of variables and functions to prevent aggressive indenting.
 This variable is a list where each element is a lisp form.
@@ -209,24 +253,6 @@ erroring again."
             (setq -has-errored t)
             (message -error-message er))))))
 
-(defmacro -do-softly (&rest body)
-  "Execute body unobstrusively.
-This means: do nothing if mark is active (to avoid deactivaing
-it), or if buffer is not modified (to avoid creating accidental
-modifications), or if any of the forms in
-`aggressive-indent-dont-indent-if' evaluates to non-nil.
-
-Also, never throw errors nor messages.
-Meant for use in functions which go in hooks."
-  (declare (debug t))
-  `(unless (or (run-hook-wrapped
-                'aggressive-indent--internal-dont-indent-if
-                #'eval)
-               (aggressive-indent--run-user-hooks))
-     (ignore-errors
-       (cl-letf (((symbol-function 'message) #'ignore))
-         ,@body))))
-
 :autoload
 (defun indent-defun ()
   "Indent current defun.
@@ -247,9 +273,8 @@ Like `aggressive-indent-indent-defun', but wrapped in a
                'aggressive-indent--internal-dont-indent-if
                #'eval)
               (aggressive-indent--run-user-hooks))
-    (ignore-errors
-      (cl-letf (((symbol-function 'message) #'ignore))
-        (indent-defun)))))
+    (cl-letf (((symbol-function 'message) #'ignore))
+      (ignore-errors (indent-defun)))))
 
 :autoload
 (defun indent-region-and-on (l r)
@@ -260,22 +285,30 @@ until nothing more happens."
   (let ((p (point-marker))
         was-begining-of-line)
     (set-marker-insertion-type p t)
-    (goto-char r)
-    (setq was-begining-of-line
-          (= r (line-beginning-position)))
-    ;; Indent the affected region.
-    (unless (= l r) (indent-region l r))
-    ;; `indent-region' doesn't do anything if R was the beginning of a line, so we indent manually there.
-    (when was-begining-of-line
-      (indent-according-to-mode))
-    ;; And then we indent each following line until nothing happens.
-    (forward-line 1)
-    (while (/= (progn (skip-chars-forward "[:blank:]\n")
-                      (point))
-               (progn (indent-according-to-mode)
-                      (point)))
-      (forward-line 1))
-    (goto-char p)))
+    (unwind-protect
+        (progn
+          (goto-char r)
+          (setq was-begining-of-line
+                (= r (line-beginning-position)))
+          ;; If L is at the end of a line, skip that line.
+          (unless (= l r)
+            (goto-char l)
+            (when (= l (line-end-position))
+              (cl-incf l)))
+          ;; Indent the affected region.
+          (unless (= l r) (indent-region l r))
+          ;; `indent-region' doesn't do anything if R was the beginning of a line, so we indent manually there.
+          (when was-begining-of-line
+            (indent-according-to-mode))
+          ;; And then we indent each following line until nothing happens.
+          (forward-line 1)
+          (while (and (null (eobp))
+                      (/= (progn (skip-chars-forward "[:blank:]\n")
+                                 (point))
+                          (progn (indent-according-to-mode)
+                                 (point))))
+            (forward-line 1)))
+      (goto-char p))))
 
 (defun -softly-indent-region-and-on (l r &rest _)
   "Indent current defun unobstrusively.
@@ -285,48 +318,67 @@ Like `aggressive-indent-indent-region-and-on', but wrapped in a
                'aggressive-indent--internal-dont-indent-if
                #'eval)
               (aggressive-indent--run-user-hooks))
-    (ignore-errors
-      (cl-letf (((symbol-function 'message) #'ignore))
-        (indent-region-and-on l r)))))
+    (cl-letf (((symbol-function 'message) #'ignore))
+      (ignore-errors (indent-region-and-on l r)))))
 
-(defvar changed-list-right nil
+(defvar -changed-list-right nil
   "List of right limit of regions changed in the last command loop.")
 
-(defvar changed-list-left nil
+(defvar -changed-list-left nil
   "List of left limit of regions changed in the last command loop.")
 
 (defun -indent-if-changed ()
   "Indent any region that changed in the last command loop."
   (let ((inhibit-modification-hooks t))
-    (when changed-list-left
+    (when -changed-list-left
       (-softly-indent-region-and-on
-       (apply #'min changed-list-left)
-       (apply #'max changed-list-right))
-      (setq changed-list-left nil
-            changed-list-right nil))))
+       (apply #'min -changed-list-left)
+       (apply #'max -changed-list-right))
+      (setq -changed-list-left nil
+            -changed-list-right nil))))
 
 (defun -keep-track-of-changes (l r &rest _)
   "Store the limits of each change that happens in the buffer."
-  (push l changed-list-left)
-  (push r changed-list-right))
+  (push l -changed-list-left)
+  (push r -changed-list-right))
 
 (defun -in-comment-p ()
   "Return non-nil if point is inside a comment.
 Assumes that the syntax table is sufficient to find comments."
   (nth 4 (syntax-ppss)))
 
+(defun -in-string-p ()
+  "Return non-nil if point is inside a string.
+Assumes that the syntax table is sufficient for recognizing
+strings."
+  (nth 3 (syntax-ppss)))
+
 \f
 ;;; Minor modes
 :autoload
 (define-minor-mode mode nil nil " =>"
-  '(("\C-c\C-q" . aggressive-indent-indent-defun))
+  '(("\ 3\11" . aggressive-indent-indent-defun)
+    ([backspace] menu-item "maybe-delete-indentation" ignore
+     :filter (lambda (&optional _)
+               (when (and (looking-back "^[[:blank:]]+")
+                          ;; Wherever we don't want to indent, we probably also
+                          ;; want the default backspace behavior.
+                          (not (run-hook-wrapped
+                                'aggressive-indent--internal-dont-indent-if
+                                #'eval))
+                          (not (aggressive-indent--run-user-hooks)))
+                 #'delete-indentation))))
   (if mode
       (if (and global-aggressive-indent-mode
                (or (cl-member-if #'derived-mode-p excluded-modes)
+                   (memq major-mode '(text-mode fundamental-mode))
                    buffer-read-only))
           (mode -1)
-        (when (fboundp 'electric-indent-local-mode)
-          (electric-indent-local-mode 1))
+        ;; Should electric indent be ON or OFF?
+        (if (or (eq dont-electric-modes t)
+                (cl-member-if #'derived-mode-p dont-electric-modes))
+            (-local-electric nil)
+          (-local-electric t))
         (if (cl-member-if #'derived-mode-p modes-to-prefer-defun)
             (add-hook 'post-command-hook #'-softly-indent-defun nil 'local)
           (add-hook 'after-change-functions #'-keep-track-of-changes nil 'local)
@@ -336,6 +388,12 @@ Assumes that the syntax table is sufficient to find comments."
     (remove-hook 'post-command-hook #'-indent-if-changed 'local)
     (remove-hook 'post-command-hook #'-softly-indent-defun 'local)))
 
+(defun -local-electric (on)
+  "Turn `electric-indent-mode' on or off locally, as given by boolean ON."
+  (if (fboundp 'electric-indent-local-mode)
+      (electric-indent-local-mode (if on 1 -1))
+    (set (make-local-variable 'electric-indent-mode) on)))
+
 :autoload
 (define-globalized-minor-mode global-aggressive-indent-mode
   mode mode)