Mahurshi Akilla schrieb:
> I found the following map command on a wiki, which tells the module
> name in a verilog file when F6 is pressed.
>
> map <F6> ma?^\s*\<module\><CR>Wyiw'a:echo "module -->" @0<CR>
> (source: http://vim.wikia.com/wiki/For_verilog_users_only)
>
> I would like to have a similar map statement match an instance name
> when F6 is pressed since it is more useful to me.
>
> Basically, I want it to match something like:
>
> ^\s*([A-Za-z0-9\_]+)\s+(\S+)\s*(

As a vim pattern:
    ^\s*\(\w\+\)\s\+\(\S\+\)\s*(

or in "very magic" notation (which looks more like perl):
    \v^\s*(\w+)\s+(\S+)\s*\(

Vim patterns:
    :h pattern
    :h /\w
    :h magic

> That is how I would describe this as a perl regexp.  I want to match
> both $1 and $2 and have it print something like:  module: $1,
> instance: $2
>
> I am aware that the regexp is not foolproof but it should be okay for
> me for now..
>
> I am not sure how I can translate this to the above map statement.
> Could anyone provide some help?  Thanks.

(1) Here is a somewhat direct translation:

    :no <silent><F6> ma?\v^\s*(\w+)\s+(\S+)\s*\(<CR>^"myeW"iyt(`a
        \:echo printf("module: %s, instance %s", @m,@i)<CR>

Split for easier reading:
    ma
    ?\v^\s*(\w+)\s+(\S+)\s*\(<CR>
    ^
    "mye
    W
    "iyt(
    `a

The grouping parens are kept in the pattern, although not used here.
The Normal mode commands
    ^"myeW"iyt(
walk over what has been matched, but without knowing anything about the
pattern.  It's not exact anyway, "iyt(  might include the white space
before (.


(2) The following version uses a function.  It doesn't touch marks,
registers or the last search pattern; and it keeps the view of the
current window (as it doesn't move the cursor).

map <silent> <F6> :call Verilog_EchoModule()<CR>

func! Verilog_EchoModule()
    let pat = '\v^\s*(\w+)\s+(\S+)\s*\('
    let mod_lnum = search(pat, 'bWn')
    if mod_lnum > 0
        let [module, instance] = matchlist(getline(mod_lnum), pat)[1:2]
        echo printf("module: %s, instance: %s", module, instance)
    else
        echo "Couldn't determine module/instance"
    endif
endfunc


Slightly other ways to map <F6>:

if you put the mapping and the function into after/ftplugin/verilog.vim
(filetype=verilog), a buffer local mapping is recommended:
    :map <buffer><silent> <F6> :call Verilog_EchoModule()<CR>

A safer way to map <F6>:
    :noremap <buffer><silent> <F6> :<C-U>call Verilog_EchoModule()<CR>

c_CTRL-U
<C-U> removes the range that is inserted after {count}<F6>, or if <F6>
was pressed from Visual mode.  A range doesn't hurt, but it would call
the function for every line in the range (moving the cursor).

:map vs. :noremap
noremap is like map with disallowed remapping of keys on the right hand
side of the mapping.  http://vim.wikia.com/wiki/For_verilog_users_only
itself provides a good example why :noremap often is better than :map.
I wondered why they use 'a, where `a would be much better for jumping
back.  The obvious reason: The original mapping was:
    :map ` ma?module<CR>Wyiw'a:echo "module -->" @0<CR>
Thus ` is a key that must not occur on the right hand side, unless
recursion is wanted (which isn't of course).  With :noremap you don't
need the work around:
    :noremap ` ma?module<CR>Wyiw`a:echo "module -->" @0<CR>

-- 
Andy


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply via email to