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/main.h" /* macro_index */
68 #include "src/learn.h" /* learn_keys */
69 #include "src/keybind-defaults.h"
71 #include "edit-impl.h"
72 #include "editwidget.h"
77 /*** global variables ****************************************************************************/
79 int option_word_wrap_line_length
= DEFAULT_WRAP_LINE_LENGTH
;
80 int option_typewriter_wrap
= 0;
81 int option_auto_para_formatting
= 0;
82 int option_fill_tabs_with_spaces
= 0;
83 int option_return_does_auto_indent
= 1;
84 int option_backspace_through_tabs
= 0;
85 int option_fake_half_tabs
= 1;
86 int option_save_mode
= EDIT_QUICK_SAVE
;
87 int option_save_position
= 1;
88 int option_max_undo
= 32768;
89 int option_persistent_selections
= 1;
90 int option_cursor_beyond_eol
= 0;
91 int option_line_state
= 0;
92 int option_line_state_width
= 0;
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 static int left_of_four_spaces (WEdit
* edit
);
176 /* --------------------------------------------------------------------------------------------- */
178 * Initialize the buffers for an empty files.
182 edit_init_buffers (WEdit
* edit
)
186 for (j
= 0; j
<= MAXBUFF
; j
++)
188 edit
->buffers1
[j
] = NULL
;
189 edit
->buffers2
[j
] = NULL
;
194 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
197 /* --------------------------------------------------------------------------------------------- */
200 * Load file OR text into buffers. Set cursor to the beginning of file.
202 * @returns FALSE on error.
206 edit_load_file_fast (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
210 gboolean ret
= FALSE
;
212 edit
->curs2
= edit
->last_byte
;
213 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
215 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
218 gchar
*errmsg
, *filename
;
220 filename
= vfs_path_to_str (filename_vpath
);
221 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
223 edit_error_dialog (_("Error"), errmsg
);
228 if (!edit
->buffers2
[buf2
])
229 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
234 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
235 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
238 for (buf
= buf2
- 1; buf
>= 0; buf
--)
240 /* edit->buffers2[0] is already allocated */
241 if (!edit
->buffers2
[buf
])
242 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
243 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
252 gchar
*errmsg
, *filename
;
254 filename
= vfs_path_to_str (filename_vpath
);
255 errmsg
= g_strdup_printf (_("Error reading %s"), filename
);
257 edit_error_dialog (_("Error"), errmsg
);
264 /* --------------------------------------------------------------------------------------------- */
265 /** Return index of the filter or -1 is there is no appropriate filter */
268 edit_find_filter (const vfs_path_t
* filename_vpath
)
273 if (filename_vpath
== NULL
)
276 filename
= vfs_path_to_str (filename_vpath
);
277 l
= strlen (filename
);
278 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
280 e
= strlen (all_filters
[i
].extension
);
282 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
292 /* --------------------------------------------------------------------------------------------- */
295 edit_get_filter (const vfs_path_t
* filename_vpath
)
298 char *p
, *quoted_name
, *filename
;
300 i
= edit_find_filter (filename_vpath
);
304 filename
= vfs_path_to_str (filename_vpath
);
305 quoted_name
= name_quote (filename
, 0);
307 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
308 g_free (quoted_name
);
312 /* --------------------------------------------------------------------------------------------- */
315 edit_insert_stream (WEdit
* edit
, FILE * f
)
319 while ((c
= fgetc (f
)) >= 0)
321 edit_insert (edit
, c
);
327 /* --------------------------------------------------------------------------------------------- */
329 * Open file and create it if necessary.
331 * @param edit editor object
332 * @param filename_vpath file name
333 * @param st buffer for store stat info
334 * @returns TRUE for success, FALSE for error.
338 check_file_access (WEdit
* edit
, const vfs_path_t
* filename_vpath
, struct stat
*st
)
341 gchar
*errmsg
= NULL
;
343 /* Try opening an existing file */
344 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
348 * Try creating the file. O_EXCL prevents following broken links
349 * and opening existing files.
351 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
356 filename
= vfs_path_to_str (filename_vpath
);
357 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
362 /* New file, delete it if it's not modified or saved */
363 edit
->delete_file
= 1;
366 /* Check what we have opened */
367 if (mc_fstat (file
, st
) < 0)
371 filename
= vfs_path_to_str (filename_vpath
);
372 errmsg
= g_strdup_printf (_("Cannot get size/permissions for %s"), filename
);
377 /* We want to open regular files only */
378 if (!S_ISREG (st
->st_mode
))
382 filename
= vfs_path_to_str (filename_vpath
);
383 errmsg
= g_strdup_printf (_("\"%s\" is not a regular file"), filename
);
389 * Don't delete non-empty files.
390 * O_EXCL should prevent it, but let's be on the safe side.
393 edit
->delete_file
= 0;
395 if (st
->st_size
>= SIZE_LIMIT
)
399 filename
= vfs_path_to_str (filename_vpath
);
400 errmsg
= g_strdup_printf (_("File \"%s\" is too large"), filename
);
405 (void) mc_close (file
);
409 edit_error_dialog (_("Error"), errmsg
);
416 /* --------------------------------------------------------------------------------------------- */
419 * Open the file and load it into the buffers, either directly or using
420 * a filter. Return TRUE on success, FALSE on error.
422 * Fast loading (edit_load_file_fast) is used when the file size is
423 * known. In this case the data is read into the buffers by blocks.
424 * If the file size is not known, the data is loaded byte by byte in
427 * @param edit editor object
428 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
431 edit_load_file (WEdit
* edit
)
433 gboolean fast_load
= TRUE
;
435 /* Cannot do fast load if a filter is used */
436 if (edit_find_filter (edit
->filename_vpath
) >= 0)
440 * FIXME: line end translation should disable fast loading as well
441 * Consider doing fseek() to the end and ftell() for the real size.
443 if (edit
->filename_vpath
!= NULL
)
446 * VFS may report file size incorrectly, and slow load is not a big
447 * deal considering overhead in VFS.
449 if (!vfs_file_is_local (edit
->filename_vpath
))
452 /* If we are dealing with a real file, check that it exists */
453 if (!check_file_access (edit
, edit
->filename_vpath
, &edit
->stat1
))
461 /* nothing to load */
465 edit_init_buffers (edit
);
469 edit
->last_byte
= edit
->stat1
.st_size
;
470 edit_load_file_fast (edit
, edit
->filename_vpath
);
471 /* If fast load was used, the number of lines wasn't calculated */
472 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
477 if (edit
->filename_vpath
!= NULL
478 && *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) != '\0')
480 edit
->undo_stack_disable
= 1;
481 if (edit_insert_file (edit
, edit
->filename_vpath
) < 0)
486 edit
->undo_stack_disable
= 0;
493 /* --------------------------------------------------------------------------------------------- */
494 /** Restore saved cursor position in the file */
497 edit_load_position (WEdit
* edit
)
502 if (edit
->filename_vpath
== NULL
503 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
506 load_file_position (edit
->filename_vpath
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
510 edit_move_to_line (edit
, line
- 1);
511 edit
->prev_col
= column
;
515 edit_cursor_move (edit
, offset
);
516 line
= edit
->curs_line
;
517 edit
->search_start
= edit
->curs1
;
520 book_mark_restore (edit
, BOOK_MARK_COLOR
);
522 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
523 edit_move_display (edit
, line
- (edit
->widget
.lines
/ 2));
526 /* --------------------------------------------------------------------------------------------- */
527 /** Save cursor position in the file */
530 edit_save_position (WEdit
* edit
)
532 if (edit
->filename_vpath
== NULL
533 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
536 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
537 save_file_position (edit
->filename_vpath
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
,
538 edit
->serialized_bookmarks
);
539 edit
->serialized_bookmarks
= NULL
;
542 /* --------------------------------------------------------------------------------------------- */
543 /** Clean the WEdit stricture except the widget part */
546 edit_purge_widget (WEdit
* edit
)
548 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
549 char *start
= (char *) edit
+ sizeof (Widget
);
550 memset (start
, 0, len
);
553 /* --------------------------------------------------------------------------------------------- */
556 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
557 then the file should be as it was when he loaded up. Then set edit->modified to 0.
561 edit_pop_undo_action (WEdit
* edit
)
564 unsigned long sp
= edit
->undo_stack_pointer
;
566 if (sp
== edit
->undo_stack_bottom
)
569 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
570 c
= edit
->undo_stack
[sp
];
573 /* edit->undo_stack[sp] = '@'; */
574 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
578 if (sp
== edit
->undo_stack_bottom
)
581 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
582 if (edit
->undo_stack
[sp
] == -2)
584 /* edit->undo_stack[sp] = '@'; */
585 edit
->undo_stack_pointer
= sp
;
588 edit
->undo_stack
[sp
]++;
594 edit_pop_redo_action (WEdit
* edit
)
597 unsigned long sp
= edit
->redo_stack_pointer
;
599 if (sp
== edit
->redo_stack_bottom
)
602 sp
= (sp
- 1) & edit
->redo_stack_size_mask
;
603 c
= edit
->redo_stack
[sp
];
606 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
610 if (sp
== edit
->redo_stack_bottom
)
613 c
= edit
->redo_stack
[(sp
- 1) & edit
->redo_stack_size_mask
];
614 if (edit
->redo_stack
[sp
] == -2)
615 edit
->redo_stack_pointer
= sp
;
617 edit
->redo_stack
[sp
]++;
623 get_prev_undo_action (WEdit
* edit
)
626 unsigned long sp
= edit
->undo_stack_pointer
;
628 if (sp
== edit
->undo_stack_bottom
)
631 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
632 c
= edit
->undo_stack
[sp
];
636 if (sp
== edit
->undo_stack_bottom
)
639 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
643 /* --------------------------------------------------------------------------------------------- */
644 /** is called whenever a modification is made by one of the four routines below */
647 edit_modification (WEdit
* edit
)
649 edit
->caches_valid
= FALSE
;
651 /* raise lock when file modified */
652 if (!edit
->modified
&& !edit
->delete_file
)
653 edit
->locked
= lock_file (edit
->filename_vpath
);
657 /* --------------------------------------------------------------------------------------------- */
660 edit_get_byte_ptr (WEdit
* edit
, off_t byte_index
)
662 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
665 if (byte_index
>= edit
->curs1
)
669 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
670 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
671 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
674 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
675 (byte_index
& M_EDIT_BUF_SIZE
));
678 /* --------------------------------------------------------------------------------------------- */
681 edit_get_prev_utf (WEdit
* edit
, off_t byte_index
, int *char_width
)
684 gchar utf8_buf
[3 * UTF8_CHAR_LEN
+ 1];
686 gchar
*cursor_buf_ptr
;
688 if (byte_index
> (edit
->curs1
+ edit
->curs2
) || byte_index
<= 0)
694 for (i
= 0; i
< (3 * UTF8_CHAR_LEN
); i
++)
695 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
- (2 * UTF8_CHAR_LEN
));
696 utf8_buf
[3 * UTF8_CHAR_LEN
] = '\0';
698 cursor_buf_ptr
= utf8_buf
+ (2 * UTF8_CHAR_LEN
);
699 str
= g_utf8_find_prev_char (utf8_buf
, cursor_buf_ptr
);
701 if (str
== NULL
|| g_utf8_next_char (str
) != cursor_buf_ptr
)
704 return *(cursor_buf_ptr
- 1);
708 res
= g_utf8_get_char_validated (str
, -1);
713 return *(cursor_buf_ptr
- 1);
717 *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 (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 (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 (line_is_blank (edit
, edit
->curs_line
))
836 if (line_is_blank (edit
, edit
->curs_line
- 1))
838 for (i
= edit
->curs_line
- 1; i
; i
--)
839 if (!line_is_blank (edit
, i
))
847 for (i
= edit
->curs_line
- 1; i
; i
--)
848 if (line_is_blank (edit
, i
))
854 for (i
= edit
->curs_line
- 1; i
; i
--)
855 if (line_is_blank (edit
, i
))
859 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
862 /* --------------------------------------------------------------------------------------------- */
863 /** moves down until a blank line is reached, or until just
864 before a non-blank line is reached */
867 edit_move_down_paragraph (WEdit
* edit
, gboolean do_scroll
)
871 if (edit
->curs_line
>= edit
->total_lines
- 1)
873 i
= edit
->total_lines
;
877 if (line_is_blank (edit
, edit
->curs_line
))
879 if (line_is_blank (edit
, edit
->curs_line
+ 1))
881 for (i
= edit
->curs_line
+ 1; i
; i
++)
882 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
890 for (i
= edit
->curs_line
+ 1; i
; i
++)
891 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
897 for (i
= edit
->curs_line
+ 1; i
; i
++)
898 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
902 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
905 /* --------------------------------------------------------------------------------------------- */
908 edit_begin_page (WEdit
* edit
)
910 edit_update_curs_row (edit
);
911 edit_move_up (edit
, edit
->curs_row
, 0);
914 /* --------------------------------------------------------------------------------------------- */
917 edit_end_page (WEdit
* edit
)
919 edit_update_curs_row (edit
);
920 edit_move_down (edit
, edit
->widget
.lines
- edit
->curs_row
- 1, 0);
924 /* --------------------------------------------------------------------------------------------- */
925 /** goto beginning of text */
928 edit_move_to_top (WEdit
* edit
)
932 edit_cursor_move (edit
, -edit
->curs1
);
933 edit_move_to_prev_col (edit
, 0);
934 edit
->force
|= REDRAW_PAGE
;
935 edit
->search_start
= 0;
936 edit_update_curs_row (edit
);
941 /* --------------------------------------------------------------------------------------------- */
942 /** goto end of text */
945 edit_move_to_bottom (WEdit
* edit
)
947 if (edit
->curs_line
< edit
->total_lines
)
949 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
950 edit
->start_display
= edit
->last_byte
;
951 edit
->start_line
= edit
->total_lines
;
952 edit_scroll_upward (edit
, edit
->widget
.lines
- 1);
953 edit
->force
|= REDRAW_PAGE
;
957 /* --------------------------------------------------------------------------------------------- */
958 /** goto beginning of line */
961 edit_cursor_to_bol (WEdit
* edit
)
963 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
964 edit
->search_start
= edit
->curs1
;
965 edit
->prev_col
= edit_get_col (edit
);
969 /* --------------------------------------------------------------------------------------------- */
970 /** goto end of line */
973 edit_cursor_to_eol (WEdit
* edit
)
975 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
976 edit
->search_start
= edit
->curs1
;
977 edit
->prev_col
= edit_get_col (edit
);
981 /* --------------------------------------------------------------------------------------------- */
988 const char option_chars_move_whole_word
[] =
989 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
995 if (*option_chars_move_whole_word
== '!')
999 if (g_ascii_isupper ((gchar
) c
))
1001 else if (g_ascii_islower ((gchar
) c
))
1003 else if (g_ascii_isalpha (c
))
1005 else if (isdigit (c
))
1007 else if (isspace (c
))
1009 q
= strchr (option_chars_move_whole_word
, c
);
1011 return 0xFFFFFFFFUL
;
1014 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1019 while ((q
= strchr (q
+ 1, c
)));
1023 /* --------------------------------------------------------------------------------------------- */
1026 edit_left_word_move (WEdit
* edit
, int s
)
1031 if (edit
->column_highlight
1032 && edit
->mark1
!= edit
->mark2
1033 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1035 edit_cursor_move (edit
, -1);
1038 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1039 c2
= edit_get_byte (edit
, edit
->curs1
);
1040 if (c1
== '\n' || c2
== '\n')
1042 if (!(my_type_of (c1
) & my_type_of (c2
)))
1044 if (isspace (c1
) && !isspace (c2
))
1047 if (!isspace (c1
) && isspace (c2
))
1052 /* --------------------------------------------------------------------------------------------- */
1055 edit_left_word_move_cmd (WEdit
* edit
)
1057 edit_left_word_move (edit
, 0);
1058 edit
->force
|= REDRAW_PAGE
;
1061 /* --------------------------------------------------------------------------------------------- */
1064 edit_right_word_move (WEdit
* edit
, int s
)
1069 if (edit
->column_highlight
1070 && edit
->mark1
!= edit
->mark2
1071 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
1073 edit_cursor_move (edit
, 1);
1074 if (edit
->curs1
>= edit
->last_byte
)
1076 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1077 c2
= edit_get_byte (edit
, edit
->curs1
);
1078 if (c1
== '\n' || c2
== '\n')
1080 if (!(my_type_of (c1
) & my_type_of (c2
)))
1082 if (isspace (c1
) && !isspace (c2
))
1085 if (!isspace (c1
) && isspace (c2
))
1090 /* --------------------------------------------------------------------------------------------- */
1093 edit_right_word_move_cmd (WEdit
* edit
)
1095 edit_right_word_move (edit
, 0);
1096 edit
->force
|= REDRAW_PAGE
;
1099 /* --------------------------------------------------------------------------------------------- */
1102 edit_right_char_move_cmd (WEdit
* edit
)
1108 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
1114 c
= edit_get_byte (edit
, edit
->curs1
);
1116 if (option_cursor_beyond_eol
&& c
== '\n')
1122 edit_cursor_move (edit
, cw
);
1126 /* --------------------------------------------------------------------------------------------- */
1129 edit_left_char_move_cmd (WEdit
* edit
)
1132 if (edit
->column_highlight
1133 && option_cursor_beyond_eol
1134 && edit
->mark1
!= edit
->mark2
1135 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1139 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1143 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1149 edit_cursor_move (edit
, -cw
);
1153 /* --------------------------------------------------------------------------------------------- */
1154 /** Up or down cursor moving.
1155 direction = TRUE - move up
1160 edit_move_updown (WEdit
* edit
, long lines
, gboolean do_scroll
, gboolean direction
)
1163 long l
= direction
? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
1172 edit
->force
|= REDRAW_PAGE
;
1176 edit_scroll_upward (edit
, lines
);
1178 edit_scroll_downward (edit
, lines
);
1180 p
= edit_bol (edit
, edit
->curs1
);
1182 p
= direction
? edit_move_backward (edit
, p
, lines
) : edit_move_forward (edit
, p
, lines
, 0);
1184 edit_cursor_move (edit
, p
- edit
->curs1
);
1186 edit_move_to_prev_col (edit
, p
);
1188 /* search start of current multibyte char (like CJK) */
1189 if (edit
->curs1
+ 1 < edit
->last_byte
)
1191 edit_right_char_move_cmd (edit
);
1192 edit_left_char_move_cmd (edit
);
1195 edit
->search_start
= edit
->curs1
;
1196 edit
->found_len
= 0;
1199 /* --------------------------------------------------------------------------------------------- */
1202 edit_right_delete_word (WEdit
* edit
)
1207 if (edit
->curs1
>= edit
->last_byte
)
1209 c1
= edit_delete (edit
, 1);
1210 c2
= edit_get_byte (edit
, edit
->curs1
);
1211 if (c1
== '\n' || c2
== '\n')
1213 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1215 if (!(my_type_of (c1
) & my_type_of (c2
)))
1220 /* --------------------------------------------------------------------------------------------- */
1223 edit_left_delete_word (WEdit
* edit
)
1228 if (edit
->curs1
<= 0)
1230 c1
= edit_backspace (edit
, 1);
1231 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1232 if (c1
== '\n' || c2
== '\n')
1234 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1236 if (!(my_type_of (c1
) & my_type_of (c2
)))
1241 /* --------------------------------------------------------------------------------------------- */
1243 the start column position is not recorded, and hence does not
1244 undo as it happed. But who would notice.
1248 edit_do_undo (WEdit
* edit
)
1253 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1255 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1262 edit_cursor_move (edit
, 1);
1265 edit_cursor_move (edit
, -1);
1269 edit_backspace (edit
, 1);
1273 edit_delete (edit
, 1);
1276 edit
->column_highlight
= 1;
1279 edit
->column_highlight
= 0;
1282 if (ac
>= 256 && ac
< 512)
1283 edit_insert_ahead (edit
, ac
- 256);
1284 if (ac
>= 0 && ac
< 256)
1285 edit_insert (edit
, ac
);
1287 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1289 edit
->mark1
= ac
- MARK_1
;
1291 (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1293 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1295 edit
->mark2
= ac
- MARK_2
;
1297 (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1299 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1301 edit
->end_mark_curs
= ac
- MARK_CURS
;
1304 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1307 if (edit
->start_display
> ac
- KEY_PRESS
)
1309 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1310 edit
->force
|= REDRAW_PAGE
;
1312 else if (edit
->start_display
< ac
- KEY_PRESS
)
1314 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1315 edit
->force
|= REDRAW_PAGE
;
1317 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1318 edit_update_curs_row (edit
);
1321 edit
->undo_stack_disable
= 0;
1325 edit_do_redo (WEdit
* edit
)
1330 if (edit
->redo_stack_reset
)
1334 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1341 edit_cursor_move (edit
, 1);
1344 edit_cursor_move (edit
, -1);
1347 edit_backspace (edit
, 1);
1350 edit_delete (edit
, 1);
1353 edit
->column_highlight
= 1;
1356 edit
->column_highlight
= 0;
1359 if (ac
>= 256 && ac
< 512)
1360 edit_insert_ahead (edit
, ac
- 256);
1361 if (ac
>= 0 && ac
< 256)
1362 edit_insert (edit
, ac
);
1364 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1366 edit
->mark1
= ac
- MARK_1
;
1368 (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1370 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1372 edit
->mark2
= ac
- MARK_2
;
1374 (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1376 /* more than one pop usually means something big */
1378 edit
->force
|= REDRAW_PAGE
;
1381 if (edit
->start_display
> ac
- KEY_PRESS
)
1383 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1384 edit
->force
|= REDRAW_PAGE
;
1386 else if (edit
->start_display
< ac
- KEY_PRESS
)
1388 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1389 edit
->force
|= REDRAW_PAGE
;
1391 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1392 edit_update_curs_row (edit
);
1398 edit_group_undo (WEdit
* edit
)
1400 long ac
= KEY_PRESS
;
1401 long cur_ac
= KEY_PRESS
;
1402 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1404 cur_ac
= get_prev_undo_action (edit
);
1405 edit_do_undo (edit
);
1406 ac
= get_prev_undo_action (edit
);
1407 /* exit from cycle if option_group_undo is not set,
1408 * and make single UNDO operation
1410 if (!option_group_undo
)
1415 /* --------------------------------------------------------------------------------------------- */
1418 edit_delete_to_line_end (WEdit
* edit
)
1420 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
1424 edit_delete (edit
, 1);
1428 /* --------------------------------------------------------------------------------------------- */
1431 edit_delete_to_line_begin (WEdit
* edit
)
1433 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
1437 edit_backspace (edit
, 1);
1441 /* --------------------------------------------------------------------------------------------- */
1444 is_aligned_on_a_tab (WEdit
* edit
)
1446 edit_update_curs_col (edit
);
1447 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
1448 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
1451 /* --------------------------------------------------------------------------------------------- */
1454 right_of_four_spaces (WEdit
* edit
)
1457 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1458 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1460 return is_aligned_on_a_tab (edit
);
1464 /* --------------------------------------------------------------------------------------------- */
1467 left_of_four_spaces (WEdit
* edit
)
1470 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1471 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1473 return is_aligned_on_a_tab (edit
);
1477 /* --------------------------------------------------------------------------------------------- */
1480 edit_auto_indent (WEdit
* edit
)
1485 /* use the previous line as a template */
1486 p
= edit_move_backward (edit
, p
, 1);
1487 /* copy the leading whitespace of the line */
1489 { /* no range check - the line _is_ \n-terminated */
1490 c
= edit_get_byte (edit
, p
++);
1491 if (c
!= ' ' && c
!= '\t')
1493 edit_insert (edit
, c
);
1497 /* --------------------------------------------------------------------------------------------- */
1500 edit_double_newline (WEdit
* edit
)
1502 edit_insert (edit
, '\n');
1503 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1505 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1507 edit
->force
|= REDRAW_PAGE
;
1508 edit_insert (edit
, '\n');
1512 /* --------------------------------------------------------------------------------------------- */
1515 insert_spaces_tab (WEdit
* edit
, gboolean half
)
1519 edit_update_curs_col (edit
);
1520 i
= option_tab_spacing
* space_width
;
1523 i
= ((edit
->curs_col
/ i
) + 1) * i
- edit
->curs_col
;
1526 edit_insert (edit
, ' ');
1531 /* --------------------------------------------------------------------------------------------- */
1534 edit_tab_cmd (WEdit
* edit
)
1538 if (option_fake_half_tabs
)
1540 if (is_in_indent (edit
))
1542 /*insert a half tab (usually four spaces) unless there is a
1543 half tab already behind, then delete it and insert a
1545 if (option_fill_tabs_with_spaces
|| !right_of_four_spaces (edit
))
1546 insert_spaces_tab (edit
, TRUE
);
1549 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1550 edit_backspace (edit
, 1);
1551 edit_insert (edit
, '\t');
1556 if (option_fill_tabs_with_spaces
)
1557 insert_spaces_tab (edit
, FALSE
);
1559 edit_insert (edit
, '\t');
1562 /* --------------------------------------------------------------------------------------------- */
1565 check_and_wrap_line (WEdit
* edit
)
1570 if (!option_typewriter_wrap
)
1572 edit_update_curs_col (edit
);
1573 if (edit
->curs_col
< option_word_wrap_line_length
)
1579 c
= edit_get_byte (edit
, curs
);
1580 if (c
== '\n' || curs
<= 0)
1582 edit_insert (edit
, '\n');
1585 if (c
== ' ' || c
== '\t')
1587 off_t current
= edit
->curs1
;
1588 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
1589 edit_insert (edit
, '\n');
1590 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
1596 /* --------------------------------------------------------------------------------------------- */
1597 /** this find the matching bracket in either direction, and sets edit->bracket
1599 * @param edit editor object
1600 * @param in_screen seach only on the current screen
1601 * @param furthest_bracket_search count of the bytes for search
1603 * @return position of the found bracket (-1 if no match)
1607 edit_get_bracket (WEdit
* edit
, gboolean in_screen
, unsigned long furthest_bracket_search
)
1609 const char *const b
= "{}{[][()(", *p
;
1610 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
1611 unsigned long j
= 0;
1613 edit_update_curs_row (edit
);
1614 c
= edit_get_byte (edit
, edit
->curs1
);
1617 if (!furthest_bracket_search
)
1618 furthest_bracket_search
--;
1619 /* not on a bracket at all */
1622 /* the matching bracket */
1624 /* going left or right? */
1625 if (strchr ("{[(", c
))
1627 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
1629 /* out of buffer? */
1630 if (q
>= edit
->last_byte
|| q
< 0)
1632 a
= edit_get_byte (edit
, q
);
1633 /* don't want to eat CPU */
1634 if (j
++ > furthest_bracket_search
)
1636 /* out of screen? */
1639 if (q
< edit
->start_display
)
1641 /* count lines if searching downward */
1642 if (inc
> 0 && a
== '\n')
1643 if (n
++ >= edit
->widget
.lines
- edit
->curs_row
) /* out of screen */
1646 /* count bracket depth */
1647 i
+= (a
== c
) - (a
== d
);
1648 /* return if bracket depth is zero */
1656 /* --------------------------------------------------------------------------------------------- */
1659 edit_goto_matching_bracket (WEdit
* edit
)
1663 q
= edit_get_bracket (edit
, 0, 0);
1666 edit
->bracket
= edit
->curs1
;
1667 edit
->force
|= REDRAW_PAGE
;
1668 edit_cursor_move (edit
, q
- edit
->curs1
);
1672 /* --------------------------------------------------------------------------------------------- */
1675 edit_move_block_to_right (WEdit
* edit
)
1677 off_t start_mark
, end_mark
;
1678 long cur_bol
, start_bol
;
1680 if (eval_marks (edit
, &start_mark
, &end_mark
))
1683 start_bol
= edit_bol (edit
, start_mark
);
1684 cur_bol
= edit_bol (edit
, end_mark
- 1);
1688 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1689 if (option_fill_tabs_with_spaces
)
1690 insert_spaces_tab (edit
, option_fake_half_tabs
);
1692 edit_insert (edit
, '\t');
1693 edit_cursor_move (edit
, edit_bol (edit
, cur_bol
) - edit
->curs1
);
1698 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1700 while (cur_bol
>= start_bol
);
1702 edit
->force
|= REDRAW_PAGE
;
1705 /* --------------------------------------------------------------------------------------------- */
1708 edit_move_block_to_left (WEdit
* edit
)
1710 off_t start_mark
, end_mark
;
1711 off_t cur_bol
, start_bol
;
1714 if (eval_marks (edit
, &start_mark
, &end_mark
))
1717 start_bol
= edit_bol (edit
, start_mark
);
1718 cur_bol
= edit_bol (edit
, end_mark
- 1);
1725 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1727 if (option_fake_half_tabs
)
1728 del_tab_width
= HALF_TAB_SIZE
;
1730 del_tab_width
= option_tab_spacing
;
1732 next_char
= edit_get_byte (edit
, edit
->curs1
);
1733 if (next_char
== '\t')
1734 edit_delete (edit
, 1);
1735 else if (next_char
== ' ')
1736 for (i
= 1; i
<= del_tab_width
; i
++)
1738 if (next_char
== ' ')
1739 edit_delete (edit
, 1);
1740 next_char
= edit_get_byte (edit
, edit
->curs1
);
1746 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1748 while (cur_bol
>= start_bol
);
1750 edit
->force
|= REDRAW_PAGE
;
1753 /* --------------------------------------------------------------------------------------------- */
1755 * prints at the cursor
1756 * @returns the number of chars printed
1760 edit_print_string (WEdit
* e
, const char *s
)
1764 while (s
[i
] != '\0')
1765 edit_execute_cmd (e
, CK_InsertChar
, (unsigned char) s
[i
++]);
1766 e
->force
|= REDRAW_COMPLETELY
;
1767 edit_update_screen (e
);
1771 /* --------------------------------------------------------------------------------------------- */
1772 /*** public functions ****************************************************************************/
1773 /* --------------------------------------------------------------------------------------------- */
1775 /** User edit menu, like user menu (F2) but only in editor. */
1778 user_menu (WEdit
* edit
, const char *menu_file
, int selected_entry
)
1783 off_t start_mark
, end_mark
;
1785 vfs_path_t
*block_file_vpath
;
1787 block_file
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
1788 block_file_vpath
= vfs_path_from_str (block_file
);
1790 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
1792 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1794 /* run shell scripts from menu */
1795 if (user_menu_cmd (edit
, menu_file
, selected_entry
)
1796 && (mc_stat (block_file_vpath
, &status
) == 0) && (status
.st_size
!= 0))
1801 /* i.e. we have marked block */
1803 rc
= edit_block_delete_cmd (edit
);
1809 ins_len
= edit_insert_file (edit
, block_file_vpath
);
1810 if (nomark
== 0 && ins_len
> 0)
1811 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1813 /* truncate block file */
1814 fd
= fopen (block_file
, "w");
1818 g_free (block_file
);
1819 vfs_path_free (block_file_vpath
);
1821 edit_cursor_move (edit
, curs
- edit
->curs1
);
1822 edit
->force
|= REDRAW_PAGE
;
1823 send_message ((Widget
*) edit
, WIDGET_DRAW
, 0);
1826 /* --------------------------------------------------------------------------------------------- */
1829 edit_get_byte (WEdit
* edit
, off_t byte_index
)
1832 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1835 if (byte_index
>= edit
->curs1
)
1837 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1838 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
1842 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
1846 /* --------------------------------------------------------------------------------------------- */
1849 edit_get_utf (WEdit
* edit
, off_t byte_index
, int *char_width
)
1854 gchar
*next_ch
= NULL
;
1856 gchar utf8_buf
[UTF8_CHAR_LEN
+ 1];
1858 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1864 str
= edit_get_byte_ptr (edit
, byte_index
);
1872 res
= g_utf8_get_char_validated (str
, -1);
1876 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1878 for (i
= 0; i
< UTF8_CHAR_LEN
; i
++)
1879 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
);
1880 utf8_buf
[UTF8_CHAR_LEN
] = '\0';
1882 res
= g_utf8_get_char_validated (str
, -1);
1893 /* Calculate UTF-8 char width */
1894 next_ch
= g_utf8_next_char (str
);
1897 width
= next_ch
- str
;
1905 *char_width
= width
;
1909 /* --------------------------------------------------------------------------------------------- */
1912 edit_get_write_filter (const vfs_path_t
* write_name_vpath
, const vfs_path_t
* filename_vpath
)
1915 char *p
, *writename
;
1916 const vfs_path_element_t
*path_element
;
1918 i
= edit_find_filter (filename_vpath
);
1922 path_element
= vfs_path_get_by_index (write_name_vpath
, -1);
1923 writename
= name_quote (path_element
->path
, 0);
1924 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
1929 /* --------------------------------------------------------------------------------------------- */
1931 * @param edit editor object
1932 * @param f value of stream file
1933 * @returns the length of the file
1937 edit_write_stream (WEdit
* edit
, FILE * f
)
1941 if (edit
->lb
== LB_ASIS
)
1943 for (i
= 0; i
< edit
->last_byte
; i
++)
1944 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
1949 /* change line breaks */
1950 for (i
= 0; i
< edit
->last_byte
; i
++)
1952 unsigned char c
= edit_get_byte (edit
, i
);
1954 if (!(c
== '\n' || c
== '\r'))
1956 /* not line break */
1957 if (fputc (c
, f
) < 0)
1961 { /* (c == '\n' || c == '\r') */
1962 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
1966 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
1967 /* put one line break unconditionally */
1968 if (fputc ('\n', f
) < 0)
1971 i
++; /* 2 chars are processed */
1973 if (c
== '\r' && c1
== '\n')
1974 /* Windows line break; go to the next char */
1977 if (c
== '\r' && c1
== '\r')
1979 /* two Macintosh line breaks; put second line break */
1980 if (fputc ('\n', f
) < 0)
1985 if (fputc (c1
, f
) < 0)
1989 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
1990 /* put one line break unconditionally */
1991 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
1994 if (c
== '\r' && c1
== '\n')
1995 /* Windows line break; go to the next char */
1999 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
2000 /* put one line break unconditionally */
2001 if (fputc ('\r', f
) < 0)
2004 i
++; /* 2 chars are processed */
2006 if (c
== '\r' && c1
== '\n')
2007 /* Windows line break; go to the next char */
2010 if (c
== '\n' && c1
== '\n')
2012 /* two Windows line breaks; put second line break */
2013 if (fputc ('\r', f
) < 0)
2018 if (fputc (c1
, f
) < 0)
2021 case LB_ASIS
: /* default without changes */
2027 return edit
->last_byte
;
2030 /* --------------------------------------------------------------------------------------------- */
2033 is_break_char (char c
)
2035 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
2038 /* --------------------------------------------------------------------------------------------- */
2041 edit_get_word_from_pos (WEdit
* edit
, off_t start_pos
, off_t
* start
, gsize
* len
, gsize
* cut
)
2045 GString
*match_expr
;
2046 unsigned char *bufpos
;
2049 for (word_start
= start_pos
; word_start
!= 0; word_start
--, cut_len
++)
2051 c1
= edit_get_byte (edit
, word_start
);
2052 c2
= edit_get_byte (edit
, word_start
- 1);
2054 if (is_break_char (c1
) != is_break_char (c2
) || c1
== '\n' || c2
== '\n')
2058 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
2059 match_expr
= g_string_sized_new (16);
2063 c1
= edit_get_byte (edit
, word_start
+ match_expr
->len
);
2064 c2
= edit_get_byte (edit
, word_start
+ match_expr
->len
+ 1);
2065 g_string_append_c (match_expr
, c1
);
2067 while (!(is_break_char (c1
) != is_break_char (c2
) || c1
== '\n' || c2
== '\n'));
2069 *len
= match_expr
->len
;
2070 *start
= word_start
;
2073 return g_string_free (match_expr
, FALSE
);
2076 /* --------------------------------------------------------------------------------------------- */
2077 /** inserts a file at the cursor, returns count of inserted bytes on success */
2080 edit_insert_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
2085 p
= edit_get_filter (filename_vpath
);
2089 off_t current
= edit
->curs1
;
2091 f
= (FILE *) popen (p
, "r");
2094 edit_insert_stream (edit
, f
);
2095 ins_len
= edit
->curs1
- current
;
2096 edit_cursor_move (edit
, -ins_len
);
2101 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
2102 edit_error_dialog (_("Error"), errmsg
);
2111 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
2112 edit_error_dialog (_("Error"), errmsg
);
2122 off_t current
= edit
->curs1
;
2123 int vertical_insertion
= 0;
2126 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
2130 buf
= g_malloc0 (TEMP_BUF_LEN
);
2131 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2134 /* if contain signature VERTICAL_MAGIC then it vertical block */
2135 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2136 vertical_insertion
= 1;
2138 mc_lseek (file
, 0, SEEK_SET
);
2141 if (vertical_insertion
)
2146 blocklen
= edit_insert_column_of_text_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
2147 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2148 /* highlight inserted text then not persistent blocks */
2149 if (!option_persistent_selections
)
2151 if (!edit
->column_highlight
)
2152 edit_push_undo_action (edit
, COLUMN_OFF
);
2153 edit
->column_highlight
= 1;
2160 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2162 for (i
= 0; i
< blocklen
; i
++)
2163 edit_insert (edit
, buf
[i
]);
2165 /* highlight inserted text then not persistent blocks */
2166 if (!option_persistent_selections
&& edit
->modified
)
2168 edit_set_markers (edit
, edit
->curs1
, current
, 0, 0);
2169 if (edit
->column_highlight
)
2170 edit_push_undo_action (edit
, COLUMN_ON
);
2171 edit
->column_highlight
= 0;
2175 edit
->force
|= REDRAW_PAGE
;
2176 ins_len
= edit
->curs1
- current
;
2177 edit_cursor_move (edit
, -ins_len
);
2187 /* --------------------------------------------------------------------------------------------- */
2189 * Fill in the edit structure. Return NULL on failure. Pass edit as
2190 * NULL to allocate a new structure.
2192 * If line is 0, try to restore saved position. Otherwise put the
2193 * cursor on that line and show it in the middle of the screen.
2197 edit_init (WEdit
* edit
, int y
, int x
, int lines
, int cols
, const vfs_path_t
* filename_vpath
,
2200 gboolean to_free
= FALSE
;
2202 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2203 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
2206 edit_purge_widget (edit
);
2211 * Expand option_whole_chars_search by national letters using
2215 static char option_whole_chars_search_buf
[256];
2217 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
2220 size_t len
= str_term_width1 (option_whole_chars_search
);
2222 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
2224 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
2226 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
2228 option_whole_chars_search_buf
[len
++] = i
;
2232 option_whole_chars_search_buf
[len
] = 0;
2233 option_whole_chars_search
= option_whole_chars_search_buf
;
2235 #endif /* ENABLE_NLS */
2236 edit
= g_malloc0 (sizeof (WEdit
));
2240 edit
->drag_state
= MCEDIT_DRAG_NORMAL
;
2243 edit
->widget
.lines
= lines
;
2244 edit
->widget
.cols
= cols
;
2245 edit_save_size (edit
);
2246 edit
->fullscreen
= TRUE
;
2248 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2249 edit
->stat1
.st_uid
= getuid ();
2250 edit
->stat1
.st_gid
= getgid ();
2251 edit
->stat1
.st_mtime
= 0;
2255 edit
->force
|= REDRAW_PAGE
;
2257 /* set file name before load file */
2258 edit_set_filename (edit
, filename_vpath
);
2260 edit
->undo_stack_size
= START_STACK_SIZE
;
2261 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2262 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2264 edit
->redo_stack_size
= START_STACK_SIZE
;
2265 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2266 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2269 edit
->converter
= str_cnv_from_term
;
2270 edit_set_codeset (edit
);
2272 if (!edit_load_file (edit
))
2274 /* edit_load_file already gives an error message */
2280 edit
->loading_done
= 1;
2283 edit_load_syntax (edit
, NULL
, NULL
);
2286 edit_get_syntax_color (edit
, -1, &color
);
2289 /* load saved cursor position */
2290 if ((line
== 0) && option_save_position
)
2291 edit_load_position (edit
);
2296 edit_move_display (edit
, line
- 1);
2297 edit_move_to_line (edit
, line
- 1);
2300 edit_load_macro_cmd (edit
);
2305 /* --------------------------------------------------------------------------------------------- */
2307 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2309 edit_clean (WEdit
* edit
)
2316 /* a stale lock, remove it */
2318 edit
->locked
= unlock_file (edit
->filename_vpath
);
2320 /* save cursor position */
2321 if (option_save_position
)
2322 edit_save_position (edit
);
2323 else if (edit
->serialized_bookmarks
!= NULL
)
2324 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2326 /* File specified on the mcedit command line and never saved */
2327 if (edit
->delete_file
)
2328 unlink (vfs_path_get_last_path_str (edit
->filename_vpath
));
2330 edit_free_syntax_rules (edit
);
2331 book_mark_flush (edit
, -1);
2332 for (; j
<= MAXBUFF
; j
++)
2334 g_free (edit
->buffers1
[j
]);
2335 g_free (edit
->buffers2
[j
]);
2338 g_free (edit
->undo_stack
);
2339 g_free (edit
->redo_stack
);
2340 vfs_path_free (edit
->filename_vpath
);
2341 vfs_path_free (edit
->dir_vpath
);
2342 mc_search_free (edit
->search
);
2343 edit
->search
= NULL
;
2345 if (edit
->converter
!= str_cnv_from_term
)
2346 str_close_conv (edit
->converter
);
2348 edit_purge_widget (edit
);
2353 /* --------------------------------------------------------------------------------------------- */
2356 * Load a new file into the editor and set line. If it fails, preserve the old file.
2357 * To do it, allocate a new widget, initialize it and, if the new file
2358 * was loaded, copy the data to the old widget.
2360 * @returns TRUE on success, FALSE on failure.
2363 edit_reload_line (WEdit
* edit
, const vfs_path_t
* filename_vpath
, long line
)
2366 int y
= edit
->widget
.y
;
2367 int x
= edit
->widget
.x
;
2368 int lines
= edit
->widget
.lines
;
2369 int columns
= edit
->widget
.cols
;
2371 e
= g_malloc0 (sizeof (WEdit
));
2372 e
->widget
= edit
->widget
;
2374 if (edit_init (e
, y
, x
, lines
, columns
, filename_vpath
, line
) == NULL
)
2381 memcpy (edit
, e
, sizeof (WEdit
));
2387 /* --------------------------------------------------------------------------------------------- */
2390 edit_set_codeset (WEdit
* edit
)
2396 get_codepage_id (mc_global
.source_codepage
>=
2397 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2402 conv
= str_crt_conv_from (cp_id
);
2403 if (conv
!= INVALID_CONV
)
2405 if (edit
->converter
!= str_cnv_from_term
)
2406 str_close_conv (edit
->converter
);
2407 edit
->converter
= conv
;
2412 edit
->utf8
= str_isutf8 (cp_id
);
2419 /* --------------------------------------------------------------------------------------------- */
2421 Recording stack for undo:
2422 The following is an implementation of a compressed stack. Identical
2423 pushes are recorded by a negative prefix indicating the number of times the
2424 same char was pushed. This saves space for repeated curs-left or curs-right
2441 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2442 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2443 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2444 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2447 The only way the cursor moves or the buffer is changed is through the routines:
2448 insert, backspace, insert_ahead, delete, and cursor_move.
2449 These record the reverse undo movements onto the stack each time they are
2452 Each key press results in a set of actions (insert; delete ...). So each time
2453 a key is pressed the current position of start_display is pushed as
2454 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2455 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2456 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2461 edit_push_undo_action (WEdit
* edit
, long c
, ...)
2463 unsigned long sp
= edit
->undo_stack_pointer
;
2467 /* first enlarge the stack if necessary */
2468 if (sp
> edit
->undo_stack_size
- 10)
2470 if (option_max_undo
< 256)
2471 option_max_undo
= 256;
2472 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2474 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2477 edit
->undo_stack
= t
;
2478 edit
->undo_stack_size
<<= 1;
2479 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2483 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2484 if (edit
->undo_stack_disable
)
2486 edit_push_redo_action (edit
, KEY_PRESS
);
2487 edit_push_redo_action (edit
, c
);
2490 else if (edit
->redo_stack_reset
)
2492 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2495 if (edit
->undo_stack_bottom
!= sp
2496 && spm1
!= edit
->undo_stack_bottom
2497 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2500 if (edit
->undo_stack
[spm1
] < 0)
2502 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2505 if (edit
->undo_stack
[spm1
] > -1000000000)
2507 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2509 edit
->undo_stack
[spm1
]--;
2517 d
= edit
->undo_stack
[spm1
];
2521 return; /* --> no need to push multiple do-nothings */
2522 edit
->undo_stack
[sp
] = -2;
2527 edit
->undo_stack
[sp
] = c
;
2530 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2532 /* if the sp wraps round and catches the undo_stack_bottom then erase
2533 * the first set of actions on the stack to make space - by moving
2534 * undo_stack_bottom forward one "key press" */
2535 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2536 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2537 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2540 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2542 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2543 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2545 /*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: */
2546 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2547 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2549 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2554 edit_push_redo_action (WEdit
* edit
, long c
, ...)
2556 unsigned long sp
= edit
->redo_stack_pointer
;
2559 /* first enlarge the stack if necessary */
2560 if (sp
> edit
->redo_stack_size
- 10)
2562 if (option_max_undo
< 256)
2563 option_max_undo
= 256;
2564 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2566 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2569 edit
->redo_stack
= t
;
2570 edit
->redo_stack_size
<<= 1;
2571 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2575 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2577 if (edit
->redo_stack_bottom
!= sp
2578 && spm1
!= edit
->redo_stack_bottom
2579 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2582 if (edit
->redo_stack
[spm1
] < 0)
2584 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2587 if (edit
->redo_stack
[spm1
] > -1000000000)
2589 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2590 edit
->redo_stack
[spm1
]--;
2597 d
= edit
->redo_stack
[spm1
];
2601 return; /* --> no need to push multiple do-nothings */
2602 edit
->redo_stack
[sp
] = -2;
2603 goto redo_check_bottom
;
2607 edit
->redo_stack
[sp
] = c
;
2610 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2612 /* if the sp wraps round and catches the redo_stack_bottom then erase
2613 * the first set of actions on the stack to make space - by moving
2614 * redo_stack_bottom forward one "key press" */
2615 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2616 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2617 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2620 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2622 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2623 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2626 * If a single key produced enough pushes to wrap all the way round then
2627 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2628 * The stack is then initialised:
2631 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2632 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2633 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2637 /* --------------------------------------------------------------------------------------------- */
2639 Basic low level single character buffer alterations and movements at the cursor.
2640 Returns char passed over, inserted or removed.
2644 edit_insert (WEdit
* edit
, int c
)
2646 /* check if file has grown to large */
2647 if (edit
->last_byte
>= SIZE_LIMIT
)
2650 /* first we must update the position of the display window */
2651 if (edit
->curs1
< edit
->start_display
)
2653 edit
->start_display
++;
2658 /* Mark file as modified, unless the file hasn't been fully loaded */
2659 if (edit
->loading_done
)
2661 edit_modification (edit
);
2664 /* now we must update some info on the file and check if a redraw is required */
2667 if (edit
->book_mark
)
2668 book_mark_inc (edit
, edit
->curs_line
);
2670 edit
->total_lines
++;
2671 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2674 /* save the reverse command onto the undo stack */
2675 /* ordinary char and not space */
2677 edit_push_undo_action (edit
, BACKSPACE
);
2679 edit_push_undo_action (edit
, BACKSPACE_BR
);
2680 /* update markers */
2681 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2682 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2683 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2685 /* add a new buffer if we've reached the end of the last one */
2686 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2687 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2689 /* perform the insertion */
2690 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2691 = (unsigned char) c
;
2693 /* update file length */
2696 /* update cursor position */
2700 /* --------------------------------------------------------------------------------------------- */
2701 /** same as edit_insert and move left */
2704 edit_insert_ahead (WEdit
* edit
, int c
)
2706 if (edit
->last_byte
>= SIZE_LIMIT
)
2709 if (edit
->curs1
< edit
->start_display
)
2711 edit
->start_display
++;
2715 edit_modification (edit
);
2718 if (edit
->book_mark
)
2719 book_mark_inc (edit
, edit
->curs_line
);
2720 edit
->total_lines
++;
2721 edit
->force
|= REDRAW_AFTER_CURSOR
;
2723 /* ordinary char and not space */
2725 edit_push_undo_action (edit
, DELCHAR
);
2727 edit_push_undo_action (edit
, DELCHAR_BR
);
2729 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2730 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2731 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2733 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2734 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2735 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2736 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2743 /* --------------------------------------------------------------------------------------------- */
2746 edit_delete (WEdit
* edit
, const int byte_delete
)
2756 /* if byte_delete = 1 then delete only one byte not multibyte char */
2757 if (edit
->utf8
&& byte_delete
== 0)
2759 edit_get_utf (edit
, edit
->curs1
, &cw
);
2764 if (edit
->mark2
!= edit
->mark1
)
2765 edit_push_markers (edit
);
2767 for (i
= 1; i
<= cw
; i
++)
2769 if (edit
->mark1
> edit
->curs1
)
2772 edit
->end_mark_curs
--;
2774 if (edit
->mark2
> edit
->curs1
)
2776 if (edit
->last_get_rule
> edit
->curs1
)
2777 edit
->last_get_rule
--;
2779 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2781 1) & M_EDIT_BUF_SIZE
) - 1];
2783 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2785 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2786 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2790 edit_push_undo_action (edit
, p
+ 256);
2793 edit_modification (edit
);
2796 if (edit
->book_mark
)
2797 book_mark_dec (edit
, edit
->curs_line
);
2798 edit
->total_lines
--;
2799 edit
->force
|= REDRAW_AFTER_CURSOR
;
2801 if (edit
->curs1
< edit
->start_display
)
2803 edit
->start_display
--;
2811 /* --------------------------------------------------------------------------------------------- */
2814 edit_backspace (WEdit
* edit
, const int byte_delete
)
2820 if (edit
->curs1
== 0)
2825 if (edit
->mark2
!= edit
->mark1
)
2826 edit_push_markers (edit
);
2828 if (edit
->utf8
&& byte_delete
== 0)
2830 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
2834 for (i
= 1; i
<= cw
; i
++)
2836 if (edit
->mark1
>= edit
->curs1
)
2839 edit
->end_mark_curs
--;
2841 if (edit
->mark2
>= edit
->curs1
)
2843 if (edit
->last_get_rule
>= edit
->curs1
)
2844 edit
->last_get_rule
--;
2846 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
2847 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
2848 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2850 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2851 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2855 edit_push_undo_action (edit
, p
);
2857 edit_modification (edit
);
2860 if (edit
->book_mark
)
2861 book_mark_dec (edit
, edit
->curs_line
);
2863 edit
->total_lines
--;
2864 edit
->force
|= REDRAW_AFTER_CURSOR
;
2867 if (edit
->curs1
< edit
->start_display
)
2869 edit
->start_display
--;
2877 /* --------------------------------------------------------------------------------------------- */
2878 /** moves the cursor right or left: increment positive or negative respectively */
2881 edit_cursor_move (WEdit
* edit
, off_t increment
)
2883 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2888 for (; increment
< 0; increment
++)
2890 if (edit
->curs1
== 0)
2893 edit_push_undo_action (edit
, CURS_RIGHT
);
2895 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2896 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2897 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2898 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2899 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2901 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2902 1) & M_EDIT_BUF_SIZE
];
2903 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2905 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2906 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2912 edit
->force
|= REDRAW_LINE_BELOW
;
2917 else if (increment
> 0)
2919 for (; increment
> 0; increment
--)
2921 if (edit
->curs2
== 0)
2924 edit_push_undo_action (edit
, CURS_LEFT
);
2926 c
= edit_get_byte (edit
, edit
->curs1
);
2927 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2928 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2929 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2931 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2933 1) & M_EDIT_BUF_SIZE
) - 1];
2934 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2936 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2937 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2943 edit
->force
|= REDRAW_LINE_ABOVE
;
2949 /* These functions return positions relative to lines */
2951 /* --------------------------------------------------------------------------------------------- */
2952 /** returns index of last char on line + 1 */
2955 edit_eol (WEdit
* edit
, off_t current
)
2957 if (current
>= edit
->last_byte
)
2958 return edit
->last_byte
;
2961 if (edit_get_byte (edit
, current
) == '\n')
2966 /* --------------------------------------------------------------------------------------------- */
2967 /** returns index of first char on line */
2970 edit_bol (WEdit
* edit
, off_t current
)
2976 if (edit_get_byte (edit
, current
- 1) == '\n')
2981 /* --------------------------------------------------------------------------------------------- */
2984 edit_count_lines (WEdit
* edit
, off_t current
, off_t upto
)
2987 if (upto
> edit
->last_byte
)
2988 upto
= edit
->last_byte
;
2991 while (current
< upto
)
2992 if (edit_get_byte (edit
, current
++) == '\n')
2997 /* --------------------------------------------------------------------------------------------- */
2998 /* If lines is zero this returns the count of lines from current to upto. */
2999 /* If upto is zero returns index of lines forward current. */
3002 edit_move_forward (WEdit
* edit
, off_t current
, long lines
, off_t upto
)
3006 return (off_t
) edit_count_lines (edit
, current
, upto
);
3015 next
= edit_eol (edit
, current
) + 1;
3016 if (next
> edit
->last_byte
)
3025 /* --------------------------------------------------------------------------------------------- */
3026 /** Returns offset of 'lines' lines up from current */
3029 edit_move_backward (WEdit
* edit
, off_t current
, long lines
)
3033 current
= edit_bol (edit
, current
);
3034 while ((lines
--) && current
!= 0)
3035 current
= edit_bol (edit
, current
- 1);
3039 /* --------------------------------------------------------------------------------------------- */
3040 /* If cols is zero this returns the count of columns from current to upto. */
3041 /* If upto is zero returns index of cols across from current. */
3044 edit_move_forward3 (WEdit
* edit
, off_t current
, long cols
, off_t upto
)
3055 q
= edit
->last_byte
+ 2;
3057 for (col
= 0, p
= current
; p
< q
; p
++)
3069 orig_c
= c
= edit_get_byte (edit
, p
);
3077 utf_ch
= edit_get_utf (edit
, p
, &cw
);
3078 if (mc_global
.utf8_display
)
3082 if (g_unichar_iswide (utf_ch
))
3085 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
3089 c
= convert_to_display_c (c
);
3093 col
+= TAB_SIZE
- col
% TAB_SIZE
;
3101 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!mc_global
.utf8_display
&& !edit
->utf8
)))
3102 /* '\r' is shown as ^M, so we must advance 2 characters */
3103 /* Caret notation for control characters */
3111 /* --------------------------------------------------------------------------------------------- */
3112 /** returns the current column position of the cursor */
3115 edit_get_col (WEdit
* edit
)
3117 return (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3120 /* --------------------------------------------------------------------------------------------- */
3121 /* Scrolling functions */
3122 /* --------------------------------------------------------------------------------------------- */
3125 edit_update_curs_row (WEdit
* edit
)
3127 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
3130 /* --------------------------------------------------------------------------------------------- */
3133 edit_update_curs_col (WEdit
* edit
)
3135 edit
->curs_col
= (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3138 /* --------------------------------------------------------------------------------------------- */
3141 edit_get_curs_col (const WEdit
* edit
)
3143 return edit
->curs_col
;
3146 /* --------------------------------------------------------------------------------------------- */
3147 /** moves the display start position up by i lines */
3150 edit_scroll_upward (WEdit
* edit
, long i
)
3152 long lines_above
= edit
->start_line
;
3154 if (i
> lines_above
)
3158 edit
->start_line
-= i
;
3159 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
3160 edit
->force
|= REDRAW_PAGE
;
3161 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3163 edit_update_curs_row (edit
);
3167 /* --------------------------------------------------------------------------------------------- */
3170 edit_scroll_downward (WEdit
* edit
, long i
)
3174 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->widget
.lines
- 1);
3175 if (lines_below
> 0)
3177 if (i
> lines_below
)
3179 edit
->start_line
+= i
;
3180 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
3181 edit
->force
|= REDRAW_PAGE
;
3182 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3184 edit_update_curs_row (edit
);
3187 /* --------------------------------------------------------------------------------------------- */
3190 edit_scroll_right (WEdit
* edit
, long i
)
3192 edit
->force
|= REDRAW_PAGE
;
3193 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3194 edit
->start_col
-= i
;
3197 /* --------------------------------------------------------------------------------------------- */
3200 edit_scroll_left (WEdit
* edit
, long i
)
3202 if (edit
->start_col
)
3204 edit
->start_col
+= i
;
3205 if (edit
->start_col
> 0)
3206 edit
->start_col
= 0;
3207 edit
->force
|= REDRAW_PAGE
;
3208 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3212 /* --------------------------------------------------------------------------------------------- */
3213 /* high level cursor movement commands */
3214 /* --------------------------------------------------------------------------------------------- */
3217 edit_move_to_prev_col (WEdit
* edit
, off_t p
)
3219 long prev
= edit
->prev_col
;
3220 long over
= edit
->over_col
;
3222 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
3224 if (option_cursor_beyond_eol
)
3228 line_len
= (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
3229 edit_eol (edit
, edit
->curs1
));
3230 if (line_len
< prev
+ edit
->over_col
)
3232 edit
->over_col
= prev
+ over
- line_len
;
3233 edit
->prev_col
= line_len
;
3234 edit
->curs_col
= line_len
;
3238 edit
->curs_col
= prev
+ over
;
3239 edit
->prev_col
= edit
->curs_col
;
3246 if (is_in_indent (edit
) && option_fake_half_tabs
)
3248 edit_update_curs_col (edit
);
3251 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
3256 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
3257 p
= edit_bol (edit
, edit
->curs1
);
3259 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->curs_col
, 0)
3262 if (!left_of_four_spaces (edit
))
3263 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3270 /* --------------------------------------------------------------------------------------------- */
3271 /** check whether line in editor is blank or not
3273 * @param edit editor object
3274 * @param line number of line
3276 * @return TRUE if line in blank, FALSE otherwise
3280 line_is_blank (WEdit
* edit
, long line
)
3282 return is_blank (edit
, edit_find_line (edit
, line
));
3285 /* --------------------------------------------------------------------------------------------- */
3286 /** move cursor to line 'line' */
3289 edit_move_to_line (WEdit
* e
, long line
)
3291 if (line
< e
->curs_line
)
3292 edit_move_up (e
, e
->curs_line
- line
, 0);
3294 edit_move_down (e
, line
- e
->curs_line
, 0);
3295 edit_scroll_screen_over_cursor (e
);
3298 /* --------------------------------------------------------------------------------------------- */
3299 /** scroll window so that first visible line is 'line' */
3302 edit_move_display (WEdit
* e
, long line
)
3304 if (line
< e
->start_line
)
3305 edit_scroll_upward (e
, e
->start_line
- line
);
3307 edit_scroll_downward (e
, line
- e
->start_line
);
3310 /* --------------------------------------------------------------------------------------------- */
3311 /** save markers onto undo stack */
3314 edit_push_markers (WEdit
* edit
)
3316 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3317 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3318 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3321 /* --------------------------------------------------------------------------------------------- */
3324 edit_set_markers (WEdit
* edit
, off_t m1
, off_t m2
, long c1
, long c2
)
3333 /* --------------------------------------------------------------------------------------------- */
3334 /** highlight marker toggle */
3337 edit_mark_cmd (WEdit
* edit
, gboolean unmark
)
3339 edit_push_markers (edit
);
3342 edit_set_markers (edit
, 0, 0, 0, 0);
3343 edit
->force
|= REDRAW_PAGE
;
3347 if (edit
->mark2
>= 0)
3349 edit
->end_mark_curs
= -1;
3350 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3351 edit
->curs_col
+ edit
->over_col
);
3352 edit
->force
|= REDRAW_PAGE
;
3356 edit
->end_mark_curs
= edit
->curs1
;
3357 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3358 edit
->curs_col
+ edit
->over_col
);
3363 /* --------------------------------------------------------------------------------------------- */
3364 /** highlight the word under cursor */
3367 edit_mark_current_word_cmd (WEdit
* edit
)
3371 for (pos
= edit
->curs1
; pos
!= 0; pos
--)
3375 c1
= edit_get_byte (edit
, pos
);
3376 c2
= edit_get_byte (edit
, pos
- 1);
3377 if (!isspace (c1
) && isspace (c2
))
3379 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3384 for (; pos
< edit
->last_byte
; pos
++)
3388 c1
= edit_get_byte (edit
, pos
);
3389 c2
= edit_get_byte (edit
, pos
+ 1);
3390 if (!isspace (c1
) && isspace (c2
))
3392 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3395 edit
->mark2
= min (pos
+ 1, edit
->last_byte
);
3397 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3400 /* --------------------------------------------------------------------------------------------- */
3403 edit_mark_current_line_cmd (WEdit
* edit
)
3405 long pos
= edit
->curs1
;
3407 edit
->mark1
= edit_bol (edit
, pos
);
3408 edit
->mark2
= edit_eol (edit
, pos
);
3410 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3413 /* --------------------------------------------------------------------------------------------- */
3416 edit_delete_line (WEdit
* edit
)
3419 * Delete right part of the line.
3420 * Note that edit_get_byte() returns '\n' when byte position is
3423 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3425 (void) edit_delete (edit
, 1);
3430 * Note that edit_delete() will not corrupt anything if called while
3431 * cursor position is EOF.
3433 (void) edit_delete (edit
, 1);
3436 * Delete left part of the line.
3437 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3439 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3441 (void) edit_backspace (edit
, 1);
3445 /* --------------------------------------------------------------------------------------------- */
3448 edit_indent_width (WEdit
* edit
, off_t p
)
3452 /* move to the end of the leading whitespace of the line */
3453 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1)
3455 /* count the number of columns of indentation */
3456 return (long) edit_move_forward3 (edit
, p
, 0, q
);
3459 /* --------------------------------------------------------------------------------------------- */
3462 edit_insert_indent (WEdit
* edit
, int indent
)
3464 if (!option_fill_tabs_with_spaces
)
3466 while (indent
>= TAB_SIZE
)
3468 edit_insert (edit
, '\t');
3472 while (indent
-- > 0)
3473 edit_insert (edit
, ' ');
3476 /* --------------------------------------------------------------------------------------------- */
3479 edit_push_key_press (WEdit
* edit
)
3481 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3482 if (edit
->mark2
== -1)
3484 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3485 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3489 /* --------------------------------------------------------------------------------------------- */
3492 edit_find_bracket (WEdit
* edit
)
3494 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3495 if (last_bracket
!= edit
->bracket
)
3496 edit
->force
|= REDRAW_PAGE
;
3497 last_bracket
= edit
->bracket
;
3500 /* --------------------------------------------------------------------------------------------- */
3502 * This executes a command as though the user initiated it through a key
3503 * press. Callback with WIDGET_KEY as a message calls this after
3504 * translating the key press. This function can be used to pass any
3505 * command to the editor. Note that the screen wouldn't update
3506 * automatically. Either of command or char_for_insertion must be
3507 * passed as -1. Commands are executed, and char_for_insertion is
3508 * inserted at the cursor.
3512 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3514 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3516 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3519 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3522 if (macro_index
!= -1)
3524 edit
->force
|= REDRAW_COMPLETELY
;
3525 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3527 edit_store_macro_cmd (edit
);
3531 else if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3533 edit_repeat_macro_cmd (edit
);
3539 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3541 record_macro_buf
[macro_index
].action
= command
;
3542 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3544 /* record the beginning of a set of editing actions initiated by a key press */
3545 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3546 edit_push_key_press (edit
);
3548 edit_execute_cmd (edit
, command
, char_for_insertion
);
3549 if (edit
->column_highlight
)
3550 edit
->force
|= REDRAW_PAGE
;
3553 /* --------------------------------------------------------------------------------------------- */
3555 This executes a command at a lower level than macro recording.
3556 It also does not push a key_press onto the undo stack. This means
3557 that if it is called many times, a single undo command will undo
3558 all of them. It also does not check for the Undo command.
3561 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3563 if (command
== CK_WindowFullscreen
)
3565 edit_toggle_fullscreen (edit
);
3569 /* handle window state */
3570 if (edit_handle_move_resize (edit
, command
))
3573 edit
->force
|= REDRAW_LINE
;
3575 /* The next key press will unhighlight the found string, so update
3577 if (edit
->found_len
|| edit
->column_highlight
)
3578 edit
->force
|= REDRAW_PAGE
;
3582 /* a mark command with shift-arrow */
3585 case CK_MarkToWordBegin
:
3586 case CK_MarkToWordEnd
:
3592 case CK_MarkPageDown
:
3593 case CK_MarkToFileBegin
:
3594 case CK_MarkToFileEnd
:
3595 case CK_MarkToPageBegin
:
3596 case CK_MarkToPageEnd
:
3597 case CK_MarkScrollUp
:
3598 case CK_MarkScrollDown
:
3599 case CK_MarkParagraphUp
:
3600 case CK_MarkParagraphDown
:
3601 /* a mark command with alt-arrow */
3602 case CK_MarkColumnPageUp
:
3603 case CK_MarkColumnPageDown
:
3604 case CK_MarkColumnLeft
:
3605 case CK_MarkColumnRight
:
3606 case CK_MarkColumnUp
:
3607 case CK_MarkColumnDown
:
3608 case CK_MarkColumnScrollUp
:
3609 case CK_MarkColumnScrollDown
:
3610 case CK_MarkColumnParagraphUp
:
3611 case CK_MarkColumnParagraphDown
:
3612 edit
->column_highlight
= 0;
3613 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3615 edit_mark_cmd (edit
, TRUE
); /* clear */
3616 edit_mark_cmd (edit
, FALSE
); /* marking on */
3618 edit
->highlight
= 1;
3621 /* any other command */
3623 if (edit
->highlight
)
3624 edit_mark_cmd (edit
, FALSE
); /* clear */
3625 edit
->highlight
= 0;
3628 /* first check for undo */
3629 if (command
== CK_Undo
)
3631 edit
->redo_stack_reset
= 0;
3632 edit_group_undo (edit
);
3633 edit
->found_len
= 0;
3634 edit
->prev_col
= edit_get_col (edit
);
3635 edit
->search_start
= edit
->curs1
;
3638 /* check for redo */
3639 if (command
== CK_Redo
)
3641 edit
->redo_stack_reset
= 0;
3642 edit_do_redo (edit
);
3643 edit
->found_len
= 0;
3644 edit
->prev_col
= edit_get_col (edit
);
3645 edit
->search_start
= edit
->curs1
;
3649 edit
->redo_stack_reset
= 1;
3651 /* An ordinary key press */
3652 if (char_for_insertion
>= 0)
3654 /* if non persistent selection and text selected */
3655 if (!option_persistent_selections
)
3657 if (edit
->mark1
!= edit
->mark2
)
3658 edit_block_delete_cmd (edit
);
3660 if (edit
->overwrite
)
3662 /* remove char only one time, after input first byte, multibyte chars */
3663 if ((!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3664 && edit_get_byte (edit
, edit
->curs1
) != '\n')
3665 edit_delete (edit
, 0);
3667 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3668 edit_insert_over (edit
);
3670 if (char_for_insertion
> 255 && !mc_global
.utf8_display
)
3672 unsigned char str
[6 + 1];
3676 res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3686 while (str
[i
] != 0 && i
<= 6)
3688 char_for_insertion
= str
[i
];
3689 edit_insert (edit
, char_for_insertion
);
3695 edit_insert (edit
, char_for_insertion
);
3697 if (option_auto_para_formatting
)
3699 format_paragraph (edit
, 0);
3700 edit
->force
|= REDRAW_PAGE
;
3703 check_and_wrap_line (edit
);
3704 edit
->found_len
= 0;
3705 edit
->prev_col
= edit_get_col (edit
);
3706 edit
->search_start
= edit
->curs1
;
3707 edit_find_bracket (edit
);
3713 case CK_TopOnScreen
:
3714 case CK_BottomOnScreen
:
3727 if (edit
->mark2
>= 0)
3729 if (!option_persistent_selections
)
3731 if (edit
->column_highlight
)
3732 edit_push_undo_action (edit
, COLUMN_ON
);
3733 edit
->column_highlight
= 0;
3734 edit_mark_cmd (edit
, TRUE
);
3741 case CK_TopOnScreen
:
3742 case CK_BottomOnScreen
:
3743 case CK_MarkToPageBegin
:
3744 case CK_MarkToPageEnd
:
3749 case CK_MarkToWordBegin
:
3750 case CK_MarkToWordEnd
:
3753 case CK_MarkColumnUp
:
3754 case CK_MarkColumnDown
:
3755 if (edit
->mark2
== -1)
3756 break; /*marking is following the cursor: may need to highlight a whole line */
3761 edit
->force
|= REDRAW_CHAR_ONLY
;
3764 /* basic cursor key commands */
3768 /* if non persistent selection and text selected */
3769 if (!option_persistent_selections
)
3771 if (edit
->mark1
!= edit
->mark2
)
3773 edit_block_delete_cmd (edit
);
3777 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3782 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3784 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3785 edit_backspace (edit
, 1);
3790 if (option_fake_half_tabs
)
3793 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3795 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3796 edit_backspace (edit
, 1);
3801 edit_backspace (edit
, 0);
3804 /* if non persistent selection and text selected */
3805 if (!option_persistent_selections
)
3807 if (edit
->mark1
!= edit
->mark2
)
3809 edit_block_delete_cmd (edit
);
3814 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3815 edit_insert_over (edit
);
3817 if (option_fake_half_tabs
)
3820 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3822 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3823 edit_delete (edit
, 1);
3827 edit_delete (edit
, 0);
3829 case CK_DeleteToWordBegin
:
3831 edit_left_delete_word (edit
);
3833 case CK_DeleteToWordEnd
:
3834 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3835 edit_insert_over (edit
);
3837 edit_right_delete_word (edit
);
3840 edit_delete_line (edit
);
3842 case CK_DeleteToHome
:
3843 edit_delete_to_line_begin (edit
);
3845 case CK_DeleteToEnd
:
3846 edit_delete_to_line_end (edit
);
3850 if (option_auto_para_formatting
)
3852 edit_double_newline (edit
);
3853 if (option_return_does_auto_indent
)
3854 edit_auto_indent (edit
);
3855 format_paragraph (edit
, 0);
3859 edit_insert (edit
, '\n');
3860 if (option_return_does_auto_indent
)
3862 edit_auto_indent (edit
);
3867 edit_insert (edit
, '\n');
3870 case CK_MarkColumnPageUp
:
3871 edit
->column_highlight
= 1;
3874 edit_move_up (edit
, edit
->widget
.lines
- 1, 1);
3876 case CK_MarkColumnPageDown
:
3877 edit
->column_highlight
= 1;
3879 case CK_MarkPageDown
:
3880 edit_move_down (edit
, edit
->widget
.lines
- 1, 1);
3882 case CK_MarkColumnLeft
:
3883 edit
->column_highlight
= 1;
3886 if (option_fake_half_tabs
)
3888 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3890 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3893 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3894 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3898 edit_left_char_move_cmd (edit
);
3900 case CK_MarkColumnRight
:
3901 edit
->column_highlight
= 1;
3904 if (option_fake_half_tabs
)
3906 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3908 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3909 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3913 edit_right_char_move_cmd (edit
);
3915 case CK_TopOnScreen
:
3916 case CK_MarkToPageBegin
:
3917 edit_begin_page (edit
);
3919 case CK_BottomOnScreen
:
3920 case CK_MarkToPageEnd
:
3921 edit_end_page (edit
);
3924 case CK_MarkToWordBegin
:
3926 edit_left_word_move_cmd (edit
);
3929 case CK_MarkToWordEnd
:
3931 edit_right_word_move_cmd (edit
);
3933 case CK_MarkColumnUp
:
3934 edit
->column_highlight
= 1;
3937 edit_move_up (edit
, 1, 0);
3939 case CK_MarkColumnDown
:
3940 edit
->column_highlight
= 1;
3943 edit_move_down (edit
, 1, 0);
3945 case CK_MarkColumnParagraphUp
:
3946 edit
->column_highlight
= 1;
3947 case CK_ParagraphUp
:
3948 case CK_MarkParagraphUp
:
3949 edit_move_up_paragraph (edit
, 0);
3951 case CK_MarkColumnParagraphDown
:
3952 edit
->column_highlight
= 1;
3953 case CK_ParagraphDown
:
3954 case CK_MarkParagraphDown
:
3955 edit_move_down_paragraph (edit
, 0);
3957 case CK_MarkColumnScrollUp
:
3958 edit
->column_highlight
= 1;
3960 case CK_MarkScrollUp
:
3961 edit_move_up (edit
, 1, 1);
3963 case CK_MarkColumnScrollDown
:
3964 edit
->column_highlight
= 1;
3966 case CK_MarkScrollDown
:
3967 edit_move_down (edit
, 1, 1);
3971 edit_cursor_to_bol (edit
);
3975 edit_cursor_to_eol (edit
);
3978 /* if text marked shift block */
3979 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3981 if (edit
->mark2
< 0)
3982 edit_mark_cmd (edit
, FALSE
);
3983 edit_move_block_to_right (edit
);
3987 if (option_cursor_beyond_eol
)
3988 edit_insert_over (edit
);
3989 edit_tab_cmd (edit
);
3990 if (option_auto_para_formatting
)
3992 format_paragraph (edit
, 0);
3993 edit
->force
|= REDRAW_PAGE
;
3997 check_and_wrap_line (edit
);
4002 case CK_InsertOverwrite
:
4003 edit
->overwrite
= !edit
->overwrite
;
4007 if (edit
->mark2
>= 0)
4009 if (edit
->column_highlight
)
4010 edit_push_undo_action (edit
, COLUMN_ON
);
4011 edit
->column_highlight
= 0;
4013 edit_mark_cmd (edit
, FALSE
);
4016 if (!edit
->column_highlight
)
4017 edit_push_undo_action (edit
, COLUMN_OFF
);
4018 edit
->column_highlight
= 1;
4019 edit_mark_cmd (edit
, FALSE
);
4022 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
4023 edit
->force
|= REDRAW_PAGE
;
4026 if (edit
->column_highlight
)
4027 edit_push_undo_action (edit
, COLUMN_ON
);
4028 edit
->column_highlight
= 0;
4029 edit_mark_cmd (edit
, TRUE
);
4032 if (edit
->column_highlight
)
4033 edit_push_undo_action (edit
, COLUMN_ON
);
4034 edit
->column_highlight
= 0;
4035 edit_mark_current_word_cmd (edit
);
4038 if (edit
->column_highlight
)
4039 edit_push_undo_action (edit
, COLUMN_ON
);
4040 edit
->column_highlight
= 0;
4041 edit_mark_current_line_cmd (edit
);
4045 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
4046 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
4047 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4049 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4051 case CK_BookmarkFlush
:
4052 book_mark_flush (edit
, BOOK_MARK_COLOR
);
4053 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
4054 edit
->force
|= REDRAW_PAGE
;
4056 case CK_BookmarkNext
:
4057 if (edit
->book_mark
)
4059 struct _book_mark
*p
;
4060 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4064 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4065 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4066 edit_move_to_line (edit
, p
->line
);
4070 case CK_BookmarkPrev
:
4071 if (edit
->book_mark
)
4073 struct _book_mark
*p
;
4074 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4075 while (p
->line
== edit
->curs_line
)
4080 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4081 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4082 edit_move_to_line (edit
, p
->line
);
4088 case CK_MarkToFileBegin
:
4089 edit_move_to_top (edit
);
4092 case CK_MarkToFileEnd
:
4093 edit_move_to_bottom (edit
);
4097 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4098 edit_insert_over (edit
);
4099 edit_block_copy_cmd (edit
);
4102 edit_block_delete_cmd (edit
);
4105 edit_block_move_cmd (edit
);
4108 case CK_BlockShiftLeft
:
4109 if (edit
->mark1
!= edit
->mark2
)
4110 edit_move_block_to_left (edit
);
4112 case CK_BlockShiftRight
:
4113 if (edit
->mark1
!= edit
->mark2
)
4114 edit_move_block_to_right (edit
);
4117 edit_copy_to_X_buf_cmd (edit
);
4120 edit_cut_to_X_buf_cmd (edit
);
4123 /* if non persistent selection and text selected */
4124 if (!option_persistent_selections
)
4126 if (edit
->mark1
!= edit
->mark2
)
4127 edit_block_delete_cmd (edit
);
4129 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4130 edit_insert_over (edit
);
4131 edit_paste_from_X_buf_cmd (edit
);
4134 edit_paste_from_history (edit
);
4138 edit_save_as_cmd (edit
);
4141 edit_save_confirm_cmd (edit
);
4144 edit_save_block_cmd (edit
);
4147 edit_insert_file_cmd (edit
);
4151 edit_load_back_cmd (edit
);
4154 edit_load_forward_cmd (edit
);
4157 case CK_SyntaxChoose
:
4158 edit_syntax_dialog (edit
);
4162 edit_search_cmd (edit
, FALSE
);
4164 case CK_SearchContinue
:
4165 edit_search_cmd (edit
, TRUE
);
4168 edit_replace_cmd (edit
, 0);
4170 case CK_ReplaceContinue
:
4171 edit_replace_cmd (edit
, 1);
4174 /* if text marked shift block */
4175 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
4177 edit_move_block_to_left (edit
);
4181 edit_complete_word_cmd (edit
);
4185 edit_get_match_keyword_cmd (edit
);
4188 case CK_SpellCheckCurrentWord
:
4189 edit_suggest_current_word (edit
);
4192 edit_spellcheck_file (edit
);
4194 case CK_SpellCheckSelectLang
:
4195 edit_set_spell_lang ();
4201 /* fool gcc to prevent a Y2K warning */
4202 char time_format
[] = "_c";
4203 time_format
[0] = '%';
4205 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4206 edit_print_string (edit
, s
);
4207 edit
->force
|= REDRAW_PAGE
;
4212 edit_goto_cmd (edit
);
4214 case CK_ParagraphFormat
:
4215 format_paragraph (edit
, 1);
4216 edit
->force
|= REDRAW_PAGE
;
4218 case CK_MacroDelete
:
4219 edit_delete_macro_cmd (edit
);
4221 case CK_MatchBracket
:
4222 edit_goto_matching_bracket (edit
);
4225 user_menu (edit
, NULL
, -1);
4228 edit_sort_cmd (edit
);
4230 case CK_ExternalCommand
:
4231 edit_ext_cmd (edit
);
4234 edit_mail_dialog (edit
);
4237 case CK_SelectCodepage
:
4238 edit_select_codepage_cmd (edit
);
4241 case CK_InsertLiteral
:
4242 edit_insert_literal_cmd (edit
);
4244 case CK_MacroStartStopRecord
:
4245 edit_begin_end_macro_cmd (edit
);
4247 case CK_RepeatStartStopRecord
:
4248 edit_begin_end_repeat_cmd (edit
);
4250 case CK_ExtendedKeyMap
:
4251 edit
->extmod
= TRUE
;
4258 if ((command
/ CK_PipeBlock (0)) == 1)
4259 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
4261 /* keys which must set the col position, and the search vars */
4265 case CK_SearchContinue
:
4267 case CK_ReplaceContinue
:
4269 edit
->prev_col
= edit_get_col (edit
);
4273 case CK_MarkColumnUp
:
4276 case CK_MarkColumnDown
:
4279 case CK_MarkColumnPageUp
:
4281 case CK_MarkPageDown
:
4282 case CK_MarkColumnPageDown
:
4284 case CK_MarkToFileBegin
:
4286 case CK_MarkToFileEnd
:
4287 case CK_ParagraphUp
:
4288 case CK_MarkParagraphUp
:
4289 case CK_MarkColumnParagraphUp
:
4290 case CK_ParagraphDown
:
4291 case CK_MarkParagraphDown
:
4292 case CK_MarkColumnParagraphDown
:
4294 case CK_MarkScrollUp
:
4295 case CK_MarkColumnScrollUp
:
4297 case CK_MarkScrollDown
:
4298 case CK_MarkColumnScrollDown
:
4299 edit
->search_start
= edit
->curs1
;
4300 edit
->found_len
= 0;
4303 edit
->found_len
= 0;
4304 edit
->prev_col
= edit_get_col (edit
);
4305 edit
->search_start
= edit
->curs1
;
4307 edit_find_bracket (edit
);
4309 if (option_auto_para_formatting
)
4315 case CK_DeleteToWordBegin
:
4316 case CK_DeleteToWordEnd
:
4317 case CK_DeleteToHome
:
4318 case CK_DeleteToEnd
:
4319 format_paragraph (edit
, 0);
4320 edit
->force
|= REDRAW_PAGE
;
4325 /* --------------------------------------------------------------------------------------------- */
4328 edit_stack_init (void)
4330 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4332 edit_history_moveto
[edit_stack_iterator
].filename_vpath
= NULL
;
4333 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4336 edit_stack_iterator
= 0;
4339 /* --------------------------------------------------------------------------------------------- */
4342 edit_stack_free (void)
4344 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4345 vfs_path_free (edit_history_moveto
[edit_stack_iterator
].filename_vpath
);
4348 /* --------------------------------------------------------------------------------------------- */
4352 edit_move_up (WEdit
* edit
, long i
, gboolean do_scroll
)
4354 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4357 /* --------------------------------------------------------------------------------------------- */
4361 edit_move_down (WEdit
* edit
, long i
, gboolean do_scroll
)
4363 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4366 /* --------------------------------------------------------------------------------------------- */