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
6 The Free Software Foundation, Inc.
10 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
11 Andrew Borodin <aborodin@vmail.ru> 2012.
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 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
104 char *option_backup_ext
= NULL
;
106 unsigned int edit_stack_iterator
= 0;
107 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
108 /* magic sequense for say than block is vertical */
109 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
111 /*** file scope macro definitions ****************************************************************/
113 #define TEMP_BUF_LEN 1024
115 #define space_width 1
117 /*** file scope type declarations ****************************************************************/
119 /*** file scope variables ************************************************************************/
121 /* detecting an error on save is easy: just check if every byte has been written. */
122 /* detecting an error on read, is not so easy 'cos there is not way to tell
123 whether you read everything or not. */
124 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
125 static const struct edit_filters
127 const char *read
, *write
, *extension
;
131 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
132 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
133 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
134 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
135 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
139 static off_t last_bracket
= -1;
141 /*** file scope functions ************************************************************************/
142 /* --------------------------------------------------------------------------------------------- */
146 * here's a quick sketch of the layout: (don't run this through indent.)
148 * (b1 is buffers1 and b2 is buffers2)
151 * \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
152 * ______________________________________|______________________________________
154 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
155 * |-> |-> |-> |-> |-> |-> |
157 * _<------------------------->|<----------------->_
158 * WEdit->curs2 | WEdit->curs1
163 * file end|||file beginning
172 /* --------------------------------------------------------------------------------------------- */
174 * Initialize the buffers for an empty files.
178 edit_init_buffers (WEdit
* edit
)
182 for (j
= 0; j
<= MAXBUFF
; j
++)
184 edit
->buffers1
[j
] = NULL
;
185 edit
->buffers2
[j
] = NULL
;
190 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
193 /* --------------------------------------------------------------------------------------------- */
196 * Load file OR text into buffers. Set cursor to the beginning of file.
198 * @return FALSE on error.
202 edit_load_file_fast (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
206 gboolean ret
= FALSE
;
208 edit
->curs2
= edit
->last_byte
;
209 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
211 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
214 gchar
*errmsg
, *filename
;
216 filename
= vfs_path_to_str (filename_vpath
);
217 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
219 edit_error_dialog (_("Error"), errmsg
);
224 if (!edit
->buffers2
[buf2
])
225 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
230 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
231 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
234 for (buf
= buf2
- 1; buf
>= 0; buf
--)
236 /* edit->buffers2[0] is already allocated */
237 if (!edit
->buffers2
[buf
])
238 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
239 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
248 gchar
*errmsg
, *filename
;
250 filename
= vfs_path_to_str (filename_vpath
);
251 errmsg
= g_strdup_printf (_("Error reading %s"), filename
);
253 edit_error_dialog (_("Error"), errmsg
);
260 /* --------------------------------------------------------------------------------------------- */
261 /** Return index of the filter or -1 is there is no appropriate filter */
264 edit_find_filter (const vfs_path_t
* filename_vpath
)
269 if (filename_vpath
== NULL
)
272 filename
= vfs_path_to_str (filename_vpath
);
273 l
= strlen (filename
);
274 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
276 e
= strlen (all_filters
[i
].extension
);
278 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
288 /* --------------------------------------------------------------------------------------------- */
291 edit_get_filter (const vfs_path_t
* filename_vpath
)
294 char *p
, *quoted_name
, *filename
;
296 i
= edit_find_filter (filename_vpath
);
300 filename
= vfs_path_to_str (filename_vpath
);
301 quoted_name
= name_quote (filename
, 0);
303 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
304 g_free (quoted_name
);
308 /* --------------------------------------------------------------------------------------------- */
311 edit_insert_stream (WEdit
* edit
, FILE * f
)
315 while ((c
= fgetc (f
)) >= 0)
317 edit_insert (edit
, c
);
323 /* --------------------------------------------------------------------------------------------- */
325 * Open file and create it if necessary.
327 * @param edit editor object
328 * @param filename_vpath file name
329 * @param st buffer for store stat info
330 * @return TRUE for success, FALSE for error.
334 check_file_access (WEdit
* edit
, const vfs_path_t
* filename_vpath
, struct stat
*st
)
337 gchar
*errmsg
= NULL
;
339 /* Try opening an existing file */
340 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
344 * Try creating the file. O_EXCL prevents following broken links
345 * and opening existing files.
347 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
352 filename
= vfs_path_to_str (filename_vpath
);
353 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
358 /* New file, delete it if it's not modified or saved */
359 edit
->delete_file
= 1;
362 /* Check what we have opened */
363 if (mc_fstat (file
, st
) < 0)
367 filename
= vfs_path_to_str (filename_vpath
);
368 errmsg
= g_strdup_printf (_("Cannot get size/permissions for %s"), filename
);
373 /* We want to open regular files only */
374 if (!S_ISREG (st
->st_mode
))
378 filename
= vfs_path_to_str (filename_vpath
);
379 errmsg
= g_strdup_printf (_("\"%s\" is not a regular file"), filename
);
385 * Don't delete non-empty files.
386 * O_EXCL should prevent it, but let's be on the safe side.
389 edit
->delete_file
= 0;
391 if (st
->st_size
>= SIZE_LIMIT
)
395 filename
= vfs_path_to_str (filename_vpath
);
396 errmsg
= g_strdup_printf (_("File \"%s\" is too large"), filename
);
401 (void) mc_close (file
);
405 edit_error_dialog (_("Error"), errmsg
);
412 /* --------------------------------------------------------------------------------------------- */
415 * Open the file and load it into the buffers, either directly or using
416 * a filter. Return TRUE on success, FALSE on error.
418 * Fast loading (edit_load_file_fast) is used when the file size is
419 * known. In this case the data is read into the buffers by blocks.
420 * If the file size is not known, the data is loaded byte by byte in
423 * @param edit editor object
424 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
427 edit_load_file (WEdit
* edit
)
429 gboolean fast_load
= TRUE
;
431 /* Cannot do fast load if a filter is used */
432 if (edit_find_filter (edit
->filename_vpath
) >= 0)
436 * FIXME: line end translation should disable fast loading as well
437 * Consider doing fseek() to the end and ftell() for the real size.
439 if (edit
->filename_vpath
!= NULL
)
442 * VFS may report file size incorrectly, and slow load is not a big
443 * deal considering overhead in VFS.
445 if (!vfs_file_is_local (edit
->filename_vpath
))
448 /* If we are dealing with a real file, check that it exists */
449 if (!check_file_access (edit
, edit
->filename_vpath
, &edit
->stat1
))
457 /* nothing to load */
461 edit_init_buffers (edit
);
465 edit
->last_byte
= edit
->stat1
.st_size
;
466 edit_load_file_fast (edit
, edit
->filename_vpath
);
467 /* If fast load was used, the number of lines wasn't calculated */
468 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
473 if (edit
->filename_vpath
!= NULL
474 && *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) != '\0')
476 edit
->undo_stack_disable
= 1;
477 if (edit_insert_file (edit
, edit
->filename_vpath
) < 0)
482 edit
->undo_stack_disable
= 0;
489 /* --------------------------------------------------------------------------------------------- */
490 /** Restore saved cursor position in the file */
493 edit_load_position (WEdit
* edit
)
498 if (edit
->filename_vpath
== NULL
499 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
502 load_file_position (edit
->filename_vpath
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
506 edit_move_to_line (edit
, line
- 1);
507 edit
->prev_col
= column
;
511 edit_cursor_move (edit
, offset
);
512 line
= edit
->curs_line
;
513 edit
->search_start
= edit
->curs1
;
516 book_mark_restore (edit
, BOOK_MARK_COLOR
);
518 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
519 edit_move_display (edit
, line
- (WIDGET (edit
)->lines
/ 2));
522 /* --------------------------------------------------------------------------------------------- */
523 /** Save cursor position in the file */
526 edit_save_position (WEdit
* edit
)
528 if (edit
->filename_vpath
== NULL
529 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
532 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
533 save_file_position (edit
->filename_vpath
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
,
534 edit
->serialized_bookmarks
);
535 edit
->serialized_bookmarks
= NULL
;
538 /* --------------------------------------------------------------------------------------------- */
539 /** Clean the WEdit stricture except the widget part */
542 edit_purge_widget (WEdit
* edit
)
544 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
545 char *start
= (char *) edit
+ sizeof (Widget
);
546 memset (start
, 0, len
);
549 /* --------------------------------------------------------------------------------------------- */
552 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
553 then the file should be as it was when he loaded up. Then set edit->modified to 0.
557 edit_pop_undo_action (WEdit
* edit
)
560 unsigned long sp
= edit
->undo_stack_pointer
;
562 if (sp
== edit
->undo_stack_bottom
)
565 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
566 c
= edit
->undo_stack
[sp
];
569 /* edit->undo_stack[sp] = '@'; */
570 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
574 if (sp
== edit
->undo_stack_bottom
)
577 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
578 if (edit
->undo_stack
[sp
] == -2)
580 /* edit->undo_stack[sp] = '@'; */
581 edit
->undo_stack_pointer
= sp
;
584 edit
->undo_stack
[sp
]++;
590 edit_pop_redo_action (WEdit
* edit
)
593 unsigned long sp
= edit
->redo_stack_pointer
;
595 if (sp
== edit
->redo_stack_bottom
)
598 sp
= (sp
- 1) & edit
->redo_stack_size_mask
;
599 c
= edit
->redo_stack
[sp
];
602 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
606 if (sp
== edit
->redo_stack_bottom
)
609 c
= edit
->redo_stack
[(sp
- 1) & edit
->redo_stack_size_mask
];
610 if (edit
->redo_stack
[sp
] == -2)
611 edit
->redo_stack_pointer
= sp
;
613 edit
->redo_stack
[sp
]++;
619 get_prev_undo_action (WEdit
* edit
)
622 unsigned long sp
= edit
->undo_stack_pointer
;
624 if (sp
== edit
->undo_stack_bottom
)
627 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
628 c
= edit
->undo_stack
[sp
];
632 if (sp
== edit
->undo_stack_bottom
)
635 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
639 /* --------------------------------------------------------------------------------------------- */
640 /** is called whenever a modification is made by one of the four routines below */
643 edit_modification (WEdit
* edit
)
645 edit
->caches_valid
= FALSE
;
647 /* raise lock when file modified */
648 if (!edit
->modified
&& !edit
->delete_file
)
649 edit
->locked
= lock_file (edit
->filename_vpath
);
653 /* --------------------------------------------------------------------------------------------- */
657 edit_get_byte_ptr (const WEdit
* edit
, off_t byte_index
)
659 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
662 if (byte_index
>= edit
->curs1
)
666 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
667 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
668 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
671 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
672 (byte_index
& M_EDIT_BUF_SIZE
));
676 /* --------------------------------------------------------------------------------------------- */
680 edit_get_prev_utf (const WEdit
* edit
, off_t byte_index
, int *char_width
)
683 gchar utf8_buf
[3 * UTF8_CHAR_LEN
+ 1];
685 gchar
*cursor_buf_ptr
;
687 if (byte_index
> (edit
->curs1
+ edit
->curs2
) || byte_index
<= 0)
693 for (i
= 0; i
< (3 * UTF8_CHAR_LEN
); i
++)
694 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
- (2 * UTF8_CHAR_LEN
));
695 utf8_buf
[3 * UTF8_CHAR_LEN
] = '\0';
697 cursor_buf_ptr
= utf8_buf
+ (2 * UTF8_CHAR_LEN
);
698 str
= g_utf8_find_prev_char (utf8_buf
, cursor_buf_ptr
);
700 if (str
== NULL
|| g_utf8_next_char (str
) != cursor_buf_ptr
)
703 return *(cursor_buf_ptr
- 1);
707 res
= g_utf8_get_char_validated (str
, -1);
712 return *(cursor_buf_ptr
- 1);
716 *char_width
= cursor_buf_ptr
- str
;
723 /* --------------------------------------------------------------------------------------------- */
724 /* high level cursor movement commands */
725 /* --------------------------------------------------------------------------------------------- */
726 /** check whether cursor is in indent part of line
728 * @param edit editor object
730 * @return TRUE if cursor is in indent, FALSE otherwise
734 is_in_indent (const WEdit
* edit
)
738 for (p
= edit_bol (edit
, edit
->curs1
); p
< edit
->curs1
; p
++)
739 if (strchr (" \t", edit_get_byte (edit
, p
)) == NULL
)
745 /* --------------------------------------------------------------------------------------------- */
746 /** check whether line in editor is blank or not
748 * @param edit editor object
749 * @param offset position in file
751 * @return TRUE if line in blank, FALSE otherwise
755 is_blank (const WEdit
* edit
, off_t offset
)
760 s
= edit_bol (edit
, offset
);
761 f
= edit_eol (edit
, offset
) - 1;
764 c
= edit_get_byte (edit
, s
++);
771 /* --------------------------------------------------------------------------------------------- */
772 /** returns the offset of line i */
775 edit_find_line (WEdit
* edit
, long line
)
778 long m
= 2000000000; /* what is the magic number? */
780 if (!edit
->caches_valid
)
782 memset (edit
->line_numbers
, 0, sizeof (edit
->line_numbers
));
783 memset (edit
->line_offsets
, 0, sizeof (edit
->line_offsets
));
784 /* three offsets that we *know* are line 0 at 0 and these two: */
785 edit
->line_numbers
[1] = edit
->curs_line
;
786 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
787 edit
->line_numbers
[2] = edit
->total_lines
;
788 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
789 edit
->caches_valid
= TRUE
;
791 if (line
>= edit
->total_lines
)
792 return edit
->line_offsets
[2];
795 /* find the closest known point */
796 for (i
= 0; i
< N_LINE_CACHES
; i
++)
800 n
= abs (edit
->line_numbers
[i
] - line
);
808 return edit
->line_offsets
[j
]; /* know the offset exactly */
809 if (m
== 1 && j
>= 3)
810 i
= j
; /* one line different - caller might be looping, so stay in this cache */
812 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
813 if (line
> edit
->line_numbers
[j
])
814 edit
->line_offsets
[i
] =
815 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
817 edit
->line_offsets
[i
] =
818 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
819 edit
->line_numbers
[i
] = line
;
820 return edit
->line_offsets
[i
];
823 /* --------------------------------------------------------------------------------------------- */
824 /** moves up until a blank line is reached, or until just
825 before a non-blank line is reached */
828 edit_move_up_paragraph (WEdit
* edit
, gboolean do_scroll
)
832 if (edit
->curs_line
> 1)
834 if (!edit_line_is_blank (edit
, edit
->curs_line
))
836 for (i
= edit
->curs_line
- 1; i
!= 0; i
--)
837 if (edit_line_is_blank (edit
, i
))
840 else if (edit_line_is_blank (edit
, edit
->curs_line
- 1))
842 for (i
= edit
->curs_line
- 1; i
!= 0; i
--)
843 if (!edit_line_is_blank (edit
, i
))
851 for (i
= edit
->curs_line
- 1; i
!= 0; i
--)
852 if (edit_line_is_blank (edit
, i
))
857 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
860 /* --------------------------------------------------------------------------------------------- */
861 /** moves down until a blank line is reached, or until just
862 before a non-blank line is reached */
865 edit_move_down_paragraph (WEdit
* edit
, gboolean do_scroll
)
869 if (edit
->curs_line
>= edit
->total_lines
- 1)
870 i
= edit
->total_lines
;
871 else if (!edit_line_is_blank (edit
, edit
->curs_line
))
873 for (i
= edit
->curs_line
+ 1; i
!= 0; i
++)
874 if (edit_line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
877 else if (edit_line_is_blank (edit
, edit
->curs_line
+ 1))
879 for (i
= edit
->curs_line
+ 1; i
!= 0; i
++)
880 if (!edit_line_is_blank (edit
, i
) || i
> edit
->total_lines
)
888 for (i
= edit
->curs_line
+ 1; i
!= 0; i
++)
889 if (edit_line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
892 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
895 /* --------------------------------------------------------------------------------------------- */
898 edit_begin_page (WEdit
* edit
)
900 edit_update_curs_row (edit
);
901 edit_move_up (edit
, edit
->curs_row
, 0);
904 /* --------------------------------------------------------------------------------------------- */
907 edit_end_page (WEdit
* edit
)
909 edit_update_curs_row (edit
);
910 edit_move_down (edit
, WIDGET (edit
)->lines
- edit
->curs_row
- 1, 0);
914 /* --------------------------------------------------------------------------------------------- */
915 /** goto beginning of text */
918 edit_move_to_top (WEdit
* edit
)
922 edit_cursor_move (edit
, -edit
->curs1
);
923 edit_move_to_prev_col (edit
, 0);
924 edit
->force
|= REDRAW_PAGE
;
925 edit
->search_start
= 0;
926 edit_update_curs_row (edit
);
931 /* --------------------------------------------------------------------------------------------- */
932 /** goto end of text */
935 edit_move_to_bottom (WEdit
* edit
)
937 if (edit
->curs_line
< edit
->total_lines
)
939 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
940 edit
->start_display
= edit
->last_byte
;
941 edit
->start_line
= edit
->total_lines
;
942 edit_scroll_upward (edit
, WIDGET (edit
)->lines
- 1);
943 edit
->force
|= REDRAW_PAGE
;
947 /* --------------------------------------------------------------------------------------------- */
948 /** goto beginning of line */
951 edit_cursor_to_bol (WEdit
* edit
)
953 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
954 edit
->search_start
= edit
->curs1
;
955 edit
->prev_col
= edit_get_col (edit
);
959 /* --------------------------------------------------------------------------------------------- */
960 /** goto end of line */
963 edit_cursor_to_eol (WEdit
* edit
)
965 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
966 edit
->search_start
= edit
->curs1
;
967 edit
->prev_col
= edit_get_col (edit
);
971 /* --------------------------------------------------------------------------------------------- */
978 const char option_chars_move_whole_word
[] =
979 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
985 if (*option_chars_move_whole_word
== '!')
989 if (g_ascii_isupper ((gchar
) c
))
991 else if (g_ascii_islower ((gchar
) c
))
993 else if (g_ascii_isalpha (c
))
995 else if (isdigit (c
))
997 else if (isspace (c
))
999 q
= strchr (option_chars_move_whole_word
, c
);
1001 return 0xFFFFFFFFUL
;
1004 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1009 while ((q
= strchr (q
+ 1, c
)));
1013 /* --------------------------------------------------------------------------------------------- */
1016 edit_left_word_move (WEdit
* edit
, int s
)
1022 if (edit
->column_highlight
1023 && edit
->mark1
!= edit
->mark2
1024 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1026 edit_cursor_move (edit
, -1);
1027 if (edit
->curs1
== 0)
1029 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1030 c2
= edit_get_byte (edit
, edit
->curs1
);
1031 if (c1
== '\n' || c2
== '\n')
1033 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1035 if (isspace (c1
) && !isspace (c2
))
1037 if (s
!= 0 && !isspace (c1
) && isspace (c2
))
1042 /* --------------------------------------------------------------------------------------------- */
1045 edit_left_word_move_cmd (WEdit
* edit
)
1047 edit_left_word_move (edit
, 0);
1048 edit
->force
|= REDRAW_PAGE
;
1051 /* --------------------------------------------------------------------------------------------- */
1054 edit_right_word_move (WEdit
* edit
, int s
)
1060 if (edit
->column_highlight
1061 && edit
->mark1
!= edit
->mark2
1062 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
1064 edit_cursor_move (edit
, 1);
1065 if (edit
->curs1
>= edit
->last_byte
)
1067 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1068 c2
= edit_get_byte (edit
, edit
->curs1
);
1069 if (c1
== '\n' || c2
== '\n')
1071 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1073 if (isspace (c1
) && !isspace (c2
))
1075 if (s
!= 0 && !isspace (c1
) && isspace (c2
))
1080 /* --------------------------------------------------------------------------------------------- */
1083 edit_right_word_move_cmd (WEdit
* edit
)
1085 edit_right_word_move (edit
, 0);
1086 edit
->force
|= REDRAW_PAGE
;
1089 /* --------------------------------------------------------------------------------------------- */
1092 edit_right_char_move_cmd (WEdit
* edit
)
1099 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
1106 c
= edit_get_byte (edit
, edit
->curs1
);
1108 if (option_cursor_beyond_eol
&& c
== '\n')
1114 edit_cursor_move (edit
, cw
);
1118 /* --------------------------------------------------------------------------------------------- */
1121 edit_left_char_move_cmd (WEdit
* edit
)
1124 if (edit
->column_highlight
1125 && option_cursor_beyond_eol
1126 && edit
->mark1
!= edit
->mark2
1127 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1132 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1137 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1143 edit_cursor_move (edit
, -cw
);
1147 /* --------------------------------------------------------------------------------------------- */
1148 /** Up or down cursor moving.
1149 direction = TRUE - move up
1154 edit_move_updown (WEdit
* edit
, long lines
, gboolean do_scroll
, gboolean direction
)
1157 long l
= direction
? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
1166 edit
->force
|= REDRAW_PAGE
;
1170 edit_scroll_upward (edit
, lines
);
1172 edit_scroll_downward (edit
, lines
);
1174 p
= edit_bol (edit
, edit
->curs1
);
1176 p
= direction
? edit_move_backward (edit
, p
, lines
) : edit_move_forward (edit
, p
, lines
, 0);
1178 edit_cursor_move (edit
, p
- edit
->curs1
);
1180 edit_move_to_prev_col (edit
, p
);
1182 /* search start of current multibyte char (like CJK) */
1183 if (edit
->curs1
+ 1 < edit
->last_byte
)
1185 edit_right_char_move_cmd (edit
);
1186 edit_left_char_move_cmd (edit
);
1189 edit
->search_start
= edit
->curs1
;
1190 edit
->found_len
= 0;
1193 /* --------------------------------------------------------------------------------------------- */
1196 edit_right_delete_word (WEdit
* edit
)
1198 while (edit
->curs1
< edit
->last_byte
)
1202 c1
= edit_delete (edit
, TRUE
);
1203 c2
= edit_get_byte (edit
, edit
->curs1
);
1204 if (c1
== '\n' || c2
== '\n')
1206 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1208 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1213 /* --------------------------------------------------------------------------------------------- */
1216 edit_left_delete_word (WEdit
* edit
)
1218 while (edit
->curs1
> 0)
1222 c1
= edit_backspace (edit
, TRUE
);
1223 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1224 if (c1
== '\n' || c2
== '\n')
1226 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1228 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1233 /* --------------------------------------------------------------------------------------------- */
1235 the start column position is not recorded, and hence does not
1236 undo as it happed. But who would notice.
1240 edit_do_undo (WEdit
* edit
)
1245 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1247 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1254 edit_cursor_move (edit
, 1);
1257 edit_cursor_move (edit
, -1);
1261 edit_backspace (edit
, TRUE
);
1265 edit_delete (edit
, TRUE
);
1268 edit
->column_highlight
= 1;
1271 edit
->column_highlight
= 0;
1274 if (ac
>= 256 && ac
< 512)
1275 edit_insert_ahead (edit
, ac
- 256);
1276 if (ac
>= 0 && ac
< 256)
1277 edit_insert (edit
, ac
);
1279 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1281 edit
->mark1
= ac
- MARK_1
;
1283 (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1285 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1287 edit
->mark2
= ac
- MARK_2
;
1289 (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1291 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1293 edit
->end_mark_curs
= ac
- MARK_CURS
;
1296 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1299 if (edit
->start_display
> ac
- KEY_PRESS
)
1301 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1302 edit
->force
|= REDRAW_PAGE
;
1304 else if (edit
->start_display
< ac
- KEY_PRESS
)
1306 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1307 edit
->force
|= REDRAW_PAGE
;
1309 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1310 edit_update_curs_row (edit
);
1313 edit
->undo_stack_disable
= 0;
1316 /* --------------------------------------------------------------------------------------------- */
1319 edit_do_redo (WEdit
* edit
)
1324 if (edit
->redo_stack_reset
)
1328 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1335 edit_cursor_move (edit
, 1);
1338 edit_cursor_move (edit
, -1);
1341 edit_backspace (edit
, TRUE
);
1344 edit_delete (edit
, TRUE
);
1347 edit
->column_highlight
= 1;
1350 edit
->column_highlight
= 0;
1353 if (ac
>= 256 && ac
< 512)
1354 edit_insert_ahead (edit
, ac
- 256);
1355 if (ac
>= 0 && ac
< 256)
1356 edit_insert (edit
, ac
);
1358 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1360 edit
->mark1
= ac
- MARK_1
;
1362 (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1364 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1366 edit
->mark2
= ac
- MARK_2
;
1368 (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1370 /* more than one pop usually means something big */
1372 edit
->force
|= REDRAW_PAGE
;
1375 if (edit
->start_display
> ac
- KEY_PRESS
)
1377 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1378 edit
->force
|= REDRAW_PAGE
;
1380 else if (edit
->start_display
< ac
- KEY_PRESS
)
1382 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1383 edit
->force
|= REDRAW_PAGE
;
1385 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1386 edit_update_curs_row (edit
);
1392 /* --------------------------------------------------------------------------------------------- */
1395 edit_group_undo (WEdit
* edit
)
1397 long ac
= KEY_PRESS
;
1398 long cur_ac
= KEY_PRESS
;
1399 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1401 cur_ac
= get_prev_undo_action (edit
);
1402 edit_do_undo (edit
);
1403 ac
= get_prev_undo_action (edit
);
1404 /* exit from cycle if option_group_undo is not set,
1405 * and make single UNDO operation
1407 if (!option_group_undo
)
1412 /* --------------------------------------------------------------------------------------------- */
1415 edit_delete_to_line_end (WEdit
* edit
)
1417 while (edit_get_byte (edit
, edit
->curs1
) != '\n' && edit
->curs2
!= 0)
1418 edit_delete (edit
, TRUE
);
1421 /* --------------------------------------------------------------------------------------------- */
1424 edit_delete_to_line_begin (WEdit
* edit
)
1426 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
!= 0)
1427 edit_backspace (edit
, TRUE
);
1430 /* --------------------------------------------------------------------------------------------- */
1433 is_aligned_on_a_tab (WEdit
* edit
)
1437 edit_update_curs_col (edit
);
1438 curs_col
= edit
->curs_col
% (TAB_SIZE
* space_width
);
1439 return (curs_col
== 0 || curs_col
== (HALF_TAB_SIZE
* space_width
));
1442 /* --------------------------------------------------------------------------------------------- */
1445 right_of_four_spaces (WEdit
* edit
)
1449 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1450 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1452 return (ch
== ' ' && is_aligned_on_a_tab (edit
));
1455 /* --------------------------------------------------------------------------------------------- */
1458 left_of_four_spaces (WEdit
* edit
)
1462 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1463 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1465 return (ch
== ' ' && is_aligned_on_a_tab (edit
));
1468 /* --------------------------------------------------------------------------------------------- */
1471 edit_auto_indent (WEdit
* edit
)
1477 /* use the previous line as a template */
1478 p
= edit_move_backward (edit
, p
, 1);
1479 /* copy the leading whitespace of the line */
1481 { /* no range check - the line _is_ \n-terminated */
1482 c
= edit_get_byte (edit
, p
++);
1483 if (c
!= ' ' && c
!= '\t')
1485 edit_insert (edit
, c
);
1489 /* --------------------------------------------------------------------------------------------- */
1492 edit_double_newline (WEdit
* edit
)
1494 edit_insert (edit
, '\n');
1495 if (edit_get_byte (edit
, edit
->curs1
) == '\n' || edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1497 edit
->force
|= REDRAW_PAGE
;
1498 edit_insert (edit
, '\n');
1501 /* --------------------------------------------------------------------------------------------- */
1504 insert_spaces_tab (WEdit
* edit
, gboolean half
)
1508 edit_update_curs_col (edit
);
1509 i
= option_tab_spacing
* space_width
;
1512 i
= ((edit
->curs_col
/ i
) + 1) * i
- edit
->curs_col
;
1515 edit_insert (edit
, ' ');
1520 /* --------------------------------------------------------------------------------------------- */
1523 edit_tab_cmd (WEdit
* edit
)
1525 if (option_fake_half_tabs
&& is_in_indent (edit
))
1527 /* insert a half tab (usually four spaces) unless there is a
1528 half tab already behind, then delete it and insert a
1530 if (option_fill_tabs_with_spaces
|| !right_of_four_spaces (edit
))
1531 insert_spaces_tab (edit
, TRUE
);
1536 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1537 edit_backspace (edit
, TRUE
);
1538 edit_insert (edit
, '\t');
1541 else if (option_fill_tabs_with_spaces
)
1542 insert_spaces_tab (edit
, FALSE
);
1544 edit_insert (edit
, '\t');
1547 /* --------------------------------------------------------------------------------------------- */
1550 check_and_wrap_line (WEdit
* edit
)
1555 if (!option_typewriter_wrap
)
1557 edit_update_curs_col (edit
);
1558 if (edit
->curs_col
< option_word_wrap_line_length
)
1564 c
= edit_get_byte (edit
, curs
);
1565 if (c
== '\n' || curs
<= 0)
1567 edit_insert (edit
, '\n');
1570 if (c
== ' ' || c
== '\t')
1572 off_t current
= edit
->curs1
;
1573 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
1574 edit_insert (edit
, '\n');
1575 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
1581 /* --------------------------------------------------------------------------------------------- */
1582 /** this find the matching bracket in either direction, and sets edit->bracket
1584 * @param edit editor object
1585 * @param in_screen seach only on the current screen
1586 * @param furthest_bracket_search count of the bytes for search
1588 * @return position of the found bracket (-1 if no match)
1592 edit_get_bracket (WEdit
* edit
, gboolean in_screen
, unsigned long furthest_bracket_search
)
1594 const char *const b
= "{}{[][()(", *p
;
1595 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
1596 unsigned long j
= 0;
1598 edit_update_curs_row (edit
);
1599 c
= edit_get_byte (edit
, edit
->curs1
);
1602 if (!furthest_bracket_search
)
1603 furthest_bracket_search
--;
1604 /* not on a bracket at all */
1607 /* the matching bracket */
1609 /* going left or right? */
1610 if (strchr ("{[(", c
))
1612 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
1614 /* out of buffer? */
1615 if (q
>= edit
->last_byte
|| q
< 0)
1617 a
= edit_get_byte (edit
, q
);
1618 /* don't want to eat CPU */
1619 if (j
++ > furthest_bracket_search
)
1621 /* out of screen? */
1624 if (q
< edit
->start_display
)
1626 /* count lines if searching downward */
1627 if (inc
> 0 && a
== '\n')
1628 if (n
++ >= WIDGET (edit
)->lines
- edit
->curs_row
) /* out of screen */
1631 /* count bracket depth */
1632 i
+= (a
== c
) - (a
== d
);
1633 /* return if bracket depth is zero */
1641 /* --------------------------------------------------------------------------------------------- */
1644 edit_goto_matching_bracket (WEdit
* edit
)
1648 q
= edit_get_bracket (edit
, 0, 0);
1651 edit
->bracket
= edit
->curs1
;
1652 edit
->force
|= REDRAW_PAGE
;
1653 edit_cursor_move (edit
, q
- edit
->curs1
);
1657 /* --------------------------------------------------------------------------------------------- */
1660 edit_move_block_to_right (WEdit
* edit
)
1662 off_t start_mark
, end_mark
;
1663 long cur_bol
, start_bol
;
1665 if (eval_marks (edit
, &start_mark
, &end_mark
))
1668 start_bol
= edit_bol (edit
, start_mark
);
1669 cur_bol
= edit_bol (edit
, end_mark
- 1);
1673 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1674 if (!edit_line_is_blank (edit
, edit
->curs_line
))
1676 if (option_fill_tabs_with_spaces
)
1677 insert_spaces_tab (edit
, option_fake_half_tabs
);
1679 edit_insert (edit
, '\t');
1680 edit_cursor_move (edit
, edit_bol (edit
, cur_bol
) - edit
->curs1
);
1686 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1688 while (cur_bol
>= start_bol
);
1690 edit
->force
|= REDRAW_PAGE
;
1693 /* --------------------------------------------------------------------------------------------- */
1696 edit_move_block_to_left (WEdit
* edit
)
1698 off_t start_mark
, end_mark
;
1699 off_t cur_bol
, start_bol
;
1702 if (eval_marks (edit
, &start_mark
, &end_mark
))
1705 start_bol
= edit_bol (edit
, start_mark
);
1706 cur_bol
= edit_bol (edit
, end_mark
- 1);
1713 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1715 if (option_fake_half_tabs
)
1716 del_tab_width
= HALF_TAB_SIZE
;
1718 del_tab_width
= option_tab_spacing
;
1720 next_char
= edit_get_byte (edit
, edit
->curs1
);
1721 if (next_char
== '\t')
1722 edit_delete (edit
, TRUE
);
1723 else if (next_char
== ' ')
1724 for (i
= 1; i
<= del_tab_width
; i
++)
1726 if (next_char
== ' ')
1727 edit_delete (edit
, TRUE
);
1728 next_char
= edit_get_byte (edit
, edit
->curs1
);
1734 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1736 while (cur_bol
>= start_bol
);
1738 edit
->force
|= REDRAW_PAGE
;
1741 /* --------------------------------------------------------------------------------------------- */
1743 * prints at the cursor
1744 * @return number of chars printed
1748 edit_print_string (WEdit
* e
, const char *s
)
1752 while (s
[i
] != '\0')
1753 edit_execute_cmd (e
, CK_InsertChar
, (unsigned char) s
[i
++]);
1754 e
->force
|= REDRAW_COMPLETELY
;
1755 edit_update_screen (e
);
1759 /* --------------------------------------------------------------------------------------------- */
1760 /*** public functions ****************************************************************************/
1761 /* --------------------------------------------------------------------------------------------- */
1763 /** User edit menu, like user menu (F2) but only in editor. */
1766 user_menu (WEdit
* edit
, const char *menu_file
, int selected_entry
)
1771 off_t start_mark
, end_mark
;
1773 vfs_path_t
*block_file_vpath
;
1775 block_file
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
1776 block_file_vpath
= vfs_path_from_str (block_file
);
1778 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
1780 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1782 /* run shell scripts from menu */
1783 if (user_menu_cmd (edit
, menu_file
, selected_entry
)
1784 && (mc_stat (block_file_vpath
, &status
) == 0) && (status
.st_size
!= 0))
1789 /* i.e. we have marked block */
1791 rc
= edit_block_delete_cmd (edit
);
1797 ins_len
= edit_insert_file (edit
, block_file_vpath
);
1798 if (nomark
== 0 && ins_len
> 0)
1799 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1801 /* truncate block file */
1802 fd
= fopen (block_file
, "w");
1806 g_free (block_file
);
1807 vfs_path_free (block_file_vpath
);
1809 edit_cursor_move (edit
, curs
- edit
->curs1
);
1810 edit
->force
|= REDRAW_PAGE
;
1811 send_message (edit
, NULL
, MSG_DRAW
, 0, NULL
);
1814 /* --------------------------------------------------------------------------------------------- */
1817 edit_get_byte (const WEdit
* edit
, off_t byte_index
)
1821 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1824 if (byte_index
>= edit
->curs1
)
1826 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1827 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
1830 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
1833 /* --------------------------------------------------------------------------------------------- */
1837 edit_get_utf (const WEdit
* edit
, off_t byte_index
, int *char_width
)
1842 gchar
*next_ch
= NULL
;
1844 gchar utf8_buf
[UTF8_CHAR_LEN
+ 1];
1846 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1852 str
= edit_get_byte_ptr (edit
, byte_index
);
1860 res
= g_utf8_get_char_validated (str
, -1);
1864 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1866 for (i
= 0; i
< UTF8_CHAR_LEN
; i
++)
1867 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
);
1868 utf8_buf
[UTF8_CHAR_LEN
] = '\0';
1870 res
= g_utf8_get_char_validated (str
, -1);
1881 /* Calculate UTF-8 char width */
1882 next_ch
= g_utf8_next_char (str
);
1885 width
= next_ch
- str
;
1893 *char_width
= width
;
1898 /* --------------------------------------------------------------------------------------------- */
1901 edit_get_write_filter (const vfs_path_t
* write_name_vpath
, const vfs_path_t
* filename_vpath
)
1904 char *p
, *writename
;
1905 const vfs_path_element_t
*path_element
;
1907 i
= edit_find_filter (filename_vpath
);
1911 path_element
= vfs_path_get_by_index (write_name_vpath
, -1);
1912 writename
= name_quote (path_element
->path
, 0);
1913 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
1918 /* --------------------------------------------------------------------------------------------- */
1920 * @param edit editor object
1921 * @param f value of stream file
1922 * @return the length of the file
1926 edit_write_stream (WEdit
* edit
, FILE * f
)
1930 if (edit
->lb
== LB_ASIS
)
1932 for (i
= 0; i
< edit
->last_byte
; i
++)
1933 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
1938 /* change line breaks */
1939 for (i
= 0; i
< edit
->last_byte
; i
++)
1941 unsigned char c
= edit_get_byte (edit
, i
);
1943 if (!(c
== '\n' || c
== '\r'))
1945 /* not line break */
1946 if (fputc (c
, f
) < 0)
1950 { /* (c == '\n' || c == '\r') */
1951 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
1955 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
1956 /* put one line break unconditionally */
1957 if (fputc ('\n', f
) < 0)
1960 i
++; /* 2 chars are processed */
1962 if (c
== '\r' && c1
== '\n')
1963 /* Windows line break; go to the next char */
1966 if (c
== '\r' && c1
== '\r')
1968 /* two Macintosh line breaks; put second line break */
1969 if (fputc ('\n', f
) < 0)
1974 if (fputc (c1
, f
) < 0)
1978 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
1979 /* put one line break unconditionally */
1980 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
1983 if (c
== '\r' && c1
== '\n')
1984 /* Windows line break; go to the next char */
1988 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
1989 /* put one line break unconditionally */
1990 if (fputc ('\r', f
) < 0)
1993 i
++; /* 2 chars are processed */
1995 if (c
== '\r' && c1
== '\n')
1996 /* Windows line break; go to the next char */
1999 if (c
== '\n' && c1
== '\n')
2001 /* two Windows line breaks; put second line break */
2002 if (fputc ('\r', f
) < 0)
2007 if (fputc (c1
, f
) < 0)
2010 case LB_ASIS
: /* default without changes */
2016 return edit
->last_byte
;
2019 /* --------------------------------------------------------------------------------------------- */
2022 is_break_char (char c
)
2024 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
2027 /* --------------------------------------------------------------------------------------------- */
2030 edit_get_word_from_pos (const WEdit
* edit
, off_t start_pos
, off_t
* start
, gsize
* len
,
2035 GString
*match_expr
;
2036 unsigned char *bufpos
;
2039 for (word_start
= start_pos
; word_start
!= 0; word_start
--, cut_len
++)
2041 c1
= edit_get_byte (edit
, word_start
);
2042 c2
= edit_get_byte (edit
, word_start
- 1);
2044 if (is_break_char (c1
) != is_break_char (c2
) || c1
== '\n' || c2
== '\n')
2048 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
2049 match_expr
= g_string_sized_new (16);
2053 c1
= edit_get_byte (edit
, word_start
+ match_expr
->len
);
2054 c2
= edit_get_byte (edit
, word_start
+ match_expr
->len
+ 1);
2055 g_string_append_c (match_expr
, c1
);
2057 while (!(is_break_char (c1
) != is_break_char (c2
) || c1
== '\n' || c2
== '\n'));
2059 *len
= match_expr
->len
;
2060 *start
= word_start
;
2063 return g_string_free (match_expr
, FALSE
);
2066 /* --------------------------------------------------------------------------------------------- */
2067 /** inserts a file at the cursor, returns count of inserted bytes on success */
2070 edit_insert_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
2076 p
= edit_get_filter (filename_vpath
);
2077 current
= edit
->curs1
;
2083 f
= (FILE *) popen (p
, "r");
2086 edit_insert_stream (edit
, f
);
2088 /* Place cursor at the end of text selection */
2089 if (!option_cursor_after_inserted_block
)
2091 ins_len
= edit
->curs1
- current
;
2092 edit_cursor_move (edit
, -ins_len
);
2098 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
2099 edit_error_dialog (_("Error"), errmsg
);
2108 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
2109 edit_error_dialog (_("Error"), errmsg
);
2119 int vertical_insertion
= 0;
2122 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
2126 buf
= g_malloc0 (TEMP_BUF_LEN
);
2127 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2130 /* if contain signature VERTICAL_MAGIC then it vertical block */
2131 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2132 vertical_insertion
= 1;
2134 mc_lseek (file
, 0, SEEK_SET
);
2137 if (vertical_insertion
)
2142 blocklen
= edit_insert_column_of_text_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
2143 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2145 /* highlight inserted text then not persistent blocks */
2146 if (!option_persistent_selections
&& edit
->modified
)
2148 if (!edit
->column_highlight
)
2149 edit_push_undo_action (edit
, COLUMN_OFF
);
2150 edit
->column_highlight
= 1;
2157 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2159 for (i
= 0; i
< blocklen
; i
++)
2160 edit_insert (edit
, buf
[i
]);
2162 /* highlight inserted text then not persistent blocks */
2163 if (!option_persistent_selections
&& edit
->modified
)
2165 edit_set_markers (edit
, edit
->curs1
, current
, 0, 0);
2166 if (edit
->column_highlight
)
2167 edit_push_undo_action (edit
, COLUMN_ON
);
2168 edit
->column_highlight
= 0;
2171 /* Place cursor at the end of text selection */
2172 if (!option_cursor_after_inserted_block
)
2174 ins_len
= edit
->curs1
- current
;
2175 edit_cursor_move (edit
, -ins_len
);
2179 edit
->force
|= REDRAW_PAGE
;
2189 /* --------------------------------------------------------------------------------------------- */
2191 * Fill in the edit structure. Return NULL on failure. Pass edit as
2192 * NULL to allocate a new structure.
2194 * If line is 0, try to restore saved position. Otherwise put the
2195 * cursor on that line and show it in the middle of the screen.
2199 edit_init (WEdit
* edit
, int y
, int x
, int lines
, int cols
, const vfs_path_t
* filename_vpath
,
2202 gboolean to_free
= FALSE
;
2204 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2205 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
2209 /* save some widget parameters */
2210 gboolean fullscreen
= edit
->fullscreen
;
2211 int y_prev
= edit
->y_prev
;
2212 int x_prev
= edit
->x_prev
;
2213 int lines_prev
= edit
->lines_prev
;
2214 int cols_prev
= edit
->cols_prev
;
2216 edit_purge_widget (edit
);
2218 /* restore saved parameters */
2219 edit
->fullscreen
= fullscreen
;
2220 edit
->y_prev
= y_prev
;
2221 edit
->x_prev
= x_prev
;
2222 edit
->lines_prev
= lines_prev
;
2223 edit
->cols_prev
= cols_prev
;
2229 * Expand option_whole_chars_search by national letters using
2233 static char option_whole_chars_search_buf
[256];
2235 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
2238 size_t len
= str_term_width1 (option_whole_chars_search
);
2240 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
2242 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
2244 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
2246 option_whole_chars_search_buf
[len
++] = i
;
2250 option_whole_chars_search_buf
[len
] = 0;
2251 option_whole_chars_search
= option_whole_chars_search_buf
;
2253 #endif /* ENABLE_NLS */
2254 edit
= g_malloc0 (sizeof (WEdit
));
2257 init_widget (WIDGET (edit
), y
, x
, lines
, cols
, NULL
, NULL
);
2258 edit
->fullscreen
= TRUE
;
2259 edit_save_size (edit
);
2262 edit
->drag_state
= MCEDIT_DRAG_NORMAL
;
2264 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2265 edit
->stat1
.st_uid
= getuid ();
2266 edit
->stat1
.st_gid
= getgid ();
2267 edit
->stat1
.st_mtime
= 0;
2271 edit
->force
|= REDRAW_PAGE
;
2273 /* set file name before load file */
2274 edit_set_filename (edit
, filename_vpath
);
2276 edit
->undo_stack_size
= START_STACK_SIZE
;
2277 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2278 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2280 edit
->redo_stack_size
= START_STACK_SIZE
;
2281 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2282 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2286 edit
->converter
= str_cnv_from_term
;
2287 edit_set_codeset (edit
);
2290 if (!edit_load_file (edit
))
2292 /* edit_load_file already gives an error message */
2298 edit
->loading_done
= 1;
2301 edit_load_syntax (edit
, NULL
, NULL
);
2302 edit_get_syntax_color (edit
, -1);
2304 /* load saved cursor position */
2305 if ((line
== 0) && option_save_position
)
2306 edit_load_position (edit
);
2311 edit_move_display (edit
, line
- 1);
2312 edit_move_to_line (edit
, line
- 1);
2315 edit_load_macro_cmd (edit
);
2320 /* --------------------------------------------------------------------------------------------- */
2322 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2324 edit_clean (WEdit
* edit
)
2331 /* a stale lock, remove it */
2333 edit
->locked
= unlock_file (edit
->filename_vpath
);
2335 /* save cursor position */
2336 if (option_save_position
)
2337 edit_save_position (edit
);
2338 else if (edit
->serialized_bookmarks
!= NULL
)
2339 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2341 /* File specified on the mcedit command line and never saved */
2342 if (edit
->delete_file
)
2343 unlink (vfs_path_get_last_path_str (edit
->filename_vpath
));
2345 edit_free_syntax_rules (edit
);
2346 book_mark_flush (edit
, -1);
2347 for (; j
<= MAXBUFF
; j
++)
2349 g_free (edit
->buffers1
[j
]);
2350 g_free (edit
->buffers2
[j
]);
2353 g_free (edit
->undo_stack
);
2354 g_free (edit
->redo_stack
);
2355 vfs_path_free (edit
->filename_vpath
);
2356 vfs_path_free (edit
->dir_vpath
);
2357 mc_search_free (edit
->search
);
2358 edit
->search
= NULL
;
2361 if (edit
->converter
!= str_cnv_from_term
)
2362 str_close_conv (edit
->converter
);
2365 edit_purge_widget (edit
);
2370 /* --------------------------------------------------------------------------------------------- */
2373 * Load a new file into the editor and set line. If it fails, preserve the old file.
2374 * To do it, allocate a new widget, initialize it and, if the new file
2375 * was loaded, copy the data to the old widget.
2377 * @return TRUE on success, FALSE on failure.
2380 edit_reload_line (WEdit
* edit
, const vfs_path_t
* filename_vpath
, long line
)
2382 Widget
*w
= WIDGET (edit
);
2385 e
= g_malloc0 (sizeof (WEdit
));
2387 /* save some widget parameters */
2388 e
->fullscreen
= edit
->fullscreen
;
2389 e
->y_prev
= edit
->y_prev
;
2390 e
->x_prev
= edit
->x_prev
;
2391 e
->lines_prev
= edit
->lines_prev
;
2392 e
->cols_prev
= edit
->cols_prev
;
2394 if (edit_init (e
, w
->y
, w
->x
, w
->lines
, w
->cols
, filename_vpath
, line
) == NULL
)
2401 memcpy (edit
, e
, sizeof (WEdit
));
2407 /* --------------------------------------------------------------------------------------------- */
2411 edit_set_codeset (WEdit
* edit
)
2416 get_codepage_id (mc_global
.source_codepage
>=
2417 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2422 conv
= str_crt_conv_from (cp_id
);
2423 if (conv
!= INVALID_CONV
)
2425 if (edit
->converter
!= str_cnv_from_term
)
2426 str_close_conv (edit
->converter
);
2427 edit
->converter
= conv
;
2432 edit
->utf8
= str_isutf8 (cp_id
);
2437 /* --------------------------------------------------------------------------------------------- */
2440 * Recording stack for undo:
2441 * The following is an implementation of a compressed stack. Identical
2442 * pushes are recorded by a negative prefix indicating the number of times the
2443 * same char was pushed. This saves space for repeated curs-left or curs-right
2460 * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2461 * 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2462 * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2463 * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2466 * The only way the cursor moves or the buffer is changed is through the routines:
2467 * insert, backspace, insert_ahead, delete, and cursor_move.
2468 * These record the reverse undo movements onto the stack each time they are
2471 * Each key press results in a set of actions (insert; delete ...). So each time
2472 * a key is pressed the current position of start_display is pushed as
2473 * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2474 * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2475 * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2479 * @param edit editor object
2480 * @param c code of the action
2484 edit_push_undo_action (WEdit
* edit
, long c
)
2486 unsigned long sp
= edit
->undo_stack_pointer
;
2490 /* first enlarge the stack if necessary */
2491 if (sp
> edit
->undo_stack_size
- 10)
2493 if (option_max_undo
< 256)
2494 option_max_undo
= 256;
2495 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2497 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2500 edit
->undo_stack
= t
;
2501 edit
->undo_stack_size
<<= 1;
2502 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2506 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2507 if (edit
->undo_stack_disable
)
2509 edit_push_redo_action (edit
, KEY_PRESS
);
2510 edit_push_redo_action (edit
, c
);
2514 if (edit
->redo_stack_reset
)
2515 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2517 if (edit
->undo_stack_bottom
!= sp
2518 && spm1
!= edit
->undo_stack_bottom
2519 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2522 if (edit
->undo_stack
[spm1
] < 0)
2524 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2525 if (d
== c
&& edit
->undo_stack
[spm1
] > -1000000000)
2527 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2528 edit
->undo_stack
[spm1
]--;
2534 d
= edit
->undo_stack
[spm1
];
2538 return; /* --> no need to push multiple do-nothings */
2539 edit
->undo_stack
[sp
] = -2;
2544 edit
->undo_stack
[sp
] = c
;
2547 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2549 /* if the sp wraps round and catches the undo_stack_bottom then erase
2550 * the first set of actions on the stack to make space - by moving
2551 * undo_stack_bottom forward one "key press" */
2552 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2553 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2554 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2557 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2559 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2560 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2562 /*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: */
2563 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2564 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2566 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2571 edit_push_redo_action (WEdit
* edit
, long c
)
2573 unsigned long sp
= edit
->redo_stack_pointer
;
2576 /* first enlarge the stack if necessary */
2577 if (sp
> edit
->redo_stack_size
- 10)
2579 if (option_max_undo
< 256)
2580 option_max_undo
= 256;
2581 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2583 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2586 edit
->redo_stack
= t
;
2587 edit
->redo_stack_size
<<= 1;
2588 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2592 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2594 if (edit
->redo_stack_bottom
!= sp
2595 && spm1
!= edit
->redo_stack_bottom
2596 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2599 if (edit
->redo_stack
[spm1
] < 0)
2601 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2602 if (d
== c
&& edit
->redo_stack
[spm1
] > -1000000000)
2604 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2605 edit
->redo_stack
[spm1
]--;
2611 d
= edit
->redo_stack
[spm1
];
2615 return; /* --> no need to push multiple do-nothings */
2616 edit
->redo_stack
[sp
] = -2;
2617 goto redo_check_bottom
;
2621 edit
->redo_stack
[sp
] = c
;
2624 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2626 /* if the sp wraps round and catches the redo_stack_bottom then erase
2627 * the first set of actions on the stack to make space - by moving
2628 * redo_stack_bottom forward one "key press" */
2629 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2630 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2631 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2634 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2636 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2637 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2640 * If a single key produced enough pushes to wrap all the way round then
2641 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2642 * The stack is then initialised:
2645 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2646 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2647 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2650 /* --------------------------------------------------------------------------------------------- */
2652 Basic low level single character buffer alterations and movements at the cursor.
2653 Returns char passed over, inserted or removed.
2657 edit_insert (WEdit
* edit
, int c
)
2659 /* check if file has grown to large */
2660 if (edit
->last_byte
>= SIZE_LIMIT
)
2663 /* first we must update the position of the display window */
2664 if (edit
->curs1
< edit
->start_display
)
2666 edit
->start_display
++;
2671 /* Mark file as modified, unless the file hasn't been fully loaded */
2672 if (edit
->loading_done
)
2673 edit_modification (edit
);
2675 /* now we must update some info on the file and check if a redraw is required */
2678 book_mark_inc (edit
, edit
->curs_line
);
2680 edit
->total_lines
++;
2681 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2684 /* save the reverse command onto the undo stack */
2685 /* ordinary char and not space */
2687 edit_push_undo_action (edit
, BACKSPACE
);
2689 edit_push_undo_action (edit
, BACKSPACE_BR
);
2690 /* update markers */
2691 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2692 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2693 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2695 /* add a new buffer if we've reached the end of the last one */
2696 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2697 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2699 /* perform the insertion */
2700 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2701 = (unsigned char) c
;
2703 /* update file length */
2706 /* update cursor position */
2710 /* --------------------------------------------------------------------------------------------- */
2711 /** same as edit_insert and move left */
2714 edit_insert_ahead (WEdit
* edit
, int c
)
2716 if (edit
->last_byte
>= SIZE_LIMIT
)
2719 if (edit
->curs1
< edit
->start_display
)
2721 edit
->start_display
++;
2725 edit_modification (edit
);
2728 book_mark_inc (edit
, edit
->curs_line
);
2729 edit
->total_lines
++;
2730 edit
->force
|= REDRAW_AFTER_CURSOR
;
2732 /* ordinary char and not space */
2734 edit_push_undo_action (edit
, DELCHAR
);
2736 edit_push_undo_action (edit
, DELCHAR_BR
);
2738 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2739 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2740 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2742 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2743 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2744 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2745 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2752 /* --------------------------------------------------------------------------------------------- */
2755 edit_delete (WEdit
* edit
, gboolean byte_delete
)
2761 if (edit
->curs2
== 0)
2765 /* if byte_delete == TRUE then delete only one byte not multibyte char */
2766 if (edit
->utf8
&& !byte_delete
)
2768 edit_get_utf (edit
, edit
->curs1
, &cw
);
2776 if (edit
->mark2
!= edit
->mark1
)
2777 edit_push_markers (edit
);
2779 for (i
= 1; i
<= cw
; i
++)
2781 if (edit
->mark1
> edit
->curs1
)
2784 edit
->end_mark_curs
--;
2786 if (edit
->mark2
> edit
->curs1
)
2788 if (edit
->last_get_rule
> edit
->curs1
)
2789 edit
->last_get_rule
--;
2791 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2793 1) & M_EDIT_BUF_SIZE
) - 1];
2795 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2797 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2798 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2802 edit_push_undo_action (edit
, p
+ 256);
2805 edit_modification (edit
);
2808 book_mark_dec (edit
, edit
->curs_line
);
2809 edit
->total_lines
--;
2810 edit
->force
|= REDRAW_AFTER_CURSOR
;
2812 if (edit
->curs1
< edit
->start_display
)
2814 edit
->start_display
--;
2822 /* --------------------------------------------------------------------------------------------- */
2825 edit_backspace (WEdit
* edit
, gboolean byte_delete
)
2831 if (edit
->curs1
== 0)
2834 if (edit
->mark2
!= edit
->mark1
)
2835 edit_push_markers (edit
);
2838 if (edit
->utf8
&& !byte_delete
)
2840 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
2848 for (i
= 1; i
<= cw
; i
++)
2850 if (edit
->mark1
>= edit
->curs1
)
2853 edit
->end_mark_curs
--;
2855 if (edit
->mark2
>= edit
->curs1
)
2857 if (edit
->last_get_rule
>= edit
->curs1
)
2858 edit
->last_get_rule
--;
2860 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
2861 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
2862 if (((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
) == 0)
2864 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2865 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2869 edit_push_undo_action (edit
, p
);
2871 edit_modification (edit
);
2874 book_mark_dec (edit
, edit
->curs_line
);
2876 edit
->total_lines
--;
2877 edit
->force
|= REDRAW_AFTER_CURSOR
;
2880 if (edit
->curs1
< edit
->start_display
)
2882 edit
->start_display
--;
2890 /* --------------------------------------------------------------------------------------------- */
2891 /** moves the cursor right or left: increment positive or negative respectively */
2894 edit_cursor_move (WEdit
* edit
, off_t increment
)
2896 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2901 for (; increment
< 0; increment
++)
2903 if (edit
->curs1
== 0)
2906 edit_push_undo_action (edit
, CURS_RIGHT
);
2908 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2909 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2910 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2911 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2912 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2914 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2915 1) & M_EDIT_BUF_SIZE
];
2916 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2918 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2919 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2925 edit
->force
|= REDRAW_LINE_BELOW
;
2932 for (; increment
> 0; increment
--)
2934 if (edit
->curs2
== 0)
2937 edit_push_undo_action (edit
, CURS_LEFT
);
2939 c
= edit_get_byte (edit
, edit
->curs1
);
2940 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2941 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2942 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2944 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2946 1) & M_EDIT_BUF_SIZE
) - 1];
2947 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2949 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2950 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2956 edit
->force
|= REDRAW_LINE_ABOVE
;
2962 /* These functions return positions relative to lines */
2964 /* --------------------------------------------------------------------------------------------- */
2965 /** returns index of last char on line + 1 */
2968 edit_eol (const WEdit
* edit
, off_t current
)
2970 if (current
>= edit
->last_byte
)
2971 return edit
->last_byte
;
2973 for (; edit_get_byte (edit
, current
) != '\n'; current
++)
2979 /* --------------------------------------------------------------------------------------------- */
2980 /** returns index of first char on line */
2983 edit_bol (const WEdit
* edit
, off_t current
)
2988 for (; edit_get_byte (edit
, current
- 1) != '\n'; current
--)
2994 /* --------------------------------------------------------------------------------------------- */
2997 edit_count_lines (const WEdit
* edit
, off_t current
, off_t upto
)
3001 if (upto
> edit
->last_byte
)
3002 upto
= edit
->last_byte
;
3005 while (current
< upto
)
3006 if (edit_get_byte (edit
, current
++) == '\n')
3011 /* --------------------------------------------------------------------------------------------- */
3012 /* If lines is zero this returns the count of lines from current to upto. */
3013 /* If upto is zero returns index of lines forward current. */
3016 edit_move_forward (const WEdit
* edit
, off_t current
, long lines
, off_t upto
)
3020 return (off_t
) edit_count_lines (edit
, current
, upto
);
3027 while (lines
-- != 0)
3029 next
= edit_eol (edit
, current
) + 1;
3030 if (next
> edit
->last_byte
)
3039 /* --------------------------------------------------------------------------------------------- */
3040 /** Returns offset of 'lines' lines up from current */
3043 edit_move_backward (const WEdit
* edit
, off_t current
, long lines
)
3047 current
= edit_bol (edit
, current
);
3048 while (lines
-- != 0 && current
!= 0)
3049 current
= edit_bol (edit
, current
- 1);
3053 /* --------------------------------------------------------------------------------------------- */
3054 /* If cols is zero this returns the count of columns from current to upto. */
3055 /* If upto is zero returns index of cols across from current. */
3058 edit_move_forward3 (const WEdit
* edit
, off_t current
, long cols
, off_t upto
)
3069 q
= edit
->last_byte
+ 2;
3071 for (col
= 0, p
= current
; p
< q
; p
++)
3083 orig_c
= c
= edit_get_byte (edit
, p
);
3091 utf_ch
= edit_get_utf (edit
, p
, &cw
);
3092 if (mc_global
.utf8_display
)
3096 if (g_unichar_iswide (utf_ch
))
3099 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
3103 c
= convert_to_display_c (c
);
3107 return (upto
!= 0 ? (off_t
) col
: p
);
3109 col
+= TAB_SIZE
- col
% TAB_SIZE
;
3110 else if ((c
< 32 || c
== 127) && (orig_c
== c
3112 || (!mc_global
.utf8_display
&& !edit
->utf8
)
3115 /* '\r' is shown as ^M, so we must advance 2 characters */
3116 /* Caret notation for control characters */
3124 /* --------------------------------------------------------------------------------------------- */
3125 /** returns the current column position of the cursor */
3128 edit_get_col (const WEdit
* edit
)
3130 return (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3133 /* --------------------------------------------------------------------------------------------- */
3134 /* Scrolling functions */
3135 /* --------------------------------------------------------------------------------------------- */
3138 edit_update_curs_row (WEdit
* edit
)
3140 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
3143 /* --------------------------------------------------------------------------------------------- */
3146 edit_update_curs_col (WEdit
* edit
)
3148 edit
->curs_col
= (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3151 /* --------------------------------------------------------------------------------------------- */
3154 edit_get_curs_col (const WEdit
* edit
)
3156 return edit
->curs_col
;
3159 /* --------------------------------------------------------------------------------------------- */
3160 /** moves the display start position up by i lines */
3163 edit_scroll_upward (WEdit
* edit
, long i
)
3165 long lines_above
= edit
->start_line
;
3167 if (i
> lines_above
)
3171 edit
->start_line
-= i
;
3172 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
3173 edit
->force
|= REDRAW_PAGE
;
3174 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3176 edit_update_curs_row (edit
);
3180 /* --------------------------------------------------------------------------------------------- */
3183 edit_scroll_downward (WEdit
* edit
, long i
)
3187 lines_below
= edit
->total_lines
- edit
->start_line
- (WIDGET (edit
)->lines
- 1);
3188 if (lines_below
> 0)
3190 if (i
> lines_below
)
3192 edit
->start_line
+= i
;
3193 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
3194 edit
->force
|= REDRAW_PAGE
;
3195 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3197 edit_update_curs_row (edit
);
3200 /* --------------------------------------------------------------------------------------------- */
3203 edit_scroll_right (WEdit
* edit
, long i
)
3205 edit
->force
|= REDRAW_PAGE
;
3206 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3207 edit
->start_col
-= i
;
3210 /* --------------------------------------------------------------------------------------------- */
3213 edit_scroll_left (WEdit
* edit
, long i
)
3215 if (edit
->start_col
)
3217 edit
->start_col
+= i
;
3218 if (edit
->start_col
> 0)
3219 edit
->start_col
= 0;
3220 edit
->force
|= REDRAW_PAGE
;
3221 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3225 /* --------------------------------------------------------------------------------------------- */
3226 /* high level cursor movement commands */
3227 /* --------------------------------------------------------------------------------------------- */
3230 edit_move_to_prev_col (WEdit
* edit
, off_t p
)
3232 long prev
= edit
->prev_col
;
3233 long over
= edit
->over_col
;
3235 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
3237 if (option_cursor_beyond_eol
)
3241 line_len
= (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
3242 edit_eol (edit
, edit
->curs1
));
3243 if (line_len
< prev
+ edit
->over_col
)
3245 edit
->over_col
= prev
+ over
- line_len
;
3246 edit
->prev_col
= line_len
;
3247 edit
->curs_col
= line_len
;
3251 edit
->curs_col
= prev
+ over
;
3252 edit
->prev_col
= edit
->curs_col
;
3259 if (option_fake_half_tabs
&& is_in_indent (edit
))
3261 edit_update_curs_col (edit
);
3262 if (space_width
!= 0 && edit
->curs_col
% (HALF_TAB_SIZE
* space_width
) != 0)
3267 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
3268 p
= edit_bol (edit
, edit
->curs1
);
3269 edit_cursor_move (edit
,
3270 edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->curs1
);
3271 if (!left_of_four_spaces (edit
))
3272 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3278 /* --------------------------------------------------------------------------------------------- */
3279 /** check whether line in editor is blank or not
3281 * @param edit editor object
3282 * @param line number of line
3284 * @return TRUE if line in blank, FALSE otherwise
3288 edit_line_is_blank (WEdit
* edit
, long line
)
3290 return is_blank (edit
, edit_find_line (edit
, line
));
3293 /* --------------------------------------------------------------------------------------------- */
3294 /** move cursor to line 'line' */
3297 edit_move_to_line (WEdit
* e
, long line
)
3299 if (line
< e
->curs_line
)
3300 edit_move_up (e
, e
->curs_line
- line
, 0);
3302 edit_move_down (e
, line
- e
->curs_line
, 0);
3303 edit_scroll_screen_over_cursor (e
);
3306 /* --------------------------------------------------------------------------------------------- */
3307 /** scroll window so that first visible line is 'line' */
3310 edit_move_display (WEdit
* e
, long line
)
3312 if (line
< e
->start_line
)
3313 edit_scroll_upward (e
, e
->start_line
- line
);
3315 edit_scroll_downward (e
, line
- e
->start_line
);
3318 /* --------------------------------------------------------------------------------------------- */
3319 /** save markers onto undo stack */
3322 edit_push_markers (WEdit
* edit
)
3324 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3325 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3326 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3329 /* --------------------------------------------------------------------------------------------- */
3332 edit_set_markers (WEdit
* edit
, off_t m1
, off_t m2
, long c1
, long c2
)
3341 /* --------------------------------------------------------------------------------------------- */
3342 /** highlight marker toggle */
3345 edit_mark_cmd (WEdit
* edit
, gboolean unmark
)
3347 edit_push_markers (edit
);
3350 edit_set_markers (edit
, 0, 0, 0, 0);
3351 edit
->force
|= REDRAW_PAGE
;
3353 else if (edit
->mark2
>= 0)
3355 edit
->end_mark_curs
= -1;
3356 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3357 edit
->curs_col
+ edit
->over_col
);
3358 edit
->force
|= REDRAW_PAGE
;
3362 edit
->end_mark_curs
= edit
->curs1
;
3363 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3364 edit
->curs_col
+ edit
->over_col
);
3368 /* --------------------------------------------------------------------------------------------- */
3369 /** highlight the word under cursor */
3372 edit_mark_current_word_cmd (WEdit
* edit
)
3376 for (pos
= edit
->curs1
; pos
!= 0; pos
--)
3380 c1
= edit_get_byte (edit
, pos
);
3381 c2
= edit_get_byte (edit
, pos
- 1);
3382 if (!isspace (c1
) && isspace (c2
))
3384 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3389 for (; pos
< edit
->last_byte
; pos
++)
3393 c1
= edit_get_byte (edit
, pos
);
3394 c2
= edit_get_byte (edit
, pos
+ 1);
3395 if (!isspace (c1
) && isspace (c2
))
3397 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3400 edit
->mark2
= min (pos
+ 1, edit
->last_byte
);
3402 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3405 /* --------------------------------------------------------------------------------------------- */
3408 edit_mark_current_line_cmd (WEdit
* edit
)
3410 long pos
= edit
->curs1
;
3412 edit
->mark1
= edit_bol (edit
, pos
);
3413 edit
->mark2
= edit_eol (edit
, pos
);
3415 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3418 /* --------------------------------------------------------------------------------------------- */
3421 edit_delete_line (WEdit
* edit
)
3424 * Delete right part of the line.
3425 * Note that edit_get_byte() returns '\n' when byte position is
3428 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3429 (void) edit_delete (edit
, TRUE
);
3433 * Note that edit_delete() will not corrupt anything if called while
3434 * cursor position is EOF.
3436 (void) edit_delete (edit
, TRUE
);
3439 * Delete left part of the line.
3440 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3442 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3443 (void) edit_backspace (edit
, TRUE
);
3446 /* --------------------------------------------------------------------------------------------- */
3449 edit_indent_width (const WEdit
* edit
, off_t p
)
3453 /* move to the end of the leading whitespace of the line */
3454 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1)
3456 /* count the number of columns of indentation */
3457 return (long) edit_move_forward3 (edit
, p
, 0, q
);
3460 /* --------------------------------------------------------------------------------------------- */
3463 edit_insert_indent (WEdit
* edit
, int indent
)
3465 if (!option_fill_tabs_with_spaces
)
3467 while (indent
>= TAB_SIZE
)
3469 edit_insert (edit
, '\t');
3473 while (indent
-- > 0)
3474 edit_insert (edit
, ' ');
3477 /* --------------------------------------------------------------------------------------------- */
3480 edit_push_key_press (WEdit
* edit
)
3482 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3483 if (edit
->mark2
== -1)
3485 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3486 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3490 /* --------------------------------------------------------------------------------------------- */
3493 edit_find_bracket (WEdit
* edit
)
3495 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3496 if (last_bracket
!= edit
->bracket
)
3497 edit
->force
|= REDRAW_PAGE
;
3498 last_bracket
= edit
->bracket
;
3501 /* --------------------------------------------------------------------------------------------- */
3503 * This executes a command as though the user initiated it through a key
3504 * press. Callback with MSG_KEY as a message calls this after
3505 * translating the key press. This function can be used to pass any
3506 * command to the editor. Note that the screen wouldn't update
3507 * automatically. Either of command or char_for_insertion must be
3508 * passed as -1. Commands are executed, and char_for_insertion is
3509 * inserted at the cursor.
3513 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3515 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3517 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3520 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3523 if (macro_index
!= -1)
3525 edit
->force
|= REDRAW_COMPLETELY
;
3526 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3528 edit_store_macro_cmd (edit
);
3532 if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3534 edit_repeat_macro_cmd (edit
);
3540 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3542 record_macro_buf
[macro_index
].action
= command
;
3543 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3545 /* record the beginning of a set of editing actions initiated by a key press */
3546 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3547 edit_push_key_press (edit
);
3549 edit_execute_cmd (edit
, command
, char_for_insertion
);
3550 if (edit
->column_highlight
)
3551 edit
->force
|= REDRAW_PAGE
;
3554 /* --------------------------------------------------------------------------------------------- */
3556 This executes a command at a lower level than macro recording.
3557 It also does not push a key_press onto the undo stack. This means
3558 that if it is called many times, a single undo command will undo
3559 all of them. It also does not check for the Undo command.
3562 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3564 Widget
*w
= WIDGET (edit
);
3566 if (command
== CK_WindowFullscreen
)
3568 edit_toggle_fullscreen (edit
);
3572 /* handle window state */
3573 if (edit_handle_move_resize (edit
, command
))
3576 edit
->force
|= REDRAW_LINE
;
3578 /* The next key press will unhighlight the found string, so update
3580 if (edit
->found_len
|| edit
->column_highlight
)
3581 edit
->force
|= REDRAW_PAGE
;
3585 /* a mark command with shift-arrow */
3588 case CK_MarkToWordBegin
:
3589 case CK_MarkToWordEnd
:
3595 case CK_MarkPageDown
:
3596 case CK_MarkToFileBegin
:
3597 case CK_MarkToFileEnd
:
3598 case CK_MarkToPageBegin
:
3599 case CK_MarkToPageEnd
:
3600 case CK_MarkScrollUp
:
3601 case CK_MarkScrollDown
:
3602 case CK_MarkParagraphUp
:
3603 case CK_MarkParagraphDown
:
3604 /* a mark command with alt-arrow */
3605 case CK_MarkColumnPageUp
:
3606 case CK_MarkColumnPageDown
:
3607 case CK_MarkColumnLeft
:
3608 case CK_MarkColumnRight
:
3609 case CK_MarkColumnUp
:
3610 case CK_MarkColumnDown
:
3611 case CK_MarkColumnScrollUp
:
3612 case CK_MarkColumnScrollDown
:
3613 case CK_MarkColumnParagraphUp
:
3614 case CK_MarkColumnParagraphDown
:
3615 edit
->column_highlight
= 0;
3616 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3618 edit_mark_cmd (edit
, TRUE
); /* clear */
3619 edit_mark_cmd (edit
, FALSE
); /* marking on */
3621 edit
->highlight
= 1;
3624 /* any other command */
3626 if (edit
->highlight
)
3627 edit_mark_cmd (edit
, FALSE
); /* clear */
3628 edit
->highlight
= 0;
3631 /* first check for undo */
3632 if (command
== CK_Undo
)
3634 edit
->redo_stack_reset
= 0;
3635 edit_group_undo (edit
);
3636 edit
->found_len
= 0;
3637 edit
->prev_col
= edit_get_col (edit
);
3638 edit
->search_start
= edit
->curs1
;
3641 /* check for redo */
3642 if (command
== CK_Redo
)
3644 edit
->redo_stack_reset
= 0;
3645 edit_do_redo (edit
);
3646 edit
->found_len
= 0;
3647 edit
->prev_col
= edit_get_col (edit
);
3648 edit
->search_start
= edit
->curs1
;
3652 edit
->redo_stack_reset
= 1;
3654 /* An ordinary key press */
3655 if (char_for_insertion
>= 0)
3657 /* if non persistent selection and text selected */
3658 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3659 edit_block_delete_cmd (edit
);
3661 if (edit
->overwrite
)
3663 /* remove char only one time, after input first byte, multibyte chars */
3665 if (!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3667 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
3669 edit_delete (edit
, FALSE
);
3671 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3672 edit_insert_over (edit
);
3674 if (char_for_insertion
> 255 && !mc_global
.utf8_display
)
3676 unsigned char str
[6 + 1];
3680 res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3690 while (str
[i
] != 0 && i
<= 6)
3692 char_for_insertion
= str
[i
];
3693 edit_insert (edit
, char_for_insertion
);
3699 edit_insert (edit
, char_for_insertion
);
3701 if (option_auto_para_formatting
)
3703 format_paragraph (edit
, 0);
3704 edit
->force
|= REDRAW_PAGE
;
3707 check_and_wrap_line (edit
);
3708 edit
->found_len
= 0;
3709 edit
->prev_col
= edit_get_col (edit
);
3710 edit
->search_start
= edit
->curs1
;
3711 edit_find_bracket (edit
);
3717 case CK_TopOnScreen
:
3718 case CK_BottomOnScreen
:
3731 if (!option_persistent_selections
&& edit
->mark2
>= 0)
3733 if (edit
->column_highlight
)
3734 edit_push_undo_action (edit
, COLUMN_ON
);
3735 edit
->column_highlight
= 0;
3736 edit_mark_cmd (edit
, TRUE
);
3742 case CK_TopOnScreen
:
3743 case CK_BottomOnScreen
:
3744 case CK_MarkToPageBegin
:
3745 case CK_MarkToPageEnd
:
3750 case CK_MarkToWordBegin
:
3751 case CK_MarkToWordEnd
:
3754 case CK_MarkColumnUp
:
3755 case CK_MarkColumnDown
:
3756 if (edit
->mark2
== -1)
3757 break; /*marking is following the cursor: may need to highlight a whole line */
3762 edit
->force
|= REDRAW_CHAR_ONLY
;
3765 /* basic cursor key commands */
3769 /* if non persistent selection and text selected */
3770 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3771 edit_block_delete_cmd (edit
);
3772 else if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3774 else if (option_backspace_through_tabs
&& is_in_indent (edit
))
3776 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3777 edit_backspace (edit
, TRUE
);
3779 else if (option_fake_half_tabs
&& is_in_indent (edit
) && right_of_four_spaces (edit
))
3783 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3784 edit_backspace (edit
, TRUE
);
3787 edit_backspace (edit
, FALSE
);
3790 /* if non persistent selection and text selected */
3791 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3792 edit_block_delete_cmd (edit
);
3795 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3796 edit_insert_over (edit
);
3798 if (option_fake_half_tabs
&& is_in_indent (edit
) && left_of_four_spaces (edit
))
3802 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3803 edit_delete (edit
, TRUE
);
3806 edit_delete (edit
, FALSE
);
3809 case CK_DeleteToWordBegin
:
3811 edit_left_delete_word (edit
);
3813 case CK_DeleteToWordEnd
:
3814 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3815 edit_insert_over (edit
);
3817 edit_right_delete_word (edit
);
3820 edit_delete_line (edit
);
3822 case CK_DeleteToHome
:
3823 edit_delete_to_line_begin (edit
);
3825 case CK_DeleteToEnd
:
3826 edit_delete_to_line_end (edit
);
3830 if (option_auto_para_formatting
)
3832 edit_double_newline (edit
);
3833 if (option_return_does_auto_indent
)
3834 edit_auto_indent (edit
);
3835 format_paragraph (edit
, 0);
3839 edit_insert (edit
, '\n');
3840 if (option_return_does_auto_indent
)
3841 edit_auto_indent (edit
);
3845 edit_insert (edit
, '\n');
3848 case CK_MarkColumnPageUp
:
3849 edit
->column_highlight
= 1;
3852 edit_move_up (edit
, w
->lines
- 1, 1);
3854 case CK_MarkColumnPageDown
:
3855 edit
->column_highlight
= 1;
3857 case CK_MarkPageDown
:
3858 edit_move_down (edit
, w
->lines
- 1, 1);
3860 case CK_MarkColumnLeft
:
3861 edit
->column_highlight
= 1;
3864 if (option_fake_half_tabs
&& is_in_indent (edit
) && right_of_four_spaces (edit
))
3866 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3869 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3870 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3873 edit_left_char_move_cmd (edit
);
3875 case CK_MarkColumnRight
:
3876 edit
->column_highlight
= 1;
3879 if (option_fake_half_tabs
&& is_in_indent (edit
) && left_of_four_spaces (edit
))
3881 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3882 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3885 edit_right_char_move_cmd (edit
);
3887 case CK_TopOnScreen
:
3888 case CK_MarkToPageBegin
:
3889 edit_begin_page (edit
);
3891 case CK_BottomOnScreen
:
3892 case CK_MarkToPageEnd
:
3893 edit_end_page (edit
);
3896 case CK_MarkToWordBegin
:
3898 edit_left_word_move_cmd (edit
);
3901 case CK_MarkToWordEnd
:
3903 edit_right_word_move_cmd (edit
);
3905 case CK_MarkColumnUp
:
3906 edit
->column_highlight
= 1;
3909 edit_move_up (edit
, 1, 0);
3911 case CK_MarkColumnDown
:
3912 edit
->column_highlight
= 1;
3915 edit_move_down (edit
, 1, 0);
3917 case CK_MarkColumnParagraphUp
:
3918 edit
->column_highlight
= 1;
3919 case CK_ParagraphUp
:
3920 case CK_MarkParagraphUp
:
3921 edit_move_up_paragraph (edit
, 0);
3923 case CK_MarkColumnParagraphDown
:
3924 edit
->column_highlight
= 1;
3925 case CK_ParagraphDown
:
3926 case CK_MarkParagraphDown
:
3927 edit_move_down_paragraph (edit
, 0);
3929 case CK_MarkColumnScrollUp
:
3930 edit
->column_highlight
= 1;
3932 case CK_MarkScrollUp
:
3933 edit_move_up (edit
, 1, 1);
3935 case CK_MarkColumnScrollDown
:
3936 edit
->column_highlight
= 1;
3938 case CK_MarkScrollDown
:
3939 edit_move_down (edit
, 1, 1);
3943 edit_cursor_to_bol (edit
);
3947 edit_cursor_to_eol (edit
);
3950 /* if text marked shift block */
3951 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3953 if (edit
->mark2
< 0)
3954 edit_mark_cmd (edit
, FALSE
);
3955 edit_move_block_to_right (edit
);
3959 if (option_cursor_beyond_eol
)
3960 edit_insert_over (edit
);
3961 edit_tab_cmd (edit
);
3962 if (option_auto_para_formatting
)
3964 format_paragraph (edit
, 0);
3965 edit
->force
|= REDRAW_PAGE
;
3968 check_and_wrap_line (edit
);
3972 case CK_InsertOverwrite
:
3973 edit
->overwrite
= !edit
->overwrite
;
3977 if (edit
->mark2
>= 0)
3979 if (edit
->column_highlight
)
3980 edit_push_undo_action (edit
, COLUMN_ON
);
3981 edit
->column_highlight
= 0;
3983 edit_mark_cmd (edit
, FALSE
);
3986 if (!edit
->column_highlight
)
3987 edit_push_undo_action (edit
, COLUMN_OFF
);
3988 edit
->column_highlight
= 1;
3989 edit_mark_cmd (edit
, FALSE
);
3992 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3993 edit
->force
|= REDRAW_PAGE
;
3996 if (edit
->column_highlight
)
3997 edit_push_undo_action (edit
, COLUMN_ON
);
3998 edit
->column_highlight
= 0;
3999 edit_mark_cmd (edit
, TRUE
);
4002 if (edit
->column_highlight
)
4003 edit_push_undo_action (edit
, COLUMN_ON
);
4004 edit
->column_highlight
= 0;
4005 edit_mark_current_word_cmd (edit
);
4008 if (edit
->column_highlight
)
4009 edit_push_undo_action (edit
, COLUMN_ON
);
4010 edit
->column_highlight
= 0;
4011 edit_mark_current_line_cmd (edit
);
4015 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
4016 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
4017 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4019 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4021 case CK_BookmarkFlush
:
4022 book_mark_flush (edit
, BOOK_MARK_COLOR
);
4023 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
4024 edit
->force
|= REDRAW_PAGE
;
4026 case CK_BookmarkNext
:
4027 if (edit
->book_mark
!= NULL
)
4029 edit_book_mark_t
*p
;
4031 p
= book_mark_find (edit
, edit
->curs_line
);
4032 if (p
->next
!= NULL
)
4035 if (p
->line
>= edit
->start_line
+ w
->lines
|| p
->line
< edit
->start_line
)
4036 edit_move_display (edit
, p
->line
- w
->lines
/ 2);
4037 edit_move_to_line (edit
, p
->line
);
4041 case CK_BookmarkPrev
:
4042 if (edit
->book_mark
!= NULL
)
4044 edit_book_mark_t
*p
;
4046 p
= book_mark_find (edit
, edit
->curs_line
);
4047 while (p
->line
== edit
->curs_line
)
4048 if (p
->prev
!= NULL
)
4052 if (p
->line
>= edit
->start_line
+ w
->lines
|| p
->line
< edit
->start_line
)
4053 edit_move_display (edit
, p
->line
- w
->lines
/ 2);
4054 edit_move_to_line (edit
, p
->line
);
4060 case CK_MarkToFileBegin
:
4061 edit_move_to_top (edit
);
4064 case CK_MarkToFileEnd
:
4065 edit_move_to_bottom (edit
);
4069 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4070 edit_insert_over (edit
);
4071 edit_block_copy_cmd (edit
);
4074 edit_block_delete_cmd (edit
);
4077 edit_block_move_cmd (edit
);
4080 case CK_BlockShiftLeft
:
4081 if (edit
->mark1
!= edit
->mark2
)
4082 edit_move_block_to_left (edit
);
4084 case CK_BlockShiftRight
:
4085 if (edit
->mark1
!= edit
->mark2
)
4086 edit_move_block_to_right (edit
);
4089 edit_copy_to_X_buf_cmd (edit
);
4092 edit_cut_to_X_buf_cmd (edit
);
4095 /* if non persistent selection and text selected */
4096 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
4097 edit_block_delete_cmd (edit
);
4098 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4099 edit_insert_over (edit
);
4100 edit_paste_from_X_buf_cmd (edit
);
4101 if (!option_persistent_selections
&& edit
->mark2
>= 0)
4103 if (edit
->column_highlight
)
4104 edit_push_undo_action (edit
, COLUMN_ON
);
4105 edit
->column_highlight
= 0;
4106 edit_mark_cmd (edit
, TRUE
);
4110 edit_paste_from_history (edit
);
4114 edit_save_as_cmd (edit
);
4117 edit_save_confirm_cmd (edit
);
4120 edit_save_block_cmd (edit
);
4123 edit_insert_file_cmd (edit
);
4127 edit_load_back_cmd (edit
);
4130 edit_load_forward_cmd (edit
);
4133 case CK_SyntaxChoose
:
4134 edit_syntax_dialog (edit
);
4138 edit_search_cmd (edit
, FALSE
);
4140 case CK_SearchContinue
:
4141 edit_search_cmd (edit
, TRUE
);
4144 edit_replace_cmd (edit
, 0);
4146 case CK_ReplaceContinue
:
4147 edit_replace_cmd (edit
, 1);
4150 /* if text marked shift block */
4151 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
4152 edit_move_block_to_left (edit
);
4154 edit_complete_word_cmd (edit
);
4157 edit_get_match_keyword_cmd (edit
);
4161 case CK_SpellCheckCurrentWord
:
4162 edit_suggest_current_word (edit
);
4165 edit_spellcheck_file (edit
);
4167 case CK_SpellCheckSelectLang
:
4168 edit_set_spell_lang ();
4175 /* fool gcc to prevent a Y2K warning */
4176 char time_format
[] = "_c";
4177 time_format
[0] = '%';
4179 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4180 edit_print_string (edit
, s
);
4181 edit
->force
|= REDRAW_PAGE
;
4185 edit_goto_cmd (edit
);
4187 case CK_ParagraphFormat
:
4188 format_paragraph (edit
, 1);
4189 edit
->force
|= REDRAW_PAGE
;
4191 case CK_MacroDelete
:
4192 edit_delete_macro_cmd (edit
);
4194 case CK_MatchBracket
:
4195 edit_goto_matching_bracket (edit
);
4198 user_menu (edit
, NULL
, -1);
4201 edit_sort_cmd (edit
);
4203 case CK_ExternalCommand
:
4204 edit_ext_cmd (edit
);
4207 edit_mail_dialog (edit
);
4210 case CK_SelectCodepage
:
4211 edit_select_codepage_cmd (edit
);
4214 case CK_InsertLiteral
:
4215 edit_insert_literal_cmd (edit
);
4217 case CK_MacroStartStopRecord
:
4218 edit_begin_end_macro_cmd (edit
);
4220 case CK_RepeatStartStopRecord
:
4221 edit_begin_end_repeat_cmd (edit
);
4223 case CK_ExtendedKeyMap
:
4224 edit
->extmod
= TRUE
;
4231 if ((command
/ CK_PipeBlock (0)) == 1)
4232 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
4234 /* keys which must set the col position, and the search vars */
4238 case CK_SearchContinue
:
4240 case CK_ReplaceContinue
:
4242 edit
->prev_col
= edit_get_col (edit
);
4246 case CK_MarkColumnUp
:
4249 case CK_MarkColumnDown
:
4252 case CK_MarkColumnPageUp
:
4254 case CK_MarkPageDown
:
4255 case CK_MarkColumnPageDown
:
4257 case CK_MarkToFileBegin
:
4259 case CK_MarkToFileEnd
:
4260 case CK_ParagraphUp
:
4261 case CK_MarkParagraphUp
:
4262 case CK_MarkColumnParagraphUp
:
4263 case CK_ParagraphDown
:
4264 case CK_MarkParagraphDown
:
4265 case CK_MarkColumnParagraphDown
:
4267 case CK_MarkScrollUp
:
4268 case CK_MarkColumnScrollUp
:
4270 case CK_MarkScrollDown
:
4271 case CK_MarkColumnScrollDown
:
4272 edit
->search_start
= edit
->curs1
;
4273 edit
->found_len
= 0;
4276 edit
->found_len
= 0;
4277 edit
->prev_col
= edit_get_col (edit
);
4278 edit
->search_start
= edit
->curs1
;
4280 edit_find_bracket (edit
);
4282 if (option_auto_para_formatting
)
4288 case CK_DeleteToWordBegin
:
4289 case CK_DeleteToWordEnd
:
4290 case CK_DeleteToHome
:
4291 case CK_DeleteToEnd
:
4292 format_paragraph (edit
, 0);
4293 edit
->force
|= REDRAW_PAGE
;
4298 /* --------------------------------------------------------------------------------------------- */
4301 edit_stack_init (void)
4303 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4305 edit_history_moveto
[edit_stack_iterator
].filename_vpath
= NULL
;
4306 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4309 edit_stack_iterator
= 0;
4312 /* --------------------------------------------------------------------------------------------- */
4315 edit_stack_free (void)
4317 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4318 vfs_path_free (edit_history_moveto
[edit_stack_iterator
].filename_vpath
);
4321 /* --------------------------------------------------------------------------------------------- */
4325 edit_move_up (WEdit
* edit
, long i
, gboolean do_scroll
)
4327 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4330 /* --------------------------------------------------------------------------------------------- */
4334 edit_move_down (WEdit
* edit
, long i
, gboolean do_scroll
)
4336 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4339 /* --------------------------------------------------------------------------------------------- */