In order to test certain aspects of mg, I use the eval-current-buffer
function. However, that doesn't let me insert a character like 'a' unless
mg is getting that character from another file or previously existing
text, somewhere on disk, or in RAM.
This diff allows eval-current-buffer (or an mg startup file) to insert a
character into a buffer via the self-insert-char function, which emulates
the self-insert-command. For example:
find-file test
self-insert-char a
self-insert-char b
self-insert-char c
newline
save-buffer
The above would create a file called test (if it didn't exist already)
with the contents 'abc\n' when invoked via eval-current-buffer (or an mg
startup file).
The self-insert-command (which is the usual way of inserting a character
into a buffer - but invoked via a 'normal' key press, not M-x
self-insert-command) has other functionality when invoked via
eval-current-buffer (or an mg startup file), see the man page. However, I
may have missed some functionality of mg that allows a character to be
inserted into a buffer via eval-current-buffer in this way. If so, please
let me know. Though this new function does use the code I want to use/test
(self-insert-command). Also, I have not included a man page addition since
I am not too sure how useful the new function is to others. However should
you run mg regress tests, it could be useful.
Comments/criticisms/advice welcome. As are oks.
Mark
Index: def.h
===================================================================
RCS file: /cvs/src/usr.bin/mg/def.h,v
retrieving revision 1.161
diff -u -p -u -p -r1.161 def.h
--- def.h 2 Jul 2019 16:25:39 -0000 1.161
+++ def.h 2 Jul 2019 16:56:09 -0000
@@ -490,6 +490,7 @@ int rescan(int, int);
int universal_argument(int, int);
int digit_argument(int, int);
int negative_argument(int, int);
+int ask_selfinsert(int, int);
int selfinsert(int, int);
int quote(int, int);
Index: funmap.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/funmap.c,v
retrieving revision 1.57
diff -u -p -u -p -r1.57 funmap.c
--- funmap.c 2 Jul 2019 16:25:39 -0000 1.57
+++ funmap.c 2 Jul 2019 16:56:09 -0000
@@ -179,6 +179,7 @@ static struct funmap functnames[] = {
{searchagain, "search-again",},
{backsearch, "search-backward",},
{forwsearch, "search-forward",},
+ {ask_selfinsert, "self-insert-char",},
{selfinsert, "self-insert-command",},
{sentencespace, "sentence-end-double-space",},
#ifdef REGEX
Index: kbd.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/kbd.c,v
retrieving revision 1.32
diff -u -p -u -p -r1.32 kbd.c
--- kbd.c 26 Jun 2019 16:54:29 -0000 1.32
+++ kbd.c 2 Jul 2019 16:56:09 -0000
@@ -323,6 +323,22 @@ negative_argument(int f, int n)
return (mgwrap(funct, FFNEGARG, nn));
}
+int
+ask_selfinsert(int f, int n)
+{
+ char *c, cbuf[2];
+
+ if ((c = eread("Insert a character: ", cbuf, sizeof(cbuf),
+ EFNEW)) == NULL || (c[0] == '\0'))
+ return (ABORT);
+
+ key.k_chars[0] = *c;
+ key.k_chars[1] = '\0';
+ key.k_count = 1;
+
+ return (selfinsert(FFRAND, 1));
+}
+
/*
* Insert a character. While defining a macro, create a "LINE" containing
* all inserted characters.