Browse Source

Return ? as a help key. Use INS to insert question symbol itself

Serj Kalichev 3 years ago
parent
commit
d7bf6375ff
4 changed files with 47 additions and 3 deletions
  1. 2 2
      clish/shell/shell_tinyrl.c
  2. 1 0
      tinyrl/private.h
  3. 42 0
      tinyrl/tinyrl.c
  4. 2 1
      tinyrl/tinyrl.h

+ 2 - 2
clish/shell/shell_tinyrl.c

@@ -48,7 +48,7 @@ static bool_t clish_shell_tinyrl_key_help(tinyrl_t *this, int key)
 {
 	bool_t result = BOOL_TRUE;
 
-	if (tinyrl_is_quoting(this)) {
+	if (tinyrl_is_quoting(this) || tinyrl_get_ins_flag(this)) {
 		/* if we are in the middle of a quote then simply enter a space */
 		result = tinyrl_insert_text(this, "?");
 	} else {
@@ -422,7 +422,7 @@ static void clish_shell_tinyrl_init(tinyrl_t * this)
 {
 	bool_t status;
 	/* bind the '?' key to the help function */
-	status = tinyrl_bind_key(this, KEY_US, clish_shell_tinyrl_key_help);
+	status = tinyrl_bind_key(this, '?', clish_shell_tinyrl_key_help);
 	assert(status);
 
 	/* bind the <RET> key to the help function */

+ 1 - 0
tinyrl/private.h

@@ -20,6 +20,7 @@ struct _tinyrl {
 	tinyrl_completion_func_t *attempted_completion_function;
 	tinyrl_timeout_fn_t *timeout_fn; /* timeout callback */
 	tinyrl_keypress_fn_t *keypress_fn; /* keypress callback */
+	bool_t ins_flag; // Ins key changes mode of pressed keys
 	int state;
 #define RL_STATE_COMPLETING (0x00000001)
 	char *kill_string;

+ 42 - 0
tinyrl/tinyrl.c

@@ -527,8 +527,46 @@ static bool_t tinyrl_key_erase_line(tinyrl_t * this, int key)
 
 	return BOOL_TRUE;
 }
+
 /*-------------------------------------------------------- */
+/* Toggle INSert flag. When INS flag is on then pressing "?" you will get
+ * the "?" itself. When INS flag is off then pressing "?" you will get help.
+ * On each interactive line the INS flag wil be reset. May be it will be used
+ * for another symbols (and hotkeys) later.
+ */
+static bool_t tinyrl_toggle_ins_flag(tinyrl_t *this)
+{
+	if (!this)
+		return BOOL_FALSE;
+
+	if (this->ins_flag)
+		this->ins_flag = BOOL_FALSE;
+	else
+		this->ins_flag = BOOL_TRUE;
 
+	return BOOL_TRUE;
+}
+
+/*-------------------------------------------------------- */
+static bool_t tinyrl_reset_ins_flag(tinyrl_t *this)
+{
+	if (!this)
+		return BOOL_FALSE;
+
+	this->ins_flag = BOOL_FALSE;
+	return BOOL_TRUE;
+}
+
+/*-------------------------------------------------------- */
+bool_t tinyrl_get_ins_flag(tinyrl_t *this)
+{
+	if (!this)
+		return BOOL_FALSE;
+
+	return this->ins_flag;
+}
+
+/*-------------------------------------------------------- */
 /* It's a dirty hack. The function to print help is external but we need
  * to show help on some escape sequences. So suppose raw help key is
  * Ctrl+_ (Control_underscore 0x1f KEY_US) execute external function for this
@@ -573,6 +611,8 @@ static bool_t tinyrl_escape_seq(tinyrl_t *this, const char *esc_seq)
 		result = tinyrl_key_delete(this,key);
 		break;
 	case tinyrl_vt100_INSERT:
+		result = tinyrl_toggle_ins_flag(this);
+		break;
 	case tinyrl_vt100_PGDOWN:
 	case tinyrl_vt100_PGUP:
 	case tinyrl_vt100_UNKNOWN:
@@ -671,6 +711,7 @@ static void tinyrl_init(tinyrl_t * this, FILE * istream, FILE * ostream,
 	this->last_line_size = 0;
 	this->utf8 = BOOL_FALSE;
 	this->machine_interface = BOOL_FALSE;
+	this->ins_flag = BOOL_FALSE;
 
 	/* create the vt100 terminal */
 	this->term = tinyrl_vt100_new(NULL, ostream);
@@ -899,6 +940,7 @@ static char *internal_readline(tinyrl_t * this,
 	this->buffer_size = strlen(this->buffer);
 	this->line = this->buffer;
 	this->context = context;
+	tinyrl_reset_ins_flag(this);
 
 	/* Interactive session */
 	if (tinyrl__get_isatty(this) && !str) {

+ 2 - 1
tinyrl/tinyrl.h

@@ -225,7 +225,8 @@ extern int tinyrl__restore_history(tinyrl_t *instance, const char *fname);
 extern void tinyrl__stifle_history(tinyrl_t *instance, unsigned int stifle);
 extern void tinyrl_set_machine_interface(tinyrl_t *instance);
 extern void tinyrl_set_human_interface(tinyrl_t *instance);
-bool_t tinyrl_is_machine_interface(const tinyrl_t *instance);
+extern bool_t tinyrl_is_machine_interface(const tinyrl_t *instance);
+extern bool_t tinyrl_get_ins_flag(tinyrl_t *this);
 
 _END_C_DECL
 #endif				/* _tinyrl_tinyrl_h */