This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/blenkernel/intern/text.c

2950 lines
65 KiB
C
Raw Normal View History

2011-10-10 09:38:02 +00:00
/*
* ***** BEGIN GPL LICENSE BLOCK *****
2002-10-12 11:37:38 +00:00
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
2002-10-12 11:37:38 +00:00
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
2010-02-12 13:34:04 +00:00
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2002-10-12 11:37:38 +00:00
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
2002-10-12 11:37:38 +00:00
*/
2011-02-27 20:40:57 +00:00
/** \file blender/blenkernel/intern/text.c
* \ingroup bke
*/
#include <stdlib.h> /* abort */
2002-10-12 11:37:38 +00:00
#include <string.h> /* strstr */
#include <sys/types.h>
#include <sys/stat.h>
#include <wchar.h>
#include <wctype.h>
2002-10-12 11:37:38 +00:00
#include "MEM_guardedalloc.h"
#include "BLI_path_util.h"
#include "BLI_string.h"
#include "BLI_string_cursor_utf8.h"
#include "BLI_string_utf8.h"
#include "BLI_listbase.h"
#include "BLI_utildefines.h"
#include "BLI_fileops.h"
2002-10-12 11:37:38 +00:00
#include "DNA_constraint_types.h"
#include "DNA_controller_types.h"
#include "DNA_actuator_types.h"
Giant commit! A full detailed description of this will be done later... is several days of work. Here's a summary: Render: - Full cleanup of render code, removing *all* globals and bad level calls all over blender. Render module is now not called abusive anymore - API-fied calls to rendering - Full recode of internal render pipeline. Is now rendering tiles by default, prepared for much smarter 'bucket' render later. - Each thread now can render a full part - Renders were tested with 4 threads, goes fine, apart from some lookup tables in softshadow and AO still - Rendering is prepared to do multiple layers and passes - No single 32 bits trick in render code anymore, all 100% floats now. Writing images/movies - moved writing images to blender kernel (bye bye 'schrijfplaatje'!) - made a new Movie handle system, also in kernel. This will enable much easier use of movies in Blender PreviewRender: - Using new render API, previewrender (in buttons) now uses regular render code to generate images. - new datafile 'preview.blend.c' has the preview scenes in it - previews get rendered in exact displayed size (1 pixel = 1 pixel) 3D Preview render - new; press Pkey in 3d window, for a panel that continuously renders (pkey is for games, i know... but we dont do that in orange now!) - this render works nearly identical to buttons-preview render, so it stops rendering on any event (mouse, keyboard, etc) - on moving/scaling the panel, the render code doesn't recreate all geometry - same for shifting/panning view - all other operations (now) regenerate the full render database still. - this is WIP... but big fun, especially for simple scenes! Compositor - Using same node system as now in use for shaders, you can composit images - works pretty straightforward... needs much more options/tools and integration with rendering still - is not threaded yet, nor is so smart to only recalculate changes... will be done soon! - the "Render Result" node will get all layers/passes as output sockets - The "Output" node renders to a builtin image, which you can view in the Image window. (yes, output nodes to render-result, and to files, is on the list!) The Bad News - "Unified Render" is removed. It might come back in some stage, but this system should be built from scratch. I can't really understand this code... I expect it is not much needed, especially with advanced layer/passes control - Panorama render, Field render, Motion blur, is not coded yet... (I had to recode every single feature in render, so...!) - Lens Flare is also not back... needs total revision, might become composit effect though (using zbuffer for visibility) - Part render is gone! (well, thats obvious, its default now). - The render window is only restored with limited functionality... I am going to check first the option to render to a Image window, so Blender can become a true single-window application. :) For example, the 'Spare render buffer' (jkey) doesnt work. - Render with border, now default creates a smaller image - No zbuffers are written yet... on the todo! - Scons files and MSVC will need work to get compiling again OK... thats what I can quickly recall. Now go compiling!
2006-01-23 22:05:47 +00:00
#include "DNA_scene_types.h"
#include "DNA_screen_types.h"
#include "DNA_space_types.h"
2002-10-12 11:37:38 +00:00
#include "DNA_text_types.h"
#include "DNA_userdef_types.h"
#include "DNA_object_types.h"
#include "DNA_node_types.h"
#include "DNA_material_types.h"
2002-10-12 11:37:38 +00:00
#include "BKE_depsgraph.h"
2002-10-12 11:37:38 +00:00
#include "BKE_global.h"
#include "BKE_library.h"
2002-10-12 11:37:38 +00:00
#include "BKE_main.h"
#include "BKE_text.h"
#include "BKE_node.h"
2002-10-12 11:37:38 +00:00
#ifdef WITH_PYTHON
#include "BPY_extern.h"
#endif
2012-03-09 18:28:30 +00:00
/*
* How Texts should work
* --
* A text should relate to a file as follows -
* (Text *)->name should be the place where the
* file will or has been saved.
*
* (Text *)->flags has the following bits
* TXT_ISDIRTY - should always be set if the file in mem. differs from
* the file on disk, or if there is no file on disk.
* TXT_ISMEM - should always be set if the Text has not been mapped to
* a file, in which case (Text *)->name may be NULL or garbage.
* TXT_ISEXT - should always be set if the Text is not to be written into
* the .blend
* TXT_ISSCRIPT - should be set if the user has designated the text
* as a script. (NEW: this was unused, but now it is needed by
* space handler script links (see header_view3d.c, for example)
*
* ->>> see also: /makesdna/DNA_text_types.h
*
* Display
* --
* The st->top determines at what line the top of the text is displayed.
* If the user moves the cursor the st containing that cursor should
* be popped ... other st's retain their own top location.
*
* Undo
* --
* Undo/Redo works by storing
* events in a queue, and a pointer
* to the current position in the
* queue...
*
* Events are stored using an
* arbitrary op-code system
* to keep track of
* a) the two cursors (normal and selected)
* b) input (visible and control (ie backspace))
*
* input data is stored as its
* ASCII value, the opcodes are
* then selected to not conflict.
*
* opcodes with data in between are
* written at the beginning and end
* of the data to allow undo and redo
* to simply check the code at the current
* undo position
*
*/
2002-10-12 11:37:38 +00:00
/***/
static void txt_pop_first(Text *text);
static void txt_pop_last(Text *text);
static void txt_undo_add_blockop(Text *text, int op, const char *buf);
2002-10-12 11:37:38 +00:00
static void txt_delete_line(Text *text, TextLine *line);
static void txt_delete_sel(Text *text);
static void txt_make_dirty(Text *text);
2002-10-12 11:37:38 +00:00
/***/
static unsigned char undoing;
/* allow to switch off undoing externally */
void txt_set_undostate(int u)
{
undoing = u;
}
int txt_get_undostate(void)
{
return undoing;
}
static void init_undo_text(Text *text)
{
text->undo_pos = -1;
text->undo_len = TXT_INIT_UNDO;
text->undo_buf = MEM_mallocN(text->undo_len, "undo buf");
}
void BKE_text_free(Text *text)
2002-10-12 11:37:38 +00:00
{
TextLine *tmp;
for (tmp = text->lines.first; tmp; tmp = tmp->next) {
2002-10-12 11:37:38 +00:00
MEM_freeN(tmp->line);
if (tmp->format)
MEM_freeN(tmp->format);
}
2002-10-12 11:37:38 +00:00
BLI_freelistN(&text->lines);
if (text->name) MEM_freeN(text->name);
2002-10-12 11:37:38 +00:00
MEM_freeN(text->undo_buf);
#ifdef WITH_PYTHON
if (text->compiled) BPY_text_free_code(text);
#endif
2002-10-12 11:37:38 +00:00
}
Text *BKE_text_add(const char *name)
2002-10-12 11:37:38 +00:00
{
Main *bmain = G.main;
2002-10-12 11:37:38 +00:00
Text *ta;
TextLine *tmp;
ta = BKE_libblock_alloc(&bmain->text, ID_TXT, name);
ta->id.us = 1;
2002-10-12 11:37:38 +00:00
ta->name = NULL;
2002-10-12 11:37:38 +00:00
init_undo_text(ta);
ta->nlines = 1;
ta->flags = TXT_ISDIRTY | TXT_ISMEM;
if ((U.flag & USER_TXT_TABSTOSPACES_DISABLE) == 0)
ta->flags |= TXT_TABSTOSPACES;
2002-10-12 11:37:38 +00:00
ta->lines.first = ta->lines.last = NULL;
2002-10-12 11:37:38 +00:00
tmp = (TextLine *) MEM_mallocN(sizeof(TextLine), "textline");
tmp->line = (char *) MEM_mallocN(1, "textline_string");
tmp->format = NULL;
tmp->line[0] = 0;
tmp->len = 0;
2002-10-12 11:37:38 +00:00
tmp->next = NULL;
tmp->prev = NULL;
2002-10-12 11:37:38 +00:00
BLI_addhead(&ta->lines, tmp);
ta->curl = ta->lines.first;
ta->curc = 0;
ta->sell = ta->lines.first;
ta->selc = 0;
2002-10-12 11:37:38 +00:00
return ta;
}
/* this function replaces extended ascii characters */
/* to a valid utf-8 sequences */
int txt_extended_ascii_as_utf8(char **str)
{
int bad_char, added = 0, i = 0;
int length = strlen(*str);
while ((*str)[i]) {
if ((bad_char = BLI_utf8_invalid_byte(*str + i, length - i)) == -1)
break;
added++;
i += bad_char + 1;
}
if (added != 0) {
char *newstr = MEM_mallocN(length + added + 1, "text_line");
int mi = 0;
i = 0;
while ((*str)[i]) {
if ((bad_char = BLI_utf8_invalid_byte((*str) + i, length - i)) == -1) {
memcpy(newstr + mi, (*str) + i, length - i + 1);
break;
}
memcpy(newstr + mi, (*str) + i, bad_char);
BLI_str_utf8_from_unicode((*str)[i + bad_char], newstr + mi + bad_char);
i += bad_char + 1;
mi += bad_char + 2;
}
newstr[length + added] = '\0';
MEM_freeN(*str);
*str = newstr;
}
return added;
}
2002-10-12 11:37:38 +00:00
// this function removes any control characters from
// a textline and fixes invalid utf-8 sequences
2002-10-12 11:37:38 +00:00
static void cleanup_textline(TextLine *tl)
2002-10-12 11:37:38 +00:00
{
int i;
for (i = 0; i < tl->len; i++) {
2002-10-12 11:37:38 +00:00
if (tl->line[i] < ' ' && tl->line[i] != '\t') {
memmove(tl->line + i, tl->line + i + 1, tl->len - i);
tl->len--;
i--;
}
}
tl->len += txt_extended_ascii_as_utf8(&tl->line);
2002-10-12 11:37:38 +00:00
}
int BKE_text_reload(Text *text)
2002-10-12 11:37:38 +00:00
{
FILE *fp;
int i, llen, len;
2002-10-12 11:37:38 +00:00
unsigned char *buffer;
TextLine *tmp;
char str[FILE_MAX];
struct stat st;
2002-10-12 11:37:38 +00:00
if (!text || !text->name) return 0;
BLI_strncpy(str, text->name, FILE_MAX);
BLI_path_abs(str, G.main->name);
2002-10-12 11:37:38 +00:00
fp = BLI_fopen(str, "r");
if (fp == NULL) return 0;
2002-10-12 11:37:38 +00:00
/* free memory: */
for (tmp = text->lines.first; tmp; tmp = tmp->next) {
2002-10-12 11:37:38 +00:00
MEM_freeN(tmp->line);
if (tmp->format) MEM_freeN(tmp->format);
}
2002-10-12 11:37:38 +00:00
BLI_freelistN(&text->lines);
text->lines.first = text->lines.last = NULL;
text->curl = text->sell = NULL;
2002-10-12 11:37:38 +00:00
/* clear undo buffer */
MEM_freeN(text->undo_buf);
init_undo_text(text);
2002-10-12 11:37:38 +00:00
fseek(fp, 0L, SEEK_END);
len = ftell(fp);
fseek(fp, 0L, SEEK_SET);
2002-10-12 11:37:38 +00:00
text->undo_pos = -1;
2002-10-12 11:37:38 +00:00
buffer = MEM_mallocN(len, "text_buffer");
2002-10-12 11:37:38 +00:00
// under windows fread can return less then len bytes because
// of CR stripping
len = fread(buffer, 1, len, fp);
fclose(fp);
stat(str, &st);
text->mtime = st.st_mtime;
text->nlines = 0;
llen = 0;
for (i = 0; i < len; i++) {
if (buffer[i] == '\n') {
tmp = (TextLine *) MEM_mallocN(sizeof(TextLine), "textline");
tmp->line = (char *) MEM_mallocN(llen + 1, "textline_string");
tmp->format = NULL;
if (llen) memcpy(tmp->line, &buffer[i - llen], llen);
tmp->line[llen] = 0;
tmp->len = llen;
2002-10-12 11:37:38 +00:00
cleanup_textline(tmp);
BLI_addtail(&text->lines, tmp);
text->nlines++;
llen = 0;
2002-10-12 11:37:38 +00:00
continue;
}
llen++;
}
if (llen != 0 || text->nlines == 0) {
tmp = (TextLine *) MEM_mallocN(sizeof(TextLine), "textline");
tmp->line = (char *) MEM_mallocN(llen + 1, "textline_string");
tmp->format = NULL;
2002-10-12 11:37:38 +00:00
if (llen) memcpy(tmp->line, &buffer[i - llen], llen);
2002-10-12 11:37:38 +00:00
tmp->line[llen] = 0;
tmp->len = llen;
2002-10-12 11:37:38 +00:00
cleanup_textline(tmp);
BLI_addtail(&text->lines, tmp);
text->nlines++;
}
text->curl = text->sell = text->lines.first;
text->curc = text->selc = 0;
2002-10-12 11:37:38 +00:00
MEM_freeN(buffer);
2002-10-12 11:37:38 +00:00
return 1;
}
Text *BKE_text_load(const char *file, const char *relpath)
2002-10-12 11:37:38 +00:00
{
Main *bmain = G.main;
2002-10-12 11:37:38 +00:00
FILE *fp;
int i, llen, len;
2002-10-12 11:37:38 +00:00
unsigned char *buffer;
TextLine *tmp;
Text *ta;
char str[FILE_MAX];
struct stat st;
2002-10-12 11:37:38 +00:00
BLI_strncpy(str, file, FILE_MAX);
if (relpath) /* can be NULL (bg mode) */
BLI_path_abs(str, relpath);
2002-10-12 11:37:38 +00:00
fp = BLI_fopen(str, "r");
if (fp == NULL) return NULL;
2002-10-12 11:37:38 +00:00
ta = BKE_libblock_alloc(&bmain->text, ID_TXT, BLI_path_basename(str));
ta->id.us = 1;
2002-10-12 11:37:38 +00:00
ta->lines.first = ta->lines.last = NULL;
ta->curl = ta->sell = NULL;
if ((U.flag & USER_TXT_TABSTOSPACES_DISABLE) == 0)
ta->flags = TXT_TABSTOSPACES;
2002-10-12 11:37:38 +00:00
fseek(fp, 0L, SEEK_END);
len = ftell(fp);
fseek(fp, 0L, SEEK_SET);
2002-10-12 11:37:38 +00:00
ta->name = MEM_mallocN(strlen(file) + 1, "text_name");
2002-10-12 11:37:38 +00:00
strcpy(ta->name, file);
init_undo_text(ta);
2002-10-12 11:37:38 +00:00
buffer = MEM_mallocN(len, "text_buffer");
2002-10-12 11:37:38 +00:00
// under windows fread can return less then len bytes because
// of CR stripping
len = fread(buffer, 1, len, fp);
fclose(fp);
stat(str, &st);
ta->mtime = st.st_mtime;
ta->nlines = 0;
llen = 0;
for (i = 0; i < len; i++) {
if (buffer[i] == '\n') {
tmp = (TextLine *) MEM_mallocN(sizeof(TextLine), "textline");
tmp->line = (char *) MEM_mallocN(llen + 1, "textline_string");
tmp->format = NULL;
if (llen) memcpy(tmp->line, &buffer[i - llen], llen);
tmp->line[llen] = 0;
tmp->len = llen;
2002-10-12 11:37:38 +00:00
cleanup_textline(tmp);
BLI_addtail(&ta->lines, tmp);
ta->nlines++;
llen = 0;
2002-10-12 11:37:38 +00:00
continue;
}
llen++;
}
/* create new line in cases:
* - rest of line (if last line in file hasn't got \n terminator).
* in this case content of such line would be used to fill text line buffer
* - file is empty. in this case new line is needed to start editing from.
* - last characted in buffer is \n. in this case new line is needed to
* deal with newline at end of file. (see [#28087]) (sergey) */
if (llen != 0 || ta->nlines == 0 || buffer[len - 1] == '\n') {
tmp = (TextLine *) MEM_mallocN(sizeof(TextLine), "textline");
tmp->line = (char *) MEM_mallocN(llen + 1, "textline_string");
tmp->format = NULL;
2002-10-12 11:37:38 +00:00
if (llen) memcpy(tmp->line, &buffer[i - llen], llen);
2002-10-12 11:37:38 +00:00
tmp->line[llen] = 0;
tmp->len = llen;
2002-10-12 11:37:38 +00:00
cleanup_textline(tmp);
BLI_addtail(&ta->lines, tmp);
ta->nlines++;
}
ta->curl = ta->sell = ta->lines.first;
ta->curc = ta->selc = 0;
2002-10-12 11:37:38 +00:00
MEM_freeN(buffer);
2002-10-12 11:37:38 +00:00
return ta;
}
Text *BKE_text_copy(Text *ta)
2002-10-12 11:37:38 +00:00
{
Text *tan;
TextLine *line, *tmp;
tan = BKE_libblock_copy(&ta->id);
2002-10-12 11:37:38 +00:00
/* file name can be NULL */
if (ta->name) {
tan->name = MEM_mallocN(strlen(ta->name) + 1, "text_name");
strcpy(tan->name, ta->name);
}
else {
tan->name = NULL;
}
2.5: Text Editor back. There was very little structure in this code, using many globals and duplicated code. Now it should be better structured. Most things should work, the main parts that are not back yet are the python plugins and markers. Notes: * Blenfont is used for drawing the text, nicely anti-aliased. * A monospace truetype font was added, since that is needed for the text editor. It's Bitstream Vera Sans Mono. This is the default gnome terminal font, but it doesn't fit entirely well with the other font I think, can be changed easily of course. * Clipboard copy/cut/paste now always uses the system clipboard, the code for the own cut buffer was removed. * The interface buttons should support copy/cut/paste again now as well. * WM_clipboard_text_get/WM_clipboard_text_set were added to the windowmanager code. * Find panel is now a kind of second header, instead of a panel. This needs especially a way to start editing the text field immediately on open still. * Operators are independent of the actual space when possible, was a bit of puzzling but got it solved nice with notifiers, and some lazy init for syntax highlight in the drawing code. * RNA was created for the text editor space and used for buttons. * Operators: * New, Open, Reload, Save, Save As, Make Internal * Run Script, Refresh Pyconstraints * Copy, Cut, Paste * Convert Whitespace, Uncomment, Comment, Indent, Unindent * Line Break, Insert * Next Marker, Previous Marker, Clear All Markers, Mark All * Select Line, Select All * Jump, Move, Move Select, Delete, Toggle Overwrite * Scroll, Scroll Bar, Set Cursor, Line Number * Find and Replace, Find, Replace, Find Set Selected, Replace Set Selected * To 3D Object * Resolve Conflict
2009-02-28 23:33:35 +00:00
tan->flags = ta->flags | TXT_ISDIRTY;
2002-10-12 11:37:38 +00:00
tan->lines.first = tan->lines.last = NULL;
tan->curl = tan->sell = NULL;
2002-10-12 11:37:38 +00:00
tan->nlines = ta->nlines;
2002-10-12 11:37:38 +00:00
line = ta->lines.first;
2002-10-12 11:37:38 +00:00
/* Walk down, reconstructing */
while (line) {
tmp = (TextLine *) MEM_mallocN(sizeof(TextLine), "textline");
tmp->line = MEM_mallocN(line->len + 1, "textline_string");
tmp->format = NULL;
2002-10-12 11:37:38 +00:00
strcpy(tmp->line, line->line);
tmp->len = line->len;
2002-10-12 11:37:38 +00:00
BLI_addtail(&tan->lines, tmp);
line = line->next;
2002-10-12 11:37:38 +00:00
}
tan->curl = tan->sell = tan->lines.first;
tan->curc = tan->selc = 0;
2002-10-12 11:37:38 +00:00
init_undo_text(tan);
2002-10-12 11:37:38 +00:00
return tan;
}
void BKE_text_unlink(Main *bmain, Text *text)
{
bScreen *scr;
ScrArea *area;
SpaceLink *sl;
Object *ob;
bController *cont;
bActuator *act;
bConstraint *con;
bNodeTree *ntree;
bNode *node;
Material *mat;
short update;
for (ob = bmain->object.first; ob; ob = ob->id.next) {
/* game controllers */
for (cont = ob->controllers.first; cont; cont = cont->next) {
if (cont->type == CONT_PYTHON) {
bPythonCont *pc;
pc = cont->data;
if (pc->text == text) pc->text = NULL;
}
}
/* game actuators */
for (act = ob->actuators.first; act; act = act->next) {
if (act->type == ACT_2DFILTER) {
bTwoDFilterActuator *tfa;
tfa = act->data;
if (tfa->text == text) tfa->text = NULL;
}
}
/* pyconstraints */
update = 0;
if (ob->type == OB_ARMATURE && ob->pose) {
bPoseChannel *pchan;
for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
for (con = pchan->constraints.first; con; con = con->next) {
if (con->type == CONSTRAINT_TYPE_PYTHON) {
bPythonConstraint *data = con->data;
if (data->text == text) data->text = NULL;
update = 1;
}
}
}
}
for (con = ob->constraints.first; con; con = con->next) {
if (con->type == CONSTRAINT_TYPE_PYTHON) {
bPythonConstraint *data = con->data;
if (data->text == text) data->text = NULL;
update = 1;
}
}
if (update)
DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
}
/* nodes */
for (mat = bmain->mat.first; mat; mat = mat->id.next) {
ntree = mat->nodetree;
if (!ntree)
continue;
for (node = ntree->nodes.first; node; node = node->next) {
if (node->type == SH_NODE_SCRIPT) {
Text *ntext = (Text *)node->id;
if (ntext == text) node->id = NULL;
}
}
}
for (ntree = bmain->nodetree.first; ntree; ntree = ntree->id.next) {
for (node = ntree->nodes.first; node; node = node->next) {
if (node->type == SH_NODE_SCRIPT) {
Text *ntext = (Text *)node->id;
if (ntext == text) node->id = NULL;
}
}
}
/* text space */
for (scr = bmain->screen.first; scr; scr = scr->id.next) {
for (area = scr->areabase.first; area; area = area->next) {
for (sl = area->spacedata.first; sl; sl = sl->next) {
if (sl->spacetype == SPACE_TEXT) {
SpaceText *st = (SpaceText *) sl;
if (st->text == text) {
st->text = NULL;
st->top = 0;
}
}
}
}
}
text->id.us = 0;
}
void BKE_text_clear(Text *text) /* called directly from rna */
{
int oldstate;
oldstate = txt_get_undostate( );
txt_set_undostate(1);
txt_sel_all(text);
txt_delete_sel(text);
txt_set_undostate(oldstate);
txt_make_dirty(text);
}
void BKE_text_write(Text *text, const char *str) /* called directly from rna */
{
int oldstate;
oldstate = txt_get_undostate();
txt_insert_buf(text, str);
txt_move_eof(text, 0);
txt_set_undostate(oldstate);
txt_make_dirty(text);
}
2002-10-12 11:37:38 +00:00
/*****************************/
/* Editing utility functions */
/*****************************/
static void make_new_line(TextLine *line, char *newline)
2002-10-12 11:37:38 +00:00
{
2005-05-18 23:54:56 +00:00
if (line->line) MEM_freeN(line->line);
if (line->format) MEM_freeN(line->format);
2002-10-12 11:37:38 +00:00
line->line = newline;
line->len = strlen(newline);
line->format = NULL;
2002-10-12 11:37:38 +00:00
}
static TextLine *txt_new_line(const char *str)
2002-10-12 11:37:38 +00:00
{
TextLine *tmp;
if (!str) str = "";
2002-10-12 11:37:38 +00:00
tmp = (TextLine *) MEM_mallocN(sizeof(TextLine), "textline");
tmp->line = MEM_mallocN(strlen(str) + 1, "textline_string");
tmp->format = NULL;
2002-10-12 11:37:38 +00:00
strcpy(tmp->line, str);
tmp->len = strlen(str);
tmp->next = tmp->prev = NULL;
2002-10-12 11:37:38 +00:00
return tmp;
}
static TextLine *txt_new_linen(const char *str, int n)
2002-10-12 11:37:38 +00:00
{
TextLine *tmp;
tmp = (TextLine *) MEM_mallocN(sizeof(TextLine), "textline");
tmp->line = MEM_mallocN(n + 1, "textline_string");
tmp->format = NULL;
BLI_strncpy(tmp->line, (str) ? str : "", n + 1);
2002-10-12 11:37:38 +00:00
tmp->len = strlen(tmp->line);
tmp->next = tmp->prev = NULL;
2002-10-12 11:37:38 +00:00
return tmp;
}
void txt_clean_text(Text *text)
2002-10-12 11:37:38 +00:00
{
TextLine **top, **bot;
if (!text) return;
if (!text->lines.first) {
if (text->lines.last) text->lines.first = text->lines.last;
else text->lines.first = text->lines.last = txt_new_line(NULL);
}
2002-10-12 11:37:38 +00:00
if (!text->lines.last) text->lines.last = text->lines.first;
2002-10-12 11:37:38 +00:00
top = (TextLine **) &text->lines.first;
bot = (TextLine **) &text->lines.last;
2002-10-12 11:37:38 +00:00
while ((*top)->prev) *top = (*top)->prev;
while ((*bot)->next) *bot = (*bot)->next;
2002-10-12 11:37:38 +00:00
if (!text->curl) {
if (text->sell) text->curl = text->sell;
else text->curl = text->lines.first;
text->curc = 0;
2002-10-12 11:37:38 +00:00
}
if (!text->sell) {
text->sell = text->curl;
text->selc = 0;
2002-10-12 11:37:38 +00:00
}
}
int txt_get_span(TextLine *from, TextLine *to)
2002-10-12 11:37:38 +00:00
{
int ret = 0;
TextLine *tmp = from;
2002-10-12 11:37:38 +00:00
if (!to || !from) return 0;
if (from == to) return 0;
2002-10-12 11:37:38 +00:00
/* Look forwards */
while (tmp) {
if (tmp == to) return ret;
ret++;
tmp = tmp->next;
2002-10-12 11:37:38 +00:00
}
/* Look backwards */
if (!tmp) {
tmp = from;
ret = 0;
while (tmp) {
2002-10-12 11:37:38 +00:00
if (tmp == to) break;
ret--;
tmp = tmp->prev;
2002-10-12 11:37:38 +00:00
}
if (!tmp) ret = 0;
2002-10-12 11:37:38 +00:00
}
return ret;
2002-10-12 11:37:38 +00:00
}
static void txt_make_dirty(Text *text)
2002-10-12 11:37:38 +00:00
{
text->flags |= TXT_ISDIRTY;
#ifdef WITH_PYTHON
if (text->compiled) BPY_text_free_code(text);
#endif
2002-10-12 11:37:38 +00:00
}
/****************************/
/* Cursor utility functions */
/****************************/
static void txt_curs_cur(Text *text, TextLine ***linep, int **charp)
2002-10-12 11:37:38 +00:00
{
*linep = &text->curl; *charp = &text->curc;
2002-10-12 11:37:38 +00:00
}
static void txt_curs_sel(Text *text, TextLine ***linep, int **charp)
2002-10-12 11:37:38 +00:00
{
*linep = &text->sell; *charp = &text->selc;
2002-10-12 11:37:38 +00:00
}
/*****************************/
2002-10-12 11:37:38 +00:00
/* Cursor movement functions */
/*****************************/
int txt_utf8_offset_to_index(const char *str, int offset)
{
int index = 0, pos = 0;
while (pos != offset) {
pos += BLI_str_utf8_size(str + pos);
index++;
}
return index;
}
int txt_utf8_index_to_offset(const char *str, int index)
{
int offset = 0, pos = 0;
while (pos != index) {
offset += BLI_str_utf8_size(str + offset);
pos++;
}
return offset;
}
/* returns the real number of characters in string */
/* not the same as BLI_strlen_utf8, which returns length for wide characters */
static int txt_utf8_len(const char *src)
{
int len;
for (len = 0; *src; len++) {
src += BLI_str_utf8_size(src);
}
return len;
}
2002-10-12 11:37:38 +00:00
void txt_move_up(Text *text, short sel)
{
TextLine **linep;
2012-01-22 17:26:56 +00:00
int *charp;
2002-10-12 11:37:38 +00:00
if (!text) return;
if (sel) txt_curs_sel(text, &linep, &charp);
2002-10-12 11:37:38 +00:00
else { txt_pop_first(text); txt_curs_cur(text, &linep, &charp); }
if (!*linep) return;
if ((*linep)->prev) {
int index = txt_utf8_offset_to_index((*linep)->line, *charp);
*linep = (*linep)->prev;
if (index > txt_utf8_len((*linep)->line)) *charp = (*linep)->len;
else *charp = txt_utf8_index_to_offset((*linep)->line, index);
}
else {
txt_move_bol(text, sel);
2002-10-12 11:37:38 +00:00
}
if (!sel) txt_pop_sel(text);
2002-10-12 11:37:38 +00:00
}
void txt_move_down(Text *text, short sel)
{
TextLine **linep;
2012-01-22 17:26:56 +00:00
int *charp;
2002-10-12 11:37:38 +00:00
if (!text) return;
if (sel) txt_curs_sel(text, &linep, &charp);
2002-10-12 11:37:38 +00:00
else { txt_pop_last(text); txt_curs_cur(text, &linep, &charp); }
if (!*linep) return;
if ((*linep)->next) {
int index = txt_utf8_offset_to_index((*linep)->line, *charp);
*linep = (*linep)->next;
if (index > txt_utf8_len((*linep)->line)) *charp = (*linep)->len;
else *charp = txt_utf8_index_to_offset((*linep)->line, index);
}
else {
txt_move_eol(text, sel);
2002-10-12 11:37:38 +00:00
}
if (!sel) txt_pop_sel(text);
2002-10-12 11:37:38 +00:00
}
void txt_move_left(Text *text, short sel)
{
TextLine **linep;
int *charp;
int tabsize = 0, i = 0;
2002-10-12 11:37:38 +00:00
if (!text) return;
if (sel) txt_curs_sel(text, &linep, &charp);
2002-10-12 11:37:38 +00:00
else { txt_pop_first(text); txt_curs_cur(text, &linep, &charp); }
if (!*linep) return;
if (*charp == 0) {
if ((*linep)->prev) {
txt_move_up(text, sel);
*charp = (*linep)->len;
}
}
else {
// do nice left only if there are only spaces
// TXT_TABSIZE hardcoded in DNA_text_types.h
if (text->flags & TXT_TABSTOSPACES) {
tabsize = (*charp < TXT_TABSIZE) ? *charp : TXT_TABSIZE;
for (i = 0; i < (*charp); i++)
if ((*linep)->line[i] != ' ') {
tabsize = 0;
break;
}
// if in the middle of the space-tab
if (tabsize && (*charp) % TXT_TABSIZE != 0)
tabsize = ((*charp) % TXT_TABSIZE);
}
if (tabsize)
(*charp) -= tabsize;
else {
const char *prev = BLI_str_prev_char_utf8((*linep)->line + *charp);
*charp = prev - (*linep)->line;
2002-10-12 11:37:38 +00:00
}
}
if (!sel) txt_pop_sel(text);
2002-10-12 11:37:38 +00:00
}
void txt_move_right(Text *text, short sel)
{
TextLine **linep;
int *charp, do_tab = FALSE, i;
2002-10-12 11:37:38 +00:00
if (!text) return;
if (sel) txt_curs_sel(text, &linep, &charp);
2002-10-12 11:37:38 +00:00
else { txt_pop_last(text); txt_curs_cur(text, &linep, &charp); }
if (!*linep) return;
if (*charp == (*linep)->len) {
2002-10-12 11:37:38 +00:00
if ((*linep)->next) {
txt_move_down(text, sel);
*charp = 0;
2002-10-12 11:37:38 +00:00
}
}
else {
// do nice right only if there are only spaces
// spaces hardcoded in DNA_text_types.h
if (text->flags & TXT_TABSTOSPACES && (*linep)->line[*charp] == ' ') {
do_tab = TRUE;
for (i = 0; i < *charp; i++)
if ((*linep)->line[i] != ' ') {
do_tab = FALSE;
break;
}
}
if (do_tab) {
int tabsize = (*charp) % TXT_TABSIZE + 1;
for (i = *charp + 1; (*linep)->line[i] == ' ' && tabsize < TXT_TABSIZE; i++)
tabsize++;
(*charp) = i;
}
else (*charp) += BLI_str_utf8_size((*linep)->line + *charp);
2002-10-12 11:37:38 +00:00
}
if (!sel) txt_pop_sel(text);
2002-10-12 11:37:38 +00:00
}
void txt_jump_left(Text *text, short sel)
{
TextLine **linep;
int *charp;
if (!text) return;
if (sel) txt_curs_sel(text, &linep, &charp);
else { txt_pop_first(text); txt_curs_cur(text, &linep, &charp); }
if (!*linep) return;
BLI_str_cursor_step_utf8((*linep)->line, (*linep)->len,
charp, STRCUR_DIR_PREV,
STRCUR_JUMP_DELIM);
if (!sel) txt_pop_sel(text);
}
void txt_jump_right(Text *text, short sel)
{
TextLine **linep;
int *charp;
if (!text) return;
if (sel) txt_curs_sel(text, &linep, &charp);
else { txt_pop_last(text); txt_curs_cur(text, &linep, &charp); }
if (!*linep) return;
BLI_str_cursor_step_utf8((*linep)->line, (*linep)->len,
charp, STRCUR_DIR_NEXT,
STRCUR_JUMP_DELIM);
if (!sel) txt_pop_sel(text);
}
void txt_move_bol(Text *text, short sel)
2002-10-12 11:37:38 +00:00
{
TextLine **linep;
int *charp;
2002-10-12 11:37:38 +00:00
if (!text) return;
if (sel) txt_curs_sel(text, &linep, &charp);
2002-10-12 11:37:38 +00:00
else txt_curs_cur(text, &linep, &charp);
if (!*linep) return;
*charp = 0;
2002-10-12 11:37:38 +00:00
if (!sel) txt_pop_sel(text);
2002-10-12 11:37:38 +00:00
}
void txt_move_eol(Text *text, short sel)
2002-10-12 11:37:38 +00:00
{
TextLine **linep;
int *charp;
2002-10-12 11:37:38 +00:00
if (!text) return;
if (sel) txt_curs_sel(text, &linep, &charp);
2002-10-12 11:37:38 +00:00
else txt_curs_cur(text, &linep, &charp);
if (!*linep) return;
*charp = (*linep)->len;
2002-10-12 11:37:38 +00:00
if (!sel) txt_pop_sel(text);
2002-10-12 11:37:38 +00:00
}
void txt_move_bof(Text *text, short sel)
2002-10-12 11:37:38 +00:00
{
TextLine **linep;
int *charp;
2002-10-12 11:37:38 +00:00
if (!text) return;
if (sel) txt_curs_sel(text, &linep, &charp);
2002-10-12 11:37:38 +00:00
else txt_curs_cur(text, &linep, &charp);
if (!*linep) return;
*linep = text->lines.first;
*charp = 0;
2002-10-12 11:37:38 +00:00
if (!sel) txt_pop_sel(text);
2002-10-12 11:37:38 +00:00
}
void txt_move_eof(Text *text, short sel)
2002-10-12 11:37:38 +00:00
{
TextLine **linep;
int *charp;
2002-10-12 11:37:38 +00:00
if (!text) return;
if (sel) txt_curs_sel(text, &linep, &charp);
2002-10-12 11:37:38 +00:00
else txt_curs_cur(text, &linep, &charp);
if (!*linep) return;
*linep = text->lines.last;
*charp = (*linep)->len;
2002-10-12 11:37:38 +00:00
if (!sel) txt_pop_sel(text);
2002-10-12 11:37:38 +00:00
}
void txt_move_toline(Text *text, unsigned int line, short sel)
{
txt_move_to(text, line, 0, sel);
}
/* Moves to a certain byte in a line, not a certain utf8-character! */
void txt_move_to(Text *text, unsigned int line, unsigned int ch, short sel)
2002-10-12 11:37:38 +00:00
{
TextLine **linep;
int *charp;
2002-10-12 11:37:38 +00:00
unsigned int i;
if (!text) return;
if (sel) txt_curs_sel(text, &linep, &charp);
2002-10-12 11:37:38 +00:00
else txt_curs_cur(text, &linep, &charp);
if (!*linep) return;
*linep = text->lines.first;
for (i = 0; i < line; i++) {
if ((*linep)->next) *linep = (*linep)->next;
2002-10-12 11:37:38 +00:00
else break;
}
if (ch > (unsigned int)((*linep)->len))
ch = (unsigned int)((*linep)->len);
*charp = ch;
2002-10-12 11:37:38 +00:00
if (!sel) txt_pop_sel(text);
2002-10-12 11:37:38 +00:00
}
/****************************/
/* Text selection functions */
/****************************/
static void txt_curs_swap(Text *text)
2002-10-12 11:37:38 +00:00
{
TextLine *tmpl;
int tmpc;
tmpl = text->curl;
text->curl = text->sell;
text->sell = tmpl;
tmpc = text->curc;
text->curc = text->selc;
text->selc = tmpc;
2002-10-12 11:37:38 +00:00
}
static void txt_pop_first(Text *text)
2002-10-12 11:37:38 +00:00
{
if (txt_get_span(text->curl, text->sell) < 0 ||
(text->curl == text->sell && text->curc > text->selc))
{
2002-10-12 11:37:38 +00:00
txt_curs_swap(text);
}
txt_pop_sel(text);
}
static void txt_pop_last(Text *text)
2002-10-12 11:37:38 +00:00
{
if (txt_get_span(text->curl, text->sell) > 0 ||
(text->curl == text->sell && text->curc < text->selc))
{
2002-10-12 11:37:38 +00:00
txt_curs_swap(text);
}
txt_pop_sel(text);
}
/* never used: CVS 1.19 */
/* static void txt_pop_selr (Text *text) */
void txt_pop_sel(Text *text)
2002-10-12 11:37:38 +00:00
{
text->sell = text->curl;
text->selc = text->curc;
2002-10-12 11:37:38 +00:00
}
void txt_order_cursors(Text *text)
{
if (!text) return;
if (!text->curl) return;
if (!text->sell) return;
/* Flip so text->curl is before text->sell */
if ((txt_get_span(text->curl, text->sell) < 0) ||
(text->curl == text->sell && text->curc > text->selc))
{
2002-10-12 11:37:38 +00:00
txt_curs_swap(text);
}
2002-10-12 11:37:38 +00:00
}
int txt_has_sel(Text *text)
{
return ((text->curl != text->sell) || (text->curc != text->selc));
2002-10-12 11:37:38 +00:00
}
static void txt_delete_sel(Text *text)
2002-10-12 11:37:38 +00:00
{
TextLine *tmpl;
char *buf;
2002-10-12 11:37:38 +00:00
if (!text) return;
if (!text->curl) return;
if (!text->sell) return;
if (!txt_has_sel(text)) return;
txt_order_cursors(text);
if (!undoing) {
buf = txt_sel_to_buf(text);
txt_undo_add_blockop(text, UNDO_DBLOCK, buf);
2002-10-12 11:37:38 +00:00
MEM_freeN(buf);
}
buf = MEM_mallocN(text->curc + (text->sell->len - text->selc) + 1, "textline_string");
2002-10-12 11:37:38 +00:00
strncpy(buf, text->curl->line, text->curc);
strcpy(buf + text->curc, text->sell->line + text->selc);
buf[text->curc + (text->sell->len - text->selc)] = 0;
make_new_line(text->curl, buf);
2002-10-12 11:37:38 +00:00
tmpl = text->sell;
2002-10-12 11:37:38 +00:00
while (tmpl != text->curl) {
tmpl = tmpl->prev;
2002-10-12 11:37:38 +00:00
if (!tmpl) break;
txt_delete_line(text, tmpl->next);
}
text->sell = text->curl;
text->selc = text->curc;
2002-10-12 11:37:38 +00:00
}
void txt_sel_all(Text *text)
2002-10-12 11:37:38 +00:00
{
if (!text) return;
text->curl = text->lines.first;
text->curc = 0;
2002-10-12 11:37:38 +00:00
text->sell = text->lines.last;
text->selc = text->sell->len;
2002-10-12 11:37:38 +00:00
}
void txt_sel_line(Text *text)
2002-10-12 11:37:38 +00:00
{
if (!text) return;
if (!text->curl) return;
text->curc = 0;
text->sell = text->curl;
text->selc = text->sell->len;
2002-10-12 11:37:38 +00:00
}
/***************************/
/* Cut and paste functions */
/***************************/
char *txt_to_buf(Text *text)
2002-10-12 11:37:38 +00:00
{
int length;
TextLine *tmp, *linef, *linel;
int charf, charl;
char *buf;
if (!text) return NULL;
if (!text->curl) return NULL;
if (!text->sell) return NULL;
if (!text->lines.first) return NULL;
linef = text->lines.first;
charf = 0;
2002-10-12 11:37:38 +00:00
linel = text->lines.last;
charl = linel->len;
2002-10-12 11:37:38 +00:00
if (linef == text->lines.last) {
length = charl - charf;
2002-10-12 11:37:38 +00:00
buf = MEM_mallocN(length + 2, "text buffer");
2002-10-12 11:37:38 +00:00
BLI_strncpy(buf, linef->line + charf, length + 1);
buf[length] = 0;
}
else {
length = linef->len - charf;
length += charl;
length += 2; /* For the 2 '\n' */
2002-10-12 11:37:38 +00:00
tmp = linef->next;
while (tmp && tmp != linel) {
length += tmp->len + 1;
tmp = tmp->next;
2002-10-12 11:37:38 +00:00
}
buf = MEM_mallocN(length + 1, "cut buffer");
2002-10-12 11:37:38 +00:00
strncpy(buf, linef->line + charf, linef->len - charf);
length = linef->len - charf;
2002-10-12 11:37:38 +00:00
buf[length++] = '\n';
2002-10-12 11:37:38 +00:00
tmp = linef->next;
while (tmp && tmp != linel) {
strncpy(buf + length, tmp->line, tmp->len);
length += tmp->len;
2002-10-12 11:37:38 +00:00
buf[length++] = '\n';
2002-10-12 11:37:38 +00:00
tmp = tmp->next;
2002-10-12 11:37:38 +00:00
}
strncpy(buf + length, linel->line, charl);
length += charl;
2002-10-12 11:37:38 +00:00
/* python compiler wants an empty end line */
buf[length++] = '\n';
buf[length] = 0;
2002-10-12 11:37:38 +00:00
}
return buf;
}
int txt_find_string(Text *text, const char *findstr, int wrap, int match_case)
2002-10-12 11:37:38 +00:00
{
TextLine *tl, *startl;
char *s = NULL;
2002-10-12 11:37:38 +00:00
if (!text || !text->curl || !text->sell) return 0;
txt_order_cursors(text);
tl = startl = text->sell;
2002-10-12 11:37:38 +00:00
if (match_case) s = strstr(&tl->line[text->selc], findstr);
else s = BLI_strcasestr(&tl->line[text->selc], findstr);
2002-10-12 11:37:38 +00:00
while (!s) {
tl = tl->next;
if (!tl) {
if (wrap)
tl = text->lines.first;
else
break;
}
2002-10-12 11:37:38 +00:00
if (match_case) s = strstr(tl->line, findstr);
else s = BLI_strcasestr(tl->line, findstr);
if (tl == startl)
2002-10-12 11:37:38 +00:00
break;
}
if (s) {
int newl = txt_get_span(text->lines.first, tl);
int newc = (int)(s - tl->line);
txt_move_to(text, newl, newc, 0);
txt_move_to(text, newl, newc + strlen(findstr), 1);
return 1;
}
else
2002-10-12 11:37:38 +00:00
return 0;
}
char *txt_sel_to_buf(Text *text)
2002-10-12 11:37:38 +00:00
{
char *buf;
int length = 0;
2002-10-12 11:37:38 +00:00
TextLine *tmp, *linef, *linel;
int charf, charl;
if (!text) return NULL;
if (!text->curl) return NULL;
if (!text->sell) return NULL;
if (text->curl == text->sell) {
linef = linel = text->curl;
2002-10-12 11:37:38 +00:00
if (text->curc < text->selc) {
charf = text->curc;
charl = text->selc;
}
else {
charf = text->selc;
charl = text->curc;
2002-10-12 11:37:38 +00:00
}
}
else if (txt_get_span(text->curl, text->sell) < 0) {
linef = text->sell;
linel = text->curl;
2002-10-12 11:37:38 +00:00
charf = text->selc;
charl = text->curc;
}
else {
linef = text->curl;
linel = text->sell;
2002-10-12 11:37:38 +00:00
charf = text->curc;
charl = text->selc;
2002-10-12 11:37:38 +00:00
}
if (linef == linel) {
length = charl - charf;
2002-10-12 11:37:38 +00:00
buf = MEM_mallocN(length + 1, "sel buffer");
2002-10-12 11:37:38 +00:00
BLI_strncpy(buf, linef->line + charf, length + 1);
}
else {
length += linef->len - charf;
length += charl;
2002-10-12 11:37:38 +00:00
length++; /* For the '\n' */
tmp = linef->next;
while (tmp && tmp != linel) {
length += tmp->len + 1;
tmp = tmp->next;
2002-10-12 11:37:38 +00:00
}
buf = MEM_mallocN(length + 1, "sel buffer");
2002-10-12 11:37:38 +00:00
strncpy(buf, linef->line + charf, linef->len - charf);
length = linef->len - charf;
2002-10-12 11:37:38 +00:00
buf[length++] = '\n';
2002-10-12 11:37:38 +00:00
tmp = linef->next;
while (tmp && tmp != linel) {
strncpy(buf + length, tmp->line, tmp->len);
length += tmp->len;
2002-10-12 11:37:38 +00:00
buf[length++] = '\n';
2002-10-12 11:37:38 +00:00
tmp = tmp->next;
2002-10-12 11:37:38 +00:00
}
strncpy(buf + length, linel->line, charl);
length += charl;
2002-10-12 11:37:38 +00:00
buf[length] = 0;
}
2002-10-12 11:37:38 +00:00
return buf;
}
void txt_insert_buf(Text *text, const char *in_buffer)
2002-10-12 11:37:38 +00:00
{
int l = 0, u, len;
size_t i = 0, j;
2002-10-12 11:37:38 +00:00
TextLine *add;
char *buffer;
2002-10-12 11:37:38 +00:00
if (!text) return;
if (!in_buffer) return;
txt_delete_sel(text);
len = strlen(in_buffer);
buffer = BLI_strdupn(in_buffer, len);
len += txt_extended_ascii_as_utf8(&buffer);
if (!undoing) txt_undo_add_blockop(text, UNDO_IBLOCK, buffer);
2002-10-12 11:37:38 +00:00
u = undoing;
undoing = 1;
2002-10-12 11:37:38 +00:00
/* Read the first line (or as close as possible */
while (buffer[i] && buffer[i] != '\n')
txt_add_raw_char(text, BLI_str_utf8_as_unicode_step(buffer, &i));
2002-10-12 11:37:38 +00:00
if (buffer[i] == '\n') txt_split_curline(text);
else { undoing = u; MEM_freeN(buffer); return; }
2002-10-12 11:37:38 +00:00
i++;
while (i < len) {
l = 0;
2002-10-12 11:37:38 +00:00
while (buffer[i] && buffer[i] != '\n') {
2002-10-12 11:37:38 +00:00
i++; l++;
}
if (buffer[i] == '\n') {
add = txt_new_linen(buffer + (i - l), l);
2002-10-12 11:37:38 +00:00
BLI_insertlinkbefore(&text->lines, text->curl, add);
i++;
}
else {
for (j = i - l; j < i && j < len; )
txt_add_raw_char(text, BLI_str_utf8_as_unicode_step(buffer, &j));
2002-10-12 11:37:38 +00:00
break;
}
}
MEM_freeN(buffer);
undoing = u;
2002-10-12 11:37:38 +00:00
}
/******************/
/* Undo functions */
/******************/
static int max_undo_test(Text *text, int x)
{
while (text->undo_pos + x >= text->undo_len) {
if (text->undo_len * 2 > TXT_MAX_UNDO) {
/* XXX error("Undo limit reached, buffer cleared\n"); */
MEM_freeN(text->undo_buf);
init_undo_text(text);
return 0;
}
else {
void *tmp = text->undo_buf;
text->undo_buf = MEM_callocN(text->undo_len * 2, "undo buf");
memcpy(text->undo_buf, tmp, text->undo_len);
text->undo_len *= 2;
MEM_freeN(tmp);
}
2002-10-12 11:37:38 +00:00
}
return 1;
}
static void dump_buffer(Text *text)
{
int i = 0;
2002-10-12 11:37:38 +00:00
while (i++ < text->undo_pos) printf("%d: %d %c\n", i, text->undo_buf[i], text->undo_buf[i]);
2002-10-12 11:37:38 +00:00
}
/* Note: this function is outdated and must be updated if needed for future use */
2002-10-12 11:37:38 +00:00
void txt_print_undo(Text *text)
{
int i = 0;
2002-10-12 11:37:38 +00:00
int op;
const char *ops;
2002-10-12 11:37:38 +00:00
int linep, charp;
dump_buffer(text);
printf("---< Undo Buffer >---\n");
2002-10-12 11:37:38 +00:00
printf("UndoPosition is %d\n", text->undo_pos);
2002-10-12 11:37:38 +00:00
while (i <= text->undo_pos) {
op = text->undo_buf[i];
2002-10-12 11:37:38 +00:00
if (op == UNDO_INSERT_1) {
ops = "Insert ascii ";
}
else if (op == UNDO_INSERT_2) {
ops = "Insert 2 bytes ";
}
else if (op == UNDO_INSERT_3) {
ops = "Insert 3 bytes ";
}
else if (op == UNDO_INSERT_4) {
ops = "Insert unicode ";
}
else if (op == UNDO_BS_1) {
ops = "Backspace for ascii ";
}
else if (op == UNDO_BS_2) {
ops = "Backspace for 2 bytes ";
}
else if (op == UNDO_BS_3) {
ops = "Backspace for 3 bytes ";
}
else if (op == UNDO_BS_4) {
ops = "Backspace for unicode ";
}
else if (op == UNDO_DEL_1) {
ops = "Delete ascii ";
}
else if (op == UNDO_DEL_2) {
ops = "Delete 2 bytes ";
}
else if (op == UNDO_DEL_3) {
ops = "Delete 3 bytes ";
}
else if (op == UNDO_DEL_4) {
ops = "Delete unicode ";
}
else if (op == UNDO_DBLOCK) {
ops = "Delete text block";
}
else if (op == UNDO_IBLOCK) {
ops = "Insert text block";
}
else if (op == UNDO_INDENT) {
ops = "Indent ";
}
else if (op == UNDO_UNINDENT) {
ops = "Unindent ";
}
else if (op == UNDO_COMMENT) {
ops = "Comment ";
}
else if (op == UNDO_UNCOMMENT) {
ops = "Uncomment ";
}
else {
ops = "Unknown";
2002-10-12 11:37:38 +00:00
}
printf("Op (%o) at %d = %s", op, i, ops);
if (op >= UNDO_INSERT_1 && op <= UNDO_DEL_4) {
2002-10-12 11:37:38 +00:00
i++;
printf(" - Char is ");
switch (op) {
case UNDO_INSERT_1: case UNDO_BS_1: case UNDO_DEL_1:
printf("%c", text->undo_buf[i]);
i++;
break;
case UNDO_INSERT_2: case UNDO_BS_2: case UNDO_DEL_2:
printf("%c%c", text->undo_buf[i], text->undo_buf[i + 1]);
i += 2;
break;
case UNDO_INSERT_3: case UNDO_BS_3: case UNDO_DEL_3:
printf("%c%c%c", text->undo_buf[i], text->undo_buf[i + 1], text->undo_buf[i + 2]);
i += 3;
break;
2012-09-08 08:59:47 +00:00
case UNDO_INSERT_4: case UNDO_BS_4: case UNDO_DEL_4:
{
unsigned int uc;
char c[BLI_UTF8_MAX + 1];
size_t c_len;
uc = text->undo_buf[i]; i++;
uc = uc + (text->undo_buf[i] << 8); i++;
uc = uc + (text->undo_buf[i] << 16); i++;
uc = uc + (text->undo_buf[i] << 24); i++;
c_len = BLI_str_utf8_from_unicode(uc, c);
c[c_len] = '\0';
puts(c);
}
}
}
else if (op == UNDO_DBLOCK || op == UNDO_IBLOCK) {
2002-10-12 11:37:38 +00:00
i++;
linep = text->undo_buf[i]; i++;
linep = linep + (text->undo_buf[i] << 8); i++;
linep = linep + (text->undo_buf[i] << 16); i++;
linep = linep + (text->undo_buf[i] << 24); i++;
2002-10-12 11:37:38 +00:00
printf(" (length %d) <", linep);
2002-10-12 11:37:38 +00:00
while (linep > 0) {
2002-10-12 11:37:38 +00:00
putchar(text->undo_buf[i]);
linep--; i++;
}
linep = text->undo_buf[i]; i++;
linep = linep + (text->undo_buf[i] << 8); i++;
linep = linep + (text->undo_buf[i] << 16); i++;
linep = linep + (text->undo_buf[i] << 24); i++;
printf("> (%d)", linep);
}
else if (op == UNDO_INDENT || op == UNDO_UNINDENT) {
i++;
charp = text->undo_buf[i]; i++;
charp = charp + (text->undo_buf[i] << 8); i++;
linep = text->undo_buf[i]; i++;
linep = linep + (text->undo_buf[i] << 8); i++;
linep = linep + (text->undo_buf[i] << 16); i++;
linep = linep + (text->undo_buf[i] << 24); i++;
printf("to <%d, %d> ", linep, charp);
charp = text->undo_buf[i]; i++;
charp = charp + (text->undo_buf[i] << 8); i++;
linep = text->undo_buf[i]; i++;
linep = linep + (text->undo_buf[i] << 8); i++;
linep = linep + (text->undo_buf[i] << 16); i++;
linep = linep + (text->undo_buf[i] << 24); i++;
printf("from <%d, %d>", linep, charp);
2002-10-12 11:37:38 +00:00
}
printf(" %d\n", i);
2002-10-12 11:37:38 +00:00
i++;
}
}
static void txt_undo_store_uint16(char *undo_buf, int *undo_pos, unsigned short value)
{
undo_buf[*undo_pos] = (value) & 0xff;
(*undo_pos)++;
undo_buf[*undo_pos] = (value >> 8) & 0xff;
(*undo_pos)++;
}
static void txt_undo_store_uint32(char *undo_buf, int *undo_pos, unsigned int value)
{
undo_buf[*undo_pos] = (value) & 0xff;
(*undo_pos)++;
undo_buf[*undo_pos] = (value >> 8) & 0xff;
(*undo_pos)++;
undo_buf[*undo_pos] = (value >> 16) & 0xff;
(*undo_pos)++;
undo_buf[*undo_pos] = (value >> 24) & 0xff;
(*undo_pos)++;
}
/* store the cur cursor to the undo buffer */
static void txt_undo_store_cur(Text *text)
{
txt_undo_store_uint16(text->undo_buf, &text->undo_pos, text->curc);
txt_undo_store_uint32(text->undo_buf, &text->undo_pos, txt_get_span(text->lines.first, text->curl));
}
/* store the sel cursor to the undo buffer */
static void txt_undo_store_sel(Text *text)
{
txt_undo_store_uint16(text->undo_buf, &text->undo_pos, text->selc);
txt_undo_store_uint32(text->undo_buf, &text->undo_pos, txt_get_span(text->lines.first, text->sell));
}
/* store both cursors to the undo buffer */
static void txt_undo_store_cursors(Text *text)
{
txt_undo_store_cur(text);
txt_undo_store_sel(text);
}
/* store an operator along with a block of data */
static void txt_undo_add_blockop(Text *text, int op, const char *buf)
2002-10-12 11:37:38 +00:00
{
unsigned int length = strlen(buf);
2002-10-12 11:37:38 +00:00
if (!max_undo_test(text, length + 11 + 12))
return;
2002-10-12 11:37:38 +00:00
text->undo_pos++;
text->undo_buf[text->undo_pos] = op;
2002-10-12 11:37:38 +00:00
text->undo_pos++;
txt_undo_store_cursors(text);
txt_undo_store_uint32(text->undo_buf, &text->undo_pos, length);
strncpy(text->undo_buf + text->undo_pos, buf, length);
text->undo_pos += length;
2002-10-12 11:37:38 +00:00
txt_undo_store_uint32(text->undo_buf, &text->undo_pos, length);
text->undo_buf[text->undo_pos] = op;
2002-10-12 11:37:38 +00:00
text->undo_buf[text->undo_pos + 1] = 0;
2002-10-12 11:37:38 +00:00
}
/* store a regular operator */
void txt_undo_add_op(Text *text, int op)
2002-10-12 11:37:38 +00:00
{
if (!max_undo_test(text, 15))
return;
2002-10-12 11:37:38 +00:00
text->undo_pos++;
text->undo_buf[text->undo_pos] = op;
2002-10-12 11:37:38 +00:00
text->undo_pos++;
txt_undo_store_cursors(text);
text->undo_buf[text->undo_pos] = op;
text->undo_buf[text->undo_pos + 1] = 0;
}
2002-10-12 11:37:38 +00:00
/* store an operator for a single character */
static void txt_undo_add_charop(Text *text, int op_start, unsigned int c)
{
char utf8[BLI_UTF8_MAX];
size_t i, utf8_size = BLI_str_utf8_from_unicode(c, utf8);
if (!max_undo_test(text, 3 + utf8_size + 12))
return;
2002-10-12 11:37:38 +00:00
text->undo_pos++;
if (utf8_size < 4) {
text->undo_buf[text->undo_pos] = op_start + utf8_size - 1;
text->undo_pos++;
txt_undo_store_cur(text);
for (i = 0; i < utf8_size; i++) {
text->undo_buf[text->undo_pos] = utf8[i];
text->undo_pos++;
}
text->undo_buf[text->undo_pos] = op_start + utf8_size - 1;
}
else {
text->undo_buf[text->undo_pos] = op_start + 3;
text->undo_pos++;
txt_undo_store_cursors(text);
txt_undo_store_uint32(text->undo_buf, &text->undo_pos, c);
text->undo_buf[text->undo_pos] = op_start + 3;
}
text->undo_buf[text->undo_pos + 1] = 0;
}
2002-10-12 11:37:38 +00:00
static unsigned short txt_undo_read_uint16(const char *undo_buf, int *undo_pos)
{
unsigned short val;
val = undo_buf[*undo_pos]; (*undo_pos)--;
val = (val << 8) + undo_buf[*undo_pos]; (*undo_pos)--;
return val;
}
2002-10-12 11:37:38 +00:00
static unsigned int txt_undo_read_uint32(const char *undo_buf, int *undo_pos)
{
unsigned int val;
val = undo_buf[*undo_pos]; (*undo_pos)--;
val = (val << 8) + undo_buf[*undo_pos]; (*undo_pos)--;
val = (val << 8) + undo_buf[*undo_pos]; (*undo_pos)--;
val = (val << 8) + undo_buf[*undo_pos]; (*undo_pos)--;
return val;
}
2002-10-12 11:37:38 +00:00
/* read the cur cursor from the undo buffer */
static void txt_undo_read_cur(const char *undo_buf, int *undo_pos, unsigned int *curln, unsigned short *curc)
{
*curln = txt_undo_read_uint32(undo_buf, undo_pos);
*curc = txt_undo_read_uint16(undo_buf, undo_pos);
}
/* read the sel cursor from the undo buffer */
static void txt_undo_read_sel(const char *undo_buf, int *undo_pos, unsigned int *selln, unsigned short *selc)
{
*selln = txt_undo_read_uint32(undo_buf, undo_pos);
*selc = txt_undo_read_uint16(undo_buf, undo_pos);
}
/* read both cursors from the undo buffer */
static void txt_undo_read_cursors(const char *undo_buf, int *undo_pos,
unsigned int *curln, unsigned short *curc,
unsigned int *selln, unsigned short *selc)
{
txt_undo_read_sel(undo_buf, undo_pos, selln, selc);
txt_undo_read_cur(undo_buf, undo_pos, curln, curc);
}
static unsigned int txt_undo_read_unicode(const char *undo_buf, int *undo_pos, short bytes)
{
unsigned int unicode;
char utf8[BLI_UTF8_MAX + 1];
switch (bytes) {
case 1: /* ascii */
unicode = undo_buf[*undo_pos]; (*undo_pos)--;
break;
case 2: /* 2-byte symbol */
utf8[2] = '\0';
utf8[1] = undo_buf[*undo_pos]; (*undo_pos)--;
utf8[0] = undo_buf[*undo_pos]; (*undo_pos)--;
unicode = BLI_str_utf8_as_unicode(utf8);
break;
case 3: /* 3-byte symbol */
utf8[3] = '\0';
utf8[2] = undo_buf[*undo_pos]; (*undo_pos)--;
utf8[1] = undo_buf[*undo_pos]; (*undo_pos)--;
utf8[0] = undo_buf[*undo_pos]; (*undo_pos)--;
unicode = BLI_str_utf8_as_unicode(utf8);
break;
case 4: /* 32-bit unicode symbol */
unicode = txt_undo_read_uint32(undo_buf, undo_pos);
break;
2012-01-22 17:26:56 +00:00
default:
/* should never happen */
BLI_assert(0);
unicode = 0;
}
return unicode;
2002-10-12 11:37:38 +00:00
}
static unsigned short txt_redo_read_uint16(const char *undo_buf, int *undo_pos)
2002-10-12 11:37:38 +00:00
{
unsigned short val;
val = undo_buf[*undo_pos]; (*undo_pos)++;
val = val + (undo_buf[*undo_pos] << 8); (*undo_pos)++;
return val;
}
2002-10-12 11:37:38 +00:00
static unsigned int txt_redo_read_uint32(const char *undo_buf, int *undo_pos)
{
unsigned int val;
val = undo_buf[*undo_pos]; (*undo_pos)++;
val = val + (undo_buf[*undo_pos] << 8); (*undo_pos)++;
val = val + (undo_buf[*undo_pos] << 16); (*undo_pos)++;
val = val + (undo_buf[*undo_pos] << 24); (*undo_pos)++;
return val;
}
/* redo read cur cursor from the undo buffer */
static void txt_redo_read_cur(const char *undo_buf, int *undo_pos, unsigned int *curln, unsigned short *curc)
{
*curc = txt_redo_read_uint16(undo_buf, undo_pos);
*curln = txt_redo_read_uint32(undo_buf, undo_pos);
}
/* redo read sel cursor from the undo buffer */
static void txt_redo_read_sel(const char *undo_buf, int *undo_pos, unsigned int *selln, unsigned short *selc)
{
*selc = txt_redo_read_uint16(undo_buf, undo_pos);
*selln = txt_redo_read_uint32(undo_buf, undo_pos);
}
/* redo read both cursors from the undo buffer */
static void txt_redo_read_cursors(const char *undo_buf, int *undo_pos,
unsigned int *curln, unsigned short *curc,
unsigned int *selln, unsigned short *selc)
{
txt_redo_read_cur(undo_buf, undo_pos, curln, curc);
txt_redo_read_sel(undo_buf, undo_pos, selln, selc);
}
static unsigned int txt_redo_read_unicode(const char *undo_buf, int *undo_pos, short bytes)
{
unsigned int unicode;
char utf8[BLI_UTF8_MAX + 1];
switch (bytes) {
case 1: /* ascii */
unicode = undo_buf[*undo_pos]; (*undo_pos)++;
break;
case 2: /* 2-byte symbol */
utf8[0] = undo_buf[*undo_pos]; (*undo_pos)++;
utf8[1] = undo_buf[*undo_pos]; (*undo_pos)++;
utf8[2] = '\0';
unicode = BLI_str_utf8_as_unicode(utf8);
break;
case 3: /* 3-byte symbol */
utf8[0] = undo_buf[*undo_pos]; (*undo_pos)++;
utf8[1] = undo_buf[*undo_pos]; (*undo_pos)++;
utf8[2] = undo_buf[*undo_pos]; (*undo_pos)++;
utf8[3] = '\0';
unicode = BLI_str_utf8_as_unicode(utf8);
break;
case 4: /* 32-bit unicode symbol */
unicode = txt_undo_read_uint32(undo_buf, undo_pos);
break;
default:
/* should never happen */
BLI_assert(0);
unicode = 0;
}
return unicode;
2002-10-12 11:37:38 +00:00
}
void txt_do_undo(Text *text)
{
int op = text->undo_buf[text->undo_pos];
unsigned int linep, i;
unsigned int uchar;
unsigned int curln, selln;
unsigned short curc, selc;
2002-10-12 11:37:38 +00:00
unsigned short charp;
char *buf;
if (text->undo_pos < 0) {
2002-10-12 11:37:38 +00:00
return;
}
text->undo_pos--;
undoing = 1;
2002-10-12 11:37:38 +00:00
switch (op) {
case UNDO_INSERT_1:
case UNDO_INSERT_2:
case UNDO_INSERT_3:
case UNDO_INSERT_4:
text->undo_pos -= op - UNDO_INSERT_1 + 1;
2002-10-12 11:37:38 +00:00
/* get and restore the cursors */
txt_undo_read_cur(text->undo_buf, &text->undo_pos, &curln, &curc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, curln, curc, 1);
2002-10-12 11:37:38 +00:00
txt_delete_char(text);
2002-10-12 11:37:38 +00:00
text->undo_pos--;
2002-10-12 11:37:38 +00:00
break;
case UNDO_BS_1:
case UNDO_BS_2:
case UNDO_BS_3:
case UNDO_BS_4:
charp = op - UNDO_BS_1 + 1;
uchar = txt_undo_read_unicode(text->undo_buf, &text->undo_pos, charp);
2002-10-12 11:37:38 +00:00
/* get and restore the cursors */
txt_undo_read_cur(text->undo_buf, &text->undo_pos, &curln, &curc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, curln, curc, 1);
2002-10-12 11:37:38 +00:00
txt_add_char(text, uchar);
2002-10-12 11:37:38 +00:00
text->undo_pos--;
break;
case UNDO_DEL_1:
case UNDO_DEL_2:
case UNDO_DEL_3:
case UNDO_DEL_4:
charp = op - UNDO_DEL_1 + 1;
uchar = txt_undo_read_unicode(text->undo_buf, &text->undo_pos, charp);
/* get and restore the cursors */
txt_undo_read_cur(text->undo_buf, &text->undo_pos, &curln, &curc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, curln, curc, 1);
txt_add_char(text, uchar);
2002-10-12 11:37:38 +00:00
txt_move_left(text, 0);
text->undo_pos--;
2002-10-12 11:37:38 +00:00
break;
case UNDO_DBLOCK:
/* length of the string in the buffer */
linep = txt_undo_read_uint32(text->undo_buf, &text->undo_pos);
2002-10-12 11:37:38 +00:00
buf = MEM_mallocN(linep + 1, "dblock buffer");
for (i = 0; i < linep; i++) {
buf[(linep - 1) - i] = text->undo_buf[text->undo_pos];
2002-10-12 11:37:38 +00:00
text->undo_pos--;
}
buf[i] = 0;
2002-10-12 11:37:38 +00:00
/* skip over the length that was stored again */
text->undo_pos--;
text->undo_pos--;
text->undo_pos--;
text->undo_pos--;
2002-10-12 11:37:38 +00:00
/* Get the cursor positions */
txt_undo_read_cursors(text->undo_buf, &text->undo_pos, &curln, &curc, &selln, &selc);
/* move cur to location that needs buff inserted */
txt_move_to(text, curln, curc, 0);
txt_insert_buf(text, buf);
MEM_freeN(buf);
/* restore the cursors */
txt_move_to(text, curln, curc, 0);
txt_move_to(text, selln, selc, 1);
2002-10-12 11:37:38 +00:00
text->undo_pos--;
break;
case UNDO_IBLOCK:
/* length of the string in the buffer */
linep = txt_undo_read_uint32(text->undo_buf, &text->undo_pos);
/* txt_backspace_char removes utf8-characters, not bytes */
buf = MEM_mallocN(linep + 1, "iblock buffer");
for (i = 0; i < linep; i++) {
buf[(linep - 1) - i] = text->undo_buf[text->undo_pos];
text->undo_pos--;
}
buf[i] = 0;
linep = txt_utf8_len(buf);
MEM_freeN(buf);
/* skip over the length that was stored again */
2002-10-12 11:37:38 +00:00
text->undo_pos--;
text->undo_pos--;
text->undo_pos--;
text->undo_pos--;
/* get and restore the cursors */
txt_undo_read_cursors(text->undo_buf, &text->undo_pos, &curln, &curc, &selln, &selc);
2012-11-30 22:46:28 +00:00
txt_move_to(text, curln, curc, 0);
2012-11-30 22:46:28 +00:00
txt_move_to(text, selln, selc, 1);
if ((curln == selln) && (curc == selc)) {
for (i = 0; i < linep; i++)
txt_move_right(text, 1);
}
txt_delete_selected(text);
text->undo_pos--;
2002-10-12 11:37:38 +00:00
break;
case UNDO_INDENT:
case UNDO_UNINDENT:
case UNDO_COMMENT:
case UNDO_UNCOMMENT:
case UNDO_DUPLICATE:
case UNDO_MOVE_LINES_UP:
case UNDO_MOVE_LINES_DOWN:
/* get and restore the cursors */
txt_undo_read_cursors(text->undo_buf, &text->undo_pos, &curln, &curc, &selln, &selc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, selln, selc, 1);
2002-10-12 11:37:38 +00:00
if (op == UNDO_INDENT) {
txt_unindent(text);
}
else if (op == UNDO_UNINDENT) {
txt_indent(text);
}
else if (op == UNDO_COMMENT) {
txt_uncomment(text);
}
else if (op == UNDO_UNCOMMENT) {
txt_comment(text);
}
else if (op == UNDO_DUPLICATE) {
txt_delete_line(text, text->curl->next);
}
else if (op == UNDO_MOVE_LINES_UP) {
txt_move_lines(text, TXT_MOVE_LINE_DOWN);
}
else if (op == UNDO_MOVE_LINES_DOWN) {
txt_move_lines(text, TXT_MOVE_LINE_UP);
}
text->undo_pos--;
break;
2002-10-12 11:37:38 +00:00
default:
//XXX error("Undo buffer error - resetting");
text->undo_pos = -1;
2002-10-12 11:37:38 +00:00
break;
}
undoing = 0;
2002-10-12 11:37:38 +00:00
}
void txt_do_redo(Text *text)
{
char op;
char *buf;
unsigned int linep;
unsigned short charp;
unsigned int uchar;
unsigned int curln, selln;
unsigned short curc, selc;
2002-10-12 11:37:38 +00:00
text->undo_pos++;
op = text->undo_buf[text->undo_pos];
2002-10-12 11:37:38 +00:00
if (!op) {
text->undo_pos--;
return;
}
undoing = 1;
switch (op) {
case UNDO_INSERT_1:
case UNDO_INSERT_2:
case UNDO_INSERT_3:
case UNDO_INSERT_4:
text->undo_pos++;
2002-10-12 11:37:38 +00:00
/* get and restore the cursors */
txt_redo_read_cur(text->undo_buf, &text->undo_pos, &curln, &curc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, curln, curc, 1);
2002-10-12 11:37:38 +00:00
charp = op - UNDO_INSERT_1 + 1;
uchar = txt_redo_read_unicode(text->undo_buf, &text->undo_pos, charp);
2002-10-12 11:37:38 +00:00
txt_add_char(text, uchar);
2002-10-12 11:37:38 +00:00
break;
case UNDO_BS_1:
case UNDO_BS_2:
case UNDO_BS_3:
case UNDO_BS_4:
2002-10-12 11:37:38 +00:00
text->undo_pos++;
/* get and restore the cursors */
txt_redo_read_cur(text->undo_buf, &text->undo_pos, &curln, &curc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, curln, curc, 1);
text->undo_pos += op - UNDO_BS_1 + 1;
2002-10-12 11:37:38 +00:00
/* move right so we backspace the correct char */
txt_move_right(text, 0);
txt_backspace_char(text);
2002-10-12 11:37:38 +00:00
break;
2002-10-12 11:37:38 +00:00
case UNDO_DEL_1:
case UNDO_DEL_2:
case UNDO_DEL_3:
case UNDO_DEL_4:
2002-10-12 11:37:38 +00:00
text->undo_pos++;
/* get and restore the cursors */
txt_redo_read_cur(text->undo_buf, &text->undo_pos, &curln, &curc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, curln, curc, 1);
2002-10-12 11:37:38 +00:00
text->undo_pos += op - UNDO_DEL_1 + 1;
txt_delete_char(text);
2002-10-12 11:37:38 +00:00
break;
case UNDO_DBLOCK:
text->undo_pos++;
/* get and restore the cursors */
txt_redo_read_cursors(text->undo_buf, &text->undo_pos, &curln, &curc, &selln, &selc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, selln, selc, 1);
/* length of the block */
linep = txt_redo_read_uint32(text->undo_buf, &text->undo_pos);
text->undo_pos += linep;
2002-10-12 11:37:38 +00:00
/* skip over the length that was stored again */
2002-10-12 11:37:38 +00:00
text->undo_pos++;
text->undo_pos++;
text->undo_pos++;
text->undo_pos++;
txt_delete_sel(text);
2002-10-12 11:37:38 +00:00
break;
case UNDO_IBLOCK:
text->undo_pos++;
/* get and restore the cursors */
txt_redo_read_cursors(text->undo_buf, &text->undo_pos, &curln, &curc, &selln, &selc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, curln, curc, 1);
/* length of the block */
linep = txt_redo_read_uint32(text->undo_buf, &text->undo_pos);
2002-10-12 11:37:38 +00:00
buf = MEM_mallocN(linep + 1, "iblock buffer");
memcpy(buf, &text->undo_buf[text->undo_pos], linep);
text->undo_pos += linep;
buf[linep] = 0;
2002-10-12 11:37:38 +00:00
txt_insert_buf(text, buf);
2002-10-12 11:37:38 +00:00
MEM_freeN(buf);
/* skip over the length that was stored again */
text->undo_pos++;
text->undo_pos++;
text->undo_pos++;
text->undo_pos++;
2002-10-12 11:37:38 +00:00
break;
case UNDO_INDENT:
case UNDO_UNINDENT:
case UNDO_COMMENT:
case UNDO_UNCOMMENT:
case UNDO_DUPLICATE:
case UNDO_MOVE_LINES_UP:
case UNDO_MOVE_LINES_DOWN:
text->undo_pos++;
/* get and restore the cursors */
txt_redo_read_cursors(text->undo_buf, &text->undo_pos, &curln, &curc, &selln, &selc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, selln, selc, 1);
2002-10-12 11:37:38 +00:00
if (op == UNDO_INDENT) {
txt_indent(text);
}
else if (op == UNDO_UNINDENT) {
txt_unindent(text);
}
else if (op == UNDO_COMMENT) {
txt_comment(text);
}
else if (op == UNDO_UNCOMMENT) {
txt_uncomment(text);
}
else if (op == UNDO_DUPLICATE) {
txt_duplicate_line(text);
}
else if (op == UNDO_MOVE_LINES_UP) {
/* offset the cursor by + 1 */
txt_move_to(text, curln + 1, curc, 0);
txt_move_to(text, selln + 1, selc, 1);
txt_move_lines(text, TXT_MOVE_LINE_UP);
}
else if (op == UNDO_MOVE_LINES_DOWN) {
/* offset the cursor by - 1 */
txt_move_to(text, curln - 1, curc, 0);
txt_move_to(text, selln - 1, selc, 1);
txt_move_lines(text, TXT_MOVE_LINE_DOWN);
}
/* re-restore the cursors since they got moved when redoing */
txt_move_to(text, curln, curc, 0);
txt_move_to(text, selln, selc, 1);
break;
2002-10-12 11:37:38 +00:00
default:
//XXX error("Undo buffer error - resetting");
text->undo_pos = -1;
2002-10-12 11:37:38 +00:00
break;
}
undoing = 0;
2002-10-12 11:37:38 +00:00
}
/**************************/
/* Line editing functions */
/**************************/
void txt_split_curline(Text *text)
{
2002-10-12 11:37:38 +00:00
TextLine *ins;
char *left, *right;
2002-10-12 11:37:38 +00:00
if (!text) return;
if (!text->curl) return;
txt_delete_sel(text);
2012-11-30 22:46:28 +00:00
if (!undoing) txt_undo_add_charop(text, UNDO_INSERT_1, '\n');
2002-10-12 11:37:38 +00:00
/* Make the two half strings */
left = MEM_mallocN(text->curc + 1, "textline_string");
2002-10-12 11:37:38 +00:00
if (text->curc) memcpy(left, text->curl->line, text->curc);
left[text->curc] = 0;
2002-10-12 11:37:38 +00:00
right = MEM_mallocN(text->curl->len - text->curc + 1, "textline_string");
memcpy(right, text->curl->line + text->curc, text->curl->len - text->curc + 1);
2002-10-12 11:37:38 +00:00
MEM_freeN(text->curl->line);
if (text->curl->format) MEM_freeN(text->curl->format);
2002-10-12 11:37:38 +00:00
/* Make the new TextLine */
ins = MEM_mallocN(sizeof(TextLine), "textline");
ins->line = left;
ins->format = NULL;
ins->len = text->curc;
text->curl->line = right;
text->curl->format = NULL;
text->curl->len = text->curl->len - text->curc;
2002-10-12 11:37:38 +00:00
BLI_insertlinkbefore(&text->lines, text->curl, ins);
2002-10-12 11:37:38 +00:00
text->curc = 0;
2002-10-12 11:37:38 +00:00
txt_make_dirty(text);
txt_clean_text(text);
txt_pop_sel(text);
}
static void txt_delete_line(Text *text, TextLine *line)
2002-10-12 11:37:38 +00:00
{
if (!text) return;
if (!text->curl) return;
BLI_remlink(&text->lines, line);
2002-10-12 11:37:38 +00:00
if (line->line) MEM_freeN(line->line);
if (line->format) MEM_freeN(line->format);
2002-10-12 11:37:38 +00:00
MEM_freeN(line);
txt_make_dirty(text);
txt_clean_text(text);
}
static void txt_combine_lines(Text *text, TextLine *linea, TextLine *lineb)
2002-10-12 11:37:38 +00:00
{
char *tmp;
2002-10-12 11:37:38 +00:00
if (!text) return;
if (!linea || !lineb) return;
tmp = MEM_mallocN(linea->len + lineb->len + 1, "textline_string");
2002-10-12 11:37:38 +00:00
strcpy(tmp, linea->line);
strcat(tmp, lineb->line);
make_new_line(linea, tmp);
2002-10-12 11:37:38 +00:00
txt_delete_line(text, lineb);
2002-10-12 11:37:38 +00:00
txt_make_dirty(text);
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)
2002-10-12 11:37:38 +00:00
{
unsigned int c = '\n';
2002-10-12 11:37:38 +00:00
if (!text) return;
if (!text->curl) return;
if (txt_has_sel(text)) { /* deleting a selection */
txt_delete_sel(text);
txt_make_dirty(text);
return;
2002-10-12 11:37:38 +00:00
}
else if (text->curc == text->curl->len) { /* Appending two lines */
2002-10-12 11:37:38 +00:00
if (text->curl->next) {
txt_combine_lines(text, text->curl, text->curl->next);
txt_pop_sel(text);
}
else
return;
}
else { /* Just deleting a char */
size_t c_len = 0;
c = BLI_str_utf8_as_unicode_and_size(text->curl->line + text->curc, &c_len);
2002-10-12 11:37:38 +00:00
memmove(text->curl->line + text->curc, text->curl->line + text->curc + c_len, text->curl->len - text->curc - c_len + 1);
text->curl->len -= c_len;
2002-10-12 11:37:38 +00:00
txt_pop_sel(text);
}
txt_make_dirty(text);
txt_clean_text(text);
if (!undoing) txt_undo_add_charop(text, UNDO_DEL_1, c);
2002-10-12 11:37:38 +00:00
}
void txt_delete_word(Text *text)
{
txt_jump_right(text, 1);
txt_delete_sel(text);
}
void txt_backspace_char(Text *text)
{
unsigned int c = '\n';
2002-10-12 11:37:38 +00:00
if (!text) return;
if (!text->curl) return;
if (txt_has_sel(text)) { /* deleting a selection */
txt_delete_sel(text);
txt_make_dirty(text);
return;
2002-10-12 11:37:38 +00:00
}
else if (text->curc == 0) { /* Appending two lines */
if (!text->curl->prev) return;
2002-10-12 11:37:38 +00:00
text->curl = text->curl->prev;
text->curc = text->curl->len;
2002-10-12 11:37:38 +00:00
txt_combine_lines(text, text->curl, text->curl->next);
txt_pop_sel(text);
}
else { /* Just backspacing a char */
size_t c_len = 0;
char *prev = BLI_str_prev_char_utf8(text->curl->line + text->curc);
c = BLI_str_utf8_as_unicode_and_size(prev, &c_len);
2002-10-12 11:37:38 +00:00
/* source and destination overlap, don't use memcpy() */
memmove(text->curl->line + text->curc - c_len,
text->curl->line + text->curc,
text->curl->len - text->curc + 1);
text->curl->len -= c_len;
text->curc -= c_len;
txt_pop_sel(text);
2002-10-12 11:37:38 +00:00
}
txt_make_dirty(text);
txt_clean_text(text);
if (!undoing) txt_undo_add_charop(text, UNDO_BS_1, c);
2002-10-12 11:37:38 +00:00
}
void txt_backspace_word(Text *text)
{
txt_jump_left(text, 1);
txt_delete_sel(text);
}
/* Max spaces to replace a tab with, currently hardcoded to TXT_TABSIZE = 4.
2012-03-01 12:20:18 +00:00
* Used by txt_convert_tab_to_spaces, indent and unindent.
* Remember to change this string according to max tab size */
static char tab_to_spaces[] = " ";
static void txt_convert_tab_to_spaces(Text *text)
{
/* sb aims to pad adjust the tab-width needed so that the right number of spaces
* is added so that the indention of the line is the right width (i.e. aligned
* to multiples of TXT_TABSIZE)
*/
char *sb = &tab_to_spaces[text->curc % TXT_TABSIZE];
txt_insert_buf(text, sb);
}
static int txt_add_char_intern(Text *text, unsigned int add, int replace_tabs)
{
char *tmp, ch[BLI_UTF8_MAX];
size_t add_len;
2002-10-12 11:37:38 +00:00
if (!text) return 0;
if (!text->curl) return 0;
if (add == '\n') {
2002-10-12 11:37:38 +00:00
txt_split_curline(text);
return 1;
}
/* insert spaces rather than tabs */
if (add == '\t' && replace_tabs) {
txt_convert_tab_to_spaces(text);
return 1;
}
2002-10-12 11:37:38 +00:00
txt_delete_sel(text);
if (!undoing) txt_undo_add_charop(text, UNDO_INSERT_1, add);
add_len = BLI_str_utf8_from_unicode(add, ch);
tmp = MEM_mallocN(text->curl->len + add_len + 1, "textline_string");
2002-10-12 11:37:38 +00:00
memcpy(tmp, text->curl->line, text->curc);
memcpy(tmp + text->curc, ch, add_len);
memcpy(tmp + text->curc + add_len, text->curl->line + text->curc, text->curl->len - text->curc + 1);
make_new_line(text->curl, tmp);
2002-10-12 11:37:38 +00:00
text->curc += add_len;
2002-10-12 11:37:38 +00:00
txt_pop_sel(text);
txt_make_dirty(text);
txt_clean_text(text);
return 1;
}
int txt_add_char(Text *text, unsigned int add)
{
return txt_add_char_intern(text, add, text->flags & TXT_TABSTOSPACES);
}
int txt_add_raw_char(Text *text, unsigned int add)
{
return txt_add_char_intern(text, add, 0);
}
2.5: Text Editor back. There was very little structure in this code, using many globals and duplicated code. Now it should be better structured. Most things should work, the main parts that are not back yet are the python plugins and markers. Notes: * Blenfont is used for drawing the text, nicely anti-aliased. * A monospace truetype font was added, since that is needed for the text editor. It's Bitstream Vera Sans Mono. This is the default gnome terminal font, but it doesn't fit entirely well with the other font I think, can be changed easily of course. * Clipboard copy/cut/paste now always uses the system clipboard, the code for the own cut buffer was removed. * The interface buttons should support copy/cut/paste again now as well. * WM_clipboard_text_get/WM_clipboard_text_set were added to the windowmanager code. * Find panel is now a kind of second header, instead of a panel. This needs especially a way to start editing the text field immediately on open still. * Operators are independent of the actual space when possible, was a bit of puzzling but got it solved nice with notifiers, and some lazy init for syntax highlight in the drawing code. * RNA was created for the text editor space and used for buttons. * Operators: * New, Open, Reload, Save, Save As, Make Internal * Run Script, Refresh Pyconstraints * Copy, Cut, Paste * Convert Whitespace, Uncomment, Comment, Indent, Unindent * Line Break, Insert * Next Marker, Previous Marker, Clear All Markers, Mark All * Select Line, Select All * Jump, Move, Move Select, Delete, Toggle Overwrite * Scroll, Scroll Bar, Set Cursor, Line Number * Find and Replace, Find, Replace, Find Set Selected, Replace Set Selected * To 3D Object * Resolve Conflict
2009-02-28 23:33:35 +00:00
void txt_delete_selected(Text *text)
{
txt_delete_sel(text);
txt_make_dirty(text);
}
int txt_replace_char(Text *text, unsigned int add)
{
unsigned int del;
size_t del_size = 0, add_size;
char ch[BLI_UTF8_MAX];
if (!text) return 0;
if (!text->curl) return 0;
/* If text is selected or we're at the end of the line just use txt_add_char */
if (text->curc == text->curl->len || txt_has_sel(text) || add == '\n') {
return txt_add_char(text, add);
}
del = BLI_str_utf8_as_unicode_and_size(text->curl->line + text->curc, &del_size);
add_size = BLI_str_utf8_from_unicode(add, ch);
if (add_size > del_size) {
char *tmp = MEM_mallocN(text->curl->len + add_size - del_size + 1, "textline_string");
memcpy(tmp, text->curl->line, text->curc);
memcpy(tmp + text->curc + add_size, text->curl->line + text->curc + del_size, text->curl->len - text->curc - del_size + 1);
MEM_freeN(text->curl->line);
text->curl->line = tmp;
}
else if (add_size < del_size) {
char *tmp = text->curl->line;
memmove(tmp + text->curc + add_size, tmp + text->curc + del_size, text->curl->len - text->curc - del_size + 1);
}
memcpy(text->curl->line + text->curc, ch, add_size);
text->curc += add_size;
txt_pop_sel(text);
txt_make_dirty(text);
txt_clean_text(text);
/* Should probably create a new op for this */
if (!undoing) {
txt_undo_add_charop(text, UNDO_INSERT_1, add);
txt_undo_add_charop(text, UNDO_DEL_1, del);
}
return 1;
}
void txt_indent(Text *text)
{
int len, num, curc_old;
char *tmp;
const char *add = "\t";
int indentlen = 1;
/* hardcoded: TXT_TABSIZE = 4 spaces: */
int spaceslen = TXT_TABSIZE;
if (ELEM3(NULL, text, text->curl, text->sell)) {
return;
}
if (!text) return;
if (!text->curl) return;
if (!text->sell) return;
/* insert spaces rather than tabs */
if (text->flags & TXT_TABSTOSPACES) {
add = tab_to_spaces;
indentlen = spaceslen;
}
curc_old = text->curc;
num = 0;
while (TRUE) {
tmp = MEM_mallocN(text->curl->len + indentlen + 1, "textline_string");
text->curc = 0;
if (text->curc) memcpy(tmp, text->curl->line, text->curc); /* XXX never true, check prev line */
memcpy(tmp + text->curc, add, indentlen);
len = text->curl->len - text->curc;
if (len > 0) memcpy(tmp + text->curc + indentlen, text->curl->line + text->curc, len);
tmp[text->curl->len + indentlen] = 0;
make_new_line(text->curl, tmp);
text->curc += indentlen;
txt_make_dirty(text);
txt_clean_text(text);
if (text->curl == text->sell) {
text->selc += indentlen;
break;
}
else {
text->curl = text->curl->next;
num++;
}
}
if (!curc_old) text->curc = 0;
else text->curc = curc_old + indentlen;
while (num > 0) {
text->curl = text->curl->prev;
num--;
}
if (!undoing) {
txt_undo_add_op(text, UNDO_INDENT);
}
}
void txt_unindent(Text *text)
{
int num = 0;
const char *remove = "\t";
int indentlen = 1;
int unindented_first = FALSE;
/* hardcoded: TXT_TABSIZE = 4 spaces: */
int spaceslen = TXT_TABSIZE;
if (!text) return;
if (!text->curl) return;
if (!text->sell) return;
/* insert spaces rather than tabs */
if (text->flags & TXT_TABSTOSPACES) {
remove = tab_to_spaces;
indentlen = spaceslen;
}
while (TRUE) {
int i = 0;
if (BLI_strncasecmp(text->curl->line, remove, indentlen) == 0) {
if (num == 0) unindented_first = TRUE;
while (i < text->curl->len) {
text->curl->line[i] = text->curl->line[i + indentlen];
i++;
}
text->curl->len -= indentlen;
}
txt_make_dirty(text);
txt_clean_text(text);
if (text->curl == text->sell) {
if (i > 0) text->selc = MAX2(text->selc - indentlen, 0);
break;
}
else {
text->curl = text->curl->next;
num++;
}
}
if (unindented_first) text->curc = MAX2(text->curc - indentlen, 0);
while (num > 0) {
text->curl = text->curl->prev;
num--;
}
if (!undoing) {
txt_undo_add_op(text, UNDO_UNINDENT);
}
}
void txt_comment(Text *text)
{
int len, num;
char *tmp;
char add = '#';
if (!text) return;
if (!text->curl) return;
if (!text->sell) return; // Need to change this need to check if only one line is selected to more then one
num = 0;
while (TRUE) {
tmp = MEM_mallocN(text->curl->len + 2, "textline_string");
text->curc = 0;
if (text->curc) memcpy(tmp, text->curl->line, text->curc);
tmp[text->curc] = add;
len = text->curl->len - text->curc;
if (len > 0) memcpy(tmp + text->curc + 1, text->curl->line + text->curc, len);
tmp[text->curl->len + 1] = 0;
make_new_line(text->curl, tmp);
text->curc++;
txt_make_dirty(text);
txt_clean_text(text);
if (text->curl == text->sell) {
text->selc = text->sell->len;
break;
}
else {
text->curl = text->curl->next;
num++;
}
}
text->curc = 0;
while (num > 0) {
text->curl = text->curl->prev;
num--;
}
if (!undoing) {
txt_undo_add_op(text, UNDO_COMMENT);
}
}
void txt_uncomment(Text *text)
{
int num = 0;
char remove = '#';
if (!text) return;
if (!text->curl) return;
if (!text->sell) return;
while (TRUE) {
int i = 0;
if (text->curl->line[i] == remove) {
while (i < text->curl->len) {
text->curl->line[i] = text->curl->line[i + 1];
i++;
}
text->curl->len--;
}
txt_make_dirty(text);
txt_clean_text(text);
if (text->curl == text->sell) {
text->selc = text->sell->len;
break;
}
else {
text->curl = text->curl->next;
num++;
}
}
text->curc = 0;
while (num > 0) {
text->curl = text->curl->prev;
num--;
}
if (!undoing) {
txt_undo_add_op(text, UNDO_UNCOMMENT);
}
}
void txt_move_lines(struct Text *text, const int direction)
{
TextLine *line_other;
BLI_assert(ELEM(direction, TXT_MOVE_LINE_UP, TXT_MOVE_LINE_DOWN));
if (!text || !text->curl || !text->sell) return;
txt_order_cursors(text);
line_other = (direction == TXT_MOVE_LINE_DOWN) ? text->sell->next : text->curl->prev;
if (!line_other) return;
BLI_remlink(&text->lines, line_other);
if (direction == TXT_MOVE_LINE_DOWN) {
BLI_insertlinkbefore(&text->lines, text->curl, line_other);
}
else {
BLI_insertlinkafter(&text->lines, text->sell, line_other);
}
txt_make_dirty(text);
txt_clean_text(text);
if (!undoing) {
txt_undo_add_op(text, (direction == TXT_MOVE_LINE_DOWN) ? UNDO_MOVE_LINES_DOWN : UNDO_MOVE_LINES_UP);
}
}
int setcurr_tab_spaces(Text *text, int space)
{
int i = 0;
int test = 0;
const char *word = ":";
const char *comm = "#";
const char indent = (text->flags & TXT_TABSTOSPACES) ? ' ' : '\t';
static const char *back_words[] = {"return", "break", "continue", "pass", "yield", NULL};
if (!text->curl) return 0;
while (text->curl->line[i] == indent) {
//we only count those tabs/spaces that are before any text or before the curs;
if (i == text->curc) {
return i;
}
else {
i++;
}
}
if (strstr(text->curl->line, word)) {
/* if we find a ':' on this line, then add a tab but not if it is:
* 1) in a comment
* 2) within an identifier
* 3) after the cursor (text->curc), i.e. when creating space before a function def [#25414]
*/
int a, is_indent = 0;
for (a = 0; (a < text->curc) && (text->curl->line[a] != '\0'); a++) {
char ch = text->curl->line[a];
if (ch == '#') {
break;
}
else if (ch == ':') {
is_indent = 1;
}
else if (ch != ' ' && ch != '\t') {
is_indent = 0;
}
}
if (is_indent) {
i += space;
}
}
for (test = 0; back_words[test]; test++) {
/* if there are these key words then remove a tab because we are done with the block */
if (strstr(text->curl->line, back_words[test]) && i > 0) {
if (strcspn(text->curl->line, back_words[test]) < strcspn(text->curl->line, comm)) {
i -= space;
}
}
}
return i;
}
/*******************************/
/* Character utility functions */
/*******************************/
int text_check_bracket(const char ch)
{
int a;
char opens[] = "([{";
char close[] = ")]}";
for (a = 0; a < (sizeof(opens) - 1); a++) {
if (ch == opens[a])
return a + 1;
else if (ch == close[a])
return -(a + 1);
}
return 0;
}
/* TODO, have a function for operators - http://docs.python.org/py3k/reference/lexical_analysis.html#operators */
int text_check_delim(const char ch)
{
int a;
char delims[] = "():\"\' ~!%^&*-+=[]{};/<>|.#\t,@";
for (a = 0; a < (sizeof(delims) - 1); a++) {
if (ch == delims[a])
return 1;
}
return 0;
}
int text_check_digit(const char ch)
{
if (ch < '0') return 0;
if (ch <= '9') return 1;
return 0;
}
int text_check_identifier(const char ch)
{
if (ch < '0') return 0;
if (ch <= '9') return 1;
if (ch < 'A') return 0;
if (ch <= 'Z' || ch == '_') return 1;
if (ch < 'a') return 0;
if (ch <= 'z') return 1;
return 0;
}
int text_check_whitespace(const char ch)
{
if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n')
return 1;
return 0;
}
int text_find_identifier_start(const char *str, int i)
{
while (i--) {
if (!text_check_identifier(str[i])) {
break;
}
}
i++;
return i;
}