4

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.

7

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.

  • Thank you romainl. There is quite a lot that I learned from this post. – dotancohen Jan 21 '16 at 13:57
4

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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