1 // This file is part of e93.
2 //
3 // e93 is free software; you can redistribute it and/or modify
4 // it under the terms of the e93 LICENSE AGREEMENT.
5 //
6 // e93 is distributed in the hope that it will be useful,
7 // but WITHOUT ANY WARRANTY; without even the implied warranty of
8 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9 // e93 LICENSE AGREEMENT for more details.
10 //
11 // You should have received a copy of the e93 LICENSE AGREEMENT
12 // along with e93; see the file "LICENSE.TXT".
13 
14 
15 // global definitions and structures known by the editor
16 
17 #define	VERSION	"1.4"
18 #define	EDITION	"r3"
19 
20 #define		Max(a,b) 	((a)>(b)?(a):(b))
21 #define		Min(a,b) 	((a)<(b)?(a):(b))
22 
23 typedef	long long			INT64;				// 64 bits signed
24 typedef	unsigned long long	UINT64;				// 64 bits unsigned
25 typedef	int					INT32;				// 32 bits signed
26 typedef	unsigned int		UINT32;				// 32 bits unsigned
27 typedef	short int			INT16;				// 16 bits signed
28 typedef	unsigned short int	UINT16;				// 16 bits unsigned
29 typedef	signed char			INT8;				// 8 bits signed
30 typedef	unsigned char		UINT8;				// 8 bits unsigned
31 
32 // defines needed to handle text
33 
34 #define	CHUNK_SIZE					1024		// size in bytes of data chunks used while editing text
35 
36 #define	MAX_PATH_NAME_LENGTH		4096		// size in bytes maximum path name
37 
38 #define	SELECTION_CHUNK_ELEMENTS	256			// number of SELECTIONELEMENTs in an array chunk
39 #define	UNDO_CHUNK_ELEMENTS			256			// number of undo elements in an undo chunk
40 
41 #define	VIEW_MAX_STYLES				128			// maximum number of styles in a view (must be a power of 2)
42 
43 #define	HORIZONTAL_SCROLL_THRESHOLD	32			// number of "pixels" to scroll horizontally whenever we do so
44 
45 // modifier bits (can come from keyboard, or elsewhere (like mouse))
46 
47 // masks
48 enum
49 {
50 	EEM_CAPSLOCK=0x00000001,
51 	EEM_SHIFT=0x00000002,
52 	EEM_CTL=0x00000004,
53 	EEM_MOD0=0x00000008,
54 	EEM_MOD1=0x00000010,
55 	EEM_MOD2=0x00000020,
56 	EEM_MOD3=0x00000040,
57 	EEM_MOD4=0x00000080,
58 	EEM_MOD5=0x00000100,
59 	EEM_MOD6=0x00000200,
60 	EEM_MOD7=0x00000400,
61 	EEM_STATE0=0x00F0000,		// traditionally used to keep track of double, triple, quadruple, etc... clicks of mouse
62 	EEM_STATE1=0x0F00000,
63 	EEM_STATE2=0xF000000
64 };
65 
66 // shifts
67 enum
68 {
69 	EES_CAPSLOCK=0,
70 	EES_SHIFT=1,
71 	EES_CTL=2,
72 	EES_MOD0=3,
73 	EES_MOD1=4,
74 	EES_MOD2=5,
75 	EES_MOD3=6,
76 	EES_MOD4=7,
77 	EES_MOD5=8,
78 	EES_MOD6=9,
79 	EES_MOD7=10,
80 	EES_STATE0=16,
81 	EES_STATE1=20,
82 	EES_STATE2=24
83 };
84 
85 // view event types
86 
87 enum
88 {
89 	VET_KEYDOWN,			// saw key/virtual key press within view
90 	VET_KEYREPEAT,			// saw repeat within view
91 	VET_POSITIONVERTICAL,	// attempt to position the view to given absolute line
92 	VET_POSITIONHORIZONTAL,	// attempt to position the view to given absolute pixel
93 	VET_CLICK,				// initial click in view
94 	VET_CLICKHOLD			// send while click is being held in view
95 };
96 
97 // virtual view keys
98 
99 enum
100 {
101 	VVK_SCROLLUP,
102 	VVK_SCROLLDOWN,
103 	VVK_DOCUMENTPAGEUP,
104 	VVK_DOCUMENTPAGEDOWN,
105 	VVK_SCROLLLEFT,
106 	VVK_SCROLLRIGHT,
107 	VVK_DOCUMENTPAGELEFT,
108 	VVK_DOCUMENTPAGERIGHT,
109 // keys that are normally found on a keyboard, but not clearly associated with an ASCII code
110 // these are allowed to have modifier data sent with them
111 	VVK_LEFTARROW,
112 	VVK_RIGHTARROW,
113 	VVK_UPARROW,
114 	VVK_DOWNARROW,
115 	VVK_HOME,
116 	VVK_END,
117 	VVK_PAGEUP,
118 	VVK_PAGEDOWN,
119 	VVK_INSERT,
120 	VVK_BACKSPACE,
121 	VVK_DELETE,
122 	VVK_HELP,
123 	VVK_UNDO,
124 	VVK_REDO,
125 	VVK_UNDOTOGGLE,
126 	VVK_CUT,
127 	VVK_COPY,
128 	VVK_PASTE,
129 	VVK_FIND,
130 	VVK_RETURN,
131 	VVK_TAB,
132 	VVK_ESC,
133 	VVK_F1,
134 	VVK_F2,
135 	VVK_F3,
136 	VVK_F4,
137 	VVK_F5,
138 	VVK_F6,
139 	VVK_F7,
140 	VVK_F8,
141 	VVK_F9,
142 	VVK_F10,
143 	VVK_F11,
144 	VVK_F12,
145 	VVK_F13,
146 	VVK_F14,
147 	VVK_F15,
148 	VVK_F16,
149 	VVK_F17,
150 	VVK_F18,
151 	VVK_F19,
152 	VVK_F20
153 };
154 
155 // menu relationships
156 enum
157 {
158 	MR_NEXTSIBLING,			// next sibling
159 	MR_PREVIOUSSIBLING,		// previous sibling
160 	MR_FIRSTCHILD,			// first child
161 	MR_LASTCHILD			// last child
162 };
163 
164 // ways to home view (this type is used horizontally and vertically)
165 enum
166 {
167 	HT_NONE,				// do not move
168 	HT_LENIENT,				// if already on view, do not move, if off view, move as little as possible to get on view
169 	HT_SEMISTRICT,			// if already on view, do not move, if off view, move strict
170 	HT_STRICT				// move strict
171 };
172 
173 // search types
174 enum
175 {
176 	ST_FIND,
177 	ST_FINDALL,
178 	ST_REPLACE,
179 	ST_REPLACEALL
180 };
181 
182 typedef bool ABORT_TEST_FUNCTION();			// function passed to test for abort various time consuming operations
183 
184 typedef	struct editorFile EDITOR_FILE;		// this type is hidden from the editor (the editor only needs pointers to it)
185 typedef struct editorMenu EDITOR_MENU;		// menu definition (actual structure hidden from the editor)
186 typedef struct editorTask EDITOR_TASK;		// task definition (actual structure hidden)
187 typedef struct editorFont EDITOR_FONT;		// font definition (actual structure hidden)
188 typedef struct guiViewInfo GUI_VIEW_INFO;	// GUI's local view information
189 
190 typedef struct tokenlist					// token list definition
191 {
192 	const char
193 		*token;
194 	int
195 		tokenNum;
196 } TOKEN_LIST;
197 
198 typedef struct variableBinding VARIABLE_BINDING;	// opaque variable binding
199 
200 typedef struct variableTable
201 {
202 	VARIABLE_BINDING
203 		*variableBindingTable[256];		// array of heads of linked lists of variable bindings (crude hash table to get some speed when locating a binding)
204 	VARIABLE_BINDING
205 		*variableBindingListHead;		// head of list than runs through all variables
206 } VARIABLE_TABLE;
207 
208 typedef struct
209 {
210 	INT32
211 		x,y;
212 	UINT32
213 		w,h;
214 } EDITOR_RECT;
215 
216 typedef UINT32	EDITOR_COLOR;		// intensity values for each of the colors in RGB space (3 bytes R,G,B)
217 
218 typedef struct chunkHeader
219 {
220 	struct chunkHeader
221 		*nextHeader,
222 		*previousHeader;
223 	UINT8
224 		*data;						// pointer to the data buffer for this chunk
225 	UINT32
226 		totalBytes;					// number of bytes of this chunk that are in use
227 	UINT32
228 		totalLines;					// number of new line characters in this chunk
229 } CHUNK_HEADER;
230 
231 typedef struct
232 {
233 	CHUNK_HEADER
234 		*firstChunkHeader,			// pointer to the first chunk (NULL if there is none)
235 		*lastChunkHeader,			// pointer to the last chunk (NULL if there is none)
236 		*cacheChunkHeader;			// pointer to the cached chunk (NULL if there is none)
237 	UINT32
238 		totalBytes;					// total number of bytes of text
239 	UINT32
240 		totalLines;					// total number of new-line characters in text
241 	UINT32
242 		cacheBytes;					// total number of bytes up until the cacheChunkHeader
243 	UINT32
244 		cacheLines;					// total number of lines up until the cacheChunkHeader
245 } TEXT_UNIVERSE;
246 
247 typedef struct arrayChunkHeader
248 {
249 	struct arrayChunkHeader
250 		*nextHeader,
251 		*previousHeader;
252 	UINT8
253 		*data;						// pointer to the element data for this chunk
254 	UINT32
255 		totalElements;				// number of elements of this chunk that are in use
256 } ARRAY_CHUNK_HEADER;
257 
258 typedef struct
259 {
260 	ARRAY_CHUNK_HEADER
261 		*firstChunkHeader,			// points to the first header of the chunk array (or NULL if there are no chunks)
262 		*lastChunkHeader;
263 	UINT32
264 		totalElements;				// total number of elements in this universe
265 	UINT32
266 		maxElements,				// maximum number of elements in a chunk of this universe
267 		elementSize;				// size of an element of a chunk in this universe
268 } ARRAY_UNIVERSE;
269 
270 typedef struct selectionUniverse SELECTION_UNIVERSE;
271 typedef struct styleUniverse STYLE_UNIVERSE;
272 typedef struct syntaxInstance SYNTAX_INSTANCE;
273 
274 typedef struct undoElement
275 {
276 	UINT32
277 		frameNumber,				// tells which frame of undo this belongs to
278 		textStartPosition,			// position in text where replacement begins
279 		textLength,					// number of bytes to replace
280 		undoStartPosition,			// position in undo buffer of replacement text/style
281 		undoLength;					// length of replacement text
282 } UNDO_ELEMENT;
283 
284 typedef struct
285 {
286 	ARRAY_UNIVERSE
287 		undoArray;					// array which is used to keep track of the individual undos
288 	TEXT_UNIVERSE
289 		*undoText;					// text used while undoing
290 	STYLE_UNIVERSE
291 		*undoStyles;				// styles used while undoing
292 } UNDO_GROUP;
293 
294 typedef struct
295 {
296 	UINT32
297 		currentFrame;				// keeps track of the current undo frame index
298 	UINT32
299 		groupingDepth;				// knows the depth of begin/end undo group calls
300 	UINT32
301 		cleanElements;				// tells the number of elements in the undo-array when the buffer is considered "clean"
302 	bool
303 		cleanKnown;					// if false, cleanElements is meaningless and buffer is considered dirty
304 	bool
305 		undoActive;					// set true while undoing/redoing (tells not to delete redo information)
306 	bool
307 		undoing;					// set true while undoing (tells to place undo info into redo buffer)
308 	UNDO_GROUP
309 		undoGroup;					// used to keep track of undos
310 	UNDO_GROUP
311 		redoGroup;					// used to keep track of redos
312 } UNDO_UNIVERSE;
313 
314 typedef struct markVisibility
315 {
316 	struct editorMark				// mark that is visible in the view
317 		*parentMark;
318 	struct editorView				// view that the mark is visible in
319 		*parentView;
320 	struct markVisibility			// links through list of marks visible on a given view
321 		*nextMark,
322 		*prevMark;
323 	struct markVisibility			// links through list of views a given mark is visible in
324 		*nextView,
325 		*prevView;
326 	EDITOR_COLOR
327 		backgroundColor;			// color to use when drawing background of visible mark
328 	SELECTION_UNIVERSE
329 		*heldMark;					// keeps track of the part of the mark in the view when mark is changed
330 } MARK_VISIBILITY;
331 
332 typedef struct editorMark			// marks hanging off of editor universe
333 {
334 	char
335 		*markName;					// if NULL, then this mark is considered temporary
336 	SELECTION_UNIVERSE
337 		*selectionUniverse;			// the selection universe that defines this mark
338 	MARK_VISIBILITY
339 		*headVisibility,			// linked list of views this mark is visible in
340 		*tailVisibility;
341 	struct editorMark
342 		*nextMark;					// next mark in the list, NULL if no more
343 } EDITOR_MARK;
344 
345 typedef struct editorBufferHandle
346 {
347 	struct editorBuffer
348 		*buffer;					// points to the buffer being held
349 	struct editorBufferHandle
350 		*nextHandle; 				// next in linked list of handles
351 } EDITOR_BUFFER_HANDLE;
352 
353 typedef struct editorBuffer
354 {
355 	EDITOR_BUFFER_HANDLE
356 		*bufferHandles;				// when something needs to know when a buffer has been deleted, it can get a handle to it which the buffer code will clear on deletion
357 	bool
358 		shellBusy;					// used by the shell to make sure it cannot modify a buffer while it is in use by some previous shell command
359 	bool
360 		fromFile;					// tells if contents of this buffer represent an actual file
361 	char
362 		*contentName;				// name that describes the contents of the buffer (if fromFile=true, then this is full absolute pathname of file)
363 	TEXT_UNIVERSE
364 		*textUniverse;				// all the text
365 	UNDO_UNIVERSE
366 		*undoUniverse;				// undo/redo information
367 	SELECTION_UNIVERSE
368 		*selectionUniverse,			// selection and cursor information
369 		*xorSelectionUniverse,		// used when updating the selection in th XOR mode (holds previous version to XOR with)
370 		*auxSelectionUniverse;		// aux selection and cursor information (allows tasks to write to universe at place other than cursor)
371 	EDITOR_MARK
372 		*marks;						// pointer to head of list of marks for this universe, NULL if none
373 	STYLE_UNIVERSE
374 		*styleUniverse;				// maintains style array
375 	SYNTAX_INSTANCE
376 		*syntaxInstance;			// keeps track of syntax highlighting (no syntax highlights if NULL)
377 	EDITOR_TASK
378 		*task;						// pointer to the task record for this buffer, or NULL if no task linked to this buffer
379 	UINT32
380 		taskBytes;					// how many bytes of data the last task added to buffer
381 	struct editorWindow
382 		*window;					// pointer to document window for this buffer (if there is one, NULL if none)
383 	struct editorView
384 		*firstView;					// head of linked list of views onto this buffer
385 	VARIABLE_TABLE
386 		variableTable;				// table of variables linked to this buffer
387 	// NOTE: the members below help keep track of various things during replacements
388 	bool
389 		replaceHaveRange;			// tells if replacement range encountered yet
390 	UINT32
391 		replaceStartOffset,			// composite start offset in text
392 		replaceEndOffset;			// composite end offset in text (during the change)
393 	INT32
394 		replaceNumBytes;			// number of bytes added/(subtracted) from text
395 	// NOTE: the members below help keep track of various things during selection (this is a bit of a mess and should be simplified)
396 	UINT32
397 		anchorStartPosition,		// hold the ends of the selection while tracking
398 		anchorEndPosition;
399 	UINT32
400 		anchorLine;					// holds one corner of columnar selection while tracking
401 	INT32
402 		anchorX;
403 	UINT32
404 		columnarTopLine;			// when building a columnar selection, these remember the "rect" that bounds the selection
405 	INT32
406 		columnarLeftX;				// they are used when adding to the selection
407 	UINT32
408 		columnarBottomLine;
409 	INT32
410 		columnarRightX;
411 	bool
412 		haveStartX,					// tell if desiredStartX/desiredEndX are valid
413 		haveEndX;
414 	INT32
415 		desiredStartX,				// desired X position of the start of the selection when expanding/reducing, or of the cursor when no selection (used when moving the cursor up or down)
416 		desiredEndX;
417 	bool
418 		haveCurrentEnd;				// true if there is a current end of the selection being moved (used when extending the selection with the cursor keys)
419 	INT32
420 		currentIsStart;				// true if the current end being moved is the start
421 	struct editorBuffer
422 		*nextBuffer;				// next buffer in linked list of buffers
423 } EDITOR_BUFFER;
424 
425 typedef struct editorWindow
426 {
427 	UINT16
428 		windowType;					// which type of editor window this is
429 	EDITOR_BUFFER
430 		*buffer;					// if this is a document window, then this points to the buffer, otherwise it is NULL
431 	void
432 		*windowInfo;				// pointer to gui specific structure that keeps all info needed for this window type
433 	void
434 		*userData;					// pointer to any additional gui specific info for this window (like the gui's actual window structure)
435 } EDITOR_WINDOW;
436 
437 typedef struct viewDescriptor		// describes a view to creation routines
438 {
439 	EDITOR_BUFFER
440 		*buffer;					// the buffer on which to view
441 	EDITOR_RECT
442 		rect;						// position and size within parent window
443 	UINT32
444 		topLine;
445 	INT32
446 		leftPixel;
447 	UINT32
448 		tabSize;
449 	EDITOR_COLOR
450 		backgroundColor;			// default color to use when drawing background (style 0)
451 	EDITOR_COLOR
452 		foregroundColor;			// default color to use when drawing foreground (style 0)
453 	char
454 		*fontName;					// default font to use (style 0)
455 	bool
456 		active;						// tells if the view is created active or not
457 	void
458 		(*viewTextChangedVector)(struct editorView *view);	// this vector is called by the view when it has finished a text change
459 	void
460 		(*viewSelectionChangedVector)(struct editorView *view);	// this vector is called by the view when it has finished a selection change
461 	void
462 		(*viewPositionChangedVector)(struct editorView *view);	// this vector is called by the view when it has finished a position change
463 } EDITOR_VIEW_DESCRIPTOR;
464 
465 typedef struct
466 {
467 	EDITOR_FONT
468 		*font;						// pointer to font in use (NULL if default should be used)
469 	bool
470 		haveForegroundColor,		// tells if foreground color has been specified
471 		haveBackgroundColor;		// tells if background color has been specified
472 	EDITOR_COLOR
473 		foregroundColor,			// color to use when drawing foreground
474 		backgroundColor;			// color to use when drawing background
475 } VIEW_STYLE;
476 
477 // NOTE: it is possible to have multiple views of the same editor buffer
478 typedef struct editorView
479 {
480 	EDITOR_BUFFER
481 		*parentBuffer;				// the buffer on which this view is placed
482 	EDITOR_WINDOW
483 		*parentWindow;				// the window that contains this view
484 	struct editorView
485 		*nextBufferView;			// next view of the buffer that this view is on
486 	bool
487 		active;						// if true, then view is displayed as active
488 	bool
489 		wantHome;					// used during buffer update, tells if given view should be homed
490 	UINT32
491 		tabSize;					// number of columns per tab
492 	bool
493 		haveSelectionForegroundColor,	// tells if color has been specified
494 		haveSelectionBackgroundColor;	// tells if color has been specified
495 	EDITOR_COLOR
496 		selectionForegroundColor,	// color to use when drawing selection foreground
497 		selectionBackgroundColor;	// color to use when drawing selection background
498 	MARK_VISIBILITY
499 		*headVisibility,			// linked list of marks visible in this view
500 		*tailVisibility;
501 	void
502 		(*viewTextChangedVector)(struct editorView *view);	// this vector is called by the view when it has finished a text change
503 	void
504 		(*viewSelectionChangedVector)(struct editorView *view);	// this vector is called by the view when it has finished a selection change
505 	void
506 		(*viewPositionChangedVector)(struct editorView *view);	// this vector is called by the view when it has finished a position change
507 	// all of these variables are used to facilitate updates of views
508 	INT32
509 		updateStartX;				// used during update, keeps the X position into the line being updated (if it is on the view)
510 	UINT32
511 		startPosition,				// used during update, keep track of the character positions that the view spans
512 		endPosition;
513 	VIEW_STYLE
514 		viewStyles[VIEW_MAX_STYLES];	// array of styles
515 	SELECTION_UNIVERSE
516 		*heldSelection;				// keeps track of selections that were in the view across calls to Start/EndSelectionChange
517 	STYLE_UNIVERSE
518 		*heldStyle;					// keeps track of styles that were in the view across calls to Start/EndStyleChange
519 	GUI_VIEW_INFO
520 		*viewInfo;					// pointer to gui specific info about this view (gui needs to remember font, size, bounds, top line, etc)
521 } EDITOR_VIEW;
522 
523 typedef struct
524 {
525 	bool
526 		isVirtual;					// tells if the keyCode is a virtual one or not
527 	UINT32
528 		keyCode;					// gives keyboard position of non-modifier key that was hit, or the virtual key number if it was a virtual key
529 	UINT32
530 		modifiers;					// modifier bits (enumerated above)
531 } EDITOR_KEY;
532 
533 typedef struct
534 {
535 	UINT32
536 		modifiers;					// keyboard style modifier bits (enumerated above)
537 	UINT32
538 		position;					// absolute position (in pixels, or lines)
539 } VIEW_POS_EVENT_DATA;
540 
541 typedef struct
542 {
543 	UINT32
544 		keyCode;					// mouse button (0-n)
545 	UINT32
546 		modifiers;					// keyboard style modifier bits (enumerated above)
547 	INT32
548 		xClick,
549 		yClick;						// view relative click point
550 } VIEW_CLICK_EVENT_DATA;
551 
552 typedef struct						// high level view events (sent to the shell from the view)
553 {
554 	UINT16
555 		eventType;
556 	EDITOR_VIEW
557 		*view;						// the view that this event belongs to
558 	void
559 		*eventData;					// pointer to event specific data for event
560 } VIEW_EVENT;
561 
562