PyConsole: double-click to select word
authorCampbell Barton <ideasman42@gmail.com>
Mon, 16 Mar 2015 05:01:32 +0000 (16:01 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Mon, 16 Mar 2015 05:03:32 +0000 (16:03 +1100)
patch T43641 by @v-disp with own edits

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

index 00f1f8c21c93f7e201819982bb2c40215b1a1814..a3746e091ce97658c329d2d317c37bdb075baadb 100644 (file)
@@ -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 };
index 8263268898f4a2fd04e31348c2579c9cbcc67613..92731c2f1357a4c200d10b540fe0ce117987391b 100644 (file)
@@ -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;
+}
index e4a61a8f06e2857dcd8f64d328f05026d842e679..a592f35f6294d7623fb9f73d8e167ebb5ef981b2 100644 (file)
@@ -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 */