12 # include <sys/types.h>
17 # include "../src/tty.h"
18 # include <sys/stat.h>
28 #else /* ! MIDNIGHT */
33 # include <sys/types.h>
39 # include <my_string.h>
40 # include <sys/stat.h>
49 # if TIME_WITH_SYS_TIME
50 # include <sys/time.h>
54 # include <sys/time.h>
67 # include <X11/Xlib.h>
68 # include <X11/Xutil.h>
69 # include <X11/Xresource.h>
71 # include "coolwidget.h"
72 # include "app_glob.c"
73 # include "coollocal.h"
74 # include "stringtools.h"
78 # include "../src/main.h" /* for char *shell */
79 # include "../src/mad.h"
80 # include "../src/dlg.h"
81 # include "../src/widget.h"
82 # include "../src/color.h"
83 # include "../src/dialog.h"
84 # include "../src/mouse.h"
85 # include "../src/global.h"
86 # include "../src/help.h"
87 # include "../src/key.h"
88 # include "../src/wtools.h" /* for QuickWidgets */
89 # include "../src/win.h"
90 # include "../vfs/vfs.h"
91 # include "../src/menu.h"
92 # include "../src/regex.h"
95 # define WIDGET_COMMAND (WIDGET_USER + 10)
100 #define SEARCH_DIALOG_OPTION_NO_SCANF 1
101 #define SEARCH_DIALOG_OPTION_NO_REGEX 2
102 #define SEARCH_DIALOG_OPTION_NO_CASE 4
103 #define SEARCH_DIALOG_OPTION_BACKWARDS 8
105 #define SYNTAX_FILE "/.cedit/syntax"
106 #define CLIP_FILE "/.cedit/cooledit.clip"
107 #define MACRO_FILE "/.cedit/cooledit.macros"
108 #define BLOCK_FILE "/.cedit/cooledit.block"
109 #define ERROR_FILE "/.cedit/cooledit.error"
110 #define TEMP_FILE "/.cedit/cooledit.temp"
111 #define SCRIPT_FILE "/.cedit/cooledit.script"
112 #define EDIT_DIR "/.cedit"
114 #define EDIT_KEY_EMULATION_NORMAL 0
115 #define EDIT_KEY_EMULATION_EMACS 1
117 #define REDRAW_LINE (1 << 0)
118 #define REDRAW_LINE_ABOVE (1 << 1)
119 #define REDRAW_LINE_BELOW (1 << 2)
120 #define REDRAW_AFTER_CURSOR (1 << 3)
121 #define REDRAW_BEFORE_CURSOR (1 << 4)
122 #define REDRAW_PAGE (1 << 5)
123 #define REDRAW_IN_BOUNDS (1 << 6)
124 #define REDRAW_CHAR_ONLY (1 << 7)
125 #define REDRAW_COMPLETELY (1 << 8)
127 #define MOD_ABNORMAL (1 << 0)
128 #define MOD_UNDERLINED (1 << 1)
129 #define MOD_BOLD (1 << 2)
130 #define MOD_HIGHLIGHTED (1 << 3)
131 #define MOD_MARKED (1 << 4)
132 #define MOD_ITALIC (1 << 5)
133 #define MOD_CURSOR (1 << 6)
134 #define MOD_INVERSE (1 << 7)
137 # define EDIT_TEXT_HORIZONTAL_OFFSET 4
138 # define EDIT_TEXT_VERTICAL_OFFSET 3
140 # define EDIT_TEXT_HORIZONTAL_OFFSET 0
141 # define EDIT_TEXT_VERTICAL_OFFSET 1
142 # define FONT_OFFSET_X 0
143 # define FONT_OFFSET_Y 0
146 #define EDIT_RIGHT_EXTREME option_edit_right_extreme
147 #define EDIT_LEFT_EXTREME option_edit_left_extreme
148 #define EDIT_TOP_EXTREME option_edit_top_extreme
149 #define EDIT_BOTTOM_EXTREME option_edit_bottom_extreme
151 #define MAX_MACRO_LENGTH 1024
153 /*there are a maximum of ... */
155 /*... edit buffers, each of which is ... */
156 #define EDIT_BUF_SIZE 16384
157 /* ...bytes in size. */
159 /*x / EDIT_BUF_SIZE equals x >> ... */
160 #define S_EDIT_BUF_SIZE 14
162 /* x % EDIT_BUF_SIZE is equal to x && ... */
163 #define M_EDIT_BUF_SIZE 16383
165 #define SIZE_LIMIT (EDIT_BUF_SIZE * (MAXBUFF - 2))
166 /* Note a 16k stack is 64k of data and enough to hold (usually) around 10
167 pages of undo info. */
170 #define START_STACK_SIZE 32
173 /*some codes that may be pushed onto or returned from the undo stack: */
174 #define CURS_LEFT 601
175 #define CURS_RIGHT 602
177 #define BACKSPACE 604
178 #define STACK_BOTTOM 605
179 #define CURS_LEFT_LOTS 606
180 #define CURS_RIGHT_LOTS 607
182 #define MARK_2 700000000
183 #define KEY_PRESS 1400000000
185 /*Tabs spaces: (sofar only HALF_TAB_SIZE is used: */
186 #define TAB_SIZE option_tab_spacing
187 #define HALF_TAB_SIZE ((int) option_tab_spacing / 2)
195 unsigned char * text
;
200 #define RULE_CONTEXT 0x00FFF000UL
201 #define RULE_CONTEXT_SHIFT 12
202 #define RULE_WORD 0x00000FFFUL
203 #define RULE_WORD_SHIFT 0
204 #define RULE_ON_LEFT_BORDER 0x02000000UL
205 #define RULE_ON_RIGHT_BORDER 0x01000000UL
211 char *whole_word_chars_left
;
212 char *whole_word_chars_right
;
213 #define NO_COLOR ((unsigned long) -1);
219 struct context_rule
{
224 char line_start_left
;
228 char line_start_right
;
230 int between_delimiters
;
231 char *whole_word_chars_left
;
232 char *whole_word_chars_right
;
233 unsigned char *conflicts
;
234 char *keyword_first_chars
;
235 char *keyword_last_chars
;
236 /* first word is word[1] */
237 struct key_word
**keyword
;
242 struct editor_widget
{
246 struct cool_widget
*widget
;
248 #define from_here num_widget_lines
249 int num_widget_lines
;
250 int num_widget_columns
;
258 char *filename
; /* Name of the file */
259 char *dir
; /* current directory */
261 /* dynamic buffers and curser position for editor: */
262 long curs1
; /*position of the cursor from the beginning of the file. */
263 long curs2
; /*position from the end of the file */
264 unsigned char *buffers1
[MAXBUFF
+ 1]; /*all data up to curs1 */
265 unsigned char *buffers2
[MAXBUFF
+ 1]; /*all data from end of file down to curs2 */
267 /* search variables */
268 long search_start
; /* First character to start searching from */
269 int found_len
; /* Length of found string or 0 if none was found */
270 long found_start
; /* the found word from a search - start position */
272 /* display information */
273 long last_byte
; /* Last byte of file */
274 long start_display
; /* First char displayed */
275 long start_col
; /* First displayed column, negative */
276 long max_column
; /* The maximum cursor position ever reached used to calc hori scroll bar */
277 long curs_row
; /*row position of curser on the screen */
278 long curs_col
; /*column position on screen */
279 int force
; /* how much of the screen do we redraw? */
280 unsigned char overwrite
;
281 unsigned char modified
; /*has the file been changed?: 1 if char inserted or
282 deleted at all since last load or save */
284 int delete_file
; /* has the file been created in edit_load_file? Delete
285 it at end of editing when it hasn't been modified
288 unsigned char highlight
;
289 long prev_col
; /*recent column position of the curser - used when moving
290 up or down past lines that are shorter than the current line */
291 long curs_line
; /*line number of the cursor. */
292 long start_line
; /*line nummber of the top of the page */
295 long total_lines
; /*total lines in the file */
296 long mark1
; /*position of highlight start */
297 long mark2
; /*position of highlight end */
298 int column1
; /*position of column highlight start */
299 int column2
; /*position of column highlight end */
300 long bracket
; /*position of a matching bracket */
302 /* undo stack and pointers */
303 unsigned long stack_pointer
;
305 unsigned long stack_size
;
306 unsigned long stack_size_mask
;
307 unsigned long stack_bottom
;
310 /* syntax higlighting */
311 struct context_rule
**rules
;
314 char *syntax_type
; /* description of syntax highlighting type being used */
315 int explicit_syntax
; /* have we forced the syntax hi. type in spite of the filename? */
317 int to_here
; /* dummy marker */
321 int macro_i
; /* -1 if not recording index to macro[] otherwise */
322 struct macro macro
[MAX_MACRO_LENGTH
];
325 typedef struct editor_widget WEdit
;
329 void edit_render_expose (WEdit
* edit
, XExposeEvent
* xexpose
);
330 void edit_render_tidbits (struct cool_widget
*w
);
331 int eh_editor (CWidget
* w
, XEvent
* xevent
, CEvent
* cwevent
);
332 void edit_draw_menus (Window parent
, int x
, int y
);
333 void edit_run_make (void);
334 void edit_change_directory (void);
335 int edit_man_page_cmd (WEdit
* edit
);
336 void edit_search_replace_dialog (Window parent
, int x
, int y
, char **search_text
, char **replace_text
, char **arg_order
, char *heading
, int option
);
337 void edit_search_dialog (WEdit
* edit
, char **search_text
);
338 long edit_find (long search_start
, unsigned char *exp
, int *len
, long last_byte
, int (*get_byte
) (void *, long), void *data
);
339 void edit_set_foreground_colors (unsigned long normal
, unsigned long bold
, unsigned long italic
);
340 void edit_set_background_colors (unsigned long normal
, unsigned long abnormal
, unsigned long marked
, unsigned long marked_abnormal
, unsigned long highlighted
);
341 void edit_set_cursor_color (unsigned long c
);
342 void draw_options_dialog (Window parent
, int x
, int y
);
343 void CRefreshEditor (WEdit
* edit
);
344 void edit_set_user_command (void (*func
) (WEdit
*, int));
345 void edit_draw_this_line_proportional (WEdit
* edit
, long b
, int curs_row
, int start_column
, int end_column
);
346 unsigned char get_international_character (unsigned char key_press
);
347 void edit_set_user_key_function (int (*user_def_key_func
) (unsigned int, unsigned int, KeySym keysym
));
351 int edit_drop_hotkey_menu (WEdit
* e
, int key
);
352 void edit_menu_cmd (WEdit
* e
);
353 void edit_init_menu_emacs (void);
354 void edit_init_menu_normal (void);
355 void edit_done_menu (void);
356 int edit_raw_key_query (char *heading
, char *query
, int cancel
);
357 char *strcasechr (const char *s
, int c
);
358 int edit (const char *_file
, int line
);
359 int edit_translate_key (WEdit
* edit
, unsigned int x_keycode
, long x_key
, int x_state
, int *cmd
, int *ch
);
363 int edit_get_byte (WEdit
* edit
, long byte_index
);
364 char *edit_get_buffer_as_text (WEdit
* edit
);
365 int edit_load_file (WEdit
* edit
, const char *filename
, const char *text
, unsigned long text_size
);
366 int edit_count_lines (WEdit
* edit
, long current
, int upto
);
367 long edit_move_forward (WEdit
* edit
, long current
, int lines
, long upto
);
368 long edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
);
369 long edit_move_backward (WEdit
* edit
, long current
, int lines
);
370 void edit_scroll_screen_over_cursor (WEdit
* edit
);
371 void edit_render_keypress (WEdit
* edit
);
372 void edit_scroll_upward (WEdit
* edit
, unsigned long i
);
373 void edit_scroll_downward (WEdit
* edit
, int i
);
374 void edit_scroll_right (WEdit
* edit
, int i
);
375 void edit_scroll_left (WEdit
* edit
, int i
);
376 int edit_get_col (WEdit
* edit
);
377 long edit_bol (WEdit
* edit
, long current
);
378 long edit_eol (WEdit
* edit
, long current
);
379 void edit_update_curs_row (WEdit
* edit
);
380 void edit_update_curs_col (WEdit
* edit
);
382 void edit_block_copy_cmd (WEdit
* edit
);
383 void edit_block_move_cmd (WEdit
* edit
);
384 int edit_block_delete_cmd (WEdit
* edit
);
386 int edit_delete (WEdit
* edit
);
387 void edit_insert (WEdit
* edit
, int c
);
388 int edit_cursor_move (WEdit
* edit
, long increment
);
389 void edit_push_action (WEdit
* edit
, long c
,...);
390 void edit_push_key_press (WEdit
* edit
);
391 void edit_insert_ahead (WEdit
* edit
, int c
);
392 int edit_save_file (WEdit
* edit
, const char *filename
);
393 int edit_save_cmd (WEdit
* edit
);
394 int edit_save_confirm_cmd (WEdit
* edit
);
395 int edit_save_as_cmd (WEdit
* edit
);
396 WEdit
*edit_init (WEdit
* edit
, int lines
, int columns
, const char *filename
, const char *text
, const char *dir
, unsigned long text_size
);
397 int edit_clean (WEdit
* edit
);
398 int edit_renew (WEdit
* edit
);
399 int edit_new_cmd (WEdit
* edit
);
400 int edit_reload (WEdit
* edit
, const char *filename
, const char *text
, const char *dir
, unsigned long text_size
);
401 int edit_load_cmd (WEdit
* edit
);
402 void edit_mark_cmd (WEdit
* edit
, int unmark
);
403 void edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
);
404 void edit_push_markers (WEdit
* edit
);
405 void edit_quit_cmd (WEdit
* edit
);
406 void edit_replace_cmd (WEdit
* edit
, int again
);
407 void edit_search_cmd (WEdit
* edit
, int again
);
408 int edit_save_block_cmd (WEdit
* edit
);
409 int edit_insert_file_cmd (WEdit
* edit
);
410 int edit_insert_file (WEdit
* edit
, const char *filename
);
411 void edit_block_process_cmd (WEdit
* edit
, const char *shell_cmd
, int block
);
412 char *catstrs (const char *first
,...);
413 void edit_refresh_cmd (WEdit
* edit
);
414 void edit_date_cmd (WEdit
* edit
);
415 void edit_goto_cmd (WEdit
* edit
);
416 int eval_marks (WEdit
* edit
, long *start_mark
, long *end_mark
);
417 void edit_status (WEdit
* edit
);
418 int edit_execute_command (WEdit
* edit
, int command
, int char_for_insertion
);
419 int edit_execute_key_command (WEdit
* edit
, int command
, int char_for_insertion
);
420 void edit_update_screen (WEdit
* edit
);
421 int edit_printf (WEdit
* e
, const char *fmt
,...);
422 int edit_print_string (WEdit
* e
, const char *s
);
423 void edit_move_to_line (WEdit
* e
, long line
);
424 void edit_move_display (WEdit
* e
, long line
);
425 void edit_word_wrap (WEdit
* edit
);
426 unsigned char *edit_get_block (WEdit
* edit
, long start
, long finish
, int *l
);
427 int edit_sort_cmd (WEdit
* edit
);
428 void edit_help_cmd (WEdit
* edit
);
429 void edit_left_word_move (WEdit
* edit
);
430 void edit_right_word_move (WEdit
* edit
);
431 void edit_get_selection (WEdit
* edit
);
433 int edit_save_macro_cmd (WEdit
* edit
, struct macro macro
[], int n
);
434 int edit_load_macro_cmd (WEdit
* edit
, struct macro macro
[], int *n
, int k
);
435 void edit_delete_macro_cmd (WEdit
* edit
);
437 int edit_copy_to_X_buf_cmd (WEdit
* edit
);
438 int edit_cut_to_X_buf_cmd (WEdit
* edit
);
439 void edit_paste_from_X_buf_cmd (WEdit
* edit
);
441 void edit_paste_from_history (WEdit
*edit
);
443 void edit_split_filename (WEdit
* edit
, char *name
);
446 #define CWidget Widget
448 void edit_set_syntax_change_callback (void (*callback
) (CWidget
*));
449 void edit_load_syntax (WEdit
* edit
, char **names
, char *type
);
450 void edit_free_syntax_rules (WEdit
* edit
);
451 void edit_get_syntax_color (WEdit
* edit
, long byte_index
, int *fg
, int *bg
);
456 /* put OS2/NT/WIN95 defines here */
459 # define MY_O_TEXT O_TEXT
464 # define FONT_PIX_PER_LINE 1
465 # define FONT_MEAN_WIDTH 1
467 # define get_sys_error(s) (s)
468 # define open mc_open
469 # define close(f) mc_close(f)
470 # define read(f,b,c) mc_read(f,b,c)
471 # define write(f,b,c) mc_write(f,b,c)
472 # define stat(f,s) mc_stat(f,s)
473 # define mkdir(s,m) mc_mkdir(s,m)
474 # define itoa MY_itoa
476 # define edit_get_load_file(d,f,h) input_dialog (h, _(" Enter file name: "), f)
477 # define edit_get_save_file(d,f,h) input_dialog (h, _(" Enter file name: "), f)
478 # define CMalloc(x) malloc(x)
480 # define set_error_msg(s) edit_init_error_msg = strdup(s)
484 # define edit_error_dialog(h,s) set_error_msg(s)
485 char *edit_init_error_msg
= NULL
;
487 # else /* ! _EDIT_C */
489 # define edit_error_dialog(h,s) query_dialog (h, s, 0, 1, _("&Dismiss"))
490 # define edit_message_dialog(h,s) query_dialog (h, s, 0, 1, _("&Ok"))
491 extern char *edit_init_error_msg
;
493 # endif /* ! _EDIT_C */
496 # define get_error_msg(s) edit_init_error_msg
497 # define edit_query_dialog2(h,t,a,b) query_dialog(h,t,0,2,a,b)
498 # define edit_query_dialog3(h,t,a,b,c) query_dialog(h,t,0,3,a,b,c)
499 # define edit_query_dialog4(h,t,a,b,c,d) query_dialog(h,t,0,4,a,b,c,d)
501 #else /* ! MIDNIGHT */
504 # define WIN_MESSAGES edit->widget->mainid, 20, 20
506 # define edit_get_load_file(d,f,h) CGetLoadFile(WIN_MESSAGES,d,f,h)
507 # define edit_get_save_file(d,f,h) CGetSaveFile(WIN_MESSAGES,d,f,h)
508 # define edit_error_dialog(h,t) CErrorDialog(WIN_MESSAGES,h,"%s",t)
509 # define edit_message_dialog(h,t) CMessageDialog(WIN_MESSAGES,0,h,"%s",t)
510 # define edit_query_dialog2(h,t,a,b) CQueryDialog(WIN_MESSAGES,h,t,a,b,0)
511 # define edit_query_dialog3(h,t,a,b,c) CQueryDialog(WIN_MESSAGES,h,t,a,b,c,0)
512 # define edit_query_dialog4(h,t,a,b,c,d) CQueryDialog(WIN_MESSAGES,h,t,a,b,c,d,0)
514 #endif /* ! MIDNIGHT */
516 extern char *home_dir
;
518 #define NUM_SELECTION_HISTORY 32
522 struct selection selection
=
524 int current_selection
= 0;
525 /* Note: selection.text = selection_history[current_selection].text */
526 struct selection selection_history
[NUM_SELECTION_HISTORY
] =
542 what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
543 or EDIT_KEY_EMULATION_EMACS
545 int edit_key_emulation
= EDIT_KEY_EMULATION_NORMAL
;
546 #endif /* ! MIDNIGHT */
548 int option_word_wrap_line_length
= 72;
549 int option_typewriter_wrap
= 0;
550 int option_auto_para_formatting
= 0;
551 int option_international_characters
= 0;
552 int option_tab_spacing
= 8;
553 int option_fill_tabs_with_spaces
= 0;
554 int option_return_does_auto_indent
= 1;
555 int option_backspace_through_tabs
= 0;
556 int option_fake_half_tabs
= 1;
557 int option_save_mode
= 0;
558 int option_backup_ext_int
= -1;
559 int option_find_bracket
= 1;
560 int option_max_undo
= 8192;
562 int option_editor_fg_normal
= 26;
563 int option_editor_fg_bold
= 8;
564 int option_editor_fg_italic
= 10;
566 int option_edit_right_extreme
= 0;
567 int option_edit_left_extreme
= 0;
568 int option_edit_top_extreme
= 0;
569 int option_edit_bottom_extreme
= 0;
571 int option_editor_bg_normal
= 1;
572 int option_editor_bg_abnormal
= 0;
573 int option_editor_bg_marked
= 2;
574 int option_editor_bg_marked_abnormal
= 9;
575 int option_editor_bg_highlighted
= 12;
576 int option_editor_fg_cursor
= 18;
578 char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
579 char *option_whole_chars_move
= "0123456789abcdefghijklmnopqrstuvwxyz_; ,[](){}";
580 char *option_backup_ext
= "~";
582 #else /* ! _EDIT_C */
584 extern struct selection selection
;
585 extern struct selection selection_history
[];
586 extern int current_selection
;
590 what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
591 or EDIT_KEY_EMULATION_EMACS
593 extern int edit_key_emulation
;
594 #endif /* ! MIDNIGHT */
596 extern int option_word_wrap_line_length
;
597 extern int option_typewriter_wrap
;
598 extern int option_auto_para_formatting
;
599 extern int option_international_characters
;
600 extern int option_tab_spacing
;
601 extern int option_fill_tabs_with_spaces
;
602 extern int option_return_does_auto_indent
;
603 extern int option_backspace_through_tabs
;
604 extern int option_fake_half_tabs
;
605 extern int option_save_mode
;
606 extern int option_backup_ext_int
;
607 extern int option_find_bracket
;
608 extern int option_max_undo
;
610 extern int option_editor_fg_normal
;
611 extern int option_editor_fg_bold
;
612 extern int option_editor_fg_italic
;
614 extern int option_edit_right_extreme
;
615 extern int option_edit_left_extreme
;
616 extern int option_edit_top_extreme
;
617 extern int option_edit_bottom_extreme
;
619 extern int option_editor_bg_normal
;
620 extern int option_editor_bg_abnormal
;
621 extern int option_editor_bg_marked
;
622 extern int option_editor_bg_marked_abnormal
;
623 extern int option_editor_bg_highlighted
;
624 extern int option_editor_fg_cursor
;
626 extern char *option_whole_chars_search
;
627 extern char *option_whole_chars_move
;
628 extern char *option_backup_ext
;
630 extern int edit_confirm_save
;
632 #endif /* ! _EDIT_C */
633 #endif /* __EDIT_H */