On Sat, 17 Mar 2012, Tim Chase wrote:

On 03/17/12 13:12, numbchild wrote:
On Sun, Mar 18, 2012 at 1:27 AM, Tim Chase  wrote:
  :autocmd BufWrite * 
%s/\c\<\(select\|insert\|update\|create\|where\|from\|**like\|group\_s\+by\|order\_s\+by\|having\)\>/\U&/g

Adjust for whatever SQL keywords you want to include.

There is a syntax file in vim which is sqlanywhere.vim, it will highlight a lot of keyword. So the smart way is to make uppercase for all of those keywords.

yes, there are a lot of SQL keywords, and optionally functions if you want to highlight those. It would be handy to have something like

:autocmd BufWrite * exec 
'%s/\c\<\('.join(synkeywords('sqlKeyword'),'\|').'\)\>/\U&/g'

but I don't know of any "synkeywords()" sort of function.

Yes, there's no such function built in, but here's a first pass (partially to practice my VimL, partially because I thought it'd be easier):

fun! SynKeywords(group)
    let keywords = []
    redir => txt
    let v:errmsg = ''
    sil! exe 'syn list' a:group
    let had_error = len(v:errmsg)
    redir END
    if had_error
        return keywords
    endif
    let first = 1
    for line in split(txt, "\n")
        " skip the header
        if line =~ '^---'
            continue
        endif
        if first
            let first = 0

            " remove the name and 'xxx'
            let line = substitute(line, '^\S\+\s\+\S\+', '', '')
        endif

        " remove leading whitespace
        let line = substitute(line, '^\s\+', '', '')

        " skip syn-match, syn-region, and hi-link info
        if line =~ 'start\|match\|links to'
            continue
        endif

        " remove nextgroup, skipwhite, contained
        let line = substitute(line, 
'^\(\%(nextgroup=\S\+\|skipwhite\|contained\)\s\+\)\+', '', '')
        let keywords += split(line)
    endfor
    return sort(keywords)
endf

For use in a regex, it's generally a good idea to call reverse() on the returned result, before join()-ing it with '\|'. That's so, e.g. 'some' and 'something' match the longest possible keyword. Don't think it matters in this case, though, since you're anchoring it with '\<' and '\>'.

And it's generally a good idea to add the 'e' flag on the :s///.  (:help 
:s_flags)

:autocmd BufWrite * exec 
'%s/\c\<\('.join(SynKeywords('sqlKeyword'),'\|').'\)\>/\U&/ge'

--
Best,
Ben

--
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

Reply via email to