[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [46279] trunk/blender: Patch [#30654] Wiki Quick Hack: Text editor move lines up/down

Joshua Leung aligorith at gmail.com
Fri May 4 16:27:13 CEST 2012


Revision: 46279
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=46279
Author:   aligorith
Date:     2012-05-04 14:27:13 +0000 (Fri, 04 May 2012)
Log Message:
-----------
Patch [#30654] Wiki Quick Hack: Text editor move lines up/down
Submitted by: Justin Dailey (dail)

Patch allows the current line (or selected lines) to be moved up and down with
Ctrl+Shift+Up and Ctrl+Shift+Down. Has undo/redo support and operators in python
menu.

Modified Paths:
--------------
    trunk/blender/release/scripts/startup/bl_ui/space_text.py
    trunk/blender/source/blender/blenkernel/BKE_text.h
    trunk/blender/source/blender/blenkernel/intern/text.c
    trunk/blender/source/blender/editors/space_text/space_text.c
    trunk/blender/source/blender/editors/space_text/text_intern.h
    trunk/blender/source/blender/editors/space_text/text_ops.c

Modified: trunk/blender/release/scripts/startup/bl_ui/space_text.py
===================================================================
--- trunk/blender/release/scripts/startup/bl_ui/space_text.py	2012-05-04 13:28:02 UTC (rev 46278)
+++ trunk/blender/release/scripts/startup/bl_ui/space_text.py	2012-05-04 14:27:13 UTC (rev 46279)
@@ -277,6 +277,11 @@
 
         layout.separator()
 
+        layout.operator("text.move_lines_up")
+        layout.operator("text.move_lines_down")
+
+        layout.separator()
+
         layout.menu("TEXT_MT_edit_select")
         layout.menu("TEXT_MT_edit_markers")
 

Modified: trunk/blender/source/blender/blenkernel/BKE_text.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_text.h	2012-05-04 13:28:02 UTC (rev 46278)
+++ trunk/blender/source/blender/blenkernel/BKE_text.h	2012-05-04 14:27:13 UTC (rev 46279)
@@ -96,6 +96,8 @@
 void 	txt_comment			(struct Text *text);
 void 	txt_indent			(struct Text *text);
 void	txt_uncomment		(struct Text *text);
+void	txt_move_lines_up	(struct Text *text);
+void	txt_move_lines_down	(struct Text *text);
 void	txt_duplicate_line	(struct Text *text);
 int	setcurr_tab_spaces	(struct Text *text, int space);
 
@@ -170,6 +172,9 @@
 #define UNDO_COMMENT    034
 #define UNDO_UNCOMMENT  035
 
+#define UNDO_MOVE_LINES_UP      036
+#define UNDO_MOVE_LINES_DOWN    037
+
 #define UNDO_DUPLICATE  040
 
 /* Marker flags */

Modified: trunk/blender/source/blender/blenkernel/intern/text.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/text.c	2012-05-04 13:28:02 UTC (rev 46278)
+++ trunk/blender/source/blender/blenkernel/intern/text.c	2012-05-04 14:27:13 UTC (rev 46279)
@@ -2129,7 +2129,7 @@
 		case UNDO_IBLOCK:
 			linep= txt_undo_read_uint32(text->undo_buf, &text->undo_pos);
 			txt_delete_sel(text);
-
+			
 			/* txt_backspace_char removes utf8-characters, not bytes */
 			buf= MEM_mallocN(linep+1, "iblock buffer");
 			for (i=0; i < linep; i++) {
@@ -2139,19 +2139,19 @@
 			buf[i]= 0;
 			linep= txt_utf8_len(buf);
 			MEM_freeN(buf);
-
+			
 			while (linep>0) {
 				txt_backspace_char(text);
 				linep--;
 			}
-
+			
 			text->undo_pos--;
 			text->undo_pos--;
 			text->undo_pos--; 
 			text->undo_pos--;
 			
 			text->undo_pos--;
-
+			
 			break;
 		case UNDO_INDENT:
 		case UNDO_UNINDENT:
@@ -2169,7 +2169,7 @@
 			for (i= 0; i < linep; i++) {
 				text->sell = text->sell->next;
 			}
-
+			
 			linep= txt_undo_read_uint32(text->undo_buf, &text->undo_pos);
 			//first line to be selected
 			
@@ -2180,8 +2180,8 @@
 			for (i = 0; i < linep; i++) {
 				text->curl = text->curl->next;
 			}
-
 			
+			
 			if (op==UNDO_INDENT) {
 				txt_unindent(text);
 			}
@@ -2194,12 +2194,18 @@
 			else if (op == UNDO_UNCOMMENT) {
 				txt_comment(text);
 			}
-
+			
 			text->undo_pos--;
 			break;
 		case UNDO_DUPLICATE:
 			txt_delete_line(text, text->curl->next);
 			break;
+		case UNDO_MOVE_LINES_UP:
+			txt_move_lines_down(text);
+			break;
+		case UNDO_MOVE_LINES_DOWN:
+			txt_move_lines_up(text);
+			break;
 		default:
 			//XXX error("Undo buffer error - resetting");
 			text->undo_pos= -1;
@@ -2400,10 +2406,16 @@
 		case UNDO_DUPLICATE:
 			txt_duplicate_line(text);
 			break;
+		case UNDO_MOVE_LINES_UP:
+			txt_move_lines_up(text);
+			break;
+		case UNDO_MOVE_LINES_DOWN:
+			txt_move_lines_down(text);
+			break;
 		default:
 			//XXX error("Undo buffer error - resetting");
 			text->undo_pos= -1;
-
+			
 			break;
 	}
 	
@@ -3033,6 +3045,53 @@
 	}
 }
 
+
+void txt_move_lines_up(struct Text *text)
+{
+	TextLine *prev_line;
+	
+	if (!text || !text->curl || !text->sell) return;
+	
+	txt_order_cursors(text);
+	
+	prev_line= text->curl->prev;
+	
+	if (!prev_line) return;
+	
+	BLI_remlink(&text->lines, prev_line);
+	BLI_insertlinkafter(&text->lines, text->sell, prev_line);
+	
+	txt_make_dirty(text);
+	txt_clean_text(text);
+	
+	if (!undoing) {
+		txt_undo_add_op(text, UNDO_MOVE_LINES_UP);
+	}
+}
+
+void txt_move_lines_down(struct Text *text)
+{
+	TextLine *next_line;
+	
+	if (!text || !text->curl || !text->sell) return;
+	
+	txt_order_cursors(text);
+	
+	next_line= text->sell->next;
+	
+	if (!next_line) return;
+		
+	BLI_remlink(&text->lines, next_line);
+	BLI_insertlinkbefore(&text->lines, text->curl, next_line);
+	
+	txt_make_dirty(text);
+	txt_clean_text(text);
+	
+	if (!undoing) {	
+		txt_undo_add_op(text, UNDO_MOVE_LINES_DOWN);
+	}
+}
+
 int setcurr_tab_spaces(Text *text, int space)
 {
 	int i = 0;

Modified: trunk/blender/source/blender/editors/space_text/space_text.c
===================================================================
--- trunk/blender/source/blender/editors/space_text/space_text.c	2012-05-04 13:28:02 UTC (rev 46278)
+++ trunk/blender/source/blender/editors/space_text/space_text.c	2012-05-04 14:27:13 UTC (rev 46279)
@@ -202,6 +202,9 @@
 	WM_operatortype_append(TEXT_OT_select_line);
 	WM_operatortype_append(TEXT_OT_select_all);
 	WM_operatortype_append(TEXT_OT_select_word);
+	
+	WM_operatortype_append(TEXT_OT_move_lines_up);
+	WM_operatortype_append(TEXT_OT_move_lines_down);
 
 	WM_operatortype_append(TEXT_OT_jump);
 	WM_operatortype_append(TEXT_OT_move);
@@ -321,7 +324,9 @@
 	WM_keymap_add_item(keymap, "TEXT_OT_select_line", AKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0);
 	WM_keymap_add_item(keymap, "TEXT_OT_select_word", LEFTMOUSE, KM_DBL_CLICK, 0, 0);
 	
-
+	WM_keymap_add_item(keymap, "TEXT_OT_move_lines_up", UPARROWKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
+	WM_keymap_add_item(keymap, "TEXT_OT_move_lines_down", DOWNARROWKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
+	
 	WM_keymap_add_item(keymap, "TEXT_OT_indent", TABKEY, KM_PRESS, 0, 0);
 	WM_keymap_add_item(keymap, "TEXT_OT_unindent", TABKEY, KM_PRESS, KM_SHIFT, 0);
 	WM_keymap_add_item(keymap, "TEXT_OT_uncomment", DKEY, KM_PRESS, KM_CTRL | KM_SHIFT, 0);

Modified: trunk/blender/source/blender/editors/space_text/text_intern.h
===================================================================
--- trunk/blender/source/blender/editors/space_text/text_intern.h	2012-05-04 13:28:02 UTC (rev 46278)
+++ trunk/blender/source/blender/editors/space_text/text_intern.h	2012-05-04 14:27:13 UTC (rev 46279)
@@ -137,6 +137,9 @@
 void TEXT_OT_select_all(struct wmOperatorType *ot);
 void TEXT_OT_select_word(struct wmOperatorType *ot);
 
+void TEXT_OT_move_lines_up(struct wmOperatorType *ot);
+void TEXT_OT_move_lines_down(struct wmOperatorType *ot);
+
 void TEXT_OT_jump(struct wmOperatorType *ot);
 void TEXT_OT_move(struct wmOperatorType *ot);
 void TEXT_OT_move_select(struct wmOperatorType *ot);

Modified: trunk/blender/source/blender/editors/space_text/text_ops.c
===================================================================
--- trunk/blender/source/blender/editors/space_text/text_ops.c	2012-05-04 13:28:02 UTC (rev 46278)
+++ trunk/blender/source/blender/editors/space_text/text_ops.c	2012-05-04 14:27:13 UTC (rev 46279)
@@ -1331,6 +1331,64 @@
 	ot->poll = text_edit_poll;
 }
 
+/********************* move lines operators ***********************/
+
+static int move_lines_up_exec(bContext *C, wmOperator *UNUSED(op))
+{
+	Text *text = CTX_data_edit_text(C);
+	
+	txt_move_lines_up(text);
+	
+	text_update_cursor_moved(C);
+	WM_event_add_notifier(C, NC_TEXT|NA_EDITED, text);
+
+	/* run the script while editing, evil but useful */
+	if (CTX_wm_space_text(C)->live_edit)
+		text_run_script(C, NULL);
+	
+	return OPERATOR_FINISHED;
+}
+
+void TEXT_OT_move_lines_up(wmOperatorType *ot)
+{
+	/* identifiers */
+	ot->name = "Move Lines Up";
+	ot->idname = "TEXT_OT_move_lines_up";
+	ot->description = "Moves the currently selected line(s) up.";
+	
+	/* api callbacks */
+	ot->exec = move_lines_up_exec;
+	ot->poll = text_edit_poll;
+}
+
+static int move_lines_down_exec(bContext *C, wmOperator *UNUSED(op))
+{
+	Text *text = CTX_data_edit_text(C);
+	
+	txt_move_lines_down(text);
+	
+	text_update_cursor_moved(C);
+	WM_event_add_notifier(C, NC_TEXT|NA_EDITED, text);
+
+	/* run the script while editing, evil but useful */
+	if (CTX_wm_space_text(C)->live_edit)
+		text_run_script(C, NULL);
+	
+	return OPERATOR_FINISHED;
+}
+
+void TEXT_OT_move_lines_down(wmOperatorType *ot)
+{
+	/* identifiers */
+	ot->name = "Move Lines Down";
+	ot->idname = "TEXT_OT_move_lines_down";
+	ot->description = "Moves the currently selected line(s) down.";
+	
+	/* api callbacks */
+	ot->exec = move_lines_down_exec;
+	ot->poll = text_edit_poll;
+}
+
 /******************* previous marker operator *********************/
 
 static int text_previous_marker_exec(bContext *C, wmOperator *UNUSED(op))




More information about the Bf-blender-cvs mailing list