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;
2167 edit
->attrs_ok
= (mc_fgetflags (arg
->file_vpath
, &edit
->attrs
) == 0);
2171 edit
->last_bracket
= -1;
2172 edit
->force
|= REDRAW_PAGE
;
2174 /* set file name before load file */
2177 edit_set_filename (edit
, arg
->file_vpath
);
2178 line
= arg
->line_number
;
2182 edit_set_filename (edit
, NULL
);
2186 edit
->undo_stack_size
= START_STACK_SIZE
;
2187 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2188 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2190 edit
->redo_stack_size
= START_STACK_SIZE
;
2191 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2192 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2196 edit
->converter
= str_cnv_from_term
;
2197 edit_set_codeset (edit
);
2200 if (!edit_load_file (edit
))
2202 /* edit_load_file already gives an error message */
2208 edit
->loading_done
= 1;
2211 edit_load_syntax (edit
, NULL
, NULL
);
2212 edit_get_syntax_color (edit
, -1);
2214 /* load saved cursor position and/or boolmarks */
2215 if ((line
== 0) && edit_options
.save_position
)
2216 edit_load_position (edit
, TRUE
);
2219 edit_load_position (edit
, FALSE
);
2222 edit_move_display (edit
, line
- 1);
2223 edit_move_to_line (edit
, line
- 1);
2226 edit_load_macro_cmd (edit
);
2231 /* --------------------------------------------------------------------------------------------- */
2233 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2235 edit_clean (WEdit
*edit
)
2240 /* a stale lock, remove it */
2242 (void) unlock_file (edit
->filename_vpath
);
2244 /* save cursor position */
2245 if (edit_options
.save_position
)
2246 edit_save_position (edit
);
2247 else if (edit
->serialized_bookmarks
!= NULL
)
2248 g_array_free (edit
->serialized_bookmarks
, TRUE
);
2250 /* File specified on the mcedit command line and never saved */
2251 if (edit
->delete_file
)
2252 unlink (vfs_path_get_last_path_str (edit
->filename_vpath
));
2254 edit_free_syntax_rules (edit
);
2255 book_mark_flush (edit
, -1);
2257 edit_buffer_clean (&edit
->buffer
);
2259 g_free (edit
->undo_stack
);
2260 g_free (edit
->redo_stack
);
2261 vfs_path_free (edit
->filename_vpath
, TRUE
);
2262 vfs_path_free (edit
->dir_vpath
, TRUE
);
2263 edit_search_deinit (edit
);
2266 if (edit
->converter
!= str_cnv_from_term
)
2267 str_close_conv (edit
->converter
);
2270 edit_purge_widget (edit
);
2275 /* --------------------------------------------------------------------------------------------- */
2278 * Load a new file into the editor and set line. If it fails, preserve the old file.
2279 * To do it, allocate a new widget, initialize it and, if the new file
2280 * was loaded, copy the data to the old widget.
2282 * @return TRUE on success, FALSE on failure.
2285 edit_reload_line (WEdit
*edit
, const edit_arg_t
*arg
)
2287 Widget
*w
= WIDGET (edit
);
2290 e
= g_malloc0 (sizeof (WEdit
));
2292 /* save some widget parameters */
2293 e
->fullscreen
= edit
->fullscreen
;
2294 e
->loc_prev
= edit
->loc_prev
;
2296 if (edit_init (e
, &w
->rect
, arg
) == NULL
)
2303 memcpy (edit
, e
, sizeof (*edit
));
2309 /* --------------------------------------------------------------------------------------------- */
2313 edit_set_codeset (WEdit
*edit
)
2318 get_codepage_id (mc_global
.source_codepage
>=
2319 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2324 conv
= str_crt_conv_from (cp_id
);
2325 if (conv
!= INVALID_CONV
)
2327 if (edit
->converter
!= str_cnv_from_term
)
2328 str_close_conv (edit
->converter
);
2329 edit
->converter
= conv
;
2334 edit
->utf8
= str_isutf8 (cp_id
);
2338 /* --------------------------------------------------------------------------------------------- */
2341 * Recording stack for undo:
2342 * The following is an implementation of a compressed stack. Identical
2343 * pushes are recorded by a negative prefix indicating the number of times the
2344 * same char was pushed. This saves space for repeated curs-left or curs-right
2361 * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2362 * 256-512 is an insert ahead (from a delete), If it is between 600 and 700 it is one
2363 * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2364 * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2367 * The only way the cursor moves or the buffer is changed is through the routines:
2368 * insert, backspace, insert_ahead, delete, and cursor_move.
2369 * These record the reverse undo movements onto the stack each time they are
2372 * Each key press results in a set of actions (insert; delete ...). So each time
2373 * a key is pressed the current position of start_display is pushed as
2374 * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2375 * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2376 * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2380 * @param edit editor object
2381 * @param c code of the action
2385 edit_push_undo_action (WEdit
*edit
, long c
)
2387 unsigned long sp
= edit
->undo_stack_pointer
;
2390 /* first enlarge the stack if necessary */
2391 if (sp
> edit
->undo_stack_size
- 10)
2395 if (edit
->undo_stack_size
< (unsigned long) max_undo
)
2399 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2402 edit
->undo_stack
= t
;
2403 edit
->undo_stack_size
<<= 1;
2404 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2408 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2409 if (edit
->undo_stack_disable
)
2411 edit_push_redo_action (edit
, KEY_PRESS
);
2412 edit_push_redo_action (edit
, c
);
2416 if (edit
->redo_stack_reset
)
2417 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2419 if (edit
->undo_stack_bottom
!= sp
2420 && spm1
!= edit
->undo_stack_bottom
2421 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2425 if (edit
->undo_stack
[spm1
] < 0)
2427 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2428 if (d
== c
&& edit
->undo_stack
[spm1
] > -1000000000)
2430 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2431 edit
->undo_stack
[spm1
]--;
2437 d
= edit
->undo_stack
[spm1
];
2441 return; /* --> no need to push multiple do-nothings */
2442 edit
->undo_stack
[sp
] = -2;
2447 edit
->undo_stack
[sp
] = c
;
2450 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2452 /* if the sp wraps round and catches the undo_stack_bottom then erase
2453 * the first set of actions on the stack to make space - by moving
2454 * undo_stack_bottom forward one "key press" */
2455 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2456 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2457 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2460 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2462 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2463 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2465 /*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: */
2466 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2467 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2469 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2473 /* --------------------------------------------------------------------------------------------- */
2476 edit_push_redo_action (WEdit
*edit
, long c
)
2478 unsigned long sp
= edit
->redo_stack_pointer
;
2480 /* first enlarge the stack if necessary */
2481 if (sp
> edit
->redo_stack_size
- 10)
2485 if (edit
->redo_stack_size
< (unsigned long) max_undo
)
2489 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2492 edit
->redo_stack
= t
;
2493 edit
->redo_stack_size
<<= 1;
2494 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2498 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2500 if (edit
->redo_stack_bottom
!= sp
2501 && spm1
!= edit
->redo_stack_bottom
2502 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2506 if (edit
->redo_stack
[spm1
] < 0)
2508 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2509 if (d
== c
&& edit
->redo_stack
[spm1
] > -1000000000)
2511 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2512 edit
->redo_stack
[spm1
]--;
2518 d
= edit
->redo_stack
[spm1
];
2522 return; /* --> no need to push multiple do-nothings */
2523 edit
->redo_stack
[sp
] = -2;
2524 goto redo_check_bottom
;
2528 edit
->redo_stack
[sp
] = c
;
2531 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2533 /* if the sp wraps round and catches the redo_stack_bottom then erase
2534 * the first set of actions on the stack to make space - by moving
2535 * redo_stack_bottom forward one "key press" */
2536 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2537 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2538 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2541 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2543 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2544 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2547 * If a single key produced enough pushes to wrap all the way round then
2548 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2549 * The stack is then initialised:
2552 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2553 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2554 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2557 /* --------------------------------------------------------------------------------------------- */
2559 Basic low level single character buffer alterations and movements at the cursor.
2563 edit_insert (WEdit
*edit
, int c
)
2565 /* first we must update the position of the display window */
2566 if (edit
->buffer
.curs1
< edit
->start_display
)
2568 edit
->start_display
++;
2573 /* Mark file as modified, unless the file hasn't been fully loaded */
2574 if (edit
->loading_done
)
2575 edit_modification (edit
);
2577 /* now we must update some info on the file and check if a redraw is required */
2580 book_mark_inc (edit
, edit
->buffer
.curs_line
);
2581 edit
->buffer
.curs_line
++;
2582 edit
->buffer
.lines
++;
2583 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2586 /* save the reverse command onto the undo stack */
2587 /* ordinary char and not space */
2589 edit_push_undo_action (edit
, BACKSPACE
);
2591 edit_push_undo_action (edit
, BACKSPACE_BR
);
2592 /* update markers */
2593 edit
->mark1
+= (edit
->mark1
> edit
->buffer
.curs1
) ? 1 : 0;
2594 edit
->mark2
+= (edit
->mark2
> edit
->buffer
.curs1
) ? 1 : 0;
2595 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->buffer
.curs1
) ? 1 : 0;
2597 edit_buffer_insert (&edit
->buffer
, c
);
2600 /* --------------------------------------------------------------------------------------------- */
2601 /** same as edit_insert and move left */
2604 edit_insert_ahead (WEdit
*edit
, int c
)
2606 if (edit
->buffer
.curs1
< edit
->start_display
)
2608 edit
->start_display
++;
2612 edit_modification (edit
);
2615 book_mark_inc (edit
, edit
->buffer
.curs_line
);
2616 edit
->buffer
.lines
++;
2617 edit
->force
|= REDRAW_AFTER_CURSOR
;
2619 /* ordinary char and not space */
2621 edit_push_undo_action (edit
, DELCHAR
);
2623 edit_push_undo_action (edit
, DELCHAR_BR
);
2625 edit
->mark1
+= (edit
->mark1
>= edit
->buffer
.curs1
) ? 1 : 0;
2626 edit
->mark2
+= (edit
->mark2
>= edit
->buffer
.curs1
) ? 1 : 0;
2627 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->buffer
.curs1
) ? 1 : 0;
2629 edit_buffer_insert_ahead (&edit
->buffer
, c
);
2632 /* --------------------------------------------------------------------------------------------- */
2635 edit_insert_over (WEdit
*edit
)
2639 for (i
= 0; i
< edit
->over_col
; i
++)
2640 edit_insert (edit
, ' ');
2645 /* --------------------------------------------------------------------------------------------- */
2648 edit_delete (WEdit
*edit
, gboolean byte_delete
)
2651 int char_length
= 1;
2654 if (edit
->buffer
.curs2
== 0)
2658 /* if byte_delete == TRUE then delete only one byte not multibyte char */
2659 if (edit
->utf8
&& !byte_delete
)
2661 edit_buffer_get_utf (&edit
->buffer
, edit
->buffer
.curs1
, &char_length
);
2662 if (char_length
< 1)
2669 if (edit
->mark2
!= edit
->mark1
)
2670 edit_push_markers (edit
);
2672 for (i
= 1; i
<= char_length
; i
++)
2674 if (edit
->mark1
> edit
->buffer
.curs1
)
2677 edit
->end_mark_curs
--;
2679 if (edit
->mark2
> edit
->buffer
.curs1
)
2681 if (edit
->last_get_rule
> edit
->buffer
.curs1
)
2682 edit
->last_get_rule
--;
2684 p
= edit_buffer_delete (&edit
->buffer
);
2686 edit_push_undo_action (edit
, p
+ 256);
2689 edit_modification (edit
);
2692 book_mark_dec (edit
, edit
->buffer
.curs_line
);
2693 edit
->buffer
.lines
--;
2694 edit
->force
|= REDRAW_AFTER_CURSOR
;
2696 if (edit
->buffer
.curs1
< edit
->start_display
)
2698 edit
->start_display
--;
2706 /* --------------------------------------------------------------------------------------------- */
2709 edit_backspace (WEdit
*edit
, gboolean byte_delete
)
2712 int char_length
= 1;
2715 if (edit
->buffer
.curs1
== 0)
2718 if (edit
->mark2
!= edit
->mark1
)
2719 edit_push_markers (edit
);
2722 if (edit
->utf8
&& !byte_delete
)
2724 edit_buffer_get_prev_utf (&edit
->buffer
, edit
->buffer
.curs1
, &char_length
);
2725 if (char_length
< 1)
2732 for (i
= 1; i
<= char_length
; i
++)
2734 if (edit
->mark1
>= edit
->buffer
.curs1
)
2737 edit
->end_mark_curs
--;
2739 if (edit
->mark2
>= edit
->buffer
.curs1
)
2741 if (edit
->last_get_rule
>= edit
->buffer
.curs1
)
2742 edit
->last_get_rule
--;
2744 p
= edit_buffer_backspace (&edit
->buffer
);
2746 edit_push_undo_action (edit
, p
);
2748 edit_modification (edit
);
2751 book_mark_dec (edit
, edit
->buffer
.curs_line
);
2752 edit
->buffer
.curs_line
--;
2753 edit
->buffer
.lines
--;
2754 edit
->force
|= REDRAW_AFTER_CURSOR
;
2757 if (edit
->buffer
.curs1
< edit
->start_display
)
2759 edit
->start_display
--;
2767 /* --------------------------------------------------------------------------------------------- */
2768 /** moves the cursor right or left: increment positive or negative respectively */
2771 edit_cursor_move (WEdit
*edit
, off_t increment
)
2775 for (; increment
< 0 && edit
->buffer
.curs1
!= 0; increment
++)
2779 edit_push_undo_action (edit
, CURS_RIGHT
);
2781 c
= edit_buffer_get_previous_byte (&edit
->buffer
);
2782 edit_buffer_insert_ahead (&edit
->buffer
, c
);
2783 c
= edit_buffer_backspace (&edit
->buffer
);
2786 edit
->buffer
.curs_line
--;
2787 edit
->force
|= REDRAW_LINE_BELOW
;
2793 for (; increment
> 0 && edit
->buffer
.curs2
!= 0; increment
--)
2797 edit_push_undo_action (edit
, CURS_LEFT
);
2799 c
= edit_buffer_get_current_byte (&edit
->buffer
);
2800 edit_buffer_insert (&edit
->buffer
, c
);
2801 c
= edit_buffer_delete (&edit
->buffer
);
2804 edit
->buffer
.curs_line
++;
2805 edit
->force
|= REDRAW_LINE_ABOVE
;
2811 /* --------------------------------------------------------------------------------------------- */
2812 /* If cols is zero this returns the count of columns from current to upto. */
2813 /* If upto is zero returns index of cols across from current. */
2816 edit_move_forward3 (const WEdit
*edit
, off_t current
, long cols
, off_t upto
)
2827 q
= edit
->buffer
.size
+ 2;
2829 for (col
= 0, p
= current
; p
< q
; p
++)
2841 orig_c
= c
= edit_buffer_get_byte (&edit
->buffer
, p
);
2847 int char_length
= 1;
2849 utf_ch
= edit_buffer_get_utf (&edit
->buffer
, p
, &char_length
);
2850 if (mc_global
.utf8_display
)
2852 if (char_length
> 1)
2853 col
-= char_length
- 1;
2854 if (g_unichar_iswide (utf_ch
))
2857 else if (char_length
> 1 && g_unichar_isprint (utf_ch
))
2858 col
-= char_length
- 1;
2861 c
= convert_to_display_c (c
);
2865 return (upto
!= 0 ? (off_t
) col
: p
);
2867 col
+= TAB_SIZE
- col
% TAB_SIZE
;
2868 else if ((c
< 32 || c
== 127) && (orig_c
== c
2870 || (!mc_global
.utf8_display
&& !edit
->utf8
)
2873 /* '\r' is shown as ^M, so we must advance 2 characters */
2874 /* Caret notation for control characters */
2882 /* --------------------------------------------------------------------------------------------- */
2883 /** returns the current offset of the cursor from the beginning of a file */
2886 edit_get_cursor_offset (const WEdit
*edit
)
2888 return edit
->buffer
.curs1
;
2891 /* --------------------------------------------------------------------------------------------- */
2892 /** returns the current column position of the cursor */
2895 edit_get_col (const WEdit
*edit
)
2899 b
= edit_buffer_get_current_bol (&edit
->buffer
);
2900 return (long) edit_move_forward3 (edit
, b
, 0, edit
->buffer
.curs1
);
2903 /* --------------------------------------------------------------------------------------------- */
2904 /* Scrolling functions */
2905 /* --------------------------------------------------------------------------------------------- */
2908 edit_update_curs_row (WEdit
*edit
)
2910 edit
->curs_row
= edit
->buffer
.curs_line
- edit
->start_line
;
2913 /* --------------------------------------------------------------------------------------------- */
2916 edit_update_curs_col (WEdit
*edit
)
2920 b
= edit_buffer_get_current_bol (&edit
->buffer
);
2921 edit
->curs_col
= (long) edit_move_forward3 (edit
, b
, 0, edit
->buffer
.curs1
);
2924 /* --------------------------------------------------------------------------------------------- */
2927 edit_get_curs_col (const WEdit
*edit
)
2929 return edit
->curs_col
;
2932 /* --------------------------------------------------------------------------------------------- */
2933 /** moves the display start position up by i lines */
2936 edit_scroll_upward (WEdit
*edit
, long i
)
2938 long lines_above
= edit
->start_line
;
2940 if (i
> lines_above
)
2944 edit
->start_line
-= i
;
2945 edit
->start_display
=
2946 edit_buffer_get_backward_offset (&edit
->buffer
, edit
->start_display
, i
);
2947 edit
->force
|= REDRAW_PAGE
;
2948 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2950 edit_update_curs_row (edit
);
2953 /* --------------------------------------------------------------------------------------------- */
2956 edit_scroll_downward (WEdit
*edit
, long i
)
2960 lines_below
= edit
->buffer
.lines
- edit
->start_line
- (WIDGET (edit
)->rect
.lines
- 1);
2961 if (lines_below
> 0)
2963 if (i
> lines_below
)
2965 edit
->start_line
+= i
;
2966 edit
->start_display
=
2967 edit_buffer_get_forward_offset (&edit
->buffer
, edit
->start_display
, i
, 0);
2968 edit
->force
|= REDRAW_PAGE
;
2969 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2971 edit_update_curs_row (edit
);
2974 /* --------------------------------------------------------------------------------------------- */
2977 edit_scroll_right (WEdit
*edit
, long i
)
2979 edit
->force
|= REDRAW_PAGE
;
2980 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2981 edit
->start_col
-= i
;
2984 /* --------------------------------------------------------------------------------------------- */
2987 edit_scroll_left (WEdit
*edit
, long i
)
2989 if (edit
->start_col
)
2991 edit
->start_col
+= i
;
2992 if (edit
->start_col
> 0)
2993 edit
->start_col
= 0;
2994 edit
->force
|= REDRAW_PAGE
;
2995 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2999 /* --------------------------------------------------------------------------------------------- */
3000 /* high level cursor movement commands */
3001 /* --------------------------------------------------------------------------------------------- */
3004 edit_move_to_prev_col (WEdit
*edit
, off_t p
)
3006 long prev
= edit
->prev_col
;
3007 long over
= edit
->over_col
;
3010 edit_cursor_move (edit
,
3011 edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->buffer
.curs1
);
3013 if (edit_options
.cursor_beyond_eol
)
3018 b
= edit_buffer_get_current_bol (&edit
->buffer
);
3019 e
= edit_buffer_get_current_eol (&edit
->buffer
);
3020 line_len
= (long) edit_move_forward3 (edit
, b
, 0, e
);
3021 if (line_len
< prev
+ edit
->over_col
)
3023 edit
->over_col
= prev
+ over
- line_len
;
3024 edit
->prev_col
= line_len
;
3025 edit
->curs_col
= line_len
;
3029 edit
->curs_col
= prev
+ over
;
3030 edit
->prev_col
= edit
->curs_col
;
3037 if (edit_options
.fake_half_tabs
&& is_in_indent (&edit
->buffer
))
3039 long fake_half_tabs
;
3041 edit_update_curs_col (edit
);
3043 fake_half_tabs
= HALF_TAB_SIZE
* space_width
;
3044 if (fake_half_tabs
!= 0 && edit
->curs_col
% fake_half_tabs
!= 0)
3049 edit
->curs_col
-= (edit
->curs_col
% fake_half_tabs
);
3050 p
= edit_buffer_get_current_bol (&edit
->buffer
);
3051 b
= edit_move_forward3 (edit
, p
, edit
->curs_col
, 0);
3052 edit_cursor_move (edit
, b
- edit
->buffer
.curs1
);
3053 if (!left_of_four_spaces (edit
))
3055 b
= edit_move_forward3 (edit
, p
, q
, 0);
3056 edit_cursor_move (edit
, b
- edit
->buffer
.curs1
);
3063 /* --------------------------------------------------------------------------------------------- */
3064 /** check whether line in editor is blank or not
3066 * @param edit editor object
3067 * @param line number of line
3069 * @return TRUE if line in blank, FALSE otherwise
3073 edit_line_is_blank (WEdit
*edit
, long line
)
3075 return is_blank (&edit
->buffer
, edit_find_line (edit
, line
));
3078 /* --------------------------------------------------------------------------------------------- */
3079 /** move cursor to line 'line' */
3082 edit_move_to_line (WEdit
*e
, long line
)
3084 if (line
< e
->buffer
.curs_line
)
3085 edit_move_up (e
, e
->buffer
.curs_line
- line
, FALSE
);
3087 edit_move_down (e
, line
- e
->buffer
.curs_line
, FALSE
);
3088 edit_scroll_screen_over_cursor (e
);
3091 /* --------------------------------------------------------------------------------------------- */
3092 /** scroll window so that first visible line is 'line' */
3095 edit_move_display (WEdit
*e
, long line
)
3097 if (line
< e
->start_line
)
3098 edit_scroll_upward (e
, e
->start_line
- line
);
3100 edit_scroll_downward (e
, line
- e
->start_line
);
3103 /* --------------------------------------------------------------------------------------------- */
3104 /** save markers onto undo stack */
3107 edit_push_markers (WEdit
*edit
)
3109 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3110 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3111 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3114 /* --------------------------------------------------------------------------------------------- */
3117 edit_set_markers (WEdit
*edit
, off_t m1
, off_t m2
, long c1
, long c2
)
3125 /* --------------------------------------------------------------------------------------------- */
3127 if mark2 is -1 then marking is from mark1 to the cursor.
3128 Otherwise its between the markers. This handles this.
3129 Returns FALSE if no text is marked.
3133 eval_marks (WEdit
*edit
, off_t
*start_mark
, off_t
*end_mark
)
3137 if (edit
->mark1
== edit
->mark2
)
3139 *start_mark
= *end_mark
= 0;
3140 edit
->column2
= edit
->column1
= 0;
3144 if (edit
->end_mark_curs
< 0)
3145 end_mark_curs
= edit
->buffer
.curs1
;
3147 end_mark_curs
= edit
->end_mark_curs
;
3149 if (edit
->mark2
>= 0)
3151 *start_mark
= MIN (edit
->mark1
, edit
->mark2
);
3152 *end_mark
= MAX (edit
->mark1
, edit
->mark2
);
3156 *start_mark
= MIN (edit
->mark1
, end_mark_curs
);
3157 *end_mark
= MAX (edit
->mark1
, end_mark_curs
);
3158 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
3161 if (edit
->column_highlight
3162 && ((edit
->mark1
> end_mark_curs
&& edit
->column1
< edit
->column2
)
3163 || (edit
->mark1
< end_mark_curs
&& edit
->column1
> edit
->column2
)))
3165 off_t start_bol
, start_eol
;
3166 off_t end_bol
, end_eol
;
3170 start_bol
= edit_buffer_get_bol (&edit
->buffer
, *start_mark
);
3171 start_eol
= edit_buffer_get_eol (&edit
->buffer
, start_bol
- 1) + 1;
3172 end_bol
= edit_buffer_get_bol (&edit
->buffer
, *end_mark
);
3173 end_eol
= edit_buffer_get_eol (&edit
->buffer
, *end_mark
);
3174 col1
= MIN (edit
->column1
, edit
->column2
);
3175 col2
= MAX (edit
->column1
, edit
->column2
);
3177 diff1
= edit_move_forward3 (edit
, start_bol
, col2
, 0) -
3178 edit_move_forward3 (edit
, start_bol
, col1
, 0);
3179 diff2
= edit_move_forward3 (edit
, end_bol
, col2
, 0) -
3180 edit_move_forward3 (edit
, end_bol
, col1
, 0);
3182 *start_mark
-= diff1
;
3184 *start_mark
= MAX (*start_mark
, start_eol
);
3185 *end_mark
= MIN (*end_mark
, end_eol
);
3191 /* --------------------------------------------------------------------------------------------- */
3192 /** highlight marker toggle */
3195 edit_mark_cmd (WEdit
*edit
, gboolean unmark
)
3197 edit_push_markers (edit
);
3200 edit_set_markers (edit
, 0, 0, 0, 0);
3201 edit
->force
|= REDRAW_PAGE
;
3203 else if (edit
->mark2
>= 0)
3205 edit
->end_mark_curs
= -1;
3206 edit_set_markers (edit
, edit
->buffer
.curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3207 edit
->curs_col
+ edit
->over_col
);
3208 edit
->force
|= REDRAW_PAGE
;
3212 edit
->end_mark_curs
= edit
->buffer
.curs1
;
3213 edit_set_markers (edit
, edit
->mark1
, edit
->buffer
.curs1
, edit
->column1
,
3214 edit
->curs_col
+ edit
->over_col
);
3218 /* --------------------------------------------------------------------------------------------- */
3219 /** highlight the word under cursor */
3222 edit_mark_current_word_cmd (WEdit
*edit
)
3226 for (pos
= edit
->buffer
.curs1
; pos
!= 0; pos
--)
3230 c1
= edit_buffer_get_byte (&edit
->buffer
, pos
);
3231 c2
= edit_buffer_get_byte (&edit
->buffer
, pos
- 1);
3232 if (!isspace (c1
) && isspace (c2
))
3234 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3239 for (; pos
< edit
->buffer
.size
; pos
++)
3243 c1
= edit_buffer_get_byte (&edit
->buffer
, pos
);
3244 c2
= edit_buffer_get_byte (&edit
->buffer
, pos
+ 1);
3245 if (!isspace (c1
) && isspace (c2
))
3247 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3250 edit
->mark2
= MIN (pos
+ 1, edit
->buffer
.size
);
3252 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3255 /* --------------------------------------------------------------------------------------------- */
3258 edit_mark_current_line_cmd (WEdit
*edit
)
3260 edit
->mark1
= edit_buffer_get_current_bol (&edit
->buffer
);
3261 edit
->mark2
= edit_buffer_get_current_eol (&edit
->buffer
);
3263 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3266 /* --------------------------------------------------------------------------------------------- */
3269 edit_delete_line (WEdit
*edit
)
3272 * Delete right part of the line.
3273 * Note that edit_buffer_get_byte() returns '\n' when byte position is
3276 while (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
3277 (void) edit_delete (edit
, TRUE
);
3281 * Note that edit_delete() will not corrupt anything if called while
3282 * cursor position is EOF.
3284 (void) edit_delete (edit
, TRUE
);
3287 * Delete left part of the line.
3288 * Note, that edit_buffer_get_byte() returns '\n' when byte position is < 0.
3290 while (edit_buffer_get_previous_byte (&edit
->buffer
) != '\n')
3291 (void) edit_backspace (edit
, TRUE
);
3294 /* --------------------------------------------------------------------------------------------- */
3297 edit_push_key_press (WEdit
*edit
)
3299 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3300 if (edit
->mark2
== -1)
3302 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3303 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3307 /* --------------------------------------------------------------------------------------------- */
3310 edit_find_bracket (WEdit
*edit
)
3312 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3313 if (edit
->last_bracket
!= edit
->bracket
)
3314 edit
->force
|= REDRAW_PAGE
;
3315 edit
->last_bracket
= edit
->bracket
;
3318 /* --------------------------------------------------------------------------------------------- */
3320 * This executes a command as though the user initiated it through a key
3321 * press. Callback with MSG_KEY as a message calls this after
3322 * translating the key press. This function can be used to pass any
3323 * command to the editor. Note that the screen wouldn't update
3324 * automatically. Either of command or char_for_insertion must be
3325 * passed as -1. Commands are executed, and char_for_insertion is
3326 * inserted at the cursor.
3330 edit_execute_key_command (WEdit
*edit
, long command
, int char_for_insertion
)
3332 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3334 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3337 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3340 if (macro_index
!= -1)
3342 edit
->force
|= REDRAW_COMPLETELY
;
3343 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3345 edit_store_macro_cmd (edit
);
3349 if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3351 edit_repeat_macro_cmd (edit
);
3357 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3359 record_macro_buf
[macro_index
].action
= command
;
3360 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3362 /* record the beginning of a set of editing actions initiated by a key press */
3363 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3364 edit_push_key_press (edit
);
3366 edit_execute_cmd (edit
, command
, char_for_insertion
);
3367 if (edit
->column_highlight
)
3368 edit
->force
|= REDRAW_PAGE
;
3371 /* --------------------------------------------------------------------------------------------- */
3373 This executes a command at a lower level than macro recording.
3374 It also does not push a key_press onto the undo stack. This means
3375 that if it is called many times, a single undo command will undo
3376 all of them. It also does not check for the Undo command.
3379 edit_execute_cmd (WEdit
*edit
, long command
, int char_for_insertion
)
3381 WRect
*w
= &WIDGET (edit
)->rect
;
3383 if (command
== CK_WindowFullscreen
)
3385 edit_toggle_fullscreen (edit
);
3389 /* handle window state */
3390 if (edit_handle_move_resize (edit
, command
))
3393 edit
->force
|= REDRAW_LINE
;
3395 /* The next key press will unhighlight the found string, so update
3397 if (edit
->found_len
|| edit
->column_highlight
)
3398 edit
->force
|= REDRAW_PAGE
;
3402 /* a mark command with shift-arrow */
3405 case CK_MarkToWordBegin
:
3406 case CK_MarkToWordEnd
:
3412 case CK_MarkPageDown
:
3413 case CK_MarkToFileBegin
:
3414 case CK_MarkToFileEnd
:
3415 case CK_MarkToPageBegin
:
3416 case CK_MarkToPageEnd
:
3417 case CK_MarkScrollUp
:
3418 case CK_MarkScrollDown
:
3419 case CK_MarkParagraphUp
:
3420 case CK_MarkParagraphDown
:
3421 /* a mark command with alt-arrow */
3422 case CK_MarkColumnPageUp
:
3423 case CK_MarkColumnPageDown
:
3424 case CK_MarkColumnLeft
:
3425 case CK_MarkColumnRight
:
3426 case CK_MarkColumnUp
:
3427 case CK_MarkColumnDown
:
3428 case CK_MarkColumnScrollUp
:
3429 case CK_MarkColumnScrollDown
:
3430 case CK_MarkColumnParagraphUp
:
3431 case CK_MarkColumnParagraphDown
:
3432 edit
->column_highlight
= 0;
3433 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3435 edit_mark_cmd (edit
, TRUE
); /* clear */
3436 edit_mark_cmd (edit
, FALSE
); /* marking on */
3438 edit
->highlight
= 1;
3441 /* any other command */
3443 if (edit
->highlight
)
3444 edit_mark_cmd (edit
, FALSE
); /* clear */
3445 edit
->highlight
= 0;
3448 /* first check for undo */
3449 if (command
== CK_Undo
)
3451 edit
->redo_stack_reset
= 0;
3452 edit_group_undo (edit
);
3453 edit
->found_len
= 0;
3454 edit
->prev_col
= edit_get_col (edit
);
3455 edit
->search_start
= edit
->buffer
.curs1
;
3458 /* check for redo */
3459 if (command
== CK_Redo
)
3461 edit
->redo_stack_reset
= 0;
3462 edit_do_redo (edit
);
3463 edit
->found_len
= 0;
3464 edit
->prev_col
= edit_get_col (edit
);
3465 edit
->search_start
= edit
->buffer
.curs1
;
3469 edit
->redo_stack_reset
= 1;
3471 /* An ordinary key press */
3472 if (char_for_insertion
>= 0)
3474 /* if non persistent selection and text selected */
3475 if (!edit_options
.persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3476 edit_block_delete_cmd (edit
);
3478 if (edit
->overwrite
)
3480 /* remove char only one time, after input first byte, multibyte chars */
3482 if (!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3484 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
3485 edit_delete (edit
, FALSE
);
3487 if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
3488 edit_insert_over (edit
);
3491 Encode 8-bit input as UTF-8, if display (locale) is *not* UTF-8,
3492 *but* source encoding *is* set to UTF-8; see ticket #3843 for the details.
3494 if (char_for_insertion
> 127 && str_isutf8 (get_codepage_id (mc_global
.source_codepage
))
3495 && !mc_global
.utf8_display
)
3497 unsigned char str
[UTF8_CHAR_LEN
+ 1];
3501 res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3510 for (i
= 0; i
<= UTF8_CHAR_LEN
&& str
[i
] != '\0'; i
++)
3512 char_for_insertion
= str
[i
];
3513 edit_insert (edit
, char_for_insertion
);
3518 edit_insert (edit
, char_for_insertion
);
3520 if (edit_options
.auto_para_formatting
)
3522 format_paragraph (edit
, FALSE
);
3523 edit
->force
|= REDRAW_PAGE
;
3526 check_and_wrap_line (edit
);
3527 edit
->found_len
= 0;
3528 edit
->prev_col
= edit_get_col (edit
);
3529 edit
->search_start
= edit
->buffer
.curs1
;
3530 edit_find_bracket (edit
);
3536 case CK_TopOnScreen
:
3537 case CK_BottomOnScreen
:
3550 if (!edit_options
.persistent_selections
&& edit
->mark2
>= 0)
3552 if (edit
->column_highlight
)
3553 edit_push_undo_action (edit
, COLUMN_ON
);
3554 edit
->column_highlight
= 0;
3555 edit_mark_cmd (edit
, TRUE
);
3564 case CK_TopOnScreen
:
3565 case CK_BottomOnScreen
:
3566 case CK_MarkToPageBegin
:
3567 case CK_MarkToPageEnd
:
3572 case CK_MarkToWordBegin
:
3573 case CK_MarkToWordEnd
:
3576 case CK_MarkColumnUp
:
3577 case CK_MarkColumnDown
:
3578 if (edit
->mark2
== -1)
3579 break; /*marking is following the cursor: may need to highlight a whole line */
3585 edit
->force
|= REDRAW_CHAR_ONLY
;
3591 /* basic cursor key commands */
3595 /* if non persistent selection and text selected */
3596 if (!edit_options
.persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3597 edit_block_delete_cmd (edit
);
3598 else if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
3600 else if (edit_options
.backspace_through_tabs
&& is_in_indent (&edit
->buffer
))
3602 while (edit_buffer_get_previous_byte (&edit
->buffer
) != '\n' && edit
->buffer
.curs1
> 0)
3603 edit_backspace (edit
, TRUE
);
3605 else if (edit_options
.fake_half_tabs
&& is_in_indent (&edit
->buffer
)
3606 && right_of_four_spaces (edit
))
3610 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3611 edit_backspace (edit
, TRUE
);
3614 edit_backspace (edit
, FALSE
);
3617 /* if non persistent selection and text selected */
3618 if (!edit_options
.persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3619 edit_block_delete_cmd (edit
);
3622 if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
3623 edit_insert_over (edit
);
3625 if (edit_options
.fake_half_tabs
&& is_in_indent (&edit
->buffer
)
3626 && left_of_four_spaces (edit
))
3630 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3631 edit_delete (edit
, TRUE
);
3634 edit_delete (edit
, FALSE
);
3637 case CK_DeleteToWordBegin
:
3639 edit_left_delete_word (edit
);
3641 case CK_DeleteToWordEnd
:
3642 if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
3643 edit_insert_over (edit
);
3645 edit_right_delete_word (edit
);
3648 edit_delete_line (edit
);
3650 case CK_DeleteToHome
:
3651 edit_delete_to_line_begin (edit
);
3653 case CK_DeleteToEnd
:
3654 edit_delete_to_line_end (edit
);
3658 if (edit_options
.auto_para_formatting
)
3660 edit_double_newline (edit
);
3661 if (edit_options
.return_does_auto_indent
&& !bracketed_pasting_in_progress
)
3662 edit_auto_indent (edit
);
3663 format_paragraph (edit
, FALSE
);
3667 edit_insert (edit
, '\n');
3668 if (edit_options
.return_does_auto_indent
&& !bracketed_pasting_in_progress
)
3669 edit_auto_indent (edit
);
3673 edit_insert (edit
, '\n');
3676 case CK_MarkColumnPageUp
:
3677 edit
->column_highlight
= 1;
3681 edit_move_up (edit
, w
->lines
- 1, TRUE
);
3683 case CK_MarkColumnPageDown
:
3684 edit
->column_highlight
= 1;
3687 case CK_MarkPageDown
:
3688 edit_move_down (edit
, w
->lines
- 1, TRUE
);
3690 case CK_MarkColumnLeft
:
3691 edit
->column_highlight
= 1;
3695 if (edit_options
.fake_half_tabs
&& is_in_indent (&edit
->buffer
)
3696 && right_of_four_spaces (edit
))
3698 if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
3701 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3702 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3705 edit_left_char_move_cmd (edit
);
3707 case CK_MarkColumnRight
:
3708 edit
->column_highlight
= 1;
3712 if (edit_options
.fake_half_tabs
&& is_in_indent (&edit
->buffer
)
3713 && left_of_four_spaces (edit
))
3715 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3716 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3719 edit_right_char_move_cmd (edit
);
3721 case CK_TopOnScreen
:
3722 case CK_MarkToPageBegin
:
3723 edit_begin_page (edit
);
3725 case CK_BottomOnScreen
:
3726 case CK_MarkToPageEnd
:
3727 edit_end_page (edit
);
3730 case CK_MarkToWordBegin
:
3732 edit_left_word_move_cmd (edit
);
3735 case CK_MarkToWordEnd
:
3737 edit_right_word_move_cmd (edit
);
3739 case CK_MarkColumnUp
:
3740 edit
->column_highlight
= 1;
3744 edit_move_up (edit
, 1, FALSE
);
3746 case CK_MarkColumnDown
:
3747 edit
->column_highlight
= 1;
3751 edit_move_down (edit
, 1, FALSE
);
3753 case CK_MarkColumnParagraphUp
:
3754 edit
->column_highlight
= 1;
3756 case CK_ParagraphUp
:
3757 case CK_MarkParagraphUp
:
3758 edit_move_up_paragraph (edit
, FALSE
);
3760 case CK_MarkColumnParagraphDown
:
3761 edit
->column_highlight
= 1;
3763 case CK_ParagraphDown
:
3764 case CK_MarkParagraphDown
:
3765 edit_move_down_paragraph (edit
, FALSE
);
3767 case CK_MarkColumnScrollUp
:
3768 edit
->column_highlight
= 1;
3771 case CK_MarkScrollUp
:
3772 edit_move_up (edit
, 1, TRUE
);
3774 case CK_MarkColumnScrollDown
:
3775 edit
->column_highlight
= 1;
3778 case CK_MarkScrollDown
:
3779 edit_move_down (edit
, 1, TRUE
);
3783 edit_cursor_to_bol (edit
);
3787 edit_cursor_to_eol (edit
);
3790 /* if text marked shift block */
3791 if (edit
->mark1
!= edit
->mark2
&& !edit_options
.persistent_selections
)
3793 if (edit
->mark2
< 0)
3794 edit_mark_cmd (edit
, FALSE
);
3795 edit_move_block_to_right (edit
);
3799 if (edit_options
.cursor_beyond_eol
)
3800 edit_insert_over (edit
);
3801 edit_tab_cmd (edit
);
3802 if (edit_options
.auto_para_formatting
)
3804 format_paragraph (edit
, FALSE
);
3805 edit
->force
|= REDRAW_PAGE
;
3808 check_and_wrap_line (edit
);
3812 case CK_InsertOverwrite
:
3813 edit
->overwrite
= !edit
->overwrite
;
3817 if (edit
->mark2
>= 0)
3819 if (edit
->column_highlight
)
3820 edit_push_undo_action (edit
, COLUMN_ON
);
3821 edit
->column_highlight
= 0;
3823 edit_mark_cmd (edit
, FALSE
);
3826 if (!edit
->column_highlight
)
3827 edit_push_undo_action (edit
, COLUMN_OFF
);
3828 edit
->column_highlight
= 1;
3829 edit_mark_cmd (edit
, FALSE
);
3832 edit_set_markers (edit
, 0, edit
->buffer
.size
, 0, 0);
3833 edit
->force
|= REDRAW_PAGE
;
3836 if (edit
->column_highlight
)
3837 edit_push_undo_action (edit
, COLUMN_ON
);
3838 edit
->column_highlight
= 0;
3839 edit_mark_cmd (edit
, TRUE
);
3842 if (edit
->column_highlight
)
3843 edit_push_undo_action (edit
, COLUMN_ON
);
3844 edit
->column_highlight
= 0;
3845 edit_mark_current_word_cmd (edit
);
3848 if (edit
->column_highlight
)
3849 edit_push_undo_action (edit
, COLUMN_ON
);
3850 edit
->column_highlight
= 0;
3851 edit_mark_current_line_cmd (edit
);
3855 book_mark_clear (edit
, edit
->buffer
.curs_line
, BOOK_MARK_FOUND_COLOR
);
3856 if (book_mark_query_color (edit
, edit
->buffer
.curs_line
, BOOK_MARK_COLOR
))
3857 book_mark_clear (edit
, edit
->buffer
.curs_line
, BOOK_MARK_COLOR
);
3859 book_mark_insert (edit
, edit
->buffer
.curs_line
, BOOK_MARK_COLOR
);
3861 case CK_BookmarkFlush
:
3862 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3863 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3864 edit
->force
|= REDRAW_PAGE
;
3866 case CK_BookmarkNext
:
3867 if (edit
->book_mark
!= NULL
)
3869 edit_book_mark_t
*p
;
3871 p
= book_mark_find (edit
, edit
->buffer
.curs_line
);
3872 if (p
->next
!= NULL
)
3875 if (p
->line
>= edit
->start_line
+ w
->lines
|| p
->line
< edit
->start_line
)
3876 edit_move_display (edit
, p
->line
- w
->lines
/ 2);
3877 edit_move_to_line (edit
, p
->line
);
3881 case CK_BookmarkPrev
:
3882 if (edit
->book_mark
!= NULL
)
3884 edit_book_mark_t
*p
;
3886 p
= book_mark_find (edit
, edit
->buffer
.curs_line
);
3887 while (p
->line
== edit
->buffer
.curs_line
)
3888 if (p
->prev
!= NULL
)
3892 if (p
->line
>= edit
->start_line
+ w
->lines
|| p
->line
< edit
->start_line
)
3893 edit_move_display (edit
, p
->line
- w
->lines
/ 2);
3894 edit_move_to_line (edit
, p
->line
);
3900 case CK_MarkToFileBegin
:
3901 edit_move_to_top (edit
);
3904 case CK_MarkToFileEnd
:
3905 edit_move_to_bottom (edit
);
3909 if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
3910 edit_insert_over (edit
);
3911 edit_block_copy_cmd (edit
);
3914 edit_block_delete_cmd (edit
);
3917 edit_block_move_cmd (edit
);
3920 case CK_BlockShiftLeft
:
3921 if (edit
->mark1
!= edit
->mark2
)
3922 edit_move_block_to_left (edit
);
3924 case CK_BlockShiftRight
:
3925 if (edit
->mark1
!= edit
->mark2
)
3926 edit_move_block_to_right (edit
);
3929 edit_copy_to_X_buf_cmd (edit
);
3932 edit_cut_to_X_buf_cmd (edit
);
3935 /* if non persistent selection and text selected */
3936 if (!edit_options
.persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3937 edit_block_delete_cmd (edit
);
3938 if (edit_options
.cursor_beyond_eol
&& edit
->over_col
> 0)
3939 edit_insert_over (edit
);
3940 edit_paste_from_X_buf_cmd (edit
);
3941 if (!edit_options
.persistent_selections
&& edit
->mark2
>= 0)
3943 if (edit
->column_highlight
)
3944 edit_push_undo_action (edit
, COLUMN_ON
);
3945 edit
->column_highlight
= 0;
3946 edit_mark_cmd (edit
, TRUE
);
3950 edit_paste_from_history (edit
);
3954 edit_save_as_cmd (edit
);
3957 edit_save_confirm_cmd (edit
);
3960 edit_save_block_cmd (edit
);
3963 edit_insert_file_cmd (edit
);
3967 edit_load_back_cmd (edit
);
3970 edit_load_forward_cmd (edit
);
3973 case CK_SyntaxChoose
:
3974 edit_syntax_dialog (edit
);
3978 edit_search_cmd (edit
, FALSE
);
3980 case CK_SearchContinue
:
3981 edit_search_cmd (edit
, TRUE
);
3984 edit_replace_cmd (edit
, FALSE
);
3986 case CK_ReplaceContinue
:
3987 edit_replace_cmd (edit
, TRUE
);
3990 /* if text marked shift block */
3991 if (edit
->mark1
!= edit
->mark2
&& !edit_options
.persistent_selections
)
3992 edit_move_block_to_left (edit
);
3994 edit_complete_word_cmd (edit
);
3997 edit_get_match_keyword_cmd (edit
);
4001 case CK_SpellCheckCurrentWord
:
4002 edit_suggest_current_word (edit
);
4005 edit_spellcheck_file (edit
);
4007 case CK_SpellCheckSelectLang
:
4008 edit_set_spell_lang ();
4015 /* fool gcc to prevent a Y2K warning */
4016 char time_format
[] = "_c";
4017 time_format
[0] = '%';
4019 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4020 edit_print_string (edit
, s
);
4021 edit
->force
|= REDRAW_PAGE
;
4025 edit_goto_cmd (edit
);
4027 case CK_ParagraphFormat
:
4028 format_paragraph (edit
, TRUE
);
4029 edit
->force
|= REDRAW_PAGE
;
4031 case CK_MacroDelete
:
4032 edit_delete_macro_cmd (edit
);
4034 case CK_MatchBracket
:
4035 edit_goto_matching_bracket (edit
);
4038 edit_user_menu (edit
, NULL
, -1);
4041 edit_sort_cmd (edit
);
4043 case CK_ExternalCommand
:
4044 edit_ext_cmd (edit
);
4047 edit_mail_dialog (edit
);
4050 case CK_SelectCodepage
:
4051 edit_select_codepage_cmd (edit
);
4054 case CK_InsertLiteral
:
4055 edit_insert_literal_cmd (edit
);
4057 case CK_MacroStartStopRecord
:
4058 edit_begin_end_macro_cmd (edit
);
4060 case CK_RepeatStartStopRecord
:
4061 edit_begin_end_repeat_cmd (edit
);
4063 case CK_ExtendedKeyMap
:
4064 WIDGET (edit
)->ext_mode
= TRUE
;
4071 if ((command
/ CK_PipeBlock (0)) == 1)
4072 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
4074 /* keys which must set the col position, and the search vars */
4078 case CK_SearchContinue
:
4080 case CK_ReplaceContinue
:
4082 edit
->prev_col
= edit_get_col (edit
);
4086 case CK_MarkColumnUp
:
4089 case CK_MarkColumnDown
:
4092 case CK_MarkColumnPageUp
:
4094 case CK_MarkPageDown
:
4095 case CK_MarkColumnPageDown
:
4097 case CK_MarkToFileBegin
:
4099 case CK_MarkToFileEnd
:
4100 case CK_ParagraphUp
:
4101 case CK_MarkParagraphUp
:
4102 case CK_MarkColumnParagraphUp
:
4103 case CK_ParagraphDown
:
4104 case CK_MarkParagraphDown
:
4105 case CK_MarkColumnParagraphDown
:
4107 case CK_MarkScrollUp
:
4108 case CK_MarkColumnScrollUp
:
4110 case CK_MarkScrollDown
:
4111 case CK_MarkColumnScrollDown
:
4112 edit
->search_start
= edit
->buffer
.curs1
;
4113 edit
->found_len
= 0;
4116 edit
->found_len
= 0;
4117 edit
->prev_col
= edit_get_col (edit
);
4118 edit
->search_start
= edit
->buffer
.curs1
;
4120 edit_find_bracket (edit
);
4122 if (edit_options
.auto_para_formatting
)
4128 case CK_DeleteToWordBegin
:
4129 case CK_DeleteToWordEnd
:
4130 case CK_DeleteToHome
:
4131 case CK_DeleteToEnd
:
4132 format_paragraph (edit
, FALSE
);
4133 edit
->force
|= REDRAW_PAGE
;
4141 /* --------------------------------------------------------------------------------------------- */
4144 edit_stack_init (void)
4146 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4147 edit_arg_init (&edit_history_moveto
[edit_stack_iterator
], NULL
, -1);
4149 edit_stack_iterator
= 0;
4152 /* --------------------------------------------------------------------------------------------- */
4155 edit_stack_free (void)
4157 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4158 vfs_path_free (edit_history_moveto
[edit_stack_iterator
].file_vpath
, TRUE
);
4161 /* --------------------------------------------------------------------------------------------- */
4165 edit_move_up (WEdit
*edit
, long i
, gboolean do_scroll
)
4167 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4170 /* --------------------------------------------------------------------------------------------- */
4174 edit_move_down (WEdit
*edit
, long i
, gboolean do_scroll
)
4176 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4179 /* --------------------------------------------------------------------------------------------- */
4181 * Create edit_arg_t object from vfs_path_t object and the line number.
4183 * @param file_vpath file path object
4184 * @param line_number line number. If value is 0, try to restore saved position.
4185 * @return edit_arg_t object
4189 edit_arg_vpath_new (vfs_path_t
*file_vpath
, long line_number
)
4193 arg
= g_new (edit_arg_t
, 1);
4194 arg
->file_vpath
= file_vpath
;
4195 arg
->line_number
= line_number
;
4200 /* --------------------------------------------------------------------------------------------- */
4202 * Create edit_arg_t object from file name and the line number.
4204 * @param file_name file name
4205 * @param line_number line number. If value is 0, try to restore saved position.
4206 * @return edit_arg_t object
4210 edit_arg_new (const char *file_name
, long line_number
)
4212 return edit_arg_vpath_new (vfs_path_from_str (file_name
), line_number
);
4215 /* --------------------------------------------------------------------------------------------- */
4217 * Initialize edit_arg_t object.
4219 * @param arg edit_arg_t object
4220 * @param vpath vfs_path_t object
4221 * @param line line number
4225 edit_arg_init (edit_arg_t
*arg
, vfs_path_t
*vpath
, long line
)
4227 arg
->file_vpath
= (vfs_path_t
*) vpath
;
4228 arg
->line_number
= line
;
4231 /* --------------------------------------------------------------------------------------------- */
4233 * Apply new values to edit_arg_t object members.
4235 * @param arg edit_arg_t object
4236 * @param vpath vfs_path_t object
4237 * @param line line number
4241 edit_arg_assign (edit_arg_t
*arg
, vfs_path_t
*vpath
, long line
)
4243 vfs_path_free (arg
->file_vpath
, TRUE
);
4244 edit_arg_init (arg
, vpath
, line
);
4247 /* --------------------------------------------------------------------------------------------- */
4249 * Free the edit_arg_t object.
4251 * @param arg edit_arg_t object
4255 edit_arg_free (edit_arg_t
*arg
)
4257 vfs_path_free (arg
->file_vpath
, TRUE
);
4261 /* --------------------------------------------------------------------------------------------- */
4264 edit_get_file_name (const WEdit
*edit
)
4266 return vfs_path_as_str (edit
->filename_vpath
);
4269 /* --------------------------------------------------------------------------------------------- */