Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add an option to remove common indentation from a block. #19

Closed
jszakmeister opened this Issue Aug 10, 2013 · 11 comments

Comments

Projects
None yet
2 participants

It'd be really nice if there was an option in NrrwRgn to help remove common indentation from the block being narrowed, and insert it back when finished. It would help editing embedded documents, such as code buried in Sphinx code blocks, and other formats where the indentation is normally not welcome.

Owner

chrisbra commented Aug 10, 2013

Hi John!

On Sa, 10 Aug 2013, John Szakmeister wrote:

It'd be really nice if there was an option in NrrwRgn to help remove common indentation from the block being narrowed, and insert it back when finished. It would help editing embedded documents, such as code buried in Sphinx code blocks, and other formats where the indentation is normally not welcome.

Isn't this already possible by block-selecting the desired region (while
leaving the indentation off) and then using \nr to narrow the visual
selected region?

regards,

Christian

Für Zeiten standen Junge
vor Alten höflich auf,
Jetzt heißt es: Junger sitze!
Und alter Greiner, lauf!
-- Friedrich von Logau (Sinngedichte)

Perhaps a picture would help:

screen shot 2013-08-10 at 8 47 23 am

I selected the code embedded in the code block, but in the narrow region buffer, you can see that it's still indented. I'd like an option that would remove the common indentation, so it would look more like this:

screen shot 2013-08-10 at 8 50 31 am

Hopefully that clarifies what I'm asking for. Thanks!

Owner

chrisbra commented Aug 11, 2013

Hi John!

On Sa, 10 Aug 2013, John Szakmeister wrote:

Perhaps a picture would help:

screen shot 2013-08-10 at 8 47 23 am

I selected the code embedded in the code block, but in the narrow region buffer, you can see that it's still indented. I'd like an option that would remove the common indentation, so it would look more like this:

screen shot 2013-08-10 at 8 50 31 am

Hopefully that clarifies what I'm asking for. Thanks!

This should be possible, using the \nr key-binding together with
block-selecting the selected region.

See this screencast (attached to this mail, not sure it works with
github):

regards,

Christian

Sowenig man die Liebe den Prostituierten anvertrauen darf, sowenig die
Religion den Pfaffen.
-- Karlheinz Deschner

Owner

chrisbra commented Aug 11, 2013

Ok, I almost thought, attaching files wouldn't work for githut. Well see this screencast:
http://www.256bit.org/~chrisbra/Vim_Recording_20130811.mkv

Owner

chrisbra commented Aug 11, 2013

If this doesn't work good enough for you (since Vim will write the changes back using block mode), I'll probably add the desired option.

My apologies, I saw "block select" and thought you meant "select the area". I was hoping to get by with this using a linewise selection though. I have some mapping to help select regions, and they use linewise selections (which are more useful to me than block selections). Plus block selections get a little wonky to work with when the last line is not the longest line.

Thanks for taking the time to put the little screencast together! That was awfully nice of you.

Owner

chrisbra commented Aug 12, 2013

Hi John!

On So, 11 Aug 2013, John Szakmeister wrote:

My apologies, I saw "block select" and thought you meant "select the area". I was hoping to get by with this using a linewise selection though. I have some mapping to help select regions, and they use linewise selections (which are more useful to me than block selections). Plus block selections get a little wonky to work with when the last line is not the longest line.

Thanks for taking the time to put the little screencast together! That was awfully nice of you.

How about this. NarrowRegion supports hooks, that can be run when the
text is placed in the new window ("create" hook) or before the text is
written back ("close" hook). The help even has an entry about this
(:h NR-hooks)

Using this, I think you can already do what you want:

:let b:nrrw_aucmd_create=':%s/^\s\{4}//'

This removes 4 spaces at the beginning of the line when creating the
narrowed window

:let b:nrrw_aucmd_close='%s/^/    /'

This adds 4 spaces at the start of the line, before the content is
written back

Does this do what you want or is this to complicated?

regards,

Christian

Lieber voll heimkommen, als leer ausgehen.

How about this. NarrowRegion supports hooks, that can be run when the
text is placed in the new window ("create" hook) or before the text is
written back ("close" hook). The help even has an entry about this
(:h NR-hooks)

Using this, I think you can already do what you want:

:let b:nrrw_aucmd_create=':%s/^\s\{4}//'

This removes 4 spaces at the beginning of the line when creating the
narrowed window

:let b:nrrw_aucmd_close='%s/^/    /'

This adds 4 spaces at the start of the line, before the content is
written back

I actually want to remove all common indentation (not just the 4 spaces in this one case) and insert it back when done, but I'll play around with the hooks and see if I can get what I want.

Does this do what you want or is this to complicated?

I saw the hooks before, but for some reason was reluctant to use them. Let me see what I can put together though.

Thanks!

@chrisbra I tried the hooks and managed to get something that works. However, I think I found a bug in NrrwRgn. Using your example:

let b:nrrw_aucmd_create=':%s/^\s\{4}//'
let b:nrrw_aucmd_close='%s/^/    /'

If I select the block, narrow it, and then run :WidenRegion the text in the narrow region buffer is re-indented via the close hook, but the buffer is not closed (the last part is expected). If you then run :WidenRegion! the close hook executes again, and the result is then double indented.

Should :WidenRegion be running the create hook again if it's not going to close the buffer?

chrisbra added a commit that referenced this issue Aug 14, 2013

Owner

chrisbra commented Aug 14, 2013

I think, if :WidenRegion doesn't close the window, it should undo the hook command and that is what I have now implemented. If you notice any more errors, just tell me.

Yes, that makes more sense. I tried it out and it fixed the issue. Thanks!

vim-scripts added a commit to vim-scripts/NrrwRgn that referenced this issue Mar 28, 2014

Version 32
- hooks could corrupt the narrowed buffer, if it wasn't closed (reported by  jszakemeister chrisbra/NrrwRgn#19, thanks!)- Don't parse /doc/options.txt for finding out buffer-local options  (reported by AguirreIF chrisbra/NrrwRgn#21,  thanks!), instead include a fix set of option names to set when opening the  narrowed buffer.- Switching buffers in the original narrowed buffer, may confuse NrrwRgn.- Code cleanup (no more separate functions for visual and normal mode)- fix issue 22 (characterwise narrowing was brocken in last commit, reported  by Matthew Boehm in chrisbra/NrrwRgn#22, thanks!)- in characterwise visual selection, trailing \n is not stripped when writing  (reported by Matthew Boehm in https://github.com/chrisbra/NrrwRgn/23,  thanks!)- highlighting was wrong for characterwise visual selections- update original window for multi narrowed regions (  https://github.com/chrisbra/NrrwRgn/24, reported by Dane Summers, thanks!),  use the g:nrrw_rgn_update_orig_win variable to enable- error when narrowed window was moved to new tab and trying to quit  (https://github.com/chrisbra/NrrwRgn/2, reported by Mario Ricalde, thanks!)- better default names for the narrowed window  (https://github.com/chrisbra/Nrrwrgn/28, reported by Mario Ricalde, thanks!)- when setting g:nrrw_rgn_write_on_sync the original file will be saved,  whenever the narrowed window is written back  (https://github.com/chrisbra/26, reported by Mario Ricalde, thanks!)- Some more error handling when using |:WidenRegion|- Make sure highlighting is removed when using |:WidenRegion|(automatically uploaded)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment