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): none yet.
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_windowmanager_types.h"
42 #include "BLI_blenlib.h"
45 #include "BKE_utildefines.h"
46 #include "BKE_context.h"
47 #include "BKE_depsgraph.h"
48 #include "BKE_global.h"
49 #include "BKE_library.h"
51 #include "BKE_report.h"
52 // #include "BKE_suggestions.h"
53 //#include "BKE_text.h"
58 #include "ED_screen.h"
59 #include "UI_interface.h"
60 #include "UI_resources.h"
62 #include "RNA_access.h"
63 #include "RNA_define.h"
65 #include "console_intern.h"
67 void console_history_free(SpaceConsole *sc, ConsoleLine *cl)
69 BLI_remlink(&sc->history, cl);
73 void console_scrollback_free(SpaceConsole *sc, ConsoleLine *cl)
75 BLI_remlink(&sc->scrollback, cl);
80 void console_scrollback_limit(SpaceConsole *sc)
83 for(tot= BLI_countlist(&sc->scrollback); tot > CONSOLE_SCROLLBACK_LIMIT; tot--)
84 console_scrollback_free(sc, sc->scrollback.first);
87 /* return 0 if no change made, clamps the range */
88 static int console_line_cursor_set(ConsoleLine *cl, int cursor)
92 if(cursor < 0) cursor_new= 0;
93 else if(cursor > cl->len) cursor_new= cl->len;
94 else cursor_new= cursor;
96 if(cursor_new == cl->cursor)
99 cl->cursor= cursor_new;
103 static ConsoleLine *console_lb_add__internal(ListBase *lb, ConsoleLine *from)
105 ConsoleLine *ci= MEM_callocN(sizeof(ConsoleLine), "ConsoleLine Add");
108 ci->line= BLI_strdup(from->line);
109 ci->len= strlen(ci->line);
110 ci->len_alloc= ci->len;
112 ci->cursor= from->cursor;
113 ci->type= from->type;
115 ci->line= MEM_callocN(64, "console-in-line");
124 static ConsoleLine *console_history_add(const bContext *C, ConsoleLine *from)
126 SpaceConsole *sc= CTX_wm_space_console(C);
128 return console_lb_add__internal(&sc->history, from);
131 static ConsoleLine *console_scrollback_add(const bContext *C, ConsoleLine *from)
133 SpaceConsole *sc= CTX_wm_space_console(C);
135 return console_lb_add__internal(&sc->scrollback, from);
138 static ConsoleLine *console_lb_add_str__internal(ListBase *lb, const bContext *C, char *str, int own)
140 ConsoleLine *ci= MEM_callocN(sizeof(ConsoleLine), "ConsoleLine Add");
141 if(own) ci->line= str;
142 else ci->line= BLI_strdup(str);
144 ci->len = ci->len_alloc = strlen(str);
149 ConsoleLine *console_history_add_str(const bContext *C, char *str, int own)
151 return console_lb_add_str__internal(&CTX_wm_space_console(C)->history, C, str, own);
153 ConsoleLine *console_scrollback_add_str(const bContext *C, char *str, int own)
155 return console_lb_add_str__internal(&CTX_wm_space_console(C)->scrollback, C, str, own);
158 ConsoleLine *console_history_verify(const bContext *C)
160 SpaceConsole *sc= CTX_wm_space_console(C);
161 ConsoleLine *ci= sc->history.last;
163 ci= console_history_add(C, NULL);
169 static void console_line_verify_length(ConsoleLine *ci, int len)
171 /* resize the buffer if needed */
172 if(len > ci->len_alloc) {
173 int new_len= len * 2; /* new length */
174 char *new_line= MEM_callocN(new_len, "console line");
175 memcpy(new_line, ci->line, ci->len);
179 ci->len_alloc= new_len;
183 static int console_line_insert(ConsoleLine *ci, char *str)
185 int len = strlen(str);
190 console_line_verify_length(ci, len + ci->len);
192 memmove(ci->line+ci->cursor+len, ci->line+ci->cursor, (ci->len - ci->cursor)+1);
193 memcpy(ci->line+ci->cursor, str, len);
201 static int console_edit_poll(const bContext *C)
203 SpaceConsole *sc= CTX_wm_space_console(C);
205 if(!sc || sc->type != CONSOLE_TYPE_PYTHON)
211 /* static funcs for text editing */
214 /* similar to the text editor, with some not used. keep compatible */
215 static EnumPropertyItem move_type_items[]= {
216 {LINE_BEGIN, "LINE_BEGIN", 0, "Line Begin", ""},
217 {LINE_END, "LINE_END", 0, "Line End", ""},
218 {PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""},
219 {NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""},
220 {PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""},
221 {NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""},
222 {0, NULL, 0, NULL, NULL}};
224 static int move_exec(const bContext *C, wmOperator *op)
226 ConsoleLine *ci= console_history_verify(C);
228 int type= RNA_enum_get(op->ptr, "type");
233 done= console_line_cursor_set(ci, 0);
236 done= console_line_cursor_set(ci, INT_MAX);
239 done= console_line_cursor_set(ci, ci->cursor-1);
242 done= console_line_cursor_set(ci, ci->cursor+1);
247 ED_area_tag_redraw(CTX_wm_area(C));
250 return OPERATOR_FINISHED;
253 void CONSOLE_OT_move(wmOperatorType *ot)
256 ot->name= "Move Cursor";
257 ot->idname= "CONSOLE_OT_move";
261 ot->poll= console_edit_poll;
264 ot->flag= OPTYPE_REGISTER;
267 RNA_def_enum(ot->srna, "type", move_type_items, LINE_BEGIN, "Type", "Where to move cursor to.");
271 static int insert_exec(const bContext *C, wmOperator *op)
273 ConsoleLine *ci= console_history_verify(C);
274 char *str= RNA_string_get_alloc(op->ptr, "text", NULL, 0);
276 int len= console_line_insert(ci, str);
281 return OPERATOR_CANCELLED;
283 ED_area_tag_redraw(CTX_wm_area(C));
285 return OPERATOR_FINISHED;
288 static int insert_invoke(const bContext *C, wmOperator *op, wmEvent *event)
290 if(!RNA_property_is_set(op->ptr, "text")) {
291 char str[2] = {event->ascii, '\0'};
292 RNA_string_set(op->ptr, "text", str);
294 return insert_exec(C, op);
297 void CONSOLE_OT_insert(wmOperatorType *ot)
301 ot->idname= "CONSOLE_OT_insert";
304 ot->exec= insert_exec;
305 ot->invoke= insert_invoke;
306 ot->poll= console_edit_poll;
309 ot->flag= OPTYPE_REGISTER;
312 RNA_def_string(ot->srna, "text", "", 0, "Text", "Text to insert at the cursor position.");
316 static EnumPropertyItem delete_type_items[]= {
317 {DEL_NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""},
318 {DEL_PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""},
319 // {DEL_NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""},
320 // {DEL_PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""},
321 {0, NULL, 0, NULL, NULL}};
323 static int delete_exec(const bContext *C, wmOperator *op)
326 ConsoleLine *ci= console_history_verify(C);
331 int type= RNA_enum_get(op->ptr, "type");
334 return OPERATOR_CANCELLED;
339 if(ci->cursor < ci->len) {
340 memmove(ci->line + ci->cursor, ci->line + ci->cursor+1, (ci->len - ci->cursor)+1);
347 ci->cursor--; /* same as above */
348 memmove(ci->line + ci->cursor, ci->line + ci->cursor+1, (ci->len - ci->cursor)+1);
356 return OPERATOR_CANCELLED;
358 ED_area_tag_redraw(CTX_wm_area(C));
360 return OPERATOR_FINISHED;
364 void CONSOLE_OT_delete(wmOperatorType *ot)
368 ot->idname= "CONSOLE_OT_delete";
371 ot->exec= delete_exec;
372 ot->poll= console_edit_poll;
375 ot->flag= OPTYPE_REGISTER;
378 RNA_def_enum(ot->srna, "type", delete_type_items, DEL_NEXT_CHAR, "Type", "Which part of the text to delete.");
382 /* the python exec operator uses this */
383 static int clear_exec(const bContext *C, wmOperator *op)
385 SpaceConsole *sc= CTX_wm_space_console(C);
387 short scrollback= RNA_boolean_get(op->ptr, "scrollback");
388 short history= RNA_boolean_get(op->ptr, "history");
390 /*ConsoleLine *ci= */ console_history_verify(C);
392 if(scrollback) { /* last item in mistory */
393 while(sc->scrollback.first)
394 console_scrollback_free(sc, sc->scrollback.first);
398 while(sc->history.first)
399 console_history_free(sc, sc->history.first);
402 ED_area_tag_redraw(CTX_wm_area(C));
404 return OPERATOR_FINISHED;
407 void CONSOLE_OT_clear(wmOperatorType *ot)
411 ot->idname= "CONSOLE_OT_clear";
414 ot->exec= clear_exec;
415 ot->poll= console_edit_poll;
418 ot->flag= OPTYPE_REGISTER;
421 RNA_def_boolean(ot->srna, "scrollback", 1, "Scrollback", "Clear the scrollback history");
422 RNA_def_boolean(ot->srna, "history", 0, "History", "Clear the command history");
427 /* the python exec operator uses this */
428 static int history_cycle_exec(const bContext *C, wmOperator *op)
430 SpaceConsole *sc= CTX_wm_space_console(C);
431 ConsoleLine *ci= console_history_verify(C); /* TODO - stupid, just prevernts crashes when no command line */
433 short reverse= RNA_boolean_get(op->ptr, "reverse"); /* assumes down, reverse is up */
435 if(reverse) { /* last item in mistory */
436 ci= sc->history.last;
437 BLI_remlink(&sc->history, ci);
438 BLI_addhead(&sc->history, ci);
441 ci= sc->history.first;
442 BLI_remlink(&sc->history, ci);
443 BLI_addtail(&sc->history, ci);
446 ED_area_tag_redraw(CTX_wm_area(C));
448 return OPERATOR_FINISHED;
451 void CONSOLE_OT_history_cycle(wmOperatorType *ot)
454 ot->name= "History Cycle";
455 ot->idname= "CONSOLE_OT_history_cycle";
458 ot->exec= history_cycle_exec;
459 ot->poll= console_edit_poll;
462 ot->flag= OPTYPE_REGISTER;
465 RNA_def_boolean(ot->srna, "reverse", 0, "Reverse", "reverse cycle history");
469 /* the python exec operator uses this */
470 static int history_append_exec(const bContext *C, wmOperator *op)
472 ConsoleLine *ci= console_history_verify(C);
475 char *str= RNA_string_get_alloc(op->ptr, "text", NULL, 0); /* own this text in the new line, dont free */
476 int cursor= RNA_int_get(op->ptr, "current_character");
478 ci= console_history_add_str(C, str, 1); /* own the string */
479 console_line_cursor_set(ci, cursor);
481 ED_area_tag_redraw(CTX_wm_area(C));
483 return OPERATOR_FINISHED;
486 void CONSOLE_OT_history_append(wmOperatorType *ot)
489 ot->name= "History Append";
490 ot->idname= "CONSOLE_OT_history_append";
493 ot->exec= history_append_exec;
494 ot->poll= console_edit_poll;
497 ot->flag= OPTYPE_REGISTER;
500 RNA_def_string(ot->srna, "text", "", 0, "Text", "Text to insert at the cursor position.");
501 RNA_def_int(ot->srna, "current_character", 0, 0, INT_MAX, "Cursor", "The index of the cursor.", 0, 10000);
505 /* the python exec operator uses this */
506 static int scrollback_append_exec(const bContext *C, wmOperator *op)
508 SpaceConsole *sc= CTX_wm_space_console(C);
509 ConsoleLine *ci= console_history_verify(C);
511 char *str= RNA_string_get_alloc(op->ptr, "text", NULL, 0); /* own this text in the new line, dont free */
512 int type= RNA_enum_get(op->ptr, "type");
514 ci= console_scrollback_add_str(C, str, 1); /* own the string */
517 console_scrollback_limit(sc);
519 ED_area_tag_redraw(CTX_wm_area(C));
521 return OPERATOR_FINISHED;
524 void CONSOLE_OT_scrollback_append(wmOperatorType *ot)
527 ot->name= "Scrollback Append";
528 ot->idname= "CONSOLE_OT_scrollback_append";
531 ot->exec= scrollback_append_exec;
532 ot->poll= console_edit_poll;
535 ot->flag= OPTYPE_REGISTER;
538 RNA_def_string(ot->srna, "text", "", 0, "Text", "Text to insert at the cursor position.");
539 RNA_def_enum(ot->srna, "type", console_line_type_items, CONSOLE_LINE_OUTPUT, "Type", "Console output type.");
542 static int zoom_exec(const bContext *C, wmOperator *op)
544 SpaceConsole *sc= CTX_wm_space_console(C);
546 int delta= RNA_int_get(op->ptr, "delta");
548 sc->lheight += delta;
549 CLAMP(sc->lheight, 8, 32);
551 ED_area_tag_redraw(CTX_wm_area(C));
553 return OPERATOR_FINISHED;
557 void CONSOLE_OT_zoom(wmOperatorType *ot)
560 ot->name= "Console Zoom";
561 ot->idname= "CONSOLE_OT_zoom";
567 /* ot->flag= OPTYPE_REGISTER; */ /* super annoying */
570 RNA_def_int(ot->srna, "delta", 0, 0, INT_MAX, "Delta", "Scale the view font.", 0, 1000);