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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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];
138 static char cursor_char_next(ConsoleLine *cl)
140 /* assume cursor is clamped */
141 if(cl->cursor + 1 >= cl->len)
144 return cl->line[cl->cursor+1];
147 static void console_lb_debug__internal(ListBase *lb)
151 printf("%d: ", BLI_countlist(lb));
152 for(cl= lb->first; cl; cl= cl->next)
153 printf("<%s> ", cl->line);
158 static void console_history_debug(const bContext *C)
160 SpaceConsole *sc= CTX_wm_space_console(C);
162 console_lb_debug__internal(&sc->history);
165 static ConsoleLine *console_lb_add__internal(ListBase *lb, ConsoleLine *from)
167 ConsoleLine *ci= MEM_callocN(sizeof(ConsoleLine), "ConsoleLine Add");
170 ci->line= BLI_strdup(from->line);
171 ci->len= strlen(ci->line);
172 ci->len_alloc= ci->len;
174 ci->cursor= from->cursor;
175 ci->type= from->type;
177 ci->line= MEM_callocN(64, "console-in-line");
186 static ConsoleLine *console_history_add(const bContext *C, ConsoleLine *from)
188 SpaceConsole *sc= CTX_wm_space_console(C);
190 return console_lb_add__internal(&sc->history, from);
193 #if 0 /* may use later ? */
194 static ConsoleLine *console_scrollback_add(const bContext *C, ConsoleLine *from)
196 SpaceConsole *sc= CTX_wm_space_console(C);
198 return console_lb_add__internal(&sc->scrollback, from);
202 static ConsoleLine *console_lb_add_str__internal(ListBase *lb, const bContext *C, char *str, int own)
204 ConsoleLine *ci= MEM_callocN(sizeof(ConsoleLine), "ConsoleLine Add");
205 if(own) ci->line= str;
206 else ci->line= BLI_strdup(str);
208 ci->len = ci->len_alloc = strlen(str);
213 ConsoleLine *console_history_add_str(const bContext *C, char *str, int own)
215 return console_lb_add_str__internal(&CTX_wm_space_console(C)->history, C, str, own);
217 ConsoleLine *console_scrollback_add_str(const bContext *C, char *str, int own)
219 return console_lb_add_str__internal(&CTX_wm_space_console(C)->scrollback, C, str, own);
222 ConsoleLine *console_history_verify(const bContext *C)
224 SpaceConsole *sc= CTX_wm_space_console(C);
225 ConsoleLine *ci= sc->history.last;
227 ci= console_history_add(C, NULL);
233 static void console_line_verify_length(ConsoleLine *ci, int len)
235 /* resize the buffer if needed */
236 if(len >= ci->len_alloc) {
237 int new_len= len * 2; /* new length */
238 char *new_line= MEM_callocN(new_len, "console line");
239 memcpy(new_line, ci->line, ci->len);
243 ci->len_alloc= new_len;
247 static int console_line_insert(ConsoleLine *ci, char *str)
249 int len = strlen(str);
251 if(len>0 && str[len-1]=='\n') {/* stop new lines being pasted at the end of lines */
259 console_line_verify_length(ci, len + ci->len);
261 memmove(ci->line+ci->cursor+len, ci->line+ci->cursor, (ci->len - ci->cursor)+1);
262 memcpy(ci->line+ci->cursor, str, len);
270 static int console_edit_poll(bContext *C)
272 SpaceConsole *sc= CTX_wm_space_console(C);
274 if(!sc || sc->type != CONSOLE_TYPE_PYTHON)
280 static int console_poll(bContext *C)
282 return (CTX_wm_space_console(C) != NULL);
286 /* static funcs for text editing */
288 /* similar to the text editor, with some not used. keep compatible */
289 static EnumPropertyItem move_type_items[]= {
290 {LINE_BEGIN, "LINE_BEGIN", 0, "Line Begin", ""},
291 {LINE_END, "LINE_END", 0, "Line End", ""},
292 {PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""},
293 {NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""},
294 {PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""},
295 {NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""},
296 {0, NULL, 0, NULL, NULL}};
298 static int move_exec(bContext *C, wmOperator *op)
300 ConsoleLine *ci= console_history_verify(C);
302 int type= RNA_enum_get(op->ptr, "type");
307 done= console_line_cursor_set(ci, 0);
310 done= console_line_cursor_set(ci, INT_MAX);
313 done= console_line_cursor_set(ci, ci->cursor-1);
316 done= console_line_cursor_set(ci, ci->cursor+1);
319 /* - if the character is a delimiter then skip delimiters (including white space)
320 * - when jump over the word */
322 while(text_check_delim(cursor_char_prev(ci)))
323 if(console_line_cursor_set(ci, ci->cursor-1)==FALSE)
326 while(text_check_delim(cursor_char_prev(ci))==FALSE)
327 if(console_line_cursor_set(ci, ci->cursor-1)==FALSE)
330 /* This isnt used for NEXT_WORD because when going back
331 * its more useful to have the cursor directly after a word then whitespace */
332 while(text_check_whitespace(cursor_char_prev(ci))==TRUE)
333 if(console_line_cursor_set(ci, ci->cursor-1)==FALSE)
336 done= 1; /* assume changed */
339 while(text_check_delim(cursor_char(ci))==TRUE)
340 if (console_line_cursor_set(ci, ci->cursor+1)==FALSE)
343 while(text_check_delim(cursor_char(ci))==FALSE)
344 if (console_line_cursor_set(ci, ci->cursor+1)==FALSE)
347 done= 1; /* assume changed */
352 ED_area_tag_redraw(CTX_wm_area(C));
355 return OPERATOR_FINISHED;
358 void CONSOLE_OT_move(wmOperatorType *ot)
361 ot->name= "Move Cursor";
362 ot->description= "Move cursor position.";
363 ot->idname= "CONSOLE_OT_move";
367 ot->poll= console_edit_poll;
370 ot->flag= OPTYPE_REGISTER;
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 ot->flag= OPTYPE_REGISTER;
419 RNA_def_string(ot->srna, "text", "", 0, "Text", "Text to insert at the cursor position.");
423 static EnumPropertyItem delete_type_items[]= {
424 {DEL_NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""},
425 {DEL_PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""},
426 // {DEL_NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""},
427 // {DEL_PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""},
428 {0, NULL, 0, NULL, NULL}};
430 static int delete_exec(bContext *C, wmOperator *op)
433 ConsoleLine *ci= console_history_verify(C);
438 int type= RNA_enum_get(op->ptr, "type");
441 return OPERATOR_CANCELLED;
446 if(ci->cursor < ci->len) {
447 memmove(ci->line + ci->cursor, ci->line + ci->cursor+1, (ci->len - ci->cursor)+1);
454 ci->cursor--; /* same as above */
455 memmove(ci->line + ci->cursor, ci->line + ci->cursor+1, (ci->len - ci->cursor)+1);
463 return OPERATOR_CANCELLED;
465 ED_area_tag_redraw(CTX_wm_area(C));
467 return OPERATOR_FINISHED;
471 void CONSOLE_OT_delete(wmOperatorType *ot)
475 ot->description= "Delete text by cursor position.";
476 ot->idname= "CONSOLE_OT_delete";
479 ot->exec= delete_exec;
480 ot->poll= console_edit_poll;
483 ot->flag= OPTYPE_REGISTER;
486 RNA_def_enum(ot->srna, "type", delete_type_items, DEL_NEXT_CHAR, "Type", "Which part of the text to delete.");
490 /* the python exec operator uses this */
491 static int clear_exec(bContext *C, wmOperator *op)
493 SpaceConsole *sc= CTX_wm_space_console(C);
495 short scrollback= RNA_boolean_get(op->ptr, "scrollback");
496 short history= RNA_boolean_get(op->ptr, "history");
498 /*ConsoleLine *ci= */ console_history_verify(C);
500 if(scrollback) { /* last item in mistory */
501 while(sc->scrollback.first)
502 console_scrollback_free(sc, sc->scrollback.first);
506 while(sc->history.first)
507 console_history_free(sc, sc->history.first);
510 ED_area_tag_redraw(CTX_wm_area(C));
512 return OPERATOR_FINISHED;
515 void CONSOLE_OT_clear(wmOperatorType *ot)
519 ot->description= "Clear text by type.";
520 ot->idname= "CONSOLE_OT_clear";
523 ot->exec= clear_exec;
524 ot->poll= console_edit_poll;
527 ot->flag= OPTYPE_REGISTER;
530 RNA_def_boolean(ot->srna, "scrollback", 1, "Scrollback", "Clear the scrollback history");
531 RNA_def_boolean(ot->srna, "history", 0, "History", "Clear the command history");
536 /* the python exec operator uses this */
537 static int history_cycle_exec(bContext *C, wmOperator *op)
539 SpaceConsole *sc= CTX_wm_space_console(C);
540 ConsoleLine *ci= console_history_verify(C); /* TODO - stupid, just prevernts crashes when no command line */
542 short reverse= RNA_boolean_get(op->ptr, "reverse"); /* assumes down, reverse is up */
544 /* keep a copy of the line above so when history is cycled
545 * this is the only function that needs to know about the double-up */
547 ConsoleLine *ci_prev= (ConsoleLine *)ci->prev;
549 if(strcmp(ci->line, ci_prev->line)==0)
550 console_history_free(sc, ci_prev);
553 if(reverse) { /* last item in mistory */
554 ci= sc->history.last;
555 BLI_remlink(&sc->history, ci);
556 BLI_addhead(&sc->history, ci);
559 ci= sc->history.first;
560 BLI_remlink(&sc->history, ci);
561 BLI_addtail(&sc->history, ci);
564 { /* add a duplicate of the new arg and remove all other instances */
566 while((cl= console_history_find(sc, ci->line, ci)))
567 console_history_free(sc, cl);
569 console_history_add(C, (ConsoleLine *)sc->history.last);
572 ED_area_tag_redraw(CTX_wm_area(C));
574 return OPERATOR_FINISHED;
577 void CONSOLE_OT_history_cycle(wmOperatorType *ot)
580 ot->name= "History Cycle";
581 ot->description= "Cycle through history.";
582 ot->idname= "CONSOLE_OT_history_cycle";
585 ot->exec= history_cycle_exec;
586 ot->poll= console_edit_poll;
589 ot->flag= OPTYPE_REGISTER;
592 RNA_def_boolean(ot->srna, "reverse", 0, "Reverse", "reverse cycle history");
596 /* the python exec operator uses this */
597 static int history_append_exec(bContext *C, wmOperator *op)
599 ConsoleLine *ci= console_history_verify(C);
600 char *str= RNA_string_get_alloc(op->ptr, "text", NULL, 0); /* own this text in the new line, dont free */
601 int cursor= RNA_int_get(op->ptr, "current_character");
602 short rem_dupes= RNA_boolean_get(op->ptr, "remove_duplicates");
605 SpaceConsole *sc= CTX_wm_space_console(C);
608 while((cl= console_history_find(sc, ci->line, ci)))
609 console_history_free(sc, cl);
611 if(strcmp(str, ci->line)==0) {
613 return OPERATOR_FINISHED;
617 ci= console_history_add_str(C, str, 1); /* own the string */
618 console_line_cursor_set(ci, cursor);
620 ED_area_tag_redraw(CTX_wm_area(C));
622 return OPERATOR_FINISHED;
625 void CONSOLE_OT_history_append(wmOperatorType *ot)
628 ot->name= "History Append";
629 ot->description= "Append history at cursor position.";
630 ot->idname= "CONSOLE_OT_history_append";
633 ot->exec= history_append_exec;
634 ot->poll= console_edit_poll;
637 ot->flag= OPTYPE_REGISTER;
640 RNA_def_string(ot->srna, "text", "", 0, "Text", "Text to insert at the cursor position.");
641 RNA_def_int(ot->srna, "current_character", 0, 0, INT_MAX, "Cursor", "The index of the cursor.", 0, 10000);
642 RNA_def_boolean(ot->srna, "remove_duplicates", 0, "Remove Duplicates", "Remove duplicate items in the history");
646 /* the python exec operator uses this */
647 static int scrollback_append_exec(bContext *C, wmOperator *op)
649 SpaceConsole *sc= CTX_wm_space_console(C);
650 ConsoleLine *ci= console_history_verify(C);
652 char *str= RNA_string_get_alloc(op->ptr, "text", NULL, 0); /* own this text in the new line, dont free */
653 int type= RNA_enum_get(op->ptr, "type");
655 ci= console_scrollback_add_str(C, str, 1); /* own the string */
658 console_scrollback_limit(sc);
660 ED_area_tag_redraw(CTX_wm_area(C));
662 return OPERATOR_FINISHED;
665 void CONSOLE_OT_scrollback_append(wmOperatorType *ot)
667 /* defined in DNA_space_types.h */
668 static EnumPropertyItem console_line_type_items[] = {
669 {CONSOLE_LINE_OUTPUT, "OUTPUT", 0, "Output", ""},
670 {CONSOLE_LINE_INPUT, "INPUT", 0, "Input", ""},
671 {CONSOLE_LINE_INFO, "INFO", 0, "Information", ""},
672 {CONSOLE_LINE_ERROR, "ERROR", 0, "Error", ""},
673 {0, NULL, 0, NULL, NULL}};
676 ot->name= "Scrollback Append";
677 ot->description= "Append scrollback text by type.";
678 ot->idname= "CONSOLE_OT_scrollback_append";
681 ot->exec= scrollback_append_exec;
682 ot->poll= console_edit_poll;
685 ot->flag= OPTYPE_REGISTER;
688 RNA_def_string(ot->srna, "text", "", 0, "Text", "Text to insert at the cursor position.");
689 RNA_def_enum(ot->srna, "type", console_line_type_items, CONSOLE_LINE_OUTPUT, "Type", "Console output type.");
693 static int copy_exec(bContext *C, wmOperator *op)
695 SpaceConsole *sc= CTX_wm_space_console(C);
697 DynStr *buf_dyn= BLI_dynstr_new();
702 for(cl= sc->scrollback.first; cl; cl= cl->next) {
703 BLI_dynstr_append(buf_dyn, cl->line);
704 BLI_dynstr_append(buf_dyn, "\n");
707 buf_str= BLI_dynstr_get_cstring(buf_dyn);
708 BLI_dynstr_free(buf_dyn);
710 WM_clipboard_text_set(buf_str, 0);
713 return OPERATOR_FINISHED;
716 void CONSOLE_OT_copy(wmOperatorType *ot)
719 ot->name= "Copy to Clipboard";
720 ot->description= "Copy selected text to clipboard.";
721 ot->idname= "CONSOLE_OT_copy";
724 ot->poll= console_edit_poll;
728 ot->flag= OPTYPE_REGISTER;
733 static int paste_exec(bContext *C, wmOperator *op)
735 ConsoleLine *ci= console_history_verify(C);
737 char *buf_str= WM_clipboard_text_get(0);
740 return OPERATOR_CANCELLED;
742 console_line_insert(ci, buf_str); /* TODO - Multiline copy?? */
746 ED_area_tag_redraw(CTX_wm_area(C));
748 return OPERATOR_FINISHED;
751 void CONSOLE_OT_paste(wmOperatorType *ot)
754 ot->name= "Paste from Clipboard";
755 ot->description= "Paste text from clipboard.";
756 ot->idname= "CONSOLE_OT_paste";
759 ot->poll= console_edit_poll;
760 ot->exec= paste_exec;
763 ot->flag= OPTYPE_REGISTER;
768 static int zoom_exec(bContext *C, wmOperator *op)
770 SpaceConsole *sc= CTX_wm_space_console(C);
772 int delta= RNA_int_get(op->ptr, "delta");
774 sc->lheight += delta;
775 CLAMP(sc->lheight, 8, 32);
777 ED_area_tag_redraw(CTX_wm_area(C));
779 return OPERATOR_FINISHED;
783 void CONSOLE_OT_zoom(wmOperatorType *ot)
786 ot->name= "Console Zoom";
789 ot->description= "Zoom screen area.";
790 ot->idname= "CONSOLE_OT_zoom";
794 ot->poll= console_poll;
797 /* ot->flag= OPTYPE_REGISTER; */ /* super annoying */
800 RNA_def_int(ot->srna, "delta", 0, 0, INT_MAX, "Delta", "Scale the view font.", 0, 1000);