patch [#30821] Wiki Quick Hack: Text editor duplicate line

from Justin Dailey (dail)

made this Ctrl+D, to replace Delete.
This commit is contained in:
Campbell Barton 2012-04-29 18:53:43 +00:00
parent daae72e17b
commit 44d81faa43
6 changed files with 61 additions and 1 deletions

@ -273,6 +273,7 @@ class TEXT_MT_edit(Menu):
layout.operator("text.cut")
layout.operator("text.copy")
layout.operator("text.paste")
layout.operator("text.duplicate_line")
layout.separator()

@ -96,6 +96,7 @@ 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_duplicate_line (struct Text *text);
int setcurr_tab_spaces (struct Text *text, int space);
void txt_add_marker (struct Text *text, struct TextLine *line, int start, int end, const unsigned char color[4], int group, int flags);
@ -169,6 +170,8 @@ int text_check_whitespace(const char ch);
#define UNDO_COMMENT 034
#define UNDO_UNCOMMENT 035
#define UNDO_DUPLICATE 040
/* Marker flags */
#define TMARK_TEMP 0x01 /* Remove on non-editing events, don't save */
#define TMARK_EDITALL 0x02 /* Edit all markers of the same group as one */

@ -2207,6 +2207,9 @@ void txt_do_undo(Text *text)
text->undo_pos--;
break;
case UNDO_DUPLICATE:
txt_delete_line(text, text->curl->next);
break;
default:
//XXX error("Undo buffer error - resetting");
text->undo_pos= -1;
@ -2404,6 +2407,9 @@ void txt_do_redo(Text *text)
txt_uncomment(text);
}
break;
case UNDO_DUPLICATE:
txt_duplicate_line(text);
break;
default:
//XXX error("Undo buffer error - resetting");
text->undo_pos= -1;
@ -2545,6 +2551,23 @@ static void txt_combine_lines (Text *text, TextLine *linea, TextLine *lineb)
txt_clean_text(text);
}
void txt_duplicate_line(Text *text)
{
TextLine *textline;
if (!text || !text->curl) return;
if (text->curl == text->sell) {
textline = txt_new_line(text->curl->line);
BLI_insertlinkafter(&text->lines, text->curl, textline);
txt_make_dirty(text);
txt_clean_text(text);
if (!undoing) txt_undo_add_op(text, UNDO_DUPLICATE);
}
}
void txt_delete_char(Text *text)
{
unsigned int c='\n';

@ -187,6 +187,7 @@ static void text_operatortypes(void)
WM_operatortype_append(TEXT_OT_paste);
WM_operatortype_append(TEXT_OT_copy);
WM_operatortype_append(TEXT_OT_cut);
WM_operatortype_append(TEXT_OT_duplicate_line);
WM_operatortype_append(TEXT_OT_convert_whitespace);
WM_operatortype_append(TEXT_OT_uncomment);
@ -297,6 +298,8 @@ static void text_keymap(struct wmKeyConfig *keyconf)
WM_keymap_add_item(keymap, "TEXT_OT_cut", DELKEY, KM_PRESS, KM_SHIFT, 0);
WM_keymap_add_item(keymap, "TEXT_OT_copy", INSERTKEY, KM_PRESS, KM_CTRL, 0);
WM_keymap_add_item(keymap, "TEXT_OT_paste", INSERTKEY, KM_PRESS, KM_SHIFT, 0);
WM_keymap_add_item(keymap, "TEXT_OT_duplicate_line", DKEY, KM_PRESS, KM_CTRL, 0);
if (U.uiflag & USER_MMB_PASTE) { // XXX not dynamic
kmi = WM_keymap_add_item(keymap, "TEXT_OT_paste", MIDDLEMOUSE, KM_PRESS, 0, 0);
@ -353,7 +356,6 @@ static void text_keymap(struct wmKeyConfig *keyconf)
RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", ENDKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0)->ptr, "type", FILE_BOTTOM);
RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", DELKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_NEXT_CHAR);
RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", DKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_CHAR);
RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", BACKSPACEKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_PREV_CHAR);
RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", BACKSPACEKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", DEL_PREV_CHAR); /* same as above [#26623] */
RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", DELKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_WORD);

@ -118,6 +118,7 @@ void TEXT_OT_refresh_pyconstraints(struct wmOperatorType *ot);
void TEXT_OT_paste(struct wmOperatorType *ot);
void TEXT_OT_copy(struct wmOperatorType *ot);
void TEXT_OT_cut(struct wmOperatorType *ot);
void TEXT_OT_duplicate_line(struct wmOperatorType *ot);
void TEXT_OT_convert_whitespace(struct wmOperatorType *ot);
void TEXT_OT_uncomment(struct wmOperatorType *ot);

@ -826,6 +826,36 @@ void TEXT_OT_paste(wmOperatorType *ot)
RNA_def_boolean(ot->srna, "selection", 0, "Selection", "Paste text selected elsewhere rather than copied (X11 only)");
}
/**************** duplicate operator *******************/
static int text_duplicate_line_exec(bContext *C, wmOperator *UNUSED(op))
{
Text *text= CTX_data_edit_text(C);
txt_duplicate_line(text);
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_duplicate_line(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Duplicate Line";
ot->idname = "TEXT_OT_duplicate_line";
ot->description = "Duplicate the current line";
/* api callbacks */
ot->exec = text_duplicate_line_exec;
ot->poll = text_edit_poll;
}
/******************* copy operator *********************/
static void txt_copy_clipboard(Text *text)