aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2018-07-02 10:51:31 +1200
committerTom Ryder <tom@sanctum.geek.nz>2018-07-02 10:58:35 +1200
commitec0551228a91752d2be5f9d771e2cb5a624e6ba4 (patch)
treea8b9855a8378fca5d37f613aefa766a7a3702589
downloadvim-insert-cancel-ec0551228a91752d2be5f9d771e2cb5a624e6ba4.tar.gz
vim-insert-cancel-ec0551228a91752d2be5f9d771e2cb5a624e6ba4.zip
First commit
-rw-r--r--README.md14
-rw-r--r--VERSION1
-rw-r--r--doc/insert_cancel.txt33
-rw-r--r--plugin/insert_cancel.vim35
4 files changed, 83 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..85e81ab
--- /dev/null
+++ b/README.md
@@ -0,0 +1,14 @@
+insert\_cancel.vim
+==================
+
+This plugin provides a mapping target to cancel an insert operation, by
+triggering `i_CTRL-C` and undoing a change if one was made by that insert
+operation.
+
+License
+-------
+
+Copyright (c) [Tom Ryder][1]. Distributed under the same terms as Vim itself.
+See `:help license`.
+
+[1]: https://sanctum.geek.nz/
diff --git a/VERSION b/VERSION
new file mode 100644
index 0000000..6e8bf73
--- /dev/null
+++ b/VERSION
@@ -0,0 +1 @@
+0.1.0
diff --git a/doc/insert_cancel.txt b/doc/insert_cancel.txt
new file mode 100644
index 0000000..df3205f
--- /dev/null
+++ b/doc/insert_cancel.txt
@@ -0,0 +1,33 @@
+*insert_cancel.txt* For Vim version 6.0 Last change: 2018 July 02
+
+DESCRIPTION *insert_cancel*
+
+This plugin provides a mapping target to cancel an insert operation, by
+triggering |i_CTRL-C| and undoing a change if one was made by that insert
+operation.
+
+REQUIREMENTS *insert_cancel-requirements*
+
+This plugin is only available if 'compatible' is not set. It works best if
+|+autocmd| is available, with the |InsertLeave| event from |version7|. It does
+a much better job of telling whether a change was made with these features
+available, which avoids undoing unrelated changes inappropriately.
+
+MAPPINGS *insert_cancel-mappings*
+
+ *<Plug>InsertCancel*
+The single mapping target provided is |<Plug>InsertCancel|, mappable in any
+mode. There is no default key mapping to the target; you should define this
+yourself in your |vimrc|. For example:
+>
+ imap <C-C> <Plug>InsertCancel
+<
+AUTHOR *insert_cancel-author*
+
+Written and maintained by Tom Ryder <tom@sanctum.geek.nz>.
+
+LICENSE *insert_cancel-license*
+
+Licensed for distribution under the same terms as Vim itself (see |license|).
+
+ vim:tw=78:ts=8:ft=help:norl:
diff --git a/plugin/insert_cancel.vim b/plugin/insert_cancel.vim
new file mode 100644
index 0000000..c5af7e2
--- /dev/null
+++ b/plugin/insert_cancel.vim
@@ -0,0 +1,35 @@
+"
+" insert_cancel.vim: Cancel the current insert operation by undoing the last
+" change upon insert exit, if we made a change; intended for remapping
+" insert-mode Ctrl-C.
+"
+" Author: Tom Ryder <tom@sanctum.geek.nz>
+" License: Same as Vim itself
+"
+if exists('g:loaded_insert_cancel') || &compatible
+ finish
+endif
+if v:version < 600
+ finish
+endif
+let g:loaded_insert_cancel = 1
+
+" InsertEnter is only available from Vim 7
+if has('autocmd') && v:version > 700
+ augroup insert_cancel
+ autocmd!
+ autocmd InsertEnter *
+ \ let s:changenr = changenr()
+ augroup END
+endif
+
+" Try to figure out whether we made a change to undo, undo it if so
+function! s:InsertCancel()
+ if &modified && (!exists('s:changenr') || changenr() > s:changenr)
+ silent undo
+ endif
+endfunction
+
+" Provide plugin mapping
+inoremap <silent> <Plug>InsertCancel
+ \ <C-C>:<C-U>call <SID>InsertCancel()<CR>