Thursday, September 26, 2013

Re: Something to wrap a selection

Harry Putnam <reader@newsguy.com> a écrit:
> Paul Isambert <zappathustra@free.fr> writes:
>
> [...]
>
> >> It will look like this when done and I've removed the one comment
> >> to let the new rule work
> >>
> >> # Keywords: Replace old rule with new experimental rule
> >> # [Keydate:130922_214226 0 - Sun Sep 22, 2013
> >> # This is the default setting because blah blah blah
> >> # more blah blah blah blah blah
> >> # current rule in rc file
> >> New rule replacing line above
> >> # &&
> >
> > ... how is the "current rule in rc file" identified as an old code
> > line to be commented? Here I'll assume it is the last but one line,
> > following your example, but I suppose it's quite unlikely that it will
> > always be so. The ":MarkChange" command should be called while the
> > region to be processed is selected; it could be modified so it is
> > called on the old code (to be commented) and then find the limits of
> > the entire region by itself (e.g. with blank lines).
>
> Thanks for the input, I'm just getting started trying to understand
> and use your suggestions.
>
> It's going to sound horribly lame, but I'm already stuck just trying
> to call the function you wrote.
> This isn't the way, I can see
> :markchange <press enter> NOT
>
> So how is it called?

You shall try to call the Ex command (defined with ":com") called
MarkChange, not the function (defined with ":function") called
s:markchange (why I didn't put uppercase letters in the later case, I
don't know, except probably that I don't have too, unlike the Ex
command).

Select (in visual mode) the lines you want to modify, and type

:MarkChange <optional keywords>

If the keywords aren't given, the command will prompt for them.
Actually, since you're in visual mode, the command line will appear as:


:'<,'>MarkChange <optional keywords>

and you could also not go in visual mode at all and specify line
numbers instead, e.g.:


:30,35MarkChange <optional keywords>

For any further fun, try ":help [range]".

> Also I'm afraid I was not very clear about what I'm after. So
> explaining a bit further.
>
> I didn't expect the code to try to identify anything special in the
> selected section, like an uncommented line. Just to comment it all
> after asking me for Keywords.
>
> A typical entry in a rc file with some helpful commented lines might
> look like:
>
> # helpful info
> # commented value
>
> Then I might add
>
> # helpful info
> # commented value
> uncommented value
>
> Then I would select those three lines, call my handy wrapping code
> which would first prompt for keywords, then insert what I gave
> it along with the commented word `Keywords:' like:
>
> # Keywords: prompted words
>
> then Todays date
>
> # [todays date]
>
> next - it would comment every thing selected:
>
> # # helpful info
> # # commented value
> # uncommented value
>
> And finally the closing symbol
>
> # &&
>
> So the end result would be:
>
> # Keywords: prompted words
> # [todays date]
> # # helpful info
> # # commented value
> # uncommented value
>
> Last step... I would manually uncomment the line I wanted:
>
> # Keywords: prompted words
> # [todays date]
> # # helpful info
> # # commented value
> uncommented value
>
> (Sorry if this seems a bit over the top... but wasn't able to make
> clear what I was after and my example was actually incorrect too.)

Your explanation is very fine, but there is one thing I don't
understand: why commenting everything and not just the additional
lines (keywords, date, end symbol), so you won't have to uncomment by
hand afterward?

The code below does just that: it adds the (commented) requested lines
without commenting the new code (nor the old code, since you
mentionned that you did that by hand, which is probably safer; that's
actually the only difference with my original function). I hope it'll
do what you want.

" Put this in your .vimrc file.
function! s:markchange (kw) range
" Retrieve the one-line comment string, hoping it exists (could be
" defined otherwise, too).
let comment = matchstr(&comments, '[[:alnum:]]\@<!:\zs[^,]*\ze')

" Ask for keywords if not given when calling MarkChange.
if a:kw !~ '\S'
let keywords = input("Keywords, please?\n")
else
let keywords = a:kw
endif
let keywords = comment . "Keywords: " . keywords

" Append the end symbol.
call append(a:lastline, comment . "&&")

" Append the keywords and date lines; use :undojoin so ``u'' will undo
" the entire operation.
undojoin
call append(a:firstline-1, [keywords, comment . "[Keydate:" . strftime("%c")])
endfunction

com! -range -nargs=* MarkChange <line1>,<line2>call s:markchange(<q-args>)
" End of code.

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/groups/opt_out.

No comments: