Browse Source

ksym: ksym_new_ext() function and aliases for permanent and sync flags

Serj Kalichev 1 year ago
parent
commit
15dc3ca2f5
3 changed files with 42 additions and 3 deletions
  1. 16 0
      klish/kscheme/ksym.c
  2. 12 0
      klish/ksym.h
  3. 14 3
      plugins/klish/plugin_init.c

+ 16 - 0
klish/kscheme/ksym.c

@@ -60,6 +60,22 @@ ksym_t *ksym_new(const char *name, ksym_fn function)
 }
 
 
+ksym_t *ksym_new_ext(const char *name, ksym_fn function,
+	tri_t permanent, tri_t sync)
+{
+	ksym_t *sym = NULL;
+
+	sym = ksym_new(name, function);
+	if (!sym)
+		return NULL;
+
+	ksym_set_permanent(sym, permanent);
+	ksym_set_sync(sym, sync);
+
+	return sym;
+}
+
+
 void ksym_free(ksym_t *sym)
 {
 	if (!sym)

+ 12 - 0
klish/ksym.h

@@ -13,11 +13,23 @@ typedef struct ksym_s ksym_t;
 // Callback function prototype
 typedef int (*ksym_fn)(kcontext_t *context);
 
+// Aliases for permanent flag
+#define KSYM_USERDEFINED_PERMANENT TRI_UNDEFINED
+#define KSYM_NONPERMANENT TRI_FALSE
+#define KSYM_PERMANENT TRI_TRUE
+
+// Aliases for sync flag
+#define KSYM_USERDEFINED_SYNC TRI_UNDEFINED
+#define KSYM_UNSYNC TRI_FALSE
+#define KSYM_SYNC TRI_TRUE
+
 
 C_DECL_BEGIN
 
 // ksym_t
 ksym_t *ksym_new(const char *name, ksym_fn function);
+ksym_t *ksym_new_ext(const char *name, ksym_fn function,
+	tri_t permanent, tri_t sync);
 void ksym_free(ksym_t *sym);
 
 const char *ksym_name(const ksym_t *sym);

+ 14 - 3
plugins/klish/plugin_init.c

@@ -20,18 +20,29 @@ const uint8_t kplugin_klish_minor = KPLUGIN_MINOR;
 int kplugin_klish_init(kcontext_t *context)
 {
 	kplugin_t *plugin = NULL;
+	ksym_t *sym = NULL;
 
 	assert(context);
 	plugin = kcontext_plugin(context);
 	assert(plugin);
 
 	// Misc
-	kplugin_add_syms(plugin, ksym_new("nop", klish_nop));
+	kplugin_add_syms(plugin, ksym_new_ext("nop", klish_nop,
+		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
 	kplugin_add_syms(plugin, ksym_new("tsym", klish_tsym));
 
+	// Navigation
+	// Navigation must be permanent (no dry-run) and sync. Because unsync
+	// actions will be fork()-ed so it can't change current path.
+	kplugin_add_syms(plugin, ksym_new_ext("nav", klish_nav,
+		KSYM_PERMANENT, KSYM_SYNC));
+
 	// PTYPEs
-	kplugin_add_syms(plugin, ksym_new("COMMAND", klish_ptype_COMMAND));
-	kplugin_add_syms(plugin, ksym_new("COMMAND_CASE", klish_ptype_COMMAND_CASE));
+	// These PTYPEs are simple and fast so set SYNC flag
+	kplugin_add_syms(plugin, ksym_new_ext("COMMAND", klish_ptype_COMMAND,
+		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
+	kplugin_add_syms(plugin, ksym_new_ext("COMMAND_CASE", klish_ptype_COMMAND_CASE,
+		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
 
 	context = context; // Happy compiler