Skip to content

Instantly share code, notes, and snippets.

@mattn
Created October 21, 2011 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattn/1303356 to your computer and use it in GitHub Desktop.
Save mattn/1303356 to your computer and use it in GitHub Desktop.
diff -r 37ecb8ff4560 src/edit.c
--- a/src/edit.c Thu Oct 20 22:22:38 2011 +0200
+++ b/src/edit.c Fri Oct 21 16:19:52 2011 +0900
@@ -784,7 +784,17 @@
* completion: Add to "compl_leader". */
if (ins_compl_accept_char(c))
{
- ins_compl_addleader(c);
+ set_vim_var_char(c);
+ ++textlock;
+ if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL,
+ FALSE, curbuf))
+ {
+ c = PTR2CHAR(get_vim_var_str(VV_CHAR));
+ }
+ if (c)
+ ins_compl_addleader(c);
+ set_vim_var_string(VV_CHAR, NULL, -1);
+ --textlock;
continue;
}
@@ -1393,31 +1403,37 @@
#ifdef FEAT_AUTOCMD
if (!p_paste)
{
+ int executed;
/* Trigger the InsertCharPre event. Lock the text to avoid
* weird things from happening. */
set_vim_var_char(c);
++textlock;
- if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL,
- FALSE, curbuf))
+ executed = apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL,
+ FALSE, curbuf);
+ --textlock;
+ if (executed)
{
/* Get the new value of v:char. If it is more than one
* character insert it literally. */
- char_u *s = get_vim_var_str(VV_CHAR);
- if (MB_CHARLEN(s) > 1)
+ if (stop_arrow() != FAIL)
{
- if (stop_arrow() != FAIL)
+ char_u *str = get_vim_var_str(VV_CHAR);
+ char_u *ptr = str;
+ while (*ptr != NUL)
{
- ins_str(s);
- AppendToRedobuffLit(s, -1);
+ c = PTR2CHAR(ptr);
+ if (c == CAR || c == K_KENTER || c == NL)
+ ins_eol(c);
+ else
+ ins_char(c);
+ ptr += (*mb_ptr2len)(ptr);
}
- c = NUL;
+ AppendToRedobuffLit(str, -1);
}
- else
- c = PTR2CHAR(s);
+ c = NUL;
}
set_vim_var_string(VV_CHAR, NULL, -1);
- --textlock;
/* If the new value is an empty string then don't insert a
* char. */
@@ -5867,6 +5883,8 @@
* Don't do this when 'cindent' or 'indentexpr' is set, because we might
* need to re-indent at a ':', or any other character (but not what
* 'paste' is set)..
+ * Don't do this when there is InsertCharPre autocommand defined,
+ * because we need to fire the event for every character.
*/
#ifdef USE_ON_FLY_SCROLL
dont_scroll = FALSE; /* allow scrolling here */
@@ -5884,6 +5902,9 @@
#ifdef FEAT_RIGHTLEFT
&& !p_ri
#endif
+#ifdef FEAT_AUTOCMD
+ && !has_insertcharpre()
+#endif
)
{
#define INPUT_BUFLEN 100
diff -r 37ecb8ff4560 src/fileio.c
--- a/src/fileio.c Thu Oct 20 22:22:38 2011 +0200
+++ b/src/fileio.c Fri Oct 21 16:19:52 2011 +0900
@@ -9092,6 +9097,15 @@
return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL);
}
+/*
+ * Return TRUE when there is a InsertCharPre autocommand defined.
+ */
+ int
+has_insertcharpre()
+{
+ return (first_autopat[(int)EVENT_INSERTCHARPRE] != NULL);
+}
+
static int
apply_autocmds_group(event, fname, fname_io, force, group, buf, eap)
event_T event;
diff -r 37ecb8ff4560 src/proto/fileio.pro
--- a/src/proto/fileio.pro Thu Oct 20 22:22:38 2011 +0200
+++ b/src/proto/fileio.pro Fri Oct 21 16:19:52 2011 +0900
@@ -43,6 +43,7 @@
int trigger_cursorhold __ARGS((void));
int has_cursormoved __ARGS((void));
int has_cursormovedI __ARGS((void));
+int has_insertcharpre __ARGS((void));
void block_autocmds __ARGS((void));
void unblock_autocmds __ARGS((void));
int has_autocmd __ARGS((event_T event, char_u *sfname, buf_T *buf));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment