Browse Source

hotkey: Add hotkey list to entry

Serj Kalichev 1 year ago
parent
commit
7498451dd5
5 changed files with 75 additions and 0 deletions
  1. 2 0
      klish/ientry.h
  2. 4 0
      klish/ischeme.h
  3. 45 0
      klish/ischeme/ientry.c
  4. 9 0
      klish/kentry.h
  5. 15 0
      klish/kscheme/kentry.c

+ 2 - 0
klish/ientry.h

@@ -8,6 +8,7 @@
 
 #include <faux/error.h>
 #include <klish/iaction.h>
+#include <klish/ihotkey.h>
 #include <klish/kentry.h>
 
 typedef struct ientry_s ientry_t;
@@ -27,6 +28,7 @@ struct ientry_s {
 	char *filter;
 	ientry_t * (*entrys)[]; // Nested entrys
 	iaction_t * (*actions)[];
+	ihotkey_t * (*hotkeys)[];
 };
 
 C_DECL_BEGIN

+ 4 - 0
klish/ischeme.h

@@ -15,6 +15,10 @@
 #define END_ACTION_LIST NULL }
 #define ACTION &(iaction_t)
 
+#define HOTKEY_LIST .hotkeys = &(ihotkey_t * []) {
+#define END_HOTKEY_LIST NULL }
+#define HOTKEY &(ihotkey_t)
+
 #define PLUGIN_LIST .plugins = &(iplugin_t * []) {
 #define END_PLUGIN_LIST NULL }
 #define PLUGIN &(iplugin_t)

+ 45 - 0
klish/ischeme/ientry.c

@@ -227,6 +227,29 @@ bool_t ientry_parse_nested(const ientry_t *ientry, kentry_t *kentry,
 		}
 	}
 
+	// HOTKEY list
+	if (ientry->hotkeys) {
+		ihotkey_t **p_ihotkey = NULL;
+		for (p_ihotkey = *ientry->hotkeys; *p_ihotkey; p_ihotkey++) {
+			khotkey_t *khotkey = NULL;
+			ihotkey_t *ihotkey = *p_ihotkey;
+
+			khotkey = ihotkey_load(ihotkey, error);
+			if (!khotkey) {
+				retval = BOOL_FALSE;
+				continue;
+			}
+			if (!kentry_add_hotkeys(kentry, khotkey)) {
+				faux_error_sprintf(error,
+					TAG": Can't add HOTKEY \"%s\"",
+					khotkey_key(khotkey));
+				khotkey_free(khotkey);
+				retval = BOOL_FALSE;
+				continue;
+			}
+		}
+	}
+
 	if (!retval)
 		faux_error_sprintf(error, TAG" \"%s\": Illegal nested elements",
 			kentry_name(kentry));
@@ -278,6 +301,7 @@ char *ientry_deploy(const kentry_t *kentry, int level)
 	char *purpose = NULL;
 	kentry_entrys_node_t *entrys_iter = NULL;
 	kentry_actions_node_t *actions_iter = NULL;
+	kentry_hotkeys_node_t *hotkeys_iter = NULL;
 	char *num = NULL;
 
 	tmp = faux_str_sprintf("%*cENTRY {\n", level, ' ');
@@ -391,6 +415,27 @@ char *ientry_deploy(const kentry_t *kentry, int level)
 			faux_str_cat(&str, tmp);
 			faux_str_free(tmp);
 		}
+
+		// HOTKEY list
+		hotkeys_iter = kentry_hotkeys_iter(kentry);
+		if (hotkeys_iter) {
+			khotkey_t *hotkey = NULL;
+
+			tmp = faux_str_sprintf("\n%*cHOTKEY_LIST\n\n", level + 1, ' ');
+			faux_str_cat(&str, tmp);
+			faux_str_free(tmp);
+
+			while ((hotkey = kentry_hotkeys_each(&hotkeys_iter))) {
+				tmp = ihotkey_deploy(hotkey, level + 2);
+				faux_str_cat(&str, tmp);
+				faux_str_free(tmp);
+			}
+
+			tmp = faux_str_sprintf("%*cEND_HOTKEY_LIST,\n", level + 1, ' ');
+			faux_str_cat(&str, tmp);
+			faux_str_free(tmp);
+		}
+
 	} // ref_str
 
 	tmp = faux_str_sprintf("%*c},\n\n", level, ' ');

+ 9 - 0
klish/kentry.h

@@ -8,11 +8,13 @@
 
 #include <faux/list.h>
 #include <klish/kaction.h>
+#include <klish/khotkey.h>
 
 typedef struct kentry_s kentry_t;
 
 typedef faux_list_node_t kentry_entrys_node_t;
 typedef faux_list_node_t kentry_actions_node_t;
+typedef faux_list_node_t kentry_hotkeys_node_t;
 
 // Mode of nested entrys list
 typedef enum {
@@ -109,6 +111,13 @@ ssize_t kentry_actions_len(const kentry_t *entry);
 kentry_actions_node_t *kentry_actions_iter(const kentry_t *entry);
 kaction_t *kentry_actions_each(kentry_actions_node_t **iter);
 
+// HOTKEYs
+faux_list_t *kentry_hotkeys(const kentry_t *entry);
+bool_t kentry_add_hotkeys(kentry_t *entry, khotkey_t *hotkey);
+ssize_t kentry_hotkeys_len(const kentry_t *entry);
+kentry_hotkeys_node_t *kentry_hotkeys_iter(const kentry_t *entry);
+khotkey_t *kentry_hotkeys_each(kentry_hotkeys_node_t **iter);
+
 // Fast access for nested entries with special purposes
 kentry_t *kentry_nested_by_purpose(const kentry_t *entry, kentry_purpose_e purpose);
 bool_t kentry_set_nested_by_purpose(kentry_t *entry, kentry_purpose_e purpose,

+ 15 - 0
klish/kscheme/kentry.c

@@ -9,6 +9,7 @@
 #include <klish/khelper.h>
 #include <klish/kaction.h>
 #include <klish/kentry.h>
+#include <klish/khotkey.h>
 
 
 struct kentry_s {
@@ -27,6 +28,7 @@ struct kentry_s {
 	bool_t filter; // Is entry filter. Filter can't have inline actions.
 	faux_list_t *entrys; // Nested ENTRYs
 	faux_list_t *actions; // Nested ACTIONs
+	faux_list_t *hotkeys; // Hotkeys
 	// Fast links to nested entries with special purposes.
 	kentry_t** nested_by_purpose;
 	void *udata;
@@ -105,6 +107,14 @@ KNESTED_LEN(entry, actions);
 KNESTED_ITER(entry, actions);
 KNESTED_EACH(entry, kaction_t *, actions);
 
+// HOTKEY list
+KGET(entry, faux_list_t *, hotkeys);
+static KCMP_NESTED(entry, hotkey, key);
+KADD_NESTED(entry, khotkey_t *, hotkeys);
+KNESTED_LEN(entry, hotkeys);
+KNESTED_ITER(entry, hotkeys);
+KNESTED_EACH(entry, khotkey_t *, hotkeys);
+
 
 kentry_t *kentry_new(const char *name)
 {
@@ -146,6 +156,11 @@ kentry_t *kentry_new(const char *name)
 		NULL, NULL, (void (*)(void *))kaction_free);
 	assert(entry->actions);
 
+	// HOTKEY list
+	entry->hotkeys = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
+		kentry_hotkey_compare, NULL, (void (*)(void *))khotkey_free);
+	assert(entry->hotkeys);
+
 	entry->nested_by_purpose = faux_zmalloc(
 		KENTRY_PURPOSE_MAX * sizeof(*(entry->nested_by_purpose)));