unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* align mysteries
@ 2021-10-14 18:59 Lars Ingebrigtsen
  2021-10-14 19:58 ` dick
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-14 18:59 UTC (permalink / raw)
  To: emacs-devel

I've never looked looking into how `align' works (because I don't
usually align anything), but it's kinda weird?

With the following in a text-mode buffer:

foo bar zot
lalala y indeed

`M-x align RET'

with that in the region does nothing.  `C-u M-x align RET' does align...
something...  And I get:

foo	bar	zot
lalala	y indeed

That is, it made all the spaces in the first line into TABs, and the
first space on the second line into a TAB.  ¯\_(ツ)_/¯

The prefix isn't mentioned in the `align' doc string, and it's not
referred to in the function itself, so it's probably reacted to in a
function called by `align'?  (I didn't try to debug it.)

So we should at least have a doc string fix here.  But is this how
`align' is supposed to work?  It seems awfully quirky.

The other thing I wondered about is -- do we have a sexp-aware
alignment command somewhere?  That is, that would use `forward-sexp' to
compute the columns?

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: align mysteries
  2021-10-14 18:59 align mysteries Lars Ingebrigtsen
@ 2021-10-14 19:58 ` dick
  2021-10-14 20:59   ` Lars Ingebrigtsen
  2021-10-14 22:04 ` Stefan Monnier
  2021-10-15  9:32 ` Teemu Likonen
  2 siblings, 1 reply; 13+ messages in thread
From: dick @ 2021-10-14 19:58 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: emacs-devel

I mailed everyone the exact same question on Wed, 17 Feb 2021 19:26:32 -0500.

As near as I can tell, the only sensible solution is this:

(defun line-up (start end)
  "Align columns by whitespace"
  (interactive "r")
  (align-regexp start end
                  "\\(\\s-*\\)\\s-" 1 0 t))

I must have gotten this from stackoverflow, so sorry to whomever wants credit
for this masterpiece.



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: align mysteries
  2021-10-14 19:58 ` dick
@ 2021-10-14 20:59   ` Lars Ingebrigtsen
  2021-10-14 21:19     ` H. Dieter Wilhelm
  0 siblings, 1 reply; 13+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-14 20:59 UTC (permalink / raw)
  To: dick; +Cc: emacs-devel

dick <dick.r.chiang@gmail.com> writes:

> I mailed everyone the exact same question on Wed, 17 Feb 2021 19:26:32 -0500.
>
> As near as I can tell, the only sensible solution is this:
>
> (defun line-up (start end)
>   "Align columns by whitespace"
>   (interactive "r")
>   (align-regexp start end
>                   "\\(\\s-*\\)\\s-" 1 0 t))
>
> I must have gotten this from stackoverflow, so sorry to whomever wants credit
> for this masterpiece.

Yup; this does exactly what I expected `M-x align' to do.

Is anybody here using `M-x align'?  If so, is it doing what you expect
it to?  :-)

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: align mysteries
  2021-10-14 20:59   ` Lars Ingebrigtsen
@ 2021-10-14 21:19     ` H. Dieter Wilhelm
  2021-10-14 21:35       ` Lars Ingebrigtsen
  0 siblings, 1 reply; 13+ messages in thread
From: H. Dieter Wilhelm @ 2021-10-14 21:19 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: dick, emacs-devel

Lars Ingebrigtsen <larsi@gnus.org> writes:

> Is anybody here using `M-x align'?  If so, is it doing what you expect
> it to?  :-)

For my needs it is working perfectly (I didn't study it, because it
works so nicely in my programming mode "APDL-Mode" on Melpa). Here's an
example when setting parameters:

mp = 3333333
l =40.0
kkkkk=8

M-x apdl-align

mp    = 3333333
l     =	     40.0
kkkkk =	      8

(Don't know if this is convincing for you :-))

(defun apdl-align (p-min p-max)
  "Align current paragraph or selection of APDL variable definitions.
If a region is selected align it (with the region borders P-MIN
and P-MAX) otherwise align the current code paragraph."
  (interactive "r")
  (if mark-active
      (align p-min p-max)
    (align-current))) ; align-current needs a mark


    Dieter
-- 
Best wishes
H. Dieter Wilhelm
Zwingenberg, Germany



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: align mysteries
  2021-10-14 21:19     ` H. Dieter Wilhelm
@ 2021-10-14 21:35       ` Lars Ingebrigtsen
  2021-10-14 21:41         ` Lars Ingebrigtsen
  0 siblings, 1 reply; 13+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-14 21:35 UTC (permalink / raw)
  To: H. Dieter Wilhelm; +Cc: dick, emacs-devel

"H. Dieter Wilhelm" <dieter@duenenhof-wilhelm.de> writes:

> For my needs it is working perfectly (I didn't study it, because it
> works so nicely in my programming mode "APDL-Mode" on Melpa). Here's an
> example when setting parameters:
>
> mp = 3333333
> l =40.0
> kkkkk=8
>
> M-x apdl-align
>
> mp    = 3333333
> l     =	     40.0
> kkkkk =	      8

Ah, I see.  apdl-mode has set up a bunch of rules in
apdl-align-rules-list, and that makes `M-x align' work perfectly.

text-mode hasn't set up any alignment rules, so `M-x align' does nothing
and `C-u M-x align' does...  weird stuff?

No, not quite?

(defcustom align-rules-list
[...]
    ;; With a numeric prefix argument, or C-u, space delimited text
    ;; tables will be aligned.
    (text-column
     (regexp   . "\\(^\\|\\S-\\)\\([ \t]+\\)\\(\\S-\\|$\\)")
     (group    . 2)
     (modes    . align-text-modes)
     (repeat   . t)
     (run-if   . ,(lambda ()
                    (and current-prefix-arg
                         (not (eq '- current-prefix-arg))))))

So this is where the prefix action comes from!  Wow, that's a weird way
to control this.

But...  why?  Why only have `M-x align' do something in text modes if
you give it a prefix?

And the other oddness I was seeing with this indentation:

foo	bar	zot	
lalala	y indeed

It indents it the expected way if I change the "y" into a "yy".

foo	bar	zot	
lalala	yy	indeed

I guess there's some heavy DWIM involved here?

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: align mysteries
  2021-10-14 21:35       ` Lars Ingebrigtsen
@ 2021-10-14 21:41         ` Lars Ingebrigtsen
  2021-10-15  6:20           ` Eli Zaretskii
  2021-10-16 17:48           ` H. Dieter Wilhelm
  0 siblings, 2 replies; 13+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-14 21:41 UTC (permalink / raw)
  To: H. Dieter Wilhelm; +Cc: dick, emacs-devel

Lars Ingebrigtsen <larsi@gnus.org> writes:

> But...  why?  Why only have `M-x align' do something in text modes if
> you give it a prefix?

It's to be able to have different predefined alignments depending on the
prefix, I guess:

    ;; With a numeric prefix argument, or C-u, space delimited text
    ;; tables will be aligned.
    (text-column
     (regexp   . "\\(^\\|\\S-\\)\\([ \t]+\\)\\(\\S-\\|$\\)")
     (group    . 2)
     (modes    . align-text-modes)
     (repeat   . t)
     (run-if   . ,(lambda ()
                    (and current-prefix-arg
                         (not (eq '- current-prefix-arg))))))

    ;; With a negative prefix argument, lists of dollar figures will
    ;; be aligned.
    (text-dollar-figure
     (regexp   . "\\$?\\(\\s-+[0-9]+\\)\\.")
     (modes    . align-text-modes)
     (justify  . t)
     (run-if   . ,(lambda ()
                    (eq '- current-prefix-arg))))

It still doesn't explain why the first one doesn't just test that it's
not minus.

The `align' doc string should mention that the prefix can influence
which rule is used, at the very least.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: align mysteries
  2021-10-14 18:59 align mysteries Lars Ingebrigtsen
  2021-10-14 19:58 ` dick
@ 2021-10-14 22:04 ` Stefan Monnier
  2021-10-15 10:00   ` Lars Ingebrigtsen
  2021-10-15  9:32 ` Teemu Likonen
  2 siblings, 1 reply; 13+ messages in thread
From: Stefan Monnier @ 2021-10-14 22:04 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: emacs-devel

> The other thing I wondered about is -- do we have a sexp-aware
> alignment command somewhere?  That is, that would use `forward-sexp' to
> compute the columns?

I have a FIXME in `smie.el` to provide an alignment command that uses
the SMIE grammar info.  I think the code is hidden in the micro-dot
at the end of the FIXME, but I never managed to convince the NSA to
extract it for me.


        Stefan




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: align mysteries
  2021-10-14 21:41         ` Lars Ingebrigtsen
@ 2021-10-15  6:20           ` Eli Zaretskii
  2021-10-15 10:01             ` Lars Ingebrigtsen
  2021-10-16 17:48           ` H. Dieter Wilhelm
  1 sibling, 1 reply; 13+ messages in thread
From: Eli Zaretskii @ 2021-10-15  6:20 UTC (permalink / raw)
  To: Lars Ingebrigtsen, John Wiegley; +Cc: dieter, dick.r.chiang, emacs-devel

> From: Lars Ingebrigtsen <larsi@gnus.org>
> Date: Thu, 14 Oct 2021 23:41:44 +0200
> Cc: dick <dick.r.chiang@gmail.com>, emacs-devel@gnu.org
> 
> Lars Ingebrigtsen <larsi@gnus.org> writes:
> 
> > But...  why?  Why only have `M-x align' do something in text modes if
> > you give it a prefix?
> 
> It's to be able to have different predefined alignments depending on the
> prefix, I guess:
> 
>     ;; With a numeric prefix argument, or C-u, space delimited text
>     ;; tables will be aligned.
>     (text-column
>      (regexp   . "\\(^\\|\\S-\\)\\([ \t]+\\)\\(\\S-\\|$\\)")
>      (group    . 2)
>      (modes    . align-text-modes)
>      (repeat   . t)
>      (run-if   . ,(lambda ()
>                     (and current-prefix-arg
>                          (not (eq '- current-prefix-arg))))))
> 
>     ;; With a negative prefix argument, lists of dollar figures will
>     ;; be aligned.
>     (text-dollar-figure
>      (regexp   . "\\$?\\(\\s-+[0-9]+\\)\\.")
>      (modes    . align-text-modes)
>      (justify  . t)
>      (run-if   . ,(lambda ()
>                     (eq '- current-prefix-arg))))
> 
> It still doesn't explain why the first one doesn't just test that it's
> not minus.

It really doesn't make sense to have this discussion without involving
John Wiegley, the author (CC'ed).

> The `align' doc string should mention that the prefix can influence
> which rule is used, at the very least.

The doc string needs a lot of work, but it cannot be improved before
we understand well enough what the function is doing in the various
use cases.



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: align mysteries
  2021-10-14 18:59 align mysteries Lars Ingebrigtsen
  2021-10-14 19:58 ` dick
  2021-10-14 22:04 ` Stefan Monnier
@ 2021-10-15  9:32 ` Teemu Likonen
  2021-10-15 10:10   ` Lars Ingebrigtsen
  2 siblings, 1 reply; 13+ messages in thread
From: Teemu Likonen @ 2021-10-15  9:32 UTC (permalink / raw)
  To: Lars Ingebrigtsen, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1036 bytes --]

* 2021-10-14 20:59:54+0200, Lars Ingebrigtsen wrote:

> So we should at least have a doc string fix here.  But is this how
> `align' is supposed to work?  It seems awfully quirky.

I will add that I have tried to use `align' a couple of times but I
couldn't figure how to use it. It has been easier to manually align
things.

But now I was successful with `align-regexp'. Consider the following
text as region:

    foo bar zot
    lalala y indeed

C-u M-x align-regexp

    Complex align using regexp: \(\s-*\)      [add one space at the end]
    Parenthesis group to modify (justify if negative): 1
    Amount of spacing (or column if negative): 1
    Repeat throughout line? (y or n) y

The resulting text:

    foo     bar  zot
    lalala  y    indeed

I don't quite understand those options but I mostly accepted the
defaults. For the first question I had to add one space (see above).

-- 
/// Teemu Likonen - .-.. https://www.iki.fi/tlikonen/
// OpenPGP: 4E1055DC84E9DFF613D78557719D69D324539450

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 251 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: align mysteries
  2021-10-14 22:04 ` Stefan Monnier
@ 2021-10-15 10:00   ` Lars Ingebrigtsen
  0 siblings, 0 replies; 13+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-15 10:00 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

> I have a FIXME in `smie.el` to provide an alignment command that uses
> the SMIE grammar info.  I think the code is hidden in the micro-dot
> at the end of the FIXME, but I never managed to convince the NSA to
> extract it for me.

Then what is our tax money good for if not for that?  The outrage.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: align mysteries
  2021-10-15  6:20           ` Eli Zaretskii
@ 2021-10-15 10:01             ` Lars Ingebrigtsen
  0 siblings, 0 replies; 13+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-15 10:01 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: dieter, John Wiegley, dick.r.chiang, emacs-devel

Eli Zaretskii <eliz@gnu.org> writes:

> The doc string needs a lot of work, but it cannot be improved before
> we understand well enough what the function is doing in the various
> use cases.

Yup.  I'll open a bug report for this, though, so that we don't lose
track of the issue.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: align mysteries
  2021-10-15  9:32 ` Teemu Likonen
@ 2021-10-15 10:10   ` Lars Ingebrigtsen
  0 siblings, 0 replies; 13+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-15 10:10 UTC (permalink / raw)
  To: Teemu Likonen; +Cc: emacs-devel

Teemu Likonen <tlikonen@iki.fi> writes:

> C-u M-x align-regexp
>
>     Complex align using regexp: \(\s-*\)      [add one space at the end]
>     Parenthesis group to modify (justify if negative): 1
>     Amount of spacing (or column if negative): 1
>     Repeat throughout line? (y or n) y
>
> The resulting text:
>
>     foo     bar  zot
>     lalala  y    indeed
>
> I don't quite understand those options but I mostly accepted the
> defaults. For the first question I had to add one space (see above).

Aah, I see; thanks.  I tried `M-x align-regexp', too, but without the
prefix, which (again) didn't really seem to do what you'd expect.  But
the doc string for that command at least mentions the prefix argument.
But makes it sound like it's more cumbersome:

---
Non-interactively (or if you specify a prefix argument), you must
enter the full regular expression, including the subexpression.
---

While instead it gives you defaults and a guided tour through the other
parameters.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: align mysteries
  2021-10-14 21:41         ` Lars Ingebrigtsen
  2021-10-15  6:20           ` Eli Zaretskii
@ 2021-10-16 17:48           ` H. Dieter Wilhelm
  1 sibling, 0 replies; 13+ messages in thread
From: H. Dieter Wilhelm @ 2021-10-16 17:48 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: dick, emacs-devel

Lars Ingebrigtsen <larsi@gnus.org> writes:

> The `align' doc string should mention that the prefix can influence
> which rule is used, at the very least.

Indeed, even as a - admittedly superficial - user I didn't realise that
align can react to a prefix argument.  Thank you very much for making me
aware of this.

      Dieter
-- 
Best wishes
H. Dieter Wilhelm
Zwingenberg, Germany



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2021-10-16 17:48 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-14 18:59 align mysteries Lars Ingebrigtsen
2021-10-14 19:58 ` dick
2021-10-14 20:59   ` Lars Ingebrigtsen
2021-10-14 21:19     ` H. Dieter Wilhelm
2021-10-14 21:35       ` Lars Ingebrigtsen
2021-10-14 21:41         ` Lars Ingebrigtsen
2021-10-15  6:20           ` Eli Zaretskii
2021-10-15 10:01             ` Lars Ingebrigtsen
2021-10-16 17:48           ` H. Dieter Wilhelm
2021-10-14 22:04 ` Stefan Monnier
2021-10-15 10:00   ` Lars Ingebrigtsen
2021-10-15  9:32 ` Teemu Likonen
2021-10-15 10:10   ` Lars Ingebrigtsen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).