]> code.delx.au - gnu-emacs-elpa/blobdiff - aggressive-indent.el
Don't indent if the user is starting to type a comment. Fix #51
[gnu-emacs-elpa] / aggressive-indent.el
index 7ea9cf252659f450d98ce52b87e063ba1d2b7cc0..dfa431a93bbcc1e4188a8eebe4ee530a46de16b4 100644 (file)
@@ -1,11 +1,11 @@
 ;;; aggressive-indent.el --- Minor mode to aggressively keep your code always indented
 
-;; Copyright (C) 2014 Artur Malabarba <bruce.connor.am@gmail.com>
+;; Copyright (C) 2014 Free Software Foundation, Inc.
 
-;; Author: Artur Malabarba <bruce.connor.am@gmail.com>
-;; URL: http://github.com/Bruce-Connor/aggressive-indent-mode
-;; Version: 0.3.5
-;; Package-Requires: ((emacs "24.1") (names "0.5") (cl-lib "0.5"))
+;; Author: Artur Malabarba <emacs@endlessparentheses.com>
+;; URL: http://github.com/Malabarba/aggressive-indent-mode
+;; Version: 1.1.1
+;; Package-Requires: ((emacs "24.1") (names "20150125.9") (cl-lib "0.5"))
 ;; Keywords: indent lisp maint tools
 ;; Prefix: aggressive-indent
 ;; Separator: -
 ;;; Commentary:
 ;;
 ;; `electric-indent-mode' is enough to keep your code nicely aligned when
-;; all you do is type. However, once you start shifting blocks around,
+;; all you do is type.  However, once you start shifting blocks around,
 ;; transposing lines, or slurping and barfing sexps, indentation is bound
 ;; to go wrong.
 ;;
 ;; `aggressive-indent-mode' is a minor mode that keeps your code always
-;; indented. It reindents after every command, making it more reliable
+;; indented.  It reindents after every change, making it more reliable
 ;; than `electric-indent-mode'.
 ;;
 ;; ### Instructions ###
@@ -33,7 +33,7 @@
 ;;     (add-hook 'css-mode-hook #'aggressive-indent-mode)
 ;;
 ;; You can use this hook on any mode you want, `aggressive-indent' is not
-;; exclusive to emacs-lisp code. In fact, if you want to turn it on for
+;; exclusive to emacs-lisp code.  In fact, if you want to turn it on for
 ;; every programming mode, you can do something like:
 ;;
 ;;     (global-aggressive-indent-mode 1)
 ;;; 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))))
+(eval-when-compile (require 'names))
 
 ;;;###autoload
-(define-namespace aggressive-indent- :group indent
+(define-namespace aggressive-indent-
+:group indent
 
-(defconst version "0.3.5" "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."
+  "Opens github issues page in a web browser.  Please send any bugs you find.
+Please include your Emacs and `aggressive-indent' versions."
   (interactive)
+  (require 'lisp-mnt)
   (message "Your `aggressive-indent-version' is: %s, and your emacs version is: %s.
 Please include this in your report!"
-           version emacs-version)
+    (lm-version (find-library-name "aggressive-indent"))
+    emacs-version)
   (browse-url "https://github.com/Bruce-Connor/aggressive-indent-mode/issues/new"))
 
 \f
 ;;; Start of actual Code:
 (defcustom dont-electric-modes '(ruby-mode)
-  "List of major-modes where `electric-indent-mode' should be disabled."
+  "List of major-modes where `electric-indent' 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))
@@ -132,15 +117,19 @@ Please include this in your report!"
 (defcustom excluded-modes
   '(
     bibtex-mode
+    cider-repl-mode
     coffee-mode
+    comint-mode
     conf-mode
     Custom-mode
     diff-mode
+    doc-view-mode
     dos-mode
     erc-mode
     jabber-chat-mode
     haml-mode
     haskell-mode
+    image-mode
     makefile-mode
     makefile-gmake-mode
     minibuffer-inactive-mode
@@ -160,7 +149,7 @@ Please include this in your report!"
     )
   "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,
+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.3.1"))
@@ -184,25 +173,36 @@ commands will NOT be followed by a re-indent."
     buffer-read-only
     (null (buffer-modified-p))
     (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-string-p))
+    (let ((line (thing-at-point 'line)))
+      (when (stringp line)
+        (or (string-match "\\`[[:blank:]]*\n?\\'" line)
+            ;; If the user is starting to type a comment.
+            (and (stringp comment-start)
+                 (string-match (concat "\\`[[:blank:]]*"
+                                       (substring comment-start 0 1)
+                                       "[[:blank:]]*$")
+                               line)))))
+    (let ((sp (syntax-ppss)))
+      ;; Comments.
+      (or (and (not aggressive-indent-comments-too) (elt sp 4))
+          ;; Strings.
+          (elt sp 3))))
   "List of forms which prevent indentation when they evaluate to non-nil.
-This is for internal use only. For user customization, use
+This is for internal use only.  For user customization, use
 `aggressive-indent-dont-indent-if' instead.")
 
-(defcustom modes-to-prefer-defun '(emacs-lisp-mode lisp-mode scheme-mode)
+(defcustom modes-to-prefer-defun
+  '(emacs-lisp-mode lisp-mode scheme-mode clojure-mode)
   "List of major-modes in which indenting defun is preferred.
 Add here any major modes with very good definitions of
 `end-of-defun' and `beginning-of-defun', or modes which bug out
 if you have `after-change-functions' (such as paredit).
 
 If current major mode is derived from one of these,
-`aggressive-indent-mode' will call
-`aggressive-indent-indent-defun' after every command. Otherwise,
-it will call `aggressive-indent-indent-region-and-on' after every
-buffer change."
+`aggressive-indent' will call `aggressive-indent-indent-defun'
+after every command.  Otherwise, it will call
+`aggressive-indent-indent-region-and-on' after every buffer
+change."
   :type '(repeat symbol)
   :package-version '(aggressive-indent . "0.3"))
 
@@ -221,10 +221,19 @@ buffer change."
   '(when (boundp 'ac-completing)
      (add-to-list 'aggressive-indent--internal-dont-indent-if
                   'ac-completing)))
+(eval-after-load 'iedit
+  '(when (boundp 'iedit-mode)
+     (add-to-list 'aggressive-indent--internal-dont-indent-if
+                  'iedit-mode)))
+(eval-after-load 'coq
+  '(add-to-list 'aggressive-indent--internal-dont-indent-if
+                '(and (derived-mode-p 'coq-mode)
+                      (not (string-match "\\.[[:space:]]*$"
+                                         (thing-at-point 'line))))))
 
 (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.
+This variable is a list where each element is a Lisp form.
 As long as any one of these forms returns non-nil,
 aggressive-indent will not perform any indentation.
 
@@ -249,33 +258,38 @@ erroring again."
        (condition-case er
            (prog1 (eval (cons 'or dont-indent-if))
              (setq -has-errored nil))
-         (error
-          (unless -has-errored
-            (setq -has-errored t)
-            (message -error-message er))))))
+         (error (unless -has-errored
+                  (setq -has-errored t)
+                  (message -error-message er))))))
 
+\f
 :autoload
-(defun indent-defun ()
+(defun indent-defun (&optional l r)
   "Indent current defun.
-Throw an error if parentheses are unbalanced."
+Throw an error if parentheses are unbalanced.
+If L and R are provided, use them for finding the start and end of defun."
   (interactive)
   (let ((p (point-marker)))
     (set-marker-insertion-type p t)
     (indent-region
-     (save-excursion (beginning-of-defun 1) (point))
-     (save-excursion (end-of-defun 1) (point)))
+     (save-excursion
+       (when l (goto-char l))
+       (beginning-of-defun 1) (point))
+     (save-excursion
+       (when r (goto-char r))
+       (end-of-defun 1) (point)))
     (goto-char p)))
 
-(defun -softly-indent-defun ()
+(defun -softly-indent-defun (&optional l r)
   "Indent current defun unobstrusively.
-Like `aggressive-indent-indent-defun', but wrapped in a
-`aggressive-indent--do-softly'."
+Like `aggressive-indent-indent-defun', but without errors or
+messages.  L and R passed to `aggressive-indent-indent-defun'."
   (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 (indent-defun)))))
+      (ignore-errors (indent-defun l r)))))
 
 :autoload
 (defun indent-region-and-on (l r)
@@ -303,18 +317,25 @@ until nothing more happens."
             (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)))
+          (skip-chars-forward "[:blank:]\n")
+          (let* ((eod (ignore-errors
+                        (save-excursion (end-of-defun)
+                                        (point-marker))))
+                 (point-limit (if (and eod (< (point) eod))
+                                  eod (point-max-marker))))
+            (while (and (null (eobp))
+                        (< (point) point-limit)
+                        (/= (point)
+                            (progn (indent-according-to-mode)
+                                   (point))))
+              (forward-line 1)
+              (skip-chars-forward "[:blank:]\n"))))
       (goto-char p))))
 
 (defun -softly-indent-region-and-on (l r &rest _)
-  "Indent current defun unobstrusively.
-Like `aggressive-indent-indent-region-and-on', but wrapped in a
-`aggressive-indent--do-softly'."
+  "Indent region between L and R, and a bit more.
+Like `aggressive-indent-indent-region-and-on', but without errors
+or messages."
   (unless (or (run-hook-wrapped
                'aggressive-indent--internal-dont-indent-if
                #'eval)
@@ -322,42 +343,32 @@ Like `aggressive-indent-indent-region-and-on', but wrapped in a
     (cl-letf (((symbol-function 'message) #'ignore))
       (ignore-errors (indent-region-and-on l r)))))
 
-(defvar -changed-list-right nil
-  "List of right limit of regions changed in the last command loop.")
-
-(defvar -changed-list-left nil
-  "List of left limit of regions changed in the last command loop.")
+(defvar -changed-list nil
+  "List of (left right) 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
-      (-softly-indent-region-and-on
-       (apply #'min -changed-list-left)
-       (apply #'max -changed-list-right))
-      (setq -changed-list-left nil
-            -changed-list-right nil))))
+  (when -changed-list
+    (while-no-input
+      (let ((inhibit-modification-hooks t)
+            (inhibit-point-motion-hooks t)
+            (indent-function
+             (if (cl-member-if #'derived-mode-p modes-to-prefer-defun)
+                 #'-softly-indent-defun
+               #'-softly-indent-region-and-on)))
+        (while -changed-list
+          (apply indent-function (car -changed-list))
+          (setq -changed-list (cdr -changed-list)))))))
 
 (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))
-
-(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)))
+  "Store the limits (L and R) of each change in the buffer."
+  (push (list l r) -changed-list))
 
 \f
 ;;; Minor modes
 :autoload
-(define-minor-mode mode nil nil " =>"
+(define-minor-mode mode
+  nil nil " =>"
   '(("\ 3\11" . aggressive-indent-indent-defun)
     ([backspace] menu-item "maybe-delete-indentation" ignore
      :filter (lambda (&optional _)
@@ -380,17 +391,16 @@ strings."
                 (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)
-          (add-hook 'post-command-hook #'-indent-if-changed nil 'local)))
+        (add-hook 'after-change-functions #'-keep-track-of-changes nil 'local)
+        ;; (add-hook 'post-command-hook #'-softly-indent-defun nil 'local)
+        (add-hook 'post-command-hook #'-indent-if-changed nil 'local))
     ;; Clean the hooks
     (remove-hook 'after-change-functions #'-keep-track-of-changes 'local)
     (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."
+  "Turn variable `electric-indent-mode' on or off locally, as per boolean ON."
   (if (fboundp 'electric-indent-local-mode)
       (electric-indent-local-mode (if on 1 -1))
     (set (make-local-variable 'electric-indent-mode) on)))
@@ -405,4 +415,4 @@ strings."
 )
 
 (provide 'aggressive-indent)
-;;; aggressive-indent.el ends here.
+;;; aggressive-indent.el ends here