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.
This commit is contained in:
Joshua Leung
2012-05-04 14:27:13 +00:00
parent e62f13ac31
commit 2bc29ff426
6 changed files with 144 additions and 9 deletions

View File

@@ -277,6 +277,11 @@ class TEXT_MT_edit(Menu):
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")

View File

@@ -96,6 +96,8 @@ void txt_unindent (struct Text *text);
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 @@ int text_check_whitespace(const char ch);
#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 */

View File

@@ -2129,7 +2129,7 @@ void txt_do_undo(Text *text)
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 @@ void txt_do_undo(Text *text)
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 @@ void txt_do_undo(Text *text)
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,7 +2180,7 @@ void txt_do_undo(Text *text)
for (i = 0; i < linep; i++) {
text->curl = text->curl->next;
}
if (op==UNDO_INDENT) {
txt_unindent(text);
@@ -2194,12 +2194,18 @@ void txt_do_undo(Text *text)
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 @@ void txt_do_redo(Text *text)
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_uncomment(Text *text)
}
}
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;

View File

@@ -202,6 +202,9 @@ static void text_operatortypes(void)
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 @@ static void text_keymap(struct wmKeyConfig *keyconf)
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);

View File

@@ -137,6 +137,9 @@ void TEXT_OT_select_line(struct wmOperatorType *ot);
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);

View File

@@ -1331,6 +1331,64 @@ void TEXT_OT_select_word(wmOperatorType *ot)
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))