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 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 long 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
= 0;
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
, long 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
, long 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 /* --------------------------------------------------------------------------------------------- */
728 is_in_indent (WEdit
* edit
)
730 long p
= edit_bol (edit
, edit
->curs1
);
731 while (p
< edit
->curs1
)
732 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
737 /* --------------------------------------------------------------------------------------------- */
740 is_blank (WEdit
* edit
, long offset
)
744 s
= edit_bol (edit
, offset
);
745 f
= edit_eol (edit
, offset
) - 1;
748 c
= edit_get_byte (edit
, s
++);
756 /* --------------------------------------------------------------------------------------------- */
757 /** returns the offset of line i */
760 edit_find_line (WEdit
* edit
, int line
)
764 if (!edit
->caches_valid
)
766 for (i
= 0; i
< N_LINE_CACHES
; i
++)
767 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
768 /* three offsets that we *know* are line 0 at 0 and these two: */
769 edit
->line_numbers
[1] = edit
->curs_line
;
770 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
771 edit
->line_numbers
[2] = edit
->total_lines
;
772 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
773 edit
->caches_valid
= 1;
775 if (line
>= edit
->total_lines
)
776 return edit
->line_offsets
[2];
779 /* find the closest known point */
780 for (i
= 0; i
< N_LINE_CACHES
; i
++)
783 n
= abs (edit
->line_numbers
[i
] - line
);
791 return edit
->line_offsets
[j
]; /* know the offset exactly */
792 if (m
== 1 && j
>= 3)
793 i
= j
; /* one line different - caller might be looping, so stay in this cache */
795 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
796 if (line
> edit
->line_numbers
[j
])
797 edit
->line_offsets
[i
] =
798 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
800 edit
->line_offsets
[i
] =
801 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
802 edit
->line_numbers
[i
] = line
;
803 return edit
->line_offsets
[i
];
806 /* --------------------------------------------------------------------------------------------- */
807 /** moves up until a blank line is reached, or until just
808 before a non-blank line is reached */
811 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
814 if (edit
->curs_line
> 1)
816 if (line_is_blank (edit
, edit
->curs_line
))
818 if (line_is_blank (edit
, edit
->curs_line
- 1))
820 for (i
= edit
->curs_line
- 1; i
; i
--)
821 if (!line_is_blank (edit
, i
))
829 for (i
= edit
->curs_line
- 1; i
; i
--)
830 if (line_is_blank (edit
, i
))
836 for (i
= edit
->curs_line
- 1; i
; i
--)
837 if (line_is_blank (edit
, i
))
841 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
844 /* --------------------------------------------------------------------------------------------- */
845 /** moves down until a blank line is reached, or until just
846 before a non-blank line is reached */
849 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
852 if (edit
->curs_line
>= edit
->total_lines
- 1)
854 i
= edit
->total_lines
;
858 if (line_is_blank (edit
, edit
->curs_line
))
860 if (line_is_blank (edit
, edit
->curs_line
+ 1))
862 for (i
= edit
->curs_line
+ 1; i
; i
++)
863 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
871 for (i
= edit
->curs_line
+ 1; i
; i
++)
872 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
878 for (i
= edit
->curs_line
+ 1; i
; i
++)
879 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
883 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
886 /* --------------------------------------------------------------------------------------------- */
889 edit_begin_page (WEdit
* edit
)
891 edit_update_curs_row (edit
);
892 edit_move_up (edit
, edit
->curs_row
, 0);
895 /* --------------------------------------------------------------------------------------------- */
898 edit_end_page (WEdit
* edit
)
900 edit_update_curs_row (edit
);
901 edit_move_down (edit
, edit
->widget
.lines
- edit
->curs_row
- 1, 0);
905 /* --------------------------------------------------------------------------------------------- */
906 /** goto beginning of text */
909 edit_move_to_top (WEdit
* edit
)
913 edit_cursor_move (edit
, -edit
->curs1
);
914 edit_move_to_prev_col (edit
, 0);
915 edit
->force
|= REDRAW_PAGE
;
916 edit
->search_start
= 0;
917 edit_update_curs_row (edit
);
922 /* --------------------------------------------------------------------------------------------- */
923 /** goto end of text */
926 edit_move_to_bottom (WEdit
* edit
)
928 if (edit
->curs_line
< edit
->total_lines
)
930 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
931 edit
->start_display
= edit
->last_byte
;
932 edit
->start_line
= edit
->total_lines
;
933 edit_scroll_upward (edit
, edit
->widget
.lines
- 1);
934 edit
->force
|= REDRAW_PAGE
;
938 /* --------------------------------------------------------------------------------------------- */
939 /** goto beginning of line */
942 edit_cursor_to_bol (WEdit
* edit
)
944 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
945 edit
->search_start
= edit
->curs1
;
946 edit
->prev_col
= edit_get_col (edit
);
950 /* --------------------------------------------------------------------------------------------- */
951 /** goto end of line */
954 edit_cursor_to_eol (WEdit
* edit
)
956 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
957 edit
->search_start
= edit
->curs1
;
958 edit
->prev_col
= edit_get_col (edit
);
962 /* --------------------------------------------------------------------------------------------- */
969 const char option_chars_move_whole_word
[] =
970 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
976 if (*option_chars_move_whole_word
== '!')
980 if (g_ascii_isupper ((gchar
) c
))
982 else if (g_ascii_islower ((gchar
) c
))
984 else if (g_ascii_isalpha (c
))
986 else if (isdigit (c
))
988 else if (isspace (c
))
990 q
= strchr (option_chars_move_whole_word
, c
);
995 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1000 while ((q
= strchr (q
+ 1, c
)));
1004 /* --------------------------------------------------------------------------------------------- */
1007 edit_left_word_move (WEdit
* edit
, int s
)
1012 if (edit
->column_highlight
1013 && edit
->mark1
!= edit
->mark2
1014 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1016 edit_cursor_move (edit
, -1);
1019 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1020 c2
= edit_get_byte (edit
, edit
->curs1
);
1021 if (c1
== '\n' || c2
== '\n')
1023 if (!(my_type_of (c1
) & my_type_of (c2
)))
1025 if (isspace (c1
) && !isspace (c2
))
1028 if (!isspace (c1
) && isspace (c2
))
1033 /* --------------------------------------------------------------------------------------------- */
1036 edit_left_word_move_cmd (WEdit
* edit
)
1038 edit_left_word_move (edit
, 0);
1039 edit
->force
|= REDRAW_PAGE
;
1042 /* --------------------------------------------------------------------------------------------- */
1045 edit_right_word_move (WEdit
* edit
, int s
)
1050 if (edit
->column_highlight
1051 && edit
->mark1
!= edit
->mark2
1052 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
1054 edit_cursor_move (edit
, 1);
1055 if (edit
->curs1
>= edit
->last_byte
)
1057 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1058 c2
= edit_get_byte (edit
, edit
->curs1
);
1059 if (c1
== '\n' || c2
== '\n')
1061 if (!(my_type_of (c1
) & my_type_of (c2
)))
1063 if (isspace (c1
) && !isspace (c2
))
1066 if (!isspace (c1
) && isspace (c2
))
1071 /* --------------------------------------------------------------------------------------------- */
1074 edit_right_word_move_cmd (WEdit
* edit
)
1076 edit_right_word_move (edit
, 0);
1077 edit
->force
|= REDRAW_PAGE
;
1080 /* --------------------------------------------------------------------------------------------- */
1083 edit_right_char_move_cmd (WEdit
* edit
)
1089 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
1095 c
= edit_get_byte (edit
, edit
->curs1
);
1097 if (option_cursor_beyond_eol
&& c
== '\n')
1103 edit_cursor_move (edit
, cw
);
1107 /* --------------------------------------------------------------------------------------------- */
1110 edit_left_char_move_cmd (WEdit
* edit
)
1113 if (edit
->column_highlight
1114 && option_cursor_beyond_eol
1115 && edit
->mark1
!= edit
->mark2
1116 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1120 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1124 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1130 edit_cursor_move (edit
, -cw
);
1134 /* --------------------------------------------------------------------------------------------- */
1135 /** Up or down cursor moving.
1136 direction = TRUE - move up
1141 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
1144 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
1153 edit
->force
|= REDRAW_PAGE
;
1157 edit_scroll_upward (edit
, i
);
1159 edit_scroll_downward (edit
, i
);
1161 p
= edit_bol (edit
, edit
->curs1
);
1163 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
1165 edit_cursor_move (edit
, p
- edit
->curs1
);
1167 edit_move_to_prev_col (edit
, p
);
1169 /* search start of current multibyte char (like CJK) */
1170 if (edit
->curs1
+ 1 < edit
->last_byte
)
1172 edit_right_char_move_cmd (edit
);
1173 edit_left_char_move_cmd (edit
);
1176 edit
->search_start
= edit
->curs1
;
1177 edit
->found_len
= 0;
1180 /* --------------------------------------------------------------------------------------------- */
1183 edit_right_delete_word (WEdit
* edit
)
1188 if (edit
->curs1
>= edit
->last_byte
)
1190 c1
= edit_delete (edit
, 1);
1191 c2
= edit_get_byte (edit
, edit
->curs1
);
1192 if (c1
== '\n' || c2
== '\n')
1194 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1196 if (!(my_type_of (c1
) & my_type_of (c2
)))
1201 /* --------------------------------------------------------------------------------------------- */
1204 edit_left_delete_word (WEdit
* edit
)
1209 if (edit
->curs1
<= 0)
1211 c1
= edit_backspace (edit
, 1);
1212 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1213 if (c1
== '\n' || c2
== '\n')
1215 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1217 if (!(my_type_of (c1
) & my_type_of (c2
)))
1222 /* --------------------------------------------------------------------------------------------- */
1224 the start column position is not recorded, and hence does not
1225 undo as it happed. But who would notice.
1229 edit_do_undo (WEdit
* edit
)
1234 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1236 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1243 edit_cursor_move (edit
, 1);
1246 edit_cursor_move (edit
, -1);
1250 edit_backspace (edit
, 1);
1254 edit_delete (edit
, 1);
1257 edit
->column_highlight
= 1;
1260 edit
->column_highlight
= 0;
1263 if (ac
>= 256 && ac
< 512)
1264 edit_insert_ahead (edit
, ac
- 256);
1265 if (ac
>= 0 && ac
< 256)
1266 edit_insert (edit
, ac
);
1268 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1270 edit
->mark1
= ac
- MARK_1
;
1271 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1273 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1275 edit
->mark2
= ac
- MARK_2
;
1276 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1278 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1280 edit
->end_mark_curs
= ac
- MARK_CURS
;
1283 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1286 if (edit
->start_display
> ac
- KEY_PRESS
)
1288 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1289 edit
->force
|= REDRAW_PAGE
;
1291 else if (edit
->start_display
< ac
- KEY_PRESS
)
1293 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1294 edit
->force
|= REDRAW_PAGE
;
1296 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1297 edit_update_curs_row (edit
);
1300 edit
->undo_stack_disable
= 0;
1304 edit_do_redo (WEdit
* edit
)
1309 if (edit
->redo_stack_reset
)
1313 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1320 edit_cursor_move (edit
, 1);
1323 edit_cursor_move (edit
, -1);
1326 edit_backspace (edit
, 1);
1329 edit_delete (edit
, 1);
1332 edit
->column_highlight
= 1;
1335 edit
->column_highlight
= 0;
1338 if (ac
>= 256 && ac
< 512)
1339 edit_insert_ahead (edit
, ac
- 256);
1340 if (ac
>= 0 && ac
< 256)
1341 edit_insert (edit
, ac
);
1343 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1345 edit
->mark1
= ac
- MARK_1
;
1346 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1348 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1350 edit
->mark2
= ac
- MARK_2
;
1351 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1353 /* more than one pop usually means something big */
1355 edit
->force
|= REDRAW_PAGE
;
1358 if (edit
->start_display
> ac
- KEY_PRESS
)
1360 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1361 edit
->force
|= REDRAW_PAGE
;
1363 else if (edit
->start_display
< ac
- KEY_PRESS
)
1365 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1366 edit
->force
|= REDRAW_PAGE
;
1368 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1369 edit_update_curs_row (edit
);
1375 edit_group_undo (WEdit
* edit
)
1377 long ac
= KEY_PRESS
;
1378 long cur_ac
= KEY_PRESS
;
1379 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1381 cur_ac
= get_prev_undo_action (edit
);
1382 edit_do_undo (edit
);
1383 ac
= get_prev_undo_action (edit
);
1384 /* exit from cycle if option_group_undo is not set,
1385 * and make single UNDO operation
1387 if (!option_group_undo
)
1392 /* --------------------------------------------------------------------------------------------- */
1395 edit_delete_to_line_end (WEdit
* edit
)
1397 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
1401 edit_delete (edit
, 1);
1405 /* --------------------------------------------------------------------------------------------- */
1408 edit_delete_to_line_begin (WEdit
* edit
)
1410 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
1414 edit_backspace (edit
, 1);
1418 /* --------------------------------------------------------------------------------------------- */
1421 is_aligned_on_a_tab (WEdit
* edit
)
1423 edit_update_curs_col (edit
);
1424 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
1425 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
1428 /* --------------------------------------------------------------------------------------------- */
1431 right_of_four_spaces (WEdit
* edit
)
1434 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1435 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1437 return is_aligned_on_a_tab (edit
);
1441 /* --------------------------------------------------------------------------------------------- */
1444 left_of_four_spaces (WEdit
* edit
)
1447 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1448 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1450 return is_aligned_on_a_tab (edit
);
1454 /* --------------------------------------------------------------------------------------------- */
1457 edit_auto_indent (WEdit
* edit
)
1462 /* use the previous line as a template */
1463 p
= edit_move_backward (edit
, p
, 1);
1464 /* copy the leading whitespace of the line */
1466 { /* no range check - the line _is_ \n-terminated */
1467 c
= edit_get_byte (edit
, p
++);
1468 if (c
!= ' ' && c
!= '\t')
1470 edit_insert (edit
, c
);
1474 /* --------------------------------------------------------------------------------------------- */
1477 edit_double_newline (WEdit
* edit
)
1479 edit_insert (edit
, '\n');
1480 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1482 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1484 edit
->force
|= REDRAW_PAGE
;
1485 edit_insert (edit
, '\n');
1489 /* --------------------------------------------------------------------------------------------- */
1492 insert_spaces_tab (WEdit
* edit
, gboolean half
)
1496 edit_update_curs_col (edit
);
1497 i
= option_tab_spacing
* space_width
;
1500 i
= ((edit
->curs_col
/ i
) + 1) * i
- edit
->curs_col
;
1503 edit_insert (edit
, ' ');
1508 /* --------------------------------------------------------------------------------------------- */
1511 edit_tab_cmd (WEdit
* edit
)
1515 if (option_fake_half_tabs
)
1517 if (is_in_indent (edit
))
1519 /*insert a half tab (usually four spaces) unless there is a
1520 half tab already behind, then delete it and insert a
1522 if (option_fill_tabs_with_spaces
|| !right_of_four_spaces (edit
))
1523 insert_spaces_tab (edit
, TRUE
);
1526 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1527 edit_backspace (edit
, 1);
1528 edit_insert (edit
, '\t');
1533 if (option_fill_tabs_with_spaces
)
1534 insert_spaces_tab (edit
, FALSE
);
1536 edit_insert (edit
, '\t');
1539 /* --------------------------------------------------------------------------------------------- */
1542 check_and_wrap_line (WEdit
* edit
)
1545 if (!option_typewriter_wrap
)
1547 edit_update_curs_col (edit
);
1548 if (edit
->curs_col
< option_word_wrap_line_length
)
1554 c
= edit_get_byte (edit
, curs
);
1555 if (c
== '\n' || curs
<= 0)
1557 edit_insert (edit
, '\n');
1560 if (c
== ' ' || c
== '\t')
1562 int current
= edit
->curs1
;
1563 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
1564 edit_insert (edit
, '\n');
1565 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
1571 /* --------------------------------------------------------------------------------------------- */
1572 /** this find the matching bracket in either direction, and sets edit->bracket */
1575 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
1577 const char *const b
= "{}{[][()(", *p
;
1578 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
1579 unsigned long j
= 0;
1581 edit_update_curs_row (edit
);
1582 c
= edit_get_byte (edit
, edit
->curs1
);
1585 if (!furthest_bracket_search
)
1586 furthest_bracket_search
--;
1587 /* not on a bracket at all */
1590 /* the matching bracket */
1592 /* going left or right? */
1593 if (strchr ("{[(", c
))
1595 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
1597 /* out of buffer? */
1598 if (q
>= edit
->last_byte
|| q
< 0)
1600 a
= edit_get_byte (edit
, q
);
1601 /* don't want to eat CPU */
1602 if (j
++ > furthest_bracket_search
)
1604 /* out of screen? */
1607 if (q
< edit
->start_display
)
1609 /* count lines if searching downward */
1610 if (inc
> 0 && a
== '\n')
1611 if (n
++ >= edit
->widget
.lines
- edit
->curs_row
) /* out of screen */
1614 /* count bracket depth */
1615 i
+= (a
== c
) - (a
== d
);
1616 /* return if bracket depth is zero */
1624 /* --------------------------------------------------------------------------------------------- */
1627 edit_goto_matching_bracket (WEdit
* edit
)
1631 q
= edit_get_bracket (edit
, 0, 0);
1634 edit
->bracket
= edit
->curs1
;
1635 edit
->force
|= REDRAW_PAGE
;
1636 edit_cursor_move (edit
, q
- edit
->curs1
);
1640 /* --------------------------------------------------------------------------------------------- */
1643 edit_move_block_to_right (WEdit
* edit
)
1645 long start_mark
, end_mark
;
1646 long cur_bol
, start_bol
;
1648 if (eval_marks (edit
, &start_mark
, &end_mark
))
1651 start_bol
= edit_bol (edit
, start_mark
);
1652 cur_bol
= edit_bol (edit
, end_mark
- 1);
1656 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1657 if (option_fill_tabs_with_spaces
)
1658 insert_spaces_tab (edit
, option_fake_half_tabs
);
1660 edit_insert (edit
, '\t');
1661 edit_cursor_move (edit
, edit_bol (edit
, cur_bol
) - edit
->curs1
);
1666 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1668 while (cur_bol
>= start_bol
);
1670 edit
->force
|= REDRAW_PAGE
;
1673 /* --------------------------------------------------------------------------------------------- */
1676 edit_move_block_to_left (WEdit
* edit
)
1678 long start_mark
, end_mark
;
1679 long cur_bol
, start_bol
;
1682 if (eval_marks (edit
, &start_mark
, &end_mark
))
1685 start_bol
= edit_bol (edit
, start_mark
);
1686 cur_bol
= edit_bol (edit
, end_mark
- 1);
1693 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1695 if (option_fake_half_tabs
)
1696 del_tab_width
= HALF_TAB_SIZE
;
1698 del_tab_width
= option_tab_spacing
;
1700 next_char
= edit_get_byte (edit
, edit
->curs1
);
1701 if (next_char
== '\t')
1702 edit_delete (edit
, 1);
1703 else if (next_char
== ' ')
1704 for (i
= 1; i
<= del_tab_width
; i
++)
1706 if (next_char
== ' ')
1707 edit_delete (edit
, 1);
1708 next_char
= edit_get_byte (edit
, edit
->curs1
);
1714 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1716 while (cur_bol
>= start_bol
);
1718 edit
->force
|= REDRAW_PAGE
;
1721 /* --------------------------------------------------------------------------------------------- */
1723 * prints at the cursor
1724 * @returns the number of chars printed
1728 edit_print_string (WEdit
* e
, const char *s
)
1732 while (s
[i
] != '\0')
1733 edit_execute_cmd (e
, CK_InsertChar
, (unsigned char) s
[i
++]);
1734 e
->force
|= REDRAW_COMPLETELY
;
1735 edit_update_screen (e
);
1739 /* --------------------------------------------------------------------------------------------- */
1740 /*** public functions ****************************************************************************/
1741 /* --------------------------------------------------------------------------------------------- */
1743 /** User edit menu, like user menu (F2) but only in editor. */
1746 user_menu (WEdit
* edit
, const char *menu_file
, int selected_entry
)
1751 long start_mark
, end_mark
;
1753 vfs_path_t
*block_file_vpath
;
1755 block_file
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
1756 block_file_vpath
= vfs_path_from_str (block_file
);
1758 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
1760 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1762 /* run shell scripts from menu */
1763 if (user_menu_cmd (edit
, menu_file
, selected_entry
)
1764 && (mc_stat (block_file_vpath
, &status
) == 0) && (status
.st_size
!= 0))
1769 /* i.e. we have marked block */
1771 rc
= edit_block_delete_cmd (edit
);
1777 ins_len
= edit_insert_file (edit
, block_file_vpath
);
1778 if (nomark
== 0 && ins_len
> 0)
1779 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1781 /* truncate block file */
1782 fd
= fopen (block_file
, "w");
1786 g_free (block_file
);
1787 vfs_path_free (block_file_vpath
);
1789 edit_cursor_move (edit
, curs
- edit
->curs1
);
1790 edit
->force
|= REDRAW_PAGE
;
1791 send_message ((Widget
*) edit
, WIDGET_DRAW
, 0);
1794 /* --------------------------------------------------------------------------------------------- */
1797 edit_get_byte (WEdit
* edit
, long byte_index
)
1800 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1803 if (byte_index
>= edit
->curs1
)
1805 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1806 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
1810 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
1814 /* --------------------------------------------------------------------------------------------- */
1817 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1822 gchar
*next_ch
= NULL
;
1824 gchar utf8_buf
[UTF8_CHAR_LEN
+ 1];
1826 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1832 str
= edit_get_byte_ptr (edit
, byte_index
);
1840 res
= g_utf8_get_char_validated (str
, -1);
1844 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1846 for (i
= 0; i
< UTF8_CHAR_LEN
; i
++)
1847 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
);
1848 utf8_buf
[UTF8_CHAR_LEN
] = '\0';
1850 res
= g_utf8_get_char_validated (str
, -1);
1861 /* Calculate UTF-8 char width */
1862 next_ch
= g_utf8_next_char (str
);
1865 width
= next_ch
- str
;
1873 *char_width
= width
;
1877 /* --------------------------------------------------------------------------------------------- */
1880 edit_get_write_filter (const vfs_path_t
* write_name_vpath
, const vfs_path_t
* filename_vpath
)
1883 char *p
, *writename
;
1884 const vfs_path_element_t
*path_element
;
1886 i
= edit_find_filter (filename_vpath
);
1890 path_element
= vfs_path_get_by_index (write_name_vpath
, -1);
1891 writename
= name_quote (path_element
->path
, 0);
1892 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
1897 /* --------------------------------------------------------------------------------------------- */
1900 edit_write_stream (WEdit
* edit
, FILE * f
)
1904 if (edit
->lb
== LB_ASIS
)
1906 for (i
= 0; i
< edit
->last_byte
; i
++)
1907 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
1912 /* change line breaks */
1913 for (i
= 0; i
< edit
->last_byte
; i
++)
1915 unsigned char c
= edit_get_byte (edit
, i
);
1917 if (!(c
== '\n' || c
== '\r'))
1919 /* not line break */
1920 if (fputc (c
, f
) < 0)
1924 { /* (c == '\n' || c == '\r') */
1925 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
1929 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
1930 /* put one line break unconditionally */
1931 if (fputc ('\n', f
) < 0)
1934 i
++; /* 2 chars are processed */
1936 if (c
== '\r' && c1
== '\n')
1937 /* Windows line break; go to the next char */
1940 if (c
== '\r' && c1
== '\r')
1942 /* two Macintosh line breaks; put second line break */
1943 if (fputc ('\n', f
) < 0)
1948 if (fputc (c1
, f
) < 0)
1952 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
1953 /* put one line break unconditionally */
1954 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
1957 if (c
== '\r' && c1
== '\n')
1958 /* Windows line break; go to the next char */
1962 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
1963 /* put one line break unconditionally */
1964 if (fputc ('\r', f
) < 0)
1967 i
++; /* 2 chars are processed */
1969 if (c
== '\r' && c1
== '\n')
1970 /* Windows line break; go to the next char */
1973 if (c
== '\n' && c1
== '\n')
1975 /* two Windows line breaks; put second line break */
1976 if (fputc ('\r', f
) < 0)
1981 if (fputc (c1
, f
) < 0)
1984 case LB_ASIS
: /* default without changes */
1990 return edit
->last_byte
;
1993 /* --------------------------------------------------------------------------------------------- */
1996 is_break_char (char c
)
1998 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
2001 /* --------------------------------------------------------------------------------------------- */
2004 edit_get_word_from_pos (WEdit
* edit
, long start_pos
, long *start
, gsize
* len
, gsize
* cut
)
2008 GString
*match_expr
;
2009 unsigned char *bufpos
;
2012 for (word_start
= start_pos
; word_start
!= 0; word_start
--, cut_len
++)
2014 c1
= edit_get_byte (edit
, word_start
);
2015 c2
= edit_get_byte (edit
, word_start
- 1);
2017 if (is_break_char (c1
) != is_break_char (c2
) || c1
== '\n' || c2
== '\n')
2021 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
2022 match_expr
= g_string_sized_new (16);
2026 c1
= edit_get_byte (edit
, word_start
+ match_expr
->len
);
2027 c2
= edit_get_byte (edit
, word_start
+ match_expr
->len
+ 1);
2028 g_string_append_c (match_expr
, c1
);
2030 while (!(is_break_char (c1
) != is_break_char (c2
) || c1
== '\n' || c2
== '\n'));
2032 *len
= match_expr
->len
;
2033 *start
= word_start
;
2036 return g_string_free (match_expr
, FALSE
);
2039 /* --------------------------------------------------------------------------------------------- */
2040 /** inserts a file at the cursor, returns count of inserted bytes on success */
2043 edit_insert_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
2048 p
= edit_get_filter (filename_vpath
);
2052 long current
= edit
->curs1
;
2054 f
= (FILE *) popen (p
, "r");
2057 edit_insert_stream (edit
, f
);
2058 ins_len
= edit
->curs1
- current
;
2059 edit_cursor_move (edit
, -ins_len
);
2064 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
2065 edit_error_dialog (_("Error"), errmsg
);
2074 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
2075 edit_error_dialog (_("Error"), errmsg
);
2083 int i
, file
, blocklen
;
2084 long current
= edit
->curs1
;
2085 int vertical_insertion
= 0;
2088 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
2092 buf
= g_malloc0 (TEMP_BUF_LEN
);
2093 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2096 /* if contain signature VERTICAL_MAGIC then it vertical block */
2097 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2098 vertical_insertion
= 1;
2100 mc_lseek (file
, 0, SEEK_SET
);
2103 if (vertical_insertion
)
2108 blocklen
= edit_insert_column_of_text_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
2109 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2110 /* highlight inserted text then not persistent blocks */
2111 if (!option_persistent_selections
)
2113 if (!edit
->column_highlight
)
2114 edit_push_undo_action (edit
, COLUMN_OFF
);
2115 edit
->column_highlight
= 1;
2120 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2122 for (i
= 0; i
< blocklen
; i
++)
2123 edit_insert (edit
, buf
[i
]);
2125 /* highlight inserted text then not persistent blocks */
2126 if (!option_persistent_selections
&& edit
->modified
)
2128 edit_set_markers (edit
, edit
->curs1
, current
, 0, 0);
2129 if (edit
->column_highlight
)
2130 edit_push_undo_action (edit
, COLUMN_ON
);
2131 edit
->column_highlight
= 0;
2135 edit
->force
|= REDRAW_PAGE
;
2136 ins_len
= edit
->curs1
- current
;
2137 edit_cursor_move (edit
, -ins_len
);
2147 /* --------------------------------------------------------------------------------------------- */
2149 * Fill in the edit structure. Return NULL on failure. Pass edit as
2150 * NULL to allocate a new structure.
2152 * If line is 0, try to restore saved position. Otherwise put the
2153 * cursor on that line and show it in the middle of the screen.
2157 edit_init (WEdit
* edit
, int y
, int x
, int lines
, int cols
, const vfs_path_t
* filename_vpath
,
2160 gboolean to_free
= FALSE
;
2162 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2163 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
2166 edit_purge_widget (edit
);
2171 * Expand option_whole_chars_search by national letters using
2175 static char option_whole_chars_search_buf
[256];
2177 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
2180 size_t len
= str_term_width1 (option_whole_chars_search
);
2182 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
2184 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
2186 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
2188 option_whole_chars_search_buf
[len
++] = i
;
2192 option_whole_chars_search_buf
[len
] = 0;
2193 option_whole_chars_search
= option_whole_chars_search_buf
;
2195 #endif /* ENABLE_NLS */
2196 edit
= g_malloc0 (sizeof (WEdit
));
2200 edit
->drag_state
= MCEDIT_DRAG_NORMAL
;
2203 edit
->widget
.lines
= lines
;
2204 edit
->widget
.cols
= cols
;
2205 edit_save_size (edit
);
2206 edit
->fullscreen
= TRUE
;
2208 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2209 edit
->stat1
.st_uid
= getuid ();
2210 edit
->stat1
.st_gid
= getgid ();
2211 edit
->stat1
.st_mtime
= 0;
2215 edit
->force
|= REDRAW_PAGE
;
2217 /* set file name before load file */
2218 edit_set_filename (edit
, filename_vpath
);
2220 edit
->undo_stack_size
= START_STACK_SIZE
;
2221 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2222 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2224 edit
->redo_stack_size
= START_STACK_SIZE
;
2225 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2226 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2229 edit
->converter
= str_cnv_from_term
;
2230 edit_set_codeset (edit
);
2232 if (!edit_load_file (edit
))
2234 /* edit_load_file already gives an error message */
2240 edit
->loading_done
= 1;
2243 edit_load_syntax (edit
, NULL
, NULL
);
2246 edit_get_syntax_color (edit
, -1, &color
);
2249 /* load saved cursor position */
2250 if ((line
== 0) && option_save_position
)
2251 edit_load_position (edit
);
2256 edit_move_display (edit
, line
- 1);
2257 edit_move_to_line (edit
, line
- 1);
2260 edit_load_macro_cmd (edit
);
2265 /* --------------------------------------------------------------------------------------------- */
2267 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2269 edit_clean (WEdit
* edit
)
2276 /* a stale lock, remove it */
2278 edit
->locked
= unlock_file (edit
->filename_vpath
);
2280 /* save cursor position */
2281 if (option_save_position
)
2282 edit_save_position (edit
);
2283 else if (edit
->serialized_bookmarks
!= NULL
)
2284 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2286 /* File specified on the mcedit command line and never saved */
2287 if (edit
->delete_file
)
2288 unlink (vfs_path_get_last_path_str (edit
->filename_vpath
));
2290 edit_free_syntax_rules (edit
);
2291 book_mark_flush (edit
, -1);
2292 for (; j
<= MAXBUFF
; j
++)
2294 g_free (edit
->buffers1
[j
]);
2295 g_free (edit
->buffers2
[j
]);
2298 g_free (edit
->undo_stack
);
2299 g_free (edit
->redo_stack
);
2300 vfs_path_free (edit
->filename_vpath
);
2301 vfs_path_free (edit
->dir_vpath
);
2302 mc_search_free (edit
->search
);
2303 edit
->search
= NULL
;
2305 if (edit
->converter
!= str_cnv_from_term
)
2306 str_close_conv (edit
->converter
);
2308 edit_purge_widget (edit
);
2313 /* --------------------------------------------------------------------------------------------- */
2316 * Load a new file into the editor and set line. If it fails, preserve the old file.
2317 * To do it, allocate a new widget, initialize it and, if the new file
2318 * was loaded, copy the data to the old widget.
2320 * @returns TRUE on success, FALSE on failure.
2323 edit_reload_line (WEdit
* edit
, const vfs_path_t
* filename_vpath
, long line
)
2326 int y
= edit
->widget
.y
;
2327 int x
= edit
->widget
.x
;
2328 int lines
= edit
->widget
.lines
;
2329 int columns
= edit
->widget
.cols
;
2331 e
= g_malloc0 (sizeof (WEdit
));
2332 e
->widget
= edit
->widget
;
2334 if (edit_init (e
, y
, x
, lines
, columns
, filename_vpath
, line
) == NULL
)
2341 memcpy (edit
, e
, sizeof (WEdit
));
2347 /* --------------------------------------------------------------------------------------------- */
2350 edit_set_codeset (WEdit
* edit
)
2356 get_codepage_id (mc_global
.source_codepage
>=
2357 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2362 conv
= str_crt_conv_from (cp_id
);
2363 if (conv
!= INVALID_CONV
)
2365 if (edit
->converter
!= str_cnv_from_term
)
2366 str_close_conv (edit
->converter
);
2367 edit
->converter
= conv
;
2372 edit
->utf8
= str_isutf8 (cp_id
);
2379 /* --------------------------------------------------------------------------------------------- */
2381 Recording stack for undo:
2382 The following is an implementation of a compressed stack. Identical
2383 pushes are recorded by a negative prefix indicating the number of times the
2384 same char was pushed. This saves space for repeated curs-left or curs-right
2401 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2402 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2403 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2404 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2407 The only way the cursor moves or the buffer is changed is through the routines:
2408 insert, backspace, insert_ahead, delete, and cursor_move.
2409 These record the reverse undo movements onto the stack each time they are
2412 Each key press results in a set of actions (insert; delete ...). So each time
2413 a key is pressed the current position of start_display is pushed as
2414 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2415 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2416 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2421 edit_push_undo_action (WEdit
* edit
, long c
, ...)
2423 unsigned long sp
= edit
->undo_stack_pointer
;
2427 /* first enlarge the stack if necessary */
2428 if (sp
> edit
->undo_stack_size
- 10)
2430 if (option_max_undo
< 256)
2431 option_max_undo
= 256;
2432 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2434 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2437 edit
->undo_stack
= t
;
2438 edit
->undo_stack_size
<<= 1;
2439 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2443 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2444 if (edit
->undo_stack_disable
)
2446 edit_push_redo_action (edit
, KEY_PRESS
);
2447 edit_push_redo_action (edit
, c
);
2450 else if (edit
->redo_stack_reset
)
2452 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2455 if (edit
->undo_stack_bottom
!= sp
2456 && spm1
!= edit
->undo_stack_bottom
2457 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2460 if (edit
->undo_stack
[spm1
] < 0)
2462 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2465 if (edit
->undo_stack
[spm1
] > -1000000000)
2467 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2469 edit
->undo_stack
[spm1
]--;
2477 d
= edit
->undo_stack
[spm1
];
2481 return; /* --> no need to push multiple do-nothings */
2482 edit
->undo_stack
[sp
] = -2;
2487 edit
->undo_stack
[sp
] = c
;
2490 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2492 /* if the sp wraps round and catches the undo_stack_bottom then erase
2493 * the first set of actions on the stack to make space - by moving
2494 * undo_stack_bottom forward one "key press" */
2495 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2496 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2497 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2500 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2502 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2503 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2505 /*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: */
2506 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2507 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2509 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2514 edit_push_redo_action (WEdit
* edit
, long c
, ...)
2516 unsigned long sp
= edit
->redo_stack_pointer
;
2519 /* first enlarge the stack if necessary */
2520 if (sp
> edit
->redo_stack_size
- 10)
2522 if (option_max_undo
< 256)
2523 option_max_undo
= 256;
2524 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2526 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2529 edit
->redo_stack
= t
;
2530 edit
->redo_stack_size
<<= 1;
2531 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2535 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2537 if (edit
->redo_stack_bottom
!= sp
2538 && spm1
!= edit
->redo_stack_bottom
2539 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2542 if (edit
->redo_stack
[spm1
] < 0)
2544 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2547 if (edit
->redo_stack
[spm1
] > -1000000000)
2549 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2550 edit
->redo_stack
[spm1
]--;
2557 d
= edit
->redo_stack
[spm1
];
2561 return; /* --> no need to push multiple do-nothings */
2562 edit
->redo_stack
[sp
] = -2;
2563 goto redo_check_bottom
;
2567 edit
->redo_stack
[sp
] = c
;
2570 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2572 /* if the sp wraps round and catches the redo_stack_bottom then erase
2573 * the first set of actions on the stack to make space - by moving
2574 * redo_stack_bottom forward one "key press" */
2575 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2576 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2577 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2580 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2582 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2583 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2586 * If a single key produced enough pushes to wrap all the way round then
2587 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2588 * The stack is then initialised:
2591 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2592 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2593 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2597 /* --------------------------------------------------------------------------------------------- */
2599 Basic low level single character buffer alterations and movements at the cursor.
2600 Returns char passed over, inserted or removed.
2604 edit_insert (WEdit
* edit
, int c
)
2606 /* check if file has grown to large */
2607 if (edit
->last_byte
>= SIZE_LIMIT
)
2610 /* first we must update the position of the display window */
2611 if (edit
->curs1
< edit
->start_display
)
2613 edit
->start_display
++;
2618 /* Mark file as modified, unless the file hasn't been fully loaded */
2619 if (edit
->loading_done
)
2621 edit_modification (edit
);
2624 /* now we must update some info on the file and check if a redraw is required */
2627 if (edit
->book_mark
)
2628 book_mark_inc (edit
, edit
->curs_line
);
2630 edit
->total_lines
++;
2631 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2634 /* save the reverse command onto the undo stack */
2635 /* ordinary char and not space */
2637 edit_push_undo_action (edit
, BACKSPACE
);
2639 edit_push_undo_action (edit
, BACKSPACE_BR
);
2640 /* update markers */
2641 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2642 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2643 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2645 /* add a new buffer if we've reached the end of the last one */
2646 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2647 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2649 /* perform the insertion */
2650 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2651 = (unsigned char) c
;
2653 /* update file length */
2656 /* update cursor position */
2660 /* --------------------------------------------------------------------------------------------- */
2661 /** same as edit_insert and move left */
2664 edit_insert_ahead (WEdit
* edit
, int c
)
2666 if (edit
->last_byte
>= SIZE_LIMIT
)
2669 if (edit
->curs1
< edit
->start_display
)
2671 edit
->start_display
++;
2675 edit_modification (edit
);
2678 if (edit
->book_mark
)
2679 book_mark_inc (edit
, edit
->curs_line
);
2680 edit
->total_lines
++;
2681 edit
->force
|= REDRAW_AFTER_CURSOR
;
2683 /* ordinary char and not space */
2685 edit_push_undo_action (edit
, DELCHAR
);
2687 edit_push_undo_action (edit
, DELCHAR_BR
);
2689 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2690 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2691 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2693 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2694 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2695 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2696 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2703 /* --------------------------------------------------------------------------------------------- */
2706 edit_delete (WEdit
* edit
, const int byte_delete
)
2716 /* if byte_delete = 1 then delete only one byte not multibyte char */
2717 if (edit
->utf8
&& byte_delete
== 0)
2719 edit_get_utf (edit
, edit
->curs1
, &cw
);
2724 if (edit
->mark2
!= edit
->mark1
)
2725 edit_push_markers (edit
);
2727 for (i
= 1; i
<= cw
; i
++)
2729 if (edit
->mark1
> edit
->curs1
)
2732 edit
->end_mark_curs
--;
2734 if (edit
->mark2
> edit
->curs1
)
2736 if (edit
->last_get_rule
> edit
->curs1
)
2737 edit
->last_get_rule
--;
2739 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2741 1) & M_EDIT_BUF_SIZE
) - 1];
2743 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2745 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2746 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2750 edit_push_undo_action (edit
, p
+ 256);
2753 edit_modification (edit
);
2756 if (edit
->book_mark
)
2757 book_mark_dec (edit
, edit
->curs_line
);
2758 edit
->total_lines
--;
2759 edit
->force
|= REDRAW_AFTER_CURSOR
;
2761 if (edit
->curs1
< edit
->start_display
)
2763 edit
->start_display
--;
2771 /* --------------------------------------------------------------------------------------------- */
2774 edit_backspace (WEdit
* edit
, const int byte_delete
)
2780 if (edit
->curs1
== 0)
2785 if (edit
->mark2
!= edit
->mark1
)
2786 edit_push_markers (edit
);
2788 if (edit
->utf8
&& byte_delete
== 0)
2790 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
2794 for (i
= 1; i
<= cw
; i
++)
2796 if (edit
->mark1
>= edit
->curs1
)
2799 edit
->end_mark_curs
--;
2801 if (edit
->mark2
>= edit
->curs1
)
2803 if (edit
->last_get_rule
>= edit
->curs1
)
2804 edit
->last_get_rule
--;
2806 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
2807 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
2808 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2810 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2811 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2815 edit_push_undo_action (edit
, p
);
2817 edit_modification (edit
);
2820 if (edit
->book_mark
)
2821 book_mark_dec (edit
, edit
->curs_line
);
2823 edit
->total_lines
--;
2824 edit
->force
|= REDRAW_AFTER_CURSOR
;
2827 if (edit
->curs1
< edit
->start_display
)
2829 edit
->start_display
--;
2837 /* --------------------------------------------------------------------------------------------- */
2838 /** moves the cursor right or left: increment positive or negative respectively */
2841 edit_cursor_move (WEdit
* edit
, long increment
)
2843 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2848 for (; increment
< 0; increment
++)
2853 edit_push_undo_action (edit
, CURS_RIGHT
);
2855 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2856 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2857 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2858 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2859 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2861 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2862 1) & M_EDIT_BUF_SIZE
];
2863 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2865 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2866 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2872 edit
->force
|= REDRAW_LINE_BELOW
;
2877 else if (increment
> 0)
2879 for (; increment
> 0; increment
--)
2884 edit_push_undo_action (edit
, CURS_LEFT
);
2886 c
= edit_get_byte (edit
, edit
->curs1
);
2887 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2888 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2889 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2891 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2893 1) & M_EDIT_BUF_SIZE
) - 1];
2894 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2896 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2897 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2903 edit
->force
|= REDRAW_LINE_ABOVE
;
2909 /* These functions return positions relative to lines */
2911 /* --------------------------------------------------------------------------------------------- */
2912 /** returns index of last char on line + 1 */
2915 edit_eol (WEdit
* edit
, long current
)
2917 if (current
>= edit
->last_byte
)
2918 return edit
->last_byte
;
2921 if (edit_get_byte (edit
, current
) == '\n')
2926 /* --------------------------------------------------------------------------------------------- */
2927 /** returns index of first char on line */
2930 edit_bol (WEdit
* edit
, long current
)
2936 if (edit_get_byte (edit
, current
- 1) == '\n')
2941 /* --------------------------------------------------------------------------------------------- */
2944 edit_count_lines (WEdit
* edit
, long current
, long upto
)
2947 if (upto
> edit
->last_byte
)
2948 upto
= edit
->last_byte
;
2951 while (current
< upto
)
2952 if (edit_get_byte (edit
, current
++) == '\n')
2957 /* --------------------------------------------------------------------------------------------- */
2958 /* If lines is zero this returns the count of lines from current to upto. */
2959 /* If upto is zero returns index of lines forward current. */
2962 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
2966 return edit_count_lines (edit
, current
, upto
);
2975 next
= edit_eol (edit
, current
) + 1;
2976 if (next
> edit
->last_byte
)
2985 /* --------------------------------------------------------------------------------------------- */
2986 /** Returns offset of 'lines' lines up from current */
2989 edit_move_backward (WEdit
* edit
, long current
, long lines
)
2993 current
= edit_bol (edit
, current
);
2994 while ((lines
--) && current
!= 0)
2995 current
= edit_bol (edit
, current
- 1);
2999 /* --------------------------------------------------------------------------------------------- */
3000 /* If cols is zero this returns the count of columns from current to upto. */
3001 /* If upto is zero returns index of cols across from current. */
3004 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
3015 q
= edit
->last_byte
+ 2;
3017 for (col
= 0, p
= current
; p
< q
; p
++)
3029 orig_c
= c
= edit_get_byte (edit
, p
);
3037 utf_ch
= edit_get_utf (edit
, p
, &cw
);
3038 if (mc_global
.utf8_display
)
3042 if (g_unichar_iswide (utf_ch
))
3045 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
3049 c
= convert_to_display_c (c
);
3053 col
+= TAB_SIZE
- col
% TAB_SIZE
;
3061 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!mc_global
.utf8_display
&& !edit
->utf8
)))
3062 /* '\r' is shown as ^M, so we must advance 2 characters */
3063 /* Caret notation for control characters */
3071 /* --------------------------------------------------------------------------------------------- */
3072 /** returns the current column position of the cursor */
3075 edit_get_col (WEdit
* edit
)
3077 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3080 /* --------------------------------------------------------------------------------------------- */
3081 /* Scrolling functions */
3082 /* --------------------------------------------------------------------------------------------- */
3085 edit_update_curs_row (WEdit
* edit
)
3087 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
3090 /* --------------------------------------------------------------------------------------------- */
3093 edit_update_curs_col (WEdit
* edit
)
3095 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3098 /* --------------------------------------------------------------------------------------------- */
3101 edit_get_curs_col (const WEdit
* edit
)
3103 return edit
->curs_col
;
3106 /* --------------------------------------------------------------------------------------------- */
3107 /** moves the display start position up by i lines */
3110 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
3112 unsigned long lines_above
= edit
->start_line
;
3113 if (i
> lines_above
)
3117 edit
->start_line
-= i
;
3118 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
3119 edit
->force
|= REDRAW_PAGE
;
3120 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3122 edit_update_curs_row (edit
);
3126 /* --------------------------------------------------------------------------------------------- */
3127 /** returns 1 if could scroll, 0 otherwise */
3130 edit_scroll_downward (WEdit
* edit
, int i
)
3133 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->widget
.lines
- 1);
3134 if (lines_below
> 0)
3136 if (i
> lines_below
)
3138 edit
->start_line
+= i
;
3139 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
3140 edit
->force
|= REDRAW_PAGE
;
3141 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3143 edit_update_curs_row (edit
);
3146 /* --------------------------------------------------------------------------------------------- */
3149 edit_scroll_right (WEdit
* edit
, int i
)
3151 edit
->force
|= REDRAW_PAGE
;
3152 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3153 edit
->start_col
-= i
;
3156 /* --------------------------------------------------------------------------------------------- */
3159 edit_scroll_left (WEdit
* edit
, int i
)
3161 if (edit
->start_col
)
3163 edit
->start_col
+= i
;
3164 if (edit
->start_col
> 0)
3165 edit
->start_col
= 0;
3166 edit
->force
|= REDRAW_PAGE
;
3167 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3171 /* --------------------------------------------------------------------------------------------- */
3172 /* high level cursor movement commands */
3173 /* --------------------------------------------------------------------------------------------- */
3176 edit_move_to_prev_col (WEdit
* edit
, long p
)
3178 int prev
= edit
->prev_col
;
3179 int over
= edit
->over_col
;
3180 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
3182 if (option_cursor_beyond_eol
)
3184 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
3185 edit_eol (edit
, edit
->curs1
));
3187 if (line_len
< prev
+ edit
->over_col
)
3189 edit
->over_col
= prev
+ over
- line_len
;
3190 edit
->prev_col
= line_len
;
3191 edit
->curs_col
= line_len
;
3195 edit
->curs_col
= prev
+ over
;
3196 edit
->prev_col
= edit
->curs_col
;
3203 if (is_in_indent (edit
) && option_fake_half_tabs
)
3205 edit_update_curs_col (edit
);
3207 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
3209 int q
= edit
->curs_col
;
3210 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
3211 p
= edit_bol (edit
, edit
->curs1
);
3212 edit_cursor_move (edit
,
3213 edit_move_forward3 (edit
, p
, edit
->curs_col
,
3215 if (!left_of_four_spaces (edit
))
3216 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3222 /* --------------------------------------------------------------------------------------------- */
3225 line_is_blank (WEdit
* edit
, long line
)
3227 return is_blank (edit
, edit_find_line (edit
, line
));
3230 /* --------------------------------------------------------------------------------------------- */
3231 /** move cursor to line 'line' */
3234 edit_move_to_line (WEdit
* e
, long line
)
3236 if (line
< e
->curs_line
)
3237 edit_move_up (e
, e
->curs_line
- line
, 0);
3239 edit_move_down (e
, line
- e
->curs_line
, 0);
3240 edit_scroll_screen_over_cursor (e
);
3243 /* --------------------------------------------------------------------------------------------- */
3244 /** scroll window so that first visible line is 'line' */
3247 edit_move_display (WEdit
* e
, long line
)
3249 if (line
< e
->start_line
)
3250 edit_scroll_upward (e
, e
->start_line
- line
);
3252 edit_scroll_downward (e
, line
- e
->start_line
);
3255 /* --------------------------------------------------------------------------------------------- */
3256 /** save markers onto undo stack */
3259 edit_push_markers (WEdit
* edit
)
3261 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3262 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3263 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3266 /* --------------------------------------------------------------------------------------------- */
3269 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
3278 /* --------------------------------------------------------------------------------------------- */
3279 /** highlight marker toggle */
3282 edit_mark_cmd (WEdit
* edit
, int unmark
)
3284 edit_push_markers (edit
);
3287 edit_set_markers (edit
, 0, 0, 0, 0);
3288 edit
->force
|= REDRAW_PAGE
;
3292 if (edit
->mark2
>= 0)
3294 edit
->end_mark_curs
= -1;
3295 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3296 edit
->curs_col
+ edit
->over_col
);
3297 edit
->force
|= REDRAW_PAGE
;
3301 edit
->end_mark_curs
= edit
->curs1
;
3302 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3303 edit
->curs_col
+ edit
->over_col
);
3308 /* --------------------------------------------------------------------------------------------- */
3309 /** highlight the word under cursor */
3312 edit_mark_current_word_cmd (WEdit
* edit
)
3316 for (pos
= edit
->curs1
; pos
!= 0; pos
--)
3320 c1
= edit_get_byte (edit
, pos
);
3321 c2
= edit_get_byte (edit
, pos
- 1);
3322 if (!isspace (c1
) && isspace (c2
))
3324 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3329 for (; pos
< edit
->last_byte
; pos
++)
3333 c1
= edit_get_byte (edit
, pos
);
3334 c2
= edit_get_byte (edit
, pos
+ 1);
3335 if (!isspace (c1
) && isspace (c2
))
3337 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3340 edit
->mark2
= min (pos
+ 1, edit
->last_byte
);
3342 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3345 /* --------------------------------------------------------------------------------------------- */
3348 edit_mark_current_line_cmd (WEdit
* edit
)
3350 long pos
= edit
->curs1
;
3352 edit
->mark1
= edit_bol (edit
, pos
);
3353 edit
->mark2
= edit_eol (edit
, pos
);
3355 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3358 /* --------------------------------------------------------------------------------------------- */
3361 edit_delete_line (WEdit
* edit
)
3364 * Delete right part of the line.
3365 * Note that edit_get_byte() returns '\n' when byte position is
3368 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3370 (void) edit_delete (edit
, 1);
3375 * Note that edit_delete() will not corrupt anything if called while
3376 * cursor position is EOF.
3378 (void) edit_delete (edit
, 1);
3381 * Delete left part of the line.
3382 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3384 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3386 (void) edit_backspace (edit
, 1);
3390 /* --------------------------------------------------------------------------------------------- */
3393 edit_indent_width (WEdit
* edit
, long p
)
3396 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
3398 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
3401 /* --------------------------------------------------------------------------------------------- */
3404 edit_insert_indent (WEdit
* edit
, int indent
)
3406 if (!option_fill_tabs_with_spaces
)
3408 while (indent
>= TAB_SIZE
)
3410 edit_insert (edit
, '\t');
3414 while (indent
-- > 0)
3415 edit_insert (edit
, ' ');
3418 /* --------------------------------------------------------------------------------------------- */
3421 edit_push_key_press (WEdit
* edit
)
3423 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3424 if (edit
->mark2
== -1)
3426 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3427 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3431 /* --------------------------------------------------------------------------------------------- */
3434 edit_find_bracket (WEdit
* edit
)
3436 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3437 if (last_bracket
!= edit
->bracket
)
3438 edit
->force
|= REDRAW_PAGE
;
3439 last_bracket
= edit
->bracket
;
3442 /* --------------------------------------------------------------------------------------------- */
3444 * This executes a command as though the user initiated it through a key
3445 * press. Callback with WIDGET_KEY as a message calls this after
3446 * translating the key press. This function can be used to pass any
3447 * command to the editor. Note that the screen wouldn't update
3448 * automatically. Either of command or char_for_insertion must be
3449 * passed as -1. Commands are executed, and char_for_insertion is
3450 * inserted at the cursor.
3454 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3456 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3458 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3461 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3464 if (macro_index
!= -1)
3466 edit
->force
|= REDRAW_COMPLETELY
;
3467 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3469 edit_store_macro_cmd (edit
);
3473 else if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3475 edit_repeat_macro_cmd (edit
);
3481 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3483 record_macro_buf
[macro_index
].action
= command
;
3484 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3486 /* record the beginning of a set of editing actions initiated by a key press */
3487 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3488 edit_push_key_press (edit
);
3490 edit_execute_cmd (edit
, command
, char_for_insertion
);
3491 if (edit
->column_highlight
)
3492 edit
->force
|= REDRAW_PAGE
;
3495 /* --------------------------------------------------------------------------------------------- */
3497 This executes a command at a lower level than macro recording.
3498 It also does not push a key_press onto the undo stack. This means
3499 that if it is called many times, a single undo command will undo
3500 all of them. It also does not check for the Undo command.
3503 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3505 if (command
== CK_WindowFullscreen
)
3507 edit_toggle_fullscreen (edit
);
3511 /* handle window state */
3512 if (edit_handle_move_resize (edit
, command
))
3515 edit
->force
|= REDRAW_LINE
;
3517 /* The next key press will unhighlight the found string, so update
3519 if (edit
->found_len
|| edit
->column_highlight
)
3520 edit
->force
|= REDRAW_PAGE
;
3524 /* a mark command with shift-arrow */
3527 case CK_MarkToWordBegin
:
3528 case CK_MarkToWordEnd
:
3534 case CK_MarkPageDown
:
3535 case CK_MarkToFileBegin
:
3536 case CK_MarkToFileEnd
:
3537 case CK_MarkToPageBegin
:
3538 case CK_MarkToPageEnd
:
3539 case CK_MarkScrollUp
:
3540 case CK_MarkScrollDown
:
3541 case CK_MarkParagraphUp
:
3542 case CK_MarkParagraphDown
:
3543 /* a mark command with alt-arrow */
3544 case CK_MarkColumnPageUp
:
3545 case CK_MarkColumnPageDown
:
3546 case CK_MarkColumnLeft
:
3547 case CK_MarkColumnRight
:
3548 case CK_MarkColumnUp
:
3549 case CK_MarkColumnDown
:
3550 case CK_MarkColumnScrollUp
:
3551 case CK_MarkColumnScrollDown
:
3552 case CK_MarkColumnParagraphUp
:
3553 case CK_MarkColumnParagraphDown
:
3554 edit
->column_highlight
= 0;
3555 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3557 edit_mark_cmd (edit
, 1); /* clear */
3558 edit_mark_cmd (edit
, 0); /* marking on */
3560 edit
->highlight
= 1;
3563 /* any other command */
3565 if (edit
->highlight
)
3566 edit_mark_cmd (edit
, 0); /* clear */
3567 edit
->highlight
= 0;
3570 /* first check for undo */
3571 if (command
== CK_Undo
)
3573 edit
->redo_stack_reset
= 0;
3574 edit_group_undo (edit
);
3575 edit
->found_len
= 0;
3576 edit
->prev_col
= edit_get_col (edit
);
3577 edit
->search_start
= edit
->curs1
;
3580 /* check for redo */
3581 if (command
== CK_Redo
)
3583 edit
->redo_stack_reset
= 0;
3584 edit_do_redo (edit
);
3585 edit
->found_len
= 0;
3586 edit
->prev_col
= edit_get_col (edit
);
3587 edit
->search_start
= edit
->curs1
;
3591 edit
->redo_stack_reset
= 1;
3593 /* An ordinary key press */
3594 if (char_for_insertion
>= 0)
3596 /* if non persistent selection and text selected */
3597 if (!option_persistent_selections
)
3599 if (edit
->mark1
!= edit
->mark2
)
3600 edit_block_delete_cmd (edit
);
3602 if (edit
->overwrite
)
3604 /* remove char only one time, after input first byte, multibyte chars */
3605 if ((!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3606 && edit_get_byte (edit
, edit
->curs1
) != '\n')
3607 edit_delete (edit
, 0);
3609 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3610 edit_insert_over (edit
);
3612 if (char_for_insertion
> 255 && !mc_global
.utf8_display
)
3614 unsigned char str
[6 + 1];
3618 res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3628 while (str
[i
] != 0 && i
<= 6)
3630 char_for_insertion
= str
[i
];
3631 edit_insert (edit
, char_for_insertion
);
3637 edit_insert (edit
, char_for_insertion
);
3639 if (option_auto_para_formatting
)
3641 format_paragraph (edit
, 0);
3642 edit
->force
|= REDRAW_PAGE
;
3645 check_and_wrap_line (edit
);
3646 edit
->found_len
= 0;
3647 edit
->prev_col
= edit_get_col (edit
);
3648 edit
->search_start
= edit
->curs1
;
3649 edit_find_bracket (edit
);
3655 case CK_TopOnScreen
:
3656 case CK_BottomOnScreen
:
3669 if (edit
->mark2
>= 0)
3671 if (!option_persistent_selections
)
3673 if (edit
->column_highlight
)
3674 edit_push_undo_action (edit
, COLUMN_ON
);
3675 edit
->column_highlight
= 0;
3676 edit_mark_cmd (edit
, 1);
3683 case CK_TopOnScreen
:
3684 case CK_BottomOnScreen
:
3685 case CK_MarkToPageBegin
:
3686 case CK_MarkToPageEnd
:
3691 case CK_MarkToWordBegin
:
3692 case CK_MarkToWordEnd
:
3695 case CK_MarkColumnUp
:
3696 case CK_MarkColumnDown
:
3697 if (edit
->mark2
== -1)
3698 break; /*marking is following the cursor: may need to highlight a whole line */
3703 edit
->force
|= REDRAW_CHAR_ONLY
;
3706 /* basic cursor key commands */
3710 /* if non persistent selection and text selected */
3711 if (!option_persistent_selections
)
3713 if (edit
->mark1
!= edit
->mark2
)
3715 edit_block_delete_cmd (edit
);
3719 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3724 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3726 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3727 edit_backspace (edit
, 1);
3732 if (option_fake_half_tabs
)
3735 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3737 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3738 edit_backspace (edit
, 1);
3743 edit_backspace (edit
, 0);
3746 /* if non persistent selection and text selected */
3747 if (!option_persistent_selections
)
3749 if (edit
->mark1
!= edit
->mark2
)
3751 edit_block_delete_cmd (edit
);
3756 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3757 edit_insert_over (edit
);
3759 if (option_fake_half_tabs
)
3762 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3764 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3765 edit_delete (edit
, 1);
3769 edit_delete (edit
, 0);
3771 case CK_DeleteToWordBegin
:
3773 edit_left_delete_word (edit
);
3775 case CK_DeleteToWordEnd
:
3776 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3777 edit_insert_over (edit
);
3779 edit_right_delete_word (edit
);
3782 edit_delete_line (edit
);
3784 case CK_DeleteToHome
:
3785 edit_delete_to_line_begin (edit
);
3787 case CK_DeleteToEnd
:
3788 edit_delete_to_line_end (edit
);
3792 if (option_auto_para_formatting
)
3794 edit_double_newline (edit
);
3795 if (option_return_does_auto_indent
)
3796 edit_auto_indent (edit
);
3797 format_paragraph (edit
, 0);
3801 edit_insert (edit
, '\n');
3802 if (option_return_does_auto_indent
)
3804 edit_auto_indent (edit
);
3809 edit_insert (edit
, '\n');
3812 case CK_MarkColumnPageUp
:
3813 edit
->column_highlight
= 1;
3816 edit_move_up (edit
, edit
->widget
.lines
- 1, 1);
3818 case CK_MarkColumnPageDown
:
3819 edit
->column_highlight
= 1;
3821 case CK_MarkPageDown
:
3822 edit_move_down (edit
, edit
->widget
.lines
- 1, 1);
3824 case CK_MarkColumnLeft
:
3825 edit
->column_highlight
= 1;
3828 if (option_fake_half_tabs
)
3830 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3832 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3835 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3836 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3840 edit_left_char_move_cmd (edit
);
3842 case CK_MarkColumnRight
:
3843 edit
->column_highlight
= 1;
3846 if (option_fake_half_tabs
)
3848 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3850 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3851 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3855 edit_right_char_move_cmd (edit
);
3857 case CK_TopOnScreen
:
3858 case CK_MarkToPageBegin
:
3859 edit_begin_page (edit
);
3861 case CK_BottomOnScreen
:
3862 case CK_MarkToPageEnd
:
3863 edit_end_page (edit
);
3866 case CK_MarkToWordBegin
:
3868 edit_left_word_move_cmd (edit
);
3871 case CK_MarkToWordEnd
:
3873 edit_right_word_move_cmd (edit
);
3875 case CK_MarkColumnUp
:
3876 edit
->column_highlight
= 1;
3879 edit_move_up (edit
, 1, 0);
3881 case CK_MarkColumnDown
:
3882 edit
->column_highlight
= 1;
3885 edit_move_down (edit
, 1, 0);
3887 case CK_MarkColumnParagraphUp
:
3888 edit
->column_highlight
= 1;
3889 case CK_ParagraphUp
:
3890 case CK_MarkParagraphUp
:
3891 edit_move_up_paragraph (edit
, 0);
3893 case CK_MarkColumnParagraphDown
:
3894 edit
->column_highlight
= 1;
3895 case CK_ParagraphDown
:
3896 case CK_MarkParagraphDown
:
3897 edit_move_down_paragraph (edit
, 0);
3899 case CK_MarkColumnScrollUp
:
3900 edit
->column_highlight
= 1;
3902 case CK_MarkScrollUp
:
3903 edit_move_up (edit
, 1, 1);
3905 case CK_MarkColumnScrollDown
:
3906 edit
->column_highlight
= 1;
3908 case CK_MarkScrollDown
:
3909 edit_move_down (edit
, 1, 1);
3913 edit_cursor_to_bol (edit
);
3917 edit_cursor_to_eol (edit
);
3920 /* if text marked shift block */
3921 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3923 if (edit
->mark2
< 0)
3924 edit_mark_cmd (edit
, 0);
3925 edit_move_block_to_right (edit
);
3929 if (option_cursor_beyond_eol
)
3930 edit_insert_over (edit
);
3931 edit_tab_cmd (edit
);
3932 if (option_auto_para_formatting
)
3934 format_paragraph (edit
, 0);
3935 edit
->force
|= REDRAW_PAGE
;
3939 check_and_wrap_line (edit
);
3944 case CK_InsertOverwrite
:
3945 edit
->overwrite
= !edit
->overwrite
;
3949 if (edit
->mark2
>= 0)
3951 if (edit
->column_highlight
)
3952 edit_push_undo_action (edit
, COLUMN_ON
);
3953 edit
->column_highlight
= 0;
3955 edit_mark_cmd (edit
, 0);
3958 if (!edit
->column_highlight
)
3959 edit_push_undo_action (edit
, COLUMN_OFF
);
3960 edit
->column_highlight
= 1;
3961 edit_mark_cmd (edit
, 0);
3964 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3965 edit
->force
|= REDRAW_PAGE
;
3968 if (edit
->column_highlight
)
3969 edit_push_undo_action (edit
, COLUMN_ON
);
3970 edit
->column_highlight
= 0;
3971 edit_mark_cmd (edit
, 1);
3974 if (edit
->column_highlight
)
3975 edit_push_undo_action (edit
, COLUMN_ON
);
3976 edit
->column_highlight
= 0;
3977 edit_mark_current_word_cmd (edit
);
3980 if (edit
->column_highlight
)
3981 edit_push_undo_action (edit
, COLUMN_ON
);
3982 edit
->column_highlight
= 0;
3983 edit_mark_current_line_cmd (edit
);
3987 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
3988 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
3989 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3991 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3993 case CK_BookmarkFlush
:
3994 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3995 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3996 edit
->force
|= REDRAW_PAGE
;
3998 case CK_BookmarkNext
:
3999 if (edit
->book_mark
)
4001 struct _book_mark
*p
;
4002 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4006 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4007 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4008 edit_move_to_line (edit
, p
->line
);
4012 case CK_BookmarkPrev
:
4013 if (edit
->book_mark
)
4015 struct _book_mark
*p
;
4016 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4017 while (p
->line
== edit
->curs_line
)
4022 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4023 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4024 edit_move_to_line (edit
, p
->line
);
4030 case CK_MarkToFileBegin
:
4031 edit_move_to_top (edit
);
4034 case CK_MarkToFileEnd
:
4035 edit_move_to_bottom (edit
);
4039 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4040 edit_insert_over (edit
);
4041 edit_block_copy_cmd (edit
);
4044 edit_block_delete_cmd (edit
);
4047 edit_block_move_cmd (edit
);
4050 case CK_BlockShiftLeft
:
4051 if (edit
->mark1
!= edit
->mark2
)
4052 edit_move_block_to_left (edit
);
4054 case CK_BlockShiftRight
:
4055 if (edit
->mark1
!= edit
->mark2
)
4056 edit_move_block_to_right (edit
);
4059 edit_copy_to_X_buf_cmd (edit
);
4062 edit_cut_to_X_buf_cmd (edit
);
4065 /* if non persistent selection and text selected */
4066 if (!option_persistent_selections
)
4068 if (edit
->mark1
!= edit
->mark2
)
4069 edit_block_delete_cmd (edit
);
4071 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4072 edit_insert_over (edit
);
4073 edit_paste_from_X_buf_cmd (edit
);
4076 edit_paste_from_history (edit
);
4080 edit_save_as_cmd (edit
);
4083 edit_save_confirm_cmd (edit
);
4086 edit_save_block_cmd (edit
);
4089 edit_insert_file_cmd (edit
);
4093 edit_load_back_cmd (edit
);
4096 edit_load_forward_cmd (edit
);
4099 case CK_SyntaxChoose
:
4100 edit_syntax_dialog (edit
);
4104 edit_search_cmd (edit
, FALSE
);
4106 case CK_SearchContinue
:
4107 edit_search_cmd (edit
, TRUE
);
4110 edit_replace_cmd (edit
, 0);
4112 case CK_ReplaceContinue
:
4113 edit_replace_cmd (edit
, 1);
4116 /* if text marked shift block */
4117 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
4119 edit_move_block_to_left (edit
);
4123 edit_complete_word_cmd (edit
);
4127 edit_get_match_keyword_cmd (edit
);
4130 case CK_SpellCheckCurrentWord
:
4131 edit_suggest_current_word (edit
);
4134 edit_spellcheck_file (edit
);
4136 case CK_SpellCheckSelectLang
:
4137 edit_set_spell_lang ();
4143 /* fool gcc to prevent a Y2K warning */
4144 char time_format
[] = "_c";
4145 time_format
[0] = '%';
4147 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4148 edit_print_string (edit
, s
);
4149 edit
->force
|= REDRAW_PAGE
;
4154 edit_goto_cmd (edit
);
4156 case CK_ParagraphFormat
:
4157 format_paragraph (edit
, 1);
4158 edit
->force
|= REDRAW_PAGE
;
4160 case CK_MacroDelete
:
4161 edit_delete_macro_cmd (edit
);
4163 case CK_MatchBracket
:
4164 edit_goto_matching_bracket (edit
);
4167 user_menu (edit
, NULL
, -1);
4170 edit_sort_cmd (edit
);
4172 case CK_ExternalCommand
:
4173 edit_ext_cmd (edit
);
4176 edit_mail_dialog (edit
);
4179 case CK_SelectCodepage
:
4180 edit_select_codepage_cmd (edit
);
4183 case CK_InsertLiteral
:
4184 edit_insert_literal_cmd (edit
);
4186 case CK_MacroStartStopRecord
:
4187 edit_begin_end_macro_cmd (edit
);
4189 case CK_RepeatStartStopRecord
:
4190 edit_begin_end_repeat_cmd (edit
);
4192 case CK_ExtendedKeyMap
:
4193 edit
->extmod
= TRUE
;
4200 if ((command
/ CK_PipeBlock (0)) == 1)
4201 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
4203 /* keys which must set the col position, and the search vars */
4207 case CK_SearchContinue
:
4209 case CK_ReplaceContinue
:
4211 edit
->prev_col
= edit_get_col (edit
);
4215 case CK_MarkColumnUp
:
4218 case CK_MarkColumnDown
:
4221 case CK_MarkColumnPageUp
:
4223 case CK_MarkPageDown
:
4224 case CK_MarkColumnPageDown
:
4226 case CK_MarkToFileBegin
:
4228 case CK_MarkToFileEnd
:
4229 case CK_ParagraphUp
:
4230 case CK_MarkParagraphUp
:
4231 case CK_MarkColumnParagraphUp
:
4232 case CK_ParagraphDown
:
4233 case CK_MarkParagraphDown
:
4234 case CK_MarkColumnParagraphDown
:
4236 case CK_MarkScrollUp
:
4237 case CK_MarkColumnScrollUp
:
4239 case CK_MarkScrollDown
:
4240 case CK_MarkColumnScrollDown
:
4241 edit
->search_start
= edit
->curs1
;
4242 edit
->found_len
= 0;
4245 edit
->found_len
= 0;
4246 edit
->prev_col
= edit_get_col (edit
);
4247 edit
->search_start
= edit
->curs1
;
4249 edit_find_bracket (edit
);
4251 if (option_auto_para_formatting
)
4257 case CK_DeleteToWordBegin
:
4258 case CK_DeleteToWordEnd
:
4259 case CK_DeleteToHome
:
4260 case CK_DeleteToEnd
:
4261 format_paragraph (edit
, 0);
4262 edit
->force
|= REDRAW_PAGE
;
4267 /* --------------------------------------------------------------------------------------------- */
4270 edit_stack_init (void)
4272 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4274 edit_history_moveto
[edit_stack_iterator
].filename_vpath
= NULL
;
4275 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4278 edit_stack_iterator
= 0;
4281 /* --------------------------------------------------------------------------------------------- */
4284 edit_stack_free (void)
4286 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4287 vfs_path_free (edit_history_moveto
[edit_stack_iterator
].filename_vpath
);
4290 /* --------------------------------------------------------------------------------------------- */
4294 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
4296 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4299 /* --------------------------------------------------------------------------------------------- */
4303 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
4305 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4308 /* --------------------------------------------------------------------------------------------- */