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 /*** file scope functions ************************************************************************/
139 /* --------------------------------------------------------------------------------------------- */
143 * here's a quick sketch of the layout: (don't run this through indent.)
145 * (b1 is buffers1 and b2 is buffers2)
148 * \0\0\0\0\0m e _ f i l e . \nf i n . \n|T h i s _ i s _ s o\0\0\0\0\0\0\0\0\0
149 * ______________________________________|______________________________________
151 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
152 * |-> |-> |-> |-> |-> |-> |
154 * _<------------------------->|<----------------->_
155 * WEdit->curs2 | WEdit->curs1
160 * file end|||file beginning
169 /* --------------------------------------------------------------------------------------------- */
171 * Initialize the buffers for an empty files.
175 edit_init_buffers (WEdit
* edit
)
179 for (j
= 0; j
<= MAXBUFF
; j
++)
181 edit
->buffers1
[j
] = NULL
;
182 edit
->buffers2
[j
] = NULL
;
187 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
190 /* --------------------------------------------------------------------------------------------- */
193 * Load file OR text into buffers. Set cursor to the beginning of file.
195 * @return FALSE on error.
199 edit_load_file_fast (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
203 gboolean ret
= FALSE
;
205 edit
->curs2
= edit
->last_byte
;
206 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
208 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
214 g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath
));
215 edit_error_dialog (_("Error"), errmsg
);
220 if (!edit
->buffers2
[buf2
])
221 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
226 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
227 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
230 for (buf
= buf2
- 1; buf
>= 0; buf
--)
232 /* edit->buffers2[0] is already allocated */
233 if (!edit
->buffers2
[buf
])
234 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
235 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
246 errmsg
= g_strdup_printf (_("Error reading %s"), vfs_path_as_str (filename_vpath
));
247 edit_error_dialog (_("Error"), errmsg
);
254 /* --------------------------------------------------------------------------------------------- */
255 /** Return index of the filter or -1 is there is no appropriate filter */
258 edit_find_filter (const vfs_path_t
* filename_vpath
)
262 if (filename_vpath
== NULL
)
265 l
= strlen (vfs_path_as_str (filename_vpath
));
266 for (i
= 0; i
< G_N_ELEMENTS (all_filters
); i
++)
268 e
= strlen (all_filters
[i
].extension
);
270 if (!strcmp (all_filters
[i
].extension
, vfs_path_as_str (filename_vpath
) + l
- e
))
276 /* --------------------------------------------------------------------------------------------- */
279 edit_get_filter (const vfs_path_t
* filename_vpath
)
282 char *p
, *quoted_name
;
284 i
= edit_find_filter (filename_vpath
);
288 quoted_name
= name_quote (vfs_path_as_str (filename_vpath
), 0);
289 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
290 g_free (quoted_name
);
294 /* --------------------------------------------------------------------------------------------- */
297 edit_insert_stream (WEdit
* edit
, FILE * f
)
301 while ((c
= fgetc (f
)) >= 0)
303 edit_insert (edit
, c
);
309 /* --------------------------------------------------------------------------------------------- */
311 * Open file and create it if necessary.
313 * @param edit editor object
314 * @param filename_vpath file name
315 * @param st buffer for store stat info
316 * @return TRUE for success, FALSE for error.
320 check_file_access (WEdit
* edit
, const vfs_path_t
* filename_vpath
, struct stat
*st
)
323 gchar
*errmsg
= NULL
;
325 /* Try opening an existing file */
326 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
330 * Try creating the file. O_EXCL prevents following broken links
331 * and opening existing files.
333 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
337 g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath
));
341 /* New file, delete it if it's not modified or saved */
342 edit
->delete_file
= 1;
345 /* Check what we have opened */
346 if (mc_fstat (file
, st
) < 0)
349 g_strdup_printf (_("Cannot get size/permissions for %s"),
350 vfs_path_as_str (filename_vpath
));
354 /* We want to open regular files only */
355 if (!S_ISREG (st
->st_mode
))
358 g_strdup_printf (_("\"%s\" is not a regular file"), vfs_path_as_str (filename_vpath
));
363 * Don't delete non-empty files.
364 * O_EXCL should prevent it, but let's be on the safe side.
367 edit
->delete_file
= 0;
369 if (st
->st_size
>= SIZE_LIMIT
)
370 errmsg
= g_strdup_printf (_("File \"%s\" is too large"), vfs_path_as_str (filename_vpath
));
373 (void) mc_close (file
);
377 edit_error_dialog (_("Error"), errmsg
);
384 /* --------------------------------------------------------------------------------------------- */
387 * Open the file and load it into the buffers, either directly or using
388 * a filter. Return TRUE on success, FALSE on error.
390 * Fast loading (edit_load_file_fast) is used when the file size is
391 * known. In this case the data is read into the buffers by blocks.
392 * If the file size is not known, the data is loaded byte by byte in
395 * @param edit editor object
396 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
399 edit_load_file (WEdit
* edit
)
401 gboolean fast_load
= TRUE
;
403 /* Cannot do fast load if a filter is used */
404 if (edit_find_filter (edit
->filename_vpath
) >= 0)
408 * FIXME: line end translation should disable fast loading as well
409 * Consider doing fseek() to the end and ftell() for the real size.
411 if (edit
->filename_vpath
!= NULL
)
414 * VFS may report file size incorrectly, and slow load is not a big
415 * deal considering overhead in VFS.
417 if (!vfs_file_is_local (edit
->filename_vpath
))
420 /* If we are dealing with a real file, check that it exists */
421 if (!check_file_access (edit
, edit
->filename_vpath
, &edit
->stat1
))
429 /* nothing to load */
433 edit_init_buffers (edit
);
437 edit
->last_byte
= edit
->stat1
.st_size
;
438 edit_load_file_fast (edit
, edit
->filename_vpath
);
439 /* If fast load was used, the number of lines wasn't calculated */
440 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
445 if (edit
->filename_vpath
!= NULL
446 && *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) != '\0')
448 edit
->undo_stack_disable
= 1;
449 if (edit_insert_file (edit
, edit
->filename_vpath
) < 0)
454 edit
->undo_stack_disable
= 0;
461 /* --------------------------------------------------------------------------------------------- */
462 /** Restore saved cursor position in the file */
465 edit_load_position (WEdit
* edit
)
470 if (edit
->filename_vpath
== NULL
471 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
474 load_file_position (edit
->filename_vpath
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
478 edit_move_to_line (edit
, line
- 1);
479 edit
->prev_col
= column
;
483 edit_cursor_move (edit
, offset
);
484 line
= edit
->curs_line
;
485 edit
->search_start
= edit
->curs1
;
488 book_mark_restore (edit
, BOOK_MARK_COLOR
);
490 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
491 edit_move_display (edit
, line
- (WIDGET (edit
)->lines
/ 2));
494 /* --------------------------------------------------------------------------------------------- */
495 /** Save cursor position in the file */
498 edit_save_position (WEdit
* edit
)
500 if (edit
->filename_vpath
== NULL
501 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
504 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
505 save_file_position (edit
->filename_vpath
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
,
506 edit
->serialized_bookmarks
);
507 edit
->serialized_bookmarks
= NULL
;
510 /* --------------------------------------------------------------------------------------------- */
511 /** Clean the WEdit stricture except the widget part */
514 edit_purge_widget (WEdit
* edit
)
516 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
517 char *start
= (char *) edit
+ sizeof (Widget
);
518 memset (start
, 0, len
);
521 /* --------------------------------------------------------------------------------------------- */
524 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
525 then the file should be as it was when he loaded up. Then set edit->modified to 0.
529 edit_pop_undo_action (WEdit
* edit
)
532 unsigned long sp
= edit
->undo_stack_pointer
;
534 if (sp
== edit
->undo_stack_bottom
)
537 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
538 c
= edit
->undo_stack
[sp
];
541 /* edit->undo_stack[sp] = '@'; */
542 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
546 if (sp
== edit
->undo_stack_bottom
)
549 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
550 if (edit
->undo_stack
[sp
] == -2)
552 /* edit->undo_stack[sp] = '@'; */
553 edit
->undo_stack_pointer
= sp
;
556 edit
->undo_stack
[sp
]++;
562 edit_pop_redo_action (WEdit
* edit
)
565 unsigned long sp
= edit
->redo_stack_pointer
;
567 if (sp
== edit
->redo_stack_bottom
)
570 sp
= (sp
- 1) & edit
->redo_stack_size_mask
;
571 c
= edit
->redo_stack
[sp
];
574 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
578 if (sp
== edit
->redo_stack_bottom
)
581 c
= edit
->redo_stack
[(sp
- 1) & edit
->redo_stack_size_mask
];
582 if (edit
->redo_stack
[sp
] == -2)
583 edit
->redo_stack_pointer
= sp
;
585 edit
->redo_stack
[sp
]++;
591 get_prev_undo_action (WEdit
* edit
)
594 unsigned long sp
= edit
->undo_stack_pointer
;
596 if (sp
== edit
->undo_stack_bottom
)
599 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
600 c
= edit
->undo_stack
[sp
];
604 if (sp
== edit
->undo_stack_bottom
)
607 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
611 /* --------------------------------------------------------------------------------------------- */
612 /** is called whenever a modification is made by one of the four routines below */
615 edit_modification (WEdit
* edit
)
617 edit
->caches_valid
= FALSE
;
619 /* raise lock when file modified */
620 if (!edit
->modified
&& !edit
->delete_file
)
621 edit
->locked
= lock_file (edit
->filename_vpath
);
625 /* --------------------------------------------------------------------------------------------- */
628 edit_get_byte_ptr (const WEdit
* edit
, off_t byte_index
)
630 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
633 if (byte_index
>= edit
->curs1
)
637 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
638 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
639 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
642 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
643 (byte_index
& M_EDIT_BUF_SIZE
));
646 /* --------------------------------------------------------------------------------------------- */
650 edit_get_prev_utf (const WEdit
* edit
, off_t byte_index
, int *char_width
)
653 gchar utf8_buf
[3 * UTF8_CHAR_LEN
+ 1];
655 gchar
*cursor_buf_ptr
;
657 if (byte_index
> (edit
->curs1
+ edit
->curs2
) || byte_index
<= 0)
663 for (i
= 0; i
< (3 * UTF8_CHAR_LEN
); i
++)
664 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
- (2 * UTF8_CHAR_LEN
));
665 utf8_buf
[3 * UTF8_CHAR_LEN
] = '\0';
667 cursor_buf_ptr
= utf8_buf
+ (2 * UTF8_CHAR_LEN
);
668 str
= g_utf8_find_prev_char (utf8_buf
, cursor_buf_ptr
);
670 if (str
== NULL
|| g_utf8_next_char (str
) != cursor_buf_ptr
)
673 return *(cursor_buf_ptr
- 1);
677 res
= g_utf8_get_char_validated (str
, -1);
682 return *(cursor_buf_ptr
- 1);
686 *char_width
= cursor_buf_ptr
- str
;
693 /* --------------------------------------------------------------------------------------------- */
694 /* high level cursor movement commands */
695 /* --------------------------------------------------------------------------------------------- */
696 /** check whether cursor is in indent part of line
698 * @param edit editor object
700 * @return TRUE if cursor is in indent, FALSE otherwise
704 is_in_indent (const WEdit
* edit
)
708 for (p
= edit_bol (edit
, edit
->curs1
); p
< edit
->curs1
; p
++)
709 if (strchr (" \t", edit_get_byte (edit
, p
)) == NULL
)
715 /* --------------------------------------------------------------------------------------------- */
716 /** check whether line in editor is blank or not
718 * @param edit editor object
719 * @param offset position in file
721 * @return TRUE if line in blank, FALSE otherwise
725 is_blank (const WEdit
* edit
, off_t offset
)
730 s
= edit_bol (edit
, offset
);
731 f
= edit_eol (edit
, offset
) - 1;
734 c
= edit_get_byte (edit
, s
++);
741 /* --------------------------------------------------------------------------------------------- */
742 /** returns the offset of line i */
745 edit_find_line (WEdit
* edit
, long line
)
748 long m
= 2000000000; /* what is the magic number? */
750 if (!edit
->caches_valid
)
752 memset (edit
->line_numbers
, 0, sizeof (edit
->line_numbers
));
753 memset (edit
->line_offsets
, 0, sizeof (edit
->line_offsets
));
754 /* three offsets that we *know* are line 0 at 0 and these two: */
755 edit
->line_numbers
[1] = edit
->curs_line
;
756 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
757 edit
->line_numbers
[2] = edit
->total_lines
;
758 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
759 edit
->caches_valid
= TRUE
;
761 if (line
>= edit
->total_lines
)
762 return edit
->line_offsets
[2];
765 /* find the closest known point */
766 for (i
= 0; i
< N_LINE_CACHES
; i
++)
770 n
= abs (edit
->line_numbers
[i
] - line
);
778 return edit
->line_offsets
[j
]; /* know the offset exactly */
779 if (m
== 1 && j
>= 3)
780 i
= j
; /* one line different - caller might be looping, so stay in this cache */
782 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
783 if (line
> edit
->line_numbers
[j
])
784 edit
->line_offsets
[i
] =
785 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
787 edit
->line_offsets
[i
] =
788 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
789 edit
->line_numbers
[i
] = line
;
790 return edit
->line_offsets
[i
];
793 /* --------------------------------------------------------------------------------------------- */
794 /** moves up until a blank line is reached, or until just
795 before a non-blank line is reached */
798 edit_move_up_paragraph (WEdit
* edit
, gboolean do_scroll
)
802 if (edit
->curs_line
> 1)
804 if (!edit_line_is_blank (edit
, edit
->curs_line
))
806 for (i
= edit
->curs_line
- 1; i
!= 0; i
--)
807 if (edit_line_is_blank (edit
, i
))
810 else if (edit_line_is_blank (edit
, edit
->curs_line
- 1))
812 for (i
= edit
->curs_line
- 1; i
!= 0; i
--)
813 if (!edit_line_is_blank (edit
, i
))
821 for (i
= edit
->curs_line
- 1; i
!= 0; i
--)
822 if (edit_line_is_blank (edit
, i
))
827 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
830 /* --------------------------------------------------------------------------------------------- */
831 /** moves down until a blank line is reached, or until just
832 before a non-blank line is reached */
835 edit_move_down_paragraph (WEdit
* edit
, gboolean do_scroll
)
839 if (edit
->curs_line
>= edit
->total_lines
- 1)
840 i
= edit
->total_lines
;
841 else if (!edit_line_is_blank (edit
, edit
->curs_line
))
843 for (i
= edit
->curs_line
+ 1; i
!= 0; i
++)
844 if (edit_line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
847 else if (edit_line_is_blank (edit
, edit
->curs_line
+ 1))
849 for (i
= edit
->curs_line
+ 1; i
!= 0; i
++)
850 if (!edit_line_is_blank (edit
, i
) || i
> edit
->total_lines
)
858 for (i
= edit
->curs_line
+ 1; i
!= 0; i
++)
859 if (edit_line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
862 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
865 /* --------------------------------------------------------------------------------------------- */
868 edit_begin_page (WEdit
* edit
)
870 edit_update_curs_row (edit
);
871 edit_move_up (edit
, edit
->curs_row
, 0);
874 /* --------------------------------------------------------------------------------------------- */
877 edit_end_page (WEdit
* edit
)
879 edit_update_curs_row (edit
);
880 edit_move_down (edit
, WIDGET (edit
)->lines
- edit
->curs_row
- 1, 0);
884 /* --------------------------------------------------------------------------------------------- */
885 /** goto beginning of text */
888 edit_move_to_top (WEdit
* edit
)
892 edit_cursor_move (edit
, -edit
->curs1
);
893 edit_move_to_prev_col (edit
, 0);
894 edit
->force
|= REDRAW_PAGE
;
895 edit
->search_start
= 0;
896 edit_update_curs_row (edit
);
901 /* --------------------------------------------------------------------------------------------- */
902 /** goto end of text */
905 edit_move_to_bottom (WEdit
* edit
)
907 if (edit
->curs_line
< edit
->total_lines
)
909 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
910 edit
->start_display
= edit
->last_byte
;
911 edit
->start_line
= edit
->total_lines
;
912 edit_scroll_upward (edit
, WIDGET (edit
)->lines
- 1);
913 edit
->force
|= REDRAW_PAGE
;
917 /* --------------------------------------------------------------------------------------------- */
918 /** goto beginning of line */
921 edit_cursor_to_bol (WEdit
* edit
)
923 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
924 edit
->search_start
= edit
->curs1
;
925 edit
->prev_col
= edit_get_col (edit
);
929 /* --------------------------------------------------------------------------------------------- */
930 /** goto end of line */
933 edit_cursor_to_eol (WEdit
* edit
)
935 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
936 edit
->search_start
= edit
->curs1
;
937 edit
->prev_col
= edit_get_col (edit
);
941 /* --------------------------------------------------------------------------------------------- */
948 const char option_chars_move_whole_word
[] =
949 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
955 if (*option_chars_move_whole_word
== '!')
959 if (g_ascii_isupper ((gchar
) c
))
961 else if (g_ascii_islower ((gchar
) c
))
963 else if (g_ascii_isalpha (c
))
965 else if (isdigit (c
))
967 else if (isspace (c
))
969 q
= strchr (option_chars_move_whole_word
, c
);
974 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
979 while ((q
= strchr (q
+ 1, c
)));
983 /* --------------------------------------------------------------------------------------------- */
986 edit_left_word_move (WEdit
* edit
, int s
)
992 if (edit
->column_highlight
993 && edit
->mark1
!= edit
->mark2
994 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
996 edit_cursor_move (edit
, -1);
997 if (edit
->curs1
== 0)
999 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1000 c2
= edit_get_byte (edit
, edit
->curs1
);
1001 if (c1
== '\n' || c2
== '\n')
1003 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1005 if (isspace (c1
) && !isspace (c2
))
1007 if (s
!= 0 && !isspace (c1
) && isspace (c2
))
1012 /* --------------------------------------------------------------------------------------------- */
1015 edit_left_word_move_cmd (WEdit
* edit
)
1017 edit_left_word_move (edit
, 0);
1018 edit
->force
|= REDRAW_PAGE
;
1021 /* --------------------------------------------------------------------------------------------- */
1024 edit_right_word_move (WEdit
* edit
, int s
)
1030 if (edit
->column_highlight
1031 && edit
->mark1
!= edit
->mark2
1032 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
1034 edit_cursor_move (edit
, 1);
1035 if (edit
->curs1
>= edit
->last_byte
)
1037 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1038 c2
= edit_get_byte (edit
, edit
->curs1
);
1039 if (c1
== '\n' || c2
== '\n')
1041 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1043 if (isspace (c1
) && !isspace (c2
))
1045 if (s
!= 0 && !isspace (c1
) && isspace (c2
))
1050 /* --------------------------------------------------------------------------------------------- */
1053 edit_right_word_move_cmd (WEdit
* edit
)
1055 edit_right_word_move (edit
, 0);
1056 edit
->force
|= REDRAW_PAGE
;
1059 /* --------------------------------------------------------------------------------------------- */
1062 edit_right_char_move_cmd (WEdit
* edit
)
1069 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
1076 c
= edit_get_byte (edit
, edit
->curs1
);
1078 if (option_cursor_beyond_eol
&& c
== '\n')
1084 edit_cursor_move (edit
, cw
);
1088 /* --------------------------------------------------------------------------------------------- */
1091 edit_left_char_move_cmd (WEdit
* edit
)
1094 if (edit
->column_highlight
1095 && option_cursor_beyond_eol
1096 && edit
->mark1
!= edit
->mark2
1097 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1102 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1107 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1113 edit_cursor_move (edit
, -cw
);
1117 /* --------------------------------------------------------------------------------------------- */
1118 /** Up or down cursor moving.
1119 direction = TRUE - move up
1124 edit_move_updown (WEdit
* edit
, long lines
, gboolean do_scroll
, gboolean direction
)
1127 long l
= direction
? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
1136 edit
->force
|= REDRAW_PAGE
;
1140 edit_scroll_upward (edit
, lines
);
1142 edit_scroll_downward (edit
, lines
);
1144 p
= edit_bol (edit
, edit
->curs1
);
1146 p
= direction
? edit_move_backward (edit
, p
, lines
) : edit_move_forward (edit
, p
, lines
, 0);
1148 edit_cursor_move (edit
, p
- edit
->curs1
);
1150 edit_move_to_prev_col (edit
, p
);
1152 /* search start of current multibyte char (like CJK) */
1153 if (edit
->curs1
+ 1 < edit
->last_byte
)
1155 edit_right_char_move_cmd (edit
);
1156 edit_left_char_move_cmd (edit
);
1159 edit
->search_start
= edit
->curs1
;
1160 edit
->found_len
= 0;
1163 /* --------------------------------------------------------------------------------------------- */
1166 edit_right_delete_word (WEdit
* edit
)
1168 while (edit
->curs1
< edit
->last_byte
)
1172 c1
= edit_delete (edit
, TRUE
);
1173 c2
= edit_get_byte (edit
, edit
->curs1
);
1174 if (c1
== '\n' || c2
== '\n')
1176 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1178 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1183 /* --------------------------------------------------------------------------------------------- */
1186 edit_left_delete_word (WEdit
* edit
)
1188 while (edit
->curs1
> 0)
1192 c1
= edit_backspace (edit
, TRUE
);
1193 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1194 if (c1
== '\n' || c2
== '\n')
1196 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1198 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1203 /* --------------------------------------------------------------------------------------------- */
1205 the start column position is not recorded, and hence does not
1206 undo as it happed. But who would notice.
1210 edit_do_undo (WEdit
* edit
)
1215 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1217 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1224 edit_cursor_move (edit
, 1);
1227 edit_cursor_move (edit
, -1);
1231 edit_backspace (edit
, TRUE
);
1235 edit_delete (edit
, TRUE
);
1238 edit
->column_highlight
= 1;
1241 edit
->column_highlight
= 0;
1244 if (ac
>= 256 && ac
< 512)
1245 edit_insert_ahead (edit
, ac
- 256);
1246 if (ac
>= 0 && ac
< 256)
1247 edit_insert (edit
, ac
);
1249 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1251 edit
->mark1
= ac
- MARK_1
;
1253 (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1255 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1257 edit
->mark2
= ac
- MARK_2
;
1259 (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1261 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1263 edit
->end_mark_curs
= ac
- MARK_CURS
;
1266 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1269 if (edit
->start_display
> ac
- KEY_PRESS
)
1271 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1272 edit
->force
|= REDRAW_PAGE
;
1274 else if (edit
->start_display
< ac
- KEY_PRESS
)
1276 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1277 edit
->force
|= REDRAW_PAGE
;
1279 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1280 edit_update_curs_row (edit
);
1283 edit
->undo_stack_disable
= 0;
1286 /* --------------------------------------------------------------------------------------------- */
1289 edit_do_redo (WEdit
* edit
)
1294 if (edit
->redo_stack_reset
)
1298 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1305 edit_cursor_move (edit
, 1);
1308 edit_cursor_move (edit
, -1);
1311 edit_backspace (edit
, TRUE
);
1314 edit_delete (edit
, TRUE
);
1317 edit
->column_highlight
= 1;
1320 edit
->column_highlight
= 0;
1323 if (ac
>= 256 && ac
< 512)
1324 edit_insert_ahead (edit
, ac
- 256);
1325 if (ac
>= 0 && ac
< 256)
1326 edit_insert (edit
, ac
);
1328 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1330 edit
->mark1
= ac
- MARK_1
;
1332 (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1334 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1336 edit
->mark2
= ac
- MARK_2
;
1338 (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1340 /* more than one pop usually means something big */
1342 edit
->force
|= REDRAW_PAGE
;
1345 if (edit
->start_display
> ac
- KEY_PRESS
)
1347 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1348 edit
->force
|= REDRAW_PAGE
;
1350 else if (edit
->start_display
< ac
- KEY_PRESS
)
1352 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1353 edit
->force
|= REDRAW_PAGE
;
1355 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1356 edit_update_curs_row (edit
);
1362 /* --------------------------------------------------------------------------------------------- */
1365 edit_group_undo (WEdit
* edit
)
1367 long ac
= KEY_PRESS
;
1368 long cur_ac
= KEY_PRESS
;
1369 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1371 cur_ac
= get_prev_undo_action (edit
);
1372 edit_do_undo (edit
);
1373 ac
= get_prev_undo_action (edit
);
1374 /* exit from cycle if option_group_undo is not set,
1375 * and make single UNDO operation
1377 if (!option_group_undo
)
1382 /* --------------------------------------------------------------------------------------------- */
1385 edit_delete_to_line_end (WEdit
* edit
)
1387 while (edit_get_byte (edit
, edit
->curs1
) != '\n' && edit
->curs2
!= 0)
1388 edit_delete (edit
, TRUE
);
1391 /* --------------------------------------------------------------------------------------------- */
1394 edit_delete_to_line_begin (WEdit
* edit
)
1396 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
!= 0)
1397 edit_backspace (edit
, TRUE
);
1400 /* --------------------------------------------------------------------------------------------- */
1403 is_aligned_on_a_tab (WEdit
* edit
)
1407 edit_update_curs_col (edit
);
1408 curs_col
= edit
->curs_col
% (TAB_SIZE
* space_width
);
1409 return (curs_col
== 0 || curs_col
== (HALF_TAB_SIZE
* space_width
));
1412 /* --------------------------------------------------------------------------------------------- */
1415 right_of_four_spaces (WEdit
* edit
)
1419 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1420 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1422 return (ch
== ' ' && is_aligned_on_a_tab (edit
));
1425 /* --------------------------------------------------------------------------------------------- */
1428 left_of_four_spaces (WEdit
* edit
)
1432 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1433 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1435 return (ch
== ' ' && is_aligned_on_a_tab (edit
));
1438 /* --------------------------------------------------------------------------------------------- */
1441 edit_auto_indent (WEdit
* edit
)
1447 /* use the previous line as a template */
1448 p
= edit_move_backward (edit
, p
, 1);
1449 /* copy the leading whitespace of the line */
1451 { /* no range check - the line _is_ \n-terminated */
1452 c
= edit_get_byte (edit
, p
++);
1453 if (c
!= ' ' && c
!= '\t')
1455 edit_insert (edit
, c
);
1459 /* --------------------------------------------------------------------------------------------- */
1462 edit_double_newline (WEdit
* edit
)
1464 edit_insert (edit
, '\n');
1465 if (edit_get_byte (edit
, edit
->curs1
) == '\n' || edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1467 edit
->force
|= REDRAW_PAGE
;
1468 edit_insert (edit
, '\n');
1471 /* --------------------------------------------------------------------------------------------- */
1474 insert_spaces_tab (WEdit
* edit
, gboolean half
)
1478 edit_update_curs_col (edit
);
1479 i
= option_tab_spacing
* space_width
;
1484 i
= ((edit
->curs_col
/ i
) + 1) * i
- edit
->curs_col
;
1487 edit_insert (edit
, ' ');
1493 /* --------------------------------------------------------------------------------------------- */
1496 edit_tab_cmd (WEdit
* edit
)
1498 if (option_fake_half_tabs
&& is_in_indent (edit
))
1500 /* insert a half tab (usually four spaces) unless there is a
1501 half tab already behind, then delete it and insert a
1503 if (option_fill_tabs_with_spaces
|| !right_of_four_spaces (edit
))
1504 insert_spaces_tab (edit
, TRUE
);
1509 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1510 edit_backspace (edit
, TRUE
);
1511 edit_insert (edit
, '\t');
1514 else if (option_fill_tabs_with_spaces
)
1515 insert_spaces_tab (edit
, FALSE
);
1517 edit_insert (edit
, '\t');
1520 /* --------------------------------------------------------------------------------------------- */
1523 check_and_wrap_line (WEdit
* edit
)
1528 if (!option_typewriter_wrap
)
1530 edit_update_curs_col (edit
);
1531 if (edit
->curs_col
< option_word_wrap_line_length
)
1537 c
= edit_get_byte (edit
, curs
);
1538 if (c
== '\n' || curs
<= 0)
1540 edit_insert (edit
, '\n');
1543 if (c
== ' ' || c
== '\t')
1545 off_t current
= edit
->curs1
;
1546 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
1547 edit_insert (edit
, '\n');
1548 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
1554 /* --------------------------------------------------------------------------------------------- */
1555 /** this find the matching bracket in either direction, and sets edit->bracket
1557 * @param edit editor object
1558 * @param in_screen seach only on the current screen
1559 * @param furthest_bracket_search count of the bytes for search
1561 * @return position of the found bracket (-1 if no match)
1565 edit_get_bracket (WEdit
* edit
, gboolean in_screen
, unsigned long furthest_bracket_search
)
1567 const char *const b
= "{}{[][()(", *p
;
1568 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
1569 unsigned long j
= 0;
1571 edit_update_curs_row (edit
);
1572 c
= edit_get_byte (edit
, edit
->curs1
);
1575 if (!furthest_bracket_search
)
1576 furthest_bracket_search
--;
1577 /* not on a bracket at all */
1580 /* the matching bracket */
1582 /* going left or right? */
1583 if (strchr ("{[(", c
))
1585 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
1587 /* out of buffer? */
1588 if (q
>= edit
->last_byte
|| q
< 0)
1590 a
= edit_get_byte (edit
, q
);
1591 /* don't want to eat CPU */
1592 if (j
++ > furthest_bracket_search
)
1594 /* out of screen? */
1597 if (q
< edit
->start_display
)
1599 /* count lines if searching downward */
1600 if (inc
> 0 && a
== '\n')
1601 if (n
++ >= WIDGET (edit
)->lines
- edit
->curs_row
) /* out of screen */
1604 /* count bracket depth */
1605 i
+= (a
== c
) - (a
== d
);
1606 /* return if bracket depth is zero */
1614 /* --------------------------------------------------------------------------------------------- */
1617 edit_goto_matching_bracket (WEdit
* edit
)
1621 q
= edit_get_bracket (edit
, 0, 0);
1624 edit
->bracket
= edit
->curs1
;
1625 edit
->force
|= REDRAW_PAGE
;
1626 edit_cursor_move (edit
, q
- edit
->curs1
);
1630 /* --------------------------------------------------------------------------------------------- */
1633 edit_move_block_to_right (WEdit
* edit
)
1635 off_t start_mark
, end_mark
;
1636 long cur_bol
, start_bol
;
1638 if (eval_marks (edit
, &start_mark
, &end_mark
))
1641 start_bol
= edit_bol (edit
, start_mark
);
1642 cur_bol
= edit_bol (edit
, end_mark
- 1);
1646 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1647 if (!edit_line_is_blank (edit
, edit
->curs_line
))
1649 if (option_fill_tabs_with_spaces
)
1650 insert_spaces_tab (edit
, option_fake_half_tabs
);
1652 edit_insert (edit
, '\t');
1653 edit_cursor_move (edit
, edit_bol (edit
, cur_bol
) - edit
->curs1
);
1659 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1661 while (cur_bol
>= start_bol
);
1663 edit
->force
|= REDRAW_PAGE
;
1666 /* --------------------------------------------------------------------------------------------- */
1669 edit_move_block_to_left (WEdit
* edit
)
1671 off_t start_mark
, end_mark
;
1672 off_t cur_bol
, start_bol
;
1675 if (eval_marks (edit
, &start_mark
, &end_mark
))
1678 start_bol
= edit_bol (edit
, start_mark
);
1679 cur_bol
= edit_bol (edit
, end_mark
- 1);
1686 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1688 if (option_fake_half_tabs
)
1689 del_tab_width
= HALF_TAB_SIZE
;
1691 del_tab_width
= option_tab_spacing
;
1693 next_char
= edit_get_byte (edit
, edit
->curs1
);
1694 if (next_char
== '\t')
1695 edit_delete (edit
, TRUE
);
1696 else if (next_char
== ' ')
1697 for (i
= 1; i
<= del_tab_width
; i
++)
1699 if (next_char
== ' ')
1700 edit_delete (edit
, TRUE
);
1701 next_char
= edit_get_byte (edit
, edit
->curs1
);
1707 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1709 while (cur_bol
>= start_bol
);
1711 edit
->force
|= REDRAW_PAGE
;
1714 /* --------------------------------------------------------------------------------------------- */
1716 * prints at the cursor
1717 * @return number of chars printed
1721 edit_print_string (WEdit
* e
, const char *s
)
1725 while (s
[i
] != '\0')
1726 edit_execute_cmd (e
, CK_InsertChar
, (unsigned char) s
[i
++]);
1727 e
->force
|= REDRAW_COMPLETELY
;
1728 edit_update_screen (e
);
1732 /* --------------------------------------------------------------------------------------------- */
1735 edit_insert_column_from_file (WEdit
* edit
, int file
, off_t
* start_pos
, off_t
* end_pos
,
1736 long *col1
, long *col2
)
1740 off_t blocklen
= -1, width
= 0;
1741 unsigned char *data
;
1743 cursor
= edit
->curs1
;
1744 col
= edit_get_col (edit
);
1745 data
= g_malloc0 (TEMP_BUF_LEN
);
1747 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0)
1752 pn
= strchr ((char *) data
, '\n');
1753 width
= pn
== NULL
? blocklen
: pn
- (char *) data
;
1755 for (i
= 0; i
< blocklen
; i
++)
1757 if (data
[i
] != '\n')
1758 edit_insert (edit
, data
[i
]);
1760 { /* fill in and move to next line */
1764 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
1765 for (l
= width
- (edit_get_col (edit
) - col
); l
> 0; l
-= space_width
)
1766 edit_insert (edit
, ' ');
1768 for (p
= edit
->curs1
;; p
++)
1770 if (p
== edit
->last_byte
)
1772 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
1773 edit_insert_ahead (edit
, '\n');
1777 if (edit_get_byte (edit
, p
) == '\n')
1784 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
1786 for (l
= col
- edit_get_col (edit
); l
>= space_width
; l
-= space_width
)
1787 edit_insert (edit
, ' ');
1792 *col2
= col
+ width
;
1793 *start_pos
= cursor
;
1794 *end_pos
= edit
->curs1
;
1795 edit_cursor_move (edit
, cursor
- edit
->curs1
);
1801 /* --------------------------------------------------------------------------------------------- */
1802 /*** public functions ****************************************************************************/
1803 /* --------------------------------------------------------------------------------------------- */
1805 /** User edit menu, like user menu (F2) but only in editor. */
1808 user_menu (WEdit
* edit
, const char *menu_file
, int selected_entry
)
1813 off_t start_mark
, end_mark
;
1815 vfs_path_t
*block_file_vpath
;
1817 block_file
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
1818 block_file_vpath
= vfs_path_from_str (block_file
);
1820 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
1822 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1824 /* run shell scripts from menu */
1825 if (user_menu_cmd (edit
, menu_file
, selected_entry
)
1826 && (mc_stat (block_file_vpath
, &status
) == 0) && (status
.st_size
!= 0))
1831 /* i.e. we have marked block */
1833 rc
= edit_block_delete_cmd (edit
);
1839 ins_len
= edit_insert_file (edit
, block_file_vpath
);
1840 if (nomark
== 0 && ins_len
> 0)
1841 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1843 /* truncate block file */
1844 fd
= fopen (block_file
, "w");
1848 g_free (block_file
);
1849 vfs_path_free (block_file_vpath
);
1851 edit_cursor_move (edit
, curs
- edit
->curs1
);
1852 edit
->force
|= REDRAW_PAGE
;
1853 widget_redraw (WIDGET (edit
));
1856 /* --------------------------------------------------------------------------------------------- */
1859 edit_get_byte (const WEdit
* edit
, off_t byte_index
)
1863 p
= edit_get_byte_ptr (edit
, byte_index
);
1865 return (p
!= NULL
) ? *(unsigned char *) p
: '\n';
1868 /* --------------------------------------------------------------------------------------------- */
1872 edit_get_utf (const WEdit
* edit
, off_t byte_index
, int *char_width
)
1877 gchar
*next_ch
= NULL
;
1879 gchar utf8_buf
[UTF8_CHAR_LEN
+ 1];
1881 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1887 str
= edit_get_byte_ptr (edit
, byte_index
);
1895 res
= g_utf8_get_char_validated (str
, -1);
1899 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1901 for (i
= 0; i
< UTF8_CHAR_LEN
; i
++)
1902 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
);
1903 utf8_buf
[UTF8_CHAR_LEN
] = '\0';
1905 res
= g_utf8_get_char_validated (str
, -1);
1916 /* Calculate UTF-8 char width */
1917 next_ch
= g_utf8_next_char (str
);
1920 width
= next_ch
- str
;
1928 *char_width
= width
;
1933 /* --------------------------------------------------------------------------------------------- */
1936 edit_get_write_filter (const vfs_path_t
* write_name_vpath
, const vfs_path_t
* filename_vpath
)
1939 char *p
, *writename
;
1940 const vfs_path_element_t
*path_element
;
1942 i
= edit_find_filter (filename_vpath
);
1946 path_element
= vfs_path_get_by_index (write_name_vpath
, -1);
1947 writename
= name_quote (path_element
->path
, 0);
1948 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
1953 /* --------------------------------------------------------------------------------------------- */
1955 * @param edit editor object
1956 * @param f value of stream file
1957 * @return the length of the file
1961 edit_write_stream (WEdit
* edit
, FILE * f
)
1965 if (edit
->lb
== LB_ASIS
)
1967 for (i
= 0; i
< edit
->last_byte
; i
++)
1968 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
1973 /* change line breaks */
1974 for (i
= 0; i
< edit
->last_byte
; i
++)
1976 unsigned char c
= edit_get_byte (edit
, i
);
1978 if (!(c
== '\n' || c
== '\r'))
1980 /* not line break */
1981 if (fputc (c
, f
) < 0)
1985 { /* (c == '\n' || c == '\r') */
1986 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
1990 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
1991 /* put one line break unconditionally */
1992 if (fputc ('\n', f
) < 0)
1995 i
++; /* 2 chars are processed */
1997 if (c
== '\r' && c1
== '\n')
1998 /* Windows line break; go to the next char */
2001 if (c
== '\r' && c1
== '\r')
2003 /* two Macintosh line breaks; put second line break */
2004 if (fputc ('\n', f
) < 0)
2009 if (fputc (c1
, f
) < 0)
2013 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
2014 /* put one line break unconditionally */
2015 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
2018 if (c
== '\r' && c1
== '\n')
2019 /* Windows line break; go to the next char */
2023 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
2024 /* put one line break unconditionally */
2025 if (fputc ('\r', f
) < 0)
2028 i
++; /* 2 chars are processed */
2030 if (c
== '\r' && c1
== '\n')
2031 /* Windows line break; go to the next char */
2034 if (c
== '\n' && c1
== '\n')
2036 /* two Windows line breaks; put second line break */
2037 if (fputc ('\r', f
) < 0)
2042 if (fputc (c1
, f
) < 0)
2045 case LB_ASIS
: /* default without changes */
2051 return edit
->last_byte
;
2054 /* --------------------------------------------------------------------------------------------- */
2057 is_break_char (char c
)
2059 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
2062 /* --------------------------------------------------------------------------------------------- */
2065 edit_get_word_from_pos (const WEdit
* edit
, off_t start_pos
, off_t
* start
, gsize
* len
,
2070 GString
*match_expr
;
2073 for (word_start
= start_pos
; word_start
!= 0; word_start
--, cut_len
++)
2075 c1
= edit_get_byte (edit
, word_start
);
2076 c2
= edit_get_byte (edit
, word_start
- 1);
2078 if (is_break_char (c1
) != is_break_char (c2
) || c1
== '\n' || c2
== '\n')
2082 match_expr
= g_string_sized_new (16);
2086 c1
= edit_get_byte (edit
, word_start
+ match_expr
->len
);
2087 c2
= edit_get_byte (edit
, word_start
+ match_expr
->len
+ 1);
2088 g_string_append_c (match_expr
, c1
);
2090 while (!(is_break_char (c1
) != is_break_char (c2
) || c1
== '\n' || c2
== '\n'));
2092 *len
= match_expr
->len
;
2093 *start
= word_start
;
2096 return g_string_free (match_expr
, FALSE
);
2099 /* --------------------------------------------------------------------------------------------- */
2100 /** inserts a file at the cursor, returns count of inserted bytes on success */
2103 edit_insert_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
2109 p
= edit_get_filter (filename_vpath
);
2110 current
= edit
->curs1
;
2116 f
= (FILE *) popen (p
, "r");
2119 edit_insert_stream (edit
, f
);
2121 /* Place cursor at the end of text selection */
2122 if (!option_cursor_after_inserted_block
)
2124 ins_len
= edit
->curs1
- current
;
2125 edit_cursor_move (edit
, -ins_len
);
2131 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
2132 edit_error_dialog (_("Error"), errmsg
);
2141 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
2142 edit_error_dialog (_("Error"), errmsg
);
2152 int vertical_insertion
= 0;
2155 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
2159 buf
= g_malloc0 (TEMP_BUF_LEN
);
2160 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2163 /* if contain signature VERTICAL_MAGIC then it vertical block */
2164 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2165 vertical_insertion
= 1;
2167 mc_lseek (file
, 0, SEEK_SET
);
2170 if (vertical_insertion
)
2175 blocklen
= edit_insert_column_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
2176 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2178 /* highlight inserted text then not persistent blocks */
2179 if (!option_persistent_selections
&& edit
->modified
)
2181 if (!edit
->column_highlight
)
2182 edit_push_undo_action (edit
, COLUMN_OFF
);
2183 edit
->column_highlight
= 1;
2190 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2192 for (i
= 0; i
< blocklen
; i
++)
2193 edit_insert (edit
, buf
[i
]);
2195 /* highlight inserted text then not persistent blocks */
2196 if (!option_persistent_selections
&& edit
->modified
)
2198 edit_set_markers (edit
, edit
->curs1
, current
, 0, 0);
2199 if (edit
->column_highlight
)
2200 edit_push_undo_action (edit
, COLUMN_ON
);
2201 edit
->column_highlight
= 0;
2204 /* Place cursor at the end of text selection */
2205 if (!option_cursor_after_inserted_block
)
2207 ins_len
= edit
->curs1
- current
;
2208 edit_cursor_move (edit
, -ins_len
);
2212 edit
->force
|= REDRAW_PAGE
;
2222 /* --------------------------------------------------------------------------------------------- */
2224 * Fill in the edit structure. Return NULL on failure. Pass edit as
2225 * NULL to allocate a new structure.
2227 * If line is 0, try to restore saved position. Otherwise put the
2228 * cursor on that line and show it in the middle of the screen.
2232 edit_init (WEdit
* edit
, int y
, int x
, int lines
, int cols
, const vfs_path_t
* filename_vpath
,
2235 gboolean to_free
= FALSE
;
2237 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2238 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
2242 /* save some widget parameters */
2243 gboolean fullscreen
= edit
->fullscreen
;
2244 int y_prev
= edit
->y_prev
;
2245 int x_prev
= edit
->x_prev
;
2246 int lines_prev
= edit
->lines_prev
;
2247 int cols_prev
= edit
->cols_prev
;
2249 edit_purge_widget (edit
);
2251 /* restore saved parameters */
2252 edit
->fullscreen
= fullscreen
;
2253 edit
->y_prev
= y_prev
;
2254 edit
->x_prev
= x_prev
;
2255 edit
->lines_prev
= lines_prev
;
2256 edit
->cols_prev
= cols_prev
;
2260 edit
= g_malloc0 (sizeof (WEdit
));
2263 init_widget (WIDGET (edit
), y
, x
, lines
, cols
, NULL
, NULL
);
2264 edit
->fullscreen
= TRUE
;
2265 edit_save_size (edit
);
2268 edit
->drag_state
= MCEDIT_DRAG_NORMAL
;
2270 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2271 edit
->stat1
.st_uid
= getuid ();
2272 edit
->stat1
.st_gid
= getgid ();
2273 edit
->stat1
.st_mtime
= 0;
2277 edit
->last_bracket
= -1;
2278 edit
->force
|= REDRAW_PAGE
;
2280 /* set file name before load file */
2281 edit_set_filename (edit
, filename_vpath
);
2283 edit
->undo_stack_size
= START_STACK_SIZE
;
2284 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2285 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2287 edit
->redo_stack_size
= START_STACK_SIZE
;
2288 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2289 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2293 edit
->converter
= str_cnv_from_term
;
2294 edit_set_codeset (edit
);
2297 if (!edit_load_file (edit
))
2299 /* edit_load_file already gives an error message */
2305 edit
->loading_done
= 1;
2308 edit_load_syntax (edit
, NULL
, NULL
);
2309 edit_get_syntax_color (edit
, -1);
2311 /* load saved cursor position */
2312 if ((line
== 0) && option_save_position
)
2313 edit_load_position (edit
);
2318 edit_move_display (edit
, line
- 1);
2319 edit_move_to_line (edit
, line
- 1);
2322 edit_load_macro_cmd (edit
);
2327 /* --------------------------------------------------------------------------------------------- */
2329 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2331 edit_clean (WEdit
* edit
)
2338 /* a stale lock, remove it */
2340 edit
->locked
= unlock_file (edit
->filename_vpath
);
2342 /* save cursor position */
2343 if (option_save_position
)
2344 edit_save_position (edit
);
2345 else if (edit
->serialized_bookmarks
!= NULL
)
2346 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2348 /* File specified on the mcedit command line and never saved */
2349 if (edit
->delete_file
)
2350 unlink (vfs_path_get_last_path_str (edit
->filename_vpath
));
2352 edit_free_syntax_rules (edit
);
2353 book_mark_flush (edit
, -1);
2354 for (; j
<= MAXBUFF
; j
++)
2356 g_free (edit
->buffers1
[j
]);
2357 g_free (edit
->buffers2
[j
]);
2360 g_free (edit
->undo_stack
);
2361 g_free (edit
->redo_stack
);
2362 vfs_path_free (edit
->filename_vpath
);
2363 vfs_path_free (edit
->dir_vpath
);
2364 mc_search_free (edit
->search
);
2365 edit
->search
= NULL
;
2368 if (edit
->converter
!= str_cnv_from_term
)
2369 str_close_conv (edit
->converter
);
2372 edit_purge_widget (edit
);
2377 /* --------------------------------------------------------------------------------------------- */
2380 * Load a new file into the editor and set line. If it fails, preserve the old file.
2381 * To do it, allocate a new widget, initialize it and, if the new file
2382 * was loaded, copy the data to the old widget.
2384 * @return TRUE on success, FALSE on failure.
2387 edit_reload_line (WEdit
* edit
, const vfs_path_t
* filename_vpath
, long line
)
2389 Widget
*w
= WIDGET (edit
);
2392 e
= g_malloc0 (sizeof (WEdit
));
2394 /* save some widget parameters */
2395 e
->fullscreen
= edit
->fullscreen
;
2396 e
->y_prev
= edit
->y_prev
;
2397 e
->x_prev
= edit
->x_prev
;
2398 e
->lines_prev
= edit
->lines_prev
;
2399 e
->cols_prev
= edit
->cols_prev
;
2401 if (edit_init (e
, w
->y
, w
->x
, w
->lines
, w
->cols
, filename_vpath
, line
) == NULL
)
2408 memcpy (edit
, e
, sizeof (WEdit
));
2414 /* --------------------------------------------------------------------------------------------- */
2418 edit_set_codeset (WEdit
* edit
)
2423 get_codepage_id (mc_global
.source_codepage
>=
2424 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2429 conv
= str_crt_conv_from (cp_id
);
2430 if (conv
!= INVALID_CONV
)
2432 if (edit
->converter
!= str_cnv_from_term
)
2433 str_close_conv (edit
->converter
);
2434 edit
->converter
= conv
;
2439 edit
->utf8
= str_isutf8 (cp_id
);
2444 /* --------------------------------------------------------------------------------------------- */
2447 * Recording stack for undo:
2448 * The following is an implementation of a compressed stack. Identical
2449 * pushes are recorded by a negative prefix indicating the number of times the
2450 * same char was pushed. This saves space for repeated curs-left or curs-right
2467 * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2468 * 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2469 * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2470 * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2473 * The only way the cursor moves or the buffer is changed is through the routines:
2474 * insert, backspace, insert_ahead, delete, and cursor_move.
2475 * These record the reverse undo movements onto the stack each time they are
2478 * Each key press results in a set of actions (insert; delete ...). So each time
2479 * a key is pressed the current position of start_display is pushed as
2480 * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2481 * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2482 * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2486 * @param edit editor object
2487 * @param c code of the action
2491 edit_push_undo_action (WEdit
* edit
, long c
)
2493 unsigned long sp
= edit
->undo_stack_pointer
;
2497 /* first enlarge the stack if necessary */
2498 if (sp
> edit
->undo_stack_size
- 10)
2500 if (option_max_undo
< 256)
2501 option_max_undo
= 256;
2502 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2504 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2507 edit
->undo_stack
= t
;
2508 edit
->undo_stack_size
<<= 1;
2509 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2513 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2514 if (edit
->undo_stack_disable
)
2516 edit_push_redo_action (edit
, KEY_PRESS
);
2517 edit_push_redo_action (edit
, c
);
2521 if (edit
->redo_stack_reset
)
2522 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2524 if (edit
->undo_stack_bottom
!= sp
2525 && spm1
!= edit
->undo_stack_bottom
2526 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2529 if (edit
->undo_stack
[spm1
] < 0)
2531 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2532 if (d
== c
&& edit
->undo_stack
[spm1
] > -1000000000)
2534 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2535 edit
->undo_stack
[spm1
]--;
2541 d
= edit
->undo_stack
[spm1
];
2545 return; /* --> no need to push multiple do-nothings */
2546 edit
->undo_stack
[sp
] = -2;
2551 edit
->undo_stack
[sp
] = c
;
2554 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2556 /* if the sp wraps round and catches the undo_stack_bottom then erase
2557 * the first set of actions on the stack to make space - by moving
2558 * undo_stack_bottom forward one "key press" */
2559 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2560 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2561 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2564 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2566 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2567 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2569 /*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: */
2570 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2571 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2573 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2578 edit_push_redo_action (WEdit
* edit
, long c
)
2580 unsigned long sp
= edit
->redo_stack_pointer
;
2583 /* first enlarge the stack if necessary */
2584 if (sp
> edit
->redo_stack_size
- 10)
2586 if (option_max_undo
< 256)
2587 option_max_undo
= 256;
2588 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2590 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2593 edit
->redo_stack
= t
;
2594 edit
->redo_stack_size
<<= 1;
2595 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2599 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2601 if (edit
->redo_stack_bottom
!= sp
2602 && spm1
!= edit
->redo_stack_bottom
2603 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2606 if (edit
->redo_stack
[spm1
] < 0)
2608 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2609 if (d
== c
&& edit
->redo_stack
[spm1
] > -1000000000)
2611 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2612 edit
->redo_stack
[spm1
]--;
2618 d
= edit
->redo_stack
[spm1
];
2622 return; /* --> no need to push multiple do-nothings */
2623 edit
->redo_stack
[sp
] = -2;
2624 goto redo_check_bottom
;
2628 edit
->redo_stack
[sp
] = c
;
2631 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2633 /* if the sp wraps round and catches the redo_stack_bottom then erase
2634 * the first set of actions on the stack to make space - by moving
2635 * redo_stack_bottom forward one "key press" */
2636 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2637 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2638 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2641 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2643 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2644 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2647 * If a single key produced enough pushes to wrap all the way round then
2648 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2649 * The stack is then initialised:
2652 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2653 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2654 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2657 /* --------------------------------------------------------------------------------------------- */
2659 Basic low level single character buffer alterations and movements at the cursor.
2660 Returns char passed over, inserted or removed.
2664 edit_insert (WEdit
* edit
, int c
)
2666 /* check if file has grown to large */
2667 if (edit
->last_byte
>= SIZE_LIMIT
)
2670 /* first we must update the position of the display window */
2671 if (edit
->curs1
< edit
->start_display
)
2673 edit
->start_display
++;
2678 /* Mark file as modified, unless the file hasn't been fully loaded */
2679 if (edit
->loading_done
)
2680 edit_modification (edit
);
2682 /* now we must update some info on the file and check if a redraw is required */
2685 book_mark_inc (edit
, edit
->curs_line
);
2687 edit
->total_lines
++;
2688 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2691 /* save the reverse command onto the undo stack */
2692 /* ordinary char and not space */
2694 edit_push_undo_action (edit
, BACKSPACE
);
2696 edit_push_undo_action (edit
, BACKSPACE_BR
);
2697 /* update markers */
2698 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2699 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2700 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2702 /* add a new buffer if we've reached the end of the last one */
2703 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2704 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2706 /* perform the insertion */
2707 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2708 = (unsigned char) c
;
2710 /* update file length */
2713 /* update cursor position */
2717 /* --------------------------------------------------------------------------------------------- */
2718 /** same as edit_insert and move left */
2721 edit_insert_ahead (WEdit
* edit
, int c
)
2723 if (edit
->last_byte
>= SIZE_LIMIT
)
2726 if (edit
->curs1
< edit
->start_display
)
2728 edit
->start_display
++;
2732 edit_modification (edit
);
2735 book_mark_inc (edit
, edit
->curs_line
);
2736 edit
->total_lines
++;
2737 edit
->force
|= REDRAW_AFTER_CURSOR
;
2739 /* ordinary char and not space */
2741 edit_push_undo_action (edit
, DELCHAR
);
2743 edit_push_undo_action (edit
, DELCHAR_BR
);
2745 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2746 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2747 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2749 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2750 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2751 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2752 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2758 /* --------------------------------------------------------------------------------------------- */
2761 edit_insert_over (WEdit
* edit
)
2765 for (i
= 0; i
< edit
->over_col
; i
++)
2766 edit_insert (edit
, ' ');
2771 /* --------------------------------------------------------------------------------------------- */
2774 edit_delete (WEdit
* edit
, gboolean byte_delete
)
2780 if (edit
->curs2
== 0)
2784 /* if byte_delete == TRUE then delete only one byte not multibyte char */
2785 if (edit
->utf8
&& !byte_delete
)
2787 edit_get_utf (edit
, edit
->curs1
, &cw
);
2795 if (edit
->mark2
!= edit
->mark1
)
2796 edit_push_markers (edit
);
2798 for (i
= 1; i
<= cw
; i
++)
2800 if (edit
->mark1
> edit
->curs1
)
2803 edit
->end_mark_curs
--;
2805 if (edit
->mark2
> edit
->curs1
)
2807 if (edit
->last_get_rule
> edit
->curs1
)
2808 edit
->last_get_rule
--;
2810 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2812 1) & M_EDIT_BUF_SIZE
) - 1];
2814 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2816 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2817 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2821 edit_push_undo_action (edit
, p
+ 256);
2824 edit_modification (edit
);
2827 book_mark_dec (edit
, edit
->curs_line
);
2828 edit
->total_lines
--;
2829 edit
->force
|= REDRAW_AFTER_CURSOR
;
2831 if (edit
->curs1
< edit
->start_display
)
2833 edit
->start_display
--;
2841 /* --------------------------------------------------------------------------------------------- */
2844 edit_backspace (WEdit
* edit
, gboolean byte_delete
)
2850 if (edit
->curs1
== 0)
2853 if (edit
->mark2
!= edit
->mark1
)
2854 edit_push_markers (edit
);
2857 if (edit
->utf8
&& !byte_delete
)
2859 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
2867 for (i
= 1; i
<= cw
; i
++)
2869 if (edit
->mark1
>= edit
->curs1
)
2872 edit
->end_mark_curs
--;
2874 if (edit
->mark2
>= edit
->curs1
)
2876 if (edit
->last_get_rule
>= edit
->curs1
)
2877 edit
->last_get_rule
--;
2879 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
2880 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
2881 if (((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
) == 0)
2883 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2884 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2888 edit_push_undo_action (edit
, p
);
2890 edit_modification (edit
);
2893 book_mark_dec (edit
, edit
->curs_line
);
2895 edit
->total_lines
--;
2896 edit
->force
|= REDRAW_AFTER_CURSOR
;
2899 if (edit
->curs1
< edit
->start_display
)
2901 edit
->start_display
--;
2909 /* --------------------------------------------------------------------------------------------- */
2910 /** moves the cursor right or left: increment positive or negative respectively */
2913 edit_cursor_move (WEdit
* edit
, off_t increment
)
2915 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2920 for (; increment
< 0; increment
++)
2922 if (edit
->curs1
== 0)
2925 edit_push_undo_action (edit
, CURS_RIGHT
);
2927 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2928 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2929 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2930 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2931 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2933 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2934 1) & M_EDIT_BUF_SIZE
];
2935 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2937 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2938 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2944 edit
->force
|= REDRAW_LINE_BELOW
;
2951 for (; increment
> 0; increment
--)
2953 if (edit
->curs2
== 0)
2956 edit_push_undo_action (edit
, CURS_LEFT
);
2958 c
= edit_get_byte (edit
, edit
->curs1
);
2959 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2960 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2961 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2963 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2965 1) & M_EDIT_BUF_SIZE
) - 1];
2966 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2968 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2969 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2975 edit
->force
|= REDRAW_LINE_ABOVE
;
2981 /* These functions return positions relative to lines */
2983 /* --------------------------------------------------------------------------------------------- */
2984 /** returns index of last char on line + 1 */
2987 edit_eol (const WEdit
* edit
, off_t current
)
2989 if (current
>= edit
->last_byte
)
2990 return edit
->last_byte
;
2992 for (; edit_get_byte (edit
, current
) != '\n'; current
++)
2998 /* --------------------------------------------------------------------------------------------- */
2999 /** returns index of first char on line */
3002 edit_bol (const WEdit
* edit
, off_t current
)
3007 for (; edit_get_byte (edit
, current
- 1) != '\n'; current
--)
3013 /* --------------------------------------------------------------------------------------------- */
3016 edit_count_lines (const WEdit
* edit
, off_t current
, off_t upto
)
3020 if (upto
> edit
->last_byte
)
3021 upto
= edit
->last_byte
;
3024 while (current
< upto
)
3025 if (edit_get_byte (edit
, current
++) == '\n')
3030 /* --------------------------------------------------------------------------------------------- */
3031 /* If lines is zero this returns the count of lines from current to upto. */
3032 /* If upto is zero returns index of lines forward current. */
3035 edit_move_forward (const WEdit
* edit
, off_t current
, long lines
, off_t upto
)
3039 return (off_t
) edit_count_lines (edit
, current
, upto
);
3046 while (lines
-- != 0)
3048 next
= edit_eol (edit
, current
) + 1;
3049 if (next
> edit
->last_byte
)
3058 /* --------------------------------------------------------------------------------------------- */
3059 /** Returns offset of 'lines' lines up from current */
3062 edit_move_backward (const WEdit
* edit
, off_t current
, long lines
)
3066 current
= edit_bol (edit
, current
);
3067 while (lines
-- != 0 && current
!= 0)
3068 current
= edit_bol (edit
, current
- 1);
3072 /* --------------------------------------------------------------------------------------------- */
3073 /* If cols is zero this returns the count of columns from current to upto. */
3074 /* If upto is zero returns index of cols across from current. */
3077 edit_move_forward3 (const WEdit
* edit
, off_t current
, long cols
, off_t upto
)
3088 q
= edit
->last_byte
+ 2;
3090 for (col
= 0, p
= current
; p
< q
; p
++)
3102 orig_c
= c
= edit_get_byte (edit
, p
);
3110 utf_ch
= edit_get_utf (edit
, p
, &cw
);
3111 if (mc_global
.utf8_display
)
3115 if (g_unichar_iswide (utf_ch
))
3118 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
3122 c
= convert_to_display_c (c
);
3126 return (upto
!= 0 ? (off_t
) col
: p
);
3128 col
+= TAB_SIZE
- col
% TAB_SIZE
;
3129 else if ((c
< 32 || c
== 127) && (orig_c
== c
3131 || (!mc_global
.utf8_display
&& !edit
->utf8
)
3134 /* '\r' is shown as ^M, so we must advance 2 characters */
3135 /* Caret notation for control characters */
3143 /* --------------------------------------------------------------------------------------------- */
3144 /** returns the current column position of the cursor */
3147 edit_get_col (const WEdit
* edit
)
3149 return (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3152 /* --------------------------------------------------------------------------------------------- */
3153 /* Scrolling functions */
3154 /* --------------------------------------------------------------------------------------------- */
3157 edit_update_curs_row (WEdit
* edit
)
3159 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
3162 /* --------------------------------------------------------------------------------------------- */
3165 edit_update_curs_col (WEdit
* edit
)
3167 edit
->curs_col
= (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3170 /* --------------------------------------------------------------------------------------------- */
3173 edit_get_curs_col (const WEdit
* edit
)
3175 return edit
->curs_col
;
3178 /* --------------------------------------------------------------------------------------------- */
3179 /** moves the display start position up by i lines */
3182 edit_scroll_upward (WEdit
* edit
, long i
)
3184 long lines_above
= edit
->start_line
;
3186 if (i
> lines_above
)
3190 edit
->start_line
-= i
;
3191 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
3192 edit
->force
|= REDRAW_PAGE
;
3193 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3195 edit_update_curs_row (edit
);
3199 /* --------------------------------------------------------------------------------------------- */
3202 edit_scroll_downward (WEdit
* edit
, long i
)
3206 lines_below
= edit
->total_lines
- edit
->start_line
- (WIDGET (edit
)->lines
- 1);
3207 if (lines_below
> 0)
3209 if (i
> lines_below
)
3211 edit
->start_line
+= i
;
3212 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
3213 edit
->force
|= REDRAW_PAGE
;
3214 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3216 edit_update_curs_row (edit
);
3219 /* --------------------------------------------------------------------------------------------- */
3222 edit_scroll_right (WEdit
* edit
, long i
)
3224 edit
->force
|= REDRAW_PAGE
;
3225 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3226 edit
->start_col
-= i
;
3229 /* --------------------------------------------------------------------------------------------- */
3232 edit_scroll_left (WEdit
* edit
, long i
)
3234 if (edit
->start_col
)
3236 edit
->start_col
+= i
;
3237 if (edit
->start_col
> 0)
3238 edit
->start_col
= 0;
3239 edit
->force
|= REDRAW_PAGE
;
3240 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3244 /* --------------------------------------------------------------------------------------------- */
3245 /* high level cursor movement commands */
3246 /* --------------------------------------------------------------------------------------------- */
3249 edit_move_to_prev_col (WEdit
* edit
, off_t p
)
3251 long prev
= edit
->prev_col
;
3252 long over
= edit
->over_col
;
3254 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
3256 if (option_cursor_beyond_eol
)
3260 line_len
= (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
3261 edit_eol (edit
, edit
->curs1
));
3262 if (line_len
< prev
+ edit
->over_col
)
3264 edit
->over_col
= prev
+ over
- line_len
;
3265 edit
->prev_col
= line_len
;
3266 edit
->curs_col
= line_len
;
3270 edit
->curs_col
= prev
+ over
;
3271 edit
->prev_col
= edit
->curs_col
;
3278 if (option_fake_half_tabs
&& is_in_indent (edit
))
3280 long fake_half_tabs
;
3282 edit_update_curs_col (edit
);
3284 fake_half_tabs
= HALF_TAB_SIZE
* space_width
;
3285 if (fake_half_tabs
!= 0 && edit
->curs_col
% fake_half_tabs
!= 0)
3290 edit
->curs_col
-= (edit
->curs_col
% fake_half_tabs
);
3291 p
= edit_bol (edit
, edit
->curs1
);
3292 edit_cursor_move (edit
,
3293 edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->curs1
);
3294 if (!left_of_four_spaces (edit
))
3295 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3301 /* --------------------------------------------------------------------------------------------- */
3302 /** check whether line in editor is blank or not
3304 * @param edit editor object
3305 * @param line number of line
3307 * @return TRUE if line in blank, FALSE otherwise
3311 edit_line_is_blank (WEdit
* edit
, long line
)
3313 return is_blank (edit
, edit_find_line (edit
, line
));
3316 /* --------------------------------------------------------------------------------------------- */
3317 /** move cursor to line 'line' */
3320 edit_move_to_line (WEdit
* e
, long line
)
3322 if (line
< e
->curs_line
)
3323 edit_move_up (e
, e
->curs_line
- line
, 0);
3325 edit_move_down (e
, line
- e
->curs_line
, 0);
3326 edit_scroll_screen_over_cursor (e
);
3329 /* --------------------------------------------------------------------------------------------- */
3330 /** scroll window so that first visible line is 'line' */
3333 edit_move_display (WEdit
* e
, long line
)
3335 if (line
< e
->start_line
)
3336 edit_scroll_upward (e
, e
->start_line
- line
);
3338 edit_scroll_downward (e
, line
- e
->start_line
);
3341 /* --------------------------------------------------------------------------------------------- */
3342 /** save markers onto undo stack */
3345 edit_push_markers (WEdit
* edit
)
3347 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3348 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3349 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3352 /* --------------------------------------------------------------------------------------------- */
3355 edit_set_markers (WEdit
* edit
, off_t m1
, off_t m2
, long c1
, long c2
)
3364 /* --------------------------------------------------------------------------------------------- */
3365 /** highlight marker toggle */
3368 edit_mark_cmd (WEdit
* edit
, gboolean unmark
)
3370 edit_push_markers (edit
);
3373 edit_set_markers (edit
, 0, 0, 0, 0);
3374 edit
->force
|= REDRAW_PAGE
;
3376 else if (edit
->mark2
>= 0)
3378 edit
->end_mark_curs
= -1;
3379 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3380 edit
->curs_col
+ edit
->over_col
);
3381 edit
->force
|= REDRAW_PAGE
;
3385 edit
->end_mark_curs
= edit
->curs1
;
3386 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3387 edit
->curs_col
+ edit
->over_col
);
3391 /* --------------------------------------------------------------------------------------------- */
3392 /** highlight the word under cursor */
3395 edit_mark_current_word_cmd (WEdit
* edit
)
3399 for (pos
= edit
->curs1
; pos
!= 0; pos
--)
3403 c1
= edit_get_byte (edit
, pos
);
3404 c2
= edit_get_byte (edit
, pos
- 1);
3405 if (!isspace (c1
) && isspace (c2
))
3407 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3412 for (; pos
< edit
->last_byte
; pos
++)
3416 c1
= edit_get_byte (edit
, pos
);
3417 c2
= edit_get_byte (edit
, pos
+ 1);
3418 if (!isspace (c1
) && isspace (c2
))
3420 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3423 edit
->mark2
= min (pos
+ 1, edit
->last_byte
);
3425 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3428 /* --------------------------------------------------------------------------------------------- */
3431 edit_mark_current_line_cmd (WEdit
* edit
)
3433 long pos
= edit
->curs1
;
3435 edit
->mark1
= edit_bol (edit
, pos
);
3436 edit
->mark2
= edit_eol (edit
, pos
);
3438 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3441 /* --------------------------------------------------------------------------------------------- */
3444 edit_delete_line (WEdit
* edit
)
3447 * Delete right part of the line.
3448 * Note that edit_get_byte() returns '\n' when byte position is
3451 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3452 (void) edit_delete (edit
, TRUE
);
3456 * Note that edit_delete() will not corrupt anything if called while
3457 * cursor position is EOF.
3459 (void) edit_delete (edit
, TRUE
);
3462 * Delete left part of the line.
3463 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3465 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3466 (void) edit_backspace (edit
, TRUE
);
3469 /* --------------------------------------------------------------------------------------------- */
3472 edit_indent_width (const WEdit
* edit
, off_t p
)
3476 /* move to the end of the leading whitespace of the line */
3477 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1)
3479 /* count the number of columns of indentation */
3480 return (long) edit_move_forward3 (edit
, p
, 0, q
);
3483 /* --------------------------------------------------------------------------------------------- */
3486 edit_insert_indent (WEdit
* edit
, int indent
)
3488 if (!option_fill_tabs_with_spaces
)
3490 while (indent
>= TAB_SIZE
)
3492 edit_insert (edit
, '\t');
3496 while (indent
-- > 0)
3497 edit_insert (edit
, ' ');
3500 /* --------------------------------------------------------------------------------------------- */
3503 edit_push_key_press (WEdit
* edit
)
3505 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3506 if (edit
->mark2
== -1)
3508 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3509 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3513 /* --------------------------------------------------------------------------------------------- */
3516 edit_find_bracket (WEdit
* edit
)
3518 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3519 if (edit
->last_bracket
!= edit
->bracket
)
3520 edit
->force
|= REDRAW_PAGE
;
3521 edit
->last_bracket
= edit
->bracket
;
3524 /* --------------------------------------------------------------------------------------------- */
3526 * This executes a command as though the user initiated it through a key
3527 * press. Callback with MSG_KEY as a message calls this after
3528 * translating the key press. This function can be used to pass any
3529 * command to the editor. Note that the screen wouldn't update
3530 * automatically. Either of command or char_for_insertion must be
3531 * passed as -1. Commands are executed, and char_for_insertion is
3532 * inserted at the cursor.
3536 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3538 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3540 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3543 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3546 if (macro_index
!= -1)
3548 edit
->force
|= REDRAW_COMPLETELY
;
3549 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3551 edit_store_macro_cmd (edit
);
3555 if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3557 edit_repeat_macro_cmd (edit
);
3563 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3565 record_macro_buf
[macro_index
].action
= command
;
3566 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3568 /* record the beginning of a set of editing actions initiated by a key press */
3569 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3570 edit_push_key_press (edit
);
3572 edit_execute_cmd (edit
, command
, char_for_insertion
);
3573 if (edit
->column_highlight
)
3574 edit
->force
|= REDRAW_PAGE
;
3577 /* --------------------------------------------------------------------------------------------- */
3579 This executes a command at a lower level than macro recording.
3580 It also does not push a key_press onto the undo stack. This means
3581 that if it is called many times, a single undo command will undo
3582 all of them. It also does not check for the Undo command.
3585 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3587 Widget
*w
= WIDGET (edit
);
3589 if (command
== CK_WindowFullscreen
)
3591 edit_toggle_fullscreen (edit
);
3595 /* handle window state */
3596 if (edit_handle_move_resize (edit
, command
))
3599 edit
->force
|= REDRAW_LINE
;
3601 /* The next key press will unhighlight the found string, so update
3603 if (edit
->found_len
|| edit
->column_highlight
)
3604 edit
->force
|= REDRAW_PAGE
;
3608 /* a mark command with shift-arrow */
3611 case CK_MarkToWordBegin
:
3612 case CK_MarkToWordEnd
:
3618 case CK_MarkPageDown
:
3619 case CK_MarkToFileBegin
:
3620 case CK_MarkToFileEnd
:
3621 case CK_MarkToPageBegin
:
3622 case CK_MarkToPageEnd
:
3623 case CK_MarkScrollUp
:
3624 case CK_MarkScrollDown
:
3625 case CK_MarkParagraphUp
:
3626 case CK_MarkParagraphDown
:
3627 /* a mark command with alt-arrow */
3628 case CK_MarkColumnPageUp
:
3629 case CK_MarkColumnPageDown
:
3630 case CK_MarkColumnLeft
:
3631 case CK_MarkColumnRight
:
3632 case CK_MarkColumnUp
:
3633 case CK_MarkColumnDown
:
3634 case CK_MarkColumnScrollUp
:
3635 case CK_MarkColumnScrollDown
:
3636 case CK_MarkColumnParagraphUp
:
3637 case CK_MarkColumnParagraphDown
:
3638 edit
->column_highlight
= 0;
3639 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3641 edit_mark_cmd (edit
, TRUE
); /* clear */
3642 edit_mark_cmd (edit
, FALSE
); /* marking on */
3644 edit
->highlight
= 1;
3647 /* any other command */
3649 if (edit
->highlight
)
3650 edit_mark_cmd (edit
, FALSE
); /* clear */
3651 edit
->highlight
= 0;
3654 /* first check for undo */
3655 if (command
== CK_Undo
)
3657 edit
->redo_stack_reset
= 0;
3658 edit_group_undo (edit
);
3659 edit
->found_len
= 0;
3660 edit
->prev_col
= edit_get_col (edit
);
3661 edit
->search_start
= edit
->curs1
;
3664 /* check for redo */
3665 if (command
== CK_Redo
)
3667 edit
->redo_stack_reset
= 0;
3668 edit_do_redo (edit
);
3669 edit
->found_len
= 0;
3670 edit
->prev_col
= edit_get_col (edit
);
3671 edit
->search_start
= edit
->curs1
;
3675 edit
->redo_stack_reset
= 1;
3677 /* An ordinary key press */
3678 if (char_for_insertion
>= 0)
3680 /* if non persistent selection and text selected */
3681 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3682 edit_block_delete_cmd (edit
);
3684 if (edit
->overwrite
)
3686 /* remove char only one time, after input first byte, multibyte chars */
3688 if (!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3690 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
3692 edit_delete (edit
, FALSE
);
3694 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3695 edit_insert_over (edit
);
3697 if (char_for_insertion
> 255 && !mc_global
.utf8_display
)
3699 unsigned char str
[6 + 1];
3703 res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3713 while (str
[i
] != 0 && i
<= 6)
3715 char_for_insertion
= str
[i
];
3716 edit_insert (edit
, char_for_insertion
);
3722 edit_insert (edit
, char_for_insertion
);
3724 if (option_auto_para_formatting
)
3726 format_paragraph (edit
, 0);
3727 edit
->force
|= REDRAW_PAGE
;
3730 check_and_wrap_line (edit
);
3731 edit
->found_len
= 0;
3732 edit
->prev_col
= edit_get_col (edit
);
3733 edit
->search_start
= edit
->curs1
;
3734 edit_find_bracket (edit
);
3740 case CK_TopOnScreen
:
3741 case CK_BottomOnScreen
:
3754 if (!option_persistent_selections
&& edit
->mark2
>= 0)
3756 if (edit
->column_highlight
)
3757 edit_push_undo_action (edit
, COLUMN_ON
);
3758 edit
->column_highlight
= 0;
3759 edit_mark_cmd (edit
, TRUE
);
3765 case CK_TopOnScreen
:
3766 case CK_BottomOnScreen
:
3767 case CK_MarkToPageBegin
:
3768 case CK_MarkToPageEnd
:
3773 case CK_MarkToWordBegin
:
3774 case CK_MarkToWordEnd
:
3777 case CK_MarkColumnUp
:
3778 case CK_MarkColumnDown
:
3779 if (edit
->mark2
== -1)
3780 break; /*marking is following the cursor: may need to highlight a whole line */
3785 edit
->force
|= REDRAW_CHAR_ONLY
;
3788 /* basic cursor key commands */
3792 /* if non persistent selection and text selected */
3793 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3794 edit_block_delete_cmd (edit
);
3795 else if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3797 else if (option_backspace_through_tabs
&& is_in_indent (edit
))
3799 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3800 edit_backspace (edit
, TRUE
);
3802 else if (option_fake_half_tabs
&& is_in_indent (edit
) && right_of_four_spaces (edit
))
3806 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3807 edit_backspace (edit
, TRUE
);
3810 edit_backspace (edit
, FALSE
);
3813 /* if non persistent selection and text selected */
3814 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3815 edit_block_delete_cmd (edit
);
3818 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3819 edit_insert_over (edit
);
3821 if (option_fake_half_tabs
&& is_in_indent (edit
) && left_of_four_spaces (edit
))
3825 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3826 edit_delete (edit
, TRUE
);
3829 edit_delete (edit
, FALSE
);
3832 case CK_DeleteToWordBegin
:
3834 edit_left_delete_word (edit
);
3836 case CK_DeleteToWordEnd
:
3837 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3838 edit_insert_over (edit
);
3840 edit_right_delete_word (edit
);
3843 edit_delete_line (edit
);
3845 case CK_DeleteToHome
:
3846 edit_delete_to_line_begin (edit
);
3848 case CK_DeleteToEnd
:
3849 edit_delete_to_line_end (edit
);
3853 if (option_auto_para_formatting
)
3855 edit_double_newline (edit
);
3856 if (option_return_does_auto_indent
)
3857 edit_auto_indent (edit
);
3858 format_paragraph (edit
, 0);
3862 edit_insert (edit
, '\n');
3863 if (option_return_does_auto_indent
)
3864 edit_auto_indent (edit
);
3868 edit_insert (edit
, '\n');
3871 case CK_MarkColumnPageUp
:
3872 edit
->column_highlight
= 1;
3875 edit_move_up (edit
, w
->lines
- 1, 1);
3877 case CK_MarkColumnPageDown
:
3878 edit
->column_highlight
= 1;
3880 case CK_MarkPageDown
:
3881 edit_move_down (edit
, w
->lines
- 1, 1);
3883 case CK_MarkColumnLeft
:
3884 edit
->column_highlight
= 1;
3887 if (option_fake_half_tabs
&& is_in_indent (edit
) && right_of_four_spaces (edit
))
3889 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3892 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3893 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3896 edit_left_char_move_cmd (edit
);
3898 case CK_MarkColumnRight
:
3899 edit
->column_highlight
= 1;
3902 if (option_fake_half_tabs
&& is_in_indent (edit
) && left_of_four_spaces (edit
))
3904 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3905 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3908 edit_right_char_move_cmd (edit
);
3910 case CK_TopOnScreen
:
3911 case CK_MarkToPageBegin
:
3912 edit_begin_page (edit
);
3914 case CK_BottomOnScreen
:
3915 case CK_MarkToPageEnd
:
3916 edit_end_page (edit
);
3919 case CK_MarkToWordBegin
:
3921 edit_left_word_move_cmd (edit
);
3924 case CK_MarkToWordEnd
:
3926 edit_right_word_move_cmd (edit
);
3928 case CK_MarkColumnUp
:
3929 edit
->column_highlight
= 1;
3932 edit_move_up (edit
, 1, 0);
3934 case CK_MarkColumnDown
:
3935 edit
->column_highlight
= 1;
3938 edit_move_down (edit
, 1, 0);
3940 case CK_MarkColumnParagraphUp
:
3941 edit
->column_highlight
= 1;
3942 case CK_ParagraphUp
:
3943 case CK_MarkParagraphUp
:
3944 edit_move_up_paragraph (edit
, 0);
3946 case CK_MarkColumnParagraphDown
:
3947 edit
->column_highlight
= 1;
3948 case CK_ParagraphDown
:
3949 case CK_MarkParagraphDown
:
3950 edit_move_down_paragraph (edit
, 0);
3952 case CK_MarkColumnScrollUp
:
3953 edit
->column_highlight
= 1;
3955 case CK_MarkScrollUp
:
3956 edit_move_up (edit
, 1, 1);
3958 case CK_MarkColumnScrollDown
:
3959 edit
->column_highlight
= 1;
3961 case CK_MarkScrollDown
:
3962 edit_move_down (edit
, 1, 1);
3966 edit_cursor_to_bol (edit
);
3970 edit_cursor_to_eol (edit
);
3973 /* if text marked shift block */
3974 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3976 if (edit
->mark2
< 0)
3977 edit_mark_cmd (edit
, FALSE
);
3978 edit_move_block_to_right (edit
);
3982 if (option_cursor_beyond_eol
)
3983 edit_insert_over (edit
);
3984 edit_tab_cmd (edit
);
3985 if (option_auto_para_formatting
)
3987 format_paragraph (edit
, 0);
3988 edit
->force
|= REDRAW_PAGE
;
3991 check_and_wrap_line (edit
);
3995 case CK_InsertOverwrite
:
3996 edit
->overwrite
= !edit
->overwrite
;
4000 if (edit
->mark2
>= 0)
4002 if (edit
->column_highlight
)
4003 edit_push_undo_action (edit
, COLUMN_ON
);
4004 edit
->column_highlight
= 0;
4006 edit_mark_cmd (edit
, FALSE
);
4009 if (!edit
->column_highlight
)
4010 edit_push_undo_action (edit
, COLUMN_OFF
);
4011 edit
->column_highlight
= 1;
4012 edit_mark_cmd (edit
, FALSE
);
4015 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
4016 edit
->force
|= REDRAW_PAGE
;
4019 if (edit
->column_highlight
)
4020 edit_push_undo_action (edit
, COLUMN_ON
);
4021 edit
->column_highlight
= 0;
4022 edit_mark_cmd (edit
, TRUE
);
4025 if (edit
->column_highlight
)
4026 edit_push_undo_action (edit
, COLUMN_ON
);
4027 edit
->column_highlight
= 0;
4028 edit_mark_current_word_cmd (edit
);
4031 if (edit
->column_highlight
)
4032 edit_push_undo_action (edit
, COLUMN_ON
);
4033 edit
->column_highlight
= 0;
4034 edit_mark_current_line_cmd (edit
);
4038 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
4039 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
4040 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4042 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4044 case CK_BookmarkFlush
:
4045 book_mark_flush (edit
, BOOK_MARK_COLOR
);
4046 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
4047 edit
->force
|= REDRAW_PAGE
;
4049 case CK_BookmarkNext
:
4050 if (edit
->book_mark
!= NULL
)
4052 edit_book_mark_t
*p
;
4054 p
= book_mark_find (edit
, edit
->curs_line
);
4055 if (p
->next
!= NULL
)
4058 if (p
->line
>= edit
->start_line
+ w
->lines
|| p
->line
< edit
->start_line
)
4059 edit_move_display (edit
, p
->line
- w
->lines
/ 2);
4060 edit_move_to_line (edit
, p
->line
);
4064 case CK_BookmarkPrev
:
4065 if (edit
->book_mark
!= NULL
)
4067 edit_book_mark_t
*p
;
4069 p
= book_mark_find (edit
, edit
->curs_line
);
4070 while (p
->line
== edit
->curs_line
)
4071 if (p
->prev
!= NULL
)
4075 if (p
->line
>= edit
->start_line
+ w
->lines
|| p
->line
< edit
->start_line
)
4076 edit_move_display (edit
, p
->line
- w
->lines
/ 2);
4077 edit_move_to_line (edit
, p
->line
);
4083 case CK_MarkToFileBegin
:
4084 edit_move_to_top (edit
);
4087 case CK_MarkToFileEnd
:
4088 edit_move_to_bottom (edit
);
4092 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4093 edit_insert_over (edit
);
4094 edit_block_copy_cmd (edit
);
4097 edit_block_delete_cmd (edit
);
4100 edit_block_move_cmd (edit
);
4103 case CK_BlockShiftLeft
:
4104 if (edit
->mark1
!= edit
->mark2
)
4105 edit_move_block_to_left (edit
);
4107 case CK_BlockShiftRight
:
4108 if (edit
->mark1
!= edit
->mark2
)
4109 edit_move_block_to_right (edit
);
4112 edit_copy_to_X_buf_cmd (edit
);
4115 edit_cut_to_X_buf_cmd (edit
);
4118 /* if non persistent selection and text selected */
4119 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
4120 edit_block_delete_cmd (edit
);
4121 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4122 edit_insert_over (edit
);
4123 edit_paste_from_X_buf_cmd (edit
);
4124 if (!option_persistent_selections
&& edit
->mark2
>= 0)
4126 if (edit
->column_highlight
)
4127 edit_push_undo_action (edit
, COLUMN_ON
);
4128 edit
->column_highlight
= 0;
4129 edit_mark_cmd (edit
, TRUE
);
4133 edit_paste_from_history (edit
);
4137 edit_save_as_cmd (edit
);
4140 edit_save_confirm_cmd (edit
);
4143 edit_save_block_cmd (edit
);
4146 edit_insert_file_cmd (edit
);
4150 edit_load_back_cmd (edit
);
4153 edit_load_forward_cmd (edit
);
4156 case CK_SyntaxChoose
:
4157 edit_syntax_dialog (edit
);
4161 edit_search_cmd (edit
, FALSE
);
4163 case CK_SearchContinue
:
4164 edit_search_cmd (edit
, TRUE
);
4167 edit_replace_cmd (edit
, 0);
4169 case CK_ReplaceContinue
:
4170 edit_replace_cmd (edit
, 1);
4173 /* if text marked shift block */
4174 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
4175 edit_move_block_to_left (edit
);
4177 edit_complete_word_cmd (edit
);
4180 edit_get_match_keyword_cmd (edit
);
4184 case CK_SpellCheckCurrentWord
:
4185 edit_suggest_current_word (edit
);
4188 edit_spellcheck_file (edit
);
4190 case CK_SpellCheckSelectLang
:
4191 edit_set_spell_lang ();
4198 /* fool gcc to prevent a Y2K warning */
4199 char time_format
[] = "_c";
4200 time_format
[0] = '%';
4202 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4203 edit_print_string (edit
, s
);
4204 edit
->force
|= REDRAW_PAGE
;
4208 edit_goto_cmd (edit
);
4210 case CK_ParagraphFormat
:
4211 format_paragraph (edit
, 1);
4212 edit
->force
|= REDRAW_PAGE
;
4214 case CK_MacroDelete
:
4215 edit_delete_macro_cmd (edit
);
4217 case CK_MatchBracket
:
4218 edit_goto_matching_bracket (edit
);
4221 user_menu (edit
, NULL
, -1);
4224 edit_sort_cmd (edit
);
4226 case CK_ExternalCommand
:
4227 edit_ext_cmd (edit
);
4230 edit_mail_dialog (edit
);
4233 case CK_SelectCodepage
:
4234 edit_select_codepage_cmd (edit
);
4237 case CK_InsertLiteral
:
4238 edit_insert_literal_cmd (edit
);
4240 case CK_MacroStartStopRecord
:
4241 edit_begin_end_macro_cmd (edit
);
4243 case CK_RepeatStartStopRecord
:
4244 edit_begin_end_repeat_cmd (edit
);
4246 case CK_ExtendedKeyMap
:
4247 edit
->extmod
= TRUE
;
4254 if ((command
/ CK_PipeBlock (0)) == 1)
4255 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
4257 /* keys which must set the col position, and the search vars */
4261 case CK_SearchContinue
:
4263 case CK_ReplaceContinue
:
4265 edit
->prev_col
= edit_get_col (edit
);
4269 case CK_MarkColumnUp
:
4272 case CK_MarkColumnDown
:
4275 case CK_MarkColumnPageUp
:
4277 case CK_MarkPageDown
:
4278 case CK_MarkColumnPageDown
:
4280 case CK_MarkToFileBegin
:
4282 case CK_MarkToFileEnd
:
4283 case CK_ParagraphUp
:
4284 case CK_MarkParagraphUp
:
4285 case CK_MarkColumnParagraphUp
:
4286 case CK_ParagraphDown
:
4287 case CK_MarkParagraphDown
:
4288 case CK_MarkColumnParagraphDown
:
4290 case CK_MarkScrollUp
:
4291 case CK_MarkColumnScrollUp
:
4293 case CK_MarkScrollDown
:
4294 case CK_MarkColumnScrollDown
:
4295 edit
->search_start
= edit
->curs1
;
4296 edit
->found_len
= 0;
4299 edit
->found_len
= 0;
4300 edit
->prev_col
= edit_get_col (edit
);
4301 edit
->search_start
= edit
->curs1
;
4303 edit_find_bracket (edit
);
4305 if (option_auto_para_formatting
)
4311 case CK_DeleteToWordBegin
:
4312 case CK_DeleteToWordEnd
:
4313 case CK_DeleteToHome
:
4314 case CK_DeleteToEnd
:
4315 format_paragraph (edit
, 0);
4316 edit
->force
|= REDRAW_PAGE
;
4321 /* --------------------------------------------------------------------------------------------- */
4324 edit_stack_init (void)
4326 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4328 edit_history_moveto
[edit_stack_iterator
].filename_vpath
= NULL
;
4329 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4332 edit_stack_iterator
= 0;
4335 /* --------------------------------------------------------------------------------------------- */
4338 edit_stack_free (void)
4340 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4341 vfs_path_free (edit_history_moveto
[edit_stack_iterator
].filename_vpath
);
4344 /* --------------------------------------------------------------------------------------------- */
4348 edit_move_up (WEdit
* edit
, long i
, gboolean do_scroll
)
4350 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4353 /* --------------------------------------------------------------------------------------------- */
4357 edit_move_down (WEdit
* edit
, long i
, gboolean do_scroll
)
4359 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4362 /* --------------------------------------------------------------------------------------------- */