2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996-2023
5 Free Software Foundation, Inc.
9 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
10 Andrew Borodin <aborodin@vmail.ru> 2012-2022
12 This file is part of the Midnight Commander.
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.
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.
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/>.
29 * \brief Source: editor low level data handling and cursor fundamentals
37 #include <sys/types.h>
42 #include <stdint.h> /* UINTMAX_MAX */
45 #include "lib/global.h"
47 #include "lib/tty/color.h"
48 #include "lib/tty/tty.h" /* attrset() */
49 #include "lib/tty/key.h" /* is_idle() */
50 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
51 #include "lib/fileloc.h" /* EDIT_HOME_BLOCK_FILE */
52 #include "lib/vfs/vfs.h"
53 #include "lib/strutil.h" /* utf string functions */
54 #include "lib/util.h" /* load_file_position(), save_file_position() */
55 #include "lib/timefmt.h" /* time formatting */
57 #include "lib/widget.h"
60 #include "lib/charsets.h" /* get_codepage_id */
63 #include "src/usermenu.h" /* user_menu_cmd() */
65 #include "src/keymap.h"
67 #include "edit-impl.h"
68 #include "editwidget.h"
69 #include "editsearch.h"
70 #include "editcomplete.h" /* edit_complete_word_cmd() */
71 #include "editmacros.h"
72 #include "etags.h" /* edit_get_match_keyword_cmd() */
77 /*** global variables ****************************************************************************/
79 edit_options_t edit_options
= {
80 .word_wrap_line_length
= DEFAULT_WRAP_LINE_LENGTH
,
81 .typewriter_wrap
= FALSE
,
82 .auto_para_formatting
= FALSE
,
83 .fill_tabs_with_spaces
= FALSE
,
84 .return_does_auto_indent
= TRUE
,
85 .backspace_through_tabs
= FALSE
,
86 .fake_half_tabs
= TRUE
,
87 .persistent_selections
= TRUE
,
88 .drop_selection_on_copy
= TRUE
,
89 .cursor_beyond_eol
= FALSE
,
90 .cursor_after_inserted_block
= FALSE
,
91 .state_full_filename
= FALSE
,
93 .line_state_width
= 0,
94 .save_mode
= EDIT_QUICK_SAVE
,
96 .save_position
= TRUE
,
97 .syntax_highlighting
= TRUE
,
100 .filesize_threshold
= NULL
,
101 .stop_format_chars
= NULL
,
102 .visible_tabs
= TRUE
,
104 .show_right_margin
= FALSE
,
105 .simple_statusbar
= FALSE
,
106 .check_nl_at_eof
= FALSE
109 int max_undo
= 32768;
111 gboolean enable_show_tabs_tws
= TRUE
;
113 unsigned int edit_stack_iterator
= 0;
114 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
115 /* magic sequence for say than block is vertical */
116 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
118 /*** file scope macro definitions ****************************************************************/
120 #define TEMP_BUF_LEN 1024
122 #define space_width 1
124 /*** file scope type declarations ****************************************************************/
126 /*** forward declarations (file scope functions) *************************************************/
128 /*** file scope variables ************************************************************************/
130 /* detecting an error on save is easy: just check if every byte has been written. */
131 /* detecting an error on read, is not so easy 'cos there is not way to tell
132 whether you read everything or not. */
133 /* FIXME: add proper 'triple_pipe_open' to read, write and check errors. */
134 static const struct edit_filters
136 const char *read
, *write
, *extension
;
140 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
141 { "zstd -cd %s 2>&1", "zstd > %s", ".zst"},
142 { "lz4 -cd %s 2>&1", "lz4 > %s", ".lz4" },
143 { "lzip -cd %s 2>&1", "lzip > %s", ".lz"},
144 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
145 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
146 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
147 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
151 static const off_t filesize_default_threshold
= 64 * 1024 * 1024; /* 64 MB */
153 /* --------------------------------------------------------------------------------------------- */
154 /*** file scope functions ************************************************************************/
155 /* --------------------------------------------------------------------------------------------- */
158 edit_load_status_update_cb (status_msg_t
* sm
)
160 simple_status_msg_t
*ssm
= SIMPLE_STATUS_MSG (sm
);
161 edit_buffer_read_file_status_msg_t
*rsm
= (edit_buffer_read_file_status_msg_t
*) sm
;
162 Widget
*wd
= WIDGET (sm
->dlg
);
165 label_set_textv (ssm
->label
, _("Loading: %3d%%"),
166 edit_buffer_calc_percent (rsm
->buf
, rsm
->loaded
));
168 label_set_text (ssm
->label
, _("Loading..."));
172 Widget
*lw
= WIDGET (ssm
->label
);
176 r
.cols
= MAX (r
.cols
, lw
->rect
.cols
+ 6);
177 widget_set_size_rect (wd
, &r
);
179 r
.x
= wd
->rect
.x
+ (wd
->rect
.cols
- r
.cols
) / 2;
180 widget_set_size_rect (lw
, &r
);
184 return status_msg_common_update (sm
);
187 /* --------------------------------------------------------------------------------------------- */
189 * Load file OR text into buffers. Set cursor to the beginning of file.
191 * @return FALSE on error.
195 edit_load_file_fast (edit_buffer_t
* buf
, const vfs_path_t
* filename_vpath
)
199 edit_buffer_read_file_status_msg_t rsm
;
202 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
208 g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath
));
209 edit_error_dialog (_("Error"), errmsg
);
218 status_msg_init (STATUS_MSG (&rsm
), _("Load file"), 1.0, simple_status_msg_init_cb
,
219 edit_load_status_update_cb
, NULL
);
221 ret
= (edit_buffer_read_file (buf
, file
, buf
->size
, &rsm
, &aborted
) == buf
->size
);
223 status_msg_deinit (STATUS_MSG (&rsm
));
225 if (!ret
&& !aborted
)
229 errmsg
= g_strdup_printf (_("Error reading %s"), vfs_path_as_str (filename_vpath
));
230 edit_error_dialog (_("Error"), errmsg
);
238 /* --------------------------------------------------------------------------------------------- */
239 /** Return index of the filter or -1 is there is no appropriate filter */
242 edit_find_filter (const vfs_path_t
* filename_vpath
)
244 if (filename_vpath
!= NULL
)
249 s
= vfs_path_as_str (filename_vpath
);
251 for (i
= 0; i
< G_N_ELEMENTS (all_filters
); i
++)
252 if (g_str_has_suffix (s
, all_filters
[i
].extension
))
259 /* --------------------------------------------------------------------------------------------- */
262 edit_get_filter (const vfs_path_t
* filename_vpath
)
268 i
= edit_find_filter (filename_vpath
);
272 quoted_name
= name_quote (vfs_path_as_str (filename_vpath
), FALSE
);
273 if (quoted_name
!= NULL
)
275 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
276 g_free (quoted_name
);
282 /* --------------------------------------------------------------------------------------------- */
285 edit_insert_stream (WEdit
* edit
, FILE * f
)
290 for (i
= 0; (c
= fgetc (f
)) >= 0; i
++)
291 edit_insert (edit
, c
);
296 /* --------------------------------------------------------------------------------------------- */
298 * Open file and create it if necessary.
300 * @param edit editor object
301 * @param filename_vpath file name
302 * @param st buffer for store stat info
303 * @return TRUE for success, FALSE for error.
307 check_file_access (WEdit
* edit
, const vfs_path_t
* filename_vpath
, struct stat
*st
)
309 static uintmax_t threshold
= UINTMAX_MAX
;
311 gchar
*errmsg
= NULL
;
314 /* Try opening an existing file */
315 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
319 * Try creating the file. O_EXCL prevents following broken links
320 * and opening existing files.
322 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
326 g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath
));
330 /* New file, delete it if it's not modified or saved */
331 edit
->delete_file
= 1;
334 /* Check what we have opened */
335 if (mc_fstat (file
, st
) < 0)
338 g_strdup_printf (_("Cannot get size/permissions for %s"),
339 vfs_path_as_str (filename_vpath
));
343 /* We want to open regular files only */
344 if (!S_ISREG (st
->st_mode
))
347 g_strdup_printf (_("\"%s\" is not a regular file"), vfs_path_as_str (filename_vpath
));
351 /* get file size threshold for alarm */
352 if (threshold
== UINTMAX_MAX
)
354 gboolean err
= FALSE
;
356 threshold
= parse_integer (edit_options
.filesize_threshold
, &err
);
358 threshold
= filesize_default_threshold
;
362 * Don't delete non-empty files.
363 * O_EXCL should prevent it, but let's be on the safe side.
366 edit
->delete_file
= 0;
368 if ((uintmax_t) st
->st_size
> threshold
)
372 errmsg
= g_strdup_printf (_("File \"%s\" is too large.\nOpen it anyway?"),
373 vfs_path_as_str (filename_vpath
));
374 act
= edit_query_dialog2 (_("Warning"), errmsg
, _("&Yes"), _("&No"));
375 MC_PTR_FREE (errmsg
);
382 (void) mc_close (file
);
386 edit_error_dialog (_("Error"), errmsg
);
394 /* --------------------------------------------------------------------------------------------- */
397 * Open the file and load it into the buffers, either directly or using
398 * a filter. Return TRUE on success, FALSE on error.
400 * Fast loading (edit_load_file_fast) is used when the file size is
401 * known. In this case the data is read into the buffers by blocks.
402 * If the file size is not known, the data is loaded byte by byte in
405 * @param edit editor object
406 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
409 edit_load_file (WEdit
* edit
)
411 gboolean fast_load
= TRUE
;
413 /* Cannot do fast load if a filter is used */
414 if (edit_find_filter (edit
->filename_vpath
) >= 0)
418 * FIXME: line end translation should disable fast loading as well
419 * Consider doing fseek() to the end and ftell() for the real size.
421 if (edit
->filename_vpath
!= NULL
)
424 * VFS may report file size incorrectly, and slow load is not a big
425 * deal considering overhead in VFS.
427 if (!vfs_file_is_local (edit
->filename_vpath
))
430 /* If we are dealing with a real file, check that it exists */
431 if (!check_file_access (edit
, edit
->filename_vpath
, &edit
->stat1
))
439 /* nothing to load */
445 edit_buffer_init (&edit
->buffer
, edit
->stat1
.st_size
);
447 if (!edit_load_file_fast (&edit
->buffer
, edit
->filename_vpath
))
455 edit_buffer_init (&edit
->buffer
, 0);
457 if (edit
->filename_vpath
!= NULL
458 && *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) != '\0')
460 edit
->undo_stack_disable
= 1;
461 if (edit_insert_file (edit
, edit
->filename_vpath
) < 0)
466 edit
->undo_stack_disable
= 0;
473 /* --------------------------------------------------------------------------------------------- */
475 * Restore saved cursor position and/or bookmarks in the file
477 * @param edit editor object
478 * @param load_position If TRUE, load bookmarks and cursor position and apply them.
479 * If FALSE, load bookmarks only.
483 edit_load_position (WEdit
* edit
, gboolean load_position
)
488 if (edit
->filename_vpath
== NULL
489 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
492 load_file_position (edit
->filename_vpath
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
493 /* apply bookmarks in any case */
494 book_mark_restore (edit
, BOOK_MARK_COLOR
);
501 edit_move_to_line (edit
, line
- 1);
502 edit
->prev_col
= column
;
506 edit_cursor_move (edit
, offset
);
507 line
= edit
->buffer
.curs_line
;
508 edit
->search_start
= edit
->buffer
.curs1
;
511 edit_move_to_prev_col (edit
, edit_buffer_get_current_bol (&edit
->buffer
));
512 edit_move_display (edit
, line
- (WIDGET (edit
)->rect
.lines
/ 2));
515 /* --------------------------------------------------------------------------------------------- */
516 /** Save cursor position in the file */
519 edit_save_position (WEdit
* edit
)
521 if (edit
->filename_vpath
== NULL
522 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
525 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
526 save_file_position (edit
->filename_vpath
, edit
->buffer
.curs_line
+ 1, edit
->curs_col
,
527 edit
->buffer
.curs1
, edit
->serialized_bookmarks
);
528 edit
->serialized_bookmarks
= NULL
;
531 /* --------------------------------------------------------------------------------------------- */
532 /** Clean the WEdit stricture except the widget part */
535 edit_purge_widget (WEdit
* edit
)
537 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
538 char *start
= (char *) edit
+ sizeof (Widget
);
539 memset (start
, 0, len
);
542 /* --------------------------------------------------------------------------------------------- */
545 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
546 then the file should be as it was when he loaded up. Then set edit->modified to 0.
550 edit_pop_undo_action (WEdit
* edit
)
553 unsigned long sp
= edit
->undo_stack_pointer
;
555 if (sp
== edit
->undo_stack_bottom
)
558 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
559 c
= edit
->undo_stack
[sp
];
562 /* edit->undo_stack[sp] = '@'; */
563 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
567 if (sp
== edit
->undo_stack_bottom
)
570 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
571 if (edit
->undo_stack
[sp
] == -2)
573 /* edit->undo_stack[sp] = '@'; */
574 edit
->undo_stack_pointer
= sp
;
577 edit
->undo_stack
[sp
]++;
582 /* --------------------------------------------------------------------------------------------- */
585 edit_pop_redo_action (WEdit
* edit
)
588 unsigned long sp
= edit
->redo_stack_pointer
;
590 if (sp
== edit
->redo_stack_bottom
)
593 sp
= (sp
- 1) & edit
->redo_stack_size_mask
;
594 c
= edit
->redo_stack
[sp
];
597 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
601 if (sp
== edit
->redo_stack_bottom
)
604 c
= edit
->redo_stack
[(sp
- 1) & edit
->redo_stack_size_mask
];
605 if (edit
->redo_stack
[sp
] == -2)
606 edit
->redo_stack_pointer
= sp
;
608 edit
->redo_stack
[sp
]++;
613 /* --------------------------------------------------------------------------------------------- */
616 get_prev_undo_action (WEdit
* edit
)
619 unsigned long sp
= edit
->undo_stack_pointer
;
621 if (sp
== edit
->undo_stack_bottom
)
624 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
625 c
= edit
->undo_stack
[sp
];
629 if (sp
== edit
->undo_stack_bottom
)
632 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
636 /* --------------------------------------------------------------------------------------------- */
637 /** is called whenever a modification is made by one of the four routines below */
640 edit_modification (WEdit
* edit
)
642 edit
->caches_valid
= FALSE
;
644 /* raise lock when file modified */
645 if (!edit
->modified
&& !edit
->delete_file
)
646 edit
->locked
= lock_file (edit
->filename_vpath
);
650 /* --------------------------------------------------------------------------------------------- */
651 /* high level cursor movement commands */
652 /* --------------------------------------------------------------------------------------------- */
653 /** check whether cursor is in indent part of line
655 * @param edit editor object
657 * @return TRUE if cursor is in indent, FALSE otherwise
661 is_in_indent (const edit_buffer_t
* buf
)
665 for (p
= edit_buffer_get_current_bol (buf
); p
< buf
->curs1
; p
++)
666 if (strchr (" \t", edit_buffer_get_byte (buf
, p
)) == NULL
)
672 /* --------------------------------------------------------------------------------------------- */
673 /** check whether line in editor is blank or not
675 * @param edit editor object
676 * @param offset position in file
678 * @return TRUE if line in blank, FALSE otherwise
682 is_blank (const edit_buffer_t
* buf
, off_t offset
)
686 s
= edit_buffer_get_bol (buf
, offset
);
687 f
= edit_buffer_get_eol (buf
, offset
) - 1;
692 c
= edit_buffer_get_byte (buf
, s
++);
699 /* --------------------------------------------------------------------------------------------- */
700 /** returns the offset of line i */
703 edit_find_line (WEdit
* edit
, long line
)
706 long m
= 2000000000; /* what is the magic number? */
708 if (!edit
->caches_valid
)
710 memset (edit
->line_numbers
, 0, sizeof (edit
->line_numbers
));
711 memset (edit
->line_offsets
, 0, sizeof (edit
->line_offsets
));
712 /* three offsets that we *know* are line 0 at 0 and these two: */
713 edit
->line_numbers
[1] = edit
->buffer
.curs_line
;
714 edit
->line_offsets
[1] = edit_buffer_get_current_bol (&edit
->buffer
);
715 edit
->line_numbers
[2] = edit
->buffer
.lines
;
716 edit
->line_offsets
[2] = edit_buffer_get_bol (&edit
->buffer
, edit
->buffer
.size
);
717 edit
->caches_valid
= TRUE
;
719 if (line
>= edit
->buffer
.lines
)
720 return edit
->line_offsets
[2];
723 /* find the closest known point */
724 for (i
= 0; i
< N_LINE_CACHES
; i
++)
728 n
= labs (edit
->line_numbers
[i
] - line
);
736 return edit
->line_offsets
[j
]; /* know the offset exactly */
737 if (m
== 1 && j
>= 3)
738 i
= j
; /* one line different - caller might be looping, so stay in this cache */
740 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
741 if (line
> edit
->line_numbers
[j
])
742 edit
->line_offsets
[i
] =
743 edit_buffer_get_forward_offset (&edit
->buffer
, edit
->line_offsets
[j
],
744 line
- edit
->line_numbers
[j
], 0);
746 edit
->line_offsets
[i
] =
747 edit_buffer_get_backward_offset (&edit
->buffer
, edit
->line_offsets
[j
],
748 edit
->line_numbers
[j
] - line
);
749 edit
->line_numbers
[i
] = line
;
750 return edit
->line_offsets
[i
];
753 /* --------------------------------------------------------------------------------------------- */
754 /** moves up until a blank line is reached, or until just
755 before a non-blank line is reached */
758 edit_move_up_paragraph (WEdit
* edit
, gboolean do_scroll
)
762 if (edit
->buffer
.curs_line
> 1)
764 if (!edit_line_is_blank (edit
, edit
->buffer
.curs_line
))
766 for (i
= edit
->buffer
.curs_line
- 1; i
!= 0; i
--)
767 if (edit_line_is_blank (edit
, i
))
770 else if (edit_line_is_blank (edit
, edit
->buffer
.curs_line
- 1))
772 for (i
= edit
->buffer
.curs_line
- 1; i
!= 0; i
--)
773 if (!edit_line_is_blank (edit
, i
))
781 for (i
= edit
->buffer
.curs_line
- 1; i
!= 0; i
--)
782 if (edit_line_is_blank (edit
, i
))
787 edit_move_up (edit
, edit
->buffer
.curs_line
- i
, do_scroll
);
790 /* --------------------------------------------------------------------------------------------- */
791 /** moves down until a blank line is reached, or until just
792 before a non-blank line is reached */
795 edit_move_down_paragraph (WEdit
* edit
, gboolean do_scroll
)
799 if (edit
->buffer
.curs_line
>= edit
->buffer
.lines
- 1)
800 i
= edit
->buffer
.lines
;
801 else if (!edit_line_is_blank (edit
, edit
->buffer
.curs_line
))
803 for (i
= edit
->buffer
.curs_line
+ 1; i
!= 0; i
++)
804 if (edit_line_is_blank (edit
, i
) || i
>= edit
->buffer
.lines
)
807 else if (edit_line_is_blank (edit
, edit
->buffer
.curs_line
+ 1))
809 for (i
= edit
->buffer
.curs_line
+ 1; i
!= 0; i
++)
810 if (!edit_line_is_blank (edit
, i
) || i
> edit
->buffer
.lines
)
818 for (i
= edit
->buffer
.curs_line
+ 1; i
!= 0; i
++)
819 if (edit_line_is_blank (edit
, i
) || i
>= edit
->buffer
.lines
)
822 edit_move_down (edit
, i
- edit
->buffer
.curs_line
, do_scroll
);
825 /* --------------------------------------------------------------------------------------------- */
828 edit_begin_page (WEdit
* edit
)
830 edit_update_curs_row (edit
);
831 edit_move_up (edit
, edit
->curs_row
, FALSE
);
834 /* --------------------------------------------------------------------------------------------- */
837 edit_end_page (WEdit
* edit
)
839 edit_update_curs_row (edit
);
840 edit_move_down (edit
, WIDGET (edit
)->rect
.lines
- edit
->curs_row
- 1, FALSE
);
844 /* --------------------------------------------------------------------------------------------- */
845 /** goto beginning of text */
848 edit_move_to_top (WEdit
* edit
)
850 if (edit
->buffer
.curs_line
!= 0)
852 edit_cursor_move (edit
, -edit
->buffer
.curs1
);
853 edit_move_to_prev_col (edit
, 0);
854 edit
->force
|= REDRAW_PAGE
;
855 edit
->search_start
= 0;
856 edit_update_curs_row (edit
);
860 /* --------------------------------------------------------------------------------------------- */
861 /** goto end of text */
864 edit_move_to_bottom (WEdit
* edit
)
866 if (edit
->buffer
.curs_line
< edit
->buffer
.lines
)
868 edit_move_down (edit
, edit
->buffer
.lines
- edit
->curs_row
, FALSE
);
869 edit
->start_display
= edit
->buffer
.size
;
870 edit
->start_line
= edit
->buffer
.lines
;
871 edit_scroll_upward (edit
, WIDGET (edit
)->rect
.lines
- 1);
872 edit
->force
|= REDRAW_PAGE
;
876 /* --------------------------------------------------------------------------------------------- */
877 /** goto beginning of line */
880 edit_cursor_to_bol (WEdit
* edit
)
882 edit_cursor_move (edit
, edit_buffer_get_current_bol (&edit
->buffer
) - edit
->buffer
.curs1
);
883 edit
->search_start
= edit
->buffer
.curs1
;
884 edit
->prev_col
= edit_get_col (edit
);
888 /* --------------------------------------------------------------------------------------------- */
889 /** goto end of line */
892 edit_cursor_to_eol (WEdit
* edit
)
894 edit_cursor_move (edit
, edit_buffer_get_current_eol (&edit
->buffer
) - edit
->buffer
.curs1
);
895 edit
->search_start
= edit
->buffer
.curs1
;
896 edit
->prev_col
= edit_get_col (edit
);
900 /* --------------------------------------------------------------------------------------------- */
907 const char chars_move_whole_word
[] =
908 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
915 if (g_ascii_isupper ((gchar
) c
))
917 else if (g_ascii_islower ((gchar
) c
))
919 else if (g_ascii_isalpha (c
))
921 else if (isdigit (c
))
923 else if (isspace (c
))
925 q
= strchr (chars_move_whole_word
, c
);
933 for (x
= 1, p
= chars_move_whole_word
; p
< q
; p
++)
938 while ((q
= strchr (q
+ 1, c
)));
942 /* --------------------------------------------------------------------------------------------- */
945 edit_left_word_move (WEdit
* edit
, int s
)
951 if (edit
->column_highlight
952 && edit
->mark1
!= edit
->mark2
953 && edit
->over_col
== 0
954 && edit
->buffer
.curs1
== edit_buffer_get_current_bol (&edit
->buffer
))
956 edit_cursor_move (edit
, -1);
957 if (edit
->buffer
.curs1
== 0)
959 c1
= edit_buffer_get_previous_byte (&edit
->buffer
);
962 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
965 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
967 if (isspace (c1
) && !isspace (c2
))
969 if (s
!= 0 && !isspace (c1
) && isspace (c2
))
974 /* --------------------------------------------------------------------------------------------- */
977 edit_left_word_move_cmd (WEdit
* edit
)
979 edit_left_word_move (edit
, 0);
980 edit
->force
|= REDRAW_PAGE
;
983 /* --------------------------------------------------------------------------------------------- */
986 edit_right_word_move (WEdit
* edit
, int s
)
992 if (edit
->column_highlight
993 && edit
->mark1
!= edit
->mark2
994 && edit
->over_col
== 0
995 && edit
->buffer
.curs1
== edit_buffer_get_current_eol (&edit
->buffer
))
997 edit_cursor_move (edit
, 1);
998 if (edit
->buffer
.curs1
>= edit
->buffer
.size
)
1000 c1
= edit_buffer_get_previous_byte (&edit
->buffer
);
1003 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
1006 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1008 if (isspace (c1
) && !isspace (c2
))
1010 if (s
!= 0 && !isspace (c1
) && isspace (c2
))
1015 /* --------------------------------------------------------------------------------------------- */
1018 edit_right_word_move_cmd (WEdit
* edit
)
1020 edit_right_word_move (edit
, 0);
1021 edit
->force
|= REDRAW_PAGE
;
1024 /* --------------------------------------------------------------------------------------------- */
1027 edit_right_char_move_cmd (WEdit
* edit
)
1029 int char_length
= 1;
1035 c
= edit_buffer_get_utf (&edit
->buffer
, edit
->buffer
.curs1
, &char_length
);
1036 if (char_length
< 1)
1041 c
= edit_buffer_get_current_byte (&edit
->buffer
);
1043 if (edit_options
.cursor_beyond_eol
&& c
== '\n')
1046 edit_cursor_move (edit
, char_length
);
1049 /* --------------------------------------------------------------------------------------------- */
1052 edit_left_char_move_cmd (WEdit
* edit
)
1054 int char_length
= 1;
1056 if (edit
->column_highlight
1057 && edit_options
.cursor_beyond_eol
1058 && edit
->mark1
!= edit
->mark2
1059 && edit
->over_col
== 0 && edit
->buffer
.curs1
== edit_buffer_get_current_bol (&edit
->buffer
))
1064 edit_buffer_get_prev_utf (&edit
->buffer
, edit
->buffer
.curs1
, &char_length
);
1065 if (char_length
< 1)
1070 if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
1073 edit_cursor_move (edit
, -char_length
);
1076 /* --------------------------------------------------------------------------------------------- */
1077 /** Up or down cursor moving.
1078 direction = TRUE - move up
1083 edit_move_updown (WEdit
* edit
, long lines
, gboolean do_scroll
, gboolean direction
)
1086 long l
= direction
? edit
->buffer
.curs_line
: edit
->buffer
.lines
- edit
->buffer
.curs_line
;
1095 edit
->force
|= REDRAW_PAGE
;
1099 edit_scroll_upward (edit
, lines
);
1101 edit_scroll_downward (edit
, lines
);
1103 p
= edit_buffer_get_current_bol (&edit
->buffer
);
1104 p
= direction
? edit_buffer_get_backward_offset (&edit
->buffer
, p
, lines
) :
1105 edit_buffer_get_forward_offset (&edit
->buffer
, p
, lines
, 0);
1106 edit_cursor_move (edit
, p
- edit
->buffer
.curs1
);
1107 edit_move_to_prev_col (edit
, p
);
1110 /* search start of current multibyte char (like CJK) */
1111 if (edit
->buffer
.curs1
> 0 && edit
->buffer
.curs1
+ 1 < edit
->buffer
.size
1112 && edit_buffer_get_current_byte (&edit
->buffer
) >= 256)
1114 edit_right_char_move_cmd (edit
);
1115 edit_left_char_move_cmd (edit
);
1119 edit
->search_start
= edit
->buffer
.curs1
;
1120 edit
->found_len
= 0;
1123 /* --------------------------------------------------------------------------------------------- */
1126 edit_right_delete_word (WEdit
* edit
)
1128 while (edit
->buffer
.curs1
< edit
->buffer
.size
)
1132 c1
= edit_delete (edit
, TRUE
);
1135 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
1138 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1140 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1145 /* --------------------------------------------------------------------------------------------- */
1148 edit_left_delete_word (WEdit
* edit
)
1150 while (edit
->buffer
.curs1
> 0)
1154 c1
= edit_backspace (edit
, TRUE
);
1157 c2
= edit_buffer_get_previous_byte (&edit
->buffer
);
1160 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1162 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1167 /* --------------------------------------------------------------------------------------------- */
1169 the start column position is not recorded, and hence does not
1170 undo as it happed. But who would notice.
1174 edit_do_undo (WEdit
* edit
)
1179 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1181 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1188 edit_cursor_move (edit
, 1);
1191 edit_cursor_move (edit
, -1);
1195 edit_backspace (edit
, TRUE
);
1199 edit_delete (edit
, TRUE
);
1202 edit
->column_highlight
= 1;
1205 edit
->column_highlight
= 0;
1210 if (ac
>= 256 && ac
< 512)
1211 edit_insert_ahead (edit
, ac
- 256);
1212 if (ac
>= 0 && ac
< 256)
1213 edit_insert (edit
, ac
);
1215 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1217 edit
->mark1
= ac
- MARK_1
;
1219 (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, edit
->mark1
),
1222 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1224 edit
->mark2
= ac
- MARK_2
;
1226 (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, edit
->mark2
),
1229 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1231 edit
->end_mark_curs
= ac
- MARK_CURS
;
1234 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1237 if (edit
->start_display
> ac
- KEY_PRESS
)
1240 edit_buffer_count_lines (&edit
->buffer
, ac
- KEY_PRESS
, edit
->start_display
);
1241 edit
->force
|= REDRAW_PAGE
;
1243 else if (edit
->start_display
< ac
- KEY_PRESS
)
1246 edit_buffer_count_lines (&edit
->buffer
, edit
->start_display
, ac
- KEY_PRESS
);
1247 edit
->force
|= REDRAW_PAGE
;
1249 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1250 edit_update_curs_row (edit
);
1253 edit
->undo_stack_disable
= 0;
1256 /* --------------------------------------------------------------------------------------------- */
1259 edit_do_redo (WEdit
* edit
)
1264 if (edit
->redo_stack_reset
)
1268 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1275 edit_cursor_move (edit
, 1);
1278 edit_cursor_move (edit
, -1);
1281 edit_backspace (edit
, TRUE
);
1284 edit_delete (edit
, TRUE
);
1287 edit
->column_highlight
= 1;
1290 edit
->column_highlight
= 0;
1295 if (ac
>= 256 && ac
< 512)
1296 edit_insert_ahead (edit
, ac
- 256);
1297 if (ac
>= 0 && ac
< 256)
1298 edit_insert (edit
, ac
);
1300 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1302 edit
->mark1
= ac
- MARK_1
;
1304 (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, edit
->mark1
),
1307 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1309 edit
->mark2
= ac
- MARK_2
;
1311 (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, edit
->mark2
),
1314 /* more than one pop usually means something big */
1316 edit
->force
|= REDRAW_PAGE
;
1319 if (edit
->start_display
> ac
- KEY_PRESS
)
1322 edit_buffer_count_lines (&edit
->buffer
, ac
- KEY_PRESS
, edit
->start_display
);
1323 edit
->force
|= REDRAW_PAGE
;
1325 else if (edit
->start_display
< ac
- KEY_PRESS
)
1328 edit_buffer_count_lines (&edit
->buffer
, edit
->start_display
, ac
- KEY_PRESS
);
1329 edit
->force
|= REDRAW_PAGE
;
1331 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1332 edit_update_curs_row (edit
);
1338 /* --------------------------------------------------------------------------------------------- */
1341 edit_group_undo (WEdit
* edit
)
1343 long ac
= KEY_PRESS
;
1344 long cur_ac
= KEY_PRESS
;
1345 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1347 cur_ac
= get_prev_undo_action (edit
);
1348 edit_do_undo (edit
);
1349 ac
= get_prev_undo_action (edit
);
1350 /* exit from cycle if edit_options.group_undo is not set,
1351 * and make single UNDO operation
1353 if (!edit_options
.group_undo
)
1358 /* --------------------------------------------------------------------------------------------- */
1361 edit_delete_to_line_end (WEdit
* edit
)
1363 while (edit_buffer_get_current_byte (&edit
->buffer
) != '\n' && edit
->buffer
.curs2
!= 0)
1364 edit_delete (edit
, TRUE
);
1367 /* --------------------------------------------------------------------------------------------- */
1370 edit_delete_to_line_begin (WEdit
* edit
)
1372 while (edit_buffer_get_previous_byte (&edit
->buffer
) != '\n' && edit
->buffer
.curs1
!= 0)
1373 edit_backspace (edit
, TRUE
);
1376 /* --------------------------------------------------------------------------------------------- */
1379 is_aligned_on_a_tab (WEdit
* edit
)
1383 edit_update_curs_col (edit
);
1384 curs_col
= edit
->curs_col
% (TAB_SIZE
* space_width
);
1385 return (curs_col
== 0 || curs_col
== (HALF_TAB_SIZE
* space_width
));
1388 /* --------------------------------------------------------------------------------------------- */
1391 right_of_four_spaces (WEdit
* edit
)
1395 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1396 ch
|= edit_buffer_get_byte (&edit
->buffer
, edit
->buffer
.curs1
- i
);
1398 return (ch
== ' ' && is_aligned_on_a_tab (edit
));
1401 /* --------------------------------------------------------------------------------------------- */
1404 left_of_four_spaces (WEdit
* edit
)
1408 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1409 ch
|= edit_buffer_get_byte (&edit
->buffer
, edit
->buffer
.curs1
+ i
);
1411 return (ch
== ' ' && is_aligned_on_a_tab (edit
));
1414 /* --------------------------------------------------------------------------------------------- */
1417 edit_auto_indent (WEdit
* edit
)
1421 p
= edit
->buffer
.curs1
;
1422 /* use the previous line as a template */
1423 p
= edit_buffer_get_backward_offset (&edit
->buffer
, p
, 1);
1424 /* copy the leading whitespace of the line */
1426 { /* no range check - the line _is_ \n-terminated */
1429 c
= edit_buffer_get_byte (&edit
->buffer
, p
++);
1430 if (!whitespace (c
))
1432 edit_insert (edit
, c
);
1436 /* --------------------------------------------------------------------------------------------- */
1439 edit_double_newline (WEdit
* edit
)
1441 edit_insert (edit
, '\n');
1442 if (edit_buffer_get_current_byte (&edit
->buffer
) == '\n'
1443 || edit_buffer_get_byte (&edit
->buffer
, edit
->buffer
.curs1
- 2) == '\n')
1445 edit
->force
|= REDRAW_PAGE
;
1446 edit_insert (edit
, '\n');
1449 /* --------------------------------------------------------------------------------------------- */
1452 insert_spaces_tab (WEdit
* edit
, gboolean half
)
1456 edit_update_curs_col (edit
);
1457 i
= TAB_SIZE
* space_width
;
1461 for (i
= ((edit
->curs_col
/ i
) + 1) * i
- edit
->curs_col
; i
> 0; i
-= space_width
)
1462 edit_insert (edit
, ' ');
1465 /* --------------------------------------------------------------------------------------------- */
1468 edit_tab_cmd (WEdit
* edit
)
1470 if (edit_options
.fake_half_tabs
&& is_in_indent (&edit
->buffer
))
1472 /* insert a half tab (usually four spaces) unless there is a
1473 half tab already behind, then delete it and insert a
1475 if (edit_options
.fill_tabs_with_spaces
|| !right_of_four_spaces (edit
))
1476 insert_spaces_tab (edit
, TRUE
);
1481 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1482 edit_backspace (edit
, TRUE
);
1483 edit_insert (edit
, '\t');
1486 else if (edit_options
.fill_tabs_with_spaces
)
1487 insert_spaces_tab (edit
, FALSE
);
1489 edit_insert (edit
, '\t');
1492 /* --------------------------------------------------------------------------------------------- */
1495 check_and_wrap_line (WEdit
* edit
)
1499 if (!edit_options
.typewriter_wrap
)
1501 edit_update_curs_col (edit
);
1502 if (edit
->curs_col
< edit_options
.word_wrap_line_length
)
1504 curs
= edit
->buffer
.curs1
;
1510 c
= edit_buffer_get_byte (&edit
->buffer
, curs
);
1511 if (c
== '\n' || curs
<= 0)
1513 edit_insert (edit
, '\n');
1518 off_t current
= edit
->buffer
.curs1
;
1519 edit_cursor_move (edit
, curs
- edit
->buffer
.curs1
+ 1);
1520 edit_insert (edit
, '\n');
1521 edit_cursor_move (edit
, current
- edit
->buffer
.curs1
+ 1);
1527 /* --------------------------------------------------------------------------------------------- */
1528 /** this find the matching bracket in either direction, and sets edit->bracket
1530 * @param edit editor object
1531 * @param in_screen search only on the current screen
1532 * @param furthest_bracket_search count of the bytes for search
1534 * @return position of the found bracket (-1 if no match)
1538 edit_get_bracket (WEdit
* edit
, gboolean in_screen
, unsigned long furthest_bracket_search
)
1540 const char *const b
= "{}{[][()(", *p
;
1541 int i
= 1, inc
= -1, c
, d
, n
= 0;
1542 unsigned long j
= 0;
1545 edit_update_curs_row (edit
);
1546 c
= edit_buffer_get_current_byte (&edit
->buffer
);
1548 /* not on a bracket at all */
1549 if (p
== NULL
|| *p
== '\0')
1551 /* the matching bracket */
1553 /* going left or right? */
1554 if (strchr ("{[(", c
) != NULL
)
1557 if (furthest_bracket_search
== 0)
1558 furthest_bracket_search
--; /* ULONG_MAX */
1559 for (q
= edit
->buffer
.curs1
+ inc
;; q
+= inc
)
1563 /* out of buffer? */
1564 if (q
>= edit
->buffer
.size
|| q
< 0)
1566 a
= edit_buffer_get_byte (&edit
->buffer
, q
);
1567 /* don't want to eat CPU */
1568 if (j
++ > furthest_bracket_search
)
1570 /* out of screen? */
1573 if (q
< edit
->start_display
)
1575 /* count lines if searching downward */
1576 if (inc
> 0 && a
== '\n')
1577 if (n
++ >= WIDGET (edit
)->rect
.lines
- edit
->curs_row
) /* out of screen */
1580 /* count bracket depth */
1581 i
+= (a
== c
) - (a
== d
);
1582 /* return if bracket depth is zero */
1590 /* --------------------------------------------------------------------------------------------- */
1593 edit_goto_matching_bracket (WEdit
* edit
)
1597 q
= edit_get_bracket (edit
, 0, 0);
1600 edit
->bracket
= edit
->buffer
.curs1
;
1601 edit
->force
|= REDRAW_PAGE
;
1602 edit_cursor_move (edit
, q
- edit
->buffer
.curs1
);
1606 /* --------------------------------------------------------------------------------------------- */
1609 edit_move_block_to_right (WEdit
* edit
)
1611 off_t start_mark
, end_mark
;
1612 long cur_bol
, start_bol
;
1614 if (!eval_marks (edit
, &start_mark
, &end_mark
))
1617 start_bol
= edit_buffer_get_bol (&edit
->buffer
, start_mark
);
1618 cur_bol
= edit_buffer_get_bol (&edit
->buffer
, end_mark
- 1);
1622 edit_cursor_move (edit
, cur_bol
- edit
->buffer
.curs1
);
1623 if (!edit_line_is_blank (edit
, edit
->buffer
.curs_line
))
1625 if (edit_options
.fill_tabs_with_spaces
)
1626 insert_spaces_tab (edit
, edit_options
.fake_half_tabs
);
1628 edit_insert (edit
, '\t');
1629 edit_cursor_move (edit
,
1630 edit_buffer_get_bol (&edit
->buffer
, cur_bol
) - edit
->buffer
.curs1
);
1636 cur_bol
= edit_buffer_get_bol (&edit
->buffer
, cur_bol
- 1);
1638 while (cur_bol
>= start_bol
);
1640 edit
->force
|= REDRAW_PAGE
;
1643 /* --------------------------------------------------------------------------------------------- */
1646 edit_move_block_to_left (WEdit
* edit
)
1648 off_t start_mark
, end_mark
;
1649 off_t cur_bol
, start_bol
;
1651 if (!eval_marks (edit
, &start_mark
, &end_mark
))
1654 start_bol
= edit_buffer_get_bol (&edit
->buffer
, start_mark
);
1655 cur_bol
= edit_buffer_get_bol (&edit
->buffer
, end_mark
- 1);
1662 edit_cursor_move (edit
, cur_bol
- edit
->buffer
.curs1
);
1664 del_tab_width
= edit_options
.fake_half_tabs
? HALF_TAB_SIZE
: TAB_SIZE
;
1666 next_char
= edit_buffer_get_current_byte (&edit
->buffer
);
1667 if (next_char
== '\t')
1668 edit_delete (edit
, TRUE
);
1669 else if (next_char
== ' ')
1673 for (i
= 0; i
< del_tab_width
; i
++)
1675 if (next_char
== ' ')
1676 edit_delete (edit
, TRUE
);
1677 next_char
= edit_buffer_get_current_byte (&edit
->buffer
);
1684 cur_bol
= edit_buffer_get_bol (&edit
->buffer
, cur_bol
- 1);
1686 while (cur_bol
>= start_bol
);
1688 edit
->force
|= REDRAW_PAGE
;
1691 /* --------------------------------------------------------------------------------------------- */
1693 * prints at the cursor
1694 * @return number of chars printed
1698 edit_print_string (WEdit
* e
, const char *s
)
1702 for (i
= 0; s
[i
] != '\0'; i
++)
1703 edit_execute_cmd (e
, CK_InsertChar
, (unsigned char) s
[i
]);
1704 e
->force
|= REDRAW_COMPLETELY
;
1705 edit_update_screen (e
);
1709 /* --------------------------------------------------------------------------------------------- */
1712 edit_insert_column_from_file (WEdit
* edit
, int file
, off_t
* start_pos
, off_t
* end_pos
,
1713 long *col1
, long *col2
)
1717 off_t blocklen
= -1, width
= 0;
1718 unsigned char *data
;
1720 cursor
= edit
->buffer
.curs1
;
1721 col
= edit_get_col (edit
);
1722 data
= g_malloc0 (TEMP_BUF_LEN
);
1724 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0)
1729 pn
= strchr ((char *) data
, '\n');
1730 width
= pn
== NULL
? blocklen
: pn
- (char *) data
;
1732 for (i
= 0; i
< blocklen
; i
++)
1734 if (data
[i
] != '\n')
1735 edit_insert (edit
, data
[i
]);
1737 { /* fill in and move to next line */
1741 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
1742 for (l
= width
- (edit_get_col (edit
) - col
); l
> 0; l
-= space_width
)
1743 edit_insert (edit
, ' ');
1745 for (p
= edit
->buffer
.curs1
;; p
++)
1747 if (p
== edit
->buffer
.size
)
1749 edit_cursor_move (edit
, edit
->buffer
.size
- edit
->buffer
.curs1
);
1750 edit_insert_ahead (edit
, '\n');
1754 if (edit_buffer_get_byte (&edit
->buffer
, p
) == '\n')
1761 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->buffer
.curs1
);
1763 for (l
= col
- edit_get_col (edit
); l
>= space_width
; l
-= space_width
)
1764 edit_insert (edit
, ' ');
1769 *col2
= col
+ width
;
1770 *start_pos
= cursor
;
1771 *end_pos
= edit
->buffer
.curs1
;
1772 edit_cursor_move (edit
, cursor
- edit
->buffer
.curs1
);
1778 /* --------------------------------------------------------------------------------------------- */
1779 /*** public functions ****************************************************************************/
1780 /* --------------------------------------------------------------------------------------------- */
1782 /** User edit menu, like user menu (F2) but only in editor. */
1785 edit_user_menu (WEdit
* edit
, const char *menu_file
, int selected_entry
)
1790 off_t start_mark
, end_mark
;
1792 vfs_path_t
*block_file_vpath
;
1794 block_file
= mc_config_get_full_path (EDIT_HOME_BLOCK_FILE
);
1795 block_file_vpath
= vfs_path_from_str (block_file
);
1796 curs
= edit
->buffer
.curs1
;
1797 mark
= eval_marks (edit
, &start_mark
, &end_mark
);
1799 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1801 /* run shell scripts from menu */
1802 if (user_menu_cmd (CONST_WIDGET (edit
), menu_file
, selected_entry
)
1803 && (mc_stat (block_file_vpath
, &status
) == 0) && (status
.st_size
!= 0))
1808 /* i.e. we have marked block */
1810 rc
= edit_block_delete_cmd (edit
);
1816 ins_len
= edit_insert_file (edit
, block_file_vpath
);
1817 if (mark
&& ins_len
> 0)
1818 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1820 /* truncate block file */
1821 fd
= fopen (block_file
, "w");
1825 g_free (block_file
);
1826 vfs_path_free (block_file_vpath
, TRUE
);
1828 edit_cursor_move (edit
, curs
- edit
->buffer
.curs1
);
1829 edit
->force
|= REDRAW_PAGE
;
1830 widget_draw (WIDGET (edit
));
1833 /* --------------------------------------------------------------------------------------------- */
1836 edit_get_write_filter (const vfs_path_t
* write_name_vpath
, const vfs_path_t
* filename_vpath
)
1839 const char *write_name
;
1840 char *write_name_quoted
;
1843 i
= edit_find_filter (filename_vpath
);
1847 write_name
= vfs_path_get_last_path_str (write_name_vpath
);
1848 write_name_quoted
= name_quote (write_name
, FALSE
);
1849 if (write_name_quoted
!= NULL
)
1851 p
= g_strdup_printf (all_filters
[i
].write
, write_name_quoted
);
1852 g_free (write_name_quoted
);
1857 /* --------------------------------------------------------------------------------------------- */
1859 * @param edit editor object
1860 * @param f value of stream file
1861 * @return the length of the file
1865 edit_write_stream (WEdit
* edit
, FILE * f
)
1869 if (edit
->lb
== LB_ASIS
)
1871 for (i
= 0; i
< edit
->buffer
.size
; i
++)
1872 if (fputc (edit_buffer_get_byte (&edit
->buffer
, i
), f
) < 0)
1877 /* change line breaks */
1878 for (i
= 0; i
< edit
->buffer
.size
; i
++)
1882 c
= edit_buffer_get_byte (&edit
->buffer
, i
);
1883 if (!(c
== '\n' || c
== '\r'))
1885 /* not line break */
1886 if (fputc (c
, f
) < 0)
1890 { /* (c == '\n' || c == '\r') */
1893 c1
= edit_buffer_get_byte (&edit
->buffer
, i
+ 1); /* next char */
1897 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
1898 /* put one line break unconditionally */
1899 if (fputc ('\n', f
) < 0)
1902 i
++; /* 2 chars are processed */
1904 if (c
== '\r' && c1
== '\n')
1905 /* Windows line break; go to the next char */
1908 if (c
== '\r' && c1
== '\r')
1910 /* two Macintosh line breaks; put second line break */
1911 if (fputc ('\n', f
) < 0)
1916 if (fputc (c1
, f
) < 0)
1920 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
1921 /* put one line break unconditionally */
1922 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
1925 if (c
== '\r' && c1
== '\n')
1926 /* Windows line break; go to the next char */
1930 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
1931 /* put one line break unconditionally */
1932 if (fputc ('\r', f
) < 0)
1935 i
++; /* 2 chars are processed */
1937 if (c
== '\r' && c1
== '\n')
1938 /* Windows line break; go to the next char */
1941 if (c
== '\n' && c1
== '\n')
1943 /* two Windows line breaks; put second line break */
1944 if (fputc ('\r', f
) < 0)
1949 if (fputc (c1
, f
) < 0)
1952 case LB_ASIS
: /* default without changes */
1959 return edit
->buffer
.size
;
1962 /* --------------------------------------------------------------------------------------------- */
1965 is_break_char (char c
)
1967 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
1970 /* --------------------------------------------------------------------------------------------- */
1971 /** inserts a file at the cursor, returns count of inserted bytes on success */
1974 edit_insert_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
1980 p
= edit_get_filter (filename_vpath
);
1981 current
= edit
->buffer
.curs1
;
1987 f
= (FILE *) popen (p
, "r");
1990 edit_insert_stream (edit
, f
);
1992 /* Place cursor at the end of text selection */
1993 if (!edit_options
.cursor_after_inserted_block
)
1995 ins_len
= edit
->buffer
.curs1
- current
;
1996 edit_cursor_move (edit
, -ins_len
);
2002 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
2003 edit_error_dialog (_("Error"), errmsg
);
2012 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
2013 edit_error_dialog (_("Error"), errmsg
);
2023 gboolean vertical_insertion
= FALSE
;
2026 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
2030 buf
= g_malloc0 (TEMP_BUF_LEN
);
2031 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2034 /* if contain signature VERTICAL_MAGIC then it vertical block */
2035 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2036 vertical_insertion
= TRUE
;
2038 mc_lseek (file
, 0, SEEK_SET
);
2041 if (vertical_insertion
)
2046 blocklen
= edit_insert_column_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
2047 edit_set_markers (edit
, edit
->buffer
.curs1
, mark2
, c1
, c2
);
2049 /* highlight inserted text then not persistent blocks */
2050 if (!edit_options
.persistent_selections
&& edit
->modified
)
2052 if (!edit
->column_highlight
)
2053 edit_push_undo_action (edit
, COLUMN_OFF
);
2054 edit
->column_highlight
= 1;
2061 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2063 for (i
= 0; i
< blocklen
; i
++)
2064 edit_insert (edit
, buf
[i
]);
2066 /* highlight inserted text then not persistent blocks */
2067 if (!edit_options
.persistent_selections
&& edit
->modified
)
2069 edit_set_markers (edit
, edit
->buffer
.curs1
, current
, 0, 0);
2070 if (edit
->column_highlight
)
2071 edit_push_undo_action (edit
, COLUMN_ON
);
2072 edit
->column_highlight
= 0;
2075 /* Place cursor at the end of text selection */
2076 if (!edit_options
.cursor_after_inserted_block
)
2078 ins_len
= edit
->buffer
.curs1
- current
;
2079 edit_cursor_move (edit
, -ins_len
);
2083 edit
->force
|= REDRAW_PAGE
;
2093 /* --------------------------------------------------------------------------------------------- */
2095 * Fill in the edit structure. Return NULL on failure. Pass edit as
2096 * NULL to allocate a new structure.
2098 * If line is 0, try to restore saved position. Otherwise put the
2099 * cursor on that line and show it in the middle of the screen.
2103 edit_init (WEdit
* edit
, const WRect
* r
, const vfs_path_t
* filename_vpath
, long line
)
2105 gboolean to_free
= FALSE
;
2107 auto_syntax
= TRUE
; /* Resetting to auto on every invocation */
2108 edit_options
.line_state_width
= edit_options
.line_state
? LINE_STATE_WIDTH
: 0;
2112 gboolean fullscreen
;
2115 /* save some widget parameters */
2116 fullscreen
= edit
->fullscreen
;
2117 loc_prev
= edit
->loc_prev
;
2119 edit_purge_widget (edit
);
2121 /* restore saved parameters */
2122 edit
->fullscreen
= fullscreen
;
2123 edit
->loc_prev
= loc_prev
;
2129 edit
= g_malloc0 (sizeof (WEdit
));
2133 widget_init (w
, r
, NULL
, NULL
);
2134 w
->options
|= WOP_SELECTABLE
| WOP_TOP_SELECT
| WOP_WANT_CURSOR
;
2135 w
->keymap
= editor_map
;
2136 w
->ext_keymap
= editor_x_map
;
2137 edit
->fullscreen
= TRUE
;
2138 edit_save_size (edit
);
2141 edit
->drag_state
= MCEDIT_DRAG_NONE
;
2143 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2144 edit
->stat1
.st_uid
= getuid ();
2145 edit
->stat1
.st_gid
= getgid ();
2146 edit
->stat1
.st_mtime
= 0;
2150 edit
->last_bracket
= -1;
2151 edit
->force
|= REDRAW_PAGE
;
2153 /* set file name before load file */
2154 edit_set_filename (edit
, filename_vpath
);
2156 edit
->undo_stack_size
= START_STACK_SIZE
;
2157 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2158 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2160 edit
->redo_stack_size
= START_STACK_SIZE
;
2161 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2162 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2166 edit
->converter
= str_cnv_from_term
;
2167 edit_set_codeset (edit
);
2170 if (!edit_load_file (edit
))
2172 /* edit_load_file already gives an error message */
2178 edit
->loading_done
= 1;
2181 edit_load_syntax (edit
, NULL
, NULL
);
2182 edit_get_syntax_color (edit
, -1);
2184 /* load saved cursor position and/or boolmarks */
2185 if ((line
== 0) && edit_options
.save_position
)
2186 edit_load_position (edit
, TRUE
);
2189 edit_load_position (edit
, FALSE
);
2192 edit_move_display (edit
, line
- 1);
2193 edit_move_to_line (edit
, line
- 1);
2196 edit_load_macro_cmd (edit
);
2201 /* --------------------------------------------------------------------------------------------- */
2203 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2205 edit_clean (WEdit
* edit
)
2210 /* a stale lock, remove it */
2212 (void) unlock_file (edit
->filename_vpath
);
2214 /* save cursor position */
2215 if (edit_options
.save_position
)
2216 edit_save_position (edit
);
2217 else if (edit
->serialized_bookmarks
!= NULL
)
2218 g_array_free (edit
->serialized_bookmarks
, TRUE
);
2220 /* File specified on the mcedit command line and never saved */
2221 if (edit
->delete_file
)
2222 unlink (vfs_path_get_last_path_str (edit
->filename_vpath
));
2224 edit_free_syntax_rules (edit
);
2225 book_mark_flush (edit
, -1);
2227 edit_buffer_clean (&edit
->buffer
);
2229 g_free (edit
->undo_stack
);
2230 g_free (edit
->redo_stack
);
2231 vfs_path_free (edit
->filename_vpath
, TRUE
);
2232 vfs_path_free (edit
->dir_vpath
, TRUE
);
2233 edit_search_deinit (edit
);
2236 if (edit
->converter
!= str_cnv_from_term
)
2237 str_close_conv (edit
->converter
);
2240 edit_purge_widget (edit
);
2245 /* --------------------------------------------------------------------------------------------- */
2248 * Load a new file into the editor and set line. If it fails, preserve the old file.
2249 * To do it, allocate a new widget, initialize it and, if the new file
2250 * was loaded, copy the data to the old widget.
2252 * @return TRUE on success, FALSE on failure.
2255 edit_reload_line (WEdit
* edit
, const vfs_path_t
* filename_vpath
, long line
)
2257 Widget
*w
= WIDGET (edit
);
2260 e
= g_malloc0 (sizeof (WEdit
));
2262 /* save some widget parameters */
2263 e
->fullscreen
= edit
->fullscreen
;
2264 e
->loc_prev
= edit
->loc_prev
;
2266 if (edit_init (e
, &w
->rect
, filename_vpath
, line
) == NULL
)
2273 memcpy (edit
, e
, sizeof (*edit
));
2279 /* --------------------------------------------------------------------------------------------- */
2283 edit_set_codeset (WEdit
* edit
)
2288 get_codepage_id (mc_global
.source_codepage
>=
2289 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2294 conv
= str_crt_conv_from (cp_id
);
2295 if (conv
!= INVALID_CONV
)
2297 if (edit
->converter
!= str_cnv_from_term
)
2298 str_close_conv (edit
->converter
);
2299 edit
->converter
= conv
;
2304 edit
->utf8
= str_isutf8 (cp_id
);
2308 /* --------------------------------------------------------------------------------------------- */
2311 * Recording stack for undo:
2312 * The following is an implementation of a compressed stack. Identical
2313 * pushes are recorded by a negative prefix indicating the number of times the
2314 * same char was pushed. This saves space for repeated curs-left or curs-right
2331 * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2332 * 256-512 is an insert ahead (from a delete), If it is between 600 and 700 it is one
2333 * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2334 * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2337 * The only way the cursor moves or the buffer is changed is through the routines:
2338 * insert, backspace, insert_ahead, delete, and cursor_move.
2339 * These record the reverse undo movements onto the stack each time they are
2342 * Each key press results in a set of actions (insert; delete ...). So each time
2343 * a key is pressed the current position of start_display is pushed as
2344 * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2345 * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2346 * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2350 * @param edit editor object
2351 * @param c code of the action
2355 edit_push_undo_action (WEdit
* edit
, long c
)
2357 unsigned long sp
= edit
->undo_stack_pointer
;
2360 /* first enlarge the stack if necessary */
2361 if (sp
> edit
->undo_stack_size
- 10)
2365 if (edit
->undo_stack_size
< (unsigned long) max_undo
)
2369 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2372 edit
->undo_stack
= t
;
2373 edit
->undo_stack_size
<<= 1;
2374 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2378 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2379 if (edit
->undo_stack_disable
)
2381 edit_push_redo_action (edit
, KEY_PRESS
);
2382 edit_push_redo_action (edit
, c
);
2386 if (edit
->redo_stack_reset
)
2387 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2389 if (edit
->undo_stack_bottom
!= sp
2390 && spm1
!= edit
->undo_stack_bottom
2391 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2394 if (edit
->undo_stack
[spm1
] < 0)
2396 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2397 if (d
== c
&& edit
->undo_stack
[spm1
] > -1000000000)
2399 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2400 edit
->undo_stack
[spm1
]--;
2406 d
= edit
->undo_stack
[spm1
];
2410 return; /* --> no need to push multiple do-nothings */
2411 edit
->undo_stack
[sp
] = -2;
2416 edit
->undo_stack
[sp
] = c
;
2419 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2421 /* if the sp wraps round and catches the undo_stack_bottom then erase
2422 * the first set of actions on the stack to make space - by moving
2423 * undo_stack_bottom forward one "key press" */
2424 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2425 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2426 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2429 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2431 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2432 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2434 /*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: */
2435 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2436 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2438 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2442 /* --------------------------------------------------------------------------------------------- */
2445 edit_push_redo_action (WEdit
* edit
, long c
)
2447 unsigned long sp
= edit
->redo_stack_pointer
;
2449 /* first enlarge the stack if necessary */
2450 if (sp
> edit
->redo_stack_size
- 10)
2454 if (edit
->redo_stack_size
< (unsigned long) max_undo
)
2458 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2461 edit
->redo_stack
= t
;
2462 edit
->redo_stack_size
<<= 1;
2463 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2467 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2469 if (edit
->redo_stack_bottom
!= sp
2470 && spm1
!= edit
->redo_stack_bottom
2471 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2474 if (edit
->redo_stack
[spm1
] < 0)
2476 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2477 if (d
== c
&& edit
->redo_stack
[spm1
] > -1000000000)
2479 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2480 edit
->redo_stack
[spm1
]--;
2486 d
= edit
->redo_stack
[spm1
];
2490 return; /* --> no need to push multiple do-nothings */
2491 edit
->redo_stack
[sp
] = -2;
2492 goto redo_check_bottom
;
2496 edit
->redo_stack
[sp
] = c
;
2499 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2501 /* if the sp wraps round and catches the redo_stack_bottom then erase
2502 * the first set of actions on the stack to make space - by moving
2503 * redo_stack_bottom forward one "key press" */
2504 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2505 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2506 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2509 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2511 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2512 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2515 * If a single key produced enough pushes to wrap all the way round then
2516 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2517 * The stack is then initialised:
2520 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2521 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2522 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2525 /* --------------------------------------------------------------------------------------------- */
2527 Basic low level single character buffer alterations and movements at the cursor.
2531 edit_insert (WEdit
* edit
, int c
)
2533 /* first we must update the position of the display window */
2534 if (edit
->buffer
.curs1
< edit
->start_display
)
2536 edit
->start_display
++;
2541 /* Mark file as modified, unless the file hasn't been fully loaded */
2542 if (edit
->loading_done
)
2543 edit_modification (edit
);
2545 /* now we must update some info on the file and check if a redraw is required */
2548 book_mark_inc (edit
, edit
->buffer
.curs_line
);
2549 edit
->buffer
.curs_line
++;
2550 edit
->buffer
.lines
++;
2551 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2554 /* save the reverse command onto the undo stack */
2555 /* ordinary char and not space */
2557 edit_push_undo_action (edit
, BACKSPACE
);
2559 edit_push_undo_action (edit
, BACKSPACE_BR
);
2560 /* update markers */
2561 edit
->mark1
+= (edit
->mark1
> edit
->buffer
.curs1
) ? 1 : 0;
2562 edit
->mark2
+= (edit
->mark2
> edit
->buffer
.curs1
) ? 1 : 0;
2563 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->buffer
.curs1
) ? 1 : 0;
2565 edit_buffer_insert (&edit
->buffer
, c
);
2568 /* --------------------------------------------------------------------------------------------- */
2569 /** same as edit_insert and move left */
2572 edit_insert_ahead (WEdit
* edit
, int c
)
2574 if (edit
->buffer
.curs1
< edit
->start_display
)
2576 edit
->start_display
++;
2580 edit_modification (edit
);
2583 book_mark_inc (edit
, edit
->buffer
.curs_line
);
2584 edit
->buffer
.lines
++;
2585 edit
->force
|= REDRAW_AFTER_CURSOR
;
2587 /* ordinary char and not space */
2589 edit_push_undo_action (edit
, DELCHAR
);
2591 edit_push_undo_action (edit
, DELCHAR_BR
);
2593 edit
->mark1
+= (edit
->mark1
>= edit
->buffer
.curs1
) ? 1 : 0;
2594 edit
->mark2
+= (edit
->mark2
>= edit
->buffer
.curs1
) ? 1 : 0;
2595 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->buffer
.curs1
) ? 1 : 0;
2597 edit_buffer_insert_ahead (&edit
->buffer
, c
);
2600 /* --------------------------------------------------------------------------------------------- */
2603 edit_insert_over (WEdit
* edit
)
2607 for (i
= 0; i
< edit
->over_col
; i
++)
2608 edit_insert (edit
, ' ');
2613 /* --------------------------------------------------------------------------------------------- */
2616 edit_delete (WEdit
* edit
, gboolean byte_delete
)
2619 int char_length
= 1;
2622 if (edit
->buffer
.curs2
== 0)
2626 /* if byte_delete == TRUE then delete only one byte not multibyte char */
2627 if (edit
->utf8
&& !byte_delete
)
2629 edit_buffer_get_utf (&edit
->buffer
, edit
->buffer
.curs1
, &char_length
);
2630 if (char_length
< 1)
2637 if (edit
->mark2
!= edit
->mark1
)
2638 edit_push_markers (edit
);
2640 for (i
= 1; i
<= char_length
; i
++)
2642 if (edit
->mark1
> edit
->buffer
.curs1
)
2645 edit
->end_mark_curs
--;
2647 if (edit
->mark2
> edit
->buffer
.curs1
)
2649 if (edit
->last_get_rule
> edit
->buffer
.curs1
)
2650 edit
->last_get_rule
--;
2652 p
= edit_buffer_delete (&edit
->buffer
);
2654 edit_push_undo_action (edit
, p
+ 256);
2657 edit_modification (edit
);
2660 book_mark_dec (edit
, edit
->buffer
.curs_line
);
2661 edit
->buffer
.lines
--;
2662 edit
->force
|= REDRAW_AFTER_CURSOR
;
2664 if (edit
->buffer
.curs1
< edit
->start_display
)
2666 edit
->start_display
--;
2674 /* --------------------------------------------------------------------------------------------- */
2677 edit_backspace (WEdit
* edit
, gboolean byte_delete
)
2680 int char_length
= 1;
2683 if (edit
->buffer
.curs1
== 0)
2686 if (edit
->mark2
!= edit
->mark1
)
2687 edit_push_markers (edit
);
2690 if (edit
->utf8
&& !byte_delete
)
2692 edit_buffer_get_prev_utf (&edit
->buffer
, edit
->buffer
.curs1
, &char_length
);
2693 if (char_length
< 1)
2700 for (i
= 1; i
<= char_length
; i
++)
2702 if (edit
->mark1
>= edit
->buffer
.curs1
)
2705 edit
->end_mark_curs
--;
2707 if (edit
->mark2
>= edit
->buffer
.curs1
)
2709 if (edit
->last_get_rule
>= edit
->buffer
.curs1
)
2710 edit
->last_get_rule
--;
2712 p
= edit_buffer_backspace (&edit
->buffer
);
2714 edit_push_undo_action (edit
, p
);
2716 edit_modification (edit
);
2719 book_mark_dec (edit
, edit
->buffer
.curs_line
);
2720 edit
->buffer
.curs_line
--;
2721 edit
->buffer
.lines
--;
2722 edit
->force
|= REDRAW_AFTER_CURSOR
;
2725 if (edit
->buffer
.curs1
< edit
->start_display
)
2727 edit
->start_display
--;
2735 /* --------------------------------------------------------------------------------------------- */
2736 /** moves the cursor right or left: increment positive or negative respectively */
2739 edit_cursor_move (WEdit
* edit
, off_t increment
)
2743 for (; increment
< 0 && edit
->buffer
.curs1
!= 0; increment
++)
2747 edit_push_undo_action (edit
, CURS_RIGHT
);
2749 c
= edit_buffer_get_previous_byte (&edit
->buffer
);
2750 edit_buffer_insert_ahead (&edit
->buffer
, c
);
2751 c
= edit_buffer_backspace (&edit
->buffer
);
2754 edit
->buffer
.curs_line
--;
2755 edit
->force
|= REDRAW_LINE_BELOW
;
2761 for (; increment
> 0 && edit
->buffer
.curs2
!= 0; increment
--)
2765 edit_push_undo_action (edit
, CURS_LEFT
);
2767 c
= edit_buffer_get_current_byte (&edit
->buffer
);
2768 edit_buffer_insert (&edit
->buffer
, c
);
2769 c
= edit_buffer_delete (&edit
->buffer
);
2772 edit
->buffer
.curs_line
++;
2773 edit
->force
|= REDRAW_LINE_ABOVE
;
2779 /* --------------------------------------------------------------------------------------------- */
2780 /* If cols is zero this returns the count of columns from current to upto. */
2781 /* If upto is zero returns index of cols across from current. */
2784 edit_move_forward3 (const WEdit
* edit
, off_t current
, long cols
, off_t upto
)
2795 q
= edit
->buffer
.size
+ 2;
2797 for (col
= 0, p
= current
; p
< q
; p
++)
2809 orig_c
= c
= edit_buffer_get_byte (&edit
->buffer
, p
);
2815 int char_length
= 1;
2817 utf_ch
= edit_buffer_get_utf (&edit
->buffer
, p
, &char_length
);
2818 if (mc_global
.utf8_display
)
2820 if (char_length
> 1)
2821 col
-= char_length
- 1;
2822 if (g_unichar_iswide (utf_ch
))
2825 else if (char_length
> 1 && g_unichar_isprint (utf_ch
))
2826 col
-= char_length
- 1;
2829 c
= convert_to_display_c (c
);
2833 return (upto
!= 0 ? (off_t
) col
: p
);
2835 col
+= TAB_SIZE
- col
% TAB_SIZE
;
2836 else if ((c
< 32 || c
== 127) && (orig_c
== c
2838 || (!mc_global
.utf8_display
&& !edit
->utf8
)
2841 /* '\r' is shown as ^M, so we must advance 2 characters */
2842 /* Caret notation for control characters */
2850 /* --------------------------------------------------------------------------------------------- */
2851 /** returns the current offset of the cursor from the beginning of a file */
2854 edit_get_cursor_offset (const WEdit
* edit
)
2856 return edit
->buffer
.curs1
;
2859 /* --------------------------------------------------------------------------------------------- */
2860 /** returns the current column position of the cursor */
2863 edit_get_col (const WEdit
* edit
)
2865 return (long) edit_move_forward3 (edit
, edit_buffer_get_current_bol (&edit
->buffer
), 0,
2866 edit
->buffer
.curs1
);
2869 /* --------------------------------------------------------------------------------------------- */
2870 /* Scrolling functions */
2871 /* --------------------------------------------------------------------------------------------- */
2874 edit_update_curs_row (WEdit
* edit
)
2876 edit
->curs_row
= edit
->buffer
.curs_line
- edit
->start_line
;
2879 /* --------------------------------------------------------------------------------------------- */
2882 edit_update_curs_col (WEdit
* edit
)
2884 edit
->curs_col
= (long) edit_move_forward3 (edit
, edit_buffer_get_current_bol (&edit
->buffer
),
2885 0, edit
->buffer
.curs1
);
2888 /* --------------------------------------------------------------------------------------------- */
2891 edit_get_curs_col (const WEdit
* edit
)
2893 return edit
->curs_col
;
2896 /* --------------------------------------------------------------------------------------------- */
2897 /** moves the display start position up by i lines */
2900 edit_scroll_upward (WEdit
* edit
, long i
)
2902 long lines_above
= edit
->start_line
;
2904 if (i
> lines_above
)
2908 edit
->start_line
-= i
;
2909 edit
->start_display
=
2910 edit_buffer_get_backward_offset (&edit
->buffer
, edit
->start_display
, i
);
2911 edit
->force
|= REDRAW_PAGE
;
2912 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2914 edit_update_curs_row (edit
);
2918 /* --------------------------------------------------------------------------------------------- */
2921 edit_scroll_downward (WEdit
* edit
, long i
)
2925 lines_below
= edit
->buffer
.lines
- edit
->start_line
- (WIDGET (edit
)->rect
.lines
- 1);
2926 if (lines_below
> 0)
2928 if (i
> lines_below
)
2930 edit
->start_line
+= i
;
2931 edit
->start_display
=
2932 edit_buffer_get_forward_offset (&edit
->buffer
, edit
->start_display
, i
, 0);
2933 edit
->force
|= REDRAW_PAGE
;
2934 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2936 edit_update_curs_row (edit
);
2939 /* --------------------------------------------------------------------------------------------- */
2942 edit_scroll_right (WEdit
* edit
, long i
)
2944 edit
->force
|= REDRAW_PAGE
;
2945 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2946 edit
->start_col
-= i
;
2949 /* --------------------------------------------------------------------------------------------- */
2952 edit_scroll_left (WEdit
* edit
, long i
)
2954 if (edit
->start_col
)
2956 edit
->start_col
+= i
;
2957 if (edit
->start_col
> 0)
2958 edit
->start_col
= 0;
2959 edit
->force
|= REDRAW_PAGE
;
2960 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2964 /* --------------------------------------------------------------------------------------------- */
2965 /* high level cursor movement commands */
2966 /* --------------------------------------------------------------------------------------------- */
2969 edit_move_to_prev_col (WEdit
* edit
, off_t p
)
2971 long prev
= edit
->prev_col
;
2972 long over
= edit
->over_col
;
2974 edit_cursor_move (edit
,
2975 edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->buffer
.curs1
);
2977 if (edit_options
.cursor_beyond_eol
)
2981 line_len
= (long) edit_move_forward3 (edit
, edit_buffer_get_current_bol (&edit
->buffer
), 0,
2982 edit_buffer_get_current_eol (&edit
->buffer
));
2983 if (line_len
< prev
+ edit
->over_col
)
2985 edit
->over_col
= prev
+ over
- line_len
;
2986 edit
->prev_col
= line_len
;
2987 edit
->curs_col
= line_len
;
2991 edit
->curs_col
= prev
+ over
;
2992 edit
->prev_col
= edit
->curs_col
;
2999 if (edit_options
.fake_half_tabs
&& is_in_indent (&edit
->buffer
))
3001 long fake_half_tabs
;
3003 edit_update_curs_col (edit
);
3005 fake_half_tabs
= HALF_TAB_SIZE
* space_width
;
3006 if (fake_half_tabs
!= 0 && edit
->curs_col
% fake_half_tabs
!= 0)
3011 edit
->curs_col
-= (edit
->curs_col
% fake_half_tabs
);
3012 p
= edit_buffer_get_current_bol (&edit
->buffer
);
3013 edit_cursor_move (edit
,
3014 edit_move_forward3 (edit
, p
, edit
->curs_col
,
3015 0) - edit
->buffer
.curs1
);
3016 if (!left_of_four_spaces (edit
))
3017 edit_cursor_move (edit
,
3018 edit_move_forward3 (edit
, p
, q
, 0) - edit
->buffer
.curs1
);
3024 /* --------------------------------------------------------------------------------------------- */
3025 /** check whether line in editor is blank or not
3027 * @param edit editor object
3028 * @param line number of line
3030 * @return TRUE if line in blank, FALSE otherwise
3034 edit_line_is_blank (WEdit
* edit
, long line
)
3036 return is_blank (&edit
->buffer
, edit_find_line (edit
, line
));
3039 /* --------------------------------------------------------------------------------------------- */
3040 /** move cursor to line 'line' */
3043 edit_move_to_line (WEdit
* e
, long line
)
3045 if (line
< e
->buffer
.curs_line
)
3046 edit_move_up (e
, e
->buffer
.curs_line
- line
, FALSE
);
3048 edit_move_down (e
, line
- e
->buffer
.curs_line
, FALSE
);
3049 edit_scroll_screen_over_cursor (e
);
3052 /* --------------------------------------------------------------------------------------------- */
3053 /** scroll window so that first visible line is 'line' */
3056 edit_move_display (WEdit
* e
, long line
)
3058 if (line
< e
->start_line
)
3059 edit_scroll_upward (e
, e
->start_line
- line
);
3061 edit_scroll_downward (e
, line
- e
->start_line
);
3064 /* --------------------------------------------------------------------------------------------- */
3065 /** save markers onto undo stack */
3068 edit_push_markers (WEdit
* edit
)
3070 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3071 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3072 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3075 /* --------------------------------------------------------------------------------------------- */
3078 edit_set_markers (WEdit
* edit
, off_t m1
, off_t m2
, long c1
, long c2
)
3087 /* --------------------------------------------------------------------------------------------- */
3088 /** highlight marker toggle */
3091 edit_mark_cmd (WEdit
* edit
, gboolean unmark
)
3093 edit_push_markers (edit
);
3096 edit_set_markers (edit
, 0, 0, 0, 0);
3097 edit
->force
|= REDRAW_PAGE
;
3099 else if (edit
->mark2
>= 0)
3101 edit
->end_mark_curs
= -1;
3102 edit_set_markers (edit
, edit
->buffer
.curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3103 edit
->curs_col
+ edit
->over_col
);
3104 edit
->force
|= REDRAW_PAGE
;
3108 edit
->end_mark_curs
= edit
->buffer
.curs1
;
3109 edit_set_markers (edit
, edit
->mark1
, edit
->buffer
.curs1
, edit
->column1
,
3110 edit
->curs_col
+ edit
->over_col
);
3114 /* --------------------------------------------------------------------------------------------- */
3115 /** highlight the word under cursor */
3118 edit_mark_current_word_cmd (WEdit
* edit
)
3122 for (pos
= edit
->buffer
.curs1
; pos
!= 0; pos
--)
3126 c1
= edit_buffer_get_byte (&edit
->buffer
, pos
);
3127 c2
= edit_buffer_get_byte (&edit
->buffer
, pos
- 1);
3128 if (!isspace (c1
) && isspace (c2
))
3130 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3135 for (; pos
< edit
->buffer
.size
; pos
++)
3139 c1
= edit_buffer_get_byte (&edit
->buffer
, pos
);
3140 c2
= edit_buffer_get_byte (&edit
->buffer
, pos
+ 1);
3141 if (!isspace (c1
) && isspace (c2
))
3143 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3146 edit
->mark2
= MIN (pos
+ 1, edit
->buffer
.size
);
3148 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3151 /* --------------------------------------------------------------------------------------------- */
3154 edit_mark_current_line_cmd (WEdit
* edit
)
3156 edit
->mark1
= edit_buffer_get_current_bol (&edit
->buffer
);
3157 edit
->mark2
= edit_buffer_get_current_eol (&edit
->buffer
);
3159 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3162 /* --------------------------------------------------------------------------------------------- */
3165 edit_delete_line (WEdit
* edit
)
3168 * Delete right part of the line.
3169 * Note that edit_buffer_get_byte() returns '\n' when byte position is
3172 while (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
3173 (void) edit_delete (edit
, TRUE
);
3177 * Note that edit_delete() will not corrupt anything if called while
3178 * cursor position is EOF.
3180 (void) edit_delete (edit
, TRUE
);
3183 * Delete left part of the line.
3184 * Note, that edit_buffer_get_byte() returns '\n' when byte position is < 0.
3186 while (edit_buffer_get_previous_byte (&edit
->buffer
) != '\n')
3187 (void) edit_backspace (edit
, TRUE
);
3190 /* --------------------------------------------------------------------------------------------- */
3193 edit_push_key_press (WEdit
* edit
)
3195 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3196 if (edit
->mark2
== -1)
3198 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3199 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3203 /* --------------------------------------------------------------------------------------------- */
3206 edit_find_bracket (WEdit
* edit
)
3208 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3209 if (edit
->last_bracket
!= edit
->bracket
)
3210 edit
->force
|= REDRAW_PAGE
;
3211 edit
->last_bracket
= edit
->bracket
;
3214 /* --------------------------------------------------------------------------------------------- */
3216 * This executes a command as though the user initiated it through a key
3217 * press. Callback with MSG_KEY as a message calls this after
3218 * translating the key press. This function can be used to pass any
3219 * command to the editor. Note that the screen wouldn't update
3220 * automatically. Either of command or char_for_insertion must be
3221 * passed as -1. Commands are executed, and char_for_insertion is
3222 * inserted at the cursor.
3226 edit_execute_key_command (WEdit
* edit
, long command
, int char_for_insertion
)
3228 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3230 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3233 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3236 if (macro_index
!= -1)
3238 edit
->force
|= REDRAW_COMPLETELY
;
3239 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3241 edit_store_macro_cmd (edit
);
3245 if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3247 edit_repeat_macro_cmd (edit
);
3253 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3255 record_macro_buf
[macro_index
].action
= command
;
3256 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3258 /* record the beginning of a set of editing actions initiated by a key press */
3259 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3260 edit_push_key_press (edit
);
3262 edit_execute_cmd (edit
, command
, char_for_insertion
);
3263 if (edit
->column_highlight
)
3264 edit
->force
|= REDRAW_PAGE
;
3267 /* --------------------------------------------------------------------------------------------- */
3269 This executes a command at a lower level than macro recording.
3270 It also does not push a key_press onto the undo stack. This means
3271 that if it is called many times, a single undo command will undo
3272 all of them. It also does not check for the Undo command.
3275 edit_execute_cmd (WEdit
* edit
, long command
, int char_for_insertion
)
3277 WRect
*w
= &WIDGET (edit
)->rect
;
3279 if (command
== CK_WindowFullscreen
)
3281 edit_toggle_fullscreen (edit
);
3285 /* handle window state */
3286 if (edit_handle_move_resize (edit
, command
))
3289 edit
->force
|= REDRAW_LINE
;
3291 /* The next key press will unhighlight the found string, so update
3293 if (edit
->found_len
|| edit
->column_highlight
)
3294 edit
->force
|= REDRAW_PAGE
;
3298 /* a mark command with shift-arrow */
3301 case CK_MarkToWordBegin
:
3302 case CK_MarkToWordEnd
:
3308 case CK_MarkPageDown
:
3309 case CK_MarkToFileBegin
:
3310 case CK_MarkToFileEnd
:
3311 case CK_MarkToPageBegin
:
3312 case CK_MarkToPageEnd
:
3313 case CK_MarkScrollUp
:
3314 case CK_MarkScrollDown
:
3315 case CK_MarkParagraphUp
:
3316 case CK_MarkParagraphDown
:
3317 /* a mark command with alt-arrow */
3318 case CK_MarkColumnPageUp
:
3319 case CK_MarkColumnPageDown
:
3320 case CK_MarkColumnLeft
:
3321 case CK_MarkColumnRight
:
3322 case CK_MarkColumnUp
:
3323 case CK_MarkColumnDown
:
3324 case CK_MarkColumnScrollUp
:
3325 case CK_MarkColumnScrollDown
:
3326 case CK_MarkColumnParagraphUp
:
3327 case CK_MarkColumnParagraphDown
:
3328 edit
->column_highlight
= 0;
3329 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3331 edit_mark_cmd (edit
, TRUE
); /* clear */
3332 edit_mark_cmd (edit
, FALSE
); /* marking on */
3334 edit
->highlight
= 1;
3337 /* any other command */
3339 if (edit
->highlight
)
3340 edit_mark_cmd (edit
, FALSE
); /* clear */
3341 edit
->highlight
= 0;
3344 /* first check for undo */
3345 if (command
== CK_Undo
)
3347 edit
->redo_stack_reset
= 0;
3348 edit_group_undo (edit
);
3349 edit
->found_len
= 0;
3350 edit
->prev_col
= edit_get_col (edit
);
3351 edit
->search_start
= edit
->buffer
.curs1
;
3354 /* check for redo */
3355 if (command
== CK_Redo
)
3357 edit
->redo_stack_reset
= 0;
3358 edit_do_redo (edit
);
3359 edit
->found_len
= 0;
3360 edit
->prev_col
= edit_get_col (edit
);
3361 edit
->search_start
= edit
->buffer
.curs1
;
3365 edit
->redo_stack_reset
= 1;
3367 /* An ordinary key press */
3368 if (char_for_insertion
>= 0)
3370 /* if non persistent selection and text selected */
3371 if (!edit_options
.persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3372 edit_block_delete_cmd (edit
);
3374 if (edit
->overwrite
)
3376 /* remove char only one time, after input first byte, multibyte chars */
3378 if (!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3380 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
3382 edit_delete (edit
, FALSE
);
3384 if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
3385 edit_insert_over (edit
);
3388 Encode 8-bit input as UTF-8, if display (locale) is *not* UTF-8,
3389 *but* source encoding *is* set to UTF-8; see ticket #3843 for the details.
3391 if (char_for_insertion
> 127 && str_isutf8 (get_codepage_id (mc_global
.source_codepage
))
3392 && !mc_global
.utf8_display
)
3394 unsigned char str
[UTF8_CHAR_LEN
+ 1];
3398 res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3408 for (i
= 0; i
<= UTF8_CHAR_LEN
&& str
[i
] != '\0'; i
++)
3410 char_for_insertion
= str
[i
];
3411 edit_insert (edit
, char_for_insertion
);
3416 edit_insert (edit
, char_for_insertion
);
3418 if (edit_options
.auto_para_formatting
)
3420 format_paragraph (edit
, FALSE
);
3421 edit
->force
|= REDRAW_PAGE
;
3424 check_and_wrap_line (edit
);
3425 edit
->found_len
= 0;
3426 edit
->prev_col
= edit_get_col (edit
);
3427 edit
->search_start
= edit
->buffer
.curs1
;
3428 edit_find_bracket (edit
);
3434 case CK_TopOnScreen
:
3435 case CK_BottomOnScreen
:
3448 if (!edit_options
.persistent_selections
&& edit
->mark2
>= 0)
3450 if (edit
->column_highlight
)
3451 edit_push_undo_action (edit
, COLUMN_ON
);
3452 edit
->column_highlight
= 0;
3453 edit_mark_cmd (edit
, TRUE
);
3462 case CK_TopOnScreen
:
3463 case CK_BottomOnScreen
:
3464 case CK_MarkToPageBegin
:
3465 case CK_MarkToPageEnd
:
3470 case CK_MarkToWordBegin
:
3471 case CK_MarkToWordEnd
:
3474 case CK_MarkColumnUp
:
3475 case CK_MarkColumnDown
:
3476 if (edit
->mark2
== -1)
3477 break; /*marking is following the cursor: may need to highlight a whole line */
3483 edit
->force
|= REDRAW_CHAR_ONLY
;
3489 /* basic cursor key commands */
3493 /* if non persistent selection and text selected */
3494 if (!edit_options
.persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3495 edit_block_delete_cmd (edit
);
3496 else if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
3498 else if (edit_options
.backspace_through_tabs
&& is_in_indent (&edit
->buffer
))
3500 while (edit_buffer_get_previous_byte (&edit
->buffer
) != '\n' && edit
->buffer
.curs1
> 0)
3501 edit_backspace (edit
, TRUE
);
3503 else if (edit_options
.fake_half_tabs
&& is_in_indent (&edit
->buffer
)
3504 && right_of_four_spaces (edit
))
3508 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3509 edit_backspace (edit
, TRUE
);
3512 edit_backspace (edit
, FALSE
);
3515 /* if non persistent selection and text selected */
3516 if (!edit_options
.persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3517 edit_block_delete_cmd (edit
);
3520 if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
3521 edit_insert_over (edit
);
3523 if (edit_options
.fake_half_tabs
&& is_in_indent (&edit
->buffer
)
3524 && left_of_four_spaces (edit
))
3528 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3529 edit_delete (edit
, TRUE
);
3532 edit_delete (edit
, FALSE
);
3535 case CK_DeleteToWordBegin
:
3537 edit_left_delete_word (edit
);
3539 case CK_DeleteToWordEnd
:
3540 if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
3541 edit_insert_over (edit
);
3543 edit_right_delete_word (edit
);
3546 edit_delete_line (edit
);
3548 case CK_DeleteToHome
:
3549 edit_delete_to_line_begin (edit
);
3551 case CK_DeleteToEnd
:
3552 edit_delete_to_line_end (edit
);
3556 if (edit_options
.auto_para_formatting
)
3558 edit_double_newline (edit
);
3559 if (edit_options
.return_does_auto_indent
&& !bracketed_pasting_in_progress
)
3560 edit_auto_indent (edit
);
3561 format_paragraph (edit
, FALSE
);
3565 edit_insert (edit
, '\n');
3566 if (edit_options
.return_does_auto_indent
&& !bracketed_pasting_in_progress
)
3567 edit_auto_indent (edit
);
3571 edit_insert (edit
, '\n');
3574 case CK_MarkColumnPageUp
:
3575 edit
->column_highlight
= 1;
3579 edit_move_up (edit
, w
->lines
- 1, TRUE
);
3581 case CK_MarkColumnPageDown
:
3582 edit
->column_highlight
= 1;
3585 case CK_MarkPageDown
:
3586 edit_move_down (edit
, w
->lines
- 1, TRUE
);
3588 case CK_MarkColumnLeft
:
3589 edit
->column_highlight
= 1;
3593 if (edit_options
.fake_half_tabs
&& is_in_indent (&edit
->buffer
)
3594 && right_of_four_spaces (edit
))
3596 if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
3599 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3600 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3603 edit_left_char_move_cmd (edit
);
3605 case CK_MarkColumnRight
:
3606 edit
->column_highlight
= 1;
3610 if (edit_options
.fake_half_tabs
&& is_in_indent (&edit
->buffer
)
3611 && left_of_four_spaces (edit
))
3613 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3614 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3617 edit_right_char_move_cmd (edit
);
3619 case CK_TopOnScreen
:
3620 case CK_MarkToPageBegin
:
3621 edit_begin_page (edit
);
3623 case CK_BottomOnScreen
:
3624 case CK_MarkToPageEnd
:
3625 edit_end_page (edit
);
3628 case CK_MarkToWordBegin
:
3630 edit_left_word_move_cmd (edit
);
3633 case CK_MarkToWordEnd
:
3635 edit_right_word_move_cmd (edit
);
3637 case CK_MarkColumnUp
:
3638 edit
->column_highlight
= 1;
3642 edit_move_up (edit
, 1, FALSE
);
3644 case CK_MarkColumnDown
:
3645 edit
->column_highlight
= 1;
3649 edit_move_down (edit
, 1, FALSE
);
3651 case CK_MarkColumnParagraphUp
:
3652 edit
->column_highlight
= 1;
3654 case CK_ParagraphUp
:
3655 case CK_MarkParagraphUp
:
3656 edit_move_up_paragraph (edit
, FALSE
);
3658 case CK_MarkColumnParagraphDown
:
3659 edit
->column_highlight
= 1;
3661 case CK_ParagraphDown
:
3662 case CK_MarkParagraphDown
:
3663 edit_move_down_paragraph (edit
, FALSE
);
3665 case CK_MarkColumnScrollUp
:
3666 edit
->column_highlight
= 1;
3669 case CK_MarkScrollUp
:
3670 edit_move_up (edit
, 1, TRUE
);
3672 case CK_MarkColumnScrollDown
:
3673 edit
->column_highlight
= 1;
3676 case CK_MarkScrollDown
:
3677 edit_move_down (edit
, 1, TRUE
);
3681 edit_cursor_to_bol (edit
);
3685 edit_cursor_to_eol (edit
);
3688 /* if text marked shift block */
3689 if (edit
->mark1
!= edit
->mark2
&& !edit_options
.persistent_selections
)
3691 if (edit
->mark2
< 0)
3692 edit_mark_cmd (edit
, FALSE
);
3693 edit_move_block_to_right (edit
);
3697 if (edit_options
.cursor_beyond_eol
)
3698 edit_insert_over (edit
);
3699 edit_tab_cmd (edit
);
3700 if (edit_options
.auto_para_formatting
)
3702 format_paragraph (edit
, FALSE
);
3703 edit
->force
|= REDRAW_PAGE
;
3706 check_and_wrap_line (edit
);
3710 case CK_InsertOverwrite
:
3711 edit
->overwrite
= !edit
->overwrite
;
3715 if (edit
->mark2
>= 0)
3717 if (edit
->column_highlight
)
3718 edit_push_undo_action (edit
, COLUMN_ON
);
3719 edit
->column_highlight
= 0;
3721 edit_mark_cmd (edit
, FALSE
);
3724 if (!edit
->column_highlight
)
3725 edit_push_undo_action (edit
, COLUMN_OFF
);
3726 edit
->column_highlight
= 1;
3727 edit_mark_cmd (edit
, FALSE
);
3730 edit_set_markers (edit
, 0, edit
->buffer
.size
, 0, 0);
3731 edit
->force
|= REDRAW_PAGE
;
3734 if (edit
->column_highlight
)
3735 edit_push_undo_action (edit
, COLUMN_ON
);
3736 edit
->column_highlight
= 0;
3737 edit_mark_cmd (edit
, TRUE
);
3740 if (edit
->column_highlight
)
3741 edit_push_undo_action (edit
, COLUMN_ON
);
3742 edit
->column_highlight
= 0;
3743 edit_mark_current_word_cmd (edit
);
3746 if (edit
->column_highlight
)
3747 edit_push_undo_action (edit
, COLUMN_ON
);
3748 edit
->column_highlight
= 0;
3749 edit_mark_current_line_cmd (edit
);
3753 book_mark_clear (edit
, edit
->buffer
.curs_line
, BOOK_MARK_FOUND_COLOR
);
3754 if (book_mark_query_color (edit
, edit
->buffer
.curs_line
, BOOK_MARK_COLOR
))
3755 book_mark_clear (edit
, edit
->buffer
.curs_line
, BOOK_MARK_COLOR
);
3757 book_mark_insert (edit
, edit
->buffer
.curs_line
, BOOK_MARK_COLOR
);
3759 case CK_BookmarkFlush
:
3760 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3761 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3762 edit
->force
|= REDRAW_PAGE
;
3764 case CK_BookmarkNext
:
3765 if (edit
->book_mark
!= NULL
)
3767 edit_book_mark_t
*p
;
3769 p
= book_mark_find (edit
, edit
->buffer
.curs_line
);
3770 if (p
->next
!= NULL
)
3773 if (p
->line
>= edit
->start_line
+ w
->lines
|| p
->line
< edit
->start_line
)
3774 edit_move_display (edit
, p
->line
- w
->lines
/ 2);
3775 edit_move_to_line (edit
, p
->line
);
3779 case CK_BookmarkPrev
:
3780 if (edit
->book_mark
!= NULL
)
3782 edit_book_mark_t
*p
;
3784 p
= book_mark_find (edit
, edit
->buffer
.curs_line
);
3785 while (p
->line
== edit
->buffer
.curs_line
)
3786 if (p
->prev
!= NULL
)
3790 if (p
->line
>= edit
->start_line
+ w
->lines
|| p
->line
< edit
->start_line
)
3791 edit_move_display (edit
, p
->line
- w
->lines
/ 2);
3792 edit_move_to_line (edit
, p
->line
);
3798 case CK_MarkToFileBegin
:
3799 edit_move_to_top (edit
);
3802 case CK_MarkToFileEnd
:
3803 edit_move_to_bottom (edit
);
3807 if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
3808 edit_insert_over (edit
);
3809 edit_block_copy_cmd (edit
);
3812 edit_block_delete_cmd (edit
);
3815 edit_block_move_cmd (edit
);
3818 case CK_BlockShiftLeft
:
3819 if (edit
->mark1
!= edit
->mark2
)
3820 edit_move_block_to_left (edit
);
3822 case CK_BlockShiftRight
:
3823 if (edit
->mark1
!= edit
->mark2
)
3824 edit_move_block_to_right (edit
);
3827 edit_copy_to_X_buf_cmd (edit
);
3830 edit_cut_to_X_buf_cmd (edit
);
3833 /* if non persistent selection and text selected */
3834 if (!edit_options
.persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3835 edit_block_delete_cmd (edit
);
3836 if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
3837 edit_insert_over (edit
);
3838 edit_paste_from_X_buf_cmd (edit
);
3839 if (!edit_options
.persistent_selections
&& edit
->mark2
>= 0)
3841 if (edit
->column_highlight
)
3842 edit_push_undo_action (edit
, COLUMN_ON
);
3843 edit
->column_highlight
= 0;
3844 edit_mark_cmd (edit
, TRUE
);
3848 edit_paste_from_history (edit
);
3852 edit_save_as_cmd (edit
);
3855 edit_save_confirm_cmd (edit
);
3858 edit_save_block_cmd (edit
);
3861 edit_insert_file_cmd (edit
);
3865 edit_load_back_cmd (edit
);
3868 edit_load_forward_cmd (edit
);
3871 case CK_SyntaxChoose
:
3872 edit_syntax_dialog (edit
);
3876 edit_search_cmd (edit
, FALSE
);
3878 case CK_SearchContinue
:
3879 edit_search_cmd (edit
, TRUE
);
3882 edit_replace_cmd (edit
, FALSE
);
3884 case CK_ReplaceContinue
:
3885 edit_replace_cmd (edit
, TRUE
);
3888 /* if text marked shift block */
3889 if (edit
->mark1
!= edit
->mark2
&& !edit_options
.persistent_selections
)
3890 edit_move_block_to_left (edit
);
3892 edit_complete_word_cmd (edit
);
3895 edit_get_match_keyword_cmd (edit
);
3899 case CK_SpellCheckCurrentWord
:
3900 edit_suggest_current_word (edit
);
3903 edit_spellcheck_file (edit
);
3905 case CK_SpellCheckSelectLang
:
3906 edit_set_spell_lang ();
3913 /* fool gcc to prevent a Y2K warning */
3914 char time_format
[] = "_c";
3915 time_format
[0] = '%';
3917 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
3918 edit_print_string (edit
, s
);
3919 edit
->force
|= REDRAW_PAGE
;
3923 edit_goto_cmd (edit
);
3925 case CK_ParagraphFormat
:
3926 format_paragraph (edit
, TRUE
);
3927 edit
->force
|= REDRAW_PAGE
;
3929 case CK_MacroDelete
:
3930 edit_delete_macro_cmd (edit
);
3932 case CK_MatchBracket
:
3933 edit_goto_matching_bracket (edit
);
3936 edit_user_menu (edit
, NULL
, -1);
3939 edit_sort_cmd (edit
);
3941 case CK_ExternalCommand
:
3942 edit_ext_cmd (edit
);
3945 edit_mail_dialog (edit
);
3948 case CK_SelectCodepage
:
3949 edit_select_codepage_cmd (edit
);
3952 case CK_InsertLiteral
:
3953 edit_insert_literal_cmd (edit
);
3955 case CK_MacroStartStopRecord
:
3956 edit_begin_end_macro_cmd (edit
);
3958 case CK_RepeatStartStopRecord
:
3959 edit_begin_end_repeat_cmd (edit
);
3961 case CK_ExtendedKeyMap
:
3962 WIDGET (edit
)->ext_mode
= TRUE
;
3969 if ((command
/ CK_PipeBlock (0)) == 1)
3970 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
3972 /* keys which must set the col position, and the search vars */
3976 case CK_SearchContinue
:
3978 case CK_ReplaceContinue
:
3980 edit
->prev_col
= edit_get_col (edit
);
3984 case CK_MarkColumnUp
:
3987 case CK_MarkColumnDown
:
3990 case CK_MarkColumnPageUp
:
3992 case CK_MarkPageDown
:
3993 case CK_MarkColumnPageDown
:
3995 case CK_MarkToFileBegin
:
3997 case CK_MarkToFileEnd
:
3998 case CK_ParagraphUp
:
3999 case CK_MarkParagraphUp
:
4000 case CK_MarkColumnParagraphUp
:
4001 case CK_ParagraphDown
:
4002 case CK_MarkParagraphDown
:
4003 case CK_MarkColumnParagraphDown
:
4005 case CK_MarkScrollUp
:
4006 case CK_MarkColumnScrollUp
:
4008 case CK_MarkScrollDown
:
4009 case CK_MarkColumnScrollDown
:
4010 edit
->search_start
= edit
->buffer
.curs1
;
4011 edit
->found_len
= 0;
4014 edit
->found_len
= 0;
4015 edit
->prev_col
= edit_get_col (edit
);
4016 edit
->search_start
= edit
->buffer
.curs1
;
4018 edit_find_bracket (edit
);
4020 if (edit_options
.auto_para_formatting
)
4026 case CK_DeleteToWordBegin
:
4027 case CK_DeleteToWordEnd
:
4028 case CK_DeleteToHome
:
4029 case CK_DeleteToEnd
:
4030 format_paragraph (edit
, FALSE
);
4031 edit
->force
|= REDRAW_PAGE
;
4039 /* --------------------------------------------------------------------------------------------- */
4042 edit_stack_init (void)
4044 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4046 edit_history_moveto
[edit_stack_iterator
].filename_vpath
= NULL
;
4047 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4050 edit_stack_iterator
= 0;
4053 /* --------------------------------------------------------------------------------------------- */
4056 edit_stack_free (void)
4058 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4059 vfs_path_free (edit_history_moveto
[edit_stack_iterator
].filename_vpath
, TRUE
);
4062 /* --------------------------------------------------------------------------------------------- */
4066 edit_move_up (WEdit
* edit
, long i
, gboolean do_scroll
)
4068 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4071 /* --------------------------------------------------------------------------------------------- */
4075 edit_move_down (WEdit
* edit
, long i
, gboolean do_scroll
)
4077 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4080 /* --------------------------------------------------------------------------------------------- */