2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2008, 2009, 2010, 2011, 2012, 2013
6 The Free Software Foundation, Inc.
10 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
11 Andrew Borodin <aborodin@vmail.ru> 2012, 2013
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 * \brief Source: editor low level data handling and cursor fundamentals
38 #include <sys/types.h>
47 #include "lib/global.h"
49 #include "lib/tty/color.h"
50 #include "lib/tty/tty.h" /* attrset() */
51 #include "lib/tty/key.h" /* is_idle() */
52 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
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
;
94 int option_edit_right_extreme
= 0;
95 int option_edit_left_extreme
= 0;
96 int option_edit_top_extreme
= 0;
97 int option_edit_bottom_extreme
= 0;
98 int enable_show_tabs_tws
= 1;
99 int option_check_nl_at_eof
= 0;
100 int option_group_undo
= 0;
101 int show_right_margin
= 0;
103 char *option_backup_ext
= NULL
;
105 unsigned int edit_stack_iterator
= 0;
106 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
107 /* magic sequense for say than block is vertical */
108 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
110 /*** file scope macro definitions ****************************************************************/
112 #define TEMP_BUF_LEN 1024
114 #define space_width 1
116 /*** file scope type declarations ****************************************************************/
118 /*** file scope variables ************************************************************************/
120 /* detecting an error on save is easy: just check if every byte has been written. */
121 /* detecting an error on read, is not so easy 'cos there is not way to tell
122 whether you read everything or not. */
123 /* FIXME: add proper 'triple_pipe_open' to read, write and check errors. */
124 static const struct edit_filters
126 const char *read
, *write
, *extension
;
130 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
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 /* --------------------------------------------------------------------------------------------- */
139 /*** file scope functions ************************************************************************/
140 /* --------------------------------------------------------------------------------------------- */
142 * Load file OR text into buffers. Set cursor to the beginning of file.
144 * @return FALSE on error.
148 edit_load_file_fast (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
153 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
159 g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath
));
160 edit_error_dialog (_("Error"), errmsg
);
165 ret
= (edit_buffer_read_file (&edit
->buffer
, file
, edit
->last_byte
) == edit
->last_byte
);
167 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
172 errmsg
= g_strdup_printf (_("Error reading %s"), vfs_path_as_str (filename_vpath
));
173 edit_error_dialog (_("Error"), errmsg
);
181 /* --------------------------------------------------------------------------------------------- */
182 /** Return index of the filter or -1 is there is no appropriate filter */
185 edit_find_filter (const vfs_path_t
* filename_vpath
)
189 if (filename_vpath
== NULL
)
192 l
= strlen (vfs_path_as_str (filename_vpath
));
193 for (i
= 0; i
< G_N_ELEMENTS (all_filters
); i
++)
195 e
= strlen (all_filters
[i
].extension
);
197 if (!strcmp (all_filters
[i
].extension
, vfs_path_as_str (filename_vpath
) + l
- e
))
203 /* --------------------------------------------------------------------------------------------- */
206 edit_get_filter (const vfs_path_t
* filename_vpath
)
209 char *p
, *quoted_name
;
211 i
= edit_find_filter (filename_vpath
);
215 quoted_name
= name_quote (vfs_path_as_str (filename_vpath
), 0);
216 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
217 g_free (quoted_name
);
221 /* --------------------------------------------------------------------------------------------- */
224 edit_insert_stream (WEdit
* edit
, FILE * f
)
228 while ((c
= fgetc (f
)) >= 0)
230 edit_insert (edit
, c
);
236 /* --------------------------------------------------------------------------------------------- */
238 * Open file and create it if necessary.
240 * @param edit editor object
241 * @param filename_vpath file name
242 * @param st buffer for store stat info
243 * @return TRUE for success, FALSE for error.
247 check_file_access (WEdit
* edit
, const vfs_path_t
* filename_vpath
, struct stat
*st
)
250 gchar
*errmsg
= NULL
;
252 /* Try opening an existing file */
253 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
257 * Try creating the file. O_EXCL prevents following broken links
258 * and opening existing files.
260 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
264 g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath
));
268 /* New file, delete it if it's not modified or saved */
269 edit
->delete_file
= 1;
272 /* Check what we have opened */
273 if (mc_fstat (file
, st
) < 0)
276 g_strdup_printf (_("Cannot get size/permissions for %s"),
277 vfs_path_as_str (filename_vpath
));
281 /* We want to open regular files only */
282 if (!S_ISREG (st
->st_mode
))
285 g_strdup_printf (_("\"%s\" is not a regular file"), vfs_path_as_str (filename_vpath
));
290 * Don't delete non-empty files.
291 * O_EXCL should prevent it, but let's be on the safe side.
294 edit
->delete_file
= 0;
296 if (st
->st_size
>= SIZE_LIMIT
)
297 errmsg
= g_strdup_printf (_("File \"%s\" is too large"), vfs_path_as_str (filename_vpath
));
300 (void) mc_close (file
);
304 edit_error_dialog (_("Error"), errmsg
);
311 /* --------------------------------------------------------------------------------------------- */
314 * Open the file and load it into the buffers, either directly or using
315 * a filter. Return TRUE on success, FALSE on error.
317 * Fast loading (edit_load_file_fast) is used when the file size is
318 * known. In this case the data is read into the buffers by blocks.
319 * If the file size is not known, the data is loaded byte by byte in
322 * @param edit editor object
323 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
326 edit_load_file (WEdit
* edit
)
328 gboolean fast_load
= TRUE
;
330 /* Cannot do fast load if a filter is used */
331 if (edit_find_filter (edit
->filename_vpath
) >= 0)
335 * FIXME: line end translation should disable fast loading as well
336 * Consider doing fseek() to the end and ftell() for the real size.
338 if (edit
->filename_vpath
!= NULL
)
341 * VFS may report file size incorrectly, and slow load is not a big
342 * deal considering overhead in VFS.
344 if (!vfs_file_is_local (edit
->filename_vpath
))
347 /* If we are dealing with a real file, check that it exists */
348 if (!check_file_access (edit
, edit
->filename_vpath
, &edit
->stat1
))
356 /* nothing to load */
360 edit_buffer_init (&edit
->buffer
);
364 edit
->last_byte
= edit
->stat1
.st_size
;
365 if (!edit_load_file_fast (edit
, edit
->filename_vpath
))
374 if (edit
->filename_vpath
!= NULL
375 && *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) != '\0')
377 edit
->undo_stack_disable
= 1;
378 if (edit_insert_file (edit
, edit
->filename_vpath
) < 0)
383 edit
->undo_stack_disable
= 0;
390 /* --------------------------------------------------------------------------------------------- */
391 /** Restore saved cursor position in the file */
394 edit_load_position (WEdit
* edit
)
399 if (edit
->filename_vpath
== NULL
400 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
403 load_file_position (edit
->filename_vpath
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
407 edit_move_to_line (edit
, line
- 1);
408 edit
->prev_col
= column
;
412 edit_cursor_move (edit
, offset
);
413 line
= edit
->curs_line
;
414 edit
->search_start
= edit
->buffer
.curs1
;
417 book_mark_restore (edit
, BOOK_MARK_COLOR
);
419 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->buffer
.curs1
));
420 edit_move_display (edit
, line
- (WIDGET (edit
)->lines
/ 2));
423 /* --------------------------------------------------------------------------------------------- */
424 /** Save cursor position in the file */
427 edit_save_position (WEdit
* edit
)
429 if (edit
->filename_vpath
== NULL
430 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
433 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
434 save_file_position (edit
->filename_vpath
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->buffer
.curs1
,
435 edit
->serialized_bookmarks
);
436 edit
->serialized_bookmarks
= NULL
;
439 /* --------------------------------------------------------------------------------------------- */
440 /** Clean the WEdit stricture except the widget part */
443 edit_purge_widget (WEdit
* edit
)
445 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
446 char *start
= (char *) edit
+ sizeof (Widget
);
447 memset (start
, 0, len
);
450 /* --------------------------------------------------------------------------------------------- */
453 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
454 then the file should be as it was when he loaded up. Then set edit->modified to 0.
458 edit_pop_undo_action (WEdit
* edit
)
461 unsigned long sp
= edit
->undo_stack_pointer
;
463 if (sp
== edit
->undo_stack_bottom
)
466 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
467 c
= edit
->undo_stack
[sp
];
470 /* edit->undo_stack[sp] = '@'; */
471 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
475 if (sp
== edit
->undo_stack_bottom
)
478 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
479 if (edit
->undo_stack
[sp
] == -2)
481 /* edit->undo_stack[sp] = '@'; */
482 edit
->undo_stack_pointer
= sp
;
485 edit
->undo_stack
[sp
]++;
491 edit_pop_redo_action (WEdit
* edit
)
494 unsigned long sp
= edit
->redo_stack_pointer
;
496 if (sp
== edit
->redo_stack_bottom
)
499 sp
= (sp
- 1) & edit
->redo_stack_size_mask
;
500 c
= edit
->redo_stack
[sp
];
503 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
507 if (sp
== edit
->redo_stack_bottom
)
510 c
= edit
->redo_stack
[(sp
- 1) & edit
->redo_stack_size_mask
];
511 if (edit
->redo_stack
[sp
] == -2)
512 edit
->redo_stack_pointer
= sp
;
514 edit
->redo_stack
[sp
]++;
520 get_prev_undo_action (WEdit
* edit
)
523 unsigned long sp
= edit
->undo_stack_pointer
;
525 if (sp
== edit
->undo_stack_bottom
)
528 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
529 c
= edit
->undo_stack
[sp
];
533 if (sp
== edit
->undo_stack_bottom
)
536 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
540 /* --------------------------------------------------------------------------------------------- */
541 /** is called whenever a modification is made by one of the four routines below */
544 edit_modification (WEdit
* edit
)
546 edit
->caches_valid
= FALSE
;
548 /* raise lock when file modified */
549 if (!edit
->modified
&& !edit
->delete_file
)
550 edit
->locked
= lock_file (edit
->filename_vpath
);
554 /* --------------------------------------------------------------------------------------------- */
555 /* high level cursor movement commands */
556 /* --------------------------------------------------------------------------------------------- */
557 /** check whether cursor is in indent part of line
559 * @param edit editor object
561 * @return TRUE if cursor is in indent, FALSE otherwise
565 is_in_indent (const WEdit
* edit
)
569 for (p
= edit_bol (edit
, edit
->buffer
.curs1
); p
< edit
->buffer
.curs1
; p
++)
570 if (strchr (" \t", edit_buffer_get_byte (&edit
->buffer
, p
)) == NULL
)
576 /* --------------------------------------------------------------------------------------------- */
577 /** check whether line in editor is blank or not
579 * @param edit editor object
580 * @param offset position in file
582 * @return TRUE if line in blank, FALSE otherwise
586 is_blank (const WEdit
* edit
, off_t offset
)
591 s
= edit_bol (edit
, offset
);
592 f
= edit_eol (edit
, offset
) - 1;
595 c
= edit_buffer_get_byte (&edit
->buffer
, s
++);
602 /* --------------------------------------------------------------------------------------------- */
603 /** returns the offset of line i */
606 edit_find_line (WEdit
* edit
, long line
)
609 long m
= 2000000000; /* what is the magic number? */
611 if (!edit
->caches_valid
)
613 memset (edit
->line_numbers
, 0, sizeof (edit
->line_numbers
));
614 memset (edit
->line_offsets
, 0, sizeof (edit
->line_offsets
));
615 /* three offsets that we *know* are line 0 at 0 and these two: */
616 edit
->line_numbers
[1] = edit
->curs_line
;
617 edit
->line_offsets
[1] = edit_bol (edit
, edit
->buffer
.curs1
);
618 edit
->line_numbers
[2] = edit
->total_lines
;
619 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
620 edit
->caches_valid
= TRUE
;
622 if (line
>= edit
->total_lines
)
623 return edit
->line_offsets
[2];
626 /* find the closest known point */
627 for (i
= 0; i
< N_LINE_CACHES
; i
++)
631 n
= abs (edit
->line_numbers
[i
] - line
);
639 return edit
->line_offsets
[j
]; /* know the offset exactly */
640 if (m
== 1 && j
>= 3)
641 i
= j
; /* one line different - caller might be looping, so stay in this cache */
643 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
644 if (line
> edit
->line_numbers
[j
])
645 edit
->line_offsets
[i
] =
646 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
648 edit
->line_offsets
[i
] =
649 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
650 edit
->line_numbers
[i
] = line
;
651 return edit
->line_offsets
[i
];
654 /* --------------------------------------------------------------------------------------------- */
655 /** moves up until a blank line is reached, or until just
656 before a non-blank line is reached */
659 edit_move_up_paragraph (WEdit
* edit
, gboolean do_scroll
)
663 if (edit
->curs_line
> 1)
665 if (!edit_line_is_blank (edit
, edit
->curs_line
))
667 for (i
= edit
->curs_line
- 1; i
!= 0; i
--)
668 if (edit_line_is_blank (edit
, i
))
671 else if (edit_line_is_blank (edit
, edit
->curs_line
- 1))
673 for (i
= edit
->curs_line
- 1; i
!= 0; i
--)
674 if (!edit_line_is_blank (edit
, i
))
682 for (i
= edit
->curs_line
- 1; i
!= 0; i
--)
683 if (edit_line_is_blank (edit
, i
))
688 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
691 /* --------------------------------------------------------------------------------------------- */
692 /** moves down until a blank line is reached, or until just
693 before a non-blank line is reached */
696 edit_move_down_paragraph (WEdit
* edit
, gboolean do_scroll
)
700 if (edit
->curs_line
>= edit
->total_lines
- 1)
701 i
= edit
->total_lines
;
702 else if (!edit_line_is_blank (edit
, edit
->curs_line
))
704 for (i
= edit
->curs_line
+ 1; i
!= 0; i
++)
705 if (edit_line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
708 else if (edit_line_is_blank (edit
, edit
->curs_line
+ 1))
710 for (i
= edit
->curs_line
+ 1; i
!= 0; i
++)
711 if (!edit_line_is_blank (edit
, i
) || i
> edit
->total_lines
)
719 for (i
= edit
->curs_line
+ 1; i
!= 0; i
++)
720 if (edit_line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
723 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
726 /* --------------------------------------------------------------------------------------------- */
729 edit_begin_page (WEdit
* edit
)
731 edit_update_curs_row (edit
);
732 edit_move_up (edit
, edit
->curs_row
, 0);
735 /* --------------------------------------------------------------------------------------------- */
738 edit_end_page (WEdit
* edit
)
740 edit_update_curs_row (edit
);
741 edit_move_down (edit
, WIDGET (edit
)->lines
- edit
->curs_row
- 1, 0);
745 /* --------------------------------------------------------------------------------------------- */
746 /** goto beginning of text */
749 edit_move_to_top (WEdit
* edit
)
753 edit_cursor_move (edit
, -edit
->buffer
.curs1
);
754 edit_move_to_prev_col (edit
, 0);
755 edit
->force
|= REDRAW_PAGE
;
756 edit
->search_start
= 0;
757 edit_update_curs_row (edit
);
762 /* --------------------------------------------------------------------------------------------- */
763 /** goto end of text */
766 edit_move_to_bottom (WEdit
* edit
)
768 if (edit
->curs_line
< edit
->total_lines
)
770 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
771 edit
->start_display
= edit
->last_byte
;
772 edit
->start_line
= edit
->total_lines
;
773 edit_scroll_upward (edit
, WIDGET (edit
)->lines
- 1);
774 edit
->force
|= REDRAW_PAGE
;
778 /* --------------------------------------------------------------------------------------------- */
779 /** goto beginning of line */
782 edit_cursor_to_bol (WEdit
* edit
)
784 edit_cursor_move (edit
, edit_bol (edit
, edit
->buffer
.curs1
) - edit
->buffer
.curs1
);
785 edit
->search_start
= edit
->buffer
.curs1
;
786 edit
->prev_col
= edit_get_col (edit
);
790 /* --------------------------------------------------------------------------------------------- */
791 /** goto end of line */
794 edit_cursor_to_eol (WEdit
* edit
)
796 edit_cursor_move (edit
, edit_eol (edit
, edit
->buffer
.curs1
) - edit
->buffer
.curs1
);
797 edit
->search_start
= edit
->buffer
.curs1
;
798 edit
->prev_col
= edit_get_col (edit
);
802 /* --------------------------------------------------------------------------------------------- */
809 const char option_chars_move_whole_word
[] =
810 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
816 if (*option_chars_move_whole_word
== '!')
820 if (g_ascii_isupper ((gchar
) c
))
822 else if (g_ascii_islower ((gchar
) c
))
824 else if (g_ascii_isalpha (c
))
826 else if (isdigit (c
))
828 else if (isspace (c
))
830 q
= strchr (option_chars_move_whole_word
, c
);
835 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
840 while ((q
= strchr (q
+ 1, c
)));
844 /* --------------------------------------------------------------------------------------------- */
847 edit_left_word_move (WEdit
* edit
, int s
)
853 if (edit
->column_highlight
854 && edit
->mark1
!= edit
->mark2
855 && edit
->over_col
== 0 && edit
->buffer
.curs1
== edit_bol (edit
, edit
->buffer
.curs1
))
857 edit_cursor_move (edit
, -1);
858 if (edit
->buffer
.curs1
== 0)
860 c1
= edit_buffer_get_previous_byte (&edit
->buffer
);
861 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
862 if (c1
== '\n' || c2
== '\n')
864 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
866 if (isspace (c1
) && !isspace (c2
))
868 if (s
!= 0 && !isspace (c1
) && isspace (c2
))
873 /* --------------------------------------------------------------------------------------------- */
876 edit_left_word_move_cmd (WEdit
* edit
)
878 edit_left_word_move (edit
, 0);
879 edit
->force
|= REDRAW_PAGE
;
882 /* --------------------------------------------------------------------------------------------- */
885 edit_right_word_move (WEdit
* edit
, int s
)
891 if (edit
->column_highlight
892 && edit
->mark1
!= edit
->mark2
893 && edit
->over_col
== 0 && edit
->buffer
.curs1
== edit_eol (edit
, edit
->buffer
.curs1
))
895 edit_cursor_move (edit
, 1);
896 if (edit
->buffer
.curs1
>= edit
->last_byte
)
898 c1
= edit_buffer_get_previous_byte (&edit
->buffer
);
899 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
900 if (c1
== '\n' || c2
== '\n')
902 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
904 if (isspace (c1
) && !isspace (c2
))
906 if (s
!= 0 && !isspace (c1
) && isspace (c2
))
911 /* --------------------------------------------------------------------------------------------- */
914 edit_right_word_move_cmd (WEdit
* edit
)
916 edit_right_word_move (edit
, 0);
917 edit
->force
|= REDRAW_PAGE
;
920 /* --------------------------------------------------------------------------------------------- */
923 edit_right_char_move_cmd (WEdit
* edit
)
931 c
= edit_buffer_get_utf (&edit
->buffer
, edit
->buffer
.curs1
, &cw
);
937 c
= edit_buffer_get_current_byte (&edit
->buffer
);
939 if (option_cursor_beyond_eol
&& c
== '\n')
942 edit_cursor_move (edit
, cw
);
945 /* --------------------------------------------------------------------------------------------- */
948 edit_left_char_move_cmd (WEdit
* edit
)
952 if (edit
->column_highlight
953 && option_cursor_beyond_eol
954 && edit
->mark1
!= edit
->mark2
955 && edit
->over_col
== 0 && edit
->buffer
.curs1
== edit_bol (edit
, edit
->buffer
.curs1
))
960 edit_buffer_get_prev_utf (&edit
->buffer
, edit
->buffer
.curs1
, &cw
);
965 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
971 edit_cursor_move (edit
, -cw
);
975 /* --------------------------------------------------------------------------------------------- */
976 /** Up or down cursor moving.
977 direction = TRUE - move up
982 edit_move_updown (WEdit
* edit
, long lines
, gboolean do_scroll
, gboolean direction
)
985 long l
= direction
? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
994 edit
->force
|= REDRAW_PAGE
;
998 edit_scroll_upward (edit
, lines
);
1000 edit_scroll_downward (edit
, lines
);
1002 p
= edit_bol (edit
, edit
->buffer
.curs1
);
1004 p
= direction
? edit_move_backward (edit
, p
, lines
) : edit_move_forward (edit
, p
, lines
, 0);
1006 edit_cursor_move (edit
, p
- edit
->buffer
.curs1
);
1008 edit_move_to_prev_col (edit
, p
);
1011 /* search start of current multibyte char (like CJK) */
1012 if (edit
->buffer
.curs1
> 0 && edit
->buffer
.curs1
+ 1 < edit
->last_byte
1013 && edit_buffer_get_current_byte (&edit
->buffer
) >= 256)
1015 edit_right_char_move_cmd (edit
);
1016 edit_left_char_move_cmd (edit
);
1020 edit
->search_start
= edit
->buffer
.curs1
;
1021 edit
->found_len
= 0;
1024 /* --------------------------------------------------------------------------------------------- */
1027 edit_right_delete_word (WEdit
* edit
)
1029 while (edit
->buffer
.curs1
< edit
->last_byte
)
1033 c1
= edit_delete (edit
, TRUE
);
1034 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
1035 if (c1
== '\n' || c2
== '\n')
1037 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1039 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1044 /* --------------------------------------------------------------------------------------------- */
1047 edit_left_delete_word (WEdit
* edit
)
1049 while (edit
->buffer
.curs1
> 0)
1053 c1
= edit_backspace (edit
, TRUE
);
1054 c2
= edit_buffer_get_previous_byte (&edit
->buffer
);
1055 if (c1
== '\n' || c2
== '\n')
1057 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1059 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1064 /* --------------------------------------------------------------------------------------------- */
1066 the start column position is not recorded, and hence does not
1067 undo as it happed. But who would notice.
1071 edit_do_undo (WEdit
* edit
)
1076 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1078 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1085 edit_cursor_move (edit
, 1);
1088 edit_cursor_move (edit
, -1);
1092 edit_backspace (edit
, TRUE
);
1096 edit_delete (edit
, TRUE
);
1099 edit
->column_highlight
= 1;
1102 edit
->column_highlight
= 0;
1105 if (ac
>= 256 && ac
< 512)
1106 edit_insert_ahead (edit
, ac
- 256);
1107 if (ac
>= 0 && ac
< 256)
1108 edit_insert (edit
, ac
);
1110 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1112 edit
->mark1
= ac
- MARK_1
;
1114 (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1116 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1118 edit
->mark2
= ac
- MARK_2
;
1120 (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1122 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1124 edit
->end_mark_curs
= ac
- MARK_CURS
;
1127 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1130 if (edit
->start_display
> ac
- KEY_PRESS
)
1132 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1133 edit
->force
|= REDRAW_PAGE
;
1135 else if (edit
->start_display
< ac
- KEY_PRESS
)
1137 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1138 edit
->force
|= REDRAW_PAGE
;
1140 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1141 edit_update_curs_row (edit
);
1144 edit
->undo_stack_disable
= 0;
1147 /* --------------------------------------------------------------------------------------------- */
1150 edit_do_redo (WEdit
* edit
)
1155 if (edit
->redo_stack_reset
)
1159 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1166 edit_cursor_move (edit
, 1);
1169 edit_cursor_move (edit
, -1);
1172 edit_backspace (edit
, TRUE
);
1175 edit_delete (edit
, TRUE
);
1178 edit
->column_highlight
= 1;
1181 edit
->column_highlight
= 0;
1184 if (ac
>= 256 && ac
< 512)
1185 edit_insert_ahead (edit
, ac
- 256);
1186 if (ac
>= 0 && ac
< 256)
1187 edit_insert (edit
, ac
);
1189 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1191 edit
->mark1
= ac
- MARK_1
;
1193 (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1195 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1197 edit
->mark2
= ac
- MARK_2
;
1199 (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1201 /* more than one pop usually means something big */
1203 edit
->force
|= REDRAW_PAGE
;
1206 if (edit
->start_display
> ac
- KEY_PRESS
)
1208 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1209 edit
->force
|= REDRAW_PAGE
;
1211 else if (edit
->start_display
< ac
- KEY_PRESS
)
1213 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1214 edit
->force
|= REDRAW_PAGE
;
1216 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1217 edit_update_curs_row (edit
);
1223 /* --------------------------------------------------------------------------------------------- */
1226 edit_group_undo (WEdit
* edit
)
1228 long ac
= KEY_PRESS
;
1229 long cur_ac
= KEY_PRESS
;
1230 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1232 cur_ac
= get_prev_undo_action (edit
);
1233 edit_do_undo (edit
);
1234 ac
= get_prev_undo_action (edit
);
1235 /* exit from cycle if option_group_undo is not set,
1236 * and make single UNDO operation
1238 if (!option_group_undo
)
1243 /* --------------------------------------------------------------------------------------------- */
1246 edit_delete_to_line_end (WEdit
* edit
)
1248 while (edit_buffer_get_current_byte (&edit
->buffer
) != '\n' && edit
->buffer
.curs2
!= 0)
1249 edit_delete (edit
, TRUE
);
1252 /* --------------------------------------------------------------------------------------------- */
1255 edit_delete_to_line_begin (WEdit
* edit
)
1257 while (edit_buffer_get_previous_byte (&edit
->buffer
) != '\n' && edit
->buffer
.curs1
!= 0)
1258 edit_backspace (edit
, TRUE
);
1261 /* --------------------------------------------------------------------------------------------- */
1264 is_aligned_on_a_tab (WEdit
* edit
)
1268 edit_update_curs_col (edit
);
1269 curs_col
= edit
->curs_col
% (TAB_SIZE
* space_width
);
1270 return (curs_col
== 0 || curs_col
== (HALF_TAB_SIZE
* space_width
));
1273 /* --------------------------------------------------------------------------------------------- */
1276 right_of_four_spaces (WEdit
* edit
)
1280 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1281 ch
|= edit_buffer_get_byte (&edit
->buffer
, edit
->buffer
.curs1
- i
);
1283 return (ch
== ' ' && is_aligned_on_a_tab (edit
));
1286 /* --------------------------------------------------------------------------------------------- */
1289 left_of_four_spaces (WEdit
* edit
)
1293 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1294 ch
|= edit_buffer_get_byte (&edit
->buffer
, edit
->buffer
.curs1
+ i
);
1296 return (ch
== ' ' && is_aligned_on_a_tab (edit
));
1299 /* --------------------------------------------------------------------------------------------- */
1302 edit_auto_indent (WEdit
* edit
)
1307 p
= edit
->buffer
.curs1
;
1308 /* use the previous line as a template */
1309 p
= edit_move_backward (edit
, p
, 1);
1310 /* copy the leading whitespace of the line */
1312 { /* no range check - the line _is_ \n-terminated */
1313 c
= edit_buffer_get_byte (&edit
->buffer
, p
++);
1314 if (c
!= ' ' && c
!= '\t')
1316 edit_insert (edit
, c
);
1320 /* --------------------------------------------------------------------------------------------- */
1323 edit_double_newline (WEdit
* edit
)
1325 edit_insert (edit
, '\n');
1326 if (edit_buffer_get_current_byte (&edit
->buffer
) == '\n'
1327 || edit_buffer_get_byte (&edit
->buffer
, edit
->buffer
.curs1
- 2) == '\n')
1329 edit
->force
|= REDRAW_PAGE
;
1330 edit_insert (edit
, '\n');
1333 /* --------------------------------------------------------------------------------------------- */
1336 insert_spaces_tab (WEdit
* edit
, gboolean half
)
1340 edit_update_curs_col (edit
);
1341 i
= option_tab_spacing
* space_width
;
1346 i
= ((edit
->curs_col
/ i
) + 1) * i
- edit
->curs_col
;
1349 edit_insert (edit
, ' ');
1355 /* --------------------------------------------------------------------------------------------- */
1358 edit_tab_cmd (WEdit
* edit
)
1360 if (option_fake_half_tabs
&& is_in_indent (edit
))
1362 /* insert a half tab (usually four spaces) unless there is a
1363 half tab already behind, then delete it and insert a
1365 if (option_fill_tabs_with_spaces
|| !right_of_four_spaces (edit
))
1366 insert_spaces_tab (edit
, TRUE
);
1371 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1372 edit_backspace (edit
, TRUE
);
1373 edit_insert (edit
, '\t');
1376 else if (option_fill_tabs_with_spaces
)
1377 insert_spaces_tab (edit
, FALSE
);
1379 edit_insert (edit
, '\t');
1382 /* --------------------------------------------------------------------------------------------- */
1385 check_and_wrap_line (WEdit
* edit
)
1390 if (!option_typewriter_wrap
)
1392 edit_update_curs_col (edit
);
1393 if (edit
->curs_col
< option_word_wrap_line_length
)
1395 curs
= edit
->buffer
.curs1
;
1399 c
= edit_buffer_get_byte (&edit
->buffer
, curs
);
1400 if (c
== '\n' || curs
<= 0)
1402 edit_insert (edit
, '\n');
1405 if (c
== ' ' || c
== '\t')
1407 off_t current
= edit
->buffer
.curs1
;
1408 edit_cursor_move (edit
, curs
- edit
->buffer
.curs1
+ 1);
1409 edit_insert (edit
, '\n');
1410 edit_cursor_move (edit
, current
- edit
->buffer
.curs1
+ 1);
1416 /* --------------------------------------------------------------------------------------------- */
1417 /** this find the matching bracket in either direction, and sets edit->bracket
1419 * @param edit editor object
1420 * @param in_screen seach only on the current screen
1421 * @param furthest_bracket_search count of the bytes for search
1423 * @return position of the found bracket (-1 if no match)
1427 edit_get_bracket (WEdit
* edit
, gboolean in_screen
, unsigned long furthest_bracket_search
)
1429 const char *const b
= "{}{[][()(", *p
;
1430 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
1431 unsigned long j
= 0;
1434 edit_update_curs_row (edit
);
1435 c
= edit_buffer_get_current_byte (&edit
->buffer
);
1438 if (!furthest_bracket_search
)
1439 furthest_bracket_search
--;
1440 /* not on a bracket at all */
1443 /* the matching bracket */
1445 /* going left or right? */
1446 if (strchr ("{[(", c
))
1448 for (q
= edit
->buffer
.curs1
+ inc
;; q
+= inc
)
1450 /* out of buffer? */
1451 if (q
>= edit
->last_byte
|| q
< 0)
1453 a
= edit_buffer_get_byte (&edit
->buffer
, q
);
1454 /* don't want to eat CPU */
1455 if (j
++ > furthest_bracket_search
)
1457 /* out of screen? */
1460 if (q
< edit
->start_display
)
1462 /* count lines if searching downward */
1463 if (inc
> 0 && a
== '\n')
1464 if (n
++ >= WIDGET (edit
)->lines
- edit
->curs_row
) /* out of screen */
1467 /* count bracket depth */
1468 i
+= (a
== c
) - (a
== d
);
1469 /* return if bracket depth is zero */
1477 /* --------------------------------------------------------------------------------------------- */
1480 edit_goto_matching_bracket (WEdit
* edit
)
1484 q
= edit_get_bracket (edit
, 0, 0);
1487 edit
->bracket
= edit
->buffer
.curs1
;
1488 edit
->force
|= REDRAW_PAGE
;
1489 edit_cursor_move (edit
, q
- edit
->buffer
.curs1
);
1493 /* --------------------------------------------------------------------------------------------- */
1496 edit_move_block_to_right (WEdit
* edit
)
1498 off_t start_mark
, end_mark
;
1499 long cur_bol
, start_bol
;
1501 if (eval_marks (edit
, &start_mark
, &end_mark
))
1504 start_bol
= edit_bol (edit
, start_mark
);
1505 cur_bol
= edit_bol (edit
, end_mark
- 1);
1509 edit_cursor_move (edit
, cur_bol
- edit
->buffer
.curs1
);
1510 if (!edit_line_is_blank (edit
, edit
->curs_line
))
1512 if (option_fill_tabs_with_spaces
)
1513 insert_spaces_tab (edit
, option_fake_half_tabs
);
1515 edit_insert (edit
, '\t');
1516 edit_cursor_move (edit
, edit_bol (edit
, cur_bol
) - edit
->buffer
.curs1
);
1522 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1524 while (cur_bol
>= start_bol
);
1526 edit
->force
|= REDRAW_PAGE
;
1529 /* --------------------------------------------------------------------------------------------- */
1532 edit_move_block_to_left (WEdit
* edit
)
1534 off_t start_mark
, end_mark
;
1535 off_t cur_bol
, start_bol
;
1538 if (eval_marks (edit
, &start_mark
, &end_mark
))
1541 start_bol
= edit_bol (edit
, start_mark
);
1542 cur_bol
= edit_bol (edit
, end_mark
- 1);
1549 edit_cursor_move (edit
, cur_bol
- edit
->buffer
.curs1
);
1551 if (option_fake_half_tabs
)
1552 del_tab_width
= HALF_TAB_SIZE
;
1554 del_tab_width
= option_tab_spacing
;
1556 next_char
= edit_buffer_get_current_byte (&edit
->buffer
);
1557 if (next_char
== '\t')
1558 edit_delete (edit
, TRUE
);
1559 else if (next_char
== ' ')
1560 for (i
= 1; i
<= del_tab_width
; i
++)
1562 if (next_char
== ' ')
1563 edit_delete (edit
, TRUE
);
1564 next_char
= edit_buffer_get_current_byte (&edit
->buffer
);
1570 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1572 while (cur_bol
>= start_bol
);
1574 edit
->force
|= REDRAW_PAGE
;
1577 /* --------------------------------------------------------------------------------------------- */
1579 * prints at the cursor
1580 * @return number of chars printed
1584 edit_print_string (WEdit
* e
, const char *s
)
1588 while (s
[i
] != '\0')
1589 edit_execute_cmd (e
, CK_InsertChar
, (unsigned char) s
[i
++]);
1590 e
->force
|= REDRAW_COMPLETELY
;
1591 edit_update_screen (e
);
1595 /* --------------------------------------------------------------------------------------------- */
1598 edit_insert_column_from_file (WEdit
* edit
, int file
, off_t
* start_pos
, off_t
* end_pos
,
1599 long *col1
, long *col2
)
1603 off_t blocklen
= -1, width
= 0;
1604 unsigned char *data
;
1606 cursor
= edit
->buffer
.curs1
;
1607 col
= edit_get_col (edit
);
1608 data
= g_malloc0 (TEMP_BUF_LEN
);
1610 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0)
1615 pn
= strchr ((char *) data
, '\n');
1616 width
= pn
== NULL
? blocklen
: pn
- (char *) data
;
1618 for (i
= 0; i
< blocklen
; i
++)
1620 if (data
[i
] != '\n')
1621 edit_insert (edit
, data
[i
]);
1623 { /* fill in and move to next line */
1627 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
1628 for (l
= width
- (edit_get_col (edit
) - col
); l
> 0; l
-= space_width
)
1629 edit_insert (edit
, ' ');
1631 for (p
= edit
->buffer
.curs1
;; p
++)
1633 if (p
== edit
->last_byte
)
1635 edit_cursor_move (edit
, edit
->last_byte
- edit
->buffer
.curs1
);
1636 edit_insert_ahead (edit
, '\n');
1640 if (edit_buffer_get_byte (&edit
->buffer
, p
) == '\n')
1647 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->buffer
.curs1
);
1649 for (l
= col
- edit_get_col (edit
); l
>= space_width
; l
-= space_width
)
1650 edit_insert (edit
, ' ');
1655 *col2
= col
+ width
;
1656 *start_pos
= cursor
;
1657 *end_pos
= edit
->buffer
.curs1
;
1658 edit_cursor_move (edit
, cursor
- edit
->buffer
.curs1
);
1664 /* --------------------------------------------------------------------------------------------- */
1665 /*** public functions ****************************************************************************/
1666 /* --------------------------------------------------------------------------------------------- */
1668 /** User edit menu, like user menu (F2) but only in editor. */
1671 user_menu (WEdit
* edit
, const char *menu_file
, int selected_entry
)
1676 off_t start_mark
, end_mark
;
1678 vfs_path_t
*block_file_vpath
;
1680 block_file
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
1681 block_file_vpath
= vfs_path_from_str (block_file
);
1682 curs
= edit
->buffer
.curs1
;
1683 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
1685 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1687 /* run shell scripts from menu */
1688 if (user_menu_cmd (edit
, menu_file
, selected_entry
)
1689 && (mc_stat (block_file_vpath
, &status
) == 0) && (status
.st_size
!= 0))
1694 /* i.e. we have marked block */
1696 rc
= edit_block_delete_cmd (edit
);
1702 ins_len
= edit_insert_file (edit
, block_file_vpath
);
1703 if (nomark
== 0 && ins_len
> 0)
1704 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1706 /* truncate block file */
1707 fd
= fopen (block_file
, "w");
1711 g_free (block_file
);
1712 vfs_path_free (block_file_vpath
);
1714 edit_cursor_move (edit
, curs
- edit
->buffer
.curs1
);
1715 edit
->force
|= REDRAW_PAGE
;
1716 widget_redraw (WIDGET (edit
));
1719 /* --------------------------------------------------------------------------------------------- */
1722 edit_get_write_filter (const vfs_path_t
* write_name_vpath
, const vfs_path_t
* filename_vpath
)
1725 char *p
, *writename
;
1726 const vfs_path_element_t
*path_element
;
1728 i
= edit_find_filter (filename_vpath
);
1732 path_element
= vfs_path_get_by_index (write_name_vpath
, -1);
1733 writename
= name_quote (path_element
->path
, 0);
1734 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
1739 /* --------------------------------------------------------------------------------------------- */
1741 * @param edit editor object
1742 * @param f value of stream file
1743 * @return the length of the file
1747 edit_write_stream (WEdit
* edit
, FILE * f
)
1751 if (edit
->lb
== LB_ASIS
)
1753 for (i
= 0; i
< edit
->last_byte
; i
++)
1754 if (fputc (edit_buffer_get_byte (&edit
->buffer
, i
), f
) < 0)
1759 /* change line breaks */
1760 for (i
= 0; i
< edit
->last_byte
; i
++)
1764 c
= edit_buffer_get_byte (&edit
->buffer
, i
);
1765 if (!(c
== '\n' || c
== '\r'))
1767 /* not line break */
1768 if (fputc (c
, f
) < 0)
1772 { /* (c == '\n' || c == '\r') */
1775 c1
= edit_buffer_get_byte (&edit
->buffer
, i
+ 1); /* next char */
1779 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
1780 /* put one line break unconditionally */
1781 if (fputc ('\n', f
) < 0)
1784 i
++; /* 2 chars are processed */
1786 if (c
== '\r' && c1
== '\n')
1787 /* Windows line break; go to the next char */
1790 if (c
== '\r' && c1
== '\r')
1792 /* two Macintosh line breaks; put second line break */
1793 if (fputc ('\n', f
) < 0)
1798 if (fputc (c1
, f
) < 0)
1802 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
1803 /* put one line break unconditionally */
1804 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
1807 if (c
== '\r' && c1
== '\n')
1808 /* Windows line break; go to the next char */
1812 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
1813 /* put one line break unconditionally */
1814 if (fputc ('\r', f
) < 0)
1817 i
++; /* 2 chars are processed */
1819 if (c
== '\r' && c1
== '\n')
1820 /* Windows line break; go to the next char */
1823 if (c
== '\n' && c1
== '\n')
1825 /* two Windows line breaks; put second line break */
1826 if (fputc ('\r', f
) < 0)
1831 if (fputc (c1
, f
) < 0)
1834 case LB_ASIS
: /* default without changes */
1840 return edit
->last_byte
;
1843 /* --------------------------------------------------------------------------------------------- */
1846 is_break_char (char c
)
1848 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
1851 /* --------------------------------------------------------------------------------------------- */
1854 edit_get_word_from_pos (const WEdit
* edit
, off_t start_pos
, off_t
* start
, gsize
* len
,
1859 GString
*match_expr
;
1862 for (word_start
= start_pos
; word_start
!= 0; word_start
--, cut_len
++)
1864 c1
= edit_buffer_get_byte (&edit
->buffer
, word_start
);
1865 c2
= edit_buffer_get_byte (&edit
->buffer
, word_start
- 1);
1867 if (is_break_char (c1
) != is_break_char (c2
) || c1
== '\n' || c2
== '\n')
1871 match_expr
= g_string_sized_new (16);
1875 c1
= edit_buffer_get_byte (&edit
->buffer
, word_start
+ match_expr
->len
);
1876 c2
= edit_buffer_get_byte (&edit
->buffer
, word_start
+ match_expr
->len
+ 1);
1877 g_string_append_c (match_expr
, c1
);
1879 while (!(is_break_char (c1
) != is_break_char (c2
) || c1
== '\n' || c2
== '\n'));
1881 *len
= match_expr
->len
;
1882 *start
= word_start
;
1885 return g_string_free (match_expr
, FALSE
);
1888 /* --------------------------------------------------------------------------------------------- */
1889 /** inserts a file at the cursor, returns count of inserted bytes on success */
1892 edit_insert_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
1898 p
= edit_get_filter (filename_vpath
);
1899 current
= edit
->buffer
.curs1
;
1905 f
= (FILE *) popen (p
, "r");
1908 edit_insert_stream (edit
, f
);
1910 /* Place cursor at the end of text selection */
1911 if (!option_cursor_after_inserted_block
)
1913 ins_len
= edit
->buffer
.curs1
- current
;
1914 edit_cursor_move (edit
, -ins_len
);
1920 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
1921 edit_error_dialog (_("Error"), errmsg
);
1930 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
1931 edit_error_dialog (_("Error"), errmsg
);
1941 int vertical_insertion
= 0;
1944 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
1948 buf
= g_malloc0 (TEMP_BUF_LEN
);
1949 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
1952 /* if contain signature VERTICAL_MAGIC then it vertical block */
1953 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
1954 vertical_insertion
= 1;
1956 mc_lseek (file
, 0, SEEK_SET
);
1959 if (vertical_insertion
)
1964 blocklen
= edit_insert_column_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
1965 edit_set_markers (edit
, edit
->buffer
.curs1
, mark2
, c1
, c2
);
1967 /* highlight inserted text then not persistent blocks */
1968 if (!option_persistent_selections
&& edit
->modified
)
1970 if (!edit
->column_highlight
)
1971 edit_push_undo_action (edit
, COLUMN_OFF
);
1972 edit
->column_highlight
= 1;
1979 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
1981 for (i
= 0; i
< blocklen
; i
++)
1982 edit_insert (edit
, buf
[i
]);
1984 /* highlight inserted text then not persistent blocks */
1985 if (!option_persistent_selections
&& edit
->modified
)
1987 edit_set_markers (edit
, edit
->buffer
.curs1
, current
, 0, 0);
1988 if (edit
->column_highlight
)
1989 edit_push_undo_action (edit
, COLUMN_ON
);
1990 edit
->column_highlight
= 0;
1993 /* Place cursor at the end of text selection */
1994 if (!option_cursor_after_inserted_block
)
1996 ins_len
= edit
->buffer
.curs1
- current
;
1997 edit_cursor_move (edit
, -ins_len
);
2001 edit
->force
|= REDRAW_PAGE
;
2011 /* --------------------------------------------------------------------------------------------- */
2013 * Fill in the edit structure. Return NULL on failure. Pass edit as
2014 * NULL to allocate a new structure.
2016 * If line is 0, try to restore saved position. Otherwise put the
2017 * cursor on that line and show it in the middle of the screen.
2021 edit_init (WEdit
* edit
, int y
, int x
, int lines
, int cols
, const vfs_path_t
* filename_vpath
,
2024 gboolean to_free
= FALSE
;
2026 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2027 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
2031 /* save some widget parameters */
2032 gboolean fullscreen
= edit
->fullscreen
;
2033 int y_prev
= edit
->y_prev
;
2034 int x_prev
= edit
->x_prev
;
2035 int lines_prev
= edit
->lines_prev
;
2036 int cols_prev
= edit
->cols_prev
;
2038 edit_purge_widget (edit
);
2040 /* restore saved parameters */
2041 edit
->fullscreen
= fullscreen
;
2042 edit
->y_prev
= y_prev
;
2043 edit
->x_prev
= x_prev
;
2044 edit
->lines_prev
= lines_prev
;
2045 edit
->cols_prev
= cols_prev
;
2049 edit
= g_malloc0 (sizeof (WEdit
));
2052 widget_init (WIDGET (edit
), y
, x
, lines
, cols
, NULL
, NULL
);
2053 edit
->fullscreen
= TRUE
;
2054 edit_save_size (edit
);
2057 edit
->drag_state
= MCEDIT_DRAG_NORMAL
;
2059 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2060 edit
->stat1
.st_uid
= getuid ();
2061 edit
->stat1
.st_gid
= getgid ();
2062 edit
->stat1
.st_mtime
= 0;
2066 edit
->last_bracket
= -1;
2067 edit
->force
|= REDRAW_PAGE
;
2069 /* set file name before load file */
2070 edit_set_filename (edit
, filename_vpath
);
2072 edit
->undo_stack_size
= START_STACK_SIZE
;
2073 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2074 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2076 edit
->redo_stack_size
= START_STACK_SIZE
;
2077 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2078 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2082 edit
->converter
= str_cnv_from_term
;
2083 edit_set_codeset (edit
);
2086 if (!edit_load_file (edit
))
2088 /* edit_load_file already gives an error message */
2094 edit
->loading_done
= 1;
2097 edit_load_syntax (edit
, NULL
, NULL
);
2098 edit_get_syntax_color (edit
, -1);
2100 /* load saved cursor position */
2101 if ((line
== 0) && option_save_position
)
2102 edit_load_position (edit
);
2107 edit_move_display (edit
, line
- 1);
2108 edit_move_to_line (edit
, line
- 1);
2111 edit_load_macro_cmd (edit
);
2116 /* --------------------------------------------------------------------------------------------- */
2118 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2120 edit_clean (WEdit
* edit
)
2125 /* a stale lock, remove it */
2127 edit
->locked
= unlock_file (edit
->filename_vpath
);
2129 /* save cursor position */
2130 if (option_save_position
)
2131 edit_save_position (edit
);
2132 else if (edit
->serialized_bookmarks
!= NULL
)
2133 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2135 /* File specified on the mcedit command line and never saved */
2136 if (edit
->delete_file
)
2137 unlink (vfs_path_get_last_path_str (edit
->filename_vpath
));
2139 edit_free_syntax_rules (edit
);
2140 book_mark_flush (edit
, -1);
2142 edit_buffer_clean (&edit
->buffer
);
2144 g_free (edit
->undo_stack
);
2145 g_free (edit
->redo_stack
);
2146 vfs_path_free (edit
->filename_vpath
);
2147 vfs_path_free (edit
->dir_vpath
);
2148 mc_search_free (edit
->search
);
2149 edit
->search
= NULL
;
2152 if (edit
->converter
!= str_cnv_from_term
)
2153 str_close_conv (edit
->converter
);
2156 edit_purge_widget (edit
);
2161 /* --------------------------------------------------------------------------------------------- */
2164 * Load a new file into the editor and set line. If it fails, preserve the old file.
2165 * To do it, allocate a new widget, initialize it and, if the new file
2166 * was loaded, copy the data to the old widget.
2168 * @return TRUE on success, FALSE on failure.
2171 edit_reload_line (WEdit
* edit
, const vfs_path_t
* filename_vpath
, long line
)
2173 Widget
*w
= WIDGET (edit
);
2176 e
= g_malloc0 (sizeof (WEdit
));
2178 /* save some widget parameters */
2179 e
->fullscreen
= edit
->fullscreen
;
2180 e
->y_prev
= edit
->y_prev
;
2181 e
->x_prev
= edit
->x_prev
;
2182 e
->lines_prev
= edit
->lines_prev
;
2183 e
->cols_prev
= edit
->cols_prev
;
2185 if (edit_init (e
, w
->y
, w
->x
, w
->lines
, w
->cols
, filename_vpath
, line
) == NULL
)
2192 memcpy (edit
, e
, sizeof (WEdit
));
2198 /* --------------------------------------------------------------------------------------------- */
2202 edit_set_codeset (WEdit
* edit
)
2207 get_codepage_id (mc_global
.source_codepage
>=
2208 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2213 conv
= str_crt_conv_from (cp_id
);
2214 if (conv
!= INVALID_CONV
)
2216 if (edit
->converter
!= str_cnv_from_term
)
2217 str_close_conv (edit
->converter
);
2218 edit
->converter
= conv
;
2223 edit
->utf8
= str_isutf8 (cp_id
);
2228 /* --------------------------------------------------------------------------------------------- */
2231 * Recording stack for undo:
2232 * The following is an implementation of a compressed stack. Identical
2233 * pushes are recorded by a negative prefix indicating the number of times the
2234 * same char was pushed. This saves space for repeated curs-left or curs-right
2251 * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2252 * 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2253 * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2254 * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2257 * The only way the cursor moves or the buffer is changed is through the routines:
2258 * insert, backspace, insert_ahead, delete, and cursor_move.
2259 * These record the reverse undo movements onto the stack each time they are
2262 * Each key press results in a set of actions (insert; delete ...). So each time
2263 * a key is pressed the current position of start_display is pushed as
2264 * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2265 * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2266 * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2270 * @param edit editor object
2271 * @param c code of the action
2275 edit_push_undo_action (WEdit
* edit
, long c
)
2277 unsigned long sp
= edit
->undo_stack_pointer
;
2281 /* first enlarge the stack if necessary */
2282 if (sp
> edit
->undo_stack_size
- 10)
2284 if (option_max_undo
< 256)
2285 option_max_undo
= 256;
2286 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2288 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2291 edit
->undo_stack
= t
;
2292 edit
->undo_stack_size
<<= 1;
2293 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2297 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2298 if (edit
->undo_stack_disable
)
2300 edit_push_redo_action (edit
, KEY_PRESS
);
2301 edit_push_redo_action (edit
, c
);
2305 if (edit
->redo_stack_reset
)
2306 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2308 if (edit
->undo_stack_bottom
!= sp
2309 && spm1
!= edit
->undo_stack_bottom
2310 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2313 if (edit
->undo_stack
[spm1
] < 0)
2315 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2316 if (d
== c
&& edit
->undo_stack
[spm1
] > -1000000000)
2318 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2319 edit
->undo_stack
[spm1
]--;
2325 d
= edit
->undo_stack
[spm1
];
2329 return; /* --> no need to push multiple do-nothings */
2330 edit
->undo_stack
[sp
] = -2;
2335 edit
->undo_stack
[sp
] = c
;
2338 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2340 /* if the sp wraps round and catches the undo_stack_bottom then erase
2341 * the first set of actions on the stack to make space - by moving
2342 * undo_stack_bottom forward one "key press" */
2343 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2344 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2345 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2348 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2350 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2351 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2353 /*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: */
2354 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2355 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2357 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2362 edit_push_redo_action (WEdit
* edit
, long c
)
2364 unsigned long sp
= edit
->redo_stack_pointer
;
2367 /* first enlarge the stack if necessary */
2368 if (sp
> edit
->redo_stack_size
- 10)
2370 if (option_max_undo
< 256)
2371 option_max_undo
= 256;
2372 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2374 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2377 edit
->redo_stack
= t
;
2378 edit
->redo_stack_size
<<= 1;
2379 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2383 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2385 if (edit
->redo_stack_bottom
!= sp
2386 && spm1
!= edit
->redo_stack_bottom
2387 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2390 if (edit
->redo_stack
[spm1
] < 0)
2392 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2393 if (d
== c
&& edit
->redo_stack
[spm1
] > -1000000000)
2395 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2396 edit
->redo_stack
[spm1
]--;
2402 d
= edit
->redo_stack
[spm1
];
2406 return; /* --> no need to push multiple do-nothings */
2407 edit
->redo_stack
[sp
] = -2;
2408 goto redo_check_bottom
;
2412 edit
->redo_stack
[sp
] = c
;
2415 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2417 /* if the sp wraps round and catches the redo_stack_bottom then erase
2418 * the first set of actions on the stack to make space - by moving
2419 * redo_stack_bottom forward one "key press" */
2420 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2421 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2422 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2425 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2427 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2428 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2431 * If a single key produced enough pushes to wrap all the way round then
2432 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2433 * The stack is then initialised:
2436 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2437 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2438 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2441 /* --------------------------------------------------------------------------------------------- */
2443 Basic low level single character buffer alterations and movements at the cursor.
2447 edit_insert (WEdit
* edit
, int c
)
2449 /* check if file has grown to large */
2450 if (edit
->last_byte
>= SIZE_LIMIT
)
2453 /* first we must update the position of the display window */
2454 if (edit
->buffer
.curs1
< edit
->start_display
)
2456 edit
->start_display
++;
2461 /* Mark file as modified, unless the file hasn't been fully loaded */
2462 if (edit
->loading_done
)
2463 edit_modification (edit
);
2465 /* now we must update some info on the file and check if a redraw is required */
2468 book_mark_inc (edit
, edit
->curs_line
);
2470 edit
->total_lines
++;
2471 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2474 /* save the reverse command onto the undo stack */
2475 /* ordinary char and not space */
2477 edit_push_undo_action (edit
, BACKSPACE
);
2479 edit_push_undo_action (edit
, BACKSPACE_BR
);
2480 /* update markers */
2481 edit
->mark1
+= (edit
->mark1
> edit
->buffer
.curs1
) ? 1 : 0;
2482 edit
->mark2
+= (edit
->mark2
> edit
->buffer
.curs1
) ? 1 : 0;
2483 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->buffer
.curs1
) ? 1 : 0;
2485 edit_buffer_insert (&edit
->buffer
, c
);
2487 /* update file length */
2491 /* --------------------------------------------------------------------------------------------- */
2492 /** same as edit_insert and move left */
2495 edit_insert_ahead (WEdit
* edit
, int c
)
2497 if (edit
->last_byte
>= SIZE_LIMIT
)
2500 if (edit
->buffer
.curs1
< edit
->start_display
)
2502 edit
->start_display
++;
2506 edit_modification (edit
);
2509 book_mark_inc (edit
, edit
->curs_line
);
2510 edit
->total_lines
++;
2511 edit
->force
|= REDRAW_AFTER_CURSOR
;
2513 /* ordinary char and not space */
2515 edit_push_undo_action (edit
, DELCHAR
);
2517 edit_push_undo_action (edit
, DELCHAR_BR
);
2519 edit
->mark1
+= (edit
->mark1
>= edit
->buffer
.curs1
) ? 1 : 0;
2520 edit
->mark2
+= (edit
->mark2
>= edit
->buffer
.curs1
) ? 1 : 0;
2521 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->buffer
.curs1
) ? 1 : 0;
2523 edit_buffer_insert_ahead (&edit
->buffer
, c
);
2528 /* --------------------------------------------------------------------------------------------- */
2531 edit_insert_over (WEdit
* edit
)
2535 for (i
= 0; i
< edit
->over_col
; i
++)
2536 edit_insert (edit
, ' ');
2541 /* --------------------------------------------------------------------------------------------- */
2544 edit_delete (WEdit
* edit
, gboolean byte_delete
)
2550 if (edit
->buffer
.curs2
== 0)
2554 /* if byte_delete == TRUE then delete only one byte not multibyte char */
2555 if (edit
->utf8
&& !byte_delete
)
2557 edit_buffer_get_utf (&edit
->buffer
, edit
->buffer
.curs1
, &cw
);
2565 if (edit
->mark2
!= edit
->mark1
)
2566 edit_push_markers (edit
);
2568 for (i
= 1; i
<= cw
; i
++)
2570 if (edit
->mark1
> edit
->buffer
.curs1
)
2573 edit
->end_mark_curs
--;
2575 if (edit
->mark2
> edit
->buffer
.curs1
)
2577 if (edit
->last_get_rule
> edit
->buffer
.curs1
)
2578 edit
->last_get_rule
--;
2580 p
= edit_buffer_delete (&edit
->buffer
);
2583 edit_push_undo_action (edit
, p
+ 256);
2586 edit_modification (edit
);
2589 book_mark_dec (edit
, edit
->curs_line
);
2590 edit
->total_lines
--;
2591 edit
->force
|= REDRAW_AFTER_CURSOR
;
2593 if (edit
->buffer
.curs1
< edit
->start_display
)
2595 edit
->start_display
--;
2603 /* --------------------------------------------------------------------------------------------- */
2606 edit_backspace (WEdit
* edit
, gboolean byte_delete
)
2612 if (edit
->buffer
.curs1
== 0)
2615 if (edit
->mark2
!= edit
->mark1
)
2616 edit_push_markers (edit
);
2619 if (edit
->utf8
&& !byte_delete
)
2621 edit_buffer_get_prev_utf (&edit
->buffer
, edit
->buffer
.curs1
, &cw
);
2629 for (i
= 1; i
<= cw
; i
++)
2631 if (edit
->mark1
>= edit
->buffer
.curs1
)
2634 edit
->end_mark_curs
--;
2636 if (edit
->mark2
>= edit
->buffer
.curs1
)
2638 if (edit
->last_get_rule
>= edit
->buffer
.curs1
)
2639 edit
->last_get_rule
--;
2641 p
= edit_buffer_backspace (&edit
->buffer
);
2644 edit_push_undo_action (edit
, p
);
2646 edit_modification (edit
);
2649 book_mark_dec (edit
, edit
->curs_line
);
2651 edit
->total_lines
--;
2652 edit
->force
|= REDRAW_AFTER_CURSOR
;
2655 if (edit
->buffer
.curs1
< edit
->start_display
)
2657 edit
->start_display
--;
2665 /* --------------------------------------------------------------------------------------------- */
2666 /** moves the cursor right or left: increment positive or negative respectively */
2669 edit_cursor_move (WEdit
* edit
, off_t increment
)
2673 for (; increment
< 0 && edit
->buffer
.curs1
!= 0; increment
++)
2677 edit_push_undo_action (edit
, CURS_RIGHT
);
2679 c
= edit_buffer_get_previous_byte (&edit
->buffer
);
2680 edit_buffer_insert_ahead (&edit
->buffer
, c
);
2681 c
= edit_buffer_backspace (&edit
->buffer
);
2685 edit
->force
|= REDRAW_LINE_BELOW
;
2691 for (; increment
> 0 && edit
->buffer
.curs2
!= 0; increment
--)
2695 edit_push_undo_action (edit
, CURS_LEFT
);
2697 c
= edit_buffer_get_current_byte (&edit
->buffer
);
2698 edit_buffer_insert (&edit
->buffer
, c
);
2699 c
= edit_buffer_delete (&edit
->buffer
);
2703 edit
->force
|= REDRAW_LINE_ABOVE
;
2709 /* These functions return positions relative to lines */
2711 /* --------------------------------------------------------------------------------------------- */
2712 /** returns index of last char on line + 1 */
2715 edit_eol (const WEdit
* edit
, off_t current
)
2717 if (current
>= edit
->last_byte
)
2718 return edit
->last_byte
;
2720 for (; edit_buffer_get_byte (&edit
->buffer
, current
) != '\n'; current
++)
2726 /* --------------------------------------------------------------------------------------------- */
2727 /** returns index of first char on line */
2730 edit_bol (const WEdit
* edit
, off_t current
)
2735 for (; edit_buffer_get_byte (&edit
->buffer
, current
- 1) != '\n'; current
--)
2741 /* --------------------------------------------------------------------------------------------- */
2744 edit_count_lines (const WEdit
* edit
, off_t current
, off_t upto
)
2748 if (upto
> edit
->last_byte
)
2749 upto
= edit
->last_byte
;
2752 while (current
< upto
)
2753 if (edit_buffer_get_byte (&edit
->buffer
, current
++) == '\n')
2758 /* --------------------------------------------------------------------------------------------- */
2759 /* If lines is zero this returns the count of lines from current to upto. */
2760 /* If upto is zero returns index of lines forward current. */
2763 edit_move_forward (const WEdit
* edit
, off_t current
, long lines
, off_t upto
)
2767 return (off_t
) edit_count_lines (edit
, current
, upto
);
2774 while (lines
-- != 0)
2776 next
= edit_eol (edit
, current
) + 1;
2777 if (next
> edit
->last_byte
)
2786 /* --------------------------------------------------------------------------------------------- */
2787 /** Returns offset of 'lines' lines up from current */
2790 edit_move_backward (const WEdit
* edit
, off_t current
, long lines
)
2794 current
= edit_bol (edit
, current
);
2795 while (lines
-- != 0 && current
!= 0)
2796 current
= edit_bol (edit
, current
- 1);
2800 /* --------------------------------------------------------------------------------------------- */
2801 /* If cols is zero this returns the count of columns from current to upto. */
2802 /* If upto is zero returns index of cols across from current. */
2805 edit_move_forward3 (const WEdit
* edit
, off_t current
, long cols
, off_t upto
)
2816 q
= edit
->last_byte
+ 2;
2818 for (col
= 0, p
= current
; p
< q
; p
++)
2830 orig_c
= c
= edit_buffer_get_byte (&edit
->buffer
, p
);
2838 utf_ch
= edit_buffer_get_utf (&edit
->buffer
, p
, &cw
);
2839 if (mc_global
.utf8_display
)
2843 if (g_unichar_iswide (utf_ch
))
2846 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
2850 c
= convert_to_display_c (c
);
2854 return (upto
!= 0 ? (off_t
) col
: p
);
2856 col
+= TAB_SIZE
- col
% TAB_SIZE
;
2857 else if ((c
< 32 || c
== 127) && (orig_c
== c
2859 || (!mc_global
.utf8_display
&& !edit
->utf8
)
2862 /* '\r' is shown as ^M, so we must advance 2 characters */
2863 /* Caret notation for control characters */
2871 /* --------------------------------------------------------------------------------------------- */
2872 /** returns the current column position of the cursor */
2875 edit_get_col (const WEdit
* edit
)
2877 return (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->buffer
.curs1
), 0, edit
->buffer
.curs1
);
2880 /* --------------------------------------------------------------------------------------------- */
2881 /* Scrolling functions */
2882 /* --------------------------------------------------------------------------------------------- */
2885 edit_update_curs_row (WEdit
* edit
)
2887 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
2890 /* --------------------------------------------------------------------------------------------- */
2893 edit_update_curs_col (WEdit
* edit
)
2895 edit
->curs_col
= (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->buffer
.curs1
), 0, edit
->buffer
.curs1
);
2898 /* --------------------------------------------------------------------------------------------- */
2901 edit_get_curs_col (const WEdit
* edit
)
2903 return edit
->curs_col
;
2906 /* --------------------------------------------------------------------------------------------- */
2907 /** moves the display start position up by i lines */
2910 edit_scroll_upward (WEdit
* edit
, long i
)
2912 long lines_above
= edit
->start_line
;
2914 if (i
> lines_above
)
2918 edit
->start_line
-= i
;
2919 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
2920 edit
->force
|= REDRAW_PAGE
;
2921 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2923 edit_update_curs_row (edit
);
2927 /* --------------------------------------------------------------------------------------------- */
2930 edit_scroll_downward (WEdit
* edit
, long i
)
2934 lines_below
= edit
->total_lines
- edit
->start_line
- (WIDGET (edit
)->lines
- 1);
2935 if (lines_below
> 0)
2937 if (i
> lines_below
)
2939 edit
->start_line
+= i
;
2940 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
2941 edit
->force
|= REDRAW_PAGE
;
2942 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2944 edit_update_curs_row (edit
);
2947 /* --------------------------------------------------------------------------------------------- */
2950 edit_scroll_right (WEdit
* edit
, long i
)
2952 edit
->force
|= REDRAW_PAGE
;
2953 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2954 edit
->start_col
-= i
;
2957 /* --------------------------------------------------------------------------------------------- */
2960 edit_scroll_left (WEdit
* edit
, long i
)
2962 if (edit
->start_col
)
2964 edit
->start_col
+= i
;
2965 if (edit
->start_col
> 0)
2966 edit
->start_col
= 0;
2967 edit
->force
|= REDRAW_PAGE
;
2968 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2972 /* --------------------------------------------------------------------------------------------- */
2973 /* high level cursor movement commands */
2974 /* --------------------------------------------------------------------------------------------- */
2977 edit_move_to_prev_col (WEdit
* edit
, off_t p
)
2979 long prev
= edit
->prev_col
;
2980 long over
= edit
->over_col
;
2982 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->buffer
.curs1
);
2984 if (option_cursor_beyond_eol
)
2988 line_len
= (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->buffer
.curs1
), 0,
2989 edit_eol (edit
, edit
->buffer
.curs1
));
2990 if (line_len
< prev
+ edit
->over_col
)
2992 edit
->over_col
= prev
+ over
- line_len
;
2993 edit
->prev_col
= line_len
;
2994 edit
->curs_col
= line_len
;
2998 edit
->curs_col
= prev
+ over
;
2999 edit
->prev_col
= edit
->curs_col
;
3006 if (option_fake_half_tabs
&& is_in_indent (edit
))
3008 long fake_half_tabs
;
3010 edit_update_curs_col (edit
);
3012 fake_half_tabs
= HALF_TAB_SIZE
* space_width
;
3013 if (fake_half_tabs
!= 0 && edit
->curs_col
% fake_half_tabs
!= 0)
3018 edit
->curs_col
-= (edit
->curs_col
% fake_half_tabs
);
3019 p
= edit_bol (edit
, edit
->buffer
.curs1
);
3020 edit_cursor_move (edit
,
3021 edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->buffer
.curs1
);
3022 if (!left_of_four_spaces (edit
))
3023 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->buffer
.curs1
);
3029 /* --------------------------------------------------------------------------------------------- */
3030 /** check whether line in editor is blank or not
3032 * @param edit editor object
3033 * @param line number of line
3035 * @return TRUE if line in blank, FALSE otherwise
3039 edit_line_is_blank (WEdit
* edit
, long line
)
3041 return is_blank (edit
, edit_find_line (edit
, line
));
3044 /* --------------------------------------------------------------------------------------------- */
3045 /** move cursor to line 'line' */
3048 edit_move_to_line (WEdit
* e
, long line
)
3050 if (line
< e
->curs_line
)
3051 edit_move_up (e
, e
->curs_line
- line
, 0);
3053 edit_move_down (e
, line
- e
->curs_line
, 0);
3054 edit_scroll_screen_over_cursor (e
);
3057 /* --------------------------------------------------------------------------------------------- */
3058 /** scroll window so that first visible line is 'line' */
3061 edit_move_display (WEdit
* e
, long line
)
3063 if (line
< e
->start_line
)
3064 edit_scroll_upward (e
, e
->start_line
- line
);
3066 edit_scroll_downward (e
, line
- e
->start_line
);
3069 /* --------------------------------------------------------------------------------------------- */
3070 /** save markers onto undo stack */
3073 edit_push_markers (WEdit
* edit
)
3075 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3076 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3077 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3080 /* --------------------------------------------------------------------------------------------- */
3083 edit_set_markers (WEdit
* edit
, off_t m1
, off_t m2
, long c1
, long c2
)
3092 /* --------------------------------------------------------------------------------------------- */
3093 /** highlight marker toggle */
3096 edit_mark_cmd (WEdit
* edit
, gboolean unmark
)
3098 edit_push_markers (edit
);
3101 edit_set_markers (edit
, 0, 0, 0, 0);
3102 edit
->force
|= REDRAW_PAGE
;
3104 else if (edit
->mark2
>= 0)
3106 edit
->end_mark_curs
= -1;
3107 edit_set_markers (edit
, edit
->buffer
.curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3108 edit
->curs_col
+ edit
->over_col
);
3109 edit
->force
|= REDRAW_PAGE
;
3113 edit
->end_mark_curs
= edit
->buffer
.curs1
;
3114 edit_set_markers (edit
, edit
->mark1
, edit
->buffer
.curs1
, edit
->column1
,
3115 edit
->curs_col
+ edit
->over_col
);
3119 /* --------------------------------------------------------------------------------------------- */
3120 /** highlight the word under cursor */
3123 edit_mark_current_word_cmd (WEdit
* edit
)
3127 for (pos
= edit
->buffer
.curs1
; pos
!= 0; pos
--)
3131 c1
= edit_buffer_get_byte (&edit
->buffer
, pos
);
3132 c2
= edit_buffer_get_byte (&edit
->buffer
, pos
- 1);
3133 if (!isspace (c1
) && isspace (c2
))
3135 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3140 for (; pos
< edit
->last_byte
; pos
++)
3144 c1
= edit_buffer_get_byte (&edit
->buffer
, pos
);
3145 c2
= edit_buffer_get_byte (&edit
->buffer
, pos
+ 1);
3146 if (!isspace (c1
) && isspace (c2
))
3148 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3151 edit
->mark2
= min (pos
+ 1, edit
->last_byte
);
3153 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3156 /* --------------------------------------------------------------------------------------------- */
3159 edit_mark_current_line_cmd (WEdit
* edit
)
3161 long pos
= edit
->buffer
.curs1
;
3163 edit
->mark1
= edit_bol (edit
, pos
);
3164 edit
->mark2
= edit_eol (edit
, pos
);
3166 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3169 /* --------------------------------------------------------------------------------------------- */
3172 edit_delete_line (WEdit
* edit
)
3175 * Delete right part of the line.
3176 * Note that edit_buffer_get_byte() returns '\n' when byte position is
3179 while (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
3180 (void) edit_delete (edit
, TRUE
);
3184 * Note that edit_delete() will not corrupt anything if called while
3185 * cursor position is EOF.
3187 (void) edit_delete (edit
, TRUE
);
3190 * Delete left part of the line.
3191 * Note, that edit_buffer_get_byte() returns '\n' when byte position is < 0.
3193 while (edit_buffer_get_previous_byte (&edit
->buffer
) != '\n')
3194 (void) edit_backspace (edit
, TRUE
);
3197 /* --------------------------------------------------------------------------------------------- */
3200 edit_push_key_press (WEdit
* edit
)
3202 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3203 if (edit
->mark2
== -1)
3205 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3206 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3210 /* --------------------------------------------------------------------------------------------- */
3213 edit_find_bracket (WEdit
* edit
)
3215 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3216 if (edit
->last_bracket
!= edit
->bracket
)
3217 edit
->force
|= REDRAW_PAGE
;
3218 edit
->last_bracket
= edit
->bracket
;
3221 /* --------------------------------------------------------------------------------------------- */
3223 * This executes a command as though the user initiated it through a key
3224 * press. Callback with MSG_KEY as a message calls this after
3225 * translating the key press. This function can be used to pass any
3226 * command to the editor. Note that the screen wouldn't update
3227 * automatically. Either of command or char_for_insertion must be
3228 * passed as -1. Commands are executed, and char_for_insertion is
3229 * inserted at the cursor.
3233 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3235 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3237 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3240 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3243 if (macro_index
!= -1)
3245 edit
->force
|= REDRAW_COMPLETELY
;
3246 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3248 edit_store_macro_cmd (edit
);
3252 if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3254 edit_repeat_macro_cmd (edit
);
3260 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3262 record_macro_buf
[macro_index
].action
= command
;
3263 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3265 /* record the beginning of a set of editing actions initiated by a key press */
3266 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3267 edit_push_key_press (edit
);
3269 edit_execute_cmd (edit
, command
, char_for_insertion
);
3270 if (edit
->column_highlight
)
3271 edit
->force
|= REDRAW_PAGE
;
3274 /* --------------------------------------------------------------------------------------------- */
3276 This executes a command at a lower level than macro recording.
3277 It also does not push a key_press onto the undo stack. This means
3278 that if it is called many times, a single undo command will undo
3279 all of them. It also does not check for the Undo command.
3282 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3284 Widget
*w
= WIDGET (edit
);
3286 if (command
== CK_WindowFullscreen
)
3288 edit_toggle_fullscreen (edit
);
3292 /* handle window state */
3293 if (edit_handle_move_resize (edit
, command
))
3296 edit
->force
|= REDRAW_LINE
;
3298 /* The next key press will unhighlight the found string, so update
3300 if (edit
->found_len
|| edit
->column_highlight
)
3301 edit
->force
|= REDRAW_PAGE
;
3305 /* a mark command with shift-arrow */
3308 case CK_MarkToWordBegin
:
3309 case CK_MarkToWordEnd
:
3315 case CK_MarkPageDown
:
3316 case CK_MarkToFileBegin
:
3317 case CK_MarkToFileEnd
:
3318 case CK_MarkToPageBegin
:
3319 case CK_MarkToPageEnd
:
3320 case CK_MarkScrollUp
:
3321 case CK_MarkScrollDown
:
3322 case CK_MarkParagraphUp
:
3323 case CK_MarkParagraphDown
:
3324 /* a mark command with alt-arrow */
3325 case CK_MarkColumnPageUp
:
3326 case CK_MarkColumnPageDown
:
3327 case CK_MarkColumnLeft
:
3328 case CK_MarkColumnRight
:
3329 case CK_MarkColumnUp
:
3330 case CK_MarkColumnDown
:
3331 case CK_MarkColumnScrollUp
:
3332 case CK_MarkColumnScrollDown
:
3333 case CK_MarkColumnParagraphUp
:
3334 case CK_MarkColumnParagraphDown
:
3335 edit
->column_highlight
= 0;
3336 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3338 edit_mark_cmd (edit
, TRUE
); /* clear */
3339 edit_mark_cmd (edit
, FALSE
); /* marking on */
3341 edit
->highlight
= 1;
3344 /* any other command */
3346 if (edit
->highlight
)
3347 edit_mark_cmd (edit
, FALSE
); /* clear */
3348 edit
->highlight
= 0;
3351 /* first check for undo */
3352 if (command
== CK_Undo
)
3354 edit
->redo_stack_reset
= 0;
3355 edit_group_undo (edit
);
3356 edit
->found_len
= 0;
3357 edit
->prev_col
= edit_get_col (edit
);
3358 edit
->search_start
= edit
->buffer
.curs1
;
3361 /* check for redo */
3362 if (command
== CK_Redo
)
3364 edit
->redo_stack_reset
= 0;
3365 edit_do_redo (edit
);
3366 edit
->found_len
= 0;
3367 edit
->prev_col
= edit_get_col (edit
);
3368 edit
->search_start
= edit
->buffer
.curs1
;
3372 edit
->redo_stack_reset
= 1;
3374 /* An ordinary key press */
3375 if (char_for_insertion
>= 0)
3377 /* if non persistent selection and text selected */
3378 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3379 edit_block_delete_cmd (edit
);
3381 if (edit
->overwrite
)
3383 /* remove char only one time, after input first byte, multibyte chars */
3385 if (!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3387 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
3389 edit_delete (edit
, FALSE
);
3391 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3392 edit_insert_over (edit
);
3394 if (char_for_insertion
> 255 && !mc_global
.utf8_display
)
3396 unsigned char str
[6 + 1];
3400 res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3410 while (str
[i
] != 0 && i
<= 6)
3412 char_for_insertion
= str
[i
];
3413 edit_insert (edit
, char_for_insertion
);
3419 edit_insert (edit
, char_for_insertion
);
3421 if (option_auto_para_formatting
)
3423 format_paragraph (edit
, 0);
3424 edit
->force
|= REDRAW_PAGE
;
3427 check_and_wrap_line (edit
);
3428 edit
->found_len
= 0;
3429 edit
->prev_col
= edit_get_col (edit
);
3430 edit
->search_start
= edit
->buffer
.curs1
;
3431 edit_find_bracket (edit
);
3437 case CK_TopOnScreen
:
3438 case CK_BottomOnScreen
:
3451 if (!option_persistent_selections
&& edit
->mark2
>= 0)
3453 if (edit
->column_highlight
)
3454 edit_push_undo_action (edit
, COLUMN_ON
);
3455 edit
->column_highlight
= 0;
3456 edit_mark_cmd (edit
, TRUE
);
3462 case CK_TopOnScreen
:
3463 case CK_BottomOnScreen
:
3464 case CK_MarkToPageBegin
:
3465 case CK_MarkToPageEnd
:
3470 case CK_MarkToWordBegin
:
3471 case CK_MarkToWordEnd
:
3474 case CK_MarkColumnUp
:
3475 case CK_MarkColumnDown
:
3476 if (edit
->mark2
== -1)
3477 break; /*marking is following the cursor: may need to highlight a whole line */
3482 edit
->force
|= REDRAW_CHAR_ONLY
;
3485 /* basic cursor key commands */
3489 /* if non persistent selection and text selected */
3490 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3491 edit_block_delete_cmd (edit
);
3492 else if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3494 else if (option_backspace_through_tabs
&& is_in_indent (edit
))
3496 while (edit_buffer_get_previous_byte (&edit
->buffer
) != '\n'
3497 && edit
->buffer
.curs1
> 0)
3498 edit_backspace (edit
, TRUE
);
3500 else if (option_fake_half_tabs
&& is_in_indent (edit
) && right_of_four_spaces (edit
))
3504 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3505 edit_backspace (edit
, TRUE
);
3508 edit_backspace (edit
, FALSE
);
3511 /* if non persistent selection and text selected */
3512 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3513 edit_block_delete_cmd (edit
);
3516 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3517 edit_insert_over (edit
);
3519 if (option_fake_half_tabs
&& is_in_indent (edit
) && left_of_four_spaces (edit
))
3523 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3524 edit_delete (edit
, TRUE
);
3527 edit_delete (edit
, FALSE
);
3530 case CK_DeleteToWordBegin
:
3532 edit_left_delete_word (edit
);
3534 case CK_DeleteToWordEnd
:
3535 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3536 edit_insert_over (edit
);
3538 edit_right_delete_word (edit
);
3541 edit_delete_line (edit
);
3543 case CK_DeleteToHome
:
3544 edit_delete_to_line_begin (edit
);
3546 case CK_DeleteToEnd
:
3547 edit_delete_to_line_end (edit
);
3551 if (option_auto_para_formatting
)
3553 edit_double_newline (edit
);
3554 if (option_return_does_auto_indent
)
3555 edit_auto_indent (edit
);
3556 format_paragraph (edit
, 0);
3560 edit_insert (edit
, '\n');
3561 if (option_return_does_auto_indent
)
3562 edit_auto_indent (edit
);
3566 edit_insert (edit
, '\n');
3569 case CK_MarkColumnPageUp
:
3570 edit
->column_highlight
= 1;
3573 edit_move_up (edit
, w
->lines
- 1, 1);
3575 case CK_MarkColumnPageDown
:
3576 edit
->column_highlight
= 1;
3578 case CK_MarkPageDown
:
3579 edit_move_down (edit
, w
->lines
- 1, 1);
3581 case CK_MarkColumnLeft
:
3582 edit
->column_highlight
= 1;
3585 if (option_fake_half_tabs
&& is_in_indent (edit
) && right_of_four_spaces (edit
))
3587 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3590 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3591 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3594 edit_left_char_move_cmd (edit
);
3596 case CK_MarkColumnRight
:
3597 edit
->column_highlight
= 1;
3600 if (option_fake_half_tabs
&& is_in_indent (edit
) && left_of_four_spaces (edit
))
3602 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3603 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3606 edit_right_char_move_cmd (edit
);
3608 case CK_TopOnScreen
:
3609 case CK_MarkToPageBegin
:
3610 edit_begin_page (edit
);
3612 case CK_BottomOnScreen
:
3613 case CK_MarkToPageEnd
:
3614 edit_end_page (edit
);
3617 case CK_MarkToWordBegin
:
3619 edit_left_word_move_cmd (edit
);
3622 case CK_MarkToWordEnd
:
3624 edit_right_word_move_cmd (edit
);
3626 case CK_MarkColumnUp
:
3627 edit
->column_highlight
= 1;
3630 edit_move_up (edit
, 1, 0);
3632 case CK_MarkColumnDown
:
3633 edit
->column_highlight
= 1;
3636 edit_move_down (edit
, 1, 0);
3638 case CK_MarkColumnParagraphUp
:
3639 edit
->column_highlight
= 1;
3640 case CK_ParagraphUp
:
3641 case CK_MarkParagraphUp
:
3642 edit_move_up_paragraph (edit
, 0);
3644 case CK_MarkColumnParagraphDown
:
3645 edit
->column_highlight
= 1;
3646 case CK_ParagraphDown
:
3647 case CK_MarkParagraphDown
:
3648 edit_move_down_paragraph (edit
, 0);
3650 case CK_MarkColumnScrollUp
:
3651 edit
->column_highlight
= 1;
3653 case CK_MarkScrollUp
:
3654 edit_move_up (edit
, 1, 1);
3656 case CK_MarkColumnScrollDown
:
3657 edit
->column_highlight
= 1;
3659 case CK_MarkScrollDown
:
3660 edit_move_down (edit
, 1, 1);
3664 edit_cursor_to_bol (edit
);
3668 edit_cursor_to_eol (edit
);
3671 /* if text marked shift block */
3672 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3674 if (edit
->mark2
< 0)
3675 edit_mark_cmd (edit
, FALSE
);
3676 edit_move_block_to_right (edit
);
3680 if (option_cursor_beyond_eol
)
3681 edit_insert_over (edit
);
3682 edit_tab_cmd (edit
);
3683 if (option_auto_para_formatting
)
3685 format_paragraph (edit
, 0);
3686 edit
->force
|= REDRAW_PAGE
;
3689 check_and_wrap_line (edit
);
3693 case CK_InsertOverwrite
:
3694 edit
->overwrite
= !edit
->overwrite
;
3698 if (edit
->mark2
>= 0)
3700 if (edit
->column_highlight
)
3701 edit_push_undo_action (edit
, COLUMN_ON
);
3702 edit
->column_highlight
= 0;
3704 edit_mark_cmd (edit
, FALSE
);
3707 if (!edit
->column_highlight
)
3708 edit_push_undo_action (edit
, COLUMN_OFF
);
3709 edit
->column_highlight
= 1;
3710 edit_mark_cmd (edit
, FALSE
);
3713 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3714 edit
->force
|= REDRAW_PAGE
;
3717 if (edit
->column_highlight
)
3718 edit_push_undo_action (edit
, COLUMN_ON
);
3719 edit
->column_highlight
= 0;
3720 edit_mark_cmd (edit
, TRUE
);
3723 if (edit
->column_highlight
)
3724 edit_push_undo_action (edit
, COLUMN_ON
);
3725 edit
->column_highlight
= 0;
3726 edit_mark_current_word_cmd (edit
);
3729 if (edit
->column_highlight
)
3730 edit_push_undo_action (edit
, COLUMN_ON
);
3731 edit
->column_highlight
= 0;
3732 edit_mark_current_line_cmd (edit
);
3736 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
3737 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
3738 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3740 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3742 case CK_BookmarkFlush
:
3743 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3744 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3745 edit
->force
|= REDRAW_PAGE
;
3747 case CK_BookmarkNext
:
3748 if (edit
->book_mark
!= NULL
)
3750 edit_book_mark_t
*p
;
3752 p
= book_mark_find (edit
, edit
->curs_line
);
3753 if (p
->next
!= NULL
)
3756 if (p
->line
>= edit
->start_line
+ w
->lines
|| p
->line
< edit
->start_line
)
3757 edit_move_display (edit
, p
->line
- w
->lines
/ 2);
3758 edit_move_to_line (edit
, p
->line
);
3762 case CK_BookmarkPrev
:
3763 if (edit
->book_mark
!= NULL
)
3765 edit_book_mark_t
*p
;
3767 p
= book_mark_find (edit
, edit
->curs_line
);
3768 while (p
->line
== edit
->curs_line
)
3769 if (p
->prev
!= NULL
)
3773 if (p
->line
>= edit
->start_line
+ w
->lines
|| p
->line
< edit
->start_line
)
3774 edit_move_display (edit
, p
->line
- w
->lines
/ 2);
3775 edit_move_to_line (edit
, p
->line
);
3781 case CK_MarkToFileBegin
:
3782 edit_move_to_top (edit
);
3785 case CK_MarkToFileEnd
:
3786 edit_move_to_bottom (edit
);
3790 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3791 edit_insert_over (edit
);
3792 edit_block_copy_cmd (edit
);
3795 edit_block_delete_cmd (edit
);
3798 edit_block_move_cmd (edit
);
3801 case CK_BlockShiftLeft
:
3802 if (edit
->mark1
!= edit
->mark2
)
3803 edit_move_block_to_left (edit
);
3805 case CK_BlockShiftRight
:
3806 if (edit
->mark1
!= edit
->mark2
)
3807 edit_move_block_to_right (edit
);
3810 edit_copy_to_X_buf_cmd (edit
);
3813 edit_cut_to_X_buf_cmd (edit
);
3816 /* if non persistent selection and text selected */
3817 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3818 edit_block_delete_cmd (edit
);
3819 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3820 edit_insert_over (edit
);
3821 edit_paste_from_X_buf_cmd (edit
);
3822 if (!option_persistent_selections
&& edit
->mark2
>= 0)
3824 if (edit
->column_highlight
)
3825 edit_push_undo_action (edit
, COLUMN_ON
);
3826 edit
->column_highlight
= 0;
3827 edit_mark_cmd (edit
, TRUE
);
3831 edit_paste_from_history (edit
);
3835 edit_save_as_cmd (edit
);
3838 edit_save_confirm_cmd (edit
);
3841 edit_save_block_cmd (edit
);
3844 edit_insert_file_cmd (edit
);
3848 edit_load_back_cmd (edit
);
3851 edit_load_forward_cmd (edit
);
3854 case CK_SyntaxChoose
:
3855 edit_syntax_dialog (edit
);
3859 edit_search_cmd (edit
, FALSE
);
3861 case CK_SearchContinue
:
3862 edit_search_cmd (edit
, TRUE
);
3865 edit_replace_cmd (edit
, 0);
3867 case CK_ReplaceContinue
:
3868 edit_replace_cmd (edit
, 1);
3871 /* if text marked shift block */
3872 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3873 edit_move_block_to_left (edit
);
3875 edit_complete_word_cmd (edit
);
3878 edit_get_match_keyword_cmd (edit
);
3882 case CK_SpellCheckCurrentWord
:
3883 edit_suggest_current_word (edit
);
3886 edit_spellcheck_file (edit
);
3888 case CK_SpellCheckSelectLang
:
3889 edit_set_spell_lang ();
3896 /* fool gcc to prevent a Y2K warning */
3897 char time_format
[] = "_c";
3898 time_format
[0] = '%';
3900 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
3901 edit_print_string (edit
, s
);
3902 edit
->force
|= REDRAW_PAGE
;
3906 edit_goto_cmd (edit
);
3908 case CK_ParagraphFormat
:
3909 format_paragraph (edit
, 1);
3910 edit
->force
|= REDRAW_PAGE
;
3912 case CK_MacroDelete
:
3913 edit_delete_macro_cmd (edit
);
3915 case CK_MatchBracket
:
3916 edit_goto_matching_bracket (edit
);
3919 user_menu (edit
, NULL
, -1);
3922 edit_sort_cmd (edit
);
3924 case CK_ExternalCommand
:
3925 edit_ext_cmd (edit
);
3928 edit_mail_dialog (edit
);
3931 case CK_SelectCodepage
:
3932 edit_select_codepage_cmd (edit
);
3935 case CK_InsertLiteral
:
3936 edit_insert_literal_cmd (edit
);
3938 case CK_MacroStartStopRecord
:
3939 edit_begin_end_macro_cmd (edit
);
3941 case CK_RepeatStartStopRecord
:
3942 edit_begin_end_repeat_cmd (edit
);
3944 case CK_ExtendedKeyMap
:
3945 edit
->extmod
= TRUE
;
3952 if ((command
/ CK_PipeBlock (0)) == 1)
3953 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
3955 /* keys which must set the col position, and the search vars */
3959 case CK_SearchContinue
:
3961 case CK_ReplaceContinue
:
3963 edit
->prev_col
= edit_get_col (edit
);
3967 case CK_MarkColumnUp
:
3970 case CK_MarkColumnDown
:
3973 case CK_MarkColumnPageUp
:
3975 case CK_MarkPageDown
:
3976 case CK_MarkColumnPageDown
:
3978 case CK_MarkToFileBegin
:
3980 case CK_MarkToFileEnd
:
3981 case CK_ParagraphUp
:
3982 case CK_MarkParagraphUp
:
3983 case CK_MarkColumnParagraphUp
:
3984 case CK_ParagraphDown
:
3985 case CK_MarkParagraphDown
:
3986 case CK_MarkColumnParagraphDown
:
3988 case CK_MarkScrollUp
:
3989 case CK_MarkColumnScrollUp
:
3991 case CK_MarkScrollDown
:
3992 case CK_MarkColumnScrollDown
:
3993 edit
->search_start
= edit
->buffer
.curs1
;
3994 edit
->found_len
= 0;
3997 edit
->found_len
= 0;
3998 edit
->prev_col
= edit_get_col (edit
);
3999 edit
->search_start
= edit
->buffer
.curs1
;
4001 edit_find_bracket (edit
);
4003 if (option_auto_para_formatting
)
4009 case CK_DeleteToWordBegin
:
4010 case CK_DeleteToWordEnd
:
4011 case CK_DeleteToHome
:
4012 case CK_DeleteToEnd
:
4013 format_paragraph (edit
, 0);
4014 edit
->force
|= REDRAW_PAGE
;
4019 /* --------------------------------------------------------------------------------------------- */
4022 edit_stack_init (void)
4024 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4026 edit_history_moveto
[edit_stack_iterator
].filename_vpath
= NULL
;
4027 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4030 edit_stack_iterator
= 0;
4033 /* --------------------------------------------------------------------------------------------- */
4036 edit_stack_free (void)
4038 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4039 vfs_path_free (edit_history_moveto
[edit_stack_iterator
].filename_vpath
);
4042 /* --------------------------------------------------------------------------------------------- */
4046 edit_move_up (WEdit
* edit
, long i
, gboolean do_scroll
)
4048 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4051 /* --------------------------------------------------------------------------------------------- */
4055 edit_move_down (WEdit
* edit
, long i
, gboolean do_scroll
)
4057 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4060 /* --------------------------------------------------------------------------------------------- */