2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996-2017
5 Free Software Foundation, Inc.
9 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
10 Andrew Borodin <aborodin@vmail.ru> 2012, 2013
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>
43 #include <stdint.h> /* UINTMAX_MAX */
46 #include "lib/global.h"
48 #include "lib/tty/color.h"
49 #include "lib/tty/tty.h" /* attrset() */
50 #include "lib/tty/key.h" /* is_idle() */
51 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
52 #include "lib/fileloc.h" /* EDIT_BLOCK_FILE */
53 #include "lib/vfs/vfs.h"
54 #include "lib/strutil.h" /* utf string functions */
55 #include "lib/util.h" /* load_file_position(), save_file_position() */
56 #include "lib/timefmt.h" /* time formatting */
58 #include "lib/widget.h"
61 #include "lib/charsets.h" /* get_codepage_id */
64 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
66 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/learn.h" /* learn_keys */
68 #include "src/keybind-defaults.h"
70 #include "edit-impl.h"
71 #include "editwidget.h"
76 /*** global variables ****************************************************************************/
78 int option_word_wrap_line_length
= DEFAULT_WRAP_LINE_LENGTH
;
79 gboolean option_typewriter_wrap
= FALSE
;
80 gboolean option_auto_para_formatting
= FALSE
;
81 gboolean option_fill_tabs_with_spaces
= FALSE
;
82 gboolean option_return_does_auto_indent
= TRUE
;
83 gboolean option_backspace_through_tabs
= FALSE
;
84 gboolean option_fake_half_tabs
= TRUE
;
85 int option_save_mode
= EDIT_QUICK_SAVE
;
86 gboolean option_save_position
= TRUE
;
87 int option_max_undo
= 32768;
88 gboolean option_persistent_selections
= TRUE
;
89 gboolean option_cursor_beyond_eol
= FALSE
;
90 gboolean option_line_state
= FALSE
;
91 int option_line_state_width
= 0;
92 gboolean option_cursor_after_inserted_block
= FALSE
;
93 gboolean option_state_full_filename
= FALSE
;
95 gboolean enable_show_tabs_tws
= TRUE
;
96 gboolean option_check_nl_at_eof
= FALSE
;
97 gboolean option_group_undo
= FALSE
;
98 gboolean show_right_margin
= FALSE
;
100 char *option_backup_ext
= NULL
;
101 char *option_filesize_threshold
= NULL
;
103 unsigned int edit_stack_iterator
= 0;
104 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
105 /* magic sequense for say than block is vertical */
106 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
108 /*** file scope macro definitions ****************************************************************/
110 #define TEMP_BUF_LEN 1024
112 #define space_width 1
114 /*** file scope type declarations ****************************************************************/
116 /*** file scope variables ************************************************************************/
118 /* detecting an error on save is easy: just check if every byte has been written. */
119 /* detecting an error on read, is not so easy 'cos there is not way to tell
120 whether you read everything or not. */
121 /* FIXME: add proper 'triple_pipe_open' to read, write and check errors. */
122 static const struct edit_filters
124 const char *read
, *write
, *extension
;
128 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
129 { "lz4 -cd %s 2>&1", "lz4 > %s", ".lz4" },
130 { "lzip -cd %s 2>&1", "lzip > %s", ".lz"},
131 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
132 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
133 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
134 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
138 static const off_t option_filesize_default_threshold
= 64 * 1024 * 1024; /* 64 MB */
140 /* --------------------------------------------------------------------------------------------- */
141 /*** file scope functions ************************************************************************/
142 /* --------------------------------------------------------------------------------------------- */
145 edit_load_status_update_cb (status_msg_t
* sm
)
147 simple_status_msg_t
*ssm
= SIMPLE_STATUS_MSG (sm
);
148 edit_buffer_read_file_status_msg_t
*rsm
= (edit_buffer_read_file_status_msg_t
*) sm
;
149 Widget
*wd
= WIDGET (sm
->dlg
);
152 label_set_textv (ssm
->label
, _("Loading: %3d%%"),
153 edit_buffer_calc_percent (rsm
->buf
, rsm
->loaded
));
155 label_set_text (ssm
->label
, _("Loading..."));
160 Widget
*lw
= WIDGET (ssm
->label
);
162 wd_width
= MAX (wd
->cols
, lw
->cols
+ 6);
163 widget_set_size (wd
, wd
->y
, wd
->x
, wd
->lines
, wd_width
);
164 widget_set_size (lw
, lw
->y
, wd
->x
+ (wd
->cols
- lw
->cols
) / 2, lw
->lines
, lw
->cols
);
168 return status_msg_common_update (sm
);
171 /* --------------------------------------------------------------------------------------------- */
173 * Load file OR text into buffers. Set cursor to the beginning of file.
175 * @return FALSE on error.
179 edit_load_file_fast (edit_buffer_t
* buf
, const vfs_path_t
* filename_vpath
)
183 edit_buffer_read_file_status_msg_t rsm
;
186 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
192 g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath
));
193 edit_error_dialog (_("Error"), errmsg
);
202 status_msg_init (STATUS_MSG (&rsm
), _("Load file"), 1.0, simple_status_msg_init_cb
,
203 edit_load_status_update_cb
, NULL
);
205 ret
= (edit_buffer_read_file (buf
, file
, buf
->size
, &rsm
, &aborted
) == buf
->size
);
207 status_msg_deinit (STATUS_MSG (&rsm
));
209 if (!ret
&& !aborted
)
213 errmsg
= g_strdup_printf (_("Error reading %s"), vfs_path_as_str (filename_vpath
));
214 edit_error_dialog (_("Error"), errmsg
);
222 /* --------------------------------------------------------------------------------------------- */
223 /** Return index of the filter or -1 is there is no appropriate filter */
226 edit_find_filter (const vfs_path_t
* filename_vpath
)
230 if (filename_vpath
== NULL
)
233 l
= strlen (vfs_path_as_str (filename_vpath
));
234 for (i
= 0; i
< G_N_ELEMENTS (all_filters
); i
++)
238 e
= strlen (all_filters
[i
].extension
);
240 if (!strcmp (all_filters
[i
].extension
, vfs_path_as_str (filename_vpath
) + l
- e
))
246 /* --------------------------------------------------------------------------------------------- */
249 edit_get_filter (const vfs_path_t
* filename_vpath
)
252 char *p
, *quoted_name
;
254 i
= edit_find_filter (filename_vpath
);
258 quoted_name
= name_quote (vfs_path_as_str (filename_vpath
), FALSE
);
259 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
260 g_free (quoted_name
);
264 /* --------------------------------------------------------------------------------------------- */
267 edit_insert_stream (WEdit
* edit
, FILE * f
)
272 while ((c
= fgetc (f
)) >= 0)
274 edit_insert (edit
, c
);
280 /* --------------------------------------------------------------------------------------------- */
282 * Open file and create it if necessary.
284 * @param edit editor object
285 * @param filename_vpath file name
286 * @param st buffer for store stat info
287 * @return TRUE for success, FALSE for error.
291 check_file_access (WEdit
* edit
, const vfs_path_t
* filename_vpath
, struct stat
*st
)
293 static uintmax_t threshold
= UINTMAX_MAX
;
295 gchar
*errmsg
= NULL
;
298 /* Try opening an existing file */
299 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
303 * Try creating the file. O_EXCL prevents following broken links
304 * and opening existing files.
306 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
310 g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath
));
314 /* New file, delete it if it's not modified or saved */
315 edit
->delete_file
= 1;
318 /* Check what we have opened */
319 if (mc_fstat (file
, st
) < 0)
322 g_strdup_printf (_("Cannot get size/permissions for %s"),
323 vfs_path_as_str (filename_vpath
));
327 /* We want to open regular files only */
328 if (!S_ISREG (st
->st_mode
))
331 g_strdup_printf (_("\"%s\" is not a regular file"), vfs_path_as_str (filename_vpath
));
335 /* get file size threshold for alarm */
336 if (threshold
== UINTMAX_MAX
)
338 gboolean err
= FALSE
;
340 threshold
= parse_integer (option_filesize_threshold
, &err
);
342 threshold
= option_filesize_default_threshold
;
346 * Don't delete non-empty files.
347 * O_EXCL should prevent it, but let's be on the safe side.
350 edit
->delete_file
= 0;
352 if ((uintmax_t) st
->st_size
> threshold
)
356 errmsg
= g_strdup_printf (_("File \"%s\" is too large.\nOpen it anyway?"),
357 vfs_path_as_str (filename_vpath
));
358 act
= edit_query_dialog2 (_("Warning"), errmsg
, _("&Yes"), _("&No"));
359 MC_PTR_FREE (errmsg
);
366 (void) mc_close (file
);
370 edit_error_dialog (_("Error"), errmsg
);
378 /* --------------------------------------------------------------------------------------------- */
381 * Open the file and load it into the buffers, either directly or using
382 * a filter. Return TRUE on success, FALSE on error.
384 * Fast loading (edit_load_file_fast) is used when the file size is
385 * known. In this case the data is read into the buffers by blocks.
386 * If the file size is not known, the data is loaded byte by byte in
389 * @param edit editor object
390 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
393 edit_load_file (WEdit
* edit
)
395 gboolean fast_load
= TRUE
;
397 /* Cannot do fast load if a filter is used */
398 if (edit_find_filter (edit
->filename_vpath
) >= 0)
402 * FIXME: line end translation should disable fast loading as well
403 * Consider doing fseek() to the end and ftell() for the real size.
405 if (edit
->filename_vpath
!= NULL
)
408 * VFS may report file size incorrectly, and slow load is not a big
409 * deal considering overhead in VFS.
411 if (!vfs_file_is_local (edit
->filename_vpath
))
414 /* If we are dealing with a real file, check that it exists */
415 if (!check_file_access (edit
, edit
->filename_vpath
, &edit
->stat1
))
423 /* nothing to load */
429 edit_buffer_init (&edit
->buffer
, edit
->stat1
.st_size
);
431 if (!edit_load_file_fast (&edit
->buffer
, edit
->filename_vpath
))
439 edit_buffer_init (&edit
->buffer
, 0);
441 if (edit
->filename_vpath
!= NULL
442 && *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) != '\0')
444 edit
->undo_stack_disable
= 1;
445 if (edit_insert_file (edit
, edit
->filename_vpath
) < 0)
450 edit
->undo_stack_disable
= 0;
457 /* --------------------------------------------------------------------------------------------- */
459 * Restore saved cursor position and/or bookmarks in the file
461 * @param edit editor object
462 * @param load_position If TRUE, load bookmarks and cursor position and aply them.
463 * If FALSE, load bookmarks only.
467 edit_load_position (WEdit
* edit
, gboolean load_position
)
472 if (edit
->filename_vpath
== NULL
473 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
476 load_file_position (edit
->filename_vpath
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
477 /* apply bookmarks in any case */
478 book_mark_restore (edit
, BOOK_MARK_COLOR
);
485 edit_move_to_line (edit
, line
- 1);
486 edit
->prev_col
= column
;
490 edit_cursor_move (edit
, offset
);
491 line
= edit
->buffer
.curs_line
;
492 edit
->search_start
= edit
->buffer
.curs1
;
495 edit_move_to_prev_col (edit
, edit_buffer_get_current_bol (&edit
->buffer
));
496 edit_move_display (edit
, line
- (WIDGET (edit
)->lines
/ 2));
499 /* --------------------------------------------------------------------------------------------- */
500 /** Save cursor position in the file */
503 edit_save_position (WEdit
* edit
)
505 if (edit
->filename_vpath
== NULL
506 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
509 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
510 save_file_position (edit
->filename_vpath
, edit
->buffer
.curs_line
+ 1, edit
->curs_col
,
511 edit
->buffer
.curs1
, edit
->serialized_bookmarks
);
512 edit
->serialized_bookmarks
= NULL
;
515 /* --------------------------------------------------------------------------------------------- */
516 /** Clean the WEdit stricture except the widget part */
519 edit_purge_widget (WEdit
* edit
)
521 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
522 char *start
= (char *) edit
+ sizeof (Widget
);
523 memset (start
, 0, len
);
526 /* --------------------------------------------------------------------------------------------- */
529 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
530 then the file should be as it was when he loaded up. Then set edit->modified to 0.
534 edit_pop_undo_action (WEdit
* edit
)
537 unsigned long sp
= edit
->undo_stack_pointer
;
539 if (sp
== edit
->undo_stack_bottom
)
542 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
543 c
= edit
->undo_stack
[sp
];
546 /* edit->undo_stack[sp] = '@'; */
547 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
551 if (sp
== edit
->undo_stack_bottom
)
554 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
555 if (edit
->undo_stack
[sp
] == -2)
557 /* edit->undo_stack[sp] = '@'; */
558 edit
->undo_stack_pointer
= sp
;
561 edit
->undo_stack
[sp
]++;
567 edit_pop_redo_action (WEdit
* edit
)
570 unsigned long sp
= edit
->redo_stack_pointer
;
572 if (sp
== edit
->redo_stack_bottom
)
575 sp
= (sp
- 1) & edit
->redo_stack_size_mask
;
576 c
= edit
->redo_stack
[sp
];
579 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
583 if (sp
== edit
->redo_stack_bottom
)
586 c
= edit
->redo_stack
[(sp
- 1) & edit
->redo_stack_size_mask
];
587 if (edit
->redo_stack
[sp
] == -2)
588 edit
->redo_stack_pointer
= sp
;
590 edit
->redo_stack
[sp
]++;
596 get_prev_undo_action (WEdit
* edit
)
599 unsigned long sp
= edit
->undo_stack_pointer
;
601 if (sp
== edit
->undo_stack_bottom
)
604 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
605 c
= edit
->undo_stack
[sp
];
609 if (sp
== edit
->undo_stack_bottom
)
612 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
616 /* --------------------------------------------------------------------------------------------- */
617 /** is called whenever a modification is made by one of the four routines below */
620 edit_modification (WEdit
* edit
)
622 edit
->caches_valid
= FALSE
;
624 /* raise lock when file modified */
625 if (!edit
->modified
&& !edit
->delete_file
)
626 edit
->locked
= lock_file (edit
->filename_vpath
);
630 /* --------------------------------------------------------------------------------------------- */
631 /* high level cursor movement commands */
632 /* --------------------------------------------------------------------------------------------- */
633 /** check whether cursor is in indent part of line
635 * @param edit editor object
637 * @return TRUE if cursor is in indent, FALSE otherwise
641 is_in_indent (const edit_buffer_t
* buf
)
645 for (p
= edit_buffer_get_current_bol (buf
); p
< buf
->curs1
; p
++)
646 if (strchr (" \t", edit_buffer_get_byte (buf
, p
)) == NULL
)
652 /* --------------------------------------------------------------------------------------------- */
653 /** check whether line in editor is blank or not
655 * @param edit editor object
656 * @param offset position in file
658 * @return TRUE if line in blank, FALSE otherwise
662 is_blank (const edit_buffer_t
* buf
, off_t offset
)
666 s
= edit_buffer_get_bol (buf
, offset
);
667 f
= edit_buffer_get_eol (buf
, offset
) - 1;
672 c
= edit_buffer_get_byte (buf
, s
++);
679 /* --------------------------------------------------------------------------------------------- */
680 /** returns the offset of line i */
683 edit_find_line (WEdit
* edit
, long line
)
686 long m
= 2000000000; /* what is the magic number? */
688 if (!edit
->caches_valid
)
690 memset (edit
->line_numbers
, 0, sizeof (edit
->line_numbers
));
691 memset (edit
->line_offsets
, 0, sizeof (edit
->line_offsets
));
692 /* three offsets that we *know* are line 0 at 0 and these two: */
693 edit
->line_numbers
[1] = edit
->buffer
.curs_line
;
694 edit
->line_offsets
[1] = edit_buffer_get_current_bol (&edit
->buffer
);
695 edit
->line_numbers
[2] = edit
->buffer
.lines
;
696 edit
->line_offsets
[2] = edit_buffer_get_bol (&edit
->buffer
, edit
->buffer
.size
);
697 edit
->caches_valid
= TRUE
;
699 if (line
>= edit
->buffer
.lines
)
700 return edit
->line_offsets
[2];
703 /* find the closest known point */
704 for (i
= 0; i
< N_LINE_CACHES
; i
++)
708 n
= labs (edit
->line_numbers
[i
] - line
);
716 return edit
->line_offsets
[j
]; /* know the offset exactly */
717 if (m
== 1 && j
>= 3)
718 i
= j
; /* one line different - caller might be looping, so stay in this cache */
720 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
721 if (line
> edit
->line_numbers
[j
])
722 edit
->line_offsets
[i
] =
723 edit_buffer_get_forward_offset (&edit
->buffer
, edit
->line_offsets
[j
],
724 line
- edit
->line_numbers
[j
], 0);
726 edit
->line_offsets
[i
] =
727 edit_buffer_get_backward_offset (&edit
->buffer
, edit
->line_offsets
[j
],
728 edit
->line_numbers
[j
] - line
);
729 edit
->line_numbers
[i
] = line
;
730 return edit
->line_offsets
[i
];
733 /* --------------------------------------------------------------------------------------------- */
734 /** moves up until a blank line is reached, or until just
735 before a non-blank line is reached */
738 edit_move_up_paragraph (WEdit
* edit
, gboolean do_scroll
)
742 if (edit
->buffer
.curs_line
> 1)
744 if (!edit_line_is_blank (edit
, edit
->buffer
.curs_line
))
746 for (i
= edit
->buffer
.curs_line
- 1; i
!= 0; i
--)
747 if (edit_line_is_blank (edit
, i
))
750 else if (edit_line_is_blank (edit
, edit
->buffer
.curs_line
- 1))
752 for (i
= edit
->buffer
.curs_line
- 1; i
!= 0; i
--)
753 if (!edit_line_is_blank (edit
, i
))
761 for (i
= edit
->buffer
.curs_line
- 1; i
!= 0; i
--)
762 if (edit_line_is_blank (edit
, i
))
767 edit_move_up (edit
, edit
->buffer
.curs_line
- i
, do_scroll
);
770 /* --------------------------------------------------------------------------------------------- */
771 /** moves down until a blank line is reached, or until just
772 before a non-blank line is reached */
775 edit_move_down_paragraph (WEdit
* edit
, gboolean do_scroll
)
779 if (edit
->buffer
.curs_line
>= edit
->buffer
.lines
- 1)
780 i
= edit
->buffer
.lines
;
781 else if (!edit_line_is_blank (edit
, edit
->buffer
.curs_line
))
783 for (i
= edit
->buffer
.curs_line
+ 1; i
!= 0; i
++)
784 if (edit_line_is_blank (edit
, i
) || i
>= edit
->buffer
.lines
)
787 else if (edit_line_is_blank (edit
, edit
->buffer
.curs_line
+ 1))
789 for (i
= edit
->buffer
.curs_line
+ 1; i
!= 0; i
++)
790 if (!edit_line_is_blank (edit
, i
) || i
> edit
->buffer
.lines
)
798 for (i
= edit
->buffer
.curs_line
+ 1; i
!= 0; i
++)
799 if (edit_line_is_blank (edit
, i
) || i
>= edit
->buffer
.lines
)
802 edit_move_down (edit
, i
- edit
->buffer
.curs_line
, do_scroll
);
805 /* --------------------------------------------------------------------------------------------- */
808 edit_begin_page (WEdit
* edit
)
810 edit_update_curs_row (edit
);
811 edit_move_up (edit
, edit
->curs_row
, FALSE
);
814 /* --------------------------------------------------------------------------------------------- */
817 edit_end_page (WEdit
* edit
)
819 edit_update_curs_row (edit
);
820 edit_move_down (edit
, WIDGET (edit
)->lines
- edit
->curs_row
- 1, FALSE
);
824 /* --------------------------------------------------------------------------------------------- */
825 /** goto beginning of text */
828 edit_move_to_top (WEdit
* edit
)
830 if (edit
->buffer
.curs_line
!= 0)
832 edit_cursor_move (edit
, -edit
->buffer
.curs1
);
833 edit_move_to_prev_col (edit
, 0);
834 edit
->force
|= REDRAW_PAGE
;
835 edit
->search_start
= 0;
836 edit_update_curs_row (edit
);
840 /* --------------------------------------------------------------------------------------------- */
841 /** goto end of text */
844 edit_move_to_bottom (WEdit
* edit
)
846 if (edit
->buffer
.curs_line
< edit
->buffer
.lines
)
848 edit_move_down (edit
, edit
->buffer
.lines
- edit
->curs_row
, FALSE
);
849 edit
->start_display
= edit
->buffer
.size
;
850 edit
->start_line
= edit
->buffer
.lines
;
851 edit_scroll_upward (edit
, WIDGET (edit
)->lines
- 1);
852 edit
->force
|= REDRAW_PAGE
;
856 /* --------------------------------------------------------------------------------------------- */
857 /** goto beginning of line */
860 edit_cursor_to_bol (WEdit
* edit
)
862 edit_cursor_move (edit
, edit_buffer_get_current_bol (&edit
->buffer
) - edit
->buffer
.curs1
);
863 edit
->search_start
= edit
->buffer
.curs1
;
864 edit
->prev_col
= edit_get_col (edit
);
868 /* --------------------------------------------------------------------------------------------- */
869 /** goto end of line */
872 edit_cursor_to_eol (WEdit
* edit
)
874 edit_cursor_move (edit
, edit_buffer_get_current_eol (&edit
->buffer
) - edit
->buffer
.curs1
);
875 edit
->search_start
= edit
->buffer
.curs1
;
876 edit
->prev_col
= edit_get_col (edit
);
880 /* --------------------------------------------------------------------------------------------- */
885 unsigned long x
, r
= 0;
887 const char option_chars_move_whole_word
[] =
888 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
894 if (*option_chars_move_whole_word
== '!')
898 if (g_ascii_isupper ((gchar
) c
))
900 else if (g_ascii_islower ((gchar
) c
))
902 else if (g_ascii_isalpha (c
))
904 else if (isdigit (c
))
906 else if (isspace (c
))
908 q
= strchr (option_chars_move_whole_word
, c
);
913 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
918 while ((q
= strchr (q
+ 1, c
)));
922 /* --------------------------------------------------------------------------------------------- */
925 edit_left_word_move (WEdit
* edit
, int s
)
931 if (edit
->column_highlight
932 && edit
->mark1
!= edit
->mark2
933 && edit
->over_col
== 0
934 && edit
->buffer
.curs1
== edit_buffer_get_current_bol (&edit
->buffer
))
936 edit_cursor_move (edit
, -1);
937 if (edit
->buffer
.curs1
== 0)
939 c1
= edit_buffer_get_previous_byte (&edit
->buffer
);
940 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
941 if (c1
== '\n' || c2
== '\n')
943 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
945 if (isspace (c1
) && !isspace (c2
))
947 if (s
!= 0 && !isspace (c1
) && isspace (c2
))
952 /* --------------------------------------------------------------------------------------------- */
955 edit_left_word_move_cmd (WEdit
* edit
)
957 edit_left_word_move (edit
, 0);
958 edit
->force
|= REDRAW_PAGE
;
961 /* --------------------------------------------------------------------------------------------- */
964 edit_right_word_move (WEdit
* edit
, int s
)
970 if (edit
->column_highlight
971 && edit
->mark1
!= edit
->mark2
972 && edit
->over_col
== 0
973 && edit
->buffer
.curs1
== edit_buffer_get_current_eol (&edit
->buffer
))
975 edit_cursor_move (edit
, 1);
976 if (edit
->buffer
.curs1
>= edit
->buffer
.size
)
978 c1
= edit_buffer_get_previous_byte (&edit
->buffer
);
979 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
980 if (c1
== '\n' || c2
== '\n')
982 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
984 if (isspace (c1
) && !isspace (c2
))
986 if (s
!= 0 && !isspace (c1
) && isspace (c2
))
991 /* --------------------------------------------------------------------------------------------- */
994 edit_right_word_move_cmd (WEdit
* edit
)
996 edit_right_word_move (edit
, 0);
997 edit
->force
|= REDRAW_PAGE
;
1000 /* --------------------------------------------------------------------------------------------- */
1003 edit_right_char_move_cmd (WEdit
* edit
)
1005 int char_length
= 1;
1011 c
= edit_buffer_get_utf (&edit
->buffer
, edit
->buffer
.curs1
, &char_length
);
1012 if (char_length
< 1)
1017 c
= edit_buffer_get_current_byte (&edit
->buffer
);
1019 if (option_cursor_beyond_eol
&& c
== '\n')
1022 edit_cursor_move (edit
, char_length
);
1025 /* --------------------------------------------------------------------------------------------- */
1028 edit_left_char_move_cmd (WEdit
* edit
)
1030 int char_length
= 1;
1032 if (edit
->column_highlight
1033 && option_cursor_beyond_eol
1034 && edit
->mark1
!= edit
->mark2
1035 && edit
->over_col
== 0 && edit
->buffer
.curs1
== edit_buffer_get_current_bol (&edit
->buffer
))
1040 edit_buffer_get_prev_utf (&edit
->buffer
, edit
->buffer
.curs1
, &char_length
);
1041 if (char_length
< 1)
1046 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1049 edit_cursor_move (edit
, -char_length
);
1052 /* --------------------------------------------------------------------------------------------- */
1053 /** Up or down cursor moving.
1054 direction = TRUE - move up
1059 edit_move_updown (WEdit
* edit
, long lines
, gboolean do_scroll
, gboolean direction
)
1062 long l
= direction
? edit
->buffer
.curs_line
: edit
->buffer
.lines
- edit
->buffer
.curs_line
;
1071 edit
->force
|= REDRAW_PAGE
;
1075 edit_scroll_upward (edit
, lines
);
1077 edit_scroll_downward (edit
, lines
);
1079 p
= edit_buffer_get_current_bol (&edit
->buffer
);
1080 p
= direction
? edit_buffer_get_backward_offset (&edit
->buffer
, p
, lines
) :
1081 edit_buffer_get_forward_offset (&edit
->buffer
, p
, lines
, 0);
1082 edit_cursor_move (edit
, p
- edit
->buffer
.curs1
);
1083 edit_move_to_prev_col (edit
, p
);
1086 /* search start of current multibyte char (like CJK) */
1087 if (edit
->buffer
.curs1
> 0 && edit
->buffer
.curs1
+ 1 < edit
->buffer
.size
1088 && edit_buffer_get_current_byte (&edit
->buffer
) >= 256)
1090 edit_right_char_move_cmd (edit
);
1091 edit_left_char_move_cmd (edit
);
1095 edit
->search_start
= edit
->buffer
.curs1
;
1096 edit
->found_len
= 0;
1099 /* --------------------------------------------------------------------------------------------- */
1102 edit_right_delete_word (WEdit
* edit
)
1104 while (edit
->buffer
.curs1
< edit
->buffer
.size
)
1108 c1
= edit_delete (edit
, TRUE
);
1109 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
1110 if (c1
== '\n' || c2
== '\n')
1112 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1114 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1119 /* --------------------------------------------------------------------------------------------- */
1122 edit_left_delete_word (WEdit
* edit
)
1124 while (edit
->buffer
.curs1
> 0)
1128 c1
= edit_backspace (edit
, TRUE
);
1129 c2
= edit_buffer_get_previous_byte (&edit
->buffer
);
1130 if (c1
== '\n' || c2
== '\n')
1132 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1134 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1139 /* --------------------------------------------------------------------------------------------- */
1141 the start column position is not recorded, and hence does not
1142 undo as it happed. But who would notice.
1146 edit_do_undo (WEdit
* edit
)
1151 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1153 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1160 edit_cursor_move (edit
, 1);
1163 edit_cursor_move (edit
, -1);
1167 edit_backspace (edit
, TRUE
);
1171 edit_delete (edit
, TRUE
);
1174 edit
->column_highlight
= 1;
1177 edit
->column_highlight
= 0;
1182 if (ac
>= 256 && ac
< 512)
1183 edit_insert_ahead (edit
, ac
- 256);
1184 if (ac
>= 0 && ac
< 256)
1185 edit_insert (edit
, ac
);
1187 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1189 edit
->mark1
= ac
- MARK_1
;
1191 (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, edit
->mark1
),
1194 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1196 edit
->mark2
= ac
- MARK_2
;
1198 (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, edit
->mark2
),
1201 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1203 edit
->end_mark_curs
= ac
- MARK_CURS
;
1206 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1209 if (edit
->start_display
> ac
- KEY_PRESS
)
1212 edit_buffer_count_lines (&edit
->buffer
, ac
- KEY_PRESS
, edit
->start_display
);
1213 edit
->force
|= REDRAW_PAGE
;
1215 else if (edit
->start_display
< ac
- KEY_PRESS
)
1218 edit_buffer_count_lines (&edit
->buffer
, edit
->start_display
, ac
- KEY_PRESS
);
1219 edit
->force
|= REDRAW_PAGE
;
1221 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1222 edit_update_curs_row (edit
);
1225 edit
->undo_stack_disable
= 0;
1228 /* --------------------------------------------------------------------------------------------- */
1231 edit_do_redo (WEdit
* edit
)
1236 if (edit
->redo_stack_reset
)
1240 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1247 edit_cursor_move (edit
, 1);
1250 edit_cursor_move (edit
, -1);
1253 edit_backspace (edit
, TRUE
);
1256 edit_delete (edit
, TRUE
);
1259 edit
->column_highlight
= 1;
1262 edit
->column_highlight
= 0;
1267 if (ac
>= 256 && ac
< 512)
1268 edit_insert_ahead (edit
, ac
- 256);
1269 if (ac
>= 0 && ac
< 256)
1270 edit_insert (edit
, ac
);
1272 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1274 edit
->mark1
= ac
- MARK_1
;
1276 (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, edit
->mark1
),
1279 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1281 edit
->mark2
= ac
- MARK_2
;
1283 (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, edit
->mark2
),
1286 /* more than one pop usually means something big */
1288 edit
->force
|= REDRAW_PAGE
;
1291 if (edit
->start_display
> ac
- KEY_PRESS
)
1294 edit_buffer_count_lines (&edit
->buffer
, ac
- KEY_PRESS
, edit
->start_display
);
1295 edit
->force
|= REDRAW_PAGE
;
1297 else if (edit
->start_display
< ac
- KEY_PRESS
)
1300 edit_buffer_count_lines (&edit
->buffer
, edit
->start_display
, ac
- KEY_PRESS
);
1301 edit
->force
|= REDRAW_PAGE
;
1303 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1304 edit_update_curs_row (edit
);
1310 /* --------------------------------------------------------------------------------------------- */
1313 edit_group_undo (WEdit
* edit
)
1315 long ac
= KEY_PRESS
;
1316 long cur_ac
= KEY_PRESS
;
1317 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1319 cur_ac
= get_prev_undo_action (edit
);
1320 edit_do_undo (edit
);
1321 ac
= get_prev_undo_action (edit
);
1322 /* exit from cycle if option_group_undo is not set,
1323 * and make single UNDO operation
1325 if (!option_group_undo
)
1330 /* --------------------------------------------------------------------------------------------- */
1333 edit_delete_to_line_end (WEdit
* edit
)
1335 while (edit_buffer_get_current_byte (&edit
->buffer
) != '\n' && edit
->buffer
.curs2
!= 0)
1336 edit_delete (edit
, TRUE
);
1339 /* --------------------------------------------------------------------------------------------- */
1342 edit_delete_to_line_begin (WEdit
* edit
)
1344 while (edit_buffer_get_previous_byte (&edit
->buffer
) != '\n' && edit
->buffer
.curs1
!= 0)
1345 edit_backspace (edit
, TRUE
);
1348 /* --------------------------------------------------------------------------------------------- */
1351 is_aligned_on_a_tab (WEdit
* edit
)
1355 edit_update_curs_col (edit
);
1356 curs_col
= edit
->curs_col
% (TAB_SIZE
* space_width
);
1357 return (curs_col
== 0 || curs_col
== (HALF_TAB_SIZE
* space_width
));
1360 /* --------------------------------------------------------------------------------------------- */
1363 right_of_four_spaces (WEdit
* edit
)
1367 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1368 ch
|= edit_buffer_get_byte (&edit
->buffer
, edit
->buffer
.curs1
- i
);
1370 return (ch
== ' ' && is_aligned_on_a_tab (edit
));
1373 /* --------------------------------------------------------------------------------------------- */
1376 left_of_four_spaces (WEdit
* edit
)
1380 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1381 ch
|= edit_buffer_get_byte (&edit
->buffer
, edit
->buffer
.curs1
+ i
);
1383 return (ch
== ' ' && is_aligned_on_a_tab (edit
));
1386 /* --------------------------------------------------------------------------------------------- */
1389 edit_auto_indent (WEdit
* edit
)
1393 p
= edit
->buffer
.curs1
;
1394 /* use the previous line as a template */
1395 p
= edit_buffer_get_backward_offset (&edit
->buffer
, p
, 1);
1396 /* copy the leading whitespace of the line */
1398 { /* no range check - the line _is_ \n-terminated */
1401 c
= edit_buffer_get_byte (&edit
->buffer
, p
++);
1402 if (!whitespace (c
))
1404 edit_insert (edit
, c
);
1408 /* --------------------------------------------------------------------------------------------- */
1411 edit_double_newline (WEdit
* edit
)
1413 edit_insert (edit
, '\n');
1414 if (edit_buffer_get_current_byte (&edit
->buffer
) == '\n'
1415 || edit_buffer_get_byte (&edit
->buffer
, edit
->buffer
.curs1
- 2) == '\n')
1417 edit
->force
|= REDRAW_PAGE
;
1418 edit_insert (edit
, '\n');
1421 /* --------------------------------------------------------------------------------------------- */
1424 insert_spaces_tab (WEdit
* edit
, gboolean half
)
1428 edit_update_curs_col (edit
);
1429 i
= option_tab_spacing
* space_width
;
1434 i
= ((edit
->curs_col
/ i
) + 1) * i
- edit
->curs_col
;
1437 edit_insert (edit
, ' ');
1443 /* --------------------------------------------------------------------------------------------- */
1446 edit_tab_cmd (WEdit
* edit
)
1448 if (option_fake_half_tabs
&& is_in_indent (&edit
->buffer
))
1450 /* insert a half tab (usually four spaces) unless there is a
1451 half tab already behind, then delete it and insert a
1453 if (option_fill_tabs_with_spaces
|| !right_of_four_spaces (edit
))
1454 insert_spaces_tab (edit
, TRUE
);
1459 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1460 edit_backspace (edit
, TRUE
);
1461 edit_insert (edit
, '\t');
1464 else if (option_fill_tabs_with_spaces
)
1465 insert_spaces_tab (edit
, FALSE
);
1467 edit_insert (edit
, '\t');
1470 /* --------------------------------------------------------------------------------------------- */
1473 check_and_wrap_line (WEdit
* edit
)
1477 if (!option_typewriter_wrap
)
1479 edit_update_curs_col (edit
);
1480 if (edit
->curs_col
< option_word_wrap_line_length
)
1482 curs
= edit
->buffer
.curs1
;
1488 c
= edit_buffer_get_byte (&edit
->buffer
, curs
);
1489 if (c
== '\n' || curs
<= 0)
1491 edit_insert (edit
, '\n');
1496 off_t current
= edit
->buffer
.curs1
;
1497 edit_cursor_move (edit
, curs
- edit
->buffer
.curs1
+ 1);
1498 edit_insert (edit
, '\n');
1499 edit_cursor_move (edit
, current
- edit
->buffer
.curs1
+ 1);
1505 /* --------------------------------------------------------------------------------------------- */
1506 /** this find the matching bracket in either direction, and sets edit->bracket
1508 * @param edit editor object
1509 * @param in_screen seach only on the current screen
1510 * @param furthest_bracket_search count of the bytes for search
1512 * @return position of the found bracket (-1 if no match)
1516 edit_get_bracket (WEdit
* edit
, gboolean in_screen
, unsigned long furthest_bracket_search
)
1518 const char *const b
= "{}{[][()(", *p
;
1519 int i
= 1, inc
= -1, c
, d
, n
= 0;
1520 unsigned long j
= 0;
1523 edit_update_curs_row (edit
);
1524 c
= edit_buffer_get_current_byte (&edit
->buffer
);
1526 /* not on a bracket at all */
1527 if (p
== NULL
|| *p
== '\0')
1529 /* the matching bracket */
1531 /* going left or right? */
1532 if (strchr ("{[(", c
) != NULL
)
1535 if (furthest_bracket_search
== 0)
1536 furthest_bracket_search
--; /* ULONG_MAX */
1537 for (q
= edit
->buffer
.curs1
+ inc
;; q
+= inc
)
1541 /* out of buffer? */
1542 if (q
>= edit
->buffer
.size
|| q
< 0)
1544 a
= edit_buffer_get_byte (&edit
->buffer
, q
);
1545 /* don't want to eat CPU */
1546 if (j
++ > furthest_bracket_search
)
1548 /* out of screen? */
1551 if (q
< edit
->start_display
)
1553 /* count lines if searching downward */
1554 if (inc
> 0 && a
== '\n')
1555 if (n
++ >= WIDGET (edit
)->lines
- edit
->curs_row
) /* out of screen */
1558 /* count bracket depth */
1559 i
+= (a
== c
) - (a
== d
);
1560 /* return if bracket depth is zero */
1568 /* --------------------------------------------------------------------------------------------- */
1571 edit_goto_matching_bracket (WEdit
* edit
)
1575 q
= edit_get_bracket (edit
, 0, 0);
1578 edit
->bracket
= edit
->buffer
.curs1
;
1579 edit
->force
|= REDRAW_PAGE
;
1580 edit_cursor_move (edit
, q
- edit
->buffer
.curs1
);
1584 /* --------------------------------------------------------------------------------------------- */
1587 edit_move_block_to_right (WEdit
* edit
)
1589 off_t start_mark
, end_mark
;
1590 long cur_bol
, start_bol
;
1592 if (!eval_marks (edit
, &start_mark
, &end_mark
))
1595 start_bol
= edit_buffer_get_bol (&edit
->buffer
, start_mark
);
1596 cur_bol
= edit_buffer_get_bol (&edit
->buffer
, end_mark
- 1);
1600 edit_cursor_move (edit
, cur_bol
- edit
->buffer
.curs1
);
1601 if (!edit_line_is_blank (edit
, edit
->buffer
.curs_line
))
1603 if (option_fill_tabs_with_spaces
)
1604 insert_spaces_tab (edit
, option_fake_half_tabs
);
1606 edit_insert (edit
, '\t');
1607 edit_cursor_move (edit
,
1608 edit_buffer_get_bol (&edit
->buffer
, cur_bol
) - edit
->buffer
.curs1
);
1614 cur_bol
= edit_buffer_get_bol (&edit
->buffer
, cur_bol
- 1);
1616 while (cur_bol
>= start_bol
);
1618 edit
->force
|= REDRAW_PAGE
;
1621 /* --------------------------------------------------------------------------------------------- */
1624 edit_move_block_to_left (WEdit
* edit
)
1626 off_t start_mark
, end_mark
;
1627 off_t 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);
1640 edit_cursor_move (edit
, cur_bol
- edit
->buffer
.curs1
);
1642 if (option_fake_half_tabs
)
1643 del_tab_width
= HALF_TAB_SIZE
;
1645 del_tab_width
= option_tab_spacing
;
1647 next_char
= edit_buffer_get_current_byte (&edit
->buffer
);
1648 if (next_char
== '\t')
1649 edit_delete (edit
, TRUE
);
1650 else if (next_char
== ' ')
1654 for (i
= 0; i
< del_tab_width
; i
++)
1656 if (next_char
== ' ')
1657 edit_delete (edit
, TRUE
);
1658 next_char
= edit_buffer_get_current_byte (&edit
->buffer
);
1665 cur_bol
= edit_buffer_get_bol (&edit
->buffer
, cur_bol
- 1);
1667 while (cur_bol
>= start_bol
);
1669 edit
->force
|= REDRAW_PAGE
;
1672 /* --------------------------------------------------------------------------------------------- */
1674 * prints at the cursor
1675 * @return number of chars printed
1679 edit_print_string (WEdit
* e
, const char *s
)
1683 while (s
[i
] != '\0')
1684 edit_execute_cmd (e
, CK_InsertChar
, (unsigned char) s
[i
++]);
1685 e
->force
|= REDRAW_COMPLETELY
;
1686 edit_update_screen (e
);
1690 /* --------------------------------------------------------------------------------------------- */
1693 edit_insert_column_from_file (WEdit
* edit
, int file
, off_t
* start_pos
, off_t
* end_pos
,
1694 long *col1
, long *col2
)
1698 off_t blocklen
= -1, width
= 0;
1699 unsigned char *data
;
1701 cursor
= edit
->buffer
.curs1
;
1702 col
= edit_get_col (edit
);
1703 data
= g_malloc0 (TEMP_BUF_LEN
);
1705 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0)
1710 pn
= strchr ((char *) data
, '\n');
1711 width
= pn
== NULL
? blocklen
: pn
- (char *) data
;
1713 for (i
= 0; i
< blocklen
; i
++)
1715 if (data
[i
] != '\n')
1716 edit_insert (edit
, data
[i
]);
1718 { /* fill in and move to next line */
1722 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
1723 for (l
= width
- (edit_get_col (edit
) - col
); l
> 0; l
-= space_width
)
1724 edit_insert (edit
, ' ');
1726 for (p
= edit
->buffer
.curs1
;; p
++)
1728 if (p
== edit
->buffer
.size
)
1730 edit_cursor_move (edit
, edit
->buffer
.size
- edit
->buffer
.curs1
);
1731 edit_insert_ahead (edit
, '\n');
1735 if (edit_buffer_get_byte (&edit
->buffer
, p
) == '\n')
1742 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->buffer
.curs1
);
1744 for (l
= col
- edit_get_col (edit
); l
>= space_width
; l
-= space_width
)
1745 edit_insert (edit
, ' ');
1750 *col2
= col
+ width
;
1751 *start_pos
= cursor
;
1752 *end_pos
= edit
->buffer
.curs1
;
1753 edit_cursor_move (edit
, cursor
- edit
->buffer
.curs1
);
1759 /* --------------------------------------------------------------------------------------------- */
1760 /*** public functions ****************************************************************************/
1761 /* --------------------------------------------------------------------------------------------- */
1763 /** User edit menu, like user menu (F2) but only in editor. */
1766 user_menu (WEdit
* edit
, const char *menu_file
, int selected_entry
)
1771 off_t start_mark
, end_mark
;
1773 vfs_path_t
*block_file_vpath
;
1775 block_file
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
1776 block_file_vpath
= vfs_path_from_str (block_file
);
1777 curs
= edit
->buffer
.curs1
;
1778 nomark
= !eval_marks (edit
, &start_mark
, &end_mark
);
1780 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1782 /* run shell scripts from menu */
1783 if (user_menu_cmd (edit
, menu_file
, selected_entry
)
1784 && (mc_stat (block_file_vpath
, &status
) == 0) && (status
.st_size
!= 0))
1789 /* i.e. we have marked block */
1791 rc
= edit_block_delete_cmd (edit
);
1797 ins_len
= edit_insert_file (edit
, block_file_vpath
);
1798 if (!nomark
&& ins_len
> 0)
1799 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1801 /* truncate block file */
1802 fd
= fopen (block_file
, "w");
1806 g_free (block_file
);
1807 vfs_path_free (block_file_vpath
);
1809 edit_cursor_move (edit
, curs
- edit
->buffer
.curs1
);
1810 edit
->force
|= REDRAW_PAGE
;
1811 widget_redraw (WIDGET (edit
));
1814 /* --------------------------------------------------------------------------------------------- */
1817 edit_get_write_filter (const vfs_path_t
* write_name_vpath
, const vfs_path_t
* filename_vpath
)
1820 char *p
, *writename
;
1821 const vfs_path_element_t
*path_element
;
1823 i
= edit_find_filter (filename_vpath
);
1827 path_element
= vfs_path_get_by_index (write_name_vpath
, -1);
1828 writename
= name_quote (path_element
->path
, FALSE
);
1829 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
1834 /* --------------------------------------------------------------------------------------------- */
1836 * @param edit editor object
1837 * @param f value of stream file
1838 * @return the length of the file
1842 edit_write_stream (WEdit
* edit
, FILE * f
)
1846 if (edit
->lb
== LB_ASIS
)
1848 for (i
= 0; i
< edit
->buffer
.size
; i
++)
1849 if (fputc (edit_buffer_get_byte (&edit
->buffer
, i
), f
) < 0)
1854 /* change line breaks */
1855 for (i
= 0; i
< edit
->buffer
.size
; i
++)
1859 c
= edit_buffer_get_byte (&edit
->buffer
, i
);
1860 if (!(c
== '\n' || c
== '\r'))
1862 /* not line break */
1863 if (fputc (c
, f
) < 0)
1867 { /* (c == '\n' || c == '\r') */
1870 c1
= edit_buffer_get_byte (&edit
->buffer
, i
+ 1); /* next char */
1874 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
1875 /* put one line break unconditionally */
1876 if (fputc ('\n', f
) < 0)
1879 i
++; /* 2 chars are processed */
1881 if (c
== '\r' && c1
== '\n')
1882 /* Windows line break; go to the next char */
1885 if (c
== '\r' && c1
== '\r')
1887 /* two Macintosh line breaks; put second line break */
1888 if (fputc ('\n', f
) < 0)
1893 if (fputc (c1
, f
) < 0)
1897 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
1898 /* put one line break unconditionally */
1899 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
1902 if (c
== '\r' && c1
== '\n')
1903 /* Windows line break; go to the next char */
1907 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
1908 /* put one line break unconditionally */
1909 if (fputc ('\r', f
) < 0)
1912 i
++; /* 2 chars are processed */
1914 if (c
== '\r' && c1
== '\n')
1915 /* Windows line break; go to the next char */
1918 if (c
== '\n' && c1
== '\n')
1920 /* two Windows line breaks; put second line break */
1921 if (fputc ('\r', f
) < 0)
1926 if (fputc (c1
, f
) < 0)
1929 case LB_ASIS
: /* default without changes */
1936 return edit
->buffer
.size
;
1939 /* --------------------------------------------------------------------------------------------- */
1942 is_break_char (char c
)
1944 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
1947 /* --------------------------------------------------------------------------------------------- */
1948 /** inserts a file at the cursor, returns count of inserted bytes on success */
1951 edit_insert_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
1957 p
= edit_get_filter (filename_vpath
);
1958 current
= edit
->buffer
.curs1
;
1964 f
= (FILE *) popen (p
, "r");
1967 edit_insert_stream (edit
, f
);
1969 /* Place cursor at the end of text selection */
1970 if (!option_cursor_after_inserted_block
)
1972 ins_len
= edit
->buffer
.curs1
- current
;
1973 edit_cursor_move (edit
, -ins_len
);
1979 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
1980 edit_error_dialog (_("Error"), errmsg
);
1989 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
1990 edit_error_dialog (_("Error"), errmsg
);
2000 int vertical_insertion
= 0;
2003 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
2007 buf
= g_malloc0 (TEMP_BUF_LEN
);
2008 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2011 /* if contain signature VERTICAL_MAGIC then it vertical block */
2012 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2013 vertical_insertion
= 1;
2015 mc_lseek (file
, 0, SEEK_SET
);
2018 if (vertical_insertion
)
2023 blocklen
= edit_insert_column_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
2024 edit_set_markers (edit
, edit
->buffer
.curs1
, mark2
, c1
, c2
);
2026 /* highlight inserted text then not persistent blocks */
2027 if (!option_persistent_selections
&& edit
->modified
)
2029 if (!edit
->column_highlight
)
2030 edit_push_undo_action (edit
, COLUMN_OFF
);
2031 edit
->column_highlight
= 1;
2038 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2040 for (i
= 0; i
< blocklen
; i
++)
2041 edit_insert (edit
, buf
[i
]);
2043 /* highlight inserted text then not persistent blocks */
2044 if (!option_persistent_selections
&& edit
->modified
)
2046 edit_set_markers (edit
, edit
->buffer
.curs1
, current
, 0, 0);
2047 if (edit
->column_highlight
)
2048 edit_push_undo_action (edit
, COLUMN_ON
);
2049 edit
->column_highlight
= 0;
2052 /* Place cursor at the end of text selection */
2053 if (!option_cursor_after_inserted_block
)
2055 ins_len
= edit
->buffer
.curs1
- current
;
2056 edit_cursor_move (edit
, -ins_len
);
2060 edit
->force
|= REDRAW_PAGE
;
2070 /* --------------------------------------------------------------------------------------------- */
2072 * Fill in the edit structure. Return NULL on failure. Pass edit as
2073 * NULL to allocate a new structure.
2075 * If line is 0, try to restore saved position. Otherwise put the
2076 * cursor on that line and show it in the middle of the screen.
2080 edit_init (WEdit
* edit
, int y
, int x
, int lines
, int cols
, const vfs_path_t
* filename_vpath
,
2083 gboolean to_free
= FALSE
;
2085 option_auto_syntax
= TRUE
; /* Resetting to auto on every invokation */
2086 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
2090 /* save some widget parameters */
2091 gboolean fullscreen
= edit
->fullscreen
;
2092 int y_prev
= edit
->y_prev
;
2093 int x_prev
= edit
->x_prev
;
2094 int lines_prev
= edit
->lines_prev
;
2095 int cols_prev
= edit
->cols_prev
;
2097 edit_purge_widget (edit
);
2099 /* restore saved parameters */
2100 edit
->fullscreen
= fullscreen
;
2101 edit
->y_prev
= y_prev
;
2102 edit
->x_prev
= x_prev
;
2103 edit
->lines_prev
= lines_prev
;
2104 edit
->cols_prev
= cols_prev
;
2109 edit
= g_malloc0 (sizeof (WEdit
));
2113 widget_init (w
, y
, x
, lines
, cols
, NULL
, NULL
);
2114 w
->options
|= WOP_SELECTABLE
| WOP_TOP_SELECT
| WOP_WANT_CURSOR
;
2115 edit
->fullscreen
= TRUE
;
2116 edit_save_size (edit
);
2119 edit
->drag_state
= MCEDIT_DRAG_NONE
;
2121 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2122 edit
->stat1
.st_uid
= getuid ();
2123 edit
->stat1
.st_gid
= getgid ();
2124 edit
->stat1
.st_mtime
= 0;
2128 edit
->last_bracket
= -1;
2129 edit
->force
|= REDRAW_PAGE
;
2131 /* set file name before load file */
2132 edit_set_filename (edit
, filename_vpath
);
2134 edit
->undo_stack_size
= START_STACK_SIZE
;
2135 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2136 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2138 edit
->redo_stack_size
= START_STACK_SIZE
;
2139 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2140 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2144 edit
->converter
= str_cnv_from_term
;
2145 edit_set_codeset (edit
);
2148 if (!edit_load_file (edit
))
2150 /* edit_load_file already gives an error message */
2156 edit
->loading_done
= 1;
2159 edit_load_syntax (edit
, NULL
, NULL
);
2160 edit_get_syntax_color (edit
, -1);
2162 /* load saved cursor position and/or boolmarks */
2163 if ((line
== 0) && option_save_position
)
2164 edit_load_position (edit
, TRUE
);
2167 edit_load_position (edit
, FALSE
);
2170 edit_move_display (edit
, line
- 1);
2171 edit_move_to_line (edit
, line
- 1);
2174 edit_load_macro_cmd (edit
);
2179 /* --------------------------------------------------------------------------------------------- */
2181 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2183 edit_clean (WEdit
* edit
)
2188 /* a stale lock, remove it */
2190 edit
->locked
= unlock_file (edit
->filename_vpath
);
2192 /* save cursor position */
2193 if (option_save_position
)
2194 edit_save_position (edit
);
2195 else if (edit
->serialized_bookmarks
!= NULL
)
2196 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2198 /* File specified on the mcedit command line and never saved */
2199 if (edit
->delete_file
)
2200 unlink (vfs_path_get_last_path_str (edit
->filename_vpath
));
2202 edit_free_syntax_rules (edit
);
2203 book_mark_flush (edit
, -1);
2205 edit_buffer_clean (&edit
->buffer
);
2207 g_free (edit
->undo_stack
);
2208 g_free (edit
->redo_stack
);
2209 vfs_path_free (edit
->filename_vpath
);
2210 vfs_path_free (edit
->dir_vpath
);
2211 mc_search_free (edit
->search
);
2212 edit
->search
= NULL
;
2213 MC_PTR_FREE (edit
->last_search_string
);
2216 if (edit
->converter
!= str_cnv_from_term
)
2217 str_close_conv (edit
->converter
);
2220 edit_purge_widget (edit
);
2225 /* --------------------------------------------------------------------------------------------- */
2228 * Load a new file into the editor and set line. If it fails, preserve the old file.
2229 * To do it, allocate a new widget, initialize it and, if the new file
2230 * was loaded, copy the data to the old widget.
2232 * @return TRUE on success, FALSE on failure.
2235 edit_reload_line (WEdit
* edit
, const vfs_path_t
* filename_vpath
, long line
)
2237 Widget
*w
= WIDGET (edit
);
2240 e
= g_malloc0 (sizeof (WEdit
));
2242 /* save some widget parameters */
2243 e
->fullscreen
= edit
->fullscreen
;
2244 e
->y_prev
= edit
->y_prev
;
2245 e
->x_prev
= edit
->x_prev
;
2246 e
->lines_prev
= edit
->lines_prev
;
2247 e
->cols_prev
= edit
->cols_prev
;
2249 if (edit_init (e
, w
->y
, w
->x
, w
->lines
, w
->cols
, filename_vpath
, line
) == NULL
)
2256 memcpy (edit
, e
, sizeof (*edit
));
2262 /* --------------------------------------------------------------------------------------------- */
2266 edit_set_codeset (WEdit
* edit
)
2271 get_codepage_id (mc_global
.source_codepage
>=
2272 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2277 conv
= str_crt_conv_from (cp_id
);
2278 if (conv
!= INVALID_CONV
)
2280 if (edit
->converter
!= str_cnv_from_term
)
2281 str_close_conv (edit
->converter
);
2282 edit
->converter
= conv
;
2287 edit
->utf8
= str_isutf8 (cp_id
);
2291 /* --------------------------------------------------------------------------------------------- */
2294 * Recording stack for undo:
2295 * The following is an implementation of a compressed stack. Identical
2296 * pushes are recorded by a negative prefix indicating the number of times the
2297 * same char was pushed. This saves space for repeated curs-left or curs-right
2314 * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2315 * 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2316 * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2317 * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2320 * The only way the cursor moves or the buffer is changed is through the routines:
2321 * insert, backspace, insert_ahead, delete, and cursor_move.
2322 * These record the reverse undo movements onto the stack each time they are
2325 * Each key press results in a set of actions (insert; delete ...). So each time
2326 * a key is pressed the current position of start_display is pushed as
2327 * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2328 * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2329 * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2333 * @param edit editor object
2334 * @param c code of the action
2338 edit_push_undo_action (WEdit
* edit
, long c
)
2340 unsigned long sp
= edit
->undo_stack_pointer
;
2344 /* first enlarge the stack if necessary */
2345 if (sp
> edit
->undo_stack_size
- 10)
2347 if (option_max_undo
< 256)
2348 option_max_undo
= 256;
2349 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2351 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2354 edit
->undo_stack
= t
;
2355 edit
->undo_stack_size
<<= 1;
2356 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2360 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2361 if (edit
->undo_stack_disable
)
2363 edit_push_redo_action (edit
, KEY_PRESS
);
2364 edit_push_redo_action (edit
, c
);
2368 if (edit
->redo_stack_reset
)
2369 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2371 if (edit
->undo_stack_bottom
!= sp
2372 && spm1
!= edit
->undo_stack_bottom
2373 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2376 if (edit
->undo_stack
[spm1
] < 0)
2378 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2379 if (d
== c
&& edit
->undo_stack
[spm1
] > -1000000000)
2381 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2382 edit
->undo_stack
[spm1
]--;
2388 d
= edit
->undo_stack
[spm1
];
2392 return; /* --> no need to push multiple do-nothings */
2393 edit
->undo_stack
[sp
] = -2;
2398 edit
->undo_stack
[sp
] = c
;
2401 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2403 /* if the sp wraps round and catches the undo_stack_bottom then erase
2404 * the first set of actions on the stack to make space - by moving
2405 * undo_stack_bottom forward one "key press" */
2406 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2407 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2408 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2411 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2413 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2414 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2416 /*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: */
2417 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2418 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2420 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2424 /* --------------------------------------------------------------------------------------------- */
2427 edit_push_redo_action (WEdit
* edit
, long c
)
2429 unsigned long sp
= edit
->redo_stack_pointer
;
2432 /* first enlarge the stack if necessary */
2433 if (sp
> edit
->redo_stack_size
- 10)
2435 if (option_max_undo
< 256)
2436 option_max_undo
= 256;
2437 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2439 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2442 edit
->redo_stack
= t
;
2443 edit
->redo_stack_size
<<= 1;
2444 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2448 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2450 if (edit
->redo_stack_bottom
!= sp
2451 && spm1
!= edit
->redo_stack_bottom
2452 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2455 if (edit
->redo_stack
[spm1
] < 0)
2457 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2458 if (d
== c
&& edit
->redo_stack
[spm1
] > -1000000000)
2460 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2461 edit
->redo_stack
[spm1
]--;
2467 d
= edit
->redo_stack
[spm1
];
2471 return; /* --> no need to push multiple do-nothings */
2472 edit
->redo_stack
[sp
] = -2;
2473 goto redo_check_bottom
;
2477 edit
->redo_stack
[sp
] = c
;
2480 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2482 /* if the sp wraps round and catches the redo_stack_bottom then erase
2483 * the first set of actions on the stack to make space - by moving
2484 * redo_stack_bottom forward one "key press" */
2485 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2486 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2487 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2490 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2492 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2493 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2496 * If a single key produced enough pushes to wrap all the way round then
2497 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2498 * The stack is then initialised:
2501 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2502 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2503 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2506 /* --------------------------------------------------------------------------------------------- */
2508 Basic low level single character buffer alterations and movements at the cursor.
2512 edit_insert (WEdit
* edit
, int c
)
2514 /* first we must update the position of the display window */
2515 if (edit
->buffer
.curs1
< edit
->start_display
)
2517 edit
->start_display
++;
2522 /* Mark file as modified, unless the file hasn't been fully loaded */
2523 if (edit
->loading_done
)
2524 edit_modification (edit
);
2526 /* now we must update some info on the file and check if a redraw is required */
2529 book_mark_inc (edit
, edit
->buffer
.curs_line
);
2530 edit
->buffer
.curs_line
++;
2531 edit
->buffer
.lines
++;
2532 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2535 /* save the reverse command onto the undo stack */
2536 /* ordinary char and not space */
2538 edit_push_undo_action (edit
, BACKSPACE
);
2540 edit_push_undo_action (edit
, BACKSPACE_BR
);
2541 /* update markers */
2542 edit
->mark1
+= (edit
->mark1
> edit
->buffer
.curs1
) ? 1 : 0;
2543 edit
->mark2
+= (edit
->mark2
> edit
->buffer
.curs1
) ? 1 : 0;
2544 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->buffer
.curs1
) ? 1 : 0;
2546 edit_buffer_insert (&edit
->buffer
, c
);
2549 /* --------------------------------------------------------------------------------------------- */
2550 /** same as edit_insert and move left */
2553 edit_insert_ahead (WEdit
* edit
, int c
)
2555 if (edit
->buffer
.curs1
< edit
->start_display
)
2557 edit
->start_display
++;
2561 edit_modification (edit
);
2564 book_mark_inc (edit
, edit
->buffer
.curs_line
);
2565 edit
->buffer
.lines
++;
2566 edit
->force
|= REDRAW_AFTER_CURSOR
;
2568 /* ordinary char and not space */
2570 edit_push_undo_action (edit
, DELCHAR
);
2572 edit_push_undo_action (edit
, DELCHAR_BR
);
2574 edit
->mark1
+= (edit
->mark1
>= edit
->buffer
.curs1
) ? 1 : 0;
2575 edit
->mark2
+= (edit
->mark2
>= edit
->buffer
.curs1
) ? 1 : 0;
2576 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->buffer
.curs1
) ? 1 : 0;
2578 edit_buffer_insert_ahead (&edit
->buffer
, c
);
2581 /* --------------------------------------------------------------------------------------------- */
2584 edit_insert_over (WEdit
* edit
)
2588 for (i
= 0; i
< edit
->over_col
; i
++)
2589 edit_insert (edit
, ' ');
2594 /* --------------------------------------------------------------------------------------------- */
2597 edit_delete (WEdit
* edit
, gboolean byte_delete
)
2600 int char_length
= 1;
2603 if (edit
->buffer
.curs2
== 0)
2607 /* if byte_delete == TRUE then delete only one byte not multibyte char */
2608 if (edit
->utf8
&& !byte_delete
)
2610 edit_buffer_get_utf (&edit
->buffer
, edit
->buffer
.curs1
, &char_length
);
2611 if (char_length
< 1)
2618 if (edit
->mark2
!= edit
->mark1
)
2619 edit_push_markers (edit
);
2621 for (i
= 1; i
<= char_length
; i
++)
2623 if (edit
->mark1
> edit
->buffer
.curs1
)
2626 edit
->end_mark_curs
--;
2628 if (edit
->mark2
> edit
->buffer
.curs1
)
2630 if (edit
->last_get_rule
> edit
->buffer
.curs1
)
2631 edit
->last_get_rule
--;
2633 p
= edit_buffer_delete (&edit
->buffer
);
2635 edit_push_undo_action (edit
, p
+ 256);
2638 edit_modification (edit
);
2641 book_mark_dec (edit
, edit
->buffer
.curs_line
);
2642 edit
->buffer
.lines
--;
2643 edit
->force
|= REDRAW_AFTER_CURSOR
;
2645 if (edit
->buffer
.curs1
< edit
->start_display
)
2647 edit
->start_display
--;
2655 /* --------------------------------------------------------------------------------------------- */
2658 edit_backspace (WEdit
* edit
, gboolean byte_delete
)
2661 int char_length
= 1;
2664 if (edit
->buffer
.curs1
== 0)
2667 if (edit
->mark2
!= edit
->mark1
)
2668 edit_push_markers (edit
);
2671 if (edit
->utf8
&& !byte_delete
)
2673 edit_buffer_get_prev_utf (&edit
->buffer
, edit
->buffer
.curs1
, &char_length
);
2674 if (char_length
< 1)
2681 for (i
= 1; i
<= char_length
; i
++)
2683 if (edit
->mark1
>= edit
->buffer
.curs1
)
2686 edit
->end_mark_curs
--;
2688 if (edit
->mark2
>= edit
->buffer
.curs1
)
2690 if (edit
->last_get_rule
>= edit
->buffer
.curs1
)
2691 edit
->last_get_rule
--;
2693 p
= edit_buffer_backspace (&edit
->buffer
);
2695 edit_push_undo_action (edit
, p
);
2697 edit_modification (edit
);
2700 book_mark_dec (edit
, edit
->buffer
.curs_line
);
2701 edit
->buffer
.curs_line
--;
2702 edit
->buffer
.lines
--;
2703 edit
->force
|= REDRAW_AFTER_CURSOR
;
2706 if (edit
->buffer
.curs1
< edit
->start_display
)
2708 edit
->start_display
--;
2716 /* --------------------------------------------------------------------------------------------- */
2717 /** moves the cursor right or left: increment positive or negative respectively */
2720 edit_cursor_move (WEdit
* edit
, off_t increment
)
2724 for (; increment
< 0 && edit
->buffer
.curs1
!= 0; increment
++)
2728 edit_push_undo_action (edit
, CURS_RIGHT
);
2730 c
= edit_buffer_get_previous_byte (&edit
->buffer
);
2731 edit_buffer_insert_ahead (&edit
->buffer
, c
);
2732 c
= edit_buffer_backspace (&edit
->buffer
);
2735 edit
->buffer
.curs_line
--;
2736 edit
->force
|= REDRAW_LINE_BELOW
;
2742 for (; increment
> 0 && edit
->buffer
.curs2
!= 0; increment
--)
2746 edit_push_undo_action (edit
, CURS_LEFT
);
2748 c
= edit_buffer_get_current_byte (&edit
->buffer
);
2749 edit_buffer_insert (&edit
->buffer
, c
);
2750 c
= edit_buffer_delete (&edit
->buffer
);
2753 edit
->buffer
.curs_line
++;
2754 edit
->force
|= REDRAW_LINE_ABOVE
;
2760 /* --------------------------------------------------------------------------------------------- */
2761 /* If cols is zero this returns the count of columns from current to upto. */
2762 /* If upto is zero returns index of cols across from current. */
2765 edit_move_forward3 (const WEdit
* edit
, off_t current
, long cols
, off_t upto
)
2776 q
= edit
->buffer
.size
+ 2;
2778 for (col
= 0, p
= current
; p
< q
; p
++)
2790 orig_c
= c
= edit_buffer_get_byte (&edit
->buffer
, p
);
2796 int char_length
= 1;
2798 utf_ch
= edit_buffer_get_utf (&edit
->buffer
, p
, &char_length
);
2799 if (mc_global
.utf8_display
)
2801 if (char_length
> 1)
2802 col
-= char_length
- 1;
2803 if (g_unichar_iswide (utf_ch
))
2806 else if (char_length
> 1 && g_unichar_isprint (utf_ch
))
2807 col
-= char_length
- 1;
2810 c
= convert_to_display_c (c
);
2814 return (upto
!= 0 ? (off_t
) col
: p
);
2816 col
+= TAB_SIZE
- col
% TAB_SIZE
;
2817 else if ((c
< 32 || c
== 127) && (orig_c
== c
2819 || (!mc_global
.utf8_display
&& !edit
->utf8
)
2822 /* '\r' is shown as ^M, so we must advance 2 characters */
2823 /* Caret notation for control characters */
2831 /* --------------------------------------------------------------------------------------------- */
2832 /** returns the current column position of the cursor */
2835 edit_get_col (const WEdit
* edit
)
2837 return (long) edit_move_forward3 (edit
, edit_buffer_get_current_bol (&edit
->buffer
), 0,
2838 edit
->buffer
.curs1
);
2841 /* --------------------------------------------------------------------------------------------- */
2842 /* Scrolling functions */
2843 /* --------------------------------------------------------------------------------------------- */
2846 edit_update_curs_row (WEdit
* edit
)
2848 edit
->curs_row
= edit
->buffer
.curs_line
- edit
->start_line
;
2851 /* --------------------------------------------------------------------------------------------- */
2854 edit_update_curs_col (WEdit
* edit
)
2856 edit
->curs_col
= (long) edit_move_forward3 (edit
, edit_buffer_get_current_bol (&edit
->buffer
),
2857 0, edit
->buffer
.curs1
);
2860 /* --------------------------------------------------------------------------------------------- */
2863 edit_get_curs_col (const WEdit
* edit
)
2865 return edit
->curs_col
;
2868 /* --------------------------------------------------------------------------------------------- */
2869 /** moves the display start position up by i lines */
2872 edit_scroll_upward (WEdit
* edit
, long i
)
2874 long lines_above
= edit
->start_line
;
2876 if (i
> lines_above
)
2880 edit
->start_line
-= i
;
2881 edit
->start_display
=
2882 edit_buffer_get_backward_offset (&edit
->buffer
, edit
->start_display
, i
);
2883 edit
->force
|= REDRAW_PAGE
;
2884 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2886 edit_update_curs_row (edit
);
2890 /* --------------------------------------------------------------------------------------------- */
2893 edit_scroll_downward (WEdit
* edit
, long i
)
2897 lines_below
= edit
->buffer
.lines
- edit
->start_line
- (WIDGET (edit
)->lines
- 1);
2898 if (lines_below
> 0)
2900 if (i
> lines_below
)
2902 edit
->start_line
+= i
;
2903 edit
->start_display
=
2904 edit_buffer_get_forward_offset (&edit
->buffer
, edit
->start_display
, i
, 0);
2905 edit
->force
|= REDRAW_PAGE
;
2906 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2908 edit_update_curs_row (edit
);
2911 /* --------------------------------------------------------------------------------------------- */
2914 edit_scroll_right (WEdit
* edit
, long i
)
2916 edit
->force
|= REDRAW_PAGE
;
2917 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2918 edit
->start_col
-= i
;
2921 /* --------------------------------------------------------------------------------------------- */
2924 edit_scroll_left (WEdit
* edit
, long i
)
2926 if (edit
->start_col
)
2928 edit
->start_col
+= i
;
2929 if (edit
->start_col
> 0)
2930 edit
->start_col
= 0;
2931 edit
->force
|= REDRAW_PAGE
;
2932 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2936 /* --------------------------------------------------------------------------------------------- */
2937 /* high level cursor movement commands */
2938 /* --------------------------------------------------------------------------------------------- */
2941 edit_move_to_prev_col (WEdit
* edit
, off_t p
)
2943 long prev
= edit
->prev_col
;
2944 long over
= edit
->over_col
;
2946 edit_cursor_move (edit
,
2947 edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->buffer
.curs1
);
2949 if (option_cursor_beyond_eol
)
2953 line_len
= (long) edit_move_forward3 (edit
, edit_buffer_get_current_bol (&edit
->buffer
), 0,
2954 edit_buffer_get_current_eol (&edit
->buffer
));
2955 if (line_len
< prev
+ edit
->over_col
)
2957 edit
->over_col
= prev
+ over
- line_len
;
2958 edit
->prev_col
= line_len
;
2959 edit
->curs_col
= line_len
;
2963 edit
->curs_col
= prev
+ over
;
2964 edit
->prev_col
= edit
->curs_col
;
2971 if (option_fake_half_tabs
&& is_in_indent (&edit
->buffer
))
2973 long fake_half_tabs
;
2975 edit_update_curs_col (edit
);
2977 fake_half_tabs
= HALF_TAB_SIZE
* space_width
;
2978 if (fake_half_tabs
!= 0 && edit
->curs_col
% fake_half_tabs
!= 0)
2983 edit
->curs_col
-= (edit
->curs_col
% fake_half_tabs
);
2984 p
= edit_buffer_get_current_bol (&edit
->buffer
);
2985 edit_cursor_move (edit
,
2986 edit_move_forward3 (edit
, p
, edit
->curs_col
,
2987 0) - edit
->buffer
.curs1
);
2988 if (!left_of_four_spaces (edit
))
2989 edit_cursor_move (edit
,
2990 edit_move_forward3 (edit
, p
, q
, 0) - edit
->buffer
.curs1
);
2996 /* --------------------------------------------------------------------------------------------- */
2997 /** check whether line in editor is blank or not
2999 * @param edit editor object
3000 * @param line number of line
3002 * @return TRUE if line in blank, FALSE otherwise
3006 edit_line_is_blank (WEdit
* edit
, long line
)
3008 return is_blank (&edit
->buffer
, edit_find_line (edit
, line
));
3011 /* --------------------------------------------------------------------------------------------- */
3012 /** move cursor to line 'line' */
3015 edit_move_to_line (WEdit
* e
, long line
)
3017 if (line
< e
->buffer
.curs_line
)
3018 edit_move_up (e
, e
->buffer
.curs_line
- line
, FALSE
);
3020 edit_move_down (e
, line
- e
->buffer
.curs_line
, FALSE
);
3021 edit_scroll_screen_over_cursor (e
);
3024 /* --------------------------------------------------------------------------------------------- */
3025 /** scroll window so that first visible line is 'line' */
3028 edit_move_display (WEdit
* e
, long line
)
3030 if (line
< e
->start_line
)
3031 edit_scroll_upward (e
, e
->start_line
- line
);
3033 edit_scroll_downward (e
, line
- e
->start_line
);
3036 /* --------------------------------------------------------------------------------------------- */
3037 /** save markers onto undo stack */
3040 edit_push_markers (WEdit
* edit
)
3042 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3043 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3044 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3047 /* --------------------------------------------------------------------------------------------- */
3050 edit_set_markers (WEdit
* edit
, off_t m1
, off_t m2
, long c1
, long c2
)
3059 /* --------------------------------------------------------------------------------------------- */
3060 /** highlight marker toggle */
3063 edit_mark_cmd (WEdit
* edit
, gboolean unmark
)
3065 edit_push_markers (edit
);
3068 edit_set_markers (edit
, 0, 0, 0, 0);
3069 edit
->force
|= REDRAW_PAGE
;
3071 else if (edit
->mark2
>= 0)
3073 edit
->end_mark_curs
= -1;
3074 edit_set_markers (edit
, edit
->buffer
.curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3075 edit
->curs_col
+ edit
->over_col
);
3076 edit
->force
|= REDRAW_PAGE
;
3080 edit
->end_mark_curs
= edit
->buffer
.curs1
;
3081 edit_set_markers (edit
, edit
->mark1
, edit
->buffer
.curs1
, edit
->column1
,
3082 edit
->curs_col
+ edit
->over_col
);
3086 /* --------------------------------------------------------------------------------------------- */
3087 /** highlight the word under cursor */
3090 edit_mark_current_word_cmd (WEdit
* edit
)
3094 for (pos
= edit
->buffer
.curs1
; pos
!= 0; pos
--)
3098 c1
= edit_buffer_get_byte (&edit
->buffer
, pos
);
3099 c2
= edit_buffer_get_byte (&edit
->buffer
, pos
- 1);
3100 if (!isspace (c1
) && isspace (c2
))
3102 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3107 for (; pos
< edit
->buffer
.size
; pos
++)
3111 c1
= edit_buffer_get_byte (&edit
->buffer
, pos
);
3112 c2
= edit_buffer_get_byte (&edit
->buffer
, pos
+ 1);
3113 if (!isspace (c1
) && isspace (c2
))
3115 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3118 edit
->mark2
= MIN (pos
+ 1, edit
->buffer
.size
);
3120 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3123 /* --------------------------------------------------------------------------------------------- */
3126 edit_mark_current_line_cmd (WEdit
* edit
)
3128 edit
->mark1
= edit_buffer_get_current_bol (&edit
->buffer
);
3129 edit
->mark2
= edit_buffer_get_current_eol (&edit
->buffer
);
3131 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3134 /* --------------------------------------------------------------------------------------------- */
3137 edit_delete_line (WEdit
* edit
)
3140 * Delete right part of the line.
3141 * Note that edit_buffer_get_byte() returns '\n' when byte position is
3144 while (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
3145 (void) edit_delete (edit
, TRUE
);
3149 * Note that edit_delete() will not corrupt anything if called while
3150 * cursor position is EOF.
3152 (void) edit_delete (edit
, TRUE
);
3155 * Delete left part of the line.
3156 * Note, that edit_buffer_get_byte() returns '\n' when byte position is < 0.
3158 while (edit_buffer_get_previous_byte (&edit
->buffer
) != '\n')
3159 (void) edit_backspace (edit
, TRUE
);
3162 /* --------------------------------------------------------------------------------------------- */
3165 edit_push_key_press (WEdit
* edit
)
3167 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3168 if (edit
->mark2
== -1)
3170 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3171 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3175 /* --------------------------------------------------------------------------------------------- */
3178 edit_find_bracket (WEdit
* edit
)
3180 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3181 if (edit
->last_bracket
!= edit
->bracket
)
3182 edit
->force
|= REDRAW_PAGE
;
3183 edit
->last_bracket
= edit
->bracket
;
3186 /* --------------------------------------------------------------------------------------------- */
3188 * This executes a command as though the user initiated it through a key
3189 * press. Callback with MSG_KEY as a message calls this after
3190 * translating the key press. This function can be used to pass any
3191 * command to the editor. Note that the screen wouldn't update
3192 * automatically. Either of command or char_for_insertion must be
3193 * passed as -1. Commands are executed, and char_for_insertion is
3194 * inserted at the cursor.
3198 edit_execute_key_command (WEdit
* edit
, long command
, int char_for_insertion
)
3200 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3202 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3205 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3208 if (macro_index
!= -1)
3210 edit
->force
|= REDRAW_COMPLETELY
;
3211 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3213 edit_store_macro_cmd (edit
);
3217 if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3219 edit_repeat_macro_cmd (edit
);
3225 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3227 record_macro_buf
[macro_index
].action
= command
;
3228 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3230 /* record the beginning of a set of editing actions initiated by a key press */
3231 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3232 edit_push_key_press (edit
);
3234 edit_execute_cmd (edit
, command
, char_for_insertion
);
3235 if (edit
->column_highlight
)
3236 edit
->force
|= REDRAW_PAGE
;
3239 /* --------------------------------------------------------------------------------------------- */
3241 This executes a command at a lower level than macro recording.
3242 It also does not push a key_press onto the undo stack. This means
3243 that if it is called many times, a single undo command will undo
3244 all of them. It also does not check for the Undo command.
3247 edit_execute_cmd (WEdit
* edit
, long command
, int char_for_insertion
)
3249 Widget
*w
= WIDGET (edit
);
3251 if (command
== CK_WindowFullscreen
)
3253 edit_toggle_fullscreen (edit
);
3257 /* handle window state */
3258 if (edit_handle_move_resize (edit
, command
))
3261 edit
->force
|= REDRAW_LINE
;
3263 /* The next key press will unhighlight the found string, so update
3265 if (edit
->found_len
|| edit
->column_highlight
)
3266 edit
->force
|= REDRAW_PAGE
;
3270 /* a mark command with shift-arrow */
3273 case CK_MarkToWordBegin
:
3274 case CK_MarkToWordEnd
:
3280 case CK_MarkPageDown
:
3281 case CK_MarkToFileBegin
:
3282 case CK_MarkToFileEnd
:
3283 case CK_MarkToPageBegin
:
3284 case CK_MarkToPageEnd
:
3285 case CK_MarkScrollUp
:
3286 case CK_MarkScrollDown
:
3287 case CK_MarkParagraphUp
:
3288 case CK_MarkParagraphDown
:
3289 /* a mark command with alt-arrow */
3290 case CK_MarkColumnPageUp
:
3291 case CK_MarkColumnPageDown
:
3292 case CK_MarkColumnLeft
:
3293 case CK_MarkColumnRight
:
3294 case CK_MarkColumnUp
:
3295 case CK_MarkColumnDown
:
3296 case CK_MarkColumnScrollUp
:
3297 case CK_MarkColumnScrollDown
:
3298 case CK_MarkColumnParagraphUp
:
3299 case CK_MarkColumnParagraphDown
:
3300 edit
->column_highlight
= 0;
3301 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3303 edit_mark_cmd (edit
, TRUE
); /* clear */
3304 edit_mark_cmd (edit
, FALSE
); /* marking on */
3306 edit
->highlight
= 1;
3309 /* any other command */
3311 if (edit
->highlight
)
3312 edit_mark_cmd (edit
, FALSE
); /* clear */
3313 edit
->highlight
= 0;
3316 /* first check for undo */
3317 if (command
== CK_Undo
)
3319 edit
->redo_stack_reset
= 0;
3320 edit_group_undo (edit
);
3321 edit
->found_len
= 0;
3322 edit
->prev_col
= edit_get_col (edit
);
3323 edit
->search_start
= edit
->buffer
.curs1
;
3326 /* check for redo */
3327 if (command
== CK_Redo
)
3329 edit
->redo_stack_reset
= 0;
3330 edit_do_redo (edit
);
3331 edit
->found_len
= 0;
3332 edit
->prev_col
= edit_get_col (edit
);
3333 edit
->search_start
= edit
->buffer
.curs1
;
3337 edit
->redo_stack_reset
= 1;
3339 /* An ordinary key press */
3340 if (char_for_insertion
>= 0)
3342 /* if non persistent selection and text selected */
3343 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3344 edit_block_delete_cmd (edit
);
3346 if (edit
->overwrite
)
3348 /* remove char only one time, after input first byte, multibyte chars */
3350 if (!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3352 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
3354 edit_delete (edit
, FALSE
);
3356 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3357 edit_insert_over (edit
);
3360 Encode 8-bit input as UTF-8, if display (locale) is *not* UTF-8,
3361 *but* source encoding *is* set to UTF-8; see ticket #3843 for the details.
3363 if (char_for_insertion
> 127 && str_isutf8 (get_codepage_id (mc_global
.source_codepage
))
3364 && !mc_global
.utf8_display
)
3366 unsigned char str
[UTF8_CHAR_LEN
+ 1];
3370 res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3380 while (i
<= UTF8_CHAR_LEN
&& str
[i
] != '\0')
3382 char_for_insertion
= str
[i
];
3383 edit_insert (edit
, char_for_insertion
);
3389 edit_insert (edit
, char_for_insertion
);
3391 if (option_auto_para_formatting
)
3393 format_paragraph (edit
, FALSE
);
3394 edit
->force
|= REDRAW_PAGE
;
3397 check_and_wrap_line (edit
);
3398 edit
->found_len
= 0;
3399 edit
->prev_col
= edit_get_col (edit
);
3400 edit
->search_start
= edit
->buffer
.curs1
;
3401 edit_find_bracket (edit
);
3407 case CK_TopOnScreen
:
3408 case CK_BottomOnScreen
:
3421 if (!option_persistent_selections
&& edit
->mark2
>= 0)
3423 if (edit
->column_highlight
)
3424 edit_push_undo_action (edit
, COLUMN_ON
);
3425 edit
->column_highlight
= 0;
3426 edit_mark_cmd (edit
, TRUE
);
3434 case CK_TopOnScreen
:
3435 case CK_BottomOnScreen
:
3436 case CK_MarkToPageBegin
:
3437 case CK_MarkToPageEnd
:
3442 case CK_MarkToWordBegin
:
3443 case CK_MarkToWordEnd
:
3446 case CK_MarkColumnUp
:
3447 case CK_MarkColumnDown
:
3448 if (edit
->mark2
== -1)
3449 break; /*marking is following the cursor: may need to highlight a whole line */
3454 edit
->force
|= REDRAW_CHAR_ONLY
;
3459 /* basic cursor key commands */
3463 /* if non persistent selection and text selected */
3464 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3465 edit_block_delete_cmd (edit
);
3466 else if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3468 else if (option_backspace_through_tabs
&& is_in_indent (&edit
->buffer
))
3470 while (edit_buffer_get_previous_byte (&edit
->buffer
) != '\n' && edit
->buffer
.curs1
> 0)
3471 edit_backspace (edit
, TRUE
);
3473 else if (option_fake_half_tabs
&& is_in_indent (&edit
->buffer
)
3474 && right_of_four_spaces (edit
))
3478 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3479 edit_backspace (edit
, TRUE
);
3482 edit_backspace (edit
, FALSE
);
3485 /* if non persistent selection and text selected */
3486 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3487 edit_block_delete_cmd (edit
);
3490 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3491 edit_insert_over (edit
);
3493 if (option_fake_half_tabs
&& is_in_indent (&edit
->buffer
) && left_of_four_spaces (edit
))
3497 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3498 edit_delete (edit
, TRUE
);
3501 edit_delete (edit
, FALSE
);
3504 case CK_DeleteToWordBegin
:
3506 edit_left_delete_word (edit
);
3508 case CK_DeleteToWordEnd
:
3509 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3510 edit_insert_over (edit
);
3512 edit_right_delete_word (edit
);
3515 edit_delete_line (edit
);
3517 case CK_DeleteToHome
:
3518 edit_delete_to_line_begin (edit
);
3520 case CK_DeleteToEnd
:
3521 edit_delete_to_line_end (edit
);
3525 if (option_auto_para_formatting
)
3527 edit_double_newline (edit
);
3528 if (option_return_does_auto_indent
&& !bracketed_pasting_in_progress
)
3529 edit_auto_indent (edit
);
3530 format_paragraph (edit
, FALSE
);
3534 edit_insert (edit
, '\n');
3535 if (option_return_does_auto_indent
&& !bracketed_pasting_in_progress
)
3536 edit_auto_indent (edit
);
3540 edit_insert (edit
, '\n');
3543 case CK_MarkColumnPageUp
:
3544 edit
->column_highlight
= 1;
3547 edit_move_up (edit
, w
->lines
- 1, TRUE
);
3549 case CK_MarkColumnPageDown
:
3550 edit
->column_highlight
= 1;
3552 case CK_MarkPageDown
:
3553 edit_move_down (edit
, w
->lines
- 1, TRUE
);
3555 case CK_MarkColumnLeft
:
3556 edit
->column_highlight
= 1;
3559 if (option_fake_half_tabs
&& is_in_indent (&edit
->buffer
) && right_of_four_spaces (edit
))
3561 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3564 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3565 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3568 edit_left_char_move_cmd (edit
);
3570 case CK_MarkColumnRight
:
3571 edit
->column_highlight
= 1;
3574 if (option_fake_half_tabs
&& is_in_indent (&edit
->buffer
) && left_of_four_spaces (edit
))
3576 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3577 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3580 edit_right_char_move_cmd (edit
);
3582 case CK_TopOnScreen
:
3583 case CK_MarkToPageBegin
:
3584 edit_begin_page (edit
);
3586 case CK_BottomOnScreen
:
3587 case CK_MarkToPageEnd
:
3588 edit_end_page (edit
);
3591 case CK_MarkToWordBegin
:
3593 edit_left_word_move_cmd (edit
);
3596 case CK_MarkToWordEnd
:
3598 edit_right_word_move_cmd (edit
);
3600 case CK_MarkColumnUp
:
3601 edit
->column_highlight
= 1;
3604 edit_move_up (edit
, 1, FALSE
);
3606 case CK_MarkColumnDown
:
3607 edit
->column_highlight
= 1;
3610 edit_move_down (edit
, 1, FALSE
);
3612 case CK_MarkColumnParagraphUp
:
3613 edit
->column_highlight
= 1;
3614 case CK_ParagraphUp
:
3615 case CK_MarkParagraphUp
:
3616 edit_move_up_paragraph (edit
, FALSE
);
3618 case CK_MarkColumnParagraphDown
:
3619 edit
->column_highlight
= 1;
3620 case CK_ParagraphDown
:
3621 case CK_MarkParagraphDown
:
3622 edit_move_down_paragraph (edit
, FALSE
);
3624 case CK_MarkColumnScrollUp
:
3625 edit
->column_highlight
= 1;
3627 case CK_MarkScrollUp
:
3628 edit_move_up (edit
, 1, TRUE
);
3630 case CK_MarkColumnScrollDown
:
3631 edit
->column_highlight
= 1;
3633 case CK_MarkScrollDown
:
3634 edit_move_down (edit
, 1, TRUE
);
3638 edit_cursor_to_bol (edit
);
3642 edit_cursor_to_eol (edit
);
3645 /* if text marked shift block */
3646 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3648 if (edit
->mark2
< 0)
3649 edit_mark_cmd (edit
, FALSE
);
3650 edit_move_block_to_right (edit
);
3654 if (option_cursor_beyond_eol
)
3655 edit_insert_over (edit
);
3656 edit_tab_cmd (edit
);
3657 if (option_auto_para_formatting
)
3659 format_paragraph (edit
, FALSE
);
3660 edit
->force
|= REDRAW_PAGE
;
3663 check_and_wrap_line (edit
);
3667 case CK_InsertOverwrite
:
3668 edit
->overwrite
= !edit
->overwrite
;
3672 if (edit
->mark2
>= 0)
3674 if (edit
->column_highlight
)
3675 edit_push_undo_action (edit
, COLUMN_ON
);
3676 edit
->column_highlight
= 0;
3678 edit_mark_cmd (edit
, FALSE
);
3681 if (!edit
->column_highlight
)
3682 edit_push_undo_action (edit
, COLUMN_OFF
);
3683 edit
->column_highlight
= 1;
3684 edit_mark_cmd (edit
, FALSE
);
3687 edit_set_markers (edit
, 0, edit
->buffer
.size
, 0, 0);
3688 edit
->force
|= REDRAW_PAGE
;
3691 if (edit
->column_highlight
)
3692 edit_push_undo_action (edit
, COLUMN_ON
);
3693 edit
->column_highlight
= 0;
3694 edit_mark_cmd (edit
, TRUE
);
3697 if (edit
->column_highlight
)
3698 edit_push_undo_action (edit
, COLUMN_ON
);
3699 edit
->column_highlight
= 0;
3700 edit_mark_current_word_cmd (edit
);
3703 if (edit
->column_highlight
)
3704 edit_push_undo_action (edit
, COLUMN_ON
);
3705 edit
->column_highlight
= 0;
3706 edit_mark_current_line_cmd (edit
);
3710 book_mark_clear (edit
, edit
->buffer
.curs_line
, BOOK_MARK_FOUND_COLOR
);
3711 if (book_mark_query_color (edit
, edit
->buffer
.curs_line
, BOOK_MARK_COLOR
))
3712 book_mark_clear (edit
, edit
->buffer
.curs_line
, BOOK_MARK_COLOR
);
3714 book_mark_insert (edit
, edit
->buffer
.curs_line
, BOOK_MARK_COLOR
);
3716 case CK_BookmarkFlush
:
3717 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3718 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3719 edit
->force
|= REDRAW_PAGE
;
3721 case CK_BookmarkNext
:
3722 if (edit
->book_mark
!= NULL
)
3724 edit_book_mark_t
*p
;
3726 p
= book_mark_find (edit
, edit
->buffer
.curs_line
);
3727 if (p
->next
!= NULL
)
3730 if (p
->line
>= edit
->start_line
+ w
->lines
|| p
->line
< edit
->start_line
)
3731 edit_move_display (edit
, p
->line
- w
->lines
/ 2);
3732 edit_move_to_line (edit
, p
->line
);
3736 case CK_BookmarkPrev
:
3737 if (edit
->book_mark
!= NULL
)
3739 edit_book_mark_t
*p
;
3741 p
= book_mark_find (edit
, edit
->buffer
.curs_line
);
3742 while (p
->line
== edit
->buffer
.curs_line
)
3743 if (p
->prev
!= NULL
)
3747 if (p
->line
>= edit
->start_line
+ w
->lines
|| p
->line
< edit
->start_line
)
3748 edit_move_display (edit
, p
->line
- w
->lines
/ 2);
3749 edit_move_to_line (edit
, p
->line
);
3755 case CK_MarkToFileBegin
:
3756 edit_move_to_top (edit
);
3759 case CK_MarkToFileEnd
:
3760 edit_move_to_bottom (edit
);
3764 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3765 edit_insert_over (edit
);
3766 edit_block_copy_cmd (edit
);
3769 edit_block_delete_cmd (edit
);
3772 edit_block_move_cmd (edit
);
3775 case CK_BlockShiftLeft
:
3776 if (edit
->mark1
!= edit
->mark2
)
3777 edit_move_block_to_left (edit
);
3779 case CK_BlockShiftRight
:
3780 if (edit
->mark1
!= edit
->mark2
)
3781 edit_move_block_to_right (edit
);
3784 edit_copy_to_X_buf_cmd (edit
);
3787 edit_cut_to_X_buf_cmd (edit
);
3790 /* if non persistent selection and text selected */
3791 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3792 edit_block_delete_cmd (edit
);
3793 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3794 edit_insert_over (edit
);
3795 edit_paste_from_X_buf_cmd (edit
);
3796 if (!option_persistent_selections
&& edit
->mark2
>= 0)
3798 if (edit
->column_highlight
)
3799 edit_push_undo_action (edit
, COLUMN_ON
);
3800 edit
->column_highlight
= 0;
3801 edit_mark_cmd (edit
, TRUE
);
3805 edit_paste_from_history (edit
);
3809 edit_save_as_cmd (edit
);
3812 edit_save_confirm_cmd (edit
);
3815 edit_save_block_cmd (edit
);
3818 edit_insert_file_cmd (edit
);
3822 edit_load_back_cmd (edit
);
3825 edit_load_forward_cmd (edit
);
3828 case CK_SyntaxChoose
:
3829 edit_syntax_dialog (edit
);
3833 edit_search_cmd (edit
, FALSE
);
3835 case CK_SearchContinue
:
3836 edit_search_cmd (edit
, TRUE
);
3839 edit_replace_cmd (edit
, FALSE
);
3841 case CK_ReplaceContinue
:
3842 edit_replace_cmd (edit
, TRUE
);
3845 /* if text marked shift block */
3846 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3847 edit_move_block_to_left (edit
);
3849 edit_complete_word_cmd (edit
);
3852 edit_get_match_keyword_cmd (edit
);
3856 case CK_SpellCheckCurrentWord
:
3857 edit_suggest_current_word (edit
);
3860 edit_spellcheck_file (edit
);
3862 case CK_SpellCheckSelectLang
:
3863 edit_set_spell_lang ();
3870 /* fool gcc to prevent a Y2K warning */
3871 char time_format
[] = "_c";
3872 time_format
[0] = '%';
3874 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
3875 edit_print_string (edit
, s
);
3876 edit
->force
|= REDRAW_PAGE
;
3880 edit_goto_cmd (edit
);
3882 case CK_ParagraphFormat
:
3883 format_paragraph (edit
, TRUE
);
3884 edit
->force
|= REDRAW_PAGE
;
3886 case CK_MacroDelete
:
3887 edit_delete_macro_cmd (edit
);
3889 case CK_MatchBracket
:
3890 edit_goto_matching_bracket (edit
);
3893 user_menu (edit
, NULL
, -1);
3896 edit_sort_cmd (edit
);
3898 case CK_ExternalCommand
:
3899 edit_ext_cmd (edit
);
3902 edit_mail_dialog (edit
);
3905 case CK_SelectCodepage
:
3906 edit_select_codepage_cmd (edit
);
3909 case CK_InsertLiteral
:
3910 edit_insert_literal_cmd (edit
);
3912 case CK_MacroStartStopRecord
:
3913 edit_begin_end_macro_cmd (edit
);
3915 case CK_RepeatStartStopRecord
:
3916 edit_begin_end_repeat_cmd (edit
);
3918 case CK_ExtendedKeyMap
:
3919 edit
->extmod
= TRUE
;
3926 if ((command
/ CK_PipeBlock (0)) == 1)
3927 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
3929 /* keys which must set the col position, and the search vars */
3933 case CK_SearchContinue
:
3935 case CK_ReplaceContinue
:
3937 edit
->prev_col
= edit_get_col (edit
);
3941 case CK_MarkColumnUp
:
3944 case CK_MarkColumnDown
:
3947 case CK_MarkColumnPageUp
:
3949 case CK_MarkPageDown
:
3950 case CK_MarkColumnPageDown
:
3952 case CK_MarkToFileBegin
:
3954 case CK_MarkToFileEnd
:
3955 case CK_ParagraphUp
:
3956 case CK_MarkParagraphUp
:
3957 case CK_MarkColumnParagraphUp
:
3958 case CK_ParagraphDown
:
3959 case CK_MarkParagraphDown
:
3960 case CK_MarkColumnParagraphDown
:
3962 case CK_MarkScrollUp
:
3963 case CK_MarkColumnScrollUp
:
3965 case CK_MarkScrollDown
:
3966 case CK_MarkColumnScrollDown
:
3967 edit
->search_start
= edit
->buffer
.curs1
;
3968 edit
->found_len
= 0;
3971 edit
->found_len
= 0;
3972 edit
->prev_col
= edit_get_col (edit
);
3973 edit
->search_start
= edit
->buffer
.curs1
;
3975 edit_find_bracket (edit
);
3977 if (option_auto_para_formatting
)
3983 case CK_DeleteToWordBegin
:
3984 case CK_DeleteToWordEnd
:
3985 case CK_DeleteToHome
:
3986 case CK_DeleteToEnd
:
3987 format_paragraph (edit
, FALSE
);
3988 edit
->force
|= REDRAW_PAGE
;
3995 /* --------------------------------------------------------------------------------------------- */
3998 edit_stack_init (void)
4000 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4002 edit_history_moveto
[edit_stack_iterator
].filename_vpath
= NULL
;
4003 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4006 edit_stack_iterator
= 0;
4009 /* --------------------------------------------------------------------------------------------- */
4012 edit_stack_free (void)
4014 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4015 vfs_path_free (edit_history_moveto
[edit_stack_iterator
].filename_vpath
);
4018 /* --------------------------------------------------------------------------------------------- */
4022 edit_move_up (WEdit
* edit
, long i
, gboolean do_scroll
)
4024 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4027 /* --------------------------------------------------------------------------------------------- */
4031 edit_move_down (WEdit
* edit
, long i
, gboolean do_scroll
)
4033 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4036 /* --------------------------------------------------------------------------------------------- */