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.
This commit is contained in:
Campbell Barton 2009-09-22 16:23:46 +00:00
parent 87f5f194bc
commit d86864027d
6 changed files with 173 additions and 65 deletions

View File

@ -246,11 +246,17 @@ def autocomp(bcon):
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 '', []

View File

@ -104,6 +104,14 @@ struct TextMarker *txt_next_marker (struct Text *text, struct TextMarker *marke
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 */

View File

@ -2832,3 +2832,60 @@ TextMarker *txt_next_marker(Text *text, TextMarker *marker) {
}
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;
}

View File

@ -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 @@ static int console_line_cursor_set(ConsoleLine *cl, int cursor)
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 @@ static EnumPropertyItem move_type_items[]= {
{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 @@ static int move_exec(bContext *C, wmOperator *op)
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 @@ static int history_cycle_exec(bContext *C, wmOperator *op)
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 @@ static int history_cycle_exec(bContext *C, wmOperator *op)
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;
}

View File

@ -233,13 +233,13 @@ void console_keymap(struct wmWindowManager *wm)
{
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);

View File

@ -425,60 +425,6 @@ static void format_draw_color(char formatchar)
}
}
/*********************** 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 *****************************/
/***********************/ /*