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 /* return 0 if no change made, clamps the range */
92 static int console_line_cursor_set(ConsoleLine *cl, int cursor)
96 if(cursor < 0) cursor_new= 0;
97 else if(cursor > cl->len) cursor_new= cl->len;
98 else cursor_new= cursor;
100 if(cursor_new == cl->cursor)
103 cl->cursor= cursor_new;
107 static ConsoleLine *console_lb_add__internal(ListBase *lb, ConsoleLine *from)
109 ConsoleLine *ci= MEM_callocN(sizeof(ConsoleLine), "ConsoleLine Add");
112 ci->line= BLI_strdup(from->line);
113 ci->len= strlen(ci->line);
114 ci->len_alloc= ci->len;
116 ci->cursor= from->cursor;
117 ci->type= from->type;
119 ci->line= MEM_callocN(64, "console-in-line");
128 static ConsoleLine *console_history_add(const bContext *C, ConsoleLine *from)
130 SpaceConsole *sc= CTX_wm_space_console(C);
132 return console_lb_add__internal(&sc->history, from);
135 #if 0 /* may use later ? */
136 static ConsoleLine *console_scrollback_add(const bContext *C, ConsoleLine *from)
138 SpaceConsole *sc= CTX_wm_space_console(C);
140 return console_lb_add__internal(&sc->scrollback, from);
144 static ConsoleLine *console_lb_add_str__internal(ListBase *lb, const bContext *C, char *str, int own)
146 ConsoleLine *ci= MEM_callocN(sizeof(ConsoleLine), "ConsoleLine Add");
147 if(own) ci->line= str;
148 else ci->line= BLI_strdup(str);
150 ci->len = ci->len_alloc = strlen(str);
155 ConsoleLine *console_history_add_str(const bContext *C, char *str, int own)
157 return console_lb_add_str__internal(&CTX_wm_space_console(C)->history, C, str, own);
159 ConsoleLine *console_scrollback_add_str(const bContext *C, char *str, int own)
161 return console_lb_add_str__internal(&CTX_wm_space_console(C)->scrollback, C, str, own);
164 ConsoleLine *console_history_verify(const bContext *C)
166 SpaceConsole *sc= CTX_wm_space_console(C);
167 ConsoleLine *ci= sc->history.last;
169 ci= console_history_add(C, NULL);
175 static void console_line_verify_length(ConsoleLine *ci, int len)
177 /* resize the buffer if needed */
178 if(len > ci->len_alloc) {
179 int new_len= len * 2; /* new length */
180 char *new_line= MEM_callocN(new_len, "console line");
181 memcpy(new_line, ci->line, ci->len);
185 ci->len_alloc= new_len;
189 static int console_line_insert(ConsoleLine *ci, char *str)
191 int len = strlen(str);
196 console_line_verify_length(ci, len + ci->len);
198 memmove(ci->line+ci->cursor+len, ci->line+ci->cursor, (ci->len - ci->cursor)+1);
199 memcpy(ci->line+ci->cursor, str, len);
207 static int console_edit_poll(bContext *C)
209 SpaceConsole *sc= CTX_wm_space_console(C);
211 if(!sc || sc->type != CONSOLE_TYPE_PYTHON)
218 /* static funcs for text editing */
220 /* similar to the text editor, with some not used. keep compatible */
221 static EnumPropertyItem move_type_items[]= {
222 {LINE_BEGIN, "LINE_BEGIN", 0, "Line Begin", ""},
223 {LINE_END, "LINE_END", 0, "Line End", ""},
224 {PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""},
225 {NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""},
226 {PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""},
227 {NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""},
228 {0, NULL, 0, NULL, NULL}};
230 static int move_exec(bContext *C, wmOperator *op)
232 ConsoleLine *ci= console_history_verify(C);
234 int type= RNA_enum_get(op->ptr, "type");
239 done= console_line_cursor_set(ci, 0);
242 done= console_line_cursor_set(ci, INT_MAX);
245 done= console_line_cursor_set(ci, ci->cursor-1);
248 done= console_line_cursor_set(ci, ci->cursor+1);
253 ED_area_tag_redraw(CTX_wm_area(C));
256 return OPERATOR_FINISHED;
259 void CONSOLE_OT_move(wmOperatorType *ot)
262 ot->name= "Move Cursor";
263 ot->idname= "CONSOLE_OT_move";
267 ot->poll= console_edit_poll;
270 ot->flag= OPTYPE_REGISTER;
273 RNA_def_enum(ot->srna, "type", move_type_items, LINE_BEGIN, "Type", "Where to move cursor to.");
277 static int insert_exec(bContext *C, wmOperator *op)
279 ConsoleLine *ci= console_history_verify(C);
280 char *str= RNA_string_get_alloc(op->ptr, "text", NULL, 0);
282 int len= console_line_insert(ci, str);
287 return OPERATOR_CANCELLED;
289 ED_area_tag_redraw(CTX_wm_area(C));
291 return OPERATOR_FINISHED;
294 static int insert_invoke(bContext *C, wmOperator *op, wmEvent *event)
296 if(!RNA_property_is_set(op->ptr, "text")) {
297 char str[2] = {event->ascii, '\0'};
298 RNA_string_set(op->ptr, "text", str);
300 return insert_exec(C, op);
303 void CONSOLE_OT_insert(wmOperatorType *ot)
307 ot->idname= "CONSOLE_OT_insert";
310 ot->exec= insert_exec;
311 ot->invoke= insert_invoke;
312 ot->poll= console_edit_poll;
315 ot->flag= OPTYPE_REGISTER;
318 RNA_def_string(ot->srna, "text", "", 0, "Text", "Text to insert at the cursor position.");
322 static EnumPropertyItem delete_type_items[]= {
323 {DEL_NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""},
324 {DEL_PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""},
325 // {DEL_NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""},
326 // {DEL_PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""},
327 {0, NULL, 0, NULL, NULL}};
329 static int delete_exec(bContext *C, wmOperator *op)
332 ConsoleLine *ci= console_history_verify(C);
337 int type= RNA_enum_get(op->ptr, "type");
340 return OPERATOR_CANCELLED;
345 if(ci->cursor < ci->len) {
346 memmove(ci->line + ci->cursor, ci->line + ci->cursor+1, (ci->len - ci->cursor)+1);
353 ci->cursor--; /* same as above */
354 memmove(ci->line + ci->cursor, ci->line + ci->cursor+1, (ci->len - ci->cursor)+1);
362 return OPERATOR_CANCELLED;
364 ED_area_tag_redraw(CTX_wm_area(C));
366 return OPERATOR_FINISHED;
370 void CONSOLE_OT_delete(wmOperatorType *ot)
374 ot->idname= "CONSOLE_OT_delete";
377 ot->exec= delete_exec;
378 ot->poll= console_edit_poll;
381 ot->flag= OPTYPE_REGISTER;
384 RNA_def_enum(ot->srna, "type", delete_type_items, DEL_NEXT_CHAR, "Type", "Which part of the text to delete.");
388 /* the python exec operator uses this */
389 static int clear_exec(bContext *C, wmOperator *op)
391 SpaceConsole *sc= CTX_wm_space_console(C);
393 short scrollback= RNA_boolean_get(op->ptr, "scrollback");
394 short history= RNA_boolean_get(op->ptr, "history");
396 /*ConsoleLine *ci= */ console_history_verify(C);
398 if(scrollback) { /* last item in mistory */
399 while(sc->scrollback.first)
400 console_scrollback_free(sc, sc->scrollback.first);
404 while(sc->history.first)
405 console_history_free(sc, sc->history.first);
408 ED_area_tag_redraw(CTX_wm_area(C));
410 return OPERATOR_FINISHED;
413 void CONSOLE_OT_clear(wmOperatorType *ot)
417 ot->idname= "CONSOLE_OT_clear";
420 ot->exec= clear_exec;
421 ot->poll= console_edit_poll;
424 ot->flag= OPTYPE_REGISTER;
427 RNA_def_boolean(ot->srna, "scrollback", 1, "Scrollback", "Clear the scrollback history");
428 RNA_def_boolean(ot->srna, "history", 0, "History", "Clear the command history");
433 /* the python exec operator uses this */
434 static int history_cycle_exec(bContext *C, wmOperator *op)
436 SpaceConsole *sc= CTX_wm_space_console(C);
437 ConsoleLine *ci= console_history_verify(C); /* TODO - stupid, just prevernts crashes when no command line */
439 short reverse= RNA_boolean_get(op->ptr, "reverse"); /* assumes down, reverse is up */
441 if(reverse) { /* last item in mistory */
442 ci= sc->history.last;
443 BLI_remlink(&sc->history, ci);
444 BLI_addhead(&sc->history, ci);
447 ci= sc->history.first;
448 BLI_remlink(&sc->history, ci);
449 BLI_addtail(&sc->history, ci);
452 ED_area_tag_redraw(CTX_wm_area(C));
454 return OPERATOR_FINISHED;
457 void CONSOLE_OT_history_cycle(wmOperatorType *ot)
460 ot->name= "History Cycle";
461 ot->idname= "CONSOLE_OT_history_cycle";
464 ot->exec= history_cycle_exec;
465 ot->poll= console_edit_poll;
468 ot->flag= OPTYPE_REGISTER;
471 RNA_def_boolean(ot->srna, "reverse", 0, "Reverse", "reverse cycle history");
475 /* the python exec operator uses this */
476 static int history_append_exec(bContext *C, wmOperator *op)
478 ConsoleLine *ci= console_history_verify(C);
481 char *str= RNA_string_get_alloc(op->ptr, "text", NULL, 0); /* own this text in the new line, dont free */
482 int cursor= RNA_int_get(op->ptr, "current_character");
484 ci= console_history_add_str(C, str, 1); /* own the string */
485 console_line_cursor_set(ci, cursor);
487 ED_area_tag_redraw(CTX_wm_area(C));
489 return OPERATOR_FINISHED;
492 void CONSOLE_OT_history_append(wmOperatorType *ot)
495 ot->name= "History Append";
496 ot->idname= "CONSOLE_OT_history_append";
499 ot->exec= history_append_exec;
500 ot->poll= console_edit_poll;
503 ot->flag= OPTYPE_REGISTER;
506 RNA_def_string(ot->srna, "text", "", 0, "Text", "Text to insert at the cursor position.");
507 RNA_def_int(ot->srna, "current_character", 0, 0, INT_MAX, "Cursor", "The index of the cursor.", 0, 10000);
511 /* the python exec operator uses this */
512 static int scrollback_append_exec(bContext *C, wmOperator *op)
514 SpaceConsole *sc= CTX_wm_space_console(C);
515 ConsoleLine *ci= console_history_verify(C);
517 char *str= RNA_string_get_alloc(op->ptr, "text", NULL, 0); /* own this text in the new line, dont free */
518 int type= RNA_enum_get(op->ptr, "type");
520 ci= console_scrollback_add_str(C, str, 1); /* own the string */
523 console_scrollback_limit(sc);
525 ED_area_tag_redraw(CTX_wm_area(C));
527 return OPERATOR_FINISHED;
530 void CONSOLE_OT_scrollback_append(wmOperatorType *ot)
532 /* defined in DNA_space_types.h */
533 static EnumPropertyItem console_line_type_items[] = {
534 {CONSOLE_LINE_OUTPUT, "OUTPUT", 0, "Output", ""},
535 {CONSOLE_LINE_INPUT, "INPUT", 0, "Input", ""},
536 {CONSOLE_LINE_INFO, "INFO", 0, "Information", ""},
537 {CONSOLE_LINE_ERROR, "ERROR", 0, "Error", ""},
538 {0, NULL, 0, NULL, NULL}};
541 ot->name= "Scrollback Append";
542 ot->idname= "CONSOLE_OT_scrollback_append";
545 ot->exec= scrollback_append_exec;
546 ot->poll= console_edit_poll;
549 ot->flag= OPTYPE_REGISTER;
552 RNA_def_string(ot->srna, "text", "", 0, "Text", "Text to insert at the cursor position.");
553 RNA_def_enum(ot->srna, "type", console_line_type_items, CONSOLE_LINE_OUTPUT, "Type", "Console output type.");
557 static int copy_exec(bContext *C, wmOperator *op)
559 SpaceConsole *sc= CTX_wm_space_console(C);
561 DynStr *buf_dyn= BLI_dynstr_new();
566 for(cl= sc->scrollback.first; cl; cl= cl->next) {
567 BLI_dynstr_append(buf_dyn, cl->line);
568 BLI_dynstr_append(buf_dyn, "\n");
571 buf_str= BLI_dynstr_get_cstring(buf_dyn);
572 BLI_dynstr_free(buf_dyn);
574 WM_clipboard_text_set(buf_str, 0);
577 return OPERATOR_FINISHED;
580 void CONSOLE_OT_copy(wmOperatorType *ot)
583 ot->name= "Copy to Clipboard";
584 ot->idname= "CONSOLE_OT_copy";
587 ot->poll= console_edit_poll;
591 ot->flag= OPTYPE_REGISTER;
596 static int zoom_exec(bContext *C, wmOperator *op)
598 SpaceConsole *sc= CTX_wm_space_console(C);
600 int delta= RNA_int_get(op->ptr, "delta");
602 sc->lheight += delta;
603 CLAMP(sc->lheight, 8, 32);
605 ED_area_tag_redraw(CTX_wm_area(C));
607 return OPERATOR_FINISHED;
611 void CONSOLE_OT_zoom(wmOperatorType *ot)
614 ot->name= "Console Zoom";
615 ot->idname= "CONSOLE_OT_zoom";
621 /* ot->flag= OPTYPE_REGISTER; */ /* super annoying */
624 RNA_def_int(ot->srna, "delta", 0, 0, INT_MAX, "Delta", "Scale the view font.", 0, 1000);