Friday, June 26, 2020

Re: An interesting little poser [PS]

On Mon, Jun 22, 2020 at 09:40:38PM EDT, Tim Chase wrote:

Sorry for the delay... needed time for this to sink in (and test)

> In this case because each of the commands involves the :g command, it
> becomes a bit trickier. I'd likely define a function something like
>
> function! Unfootnote()
> " make all the footnotes on one line
> $?^$?+,$g/^\[\@!.*\n\[\@!/,/\n\[\|\%$/j
> " gather the footnotes into b:a
> let b:a={}
> $?^$?,$g/\[\d\+\]/let b:a[getline('.')]=getline(line(".")+1)
> " find all the footnote references
> " and replace them with the corresponding text
> 1,$?^$?s/\[\d\+]/\='^['.b:a[submatch(0)].']'/g
> endfunc

Works just fine! I wrapped the function in an if/endif block to make
sure there does exist footnotes in any given file (some of the files do
not have a footnotes section at the bottom)

:normal gg
:let b:s=getpos('.')
" check for the existence of a footnotes
:silent! normal /\[\d\+]
:let b:e=getpos('.')
if b:s[1] != b:e[1]

Testing whether the search was successful which would cause the cursor
to move... Couldn't figure out a nicer way...

> Note how each of those Ex commands we've discussed is a valid command
> in the body of a function as well. Yet another obscure corner of vim
> that escapes many folks. :-)

That at least hadn't escaped me... so much so that I wasted a couple of
hours trying to figure out why a ':normal /pattern' worked at the prompt
and didn't work when I coded it in a function... the cursor just refused
to budge and no matter how much I tested I couldn't figure out why...
until I calmed down and realized that what I coded was NOT what I had
actually typed at the ex prompt:

:normal /pattern
# ex command PLUS <CR> - big difference!

...

> Note that, while the parts of the function are reasonably tested, this
> function itself is largely untested, but *should* be pretty close.

It is perfectly suitable thank you! Without the if/endif block it didn't
break anything... just joined all the lines in the last paragraph of the
file (which didn't matter since I later reflow everything anyway — see
the other function I came up with).

Here's the function I wrote (I could integrate the code to the one you
kindly provided to do it all in one pass):

function! Delfoot()
:normal gg
:let b:s=getpos('.')
:silent! normal /^$\n\[[0-9]\{1,}]
:let b:e=getpos('.')
if b:s[1] != b:e[1]
:let b:f=expand('%:t')
:let b:fn='../ftns/ft'.b:f[2:5].'.txt'
:execute ".,$w " b:fn
:execute ".,$d"
endif
:set tw=78
:normal ggVGgq
endfunc

Nothing clever about this one! :-) ... I backup the foonote section at
the end of each file before I do away with it (and reflow everything to
a sensible textwidth to finish off the job so the files are ready for
edit/proofreading).

> Hopefully this both makes sense and helps level up your vim, letting
> you get drunk on this new-found power. :-)

Made up for that depressing feeling I get whenever I venture into vim
script coding and realize I have forgotten the little I know.

I eventually found a vim fandom article that has some examples of what
can be done with syntaxically built ranges

Thank,

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/20200626204633.GA4819%40turki.local.

No comments: