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

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

[elpa] externals/adjust-parens 4722822 6/8: * adjust-parens: Two new def


From: Stefan Monnier
Subject: [elpa] externals/adjust-parens 4722822 6/8: * adjust-parens: Two new defcustoms:
Date: Tue, 1 Dec 2020 15:12:00 -0500 (EST)

branch: externals/adjust-parens
commit 4722822a2a17f40c6a14f9991fa286ab9500657b
Author: Barry <gundaetiapo@gmail.com>
Commit: Barry <gundaetiapo@gmail.com>

    * adjust-parens: Two new defcustoms:
      adjust-parens-fallback-(in|de)dent-function
    
      Reduce code duplication.
---
 adjust-parens-tests.el |  10 -----
 adjust-parens.el       | 105 ++++++++++++++++++++++++++++---------------------
 2 files changed, 61 insertions(+), 54 deletions(-)

diff --git a/adjust-parens-tests.el b/adjust-parens-tests.el
index b0118ff..9909d76 100644
--- a/adjust-parens-tests.el
+++ b/adjust-parens-tests.el
@@ -87,16 +87,6 @@
                               "      ")
                       ")); 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
diff --git a/adjust-parens.el b/adjust-parens.el
index 4834b10..58a6632 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: 2.0
+;; Version: 3.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
@@ -259,61 +259,78 @@ scan-error to propogate up."
                          (car indent)
                        indent))))))))
 
-(defun adjust-parens-and-indent (adjust-function parg)
+(defun adjust-parens-and-indent (raw-parg
+                                 adjust-function
+                                 adjust-function-negative
+                                 fallback-function)
   "Adjust close parens and indent the region over which the parens
 moved."
-  (let ((region-of-change (list (point) (point))))
-    (cl-loop for i from 1 to (or parg 1)
-             with finished = nil
-             while (not finished)
-             do
-             (condition-case err
-                 (let ((close-paren-movement
-                        (funcall adjust-function)))
-                   (if close-paren-movement
-                       (setq region-of-change
-                             (list (min (car region-of-change)
-                                        (car close-paren-movement)
-                                        (cadr close-paren-movement))
-                                   (max (cadr region-of-change)
-                                        (car close-paren-movement)
-                                        (cadr close-paren-movement))))
-                     (setq finished t)))
-               (scan-error (setq finished err))))
-    (apply 'indent-region region-of-change))
-  (back-to-indentation)
-  t)
-
-(defun lisp-indent-adjust-parens (&optional parg)
+  (if (adjust-parens-p)
+      (let* ((parg (prefix-numeric-value raw-parg))
+             (adjust-function (if (and parg (< parg 0))
+                                  adjust-function-negative
+                                adjust-function))
+             (region-of-change (list (point) (point))))
+        (cl-loop for i from 1 to (or (and parg (abs parg)) 1)
+                 with finished = nil
+                 while (not finished)
+                 do
+                 (condition-case err
+                     (let ((close-paren-movement
+                            (funcall adjust-function)))
+                       (if close-paren-movement
+                           (setq region-of-change
+                                 (list (min (car region-of-change)
+                                            (car close-paren-movement)
+                                            (cadr close-paren-movement))
+                                       (max (cadr region-of-change)
+                                            (car close-paren-movement)
+                                            (cadr close-paren-movement))))
+                         (setq finished t)))
+                   (scan-error (setq finished err))))
+        (apply 'indent-region region-of-change)
+        (back-to-indentation)
+        t)
+    (funcall fallback-function raw-parg)))
+
+(defcustom adjust-parens-fallback-indent-function 'indent-for-tab-command
+  "The function to call with prefix arg instead of
+adjust-parens-and-indent when adjust-parens-p returns false."
+  :type 'function
+  :group 'adjust-parens)
+(defun lisp-indent-adjust-parens (&optional raw-parg)
   "Indent Lisp code to the next level while adjusting sexp balanced
 expressions to be consistent.
 
+Returns t if adjust-parens changed the buffer, else returns the
+result of calling adjust-parens-fallback-indent-function.
+
 This command can be bound to TAB instead of indent-for-tab-command. It
 potentially calls the latter."
-  (interactive "p")
-  (if (adjust-parens-p)
-      (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)
+  (interactive "P")
+  (adjust-parens-and-indent raw-parg
+                            #'adjust-close-paren-for-indent
+                            #'adjust-close-paren-for-dedent
+                            adjust-parens-fallback-indent-function))
+
+(defcustom adjust-parens-fallback-dedent-function 'indent-for-tab-command
+  "The function to call with prefix arg instead of
+adjust-parens-and-indent when adjust-parens-p returns false."
+  :type 'function
+  :group 'adjust-parens)
+(defun lisp-dedent-adjust-parens (&optional raw-parg)
   "Dedent Lisp code to the previous level while adjusting sexp
 balanced expressions to be consistent.
 
-Returns t iff this function changed the buffer.
+Returns t if adjust-parens changed the buffer, else returns the
+result of calling adjust-parens-fallback-dedent-function.
 
 Binding to <backtab> (ie Shift-Tab) is a sensible choice."
-  (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))
+  (interactive "P")
+  (adjust-parens-and-indent raw-parg
+                            #'adjust-close-paren-for-dedent
+                            #'adjust-close-paren-for-indent
+                            adjust-parens-fallback-dedent-function))
 
 (defgroup adjust-parens nil
   "Indent and dedent Lisp code, automatically adjust close parens."



reply via email to

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