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"
58 #include "ED_screen.h"
60 #include "UI_interface.h"
61 #include "UI_resources.h"
63 #include "RNA_access.h"
64 #include "RNA_define.h"
66 #include "console_intern.h"
68 void console_history_free(SpaceConsole *sc, ConsoleLine *cl)
70 BLI_remlink(&sc->history, cl);
74 void console_scrollback_free(SpaceConsole *sc, ConsoleLine *cl)
76 BLI_remlink(&sc->scrollback, cl);
81 void console_scrollback_limit(SpaceConsole *sc)
85 if (U.scrollback < 32) U.scrollback= 128; // XXX - save in user defaults
87 for(tot= BLI_countlist(&sc->scrollback); tot > U.scrollback; tot--)
88 console_scrollback_free(sc, sc->scrollback.first);
91 static ConsoleLine * console_history_find(SpaceConsole *sc, const char *str, ConsoleLine *cl_ignore)
95 for(cl= sc->history.last; cl; cl= cl->prev) {
99 if(strcmp(str, cl->line)==0)
106 /* return 0 if no change made, clamps the range */
107 static int console_line_cursor_set(ConsoleLine *cl, int cursor)
111 if(cursor < 0) cursor_new= 0;
112 else if(cursor > cl->len) cursor_new= cl->len;
113 else cursor_new= cursor;
115 if(cursor_new == cl->cursor)
118 cl->cursor= cursor_new;
122 static ConsoleLine *console_lb_add__internal(ListBase *lb, ConsoleLine *from)
124 ConsoleLine *ci= MEM_callocN(sizeof(ConsoleLine), "ConsoleLine Add");
127 ci->line= BLI_strdup(from->line);
128 ci->len= strlen(ci->line);
129 ci->len_alloc= ci->len;
131 ci->cursor= from->cursor;
132 ci->type= from->type;
134 ci->line= MEM_callocN(64, "console-in-line");
143 static ConsoleLine *console_history_add(const bContext *C, ConsoleLine *from)
145 SpaceConsole *sc= CTX_wm_space_console(C);
147 return console_lb_add__internal(&sc->history, from);
150 #if 0 /* may use later ? */
151 static ConsoleLine *console_scrollback_add(const bContext *C, ConsoleLine *from)
153 SpaceConsole *sc= CTX_wm_space_console(C);
155 return console_lb_add__internal(&sc->scrollback, from);
159 static ConsoleLine *console_lb_add_str__internal(ListBase *lb, const bContext *C, char *str, int own)
161 ConsoleLine *ci= MEM_callocN(sizeof(ConsoleLine), "ConsoleLine Add");
162 if(own) ci->line= str;
163 else ci->line= BLI_strdup(str);
165 ci->len = ci->len_alloc = strlen(str);
170 ConsoleLine *console_history_add_str(const bContext *C, char *str, int own)
172 return console_lb_add_str__internal(&CTX_wm_space_console(C)->history, C, str, own);
174 ConsoleLine *console_scrollback_add_str(const bContext *C, char *str, int own)
176 return console_lb_add_str__internal(&CTX_wm_space_console(C)->scrollback, C, str, own);
179 ConsoleLine *console_history_verify(const bContext *C)
181 SpaceConsole *sc= CTX_wm_space_console(C);
182 ConsoleLine *ci= sc->history.last;
184 ci= console_history_add(C, NULL);
190 static void console_line_verify_length(ConsoleLine *ci, int len)
192 /* resize the buffer if needed */
193 if(len > ci->len_alloc) {
194 int new_len= len * 2; /* new length */
195 char *new_line= MEM_callocN(new_len, "console line");
196 memcpy(new_line, ci->line, ci->len);
200 ci->len_alloc= new_len;
204 static int console_line_insert(ConsoleLine *ci, char *str)
206 int len = strlen(str);
208 if(len>0 && str[len-1]=='\n') {/* stop new lines being pasted at the end of lines */
216 console_line_verify_length(ci, len + ci->len);
218 memmove(ci->line+ci->cursor+len, ci->line+ci->cursor, (ci->len - ci->cursor)+1);
219 memcpy(ci->line+ci->cursor, str, len);
227 static int console_edit_poll(bContext *C)
229 SpaceConsole *sc= CTX_wm_space_console(C);
231 if(!sc || sc->type != CONSOLE_TYPE_PYTHON)
237 static int console_poll(bContext *C)
239 return (CTX_wm_space_console(C) != NULL);
243 /* static funcs for text editing */
245 /* similar to the text editor, with some not used. keep compatible */
246 static EnumPropertyItem move_type_items[]= {
247 {LINE_BEGIN, "LINE_BEGIN", 0, "Line Begin", ""},
248 {LINE_END, "LINE_END", 0, "Line End", ""},
249 {PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""},
250 {NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""},
251 {PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""},
252 {NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""},
253 {0, NULL, 0, NULL, NULL}};
255 static int move_exec(bContext *C, wmOperator *op)
257 ConsoleLine *ci= console_history_verify(C);
259 int type= RNA_enum_get(op->ptr, "type");
264 done= console_line_cursor_set(ci, 0);
267 done= console_line_cursor_set(ci, INT_MAX);
270 done= console_line_cursor_set(ci, ci->cursor-1);
273 done= console_line_cursor_set(ci, ci->cursor+1);
278 ED_area_tag_redraw(CTX_wm_area(C));
281 return OPERATOR_FINISHED;
284 void CONSOLE_OT_move(wmOperatorType *ot)
287 ot->name= "Move Cursor";
288 ot->description= "Move cursor position.";
289 ot->idname= "CONSOLE_OT_move";
293 ot->poll= console_edit_poll;
296 ot->flag= OPTYPE_REGISTER;
299 RNA_def_enum(ot->srna, "type", move_type_items, LINE_BEGIN, "Type", "Where to move cursor to.");
303 static int insert_exec(bContext *C, wmOperator *op)
305 ConsoleLine *ci= console_history_verify(C);
306 char *str= RNA_string_get_alloc(op->ptr, "text", NULL, 0);
308 int len= console_line_insert(ci, str);
313 return OPERATOR_CANCELLED;
315 ED_area_tag_redraw(CTX_wm_area(C));
317 return OPERATOR_FINISHED;
320 static int insert_invoke(bContext *C, wmOperator *op, wmEvent *event)
322 if(!RNA_property_is_set(op->ptr, "text")) {
323 char str[2] = {event->ascii, '\0'};
324 RNA_string_set(op->ptr, "text", str);
326 return insert_exec(C, op);
329 void CONSOLE_OT_insert(wmOperatorType *ot)
333 ot->description= "Insert text at cursor position.";
334 ot->idname= "CONSOLE_OT_insert";
337 ot->exec= insert_exec;
338 ot->invoke= insert_invoke;
339 ot->poll= console_edit_poll;
342 ot->flag= OPTYPE_REGISTER;
345 RNA_def_string(ot->srna, "text", "", 0, "Text", "Text to insert at the cursor position.");
349 static EnumPropertyItem delete_type_items[]= {
350 {DEL_NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""},
351 {DEL_PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""},
352 // {DEL_NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""},
353 // {DEL_PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""},
354 {0, NULL, 0, NULL, NULL}};
356 static int delete_exec(bContext *C, wmOperator *op)
359 ConsoleLine *ci= console_history_verify(C);
364 int type= RNA_enum_get(op->ptr, "type");
367 return OPERATOR_CANCELLED;
372 if(ci->cursor < ci->len) {
373 memmove(ci->line + ci->cursor, ci->line + ci->cursor+1, (ci->len - ci->cursor)+1);
380 ci->cursor--; /* same as above */
381 memmove(ci->line + ci->cursor, ci->line + ci->cursor+1, (ci->len - ci->cursor)+1);
389 return OPERATOR_CANCELLED;
391 ED_area_tag_redraw(CTX_wm_area(C));
393 return OPERATOR_FINISHED;
397 void CONSOLE_OT_delete(wmOperatorType *ot)
401 ot->description= "Delete text by cursor position.";
402 ot->idname= "CONSOLE_OT_delete";
405 ot->exec= delete_exec;
406 ot->poll= console_edit_poll;
409 ot->flag= OPTYPE_REGISTER;
412 RNA_def_enum(ot->srna, "type", delete_type_items, DEL_NEXT_CHAR, "Type", "Which part of the text to delete.");
416 /* the python exec operator uses this */
417 static int clear_exec(bContext *C, wmOperator *op)
419 SpaceConsole *sc= CTX_wm_space_console(C);
421 short scrollback= RNA_boolean_get(op->ptr, "scrollback");
422 short history= RNA_boolean_get(op->ptr, "history");
424 /*ConsoleLine *ci= */ console_history_verify(C);
426 if(scrollback) { /* last item in mistory */
427 while(sc->scrollback.first)
428 console_scrollback_free(sc, sc->scrollback.first);
432 while(sc->history.first)
433 console_history_free(sc, sc->history.first);
436 ED_area_tag_redraw(CTX_wm_area(C));
438 return OPERATOR_FINISHED;
441 void CONSOLE_OT_clear(wmOperatorType *ot)
445 ot->description= "Clear text by type.";
446 ot->idname= "CONSOLE_OT_clear";
449 ot->exec= clear_exec;
450 ot->poll= console_edit_poll;
453 ot->flag= OPTYPE_REGISTER;
456 RNA_def_boolean(ot->srna, "scrollback", 1, "Scrollback", "Clear the scrollback history");
457 RNA_def_boolean(ot->srna, "history", 0, "History", "Clear the command history");
462 /* the python exec operator uses this */
463 static int history_cycle_exec(bContext *C, wmOperator *op)
465 SpaceConsole *sc= CTX_wm_space_console(C);
466 ConsoleLine *ci= console_history_verify(C); /* TODO - stupid, just prevernts crashes when no command line */
468 short reverse= RNA_boolean_get(op->ptr, "reverse"); /* assumes down, reverse is up */
470 if(reverse) { /* last item in mistory */
471 ci= sc->history.last;
472 BLI_remlink(&sc->history, ci);
473 BLI_addhead(&sc->history, ci);
476 ci= sc->history.first;
477 BLI_remlink(&sc->history, ci);
478 BLI_addtail(&sc->history, ci);
481 ED_area_tag_redraw(CTX_wm_area(C));
483 return OPERATOR_FINISHED;
486 void CONSOLE_OT_history_cycle(wmOperatorType *ot)
489 ot->name= "History Cycle";
490 ot->description= "Cycle through history.";
491 ot->idname= "CONSOLE_OT_history_cycle";
494 ot->exec= history_cycle_exec;
495 ot->poll= console_edit_poll;
498 ot->flag= OPTYPE_REGISTER;
501 RNA_def_boolean(ot->srna, "reverse", 0, "Reverse", "reverse cycle history");
505 /* the python exec operator uses this */
506 static int history_append_exec(bContext *C, wmOperator *op)
508 ConsoleLine *ci= console_history_verify(C);
509 char *str= RNA_string_get_alloc(op->ptr, "text", NULL, 0); /* own this text in the new line, dont free */
510 int cursor= RNA_int_get(op->ptr, "current_character");
511 short rem_dupes= RNA_boolean_get(op->ptr, "remove_duplicates");
514 SpaceConsole *sc= CTX_wm_space_console(C);
517 while((cl= console_history_find(sc, ci->line, ci)))
518 console_history_free(sc, cl);
520 if(strcmp(str, ci->line)==0) {
522 return OPERATOR_FINISHED;
526 ci= console_history_add_str(C, str, 1); /* own the string */
527 console_line_cursor_set(ci, cursor);
529 ED_area_tag_redraw(CTX_wm_area(C));
531 return OPERATOR_FINISHED;
534 void CONSOLE_OT_history_append(wmOperatorType *ot)
537 ot->name= "History Append";
538 ot->description= "Append history at cursor position.";
539 ot->idname= "CONSOLE_OT_history_append";
542 ot->exec= history_append_exec;
543 ot->poll= console_edit_poll;
546 ot->flag= OPTYPE_REGISTER;
549 RNA_def_string(ot->srna, "text", "", 0, "Text", "Text to insert at the cursor position.");
550 RNA_def_int(ot->srna, "current_character", 0, 0, INT_MAX, "Cursor", "The index of the cursor.", 0, 10000);
551 RNA_def_boolean(ot->srna, "remove_duplicates", 0, "Remove Duplicates", "Remove duplicate items in the history");
555 /* the python exec operator uses this */
556 static int scrollback_append_exec(bContext *C, wmOperator *op)
558 SpaceConsole *sc= CTX_wm_space_console(C);
559 ConsoleLine *ci= console_history_verify(C);
561 char *str= RNA_string_get_alloc(op->ptr, "text", NULL, 0); /* own this text in the new line, dont free */
562 int type= RNA_enum_get(op->ptr, "type");
564 ci= console_scrollback_add_str(C, str, 1); /* own the string */
567 console_scrollback_limit(sc);
569 ED_area_tag_redraw(CTX_wm_area(C));
571 return OPERATOR_FINISHED;
574 void CONSOLE_OT_scrollback_append(wmOperatorType *ot)
576 /* defined in DNA_space_types.h */
577 static EnumPropertyItem console_line_type_items[] = {
578 {CONSOLE_LINE_OUTPUT, "OUTPUT", 0, "Output", ""},
579 {CONSOLE_LINE_INPUT, "INPUT", 0, "Input", ""},
580 {CONSOLE_LINE_INFO, "INFO", 0, "Information", ""},
581 {CONSOLE_LINE_ERROR, "ERROR", 0, "Error", ""},
582 {0, NULL, 0, NULL, NULL}};
585 ot->name= "Scrollback Append";
586 ot->description= "Append scrollback text by type.";
587 ot->idname= "CONSOLE_OT_scrollback_append";
590 ot->exec= scrollback_append_exec;
591 ot->poll= console_edit_poll;
594 ot->flag= OPTYPE_REGISTER;
597 RNA_def_string(ot->srna, "text", "", 0, "Text", "Text to insert at the cursor position.");
598 RNA_def_enum(ot->srna, "type", console_line_type_items, CONSOLE_LINE_OUTPUT, "Type", "Console output type.");
602 static int copy_exec(bContext *C, wmOperator *op)
604 SpaceConsole *sc= CTX_wm_space_console(C);
606 DynStr *buf_dyn= BLI_dynstr_new();
611 for(cl= sc->scrollback.first; cl; cl= cl->next) {
612 BLI_dynstr_append(buf_dyn, cl->line);
613 BLI_dynstr_append(buf_dyn, "\n");
616 buf_str= BLI_dynstr_get_cstring(buf_dyn);
617 BLI_dynstr_free(buf_dyn);
619 WM_clipboard_text_set(buf_str, 0);
622 return OPERATOR_FINISHED;
625 void CONSOLE_OT_copy(wmOperatorType *ot)
628 ot->name= "Copy to Clipboard";
629 ot->description= "Copy selected text to clipboard.";
630 ot->idname= "CONSOLE_OT_copy";
633 ot->poll= console_edit_poll;
637 ot->flag= OPTYPE_REGISTER;
642 static int paste_exec(bContext *C, wmOperator *op)
644 ConsoleLine *ci= console_history_verify(C);
646 char *buf_str= WM_clipboard_text_get(0);
649 return OPERATOR_CANCELLED;
651 console_line_insert(ci, buf_str); /* TODO - Multiline copy?? */
655 ED_area_tag_redraw(CTX_wm_area(C));
657 return OPERATOR_FINISHED;
660 void CONSOLE_OT_paste(wmOperatorType *ot)
663 ot->name= "Paste from Clipboard";
664 ot->description= "Paste text from clipboard.";
665 ot->idname= "CONSOLE_OT_paste";
668 ot->poll= console_edit_poll;
669 ot->exec= paste_exec;
672 ot->flag= OPTYPE_REGISTER;
677 static int zoom_exec(bContext *C, wmOperator *op)
679 SpaceConsole *sc= CTX_wm_space_console(C);
681 int delta= RNA_int_get(op->ptr, "delta");
683 sc->lheight += delta;
684 CLAMP(sc->lheight, 8, 32);
686 ED_area_tag_redraw(CTX_wm_area(C));
688 return OPERATOR_FINISHED;
692 void CONSOLE_OT_zoom(wmOperatorType *ot)
695 ot->name= "Console Zoom";
698 ot->description= "Zoom screen area.";
699 ot->idname= "CONSOLE_OT_zoom";
703 ot->poll= console_poll;
706 /* ot->flag= OPTYPE_REGISTER; */ /* super annoying */
709 RNA_def_int(ot->srna, "delta", 0, 0, INT_MAX, "Delta", "Scale the view font.", 0, 1000);