2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996-2016
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 int option_typewriter_wrap
= 0;
80 int option_auto_para_formatting
= 0;
81 int option_fill_tabs_with_spaces
= 0;
82 int option_return_does_auto_indent
= 1;
83 int option_backspace_through_tabs
= 0;
84 int option_fake_half_tabs
= 1;
85 int option_save_mode
= EDIT_QUICK_SAVE
;
86 int option_save_position
= 1;
87 int option_max_undo
= 32768;
88 int option_persistent_selections
= 1;
89 int option_cursor_beyond_eol
= 0;
90 int option_line_state
= 0;
91 int option_line_state_width
= 0;
92 gboolean option_cursor_after_inserted_block
= FALSE
;
93 int option_state_full_filename
= 0;
95 int enable_show_tabs_tws
= 1;
96 int option_check_nl_at_eof
= 0;
97 int option_group_undo
= 0;
98 int show_right_margin
= 0;
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 /* --------------------------------------------------------------------------------------------- */
458 /** Restore saved cursor position in the file */
461 edit_load_position (WEdit
* edit
)
466 if (edit
->filename_vpath
== NULL
467 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
470 load_file_position (edit
->filename_vpath
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
474 edit_move_to_line (edit
, line
- 1);
475 edit
->prev_col
= column
;
479 edit_cursor_move (edit
, offset
);
480 line
= edit
->buffer
.curs_line
;
481 edit
->search_start
= edit
->buffer
.curs1
;
484 book_mark_restore (edit
, BOOK_MARK_COLOR
);
486 edit_move_to_prev_col (edit
, edit_buffer_get_current_bol (&edit
->buffer
));
487 edit_move_display (edit
, line
- (WIDGET (edit
)->lines
/ 2));
490 /* --------------------------------------------------------------------------------------------- */
491 /** Save cursor position in the file */
494 edit_save_position (WEdit
* edit
)
496 if (edit
->filename_vpath
== NULL
497 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
500 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
501 save_file_position (edit
->filename_vpath
, edit
->buffer
.curs_line
+ 1, edit
->curs_col
,
502 edit
->buffer
.curs1
, edit
->serialized_bookmarks
);
503 edit
->serialized_bookmarks
= NULL
;
506 /* --------------------------------------------------------------------------------------------- */
507 /** Clean the WEdit stricture except the widget part */
510 edit_purge_widget (WEdit
* edit
)
512 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
513 char *start
= (char *) edit
+ sizeof (Widget
);
514 memset (start
, 0, len
);
517 /* --------------------------------------------------------------------------------------------- */
520 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
521 then the file should be as it was when he loaded up. Then set edit->modified to 0.
525 edit_pop_undo_action (WEdit
* edit
)
528 unsigned long sp
= edit
->undo_stack_pointer
;
530 if (sp
== edit
->undo_stack_bottom
)
533 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
534 c
= edit
->undo_stack
[sp
];
537 /* edit->undo_stack[sp] = '@'; */
538 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
542 if (sp
== edit
->undo_stack_bottom
)
545 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
546 if (edit
->undo_stack
[sp
] == -2)
548 /* edit->undo_stack[sp] = '@'; */
549 edit
->undo_stack_pointer
= sp
;
552 edit
->undo_stack
[sp
]++;
558 edit_pop_redo_action (WEdit
* edit
)
561 unsigned long sp
= edit
->redo_stack_pointer
;
563 if (sp
== edit
->redo_stack_bottom
)
566 sp
= (sp
- 1) & edit
->redo_stack_size_mask
;
567 c
= edit
->redo_stack
[sp
];
570 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
574 if (sp
== edit
->redo_stack_bottom
)
577 c
= edit
->redo_stack
[(sp
- 1) & edit
->redo_stack_size_mask
];
578 if (edit
->redo_stack
[sp
] == -2)
579 edit
->redo_stack_pointer
= sp
;
581 edit
->redo_stack
[sp
]++;
587 get_prev_undo_action (WEdit
* edit
)
590 unsigned long sp
= edit
->undo_stack_pointer
;
592 if (sp
== edit
->undo_stack_bottom
)
595 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
596 c
= edit
->undo_stack
[sp
];
600 if (sp
== edit
->undo_stack_bottom
)
603 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
607 /* --------------------------------------------------------------------------------------------- */
608 /** is called whenever a modification is made by one of the four routines below */
611 edit_modification (WEdit
* edit
)
613 edit
->caches_valid
= FALSE
;
615 /* raise lock when file modified */
616 if (!edit
->modified
&& !edit
->delete_file
)
617 edit
->locked
= lock_file (edit
->filename_vpath
);
621 /* --------------------------------------------------------------------------------------------- */
622 /* high level cursor movement commands */
623 /* --------------------------------------------------------------------------------------------- */
624 /** check whether cursor is in indent part of line
626 * @param edit editor object
628 * @return TRUE if cursor is in indent, FALSE otherwise
632 is_in_indent (const edit_buffer_t
* buf
)
636 for (p
= edit_buffer_get_current_bol (buf
); p
< buf
->curs1
; p
++)
637 if (strchr (" \t", edit_buffer_get_byte (buf
, p
)) == NULL
)
643 /* --------------------------------------------------------------------------------------------- */
644 /** check whether line in editor is blank or not
646 * @param edit editor object
647 * @param offset position in file
649 * @return TRUE if line in blank, FALSE otherwise
653 is_blank (const edit_buffer_t
* buf
, off_t offset
)
657 s
= edit_buffer_get_bol (buf
, offset
);
658 f
= edit_buffer_get_eol (buf
, offset
) - 1;
663 c
= edit_buffer_get_byte (buf
, s
++);
670 /* --------------------------------------------------------------------------------------------- */
671 /** returns the offset of line i */
674 edit_find_line (WEdit
* edit
, long line
)
677 long m
= 2000000000; /* what is the magic number? */
679 if (!edit
->caches_valid
)
681 memset (edit
->line_numbers
, 0, sizeof (edit
->line_numbers
));
682 memset (edit
->line_offsets
, 0, sizeof (edit
->line_offsets
));
683 /* three offsets that we *know* are line 0 at 0 and these two: */
684 edit
->line_numbers
[1] = edit
->buffer
.curs_line
;
685 edit
->line_offsets
[1] = edit_buffer_get_current_bol (&edit
->buffer
);
686 edit
->line_numbers
[2] = edit
->buffer
.lines
;
687 edit
->line_offsets
[2] = edit_buffer_get_bol (&edit
->buffer
, edit
->buffer
.size
);
688 edit
->caches_valid
= TRUE
;
690 if (line
>= edit
->buffer
.lines
)
691 return edit
->line_offsets
[2];
694 /* find the closest known point */
695 for (i
= 0; i
< N_LINE_CACHES
; i
++)
699 n
= labs (edit
->line_numbers
[i
] - line
);
707 return edit
->line_offsets
[j
]; /* know the offset exactly */
708 if (m
== 1 && j
>= 3)
709 i
= j
; /* one line different - caller might be looping, so stay in this cache */
711 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
712 if (line
> edit
->line_numbers
[j
])
713 edit
->line_offsets
[i
] =
714 edit_buffer_get_forward_offset (&edit
->buffer
, edit
->line_offsets
[j
],
715 line
- edit
->line_numbers
[j
], 0);
717 edit
->line_offsets
[i
] =
718 edit_buffer_get_backward_offset (&edit
->buffer
, edit
->line_offsets
[j
],
719 edit
->line_numbers
[j
] - line
);
720 edit
->line_numbers
[i
] = line
;
721 return edit
->line_offsets
[i
];
724 /* --------------------------------------------------------------------------------------------- */
725 /** moves up until a blank line is reached, or until just
726 before a non-blank line is reached */
729 edit_move_up_paragraph (WEdit
* edit
, gboolean do_scroll
)
733 if (edit
->buffer
.curs_line
> 1)
735 if (!edit_line_is_blank (edit
, edit
->buffer
.curs_line
))
737 for (i
= edit
->buffer
.curs_line
- 1; i
!= 0; i
--)
738 if (edit_line_is_blank (edit
, i
))
741 else if (edit_line_is_blank (edit
, edit
->buffer
.curs_line
- 1))
743 for (i
= edit
->buffer
.curs_line
- 1; i
!= 0; i
--)
744 if (!edit_line_is_blank (edit
, i
))
752 for (i
= edit
->buffer
.curs_line
- 1; i
!= 0; i
--)
753 if (edit_line_is_blank (edit
, i
))
758 edit_move_up (edit
, edit
->buffer
.curs_line
- i
, do_scroll
);
761 /* --------------------------------------------------------------------------------------------- */
762 /** moves down until a blank line is reached, or until just
763 before a non-blank line is reached */
766 edit_move_down_paragraph (WEdit
* edit
, gboolean do_scroll
)
770 if (edit
->buffer
.curs_line
>= edit
->buffer
.lines
- 1)
771 i
= edit
->buffer
.lines
;
772 else if (!edit_line_is_blank (edit
, edit
->buffer
.curs_line
))
774 for (i
= edit
->buffer
.curs_line
+ 1; i
!= 0; i
++)
775 if (edit_line_is_blank (edit
, i
) || i
>= edit
->buffer
.lines
)
778 else if (edit_line_is_blank (edit
, edit
->buffer
.curs_line
+ 1))
780 for (i
= edit
->buffer
.curs_line
+ 1; i
!= 0; i
++)
781 if (!edit_line_is_blank (edit
, i
) || i
> edit
->buffer
.lines
)
789 for (i
= edit
->buffer
.curs_line
+ 1; i
!= 0; i
++)
790 if (edit_line_is_blank (edit
, i
) || i
>= edit
->buffer
.lines
)
793 edit_move_down (edit
, i
- edit
->buffer
.curs_line
, do_scroll
);
796 /* --------------------------------------------------------------------------------------------- */
799 edit_begin_page (WEdit
* edit
)
801 edit_update_curs_row (edit
);
802 edit_move_up (edit
, edit
->curs_row
, FALSE
);
805 /* --------------------------------------------------------------------------------------------- */
808 edit_end_page (WEdit
* edit
)
810 edit_update_curs_row (edit
);
811 edit_move_down (edit
, WIDGET (edit
)->lines
- edit
->curs_row
- 1, FALSE
);
815 /* --------------------------------------------------------------------------------------------- */
816 /** goto beginning of text */
819 edit_move_to_top (WEdit
* edit
)
821 if (edit
->buffer
.curs_line
!= 0)
823 edit_cursor_move (edit
, -edit
->buffer
.curs1
);
824 edit_move_to_prev_col (edit
, 0);
825 edit
->force
|= REDRAW_PAGE
;
826 edit
->search_start
= 0;
827 edit_update_curs_row (edit
);
831 /* --------------------------------------------------------------------------------------------- */
832 /** goto end of text */
835 edit_move_to_bottom (WEdit
* edit
)
837 if (edit
->buffer
.curs_line
< edit
->buffer
.lines
)
839 edit_move_down (edit
, edit
->buffer
.lines
- edit
->curs_row
, FALSE
);
840 edit
->start_display
= edit
->buffer
.size
;
841 edit
->start_line
= edit
->buffer
.lines
;
842 edit_scroll_upward (edit
, WIDGET (edit
)->lines
- 1);
843 edit
->force
|= REDRAW_PAGE
;
847 /* --------------------------------------------------------------------------------------------- */
848 /** goto beginning of line */
851 edit_cursor_to_bol (WEdit
* edit
)
853 edit_cursor_move (edit
, edit_buffer_get_current_bol (&edit
->buffer
) - edit
->buffer
.curs1
);
854 edit
->search_start
= edit
->buffer
.curs1
;
855 edit
->prev_col
= edit_get_col (edit
);
859 /* --------------------------------------------------------------------------------------------- */
860 /** goto end of line */
863 edit_cursor_to_eol (WEdit
* edit
)
865 edit_cursor_move (edit
, edit_buffer_get_current_eol (&edit
->buffer
) - edit
->buffer
.curs1
);
866 edit
->search_start
= edit
->buffer
.curs1
;
867 edit
->prev_col
= edit_get_col (edit
);
871 /* --------------------------------------------------------------------------------------------- */
876 unsigned long x
, r
= 0;
878 const char option_chars_move_whole_word
[] =
879 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
885 if (*option_chars_move_whole_word
== '!')
889 if (g_ascii_isupper ((gchar
) c
))
891 else if (g_ascii_islower ((gchar
) c
))
893 else if (g_ascii_isalpha (c
))
895 else if (isdigit (c
))
897 else if (isspace (c
))
899 q
= strchr (option_chars_move_whole_word
, c
);
904 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
909 while ((q
= strchr (q
+ 1, c
)));
913 /* --------------------------------------------------------------------------------------------- */
916 edit_left_word_move (WEdit
* edit
, int s
)
922 if (edit
->column_highlight
923 && edit
->mark1
!= edit
->mark2
924 && edit
->over_col
== 0
925 && edit
->buffer
.curs1
== edit_buffer_get_current_bol (&edit
->buffer
))
927 edit_cursor_move (edit
, -1);
928 if (edit
->buffer
.curs1
== 0)
930 c1
= edit_buffer_get_previous_byte (&edit
->buffer
);
931 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
932 if (c1
== '\n' || c2
== '\n')
934 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
936 if (isspace (c1
) && !isspace (c2
))
938 if (s
!= 0 && !isspace (c1
) && isspace (c2
))
943 /* --------------------------------------------------------------------------------------------- */
946 edit_left_word_move_cmd (WEdit
* edit
)
948 edit_left_word_move (edit
, 0);
949 edit
->force
|= REDRAW_PAGE
;
952 /* --------------------------------------------------------------------------------------------- */
955 edit_right_word_move (WEdit
* edit
, int s
)
961 if (edit
->column_highlight
962 && edit
->mark1
!= edit
->mark2
963 && edit
->over_col
== 0
964 && edit
->buffer
.curs1
== edit_buffer_get_current_eol (&edit
->buffer
))
966 edit_cursor_move (edit
, 1);
967 if (edit
->buffer
.curs1
>= edit
->buffer
.size
)
969 c1
= edit_buffer_get_previous_byte (&edit
->buffer
);
970 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
971 if (c1
== '\n' || c2
== '\n')
973 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
975 if (isspace (c1
) && !isspace (c2
))
977 if (s
!= 0 && !isspace (c1
) && isspace (c2
))
982 /* --------------------------------------------------------------------------------------------- */
985 edit_right_word_move_cmd (WEdit
* edit
)
987 edit_right_word_move (edit
, 0);
988 edit
->force
|= REDRAW_PAGE
;
991 /* --------------------------------------------------------------------------------------------- */
994 edit_right_char_move_cmd (WEdit
* edit
)
1002 c
= edit_buffer_get_utf (&edit
->buffer
, edit
->buffer
.curs1
, &char_length
);
1003 if (char_length
< 1)
1008 c
= edit_buffer_get_current_byte (&edit
->buffer
);
1010 if (option_cursor_beyond_eol
&& c
== '\n')
1013 edit_cursor_move (edit
, char_length
);
1016 /* --------------------------------------------------------------------------------------------- */
1019 edit_left_char_move_cmd (WEdit
* edit
)
1021 int char_length
= 1;
1023 if (edit
->column_highlight
1024 && option_cursor_beyond_eol
1025 && edit
->mark1
!= edit
->mark2
1026 && edit
->over_col
== 0 && edit
->buffer
.curs1
== edit_buffer_get_current_bol (&edit
->buffer
))
1031 edit_buffer_get_prev_utf (&edit
->buffer
, edit
->buffer
.curs1
, &char_length
);
1032 if (char_length
< 1)
1037 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1040 edit_cursor_move (edit
, -char_length
);
1043 /* --------------------------------------------------------------------------------------------- */
1044 /** Up or down cursor moving.
1045 direction = TRUE - move up
1050 edit_move_updown (WEdit
* edit
, long lines
, gboolean do_scroll
, gboolean direction
)
1053 long l
= direction
? edit
->buffer
.curs_line
: edit
->buffer
.lines
- edit
->buffer
.curs_line
;
1062 edit
->force
|= REDRAW_PAGE
;
1066 edit_scroll_upward (edit
, lines
);
1068 edit_scroll_downward (edit
, lines
);
1070 p
= edit_buffer_get_current_bol (&edit
->buffer
);
1071 p
= direction
? edit_buffer_get_backward_offset (&edit
->buffer
, p
, lines
) :
1072 edit_buffer_get_forward_offset (&edit
->buffer
, p
, lines
, 0);
1073 edit_cursor_move (edit
, p
- edit
->buffer
.curs1
);
1074 edit_move_to_prev_col (edit
, p
);
1077 /* search start of current multibyte char (like CJK) */
1078 if (edit
->buffer
.curs1
> 0 && edit
->buffer
.curs1
+ 1 < edit
->buffer
.size
1079 && edit_buffer_get_current_byte (&edit
->buffer
) >= 256)
1081 edit_right_char_move_cmd (edit
);
1082 edit_left_char_move_cmd (edit
);
1086 edit
->search_start
= edit
->buffer
.curs1
;
1087 edit
->found_len
= 0;
1090 /* --------------------------------------------------------------------------------------------- */
1093 edit_right_delete_word (WEdit
* edit
)
1095 while (edit
->buffer
.curs1
< edit
->buffer
.size
)
1099 c1
= edit_delete (edit
, TRUE
);
1100 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
1101 if (c1
== '\n' || c2
== '\n')
1103 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1105 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1110 /* --------------------------------------------------------------------------------------------- */
1113 edit_left_delete_word (WEdit
* edit
)
1115 while (edit
->buffer
.curs1
> 0)
1119 c1
= edit_backspace (edit
, TRUE
);
1120 c2
= edit_buffer_get_previous_byte (&edit
->buffer
);
1121 if (c1
== '\n' || c2
== '\n')
1123 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1125 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1130 /* --------------------------------------------------------------------------------------------- */
1132 the start column position is not recorded, and hence does not
1133 undo as it happed. But who would notice.
1137 edit_do_undo (WEdit
* edit
)
1142 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1144 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1151 edit_cursor_move (edit
, 1);
1154 edit_cursor_move (edit
, -1);
1158 edit_backspace (edit
, TRUE
);
1162 edit_delete (edit
, TRUE
);
1165 edit
->column_highlight
= 1;
1168 edit
->column_highlight
= 0;
1173 if (ac
>= 256 && ac
< 512)
1174 edit_insert_ahead (edit
, ac
- 256);
1175 if (ac
>= 0 && ac
< 256)
1176 edit_insert (edit
, ac
);
1178 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1180 edit
->mark1
= ac
- MARK_1
;
1182 (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, edit
->mark1
),
1185 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1187 edit
->mark2
= ac
- MARK_2
;
1189 (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, edit
->mark2
),
1192 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1194 edit
->end_mark_curs
= ac
- MARK_CURS
;
1197 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1200 if (edit
->start_display
> ac
- KEY_PRESS
)
1203 edit_buffer_count_lines (&edit
->buffer
, ac
- KEY_PRESS
, edit
->start_display
);
1204 edit
->force
|= REDRAW_PAGE
;
1206 else if (edit
->start_display
< ac
- KEY_PRESS
)
1209 edit_buffer_count_lines (&edit
->buffer
, edit
->start_display
, ac
- KEY_PRESS
);
1210 edit
->force
|= REDRAW_PAGE
;
1212 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1213 edit_update_curs_row (edit
);
1216 edit
->undo_stack_disable
= 0;
1219 /* --------------------------------------------------------------------------------------------- */
1222 edit_do_redo (WEdit
* edit
)
1227 if (edit
->redo_stack_reset
)
1231 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1238 edit_cursor_move (edit
, 1);
1241 edit_cursor_move (edit
, -1);
1244 edit_backspace (edit
, TRUE
);
1247 edit_delete (edit
, TRUE
);
1250 edit
->column_highlight
= 1;
1253 edit
->column_highlight
= 0;
1258 if (ac
>= 256 && ac
< 512)
1259 edit_insert_ahead (edit
, ac
- 256);
1260 if (ac
>= 0 && ac
< 256)
1261 edit_insert (edit
, ac
);
1263 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1265 edit
->mark1
= ac
- MARK_1
;
1267 (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, edit
->mark1
),
1270 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1272 edit
->mark2
= ac
- MARK_2
;
1274 (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, edit
->mark2
),
1277 /* more than one pop usually means something big */
1279 edit
->force
|= REDRAW_PAGE
;
1282 if (edit
->start_display
> ac
- KEY_PRESS
)
1285 edit_buffer_count_lines (&edit
->buffer
, ac
- KEY_PRESS
, edit
->start_display
);
1286 edit
->force
|= REDRAW_PAGE
;
1288 else if (edit
->start_display
< ac
- KEY_PRESS
)
1291 edit_buffer_count_lines (&edit
->buffer
, edit
->start_display
, ac
- KEY_PRESS
);
1292 edit
->force
|= REDRAW_PAGE
;
1294 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1295 edit_update_curs_row (edit
);
1301 /* --------------------------------------------------------------------------------------------- */
1304 edit_group_undo (WEdit
* edit
)
1306 long ac
= KEY_PRESS
;
1307 long cur_ac
= KEY_PRESS
;
1308 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1310 cur_ac
= get_prev_undo_action (edit
);
1311 edit_do_undo (edit
);
1312 ac
= get_prev_undo_action (edit
);
1313 /* exit from cycle if option_group_undo is not set,
1314 * and make single UNDO operation
1316 if (!option_group_undo
)
1321 /* --------------------------------------------------------------------------------------------- */
1324 edit_delete_to_line_end (WEdit
* edit
)
1326 while (edit_buffer_get_current_byte (&edit
->buffer
) != '\n' && edit
->buffer
.curs2
!= 0)
1327 edit_delete (edit
, TRUE
);
1330 /* --------------------------------------------------------------------------------------------- */
1333 edit_delete_to_line_begin (WEdit
* edit
)
1335 while (edit_buffer_get_previous_byte (&edit
->buffer
) != '\n' && edit
->buffer
.curs1
!= 0)
1336 edit_backspace (edit
, TRUE
);
1339 /* --------------------------------------------------------------------------------------------- */
1342 is_aligned_on_a_tab (WEdit
* edit
)
1346 edit_update_curs_col (edit
);
1347 curs_col
= edit
->curs_col
% (TAB_SIZE
* space_width
);
1348 return (curs_col
== 0 || curs_col
== (HALF_TAB_SIZE
* space_width
));
1351 /* --------------------------------------------------------------------------------------------- */
1354 right_of_four_spaces (WEdit
* edit
)
1358 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1359 ch
|= edit_buffer_get_byte (&edit
->buffer
, edit
->buffer
.curs1
- i
);
1361 return (ch
== ' ' && is_aligned_on_a_tab (edit
));
1364 /* --------------------------------------------------------------------------------------------- */
1367 left_of_four_spaces (WEdit
* edit
)
1371 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1372 ch
|= edit_buffer_get_byte (&edit
->buffer
, edit
->buffer
.curs1
+ i
);
1374 return (ch
== ' ' && is_aligned_on_a_tab (edit
));
1377 /* --------------------------------------------------------------------------------------------- */
1380 edit_auto_indent (WEdit
* edit
)
1384 p
= edit
->buffer
.curs1
;
1385 /* use the previous line as a template */
1386 p
= edit_buffer_get_backward_offset (&edit
->buffer
, p
, 1);
1387 /* copy the leading whitespace of the line */
1389 { /* no range check - the line _is_ \n-terminated */
1392 c
= edit_buffer_get_byte (&edit
->buffer
, p
++);
1393 if (c
!= ' ' && c
!= '\t')
1395 edit_insert (edit
, c
);
1399 /* --------------------------------------------------------------------------------------------- */
1402 edit_double_newline (WEdit
* edit
)
1404 edit_insert (edit
, '\n');
1405 if (edit_buffer_get_current_byte (&edit
->buffer
) == '\n'
1406 || edit_buffer_get_byte (&edit
->buffer
, edit
->buffer
.curs1
- 2) == '\n')
1408 edit
->force
|= REDRAW_PAGE
;
1409 edit_insert (edit
, '\n');
1412 /* --------------------------------------------------------------------------------------------- */
1415 insert_spaces_tab (WEdit
* edit
, gboolean half
)
1419 edit_update_curs_col (edit
);
1420 i
= option_tab_spacing
* space_width
;
1425 i
= ((edit
->curs_col
/ i
) + 1) * i
- edit
->curs_col
;
1428 edit_insert (edit
, ' ');
1434 /* --------------------------------------------------------------------------------------------- */
1437 edit_tab_cmd (WEdit
* edit
)
1439 if (option_fake_half_tabs
&& is_in_indent (&edit
->buffer
))
1441 /* insert a half tab (usually four spaces) unless there is a
1442 half tab already behind, then delete it and insert a
1444 if (option_fill_tabs_with_spaces
|| !right_of_four_spaces (edit
))
1445 insert_spaces_tab (edit
, TRUE
);
1450 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1451 edit_backspace (edit
, TRUE
);
1452 edit_insert (edit
, '\t');
1455 else if (option_fill_tabs_with_spaces
)
1456 insert_spaces_tab (edit
, FALSE
);
1458 edit_insert (edit
, '\t');
1461 /* --------------------------------------------------------------------------------------------- */
1464 check_and_wrap_line (WEdit
* edit
)
1468 if (!option_typewriter_wrap
)
1470 edit_update_curs_col (edit
);
1471 if (edit
->curs_col
< option_word_wrap_line_length
)
1473 curs
= edit
->buffer
.curs1
;
1479 c
= edit_buffer_get_byte (&edit
->buffer
, curs
);
1480 if (c
== '\n' || curs
<= 0)
1482 edit_insert (edit
, '\n');
1485 if (c
== ' ' || c
== '\t')
1487 off_t current
= edit
->buffer
.curs1
;
1488 edit_cursor_move (edit
, curs
- edit
->buffer
.curs1
+ 1);
1489 edit_insert (edit
, '\n');
1490 edit_cursor_move (edit
, current
- edit
->buffer
.curs1
+ 1);
1496 /* --------------------------------------------------------------------------------------------- */
1497 /** this find the matching bracket in either direction, and sets edit->bracket
1499 * @param edit editor object
1500 * @param in_screen seach only on the current screen
1501 * @param furthest_bracket_search count of the bytes for search
1503 * @return position of the found bracket (-1 if no match)
1507 edit_get_bracket (WEdit
* edit
, gboolean in_screen
, unsigned long furthest_bracket_search
)
1509 const char *const b
= "{}{[][()(", *p
;
1510 int i
= 1, inc
= -1, c
, d
, n
= 0;
1511 unsigned long j
= 0;
1514 edit_update_curs_row (edit
);
1515 c
= edit_buffer_get_current_byte (&edit
->buffer
);
1517 /* not on a bracket at all */
1518 if (p
== NULL
|| *p
== '\0')
1520 /* the matching bracket */
1522 /* going left or right? */
1523 if (strchr ("{[(", c
) != NULL
)
1526 if (furthest_bracket_search
== 0)
1527 furthest_bracket_search
--; /* ULONG_MAX */
1528 for (q
= edit
->buffer
.curs1
+ inc
;; q
+= inc
)
1532 /* out of buffer? */
1533 if (q
>= edit
->buffer
.size
|| q
< 0)
1535 a
= edit_buffer_get_byte (&edit
->buffer
, q
);
1536 /* don't want to eat CPU */
1537 if (j
++ > furthest_bracket_search
)
1539 /* out of screen? */
1542 if (q
< edit
->start_display
)
1544 /* count lines if searching downward */
1545 if (inc
> 0 && a
== '\n')
1546 if (n
++ >= WIDGET (edit
)->lines
- edit
->curs_row
) /* out of screen */
1549 /* count bracket depth */
1550 i
+= (a
== c
) - (a
== d
);
1551 /* return if bracket depth is zero */
1559 /* --------------------------------------------------------------------------------------------- */
1562 edit_goto_matching_bracket (WEdit
* edit
)
1566 q
= edit_get_bracket (edit
, 0, 0);
1569 edit
->bracket
= edit
->buffer
.curs1
;
1570 edit
->force
|= REDRAW_PAGE
;
1571 edit_cursor_move (edit
, q
- edit
->buffer
.curs1
);
1575 /* --------------------------------------------------------------------------------------------- */
1578 edit_move_block_to_right (WEdit
* edit
)
1580 off_t start_mark
, end_mark
;
1581 long cur_bol
, start_bol
;
1583 if (!eval_marks (edit
, &start_mark
, &end_mark
))
1586 start_bol
= edit_buffer_get_bol (&edit
->buffer
, start_mark
);
1587 cur_bol
= edit_buffer_get_bol (&edit
->buffer
, end_mark
- 1);
1591 edit_cursor_move (edit
, cur_bol
- edit
->buffer
.curs1
);
1592 if (!edit_line_is_blank (edit
, edit
->buffer
.curs_line
))
1594 if (option_fill_tabs_with_spaces
)
1595 insert_spaces_tab (edit
, option_fake_half_tabs
);
1597 edit_insert (edit
, '\t');
1598 edit_cursor_move (edit
,
1599 edit_buffer_get_bol (&edit
->buffer
, cur_bol
) - edit
->buffer
.curs1
);
1605 cur_bol
= edit_buffer_get_bol (&edit
->buffer
, cur_bol
- 1);
1607 while (cur_bol
>= start_bol
);
1609 edit
->force
|= REDRAW_PAGE
;
1612 /* --------------------------------------------------------------------------------------------- */
1615 edit_move_block_to_left (WEdit
* edit
)
1617 off_t start_mark
, end_mark
;
1618 off_t cur_bol
, start_bol
;
1621 if (!eval_marks (edit
, &start_mark
, &end_mark
))
1624 start_bol
= edit_buffer_get_bol (&edit
->buffer
, start_mark
);
1625 cur_bol
= edit_buffer_get_bol (&edit
->buffer
, end_mark
- 1);
1632 edit_cursor_move (edit
, cur_bol
- edit
->buffer
.curs1
);
1634 if (option_fake_half_tabs
)
1635 del_tab_width
= HALF_TAB_SIZE
;
1637 del_tab_width
= option_tab_spacing
;
1639 next_char
= edit_buffer_get_current_byte (&edit
->buffer
);
1640 if (next_char
== '\t')
1641 edit_delete (edit
, TRUE
);
1642 else if (next_char
== ' ')
1643 for (i
= 1; i
<= del_tab_width
; i
++)
1645 if (next_char
== ' ')
1646 edit_delete (edit
, TRUE
);
1647 next_char
= edit_buffer_get_current_byte (&edit
->buffer
);
1653 cur_bol
= edit_buffer_get_bol (&edit
->buffer
, cur_bol
- 1);
1655 while (cur_bol
>= start_bol
);
1657 edit
->force
|= REDRAW_PAGE
;
1660 /* --------------------------------------------------------------------------------------------- */
1662 * prints at the cursor
1663 * @return number of chars printed
1667 edit_print_string (WEdit
* e
, const char *s
)
1671 while (s
[i
] != '\0')
1672 edit_execute_cmd (e
, CK_InsertChar
, (unsigned char) s
[i
++]);
1673 e
->force
|= REDRAW_COMPLETELY
;
1674 edit_update_screen (e
);
1678 /* --------------------------------------------------------------------------------------------- */
1681 edit_insert_column_from_file (WEdit
* edit
, int file
, off_t
* start_pos
, off_t
* end_pos
,
1682 long *col1
, long *col2
)
1686 off_t blocklen
= -1, width
= 0;
1687 unsigned char *data
;
1689 cursor
= edit
->buffer
.curs1
;
1690 col
= edit_get_col (edit
);
1691 data
= g_malloc0 (TEMP_BUF_LEN
);
1693 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0)
1698 pn
= strchr ((char *) data
, '\n');
1699 width
= pn
== NULL
? blocklen
: pn
- (char *) data
;
1701 for (i
= 0; i
< blocklen
; i
++)
1703 if (data
[i
] != '\n')
1704 edit_insert (edit
, data
[i
]);
1706 { /* fill in and move to next line */
1710 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
1711 for (l
= width
- (edit_get_col (edit
) - col
); l
> 0; l
-= space_width
)
1712 edit_insert (edit
, ' ');
1714 for (p
= edit
->buffer
.curs1
;; p
++)
1716 if (p
== edit
->buffer
.size
)
1718 edit_cursor_move (edit
, edit
->buffer
.size
- edit
->buffer
.curs1
);
1719 edit_insert_ahead (edit
, '\n');
1723 if (edit_buffer_get_byte (&edit
->buffer
, p
) == '\n')
1730 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->buffer
.curs1
);
1732 for (l
= col
- edit_get_col (edit
); l
>= space_width
; l
-= space_width
)
1733 edit_insert (edit
, ' ');
1738 *col2
= col
+ width
;
1739 *start_pos
= cursor
;
1740 *end_pos
= edit
->buffer
.curs1
;
1741 edit_cursor_move (edit
, cursor
- edit
->buffer
.curs1
);
1747 /* --------------------------------------------------------------------------------------------- */
1748 /*** public functions ****************************************************************************/
1749 /* --------------------------------------------------------------------------------------------- */
1751 /** User edit menu, like user menu (F2) but only in editor. */
1754 user_menu (WEdit
* edit
, const char *menu_file
, int selected_entry
)
1759 off_t start_mark
, end_mark
;
1761 vfs_path_t
*block_file_vpath
;
1763 block_file
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
1764 block_file_vpath
= vfs_path_from_str (block_file
);
1765 curs
= edit
->buffer
.curs1
;
1766 nomark
= !eval_marks (edit
, &start_mark
, &end_mark
);
1768 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1770 /* run shell scripts from menu */
1771 if (user_menu_cmd (edit
, menu_file
, selected_entry
)
1772 && (mc_stat (block_file_vpath
, &status
) == 0) && (status
.st_size
!= 0))
1777 /* i.e. we have marked block */
1779 rc
= edit_block_delete_cmd (edit
);
1785 ins_len
= edit_insert_file (edit
, block_file_vpath
);
1786 if (!nomark
&& ins_len
> 0)
1787 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1789 /* truncate block file */
1790 fd
= fopen (block_file
, "w");
1794 g_free (block_file
);
1795 vfs_path_free (block_file_vpath
);
1797 edit_cursor_move (edit
, curs
- edit
->buffer
.curs1
);
1798 edit
->force
|= REDRAW_PAGE
;
1799 widget_redraw (WIDGET (edit
));
1802 /* --------------------------------------------------------------------------------------------- */
1805 edit_get_write_filter (const vfs_path_t
* write_name_vpath
, const vfs_path_t
* filename_vpath
)
1808 char *p
, *writename
;
1809 const vfs_path_element_t
*path_element
;
1811 i
= edit_find_filter (filename_vpath
);
1815 path_element
= vfs_path_get_by_index (write_name_vpath
, -1);
1816 writename
= name_quote (path_element
->path
, FALSE
);
1817 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
1822 /* --------------------------------------------------------------------------------------------- */
1824 * @param edit editor object
1825 * @param f value of stream file
1826 * @return the length of the file
1830 edit_write_stream (WEdit
* edit
, FILE * f
)
1834 if (edit
->lb
== LB_ASIS
)
1836 for (i
= 0; i
< edit
->buffer
.size
; i
++)
1837 if (fputc (edit_buffer_get_byte (&edit
->buffer
, i
), f
) < 0)
1842 /* change line breaks */
1843 for (i
= 0; i
< edit
->buffer
.size
; i
++)
1847 c
= edit_buffer_get_byte (&edit
->buffer
, i
);
1848 if (!(c
== '\n' || c
== '\r'))
1850 /* not line break */
1851 if (fputc (c
, f
) < 0)
1855 { /* (c == '\n' || c == '\r') */
1858 c1
= edit_buffer_get_byte (&edit
->buffer
, i
+ 1); /* next char */
1862 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
1863 /* put one line break unconditionally */
1864 if (fputc ('\n', f
) < 0)
1867 i
++; /* 2 chars are processed */
1869 if (c
== '\r' && c1
== '\n')
1870 /* Windows line break; go to the next char */
1873 if (c
== '\r' && c1
== '\r')
1875 /* two Macintosh line breaks; put second line break */
1876 if (fputc ('\n', f
) < 0)
1881 if (fputc (c1
, f
) < 0)
1885 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
1886 /* put one line break unconditionally */
1887 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
1890 if (c
== '\r' && c1
== '\n')
1891 /* Windows line break; go to the next char */
1895 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
1896 /* put one line break unconditionally */
1897 if (fputc ('\r', f
) < 0)
1900 i
++; /* 2 chars are processed */
1902 if (c
== '\r' && c1
== '\n')
1903 /* Windows line break; go to the next char */
1906 if (c
== '\n' && c1
== '\n')
1908 /* two Windows line breaks; put second line break */
1909 if (fputc ('\r', f
) < 0)
1914 if (fputc (c1
, f
) < 0)
1917 case LB_ASIS
: /* default without changes */
1924 return edit
->buffer
.size
;
1927 /* --------------------------------------------------------------------------------------------- */
1930 is_break_char (char c
)
1932 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
1935 /* --------------------------------------------------------------------------------------------- */
1936 /** inserts a file at the cursor, returns count of inserted bytes on success */
1939 edit_insert_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
1945 p
= edit_get_filter (filename_vpath
);
1946 current
= edit
->buffer
.curs1
;
1952 f
= (FILE *) popen (p
, "r");
1955 edit_insert_stream (edit
, f
);
1957 /* Place cursor at the end of text selection */
1958 if (!option_cursor_after_inserted_block
)
1960 ins_len
= edit
->buffer
.curs1
- current
;
1961 edit_cursor_move (edit
, -ins_len
);
1967 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
1968 edit_error_dialog (_("Error"), errmsg
);
1977 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
1978 edit_error_dialog (_("Error"), errmsg
);
1988 int vertical_insertion
= 0;
1991 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
1995 buf
= g_malloc0 (TEMP_BUF_LEN
);
1996 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
1999 /* if contain signature VERTICAL_MAGIC then it vertical block */
2000 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2001 vertical_insertion
= 1;
2003 mc_lseek (file
, 0, SEEK_SET
);
2006 if (vertical_insertion
)
2011 blocklen
= edit_insert_column_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
2012 edit_set_markers (edit
, edit
->buffer
.curs1
, mark2
, c1
, c2
);
2014 /* highlight inserted text then not persistent blocks */
2015 if (!option_persistent_selections
&& edit
->modified
)
2017 if (!edit
->column_highlight
)
2018 edit_push_undo_action (edit
, COLUMN_OFF
);
2019 edit
->column_highlight
= 1;
2026 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2028 for (i
= 0; i
< blocklen
; i
++)
2029 edit_insert (edit
, buf
[i
]);
2031 /* highlight inserted text then not persistent blocks */
2032 if (!option_persistent_selections
&& edit
->modified
)
2034 edit_set_markers (edit
, edit
->buffer
.curs1
, current
, 0, 0);
2035 if (edit
->column_highlight
)
2036 edit_push_undo_action (edit
, COLUMN_ON
);
2037 edit
->column_highlight
= 0;
2040 /* Place cursor at the end of text selection */
2041 if (!option_cursor_after_inserted_block
)
2043 ins_len
= edit
->buffer
.curs1
- current
;
2044 edit_cursor_move (edit
, -ins_len
);
2048 edit
->force
|= REDRAW_PAGE
;
2058 /* --------------------------------------------------------------------------------------------- */
2060 * Fill in the edit structure. Return NULL on failure. Pass edit as
2061 * NULL to allocate a new structure.
2063 * If line is 0, try to restore saved position. Otherwise put the
2064 * cursor on that line and show it in the middle of the screen.
2068 edit_init (WEdit
* edit
, int y
, int x
, int lines
, int cols
, const vfs_path_t
* filename_vpath
,
2071 gboolean to_free
= FALSE
;
2073 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2074 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
2078 /* save some widget parameters */
2079 gboolean fullscreen
= edit
->fullscreen
;
2080 int y_prev
= edit
->y_prev
;
2081 int x_prev
= edit
->x_prev
;
2082 int lines_prev
= edit
->lines_prev
;
2083 int cols_prev
= edit
->cols_prev
;
2085 edit_purge_widget (edit
);
2087 /* restore saved parameters */
2088 edit
->fullscreen
= fullscreen
;
2089 edit
->y_prev
= y_prev
;
2090 edit
->x_prev
= x_prev
;
2091 edit
->lines_prev
= lines_prev
;
2092 edit
->cols_prev
= cols_prev
;
2096 edit
= g_malloc0 (sizeof (WEdit
));
2099 widget_init (WIDGET (edit
), y
, x
, lines
, cols
, NULL
, NULL
);
2100 edit
->fullscreen
= TRUE
;
2101 edit_save_size (edit
);
2104 edit
->drag_state
= MCEDIT_DRAG_NONE
;
2106 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2107 edit
->stat1
.st_uid
= getuid ();
2108 edit
->stat1
.st_gid
= getgid ();
2109 edit
->stat1
.st_mtime
= 0;
2113 edit
->last_bracket
= -1;
2114 edit
->force
|= REDRAW_PAGE
;
2116 /* set file name before load file */
2117 edit_set_filename (edit
, filename_vpath
);
2119 edit
->undo_stack_size
= START_STACK_SIZE
;
2120 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2121 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2123 edit
->redo_stack_size
= START_STACK_SIZE
;
2124 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2125 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2129 edit
->converter
= str_cnv_from_term
;
2130 edit_set_codeset (edit
);
2133 if (!edit_load_file (edit
))
2135 /* edit_load_file already gives an error message */
2141 edit
->loading_done
= 1;
2144 edit_load_syntax (edit
, NULL
, NULL
);
2145 edit_get_syntax_color (edit
, -1);
2147 /* load saved cursor position */
2148 if ((line
== 0) && option_save_position
)
2149 edit_load_position (edit
);
2154 edit_move_display (edit
, line
- 1);
2155 edit_move_to_line (edit
, line
- 1);
2158 edit_load_macro_cmd (edit
);
2163 /* --------------------------------------------------------------------------------------------- */
2165 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2167 edit_clean (WEdit
* edit
)
2172 /* a stale lock, remove it */
2174 edit
->locked
= unlock_file (edit
->filename_vpath
);
2176 /* save cursor position */
2177 if (option_save_position
)
2178 edit_save_position (edit
);
2179 else if (edit
->serialized_bookmarks
!= NULL
)
2180 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2182 /* File specified on the mcedit command line and never saved */
2183 if (edit
->delete_file
)
2184 unlink (vfs_path_get_last_path_str (edit
->filename_vpath
));
2186 edit_free_syntax_rules (edit
);
2187 book_mark_flush (edit
, -1);
2189 edit_buffer_clean (&edit
->buffer
);
2191 g_free (edit
->undo_stack
);
2192 g_free (edit
->redo_stack
);
2193 vfs_path_free (edit
->filename_vpath
);
2194 vfs_path_free (edit
->dir_vpath
);
2195 mc_search_free (edit
->search
);
2196 edit
->search
= NULL
;
2197 MC_PTR_FREE (edit
->last_search_string
);
2200 if (edit
->converter
!= str_cnv_from_term
)
2201 str_close_conv (edit
->converter
);
2204 edit_purge_widget (edit
);
2209 /* --------------------------------------------------------------------------------------------- */
2212 * Load a new file into the editor and set line. If it fails, preserve the old file.
2213 * To do it, allocate a new widget, initialize it and, if the new file
2214 * was loaded, copy the data to the old widget.
2216 * @return TRUE on success, FALSE on failure.
2219 edit_reload_line (WEdit
* edit
, const vfs_path_t
* filename_vpath
, long line
)
2221 Widget
*w
= WIDGET (edit
);
2224 e
= g_malloc0 (sizeof (WEdit
));
2226 /* save some widget parameters */
2227 e
->fullscreen
= edit
->fullscreen
;
2228 e
->y_prev
= edit
->y_prev
;
2229 e
->x_prev
= edit
->x_prev
;
2230 e
->lines_prev
= edit
->lines_prev
;
2231 e
->cols_prev
= edit
->cols_prev
;
2233 if (edit_init (e
, w
->y
, w
->x
, w
->lines
, w
->cols
, filename_vpath
, line
) == NULL
)
2240 memcpy (edit
, e
, sizeof (*edit
));
2246 /* --------------------------------------------------------------------------------------------- */
2250 edit_set_codeset (WEdit
* edit
)
2255 get_codepage_id (mc_global
.source_codepage
>=
2256 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2261 conv
= str_crt_conv_from (cp_id
);
2262 if (conv
!= INVALID_CONV
)
2264 if (edit
->converter
!= str_cnv_from_term
)
2265 str_close_conv (edit
->converter
);
2266 edit
->converter
= conv
;
2271 edit
->utf8
= str_isutf8 (cp_id
);
2275 /* --------------------------------------------------------------------------------------------- */
2278 * Recording stack for undo:
2279 * The following is an implementation of a compressed stack. Identical
2280 * pushes are recorded by a negative prefix indicating the number of times the
2281 * same char was pushed. This saves space for repeated curs-left or curs-right
2298 * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2299 * 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2300 * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2301 * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2304 * The only way the cursor moves or the buffer is changed is through the routines:
2305 * insert, backspace, insert_ahead, delete, and cursor_move.
2306 * These record the reverse undo movements onto the stack each time they are
2309 * Each key press results in a set of actions (insert; delete ...). So each time
2310 * a key is pressed the current position of start_display is pushed as
2311 * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2312 * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2313 * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2317 * @param edit editor object
2318 * @param c code of the action
2322 edit_push_undo_action (WEdit
* edit
, long c
)
2324 unsigned long sp
= edit
->undo_stack_pointer
;
2328 /* first enlarge the stack if necessary */
2329 if (sp
> edit
->undo_stack_size
- 10)
2331 if (option_max_undo
< 256)
2332 option_max_undo
= 256;
2333 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2335 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2338 edit
->undo_stack
= t
;
2339 edit
->undo_stack_size
<<= 1;
2340 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2344 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2345 if (edit
->undo_stack_disable
)
2347 edit_push_redo_action (edit
, KEY_PRESS
);
2348 edit_push_redo_action (edit
, c
);
2352 if (edit
->redo_stack_reset
)
2353 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2355 if (edit
->undo_stack_bottom
!= sp
2356 && spm1
!= edit
->undo_stack_bottom
2357 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2360 if (edit
->undo_stack
[spm1
] < 0)
2362 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2363 if (d
== c
&& edit
->undo_stack
[spm1
] > -1000000000)
2365 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2366 edit
->undo_stack
[spm1
]--;
2372 d
= edit
->undo_stack
[spm1
];
2376 return; /* --> no need to push multiple do-nothings */
2377 edit
->undo_stack
[sp
] = -2;
2382 edit
->undo_stack
[sp
] = c
;
2385 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2387 /* if the sp wraps round and catches the undo_stack_bottom then erase
2388 * the first set of actions on the stack to make space - by moving
2389 * undo_stack_bottom forward one "key press" */
2390 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2391 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2392 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2395 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2397 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2398 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2400 /*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: */
2401 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2402 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2404 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2408 /* --------------------------------------------------------------------------------------------- */
2411 edit_push_redo_action (WEdit
* edit
, long c
)
2413 unsigned long sp
= edit
->redo_stack_pointer
;
2416 /* first enlarge the stack if necessary */
2417 if (sp
> edit
->redo_stack_size
- 10)
2419 if (option_max_undo
< 256)
2420 option_max_undo
= 256;
2421 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2423 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2426 edit
->redo_stack
= t
;
2427 edit
->redo_stack_size
<<= 1;
2428 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2432 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2434 if (edit
->redo_stack_bottom
!= sp
2435 && spm1
!= edit
->redo_stack_bottom
2436 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2439 if (edit
->redo_stack
[spm1
] < 0)
2441 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2442 if (d
== c
&& edit
->redo_stack
[spm1
] > -1000000000)
2444 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2445 edit
->redo_stack
[spm1
]--;
2451 d
= edit
->redo_stack
[spm1
];
2455 return; /* --> no need to push multiple do-nothings */
2456 edit
->redo_stack
[sp
] = -2;
2457 goto redo_check_bottom
;
2461 edit
->redo_stack
[sp
] = c
;
2464 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2466 /* if the sp wraps round and catches the redo_stack_bottom then erase
2467 * the first set of actions on the stack to make space - by moving
2468 * redo_stack_bottom forward one "key press" */
2469 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2470 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2471 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2474 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2476 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2477 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2480 * If a single key produced enough pushes to wrap all the way round then
2481 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2482 * The stack is then initialised:
2485 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2486 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2487 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2490 /* --------------------------------------------------------------------------------------------- */
2492 Basic low level single character buffer alterations and movements at the cursor.
2496 edit_insert (WEdit
* edit
, int c
)
2498 /* first we must update the position of the display window */
2499 if (edit
->buffer
.curs1
< edit
->start_display
)
2501 edit
->start_display
++;
2506 /* Mark file as modified, unless the file hasn't been fully loaded */
2507 if (edit
->loading_done
)
2508 edit_modification (edit
);
2510 /* now we must update some info on the file and check if a redraw is required */
2513 book_mark_inc (edit
, edit
->buffer
.curs_line
);
2514 edit
->buffer
.curs_line
++;
2515 edit
->buffer
.lines
++;
2516 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2519 /* save the reverse command onto the undo stack */
2520 /* ordinary char and not space */
2522 edit_push_undo_action (edit
, BACKSPACE
);
2524 edit_push_undo_action (edit
, BACKSPACE_BR
);
2525 /* update markers */
2526 edit
->mark1
+= (edit
->mark1
> edit
->buffer
.curs1
) ? 1 : 0;
2527 edit
->mark2
+= (edit
->mark2
> edit
->buffer
.curs1
) ? 1 : 0;
2528 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->buffer
.curs1
) ? 1 : 0;
2530 edit_buffer_insert (&edit
->buffer
, c
);
2533 /* --------------------------------------------------------------------------------------------- */
2534 /** same as edit_insert and move left */
2537 edit_insert_ahead (WEdit
* edit
, int c
)
2539 if (edit
->buffer
.curs1
< edit
->start_display
)
2541 edit
->start_display
++;
2545 edit_modification (edit
);
2548 book_mark_inc (edit
, edit
->buffer
.curs_line
);
2549 edit
->buffer
.lines
++;
2550 edit
->force
|= REDRAW_AFTER_CURSOR
;
2552 /* ordinary char and not space */
2554 edit_push_undo_action (edit
, DELCHAR
);
2556 edit_push_undo_action (edit
, DELCHAR_BR
);
2558 edit
->mark1
+= (edit
->mark1
>= edit
->buffer
.curs1
) ? 1 : 0;
2559 edit
->mark2
+= (edit
->mark2
>= edit
->buffer
.curs1
) ? 1 : 0;
2560 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->buffer
.curs1
) ? 1 : 0;
2562 edit_buffer_insert_ahead (&edit
->buffer
, c
);
2565 /* --------------------------------------------------------------------------------------------- */
2568 edit_insert_over (WEdit
* edit
)
2572 for (i
= 0; i
< edit
->over_col
; i
++)
2573 edit_insert (edit
, ' ');
2578 /* --------------------------------------------------------------------------------------------- */
2581 edit_delete (WEdit
* edit
, gboolean byte_delete
)
2584 int char_length
= 1;
2587 if (edit
->buffer
.curs2
== 0)
2591 /* if byte_delete == TRUE then delete only one byte not multibyte char */
2592 if (edit
->utf8
&& !byte_delete
)
2594 edit_buffer_get_utf (&edit
->buffer
, edit
->buffer
.curs1
, &char_length
);
2595 if (char_length
< 1)
2602 if (edit
->mark2
!= edit
->mark1
)
2603 edit_push_markers (edit
);
2605 for (i
= 1; i
<= char_length
; i
++)
2607 if (edit
->mark1
> edit
->buffer
.curs1
)
2610 edit
->end_mark_curs
--;
2612 if (edit
->mark2
> edit
->buffer
.curs1
)
2614 if (edit
->last_get_rule
> edit
->buffer
.curs1
)
2615 edit
->last_get_rule
--;
2617 p
= edit_buffer_delete (&edit
->buffer
);
2619 edit_push_undo_action (edit
, p
+ 256);
2622 edit_modification (edit
);
2625 book_mark_dec (edit
, edit
->buffer
.curs_line
);
2626 edit
->buffer
.lines
--;
2627 edit
->force
|= REDRAW_AFTER_CURSOR
;
2629 if (edit
->buffer
.curs1
< edit
->start_display
)
2631 edit
->start_display
--;
2639 /* --------------------------------------------------------------------------------------------- */
2642 edit_backspace (WEdit
* edit
, gboolean byte_delete
)
2645 int char_length
= 1;
2648 if (edit
->buffer
.curs1
== 0)
2651 if (edit
->mark2
!= edit
->mark1
)
2652 edit_push_markers (edit
);
2655 if (edit
->utf8
&& !byte_delete
)
2657 edit_buffer_get_prev_utf (&edit
->buffer
, edit
->buffer
.curs1
, &char_length
);
2658 if (char_length
< 1)
2665 for (i
= 1; i
<= char_length
; i
++)
2667 if (edit
->mark1
>= edit
->buffer
.curs1
)
2670 edit
->end_mark_curs
--;
2672 if (edit
->mark2
>= edit
->buffer
.curs1
)
2674 if (edit
->last_get_rule
>= edit
->buffer
.curs1
)
2675 edit
->last_get_rule
--;
2677 p
= edit_buffer_backspace (&edit
->buffer
);
2679 edit_push_undo_action (edit
, p
);
2681 edit_modification (edit
);
2684 book_mark_dec (edit
, edit
->buffer
.curs_line
);
2685 edit
->buffer
.curs_line
--;
2686 edit
->buffer
.lines
--;
2687 edit
->force
|= REDRAW_AFTER_CURSOR
;
2690 if (edit
->buffer
.curs1
< edit
->start_display
)
2692 edit
->start_display
--;
2700 /* --------------------------------------------------------------------------------------------- */
2701 /** moves the cursor right or left: increment positive or negative respectively */
2704 edit_cursor_move (WEdit
* edit
, off_t increment
)
2708 for (; increment
< 0 && edit
->buffer
.curs1
!= 0; increment
++)
2712 edit_push_undo_action (edit
, CURS_RIGHT
);
2714 c
= edit_buffer_get_previous_byte (&edit
->buffer
);
2715 edit_buffer_insert_ahead (&edit
->buffer
, c
);
2716 c
= edit_buffer_backspace (&edit
->buffer
);
2719 edit
->buffer
.curs_line
--;
2720 edit
->force
|= REDRAW_LINE_BELOW
;
2726 for (; increment
> 0 && edit
->buffer
.curs2
!= 0; increment
--)
2730 edit_push_undo_action (edit
, CURS_LEFT
);
2732 c
= edit_buffer_get_current_byte (&edit
->buffer
);
2733 edit_buffer_insert (&edit
->buffer
, c
);
2734 c
= edit_buffer_delete (&edit
->buffer
);
2737 edit
->buffer
.curs_line
++;
2738 edit
->force
|= REDRAW_LINE_ABOVE
;
2744 /* --------------------------------------------------------------------------------------------- */
2745 /* If cols is zero this returns the count of columns from current to upto. */
2746 /* If upto is zero returns index of cols across from current. */
2749 edit_move_forward3 (const WEdit
* edit
, off_t current
, long cols
, off_t upto
)
2760 q
= edit
->buffer
.size
+ 2;
2762 for (col
= 0, p
= current
; p
< q
; p
++)
2774 orig_c
= c
= edit_buffer_get_byte (&edit
->buffer
, p
);
2780 int char_length
= 1;
2782 utf_ch
= edit_buffer_get_utf (&edit
->buffer
, p
, &char_length
);
2783 if (mc_global
.utf8_display
)
2785 if (char_length
> 1)
2786 col
-= char_length
- 1;
2787 if (g_unichar_iswide (utf_ch
))
2790 else if (char_length
> 1 && g_unichar_isprint (utf_ch
))
2791 col
-= char_length
- 1;
2794 c
= convert_to_display_c (c
);
2798 return (upto
!= 0 ? (off_t
) col
: p
);
2800 col
+= TAB_SIZE
- col
% TAB_SIZE
;
2801 else if ((c
< 32 || c
== 127) && (orig_c
== c
2803 || (!mc_global
.utf8_display
&& !edit
->utf8
)
2806 /* '\r' is shown as ^M, so we must advance 2 characters */
2807 /* Caret notation for control characters */
2815 /* --------------------------------------------------------------------------------------------- */
2816 /** returns the current column position of the cursor */
2819 edit_get_col (const WEdit
* edit
)
2821 return (long) edit_move_forward3 (edit
, edit_buffer_get_current_bol (&edit
->buffer
), 0,
2822 edit
->buffer
.curs1
);
2825 /* --------------------------------------------------------------------------------------------- */
2826 /* Scrolling functions */
2827 /* --------------------------------------------------------------------------------------------- */
2830 edit_update_curs_row (WEdit
* edit
)
2832 edit
->curs_row
= edit
->buffer
.curs_line
- edit
->start_line
;
2835 /* --------------------------------------------------------------------------------------------- */
2838 edit_update_curs_col (WEdit
* edit
)
2840 edit
->curs_col
= (long) edit_move_forward3 (edit
, edit_buffer_get_current_bol (&edit
->buffer
),
2841 0, edit
->buffer
.curs1
);
2844 /* --------------------------------------------------------------------------------------------- */
2847 edit_get_curs_col (const WEdit
* edit
)
2849 return edit
->curs_col
;
2852 /* --------------------------------------------------------------------------------------------- */
2853 /** moves the display start position up by i lines */
2856 edit_scroll_upward (WEdit
* edit
, long i
)
2858 long lines_above
= edit
->start_line
;
2860 if (i
> lines_above
)
2864 edit
->start_line
-= i
;
2865 edit
->start_display
=
2866 edit_buffer_get_backward_offset (&edit
->buffer
, edit
->start_display
, i
);
2867 edit
->force
|= REDRAW_PAGE
;
2868 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2870 edit_update_curs_row (edit
);
2874 /* --------------------------------------------------------------------------------------------- */
2877 edit_scroll_downward (WEdit
* edit
, long i
)
2881 lines_below
= edit
->buffer
.lines
- edit
->start_line
- (WIDGET (edit
)->lines
- 1);
2882 if (lines_below
> 0)
2884 if (i
> lines_below
)
2886 edit
->start_line
+= i
;
2887 edit
->start_display
=
2888 edit_buffer_get_forward_offset (&edit
->buffer
, edit
->start_display
, i
, 0);
2889 edit
->force
|= REDRAW_PAGE
;
2890 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2892 edit_update_curs_row (edit
);
2895 /* --------------------------------------------------------------------------------------------- */
2898 edit_scroll_right (WEdit
* edit
, long i
)
2900 edit
->force
|= REDRAW_PAGE
;
2901 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2902 edit
->start_col
-= i
;
2905 /* --------------------------------------------------------------------------------------------- */
2908 edit_scroll_left (WEdit
* edit
, long i
)
2910 if (edit
->start_col
)
2912 edit
->start_col
+= i
;
2913 if (edit
->start_col
> 0)
2914 edit
->start_col
= 0;
2915 edit
->force
|= REDRAW_PAGE
;
2916 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2920 /* --------------------------------------------------------------------------------------------- */
2921 /* high level cursor movement commands */
2922 /* --------------------------------------------------------------------------------------------- */
2925 edit_move_to_prev_col (WEdit
* edit
, off_t p
)
2927 long prev
= edit
->prev_col
;
2928 long over
= edit
->over_col
;
2930 edit_cursor_move (edit
,
2931 edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->buffer
.curs1
);
2933 if (option_cursor_beyond_eol
)
2937 line_len
= (long) edit_move_forward3 (edit
, edit_buffer_get_current_bol (&edit
->buffer
), 0,
2938 edit_buffer_get_current_eol (&edit
->buffer
));
2939 if (line_len
< prev
+ edit
->over_col
)
2941 edit
->over_col
= prev
+ over
- line_len
;
2942 edit
->prev_col
= line_len
;
2943 edit
->curs_col
= line_len
;
2947 edit
->curs_col
= prev
+ over
;
2948 edit
->prev_col
= edit
->curs_col
;
2955 if (option_fake_half_tabs
&& is_in_indent (&edit
->buffer
))
2957 long fake_half_tabs
;
2959 edit_update_curs_col (edit
);
2961 fake_half_tabs
= HALF_TAB_SIZE
* space_width
;
2962 if (fake_half_tabs
!= 0 && edit
->curs_col
% fake_half_tabs
!= 0)
2967 edit
->curs_col
-= (edit
->curs_col
% fake_half_tabs
);
2968 p
= edit_buffer_get_current_bol (&edit
->buffer
);
2969 edit_cursor_move (edit
,
2970 edit_move_forward3 (edit
, p
, edit
->curs_col
,
2971 0) - edit
->buffer
.curs1
);
2972 if (!left_of_four_spaces (edit
))
2973 edit_cursor_move (edit
,
2974 edit_move_forward3 (edit
, p
, q
, 0) - edit
->buffer
.curs1
);
2980 /* --------------------------------------------------------------------------------------------- */
2981 /** check whether line in editor is blank or not
2983 * @param edit editor object
2984 * @param line number of line
2986 * @return TRUE if line in blank, FALSE otherwise
2990 edit_line_is_blank (WEdit
* edit
, long line
)
2992 return is_blank (&edit
->buffer
, edit_find_line (edit
, line
));
2995 /* --------------------------------------------------------------------------------------------- */
2996 /** move cursor to line 'line' */
2999 edit_move_to_line (WEdit
* e
, long line
)
3001 if (line
< e
->buffer
.curs_line
)
3002 edit_move_up (e
, e
->buffer
.curs_line
- line
, FALSE
);
3004 edit_move_down (e
, line
- e
->buffer
.curs_line
, FALSE
);
3005 edit_scroll_screen_over_cursor (e
);
3008 /* --------------------------------------------------------------------------------------------- */
3009 /** scroll window so that first visible line is 'line' */
3012 edit_move_display (WEdit
* e
, long line
)
3014 if (line
< e
->start_line
)
3015 edit_scroll_upward (e
, e
->start_line
- line
);
3017 edit_scroll_downward (e
, line
- e
->start_line
);
3020 /* --------------------------------------------------------------------------------------------- */
3021 /** save markers onto undo stack */
3024 edit_push_markers (WEdit
* edit
)
3026 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3027 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3028 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3031 /* --------------------------------------------------------------------------------------------- */
3034 edit_set_markers (WEdit
* edit
, off_t m1
, off_t m2
, long c1
, long c2
)
3043 /* --------------------------------------------------------------------------------------------- */
3044 /** highlight marker toggle */
3047 edit_mark_cmd (WEdit
* edit
, gboolean unmark
)
3049 edit_push_markers (edit
);
3052 edit_set_markers (edit
, 0, 0, 0, 0);
3053 edit
->force
|= REDRAW_PAGE
;
3055 else if (edit
->mark2
>= 0)
3057 edit
->end_mark_curs
= -1;
3058 edit_set_markers (edit
, edit
->buffer
.curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3059 edit
->curs_col
+ edit
->over_col
);
3060 edit
->force
|= REDRAW_PAGE
;
3064 edit
->end_mark_curs
= edit
->buffer
.curs1
;
3065 edit_set_markers (edit
, edit
->mark1
, edit
->buffer
.curs1
, edit
->column1
,
3066 edit
->curs_col
+ edit
->over_col
);
3070 /* --------------------------------------------------------------------------------------------- */
3071 /** highlight the word under cursor */
3074 edit_mark_current_word_cmd (WEdit
* edit
)
3078 for (pos
= edit
->buffer
.curs1
; pos
!= 0; pos
--)
3082 c1
= edit_buffer_get_byte (&edit
->buffer
, pos
);
3083 c2
= edit_buffer_get_byte (&edit
->buffer
, pos
- 1);
3084 if (!isspace (c1
) && isspace (c2
))
3086 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3091 for (; pos
< edit
->buffer
.size
; pos
++)
3095 c1
= edit_buffer_get_byte (&edit
->buffer
, pos
);
3096 c2
= edit_buffer_get_byte (&edit
->buffer
, pos
+ 1);
3097 if (!isspace (c1
) && isspace (c2
))
3099 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3102 edit
->mark2
= MIN (pos
+ 1, edit
->buffer
.size
);
3104 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3107 /* --------------------------------------------------------------------------------------------- */
3110 edit_mark_current_line_cmd (WEdit
* edit
)
3112 edit
->mark1
= edit_buffer_get_current_bol (&edit
->buffer
);
3113 edit
->mark2
= edit_buffer_get_current_eol (&edit
->buffer
);
3115 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3118 /* --------------------------------------------------------------------------------------------- */
3121 edit_delete_line (WEdit
* edit
)
3124 * Delete right part of the line.
3125 * Note that edit_buffer_get_byte() returns '\n' when byte position is
3128 while (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
3129 (void) edit_delete (edit
, TRUE
);
3133 * Note that edit_delete() will not corrupt anything if called while
3134 * cursor position is EOF.
3136 (void) edit_delete (edit
, TRUE
);
3139 * Delete left part of the line.
3140 * Note, that edit_buffer_get_byte() returns '\n' when byte position is < 0.
3142 while (edit_buffer_get_previous_byte (&edit
->buffer
) != '\n')
3143 (void) edit_backspace (edit
, TRUE
);
3146 /* --------------------------------------------------------------------------------------------- */
3149 edit_push_key_press (WEdit
* edit
)
3151 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3152 if (edit
->mark2
== -1)
3154 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3155 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3159 /* --------------------------------------------------------------------------------------------- */
3162 edit_find_bracket (WEdit
* edit
)
3164 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3165 if (edit
->last_bracket
!= edit
->bracket
)
3166 edit
->force
|= REDRAW_PAGE
;
3167 edit
->last_bracket
= edit
->bracket
;
3170 /* --------------------------------------------------------------------------------------------- */
3172 * This executes a command as though the user initiated it through a key
3173 * press. Callback with MSG_KEY as a message calls this after
3174 * translating the key press. This function can be used to pass any
3175 * command to the editor. Note that the screen wouldn't update
3176 * automatically. Either of command or char_for_insertion must be
3177 * passed as -1. Commands are executed, and char_for_insertion is
3178 * inserted at the cursor.
3182 edit_execute_key_command (WEdit
* edit
, long command
, int char_for_insertion
)
3184 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3186 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3189 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3192 if (macro_index
!= -1)
3194 edit
->force
|= REDRAW_COMPLETELY
;
3195 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3197 edit_store_macro_cmd (edit
);
3201 if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3203 edit_repeat_macro_cmd (edit
);
3209 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3211 record_macro_buf
[macro_index
].action
= command
;
3212 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3214 /* record the beginning of a set of editing actions initiated by a key press */
3215 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3216 edit_push_key_press (edit
);
3218 edit_execute_cmd (edit
, command
, char_for_insertion
);
3219 if (edit
->column_highlight
)
3220 edit
->force
|= REDRAW_PAGE
;
3223 /* --------------------------------------------------------------------------------------------- */
3225 This executes a command at a lower level than macro recording.
3226 It also does not push a key_press onto the undo stack. This means
3227 that if it is called many times, a single undo command will undo
3228 all of them. It also does not check for the Undo command.
3231 edit_execute_cmd (WEdit
* edit
, long command
, int char_for_insertion
)
3233 Widget
*w
= WIDGET (edit
);
3235 if (command
== CK_WindowFullscreen
)
3237 edit_toggle_fullscreen (edit
);
3241 /* handle window state */
3242 if (edit_handle_move_resize (edit
, command
))
3245 edit
->force
|= REDRAW_LINE
;
3247 /* The next key press will unhighlight the found string, so update
3249 if (edit
->found_len
|| edit
->column_highlight
)
3250 edit
->force
|= REDRAW_PAGE
;
3254 /* a mark command with shift-arrow */
3257 case CK_MarkToWordBegin
:
3258 case CK_MarkToWordEnd
:
3264 case CK_MarkPageDown
:
3265 case CK_MarkToFileBegin
:
3266 case CK_MarkToFileEnd
:
3267 case CK_MarkToPageBegin
:
3268 case CK_MarkToPageEnd
:
3269 case CK_MarkScrollUp
:
3270 case CK_MarkScrollDown
:
3271 case CK_MarkParagraphUp
:
3272 case CK_MarkParagraphDown
:
3273 /* a mark command with alt-arrow */
3274 case CK_MarkColumnPageUp
:
3275 case CK_MarkColumnPageDown
:
3276 case CK_MarkColumnLeft
:
3277 case CK_MarkColumnRight
:
3278 case CK_MarkColumnUp
:
3279 case CK_MarkColumnDown
:
3280 case CK_MarkColumnScrollUp
:
3281 case CK_MarkColumnScrollDown
:
3282 case CK_MarkColumnParagraphUp
:
3283 case CK_MarkColumnParagraphDown
:
3284 edit
->column_highlight
= 0;
3285 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3287 edit_mark_cmd (edit
, TRUE
); /* clear */
3288 edit_mark_cmd (edit
, FALSE
); /* marking on */
3290 edit
->highlight
= 1;
3293 /* any other command */
3295 if (edit
->highlight
)
3296 edit_mark_cmd (edit
, FALSE
); /* clear */
3297 edit
->highlight
= 0;
3300 /* first check for undo */
3301 if (command
== CK_Undo
)
3303 edit
->redo_stack_reset
= 0;
3304 edit_group_undo (edit
);
3305 edit
->found_len
= 0;
3306 edit
->prev_col
= edit_get_col (edit
);
3307 edit
->search_start
= edit
->buffer
.curs1
;
3310 /* check for redo */
3311 if (command
== CK_Redo
)
3313 edit
->redo_stack_reset
= 0;
3314 edit_do_redo (edit
);
3315 edit
->found_len
= 0;
3316 edit
->prev_col
= edit_get_col (edit
);
3317 edit
->search_start
= edit
->buffer
.curs1
;
3321 edit
->redo_stack_reset
= 1;
3323 /* An ordinary key press */
3324 if (char_for_insertion
>= 0)
3326 /* if non persistent selection and text selected */
3327 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3328 edit_block_delete_cmd (edit
);
3330 if (edit
->overwrite
)
3332 /* remove char only one time, after input first byte, multibyte chars */
3334 if (!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3336 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
3338 edit_delete (edit
, FALSE
);
3340 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3341 edit_insert_over (edit
);
3343 if (char_for_insertion
> 255 && !mc_global
.utf8_display
)
3345 unsigned char str
[UTF8_CHAR_LEN
+ 1];
3349 res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3359 while (i
<= UTF8_CHAR_LEN
&& str
[i
] != '\0')
3361 char_for_insertion
= str
[i
];
3362 edit_insert (edit
, char_for_insertion
);
3368 edit_insert (edit
, char_for_insertion
);
3370 if (option_auto_para_formatting
)
3372 format_paragraph (edit
, FALSE
);
3373 edit
->force
|= REDRAW_PAGE
;
3376 check_and_wrap_line (edit
);
3377 edit
->found_len
= 0;
3378 edit
->prev_col
= edit_get_col (edit
);
3379 edit
->search_start
= edit
->buffer
.curs1
;
3380 edit_find_bracket (edit
);
3386 case CK_TopOnScreen
:
3387 case CK_BottomOnScreen
:
3400 if (!option_persistent_selections
&& edit
->mark2
>= 0)
3402 if (edit
->column_highlight
)
3403 edit_push_undo_action (edit
, COLUMN_ON
);
3404 edit
->column_highlight
= 0;
3405 edit_mark_cmd (edit
, TRUE
);
3413 case CK_TopOnScreen
:
3414 case CK_BottomOnScreen
:
3415 case CK_MarkToPageBegin
:
3416 case CK_MarkToPageEnd
:
3421 case CK_MarkToWordBegin
:
3422 case CK_MarkToWordEnd
:
3425 case CK_MarkColumnUp
:
3426 case CK_MarkColumnDown
:
3427 if (edit
->mark2
== -1)
3428 break; /*marking is following the cursor: may need to highlight a whole line */
3433 edit
->force
|= REDRAW_CHAR_ONLY
;
3438 /* basic cursor key commands */
3442 /* if non persistent selection and text selected */
3443 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3444 edit_block_delete_cmd (edit
);
3445 else if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3447 else if (option_backspace_through_tabs
&& is_in_indent (&edit
->buffer
))
3449 while (edit_buffer_get_previous_byte (&edit
->buffer
) != '\n' && edit
->buffer
.curs1
> 0)
3450 edit_backspace (edit
, TRUE
);
3452 else if (option_fake_half_tabs
&& is_in_indent (&edit
->buffer
)
3453 && right_of_four_spaces (edit
))
3457 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3458 edit_backspace (edit
, TRUE
);
3461 edit_backspace (edit
, FALSE
);
3464 /* if non persistent selection and text selected */
3465 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3466 edit_block_delete_cmd (edit
);
3469 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3470 edit_insert_over (edit
);
3472 if (option_fake_half_tabs
&& is_in_indent (&edit
->buffer
) && left_of_four_spaces (edit
))
3476 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3477 edit_delete (edit
, TRUE
);
3480 edit_delete (edit
, FALSE
);
3483 case CK_DeleteToWordBegin
:
3485 edit_left_delete_word (edit
);
3487 case CK_DeleteToWordEnd
:
3488 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3489 edit_insert_over (edit
);
3491 edit_right_delete_word (edit
);
3494 edit_delete_line (edit
);
3496 case CK_DeleteToHome
:
3497 edit_delete_to_line_begin (edit
);
3499 case CK_DeleteToEnd
:
3500 edit_delete_to_line_end (edit
);
3504 if (option_auto_para_formatting
)
3506 edit_double_newline (edit
);
3507 if (option_return_does_auto_indent
&& !bracketed_pasting_in_progress
)
3508 edit_auto_indent (edit
);
3509 format_paragraph (edit
, FALSE
);
3513 edit_insert (edit
, '\n');
3514 if (option_return_does_auto_indent
&& !bracketed_pasting_in_progress
)
3515 edit_auto_indent (edit
);
3519 edit_insert (edit
, '\n');
3522 case CK_MarkColumnPageUp
:
3523 edit
->column_highlight
= 1;
3526 edit_move_up (edit
, w
->lines
- 1, TRUE
);
3528 case CK_MarkColumnPageDown
:
3529 edit
->column_highlight
= 1;
3531 case CK_MarkPageDown
:
3532 edit_move_down (edit
, w
->lines
- 1, TRUE
);
3534 case CK_MarkColumnLeft
:
3535 edit
->column_highlight
= 1;
3538 if (option_fake_half_tabs
&& is_in_indent (&edit
->buffer
) && right_of_four_spaces (edit
))
3540 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3543 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3544 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3547 edit_left_char_move_cmd (edit
);
3549 case CK_MarkColumnRight
:
3550 edit
->column_highlight
= 1;
3553 if (option_fake_half_tabs
&& is_in_indent (&edit
->buffer
) && left_of_four_spaces (edit
))
3555 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3556 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3559 edit_right_char_move_cmd (edit
);
3561 case CK_TopOnScreen
:
3562 case CK_MarkToPageBegin
:
3563 edit_begin_page (edit
);
3565 case CK_BottomOnScreen
:
3566 case CK_MarkToPageEnd
:
3567 edit_end_page (edit
);
3570 case CK_MarkToWordBegin
:
3572 edit_left_word_move_cmd (edit
);
3575 case CK_MarkToWordEnd
:
3577 edit_right_word_move_cmd (edit
);
3579 case CK_MarkColumnUp
:
3580 edit
->column_highlight
= 1;
3583 edit_move_up (edit
, 1, FALSE
);
3585 case CK_MarkColumnDown
:
3586 edit
->column_highlight
= 1;
3589 edit_move_down (edit
, 1, FALSE
);
3591 case CK_MarkColumnParagraphUp
:
3592 edit
->column_highlight
= 1;
3593 case CK_ParagraphUp
:
3594 case CK_MarkParagraphUp
:
3595 edit_move_up_paragraph (edit
, FALSE
);
3597 case CK_MarkColumnParagraphDown
:
3598 edit
->column_highlight
= 1;
3599 case CK_ParagraphDown
:
3600 case CK_MarkParagraphDown
:
3601 edit_move_down_paragraph (edit
, FALSE
);
3603 case CK_MarkColumnScrollUp
:
3604 edit
->column_highlight
= 1;
3606 case CK_MarkScrollUp
:
3607 edit_move_up (edit
, 1, TRUE
);
3609 case CK_MarkColumnScrollDown
:
3610 edit
->column_highlight
= 1;
3612 case CK_MarkScrollDown
:
3613 edit_move_down (edit
, 1, TRUE
);
3617 edit_cursor_to_bol (edit
);
3621 edit_cursor_to_eol (edit
);
3624 /* if text marked shift block */
3625 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3627 if (edit
->mark2
< 0)
3628 edit_mark_cmd (edit
, FALSE
);
3629 edit_move_block_to_right (edit
);
3633 if (option_cursor_beyond_eol
)
3634 edit_insert_over (edit
);
3635 edit_tab_cmd (edit
);
3636 if (option_auto_para_formatting
)
3638 format_paragraph (edit
, FALSE
);
3639 edit
->force
|= REDRAW_PAGE
;
3642 check_and_wrap_line (edit
);
3646 case CK_InsertOverwrite
:
3647 edit
->overwrite
= !edit
->overwrite
;
3651 if (edit
->mark2
>= 0)
3653 if (edit
->column_highlight
)
3654 edit_push_undo_action (edit
, COLUMN_ON
);
3655 edit
->column_highlight
= 0;
3657 edit_mark_cmd (edit
, FALSE
);
3660 if (!edit
->column_highlight
)
3661 edit_push_undo_action (edit
, COLUMN_OFF
);
3662 edit
->column_highlight
= 1;
3663 edit_mark_cmd (edit
, FALSE
);
3666 edit_set_markers (edit
, 0, edit
->buffer
.size
, 0, 0);
3667 edit
->force
|= REDRAW_PAGE
;
3670 if (edit
->column_highlight
)
3671 edit_push_undo_action (edit
, COLUMN_ON
);
3672 edit
->column_highlight
= 0;
3673 edit_mark_cmd (edit
, TRUE
);
3676 if (edit
->column_highlight
)
3677 edit_push_undo_action (edit
, COLUMN_ON
);
3678 edit
->column_highlight
= 0;
3679 edit_mark_current_word_cmd (edit
);
3682 if (edit
->column_highlight
)
3683 edit_push_undo_action (edit
, COLUMN_ON
);
3684 edit
->column_highlight
= 0;
3685 edit_mark_current_line_cmd (edit
);
3689 book_mark_clear (edit
, edit
->buffer
.curs_line
, BOOK_MARK_FOUND_COLOR
);
3690 if (book_mark_query_color (edit
, edit
->buffer
.curs_line
, BOOK_MARK_COLOR
))
3691 book_mark_clear (edit
, edit
->buffer
.curs_line
, BOOK_MARK_COLOR
);
3693 book_mark_insert (edit
, edit
->buffer
.curs_line
, BOOK_MARK_COLOR
);
3695 case CK_BookmarkFlush
:
3696 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3697 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3698 edit
->force
|= REDRAW_PAGE
;
3700 case CK_BookmarkNext
:
3701 if (edit
->book_mark
!= NULL
)
3703 edit_book_mark_t
*p
;
3705 p
= book_mark_find (edit
, edit
->buffer
.curs_line
);
3706 if (p
->next
!= NULL
)
3709 if (p
->line
>= edit
->start_line
+ w
->lines
|| p
->line
< edit
->start_line
)
3710 edit_move_display (edit
, p
->line
- w
->lines
/ 2);
3711 edit_move_to_line (edit
, p
->line
);
3715 case CK_BookmarkPrev
:
3716 if (edit
->book_mark
!= NULL
)
3718 edit_book_mark_t
*p
;
3720 p
= book_mark_find (edit
, edit
->buffer
.curs_line
);
3721 while (p
->line
== edit
->buffer
.curs_line
)
3722 if (p
->prev
!= NULL
)
3726 if (p
->line
>= edit
->start_line
+ w
->lines
|| p
->line
< edit
->start_line
)
3727 edit_move_display (edit
, p
->line
- w
->lines
/ 2);
3728 edit_move_to_line (edit
, p
->line
);
3734 case CK_MarkToFileBegin
:
3735 edit_move_to_top (edit
);
3738 case CK_MarkToFileEnd
:
3739 edit_move_to_bottom (edit
);
3743 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3744 edit_insert_over (edit
);
3745 edit_block_copy_cmd (edit
);
3748 edit_block_delete_cmd (edit
);
3751 edit_block_move_cmd (edit
);
3754 case CK_BlockShiftLeft
:
3755 if (edit
->mark1
!= edit
->mark2
)
3756 edit_move_block_to_left (edit
);
3758 case CK_BlockShiftRight
:
3759 if (edit
->mark1
!= edit
->mark2
)
3760 edit_move_block_to_right (edit
);
3763 edit_copy_to_X_buf_cmd (edit
);
3766 edit_cut_to_X_buf_cmd (edit
);
3769 /* if non persistent selection and text selected */
3770 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3771 edit_block_delete_cmd (edit
);
3772 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3773 edit_insert_over (edit
);
3774 edit_paste_from_X_buf_cmd (edit
);
3775 if (!option_persistent_selections
&& edit
->mark2
>= 0)
3777 if (edit
->column_highlight
)
3778 edit_push_undo_action (edit
, COLUMN_ON
);
3779 edit
->column_highlight
= 0;
3780 edit_mark_cmd (edit
, TRUE
);
3784 edit_paste_from_history (edit
);
3788 edit_save_as_cmd (edit
);
3791 edit_save_confirm_cmd (edit
);
3794 edit_save_block_cmd (edit
);
3797 edit_insert_file_cmd (edit
);
3801 edit_load_back_cmd (edit
);
3804 edit_load_forward_cmd (edit
);
3807 case CK_SyntaxChoose
:
3808 edit_syntax_dialog (edit
);
3812 edit_search_cmd (edit
, FALSE
);
3814 case CK_SearchContinue
:
3815 edit_search_cmd (edit
, TRUE
);
3818 edit_replace_cmd (edit
, FALSE
);
3820 case CK_ReplaceContinue
:
3821 edit_replace_cmd (edit
, TRUE
);
3824 /* if text marked shift block */
3825 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3826 edit_move_block_to_left (edit
);
3828 edit_complete_word_cmd (edit
);
3831 edit_get_match_keyword_cmd (edit
);
3835 case CK_SpellCheckCurrentWord
:
3836 edit_suggest_current_word (edit
);
3839 edit_spellcheck_file (edit
);
3841 case CK_SpellCheckSelectLang
:
3842 edit_set_spell_lang ();
3849 /* fool gcc to prevent a Y2K warning */
3850 char time_format
[] = "_c";
3851 time_format
[0] = '%';
3853 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
3854 edit_print_string (edit
, s
);
3855 edit
->force
|= REDRAW_PAGE
;
3859 edit_goto_cmd (edit
);
3861 case CK_ParagraphFormat
:
3862 format_paragraph (edit
, TRUE
);
3863 edit
->force
|= REDRAW_PAGE
;
3865 case CK_MacroDelete
:
3866 edit_delete_macro_cmd (edit
);
3868 case CK_MatchBracket
:
3869 edit_goto_matching_bracket (edit
);
3872 user_menu (edit
, NULL
, -1);
3875 edit_sort_cmd (edit
);
3877 case CK_ExternalCommand
:
3878 edit_ext_cmd (edit
);
3881 edit_mail_dialog (edit
);
3884 case CK_SelectCodepage
:
3885 edit_select_codepage_cmd (edit
);
3888 case CK_InsertLiteral
:
3889 edit_insert_literal_cmd (edit
);
3891 case CK_MacroStartStopRecord
:
3892 edit_begin_end_macro_cmd (edit
);
3894 case CK_RepeatStartStopRecord
:
3895 edit_begin_end_repeat_cmd (edit
);
3897 case CK_ExtendedKeyMap
:
3898 edit
->extmod
= TRUE
;
3905 if ((command
/ CK_PipeBlock (0)) == 1)
3906 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
3908 /* keys which must set the col position, and the search vars */
3912 case CK_SearchContinue
:
3914 case CK_ReplaceContinue
:
3916 edit
->prev_col
= edit_get_col (edit
);
3920 case CK_MarkColumnUp
:
3923 case CK_MarkColumnDown
:
3926 case CK_MarkColumnPageUp
:
3928 case CK_MarkPageDown
:
3929 case CK_MarkColumnPageDown
:
3931 case CK_MarkToFileBegin
:
3933 case CK_MarkToFileEnd
:
3934 case CK_ParagraphUp
:
3935 case CK_MarkParagraphUp
:
3936 case CK_MarkColumnParagraphUp
:
3937 case CK_ParagraphDown
:
3938 case CK_MarkParagraphDown
:
3939 case CK_MarkColumnParagraphDown
:
3941 case CK_MarkScrollUp
:
3942 case CK_MarkColumnScrollUp
:
3944 case CK_MarkScrollDown
:
3945 case CK_MarkColumnScrollDown
:
3946 edit
->search_start
= edit
->buffer
.curs1
;
3947 edit
->found_len
= 0;
3950 edit
->found_len
= 0;
3951 edit
->prev_col
= edit_get_col (edit
);
3952 edit
->search_start
= edit
->buffer
.curs1
;
3954 edit_find_bracket (edit
);
3956 if (option_auto_para_formatting
)
3962 case CK_DeleteToWordBegin
:
3963 case CK_DeleteToWordEnd
:
3964 case CK_DeleteToHome
:
3965 case CK_DeleteToEnd
:
3966 format_paragraph (edit
, FALSE
);
3967 edit
->force
|= REDRAW_PAGE
;
3974 /* --------------------------------------------------------------------------------------------- */
3977 edit_stack_init (void)
3979 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
3981 edit_history_moveto
[edit_stack_iterator
].filename_vpath
= NULL
;
3982 edit_history_moveto
[edit_stack_iterator
].line
= -1;
3985 edit_stack_iterator
= 0;
3988 /* --------------------------------------------------------------------------------------------- */
3991 edit_stack_free (void)
3993 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
3994 vfs_path_free (edit_history_moveto
[edit_stack_iterator
].filename_vpath
);
3997 /* --------------------------------------------------------------------------------------------- */
4001 edit_move_up (WEdit
* edit
, long i
, gboolean do_scroll
)
4003 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4006 /* --------------------------------------------------------------------------------------------- */
4010 edit_move_down (WEdit
* edit
, long i
, gboolean do_scroll
)
4012 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4015 /* --------------------------------------------------------------------------------------------- */