5

Initial buffer

&ei = wFSXYvnUEvaL6dsP9f2HsAE
&ip = 195.201.109.29
&id = o-AD7SgDnv171kT3qy-KSuSbDZ3zX4YR5X7cS6Mmq237oY
&itag = 22
&source = youtube
&requiressl = yes
&vprv = 1
&mime = video%2Fmp4
&cnr = 14
&ratebypass = yes
&dur = 193.863
&lmt = 1481782852831520
&fexp = 24001373,24007246
&c = ANDROID
&sparams = expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Ccnr%2Cratebypass%2Cdur%2Clmt
&sig = AOq0QJ8wRQIhAOEaQvJ6d6Z3qVjvnAkqv_MOdyBwYGU3zYA2VGQMjvzlAiB8LLsH0WW-SFB2XuxZndVzMeLU4DEZYZUomM6TfEjfGA%3D%3D
&host = rr4---sn-4g5lznez.googlevideo.com
&title = y2meta.com%20-%20%D8%A7%D9%84%D8%AC%D8%B2%D8%A7%D8%A6%D8%B1%20:%20%D8%AC%D8%A7%D8%A6%D8%B2%D8%A9%20%D9%86%D9%88%D8%A8%D9%84%20%D9%81%D9%8A%20%D8%A7%D9%84%D9%85%D9%87%D8%B2%D9%84%D8%A9%20%D9%87%D9%87%D9%87%D9%87%D9%87%D9%87%D9%87
&cm2rm = sn-5abxgpxuxaxjvh-5abs7s,sn-hgnlz7l
&req_id = f665c13668b4a3ee
&redirect_counter = 2
&cms_redirect = yes
&cmsv = e
&mh = 1J
&mip = 197.201.1.50
&mm = 34
&mn = sn-4g5edndl
&ms = ltu
&mt = 1654084603
&mv = m
&mvi = 5
&pl = 22
&lsparams = mh,mip,mm,mn,ms,mv,mvi,pl
&lsig = AG3C_xAwRQIgDWrakAvLyWUUDp5_Ubdpz5qWE0qk9jMqT1WDKjxqY78CIQDoaW79cklMU5ts5g2WanYyNxR-y-5DXezu8vbWHeXLJA%3D%3D

After align-regex RET = RET

&ei                     = wFSXYvnUEvaL6dsP9f2HsAE
&ip                     = 195.201.109.29
&id                     = o-AD7SgDnv171kT3qy-KSuSbDZ3zX4YR5X7cS6Mmq237oY
&itag                   = 22
&source                 = youtube
&requiressl             = yes
&vprv                   = 1
&mime                   = video%2Fmp4
&cnr                    = 14
&ratebypass             = yes
&dur                    = 193.863
&lmt                    = 1481782852831520
&fexp                   = 24001373,24007246
&c                      = ANDROID
&sparams                = expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Ccnr%2Cratebypass%2Cdur%2Clmt
&sig                    = AOq0QJ8wRQIhAOEaQvJ6d6Z3qVjvnAkqv_MOdyBwYGU3zYA2VGQMjvzlAiB8LLsH0WW-SFB2XuxZndVzMeLU4DEZYZUomM6TfEjfGA%3D%3D
&host                   = rr4---sn-4g5lznez.googlevideo.com
&title                  = y2meta.com%20-%20%D8%A7%D9%84%D8%AC%D8%B2%D8%A7%D8%A6%D8%B1%20:%20%D8%AC%D8%A7%D8%A6%D8%B2%D8%A9%20%D9%86%D9%88%D8%A8%D9%84%20%D9%81%D9%8A%20%D8%A7%D9%84%D9%85%D9%87%D8%B2%D9%84%D8%A9%20%D9%87%D9%87%D9%87%D9%87%D9%87%D9%87%D9%87
&cm2rm                  = sn-5abxgpxuxaxjvh-5abs7s,sn-hgnlz7l
&req_id                 = f665c13668b4a3ee
&redirect_counter       = 2
&cms_redirect           = yes
&cmsv                   = e
&mh                     = 1J
&mip                    = 197.201.1.50
&mm                     = 34
&mn                     = sn-4g5edndl
&ms                     = ltu
&mt                     = 1654084603
&mv                     = m
&mvi                    = 5
&pl                     = 22
&lsparams               = mh,mip,mm,mn,ms,mv,mvi,pl
&lsig                   = AG3C_xAwRQIgDWrakAvLyWUUDp5_Ubdpz5qWE0qk9jMqT1WDKjxqY78CIQDoaW79cklMU5ts5g2WanYyNxR-y-5DXezu8vbWHeXLJA%3D%3D

However, I'd like to have parameters on the left (strings before the = sign) be right aligned like in this example :

 1                     AC_BR_BONANZA  Too many newlines in a row... spammy template
 2                          ACCESSDB  Message would have been caught by accessdb
 3                ACCT_PHISHING_MANY  Phishing for account information
 4                    AC_DIV_BONANZA  Too many divs in a row... spammy template
 5                 AC_FROM_MANY_DOTS  Multiple periods in From user name
 6             AC_HTML_NONSENSE_TAGS  Many consecutive multi-letter HTML tags, likely nonsense/spam
 7                    AC_POST_EXTRAS  Suspicious URL
 8          AC_SPAMMY_URI_PATTERNS10  link combos match highly spammy template
 9          AC_SPAMMY_URI_PATTERNS11  link combos match highly spammy template
10          AC_SPAMMY_URI_PATTERNS12  link combos match highly spammy template
11           AC_SPAMMY_URI_PATTERNS1  link combos match highly spammy template
12           AC_SPAMMY_URI_PATTERNS2  link combos match highly spammy template
13           AC_SPAMMY_URI_PATTERNS3  link combos match highly spammy template
14           AC_SPAMMY_URI_PATTERNS4  link combos match highly spammy template
15           AC_SPAMMY_URI_PATTERNS8  link combos match highly spammy template
16           AC_SPAMMY_URI_PATTERNS9  link combos match highly spammy template
17                      ACT_NOW_CAPS  Talks about 'acting now' with capitals
18                            ADMAIL  "admail" and variants
19                       ADMITS_SPAM  Admits this is an ad

Any idea how to achieve this?

ychaouche
  • 207
  • 1
  • 7
  • @Drew not really, `fill-paragraph` will add new lines, which I don't want. – ychaouche Jun 02 '22 at 09:15
  • @Drew This is not a duplicate of ["How to right align region and/or line?"](https://emacs.stackexchange.com/questions/3540/how-to-right-align-region-and-or-line). There the full line is right-aligned, but here only the part before the `?=` is right-aligned. – Tobias Jun 02 '22 at 14:24

1 Answers1

6

You use the "complex align"-form of align-regexp, i.e., call it as with universal prefix argument C-u M-x align-regexp RET.

Use \(\s-*&[[:alnum:]_]+\) as regexp and -1 as group number. The negative sign means "justify" instead of "align".
With "align" the full group is replaced by the right amount of spaces whereas with "justify" only the space in the group is used for alignment but not the nonspace characters. If the space is at the beginning of the group that causes the right-alignment of the nonspace-part of the group.

For the other prompted arguments, choose the appropriate amount of spacing and deny repetition.

Tobias
  • 32,569
  • 1
  • 34
  • 75
  • regex : `\([[:alpha:]_]+\s-*\)`, group `-1`, spacing `1`, repeat : `n`, result : [gist](https://gist.githubusercontent.com/ychaouche/10fba6ac56088a30a7680a21643b38c9/raw/bab59c69ebd9f46b193148151711d1f0a22772f6/gistfile1.txt) – ychaouche Jun 02 '22 at 09:23
  • @ychaouche Sorry, I used your last example for testing (first left-aligned everything and applied `align-regexp` afterwards). I have adapted the regexp for your first example now. Could you try again? – Tobias Jun 02 '22 at 10:00
  • This incredible working answer should absolutely be accepted and upvoted! – Phil Hudson Jun 02 '22 at 13:19
  • @Tobias wonderful! – ychaouche Jun 05 '22 at 16:04
  • @Tobias, I wonder what that regex matches though. See https://imgur.com/a/I9bPjqp and https://regex101.com/r/T8WR7u/1. First line isn't captured on regex101, but it still works correctly on emacs for the whole buffer (including first line). There are no spaces before the `&` in my example, so it must have taken it from previous line, assuming something with newlines has to match? – ychaouche Jun 05 '22 at 16:23
  • 1
    @ychaouche Please use Emacs to check the regexp. E.g., paste the text into the scratch buffer and call `re-builder`. Set `reb-re-syntax` to `string` via `C-c TAB` (you can use `TAB`-completion there) and paste the regexp from the question within the quotes `""`. – Tobias Jun 07 '22 at 07:58
  • 1
    @ychaouche Note also, that `\s-` depends on the syntax table of the major mode of the current buffer. – Tobias Jun 07 '22 at 08:15