Skip to content

Commit a6a654f

Browse files
committed
Disable special key mappings inside {{{ snippets }}}
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 ;-)
1 parent 7a500ba commit a6a654f

File tree

2 files changed

+81
-43
lines changed

2 files changed

+81
-43
lines changed

autoload/xolox/notes.vim

+67-23
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
" Vim auto-load script
22
" Author: Peter Odding <[email protected]>
3-
" Last Change: August 4, 2014
3+
" Last Change: September 14, 2014
44
" URL: http://peterodding.com/code/vim/notes/
55

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

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

@@ -386,6 +386,14 @@ function! xolox#notes#omni_complete(findstart, base) " {{{1
386386
endif
387387
endfunction
388388

389+
function! xolox#notes#auto_complete_tags() " {{{1
390+
" Automatic completion of tags when the user types "@".
391+
if !xolox#notes#currently_inside_snippet()
392+
return "@\<C-x>\<C-o>"
393+
endif
394+
return "@"
395+
endfunction
396+
389397
function! xolox#notes#save() abort " {{{1
390398
" When the current note's title is changed, automatically rename the file.
391399
if xolox#notes#filetype_is_note(&ft)
@@ -967,22 +975,47 @@ function! xolox#notes#insert_ruler() " {{{3
967975
call append(line1, ['', g:notes_ruler_text, ''])
968976
endfunction
969977

970-
function! xolox#notes#insert_quote(style) " {{{3
978+
function! xolox#notes#insert_quote(chr) " {{{3
971979
" XXX When I pass the below string constants as arguments from the file type
972980
" plug-in the resulting strings contain mojibake (UTF-8 interpreted as
973981
" latin1?) even if both scripts contain a UTF-8 BOM! Maybe a bug in Vim?!
974-
if xolox#notes#unicode_enabled()
975-
let [open_quote, close_quote] = a:style == 1 ? ['', ''] : ['', '']
976-
else
977-
let [open_quote, close_quote] = a:style == 1 ? ['`', "'"] : ['"', '"']
982+
if g:notes_smart_quotes && !xolox#notes#currently_inside_snippet()
983+
if xolox#notes#unicode_enabled()
984+
let [open_quote, close_quote] = (a:chr == "'") ? ['', ''] : ['', '']
985+
else
986+
let [open_quote, close_quote] = (a:chr == "'") ? ['`', "'"] : ['"', '"']
987+
endif
988+
return getline('.')[col('.')-2] =~ '[^\t (]$' ? close_quote : open_quote
978989
endif
979-
return getline('.')[col('.')-2] =~ '[^\t (]$' ? close_quote : open_quote
990+
return a:chr
991+
endfunction
992+
993+
function! xolox#notes#insert_em_dash() " {{{3
994+
" Change double-dash (--) to em-dash (—) as it is typed.
995+
return (g:notes_smart_quotes && xolox#notes#unicode_enabled() && !xolox#notes#currently_inside_snippet()) ? '' : '--'
996+
endfunction
997+
998+
function! xolox#notes#insert_left_arrow() " {{{3
999+
" Change ASCII left arrow (<-) to Unicode arrow (←) as it is typed.
1000+
return (g:notes_smart_quotes && xolox#notes#unicode_enabled() && !xolox#notes#currently_inside_snippet()) ? '' : "<-"
1001+
endfunction
1002+
1003+
function! xolox#notes#insert_right_arrow() " {{{3
1004+
" Change ASCII right arrow (->) to Unicode arrow (→) as it is typed.
1005+
return (g:notes_smart_quotes && xolox#notes#unicode_enabled() && !xolox#notes#currently_inside_snippet()) ? '' : '->'
1006+
endfunction
1007+
1008+
function! xolox#notes#insert_bidi_arrow() " {{{3
1009+
" Change bidirectional ASCII arrow (->) to Unicode arrow (→) as it is typed.
1010+
return (g:notes_smart_quotes && xolox#notes#unicode_enabled() && !xolox#notes#currently_inside_snippet()) ? '' : "<->"
9801011
endfunction
9811012

9821013
function! xolox#notes#insert_bullet(chr) " {{{3
9831014
" Insert a UTF-8 list bullet when the user types "*".
984-
if getline('.')[0 : max([0, col('.') - 2])] =~ '^\s*$'
985-
return xolox#notes#get_bullet(a:chr)
1015+
if !xolox#notes#currently_inside_snippet()
1016+
if getline('.')[0 : max([0, col('.') - 2])] =~ '^\s*$'
1017+
return xolox#notes#get_bullet(a:chr)
1018+
endif
9861019
endif
9871020
return a:chr
9881021
endfunction
@@ -1209,23 +1242,34 @@ function! xolox#notes#foldexpr() " {{{3
12091242
let retval = '>' . nextlevel
12101243
endif
12111244
endif
1212-
if retval != '='
1213-
" Check whether the change in folding introduced by 'rv'
1214-
" is invalidated because we're inside a code block.
1215-
let pos_save = getpos('.')
1216-
try
1217-
call setpos('.', [0, v:lnum, 1, 0])
1218-
if search('{{{\|\(}}}\)', 'bnpW') == 1
1219-
let retval = '='
1220-
endif
1221-
finally
1222-
" Always restore the cursor position!
1223-
call setpos('.', pos_save)
1224-
endtry
1245+
" Check whether the change in folding introduced by 'rv'
1246+
" is invalidated because we're inside a code block.
1247+
if retval != '=' && xolox#notes#inside_snippet(v:lnum, 1)
1248+
let retval = '='
12251249
endif
12261250
return retval
12271251
endfunction
12281252

1253+
function! xolox#notes#inside_snippet(lnum, col) " {{{3
1254+
" Check if the given line and column position is inside a snippet (a code
1255+
" block enclosed by triple curly brackets). This function temporarily
1256+
" changes the cursor position in the current buffer in order to search
1257+
" backwards efficiently.
1258+
let pos_save = getpos('.')
1259+
try
1260+
call setpos('.', [0, a:lnum, a:col, 0])
1261+
return search('{{{\|\(}}}\)', 'bnpW') == 1
1262+
finally
1263+
call setpos('.', pos_save)
1264+
endtry
1265+
endfunction
1266+
1267+
function! xolox#notes#currently_inside_snippet() " {{{3
1268+
" Check if the current cursor position is inside a snippet (a code block
1269+
" enclosed by triple curly brackets).
1270+
return xolox#notes#inside_snippet(line('.'), col('.'))
1271+
endfunction
1272+
12291273
function! xolox#notes#foldtext() " {{{3
12301274
" Replace atx style "#" markers with "-" fold marker.
12311275
let line = getline(v:foldstart)

ftplugin/notes.vim

+14-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
" Vim file type plug-in
22
" Author: Peter Odding <[email protected]>
3-
" Last Change: July 7, 2014
3+
" Last Change: September 14, 2014
44
" URL: http://peterodding.com/code/vim/notes/
55

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

5353
" Automatic completion of tag names after typing "@". {{{1
54-
55-
inoremap <buffer> <silent> @ @<C-x><C-o>
54+
inoremap <buffer> <silent> <expr> @ xolox#notes#auto_complete_tags()
5655
let b:undo_ftplugin .= ' | execute "iunmap <buffer> @"'
5756

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

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

6865
" Change plain quotes to curly quotes as they're typed. {{{1
69-
if g:notes_smart_quotes
70-
inoremap <buffer> <expr> ' xolox#notes#insert_quote(1)
71-
inoremap <buffer> <expr> " xolox#notes#insert_quote(2)
72-
let b:undo_ftplugin .= ' | execute "iunmap <buffer> ''"'
73-
let b:undo_ftplugin .= ' | execute ''iunmap <buffer> "'''
74-
endif
66+
inoremap <buffer> <expr> ' xolox#notes#insert_quote("'")
67+
inoremap <buffer> <expr> " xolox#notes#insert_quote('"')
68+
let b:undo_ftplugin .= ' | execute "iunmap <buffer> ''"'
69+
let b:undo_ftplugin .= ' | execute ''iunmap <buffer> "'''
7570

7671
" Change ASCII style arrows to Unicode arrows. {{{1
77-
if g:notes_smart_quotes && xolox#notes#unicode_enabled()
78-
inoremap <buffer> ->
79-
inoremap <buffer> <-
80-
inoremap <buffer> <->
81-
let b:undo_ftplugin .= ' | execute "iunmap <buffer> ->"'
82-
let b:undo_ftplugin .= ' | execute "iunmap <buffer> <-"'
83-
endif
72+
inoremap <buffer> <expr> <- xolox#notes#insert_left_arrow()
73+
inoremap <buffer> <expr> -> xolox#notes#insert_right_arrow()
74+
inoremap <buffer> <expr> <-> xolox#notes#insert_bidi_arrow()
75+
let b:undo_ftplugin .= ' | execute "iunmap <buffer> ->"'
76+
let b:undo_ftplugin .= ' | execute "iunmap <buffer> <-"'
77+
let b:undo_ftplugin .= ' | execute "iunmap <buffer> <->"'
8478

8579
" Convert ASCII list bullets to Unicode bullets. {{{1
8680
if g:notes_smart_quotes

0 commit comments

Comments
 (0)