瀏覽代碼

New function clish_view_insert_command

Allows adding commands from other clish components as well as from plugins.

Commands are owned by the view while they are inserted.
Ingo Albrecht 10 年之前
父節點
當前提交
433d8bd008
共有 2 個文件被更改,包括 29 次插入8 次删除
  1. 2 0
      clish/view.h
  2. 27 8
      clish/view/view.c

+ 2 - 0
clish/view.h

@@ -35,6 +35,8 @@ int clish_view_compare(const void *clientnode, const void *clientkey);
 void clish_view_delete(void *instance);
 clish_command_t *clish_view_new_command(clish_view_t * instance,
 	const char *name, const char *text);
+bool_t clish_view_insert_command(clish_view_t * instance,
+	clish_command_t *cmd);
 clish_command_t *clish_view_find_command(clish_view_t * instance,
 	const char *name, bool_t inherit);
 const clish_command_t *clish_view_find_next_completion(clish_view_t * instance,

+ 27 - 8
clish/view/view.c

@@ -98,18 +98,37 @@ clish_command_t *clish_view_new_command(clish_view_t * this,
 	clish_command_t *cmd = clish_command_new(name, help);
 	assert(cmd);
 
-	/* if this is a command other than the startup command... */
-	if (NULL != help) {
-		/* ...insert it into the binary tree for this view */
-		if (-1 == lub_bintree_insert(&this->tree, cmd)) {
-			/* inserting a duplicate command is bad */
-			clish_command_delete(cmd);
-			cmd = NULL;
-		}
+	/* do not insert startup/wdog command */
+	if(!help) {
+		return cmd;
 	}
+
+	/* try to insert the command */
+	if(!clish_view_insert_command(this, cmd)) {
+		/* ignore duplicate */
+		clish_command_delete(cmd);
+		cmd = NULL;
+	}
+
+	/* return */
 	return cmd;
 }
 
+/*--------------------------------------------------------- */
+bool_t clish_view_insert_command(clish_view_t * this,
+	clish_command_t *cmd)
+{
+	/* insert command into the binary tree for this view */
+	if (-1 == lub_bintree_insert(&this->tree, cmd)) {
+		/* duplicate, not inserted */
+		return BOOL_FALSE;
+	}
+	/* set the pview correctly */
+	clish_command__set_pview(cmd, this);
+	/* Return */
+	return BOOL_TRUE;
+}
+
 /*--------------------------------------------------------- */
 /* This method identifies the command (if any) which provides
  * the longest match with the specified line of text.