Syntax awareness.

This commit is contained in:
Israel Chauca Fuentes
2010-05-24 22:11:23 -05:00
parent e5ce5b0967
commit da66a3f7c9
5 changed files with 137 additions and 60 deletions

View File

@@ -5,7 +5,7 @@ AUTOL=autoload/$(PLUGIN).vim
DOC=$(wildcard doc/*.txt)
TESTS=$(wildcard autoload/*Tests.vim)
VERSION=$(shell perl -ne 'if (/\*\sCurrent\srelease:/) {s/^\s+(\d+\.\d+).*$$/\1/;print}' $(DOC))
VIMFOLDER=~/.vim
VIMFOLDER=~/.vim/
VIM=/usr/bin/vim
.PHONY: $(PLUGIN).vba README
@@ -13,7 +13,7 @@ VIM=/usr/bin/vim
install: vimball
@echo install
$(VIM) -N -c ':so %' -c':q!' $(PLUGIN)-$(VERSION).vba
cp -f autoload/$(PLUGIN)Tests.vim $(VIMFOLDER)/autoload/$(PLUGIN)Tests.vim
cp -f $(TESTS) $(VIMFOLDER)$(TESTS)
all: uninstall vimball install README zip gzip
@@ -28,7 +28,7 @@ dist-clean: clean
uninstall:
@echo uninstall
$(VIM) -N -c':RmVimball' -c':q!' $(PLUGIN)-$(VERSION).vba
rm -f $(VIMFOLDER)/autoload/$(PLUGIN)Tests.txt
rm -f $(VIMFOLDER)$(TESTS)
undo:
for i in */*.orig; do mv -f "$$i" "$${i%.*}"; done
@@ -48,7 +48,7 @@ zip:
rm -f *.zip
zip -r $(PLUGIN).zip doc plugin autoload
zip $(PLUGIN).zip -d \*.sw\?
zip $(PLUGIN).zip -d autoload/$(PLUGIN)Tests.vim
zip $(PLUGIN).zip -d $(TESTS)
ln -f $(PLUGIN).zip $(PLUGIN)-$(VERSION).zip
gzip: vimball

View File

@@ -1,7 +1,7 @@
" ============================================================================
" File: autoload/delimitMate.vim
" Version: 2.2
" Modified: 2010-05-16
" Version: 2.3
" Modified: 2010-05-24
" Description: This plugin provides auto-completion for quotes, parens, etc.
" Maintainer: Israel Chauca F. <israelchauca@gmail.com>
" Manual: Read ":help delimitMate".
@@ -304,8 +304,16 @@ function! delimitMate#RestoreRegister() " {{{
echo ""
endfunction " }}}
function! delimitMate#GetSyntaxRegion(line, col) "{{{
return synIDattr(synIDtrans(synID(a:line, a:col, 1)), 'name')
endfunction " }}}
function! delimitMate#GetCurrentSyntaxRegion() "{{{
return synIDattr(synIDtrans(synID(line('.'), col('.'), 1)), 'name')
let col = col('.')
if col == col('$')
let col = col - 1
endif
return delimitMate#GetSyntaxRegion(line('.'), col)
endfunction " }}}
function! delimitMate#GetCurrentSyntaxRegionIf(char) "{{{
@@ -313,23 +321,25 @@ function! delimitMate#GetCurrentSyntaxRegionIf(char) "{{{
let origin_line = getline('.')
let changed_line = strpart(origin_line, 0, col - 1) . a:char . strpart(origin_line, col - 1)
call setline('.', changed_line)
let region = synIDattr(synIDtrans(synID(line('.'), col, 1)), 'name')
let region = delimitMate#GetSyntaxRegion(line('.'), col)
call setline('.', origin_line)
return region
endfunction "}}}
function! delimitMate#IsForbidden(char) "{{{
if b:delimitMate_excluded_regions_enabled = 0
if b:delimitMate_excluded_regions_enabled == 0
return 0
endif
let result = index(b:delimitMate_excluded_regions_list, delimitMate#GetCurrentSyntaxRegion()) >= 0
if result >= 0
return result + 1
"let result = index(b:delimitMate_excluded_regions_list, delimitMate#GetCurrentSyntaxRegion()) >= 0
if index(b:delimitMate_excluded_regions_list, delimitMate#GetCurrentSyntaxRegion()) >= 0
"echom "Forbidden 1!"
return 1
endif
let region = delimitMate#GetCurrentSyntaxRegionIf(a:char)
let result = index(b:delimitMate_excluded_regions_list, region) >= 0
"let result = index(b:delimitMate_excluded_regions_list, region) >= 0
"return result || region == 'Comment'
return result + 1
"echom "Forbidden 2!"
return index(b:delimitMate_excluded_regions_list, region) >= 0
endfunction "}}}
function! delimitMate#FlushBuffer() " {{{
@@ -340,6 +350,9 @@ endfunction " }}}
" Doers {{{
function! delimitMate#JumpIn(char) " {{{
if delimitMate#IsForbidden(a:char)
return ''
endif
let line = getline('.')
let col = col('.')-2
if (col) < 0
@@ -354,6 +367,9 @@ function! delimitMate#JumpIn(char) " {{{
endfunction " }}}
function! delimitMate#JumpOut(char) "{{{
if delimitMate#IsForbidden(a:char)
return a:char
endif
let line = getline('.')
let col = col('.')-2
if line[col+1] == a:char
@@ -363,7 +379,13 @@ function! delimitMate#JumpOut(char) "{{{
endif
endfunction " }}}
function! delimitMate#JumpAny() " {{{
function! delimitMate#JumpAny(key) " {{{
if delimitMate#IsForbidden('')
return a:key
endif
if !delimitMate#ShouldJump()
return a:key
endif
" Let's get the character on the right.
let char = getline('.')[col('.')-1]
if char == " "
@@ -383,6 +405,9 @@ function! delimitMate#JumpAny() " {{{
endfunction " delimitMate#JumpAny() }}}
function! delimitMate#SkipDelim(char) "{{{
if delimitMate#IsForbidden(a:char)
return a:char
endif
let col = col('.') - 1
let line = getline('.')
if col > 0
@@ -410,6 +435,9 @@ function! delimitMate#SkipDelim(char) "{{{
endfunction "}}}
function! delimitMate#QuoteDelim(char) "{{{
if delimitMate#IsForbidden(a:char)
return a:char
endif
let line = getline('.')
let col = col('.') - 2
if line[col] == "\\"
@@ -442,6 +470,9 @@ function! delimitMate#MapMsg(msg) "{{{
endfunction "}}}
function! delimitMate#ExpandReturn() "{{{
if delimitMate#IsForbidden("")
return "\<CR>"
endif
if delimitMate#WithinEmptyPair()
" Expand:
call delimitMate#FlushBuffer()
@@ -453,6 +484,9 @@ function! delimitMate#ExpandReturn() "{{{
endfunction "}}}
function! delimitMate#ExpandSpace() "{{{
if delimitMate#IsForbidden("\<Space>")
return "\<Space>"
endif
if delimitMate#WithinEmptyPair()
" Expand:
call insert(b:delimitMate_buffer, 's')
@@ -463,16 +497,17 @@ function! delimitMate#ExpandSpace() "{{{
endfunction "}}}
function! delimitMate#BS() " {{{
if delimitMate#IsForbidden("")
return "\<BS>"
endif
if delimitMate#WithinEmptyPair()
"call delimitMate#RmBuffer(1)
return "\<BS>" . delimitMate#Del()
" return "\<Right>\<BS>\<BS>"
elseif b:delimitMate_expand_space &&
\ delimitMate#IsSpaceExpansion()
elseif delimitMate#IsSpaceExpansion()
"call delimitMate#RmBuffer(1)
return "\<BS>" . delimitMate#Del()
elseif b:delimitMate_expand_cr &&
\ delimitMate#IsCRExpansion()
elseif delimitMate#IsCRExpansion()
return "\<BS>\<Del>"
else
return "\<BS>"
@@ -499,7 +534,7 @@ function! delimitMate#Finish() " {{{
let b:delimitMate_buffer = []
let line = getline('.')
let col = col('.') -2
echom 'col: ' . col . '-' . line[:col] . "|" . line[col+len+1:] . '%' . buffer
"echom 'col: ' . col . '-' . line[:col] . "|" . line[col+len+1:] . '%' . buffer
if col < 0
call setline('.', line[col+len+1:])
else
@@ -529,7 +564,7 @@ endfunction " }}}
function! delimitMate#NoAutoClose() "{{{
" inoremap <buffer> ) <C-R>=delimitMate#SkipDelim('\)')<CR>
for delim in b:delimitMate_right_delims + b:delimitMate_quotes_list
exec 'inoremap <buffer> ' . delim . ' <C-R>=delimitMate#SkipDelim("' . escape(delim,'"') . '")<CR>'
exec 'inoremap <buffer> ' . delim . ' <C-R>=delimitMate#SkipDelim("' . escape(delim,'"\|') . '")<CR>'
endfor
endfunction "}}}
@@ -576,21 +611,21 @@ function! delimitMate#ExtraMappings() "{{{
inoremap <buffer> <BS> <C-R>=delimitMate#BS()<CR>
" If pair is empty, delete closing delimiter:
inoremap <buffer> <expr> <S-BS> delimitMate#WithinEmptyPair() ? "\<Del>" : "\<S-BS>"
inoremap <buffer> <expr> <S-BS> delimitMate#WithinEmptyPair() && !delimitMate#IsForbidden("") ? "\<Del>" : "\<S-BS>"
" Expand return if inside an empty pair:
if b:delimitMate_expand_cr != 0
inoremap <buffer> <expr> <CR> delimitMate#WithinEmptyPair() ? "\<C-R>=delimitMate#ExpandReturn()\<CR>" : "\<CR>"
inoremap <buffer> <CR> <C-R>=delimitMate#ExpandReturn()<CR>
endif
" Expand space if inside an empty pair:
if b:delimitMate_expand_space != 0
inoremap <buffer> <expr> <Space> delimitMate#WithinEmptyPair() ? "\<C-R>=delimitMate#ExpandSpace()\<CR>" : "\<Space>"
inoremap <buffer> <Space> <C-R>=delimitMate#ExpandSpace()<CR>
endif
" Jump out ot any empty pair:
if b:delimitMate_tab2exit
inoremap <buffer> <expr> <S-Tab> delimitMate#ShouldJump() ? "\<C-R>=delimitMate#JumpAny()\<CR>" : "\<S-Tab>"
inoremap <buffer> <S-Tab> <C-R>=delimitMate#JumpAny("\<S-Tab>")<CR>
endif
" Fix the re-do feature:

View File

@@ -226,7 +226,12 @@ function! delimitMateTests#Main() " {{{
" Deactivate parens on comments: The first call to a closing delimiter
" will not work here as expected, but it does in real life tests.
set ft=vim
call Type("Deactivate parens on comments", "\"()", ["\"()"], ["autoclose:0"], 1)
call Type("Deactivate parens on comments", "\"()[]", ["\"()[]|"], ["autoclose:0"], 1)
set ft=
" Deactivate quotes on comments: See previous note.
set ft=vim
call Type("Deactivate parens on comments", "\"(`", ["\"(``|"], [], 1)
set ft=
" Manual close at start of line

View File

@@ -1,16 +1,16 @@
*delimitMate* Trying to keep those beasts at bay! v2.2 *delimitMate.txt*
*delimitMate* Trying to keep those beasts at bay! v2.3 *delimitMate.txt*
========================================================================= ~
==== ========= ========================== ===== ===================== ~
==== ========= ========================== === ===================== ~
==== ========= ===================== === = = ========== ========= ~
==== === === == == = = === == == == == === === === == ~
== == = == ====== ======= === ===== == = === === = = ~
= = == == == == = = == === === ===== ===== === === = ~
= = == ===== == == = = == === === ===== === === === ==== ~
= = == = == == == = = == === === ===== == = === === = = ~
== === === == == = = == === == ===== === === === == ~
========================================================================= ~
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMM MMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMM MMMMM MMMMMMMMMMMMMMMMMMMMM ~
MMMM MMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMM MMM MMMMMMMMMMMMMMMMMMMMM
MMMM MMMMMMMMM MMMMMMMMMMMMMMMMMMMMM MMM M M MMMMMMMMMM MMMMMMMMM ~
MMMM MMM MMM MM MM M M MMM MM MM MM MM MMM MMM MMM MM
MM MM M MM MMMMMM MMMMMMM MMM MMMMM MM M MMM MMM M M ~
M M MM MM MM MM M M MM MMM MMM MMMMM MMMMM MMM MMM M
M M MM MMMMM MM MM M M MM MMM MMM MMMMM MMM MMM MMM MMMM ~
M M MM M MM MM MM M M MM MMM MMM MMMMM MM M MMM MMM M M
MM MMM MMM MM MM M M MM MMM MM MMMMM MMM MMM MMM MM ~
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
REFERENCE MANUAL *
@@ -25,6 +25,7 @@
2.4 Visual wrapping____________________|delimitMateVisualWrapping|
2.5 Smart Quotes_______________________|delimitMateSmartQuotes|
2.6 FileType based configuration_______|delimitMateFileType|
2.7 Syntax awareness___________________|delimitMateSyntax|
3. Customization___________________________|delimitMateOptions|
3.1 Options summary____________________|delimitMateOptionSummary|
3.2 Options details____________________|delimitMateOptionDetails|
@@ -189,9 +190,21 @@ the following way: >
| - File types for which the option will be set.
- Don't forget to put this event.
<
NOTE that if you use buffer variables (|b:vars|) to set global settings in
your vimrc, those settings will be set for the first buffer, but they'll be
ignored for new buffers and default values will be set.
NOTE that you should use buffer variables (|b:var|) only to set options with
|:autocmd|, for global options use regular variables (|g:var|) in your vimrc.
------------------------------------------------------------------------------
2.7 SYNTAX AWARENESS *delimitMateSyntax*
The features of this plug-in might not be always helpful, comments and strings
usualy don't need auto-completion. delimitMate monitors which region is being
edited and if it detects that the cursor is in a comment it'll turn itself off
until the cursor leaves the comment. The excluded regions by can be set using
the option |'delimitMate_excluded_regions'|. Read |group-name| for a list of
regions or syntax group names. NOTE that this feature relies on a
proper syntax file for the current file type, if the syntax file doesn't
define a region, delimitMate won't know about it.
==============================================================================
3. CUSTOMIZATION *delimitMateOptions*
@@ -224,6 +237,9 @@ specific file types, see |delimitMateOptionDetails| for examples.
|'delimitMate_excluded_ft'| Turns off the script for the given file types.
|'delimitMate_excluded_regions'|Turns off the script for the given regions or
syntax group names.
|'delimitMate_apostrophes'| Tells delimitMate how it should "fix"
balancing of single quotes when used as
apostrophes. NOTE: Not needed any more, kept
@@ -333,6 +349,16 @@ only if you don't want any of the features it provides on those file types.
e.g.: >
let delimitMate_excluded_ft = "mail,txt"
<
------------------------------------------------------------------------------
*'delimitMate_excluded_regions'*
Values: A string of syntax group names names separated by single commas. ~
Default: Comment ~
This options turns delimitMate off for the listed regions, read |group-name|
for more info.
e.g.: >
let delimitMate_excluded_regions = "Comments,String"
<
------------------------------------------------------------------------------
*'delimitMate_apostrophes'*
Values: Strings separated by ":". ~
@@ -437,6 +463,9 @@ represented by an "|": >
==============================================================================
5. FUNCTIONS *delimitMateFunctions*
Functions should be used enclosed between <C-R>= and <CR>, otherwise they
might not work as expected or at all.
------------------------------------------------------------------------------
delimitMate#WithinEmptyPair() *delimitMate_WithinEmptyPair*
@@ -445,23 +474,23 @@ Returns 1 if the cursor is inside an empty pair, 0 otherwise.
------------------------------------------------------------------------------
delimitMate#ExpandReturn() *delimitMate#ExpandReturn()*
Returns the expansion for <CR>.
Returns the expansion for <CR> if enabled and inside an empty pair, returns
<CR> otherwise.
e.g.: This mapping could be used to select an item on a pop-up menu or expand
<CR> inside an empty pair: >
inoremap <expr> <CR> pumvisible() ? "\<c-y>" :
\ delimitMate#WithinEmptyPair() ?
\ delimitMate#ExpandReturn() : "\<CR>"
\ "\<C-R>=delimitMate#ExpandReturn()\<CR>"
<
------------------------------------------------------------------------------
delimitMate#ExpandSpace() *delimitMate#ExpandSpace()*
Returns the expansion for <Space>.
Returns the expansion for <Space> if enabled and inside an empty pair, returns
<Space> otherwise.
e.g.: >
inoremap <expr> <Space> delimitMate#WithinEmptyPair() ?
\ delimitMate#ExpandSpace() : "\<Space>"
inoremap <Space> <C-R>=delimitMate#ExpandSpace()<CR>
<
------------------------------------------------------------------------------
delimitMate#ShouldJump() *delimitMate#ShouldJump()*
@@ -470,13 +499,13 @@ Returns 1 if there is a closing delimiter or a quote to the right of the
cursor, 0 otherwise.
------------------------------------------------------------------------------
delimitMate#JumpAny() *delimitMate#JumpAny()*
delimitMate#JumpAny(key) *delimitMate#JumpAny()*
This function returns a mapping that will make the cursor jump to the right.
This function returns a mapping that will make the cursor jump to the right
when delimitMate#ShouldJump() returns 1, returns the argument "key" otherwise.
e.g.: You can use this to create your own mapping to jump over any delimiter.
>
inoremap <expr> <C-Tab> delimitMate#ShouldJump() ?
\ delimitMate#JumpAny() : "\<C-Tab>"
inoremap <C-Tab> <C-R>=delimitMate#JumpAny("\<S-Tab>")<CR>
<
==============================================================================
@@ -519,8 +548,14 @@ This script was inspired by the auto-completion of delimiters of TextMate.
Version Date Release notes ~
|---------|------------|-----------------------------------------------------|
2.2 2010-05-16 * Current release:
- Added command to switch the plug-in on and off.
2.3 2010-05-24 * Current release:
- Syntax aware: Will turn off when editing comments
or other regions.
- Changed format of most mappings.
- Fix: <CR> expansion doesn't brake automatic
indentation adjustments anymore.
|---------|------------|-----------------------------------------------------|
2.2 2010-05-16 * - Added command to switch the plug-in on and off.
- Fix: some problems with <Left>, <Right> and <CR>.
- Fix: A small problem when inserting a delimiter at
the beginning of the line.
@@ -596,9 +631,11 @@ This script was inspired by the auto-completion of delimiters of TextMate.
|---------|------------|-----------------------------------------------------|
... |"| _ _ . . ___ ~
o,*,(o o) _|_|_ o' \,=./ `o . .:::. /_\ `* ~
8(o o)(_)Ooo (o o) (o o) :(o o): . (o o) ~
---ooO-(_)---Ooo----ooO--(_)--Ooo-ooO--(_)--Ooo-ooO--(_)--Ooo-ooO--(_)--Ooo- ~
`\|||/´ MMM \|/ www __^__ ~
(o o) (o o) @ @ (O-O) /(o o)\\ ~
ooO_(_)_Ooo__ ooO_(_)_Ooo___oOO_(_)_OOo___oOO__(_)__OOo___oOO__(_)__OOo_____ ~
_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|____ ~
__|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_ ~
_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|____ ~
vim:tw=78:ts=8:ft=help:norl:formatoptions+=tcroqn:autoindent:

View File

@@ -1,7 +1,7 @@
" ============================================================================
" File: plugin/delimitMate.vim
" Version: 2.2
" Modified: 2010-05-16
" Version: 2.3
" Modified: 2010-05-24
" Description: This plugin provides auto-completion for quotes, parens, etc.
" Maintainer: Israel Chauca F. <israelchauca@gmail.com>
" Manual: Read ":help delimitMate".
@@ -26,7 +26,7 @@ if v:version < 700
endif
let s:loaded_delimitMate = 1 " }}}
let delimitMate_version = '2.2'
let delimitMate_version = '2.3'
"}}}