Browse Source

Add hotkey callback to tinyrl lib

Serj Kalichev 11 years ago
parent
commit
19c4a3932a
3 changed files with 17 additions and 3 deletions
  1. 1 0
      tinyrl/private.h
  2. 14 3
      tinyrl/tinyrl.c
  3. 2 0
      tinyrl/tinyrl.h

+ 1 - 0
tinyrl/private.h

@@ -25,6 +25,7 @@ struct _tinyrl {
 	char *kill_string;
 #define NUM_HANDLERS 256
 	tinyrl_key_func_t *handlers[NUM_HANDLERS];
+	tinyrl_key_func_t *hotkey_fn;
 
 	tinyrl_history_t *history;
 	tinyrl_history_iterator_t hist_iter;

+ 14 - 3
tinyrl/tinyrl.c

@@ -132,9 +132,12 @@ static bool_t tinyrl_key_default(tinyrl_t * this, int key)
 		result = tinyrl_insert_text(this, tmp);
 	} else {
 		char tmp[10];
-		sprintf(tmp, "~%d", key);
-		/* inject control characters as ~N where N is the ASCII code */
-		result = tinyrl_insert_text(this, tmp);
+		/* Call the external hotkey analyzer */
+		if (!this->hotkey_fn || !this->hotkey_fn(this, key)) {
+			sprintf(tmp, "~%d", key);
+			/* inject control characters as ~N where N is the ASCII code */
+			result = tinyrl_insert_text(this, tmp);
+		}
 	}
 	return result;
 }
@@ -485,6 +488,7 @@ tinyrl_init(tinyrl_t * this,
 	this->attempted_completion_function = complete_fn;
 	this->timeout_fn = tinyrl_timeout_default;
 	this->keypress_fn = NULL;
+	this->hotkey_fn = NULL;
 	this->state = 0;
 	this->kill_string = NULL;
 	this->echo_char = '\0';
@@ -1365,6 +1369,13 @@ void tinyrl__set_keypress_fn(tinyrl_t *this,
 	this->keypress_fn = fn;
 }
 
+/*-------------------------------------------------------- */
+void tinyrl__set_hotkey_fn(tinyrl_t *this,
+	tinyrl_key_func_t *fn)
+{
+	this->hotkey_fn = fn;
+}
+
 /*-------------------------------------------------------- */
 bool_t tinyrl_is_quoting(const tinyrl_t * this)
 {

+ 2 - 0
tinyrl/tinyrl.h

@@ -110,6 +110,8 @@ extern void tinyrl__set_timeout_fn(tinyrl_t *instance,
 	tinyrl_timeout_fn_t *fn);
 extern void tinyrl__set_keypress_fn(tinyrl_t *instance,
 	tinyrl_keypress_fn_t *fn);
+extern void tinyrl__set_hotkey_fn(tinyrl_t *instance,
+	tinyrl_key_func_t *fn);
 extern char *tinyrl_readline(tinyrl_t *instance, void *context);
 extern char *tinyrl_forceline(tinyrl_t *instance, 
 	void *context, const char *line);