emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/adjust-parens d5f8a67 5/8: * adjust-parens.el (adjust-p


From: Stefan Monnier
Subject: [elpa] externals/adjust-parens d5f8a67 5/8: * adjust-parens.el (adjust-parens-mode): Convert to minor mode
Date: Tue, 1 Dec 2020 15:12:00 -0500 (EST)

branch: externals/adjust-parens
commit d5f8a67188c160de2b62f66210ea8145514a45c9
Author: Ryan C. Thompson <rct@thompsonclan.org>
Commit: Barry O'Reilly <gundaetiapo@gmail.com>

    * adjust-parens.el (adjust-parens-mode): Convert to minor mode
    (adjust-parens-p): Only adjust parens if indentation is
    correct, else defer to indent-for-tab-command.
    (adjust-parens-and-indent): Take numeric prefix instead of raw
    and handle non positive prefix arg.
    (adjust-parens-and-indent): Return t from function.
    (lisp-dedent-adjust-parens): Return nil iff no change to buffer.
    * adjust-parens-tests.el (apt-mode-test): Add tests for minor mode
    enabling/disabling
    (apt-indent-dedent-test): Add tests for behavior when indentation
    is incorrect or point is not at end of indentation
    * Makefile: Have check target not depend on .elc files, so as
    it is possible to make check and get non byte compiled
    backtraces if there's a failure.
---
 Makefile               |  3 ++-
 adjust-parens-tests.el | 55 ++++++++++++++++++++++++++++++++++++++++++--
 adjust-parens.el       | 62 +++++++++++++++++++++++++++++++++++++-------------
 3 files changed, 101 insertions(+), 19 deletions(-)

diff --git a/Makefile b/Makefile
index 30b1e3f..d4e5b94 100644
--- a/Makefile
+++ b/Makefile
@@ -11,6 +11,7 @@ all: $(ELCFILES)
 clean:
        @rm -f *.elc
 
-check: $(ELCFILES)
+# Don't depend on $(ELCFILES) so as failures may have a non byte compiled 
backtrace
+check:
        @emacs --batch -q --no-site-file -L . -l adjust-parens-tests.el -f 
ert-run-tests-batch-and-exit
 
diff --git a/adjust-parens-tests.el b/adjust-parens-tests.el
index 5b249c5..b0118ff 100644
--- a/adjust-parens-tests.el
+++ b/adjust-parens-tests.el
@@ -32,9 +32,23 @@
                    (buffer-substring-no-properties (point)
                                                    (point-max)))))
 
+(ert-deftest apt-mode-test ()
+  (with-temp-buffer
+    (emacs-lisp-mode)
+    (adjust-parens-mode -1)
+    (should-not (eq (key-binding (kbd "TAB"))
+                    #'lisp-indent-adjust-parens))
+    (adjust-parens-mode 1)
+    (should (eq (key-binding (kbd "TAB"))
+                #'lisp-indent-adjust-parens))
+    (adjust-parens-mode -1)
+    (should-not (eq (key-binding (kbd "TAB"))
+                    #'lisp-indent-adjust-parens))))
+
 (ert-deftest apt-near-bob-test ()
   (with-temp-buffer
     (emacs-lisp-mode)
+    (adjust-parens-mode 1)
     (insert "(foo)\n")
     (lisp-indent-adjust-parens)
     (apt-check-buffer "(foo\n " ")")))
@@ -42,11 +56,12 @@
 (ert-deftest apt-indent-dedent-test ()
   (with-temp-buffer
     (emacs-lisp-mode)
+    (adjust-parens-mode 1)
     (setq indent-tabs-mode nil)
     (insert ";;\n"
             "(let ((x 10) (y (some-func 20))))\n"
             "; Comment")
-    (beginning-of-line)
+    (back-to-indentation)
     (lisp-indent-adjust-parens)
     (apt-check-buffer (concat ";;\n"
                               "(let ((x 10) (y (some-func 20)))\n"
@@ -61,6 +76,42 @@
     (apt-check-buffer (concat ";;\n"
                               "(let ((x 10) (y (some-func 20))\n"
                               "      ")
-                      ")); Comment")))
+                      ")); Comment")
+    ;; Check what happens when point is not at the indentation, or
+    ;; indentation is not correct, or both
+    (beginning-of-line)                 ; Point not at indentation
+    ;; Should simply move point to indentation and not change buffer
+    (lisp-indent-adjust-parens)
+    (apt-check-buffer (concat ";;\n"
+                              "(let ((x 10) (y (some-func 20))\n"
+                              "      ")
+                      ")); Comment")
+
+    ;; Same check for dedent
+    (beginning-of-line)                 ; Point not at indentation
+    ;; Should leave point unchanged
+    (lisp-dedent-adjust-parens)
+    (apt-check-buffer (concat ";;\n"
+                              "(let ((x 10) (y (some-func 20))\n"
+                              "")
+                      "      )); Comment")
+
+    (back-to-indentation)
+    (delete-backward-char 3)            ; Incorrect indentation
+    ;; Should reindent line via indent-for-tab-command and move point to
+    ;; indentation but not change parens
+    (lisp-indent-adjust-parens)
+    (apt-check-buffer (concat ";;\n"
+                              "(let ((x 10) (y (some-func 20))\n"
+                              "      ")
+                      ")); Comment")
+    (insert "   ")                      ; Wrong indentation
+    (forward-char 2)                    ; Point is past indentation
+    ;; Should reindent line without moving point or changing parens
+    (lisp-indent-adjust-parens)
+    (apt-check-buffer (concat ";;\n"
+                              "(let ((x 10) (y (some-func 20))\n"
+                              "      ))")
+                      "; Comment")))
 
 ;;; adjust-parens-tests.el ends here
diff --git a/adjust-parens.el b/adjust-parens.el
index 0ba2e6b..4834b10 100644
--- a/adjust-parens.el
+++ b/adjust-parens.el
@@ -3,7 +3,7 @@
 ;; Copyright (C) 2013  Free Software Foundation, Inc.
 
 ;; Author: Barry O'Reilly <gundaetiapo@gmail.com>
-;; Version: 1.3
+;; Version: 2.0
 
 ;; 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
@@ -36,6 +36,9 @@
 ;;
 ;; To use:
 ;;   (require 'adjust-parens)
+;;   (add-hook 'emacs-lisp-mode-hook #'adjust-parens-mode)
+;;   (add-hook 'clojure-mode-hook #'adjust-parens-mode)
+;;   ;; etc
 ;;
 ;; This binds two keys in Lisp Mode:
 ;;   (local-set-key (kbd "TAB") 'lisp-indent-adjust-parens)
@@ -246,8 +249,15 @@ scan-error to propogate up."
   (save-excursion
     (let ((orig-pos (point)))
       (back-to-indentation)
-      (and (not (use-region-p))
-           (<= orig-pos (point))))))
+      (and (= orig-pos (point))
+           (not (use-region-p))
+           ;; Current line indented?
+           (let ((indent (calculate-lisp-indent)))
+             (and indent
+                  (= (current-column)
+                     (if (listp indent)
+                         (car indent)
+                       indent))))))))
 
 (defun adjust-parens-and-indent (adjust-function parg)
   "Adjust close parens and indent the region over which the parens
@@ -271,7 +281,8 @@ moved."
                      (setq finished t)))
                (scan-error (setq finished err))))
     (apply 'indent-region region-of-change))
-  (back-to-indentation))
+  (back-to-indentation)
+  t)
 
 (defun lisp-indent-adjust-parens (&optional parg)
   "Indent Lisp code to the next level while adjusting sexp balanced
@@ -279,26 +290,45 @@ expressions to be consistent.
 
 This command can be bound to TAB instead of indent-for-tab-command. It
 potentially calls the latter."
-  (interactive "P")
+  (interactive "p")
   (if (adjust-parens-p)
-      (adjust-parens-and-indent 'adjust-close-paren-for-indent
-                                parg)
+      (adjust-parens-and-indent
+       (if (and parg (< parg 0))
+           #'adjust-close-paren-for-dedent
+         #'adjust-close-paren-for-indent)
+       (and parg (abs parg)))
     (indent-for-tab-command parg)))
 
 (defun lisp-dedent-adjust-parens (&optional parg)
   "Dedent Lisp code to the previous level while adjusting sexp
 balanced expressions to be consistent.
 
+Returns t iff this function changed the buffer.
+
 Binding to <backtab> (ie Shift-Tab) is a sensible choice."
-  (interactive "P")
-  (when (adjust-parens-p)
-    (adjust-parens-and-indent 'adjust-close-paren-for-dedent
-                              parg)))
-
-(add-hook 'emacs-lisp-mode-hook
-          (lambda ()
-            (local-set-key (kbd "TAB") 'lisp-indent-adjust-parens)
-            (local-set-key (kbd "<backtab>") 'lisp-dedent-adjust-parens)))
+  (interactive "p")
+  (if (adjust-parens-p)
+      (adjust-parens-and-indent
+       (if (and parg (< parg 0))
+           #'adjust-close-paren-for-indent
+         'adjust-close-paren-for-dedent)
+       (and parg (abs parg)))
+    nil))
+
+(defgroup adjust-parens nil
+  "Indent and dedent Lisp code, automatically adjust close parens."
+  :prefix "adjust-parens-"
+  :group 'convenience)
+
+(defvar adjust-parens-mode-map (make-sparse-keymap)
+  "Keymap for `adjust-parens-mode'")
+(define-key adjust-parens-mode-map (kbd "TAB") 'lisp-indent-adjust-parens)
+(define-key adjust-parens-mode-map (kbd "<backtab>") 
'lisp-dedent-adjust-parens)
+
+(define-minor-mode adjust-parens-mode
+  "Indent and dedent Lisp code, automatically adjust close parens."
+  :group 'adjust-parens
+  :keymap adjust-parens-mode-map)
 
 (provide 'adjust-parens)
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]