Wednesday, July 1, 2020

Re: An interesting little poser

On Sun, Jun 28, 2020 at 05:37:04PM EDT, Mateusz Okulus wrote:
> I've made the following macro that works for the given example, assuming
> 1 line of footnote.
>
> Run the following (copy and paste)
>
> :map <F2> G?\[\d\+\]<CR>va[y/<C-r>0<CR>dd0DnF[hr^llPdt]
>
> Press F2 to run this macro. You can also hold the key
> and will get a message when it's done.
>
> Explanation:
> G - to to the bottom of the file
> ? - start reverse search (search will look up)
> \[\d\+\] - regex - nonzero digits inside square brackets
> <CR> - enter to finish entering regex
> va[ - select square brackets and digits inside
> y - yank them
> / - search (this one will wrap)
> <C-r>0 - insert content of register 0, which holds last yank
> allowing us to search for the [digits] pattern
> <CR> - enter to finish entering search
> dd - delete line with [digits]; we are now on footnote line
> 0 - go to the beginning of the line
> D - delete to the end of line, without newline, so when we paste it
> won't be with newline
> n - go to the the place where the footnote is referenced
> F[ - search backward for [
> h - 1 left
> r^ - replace space with ^
> I've noticed the space is removed in example.
> ll - go to the first digit, skipping ^[
> P - paste footnote before the digits
> dt] - delete the digits, without ]

Above pseudo-code translated to a function wrapped in a while loop.

function! Footnotes2()
:normal G
:while search('^\[\d\+\]$', 'bc')
:normal va]
:normal y
:let b:p='\'.@0
:execute "call search(b:p)"
:execute "call search(b:p)"
:normal dd
:normal 0
:normal D
:execute "call search(b:p)"
:normal h
:normal r^
:normal ll
:normal P
:normal l
:normal dt]
:normal G
:endwhile
endfunc

I prefer a function for flexibility: I can easily add pre-processing
& post-processing code - e.g. to reflow multi-line footnotes... remove
the NULL lines at EOF... etc.

Thanks,

CJ

--
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200701210350.GE4819%40turki.local.

No comments: