[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [23421] trunk/blender: PyConsole improvements

Campbell Barton ideasman42 at gmail.com
Tue Sep 22 18:23:49 CEST 2009


Revision: 23421
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=23421
Author:   campbellbarton
Date:     2009-09-22 18:23:46 +0200 (Tue, 22 Sep 2009)

Log Message:
-----------
PyConsole improvements
- Commands from the history wont get modified in-place when you cycle back and re-use them.
- Ctrl Left/Right skip words.
- Autocompletion on a variable that has no alternatives adds a '.'
  'bpy' -> 'bpy.', generally more useful since autocomp again will give the members of bpy

also moved text_check_* functions into BKE_text.h for the console to access.

Modified Paths:
--------------
    trunk/blender/release/ui/space_console.py
    trunk/blender/source/blender/blenkernel/BKE_text.h
    trunk/blender/source/blender/blenkernel/intern/text.c
    trunk/blender/source/blender/editors/space_console/console_ops.c
    trunk/blender/source/blender/editors/space_console/space_console.c
    trunk/blender/source/blender/editors/space_text/text_draw.c

Modified: trunk/blender/release/ui/space_console.py
===================================================================
--- trunk/blender/release/ui/space_console.py	2009-09-22 11:45:30 UTC (rev 23420)
+++ trunk/blender/release/ui/space_console.py	2009-09-22 16:23:46 UTC (rev 23421)
@@ -246,11 +246,17 @@
 					break
 				else:
 					autocomp_prefix_ret += char_soup.pop()
-				
-			print(autocomp_prefix_ret)
+			
 			return autocomp_prefix_ret, autocomp_members
 		elif len(autocomp_members) == 1:
-			return autocomp_members[0][len(autocomp_prefix):], []
+			if autocomp_prefix == autocomp_members[0]:
+				# the variable matched the prefix exactly
+				# add a '.' so you can quickly continue.
+				# Could try add [] or other possible extensions rather then '.' too if we had the variable.
+				return '.', []
+			else:
+				# finish off the of the word word
+				return autocomp_members[0][len(autocomp_prefix):], []
 		else:
 			return '', []
 	

Modified: trunk/blender/source/blender/blenkernel/BKE_text.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_text.h	2009-09-22 11:45:30 UTC (rev 23420)
+++ trunk/blender/source/blender/blenkernel/BKE_text.h	2009-09-22 16:23:46 UTC (rev 23421)
@@ -104,6 +104,14 @@
 struct TextMarker	*txt_prev_marker_color	(struct Text *text, struct TextMarker *marker);
 struct TextMarker	*txt_next_marker_color	(struct Text *text, struct TextMarker *marker);
 
+/* utility functions, could be moved somewhere more generic but are python/text related  */
+int text_check_bracket(char ch);
+int text_check_delim(char ch);
+int text_check_digit(char ch);
+int text_check_identifier(char ch);
+int text_check_whitespace(char ch);
+
+
 /* Undo opcodes */
 
 /* Simple main cursor movement */

Modified: trunk/blender/source/blender/blenkernel/intern/text.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/text.c	2009-09-22 11:45:30 UTC (rev 23420)
+++ trunk/blender/source/blender/blenkernel/intern/text.c	2009-09-22 16:23:46 UTC (rev 23421)
@@ -2832,3 +2832,60 @@
 	}
 	return NULL; /* Only if marker==NULL */
 }
+
+
+/*******************************/
+/* Character utility functions */
+/*******************************/
+
+int text_check_bracket(char ch)
+{
+	int a;
+	char opens[] = "([{";
+	char close[] = ")]}";
+
+	for(a=0; a<(sizeof(opens)-1); a++) {
+		if(ch==opens[a])
+			return a+1;
+		else if(ch==close[a])
+			return -(a+1);
+	}
+	return 0;
+}
+
+int text_check_delim(char ch)
+{
+	int a;
+	char delims[] = "():\"\' ~!%^&*-+=[]{};/<>|.#\t,";
+
+	for(a=0; a<(sizeof(delims)-1); a++) {
+		if(ch==delims[a])
+			return 1;
+	}
+	return 0;
+}
+
+int text_check_digit(char ch)
+{
+	if(ch < '0') return 0;
+	if(ch <= '9') return 1;
+	return 0;
+}
+
+int text_check_identifier(char ch)
+{
+	if(ch < '0') return 0;
+	if(ch <= '9') return 1;
+	if(ch < 'A') return 0;
+	if(ch <= 'Z' || ch == '_') return 1;
+	if(ch < 'a') return 0;
+	if(ch <= 'z') return 1;
+	return 0;
+}
+
+int text_check_whitespace(char ch)
+{
+	if(ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n')
+		return 1;
+	return 0;
+}

Modified: trunk/blender/source/blender/editors/space_console/console_ops.c
===================================================================
--- trunk/blender/source/blender/editors/space_console/console_ops.c	2009-09-22 11:45:30 UTC (rev 23420)
+++ trunk/blender/source/blender/editors/space_console/console_ops.c	2009-09-22 16:23:46 UTC (rev 23421)
@@ -51,6 +51,7 @@
 #include "BKE_library.h"
 #include "BKE_main.h"
 #include "BKE_report.h"
+#include "BKE_text.h" /* only for character utility funcs */
 
 #include "WM_api.h"
 #include "WM_types.h"
@@ -119,6 +120,48 @@
 	return 1;
 }
 
+static char cursor_char(ConsoleLine *cl)
+{
+	/* assume cursor is clamped */
+	return cl->line[cl->cursor];
+}
+
+static char cursor_char_prev(ConsoleLine *cl)
+{
+	/* assume cursor is clamped */
+	if(cl->cursor <= 0)
+		return '\0';
+
+	return cl->line[cl->cursor-1];
+}
+
+static char cursor_char_next(ConsoleLine *cl)
+{
+	/* assume cursor is clamped */
+	if(cl->cursor + 1 >= cl->len)
+		return '\0';
+
+	return cl->line[cl->cursor+1];
+}
+
+static void console_lb_debug__internal(ListBase *lb)
+{
+	ConsoleLine *cl;
+
+	printf("%d: ", BLI_countlist(lb));
+	for(cl= lb->first; cl; cl= cl->next)
+		printf("<%s> ", cl->line);
+	printf("\n");
+
+}
+
+static void console_history_debug(const bContext *C)
+{
+	SpaceConsole *sc= CTX_wm_space_console(C);
+
+	console_lb_debug__internal(&sc->history);
+}
+
 static ConsoleLine *console_lb_add__internal(ListBase *lb, ConsoleLine *from)
 {
 	ConsoleLine *ci= MEM_callocN(sizeof(ConsoleLine), "ConsoleLine Add");
@@ -251,7 +294,7 @@
 	{PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""},
 	{NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""},
 	{0, NULL, 0, NULL, NULL}};
-	
+
 static int move_exec(bContext *C, wmOperator *op)
 {
 	ConsoleLine *ci= console_history_verify(C);
@@ -272,6 +315,37 @@
 	case NEXT_CHAR:
 		done= console_line_cursor_set(ci, ci->cursor+1);
 		break;
+
+	/* - if the character is a delimiter then skip delimiters (including white space)
+	 * - when jump over the word */
+	case PREV_WORD:
+		while(text_check_delim(cursor_char_prev(ci)))
+			if(console_line_cursor_set(ci, ci->cursor-1)==FALSE)
+				break;
+
+		while(text_check_delim(cursor_char_prev(ci))==FALSE)
+			if(console_line_cursor_set(ci, ci->cursor-1)==FALSE)
+				break;
+
+		/* This isnt used for NEXT_WORD because when going back
+		 * its more useful to have the cursor directly after a word then whitespace */
+		while(text_check_whitespace(cursor_char_prev(ci))==TRUE)
+			if(console_line_cursor_set(ci, ci->cursor-1)==FALSE)
+				break;
+
+		done= 1; /* assume changed */
+		break;
+	case NEXT_WORD:
+		while(text_check_delim(cursor_char(ci))==TRUE)
+			if (console_line_cursor_set(ci, ci->cursor+1)==FALSE)
+				break;
+
+		while(text_check_delim(cursor_char(ci))==FALSE)
+			if (console_line_cursor_set(ci, ci->cursor+1)==FALSE)
+				break;
+
+		done= 1; /* assume changed */
+		break;
 	}
 	
 	if(done) {
@@ -466,7 +540,16 @@
 	ConsoleLine *ci= console_history_verify(C); /* TODO - stupid, just prevernts crashes when no command line */
 	
 	short reverse= RNA_boolean_get(op->ptr, "reverse"); /* assumes down, reverse is up */
-	
+
+	/* keep a copy of the line above so when history is cycled
+	 * this is the only function that needs to know about the double-up */
+	if(ci->prev) {
+		ConsoleLine *ci_prev= (ConsoleLine *)ci->prev;
+
+		if(strcmp(ci->line, ci_prev->line)==0)
+			console_history_free(sc, ci_prev);
+	}
+
 	if(reverse) { /* last item in mistory */
 		ci= sc->history.last;
 		BLI_remlink(&sc->history, ci);
@@ -477,9 +560,17 @@
 		BLI_remlink(&sc->history, ci);
 		BLI_addtail(&sc->history, ci);
 	}
-	
+
+	{	/* add a duplicate of the new arg and remove all other instances */
+		ConsoleLine *cl;
+		while((cl= console_history_find(sc, ci->line, ci)))
+			console_history_free(sc, cl);
+
+		console_history_add(C, (ConsoleLine *)sc->history.last);
+	}
+
 	ED_area_tag_redraw(CTX_wm_area(C));
-	
+
 	return OPERATOR_FINISHED;
 }
 

Modified: trunk/blender/source/blender/editors/space_console/space_console.c
===================================================================
--- trunk/blender/source/blender/editors/space_console/space_console.c	2009-09-22 11:45:30 UTC (rev 23420)
+++ trunk/blender/source/blender/editors/space_console/space_console.c	2009-09-22 16:23:46 UTC (rev 23421)
@@ -233,13 +233,13 @@
 {
 	wmKeyMap *keymap= WM_keymap_find(wm, "Console", SPACE_CONSOLE, 0);
 	
-	#ifdef __APPLE__
+#ifdef __APPLE__
 	RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
 	RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_END);
-	#endif
+#endif
 
-	RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", LINE_BEGIN);
-	RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", LINE_END);
+	RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
+	RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
 	
 	RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", HOMEKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_BEGIN);
 	RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", ENDKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_END);

Modified: trunk/blender/source/blender/editors/space_text/text_draw.c
===================================================================
--- trunk/blender/source/blender/editors/space_text/text_draw.c	2009-09-22 11:45:30 UTC (rev 23420)
+++ trunk/blender/source/blender/editors/space_text/text_draw.c	2009-09-22 16:23:46 UTC (rev 23421)
@@ -425,60 +425,6 @@
 	}
 }
 
-/*********************** utilities ************************/
-
-int text_check_bracket(char ch)
-{
-	int a;
-	char opens[] = "([{";
-	char close[] = ")]}";
-	
-	for(a=0; a<3; a++) {
-		if(ch==opens[a])
-			return a+1;
-		else if(ch==close[a])
-			return -(a+1);
-	}
-	return 0;
-}
-
-int text_check_delim(char ch) 
-{
-	int a;
-	char delims[] = "():\"\' ~!%^&*-+=[]{};/<>|.#\t,";
-	
-	for(a=0; a<28; a++) {
-		if(ch==delims[a])
-			return 1;
-	}
-	return 0;
-}
-
-int text_check_digit(char ch)
-{
-	if(ch < '0') return 0;
-	if(ch <= '9') return 1;
-	return 0;
-}
-
-int text_check_identifier(char ch)
-{
-	if(ch < '0') return 0;
-	if(ch <= '9') return 1;
-	if(ch < 'A') return 0;
-	if(ch <= 'Z' || ch == '_') return 1;
-	if(ch < 'a') return 0;
-	if(ch <= 'z') return 1;
-	return 0;
-}
-
-int text_check_whitespace(char ch)
-{
-	if(ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n')
-		return 1;
-	return 0;
-}
-
 /************************** draw text *****************************/
 
 /***********************/ /*





More information about the Bf-blender-cvs mailing list