4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): Campbell Barton
27 * ***** END GPL LICENSE BLOCK *****
32 #include <ctype.h> /* ispunct */
35 #include "MEM_guardedalloc.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_screen_types.h"
39 #include "DNA_space_types.h"
40 #include "DNA_userdef_types.h"
41 #include "DNA_windowmanager_types.h"
43 #include "BLI_blenlib.h"
44 #include "BLI_dynstr.h"
47 #include "BKE_utildefines.h"
48 #include "BKE_context.h"
49 #include "BKE_depsgraph.h"
50 #include "BKE_global.h"
51 #include "BKE_library.h"
53 #include "BKE_report.h"
54 #include "BKE_text.h" /* only for character utility funcs */
59 #include "ED_screen.h"
61 #include "UI_interface.h"
62 #include "UI_resources.h"
64 #include "RNA_access.h"
65 #include "RNA_define.h"
67 #include "console_intern.h"
69 void console_history_free(SpaceConsole *sc, ConsoleLine *cl)
71 BLI_remlink(&sc->history, cl);
75 void console_scrollback_free(SpaceConsole *sc, ConsoleLine *cl)
77 BLI_remlink(&sc->scrollback, cl);
82 void console_scrollback_limit(SpaceConsole *sc)
86 if (U.scrollback < 32) U.scrollback= 128; // XXX - save in user defaults
88 for(tot= BLI_countlist(&sc->scrollback); tot > U.scrollback; tot--)
89 console_scrollback_free(sc, sc->scrollback.first);
92 static ConsoleLine * console_history_find(SpaceConsole *sc, const char *str, ConsoleLine *cl_ignore)
96 for(cl= sc->history.last; cl; cl= cl->prev) {
100 if(strcmp(str, cl->line)==0)
107 /* return 0 if no change made, clamps the range */
108 static int console_line_cursor_set(ConsoleLine *cl, int cursor)
112 if(cursor < 0) cursor_new= 0;
113 else if(cursor > cl->len) cursor_new= cl->len;
114 else cursor_new= cursor;
116 if(cursor_new == cl->cursor)
119 cl->cursor= cursor_new;
123 static char cursor_char(ConsoleLine *cl)
125 /* assume cursor is clamped */
126 return cl->line[cl->cursor];
129 static char cursor_char_prev(ConsoleLine *cl)
131 /* assume cursor is clamped */
135 return cl->line[cl->cursor-1];
139 static char cursor_char_next(ConsoleLine *cl)
141 /* assume cursor is clamped */
142 if(cl->cursor + 1 >= cl->len)
145 return cl->line[cl->cursor+1];
148 static void console_lb_debug__internal(ListBase *lb)
152 printf("%d: ", BLI_countlist(lb));
153 for(cl= lb->first; cl; cl= cl->next)
154 printf("<%s> ", cl->line);
159 static void console_history_debug(const bContext *C)
161 SpaceConsole *sc= CTX_wm_space_console(C);
164 console_lb_debug__internal(&sc->history);
168 static ConsoleLine *console_lb_add__internal(ListBase *lb, ConsoleLine *from)
170 ConsoleLine *ci= MEM_callocN(sizeof(ConsoleLine), "ConsoleLine Add");
173 ci->line= BLI_strdup(from->line);
174 ci->len= strlen(ci->line);
175 ci->len_alloc= ci->len;
177 ci->cursor= from->cursor;
178 ci->type= from->type;
180 ci->line= MEM_callocN(64, "console-in-line");
189 static ConsoleLine *console_history_add(const bContext *C, ConsoleLine *from)
191 SpaceConsole *sc= CTX_wm_space_console(C);
193 return console_lb_add__internal(&sc->history, from);
196 #if 0 /* may use later ? */
197 static ConsoleLine *console_scrollback_add(const bContext *C, ConsoleLine *from)
199 SpaceConsole *sc= CTX_wm_space_console(C);
201 return console_lb_add__internal(&sc->scrollback, from);
205 static ConsoleLine *console_lb_add_str__internal(ListBase *lb, const bContext *C, char *str, int own)
207 ConsoleLine *ci= MEM_callocN(sizeof(ConsoleLine), "ConsoleLine Add");
208 if(own) ci->line= str;
209 else ci->line= BLI_strdup(str);
211 ci->len = ci->len_alloc = strlen(str);
216 ConsoleLine *console_history_add_str(const bContext *C, char *str, int own)
218 return console_lb_add_str__internal(&CTX_wm_space_console(C)->history, C, str, own);
220 ConsoleLine *console_scrollback_add_str(const bContext *C, char *str, int own)
222 return console_lb_add_str__internal(&CTX_wm_space_console(C)->scrollback, C, str, own);
225 ConsoleLine *console_history_verify(const bContext *C)
227 SpaceConsole *sc= CTX_wm_space_console(C);
228 ConsoleLine *ci= sc->history.last;
230 ci= console_history_add(C, NULL);
236 static void console_line_verify_length(ConsoleLine *ci, int len)
238 /* resize the buffer if needed */
239 if(len >= ci->len_alloc) {
240 int new_len= len * 2; /* new length */
241 char *new_line= MEM_callocN(new_len, "console line");
242 memcpy(new_line, ci->line, ci->len);
246 ci->len_alloc= new_len;
250 static int console_line_insert(ConsoleLine *ci, char *str)
252 int len = strlen(str);
254 if(len>0 && str[len-1]=='\n') {/* stop new lines being pasted at the end of lines */
262 console_line_verify_length(ci, len + ci->len);
264 memmove(ci->line+ci->cursor+len, ci->line+ci->cursor, (ci->len - ci->cursor)+1);
265 memcpy(ci->line+ci->cursor, str, len);
273 static int console_edit_poll(bContext *C)
275 SpaceConsole *sc= CTX_wm_space_console(C);
277 if(!sc || sc->type != CONSOLE_TYPE_PYTHON)
283 static int console_poll(bContext *C)
285 return (CTX_wm_space_console(C) != NULL);
289 /* static funcs for text editing */
291 /* similar to the text editor, with some not used. keep compatible */
292 static EnumPropertyItem move_type_items[]= {
293 {LINE_BEGIN, "LINE_BEGIN", 0, "Line Begin", ""},
294 {LINE_END, "LINE_END", 0, "Line End", ""},
295 {PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""},
296 {NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""},
297 {PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""},
298 {NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""},
299 {0, NULL, 0, NULL, NULL}};
301 static int move_exec(bContext *C, wmOperator *op)
303 ConsoleLine *ci= console_history_verify(C);
305 int type= RNA_enum_get(op->ptr, "type");
310 done= console_line_cursor_set(ci, 0);
313 done= console_line_cursor_set(ci, INT_MAX);
316 done= console_line_cursor_set(ci, ci->cursor-1);
319 done= console_line_cursor_set(ci, ci->cursor+1);
322 /* - if the character is a delimiter then skip delimiters (including white space)
323 * - when jump over the word */
325 while(text_check_delim(cursor_char_prev(ci)))
326 if(console_line_cursor_set(ci, ci->cursor-1)==FALSE)
329 while(text_check_delim(cursor_char_prev(ci))==FALSE)
330 if(console_line_cursor_set(ci, ci->cursor-1)==FALSE)
333 /* This isnt used for NEXT_WORD because when going back
334 * its more useful to have the cursor directly after a word then whitespace */
335 while(text_check_whitespace(cursor_char_prev(ci))==TRUE)
336 if(console_line_cursor_set(ci, ci->cursor-1)==FALSE)
339 done= 1; /* assume changed */
342 while(text_check_delim(cursor_char(ci))==TRUE)
343 if (console_line_cursor_set(ci, ci->cursor+1)==FALSE)
346 while(text_check_delim(cursor_char(ci))==FALSE)
347 if (console_line_cursor_set(ci, ci->cursor+1)==FALSE)
350 done= 1; /* assume changed */
355 ED_area_tag_redraw(CTX_wm_area(C));
358 return OPERATOR_FINISHED;
361 void CONSOLE_OT_move(wmOperatorType *ot)
364 ot->name= "Move Cursor";
365 ot->description= "Move cursor position";
366 ot->idname= "CONSOLE_OT_move";
370 ot->poll= console_edit_poll;
373 RNA_def_enum(ot->srna, "type", move_type_items, LINE_BEGIN, "Type", "Where to move cursor to.");
377 static int insert_exec(bContext *C, wmOperator *op)
379 ConsoleLine *ci= console_history_verify(C);
380 char *str= RNA_string_get_alloc(op->ptr, "text", NULL, 0);
382 int len= console_line_insert(ci, str);
387 return OPERATOR_CANCELLED;
389 ED_area_tag_redraw(CTX_wm_area(C));
391 return OPERATOR_FINISHED;
394 static int insert_invoke(bContext *C, wmOperator *op, wmEvent *event)
396 if(!RNA_property_is_set(op->ptr, "text")) {
397 char str[2] = {event->ascii, '\0'};
398 RNA_string_set(op->ptr, "text", str);
400 return insert_exec(C, op);
403 void CONSOLE_OT_insert(wmOperatorType *ot)
407 ot->description= "Insert text at cursor position";
408 ot->idname= "CONSOLE_OT_insert";
411 ot->exec= insert_exec;
412 ot->invoke= insert_invoke;
413 ot->poll= console_edit_poll;
416 RNA_def_string(ot->srna, "text", "", 0, "Text", "Text to insert at the cursor position.");
420 static EnumPropertyItem delete_type_items[]= {
421 {DEL_NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""},
422 {DEL_PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""},
423 // {DEL_NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""},
424 // {DEL_PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""},
425 {0, NULL, 0, NULL, NULL}};
427 static int delete_exec(bContext *C, wmOperator *op)
430 ConsoleLine *ci= console_history_verify(C);
435 int type= RNA_enum_get(op->ptr, "type");
438 return OPERATOR_CANCELLED;
443 if(ci->cursor < ci->len) {
444 memmove(ci->line + ci->cursor, ci->line + ci->cursor+1, (ci->len - ci->cursor)+1);
451 ci->cursor--; /* same as above */
452 memmove(ci->line + ci->cursor, ci->line + ci->cursor+1, (ci->len - ci->cursor)+1);
460 return OPERATOR_CANCELLED;
462 ED_area_tag_redraw(CTX_wm_area(C));
464 return OPERATOR_FINISHED;
468 void CONSOLE_OT_delete(wmOperatorType *ot)
472 ot->description= "Delete text by cursor position";
473 ot->idname= "CONSOLE_OT_delete";
476 ot->exec= delete_exec;
477 ot->poll= console_edit_poll;
480 RNA_def_enum(ot->srna, "type", delete_type_items, DEL_NEXT_CHAR, "Type", "Which part of the text to delete.");
484 /* the python exec operator uses this */
485 static int clear_exec(bContext *C, wmOperator *op)
487 SpaceConsole *sc= CTX_wm_space_console(C);
489 short scrollback= RNA_boolean_get(op->ptr, "scrollback");
490 short history= RNA_boolean_get(op->ptr, "history");
492 /*ConsoleLine *ci= */ console_history_verify(C);
494 if(scrollback) { /* last item in mistory */
495 while(sc->scrollback.first)
496 console_scrollback_free(sc, sc->scrollback.first);
500 while(sc->history.first)
501 console_history_free(sc, sc->history.first);
504 ED_area_tag_redraw(CTX_wm_area(C));
506 return OPERATOR_FINISHED;
509 void CONSOLE_OT_clear(wmOperatorType *ot)
513 ot->description= "Clear text by type";
514 ot->idname= "CONSOLE_OT_clear";
517 ot->exec= clear_exec;
518 ot->poll= console_edit_poll;
521 RNA_def_boolean(ot->srna, "scrollback", 1, "Scrollback", "Clear the scrollback history");
522 RNA_def_boolean(ot->srna, "history", 0, "History", "Clear the command history");
527 /* the python exec operator uses this */
528 static int history_cycle_exec(bContext *C, wmOperator *op)
530 SpaceConsole *sc= CTX_wm_space_console(C);
531 ConsoleLine *ci= console_history_verify(C); /* TODO - stupid, just prevernts crashes when no command line */
533 short reverse= RNA_boolean_get(op->ptr, "reverse"); /* assumes down, reverse is up */
535 /* keep a copy of the line above so when history is cycled
536 * this is the only function that needs to know about the double-up */
538 ConsoleLine *ci_prev= (ConsoleLine *)ci->prev;
540 if(strcmp(ci->line, ci_prev->line)==0)
541 console_history_free(sc, ci_prev);
544 if(reverse) { /* last item in mistory */
545 ci= sc->history.last;
546 BLI_remlink(&sc->history, ci);
547 BLI_addhead(&sc->history, ci);
550 ci= sc->history.first;
551 BLI_remlink(&sc->history, ci);
552 BLI_addtail(&sc->history, ci);
555 { /* add a duplicate of the new arg and remove all other instances */
557 while((cl= console_history_find(sc, ci->line, ci)))
558 console_history_free(sc, cl);
560 console_history_add(C, (ConsoleLine *)sc->history.last);
563 ED_area_tag_redraw(CTX_wm_area(C));
565 return OPERATOR_FINISHED;
568 void CONSOLE_OT_history_cycle(wmOperatorType *ot)
571 ot->name= "History Cycle";
572 ot->description= "Cycle through history";
573 ot->idname= "CONSOLE_OT_history_cycle";
576 ot->exec= history_cycle_exec;
577 ot->poll= console_edit_poll;
580 RNA_def_boolean(ot->srna, "reverse", 0, "Reverse", "reverse cycle history");
584 /* the python exec operator uses this */
585 static int history_append_exec(bContext *C, wmOperator *op)
587 ConsoleLine *ci= console_history_verify(C);
588 char *str= RNA_string_get_alloc(op->ptr, "text", NULL, 0); /* own this text in the new line, dont free */
589 int cursor= RNA_int_get(op->ptr, "current_character");
590 short rem_dupes= RNA_boolean_get(op->ptr, "remove_duplicates");
593 SpaceConsole *sc= CTX_wm_space_console(C);
596 while((cl= console_history_find(sc, ci->line, ci)))
597 console_history_free(sc, cl);
599 if(strcmp(str, ci->line)==0) {
601 return OPERATOR_FINISHED;
605 ci= console_history_add_str(C, str, 1); /* own the string */
606 console_line_cursor_set(ci, cursor);
608 ED_area_tag_redraw(CTX_wm_area(C));
610 return OPERATOR_FINISHED;
613 void CONSOLE_OT_history_append(wmOperatorType *ot)
616 ot->name= "History Append";
617 ot->description= "Append history at cursor position";
618 ot->idname= "CONSOLE_OT_history_append";
621 ot->exec= history_append_exec;
622 ot->poll= console_edit_poll;
625 RNA_def_string(ot->srna, "text", "", 0, "Text", "Text to insert at the cursor position.");
626 RNA_def_int(ot->srna, "current_character", 0, 0, INT_MAX, "Cursor", "The index of the cursor.", 0, 10000);
627 RNA_def_boolean(ot->srna, "remove_duplicates", 0, "Remove Duplicates", "Remove duplicate items in the history");
631 /* the python exec operator uses this */
632 static int scrollback_append_exec(bContext *C, wmOperator *op)
634 SpaceConsole *sc= CTX_wm_space_console(C);
635 ConsoleLine *ci= console_history_verify(C);
637 char *str= RNA_string_get_alloc(op->ptr, "text", NULL, 0); /* own this text in the new line, dont free */
638 int type= RNA_enum_get(op->ptr, "type");
640 ci= console_scrollback_add_str(C, str, 1); /* own the string */
643 console_scrollback_limit(sc);
645 ED_area_tag_redraw(CTX_wm_area(C));
647 return OPERATOR_FINISHED;
650 void CONSOLE_OT_scrollback_append(wmOperatorType *ot)
652 /* defined in DNA_space_types.h */
653 static EnumPropertyItem console_line_type_items[] = {
654 {CONSOLE_LINE_OUTPUT, "OUTPUT", 0, "Output", ""},
655 {CONSOLE_LINE_INPUT, "INPUT", 0, "Input", ""},
656 {CONSOLE_LINE_INFO, "INFO", 0, "Information", ""},
657 {CONSOLE_LINE_ERROR, "ERROR", 0, "Error", ""},
658 {0, NULL, 0, NULL, NULL}};
661 ot->name= "Scrollback Append";
662 ot->description= "Append scrollback text by type";
663 ot->idname= "CONSOLE_OT_scrollback_append";
666 ot->exec= scrollback_append_exec;
667 ot->poll= console_edit_poll;
670 RNA_def_string(ot->srna, "text", "", 0, "Text", "Text to insert at the cursor position.");
671 RNA_def_enum(ot->srna, "type", console_line_type_items, CONSOLE_LINE_OUTPUT, "Type", "Console output type.");
675 static int copy_exec(bContext *C, wmOperator *op)
677 SpaceConsole *sc= CTX_wm_space_console(C);
680 DynStr *buf_dyn= BLI_dynstr_new();
685 for(cl= sc->scrollback.first; cl; cl= cl->next) {
686 BLI_dynstr_append(buf_dyn, cl->line);
687 BLI_dynstr_append(buf_dyn, "\n");
690 buf_str= BLI_dynstr_get_cstring(buf_dyn);
691 buf_len= BLI_dynstr_get_len(buf_dyn);
692 BLI_dynstr_free(buf_dyn);
694 /* hack for selection */
696 if(sc->sel_start != sc->sel_end) {
697 buf_str[buf_len - sc->sel_start]= '\0';
698 WM_clipboard_text_set(buf_str+(buf_len - sc->sel_end), 0);
701 WM_clipboard_text_set(buf_str, 0);
704 return OPERATOR_FINISHED;
707 void CONSOLE_OT_copy(wmOperatorType *ot)
710 ot->name= "Copy to Clipboard";
711 ot->description= "Copy selected text to clipboard";
712 ot->idname= "CONSOLE_OT_copy";
715 ot->poll= console_edit_poll;
721 static int paste_exec(bContext *C, wmOperator *op)
723 ConsoleLine *ci= console_history_verify(C);
725 char *buf_str= WM_clipboard_text_get(0);
728 return OPERATOR_CANCELLED;
730 console_line_insert(ci, buf_str); /* TODO - Multiline copy?? */
734 ED_area_tag_redraw(CTX_wm_area(C));
736 return OPERATOR_FINISHED;
739 void CONSOLE_OT_paste(wmOperatorType *ot)
742 ot->name= "Paste from Clipboard";
743 ot->description= "Paste text from clipboard";
744 ot->idname= "CONSOLE_OT_paste";
747 ot->poll= console_edit_poll;
748 ot->exec= paste_exec;
753 typedef struct SetConsoleCursor {
758 static void set_cursor_to_pos(SpaceConsole *sc, ARegion *ar, SetConsoleCursor *scu, int mval[2], int sel)
761 pos= console_char_pick(sc, ar, NULL, mval);
763 if(scu->sel_init == INT_MAX) {
765 sc->sel_start = sc->sel_end = pos;
769 if (pos < scu->sel_init) {
771 sc->sel_end = scu->sel_init;
773 else if (pos > sc->sel_start) {
774 sc->sel_start = scu->sel_init;
778 sc->sel_start = sc->sel_end = pos;
782 static void console_modal_select_apply(bContext *C, wmOperator *op, wmEvent *event)
784 SpaceConsole *sc= CTX_wm_space_console(C);
785 ARegion *ar= CTX_wm_region(C);
786 SetConsoleCursor *scu= op->customdata;
787 int mval[2] = {event->mval[0], event->mval[1]};
789 set_cursor_to_pos(sc, ar, scu, mval, TRUE);
790 ED_area_tag_redraw(CTX_wm_area(C));
793 static void set_cursor_exit(bContext *C, wmOperator *op)
795 // SpaceConsole *sc= CTX_wm_space_console(C);
796 SetConsoleCursor *scu= op->customdata;
799 if(txt_has_sel(text)) {
800 buffer = txt_sel_to_buf(text);
801 WM_clipboard_text_set(buffer, 1);
808 static int console_modal_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
810 SpaceConsole *sc= CTX_wm_space_console(C);
811 // ARegion *ar= CTX_wm_region(C);
812 SetConsoleCursor *scu;
814 op->customdata= MEM_callocN(sizeof(SetConsoleCursor), "SetConsoleCursor");
817 scu->sel_old[0]= sc->sel_start;
818 scu->sel_old[1]= sc->sel_end;
820 scu->sel_init = INT_MAX;
822 WM_event_add_modal_handler(C, op);
824 console_modal_select_apply(C, op, event);
826 return OPERATOR_RUNNING_MODAL;
829 static int console_modal_select(bContext *C, wmOperator *op, wmEvent *event)
831 switch(event->type) {
835 set_cursor_exit(C, op);
836 return OPERATOR_FINISHED;
838 console_modal_select_apply(C, op, event);
842 return OPERATOR_RUNNING_MODAL;
845 static int console_modal_select_cancel(bContext *C, wmOperator *op)
847 set_cursor_exit(C, op);
848 return OPERATOR_FINISHED;
851 void CONSOLE_OT_select_set(wmOperatorType *ot)
854 ot->name= "Set Selection";
855 ot->idname= "CONSOLE_OT_select_set";
856 ot->description= "Set the console selection";
859 ot->invoke= console_modal_select_invoke;
860 ot->modal= console_modal_select;
861 ot->cancel= console_modal_select_cancel;
862 ot->poll= console_edit_poll;