Sunday, June 29, 2014

Re: Tabs for indentation, spaces for alignment - possible in vanilla vim?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

To not break plugin that uses Tab you either need to avoid mapping Tab or somehow run Tab "manually" in new mapping. I know how to chain mappings (run one mapping from the other), but it is rather complicated code that does not handle at least one edge case.

This mapping is not going to achieve tabs indentation+space alignment goal: this goal means that some "starter" lines have only indentation via tabs and "continuation" lines have indentation (same as "starter") and alignment:

if (ch1 == 'a'
&& ch2 == 'b'
&& ch3 == 'c')
^^^^ indentation
^^^^ alignment (lines up first "&" and next character after the opening parenthesis)

. It also means that inline alignment should be done with spaces, but this bit is pretty trivial.

To achieve this you should not only map Tab. You should also make Vim behave correctly when you add new line in a number of ways. OP simplifies it too much: the two "simple" statements are nearly not a complete description of this concept. Wiki article talks about this: see section "Continuation lines".

On June 29, 2014 9:59:40 AM GMT+03:00, Paul Isambert <zappathustra@free.fr> wrote:
>Le samedi 28 juin 2014 à 11:33, Bruno Sutic a écrit:
>> Hi,
>> I would like to use tabs for indentation and spaces for alignment.
>> This "idea" is also described here:
>http://vim.wikia.com/wiki/Indent_with_tabs%2C_align_with_spaces#Smart_tabs
>>
>> To put it simple:
>> - tabs should be used at the beginning of the line
>> - spaces should be inserted when after any character in the line
>>
>> The article linked above proposes a 'smart tab' plugin to achieve
>this. Unfortunately, this plugin breaks snipmate plugin which also uses
>tab.
>>
>> So I wonder, is there a way (an option maybe) to achieve this without
>a plugin?
>> I tried `:h 'smarttab'`, but that does something else.
>
>Try this:
>
> function! s:TabOrSpace ()
> let c = col(".")
> if c == 1 || getline(".")[c-2] =~ '\t'
> return "\t"
> else
> return " "
> endif
> endfunction
> inoremap <expr> <Tab> <SID>TabOrSpace()
>
>Then <Tab> (you can change the key, of course) will insert a tab if at
>the beginning of the line or after a tab, and a space (you can make it
>return more) otherwise.
>
>I'm not 100% sure that's what you want, but I hope it helps anyway.
>
>Best,
>Paul
>
>--
>--
>You received this message from the "vim_use" maillist.
>Do not top-post! Type your reply below the text you are replying to.
>For more information, visit http://www.vim.org/maillist.php
>
>---
>You received this message because you are subscribed to the Google
>Groups "vim_use" group.
>To unsubscribe from this group and stop receiving emails from it, send
>an email to vim_use+unsubscribe@googlegroups.com.
>For more options, visit https://groups.google.com/d/optout.
-----BEGIN PGP SIGNATURE-----
Version: APG v1.1.1

iQI1BAEBCgAfBQJTr7vNGBxaeVggPHp5eC52aW1AZ21haWwuY29tPgAKCRCf3UKj
HhHSvpzPD/9y578mB+Rr21+YcDmNieMstpVWXvzSi/B5Bp/dis3c/JPs0d+mrm1W
kBOg5czbKjro5GYo6Tr0i5VcS8p6sLthJ/DPoMzxVwfqUncFl4UJegMF0Wp7E4DA
F9zd7aFFIQtFrVsHjoEUpJm6Axkoc96cYtT3OFG+isuy+dMURETZEYGny8HAW+9b
k2nhhzFdUZyG/A9Efe6KYck649hOkKSi6mZfIpsYVFgdVD3akZXLA2wOhLUHhr+N
F3aIAdaH6q0SkHE2mfqJxyYZBzAKWB4o7FpofzQm2XSGR4IVJBvjQ98dpdlEZiEP
uZ7c0vxeoNMeOsPWZThAEcScBqW3wHqtzP1+LUk8/vXawMrnpaWjmpFejikcPYNv
QC4hRfbiw/9XQRhLCl3hRHZQ0iF7SSpzcLnGXxkGEbv1Q8fD9JwLwVb/cWgtBGDq
d1Rzi673g0IpFCzjNOMfwC4r/m+aiGBwWVxDpMj7QOdRKKVIYaiyk2y4hKMkK6S/
WQBcBwoZ/NgXgmFaolvY2gWGaX11EVnLzt55Q89Tv5WTVCxMhPjYxcRmNPPeYTaY
V9a+Pdy9c6p8roY9Qsk86BGYrFlcdCJIwtY2/KsQKGneiahhSZ55tJmfAnKOBob3
3IPTigKEYby2wOSdV5mX35j9TdrTlRw31zmcR4B+ffL4ykVuVJNIiQ==
=6MzD
-----END PGP SIGNATURE-----

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment