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

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

[nongnu] elpa/crux 6d11d2e 039/112: When opening a line above, don't rei


From: ELPA Syncer
Subject: [nongnu] elpa/crux 6d11d2e 039/112: When opening a line above, don't reindent the current line
Date: Wed, 11 Aug 2021 09:57:49 -0400 (EDT)

branch: elpa/crux
commit 6d11d2e6b56e237bb871af7e21ba6ef30e0a10da
Author: Wilfred Hughes <me@wilfred.me.uk>
Commit: Bozhidar Batsov <bozhidar.batsov@gmail.com>

    When opening a line above, don't reindent the current line
    
    For some languages, we can't call `indent-according-to-mode` as there
    
    are multiple possible indentations. Python is a great example. For these
    
    languages, `electric-indent-inhibit` is `t` to prevent emacs changing
    
    indentation without the user requesting it.
    
    
    
    In this situation, we give the new line the same indentation characters as 
the
    
    current line. This ensures we respect the user's tab/space configuration.
    
    
    
    Closes #26.
---
 crux.el | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/crux.el b/crux.el
index a0a9cb8..e88bc0c 100644
--- a/crux.el
+++ b/crux.el
@@ -136,12 +136,24 @@ the current buffer."
 
 (defun crux-smart-open-line-above ()
   "Insert an empty line above the current line.
-Position the cursor at it's beginning, according to the current mode."
+Position the cursor at its beginning, according to the current mode."
   (interactive)
   (move-beginning-of-line nil)
-  (newline-and-indent)
-  (forward-line -1)
-  (indent-according-to-mode))
+  (insert "\n")
+  (if electric-indent-inhibit
+      ;; We can't use `indent-according-to-mode' in languages like Python,
+      ;; as there are multiple possible indentations with different meanings.
+      (let* ((indent-end (progn (back-to-indentation) (point)))
+             (indent-start (progn (move-beginning-of-line nil) (point)))
+             (indent-chars (buffer-substring indent-start indent-end)))
+        (forward-line -1)
+        ;; This new line should be indented with the same characters as
+        ;; the current line.
+        (insert indent-chars))
+    ;; Just use the current major-mode's indent facility.
+    (progn
+      (forward-line -1)
+      (indent-according-to-mode))))
 
 (defun crux-smart-open-line (arg)
   "Insert an empty line after the current line.



reply via email to

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