2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996-2024
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_arg_t 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
;
139 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
140 { "zstd -cd %s 2>&1", "zstd > %s", ".zst"},
141 { "lz4 -cd %s 2>&1", "lz4 > %s", ".lz4" },
142 { "lzip -cd %s 2>&1", "lzip > %s", ".lz"},
143 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
144 { "lzop -cd %s 2>&1", "lzop > %s", ".lzo"},
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
)
489 if (edit
->filename_vpath
== NULL
490 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
493 load_file_position (edit
->filename_vpath
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
494 /* apply bookmarks in any case */
495 book_mark_restore (edit
, BOOK_MARK_COLOR
);
502 edit_move_to_line (edit
, line
- 1);
503 edit
->prev_col
= column
;
507 edit_cursor_move (edit
, offset
);
508 line
= edit
->buffer
.curs_line
;
509 edit
->search_start
= edit
->buffer
.curs1
;
512 b
= edit_buffer_get_current_bol (&edit
->buffer
);
513 edit_move_to_prev_col (edit
, b
);
514 edit_move_display (edit
, line
- (WIDGET (edit
)->rect
.lines
/ 2));
517 /* --------------------------------------------------------------------------------------------- */
518 /** Save cursor position in the file */
521 edit_save_position (WEdit
*edit
)
523 if (edit
->filename_vpath
== NULL
524 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
527 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
528 save_file_position (edit
->filename_vpath
, edit
->buffer
.curs_line
+ 1, edit
->curs_col
,
529 edit
->buffer
.curs1
, edit
->serialized_bookmarks
);
530 edit
->serialized_bookmarks
= NULL
;
533 /* --------------------------------------------------------------------------------------------- */
534 /** Clean the WEdit stricture except the widget part */
537 edit_purge_widget (WEdit
*edit
)
539 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
540 char *start
= (char *) edit
+ sizeof (Widget
);
541 memset (start
, 0, len
);
544 /* --------------------------------------------------------------------------------------------- */
547 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
548 then the file should be as it was when he loaded up. Then set edit->modified to 0.
552 edit_pop_undo_action (WEdit
*edit
)
555 unsigned long sp
= edit
->undo_stack_pointer
;
557 if (sp
== edit
->undo_stack_bottom
)
560 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
561 c
= edit
->undo_stack
[sp
];
564 /* edit->undo_stack[sp] = '@'; */
565 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
569 if (sp
== edit
->undo_stack_bottom
)
572 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
573 if (edit
->undo_stack
[sp
] == -2)
575 /* edit->undo_stack[sp] = '@'; */
576 edit
->undo_stack_pointer
= sp
;
579 edit
->undo_stack
[sp
]++;
584 /* --------------------------------------------------------------------------------------------- */
587 edit_pop_redo_action (WEdit
*edit
)
590 unsigned long sp
= edit
->redo_stack_pointer
;
592 if (sp
== edit
->redo_stack_bottom
)
595 sp
= (sp
- 1) & edit
->redo_stack_size_mask
;
596 c
= edit
->redo_stack
[sp
];
599 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
603 if (sp
== edit
->redo_stack_bottom
)
606 c
= edit
->redo_stack
[(sp
- 1) & edit
->redo_stack_size_mask
];
607 if (edit
->redo_stack
[sp
] == -2)
608 edit
->redo_stack_pointer
= sp
;
610 edit
->redo_stack
[sp
]++;
615 /* --------------------------------------------------------------------------------------------- */
618 get_prev_undo_action (WEdit
*edit
)
621 unsigned long sp
= edit
->undo_stack_pointer
;
623 if (sp
== edit
->undo_stack_bottom
)
626 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
627 c
= edit
->undo_stack
[sp
];
631 if (sp
== edit
->undo_stack_bottom
)
634 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
638 /* --------------------------------------------------------------------------------------------- */
639 /** is called whenever a modification is made by one of the four routines below */
642 edit_modification (WEdit
*edit
)
644 edit
->caches_valid
= FALSE
;
646 /* raise lock when file modified */
647 if (edit
->modified
== 0 && edit
->delete_file
== 0)
648 edit
->locked
= lock_file (edit
->filename_vpath
);
652 /* --------------------------------------------------------------------------------------------- */
653 /* high level cursor movement commands */
654 /* --------------------------------------------------------------------------------------------- */
655 /** check whether cursor is in indent part of line
657 * @param edit editor object
659 * @return TRUE if cursor is in indent, FALSE otherwise
663 is_in_indent (const edit_buffer_t
*buf
)
667 for (p
= edit_buffer_get_current_bol (buf
); p
< buf
->curs1
; p
++)
668 if (strchr (" \t", edit_buffer_get_byte (buf
, p
)) == NULL
)
674 /* --------------------------------------------------------------------------------------------- */
675 /** check whether line in editor is blank or not
677 * @param edit editor object
678 * @param offset position in file
680 * @return TRUE if line in blank, FALSE otherwise
684 is_blank (const edit_buffer_t
*buf
, off_t offset
)
688 s
= edit_buffer_get_bol (buf
, offset
);
689 f
= edit_buffer_get_eol (buf
, offset
);
694 c
= edit_buffer_get_byte (buf
, s
);
701 /* --------------------------------------------------------------------------------------------- */
702 /** returns the offset of line i */
705 edit_find_line (WEdit
*edit
, long line
)
709 long m
= 2000000000; /* what is the magic number? */
711 if (!edit
->caches_valid
)
713 memset (edit
->line_numbers
, 0, sizeof (edit
->line_numbers
));
714 memset (edit
->line_offsets
, 0, sizeof (edit
->line_offsets
));
715 /* three offsets that we *know* are line 0 at 0 and these two: */
716 edit
->line_numbers
[1] = edit
->buffer
.curs_line
;
717 edit
->line_offsets
[1] = edit_buffer_get_current_bol (&edit
->buffer
);
718 edit
->line_numbers
[2] = edit
->buffer
.lines
;
719 edit
->line_offsets
[2] = edit_buffer_get_bol (&edit
->buffer
, edit
->buffer
.size
);
720 edit
->caches_valid
= TRUE
;
722 if (line
>= edit
->buffer
.lines
)
723 return edit
->line_offsets
[2];
726 /* find the closest known point */
727 for (i
= 0; i
< N_LINE_CACHES
; i
++)
731 n
= labs (edit
->line_numbers
[i
] - line
);
739 return edit
->line_offsets
[j
]; /* know the offset exactly */
740 if (m
== 1 && j
>= 3)
741 i
= j
; /* one line different - caller might be looping, so stay in this cache */
743 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
744 if (line
> edit
->line_numbers
[j
])
745 edit
->line_offsets
[i
] =
746 edit_buffer_get_forward_offset (&edit
->buffer
, edit
->line_offsets
[j
],
747 line
- edit
->line_numbers
[j
], 0);
749 edit
->line_offsets
[i
] =
750 edit_buffer_get_backward_offset (&edit
->buffer
, edit
->line_offsets
[j
],
751 edit
->line_numbers
[j
] - line
);
752 edit
->line_numbers
[i
] = line
;
753 return edit
->line_offsets
[i
];
756 /* --------------------------------------------------------------------------------------------- */
757 /** moves up until a blank line is reached, or until just
758 before a non-blank line is reached */
761 edit_move_up_paragraph (WEdit
*edit
, gboolean do_scroll
)
765 if (edit
->buffer
.curs_line
> 1)
767 if (!edit_line_is_blank (edit
, edit
->buffer
.curs_line
))
769 for (i
= edit
->buffer
.curs_line
- 1; i
!= 0; i
--)
770 if (edit_line_is_blank (edit
, i
))
773 else if (edit_line_is_blank (edit
, edit
->buffer
.curs_line
- 1))
775 for (i
= edit
->buffer
.curs_line
- 1; i
!= 0; i
--)
776 if (!edit_line_is_blank (edit
, i
))
784 for (i
= edit
->buffer
.curs_line
- 1; i
!= 0; i
--)
785 if (edit_line_is_blank (edit
, i
))
790 edit_move_up (edit
, edit
->buffer
.curs_line
- i
, do_scroll
);
793 /* --------------------------------------------------------------------------------------------- */
794 /** moves down until a blank line is reached, or until just
795 before a non-blank line is reached */
798 edit_move_down_paragraph (WEdit
*edit
, gboolean do_scroll
)
802 if (edit
->buffer
.curs_line
>= edit
->buffer
.lines
- 1)
803 i
= edit
->buffer
.lines
;
804 else if (!edit_line_is_blank (edit
, edit
->buffer
.curs_line
))
806 for (i
= edit
->buffer
.curs_line
+ 1; i
!= 0; i
++)
807 if (edit_line_is_blank (edit
, i
) || i
>= edit
->buffer
.lines
)
810 else if (edit_line_is_blank (edit
, edit
->buffer
.curs_line
+ 1))
812 for (i
= edit
->buffer
.curs_line
+ 1; i
!= 0; i
++)
813 if (!edit_line_is_blank (edit
, i
) || i
> edit
->buffer
.lines
)
821 for (i
= edit
->buffer
.curs_line
+ 1; i
!= 0; i
++)
822 if (edit_line_is_blank (edit
, i
) || i
>= edit
->buffer
.lines
)
825 edit_move_down (edit
, i
- edit
->buffer
.curs_line
, do_scroll
);
828 /* --------------------------------------------------------------------------------------------- */
831 edit_begin_page (WEdit
*edit
)
833 edit_update_curs_row (edit
);
834 edit_move_up (edit
, edit
->curs_row
, FALSE
);
837 /* --------------------------------------------------------------------------------------------- */
840 edit_end_page (WEdit
*edit
)
842 edit_update_curs_row (edit
);
843 edit_move_down (edit
, WIDGET (edit
)->rect
.lines
- edit
->curs_row
- 1, FALSE
);
847 /* --------------------------------------------------------------------------------------------- */
848 /** goto beginning of text */
851 edit_move_to_top (WEdit
*edit
)
853 if (edit
->buffer
.curs_line
!= 0)
855 edit_cursor_move (edit
, -edit
->buffer
.curs1
);
856 edit_move_to_prev_col (edit
, 0);
857 edit
->force
|= REDRAW_PAGE
;
858 edit
->search_start
= 0;
859 edit_update_curs_row (edit
);
863 /* --------------------------------------------------------------------------------------------- */
864 /** goto end of text */
867 edit_move_to_bottom (WEdit
*edit
)
869 if (edit
->buffer
.curs_line
< edit
->buffer
.lines
)
871 edit_move_down (edit
, edit
->buffer
.lines
- edit
->curs_row
, FALSE
);
872 edit
->start_display
= edit
->buffer
.size
;
873 edit
->start_line
= edit
->buffer
.lines
;
874 edit_scroll_upward (edit
, WIDGET (edit
)->rect
.lines
- 1);
875 edit
->force
|= REDRAW_PAGE
;
879 /* --------------------------------------------------------------------------------------------- */
880 /** goto beginning of line */
883 edit_cursor_to_bol (WEdit
*edit
)
887 b
= edit_buffer_get_current_bol (&edit
->buffer
);
888 edit_cursor_move (edit
, b
- edit
->buffer
.curs1
);
889 edit
->search_start
= edit
->buffer
.curs1
;
890 edit
->prev_col
= edit_get_col (edit
);
894 /* --------------------------------------------------------------------------------------------- */
895 /** goto end of line */
898 edit_cursor_to_eol (WEdit
*edit
)
902 b
= edit_buffer_get_current_eol (&edit
->buffer
);
903 edit_cursor_move (edit
, b
- edit
->buffer
.curs1
);
904 edit
->search_start
= edit
->buffer
.curs1
;
905 edit
->prev_col
= edit_get_col (edit
);
909 /* --------------------------------------------------------------------------------------------- */
916 const char chars_move_whole_word
[] =
917 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
924 if (g_ascii_isupper ((gchar
) c
))
926 else if (g_ascii_islower ((gchar
) c
))
928 else if (g_ascii_isalpha (c
))
930 else if (isdigit (c
))
932 else if (isspace (c
))
934 q
= strchr (chars_move_whole_word
, c
);
943 for (x
= 1, p
= chars_move_whole_word
; p
< q
; p
++)
948 while ((q
= strchr (q
+ 1, c
)) != NULL
);
953 /* --------------------------------------------------------------------------------------------- */
956 edit_left_word_move (WEdit
*edit
, int s
)
962 if (edit
->column_highlight
963 && edit
->mark1
!= edit
->mark2
964 && edit
->over_col
== 0
965 && edit
->buffer
.curs1
== edit_buffer_get_current_bol (&edit
->buffer
))
967 edit_cursor_move (edit
, -1);
968 if (edit
->buffer
.curs1
== 0)
970 c1
= edit_buffer_get_previous_byte (&edit
->buffer
);
973 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
976 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
978 if (isspace (c1
) && !isspace (c2
))
980 if (s
!= 0 && !isspace (c1
) && isspace (c2
))
985 /* --------------------------------------------------------------------------------------------- */
988 edit_left_word_move_cmd (WEdit
*edit
)
990 edit_left_word_move (edit
, 0);
991 edit
->force
|= REDRAW_PAGE
;
994 /* --------------------------------------------------------------------------------------------- */
997 edit_right_word_move (WEdit
*edit
, int s
)
1003 if (edit
->column_highlight
1004 && edit
->mark1
!= edit
->mark2
1005 && edit
->over_col
== 0
1006 && edit
->buffer
.curs1
== edit_buffer_get_current_eol (&edit
->buffer
))
1008 edit_cursor_move (edit
, 1);
1009 if (edit
->buffer
.curs1
>= edit
->buffer
.size
)
1011 c1
= edit_buffer_get_previous_byte (&edit
->buffer
);
1014 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
1017 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1019 if (isspace (c1
) && !isspace (c2
))
1021 if (s
!= 0 && !isspace (c1
) && isspace (c2
))
1026 /* --------------------------------------------------------------------------------------------- */
1029 edit_right_word_move_cmd (WEdit
*edit
)
1031 edit_right_word_move (edit
, 0);
1032 edit
->force
|= REDRAW_PAGE
;
1035 /* --------------------------------------------------------------------------------------------- */
1038 edit_right_char_move_cmd (WEdit
*edit
)
1040 int char_length
= 1;
1046 c
= edit_buffer_get_utf (&edit
->buffer
, edit
->buffer
.curs1
, &char_length
);
1047 if (char_length
< 1)
1052 c
= edit_buffer_get_current_byte (&edit
->buffer
);
1054 if (edit_options
.cursor_beyond_eol
&& c
== '\n')
1057 edit_cursor_move (edit
, char_length
);
1060 /* --------------------------------------------------------------------------------------------- */
1063 edit_left_char_move_cmd (WEdit
*edit
)
1065 int char_length
= 1;
1067 if (edit
->column_highlight
1068 && edit_options
.cursor_beyond_eol
1069 && edit
->mark1
!= edit
->mark2
1070 && edit
->over_col
== 0 && edit
->buffer
.curs1
== edit_buffer_get_current_bol (&edit
->buffer
))
1075 edit_buffer_get_prev_utf (&edit
->buffer
, edit
->buffer
.curs1
, &char_length
);
1076 if (char_length
< 1)
1081 if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
1084 edit_cursor_move (edit
, -char_length
);
1087 /* --------------------------------------------------------------------------------------------- */
1088 /** Up or down cursor moving.
1089 direction = TRUE - move up
1094 edit_move_updown (WEdit
*edit
, long lines
, gboolean do_scroll
, gboolean direction
)
1097 long l
= direction
? edit
->buffer
.curs_line
: edit
->buffer
.lines
- edit
->buffer
.curs_line
;
1106 edit
->force
|= REDRAW_PAGE
;
1110 edit_scroll_upward (edit
, lines
);
1112 edit_scroll_downward (edit
, lines
);
1114 p
= edit_buffer_get_current_bol (&edit
->buffer
);
1115 p
= direction
? edit_buffer_get_backward_offset (&edit
->buffer
, p
, lines
) :
1116 edit_buffer_get_forward_offset (&edit
->buffer
, p
, lines
, 0);
1117 edit_cursor_move (edit
, p
- edit
->buffer
.curs1
);
1118 edit_move_to_prev_col (edit
, p
);
1121 /* search start of current multibyte char (like CJK) */
1122 if (edit
->buffer
.curs1
> 0 && edit
->buffer
.curs1
+ 1 < edit
->buffer
.size
1123 && edit_buffer_get_current_byte (&edit
->buffer
) >= 256)
1125 edit_right_char_move_cmd (edit
);
1126 edit_left_char_move_cmd (edit
);
1130 edit
->search_start
= edit
->buffer
.curs1
;
1131 edit
->found_len
= 0;
1134 /* --------------------------------------------------------------------------------------------- */
1137 edit_right_delete_word (WEdit
*edit
)
1139 while (edit
->buffer
.curs1
< edit
->buffer
.size
)
1143 c1
= edit_delete (edit
, TRUE
);
1146 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
1149 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1151 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1156 /* --------------------------------------------------------------------------------------------- */
1159 edit_left_delete_word (WEdit
*edit
)
1161 while (edit
->buffer
.curs1
> 0)
1165 c1
= edit_backspace (edit
, TRUE
);
1168 c2
= edit_buffer_get_previous_byte (&edit
->buffer
);
1171 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1173 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1178 /* --------------------------------------------------------------------------------------------- */
1180 the start column position is not recorded, and hence does not
1181 undo as it happed. But who would notice.
1185 edit_do_undo (WEdit
*edit
)
1190 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1193 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1202 edit_cursor_move (edit
, 1);
1205 edit_cursor_move (edit
, -1);
1209 edit_backspace (edit
, TRUE
);
1213 edit_delete (edit
, TRUE
);
1216 edit
->column_highlight
= 1;
1219 edit
->column_highlight
= 0;
1224 if (ac
>= 256 && ac
< 512)
1225 edit_insert_ahead (edit
, ac
- 256);
1226 if (ac
>= 0 && ac
< 256)
1227 edit_insert (edit
, ac
);
1229 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1231 edit
->mark1
= ac
- MARK_1
;
1232 b
= edit_buffer_get_bol (&edit
->buffer
, edit
->mark1
);
1233 edit
->column1
= (long) edit_move_forward3 (edit
, b
, 0, edit
->mark1
);
1235 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1237 edit
->mark2
= ac
- MARK_2
;
1238 b
= edit_buffer_get_bol (&edit
->buffer
, edit
->mark2
);
1239 edit
->column2
= (long) edit_move_forward3 (edit
, b
, 0, edit
->mark2
);
1241 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1243 edit
->end_mark_curs
= ac
- MARK_CURS
;
1246 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1249 if (edit
->start_display
> ac
- KEY_PRESS
)
1252 edit_buffer_count_lines (&edit
->buffer
, ac
- KEY_PRESS
, edit
->start_display
);
1253 edit
->force
|= REDRAW_PAGE
;
1255 else if (edit
->start_display
< ac
- KEY_PRESS
)
1258 edit_buffer_count_lines (&edit
->buffer
, edit
->start_display
, ac
- KEY_PRESS
);
1259 edit
->force
|= REDRAW_PAGE
;
1261 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1262 edit_update_curs_row (edit
);
1265 edit
->undo_stack_disable
= 0;
1268 /* --------------------------------------------------------------------------------------------- */
1271 edit_do_redo (WEdit
*edit
)
1276 if (edit
->redo_stack_reset
)
1281 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1290 edit_cursor_move (edit
, 1);
1293 edit_cursor_move (edit
, -1);
1296 edit_backspace (edit
, TRUE
);
1299 edit_delete (edit
, TRUE
);
1302 edit
->column_highlight
= 1;
1305 edit
->column_highlight
= 0;
1310 if (ac
>= 256 && ac
< 512)
1311 edit_insert_ahead (edit
, ac
- 256);
1312 if (ac
>= 0 && ac
< 256)
1313 edit_insert (edit
, ac
);
1315 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1317 edit
->mark1
= ac
- MARK_1
;
1318 b
= edit_buffer_get_bol (&edit
->buffer
, edit
->mark1
);
1319 edit
->column1
= (long) edit_move_forward3 (edit
, b
, 0, edit
->mark1
);
1321 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1323 edit
->mark2
= ac
- MARK_2
;
1324 b
= edit_buffer_get_bol (&edit
->buffer
, edit
->mark2
);
1325 edit
->column2
= (long) edit_move_forward3 (edit
, b
, 0, edit
->mark2
);
1327 /* more than one pop usually means something big */
1329 edit
->force
|= REDRAW_PAGE
;
1332 if (edit
->start_display
> ac
- KEY_PRESS
)
1335 edit_buffer_count_lines (&edit
->buffer
, ac
- KEY_PRESS
, edit
->start_display
);
1336 edit
->force
|= REDRAW_PAGE
;
1338 else if (edit
->start_display
< ac
- KEY_PRESS
)
1341 edit_buffer_count_lines (&edit
->buffer
, edit
->start_display
, ac
- KEY_PRESS
);
1342 edit
->force
|= REDRAW_PAGE
;
1344 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1345 edit_update_curs_row (edit
);
1351 /* --------------------------------------------------------------------------------------------- */
1354 edit_group_undo (WEdit
*edit
)
1356 long ac
= KEY_PRESS
;
1357 long cur_ac
= KEY_PRESS
;
1359 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1361 cur_ac
= get_prev_undo_action (edit
);
1362 edit_do_undo (edit
);
1363 ac
= get_prev_undo_action (edit
);
1364 /* exit from cycle if edit_options.group_undo is not set,
1365 * and make single UNDO operation
1367 if (!edit_options
.group_undo
)
1372 /* --------------------------------------------------------------------------------------------- */
1375 edit_delete_to_line_end (WEdit
*edit
)
1377 while (edit_buffer_get_current_byte (&edit
->buffer
) != '\n' && edit
->buffer
.curs2
!= 0)
1378 edit_delete (edit
, TRUE
);
1381 /* --------------------------------------------------------------------------------------------- */
1384 edit_delete_to_line_begin (WEdit
*edit
)
1386 while (edit_buffer_get_previous_byte (&edit
->buffer
) != '\n' && edit
->buffer
.curs1
!= 0)
1387 edit_backspace (edit
, TRUE
);
1390 /* --------------------------------------------------------------------------------------------- */
1393 is_aligned_on_a_tab (WEdit
*edit
)
1397 edit_update_curs_col (edit
);
1398 curs_col
= edit
->curs_col
% (TAB_SIZE
* space_width
);
1399 return (curs_col
== 0 || curs_col
== (HALF_TAB_SIZE
* space_width
));
1402 /* --------------------------------------------------------------------------------------------- */
1405 right_of_four_spaces (WEdit
*edit
)
1410 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1411 ch
|= edit_buffer_get_byte (&edit
->buffer
, edit
->buffer
.curs1
- i
);
1413 return (ch
== ' ' && is_aligned_on_a_tab (edit
));
1416 /* --------------------------------------------------------------------------------------------- */
1419 left_of_four_spaces (WEdit
*edit
)
1423 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1424 ch
|= edit_buffer_get_byte (&edit
->buffer
, edit
->buffer
.curs1
+ i
);
1426 return (ch
== ' ' && is_aligned_on_a_tab (edit
));
1429 /* --------------------------------------------------------------------------------------------- */
1432 edit_auto_indent (WEdit
*edit
)
1436 p
= edit
->buffer
.curs1
;
1437 /* use the previous line as a template */
1438 p
= edit_buffer_get_backward_offset (&edit
->buffer
, p
, 1);
1439 /* copy the leading whitespace of the line */
1441 { /* no range check - the line _is_ \n-terminated */
1444 c
= edit_buffer_get_byte (&edit
->buffer
, p
++);
1445 if (!whitespace (c
))
1447 edit_insert (edit
, c
);
1451 /* --------------------------------------------------------------------------------------------- */
1454 edit_double_newline (WEdit
*edit
)
1456 edit_insert (edit
, '\n');
1457 if (edit_buffer_get_current_byte (&edit
->buffer
) == '\n'
1458 || edit_buffer_get_byte (&edit
->buffer
, edit
->buffer
.curs1
- 2) == '\n')
1460 edit
->force
|= REDRAW_PAGE
;
1461 edit_insert (edit
, '\n');
1464 /* --------------------------------------------------------------------------------------------- */
1467 insert_spaces_tab (WEdit
*edit
, gboolean half
)
1471 edit_update_curs_col (edit
);
1472 i
= TAB_SIZE
* space_width
;
1476 for (i
= ((edit
->curs_col
/ i
) + 1) * i
- edit
->curs_col
; i
> 0; i
-= space_width
)
1477 edit_insert (edit
, ' ');
1480 /* --------------------------------------------------------------------------------------------- */
1483 edit_tab_cmd (WEdit
*edit
)
1485 if (edit_options
.fake_half_tabs
&& is_in_indent (&edit
->buffer
))
1487 /* insert a half tab (usually four spaces) unless there is a
1488 half tab already behind, then delete it and insert a
1490 if (edit_options
.fill_tabs_with_spaces
|| !right_of_four_spaces (edit
))
1491 insert_spaces_tab (edit
, TRUE
);
1496 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1497 edit_backspace (edit
, TRUE
);
1498 edit_insert (edit
, '\t');
1501 else if (edit_options
.fill_tabs_with_spaces
)
1502 insert_spaces_tab (edit
, FALSE
);
1504 edit_insert (edit
, '\t');
1507 /* --------------------------------------------------------------------------------------------- */
1510 check_and_wrap_line (WEdit
*edit
)
1514 if (!edit_options
.typewriter_wrap
)
1516 edit_update_curs_col (edit
);
1517 if (edit
->curs_col
< edit_options
.word_wrap_line_length
)
1519 curs
= edit
->buffer
.curs1
;
1525 c
= edit_buffer_get_byte (&edit
->buffer
, curs
);
1526 if (c
== '\n' || curs
<= 0)
1528 edit_insert (edit
, '\n');
1533 off_t current
= edit
->buffer
.curs1
;
1534 edit_cursor_move (edit
, curs
- edit
->buffer
.curs1
+ 1);
1535 edit_insert (edit
, '\n');
1536 edit_cursor_move (edit
, current
- edit
->buffer
.curs1
+ 1);
1542 /* --------------------------------------------------------------------------------------------- */
1543 /** this find the matching bracket in either direction, and sets edit->bracket
1545 * @param edit editor object
1546 * @param in_screen search only on the current screen
1547 * @param furthest_bracket_search count of the bytes for search
1549 * @return position of the found bracket (-1 if no match)
1553 edit_get_bracket (WEdit
*edit
, gboolean in_screen
, unsigned long furthest_bracket_search
)
1555 const char *const b
= "{}{[][()(", *p
;
1556 int i
= 1, inc
= -1, c
, d
, n
= 0;
1557 unsigned long j
= 0;
1560 edit_update_curs_row (edit
);
1561 c
= edit_buffer_get_current_byte (&edit
->buffer
);
1563 /* not on a bracket at all */
1564 if (p
== NULL
|| *p
== '\0')
1566 /* the matching bracket */
1568 /* going left or right? */
1569 if (strchr ("{[(", c
) != NULL
)
1572 if (furthest_bracket_search
== 0)
1573 furthest_bracket_search
--; /* ULONG_MAX */
1574 for (q
= edit
->buffer
.curs1
+ inc
;; q
+= inc
)
1578 /* out of buffer? */
1579 if (q
>= edit
->buffer
.size
|| q
< 0)
1581 a
= edit_buffer_get_byte (&edit
->buffer
, q
);
1582 /* don't want to eat CPU */
1583 if (j
++ > furthest_bracket_search
)
1585 /* out of screen? */
1588 if (q
< edit
->start_display
)
1590 /* count lines if searching downward */
1591 if (inc
> 0 && a
== '\n')
1592 if (n
++ >= WIDGET (edit
)->rect
.lines
- edit
->curs_row
) /* out of screen */
1595 /* count bracket depth */
1596 i
+= (a
== c
) - (a
== d
);
1597 /* return if bracket depth is zero */
1605 /* --------------------------------------------------------------------------------------------- */
1608 edit_goto_matching_bracket (WEdit
*edit
)
1612 q
= edit_get_bracket (edit
, 0, 0);
1615 edit
->bracket
= edit
->buffer
.curs1
;
1616 edit
->force
|= REDRAW_PAGE
;
1617 edit_cursor_move (edit
, q
- edit
->buffer
.curs1
);
1621 /* --------------------------------------------------------------------------------------------- */
1624 edit_move_block_to_right (WEdit
*edit
)
1626 off_t start_mark
, end_mark
;
1627 long cur_bol
, start_bol
;
1629 if (!eval_marks (edit
, &start_mark
, &end_mark
))
1632 start_bol
= edit_buffer_get_bol (&edit
->buffer
, start_mark
);
1633 cur_bol
= edit_buffer_get_bol (&edit
->buffer
, end_mark
- 1);
1639 edit_cursor_move (edit
, cur_bol
- edit
->buffer
.curs1
);
1640 if (!edit_line_is_blank (edit
, edit
->buffer
.curs_line
))
1642 if (edit_options
.fill_tabs_with_spaces
)
1643 insert_spaces_tab (edit
, edit_options
.fake_half_tabs
);
1645 edit_insert (edit
, '\t');
1647 b
= edit_buffer_get_bol (&edit
->buffer
, cur_bol
);
1648 edit_cursor_move (edit
, b
- edit
->buffer
.curs1
);
1654 cur_bol
= edit_buffer_get_bol (&edit
->buffer
, cur_bol
- 1);
1656 while (cur_bol
>= start_bol
);
1658 edit
->force
|= REDRAW_PAGE
;
1661 /* --------------------------------------------------------------------------------------------- */
1664 edit_move_block_to_left (WEdit
*edit
)
1666 off_t start_mark
, end_mark
;
1667 off_t cur_bol
, start_bol
;
1669 if (!eval_marks (edit
, &start_mark
, &end_mark
))
1672 start_bol
= edit_buffer_get_bol (&edit
->buffer
, start_mark
);
1673 cur_bol
= edit_buffer_get_bol (&edit
->buffer
, end_mark
- 1);
1680 edit_cursor_move (edit
, cur_bol
- edit
->buffer
.curs1
);
1682 del_tab_width
= edit_options
.fake_half_tabs
? HALF_TAB_SIZE
: TAB_SIZE
;
1684 next_char
= edit_buffer_get_current_byte (&edit
->buffer
);
1685 if (next_char
== '\t')
1686 edit_delete (edit
, TRUE
);
1687 else if (next_char
== ' ')
1691 for (i
= 0; i
< del_tab_width
; i
++)
1693 if (next_char
== ' ')
1694 edit_delete (edit
, TRUE
);
1695 next_char
= edit_buffer_get_current_byte (&edit
->buffer
);
1702 cur_bol
= edit_buffer_get_bol (&edit
->buffer
, cur_bol
- 1);
1704 while (cur_bol
>= start_bol
);
1706 edit
->force
|= REDRAW_PAGE
;
1709 /* --------------------------------------------------------------------------------------------- */
1711 * prints at the cursor
1712 * @return number of chars printed
1716 edit_print_string (WEdit
*e
, const char *s
)
1720 for (i
= 0; s
[i
] != '\0'; i
++)
1721 edit_execute_cmd (e
, CK_InsertChar
, (unsigned char) s
[i
]);
1722 e
->force
|= REDRAW_COMPLETELY
;
1723 edit_update_screen (e
);
1727 /* --------------------------------------------------------------------------------------------- */
1730 edit_insert_column_from_file (WEdit
*edit
, int file
, off_t
*start_pos
, off_t
*end_pos
,
1731 long *col1
, long *col2
)
1735 off_t blocklen
= -1, width
= 0;
1736 unsigned char *data
;
1738 cursor
= edit
->buffer
.curs1
;
1739 col
= edit_get_col (edit
);
1740 data
= g_malloc0 (TEMP_BUF_LEN
);
1742 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0)
1747 pn
= strchr ((char *) data
, '\n');
1748 width
= pn
== NULL
? blocklen
: pn
- (char *) data
;
1750 for (i
= 0; i
< blocklen
; i
++)
1752 if (data
[i
] != '\n')
1753 edit_insert (edit
, data
[i
]);
1755 { /* fill in and move to next line */
1759 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
1760 for (l
= width
- (edit_get_col (edit
) - col
); l
> 0; l
-= space_width
)
1761 edit_insert (edit
, ' ');
1763 for (p
= edit
->buffer
.curs1
;; p
++)
1765 if (p
== edit
->buffer
.size
)
1767 edit_cursor_move (edit
, edit
->buffer
.size
- edit
->buffer
.curs1
);
1768 edit_insert_ahead (edit
, '\n');
1772 if (edit_buffer_get_byte (&edit
->buffer
, p
) == '\n')
1779 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->buffer
.curs1
);
1781 for (l
= col
- edit_get_col (edit
); l
>= space_width
; l
-= space_width
)
1782 edit_insert (edit
, ' ');
1787 *col2
= col
+ width
;
1788 *start_pos
= cursor
;
1789 *end_pos
= edit
->buffer
.curs1
;
1790 edit_cursor_move (edit
, cursor
- edit
->buffer
.curs1
);
1796 /* --------------------------------------------------------------------------------------------- */
1797 /*** public functions ****************************************************************************/
1798 /* --------------------------------------------------------------------------------------------- */
1800 /** User edit menu, like user menu (F2) but only in editor. */
1803 edit_user_menu (WEdit
*edit
, const char *menu_file
, int selected_entry
)
1808 off_t start_mark
, end_mark
;
1810 vfs_path_t
*block_file_vpath
;
1812 block_file
= mc_config_get_full_path (EDIT_HOME_BLOCK_FILE
);
1813 block_file_vpath
= vfs_path_from_str (block_file
);
1814 curs
= edit
->buffer
.curs1
;
1815 mark
= eval_marks (edit
, &start_mark
, &end_mark
);
1817 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1819 /* run shell scripts from menu */
1820 if (user_menu_cmd (CONST_WIDGET (edit
), menu_file
, selected_entry
)
1821 && (mc_stat (block_file_vpath
, &status
) == 0) && (status
.st_size
!= 0))
1826 /* i.e. we have marked block */
1828 rc
= edit_block_delete_cmd (edit
);
1834 ins_len
= edit_insert_file (edit
, block_file_vpath
);
1835 if (mark
&& ins_len
> 0)
1836 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1838 /* truncate block file */
1839 fd
= fopen (block_file
, "w");
1843 g_free (block_file
);
1844 vfs_path_free (block_file_vpath
, TRUE
);
1846 edit_cursor_move (edit
, curs
- edit
->buffer
.curs1
);
1847 edit
->force
|= REDRAW_PAGE
;
1848 widget_draw (WIDGET (edit
));
1851 /* --------------------------------------------------------------------------------------------- */
1854 edit_get_write_filter (const vfs_path_t
*write_name_vpath
, const vfs_path_t
*filename_vpath
)
1857 const char *write_name
;
1858 char *write_name_quoted
;
1861 i
= edit_find_filter (filename_vpath
);
1865 write_name
= vfs_path_get_last_path_str (write_name_vpath
);
1866 write_name_quoted
= name_quote (write_name
, FALSE
);
1867 if (write_name_quoted
!= NULL
)
1869 p
= g_strdup_printf (all_filters
[i
].write
, write_name_quoted
);
1870 g_free (write_name_quoted
);
1875 /* --------------------------------------------------------------------------------------------- */
1877 * @param edit editor object
1878 * @param f value of stream file
1879 * @return the length of the file
1883 edit_write_stream (WEdit
*edit
, FILE *f
)
1887 if (edit
->lb
== LB_ASIS
)
1889 for (i
= 0; i
< edit
->buffer
.size
; i
++)
1890 if (fputc (edit_buffer_get_byte (&edit
->buffer
, i
), f
) < 0)
1895 /* change line breaks */
1896 for (i
= 0; i
< edit
->buffer
.size
; i
++)
1900 c
= edit_buffer_get_byte (&edit
->buffer
, i
);
1901 if (!(c
== '\n' || c
== '\r'))
1903 /* not line break */
1904 if (fputc (c
, f
) < 0)
1908 { /* (c == '\n' || c == '\r') */
1911 c1
= edit_buffer_get_byte (&edit
->buffer
, i
+ 1); /* next char */
1915 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
1916 /* put one line break unconditionally */
1917 if (fputc ('\n', f
) < 0)
1920 i
++; /* 2 chars are processed */
1922 if (c
== '\r' && c1
== '\n')
1923 /* Windows line break; go to the next char */
1926 if (c
== '\r' && c1
== '\r')
1928 /* two Macintosh line breaks; put second line break */
1929 if (fputc ('\n', f
) < 0)
1934 if (fputc (c1
, f
) < 0)
1938 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
1939 /* put one line break unconditionally */
1940 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
1943 if (c
== '\r' && c1
== '\n')
1944 /* Windows line break; go to the next char */
1948 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
1949 /* put one line break unconditionally */
1950 if (fputc ('\r', f
) < 0)
1953 i
++; /* 2 chars are processed */
1955 if (c
== '\r' && c1
== '\n')
1956 /* Windows line break; go to the next char */
1959 if (c
== '\n' && c1
== '\n')
1961 /* two Windows line breaks; put second line break */
1962 if (fputc ('\r', f
) < 0)
1967 if (fputc (c1
, f
) < 0)
1970 case LB_ASIS
: /* default without changes */
1977 return edit
->buffer
.size
;
1980 /* --------------------------------------------------------------------------------------------- */
1983 is_break_char (char c
)
1985 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
) != NULL
);
1988 /* --------------------------------------------------------------------------------------------- */
1989 /** inserts a file at the cursor, returns count of inserted bytes on success */
1992 edit_insert_file (WEdit
*edit
, const vfs_path_t
*filename_vpath
)
1998 p
= edit_get_filter (filename_vpath
);
1999 current
= edit
->buffer
.curs1
;
2005 f
= (FILE *) popen (p
, "r");
2008 edit_insert_stream (edit
, f
);
2010 /* Place cursor at the end of text selection */
2011 if (!edit_options
.cursor_after_inserted_block
)
2013 ins_len
= edit
->buffer
.curs1
- current
;
2014 edit_cursor_move (edit
, -ins_len
);
2020 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
2021 edit_error_dialog (_("Error"), errmsg
);
2030 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
2031 edit_error_dialog (_("Error"), errmsg
);
2041 gboolean vertical_insertion
= FALSE
;
2044 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
2048 buf
= g_malloc0 (TEMP_BUF_LEN
);
2049 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2052 /* if contain signature VERTICAL_MAGIC then it vertical block */
2053 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2054 vertical_insertion
= TRUE
;
2056 mc_lseek (file
, 0, SEEK_SET
);
2059 if (vertical_insertion
)
2064 blocklen
= edit_insert_column_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
2065 edit_set_markers (edit
, edit
->buffer
.curs1
, mark2
, c1
, c2
);
2067 /* highlight inserted text then not persistent blocks */
2068 if (!edit_options
.persistent_selections
&& edit
->modified
)
2070 if (!edit
->column_highlight
)
2071 edit_push_undo_action (edit
, COLUMN_OFF
);
2072 edit
->column_highlight
= 1;
2079 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2081 for (i
= 0; i
< blocklen
; i
++)
2082 edit_insert (edit
, buf
[i
]);
2084 /* highlight inserted text then not persistent blocks */
2085 if (!edit_options
.persistent_selections
&& edit
->modified
)
2087 edit_set_markers (edit
, edit
->buffer
.curs1
, current
, 0, 0);
2088 if (edit
->column_highlight
)
2089 edit_push_undo_action (edit
, COLUMN_ON
);
2090 edit
->column_highlight
= 0;
2093 /* Place cursor at the end of text selection */
2094 if (!edit_options
.cursor_after_inserted_block
)
2096 ins_len
= edit
->buffer
.curs1
- current
;
2097 edit_cursor_move (edit
, -ins_len
);
2101 edit
->force
|= REDRAW_PAGE
;
2111 /* --------------------------------------------------------------------------------------------- */
2113 * Fill in the edit structure. Return NULL on failure. Pass edit as
2114 * NULL to allocate a new structure.
2116 * If arg is NULL or arg->line_number is 0, try to restore saved position. Otherwise put the
2117 * cursor on that line and show it in the middle of the screen.
2121 edit_init (WEdit
*edit
, const WRect
*r
, const edit_arg_t
*arg
)
2123 gboolean to_free
= FALSE
;
2126 auto_syntax
= TRUE
; /* Resetting to auto on every invocation */
2127 edit_options
.line_state_width
= edit_options
.line_state
? LINE_STATE_WIDTH
: 0;
2131 gboolean fullscreen
;
2134 /* save some widget parameters */
2135 fullscreen
= edit
->fullscreen
;
2136 loc_prev
= edit
->loc_prev
;
2138 edit_purge_widget (edit
);
2140 /* restore saved parameters */
2141 edit
->fullscreen
= fullscreen
;
2142 edit
->loc_prev
= loc_prev
;
2148 edit
= g_malloc0 (sizeof (WEdit
));
2152 widget_init (w
, r
, NULL
, NULL
);
2153 w
->options
|= WOP_SELECTABLE
| WOP_TOP_SELECT
| WOP_WANT_CURSOR
;
2154 w
->keymap
= editor_map
;
2155 w
->ext_keymap
= editor_x_map
;
2156 edit
->fullscreen
= TRUE
;
2157 edit_save_size (edit
);
2160 edit
->drag_state
= MCEDIT_DRAG_NONE
;
2162 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2163 edit
->stat1
.st_uid
= getuid ();
2164 edit
->stat1
.st_gid
= getgid ();
2165 edit
->stat1
.st_mtime
= 0;
2168 edit
->attrs_ok
= (mc_fgetflags (arg
->file_vpath
, &edit
->attrs
) == 0);
2170 edit
->attrs_ok
= FALSE
;
2174 edit
->last_bracket
= -1;
2175 edit
->force
|= REDRAW_PAGE
;
2177 /* set file name before load file */
2180 edit_set_filename (edit
, arg
->file_vpath
);
2181 line
= arg
->line_number
;
2185 edit_set_filename (edit
, NULL
);
2189 edit
->undo_stack_size
= START_STACK_SIZE
;
2190 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2191 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2193 edit
->redo_stack_size
= START_STACK_SIZE
;
2194 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2195 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2199 edit
->converter
= str_cnv_from_term
;
2200 edit_set_codeset (edit
);
2203 if (!edit_load_file (edit
))
2205 /* edit_load_file already gives an error message */
2211 edit
->loading_done
= 1;
2214 edit_load_syntax (edit
, NULL
, NULL
);
2215 edit_get_syntax_color (edit
, -1);
2217 /* load saved cursor position and/or boolmarks */
2218 if ((line
== 0) && edit_options
.save_position
)
2219 edit_load_position (edit
, TRUE
);
2222 edit_load_position (edit
, FALSE
);
2225 edit_move_display (edit
, line
- 1);
2226 edit_move_to_line (edit
, line
- 1);
2229 edit_load_macro_cmd (edit
);
2234 /* --------------------------------------------------------------------------------------------- */
2236 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2238 edit_clean (WEdit
*edit
)
2243 /* a stale lock, remove it */
2245 (void) unlock_file (edit
->filename_vpath
);
2247 /* save cursor position */
2248 if (edit_options
.save_position
)
2249 edit_save_position (edit
);
2250 else if (edit
->serialized_bookmarks
!= NULL
)
2251 g_array_free (edit
->serialized_bookmarks
, TRUE
);
2253 /* File specified on the mcedit command line and never saved */
2254 if (edit
->delete_file
)
2255 unlink (vfs_path_get_last_path_str (edit
->filename_vpath
));
2257 edit_free_syntax_rules (edit
);
2258 book_mark_flush (edit
, -1);
2260 edit_buffer_clean (&edit
->buffer
);
2262 g_free (edit
->undo_stack
);
2263 g_free (edit
->redo_stack
);
2264 vfs_path_free (edit
->filename_vpath
, TRUE
);
2265 vfs_path_free (edit
->dir_vpath
, TRUE
);
2266 edit_search_deinit (edit
);
2269 if (edit
->converter
!= str_cnv_from_term
)
2270 str_close_conv (edit
->converter
);
2273 edit_purge_widget (edit
);
2278 /* --------------------------------------------------------------------------------------------- */
2281 * Load a new file into the editor and set line. If it fails, preserve the old file.
2282 * To do it, allocate a new widget, initialize it and, if the new file
2283 * was loaded, copy the data to the old widget.
2285 * @return TRUE on success, FALSE on failure.
2288 edit_reload_line (WEdit
*edit
, const edit_arg_t
*arg
)
2290 Widget
*w
= WIDGET (edit
);
2293 e
= g_malloc0 (sizeof (WEdit
));
2295 /* save some widget parameters */
2296 e
->fullscreen
= edit
->fullscreen
;
2297 e
->loc_prev
= edit
->loc_prev
;
2299 if (edit_init (e
, &w
->rect
, arg
) == NULL
)
2306 memcpy (edit
, e
, sizeof (*edit
));
2312 /* --------------------------------------------------------------------------------------------- */
2316 edit_set_codeset (WEdit
*edit
)
2321 get_codepage_id (mc_global
.source_codepage
>=
2322 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2327 conv
= str_crt_conv_from (cp_id
);
2328 if (conv
!= INVALID_CONV
)
2330 if (edit
->converter
!= str_cnv_from_term
)
2331 str_close_conv (edit
->converter
);
2332 edit
->converter
= conv
;
2337 edit
->utf8
= str_isutf8 (cp_id
);
2341 /* --------------------------------------------------------------------------------------------- */
2344 * Recording stack for undo:
2345 * The following is an implementation of a compressed stack. Identical
2346 * pushes are recorded by a negative prefix indicating the number of times the
2347 * same char was pushed. This saves space for repeated curs-left or curs-right
2364 * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2365 * 256-512 is an insert ahead (from a delete), If it is between 600 and 700 it is one
2366 * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2367 * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2370 * The only way the cursor moves or the buffer is changed is through the routines:
2371 * insert, backspace, insert_ahead, delete, and cursor_move.
2372 * These record the reverse undo movements onto the stack each time they are
2375 * Each key press results in a set of actions (insert; delete ...). So each time
2376 * a key is pressed the current position of start_display is pushed as
2377 * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2378 * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2379 * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2383 * @param edit editor object
2384 * @param c code of the action
2388 edit_push_undo_action (WEdit
*edit
, long c
)
2390 unsigned long sp
= edit
->undo_stack_pointer
;
2393 /* first enlarge the stack if necessary */
2394 if (sp
> edit
->undo_stack_size
- 10)
2398 if (edit
->undo_stack_size
< (unsigned long) max_undo
)
2402 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2405 edit
->undo_stack
= t
;
2406 edit
->undo_stack_size
<<= 1;
2407 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2411 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2412 if (edit
->undo_stack_disable
)
2414 edit_push_redo_action (edit
, KEY_PRESS
);
2415 edit_push_redo_action (edit
, c
);
2419 if (edit
->redo_stack_reset
)
2420 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2422 if (edit
->undo_stack_bottom
!= sp
2423 && spm1
!= edit
->undo_stack_bottom
2424 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2428 if (edit
->undo_stack
[spm1
] < 0)
2430 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2431 if (d
== c
&& edit
->undo_stack
[spm1
] > -1000000000)
2433 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2434 edit
->undo_stack
[spm1
]--;
2440 d
= edit
->undo_stack
[spm1
];
2444 return; /* --> no need to push multiple do-nothings */
2445 edit
->undo_stack
[sp
] = -2;
2450 edit
->undo_stack
[sp
] = c
;
2453 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2455 /* if the sp wraps round and catches the undo_stack_bottom then erase
2456 * the first set of actions on the stack to make space - by moving
2457 * undo_stack_bottom forward one "key press" */
2458 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2459 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2460 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2463 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2465 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2466 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2468 /*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: */
2469 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2470 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2472 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2476 /* --------------------------------------------------------------------------------------------- */
2479 edit_push_redo_action (WEdit
*edit
, long c
)
2481 unsigned long sp
= edit
->redo_stack_pointer
;
2483 /* first enlarge the stack if necessary */
2484 if (sp
> edit
->redo_stack_size
- 10)
2488 if (edit
->redo_stack_size
< (unsigned long) max_undo
)
2492 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2495 edit
->redo_stack
= t
;
2496 edit
->redo_stack_size
<<= 1;
2497 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2501 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2503 if (edit
->redo_stack_bottom
!= sp
2504 && spm1
!= edit
->redo_stack_bottom
2505 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2509 if (edit
->redo_stack
[spm1
] < 0)
2511 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2512 if (d
== c
&& edit
->redo_stack
[spm1
] > -1000000000)
2514 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2515 edit
->redo_stack
[spm1
]--;
2521 d
= edit
->redo_stack
[spm1
];
2525 return; /* --> no need to push multiple do-nothings */
2526 edit
->redo_stack
[sp
] = -2;
2527 goto redo_check_bottom
;
2531 edit
->redo_stack
[sp
] = c
;
2534 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2536 /* if the sp wraps round and catches the redo_stack_bottom then erase
2537 * the first set of actions on the stack to make space - by moving
2538 * redo_stack_bottom forward one "key press" */
2539 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2540 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2541 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2544 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2546 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2547 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2550 * If a single key produced enough pushes to wrap all the way round then
2551 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2552 * The stack is then initialised:
2555 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2556 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2557 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2560 /* --------------------------------------------------------------------------------------------- */
2562 Basic low level single character buffer alterations and movements at the cursor.
2566 edit_insert (WEdit
*edit
, int c
)
2568 /* first we must update the position of the display window */
2569 if (edit
->buffer
.curs1
< edit
->start_display
)
2571 edit
->start_display
++;
2576 /* Mark file as modified, unless the file hasn't been fully loaded */
2577 if (edit
->loading_done
)
2578 edit_modification (edit
);
2580 /* now we must update some info on the file and check if a redraw is required */
2583 book_mark_inc (edit
, edit
->buffer
.curs_line
);
2584 edit
->buffer
.curs_line
++;
2585 edit
->buffer
.lines
++;
2586 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2589 /* save the reverse command onto the undo stack */
2590 /* ordinary char and not space */
2592 edit_push_undo_action (edit
, BACKSPACE
);
2594 edit_push_undo_action (edit
, BACKSPACE_BR
);
2595 /* update markers */
2596 edit
->mark1
+= (edit
->mark1
> edit
->buffer
.curs1
) ? 1 : 0;
2597 edit
->mark2
+= (edit
->mark2
> edit
->buffer
.curs1
) ? 1 : 0;
2598 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->buffer
.curs1
) ? 1 : 0;
2600 edit_buffer_insert (&edit
->buffer
, c
);
2603 /* --------------------------------------------------------------------------------------------- */
2604 /** same as edit_insert and move left */
2607 edit_insert_ahead (WEdit
*edit
, int c
)
2609 if (edit
->buffer
.curs1
< edit
->start_display
)
2611 edit
->start_display
++;
2615 edit_modification (edit
);
2618 book_mark_inc (edit
, edit
->buffer
.curs_line
);
2619 edit
->buffer
.lines
++;
2620 edit
->force
|= REDRAW_AFTER_CURSOR
;
2622 /* ordinary char and not space */
2624 edit_push_undo_action (edit
, DELCHAR
);
2626 edit_push_undo_action (edit
, DELCHAR_BR
);
2628 edit
->mark1
+= (edit
->mark1
>= edit
->buffer
.curs1
) ? 1 : 0;
2629 edit
->mark2
+= (edit
->mark2
>= edit
->buffer
.curs1
) ? 1 : 0;
2630 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->buffer
.curs1
) ? 1 : 0;
2632 edit_buffer_insert_ahead (&edit
->buffer
, c
);
2635 /* --------------------------------------------------------------------------------------------- */
2638 edit_insert_over (WEdit
*edit
)
2642 for (i
= 0; i
< edit
->over_col
; i
++)
2643 edit_insert (edit
, ' ');
2648 /* --------------------------------------------------------------------------------------------- */
2651 edit_delete (WEdit
*edit
, gboolean byte_delete
)
2654 int char_length
= 1;
2657 if (edit
->buffer
.curs2
== 0)
2661 /* if byte_delete == TRUE then delete only one byte not multibyte char */
2662 if (edit
->utf8
&& !byte_delete
)
2664 edit_buffer_get_utf (&edit
->buffer
, edit
->buffer
.curs1
, &char_length
);
2665 if (char_length
< 1)
2672 if (edit
->mark2
!= edit
->mark1
)
2673 edit_push_markers (edit
);
2675 for (i
= 1; i
<= char_length
; i
++)
2677 if (edit
->mark1
> edit
->buffer
.curs1
)
2680 edit
->end_mark_curs
--;
2682 if (edit
->mark2
> edit
->buffer
.curs1
)
2684 if (edit
->last_get_rule
> edit
->buffer
.curs1
)
2685 edit
->last_get_rule
--;
2687 p
= edit_buffer_delete (&edit
->buffer
);
2689 edit_push_undo_action (edit
, p
+ 256);
2692 edit_modification (edit
);
2695 book_mark_dec (edit
, edit
->buffer
.curs_line
);
2696 edit
->buffer
.lines
--;
2697 edit
->force
|= REDRAW_AFTER_CURSOR
;
2699 if (edit
->buffer
.curs1
< edit
->start_display
)
2701 edit
->start_display
--;
2709 /* --------------------------------------------------------------------------------------------- */
2712 edit_backspace (WEdit
*edit
, gboolean byte_delete
)
2715 int char_length
= 1;
2718 if (edit
->buffer
.curs1
== 0)
2721 if (edit
->mark2
!= edit
->mark1
)
2722 edit_push_markers (edit
);
2725 if (edit
->utf8
&& !byte_delete
)
2727 edit_buffer_get_prev_utf (&edit
->buffer
, edit
->buffer
.curs1
, &char_length
);
2728 if (char_length
< 1)
2735 for (i
= 1; i
<= char_length
; i
++)
2737 if (edit
->mark1
>= edit
->buffer
.curs1
)
2740 edit
->end_mark_curs
--;
2742 if (edit
->mark2
>= edit
->buffer
.curs1
)
2744 if (edit
->last_get_rule
>= edit
->buffer
.curs1
)
2745 edit
->last_get_rule
--;
2747 p
= edit_buffer_backspace (&edit
->buffer
);
2749 edit_push_undo_action (edit
, p
);
2751 edit_modification (edit
);
2754 book_mark_dec (edit
, edit
->buffer
.curs_line
);
2755 edit
->buffer
.curs_line
--;
2756 edit
->buffer
.lines
--;
2757 edit
->force
|= REDRAW_AFTER_CURSOR
;
2760 if (edit
->buffer
.curs1
< edit
->start_display
)
2762 edit
->start_display
--;
2770 /* --------------------------------------------------------------------------------------------- */
2771 /** moves the cursor right or left: increment positive or negative respectively */
2774 edit_cursor_move (WEdit
*edit
, off_t increment
)
2778 for (; increment
< 0 && edit
->buffer
.curs1
!= 0; increment
++)
2782 edit_push_undo_action (edit
, CURS_RIGHT
);
2784 c
= edit_buffer_get_previous_byte (&edit
->buffer
);
2785 edit_buffer_insert_ahead (&edit
->buffer
, c
);
2786 c
= edit_buffer_backspace (&edit
->buffer
);
2789 edit
->buffer
.curs_line
--;
2790 edit
->force
|= REDRAW_LINE_BELOW
;
2796 for (; increment
> 0 && edit
->buffer
.curs2
!= 0; increment
--)
2800 edit_push_undo_action (edit
, CURS_LEFT
);
2802 c
= edit_buffer_get_current_byte (&edit
->buffer
);
2803 edit_buffer_insert (&edit
->buffer
, c
);
2804 c
= edit_buffer_delete (&edit
->buffer
);
2807 edit
->buffer
.curs_line
++;
2808 edit
->force
|= REDRAW_LINE_ABOVE
;
2814 /* --------------------------------------------------------------------------------------------- */
2815 /* If cols is zero this returns the count of columns from current to upto. */
2816 /* If upto is zero returns index of cols across from current. */
2819 edit_move_forward3 (const WEdit
*edit
, off_t current
, long cols
, off_t upto
)
2830 q
= edit
->buffer
.size
+ 2;
2832 for (col
= 0, p
= current
; p
< q
; p
++)
2844 orig_c
= c
= edit_buffer_get_byte (&edit
->buffer
, p
);
2850 int char_length
= 1;
2852 utf_ch
= edit_buffer_get_utf (&edit
->buffer
, p
, &char_length
);
2853 if (mc_global
.utf8_display
)
2855 if (char_length
> 1)
2856 col
-= char_length
- 1;
2857 if (g_unichar_iswide (utf_ch
))
2860 else if (char_length
> 1 && g_unichar_isprint (utf_ch
))
2861 col
-= char_length
- 1;
2864 c
= convert_to_display_c (c
);
2868 return (upto
!= 0 ? (off_t
) col
: p
);
2870 col
+= TAB_SIZE
- col
% TAB_SIZE
;
2871 else if ((c
< 32 || c
== 127) && (orig_c
== c
2873 || (!mc_global
.utf8_display
&& !edit
->utf8
)
2876 /* '\r' is shown as ^M, so we must advance 2 characters */
2877 /* Caret notation for control characters */
2885 /* --------------------------------------------------------------------------------------------- */
2886 /** returns the current offset of the cursor from the beginning of a file */
2889 edit_get_cursor_offset (const WEdit
*edit
)
2891 return edit
->buffer
.curs1
;
2894 /* --------------------------------------------------------------------------------------------- */
2895 /** returns the current column position of the cursor */
2898 edit_get_col (const WEdit
*edit
)
2902 b
= edit_buffer_get_current_bol (&edit
->buffer
);
2903 return (long) edit_move_forward3 (edit
, b
, 0, edit
->buffer
.curs1
);
2906 /* --------------------------------------------------------------------------------------------- */
2907 /* Scrolling functions */
2908 /* --------------------------------------------------------------------------------------------- */
2911 edit_update_curs_row (WEdit
*edit
)
2913 edit
->curs_row
= edit
->buffer
.curs_line
- edit
->start_line
;
2916 /* --------------------------------------------------------------------------------------------- */
2919 edit_update_curs_col (WEdit
*edit
)
2923 b
= edit_buffer_get_current_bol (&edit
->buffer
);
2924 edit
->curs_col
= (long) edit_move_forward3 (edit
, b
, 0, edit
->buffer
.curs1
);
2927 /* --------------------------------------------------------------------------------------------- */
2930 edit_get_curs_col (const WEdit
*edit
)
2932 return edit
->curs_col
;
2935 /* --------------------------------------------------------------------------------------------- */
2936 /** moves the display start position up by i lines */
2939 edit_scroll_upward (WEdit
*edit
, long i
)
2941 long lines_above
= edit
->start_line
;
2943 if (i
> lines_above
)
2947 edit
->start_line
-= i
;
2948 edit
->start_display
=
2949 edit_buffer_get_backward_offset (&edit
->buffer
, edit
->start_display
, i
);
2950 edit
->force
|= REDRAW_PAGE
;
2951 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2953 edit_update_curs_row (edit
);
2956 /* --------------------------------------------------------------------------------------------- */
2959 edit_scroll_downward (WEdit
*edit
, long i
)
2963 lines_below
= edit
->buffer
.lines
- edit
->start_line
- (WIDGET (edit
)->rect
.lines
- 1);
2964 if (lines_below
> 0)
2966 if (i
> lines_below
)
2968 edit
->start_line
+= i
;
2969 edit
->start_display
=
2970 edit_buffer_get_forward_offset (&edit
->buffer
, edit
->start_display
, i
, 0);
2971 edit
->force
|= REDRAW_PAGE
;
2972 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2974 edit_update_curs_row (edit
);
2977 /* --------------------------------------------------------------------------------------------- */
2980 edit_scroll_right (WEdit
*edit
, long i
)
2982 edit
->force
|= REDRAW_PAGE
;
2983 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2984 edit
->start_col
-= i
;
2987 /* --------------------------------------------------------------------------------------------- */
2990 edit_scroll_left (WEdit
*edit
, long i
)
2992 if (edit
->start_col
)
2994 edit
->start_col
+= i
;
2995 if (edit
->start_col
> 0)
2996 edit
->start_col
= 0;
2997 edit
->force
|= REDRAW_PAGE
;
2998 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3002 /* --------------------------------------------------------------------------------------------- */
3003 /* high level cursor movement commands */
3004 /* --------------------------------------------------------------------------------------------- */
3007 edit_move_to_prev_col (WEdit
*edit
, off_t p
)
3009 long prev
= edit
->prev_col
;
3010 long over
= edit
->over_col
;
3013 edit_cursor_move (edit
,
3014 edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->buffer
.curs1
);
3016 if (edit_options
.cursor_beyond_eol
)
3021 b
= edit_buffer_get_current_bol (&edit
->buffer
);
3022 e
= edit_buffer_get_current_eol (&edit
->buffer
);
3023 line_len
= (long) edit_move_forward3 (edit
, b
, 0, e
);
3024 if (line_len
< prev
+ edit
->over_col
)
3026 edit
->over_col
= prev
+ over
- line_len
;
3027 edit
->prev_col
= line_len
;
3028 edit
->curs_col
= line_len
;
3032 edit
->curs_col
= prev
+ over
;
3033 edit
->prev_col
= edit
->curs_col
;
3040 if (edit_options
.fake_half_tabs
&& is_in_indent (&edit
->buffer
))
3042 long fake_half_tabs
;
3044 edit_update_curs_col (edit
);
3046 fake_half_tabs
= HALF_TAB_SIZE
* space_width
;
3047 if (fake_half_tabs
!= 0 && edit
->curs_col
% fake_half_tabs
!= 0)
3052 edit
->curs_col
-= (edit
->curs_col
% fake_half_tabs
);
3053 p
= edit_buffer_get_current_bol (&edit
->buffer
);
3054 b
= edit_move_forward3 (edit
, p
, edit
->curs_col
, 0);
3055 edit_cursor_move (edit
, b
- edit
->buffer
.curs1
);
3056 if (!left_of_four_spaces (edit
))
3058 b
= edit_move_forward3 (edit
, p
, q
, 0);
3059 edit_cursor_move (edit
, b
- edit
->buffer
.curs1
);
3066 /* --------------------------------------------------------------------------------------------- */
3067 /** check whether line in editor is blank or not
3069 * @param edit editor object
3070 * @param line number of line
3072 * @return TRUE if line in blank, FALSE otherwise
3076 edit_line_is_blank (WEdit
*edit
, long line
)
3078 return is_blank (&edit
->buffer
, edit_find_line (edit
, line
));
3081 /* --------------------------------------------------------------------------------------------- */
3082 /** move cursor to line 'line' */
3085 edit_move_to_line (WEdit
*e
, long line
)
3087 if (line
< e
->buffer
.curs_line
)
3088 edit_move_up (e
, e
->buffer
.curs_line
- line
, FALSE
);
3090 edit_move_down (e
, line
- e
->buffer
.curs_line
, FALSE
);
3091 edit_scroll_screen_over_cursor (e
);
3094 /* --------------------------------------------------------------------------------------------- */
3095 /** scroll window so that first visible line is 'line' */
3098 edit_move_display (WEdit
*e
, long line
)
3100 if (line
< e
->start_line
)
3101 edit_scroll_upward (e
, e
->start_line
- line
);
3103 edit_scroll_downward (e
, line
- e
->start_line
);
3106 /* --------------------------------------------------------------------------------------------- */
3107 /** save markers onto undo stack */
3110 edit_push_markers (WEdit
*edit
)
3112 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3113 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3114 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3117 /* --------------------------------------------------------------------------------------------- */
3120 edit_set_markers (WEdit
*edit
, off_t m1
, off_t m2
, long c1
, long c2
)
3128 /* --------------------------------------------------------------------------------------------- */
3130 if mark2 is -1 then marking is from mark1 to the cursor.
3131 Otherwise its between the markers. This handles this.
3132 Returns FALSE if no text is marked.
3136 eval_marks (WEdit
*edit
, off_t
*start_mark
, off_t
*end_mark
)
3140 if (edit
->mark1
== edit
->mark2
)
3142 *start_mark
= *end_mark
= 0;
3143 edit
->column2
= edit
->column1
= 0;
3147 if (edit
->end_mark_curs
< 0)
3148 end_mark_curs
= edit
->buffer
.curs1
;
3150 end_mark_curs
= edit
->end_mark_curs
;
3152 if (edit
->mark2
>= 0)
3154 *start_mark
= MIN (edit
->mark1
, edit
->mark2
);
3155 *end_mark
= MAX (edit
->mark1
, edit
->mark2
);
3159 *start_mark
= MIN (edit
->mark1
, end_mark_curs
);
3160 *end_mark
= MAX (edit
->mark1
, end_mark_curs
);
3161 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
3164 if (edit
->column_highlight
3165 && ((edit
->mark1
> end_mark_curs
&& edit
->column1
< edit
->column2
)
3166 || (edit
->mark1
< end_mark_curs
&& edit
->column1
> edit
->column2
)))
3168 off_t start_bol
, start_eol
;
3169 off_t end_bol
, end_eol
;
3173 start_bol
= edit_buffer_get_bol (&edit
->buffer
, *start_mark
);
3174 start_eol
= edit_buffer_get_eol (&edit
->buffer
, start_bol
- 1) + 1;
3175 end_bol
= edit_buffer_get_bol (&edit
->buffer
, *end_mark
);
3176 end_eol
= edit_buffer_get_eol (&edit
->buffer
, *end_mark
);
3177 col1
= MIN (edit
->column1
, edit
->column2
);
3178 col2
= MAX (edit
->column1
, edit
->column2
);
3180 diff1
= edit_move_forward3 (edit
, start_bol
, col2
, 0) -
3181 edit_move_forward3 (edit
, start_bol
, col1
, 0);
3182 diff2
= edit_move_forward3 (edit
, end_bol
, col2
, 0) -
3183 edit_move_forward3 (edit
, end_bol
, col1
, 0);
3185 *start_mark
-= diff1
;
3187 *start_mark
= MAX (*start_mark
, start_eol
);
3188 *end_mark
= MIN (*end_mark
, end_eol
);
3194 /* --------------------------------------------------------------------------------------------- */
3195 /** highlight marker toggle */
3198 edit_mark_cmd (WEdit
*edit
, gboolean unmark
)
3200 edit_push_markers (edit
);
3203 edit_set_markers (edit
, 0, 0, 0, 0);
3204 edit
->force
|= REDRAW_PAGE
;
3206 else if (edit
->mark2
>= 0)
3208 edit
->end_mark_curs
= -1;
3209 edit_set_markers (edit
, edit
->buffer
.curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3210 edit
->curs_col
+ edit
->over_col
);
3211 edit
->force
|= REDRAW_PAGE
;
3215 edit
->end_mark_curs
= edit
->buffer
.curs1
;
3216 edit_set_markers (edit
, edit
->mark1
, edit
->buffer
.curs1
, edit
->column1
,
3217 edit
->curs_col
+ edit
->over_col
);
3221 /* --------------------------------------------------------------------------------------------- */
3222 /** highlight the word under cursor */
3225 edit_mark_current_word_cmd (WEdit
*edit
)
3229 for (pos
= edit
->buffer
.curs1
; pos
!= 0; pos
--)
3233 c1
= edit_buffer_get_byte (&edit
->buffer
, pos
);
3234 c2
= edit_buffer_get_byte (&edit
->buffer
, pos
- 1);
3235 if (!isspace (c1
) && isspace (c2
))
3237 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3242 for (; pos
< edit
->buffer
.size
; pos
++)
3246 c1
= edit_buffer_get_byte (&edit
->buffer
, pos
);
3247 c2
= edit_buffer_get_byte (&edit
->buffer
, pos
+ 1);
3248 if (!isspace (c1
) && isspace (c2
))
3250 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3253 edit
->mark2
= MIN (pos
+ 1, edit
->buffer
.size
);
3255 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3258 /* --------------------------------------------------------------------------------------------- */
3261 edit_mark_current_line_cmd (WEdit
*edit
)
3263 edit
->mark1
= edit_buffer_get_current_bol (&edit
->buffer
);
3264 edit
->mark2
= edit_buffer_get_current_eol (&edit
->buffer
);
3266 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3269 /* --------------------------------------------------------------------------------------------- */
3272 edit_delete_line (WEdit
*edit
)
3275 * Delete right part of the line.
3276 * Note that edit_buffer_get_byte() returns '\n' when byte position is
3279 while (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
3280 (void) edit_delete (edit
, TRUE
);
3284 * Note that edit_delete() will not corrupt anything if called while
3285 * cursor position is EOF.
3287 (void) edit_delete (edit
, TRUE
);
3290 * Delete left part of the line.
3291 * Note, that edit_buffer_get_byte() returns '\n' when byte position is < 0.
3293 while (edit_buffer_get_previous_byte (&edit
->buffer
) != '\n')
3294 (void) edit_backspace (edit
, TRUE
);
3297 /* --------------------------------------------------------------------------------------------- */
3300 edit_push_key_press (WEdit
*edit
)
3302 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3303 if (edit
->mark2
== -1)
3305 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3306 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3310 /* --------------------------------------------------------------------------------------------- */
3313 edit_find_bracket (WEdit
*edit
)
3315 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3316 if (edit
->last_bracket
!= edit
->bracket
)
3317 edit
->force
|= REDRAW_PAGE
;
3318 edit
->last_bracket
= edit
->bracket
;
3321 /* --------------------------------------------------------------------------------------------- */
3323 * This executes a command as though the user initiated it through a key
3324 * press. Callback with MSG_KEY as a message calls this after
3325 * translating the key press. This function can be used to pass any
3326 * command to the editor. Note that the screen wouldn't update
3327 * automatically. Either of command or char_for_insertion must be
3328 * passed as -1. Commands are executed, and char_for_insertion is
3329 * inserted at the cursor.
3333 edit_execute_key_command (WEdit
*edit
, long command
, int char_for_insertion
)
3335 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3337 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3340 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3343 if (macro_index
!= -1)
3345 edit
->force
|= REDRAW_COMPLETELY
;
3346 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3348 edit_store_macro_cmd (edit
);
3352 if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3354 edit_repeat_macro_cmd (edit
);
3360 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3362 record_macro_buf
[macro_index
].action
= command
;
3363 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3365 /* record the beginning of a set of editing actions initiated by a key press */
3366 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3367 edit_push_key_press (edit
);
3369 edit_execute_cmd (edit
, command
, char_for_insertion
);
3370 if (edit
->column_highlight
)
3371 edit
->force
|= REDRAW_PAGE
;
3374 /* --------------------------------------------------------------------------------------------- */
3376 This executes a command at a lower level than macro recording.
3377 It also does not push a key_press onto the undo stack. This means
3378 that if it is called many times, a single undo command will undo
3379 all of them. It also does not check for the Undo command.
3382 edit_execute_cmd (WEdit
*edit
, long command
, int char_for_insertion
)
3384 WRect
*w
= &WIDGET (edit
)->rect
;
3386 if (command
== CK_WindowFullscreen
)
3388 edit_toggle_fullscreen (edit
);
3392 /* handle window state */
3393 if (edit_handle_move_resize (edit
, command
))
3396 edit
->force
|= REDRAW_LINE
;
3398 /* The next key press will unhighlight the found string, so update
3400 if (edit
->found_len
|| edit
->column_highlight
)
3401 edit
->force
|= REDRAW_PAGE
;
3405 /* a mark command with shift-arrow */
3408 case CK_MarkToWordBegin
:
3409 case CK_MarkToWordEnd
:
3415 case CK_MarkPageDown
:
3416 case CK_MarkToFileBegin
:
3417 case CK_MarkToFileEnd
:
3418 case CK_MarkToPageBegin
:
3419 case CK_MarkToPageEnd
:
3420 case CK_MarkScrollUp
:
3421 case CK_MarkScrollDown
:
3422 case CK_MarkParagraphUp
:
3423 case CK_MarkParagraphDown
:
3424 /* a mark command with alt-arrow */
3425 case CK_MarkColumnPageUp
:
3426 case CK_MarkColumnPageDown
:
3427 case CK_MarkColumnLeft
:
3428 case CK_MarkColumnRight
:
3429 case CK_MarkColumnUp
:
3430 case CK_MarkColumnDown
:
3431 case CK_MarkColumnScrollUp
:
3432 case CK_MarkColumnScrollDown
:
3433 case CK_MarkColumnParagraphUp
:
3434 case CK_MarkColumnParagraphDown
:
3435 edit
->column_highlight
= 0;
3436 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3438 edit_mark_cmd (edit
, TRUE
); /* clear */
3439 edit_mark_cmd (edit
, FALSE
); /* marking on */
3441 edit
->highlight
= 1;
3444 /* any other command */
3446 if (edit
->highlight
)
3447 edit_mark_cmd (edit
, FALSE
); /* clear */
3448 edit
->highlight
= 0;
3451 /* first check for undo */
3452 if (command
== CK_Undo
)
3454 edit
->redo_stack_reset
= 0;
3455 edit_group_undo (edit
);
3456 edit
->found_len
= 0;
3457 edit
->prev_col
= edit_get_col (edit
);
3458 edit
->search_start
= edit
->buffer
.curs1
;
3461 /* check for redo */
3462 if (command
== CK_Redo
)
3464 edit
->redo_stack_reset
= 0;
3465 edit_do_redo (edit
);
3466 edit
->found_len
= 0;
3467 edit
->prev_col
= edit_get_col (edit
);
3468 edit
->search_start
= edit
->buffer
.curs1
;
3472 edit
->redo_stack_reset
= 1;
3474 /* An ordinary key press */
3475 if (char_for_insertion
>= 0)
3477 /* if non persistent selection and text selected */
3478 if (!edit_options
.persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3479 edit_block_delete_cmd (edit
);
3481 if (edit
->overwrite
)
3483 /* remove char only one time, after input first byte, multibyte chars */
3485 if (!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3487 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
3488 edit_delete (edit
, FALSE
);
3490 if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
3491 edit_insert_over (edit
);
3494 Encode 8-bit input as UTF-8, if display (locale) is *not* UTF-8,
3495 *but* source encoding *is* set to UTF-8; see ticket #3843 for the details.
3497 if (char_for_insertion
> 127 && str_isutf8 (get_codepage_id (mc_global
.source_codepage
))
3498 && !mc_global
.utf8_display
)
3500 unsigned char str
[UTF8_CHAR_LEN
+ 1];
3504 res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3513 for (i
= 0; i
<= UTF8_CHAR_LEN
&& str
[i
] != '\0'; i
++)
3515 char_for_insertion
= str
[i
];
3516 edit_insert (edit
, char_for_insertion
);
3521 edit_insert (edit
, char_for_insertion
);
3523 if (edit_options
.auto_para_formatting
)
3525 format_paragraph (edit
, FALSE
);
3526 edit
->force
|= REDRAW_PAGE
;
3529 check_and_wrap_line (edit
);
3530 edit
->found_len
= 0;
3531 edit
->prev_col
= edit_get_col (edit
);
3532 edit
->search_start
= edit
->buffer
.curs1
;
3533 edit_find_bracket (edit
);
3539 case CK_TopOnScreen
:
3540 case CK_BottomOnScreen
:
3553 if (!edit_options
.persistent_selections
&& edit
->mark2
>= 0)
3555 if (edit
->column_highlight
)
3556 edit_push_undo_action (edit
, COLUMN_ON
);
3557 edit
->column_highlight
= 0;
3558 edit_mark_cmd (edit
, TRUE
);
3567 case CK_TopOnScreen
:
3568 case CK_BottomOnScreen
:
3569 case CK_MarkToPageBegin
:
3570 case CK_MarkToPageEnd
:
3575 case CK_MarkToWordBegin
:
3576 case CK_MarkToWordEnd
:
3579 case CK_MarkColumnUp
:
3580 case CK_MarkColumnDown
:
3581 if (edit
->mark2
== -1)
3582 break; /*marking is following the cursor: may need to highlight a whole line */
3588 edit
->force
|= REDRAW_CHAR_ONLY
;
3594 /* basic cursor key commands */
3598 /* if non persistent selection and text selected */
3599 if (!edit_options
.persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3600 edit_block_delete_cmd (edit
);
3601 else if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
3603 else if (edit_options
.backspace_through_tabs
&& is_in_indent (&edit
->buffer
))
3605 while (edit_buffer_get_previous_byte (&edit
->buffer
) != '\n' && edit
->buffer
.curs1
> 0)
3606 edit_backspace (edit
, TRUE
);
3608 else if (edit_options
.fake_half_tabs
&& is_in_indent (&edit
->buffer
)
3609 && right_of_four_spaces (edit
))
3613 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3614 edit_backspace (edit
, TRUE
);
3617 edit_backspace (edit
, FALSE
);
3620 /* if non persistent selection and text selected */
3621 if (!edit_options
.persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3622 edit_block_delete_cmd (edit
);
3625 if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
3626 edit_insert_over (edit
);
3628 if (edit_options
.fake_half_tabs
&& is_in_indent (&edit
->buffer
)
3629 && left_of_four_spaces (edit
))
3633 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3634 edit_delete (edit
, TRUE
);
3637 edit_delete (edit
, FALSE
);
3640 case CK_DeleteToWordBegin
:
3642 edit_left_delete_word (edit
);
3644 case CK_DeleteToWordEnd
:
3645 if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
3646 edit_insert_over (edit
);
3648 edit_right_delete_word (edit
);
3651 edit_delete_line (edit
);
3653 case CK_DeleteToHome
:
3654 edit_delete_to_line_begin (edit
);
3656 case CK_DeleteToEnd
:
3657 edit_delete_to_line_end (edit
);
3661 if (edit_options
.auto_para_formatting
)
3663 edit_double_newline (edit
);
3664 if (edit_options
.return_does_auto_indent
&& !bracketed_pasting_in_progress
)
3665 edit_auto_indent (edit
);
3666 format_paragraph (edit
, FALSE
);
3670 edit_insert (edit
, '\n');
3671 if (edit_options
.return_does_auto_indent
&& !bracketed_pasting_in_progress
)
3672 edit_auto_indent (edit
);
3676 edit_insert (edit
, '\n');
3679 case CK_MarkColumnPageUp
:
3680 edit
->column_highlight
= 1;
3684 edit_move_up (edit
, w
->lines
- 1, TRUE
);
3686 case CK_MarkColumnPageDown
:
3687 edit
->column_highlight
= 1;
3690 case CK_MarkPageDown
:
3691 edit_move_down (edit
, w
->lines
- 1, TRUE
);
3693 case CK_MarkColumnLeft
:
3694 edit
->column_highlight
= 1;
3698 if (edit_options
.fake_half_tabs
&& is_in_indent (&edit
->buffer
)
3699 && right_of_four_spaces (edit
))
3701 if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
3704 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3705 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3708 edit_left_char_move_cmd (edit
);
3710 case CK_MarkColumnRight
:
3711 edit
->column_highlight
= 1;
3715 if (edit_options
.fake_half_tabs
&& is_in_indent (&edit
->buffer
)
3716 && left_of_four_spaces (edit
))
3718 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3719 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3722 edit_right_char_move_cmd (edit
);
3724 case CK_TopOnScreen
:
3725 case CK_MarkToPageBegin
:
3726 edit_begin_page (edit
);
3728 case CK_BottomOnScreen
:
3729 case CK_MarkToPageEnd
:
3730 edit_end_page (edit
);
3733 case CK_MarkToWordBegin
:
3735 edit_left_word_move_cmd (edit
);
3738 case CK_MarkToWordEnd
:
3740 edit_right_word_move_cmd (edit
);
3742 case CK_MarkColumnUp
:
3743 edit
->column_highlight
= 1;
3747 edit_move_up (edit
, 1, FALSE
);
3749 case CK_MarkColumnDown
:
3750 edit
->column_highlight
= 1;
3754 edit_move_down (edit
, 1, FALSE
);
3756 case CK_MarkColumnParagraphUp
:
3757 edit
->column_highlight
= 1;
3759 case CK_ParagraphUp
:
3760 case CK_MarkParagraphUp
:
3761 edit_move_up_paragraph (edit
, FALSE
);
3763 case CK_MarkColumnParagraphDown
:
3764 edit
->column_highlight
= 1;
3766 case CK_ParagraphDown
:
3767 case CK_MarkParagraphDown
:
3768 edit_move_down_paragraph (edit
, FALSE
);
3770 case CK_MarkColumnScrollUp
:
3771 edit
->column_highlight
= 1;
3774 case CK_MarkScrollUp
:
3775 edit_move_up (edit
, 1, TRUE
);
3777 case CK_MarkColumnScrollDown
:
3778 edit
->column_highlight
= 1;
3781 case CK_MarkScrollDown
:
3782 edit_move_down (edit
, 1, TRUE
);
3786 edit_cursor_to_bol (edit
);
3790 edit_cursor_to_eol (edit
);
3793 /* if text marked shift block */
3794 if (edit
->mark1
!= edit
->mark2
&& !edit_options
.persistent_selections
)
3796 if (edit
->mark2
< 0)
3797 edit_mark_cmd (edit
, FALSE
);
3798 edit_move_block_to_right (edit
);
3802 if (edit_options
.cursor_beyond_eol
)
3803 edit_insert_over (edit
);
3804 edit_tab_cmd (edit
);
3805 if (edit_options
.auto_para_formatting
)
3807 format_paragraph (edit
, FALSE
);
3808 edit
->force
|= REDRAW_PAGE
;
3811 check_and_wrap_line (edit
);
3815 case CK_InsertOverwrite
:
3816 edit
->overwrite
= !edit
->overwrite
;
3820 if (edit
->mark2
>= 0)
3822 if (edit
->column_highlight
)
3823 edit_push_undo_action (edit
, COLUMN_ON
);
3824 edit
->column_highlight
= 0;
3826 edit_mark_cmd (edit
, FALSE
);
3829 if (!edit
->column_highlight
)
3830 edit_push_undo_action (edit
, COLUMN_OFF
);
3831 edit
->column_highlight
= 1;
3832 edit_mark_cmd (edit
, FALSE
);
3835 edit_set_markers (edit
, 0, edit
->buffer
.size
, 0, 0);
3836 edit
->force
|= REDRAW_PAGE
;
3839 if (edit
->column_highlight
)
3840 edit_push_undo_action (edit
, COLUMN_ON
);
3841 edit
->column_highlight
= 0;
3842 edit_mark_cmd (edit
, TRUE
);
3845 if (edit
->column_highlight
)
3846 edit_push_undo_action (edit
, COLUMN_ON
);
3847 edit
->column_highlight
= 0;
3848 edit_mark_current_word_cmd (edit
);
3851 if (edit
->column_highlight
)
3852 edit_push_undo_action (edit
, COLUMN_ON
);
3853 edit
->column_highlight
= 0;
3854 edit_mark_current_line_cmd (edit
);
3858 book_mark_clear (edit
, edit
->buffer
.curs_line
, BOOK_MARK_FOUND_COLOR
);
3859 if (book_mark_query_color (edit
, edit
->buffer
.curs_line
, BOOK_MARK_COLOR
))
3860 book_mark_clear (edit
, edit
->buffer
.curs_line
, BOOK_MARK_COLOR
);
3862 book_mark_insert (edit
, edit
->buffer
.curs_line
, BOOK_MARK_COLOR
);
3864 case CK_BookmarkFlush
:
3865 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3866 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3867 edit
->force
|= REDRAW_PAGE
;
3869 case CK_BookmarkNext
:
3870 if (edit
->book_mark
!= NULL
)
3872 edit_book_mark_t
*p
;
3874 p
= book_mark_find (edit
, edit
->buffer
.curs_line
);
3875 if (p
->next
!= NULL
)
3878 if (p
->line
>= edit
->start_line
+ w
->lines
|| p
->line
< edit
->start_line
)
3879 edit_move_display (edit
, p
->line
- w
->lines
/ 2);
3880 edit_move_to_line (edit
, p
->line
);
3884 case CK_BookmarkPrev
:
3885 if (edit
->book_mark
!= NULL
)
3887 edit_book_mark_t
*p
;
3889 p
= book_mark_find (edit
, edit
->buffer
.curs_line
);
3890 while (p
->line
== edit
->buffer
.curs_line
)
3891 if (p
->prev
!= NULL
)
3895 if (p
->line
>= edit
->start_line
+ w
->lines
|| p
->line
< edit
->start_line
)
3896 edit_move_display (edit
, p
->line
- w
->lines
/ 2);
3897 edit_move_to_line (edit
, p
->line
);
3903 case CK_MarkToFileBegin
:
3904 edit_move_to_top (edit
);
3907 case CK_MarkToFileEnd
:
3908 edit_move_to_bottom (edit
);
3912 if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
3913 edit_insert_over (edit
);
3914 edit_block_copy_cmd (edit
);
3917 edit_block_delete_cmd (edit
);
3920 edit_block_move_cmd (edit
);
3923 case CK_BlockShiftLeft
:
3924 if (edit
->mark1
!= edit
->mark2
)
3925 edit_move_block_to_left (edit
);
3927 case CK_BlockShiftRight
:
3928 if (edit
->mark1
!= edit
->mark2
)
3929 edit_move_block_to_right (edit
);
3932 edit_copy_to_X_buf_cmd (edit
);
3935 edit_cut_to_X_buf_cmd (edit
);
3938 /* if non persistent selection and text selected */
3939 if (!edit_options
.persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3940 edit_block_delete_cmd (edit
);
3941 if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
3942 edit_insert_over (edit
);
3943 edit_paste_from_X_buf_cmd (edit
);
3944 if (!edit_options
.persistent_selections
&& edit
->mark2
>= 0)
3946 if (edit
->column_highlight
)
3947 edit_push_undo_action (edit
, COLUMN_ON
);
3948 edit
->column_highlight
= 0;
3949 edit_mark_cmd (edit
, TRUE
);
3953 edit_paste_from_history (edit
);
3957 edit_save_as_cmd (edit
);
3960 edit_save_confirm_cmd (edit
);
3963 edit_save_block_cmd (edit
);
3966 edit_insert_file_cmd (edit
);
3970 edit_load_back_cmd (edit
);
3973 edit_load_forward_cmd (edit
);
3976 case CK_SyntaxChoose
:
3977 edit_syntax_dialog (edit
);
3981 edit_search_cmd (edit
, FALSE
);
3983 case CK_SearchContinue
:
3984 edit_search_cmd (edit
, TRUE
);
3987 edit_replace_cmd (edit
, FALSE
);
3989 case CK_ReplaceContinue
:
3990 edit_replace_cmd (edit
, TRUE
);
3993 /* if text marked shift block */
3994 if (edit
->mark1
!= edit
->mark2
&& !edit_options
.persistent_selections
)
3995 edit_move_block_to_left (edit
);
3997 edit_complete_word_cmd (edit
);
4000 edit_get_match_keyword_cmd (edit
);
4004 case CK_SpellCheckCurrentWord
:
4005 edit_suggest_current_word (edit
);
4008 edit_spellcheck_file (edit
);
4010 case CK_SpellCheckSelectLang
:
4011 edit_set_spell_lang ();
4018 /* fool gcc to prevent a Y2K warning */
4019 char time_format
[] = "_c";
4020 time_format
[0] = '%';
4022 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4023 edit_print_string (edit
, s
);
4024 edit
->force
|= REDRAW_PAGE
;
4028 edit_goto_cmd (edit
);
4030 case CK_ParagraphFormat
:
4031 format_paragraph (edit
, TRUE
);
4032 edit
->force
|= REDRAW_PAGE
;
4034 case CK_MacroDelete
:
4035 edit_delete_macro_cmd (edit
);
4037 case CK_MatchBracket
:
4038 edit_goto_matching_bracket (edit
);
4041 edit_user_menu (edit
, NULL
, -1);
4044 edit_sort_cmd (edit
);
4046 case CK_ExternalCommand
:
4047 edit_ext_cmd (edit
);
4050 edit_mail_dialog (edit
);
4053 case CK_SelectCodepage
:
4054 edit_select_codepage_cmd (edit
);
4057 case CK_InsertLiteral
:
4058 edit_insert_literal_cmd (edit
);
4060 case CK_MacroStartStopRecord
:
4061 edit_begin_end_macro_cmd (edit
);
4063 case CK_RepeatStartStopRecord
:
4064 edit_begin_end_repeat_cmd (edit
);
4066 case CK_ExtendedKeyMap
:
4067 WIDGET (edit
)->ext_mode
= TRUE
;
4074 if ((command
/ CK_PipeBlock (0)) == 1)
4075 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
4077 /* keys which must set the col position, and the search vars */
4081 case CK_SearchContinue
:
4083 case CK_ReplaceContinue
:
4085 edit
->prev_col
= edit_get_col (edit
);
4089 case CK_MarkColumnUp
:
4092 case CK_MarkColumnDown
:
4095 case CK_MarkColumnPageUp
:
4097 case CK_MarkPageDown
:
4098 case CK_MarkColumnPageDown
:
4100 case CK_MarkToFileBegin
:
4102 case CK_MarkToFileEnd
:
4103 case CK_ParagraphUp
:
4104 case CK_MarkParagraphUp
:
4105 case CK_MarkColumnParagraphUp
:
4106 case CK_ParagraphDown
:
4107 case CK_MarkParagraphDown
:
4108 case CK_MarkColumnParagraphDown
:
4110 case CK_MarkScrollUp
:
4111 case CK_MarkColumnScrollUp
:
4113 case CK_MarkScrollDown
:
4114 case CK_MarkColumnScrollDown
:
4115 edit
->search_start
= edit
->buffer
.curs1
;
4116 edit
->found_len
= 0;
4119 edit
->found_len
= 0;
4120 edit
->prev_col
= edit_get_col (edit
);
4121 edit
->search_start
= edit
->buffer
.curs1
;
4123 edit_find_bracket (edit
);
4125 if (edit_options
.auto_para_formatting
)
4131 case CK_DeleteToWordBegin
:
4132 case CK_DeleteToWordEnd
:
4133 case CK_DeleteToHome
:
4134 case CK_DeleteToEnd
:
4135 format_paragraph (edit
, FALSE
);
4136 edit
->force
|= REDRAW_PAGE
;
4144 /* --------------------------------------------------------------------------------------------- */
4147 edit_stack_init (void)
4149 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4150 edit_arg_init (&edit_history_moveto
[edit_stack_iterator
], NULL
, -1);
4152 edit_stack_iterator
= 0;
4155 /* --------------------------------------------------------------------------------------------- */
4158 edit_stack_free (void)
4160 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4161 vfs_path_free (edit_history_moveto
[edit_stack_iterator
].file_vpath
, TRUE
);
4164 /* --------------------------------------------------------------------------------------------- */
4168 edit_move_up (WEdit
*edit
, long i
, gboolean do_scroll
)
4170 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4173 /* --------------------------------------------------------------------------------------------- */
4177 edit_move_down (WEdit
*edit
, long i
, gboolean do_scroll
)
4179 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4182 /* --------------------------------------------------------------------------------------------- */
4184 * Create edit_arg_t object from vfs_path_t object and the line number.
4186 * @param file_vpath file path object
4187 * @param line_number line number. If value is 0, try to restore saved position.
4188 * @return edit_arg_t object
4192 edit_arg_vpath_new (vfs_path_t
*file_vpath
, long line_number
)
4196 arg
= g_new (edit_arg_t
, 1);
4197 arg
->file_vpath
= file_vpath
;
4198 arg
->line_number
= line_number
;
4203 /* --------------------------------------------------------------------------------------------- */
4205 * Create edit_arg_t object from file name and the line number.
4207 * @param file_name file name
4208 * @param line_number line number. If value is 0, try to restore saved position.
4209 * @return edit_arg_t object
4213 edit_arg_new (const char *file_name
, long line_number
)
4215 return edit_arg_vpath_new (vfs_path_from_str (file_name
), line_number
);
4218 /* --------------------------------------------------------------------------------------------- */
4220 * Initialize edit_arg_t object.
4222 * @param arg edit_arg_t object
4223 * @param vpath vfs_path_t object
4224 * @param line line number
4228 edit_arg_init (edit_arg_t
*arg
, vfs_path_t
*vpath
, long line
)
4230 arg
->file_vpath
= (vfs_path_t
*) vpath
;
4231 arg
->line_number
= line
;
4234 /* --------------------------------------------------------------------------------------------- */
4236 * Apply new values to edit_arg_t object members.
4238 * @param arg edit_arg_t object
4239 * @param vpath vfs_path_t object
4240 * @param line line number
4244 edit_arg_assign (edit_arg_t
*arg
, vfs_path_t
*vpath
, long line
)
4246 vfs_path_free (arg
->file_vpath
, TRUE
);
4247 edit_arg_init (arg
, vpath
, line
);
4250 /* --------------------------------------------------------------------------------------------- */
4252 * Free the edit_arg_t object.
4254 * @param arg edit_arg_t object
4258 edit_arg_free (edit_arg_t
*arg
)
4260 vfs_path_free (arg
->file_vpath
, TRUE
);
4264 /* --------------------------------------------------------------------------------------------- */
4267 edit_get_file_name (const WEdit
*edit
)
4269 return vfs_path_as_str (edit
->filename_vpath
);
4272 /* --------------------------------------------------------------------------------------------- */