[Bf-blender-cvs] [ce009c8] master: PyConsole: double-click to select word

Campbell Barton noreply at git.blender.org
Mon Mar 16 06:04:56 CET 2015


Commit: ce009c80197e7d9b1466f8d15534c44a4b2a7608
Author: Campbell Barton
Date:   Mon Mar 16 16:01:32 2015 +1100
Branches: master
https://developer.blender.org/rBce009c80197e7d9b1466f8d15534c44a4b2a7608

PyConsole: double-click to select word

patch T43641 by @v-disp with own edits

===================================================================

M	source/blender/editors/space_console/console_intern.h
M	source/blender/editors/space_console/console_ops.c
M	source/blender/editors/space_console/space_console.c

===================================================================

diff --git a/source/blender/editors/space_console/console_intern.h b/source/blender/editors/space_console/console_intern.h
index 00f1f8c2..a3746e0 100644
--- a/source/blender/editors/space_console/console_intern.h
+++ b/source/blender/editors/space_console/console_intern.h
@@ -66,6 +66,7 @@ void CONSOLE_OT_history_cycle(struct wmOperatorType *ot);
 void CONSOLE_OT_copy(struct wmOperatorType *ot);
 void CONSOLE_OT_paste(struct wmOperatorType *ot);
 void CONSOLE_OT_select_set(struct wmOperatorType *ot);
+void CONSOLE_OT_select_word(struct wmOperatorType *ot);
 
 enum { LINE_BEGIN, LINE_END, PREV_CHAR, NEXT_CHAR, PREV_WORD, NEXT_WORD };
 enum { DEL_ALL, DEL_NEXT_CHAR, DEL_PREV_CHAR, DEL_NEXT_WORD, DEL_PREV_WORD, DEL_SELECTION, DEL_NEXT_SEL, DEL_PREV_SEL };
diff --git a/source/blender/editors/space_console/console_ops.c b/source/blender/editors/space_console/console_ops.c
index 8263268..92731c2 100644
--- a/source/blender/editors/space_console/console_ops.c
+++ b/source/blender/editors/space_console/console_ops.c
@@ -262,6 +262,40 @@ static int console_line_insert(ConsoleLine *ci, char *str)
 	return len;
 }
 
+/**
+ * Take an absolute index and give the line/column info.
+ *
+ * \note be sure to call console_scrollback_prompt_begin first
+ */
+static bool console_line_column_from_index(
+        SpaceConsole *sc, const int pos,
+        ConsoleLine **r_cl, int *r_cl_offset, int *r_col)
+{
+	ConsoleLine *cl;
+	int offset = 0;
+
+	for (cl = sc->scrollback.last; cl; cl = cl->prev) {
+		offset += cl->len + 1;
+		if (offset >= pos) {
+			break;
+		}
+	}
+
+	if (cl) {
+		offset -= 1;
+		*r_cl = cl;
+		*r_cl_offset = offset;
+		*r_col = offset - pos;
+		return true;
+	}
+	else {
+		*r_cl = NULL;
+		*r_cl_offset = -1;
+		*r_col = -1;
+		return false;
+	}
+}
+
 /* static funcs for text editing */
 
 /* similar to the text editor, with some not used. keep compatible */
@@ -1134,3 +1168,57 @@ void CONSOLE_OT_select_set(wmOperatorType *ot)
 	ot->cancel = console_modal_select_cancel;
 	ot->poll = ED_operator_console_active;
 }
+
+static int console_selectword_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
+{
+	SpaceConsole *sc = CTX_wm_space_console(C);
+	ARegion *ar = CTX_wm_region(C);
+
+	ConsoleLine cl_dummy = {NULL};
+	ConsoleLine *cl;
+	int ret = OPERATOR_CANCELLED;
+	int pos, offset, n;
+
+	pos = console_char_pick(sc, ar, event->mval);
+
+	console_scrollback_prompt_begin(sc, &cl_dummy);
+
+	if (console_line_column_from_index(sc, pos, &cl, &offset, &n)) {
+		int sel[2] = {n, n};
+
+		BLI_str_cursor_step_utf8(
+		        cl->line, cl->len,
+		        &sel[0], STRCUR_DIR_NEXT,
+		        STRCUR_JUMP_DELIM, true);
+
+		BLI_str_cursor_step_utf8(
+		        cl->line, cl->len,
+		        &sel[1], STRCUR_DIR_PREV,
+		        STRCUR_JUMP_DELIM, true);
+
+		sel[0] = offset - sel[0];
+		sel[1] = offset - sel[1];
+
+		if ((sel[0] != sc->sel_start) || (sel[1] != sc->sel_end)) {
+			sc->sel_start = sel[0];
+			sc->sel_end   = sel[1];
+			ED_area_tag_redraw(CTX_wm_area(C));
+			ret = OPERATOR_FINISHED;
+		}
+	}
+
+	console_scrollback_prompt_end(sc, &cl_dummy);
+	return ret;
+}
+
+void CONSOLE_OT_select_word(wmOperatorType *ot)
+{
+	/* identifiers */
+	ot->name = "Select Word";
+	ot->description = "Select word at cursor position";
+	ot->idname = "CONSOLE_OT_select_word";
+
+	/* api callbacks */
+	ot->invoke = console_selectword_invoke;
+	ot->poll = ED_operator_console_active;
+}
diff --git a/source/blender/editors/space_console/space_console.c b/source/blender/editors/space_console/space_console.c
index e4a61a8..a592f35 100644
--- a/source/blender/editors/space_console/space_console.c
+++ b/source/blender/editors/space_console/space_console.c
@@ -270,6 +270,7 @@ static void console_operatortypes(void)
 	WM_operatortype_append(CONSOLE_OT_copy);
 	WM_operatortype_append(CONSOLE_OT_paste);
 	WM_operatortype_append(CONSOLE_OT_select_set);
+	WM_operatortype_append(CONSOLE_OT_select_word);
 }
 
 static void console_keymap(struct wmKeyConfig *keyconf)
@@ -348,6 +349,7 @@ static void console_keymap(struct wmKeyConfig *keyconf)
 #endif
 	
 	WM_keymap_add_item(keymap, "CONSOLE_OT_select_set", LEFTMOUSE, KM_PRESS, 0, 0);
+	WM_keymap_add_item(keymap, "CONSOLE_OT_select_word", LEFTMOUSE, KM_DBL_CLICK, 0, 0);
 
 	RNA_string_set(WM_keymap_add_item(keymap, "CONSOLE_OT_insert", TABKEY, KM_PRESS, KM_CTRL, 0)->ptr, "text", "\t"); /* fake tabs */




More information about the Bf-blender-cvs mailing list