Code Cleanup: de-duplicate text pasting which only used the first line
This commit is contained in:
		@@ -515,14 +515,12 @@ static int paste_from_clipboard(bContext *C, ReportList *reports)
 | 
			
		||||
	int filelen;
 | 
			
		||||
	int retval;
 | 
			
		||||
 | 
			
		||||
	strp = WM_clipboard_text_get(false);
 | 
			
		||||
	strp = WM_clipboard_text_get(false, &filelen);
 | 
			
		||||
	if (strp == NULL) {
 | 
			
		||||
		BKE_report(reports, RPT_ERROR, "Clipboard empty");
 | 
			
		||||
		return OPERATOR_CANCELLED;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	filelen = strlen(strp);
 | 
			
		||||
 | 
			
		||||
	if ((filelen <= MAXTEXT) && font_paste_utf8(C, strp, filelen)) {
 | 
			
		||||
		text_update_edited(C, scene, obedit, 1, FO_EDIT);
 | 
			
		||||
		retval = OPERATOR_FINISHED;
 | 
			
		||||
 
 | 
			
		||||
@@ -1402,15 +1402,10 @@ static void ui_but_copy_paste(bContext *C, uiBut *but, uiHandleButtonData *data,
 | 
			
		||||
 | 
			
		||||
	if (mode == 'v') {
 | 
			
		||||
		/* extract first line from clipboard in case of multi-line copies */
 | 
			
		||||
		char *p, *pbuf = WM_clipboard_text_get(0);
 | 
			
		||||
		p = pbuf;
 | 
			
		||||
		if (p) {
 | 
			
		||||
			int i = 0;
 | 
			
		||||
			while (*p && *p != '\r' && *p != '\n' && i < UI_MAX_DRAW_STR) {
 | 
			
		||||
				buf[i++] = *p;
 | 
			
		||||
				p++;
 | 
			
		||||
			}
 | 
			
		||||
			buf[i] = 0;
 | 
			
		||||
		int pbuf_len;
 | 
			
		||||
		char *pbuf = WM_clipboard_text_get_firstline(false, &pbuf_len);
 | 
			
		||||
		if (pbuf) {
 | 
			
		||||
			BLI_strncpy(buf, pbuf, sizeof(buf));
 | 
			
		||||
			MEM_freeN(pbuf);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
@@ -1997,7 +1992,7 @@ enum {
 | 
			
		||||
 | 
			
		||||
static bool ui_textedit_copypaste(uiBut *but, uiHandleButtonData *data, const int mode)
 | 
			
		||||
{
 | 
			
		||||
	char *str, *p, *pbuf;
 | 
			
		||||
	char *str, *pbuf;
 | 
			
		||||
	int x;
 | 
			
		||||
	bool changed = false;
 | 
			
		||||
	int str_len, buf_len;
 | 
			
		||||
@@ -2009,17 +2004,13 @@ static bool ui_textedit_copypaste(uiBut *but, uiHandleButtonData *data, const in
 | 
			
		||||
	if (mode == UI_TEXTEDIT_PASTE) {
 | 
			
		||||
		/* TODO, ensure UTF8 ui_is_but_utf8() - campbell */
 | 
			
		||||
		/* extract the first line from the clipboard */
 | 
			
		||||
		p = pbuf = WM_clipboard_text_get(0);
 | 
			
		||||
		pbuf = WM_clipboard_text_get_firstline(false, &buf_len);
 | 
			
		||||
 | 
			
		||||
		if (p && p[0]) {
 | 
			
		||||
		if (pbuf) {
 | 
			
		||||
			char buf[UI_MAX_DRAW_STR] = {0};
 | 
			
		||||
			unsigned int y;
 | 
			
		||||
			buf_len = 0;
 | 
			
		||||
			while (*p && *p != '\r' && *p != '\n' && buf_len < UI_MAX_DRAW_STR - 1) {
 | 
			
		||||
				buf[buf_len++] = *p;
 | 
			
		||||
				p++;
 | 
			
		||||
			}
 | 
			
		||||
			buf[buf_len] = 0;
 | 
			
		||||
 | 
			
		||||
			buf_len = BLI_strncpy_rlen(buf, pbuf, sizeof(buf));
 | 
			
		||||
 | 
			
		||||
			/* paste over the current selection */
 | 
			
		||||
			if ((but->selend - but->selsta) > 0) {
 | 
			
		||||
 
 | 
			
		||||
@@ -970,8 +970,9 @@ static int console_paste_exec(bContext *C, wmOperator *UNUSED(op))
 | 
			
		||||
	SpaceConsole *sc = CTX_wm_space_console(C);
 | 
			
		||||
	ARegion *ar = CTX_wm_region(C);
 | 
			
		||||
	ConsoleLine *ci = console_history_verify(C);
 | 
			
		||||
	int buf_len;
 | 
			
		||||
 | 
			
		||||
	char *buf_str = WM_clipboard_text_get(0);
 | 
			
		||||
	char *buf_str = WM_clipboard_text_get(false, &buf_len);
 | 
			
		||||
	char *buf_step, *buf_next;
 | 
			
		||||
 | 
			
		||||
	if (buf_str == NULL)
 | 
			
		||||
 
 | 
			
		||||
@@ -797,11 +797,12 @@ static char *txt_copy_selected(Text *text)
 | 
			
		||||
 | 
			
		||||
static int text_paste_exec(bContext *C, wmOperator *op)
 | 
			
		||||
{
 | 
			
		||||
	const bool selection = RNA_boolean_get(op->ptr, "selection");
 | 
			
		||||
	Text *text = CTX_data_edit_text(C);
 | 
			
		||||
	char *buf;
 | 
			
		||||
	int selection = RNA_boolean_get(op->ptr, "selection");
 | 
			
		||||
	int buf_len;
 | 
			
		||||
 | 
			
		||||
	buf = WM_clipboard_text_get(selection);
 | 
			
		||||
	buf = WM_clipboard_text_get(selection, &buf_len);
 | 
			
		||||
 | 
			
		||||
	if (!buf)
 | 
			
		||||
		return OPERATOR_CANCELLED;
 | 
			
		||||
 
 | 
			
		||||
@@ -341,17 +341,13 @@ bool handleNumInput(bContext *C, NumInput *n, const wmEvent *event)
 | 
			
		||||
		case VKEY:
 | 
			
		||||
			if (event->ctrl) {
 | 
			
		||||
				/* extract the first line from the clipboard */
 | 
			
		||||
				char *pbuf = WM_clipboard_text_get(0);
 | 
			
		||||
				int pbuf_len;
 | 
			
		||||
				char *pbuf = WM_clipboard_text_get_firstline(false, &pbuf_len);
 | 
			
		||||
 | 
			
		||||
				if (pbuf) {
 | 
			
		||||
					bool success;
 | 
			
		||||
					/* Only copy string until first of this char. */
 | 
			
		||||
					char *cr = strchr(pbuf, '\r');
 | 
			
		||||
					char *cn = strchr(pbuf, '\n');
 | 
			
		||||
					if (cn && cn < cr) cr = cn;
 | 
			
		||||
					if (cr) *cr = '\0';
 | 
			
		||||
 | 
			
		||||
					success = editstr_insert_at_cursor(n, pbuf, strlen(pbuf));
 | 
			
		||||
					success = editstr_insert_at_cursor(n, pbuf, pbuf_len);
 | 
			
		||||
 | 
			
		||||
					MEM_freeN(pbuf);
 | 
			
		||||
					if (!success) {
 | 
			
		||||
 
 | 
			
		||||
@@ -831,10 +831,11 @@ static int rna_KeyMapItem_userdefined_get(PointerRNA *ptr)
 | 
			
		||||
static void rna_wmClipboard_get(PointerRNA *UNUSED(ptr), char *value)
 | 
			
		||||
{
 | 
			
		||||
	char *pbuf;
 | 
			
		||||
	int pbuf_len;
 | 
			
		||||
 | 
			
		||||
	pbuf = WM_clipboard_text_get(FALSE);
 | 
			
		||||
	pbuf = WM_clipboard_text_get(false, &pbuf_len);
 | 
			
		||||
	if (pbuf) {
 | 
			
		||||
		strcpy(value, pbuf);
 | 
			
		||||
		memcpy(value, pbuf, pbuf_len + 1);
 | 
			
		||||
		MEM_freeN(pbuf);
 | 
			
		||||
	}
 | 
			
		||||
	else {
 | 
			
		||||
@@ -845,19 +846,14 @@ static void rna_wmClipboard_get(PointerRNA *UNUSED(ptr), char *value)
 | 
			
		||||
static int rna_wmClipboard_length(PointerRNA *UNUSED(ptr))
 | 
			
		||||
{
 | 
			
		||||
	char *pbuf;
 | 
			
		||||
	int length;
 | 
			
		||||
	int pbuf_len;
 | 
			
		||||
 | 
			
		||||
	pbuf = WM_clipboard_text_get(FALSE);
 | 
			
		||||
	pbuf = WM_clipboard_text_get(false, &pbuf_len);
 | 
			
		||||
	if (pbuf) {
 | 
			
		||||
		length = strlen(pbuf);
 | 
			
		||||
		MEM_freeN(pbuf);
 | 
			
		||||
	}
 | 
			
		||||
	else {
 | 
			
		||||
		length = 0;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
	return length;
 | 
			
		||||
	return pbuf_len;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void rna_wmClipboard_set(PointerRNA *UNUSED(ptr), const char *value)
 | 
			
		||||
 
 | 
			
		||||
@@ -55,11 +55,6 @@ typedef struct {
 | 
			
		||||
#include "mathutils_Euler.h"
 | 
			
		||||
#include "mathutils_Color.h"
 | 
			
		||||
 | 
			
		||||
// /* utility submodules */
 | 
			
		||||
//
 | 
			
		||||
//
 | 
			
		||||
//#include "mathutils_kdtree.h"
 | 
			
		||||
 | 
			
		||||
PyObject *BaseMathObject_owner_get(BaseMathObject *self, void *);
 | 
			
		||||
PyObject *BaseMathObject_is_wrapped_get(BaseMathObject *self, void *);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -409,7 +409,8 @@ void		WM_job_main_thread_lock_acquire(struct wmJob *job);
 | 
			
		||||
void		WM_job_main_thread_lock_release(struct wmJob *job);
 | 
			
		||||
 | 
			
		||||
			/* clipboard */
 | 
			
		||||
char       *WM_clipboard_text_get(bool selection);
 | 
			
		||||
char       *WM_clipboard_text_get(bool selection, int *r_len);
 | 
			
		||||
char       *WM_clipboard_text_get_firstline(bool selection, int *r_len);
 | 
			
		||||
void        WM_clipboard_text_set(const char *buf, bool selection);
 | 
			
		||||
 | 
			
		||||
			/* progress */
 | 
			
		||||
 
 | 
			
		||||
@@ -1199,31 +1199,71 @@ void WM_event_remove_timer(wmWindowManager *wm, wmWindow *UNUSED(win), wmTimer *
 | 
			
		||||
 | 
			
		||||
/* ******************* clipboard **************** */
 | 
			
		||||
 | 
			
		||||
char *WM_clipboard_text_get(bool selection)
 | 
			
		||||
static char *wm_clipboard_text_get_ex(bool selection, int *r_len,
 | 
			
		||||
                                      bool firstline)
 | 
			
		||||
{
 | 
			
		||||
	char *p, *p2, *buf, *newbuf;
 | 
			
		||||
 | 
			
		||||
	if (G.background)
 | 
			
		||||
	if (G.background) {
 | 
			
		||||
		*r_len = 0;
 | 
			
		||||
		return NULL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	buf = (char *)GHOST_getClipboard(selection);
 | 
			
		||||
	if (!buf)
 | 
			
		||||
	if (!buf) {
 | 
			
		||||
		*r_len = 0;
 | 
			
		||||
		return NULL;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	/* always convert from \r\n to \n */
 | 
			
		||||
	newbuf = MEM_callocN(strlen(buf) + 1, __func__);
 | 
			
		||||
	p2 = newbuf = MEM_mallocN(strlen(buf) + 1, __func__);
 | 
			
		||||
 | 
			
		||||
	for (p = buf, p2 = newbuf; *p; p++) {
 | 
			
		||||
		if (*p != '\r')
 | 
			
		||||
			*(p2++) = *p;
 | 
			
		||||
	if (firstline) {
 | 
			
		||||
		/* will return an over-alloc'ed value in the case there are newlines */
 | 
			
		||||
		for (p = buf; *p; p++) {
 | 
			
		||||
			if ((*p != '\n') && (*p != '\r')) {
 | 
			
		||||
				*(p2++) = *p;
 | 
			
		||||
			}
 | 
			
		||||
			else {
 | 
			
		||||
				break;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	else {
 | 
			
		||||
		for (p = buf; *p; p++) {
 | 
			
		||||
			if (*p != '\r') {
 | 
			
		||||
				*(p2++) = *p;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	*p2 = '\0';
 | 
			
		||||
 | 
			
		||||
	free(buf); /* ghost uses regular malloc */
 | 
			
		||||
	
 | 
			
		||||
	*r_len = (p2 - newbuf);
 | 
			
		||||
 | 
			
		||||
	return newbuf;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Return text from the clipboard.
 | 
			
		||||
 *
 | 
			
		||||
 * \note Caller needs to check for valid utf8 if this is a requirement.
 | 
			
		||||
 */
 | 
			
		||||
char *WM_clipboard_text_get(bool selection, int *r_len)
 | 
			
		||||
{
 | 
			
		||||
	return wm_clipboard_text_get_ex(selection, r_len, false);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Convenience function for pasting to areas of Blender which don't support newlines.
 | 
			
		||||
 */
 | 
			
		||||
char *WM_clipboard_text_get_firstline(bool selection, int *r_len)
 | 
			
		||||
{
 | 
			
		||||
	return wm_clipboard_text_get_ex(selection, r_len, true);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void WM_clipboard_text_set(const char *buf, bool selection)
 | 
			
		||||
{
 | 
			
		||||
	if (!G.background) {
 | 
			
		||||
 
 | 
			
		||||
@@ -209,7 +209,8 @@ void WM_operator_handlers_clear(struct bContext *C, struct wmOperatorType *ot) {
 | 
			
		||||
void WM_autosave_init(struct bContext *C) {STUB_ASSERT(0);}
 | 
			
		||||
void WM_jobs_kill_all_except(struct wmWindowManager *wm) {STUB_ASSERT(0);}
 | 
			
		||||
 | 
			
		||||
char *WM_clipboard_text_get(int selection) {STUB_ASSERT(0); return (char *)0;}
 | 
			
		||||
char *WM_clipboard_text_get(bool selection, int *r_len) {STUB_ASSERT(0); return (char *)0;}
 | 
			
		||||
char *WM_clipboard_text_get_firstline(bool selection, int *r_len) {STUB_ASSERT(0); return (char *)0;}
 | 
			
		||||
void WM_clipboard_text_set(char *buf, int selection) {STUB_ASSERT(0);}
 | 
			
		||||
 | 
			
		||||
void WM_cursor_set(struct wmWindow *win, int curor) {STUB_ASSERT(0);}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user