Thursday, September 26, 2013

Re: Something to wrap a selection

Harry Putnam <reader@newsguy.com> a écrit:
> Paul Isambert <zappathustra@free.fr> writes:
>
>
> > For any further fun, try ":help [range]".
>
> Oh no... you're not getting off that easy... hehe.

I was just trying to advertise Vim's excellent help :)

> Honestly, you'll probably wish you never started, but I'm not getting
> enough out of this bit of code to see how to make it do what I want.
>
> Quest 1) How is the code determining what comment char to use?
> I created a file some.conf With some commented lines followed by
> variable value. The comments are `#', and yet the code uses '//'.
> Plus there is no space after // . But seems like it should be '# '
>
> The emacs code tries to guess the right comment char, but if it
> doesn't know from the file name, then it asks me which one to use.

The code relies on an analysis of the "comments" option, which is
normally set by filetype. For .conf files, I can see that the option
is ambiguous (for our purpose), as it contains several characters for
one-line comments, and the code picked up the wrong one (the first
encountered).

The new code below does the following thing: ":MarkChange" can be
called with a exclamation mark, as in:

:MarkChange! {<optional keywords>}

Without the mark, it works as before, trying to find the proper comment
sign by itself. With the exclamation mark, it will prompt for the
proper symbol. The code could be modified further so those symbols are
stored in an external file and you won't have to specify them again.
(Another syntax that you may prefer is ":MarkChange {optional sign}",
with, I hope, an obvious semantics, while the command would always
prompt for keywords.)

As for the space, the new code below adds it.

> Quest 2)
> Further, the already commented lines are not recommented as in my
> posted example (Not the first post... I had it wrong there) So, In
> that case, I can't tell at a glance, 2yrs later, what the original
> suggested value was
>
> Example:
> Lets say we start with this:
>
> # commented line1
> # commented line2
> # suggested value
> my newvalue
>
> Or maybe as often this:
>
> # commented line1
> # commented line2
> suggested value
> my_newvalue
>
>
>
> After calling MarkChange, I want to see this:
>
> # Keywords: whatever I typed
> # [Keydate: DATE]
> # # commented line
> # # commented line
> # # suggested value
> my_newvalue
>
> or
>
> # Keywords: whatever I typed
> # [Keydate: DATE]
> # # commented line
> # # commented line
> # suggested value
> my_newvalue
> ------- --------- ---=--- --------- --------
>
> so I can see right away what was the original value and how I changed
> it.

That's no big problem. The new code does that, although you'll have to
uncomment "my_newvalue" by hand.

> I tried changing even a few minor things in the code and right away it
> blows up and fills the buffer with numerous threats and warnings hehe.
>
> I'm not understanding the syntax much at all.

Man, you can write in Elisp but not in Vimscript? Those Emacs users
are very strange indeed.

Anyway, here's the code:

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

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

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

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

" Comment everything.
let i = a:firstline
while i <= a:lastline+3
call setline(i, comment . getline(i))
let i += 1
endwhile
endfunction

com! -bang -range -nargs=* MarkChange <line1>,<line2>call s:markchange(<q-args>, <bang>0)
" 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: