Skip to content

Commit

Permalink
Disable special key mappings inside {{{ snippets }}}
Browse files Browse the repository at this point in the history
This is my first serious attempt to resolve issue #79 and issue #83.
I was originally worried that this might have serious impact on typing
speed because the <expr> mappings now have to search back through the
current buffer to determine their context.

In practice the slowness isn't that bad and it *is* really nice to type
regular quotes inside code blocks. No doubt this will become a performance
problem for someone editing 10.000 line notes, but we'll cross that bridge
when the get there ;-)
  • Loading branch information
xolox committed Sep 14, 2014
1 parent 7a500ba commit a6a654f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 43 deletions.
90 changes: 67 additions & 23 deletions autoload/xolox/notes.vim
@@ -1,12 +1,12 @@
" Vim auto-load script
" Author: Peter Odding <peter@peterodding.com>
" Last Change: August 4, 2014
" Last Change: September 14, 2014
" URL: http://peterodding.com/code/vim/notes/

" Note: This file is encoded in UTF-8 including a byte order mark so
" that Vim loads the script using the right encoding transparently.

let g:xolox#notes#version = '0.26.1'
let g:xolox#notes#version = '0.27'
let g:xolox#notes#url_pattern = '\<\(mailto:\|javascript:\|\w\{3,}://\)\(\S*\w\)\+/\?'
let s:scriptdir = expand('<sfile>:p:h')

Expand Down Expand Up @@ -386,6 +386,14 @@ function! xolox#notes#omni_complete(findstart, base) " {{{1
endif
endfunction

function! xolox#notes#auto_complete_tags() " {{{1
" Automatic completion of tags when the user types "@".
if !xolox#notes#currently_inside_snippet()
return "@\<C-x>\<C-o>"
endif
return "@"
endfunction

function! xolox#notes#save() abort " {{{1
" When the current note's title is changed, automatically rename the file.
if xolox#notes#filetype_is_note(&ft)
Expand Down Expand Up @@ -967,22 +975,47 @@ function! xolox#notes#insert_ruler() " {{{3
call append(line1, ['', g:notes_ruler_text, ''])
endfunction

function! xolox#notes#insert_quote(style) " {{{3
function! xolox#notes#insert_quote(chr) " {{{3
" XXX When I pass the below string constants as arguments from the file type
" plug-in the resulting strings contain mojibake (UTF-8 interpreted as
" latin1?) even if both scripts contain a UTF-8 BOM! Maybe a bug in Vim?!
if xolox#notes#unicode_enabled()
let [open_quote, close_quote] = a:style == 1 ? ['', ''] : ['', '']
else
let [open_quote, close_quote] = a:style == 1 ? ['`', "'"] : ['"', '"']
if g:notes_smart_quotes && !xolox#notes#currently_inside_snippet()
if xolox#notes#unicode_enabled()
let [open_quote, close_quote] = (a:chr == "'") ? ['', ''] : ['', '']
else
let [open_quote, close_quote] = (a:chr == "'") ? ['`', "'"] : ['"', '"']
endif
return getline('.')[col('.')-2] =~ '[^\t (]$' ? close_quote : open_quote
endif
return getline('.')[col('.')-2] =~ '[^\t (]$' ? close_quote : open_quote
return a:chr
endfunction

function! xolox#notes#insert_em_dash() " {{{3
" Change double-dash (--) to em-dash (—) as it is typed.
return (g:notes_smart_quotes && xolox#notes#unicode_enabled() && !xolox#notes#currently_inside_snippet()) ? '' : '--'
endfunction

function! xolox#notes#insert_left_arrow() " {{{3
" Change ASCII left arrow (<-) to Unicode arrow (←) as it is typed.
return (g:notes_smart_quotes && xolox#notes#unicode_enabled() && !xolox#notes#currently_inside_snippet()) ? '' : "<-"
endfunction

function! xolox#notes#insert_right_arrow() " {{{3
" Change ASCII right arrow (->) to Unicode arrow (→) as it is typed.
return (g:notes_smart_quotes && xolox#notes#unicode_enabled() && !xolox#notes#currently_inside_snippet()) ? '' : '->'
endfunction

function! xolox#notes#insert_bidi_arrow() " {{{3
" Change bidirectional ASCII arrow (->) to Unicode arrow (→) as it is typed.
return (g:notes_smart_quotes && xolox#notes#unicode_enabled() && !xolox#notes#currently_inside_snippet()) ? '' : "<->"
endfunction

function! xolox#notes#insert_bullet(chr) " {{{3
" Insert a UTF-8 list bullet when the user types "*".
if getline('.')[0 : max([0, col('.') - 2])] =~ '^\s*$'
return xolox#notes#get_bullet(a:chr)
if !xolox#notes#currently_inside_snippet()
if getline('.')[0 : max([0, col('.') - 2])] =~ '^\s*$'
return xolox#notes#get_bullet(a:chr)
endif
endif
return a:chr
endfunction
Expand Down Expand Up @@ -1209,23 +1242,34 @@ function! xolox#notes#foldexpr() " {{{3
let retval = '>' . nextlevel
endif
endif
if retval != '='
" Check whether the change in folding introduced by 'rv'
" is invalidated because we're inside a code block.
let pos_save = getpos('.')
try
call setpos('.', [0, v:lnum, 1, 0])
if search('{{{\|\(}}}\)', 'bnpW') == 1
let retval = '='
endif
finally
" Always restore the cursor position!
call setpos('.', pos_save)
endtry
" Check whether the change in folding introduced by 'rv'
" is invalidated because we're inside a code block.
if retval != '=' && xolox#notes#inside_snippet(v:lnum, 1)
let retval = '='
endif
return retval
endfunction

function! xolox#notes#inside_snippet(lnum, col) " {{{3
" Check if the given line and column position is inside a snippet (a code
" block enclosed by triple curly brackets). This function temporarily
" changes the cursor position in the current buffer in order to search
" backwards efficiently.
let pos_save = getpos('.')
try
call setpos('.', [0, a:lnum, a:col, 0])
return search('{{{\|\(}}}\)', 'bnpW') == 1
finally
call setpos('.', pos_save)
endtry
endfunction

function! xolox#notes#currently_inside_snippet() " {{{3
" Check if the current cursor position is inside a snippet (a code block
" enclosed by triple curly brackets).
return xolox#notes#inside_snippet(line('.'), col('.'))
endfunction

function! xolox#notes#foldtext() " {{{3
" Replace atx style "#" markers with "-" fold marker.
let line = getline(v:foldstart)
Expand Down
34 changes: 14 additions & 20 deletions ftplugin/notes.vim
@@ -1,6 +1,6 @@
" Vim file type plug-in
" Author: Peter Odding <peter@peterodding.com>
" Last Change: July 7, 2014
" Last Change: September 14, 2014
" URL: http://peterodding.com/code/vim/notes/

if exists('b:did_ftplugin')
Expand Down Expand Up @@ -51,36 +51,30 @@ setlocal omnifunc=xolox#notes#omni_complete
let b:undo_ftplugin .= ' | set omnifunc<'

" Automatic completion of tag names after typing "@". {{{1

inoremap <buffer> <silent> @ @<C-x><C-o>
inoremap <buffer> <silent> <expr> @ xolox#notes#auto_complete_tags()
let b:undo_ftplugin .= ' | execute "iunmap <buffer> @"'

" Automatic completion of tag names should not interrupt the flow of typing,
" for this we have to change the (unfortunately) global option &completeopt.
set completeopt+=longest

" Change double-dash to em-dash as it is typed. {{{1
if g:notes_smart_quotes && xolox#notes#unicode_enabled()
inoremap <buffer> --
let b:undo_ftplugin .= ' | execute "iunmap <buffer> --"'
endif
inoremap <buffer> <expr> -- xolox#notes#insert_em_dash()
let b:undo_ftplugin .= ' | execute "iunmap <buffer> --"'

" Change plain quotes to curly quotes as they're typed. {{{1
if g:notes_smart_quotes
inoremap <buffer> <expr> ' xolox#notes#insert_quote(1)
inoremap <buffer> <expr> " xolox#notes#insert_quote(2)
let b:undo_ftplugin .= ' | execute "iunmap <buffer> ''"'
let b:undo_ftplugin .= ' | execute ''iunmap <buffer> "'''
endif
inoremap <buffer> <expr> ' xolox#notes#insert_quote("'")
inoremap <buffer> <expr> " xolox#notes#insert_quote('"')
let b:undo_ftplugin .= ' | execute "iunmap <buffer> ''"'
let b:undo_ftplugin .= ' | execute ''iunmap <buffer> "'''

" Change ASCII style arrows to Unicode arrows. {{{1
if g:notes_smart_quotes && xolox#notes#unicode_enabled()
inoremap <buffer> ->
inoremap <buffer> <-
inoremap <buffer> <->
let b:undo_ftplugin .= ' | execute "iunmap <buffer> ->"'
let b:undo_ftplugin .= ' | execute "iunmap <buffer> <-"'
endif
inoremap <buffer> <expr> <- xolox#notes#insert_left_arrow()
inoremap <buffer> <expr> -> xolox#notes#insert_right_arrow()
inoremap <buffer> <expr> <-> xolox#notes#insert_bidi_arrow()
let b:undo_ftplugin .= ' | execute "iunmap <buffer> ->"'
let b:undo_ftplugin .= ' | execute "iunmap <buffer> <-"'
let b:undo_ftplugin .= ' | execute "iunmap <buffer> <->"'

" Convert ASCII list bullets to Unicode bullets. {{{1
if g:notes_smart_quotes
Expand Down

0 comments on commit a6a654f

Please sign in to comment.