Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I a have PHP syntax highlighting file in ~/.vim/syntax/php.vim. Thus, PHP functions are highlighted differently than my own custom functions and Wordpress functions. Therefore, VIM does have a way to determine if a function is a PHP built-in or not.

I am using this script slightly modified to open PHP and Wordpress documentation from within VIM:

function! BrowseDoc()
    if b:current_syntax == "php"
        ! firefox "http://php.net/manual-lookup.php?pattern=<cword>"
        ! firefox "https://codex.wordpress.org/Function_Reference/<cword>"
    elseif b:current_syntax == "perl"
        ! firefox "http://perldoc.perl.org/search.html?q=<cword>"
    elseif b:current_syntax == "python"
        ! firefox "https://docs.python.org/3.5/search.html?q=<cword>"
    else
        return
    endif
endfunction
map <leader>ff :call BrowseDoc()<Return><Return>

How might I use the VIM feature that determines if a function is to be highlighted as PHP or not to determine if php.net or wordpress.org documentation should be opened?

I found the phpFunctions syn keyword in the syntax file, but I've been unable to figure out how to query if the word under the cursor is a member of that family or not.

share|improve this question
up vote 8 down vote accepted

You can determine the current syntax group with this expression:

count(map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")'), "phpFunction")

which should return 1 if the word under the cursor belongs to the phpFunction group and 0 if it doesn't.

share|improve this answer
    
Thank you romainl. There is quite a lot that I learned from this post. – dotancohen Jan 21 at 13:57

Based on romainl's terrific answer I concocted this function to support all the PHP highlighting types:

function! BrowseDoc()
    if b:current_syntax == "php"

        " phpRegion
        let keywords_php = ['phpFCKeyword', 'phpKeyword', 'phpBoolean', 'phpClasses', 'phpConstants', 'phpFunctions', 'phpInclude', 'phpMagicConstants', 'phpServerVars', 'phpStatement', 'phpSuperglobals', 'phpType']
        let is_php = 0

        for kw in keywords_php
            if count(map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")'), kw)
                let is_php = 1
            endif
        endfor

        if is_php
            !firefox "http://php.net/manual-lookup.php?pattern=<cword>"
        else
            !firefox "https://codex.wordpress.org/Function_Reference/<cword>"
        endif

    elseif b:current_syntax == "perl"
        !firefox "http://perldoc.perl.org/search.html?q=<cword>"

    elseif b:current_syntax == "python"
        !firefox "https://docs.python.org/3.5/search.html?q=<cword>"

    else
        execute "!firefox -new-tab 'https://google.com/?q=" . &ft . "\\%20<cword>'"

    endif

endfunction
map <leader>f :call BrowseDoc()<Return><Return>

Thank you romainl!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.