evil-lion icon indicating copy to clipboard operation
evil-lion copied to clipboard

Aligning on spaces doesn't work correctly

Open ninrod opened this issue 6 years ago • 7 comments

hi @edkolev,

is it possible to turn this:

#<ip-address>  <hostname.domain.org>                   <hostname>
127.0.0.1	localhost.localdomain	localhost
::1		localhost.localdomain	localhost
# 127.0.0.1	thinkpad.localdomain	thinkpad
192.168.0.4       julicloud.local    julicloud
127.0.1.1       thinkpad.localdomain    thinkpad

into this:

#<ip-address>   <hostname.domain.org>   <hostname>
127.0.0.1	localhost.localdomain	localhost
::1		localhost.localdomain	localhost
# 127.0.0.1	thinkpad.localdomain	thinkpad
192.168.0.4     julicloud.local         julicloud
127.0.1.1       thinkpad.localdomain    thinkpad

with your package?

ninrod avatar Jul 06 '17 16:07 ninrod

Until now evil-lion has been designed to not handle tabs. Supporting tabs would be a nice feature but the hard part would be to figure out what's the best behavior from the user's standpoint.

This particular example has intermixed tabs (lines 2, 3, 4) and spaces (lines 1, 5, 6). Let me know if you have any suggestions for an algorithm for this case, the best thing which comes to my mind is A) bail out with an error when the given region has both spaces and tabs, and B) turn tabs into spaces (but how many?) and then align

edkolev avatar Jul 07 '17 12:07 edkolev

I see. but suppose it was all spaces. would it be possible?

ninrod avatar Jul 07 '17 12:07 ninrod

I see that it's not working correctly right now. I'll rename the issue and let you know when fixed

edkolev avatar Jul 08 '17 13:07 edkolev

I have a version which works, but it only works when evil-lion-squeeze-spaces is t (it is t by default):

#<ip-address> <hostname.domain.org> <hostname>
127.0.0.1     localhost.localdomain localhost
::1           localhost.localdomain localhost
#             127.0.0.1             thinkpad.localdomain thinkpad
192.168.0.4   julicloud.local       julicloud
127.0.1.1     thinkpad.localdomain  thinkpad

Do you happen to know if align.el is capable of aligning on spaces so I can re-use its functionality?

edkolev avatar Jul 09 '17 14:07 edkolev

Hum. I use an align function from spacemacs which I think uses align-regexp. Will that test later.

ninrod avatar Jul 09 '17 17:07 ninrod

@edkolev, I've just used a function (from spacemacs) to align the block with success.

All I did was vip, then invoking the function, then insert a space at the prompt and hit enter.

As this function uses align-regexp from align.el I believe this is what you are looking for.

interesting to note, it leaved 2 spaces between the columns.

  • before:
#<ip-address>              <hostname.domain.org>                   <hostname>
127.0.0.1          localhost.localdomain    localhost
::1                localhost.localdomain    localhost
# 127.0.0.1      thinkpad.localdomain    thinkpad
192.168.0.4          julicloud.local    julicloud
127.0.1.1                 thinkpad.localdomain    thinkpad
  • after:
#<ip-address>  <hostname.domain.org>  <hostname>
127.0.0.1      localhost.localdomain  localhost
::1            localhost.localdomain  localhost
#              127.0.0.1              thinkpad.localdomain  thinkpad
192.168.0.4    julicloud.local        julicloud
127.0.1.1      thinkpad.localdomain   thinkpad
  • the function:
;; function copied from spacemacs source code
;; modified function from http://emacswiki.org/emacs/AlignCommands
(defun spacemacs/align-repeat (start end regexp &optional justify-right after)
  "Repeat alignment with respect to the given regular expression.
If JUSTIFY-RIGHT is non nil justify to the right instead of the
left. If AFTER is non-nil, add whitespace to the left instead of
the right."

  (interactive "r\nsAlign repeat regexp: ")
  (let* ((ws-regexp (if (string-empty-p regexp)
                        "\\(\\s-+\\)"
                      "\\(\\s-*\\)"))
         (complete-regexp (if after
                              (concat regexp ws-regexp)
                            (concat ws-regexp regexp)))
         (group (if justify-right -1 1)))
    (message "complete-regexp: %S" complete-regexp)
    (align-regexp start end complete-regexp group 1 t)))

ninrod avatar Jul 10 '17 16:07 ninrod

I see... so align.el also squeezes (removes) the whitespace.

Thanks for looking into this, I'll try to reuse align.el's functionality but also try to avoid the double-space after aligning.

edkolev avatar Jul 11 '17 07:07 edkolev