1 /*
2 Editor low level data handling and cursor fundamentals.
3
4 Copyright (C) 1996-2021
5 Free Software Foundation, Inc.
6
7 Written by:
8 Paul Sheer 1996, 1997
9 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
10 Andrew Borodin <aborodin@vmail.ru> 2012, 2013
11
12 This file is part of the Midnight Commander.
13
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
18
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 */
27
28 /** \file
29 * \brief Source: editor low level data handling and cursor fundamentals
30 * \author Paul Sheer
31 * \date 1996, 1997
32 */
33
34 #include <config.h>
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <ctype.h>
41 #include <errno.h>
42 #include <sys/stat.h>
43 #include <stdint.h> /* UINTMAX_MAX */
44 #include <stdlib.h>
45
46 #include "lib/global.h"
47
48 #include "lib/tty/color.h"
49 #include "lib/tty/tty.h" /* attrset() */
50 #include "lib/tty/key.h" /* is_idle() */
51 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
52 #include "lib/fileloc.h" /* EDIT_HOME_BLOCK_FILE */
53 #include "lib/vfs/vfs.h"
54 #include "lib/strutil.h" /* utf string functions */
55 #include "lib/util.h" /* load_file_position(), save_file_position() */
56 #include "lib/timefmt.h" /* time formatting */
57 #include "lib/lock.h"
58 #include "lib/widget.h"
59
60 #ifdef HAVE_CHARSET
61 #include "lib/charsets.h" /* get_codepage_id */
62 #endif
63
64 #include "src/usermenu.h" /* user_menu_cmd() */
65
66 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/keymap.h"
68
69 #include "edit-impl.h"
70 #include "editwidget.h"
71 #include "editsearch.h"
72 #include "editcomplete.h" /* edit_complete_word_cmd() */
73 #include "editmacros.h"
74 #include "etags.h" /* edit_get_match_keyword_cmd() */
75 #ifdef HAVE_ASPELL
76 #include "spell.h"
77 #endif
78
79 /*** global variables ****************************************************************************/
80
81 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
82 gboolean option_typewriter_wrap = FALSE;
83 gboolean option_auto_para_formatting = FALSE;
84 gboolean option_fill_tabs_with_spaces = FALSE;
85 gboolean option_return_does_auto_indent = TRUE;
86 gboolean option_backspace_through_tabs = FALSE;
87 gboolean option_fake_half_tabs = TRUE;
88 int option_save_mode = EDIT_QUICK_SAVE;
89 gboolean option_save_position = TRUE;
90 int option_max_undo = 32768;
91 gboolean option_persistent_selections = TRUE;
92 gboolean option_cursor_beyond_eol = FALSE;
93 gboolean option_line_state = FALSE;
94 int option_line_state_width = 0;
95 gboolean option_cursor_after_inserted_block = FALSE;
96 gboolean option_state_full_filename = FALSE;
97
98 gboolean enable_show_tabs_tws = TRUE;
99 gboolean option_check_nl_at_eof = FALSE;
100 gboolean option_group_undo = FALSE;
101 gboolean show_right_margin = FALSE;
102
103 char *option_backup_ext = NULL;
104 char *option_filesize_threshold = NULL;
105
106 unsigned int edit_stack_iterator = 0;
107 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
108 /* magic sequense for say than block is vertical */
109 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
110
111 /*** file scope macro definitions ****************************************************************/
112
113 #define TEMP_BUF_LEN 1024
114
115 #define space_width 1
116
117 /*** file scope type declarations ****************************************************************/
118
119 /*** file scope variables ************************************************************************/
120
121 /* detecting an error on save is easy: just check if every byte has been written. */
122 /* detecting an error on read, is not so easy 'cos there is not way to tell
123 whether you read everything or not. */
124 /* FIXME: add proper 'triple_pipe_open' to read, write and check errors. */
125 static const struct edit_filters
126 {
127 const char *read, *write, *extension;
128 } all_filters[] =
129 {
130 /* *INDENT-OFF* */
131 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
132 { "zstd -cd %s 2>&1", "zstd > %s", ".zst"},
133 { "lz4 -cd %s 2>&1", "lz4 > %s", ".lz4" },
134 { "lzip -cd %s 2>&1", "lzip > %s", ".lz"},
135 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
136 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
137 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
138 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
139 /* *INDENT-ON* */
140 };
141
142 static const off_t option_filesize_default_threshold = 64 * 1024 * 1024; /* 64 MB */
143
144 /* --------------------------------------------------------------------------------------------- */
145 /*** file scope functions ************************************************************************/
146 /* --------------------------------------------------------------------------------------------- */
147
148 static int
edit_load_status_update_cb(status_msg_t * sm)149 edit_load_status_update_cb (status_msg_t * sm)
150 {
151 simple_status_msg_t *ssm = SIMPLE_STATUS_MSG (sm);
152 edit_buffer_read_file_status_msg_t *rsm = (edit_buffer_read_file_status_msg_t *) sm;
153 Widget *wd = WIDGET (sm->dlg);
154
155 if (verbose)
156 label_set_textv (ssm->label, _("Loading: %3d%%"),
157 edit_buffer_calc_percent (rsm->buf, rsm->loaded));
158 else
159 label_set_text (ssm->label, _("Loading..."));
160
161 if (rsm->first)
162 {
163 int wd_width;
164 Widget *lw = WIDGET (ssm->label);
165
166 wd_width = MAX (wd->cols, lw->cols + 6);
167 widget_set_size (wd, wd->y, wd->x, wd->lines, wd_width);
168 widget_set_size (lw, lw->y, wd->x + (wd->cols - lw->cols) / 2, lw->lines, lw->cols);
169 rsm->first = FALSE;
170 }
171
172 return status_msg_common_update (sm);
173 }
174
175 /* --------------------------------------------------------------------------------------------- */
176 /**
177 * Load file OR text into buffers. Set cursor to the beginning of file.
178 *
179 * @return FALSE on error.
180 */
181
182 static gboolean
edit_load_file_fast(edit_buffer_t * buf,const vfs_path_t * filename_vpath)183 edit_load_file_fast (edit_buffer_t * buf, const vfs_path_t * filename_vpath)
184 {
185 int file;
186 gboolean ret;
187 edit_buffer_read_file_status_msg_t rsm;
188 gboolean aborted;
189
190 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
191 if (file < 0)
192 {
193 gchar *errmsg;
194
195 errmsg =
196 g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath));
197 edit_error_dialog (_("Error"), errmsg);
198 g_free (errmsg);
199 return FALSE;
200 }
201
202 rsm.first = TRUE;
203 rsm.buf = buf;
204 rsm.loaded = 0;
205
206 status_msg_init (STATUS_MSG (&rsm), _("Load file"), 1.0, simple_status_msg_init_cb,
207 edit_load_status_update_cb, NULL);
208
209 ret = (edit_buffer_read_file (buf, file, buf->size, &rsm, &aborted) == buf->size);
210
211 status_msg_deinit (STATUS_MSG (&rsm));
212
213 if (!ret && !aborted)
214 {
215 gchar *errmsg;
216
217 errmsg = g_strdup_printf (_("Error reading %s"), vfs_path_as_str (filename_vpath));
218 edit_error_dialog (_("Error"), errmsg);
219 g_free (errmsg);
220 }
221
222 mc_close (file);
223 return ret;
224 }
225
226 /* --------------------------------------------------------------------------------------------- */
227 /** Return index of the filter or -1 is there is no appropriate filter */
228
229 static int
edit_find_filter(const vfs_path_t * filename_vpath)230 edit_find_filter (const vfs_path_t * filename_vpath)
231 {
232 size_t i, l;
233
234 if (filename_vpath == NULL)
235 return -1;
236
237 l = strlen (vfs_path_as_str (filename_vpath));
238 for (i = 0; i < G_N_ELEMENTS (all_filters); i++)
239 {
240 size_t e;
241
242 e = strlen (all_filters[i].extension);
243 if (l > e)
244 if (!strcmp (all_filters[i].extension, vfs_path_as_str (filename_vpath) + l - e))
245 return i;
246 }
247 return -1;
248 }
249
250 /* --------------------------------------------------------------------------------------------- */
251
252 static char *
edit_get_filter(const vfs_path_t * filename_vpath)253 edit_get_filter (const vfs_path_t * filename_vpath)
254 {
255 int i;
256 char *p, *quoted_name;
257
258 i = edit_find_filter (filename_vpath);
259 if (i < 0)
260 return NULL;
261
262 quoted_name = name_quote (vfs_path_as_str (filename_vpath), FALSE);
263 p = g_strdup_printf (all_filters[i].read, quoted_name);
264 g_free (quoted_name);
265 return p;
266 }
267
268 /* --------------------------------------------------------------------------------------------- */
269
270 static off_t
edit_insert_stream(WEdit * edit,FILE * f)271 edit_insert_stream (WEdit * edit, FILE * f)
272 {
273 int c;
274 off_t i = 0;
275
276 while ((c = fgetc (f)) >= 0)
277 {
278 edit_insert (edit, c);
279 i++;
280 }
281 return i;
282 }
283
284 /* --------------------------------------------------------------------------------------------- */
285 /**
286 * Open file and create it if necessary.
287 *
288 * @param edit editor object
289 * @param filename_vpath file name
290 * @param st buffer for store stat info
291 * @return TRUE for success, FALSE for error.
292 */
293
294 static gboolean
check_file_access(WEdit * edit,const vfs_path_t * filename_vpath,struct stat * st)295 check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat *st)
296 {
297 static uintmax_t threshold = UINTMAX_MAX;
298 int file;
299 gchar *errmsg = NULL;
300 gboolean ret = TRUE;
301
302 /* Try opening an existing file */
303 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
304 if (file < 0)
305 {
306 /*
307 * Try creating the file. O_EXCL prevents following broken links
308 * and opening existing files.
309 */
310 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
311 if (file < 0)
312 {
313 errmsg =
314 g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath));
315 goto cleanup;
316 }
317
318 /* New file, delete it if it's not modified or saved */
319 edit->delete_file = 1;
320 }
321
322 /* Check what we have opened */
323 if (mc_fstat (file, st) < 0)
324 {
325 errmsg =
326 g_strdup_printf (_("Cannot get size/permissions for %s"),
327 vfs_path_as_str (filename_vpath));
328 goto cleanup;
329 }
330
331 /* We want to open regular files only */
332 if (!S_ISREG (st->st_mode))
333 {
334 errmsg =
335 g_strdup_printf (_("\"%s\" is not a regular file"), vfs_path_as_str (filename_vpath));
336 goto cleanup;
337 }
338
339 /* get file size threshold for alarm */
340 if (threshold == UINTMAX_MAX)
341 {
342 gboolean err = FALSE;
343
344 threshold = parse_integer (option_filesize_threshold, &err);
345 if (err)
346 threshold = option_filesize_default_threshold;
347 }
348
349 /*
350 * Don't delete non-empty files.
351 * O_EXCL should prevent it, but let's be on the safe side.
352 */
353 if (st->st_size > 0)
354 edit->delete_file = 0;
355
356 if ((uintmax_t) st->st_size > threshold)
357 {
358 int act;
359
360 errmsg = g_strdup_printf (_("File \"%s\" is too large.\nOpen it anyway?"),
361 vfs_path_as_str (filename_vpath));
362 act = edit_query_dialog2 (_("Warning"), errmsg, _("&Yes"), _("&No"));
363 MC_PTR_FREE (errmsg);
364
365 if (act != 0)
366 ret = FALSE;
367 }
368
369 cleanup:
370 (void) mc_close (file);
371
372 if (errmsg != NULL)
373 {
374 edit_error_dialog (_("Error"), errmsg);
375 g_free (errmsg);
376 ret = FALSE;
377 }
378
379 return ret;
380 }
381
382 /* --------------------------------------------------------------------------------------------- */
383
384 /**
385 * Open the file and load it into the buffers, either directly or using
386 * a filter. Return TRUE on success, FALSE on error.
387 *
388 * Fast loading (edit_load_file_fast) is used when the file size is
389 * known. In this case the data is read into the buffers by blocks.
390 * If the file size is not known, the data is loaded byte by byte in
391 * edit_insert_file.
392 *
393 * @param edit editor object
394 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
395 */
396 static gboolean
edit_load_file(WEdit * edit)397 edit_load_file (WEdit * edit)
398 {
399 gboolean fast_load = TRUE;
400
401 /* Cannot do fast load if a filter is used */
402 if (edit_find_filter (edit->filename_vpath) >= 0)
403 fast_load = FALSE;
404
405 /*
406 * FIXME: line end translation should disable fast loading as well
407 * Consider doing fseek() to the end and ftell() for the real size.
408 */
409 if (edit->filename_vpath != NULL)
410 {
411 /*
412 * VFS may report file size incorrectly, and slow load is not a big
413 * deal considering overhead in VFS.
414 */
415 if (!vfs_file_is_local (edit->filename_vpath))
416 fast_load = FALSE;
417
418 /* If we are dealing with a real file, check that it exists */
419 if (!check_file_access (edit, edit->filename_vpath, &edit->stat1))
420 {
421 edit_clean (edit);
422 return FALSE;
423 }
424 }
425 else
426 {
427 /* nothing to load */
428 fast_load = FALSE;
429 }
430
431 if (fast_load)
432 {
433 edit_buffer_init (&edit->buffer, edit->stat1.st_size);
434
435 if (!edit_load_file_fast (&edit->buffer, edit->filename_vpath))
436 {
437 edit_clean (edit);
438 return FALSE;
439 }
440 }
441 else
442 {
443 edit_buffer_init (&edit->buffer, 0);
444
445 if (edit->filename_vpath != NULL
446 && *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) != '\0')
447 {
448 edit->undo_stack_disable = 1;
449 if (edit_insert_file (edit, edit->filename_vpath) < 0)
450 {
451 edit_clean (edit);
452 return FALSE;
453 }
454 edit->undo_stack_disable = 0;
455 }
456 }
457 edit->lb = LB_ASIS;
458 return TRUE;
459 }
460
461 /* --------------------------------------------------------------------------------------------- */
462 /**
463 * Restore saved cursor position and/or bookmarks in the file
464 *
465 * @param edit editor object
466 * @param load_position If TRUE, load bookmarks and cursor position and aply them.
467 * If FALSE, load bookmarks only.
468 */
469
470 static void
edit_load_position(WEdit * edit,gboolean load_position)471 edit_load_position (WEdit * edit, gboolean load_position)
472 {
473 long line, column;
474 off_t offset;
475
476 if (edit->filename_vpath == NULL
477 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
478 return;
479
480 load_file_position (edit->filename_vpath, &line, &column, &offset, &edit->serialized_bookmarks);
481 /* apply bookmarks in any case */
482 book_mark_restore (edit, BOOK_MARK_COLOR);
483
484 if (!load_position)
485 return;
486
487 if (line > 0)
488 {
489 edit_move_to_line (edit, line - 1);
490 edit->prev_col = column;
491 }
492 else if (offset > 0)
493 {
494 edit_cursor_move (edit, offset);
495 line = edit->buffer.curs_line;
496 edit->search_start = edit->buffer.curs1;
497 }
498
499 edit_move_to_prev_col (edit, edit_buffer_get_current_bol (&edit->buffer));
500 edit_move_display (edit, line - (WIDGET (edit)->lines / 2));
501 }
502
503 /* --------------------------------------------------------------------------------------------- */
504 /** Save cursor position in the file */
505
506 static void
edit_save_position(WEdit * edit)507 edit_save_position (WEdit * edit)
508 {
509 if (edit->filename_vpath == NULL
510 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
511 return;
512
513 book_mark_serialize (edit, BOOK_MARK_COLOR);
514 save_file_position (edit->filename_vpath, edit->buffer.curs_line + 1, edit->curs_col,
515 edit->buffer.curs1, edit->serialized_bookmarks);
516 edit->serialized_bookmarks = NULL;
517 }
518
519 /* --------------------------------------------------------------------------------------------- */
520 /** Clean the WEdit stricture except the widget part */
521
522 static void
edit_purge_widget(WEdit * edit)523 edit_purge_widget (WEdit * edit)
524 {
525 size_t len = sizeof (WEdit) - sizeof (Widget);
526 char *start = (char *) edit + sizeof (Widget);
527 memset (start, 0, len);
528 }
529
530 /* --------------------------------------------------------------------------------------------- */
531
532 /*
533 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
534 then the file should be as it was when he loaded up. Then set edit->modified to 0.
535 */
536
537 static long
edit_pop_undo_action(WEdit * edit)538 edit_pop_undo_action (WEdit * edit)
539 {
540 long c;
541 unsigned long sp = edit->undo_stack_pointer;
542
543 if (sp == edit->undo_stack_bottom)
544 return STACK_BOTTOM;
545
546 sp = (sp - 1) & edit->undo_stack_size_mask;
547 c = edit->undo_stack[sp];
548 if (c >= 0)
549 {
550 /* edit->undo_stack[sp] = '@'; */
551 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
552 return c;
553 }
554
555 if (sp == edit->undo_stack_bottom)
556 return STACK_BOTTOM;
557
558 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
559 if (edit->undo_stack[sp] == -2)
560 {
561 /* edit->undo_stack[sp] = '@'; */
562 edit->undo_stack_pointer = sp;
563 }
564 else
565 edit->undo_stack[sp]++;
566
567 return c;
568 }
569
570 static long
edit_pop_redo_action(WEdit * edit)571 edit_pop_redo_action (WEdit * edit)
572 {
573 long c;
574 unsigned long sp = edit->redo_stack_pointer;
575
576 if (sp == edit->redo_stack_bottom)
577 return STACK_BOTTOM;
578
579 sp = (sp - 1) & edit->redo_stack_size_mask;
580 c = edit->redo_stack[sp];
581 if (c >= 0)
582 {
583 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
584 return c;
585 }
586
587 if (sp == edit->redo_stack_bottom)
588 return STACK_BOTTOM;
589
590 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
591 if (edit->redo_stack[sp] == -2)
592 edit->redo_stack_pointer = sp;
593 else
594 edit->redo_stack[sp]++;
595
596 return c;
597 }
598
599 static long
get_prev_undo_action(WEdit * edit)600 get_prev_undo_action (WEdit * edit)
601 {
602 long c;
603 unsigned long sp = edit->undo_stack_pointer;
604
605 if (sp == edit->undo_stack_bottom)
606 return STACK_BOTTOM;
607
608 sp = (sp - 1) & edit->undo_stack_size_mask;
609 c = edit->undo_stack[sp];
610 if (c >= 0)
611 return c;
612
613 if (sp == edit->undo_stack_bottom)
614 return STACK_BOTTOM;
615
616 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
617 return c;
618 }
619
620 /* --------------------------------------------------------------------------------------------- */
621 /** is called whenever a modification is made by one of the four routines below */
622
623 static void
edit_modification(WEdit * edit)624 edit_modification (WEdit * edit)
625 {
626 edit->caches_valid = FALSE;
627
628 /* raise lock when file modified */
629 if (!edit->modified && !edit->delete_file)
630 edit->locked = lock_file (edit->filename_vpath);
631 edit->modified = 1;
632 }
633
634 /* --------------------------------------------------------------------------------------------- */
635 /* high level cursor movement commands */
636 /* --------------------------------------------------------------------------------------------- */
637 /** check whether cursor is in indent part of line
638 *
639 * @param edit editor object
640 *
641 * @return TRUE if cursor is in indent, FALSE otherwise
642 */
643
644 static gboolean
is_in_indent(const edit_buffer_t * buf)645 is_in_indent (const edit_buffer_t * buf)
646 {
647 off_t p;
648
649 for (p = edit_buffer_get_current_bol (buf); p < buf->curs1; p++)
650 if (strchr (" \t", edit_buffer_get_byte (buf, p)) == NULL)
651 return FALSE;
652
653 return TRUE;
654 }
655
656 /* --------------------------------------------------------------------------------------------- */
657 /** check whether line in editor is blank or not
658 *
659 * @param edit editor object
660 * @param offset position in file
661 *
662 * @return TRUE if line in blank, FALSE otherwise
663 */
664
665 static gboolean
is_blank(const edit_buffer_t * buf,off_t offset)666 is_blank (const edit_buffer_t * buf, off_t offset)
667 {
668 off_t s, f;
669
670 s = edit_buffer_get_bol (buf, offset);
671 f = edit_buffer_get_eol (buf, offset) - 1;
672 while (s <= f)
673 {
674 int c;
675
676 c = edit_buffer_get_byte (buf, s++);
677 if (!isspace (c))
678 return FALSE;
679 }
680 return TRUE;
681 }
682
683 /* --------------------------------------------------------------------------------------------- */
684 /** returns the offset of line i */
685
686 static off_t
edit_find_line(WEdit * edit,long line)687 edit_find_line (WEdit * edit, long line)
688 {
689 long i, j = 0;
690 long m = 2000000000; /* what is the magic number? */
691
692 if (!edit->caches_valid)
693 {
694 memset (edit->line_numbers, 0, sizeof (edit->line_numbers));
695 memset (edit->line_offsets, 0, sizeof (edit->line_offsets));
696 /* three offsets that we *know* are line 0 at 0 and these two: */
697 edit->line_numbers[1] = edit->buffer.curs_line;
698 edit->line_offsets[1] = edit_buffer_get_current_bol (&edit->buffer);
699 edit->line_numbers[2] = edit->buffer.lines;
700 edit->line_offsets[2] = edit_buffer_get_bol (&edit->buffer, edit->buffer.size);
701 edit->caches_valid = TRUE;
702 }
703 if (line >= edit->buffer.lines)
704 return edit->line_offsets[2];
705 if (line <= 0)
706 return 0;
707 /* find the closest known point */
708 for (i = 0; i < N_LINE_CACHES; i++)
709 {
710 long n;
711
712 n = labs (edit->line_numbers[i] - line);
713 if (n < m)
714 {
715 m = n;
716 j = i;
717 }
718 }
719 if (m == 0)
720 return edit->line_offsets[j]; /* know the offset exactly */
721 if (m == 1 && j >= 3)
722 i = j; /* one line different - caller might be looping, so stay in this cache */
723 else
724 i = 3 + (rand () % (N_LINE_CACHES - 3));
725 if (line > edit->line_numbers[j])
726 edit->line_offsets[i] =
727 edit_buffer_get_forward_offset (&edit->buffer, edit->line_offsets[j],
728 line - edit->line_numbers[j], 0);
729 else
730 edit->line_offsets[i] =
731 edit_buffer_get_backward_offset (&edit->buffer, edit->line_offsets[j],
732 edit->line_numbers[j] - line);
733 edit->line_numbers[i] = line;
734 return edit->line_offsets[i];
735 }
736
737 /* --------------------------------------------------------------------------------------------- */
738 /** moves up until a blank line is reached, or until just
739 before a non-blank line is reached */
740
741 static void
edit_move_up_paragraph(WEdit * edit,gboolean do_scroll)742 edit_move_up_paragraph (WEdit * edit, gboolean do_scroll)
743 {
744 long i = 0;
745
746 if (edit->buffer.curs_line > 1)
747 {
748 if (!edit_line_is_blank (edit, edit->buffer.curs_line))
749 {
750 for (i = edit->buffer.curs_line - 1; i != 0; i--)
751 if (edit_line_is_blank (edit, i))
752 break;
753 }
754 else if (edit_line_is_blank (edit, edit->buffer.curs_line - 1))
755 {
756 for (i = edit->buffer.curs_line - 1; i != 0; i--)
757 if (!edit_line_is_blank (edit, i))
758 {
759 i++;
760 break;
761 }
762 }
763 else
764 {
765 for (i = edit->buffer.curs_line - 1; i != 0; i--)
766 if (edit_line_is_blank (edit, i))
767 break;
768 }
769 }
770
771 edit_move_up (edit, edit->buffer.curs_line - i, do_scroll);
772 }
773
774 /* --------------------------------------------------------------------------------------------- */
775 /** moves down until a blank line is reached, or until just
776 before a non-blank line is reached */
777
778 static void
edit_move_down_paragraph(WEdit * edit,gboolean do_scroll)779 edit_move_down_paragraph (WEdit * edit, gboolean do_scroll)
780 {
781 long i;
782
783 if (edit->buffer.curs_line >= edit->buffer.lines - 1)
784 i = edit->buffer.lines;
785 else if (!edit_line_is_blank (edit, edit->buffer.curs_line))
786 {
787 for (i = edit->buffer.curs_line + 1; i != 0; i++)
788 if (edit_line_is_blank (edit, i) || i >= edit->buffer.lines)
789 break;
790 }
791 else if (edit_line_is_blank (edit, edit->buffer.curs_line + 1))
792 {
793 for (i = edit->buffer.curs_line + 1; i != 0; i++)
794 if (!edit_line_is_blank (edit, i) || i > edit->buffer.lines)
795 {
796 i--;
797 break;
798 }
799 }
800 else
801 {
802 for (i = edit->buffer.curs_line + 1; i != 0; i++)
803 if (edit_line_is_blank (edit, i) || i >= edit->buffer.lines)
804 break;
805 }
806 edit_move_down (edit, i - edit->buffer.curs_line, do_scroll);
807 }
808
809 /* --------------------------------------------------------------------------------------------- */
810
811 static void
edit_begin_page(WEdit * edit)812 edit_begin_page (WEdit * edit)
813 {
814 edit_update_curs_row (edit);
815 edit_move_up (edit, edit->curs_row, FALSE);
816 }
817
818 /* --------------------------------------------------------------------------------------------- */
819
820 static void
edit_end_page(WEdit * edit)821 edit_end_page (WEdit * edit)
822 {
823 edit_update_curs_row (edit);
824 edit_move_down (edit, WIDGET (edit)->lines - edit->curs_row - 1, FALSE);
825 }
826
827
828 /* --------------------------------------------------------------------------------------------- */
829 /** goto beginning of text */
830
831 static void
edit_move_to_top(WEdit * edit)832 edit_move_to_top (WEdit * edit)
833 {
834 if (edit->buffer.curs_line != 0)
835 {
836 edit_cursor_move (edit, -edit->buffer.curs1);
837 edit_move_to_prev_col (edit, 0);
838 edit->force |= REDRAW_PAGE;
839 edit->search_start = 0;
840 edit_update_curs_row (edit);
841 }
842 }
843
844 /* --------------------------------------------------------------------------------------------- */
845 /** goto end of text */
846
847 static void
edit_move_to_bottom(WEdit * edit)848 edit_move_to_bottom (WEdit * edit)
849 {
850 if (edit->buffer.curs_line < edit->buffer.lines)
851 {
852 edit_move_down (edit, edit->buffer.lines - edit->curs_row, FALSE);
853 edit->start_display = edit->buffer.size;
854 edit->start_line = edit->buffer.lines;
855 edit_scroll_upward (edit, WIDGET (edit)->lines - 1);
856 edit->force |= REDRAW_PAGE;
857 }
858 }
859
860 /* --------------------------------------------------------------------------------------------- */
861 /** goto beginning of line */
862
863 static void
edit_cursor_to_bol(WEdit * edit)864 edit_cursor_to_bol (WEdit * edit)
865 {
866 edit_cursor_move (edit, edit_buffer_get_current_bol (&edit->buffer) - edit->buffer.curs1);
867 edit->search_start = edit->buffer.curs1;
868 edit->prev_col = edit_get_col (edit);
869 edit->over_col = 0;
870 }
871
872 /* --------------------------------------------------------------------------------------------- */
873 /** goto end of line */
874
875 static void
edit_cursor_to_eol(WEdit * edit)876 edit_cursor_to_eol (WEdit * edit)
877 {
878 edit_cursor_move (edit, edit_buffer_get_current_eol (&edit->buffer) - edit->buffer.curs1);
879 edit->search_start = edit->buffer.curs1;
880 edit->prev_col = edit_get_col (edit);
881 edit->over_col = 0;
882 }
883
884 /* --------------------------------------------------------------------------------------------- */
885
886 static unsigned long
my_type_of(int c)887 my_type_of (int c)
888 {
889 unsigned long x, r = 0;
890 const char *p, *q;
891 const char option_chars_move_whole_word[] =
892 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
893
894 if (c == 0)
895 return 0;
896 if (c == '!')
897 return 2;
898
899 if (g_ascii_isupper ((gchar) c))
900 c = 'A';
901 else if (g_ascii_islower ((gchar) c))
902 c = 'a';
903 else if (g_ascii_isalpha (c))
904 c = 'a';
905 else if (isdigit (c))
906 c = '0';
907 else if (isspace (c))
908 c = ' ';
909 q = strchr (option_chars_move_whole_word, c);
910 if (!q)
911 return 0xFFFFFFFFUL;
912 do
913 {
914 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
915 if (*p == '!')
916 x <<= 1;
917 r |= x;
918 }
919 while ((q = strchr (q + 1, c)));
920 return r;
921 }
922
923 /* --------------------------------------------------------------------------------------------- */
924
925 static void
edit_left_word_move(WEdit * edit,int s)926 edit_left_word_move (WEdit * edit, int s)
927 {
928 while (TRUE)
929 {
930 int c1, c2;
931
932 if (edit->column_highlight
933 && edit->mark1 != edit->mark2
934 && edit->over_col == 0
935 && edit->buffer.curs1 == edit_buffer_get_current_bol (&edit->buffer))
936 break;
937 edit_cursor_move (edit, -1);
938 if (edit->buffer.curs1 == 0)
939 break;
940 c1 = edit_buffer_get_previous_byte (&edit->buffer);
941 c2 = edit_buffer_get_current_byte (&edit->buffer);
942 if (c1 == '\n' || c2 == '\n')
943 break;
944 if ((my_type_of (c1) & my_type_of (c2)) == 0)
945 break;
946 if (isspace (c1) && !isspace (c2))
947 break;
948 if (s != 0 && !isspace (c1) && isspace (c2))
949 break;
950 }
951 }
952
953 /* --------------------------------------------------------------------------------------------- */
954
955 static void
edit_left_word_move_cmd(WEdit * edit)956 edit_left_word_move_cmd (WEdit * edit)
957 {
958 edit_left_word_move (edit, 0);
959 edit->force |= REDRAW_PAGE;
960 }
961
962 /* --------------------------------------------------------------------------------------------- */
963
964 static void
edit_right_word_move(WEdit * edit,int s)965 edit_right_word_move (WEdit * edit, int s)
966 {
967 while (TRUE)
968 {
969 int c1, c2;
970
971 if (edit->column_highlight
972 && edit->mark1 != edit->mark2
973 && edit->over_col == 0
974 && edit->buffer.curs1 == edit_buffer_get_current_eol (&edit->buffer))
975 break;
976 edit_cursor_move (edit, 1);
977 if (edit->buffer.curs1 >= edit->buffer.size)
978 break;
979 c1 = edit_buffer_get_previous_byte (&edit->buffer);
980 c2 = edit_buffer_get_current_byte (&edit->buffer);
981 if (c1 == '\n' || c2 == '\n')
982 break;
983 if ((my_type_of (c1) & my_type_of (c2)) == 0)
984 break;
985 if (isspace (c1) && !isspace (c2))
986 break;
987 if (s != 0 && !isspace (c1) && isspace (c2))
988 break;
989 }
990 }
991
992 /* --------------------------------------------------------------------------------------------- */
993
994 static void
edit_right_word_move_cmd(WEdit * edit)995 edit_right_word_move_cmd (WEdit * edit)
996 {
997 edit_right_word_move (edit, 0);
998 edit->force |= REDRAW_PAGE;
999 }
1000
1001 /* --------------------------------------------------------------------------------------------- */
1002
1003 static void
edit_right_char_move_cmd(WEdit * edit)1004 edit_right_char_move_cmd (WEdit * edit)
1005 {
1006 int char_length = 1;
1007 int c;
1008
1009 #ifdef HAVE_CHARSET
1010 if (edit->utf8)
1011 {
1012 c = edit_buffer_get_utf (&edit->buffer, edit->buffer.curs1, &char_length);
1013 if (char_length < 1)
1014 char_length = 1;
1015 }
1016 else
1017 #endif
1018 c = edit_buffer_get_current_byte (&edit->buffer);
1019
1020 if (option_cursor_beyond_eol && c == '\n')
1021 edit->over_col++;
1022 else
1023 edit_cursor_move (edit, char_length);
1024 }
1025
1026 /* --------------------------------------------------------------------------------------------- */
1027
1028 static void
edit_left_char_move_cmd(WEdit * edit)1029 edit_left_char_move_cmd (WEdit * edit)
1030 {
1031 int char_length = 1;
1032
1033 if (edit->column_highlight
1034 && option_cursor_beyond_eol
1035 && edit->mark1 != edit->mark2
1036 && edit->over_col == 0 && edit->buffer.curs1 == edit_buffer_get_current_bol (&edit->buffer))
1037 return;
1038 #ifdef HAVE_CHARSET
1039 if (edit->utf8)
1040 {
1041 edit_buffer_get_prev_utf (&edit->buffer, edit->buffer.curs1, &char_length);
1042 if (char_length < 1)
1043 char_length = 1;
1044 }
1045 #endif
1046
1047 if (option_cursor_beyond_eol && edit->over_col > 0)
1048 edit->over_col--;
1049 else
1050 edit_cursor_move (edit, -char_length);
1051 }
1052
1053 /* --------------------------------------------------------------------------------------------- */
1054 /** Up or down cursor moving.
1055 direction = TRUE - move up
1056 = FALSE - move down
1057 */
1058
1059 static void
edit_move_updown(WEdit * edit,long lines,gboolean do_scroll,gboolean direction)1060 edit_move_updown (WEdit * edit, long lines, gboolean do_scroll, gboolean direction)
1061 {
1062 long p;
1063 long l = direction ? edit->buffer.curs_line : edit->buffer.lines - edit->buffer.curs_line;
1064
1065 if (lines > l)
1066 lines = l;
1067
1068 if (lines == 0)
1069 return;
1070
1071 if (lines > 1)
1072 edit->force |= REDRAW_PAGE;
1073 if (do_scroll)
1074 {
1075 if (direction)
1076 edit_scroll_upward (edit, lines);
1077 else
1078 edit_scroll_downward (edit, lines);
1079 }
1080 p = edit_buffer_get_current_bol (&edit->buffer);
1081 p = direction ? edit_buffer_get_backward_offset (&edit->buffer, p, lines) :
1082 edit_buffer_get_forward_offset (&edit->buffer, p, lines, 0);
1083 edit_cursor_move (edit, p - edit->buffer.curs1);
1084 edit_move_to_prev_col (edit, p);
1085
1086 #ifdef HAVE_CHARSET
1087 /* search start of current multibyte char (like CJK) */
1088 if (edit->buffer.curs1 > 0 && edit->buffer.curs1 + 1 < edit->buffer.size
1089 && edit_buffer_get_current_byte (&edit->buffer) >= 256)
1090 {
1091 edit_right_char_move_cmd (edit);
1092 edit_left_char_move_cmd (edit);
1093 }
1094 #endif
1095
1096 edit->search_start = edit->buffer.curs1;
1097 edit->found_len = 0;
1098 }
1099
1100 /* --------------------------------------------------------------------------------------------- */
1101
1102 static void
edit_right_delete_word(WEdit * edit)1103 edit_right_delete_word (WEdit * edit)
1104 {
1105 while (edit->buffer.curs1 < edit->buffer.size)
1106 {
1107 int c1, c2;
1108
1109 c1 = edit_delete (edit, TRUE);
1110 c2 = edit_buffer_get_current_byte (&edit->buffer);
1111 if (c1 == '\n' || c2 == '\n')
1112 break;
1113 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1114 break;
1115 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1116 break;
1117 }
1118 }
1119
1120 /* --------------------------------------------------------------------------------------------- */
1121
1122 static void
edit_left_delete_word(WEdit * edit)1123 edit_left_delete_word (WEdit * edit)
1124 {
1125 while (edit->buffer.curs1 > 0)
1126 {
1127 int c1, c2;
1128
1129 c1 = edit_backspace (edit, TRUE);
1130 c2 = edit_buffer_get_previous_byte (&edit->buffer);
1131 if (c1 == '\n' || c2 == '\n')
1132 break;
1133 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1134 break;
1135 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1136 break;
1137 }
1138 }
1139
1140 /* --------------------------------------------------------------------------------------------- */
1141 /**
1142 the start column position is not recorded, and hence does not
1143 undo as it happed. But who would notice.
1144 */
1145
1146 static void
edit_do_undo(WEdit * edit)1147 edit_do_undo (WEdit * edit)
1148 {
1149 long ac;
1150 long count = 0;
1151
1152 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1153 edit->over_col = 0;
1154 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1155 {
1156 switch ((int) ac)
1157 {
1158 case STACK_BOTTOM:
1159 goto done_undo;
1160 case CURS_RIGHT:
1161 edit_cursor_move (edit, 1);
1162 break;
1163 case CURS_LEFT:
1164 edit_cursor_move (edit, -1);
1165 break;
1166 case BACKSPACE:
1167 case BACKSPACE_BR:
1168 edit_backspace (edit, TRUE);
1169 break;
1170 case DELCHAR:
1171 case DELCHAR_BR:
1172 edit_delete (edit, TRUE);
1173 break;
1174 case COLUMN_ON:
1175 edit->column_highlight = 1;
1176 break;
1177 case COLUMN_OFF:
1178 edit->column_highlight = 0;
1179 break;
1180 default:
1181 break;
1182 }
1183 if (ac >= 256 && ac < 512)
1184 edit_insert_ahead (edit, ac - 256);
1185 if (ac >= 0 && ac < 256)
1186 edit_insert (edit, ac);
1187
1188 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1189 {
1190 edit->mark1 = ac - MARK_1;
1191 edit->column1 =
1192 (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, edit->mark1),
1193 0, edit->mark1);
1194 }
1195 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1196 {
1197 edit->mark2 = ac - MARK_2;
1198 edit->column2 =
1199 (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, edit->mark2),
1200 0, edit->mark2);
1201 }
1202 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1203 {
1204 edit->end_mark_curs = ac - MARK_CURS;
1205 }
1206 if (count++)
1207 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1208 }
1209
1210 if (edit->start_display > ac - KEY_PRESS)
1211 {
1212 edit->start_line -=
1213 edit_buffer_count_lines (&edit->buffer, ac - KEY_PRESS, edit->start_display);
1214 edit->force |= REDRAW_PAGE;
1215 }
1216 else if (edit->start_display < ac - KEY_PRESS)
1217 {
1218 edit->start_line +=
1219 edit_buffer_count_lines (&edit->buffer, edit->start_display, ac - KEY_PRESS);
1220 edit->force |= REDRAW_PAGE;
1221 }
1222 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1223 edit_update_curs_row (edit);
1224
1225 done_undo:
1226 edit->undo_stack_disable = 0;
1227 }
1228
1229 /* --------------------------------------------------------------------------------------------- */
1230
1231 static void
edit_do_redo(WEdit * edit)1232 edit_do_redo (WEdit * edit)
1233 {
1234 long ac;
1235 long count = 0;
1236
1237 if (edit->redo_stack_reset)
1238 return;
1239
1240 edit->over_col = 0;
1241 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1242 {
1243 switch ((int) ac)
1244 {
1245 case STACK_BOTTOM:
1246 goto done_redo;
1247 case CURS_RIGHT:
1248 edit_cursor_move (edit, 1);
1249 break;
1250 case CURS_LEFT:
1251 edit_cursor_move (edit, -1);
1252 break;
1253 case BACKSPACE:
1254 edit_backspace (edit, TRUE);
1255 break;
1256 case DELCHAR:
1257 edit_delete (edit, TRUE);
1258 break;
1259 case COLUMN_ON:
1260 edit->column_highlight = 1;
1261 break;
1262 case COLUMN_OFF:
1263 edit->column_highlight = 0;
1264 break;
1265 default:
1266 break;
1267 }
1268 if (ac >= 256 && ac < 512)
1269 edit_insert_ahead (edit, ac - 256);
1270 if (ac >= 0 && ac < 256)
1271 edit_insert (edit, ac);
1272
1273 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1274 {
1275 edit->mark1 = ac - MARK_1;
1276 edit->column1 =
1277 (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, edit->mark1),
1278 0, edit->mark1);
1279 }
1280 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1281 {
1282 edit->mark2 = ac - MARK_2;
1283 edit->column2 =
1284 (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, edit->mark2),
1285 0, edit->mark2);
1286 }
1287 /* more than one pop usually means something big */
1288 if (count++)
1289 edit->force |= REDRAW_PAGE;
1290 }
1291
1292 if (edit->start_display > ac - KEY_PRESS)
1293 {
1294 edit->start_line -=
1295 edit_buffer_count_lines (&edit->buffer, ac - KEY_PRESS, edit->start_display);
1296 edit->force |= REDRAW_PAGE;
1297 }
1298 else if (edit->start_display < ac - KEY_PRESS)
1299 {
1300 edit->start_line +=
1301 edit_buffer_count_lines (&edit->buffer, edit->start_display, ac - KEY_PRESS);
1302 edit->force |= REDRAW_PAGE;
1303 }
1304 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1305 edit_update_curs_row (edit);
1306
1307 done_redo:
1308 ;
1309 }
1310
1311 /* --------------------------------------------------------------------------------------------- */
1312
1313 static void
edit_group_undo(WEdit * edit)1314 edit_group_undo (WEdit * edit)
1315 {
1316 long ac = KEY_PRESS;
1317 long cur_ac = KEY_PRESS;
1318 while (ac != STACK_BOTTOM && ac == cur_ac)
1319 {
1320 cur_ac = get_prev_undo_action (edit);
1321 edit_do_undo (edit);
1322 ac = get_prev_undo_action (edit);
1323 /* exit from cycle if option_group_undo is not set,
1324 * and make single UNDO operation
1325 */
1326 if (!option_group_undo)
1327 ac = STACK_BOTTOM;
1328 }
1329 }
1330
1331 /* --------------------------------------------------------------------------------------------- */
1332
1333 static void
edit_delete_to_line_end(WEdit * edit)1334 edit_delete_to_line_end (WEdit * edit)
1335 {
1336 while (edit_buffer_get_current_byte (&edit->buffer) != '\n' && edit->buffer.curs2 != 0)
1337 edit_delete (edit, TRUE);
1338 }
1339
1340 /* --------------------------------------------------------------------------------------------- */
1341
1342 static void
edit_delete_to_line_begin(WEdit * edit)1343 edit_delete_to_line_begin (WEdit * edit)
1344 {
1345 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n' && edit->buffer.curs1 != 0)
1346 edit_backspace (edit, TRUE);
1347 }
1348
1349 /* --------------------------------------------------------------------------------------------- */
1350
1351 static gboolean
is_aligned_on_a_tab(WEdit * edit)1352 is_aligned_on_a_tab (WEdit * edit)
1353 {
1354 long curs_col;
1355
1356 edit_update_curs_col (edit);
1357 curs_col = edit->curs_col % (TAB_SIZE * space_width);
1358 return (curs_col == 0 || curs_col == (HALF_TAB_SIZE * space_width));
1359 }
1360
1361 /* --------------------------------------------------------------------------------------------- */
1362
1363 static gboolean
right_of_four_spaces(WEdit * edit)1364 right_of_four_spaces (WEdit * edit)
1365 {
1366 int i, ch = 0;
1367
1368 for (i = 1; i <= HALF_TAB_SIZE; i++)
1369 ch |= edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1 - i);
1370
1371 return (ch == ' ' && is_aligned_on_a_tab (edit));
1372 }
1373
1374 /* --------------------------------------------------------------------------------------------- */
1375
1376 static gboolean
left_of_four_spaces(WEdit * edit)1377 left_of_four_spaces (WEdit * edit)
1378 {
1379 int i, ch = 0;
1380
1381 for (i = 0; i < HALF_TAB_SIZE; i++)
1382 ch |= edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1 + i);
1383
1384 return (ch == ' ' && is_aligned_on_a_tab (edit));
1385 }
1386
1387 /* --------------------------------------------------------------------------------------------- */
1388
1389 static void
edit_auto_indent(WEdit * edit)1390 edit_auto_indent (WEdit * edit)
1391 {
1392 off_t p;
1393
1394 p = edit->buffer.curs1;
1395 /* use the previous line as a template */
1396 p = edit_buffer_get_backward_offset (&edit->buffer, p, 1);
1397 /* copy the leading whitespace of the line */
1398 while (TRUE)
1399 { /* no range check - the line _is_ \n-terminated */
1400 char c;
1401
1402 c = edit_buffer_get_byte (&edit->buffer, p++);
1403 if (!whitespace (c))
1404 break;
1405 edit_insert (edit, c);
1406 }
1407 }
1408
1409 /* --------------------------------------------------------------------------------------------- */
1410
1411 static inline void
edit_double_newline(WEdit * edit)1412 edit_double_newline (WEdit * edit)
1413 {
1414 edit_insert (edit, '\n');
1415 if (edit_buffer_get_current_byte (&edit->buffer) == '\n'
1416 || edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1 - 2) == '\n')
1417 return;
1418 edit->force |= REDRAW_PAGE;
1419 edit_insert (edit, '\n');
1420 }
1421
1422 /* --------------------------------------------------------------------------------------------- */
1423
1424 static void
insert_spaces_tab(WEdit * edit,gboolean half)1425 insert_spaces_tab (WEdit * edit, gboolean half)
1426 {
1427 long i;
1428
1429 edit_update_curs_col (edit);
1430 i = option_tab_spacing * space_width;
1431 if (half)
1432 i /= 2;
1433 if (i != 0)
1434 {
1435 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1436 while (i > 0)
1437 {
1438 edit_insert (edit, ' ');
1439 i -= space_width;
1440 }
1441 }
1442 }
1443
1444 /* --------------------------------------------------------------------------------------------- */
1445
1446 static inline void
edit_tab_cmd(WEdit * edit)1447 edit_tab_cmd (WEdit * edit)
1448 {
1449 if (option_fake_half_tabs && is_in_indent (&edit->buffer))
1450 {
1451 /* insert a half tab (usually four spaces) unless there is a
1452 half tab already behind, then delete it and insert a
1453 full tab. */
1454 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1455 insert_spaces_tab (edit, TRUE);
1456 else
1457 {
1458 int i;
1459
1460 for (i = 1; i <= HALF_TAB_SIZE; i++)
1461 edit_backspace (edit, TRUE);
1462 edit_insert (edit, '\t');
1463 }
1464 }
1465 else if (option_fill_tabs_with_spaces)
1466 insert_spaces_tab (edit, FALSE);
1467 else
1468 edit_insert (edit, '\t');
1469 }
1470
1471 /* --------------------------------------------------------------------------------------------- */
1472
1473 static void
check_and_wrap_line(WEdit * edit)1474 check_and_wrap_line (WEdit * edit)
1475 {
1476 off_t curs;
1477
1478 if (!option_typewriter_wrap)
1479 return;
1480 edit_update_curs_col (edit);
1481 if (edit->curs_col < option_word_wrap_line_length)
1482 return;
1483 curs = edit->buffer.curs1;
1484 while (TRUE)
1485 {
1486 int c;
1487
1488 curs--;
1489 c = edit_buffer_get_byte (&edit->buffer, curs);
1490 if (c == '\n' || curs <= 0)
1491 {
1492 edit_insert (edit, '\n');
1493 return;
1494 }
1495 if (whitespace (c))
1496 {
1497 off_t current = edit->buffer.curs1;
1498 edit_cursor_move (edit, curs - edit->buffer.curs1 + 1);
1499 edit_insert (edit, '\n');
1500 edit_cursor_move (edit, current - edit->buffer.curs1 + 1);
1501 return;
1502 }
1503 }
1504 }
1505
1506 /* --------------------------------------------------------------------------------------------- */
1507 /** this find the matching bracket in either direction, and sets edit->bracket
1508 *
1509 * @param edit editor object
1510 * @param in_screen seach only on the current screen
1511 * @param furthest_bracket_search count of the bytes for search
1512 *
1513 * @return position of the found bracket (-1 if no match)
1514 */
1515
1516 static off_t
edit_get_bracket(WEdit * edit,gboolean in_screen,unsigned long furthest_bracket_search)1517 edit_get_bracket (WEdit * edit, gboolean in_screen, unsigned long furthest_bracket_search)
1518 {
1519 const char *const b = "{}{[][()(", *p;
1520 int i = 1, inc = -1, c, d, n = 0;
1521 unsigned long j = 0;
1522 off_t q;
1523
1524 edit_update_curs_row (edit);
1525 c = edit_buffer_get_current_byte (&edit->buffer);
1526 p = strchr (b, c);
1527 /* not on a bracket at all */
1528 if (p == NULL || *p == '\0')
1529 return -1;
1530 /* the matching bracket */
1531 d = p[1];
1532 /* going left or right? */
1533 if (strchr ("{[(", c) != NULL)
1534 inc = 1;
1535 /* no limit */
1536 if (furthest_bracket_search == 0)
1537 furthest_bracket_search--; /* ULONG_MAX */
1538 for (q = edit->buffer.curs1 + inc;; q += inc)
1539 {
1540 int a;
1541
1542 /* out of buffer? */
1543 if (q >= edit->buffer.size || q < 0)
1544 break;
1545 a = edit_buffer_get_byte (&edit->buffer, q);
1546 /* don't want to eat CPU */
1547 if (j++ > furthest_bracket_search)
1548 break;
1549 /* out of screen? */
1550 if (in_screen)
1551 {
1552 if (q < edit->start_display)
1553 break;
1554 /* count lines if searching downward */
1555 if (inc > 0 && a == '\n')
1556 if (n++ >= WIDGET (edit)->lines - edit->curs_row) /* out of screen */
1557 break;
1558 }
1559 /* count bracket depth */
1560 i += (a == c) - (a == d);
1561 /* return if bracket depth is zero */
1562 if (i == 0)
1563 return q;
1564 }
1565 /* no match */
1566 return -1;
1567 }
1568
1569 /* --------------------------------------------------------------------------------------------- */
1570
1571 static inline void
edit_goto_matching_bracket(WEdit * edit)1572 edit_goto_matching_bracket (WEdit * edit)
1573 {
1574 off_t q;
1575
1576 q = edit_get_bracket (edit, 0, 0);
1577 if (q >= 0)
1578 {
1579 edit->bracket = edit->buffer.curs1;
1580 edit->force |= REDRAW_PAGE;
1581 edit_cursor_move (edit, q - edit->buffer.curs1);
1582 }
1583 }
1584
1585 /* --------------------------------------------------------------------------------------------- */
1586
1587 static void
edit_move_block_to_right(WEdit * edit)1588 edit_move_block_to_right (WEdit * edit)
1589 {
1590 off_t start_mark, end_mark;
1591 long cur_bol, start_bol;
1592
1593 if (!eval_marks (edit, &start_mark, &end_mark))
1594 return;
1595
1596 start_bol = edit_buffer_get_bol (&edit->buffer, start_mark);
1597 cur_bol = edit_buffer_get_bol (&edit->buffer, end_mark - 1);
1598
1599 do
1600 {
1601 edit_cursor_move (edit, cur_bol - edit->buffer.curs1);
1602 if (!edit_line_is_blank (edit, edit->buffer.curs_line))
1603 {
1604 if (option_fill_tabs_with_spaces)
1605 insert_spaces_tab (edit, option_fake_half_tabs);
1606 else
1607 edit_insert (edit, '\t');
1608 edit_cursor_move (edit,
1609 edit_buffer_get_bol (&edit->buffer, cur_bol) - edit->buffer.curs1);
1610 }
1611
1612 if (cur_bol == 0)
1613 break;
1614
1615 cur_bol = edit_buffer_get_bol (&edit->buffer, cur_bol - 1);
1616 }
1617 while (cur_bol >= start_bol);
1618
1619 edit->force |= REDRAW_PAGE;
1620 }
1621
1622 /* --------------------------------------------------------------------------------------------- */
1623
1624 static void
edit_move_block_to_left(WEdit * edit)1625 edit_move_block_to_left (WEdit * edit)
1626 {
1627 off_t start_mark, end_mark;
1628 off_t cur_bol, start_bol;
1629
1630 if (!eval_marks (edit, &start_mark, &end_mark))
1631 return;
1632
1633 start_bol = edit_buffer_get_bol (&edit->buffer, start_mark);
1634 cur_bol = edit_buffer_get_bol (&edit->buffer, end_mark - 1);
1635
1636 do
1637 {
1638 int del_tab_width;
1639 int next_char;
1640
1641 edit_cursor_move (edit, cur_bol - edit->buffer.curs1);
1642
1643 if (option_fake_half_tabs)
1644 del_tab_width = HALF_TAB_SIZE;
1645 else
1646 del_tab_width = option_tab_spacing;
1647
1648 next_char = edit_buffer_get_current_byte (&edit->buffer);
1649 if (next_char == '\t')
1650 edit_delete (edit, TRUE);
1651 else if (next_char == ' ')
1652 {
1653 int i;
1654
1655 for (i = 0; i < del_tab_width; i++)
1656 {
1657 if (next_char == ' ')
1658 edit_delete (edit, TRUE);
1659 next_char = edit_buffer_get_current_byte (&edit->buffer);
1660 }
1661 }
1662
1663 if (cur_bol == 0)
1664 break;
1665
1666 cur_bol = edit_buffer_get_bol (&edit->buffer, cur_bol - 1);
1667 }
1668 while (cur_bol >= start_bol);
1669
1670 edit->force |= REDRAW_PAGE;
1671 }
1672
1673 /* --------------------------------------------------------------------------------------------- */
1674 /**
1675 * prints at the cursor
1676 * @return number of chars printed
1677 */
1678
1679 static size_t
edit_print_string(WEdit * e,const char * s)1680 edit_print_string (WEdit * e, const char *s)
1681 {
1682 size_t i = 0;
1683
1684 while (s[i] != '\0')
1685 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1686 e->force |= REDRAW_COMPLETELY;
1687 edit_update_screen (e);
1688 return i;
1689 }
1690
1691 /* --------------------------------------------------------------------------------------------- */
1692
1693 static off_t
edit_insert_column_from_file(WEdit * edit,int file,off_t * start_pos,off_t * end_pos,long * col1,long * col2)1694 edit_insert_column_from_file (WEdit * edit, int file, off_t * start_pos, off_t * end_pos,
1695 long *col1, long *col2)
1696 {
1697 off_t cursor;
1698 long col;
1699 off_t blocklen = -1, width = 0;
1700 unsigned char *data;
1701
1702 cursor = edit->buffer.curs1;
1703 col = edit_get_col (edit);
1704 data = g_malloc0 (TEMP_BUF_LEN);
1705
1706 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
1707 {
1708 off_t i;
1709 char *pn;
1710
1711 pn = strchr ((char *) data, '\n');
1712 width = pn == NULL ? blocklen : pn - (char *) data;
1713
1714 for (i = 0; i < blocklen; i++)
1715 {
1716 if (data[i] != '\n')
1717 edit_insert (edit, data[i]);
1718 else
1719 { /* fill in and move to next line */
1720 long l;
1721 off_t p;
1722
1723 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
1724 for (l = width - (edit_get_col (edit) - col); l > 0; l -= space_width)
1725 edit_insert (edit, ' ');
1726
1727 for (p = edit->buffer.curs1;; p++)
1728 {
1729 if (p == edit->buffer.size)
1730 {
1731 edit_cursor_move (edit, edit->buffer.size - edit->buffer.curs1);
1732 edit_insert_ahead (edit, '\n');
1733 p++;
1734 break;
1735 }
1736 if (edit_buffer_get_byte (&edit->buffer, p) == '\n')
1737 {
1738 p++;
1739 break;
1740 }
1741 }
1742
1743 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->buffer.curs1);
1744
1745 for (l = col - edit_get_col (edit); l >= space_width; l -= space_width)
1746 edit_insert (edit, ' ');
1747 }
1748 }
1749 }
1750 *col1 = col;
1751 *col2 = col + width;
1752 *start_pos = cursor;
1753 *end_pos = edit->buffer.curs1;
1754 edit_cursor_move (edit, cursor - edit->buffer.curs1);
1755 g_free (data);
1756
1757 return blocklen;
1758 }
1759
1760 /* --------------------------------------------------------------------------------------------- */
1761 /*** public functions ****************************************************************************/
1762 /* --------------------------------------------------------------------------------------------- */
1763
1764 /** User edit menu, like user menu (F2) but only in editor. */
1765
1766 void
user_menu(WEdit * edit,const char * menu_file,int selected_entry)1767 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1768 {
1769 char *block_file;
1770 gboolean nomark;
1771 off_t curs;
1772 off_t start_mark, end_mark;
1773 struct stat status;
1774 vfs_path_t *block_file_vpath;
1775
1776 block_file = mc_config_get_full_path (EDIT_HOME_BLOCK_FILE);
1777 block_file_vpath = vfs_path_from_str (block_file);
1778 curs = edit->buffer.curs1;
1779 nomark = !eval_marks (edit, &start_mark, &end_mark);
1780 if (!nomark)
1781 edit_save_block (edit, block_file, start_mark, end_mark);
1782
1783 /* run shell scripts from menu */
1784 if (user_menu_cmd (edit, menu_file, selected_entry)
1785 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1786 {
1787 int rc = 0;
1788 FILE *fd;
1789
1790 /* i.e. we have marked block */
1791 if (!nomark)
1792 rc = edit_block_delete_cmd (edit);
1793
1794 if (rc == 0)
1795 {
1796 off_t ins_len;
1797
1798 ins_len = edit_insert_file (edit, block_file_vpath);
1799 if (!nomark && ins_len > 0)
1800 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1801 }
1802 /* truncate block file */
1803 fd = fopen (block_file, "w");
1804 if (fd != NULL)
1805 fclose (fd);
1806 }
1807 g_free (block_file);
1808 vfs_path_free (block_file_vpath, TRUE);
1809
1810 edit_cursor_move (edit, curs - edit->buffer.curs1);
1811 edit->force |= REDRAW_PAGE;
1812 widget_draw (WIDGET (edit));
1813 }
1814
1815 /* --------------------------------------------------------------------------------------------- */
1816
1817 char *
edit_get_write_filter(const vfs_path_t * write_name_vpath,const vfs_path_t * filename_vpath)1818 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
1819 {
1820 int i;
1821 char *p, *writename;
1822 const vfs_path_element_t *path_element;
1823
1824 i = edit_find_filter (filename_vpath);
1825 if (i < 0)
1826 return NULL;
1827
1828 path_element = vfs_path_get_by_index (write_name_vpath, -1);
1829 writename = name_quote (path_element->path, FALSE);
1830 p = g_strdup_printf (all_filters[i].write, writename);
1831 g_free (writename);
1832 return p;
1833 }
1834
1835 /* --------------------------------------------------------------------------------------------- */
1836 /**
1837 * @param edit editor object
1838 * @param f value of stream file
1839 * @return the length of the file
1840 */
1841
1842 off_t
edit_write_stream(WEdit * edit,FILE * f)1843 edit_write_stream (WEdit * edit, FILE * f)
1844 {
1845 long i;
1846
1847 if (edit->lb == LB_ASIS)
1848 {
1849 for (i = 0; i < edit->buffer.size; i++)
1850 if (fputc (edit_buffer_get_byte (&edit->buffer, i), f) < 0)
1851 break;
1852 return i;
1853 }
1854
1855 /* change line breaks */
1856 for (i = 0; i < edit->buffer.size; i++)
1857 {
1858 unsigned char c;
1859
1860 c = edit_buffer_get_byte (&edit->buffer, i);
1861 if (!(c == '\n' || c == '\r'))
1862 {
1863 /* not line break */
1864 if (fputc (c, f) < 0)
1865 return i;
1866 }
1867 else
1868 { /* (c == '\n' || c == '\r') */
1869 unsigned char c1;
1870
1871 c1 = edit_buffer_get_byte (&edit->buffer, i + 1); /* next char */
1872
1873 switch (edit->lb)
1874 {
1875 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1876 /* put one line break unconditionally */
1877 if (fputc ('\n', f) < 0)
1878 return i;
1879
1880 i++; /* 2 chars are processed */
1881
1882 if (c == '\r' && c1 == '\n')
1883 /* Windows line break; go to the next char */
1884 break;
1885
1886 if (c == '\r' && c1 == '\r')
1887 {
1888 /* two Macintosh line breaks; put second line break */
1889 if (fputc ('\n', f) < 0)
1890 return i;
1891 break;
1892 }
1893
1894 if (fputc (c1, f) < 0)
1895 return i;
1896 break;
1897
1898 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
1899 /* put one line break unconditionally */
1900 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
1901 return i;
1902
1903 if (c == '\r' && c1 == '\n')
1904 /* Windows line break; go to the next char */
1905 i++;
1906 break;
1907
1908 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
1909 /* put one line break unconditionally */
1910 if (fputc ('\r', f) < 0)
1911 return i;
1912
1913 i++; /* 2 chars are processed */
1914
1915 if (c == '\r' && c1 == '\n')
1916 /* Windows line break; go to the next char */
1917 break;
1918
1919 if (c == '\n' && c1 == '\n')
1920 {
1921 /* two Windows line breaks; put second line break */
1922 if (fputc ('\r', f) < 0)
1923 return i;
1924 break;
1925 }
1926
1927 if (fputc (c1, f) < 0)
1928 return i;
1929 break;
1930 case LB_ASIS: /* default without changes */
1931 default:
1932 break;
1933 }
1934 }
1935 }
1936
1937 return edit->buffer.size;
1938 }
1939
1940 /* --------------------------------------------------------------------------------------------- */
1941
1942 gboolean
is_break_char(char c)1943 is_break_char (char c)
1944 {
1945 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
1946 }
1947
1948 /* --------------------------------------------------------------------------------------------- */
1949 /** inserts a file at the cursor, returns count of inserted bytes on success */
1950
1951 off_t
edit_insert_file(WEdit * edit,const vfs_path_t * filename_vpath)1952 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
1953 {
1954 char *p;
1955 off_t current;
1956 off_t ins_len = 0;
1957
1958 p = edit_get_filter (filename_vpath);
1959 current = edit->buffer.curs1;
1960
1961 if (p != NULL)
1962 {
1963 FILE *f;
1964
1965 f = (FILE *) popen (p, "r");
1966 if (f != NULL)
1967 {
1968 edit_insert_stream (edit, f);
1969
1970 /* Place cursor at the end of text selection */
1971 if (!option_cursor_after_inserted_block)
1972 {
1973 ins_len = edit->buffer.curs1 - current;
1974 edit_cursor_move (edit, -ins_len);
1975 }
1976 if (pclose (f) > 0)
1977 {
1978 char *errmsg;
1979
1980 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
1981 edit_error_dialog (_("Error"), errmsg);
1982 g_free (errmsg);
1983 ins_len = -1;
1984 }
1985 }
1986 else
1987 {
1988 char *errmsg;
1989
1990 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
1991 edit_error_dialog (_("Error"), errmsg);
1992 g_free (errmsg);
1993 ins_len = -1;
1994 }
1995 g_free (p);
1996 }
1997 else
1998 {
1999 int file;
2000 off_t blocklen;
2001 int vertical_insertion = 0;
2002 char *buf;
2003
2004 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2005 if (file == -1)
2006 return -1;
2007
2008 buf = g_malloc0 (TEMP_BUF_LEN);
2009 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2010 if (blocklen > 0)
2011 {
2012 /* if contain signature VERTICAL_MAGIC then it vertical block */
2013 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2014 vertical_insertion = 1;
2015 else
2016 mc_lseek (file, 0, SEEK_SET);
2017 }
2018
2019 if (vertical_insertion)
2020 {
2021 off_t mark1, mark2;
2022 long c1, c2;
2023
2024 blocklen = edit_insert_column_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2025 edit_set_markers (edit, edit->buffer.curs1, mark2, c1, c2);
2026
2027 /* highlight inserted text then not persistent blocks */
2028 if (!option_persistent_selections && edit->modified)
2029 {
2030 if (!edit->column_highlight)
2031 edit_push_undo_action (edit, COLUMN_OFF);
2032 edit->column_highlight = 1;
2033 }
2034 }
2035 else
2036 {
2037 off_t i;
2038
2039 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2040 {
2041 for (i = 0; i < blocklen; i++)
2042 edit_insert (edit, buf[i]);
2043 }
2044 /* highlight inserted text then not persistent blocks */
2045 if (!option_persistent_selections && edit->modified)
2046 {
2047 edit_set_markers (edit, edit->buffer.curs1, current, 0, 0);
2048 if (edit->column_highlight)
2049 edit_push_undo_action (edit, COLUMN_ON);
2050 edit->column_highlight = 0;
2051 }
2052
2053 /* Place cursor at the end of text selection */
2054 if (!option_cursor_after_inserted_block)
2055 {
2056 ins_len = edit->buffer.curs1 - current;
2057 edit_cursor_move (edit, -ins_len);
2058 }
2059 }
2060
2061 edit->force |= REDRAW_PAGE;
2062 g_free (buf);
2063 mc_close (file);
2064 if (blocklen != 0)
2065 ins_len = 0;
2066 }
2067
2068 return ins_len;
2069 }
2070
2071 /* --------------------------------------------------------------------------------------------- */
2072 /**
2073 * Fill in the edit structure. Return NULL on failure. Pass edit as
2074 * NULL to allocate a new structure.
2075 *
2076 * If line is 0, try to restore saved position. Otherwise put the
2077 * cursor on that line and show it in the middle of the screen.
2078 */
2079
2080 WEdit *
edit_init(WEdit * edit,int y,int x,int lines,int cols,const vfs_path_t * filename_vpath,long line)2081 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
2082 long line)
2083 {
2084 gboolean to_free = FALSE;
2085
2086 option_auto_syntax = TRUE; /* Resetting to auto on every invokation */
2087 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
2088
2089 if (edit != NULL)
2090 {
2091 gboolean fullscreen;
2092 WRect loc_prev;
2093
2094 /* save some widget parameters */
2095 fullscreen = edit->fullscreen;
2096 loc_prev = edit->loc_prev;
2097
2098 edit_purge_widget (edit);
2099
2100 /* restore saved parameters */
2101 edit->fullscreen = fullscreen;
2102 edit->loc_prev = loc_prev;
2103 }
2104 else
2105 {
2106 Widget *w;
2107 edit = g_malloc0 (sizeof (WEdit));
2108 to_free = TRUE;
2109
2110 w = WIDGET (edit);
2111 widget_init (w, y, x, lines, cols, NULL, NULL);
2112 w->options |= WOP_SELECTABLE | WOP_TOP_SELECT | WOP_WANT_CURSOR;
2113 w->keymap = editor_map;
2114 w->ext_keymap = editor_x_map;
2115 edit->fullscreen = TRUE;
2116 edit_save_size (edit);
2117 }
2118
2119 edit->drag_state = MCEDIT_DRAG_NONE;
2120
2121 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2122 edit->stat1.st_uid = getuid ();
2123 edit->stat1.st_gid = getgid ();
2124 edit->stat1.st_mtime = 0;
2125
2126 edit->over_col = 0;
2127 edit->bracket = -1;
2128 edit->last_bracket = -1;
2129 edit->force |= REDRAW_PAGE;
2130
2131 /* set file name before load file */
2132 edit_set_filename (edit, filename_vpath);
2133
2134 edit->undo_stack_size = START_STACK_SIZE;
2135 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2136 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2137
2138 edit->redo_stack_size = START_STACK_SIZE;
2139 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2140 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2141
2142 #ifdef HAVE_CHARSET
2143 edit->utf8 = FALSE;
2144 edit->converter = str_cnv_from_term;
2145 edit_set_codeset (edit);
2146 #endif
2147
2148 if (!edit_load_file (edit))
2149 {
2150 /* edit_load_file already gives an error message */
2151 if (to_free)
2152 g_free (edit);
2153 return NULL;
2154 }
2155
2156 edit->loading_done = 1;
2157 edit->modified = 0;
2158 edit->locked = 0;
2159 edit_load_syntax (edit, NULL, NULL);
2160 edit_get_syntax_color (edit, -1);
2161
2162 /* load saved cursor position and/or boolmarks */
2163 if ((line == 0) && option_save_position)
2164 edit_load_position (edit, TRUE);
2165 else
2166 {
2167 edit_load_position (edit, FALSE);
2168 if (line <= 0)
2169 line = 1;
2170 edit_move_display (edit, line - 1);
2171 edit_move_to_line (edit, line - 1);
2172 }
2173
2174 edit_load_macro_cmd (edit);
2175
2176 return edit;
2177 }
2178
2179 /* --------------------------------------------------------------------------------------------- */
2180
2181 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2182 gboolean
edit_clean(WEdit * edit)2183 edit_clean (WEdit * edit)
2184 {
2185 if (edit == NULL)
2186 return FALSE;
2187
2188 /* a stale lock, remove it */
2189 if (edit->locked)
2190 (void) unlock_file (edit->filename_vpath);
2191
2192 /* save cursor position */
2193 if (option_save_position)
2194 edit_save_position (edit);
2195 else if (edit->serialized_bookmarks != NULL)
2196 g_array_free (edit->serialized_bookmarks, TRUE);
2197
2198 /* File specified on the mcedit command line and never saved */
2199 if (edit->delete_file)
2200 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2201
2202 edit_free_syntax_rules (edit);
2203 book_mark_flush (edit, -1);
2204
2205 edit_buffer_clean (&edit->buffer);
2206
2207 g_free (edit->undo_stack);
2208 g_free (edit->redo_stack);
2209 vfs_path_free (edit->filename_vpath, TRUE);
2210 vfs_path_free (edit->dir_vpath, TRUE);
2211 edit_search_deinit (edit);
2212
2213 #ifdef HAVE_CHARSET
2214 if (edit->converter != str_cnv_from_term)
2215 str_close_conv (edit->converter);
2216 #endif
2217
2218 edit_purge_widget (edit);
2219
2220 return TRUE;
2221 }
2222
2223 /* --------------------------------------------------------------------------------------------- */
2224
2225 /**
2226 * Load a new file into the editor and set line. If it fails, preserve the old file.
2227 * To do it, allocate a new widget, initialize it and, if the new file
2228 * was loaded, copy the data to the old widget.
2229 *
2230 * @return TRUE on success, FALSE on failure.
2231 */
2232 gboolean
edit_reload_line(WEdit * edit,const vfs_path_t * filename_vpath,long line)2233 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2234 {
2235 Widget *w = WIDGET (edit);
2236 WEdit *e;
2237
2238 e = g_malloc0 (sizeof (WEdit));
2239 *WIDGET (e) = *w;
2240 /* save some widget parameters */
2241 e->fullscreen = edit->fullscreen;
2242 e->loc_prev = edit->loc_prev;
2243
2244 if (edit_init (e, w->y, w->x, w->lines, w->cols, filename_vpath, line) == NULL)
2245 {
2246 g_free (e);
2247 return FALSE;
2248 }
2249
2250 edit_clean (edit);
2251 memcpy (edit, e, sizeof (*edit));
2252 g_free (e);
2253
2254 return TRUE;
2255 }
2256
2257 /* --------------------------------------------------------------------------------------------- */
2258
2259 #ifdef HAVE_CHARSET
2260 void
edit_set_codeset(WEdit * edit)2261 edit_set_codeset (WEdit * edit)
2262 {
2263 const char *cp_id;
2264
2265 cp_id =
2266 get_codepage_id (mc_global.source_codepage >=
2267 0 ? mc_global.source_codepage : mc_global.display_codepage);
2268
2269 if (cp_id != NULL)
2270 {
2271 GIConv conv;
2272 conv = str_crt_conv_from (cp_id);
2273 if (conv != INVALID_CONV)
2274 {
2275 if (edit->converter != str_cnv_from_term)
2276 str_close_conv (edit->converter);
2277 edit->converter = conv;
2278 }
2279 }
2280
2281 if (cp_id != NULL)
2282 edit->utf8 = str_isutf8 (cp_id);
2283 }
2284 #endif
2285
2286 /* --------------------------------------------------------------------------------------------- */
2287
2288 /**
2289 * Recording stack for undo:
2290 * The following is an implementation of a compressed stack. Identical
2291 * pushes are recorded by a negative prefix indicating the number of times the
2292 * same char was pushed. This saves space for repeated curs-left or curs-right
2293 * delete etc.
2294 *
2295 * eg:
2296 *
2297 * pushed: stored:
2298 *
2299 * a
2300 * b a
2301 * b -3
2302 * b b
2303 * c --> -4
2304 * c c
2305 * c d
2306 * c
2307 * d
2308 *
2309 * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2310 * 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2311 * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2312 * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2313 * position.
2314 *
2315 * The only way the cursor moves or the buffer is changed is through the routines:
2316 * insert, backspace, insert_ahead, delete, and cursor_move.
2317 * These record the reverse undo movements onto the stack each time they are
2318 * called.
2319 *
2320 * Each key press results in a set of actions (insert; delete ...). So each time
2321 * a key is pressed the current position of start_display is pushed as
2322 * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2323 * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2324 * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2325 *
2326 *
2327 *
2328 * @param edit editor object
2329 * @param c code of the action
2330 */
2331
2332 void
edit_push_undo_action(WEdit * edit,long c)2333 edit_push_undo_action (WEdit * edit, long c)
2334 {
2335 unsigned long sp = edit->undo_stack_pointer;
2336 unsigned long spm1;
2337 long *t;
2338
2339 /* first enlarge the stack if necessary */
2340 if (sp > edit->undo_stack_size - 10)
2341 { /* say */
2342 if (option_max_undo < 256)
2343 option_max_undo = 256;
2344 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2345 {
2346 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2347 if (t)
2348 {
2349 edit->undo_stack = t;
2350 edit->undo_stack_size <<= 1;
2351 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2352 }
2353 }
2354 }
2355 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2356 if (edit->undo_stack_disable)
2357 {
2358 edit_push_redo_action (edit, KEY_PRESS);
2359 edit_push_redo_action (edit, c);
2360 return;
2361 }
2362
2363 if (edit->redo_stack_reset)
2364 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2365
2366 if (edit->undo_stack_bottom != sp
2367 && spm1 != edit->undo_stack_bottom
2368 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2369 {
2370 long d;
2371 if (edit->undo_stack[spm1] < 0)
2372 {
2373 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2374 if (d == c && edit->undo_stack[spm1] > -1000000000)
2375 {
2376 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2377 edit->undo_stack[spm1]--;
2378 return;
2379 }
2380 }
2381 else
2382 {
2383 d = edit->undo_stack[spm1];
2384 if (d == c)
2385 {
2386 if (c >= KEY_PRESS)
2387 return; /* --> no need to push multiple do-nothings */
2388 edit->undo_stack[sp] = -2;
2389 goto check_bottom;
2390 }
2391 }
2392 }
2393 edit->undo_stack[sp] = c;
2394
2395 check_bottom:
2396 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2397
2398 /* if the sp wraps round and catches the undo_stack_bottom then erase
2399 * the first set of actions on the stack to make space - by moving
2400 * undo_stack_bottom forward one "key press" */
2401 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2402 if ((unsigned long) c == edit->undo_stack_bottom ||
2403 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2404 do
2405 {
2406 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2407 }
2408 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2409 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2410
2411 /*If a single key produced enough pushes to wrap all the way round then we would notice that the [undo_stack_bottom] does not contain KEY_PRESS. The stack is then initialised: */
2412 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2413 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2414 {
2415 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2416 }
2417 }
2418
2419 /* --------------------------------------------------------------------------------------------- */
2420
2421 void
edit_push_redo_action(WEdit * edit,long c)2422 edit_push_redo_action (WEdit * edit, long c)
2423 {
2424 unsigned long sp = edit->redo_stack_pointer;
2425 unsigned long spm1;
2426 long *t;
2427 /* first enlarge the stack if necessary */
2428 if (sp > edit->redo_stack_size - 10)
2429 { /* say */
2430 if (option_max_undo < 256)
2431 option_max_undo = 256;
2432 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2433 {
2434 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2435 if (t)
2436 {
2437 edit->redo_stack = t;
2438 edit->redo_stack_size <<= 1;
2439 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2440 }
2441 }
2442 }
2443 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2444
2445 if (edit->redo_stack_bottom != sp
2446 && spm1 != edit->redo_stack_bottom
2447 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2448 {
2449 long d;
2450 if (edit->redo_stack[spm1] < 0)
2451 {
2452 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2453 if (d == c && edit->redo_stack[spm1] > -1000000000)
2454 {
2455 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2456 edit->redo_stack[spm1]--;
2457 return;
2458 }
2459 }
2460 else
2461 {
2462 d = edit->redo_stack[spm1];
2463 if (d == c)
2464 {
2465 if (c >= KEY_PRESS)
2466 return; /* --> no need to push multiple do-nothings */
2467 edit->redo_stack[sp] = -2;
2468 goto redo_check_bottom;
2469 }
2470 }
2471 }
2472 edit->redo_stack[sp] = c;
2473
2474 redo_check_bottom:
2475 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2476
2477 /* if the sp wraps round and catches the redo_stack_bottom then erase
2478 * the first set of actions on the stack to make space - by moving
2479 * redo_stack_bottom forward one "key press" */
2480 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2481 if ((unsigned long) c == edit->redo_stack_bottom ||
2482 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2483 do
2484 {
2485 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2486 }
2487 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2488 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2489
2490 /*
2491 * If a single key produced enough pushes to wrap all the way round then
2492 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2493 * The stack is then initialised:
2494 */
2495
2496 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2497 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2498 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2499 }
2500
2501 /* --------------------------------------------------------------------------------------------- */
2502 /**
2503 Basic low level single character buffer alterations and movements at the cursor.
2504 */
2505
2506 void
edit_insert(WEdit * edit,int c)2507 edit_insert (WEdit * edit, int c)
2508 {
2509 /* first we must update the position of the display window */
2510 if (edit->buffer.curs1 < edit->start_display)
2511 {
2512 edit->start_display++;
2513 if (c == '\n')
2514 edit->start_line++;
2515 }
2516
2517 /* Mark file as modified, unless the file hasn't been fully loaded */
2518 if (edit->loading_done)
2519 edit_modification (edit);
2520
2521 /* now we must update some info on the file and check if a redraw is required */
2522 if (c == '\n')
2523 {
2524 book_mark_inc (edit, edit->buffer.curs_line);
2525 edit->buffer.curs_line++;
2526 edit->buffer.lines++;
2527 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2528 }
2529
2530 /* save the reverse command onto the undo stack */
2531 /* ordinary char and not space */
2532 if (c > 32)
2533 edit_push_undo_action (edit, BACKSPACE);
2534 else
2535 edit_push_undo_action (edit, BACKSPACE_BR);
2536 /* update markers */
2537 edit->mark1 += (edit->mark1 > edit->buffer.curs1) ? 1 : 0;
2538 edit->mark2 += (edit->mark2 > edit->buffer.curs1) ? 1 : 0;
2539 edit->last_get_rule += (edit->last_get_rule > edit->buffer.curs1) ? 1 : 0;
2540
2541 edit_buffer_insert (&edit->buffer, c);
2542 }
2543
2544 /* --------------------------------------------------------------------------------------------- */
2545 /** same as edit_insert and move left */
2546
2547 void
edit_insert_ahead(WEdit * edit,int c)2548 edit_insert_ahead (WEdit * edit, int c)
2549 {
2550 if (edit->buffer.curs1 < edit->start_display)
2551 {
2552 edit->start_display++;
2553 if (c == '\n')
2554 edit->start_line++;
2555 }
2556 edit_modification (edit);
2557 if (c == '\n')
2558 {
2559 book_mark_inc (edit, edit->buffer.curs_line);
2560 edit->buffer.lines++;
2561 edit->force |= REDRAW_AFTER_CURSOR;
2562 }
2563 /* ordinary char and not space */
2564 if (c > 32)
2565 edit_push_undo_action (edit, DELCHAR);
2566 else
2567 edit_push_undo_action (edit, DELCHAR_BR);
2568
2569 edit->mark1 += (edit->mark1 >= edit->buffer.curs1) ? 1 : 0;
2570 edit->mark2 += (edit->mark2 >= edit->buffer.curs1) ? 1 : 0;
2571 edit->last_get_rule += (edit->last_get_rule >= edit->buffer.curs1) ? 1 : 0;
2572
2573 edit_buffer_insert_ahead (&edit->buffer, c);
2574 }
2575
2576 /* --------------------------------------------------------------------------------------------- */
2577
2578 void
edit_insert_over(WEdit * edit)2579 edit_insert_over (WEdit * edit)
2580 {
2581 long i;
2582
2583 for (i = 0; i < edit->over_col; i++)
2584 edit_insert (edit, ' ');
2585
2586 edit->over_col = 0;
2587 }
2588
2589 /* --------------------------------------------------------------------------------------------- */
2590
2591 int
edit_delete(WEdit * edit,gboolean byte_delete)2592 edit_delete (WEdit * edit, gboolean byte_delete)
2593 {
2594 int p = 0;
2595 int char_length = 1;
2596 int i;
2597
2598 if (edit->buffer.curs2 == 0)
2599 return 0;
2600
2601 #ifdef HAVE_CHARSET
2602 /* if byte_delete == TRUE then delete only one byte not multibyte char */
2603 if (edit->utf8 && !byte_delete)
2604 {
2605 edit_buffer_get_utf (&edit->buffer, edit->buffer.curs1, &char_length);
2606 if (char_length < 1)
2607 char_length = 1;
2608 }
2609 #else
2610 (void) byte_delete;
2611 #endif
2612
2613 if (edit->mark2 != edit->mark1)
2614 edit_push_markers (edit);
2615
2616 for (i = 1; i <= char_length; i++)
2617 {
2618 if (edit->mark1 > edit->buffer.curs1)
2619 {
2620 edit->mark1--;
2621 edit->end_mark_curs--;
2622 }
2623 if (edit->mark2 > edit->buffer.curs1)
2624 edit->mark2--;
2625 if (edit->last_get_rule > edit->buffer.curs1)
2626 edit->last_get_rule--;
2627
2628 p = edit_buffer_delete (&edit->buffer);
2629
2630 edit_push_undo_action (edit, p + 256);
2631 }
2632
2633 edit_modification (edit);
2634 if (p == '\n')
2635 {
2636 book_mark_dec (edit, edit->buffer.curs_line);
2637 edit->buffer.lines--;
2638 edit->force |= REDRAW_AFTER_CURSOR;
2639 }
2640 if (edit->buffer.curs1 < edit->start_display)
2641 {
2642 edit->start_display--;
2643 if (p == '\n')
2644 edit->start_line--;
2645 }
2646
2647 return p;
2648 }
2649
2650 /* --------------------------------------------------------------------------------------------- */
2651
2652 int
edit_backspace(WEdit * edit,gboolean byte_delete)2653 edit_backspace (WEdit * edit, gboolean byte_delete)
2654 {
2655 int p = 0;
2656 int char_length = 1;
2657 int i;
2658
2659 if (edit->buffer.curs1 == 0)
2660 return 0;
2661
2662 if (edit->mark2 != edit->mark1)
2663 edit_push_markers (edit);
2664
2665 #ifdef HAVE_CHARSET
2666 if (edit->utf8 && !byte_delete)
2667 {
2668 edit_buffer_get_prev_utf (&edit->buffer, edit->buffer.curs1, &char_length);
2669 if (char_length < 1)
2670 char_length = 1;
2671 }
2672 #else
2673 (void) byte_delete;
2674 #endif
2675
2676 for (i = 1; i <= char_length; i++)
2677 {
2678 if (edit->mark1 >= edit->buffer.curs1)
2679 {
2680 edit->mark1--;
2681 edit->end_mark_curs--;
2682 }
2683 if (edit->mark2 >= edit->buffer.curs1)
2684 edit->mark2--;
2685 if (edit->last_get_rule >= edit->buffer.curs1)
2686 edit->last_get_rule--;
2687
2688 p = edit_buffer_backspace (&edit->buffer);
2689
2690 edit_push_undo_action (edit, p);
2691 }
2692 edit_modification (edit);
2693 if (p == '\n')
2694 {
2695 book_mark_dec (edit, edit->buffer.curs_line);
2696 edit->buffer.curs_line--;
2697 edit->buffer.lines--;
2698 edit->force |= REDRAW_AFTER_CURSOR;
2699 }
2700
2701 if (edit->buffer.curs1 < edit->start_display)
2702 {
2703 edit->start_display--;
2704 if (p == '\n')
2705 edit->start_line--;
2706 }
2707
2708 return p;
2709 }
2710
2711 /* --------------------------------------------------------------------------------------------- */
2712 /** moves the cursor right or left: increment positive or negative respectively */
2713
2714 void
edit_cursor_move(WEdit * edit,off_t increment)2715 edit_cursor_move (WEdit * edit, off_t increment)
2716 {
2717 if (increment < 0)
2718 {
2719 for (; increment < 0 && edit->buffer.curs1 != 0; increment++)
2720 {
2721 int c;
2722
2723 edit_push_undo_action (edit, CURS_RIGHT);
2724
2725 c = edit_buffer_get_previous_byte (&edit->buffer);
2726 edit_buffer_insert_ahead (&edit->buffer, c);
2727 c = edit_buffer_backspace (&edit->buffer);
2728 if (c == '\n')
2729 {
2730 edit->buffer.curs_line--;
2731 edit->force |= REDRAW_LINE_BELOW;
2732 }
2733 }
2734 }
2735 else
2736 {
2737 for (; increment > 0 && edit->buffer.curs2 != 0; increment--)
2738 {
2739 int c;
2740
2741 edit_push_undo_action (edit, CURS_LEFT);
2742
2743 c = edit_buffer_get_current_byte (&edit->buffer);
2744 edit_buffer_insert (&edit->buffer, c);
2745 c = edit_buffer_delete (&edit->buffer);
2746 if (c == '\n')
2747 {
2748 edit->buffer.curs_line++;
2749 edit->force |= REDRAW_LINE_ABOVE;
2750 }
2751 }
2752 }
2753 }
2754
2755 /* --------------------------------------------------------------------------------------------- */
2756 /* If cols is zero this returns the count of columns from current to upto. */
2757 /* If upto is zero returns index of cols across from current. */
2758
2759 off_t
edit_move_forward3(const WEdit * edit,off_t current,long cols,off_t upto)2760 edit_move_forward3 (const WEdit * edit, off_t current, long cols, off_t upto)
2761 {
2762 off_t p, q;
2763 long col;
2764
2765 if (upto != 0)
2766 {
2767 q = upto;
2768 cols = -10;
2769 }
2770 else
2771 q = edit->buffer.size + 2;
2772
2773 for (col = 0, p = current; p < q; p++)
2774 {
2775 int c, orig_c;
2776
2777 if (cols != -10)
2778 {
2779 if (col == cols)
2780 return p;
2781 if (col > cols)
2782 return p - 1;
2783 }
2784
2785 orig_c = c = edit_buffer_get_byte (&edit->buffer, p);
2786
2787 #ifdef HAVE_CHARSET
2788 if (edit->utf8)
2789 {
2790 int utf_ch;
2791 int char_length = 1;
2792
2793 utf_ch = edit_buffer_get_utf (&edit->buffer, p, &char_length);
2794 if (mc_global.utf8_display)
2795 {
2796 if (char_length > 1)
2797 col -= char_length - 1;
2798 if (g_unichar_iswide (utf_ch))
2799 col++;
2800 }
2801 else if (char_length > 1 && g_unichar_isprint (utf_ch))
2802 col -= char_length - 1;
2803 }
2804
2805 c = convert_to_display_c (c);
2806 #endif
2807
2808 if (c == '\n')
2809 return (upto != 0 ? (off_t) col : p);
2810 if (c == '\t')
2811 col += TAB_SIZE - col % TAB_SIZE;
2812 else if ((c < 32 || c == 127) && (orig_c == c
2813 #ifdef HAVE_CHARSET
2814 || (!mc_global.utf8_display && !edit->utf8)
2815 #endif
2816 ))
2817 /* '\r' is shown as ^M, so we must advance 2 characters */
2818 /* Caret notation for control characters */
2819 col += 2;
2820 else
2821 col++;
2822 }
2823 return (off_t) col;
2824 }
2825
2826 /* --------------------------------------------------------------------------------------------- */
2827 /** returns the current offset of the cursor from the beginning of a file */
2828
2829 off_t
edit_get_cursor_offset(const WEdit * edit)2830 edit_get_cursor_offset (const WEdit * edit)
2831 {
2832 return edit->buffer.curs1;
2833 }
2834
2835 /* --------------------------------------------------------------------------------------------- */
2836 /** returns the current column position of the cursor */
2837
2838 long
edit_get_col(const WEdit * edit)2839 edit_get_col (const WEdit * edit)
2840 {
2841 return (long) edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), 0,
2842 edit->buffer.curs1);
2843 }
2844
2845 /* --------------------------------------------------------------------------------------------- */
2846 /* Scrolling functions */
2847 /* --------------------------------------------------------------------------------------------- */
2848
2849 void
edit_update_curs_row(WEdit * edit)2850 edit_update_curs_row (WEdit * edit)
2851 {
2852 edit->curs_row = edit->buffer.curs_line - edit->start_line;
2853 }
2854
2855 /* --------------------------------------------------------------------------------------------- */
2856
2857 void
edit_update_curs_col(WEdit * edit)2858 edit_update_curs_col (WEdit * edit)
2859 {
2860 edit->curs_col = (long) edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer),
2861 0, edit->buffer.curs1);
2862 }
2863
2864 /* --------------------------------------------------------------------------------------------- */
2865
2866 long
edit_get_curs_col(const WEdit * edit)2867 edit_get_curs_col (const WEdit * edit)
2868 {
2869 return edit->curs_col;
2870 }
2871
2872 /* --------------------------------------------------------------------------------------------- */
2873 /** moves the display start position up by i lines */
2874
2875 void
edit_scroll_upward(WEdit * edit,long i)2876 edit_scroll_upward (WEdit * edit, long i)
2877 {
2878 long lines_above = edit->start_line;
2879
2880 if (i > lines_above)
2881 i = lines_above;
2882 if (i != 0)
2883 {
2884 edit->start_line -= i;
2885 edit->start_display =
2886 edit_buffer_get_backward_offset (&edit->buffer, edit->start_display, i);
2887 edit->force |= REDRAW_PAGE;
2888 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2889 }
2890 edit_update_curs_row (edit);
2891 }
2892
2893
2894 /* --------------------------------------------------------------------------------------------- */
2895
2896 void
edit_scroll_downward(WEdit * edit,long i)2897 edit_scroll_downward (WEdit * edit, long i)
2898 {
2899 long lines_below;
2900
2901 lines_below = edit->buffer.lines - edit->start_line - (WIDGET (edit)->lines - 1);
2902 if (lines_below > 0)
2903 {
2904 if (i > lines_below)
2905 i = lines_below;
2906 edit->start_line += i;
2907 edit->start_display =
2908 edit_buffer_get_forward_offset (&edit->buffer, edit->start_display, i, 0);
2909 edit->force |= REDRAW_PAGE;
2910 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2911 }
2912 edit_update_curs_row (edit);
2913 }
2914
2915 /* --------------------------------------------------------------------------------------------- */
2916
2917 void
edit_scroll_right(WEdit * edit,long i)2918 edit_scroll_right (WEdit * edit, long i)
2919 {
2920 edit->force |= REDRAW_PAGE;
2921 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2922 edit->start_col -= i;
2923 }
2924
2925 /* --------------------------------------------------------------------------------------------- */
2926
2927 void
edit_scroll_left(WEdit * edit,long i)2928 edit_scroll_left (WEdit * edit, long i)
2929 {
2930 if (edit->start_col)
2931 {
2932 edit->start_col += i;
2933 if (edit->start_col > 0)
2934 edit->start_col = 0;
2935 edit->force |= REDRAW_PAGE;
2936 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2937 }
2938 }
2939
2940 /* --------------------------------------------------------------------------------------------- */
2941 /* high level cursor movement commands */
2942 /* --------------------------------------------------------------------------------------------- */
2943
2944 void
edit_move_to_prev_col(WEdit * edit,off_t p)2945 edit_move_to_prev_col (WEdit * edit, off_t p)
2946 {
2947 long prev = edit->prev_col;
2948 long over = edit->over_col;
2949
2950 edit_cursor_move (edit,
2951 edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->buffer.curs1);
2952
2953 if (option_cursor_beyond_eol)
2954 {
2955 long line_len;
2956
2957 line_len = (long) edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), 0,
2958 edit_buffer_get_current_eol (&edit->buffer));
2959 if (line_len < prev + edit->over_col)
2960 {
2961 edit->over_col = prev + over - line_len;
2962 edit->prev_col = line_len;
2963 edit->curs_col = line_len;
2964 }
2965 else
2966 {
2967 edit->curs_col = prev + over;
2968 edit->prev_col = edit->curs_col;
2969 edit->over_col = 0;
2970 }
2971 }
2972 else
2973 {
2974 edit->over_col = 0;
2975 if (option_fake_half_tabs && is_in_indent (&edit->buffer))
2976 {
2977 long fake_half_tabs;
2978
2979 edit_update_curs_col (edit);
2980
2981 fake_half_tabs = HALF_TAB_SIZE * space_width;
2982 if (fake_half_tabs != 0 && edit->curs_col % fake_half_tabs != 0)
2983 {
2984 long q;
2985
2986 q = edit->curs_col;
2987 edit->curs_col -= (edit->curs_col % fake_half_tabs);
2988 p = edit_buffer_get_current_bol (&edit->buffer);
2989 edit_cursor_move (edit,
2990 edit_move_forward3 (edit, p, edit->curs_col,
2991 0) - edit->buffer.curs1);
2992 if (!left_of_four_spaces (edit))
2993 edit_cursor_move (edit,
2994 edit_move_forward3 (edit, p, q, 0) - edit->buffer.curs1);
2995 }
2996 }
2997 }
2998 }
2999
3000 /* --------------------------------------------------------------------------------------------- */
3001 /** check whether line in editor is blank or not
3002 *
3003 * @param edit editor object
3004 * @param line number of line
3005 *
3006 * @return TRUE if line in blank, FALSE otherwise
3007 */
3008
3009 gboolean
edit_line_is_blank(WEdit * edit,long line)3010 edit_line_is_blank (WEdit * edit, long line)
3011 {
3012 return is_blank (&edit->buffer, edit_find_line (edit, line));
3013 }
3014
3015 /* --------------------------------------------------------------------------------------------- */
3016 /** move cursor to line 'line' */
3017
3018 void
edit_move_to_line(WEdit * e,long line)3019 edit_move_to_line (WEdit * e, long line)
3020 {
3021 if (line < e->buffer.curs_line)
3022 edit_move_up (e, e->buffer.curs_line - line, FALSE);
3023 else
3024 edit_move_down (e, line - e->buffer.curs_line, FALSE);
3025 edit_scroll_screen_over_cursor (e);
3026 }
3027
3028 /* --------------------------------------------------------------------------------------------- */
3029 /** scroll window so that first visible line is 'line' */
3030
3031 void
edit_move_display(WEdit * e,long line)3032 edit_move_display (WEdit * e, long line)
3033 {
3034 if (line < e->start_line)
3035 edit_scroll_upward (e, e->start_line - line);
3036 else
3037 edit_scroll_downward (e, line - e->start_line);
3038 }
3039
3040 /* --------------------------------------------------------------------------------------------- */
3041 /** save markers onto undo stack */
3042
3043 void
edit_push_markers(WEdit * edit)3044 edit_push_markers (WEdit * edit)
3045 {
3046 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3047 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3048 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3049 }
3050
3051 /* --------------------------------------------------------------------------------------------- */
3052
3053 void
edit_set_markers(WEdit * edit,off_t m1,off_t m2,long c1,long c2)3054 edit_set_markers (WEdit * edit, off_t m1, off_t m2, long c1, long c2)
3055 {
3056 edit->mark1 = m1;
3057 edit->mark2 = m2;
3058 edit->column1 = c1;
3059 edit->column2 = c2;
3060 }
3061
3062
3063 /* --------------------------------------------------------------------------------------------- */
3064 /** highlight marker toggle */
3065
3066 void
edit_mark_cmd(WEdit * edit,gboolean unmark)3067 edit_mark_cmd (WEdit * edit, gboolean unmark)
3068 {
3069 edit_push_markers (edit);
3070 if (unmark)
3071 {
3072 edit_set_markers (edit, 0, 0, 0, 0);
3073 edit->force |= REDRAW_PAGE;
3074 }
3075 else if (edit->mark2 >= 0)
3076 {
3077 edit->end_mark_curs = -1;
3078 edit_set_markers (edit, edit->buffer.curs1, -1, edit->curs_col + edit->over_col,
3079 edit->curs_col + edit->over_col);
3080 edit->force |= REDRAW_PAGE;
3081 }
3082 else
3083 {
3084 edit->end_mark_curs = edit->buffer.curs1;
3085 edit_set_markers (edit, edit->mark1, edit->buffer.curs1, edit->column1,
3086 edit->curs_col + edit->over_col);
3087 }
3088 }
3089
3090 /* --------------------------------------------------------------------------------------------- */
3091 /** highlight the word under cursor */
3092
3093 void
edit_mark_current_word_cmd(WEdit * edit)3094 edit_mark_current_word_cmd (WEdit * edit)
3095 {
3096 long pos;
3097
3098 for (pos = edit->buffer.curs1; pos != 0; pos--)
3099 {
3100 int c1, c2;
3101
3102 c1 = edit_buffer_get_byte (&edit->buffer, pos);
3103 c2 = edit_buffer_get_byte (&edit->buffer, pos - 1);
3104 if (!isspace (c1) && isspace (c2))
3105 break;
3106 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3107 break;
3108 }
3109 edit->mark1 = pos;
3110
3111 for (; pos < edit->buffer.size; pos++)
3112 {
3113 int c1, c2;
3114
3115 c1 = edit_buffer_get_byte (&edit->buffer, pos);
3116 c2 = edit_buffer_get_byte (&edit->buffer, pos + 1);
3117 if (!isspace (c1) && isspace (c2))
3118 break;
3119 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3120 break;
3121 }
3122 edit->mark2 = MIN (pos + 1, edit->buffer.size);
3123
3124 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3125 }
3126
3127 /* --------------------------------------------------------------------------------------------- */
3128
3129 void
edit_mark_current_line_cmd(WEdit * edit)3130 edit_mark_current_line_cmd (WEdit * edit)
3131 {
3132 edit->mark1 = edit_buffer_get_current_bol (&edit->buffer);
3133 edit->mark2 = edit_buffer_get_current_eol (&edit->buffer);
3134
3135 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3136 }
3137
3138 /* --------------------------------------------------------------------------------------------- */
3139
3140 void
edit_delete_line(WEdit * edit)3141 edit_delete_line (WEdit * edit)
3142 {
3143 /*
3144 * Delete right part of the line.
3145 * Note that edit_buffer_get_byte() returns '\n' when byte position is
3146 * beyond EOF.
3147 */
3148 while (edit_buffer_get_current_byte (&edit->buffer) != '\n')
3149 (void) edit_delete (edit, TRUE);
3150
3151 /*
3152 * Delete '\n' char.
3153 * Note that edit_delete() will not corrupt anything if called while
3154 * cursor position is EOF.
3155 */
3156 (void) edit_delete (edit, TRUE);
3157
3158 /*
3159 * Delete left part of the line.
3160 * Note, that edit_buffer_get_byte() returns '\n' when byte position is < 0.
3161 */
3162 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n')
3163 (void) edit_backspace (edit, TRUE);
3164 }
3165
3166 /* --------------------------------------------------------------------------------------------- */
3167
3168 void
edit_push_key_press(WEdit * edit)3169 edit_push_key_press (WEdit * edit)
3170 {
3171 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3172 if (edit->mark2 == -1)
3173 {
3174 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3175 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3176 }
3177 }
3178
3179 /* --------------------------------------------------------------------------------------------- */
3180
3181 void
edit_find_bracket(WEdit * edit)3182 edit_find_bracket (WEdit * edit)
3183 {
3184 edit->bracket = edit_get_bracket (edit, 1, 10000);
3185 if (edit->last_bracket != edit->bracket)
3186 edit->force |= REDRAW_PAGE;
3187 edit->last_bracket = edit->bracket;
3188 }
3189
3190 /* --------------------------------------------------------------------------------------------- */
3191 /**
3192 * This executes a command as though the user initiated it through a key
3193 * press. Callback with MSG_KEY as a message calls this after
3194 * translating the key press. This function can be used to pass any
3195 * command to the editor. Note that the screen wouldn't update
3196 * automatically. Either of command or char_for_insertion must be
3197 * passed as -1. Commands are executed, and char_for_insertion is
3198 * inserted at the cursor.
3199 */
3200
3201 void
edit_execute_key_command(WEdit * edit,long command,int char_for_insertion)3202 edit_execute_key_command (WEdit * edit, long command, int char_for_insertion)
3203 {
3204 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3205 || (macro_index < 0
3206 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3207 {
3208 macro_index = 0;
3209 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3210 return;
3211 }
3212 if (macro_index != -1)
3213 {
3214 edit->force |= REDRAW_COMPLETELY;
3215 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3216 {
3217 edit_store_macro_cmd (edit);
3218 macro_index = -1;
3219 return;
3220 }
3221 if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3222 {
3223 edit_repeat_macro_cmd (edit);
3224 macro_index = -1;
3225 return;
3226 }
3227 }
3228
3229 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3230 {
3231 record_macro_buf[macro_index].action = command;
3232 record_macro_buf[macro_index++].ch = char_for_insertion;
3233 }
3234 /* record the beginning of a set of editing actions initiated by a key press */
3235 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3236 edit_push_key_press (edit);
3237
3238 edit_execute_cmd (edit, command, char_for_insertion);
3239 if (edit->column_highlight)
3240 edit->force |= REDRAW_PAGE;
3241 }
3242
3243 /* --------------------------------------------------------------------------------------------- */
3244 /**
3245 This executes a command at a lower level than macro recording.
3246 It also does not push a key_press onto the undo stack. This means
3247 that if it is called many times, a single undo command will undo
3248 all of them. It also does not check for the Undo command.
3249 */
3250 void
edit_execute_cmd(WEdit * edit,long command,int char_for_insertion)3251 edit_execute_cmd (WEdit * edit, long command, int char_for_insertion)
3252 {
3253 Widget *w = WIDGET (edit);
3254
3255 if (command == CK_WindowFullscreen)
3256 {
3257 edit_toggle_fullscreen (edit);
3258 return;
3259 }
3260
3261 /* handle window state */
3262 if (edit_handle_move_resize (edit, command))
3263 return;
3264
3265 edit->force |= REDRAW_LINE;
3266
3267 /* The next key press will unhighlight the found string, so update
3268 * the whole page */
3269 if (edit->found_len || edit->column_highlight)
3270 edit->force |= REDRAW_PAGE;
3271
3272 switch (command)
3273 {
3274 /* a mark command with shift-arrow */
3275 case CK_MarkLeft:
3276 case CK_MarkRight:
3277 case CK_MarkToWordBegin:
3278 case CK_MarkToWordEnd:
3279 case CK_MarkToHome:
3280 case CK_MarkToEnd:
3281 case CK_MarkUp:
3282 case CK_MarkDown:
3283 case CK_MarkPageUp:
3284 case CK_MarkPageDown:
3285 case CK_MarkToFileBegin:
3286 case CK_MarkToFileEnd:
3287 case CK_MarkToPageBegin:
3288 case CK_MarkToPageEnd:
3289 case CK_MarkScrollUp:
3290 case CK_MarkScrollDown:
3291 case CK_MarkParagraphUp:
3292 case CK_MarkParagraphDown:
3293 /* a mark command with alt-arrow */
3294 case CK_MarkColumnPageUp:
3295 case CK_MarkColumnPageDown:
3296 case CK_MarkColumnLeft:
3297 case CK_MarkColumnRight:
3298 case CK_MarkColumnUp:
3299 case CK_MarkColumnDown:
3300 case CK_MarkColumnScrollUp:
3301 case CK_MarkColumnScrollDown:
3302 case CK_MarkColumnParagraphUp:
3303 case CK_MarkColumnParagraphDown:
3304 edit->column_highlight = 0;
3305 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3306 {
3307 edit_mark_cmd (edit, TRUE); /* clear */
3308 edit_mark_cmd (edit, FALSE); /* marking on */
3309 }
3310 edit->highlight = 1;
3311 break;
3312
3313 /* any other command */
3314 default:
3315 if (edit->highlight)
3316 edit_mark_cmd (edit, FALSE); /* clear */
3317 edit->highlight = 0;
3318 }
3319
3320 /* first check for undo */
3321 if (command == CK_Undo)
3322 {
3323 edit->redo_stack_reset = 0;
3324 edit_group_undo (edit);
3325 edit->found_len = 0;
3326 edit->prev_col = edit_get_col (edit);
3327 edit->search_start = edit->buffer.curs1;
3328 return;
3329 }
3330 /* check for redo */
3331 if (command == CK_Redo)
3332 {
3333 edit->redo_stack_reset = 0;
3334 edit_do_redo (edit);
3335 edit->found_len = 0;
3336 edit->prev_col = edit_get_col (edit);
3337 edit->search_start = edit->buffer.curs1;
3338 return;
3339 }
3340
3341 edit->redo_stack_reset = 1;
3342
3343 /* An ordinary key press */
3344 if (char_for_insertion >= 0)
3345 {
3346 /* if non persistent selection and text selected */
3347 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3348 edit_block_delete_cmd (edit);
3349
3350 if (edit->overwrite)
3351 {
3352 /* remove char only one time, after input first byte, multibyte chars */
3353 #ifdef HAVE_CHARSET
3354 if (!mc_global.utf8_display || edit->charpoint == 0)
3355 #endif
3356 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
3357
3358 edit_delete (edit, FALSE);
3359 }
3360 if (option_cursor_beyond_eol && edit->over_col > 0)
3361 edit_insert_over (edit);
3362 #ifdef HAVE_CHARSET
3363 /**
3364 Encode 8-bit input as UTF-8, if display (locale) is *not* UTF-8,
3365 *but* source encoding *is* set to UTF-8; see ticket #3843 for the details.
3366 */
3367 if (char_for_insertion > 127 && str_isutf8 (get_codepage_id (mc_global.source_codepage))
3368 && !mc_global.utf8_display)
3369 {
3370 unsigned char str[UTF8_CHAR_LEN + 1];
3371 size_t i = 0;
3372 int res;
3373
3374 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3375 if (res == 0)
3376 {
3377 str[0] = '.';
3378 str[1] = '\0';
3379 }
3380 else
3381 {
3382 str[res] = '\0';
3383 }
3384 while (i <= UTF8_CHAR_LEN && str[i] != '\0')
3385 {
3386 char_for_insertion = str[i];
3387 edit_insert (edit, char_for_insertion);
3388 i++;
3389 }
3390 }
3391 else
3392 #endif
3393 edit_insert (edit, char_for_insertion);
3394
3395 if (option_auto_para_formatting)
3396 {
3397 format_paragraph (edit, FALSE);
3398 edit->force |= REDRAW_PAGE;
3399 }
3400 else
3401 check_and_wrap_line (edit);
3402 edit->found_len = 0;
3403 edit->prev_col = edit_get_col (edit);
3404 edit->search_start = edit->buffer.curs1;
3405 edit_find_bracket (edit);
3406 return;
3407 }
3408
3409 switch (command)
3410 {
3411 case CK_TopOnScreen:
3412 case CK_BottomOnScreen:
3413 case CK_Top:
3414 case CK_Bottom:
3415 case CK_PageUp:
3416 case CK_PageDown:
3417 case CK_Home:
3418 case CK_End:
3419 case CK_Up:
3420 case CK_Down:
3421 case CK_Left:
3422 case CK_Right:
3423 case CK_WordLeft:
3424 case CK_WordRight:
3425 if (!option_persistent_selections && edit->mark2 >= 0)
3426 {
3427 if (edit->column_highlight)
3428 edit_push_undo_action (edit, COLUMN_ON);
3429 edit->column_highlight = 0;
3430 edit_mark_cmd (edit, TRUE);
3431 }
3432 break;
3433 default:
3434 break;
3435 }
3436
3437 switch (command)
3438 {
3439 case CK_TopOnScreen:
3440 case CK_BottomOnScreen:
3441 case CK_MarkToPageBegin:
3442 case CK_MarkToPageEnd:
3443 case CK_Up:
3444 case CK_Down:
3445 case CK_WordLeft:
3446 case CK_WordRight:
3447 case CK_MarkToWordBegin:
3448 case CK_MarkToWordEnd:
3449 case CK_MarkUp:
3450 case CK_MarkDown:
3451 case CK_MarkColumnUp:
3452 case CK_MarkColumnDown:
3453 if (edit->mark2 == -1)
3454 break; /*marking is following the cursor: may need to highlight a whole line */
3455 MC_FALLTHROUGH;
3456 case CK_Left:
3457 case CK_Right:
3458 case CK_MarkLeft:
3459 case CK_MarkRight:
3460 edit->force |= REDRAW_CHAR_ONLY;
3461 break;
3462 default:
3463 break;
3464 }
3465
3466 /* basic cursor key commands */
3467 switch (command)
3468 {
3469 case CK_BackSpace:
3470 /* if non persistent selection and text selected */
3471 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3472 edit_block_delete_cmd (edit);
3473 else if (option_cursor_beyond_eol && edit->over_col > 0)
3474 edit->over_col--;
3475 else if (option_backspace_through_tabs && is_in_indent (&edit->buffer))
3476 {
3477 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n' && edit->buffer.curs1 > 0)
3478 edit_backspace (edit, TRUE);
3479 }
3480 else if (option_fake_half_tabs && is_in_indent (&edit->buffer)
3481 && right_of_four_spaces (edit))
3482 {
3483 int i;
3484
3485 for (i = 0; i < HALF_TAB_SIZE; i++)
3486 edit_backspace (edit, TRUE);
3487 }
3488 else
3489 edit_backspace (edit, FALSE);
3490 break;
3491 case CK_Delete:
3492 /* if non persistent selection and text selected */
3493 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3494 edit_block_delete_cmd (edit);
3495 else
3496 {
3497 if (option_cursor_beyond_eol && edit->over_col > 0)
3498 edit_insert_over (edit);
3499
3500 if (option_fake_half_tabs && is_in_indent (&edit->buffer) && left_of_four_spaces (edit))
3501 {
3502 int i;
3503
3504 for (i = 1; i <= HALF_TAB_SIZE; i++)
3505 edit_delete (edit, TRUE);
3506 }
3507 else
3508 edit_delete (edit, FALSE);
3509 }
3510 break;
3511 case CK_DeleteToWordBegin:
3512 edit->over_col = 0;
3513 edit_left_delete_word (edit);
3514 break;
3515 case CK_DeleteToWordEnd:
3516 if (option_cursor_beyond_eol && edit->over_col > 0)
3517 edit_insert_over (edit);
3518
3519 edit_right_delete_word (edit);
3520 break;
3521 case CK_DeleteLine:
3522 edit_delete_line (edit);
3523 break;
3524 case CK_DeleteToHome:
3525 edit_delete_to_line_begin (edit);
3526 break;
3527 case CK_DeleteToEnd:
3528 edit_delete_to_line_end (edit);
3529 break;
3530 case CK_Enter:
3531 edit->over_col = 0;
3532 if (option_auto_para_formatting)
3533 {
3534 edit_double_newline (edit);
3535 if (option_return_does_auto_indent && !bracketed_pasting_in_progress)
3536 edit_auto_indent (edit);
3537 format_paragraph (edit, FALSE);
3538 }
3539 else
3540 {
3541 edit_insert (edit, '\n');
3542 if (option_return_does_auto_indent && !bracketed_pasting_in_progress)
3543 edit_auto_indent (edit);
3544 }
3545 break;
3546 case CK_Return:
3547 edit_insert (edit, '\n');
3548 break;
3549
3550 case CK_MarkColumnPageUp:
3551 edit->column_highlight = 1;
3552 MC_FALLTHROUGH;
3553 case CK_PageUp:
3554 case CK_MarkPageUp:
3555 edit_move_up (edit, w->lines - 1, TRUE);
3556 break;
3557 case CK_MarkColumnPageDown:
3558 edit->column_highlight = 1;
3559 MC_FALLTHROUGH;
3560 case CK_PageDown:
3561 case CK_MarkPageDown:
3562 edit_move_down (edit, w->lines - 1, TRUE);
3563 break;
3564 case CK_MarkColumnLeft:
3565 edit->column_highlight = 1;
3566 MC_FALLTHROUGH;
3567 case CK_Left:
3568 case CK_MarkLeft:
3569 if (option_fake_half_tabs && is_in_indent (&edit->buffer) && right_of_four_spaces (edit))
3570 {
3571 if (option_cursor_beyond_eol && edit->over_col > 0)
3572 edit->over_col--;
3573 else
3574 edit_cursor_move (edit, -HALF_TAB_SIZE);
3575 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3576 }
3577 else
3578 edit_left_char_move_cmd (edit);
3579 break;
3580 case CK_MarkColumnRight:
3581 edit->column_highlight = 1;
3582 MC_FALLTHROUGH;
3583 case CK_Right:
3584 case CK_MarkRight:
3585 if (option_fake_half_tabs && is_in_indent (&edit->buffer) && left_of_four_spaces (edit))
3586 {
3587 edit_cursor_move (edit, HALF_TAB_SIZE);
3588 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3589 }
3590 else
3591 edit_right_char_move_cmd (edit);
3592 break;
3593 case CK_TopOnScreen:
3594 case CK_MarkToPageBegin:
3595 edit_begin_page (edit);
3596 break;
3597 case CK_BottomOnScreen:
3598 case CK_MarkToPageEnd:
3599 edit_end_page (edit);
3600 break;
3601 case CK_WordLeft:
3602 case CK_MarkToWordBegin:
3603 edit->over_col = 0;
3604 edit_left_word_move_cmd (edit);
3605 break;
3606 case CK_WordRight:
3607 case CK_MarkToWordEnd:
3608 edit->over_col = 0;
3609 edit_right_word_move_cmd (edit);
3610 break;
3611 case CK_MarkColumnUp:
3612 edit->column_highlight = 1;
3613 MC_FALLTHROUGH;
3614 case CK_Up:
3615 case CK_MarkUp:
3616 edit_move_up (edit, 1, FALSE);
3617 break;
3618 case CK_MarkColumnDown:
3619 edit->column_highlight = 1;
3620 MC_FALLTHROUGH;
3621 case CK_Down:
3622 case CK_MarkDown:
3623 edit_move_down (edit, 1, FALSE);
3624 break;
3625 case CK_MarkColumnParagraphUp:
3626 edit->column_highlight = 1;
3627 MC_FALLTHROUGH;
3628 case CK_ParagraphUp:
3629 case CK_MarkParagraphUp:
3630 edit_move_up_paragraph (edit, FALSE);
3631 break;
3632 case CK_MarkColumnParagraphDown:
3633 edit->column_highlight = 1;
3634 MC_FALLTHROUGH;
3635 case CK_ParagraphDown:
3636 case CK_MarkParagraphDown:
3637 edit_move_down_paragraph (edit, FALSE);
3638 break;
3639 case CK_MarkColumnScrollUp:
3640 edit->column_highlight = 1;
3641 MC_FALLTHROUGH;
3642 case CK_ScrollUp:
3643 case CK_MarkScrollUp:
3644 edit_move_up (edit, 1, TRUE);
3645 break;
3646 case CK_MarkColumnScrollDown:
3647 edit->column_highlight = 1;
3648 MC_FALLTHROUGH;
3649 case CK_ScrollDown:
3650 case CK_MarkScrollDown:
3651 edit_move_down (edit, 1, TRUE);
3652 break;
3653 case CK_Home:
3654 case CK_MarkToHome:
3655 edit_cursor_to_bol (edit);
3656 break;
3657 case CK_End:
3658 case CK_MarkToEnd:
3659 edit_cursor_to_eol (edit);
3660 break;
3661 case CK_Tab:
3662 /* if text marked shift block */
3663 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3664 {
3665 if (edit->mark2 < 0)
3666 edit_mark_cmd (edit, FALSE);
3667 edit_move_block_to_right (edit);
3668 }
3669 else
3670 {
3671 if (option_cursor_beyond_eol)
3672 edit_insert_over (edit);
3673 edit_tab_cmd (edit);
3674 if (option_auto_para_formatting)
3675 {
3676 format_paragraph (edit, FALSE);
3677 edit->force |= REDRAW_PAGE;
3678 }
3679 else
3680 check_and_wrap_line (edit);
3681 }
3682 break;
3683
3684 case CK_InsertOverwrite:
3685 edit->overwrite = !edit->overwrite;
3686 break;
3687
3688 case CK_Mark:
3689 if (edit->mark2 >= 0)
3690 {
3691 if (edit->column_highlight)
3692 edit_push_undo_action (edit, COLUMN_ON);
3693 edit->column_highlight = 0;
3694 }
3695 edit_mark_cmd (edit, FALSE);
3696 break;
3697 case CK_MarkColumn:
3698 if (!edit->column_highlight)
3699 edit_push_undo_action (edit, COLUMN_OFF);
3700 edit->column_highlight = 1;
3701 edit_mark_cmd (edit, FALSE);
3702 break;
3703 case CK_MarkAll:
3704 edit_set_markers (edit, 0, edit->buffer.size, 0, 0);
3705 edit->force |= REDRAW_PAGE;
3706 break;
3707 case CK_Unmark:
3708 if (edit->column_highlight)
3709 edit_push_undo_action (edit, COLUMN_ON);
3710 edit->column_highlight = 0;
3711 edit_mark_cmd (edit, TRUE);
3712 break;
3713 case CK_MarkWord:
3714 if (edit->column_highlight)
3715 edit_push_undo_action (edit, COLUMN_ON);
3716 edit->column_highlight = 0;
3717 edit_mark_current_word_cmd (edit);
3718 break;
3719 case CK_MarkLine:
3720 if (edit->column_highlight)
3721 edit_push_undo_action (edit, COLUMN_ON);
3722 edit->column_highlight = 0;
3723 edit_mark_current_line_cmd (edit);
3724 break;
3725
3726 case CK_Bookmark:
3727 book_mark_clear (edit, edit->buffer.curs_line, BOOK_MARK_FOUND_COLOR);
3728 if (book_mark_query_color (edit, edit->buffer.curs_line, BOOK_MARK_COLOR))
3729 book_mark_clear (edit, edit->buffer.curs_line, BOOK_MARK_COLOR);
3730 else
3731 book_mark_insert (edit, edit->buffer.curs_line, BOOK_MARK_COLOR);
3732 break;
3733 case CK_BookmarkFlush:
3734 book_mark_flush (edit, BOOK_MARK_COLOR);
3735 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3736 edit->force |= REDRAW_PAGE;
3737 break;
3738 case CK_BookmarkNext:
3739 if (edit->book_mark != NULL)
3740 {
3741 edit_book_mark_t *p;
3742
3743 p = book_mark_find (edit, edit->buffer.curs_line);
3744 if (p->next != NULL)
3745 {
3746 p = p->next;
3747 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
3748 edit_move_display (edit, p->line - w->lines / 2);
3749 edit_move_to_line (edit, p->line);
3750 }
3751 }
3752 break;
3753 case CK_BookmarkPrev:
3754 if (edit->book_mark != NULL)
3755 {
3756 edit_book_mark_t *p;
3757
3758 p = book_mark_find (edit, edit->buffer.curs_line);
3759 while (p->line == edit->buffer.curs_line)
3760 if (p->prev != NULL)
3761 p = p->prev;
3762 if (p->line >= 0)
3763 {
3764 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
3765 edit_move_display (edit, p->line - w->lines / 2);
3766 edit_move_to_line (edit, p->line);
3767 }
3768 }
3769 break;
3770
3771 case CK_Top:
3772 case CK_MarkToFileBegin:
3773 edit_move_to_top (edit);
3774 break;
3775 case CK_Bottom:
3776 case CK_MarkToFileEnd:
3777 edit_move_to_bottom (edit);
3778 break;
3779
3780 case CK_Copy:
3781 if (option_cursor_beyond_eol && edit->over_col > 0)
3782 edit_insert_over (edit);
3783 edit_block_copy_cmd (edit);
3784 break;
3785 case CK_Remove:
3786 edit_block_delete_cmd (edit);
3787 break;
3788 case CK_Move:
3789 edit_block_move_cmd (edit);
3790 break;
3791
3792 case CK_BlockShiftLeft:
3793 if (edit->mark1 != edit->mark2)
3794 edit_move_block_to_left (edit);
3795 break;
3796 case CK_BlockShiftRight:
3797 if (edit->mark1 != edit->mark2)
3798 edit_move_block_to_right (edit);
3799 break;
3800 case CK_Store:
3801 edit_copy_to_X_buf_cmd (edit);
3802 break;
3803 case CK_Cut:
3804 edit_cut_to_X_buf_cmd (edit);
3805 break;
3806 case CK_Paste:
3807 /* if non persistent selection and text selected */
3808 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3809 edit_block_delete_cmd (edit);
3810 if (option_cursor_beyond_eol && edit->over_col > 0)
3811 edit_insert_over (edit);
3812 edit_paste_from_X_buf_cmd (edit);
3813 if (!option_persistent_selections && edit->mark2 >= 0)
3814 {
3815 if (edit->column_highlight)
3816 edit_push_undo_action (edit, COLUMN_ON);
3817 edit->column_highlight = 0;
3818 edit_mark_cmd (edit, TRUE);
3819 }
3820 break;
3821 case CK_History:
3822 edit_paste_from_history (edit);
3823 break;
3824
3825 case CK_SaveAs:
3826 edit_save_as_cmd (edit);
3827 break;
3828 case CK_Save:
3829 edit_save_confirm_cmd (edit);
3830 break;
3831 case CK_BlockSave:
3832 edit_save_block_cmd (edit);
3833 break;
3834 case CK_InsertFile:
3835 edit_insert_file_cmd (edit);
3836 break;
3837
3838 case CK_FilePrev:
3839 edit_load_back_cmd (edit);
3840 break;
3841 case CK_FileNext:
3842 edit_load_forward_cmd (edit);
3843 break;
3844
3845 case CK_SyntaxChoose:
3846 edit_syntax_dialog (edit);
3847 break;
3848
3849 case CK_Search:
3850 edit_search_cmd (edit, FALSE);
3851 break;
3852 case CK_SearchContinue:
3853 edit_search_cmd (edit, TRUE);
3854 break;
3855 case CK_Replace:
3856 edit_replace_cmd (edit, FALSE);
3857 break;
3858 case CK_ReplaceContinue:
3859 edit_replace_cmd (edit, TRUE);
3860 break;
3861 case CK_Complete:
3862 /* if text marked shift block */
3863 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3864 edit_move_block_to_left (edit);
3865 else
3866 edit_complete_word_cmd (edit);
3867 break;
3868 case CK_Find:
3869 edit_get_match_keyword_cmd (edit);
3870 break;
3871
3872 #ifdef HAVE_ASPELL
3873 case CK_SpellCheckCurrentWord:
3874 edit_suggest_current_word (edit);
3875 break;
3876 case CK_SpellCheck:
3877 edit_spellcheck_file (edit);
3878 break;
3879 case CK_SpellCheckSelectLang:
3880 edit_set_spell_lang ();
3881 break;
3882 #endif
3883
3884 case CK_Date:
3885 {
3886 char s[BUF_MEDIUM];
3887 /* fool gcc to prevent a Y2K warning */
3888 char time_format[] = "_c";
3889 time_format[0] = '%';
3890
3891 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
3892 edit_print_string (edit, s);
3893 edit->force |= REDRAW_PAGE;
3894 }
3895 break;
3896 case CK_Goto:
3897 edit_goto_cmd (edit);
3898 break;
3899 case CK_ParagraphFormat:
3900 format_paragraph (edit, TRUE);
3901 edit->force |= REDRAW_PAGE;
3902 break;
3903 case CK_MacroDelete:
3904 edit_delete_macro_cmd (edit);
3905 break;
3906 case CK_MatchBracket:
3907 edit_goto_matching_bracket (edit);
3908 break;
3909 case CK_UserMenu:
3910 user_menu (edit, NULL, -1);
3911 break;
3912 case CK_Sort:
3913 edit_sort_cmd (edit);
3914 break;
3915 case CK_ExternalCommand:
3916 edit_ext_cmd (edit);
3917 break;
3918 case CK_EditMail:
3919 edit_mail_dialog (edit);
3920 break;
3921 #ifdef HAVE_CHARSET
3922 case CK_SelectCodepage:
3923 edit_select_codepage_cmd (edit);
3924 break;
3925 #endif
3926 case CK_InsertLiteral:
3927 edit_insert_literal_cmd (edit);
3928 break;
3929 case CK_MacroStartStopRecord:
3930 edit_begin_end_macro_cmd (edit);
3931 break;
3932 case CK_RepeatStartStopRecord:
3933 edit_begin_end_repeat_cmd (edit);
3934 break;
3935 case CK_ExtendedKeyMap:
3936 w->ext_mode = TRUE;
3937 break;
3938 default:
3939 break;
3940 }
3941
3942 /* CK_PipeBlock */
3943 if ((command / CK_PipeBlock (0)) == 1)
3944 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
3945
3946 /* keys which must set the col position, and the search vars */
3947 switch (command)
3948 {
3949 case CK_Search:
3950 case CK_SearchContinue:
3951 case CK_Replace:
3952 case CK_ReplaceContinue:
3953 case CK_Complete:
3954 edit->prev_col = edit_get_col (edit);
3955 break;
3956 case CK_Up:
3957 case CK_MarkUp:
3958 case CK_MarkColumnUp:
3959 case CK_Down:
3960 case CK_MarkDown:
3961 case CK_MarkColumnDown:
3962 case CK_PageUp:
3963 case CK_MarkPageUp:
3964 case CK_MarkColumnPageUp:
3965 case CK_PageDown:
3966 case CK_MarkPageDown:
3967 case CK_MarkColumnPageDown:
3968 case CK_Top:
3969 case CK_MarkToFileBegin:
3970 case CK_Bottom:
3971 case CK_MarkToFileEnd:
3972 case CK_ParagraphUp:
3973 case CK_MarkParagraphUp:
3974 case CK_MarkColumnParagraphUp:
3975 case CK_ParagraphDown:
3976 case CK_MarkParagraphDown:
3977 case CK_MarkColumnParagraphDown:
3978 case CK_ScrollUp:
3979 case CK_MarkScrollUp:
3980 case CK_MarkColumnScrollUp:
3981 case CK_ScrollDown:
3982 case CK_MarkScrollDown:
3983 case CK_MarkColumnScrollDown:
3984 edit->search_start = edit->buffer.curs1;
3985 edit->found_len = 0;
3986 break;
3987 default:
3988 edit->found_len = 0;
3989 edit->prev_col = edit_get_col (edit);
3990 edit->search_start = edit->buffer.curs1;
3991 }
3992 edit_find_bracket (edit);
3993
3994 if (option_auto_para_formatting)
3995 {
3996 switch (command)
3997 {
3998 case CK_BackSpace:
3999 case CK_Delete:
4000 case CK_DeleteToWordBegin:
4001 case CK_DeleteToWordEnd:
4002 case CK_DeleteToHome:
4003 case CK_DeleteToEnd:
4004 format_paragraph (edit, FALSE);
4005 edit->force |= REDRAW_PAGE;
4006 break;
4007 default:
4008 break;
4009 }
4010 }
4011 }
4012
4013 /* --------------------------------------------------------------------------------------------- */
4014
4015 void
edit_stack_init(void)4016 edit_stack_init (void)
4017 {
4018 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4019 {
4020 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4021 edit_history_moveto[edit_stack_iterator].line = -1;
4022 }
4023
4024 edit_stack_iterator = 0;
4025 }
4026
4027 /* --------------------------------------------------------------------------------------------- */
4028
4029 void
edit_stack_free(void)4030 edit_stack_free (void)
4031 {
4032 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4033 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath, TRUE);
4034 }
4035
4036 /* --------------------------------------------------------------------------------------------- */
4037 /** move i lines */
4038
4039 void
edit_move_up(WEdit * edit,long i,gboolean do_scroll)4040 edit_move_up (WEdit * edit, long i, gboolean do_scroll)
4041 {
4042 edit_move_updown (edit, i, do_scroll, TRUE);
4043 }
4044
4045 /* --------------------------------------------------------------------------------------------- */
4046 /** move i lines */
4047
4048 void
edit_move_down(WEdit * edit,long i,gboolean do_scroll)4049 edit_move_down (WEdit * edit, long i, gboolean do_scroll)
4050 {
4051 edit_move_updown (edit, i, do_scroll, FALSE);
4052 }
4053
4054 /* --------------------------------------------------------------------------------------------- */
4055