Monday, June 29, 2020

Re: An interesting little poser

On Sun, Jun 28, 2020 at 05:37:04PM EDT, Mateusz Okulus wrote:
>
> Run the following (copy and paste)
>
> :map <F2> G?\[\d\+\]<CR>va[y/<C-r>0<CR>dd0DnF[hr^llPdt]

Nice! Really does a spectacular job when run on my toy example...
especially when one presses and holds down <F2> key.

But there appears to be a problem with the macro when the
digit(s)/number that define the footnotes (say '3' for footnote [3]) is
also present somewhere in the body of the text - e.g. in a date such as
'1234'.

I think the problem is caused by the '[3]' string yanked via 'va]' being
interpreted as a regex that is a 'collection' whose sole member is the
single character '3'. So that the ensuing '/<c-r>0' (results in '/[0]'
search) looks for all occurrences of the character '3' instead of the
intended ('['+'3'+']') string. This behaviour can be verified
interactively after adding a date such as '1234' anywhere in the body of
the example and doing a '/[3]' repeatedly (as opposed to a '/\[3]').

Here's a version that appears to fix a couple of problems:

:map <F2> G?\[\d\+\]<CR>va]y/\<C-R>0<CR>ndd0Dnhr^llPldt]
^ ^ ^
(a) (b) (c)

a. escape first '[' when searching for '[3]'.

b. added 'n' to skip the first occurrence of '[3]' (the footnote anchor) so the
cursor is back at the beginning of the footnote proper.

c. extra 'l' to avoid deleting the last character at the end of the
footnote.

I don't know if there's a way to run a macro against a bunch of files in
one pass so I may have to to convert the macro to a function that can be
run via :bufdo/:argdo — 700+ files, with some 20-30 footnotes apiece.

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/20200629231458.GC4819%40turki.local.

No comments: