Subject: bug#24766: 26.0.50: [PATCH] Confusing behaviour for indent-relative-maybe

From: Alex <[email-masked]>
Date: Mon, 07 Nov 2016 19:53:22 -0600

Eli Zaretskii <[email-masked]> writes:

>> From: Alex <[email-masked]>
>> Cc: [email-masked]
>> Date: Mon, 24 Oct 2016 13:27:43 -0600
>> 
>> > I'd prefer a backward-compatible change, i.e. make the new argument be
>> > the 2nd one, and keep the current behavior when the 1st arg is non-nil
>> > and the 2nd is nil or omitted.
>> 
>> That's what I did, but I used a new name for the old argument and the
>> old name for the new argument. I did so as the old name fits the new
>> behaviour more.
>> 
>> This is a backward-compatible change for indent-relative, but it does
>> use the new behaviour for indent-relative-maybe. Is that alright with
>> you?
>
> Yes, thanks.

Sorry for the delay.

After thinking about it some more, and after properly searching on
Github for `indent-relative-maybe', I'm not sure if my previous solution
is the best one now. I found that due to some blog posts and starter kit
configurations, a surprising amount of people use indent-relative-maybe
despite docstring claiming different functionality.

I now think the following should happen:

1) indent-relative-maybe's should be obsoleted in
favour of a name suiting the purpose (e.g. indent-relative-whitespace)
with a better docstring.

2) The docstring and second optional argument should be added as
discussed before.

3) Perhaps in the future a new function can be introduced that
automatically calls (indent-relative nil t), but I'm not sure if that
should be done now. To be honest, I lost my original reason that made me
interested in this function.

Anyway, I've attached a diff that addresses this new proposal.

diff --git a/lisp/indent.el b/lisp/indent.el
index 0f6c68d..6c907f4 100644
--- a/lisp/indent.el
+++ b/lisp/indent.el
@@ -559,24 +559,29 @@ indent-region
   ;; by hand.
   (setq deactivate-mark t))
 
-(defun indent-relative-maybe ()
-  "Indent a new line like previous nonblank line.
-If the previous nonblank line has no indent points beyond the
-column point starts at, this command does nothing.
+(define-obsolete-function-alias 'indent-relative-maybe
+  'indent-relative-whitespace "26.1")
+
+(defun indent-relative-whitespace ()
+  "Indent the current line like the previous nonblank line.
+Indent to the first indentation position in the previous nonblank
+line.
 
 See also `indent-relative'."
   (interactive)
   (indent-relative t))
 
-(defun indent-relative (&optional unindented-ok)
+(defun indent-relative (&optional first-only unindented-ok)
   "Space out to under next indent point in previous nonblank line.
 An indent point is a non-whitespace character following whitespace.
 The following line shows the indentation points in this line.
     ^         ^    ^     ^   ^           ^      ^  ^    ^
+If FIRST-ONLY is non-nil, then only the first indent point is
+considered.
+
 If the previous nonblank line has no indent points beyond the
-column point starts at, `tab-to-tab-stop' is done instead, unless
-this command is invoked with a numeric argument, in which case it
-does nothing.
+column point starts at, then `tab-to-tab-stop' is done if
+UNINDENTED-OK is nil, otherwise nothing is done in this case.
 
 See also `indent-relative-maybe'."
   (interactive "P")
@@ -594,17 +599,18 @@ indent-relative
 	    (if (> (current-column) start-column)
 		(backward-char 1))
 	    (or (looking-at "[ \t]")
-		unindented-ok
+		first-only
 		(skip-chars-forward "^ \t" end))
 	    (skip-chars-forward " \t" end)
 	    (or (= (point) end) (setq indent (current-column))))))
-    (if indent
-	(let ((opoint (point-marker)))
-	  (indent-to indent 0)
-	  (if (> opoint (point))
-	      (goto-char opoint))
-	  (move-marker opoint nil))
-      (tab-to-tab-stop))))
+    (cond (indent
+           (let ((opoint (point-marker)))
+             (indent-to indent 0)
+             (if (> opoint (point))
+                 (goto-char opoint))
+             (move-marker opoint nil)))
+          (unindented-ok nil)
+          (t (tab-to-tab-stop)))))
 
 (defcustom tab-stop-list nil
   "List of tab stop positions used by `tab-to-tab-stop'.