Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smart quotes in code comments #8

Open
zeorin opened this issue May 1, 2016 · 1 comment
Open

Smart quotes in code comments #8

zeorin opened this issue May 1, 2016 · 1 comment

Comments

@zeorin
Copy link

zeorin commented May 1, 2016

I know this plugin is designed primarily for prose, but I wanted smart quotes in comments in my code.

I've managed to do this. Currently it looks like this in my .vimrc:

" Smart quotes toggle {{{4
function! s:ToggleEducate()
    if g:textobj#quote#educate
        silent NoEducate
        silent let g:textobj#quote#educate = 0 " For smart quotes in comments
        echom "Smart quotes off"
    else
        silent Educate
        silent let g:textobj#quote#educate = 1 " For smart quotes in comments
        echom "Smart quotes on"
    endif
endfunction
nnoremap <Leader>' :call <SID>ToggleEducate()<Cr>

" Smart quotes in comments {{{4
function! s:SmartQuotesInComments()
    " Respect the setting above, only do smart quotes in comments
    " If the educate variable is truthy
    if g:textobj#quote#educate
        if synIDattr(synID(line('.'),col('.')-1,1),'name') =~? 'comment'
            exec 'silent Educate'
        else
            exec 'silent NoEducate'
        endif
    endif
endfunction
augroup smartquotes
    autocmd!
    autocmd InsertCharPre * if index(textlikeft, &filetype) < 0 |
        \   call <SID>SmartQuotesInComments()
    \ | endif
    autocmd InsertLeave * if index(textlikeft, &filetype) < 0 |
        \   exec 'silent NoEducate'
    \ | endif
augroup END

I've achieved this by loading the plugin for both text-like and non-text-like file types. For text-like file types, Educate is enabled when loading the plugin.

For non-text-like file types (basically, for source code), the plugin is loaded, but Educate is not enabled. However, the configuration variable is still set to 1 (after plugin init). I figured that the plugin only read it on initialization anyway (i.e. changing it after init doesn't change the plugin's behaviour), so I've reused it for the following purpose:

In non-text-like file types:

  • if the configuration variable is 1, a function is run before a character is inserted in insert mode. This function checks to see whether we're currently in a piece of text that has a syntax group whose name contains the string comment (case-insentive). If it does, enable Educate. If it does not, disable Educate.
  • if the configuration variable is 0, don't even check what the current syntax group is, and therefore don't enable Educate
  • always disable Educate if we leave insert mode.

The result is that I have smart quotes in my code's comments automatically. I am also able to toggle that off in case I need ASCII quotes in my comments (e.g. when typing a string in VimL, before the closing double quote is entered, Vim thinks we're in a comment—if I couldn't toggle Educate for comments I'd go mad).

I developed and tested this on an Asus Transformer Book (T100TA), which has a wimpy little Atom processor. I didn't notice any performance impact.

I thought this might be of interest to others. Perhaps you'd like to add this functionality to your plugin, or mention this in the docs. ☺

@reedes
Copy link
Collaborator

reedes commented May 4, 2016

Interesting new feature, thanks.

The blacklisting feature in my pencil plugin uses a similar technique of examining the highlight group to determine if Vim's autoformat should be disabled.

I'll mark this as an enhancement for now as I don't have time to look into it deeper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants