2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2008, 2009, 2010, 2011, 2012
6 The Free Software Foundation, Inc.
10 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
11 Andrew Borodin <aborodin@vmail.ru> 2012.
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 * \brief Source: editor low level data handling and cursor fundamentals
38 #include <sys/types.h>
47 #include "lib/global.h"
49 #include "lib/tty/color.h"
50 #include "lib/tty/tty.h" /* attrset() */
51 #include "lib/tty/key.h" /* is_idle() */
52 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
53 #include "lib/vfs/vfs.h"
54 #include "lib/strutil.h" /* utf string functions */
55 #include "lib/util.h" /* load_file_position(), save_file_position() */
56 #include "lib/timefmt.h" /* time formatting */
58 #include "lib/widget.h"
61 #include "lib/charsets.h" /* get_codepage_id */
64 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
66 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/main.h" /* macro_index */
68 #include "src/learn.h" /* learn_keys */
69 #include "src/keybind-defaults.h"
71 #include "edit-impl.h"
72 #include "editwidget.h"
77 /*** global variables ****************************************************************************/
79 int option_word_wrap_line_length
= DEFAULT_WRAP_LINE_LENGTH
;
80 int option_typewriter_wrap
= 0;
81 int option_auto_para_formatting
= 0;
82 int option_fill_tabs_with_spaces
= 0;
83 int option_return_does_auto_indent
= 1;
84 int option_backspace_through_tabs
= 0;
85 int option_fake_half_tabs
= 1;
86 int option_save_mode
= EDIT_QUICK_SAVE
;
87 int option_save_position
= 1;
88 int option_max_undo
= 32768;
89 int option_persistent_selections
= 1;
90 int option_cursor_beyond_eol
= 0;
91 int option_line_state
= 0;
92 int option_line_state_width
= 0;
94 int option_edit_right_extreme
= 0;
95 int option_edit_left_extreme
= 0;
96 int option_edit_top_extreme
= 0;
97 int option_edit_bottom_extreme
= 0;
98 int enable_show_tabs_tws
= 1;
99 int option_check_nl_at_eof
= 0;
100 int option_group_undo
= 0;
101 int show_right_margin
= 0;
103 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
104 char *option_backup_ext
= NULL
;
106 unsigned int edit_stack_iterator
= 0;
107 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
108 /* magic sequense for say than block is vertical */
109 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
111 /*** file scope macro definitions ****************************************************************/
113 #define TEMP_BUF_LEN 1024
115 #define space_width 1
117 /*** file scope type declarations ****************************************************************/
119 /*** file scope variables ************************************************************************/
121 /* detecting an error on save is easy: just check if every byte has been written. */
122 /* detecting an error on read, is not so easy 'cos there is not way to tell
123 whether you read everything or not. */
124 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
125 static const struct edit_filters
127 const char *read
, *write
, *extension
;
131 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
132 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
133 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
134 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
135 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
139 static 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
= FALSE
;
651 /* raise lock when file modified */
652 if (!edit
->modified
&& !edit
->delete_file
)
653 edit
->locked
= lock_file (edit
->filename_vpath
);
657 /* --------------------------------------------------------------------------------------------- */
660 edit_get_byte_ptr (WEdit
* edit
, 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 /* --------------------------------------------------------------------------------------------- */
726 /** check whether cursor is in indent part of line
728 * @param edit editor object
730 * @return TRUE if cursor is in indent, FALSE otherwise
734 is_in_indent (WEdit
* edit
)
738 for (p
= edit_bol (edit
, edit
->curs1
); p
< edit
->curs1
; p
++)
739 if (strchr (" \t", edit_get_byte (edit
, p
)) == NULL
)
745 /* --------------------------------------------------------------------------------------------- */
746 /** check whether line in editor is blank or not
748 * @param edit editor object
749 * @param offset position in file
751 * @return TRUE if line in blank, FALSE otherwise
755 is_blank (WEdit
* edit
, long offset
)
760 s
= edit_bol (edit
, offset
);
761 f
= edit_eol (edit
, offset
) - 1;
764 c
= edit_get_byte (edit
, s
++);
771 /* --------------------------------------------------------------------------------------------- */
772 /** returns the offset of line i */
775 edit_find_line (WEdit
* edit
, int line
)
779 if (!edit
->caches_valid
)
781 for (i
= 0; i
< N_LINE_CACHES
; i
++)
782 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
783 /* three offsets that we *know* are line 0 at 0 and these two: */
784 edit
->line_numbers
[1] = edit
->curs_line
;
785 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
786 edit
->line_numbers
[2] = edit
->total_lines
;
787 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
788 edit
->caches_valid
= TRUE
;
790 if (line
>= edit
->total_lines
)
791 return edit
->line_offsets
[2];
794 /* find the closest known point */
795 for (i
= 0; i
< N_LINE_CACHES
; i
++)
798 n
= abs (edit
->line_numbers
[i
] - line
);
806 return edit
->line_offsets
[j
]; /* know the offset exactly */
807 if (m
== 1 && j
>= 3)
808 i
= j
; /* one line different - caller might be looping, so stay in this cache */
810 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
811 if (line
> edit
->line_numbers
[j
])
812 edit
->line_offsets
[i
] =
813 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
815 edit
->line_offsets
[i
] =
816 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
817 edit
->line_numbers
[i
] = line
;
818 return edit
->line_offsets
[i
];
821 /* --------------------------------------------------------------------------------------------- */
822 /** moves up until a blank line is reached, or until just
823 before a non-blank line is reached */
826 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
829 if (edit
->curs_line
> 1)
831 if (line_is_blank (edit
, edit
->curs_line
))
833 if (line_is_blank (edit
, edit
->curs_line
- 1))
835 for (i
= edit
->curs_line
- 1; i
; i
--)
836 if (!line_is_blank (edit
, i
))
844 for (i
= edit
->curs_line
- 1; i
; i
--)
845 if (line_is_blank (edit
, i
))
851 for (i
= edit
->curs_line
- 1; i
; i
--)
852 if (line_is_blank (edit
, i
))
856 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
859 /* --------------------------------------------------------------------------------------------- */
860 /** moves down until a blank line is reached, or until just
861 before a non-blank line is reached */
864 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
867 if (edit
->curs_line
>= edit
->total_lines
- 1)
869 i
= edit
->total_lines
;
873 if (line_is_blank (edit
, edit
->curs_line
))
875 if (line_is_blank (edit
, edit
->curs_line
+ 1))
877 for (i
= edit
->curs_line
+ 1; i
; i
++)
878 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
886 for (i
= edit
->curs_line
+ 1; i
; i
++)
887 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
893 for (i
= edit
->curs_line
+ 1; i
; i
++)
894 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
898 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
901 /* --------------------------------------------------------------------------------------------- */
904 edit_begin_page (WEdit
* edit
)
906 edit_update_curs_row (edit
);
907 edit_move_up (edit
, edit
->curs_row
, 0);
910 /* --------------------------------------------------------------------------------------------- */
913 edit_end_page (WEdit
* edit
)
915 edit_update_curs_row (edit
);
916 edit_move_down (edit
, edit
->widget
.lines
- edit
->curs_row
- 1, 0);
920 /* --------------------------------------------------------------------------------------------- */
921 /** goto beginning of text */
924 edit_move_to_top (WEdit
* edit
)
928 edit_cursor_move (edit
, -edit
->curs1
);
929 edit_move_to_prev_col (edit
, 0);
930 edit
->force
|= REDRAW_PAGE
;
931 edit
->search_start
= 0;
932 edit_update_curs_row (edit
);
937 /* --------------------------------------------------------------------------------------------- */
938 /** goto end of text */
941 edit_move_to_bottom (WEdit
* edit
)
943 if (edit
->curs_line
< edit
->total_lines
)
945 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
946 edit
->start_display
= edit
->last_byte
;
947 edit
->start_line
= edit
->total_lines
;
948 edit_scroll_upward (edit
, edit
->widget
.lines
- 1);
949 edit
->force
|= REDRAW_PAGE
;
953 /* --------------------------------------------------------------------------------------------- */
954 /** goto beginning of line */
957 edit_cursor_to_bol (WEdit
* edit
)
959 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
960 edit
->search_start
= edit
->curs1
;
961 edit
->prev_col
= edit_get_col (edit
);
965 /* --------------------------------------------------------------------------------------------- */
966 /** goto end of line */
969 edit_cursor_to_eol (WEdit
* edit
)
971 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
972 edit
->search_start
= edit
->curs1
;
973 edit
->prev_col
= edit_get_col (edit
);
977 /* --------------------------------------------------------------------------------------------- */
984 const char option_chars_move_whole_word
[] =
985 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
991 if (*option_chars_move_whole_word
== '!')
995 if (g_ascii_isupper ((gchar
) c
))
997 else if (g_ascii_islower ((gchar
) c
))
999 else if (g_ascii_isalpha (c
))
1001 else if (isdigit (c
))
1003 else if (isspace (c
))
1005 q
= strchr (option_chars_move_whole_word
, c
);
1007 return 0xFFFFFFFFUL
;
1010 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1015 while ((q
= strchr (q
+ 1, c
)));
1019 /* --------------------------------------------------------------------------------------------- */
1022 edit_left_word_move (WEdit
* edit
, int s
)
1027 if (edit
->column_highlight
1028 && edit
->mark1
!= edit
->mark2
1029 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1031 edit_cursor_move (edit
, -1);
1034 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1035 c2
= edit_get_byte (edit
, edit
->curs1
);
1036 if (c1
== '\n' || c2
== '\n')
1038 if (!(my_type_of (c1
) & my_type_of (c2
)))
1040 if (isspace (c1
) && !isspace (c2
))
1043 if (!isspace (c1
) && isspace (c2
))
1048 /* --------------------------------------------------------------------------------------------- */
1051 edit_left_word_move_cmd (WEdit
* edit
)
1053 edit_left_word_move (edit
, 0);
1054 edit
->force
|= REDRAW_PAGE
;
1057 /* --------------------------------------------------------------------------------------------- */
1060 edit_right_word_move (WEdit
* edit
, int s
)
1065 if (edit
->column_highlight
1066 && edit
->mark1
!= edit
->mark2
1067 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
1069 edit_cursor_move (edit
, 1);
1070 if (edit
->curs1
>= edit
->last_byte
)
1072 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1073 c2
= edit_get_byte (edit
, edit
->curs1
);
1074 if (c1
== '\n' || c2
== '\n')
1076 if (!(my_type_of (c1
) & my_type_of (c2
)))
1078 if (isspace (c1
) && !isspace (c2
))
1081 if (!isspace (c1
) && isspace (c2
))
1086 /* --------------------------------------------------------------------------------------------- */
1089 edit_right_word_move_cmd (WEdit
* edit
)
1091 edit_right_word_move (edit
, 0);
1092 edit
->force
|= REDRAW_PAGE
;
1095 /* --------------------------------------------------------------------------------------------- */
1098 edit_right_char_move_cmd (WEdit
* edit
)
1104 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
1110 c
= edit_get_byte (edit
, edit
->curs1
);
1112 if (option_cursor_beyond_eol
&& c
== '\n')
1118 edit_cursor_move (edit
, cw
);
1122 /* --------------------------------------------------------------------------------------------- */
1125 edit_left_char_move_cmd (WEdit
* edit
)
1128 if (edit
->column_highlight
1129 && option_cursor_beyond_eol
1130 && edit
->mark1
!= edit
->mark2
1131 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1135 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1139 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1145 edit_cursor_move (edit
, -cw
);
1149 /* --------------------------------------------------------------------------------------------- */
1150 /** Up or down cursor moving.
1151 direction = TRUE - move up
1156 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
1159 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
1168 edit
->force
|= REDRAW_PAGE
;
1172 edit_scroll_upward (edit
, i
);
1174 edit_scroll_downward (edit
, i
);
1176 p
= edit_bol (edit
, edit
->curs1
);
1178 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
1180 edit_cursor_move (edit
, p
- edit
->curs1
);
1182 edit_move_to_prev_col (edit
, p
);
1184 /* search start of current multibyte char (like CJK) */
1185 if (edit
->curs1
+ 1 < edit
->last_byte
)
1187 edit_right_char_move_cmd (edit
);
1188 edit_left_char_move_cmd (edit
);
1191 edit
->search_start
= edit
->curs1
;
1192 edit
->found_len
= 0;
1195 /* --------------------------------------------------------------------------------------------- */
1198 edit_right_delete_word (WEdit
* edit
)
1203 if (edit
->curs1
>= edit
->last_byte
)
1205 c1
= edit_delete (edit
, 1);
1206 c2
= edit_get_byte (edit
, edit
->curs1
);
1207 if (c1
== '\n' || c2
== '\n')
1209 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1211 if (!(my_type_of (c1
) & my_type_of (c2
)))
1216 /* --------------------------------------------------------------------------------------------- */
1219 edit_left_delete_word (WEdit
* edit
)
1224 if (edit
->curs1
<= 0)
1226 c1
= edit_backspace (edit
, 1);
1227 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1228 if (c1
== '\n' || c2
== '\n')
1230 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1232 if (!(my_type_of (c1
) & my_type_of (c2
)))
1237 /* --------------------------------------------------------------------------------------------- */
1239 the start column position is not recorded, and hence does not
1240 undo as it happed. But who would notice.
1244 edit_do_undo (WEdit
* edit
)
1249 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1251 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1258 edit_cursor_move (edit
, 1);
1261 edit_cursor_move (edit
, -1);
1265 edit_backspace (edit
, 1);
1269 edit_delete (edit
, 1);
1272 edit
->column_highlight
= 1;
1275 edit
->column_highlight
= 0;
1278 if (ac
>= 256 && ac
< 512)
1279 edit_insert_ahead (edit
, ac
- 256);
1280 if (ac
>= 0 && ac
< 256)
1281 edit_insert (edit
, ac
);
1283 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1285 edit
->mark1
= ac
- MARK_1
;
1286 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1288 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1290 edit
->mark2
= ac
- MARK_2
;
1291 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1293 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1295 edit
->end_mark_curs
= ac
- MARK_CURS
;
1298 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1301 if (edit
->start_display
> ac
- KEY_PRESS
)
1303 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1304 edit
->force
|= REDRAW_PAGE
;
1306 else if (edit
->start_display
< ac
- KEY_PRESS
)
1308 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1309 edit
->force
|= REDRAW_PAGE
;
1311 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1312 edit_update_curs_row (edit
);
1315 edit
->undo_stack_disable
= 0;
1319 edit_do_redo (WEdit
* edit
)
1324 if (edit
->redo_stack_reset
)
1328 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1335 edit_cursor_move (edit
, 1);
1338 edit_cursor_move (edit
, -1);
1341 edit_backspace (edit
, 1);
1344 edit_delete (edit
, 1);
1347 edit
->column_highlight
= 1;
1350 edit
->column_highlight
= 0;
1353 if (ac
>= 256 && ac
< 512)
1354 edit_insert_ahead (edit
, ac
- 256);
1355 if (ac
>= 0 && ac
< 256)
1356 edit_insert (edit
, ac
);
1358 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1360 edit
->mark1
= ac
- MARK_1
;
1361 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1363 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1365 edit
->mark2
= ac
- MARK_2
;
1366 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1368 /* more than one pop usually means something big */
1370 edit
->force
|= REDRAW_PAGE
;
1373 if (edit
->start_display
> ac
- KEY_PRESS
)
1375 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1376 edit
->force
|= REDRAW_PAGE
;
1378 else if (edit
->start_display
< ac
- KEY_PRESS
)
1380 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1381 edit
->force
|= REDRAW_PAGE
;
1383 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1384 edit_update_curs_row (edit
);
1390 edit_group_undo (WEdit
* edit
)
1392 long ac
= KEY_PRESS
;
1393 long cur_ac
= KEY_PRESS
;
1394 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1396 cur_ac
= get_prev_undo_action (edit
);
1397 edit_do_undo (edit
);
1398 ac
= get_prev_undo_action (edit
);
1399 /* exit from cycle if option_group_undo is not set,
1400 * and make single UNDO operation
1402 if (!option_group_undo
)
1407 /* --------------------------------------------------------------------------------------------- */
1410 edit_delete_to_line_end (WEdit
* edit
)
1412 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
1416 edit_delete (edit
, 1);
1420 /* --------------------------------------------------------------------------------------------- */
1423 edit_delete_to_line_begin (WEdit
* edit
)
1425 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
1429 edit_backspace (edit
, 1);
1433 /* --------------------------------------------------------------------------------------------- */
1436 is_aligned_on_a_tab (WEdit
* edit
)
1438 edit_update_curs_col (edit
);
1439 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
1440 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
1443 /* --------------------------------------------------------------------------------------------- */
1446 right_of_four_spaces (WEdit
* edit
)
1449 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1450 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1452 return is_aligned_on_a_tab (edit
);
1456 /* --------------------------------------------------------------------------------------------- */
1459 left_of_four_spaces (WEdit
* edit
)
1462 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1463 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1465 return is_aligned_on_a_tab (edit
);
1469 /* --------------------------------------------------------------------------------------------- */
1472 edit_auto_indent (WEdit
* edit
)
1477 /* use the previous line as a template */
1478 p
= edit_move_backward (edit
, p
, 1);
1479 /* copy the leading whitespace of the line */
1481 { /* no range check - the line _is_ \n-terminated */
1482 c
= edit_get_byte (edit
, p
++);
1483 if (c
!= ' ' && c
!= '\t')
1485 edit_insert (edit
, c
);
1489 /* --------------------------------------------------------------------------------------------- */
1492 edit_double_newline (WEdit
* edit
)
1494 edit_insert (edit
, '\n');
1495 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1497 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1499 edit
->force
|= REDRAW_PAGE
;
1500 edit_insert (edit
, '\n');
1504 /* --------------------------------------------------------------------------------------------- */
1507 insert_spaces_tab (WEdit
* edit
, gboolean half
)
1511 edit_update_curs_col (edit
);
1512 i
= option_tab_spacing
* space_width
;
1515 i
= ((edit
->curs_col
/ i
) + 1) * i
- edit
->curs_col
;
1518 edit_insert (edit
, ' ');
1523 /* --------------------------------------------------------------------------------------------- */
1526 edit_tab_cmd (WEdit
* edit
)
1530 if (option_fake_half_tabs
)
1532 if (is_in_indent (edit
))
1534 /*insert a half tab (usually four spaces) unless there is a
1535 half tab already behind, then delete it and insert a
1537 if (option_fill_tabs_with_spaces
|| !right_of_four_spaces (edit
))
1538 insert_spaces_tab (edit
, TRUE
);
1541 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1542 edit_backspace (edit
, 1);
1543 edit_insert (edit
, '\t');
1548 if (option_fill_tabs_with_spaces
)
1549 insert_spaces_tab (edit
, FALSE
);
1551 edit_insert (edit
, '\t');
1554 /* --------------------------------------------------------------------------------------------- */
1557 check_and_wrap_line (WEdit
* edit
)
1560 if (!option_typewriter_wrap
)
1562 edit_update_curs_col (edit
);
1563 if (edit
->curs_col
< option_word_wrap_line_length
)
1569 c
= edit_get_byte (edit
, curs
);
1570 if (c
== '\n' || curs
<= 0)
1572 edit_insert (edit
, '\n');
1575 if (c
== ' ' || c
== '\t')
1577 int current
= edit
->curs1
;
1578 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
1579 edit_insert (edit
, '\n');
1580 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
1586 /* --------------------------------------------------------------------------------------------- */
1587 /** this find the matching bracket in either direction, and sets edit->bracket */
1590 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
1592 const char *const b
= "{}{[][()(", *p
;
1593 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
1594 unsigned long j
= 0;
1596 edit_update_curs_row (edit
);
1597 c
= edit_get_byte (edit
, edit
->curs1
);
1600 if (!furthest_bracket_search
)
1601 furthest_bracket_search
--;
1602 /* not on a bracket at all */
1605 /* the matching bracket */
1607 /* going left or right? */
1608 if (strchr ("{[(", c
))
1610 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
1612 /* out of buffer? */
1613 if (q
>= edit
->last_byte
|| q
< 0)
1615 a
= edit_get_byte (edit
, q
);
1616 /* don't want to eat CPU */
1617 if (j
++ > furthest_bracket_search
)
1619 /* out of screen? */
1622 if (q
< edit
->start_display
)
1624 /* count lines if searching downward */
1625 if (inc
> 0 && a
== '\n')
1626 if (n
++ >= edit
->widget
.lines
- edit
->curs_row
) /* out of screen */
1629 /* count bracket depth */
1630 i
+= (a
== c
) - (a
== d
);
1631 /* return if bracket depth is zero */
1639 /* --------------------------------------------------------------------------------------------- */
1642 edit_goto_matching_bracket (WEdit
* edit
)
1646 q
= edit_get_bracket (edit
, 0, 0);
1649 edit
->bracket
= edit
->curs1
;
1650 edit
->force
|= REDRAW_PAGE
;
1651 edit_cursor_move (edit
, q
- edit
->curs1
);
1655 /* --------------------------------------------------------------------------------------------- */
1658 edit_move_block_to_right (WEdit
* edit
)
1660 long start_mark
, end_mark
;
1661 long cur_bol
, start_bol
;
1663 if (eval_marks (edit
, &start_mark
, &end_mark
))
1666 start_bol
= edit_bol (edit
, start_mark
);
1667 cur_bol
= edit_bol (edit
, end_mark
- 1);
1671 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1672 if (option_fill_tabs_with_spaces
)
1673 insert_spaces_tab (edit
, option_fake_half_tabs
);
1675 edit_insert (edit
, '\t');
1676 edit_cursor_move (edit
, edit_bol (edit
, cur_bol
) - edit
->curs1
);
1681 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1683 while (cur_bol
>= start_bol
);
1685 edit
->force
|= REDRAW_PAGE
;
1688 /* --------------------------------------------------------------------------------------------- */
1691 edit_move_block_to_left (WEdit
* edit
)
1693 long start_mark
, end_mark
;
1694 long cur_bol
, start_bol
;
1697 if (eval_marks (edit
, &start_mark
, &end_mark
))
1700 start_bol
= edit_bol (edit
, start_mark
);
1701 cur_bol
= edit_bol (edit
, end_mark
- 1);
1708 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1710 if (option_fake_half_tabs
)
1711 del_tab_width
= HALF_TAB_SIZE
;
1713 del_tab_width
= option_tab_spacing
;
1715 next_char
= edit_get_byte (edit
, edit
->curs1
);
1716 if (next_char
== '\t')
1717 edit_delete (edit
, 1);
1718 else if (next_char
== ' ')
1719 for (i
= 1; i
<= del_tab_width
; i
++)
1721 if (next_char
== ' ')
1722 edit_delete (edit
, 1);
1723 next_char
= edit_get_byte (edit
, edit
->curs1
);
1729 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1731 while (cur_bol
>= start_bol
);
1733 edit
->force
|= REDRAW_PAGE
;
1736 /* --------------------------------------------------------------------------------------------- */
1738 * prints at the cursor
1739 * @returns the number of chars printed
1743 edit_print_string (WEdit
* e
, const char *s
)
1747 while (s
[i
] != '\0')
1748 edit_execute_cmd (e
, CK_InsertChar
, (unsigned char) s
[i
++]);
1749 e
->force
|= REDRAW_COMPLETELY
;
1750 edit_update_screen (e
);
1754 /* --------------------------------------------------------------------------------------------- */
1755 /*** public functions ****************************************************************************/
1756 /* --------------------------------------------------------------------------------------------- */
1758 /** User edit menu, like user menu (F2) but only in editor. */
1761 user_menu (WEdit
* edit
, const char *menu_file
, int selected_entry
)
1766 long start_mark
, end_mark
;
1768 vfs_path_t
*block_file_vpath
;
1770 block_file
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
1771 block_file_vpath
= vfs_path_from_str (block_file
);
1773 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
1775 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1777 /* run shell scripts from menu */
1778 if (user_menu_cmd (edit
, menu_file
, selected_entry
)
1779 && (mc_stat (block_file_vpath
, &status
) == 0) && (status
.st_size
!= 0))
1784 /* i.e. we have marked block */
1786 rc
= edit_block_delete_cmd (edit
);
1792 ins_len
= edit_insert_file (edit
, block_file_vpath
);
1793 if (nomark
== 0 && ins_len
> 0)
1794 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1796 /* truncate block file */
1797 fd
= fopen (block_file
, "w");
1801 g_free (block_file
);
1802 vfs_path_free (block_file_vpath
);
1804 edit_cursor_move (edit
, curs
- edit
->curs1
);
1805 edit
->force
|= REDRAW_PAGE
;
1806 send_message ((Widget
*) edit
, WIDGET_DRAW
, 0);
1809 /* --------------------------------------------------------------------------------------------- */
1812 edit_get_byte (WEdit
* edit
, long byte_index
)
1815 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1818 if (byte_index
>= edit
->curs1
)
1820 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1821 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
1825 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
1829 /* --------------------------------------------------------------------------------------------- */
1832 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1837 gchar
*next_ch
= NULL
;
1839 gchar utf8_buf
[UTF8_CHAR_LEN
+ 1];
1841 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1847 str
= edit_get_byte_ptr (edit
, byte_index
);
1855 res
= g_utf8_get_char_validated (str
, -1);
1859 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1861 for (i
= 0; i
< UTF8_CHAR_LEN
; i
++)
1862 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
);
1863 utf8_buf
[UTF8_CHAR_LEN
] = '\0';
1865 res
= g_utf8_get_char_validated (str
, -1);
1876 /* Calculate UTF-8 char width */
1877 next_ch
= g_utf8_next_char (str
);
1880 width
= next_ch
- str
;
1888 *char_width
= width
;
1892 /* --------------------------------------------------------------------------------------------- */
1895 edit_get_write_filter (const vfs_path_t
* write_name_vpath
, const vfs_path_t
* filename_vpath
)
1898 char *p
, *writename
;
1899 const vfs_path_element_t
*path_element
;
1901 i
= edit_find_filter (filename_vpath
);
1905 path_element
= vfs_path_get_by_index (write_name_vpath
, -1);
1906 writename
= name_quote (path_element
->path
, 0);
1907 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
1912 /* --------------------------------------------------------------------------------------------- */
1915 edit_write_stream (WEdit
* edit
, FILE * f
)
1919 if (edit
->lb
== LB_ASIS
)
1921 for (i
= 0; i
< edit
->last_byte
; i
++)
1922 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
1927 /* change line breaks */
1928 for (i
= 0; i
< edit
->last_byte
; i
++)
1930 unsigned char c
= edit_get_byte (edit
, i
);
1932 if (!(c
== '\n' || c
== '\r'))
1934 /* not line break */
1935 if (fputc (c
, f
) < 0)
1939 { /* (c == '\n' || c == '\r') */
1940 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
1944 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
1945 /* put one line break unconditionally */
1946 if (fputc ('\n', f
) < 0)
1949 i
++; /* 2 chars are processed */
1951 if (c
== '\r' && c1
== '\n')
1952 /* Windows line break; go to the next char */
1955 if (c
== '\r' && c1
== '\r')
1957 /* two Macintosh line breaks; put second line break */
1958 if (fputc ('\n', f
) < 0)
1963 if (fputc (c1
, f
) < 0)
1967 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
1968 /* put one line break unconditionally */
1969 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
1972 if (c
== '\r' && c1
== '\n')
1973 /* Windows line break; go to the next char */
1977 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
1978 /* put one line break unconditionally */
1979 if (fputc ('\r', f
) < 0)
1982 i
++; /* 2 chars are processed */
1984 if (c
== '\r' && c1
== '\n')
1985 /* Windows line break; go to the next char */
1988 if (c
== '\n' && c1
== '\n')
1990 /* two Windows line breaks; put second line break */
1991 if (fputc ('\r', f
) < 0)
1996 if (fputc (c1
, f
) < 0)
1999 case LB_ASIS
: /* default without changes */
2005 return edit
->last_byte
;
2008 /* --------------------------------------------------------------------------------------------- */
2011 is_break_char (char c
)
2013 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
2016 /* --------------------------------------------------------------------------------------------- */
2019 edit_get_word_from_pos (WEdit
* edit
, long start_pos
, long *start
, gsize
* len
, gsize
* cut
)
2023 GString
*match_expr
;
2024 unsigned char *bufpos
;
2027 for (word_start
= start_pos
; word_start
!= 0; word_start
--, cut_len
++)
2029 c1
= edit_get_byte (edit
, word_start
);
2030 c2
= edit_get_byte (edit
, word_start
- 1);
2032 if (is_break_char (c1
) != is_break_char (c2
) || c1
== '\n' || c2
== '\n')
2036 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
2037 match_expr
= g_string_sized_new (16);
2041 c1
= edit_get_byte (edit
, word_start
+ match_expr
->len
);
2042 c2
= edit_get_byte (edit
, word_start
+ match_expr
->len
+ 1);
2043 g_string_append_c (match_expr
, c1
);
2045 while (!(is_break_char (c1
) != is_break_char (c2
) || c1
== '\n' || c2
== '\n'));
2047 *len
= match_expr
->len
;
2048 *start
= word_start
;
2051 return g_string_free (match_expr
, FALSE
);
2054 /* --------------------------------------------------------------------------------------------- */
2055 /** inserts a file at the cursor, returns count of inserted bytes on success */
2058 edit_insert_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
2063 p
= edit_get_filter (filename_vpath
);
2067 long current
= edit
->curs1
;
2069 f
= (FILE *) popen (p
, "r");
2072 edit_insert_stream (edit
, f
);
2073 ins_len
= edit
->curs1
- current
;
2074 edit_cursor_move (edit
, -ins_len
);
2079 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
2080 edit_error_dialog (_("Error"), errmsg
);
2089 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
2090 edit_error_dialog (_("Error"), errmsg
);
2098 int i
, file
, blocklen
;
2099 long current
= edit
->curs1
;
2100 int vertical_insertion
= 0;
2103 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
2107 buf
= g_malloc0 (TEMP_BUF_LEN
);
2108 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2111 /* if contain signature VERTICAL_MAGIC then it vertical block */
2112 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2113 vertical_insertion
= 1;
2115 mc_lseek (file
, 0, SEEK_SET
);
2118 if (vertical_insertion
)
2123 blocklen
= edit_insert_column_of_text_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
2124 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2125 /* highlight inserted text then not persistent blocks */
2126 if (!option_persistent_selections
)
2128 if (!edit
->column_highlight
)
2129 edit_push_undo_action (edit
, COLUMN_OFF
);
2130 edit
->column_highlight
= 1;
2135 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2137 for (i
= 0; i
< blocklen
; i
++)
2138 edit_insert (edit
, buf
[i
]);
2140 /* highlight inserted text then not persistent blocks */
2141 if (!option_persistent_selections
&& edit
->modified
)
2143 edit_set_markers (edit
, edit
->curs1
, current
, 0, 0);
2144 if (edit
->column_highlight
)
2145 edit_push_undo_action (edit
, COLUMN_ON
);
2146 edit
->column_highlight
= 0;
2150 edit
->force
|= REDRAW_PAGE
;
2151 ins_len
= edit
->curs1
- current
;
2152 edit_cursor_move (edit
, -ins_len
);
2162 /* --------------------------------------------------------------------------------------------- */
2164 * Fill in the edit structure. Return NULL on failure. Pass edit as
2165 * NULL to allocate a new structure.
2167 * If line is 0, try to restore saved position. Otherwise put the
2168 * cursor on that line and show it in the middle of the screen.
2172 edit_init (WEdit
* edit
, int y
, int x
, int lines
, int cols
, const vfs_path_t
* filename_vpath
,
2175 gboolean to_free
= FALSE
;
2177 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2178 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
2181 edit_purge_widget (edit
);
2186 * Expand option_whole_chars_search by national letters using
2190 static char option_whole_chars_search_buf
[256];
2192 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
2195 size_t len
= str_term_width1 (option_whole_chars_search
);
2197 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
2199 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
2201 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
2203 option_whole_chars_search_buf
[len
++] = i
;
2207 option_whole_chars_search_buf
[len
] = 0;
2208 option_whole_chars_search
= option_whole_chars_search_buf
;
2210 #endif /* ENABLE_NLS */
2211 edit
= g_malloc0 (sizeof (WEdit
));
2215 edit
->drag_state
= MCEDIT_DRAG_NORMAL
;
2218 edit
->widget
.lines
= lines
;
2219 edit
->widget
.cols
= cols
;
2220 edit_save_size (edit
);
2221 edit
->fullscreen
= TRUE
;
2223 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2224 edit
->stat1
.st_uid
= getuid ();
2225 edit
->stat1
.st_gid
= getgid ();
2226 edit
->stat1
.st_mtime
= 0;
2230 edit
->force
|= REDRAW_PAGE
;
2232 /* set file name before load file */
2233 edit_set_filename (edit
, filename_vpath
);
2235 edit
->undo_stack_size
= START_STACK_SIZE
;
2236 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2237 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2239 edit
->redo_stack_size
= START_STACK_SIZE
;
2240 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2241 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2244 edit
->converter
= str_cnv_from_term
;
2245 edit_set_codeset (edit
);
2247 if (!edit_load_file (edit
))
2249 /* edit_load_file already gives an error message */
2255 edit
->loading_done
= 1;
2258 edit_load_syntax (edit
, NULL
, NULL
);
2261 edit_get_syntax_color (edit
, -1, &color
);
2264 /* load saved cursor position */
2265 if ((line
== 0) && option_save_position
)
2266 edit_load_position (edit
);
2271 edit_move_display (edit
, line
- 1);
2272 edit_move_to_line (edit
, line
- 1);
2275 edit_load_macro_cmd (edit
);
2280 /* --------------------------------------------------------------------------------------------- */
2282 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2284 edit_clean (WEdit
* edit
)
2291 /* a stale lock, remove it */
2293 edit
->locked
= unlock_file (edit
->filename_vpath
);
2295 /* save cursor position */
2296 if (option_save_position
)
2297 edit_save_position (edit
);
2298 else if (edit
->serialized_bookmarks
!= NULL
)
2299 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2301 /* File specified on the mcedit command line and never saved */
2302 if (edit
->delete_file
)
2303 unlink (vfs_path_get_last_path_str (edit
->filename_vpath
));
2305 edit_free_syntax_rules (edit
);
2306 book_mark_flush (edit
, -1);
2307 for (; j
<= MAXBUFF
; j
++)
2309 g_free (edit
->buffers1
[j
]);
2310 g_free (edit
->buffers2
[j
]);
2313 g_free (edit
->undo_stack
);
2314 g_free (edit
->redo_stack
);
2315 vfs_path_free (edit
->filename_vpath
);
2316 vfs_path_free (edit
->dir_vpath
);
2317 mc_search_free (edit
->search
);
2318 edit
->search
= NULL
;
2320 if (edit
->converter
!= str_cnv_from_term
)
2321 str_close_conv (edit
->converter
);
2323 edit_purge_widget (edit
);
2328 /* --------------------------------------------------------------------------------------------- */
2331 * Load a new file into the editor and set line. If it fails, preserve the old file.
2332 * To do it, allocate a new widget, initialize it and, if the new file
2333 * was loaded, copy the data to the old widget.
2335 * @returns TRUE on success, FALSE on failure.
2338 edit_reload_line (WEdit
* edit
, const vfs_path_t
* filename_vpath
, long line
)
2341 int y
= edit
->widget
.y
;
2342 int x
= edit
->widget
.x
;
2343 int lines
= edit
->widget
.lines
;
2344 int columns
= edit
->widget
.cols
;
2346 e
= g_malloc0 (sizeof (WEdit
));
2347 e
->widget
= edit
->widget
;
2349 if (edit_init (e
, y
, x
, lines
, columns
, filename_vpath
, line
) == NULL
)
2356 memcpy (edit
, e
, sizeof (WEdit
));
2362 /* --------------------------------------------------------------------------------------------- */
2365 edit_set_codeset (WEdit
* edit
)
2371 get_codepage_id (mc_global
.source_codepage
>=
2372 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2377 conv
= str_crt_conv_from (cp_id
);
2378 if (conv
!= INVALID_CONV
)
2380 if (edit
->converter
!= str_cnv_from_term
)
2381 str_close_conv (edit
->converter
);
2382 edit
->converter
= conv
;
2387 edit
->utf8
= str_isutf8 (cp_id
);
2394 /* --------------------------------------------------------------------------------------------- */
2396 Recording stack for undo:
2397 The following is an implementation of a compressed stack. Identical
2398 pushes are recorded by a negative prefix indicating the number of times the
2399 same char was pushed. This saves space for repeated curs-left or curs-right
2416 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2417 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2418 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2419 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2422 The only way the cursor moves or the buffer is changed is through the routines:
2423 insert, backspace, insert_ahead, delete, and cursor_move.
2424 These record the reverse undo movements onto the stack each time they are
2427 Each key press results in a set of actions (insert; delete ...). So each time
2428 a key is pressed the current position of start_display is pushed as
2429 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2430 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2431 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2436 edit_push_undo_action (WEdit
* edit
, long c
, ...)
2438 unsigned long sp
= edit
->undo_stack_pointer
;
2442 /* first enlarge the stack if necessary */
2443 if (sp
> edit
->undo_stack_size
- 10)
2445 if (option_max_undo
< 256)
2446 option_max_undo
= 256;
2447 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2449 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2452 edit
->undo_stack
= t
;
2453 edit
->undo_stack_size
<<= 1;
2454 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2458 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2459 if (edit
->undo_stack_disable
)
2461 edit_push_redo_action (edit
, KEY_PRESS
);
2462 edit_push_redo_action (edit
, c
);
2465 else if (edit
->redo_stack_reset
)
2467 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2470 if (edit
->undo_stack_bottom
!= sp
2471 && spm1
!= edit
->undo_stack_bottom
2472 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2475 if (edit
->undo_stack
[spm1
] < 0)
2477 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2480 if (edit
->undo_stack
[spm1
] > -1000000000)
2482 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2484 edit
->undo_stack
[spm1
]--;
2492 d
= edit
->undo_stack
[spm1
];
2496 return; /* --> no need to push multiple do-nothings */
2497 edit
->undo_stack
[sp
] = -2;
2502 edit
->undo_stack
[sp
] = c
;
2505 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2507 /* if the sp wraps round and catches the undo_stack_bottom then erase
2508 * the first set of actions on the stack to make space - by moving
2509 * undo_stack_bottom forward one "key press" */
2510 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2511 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2512 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2515 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2517 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2518 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2520 /*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: */
2521 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2522 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2524 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2529 edit_push_redo_action (WEdit
* edit
, long c
, ...)
2531 unsigned long sp
= edit
->redo_stack_pointer
;
2534 /* first enlarge the stack if necessary */
2535 if (sp
> edit
->redo_stack_size
- 10)
2537 if (option_max_undo
< 256)
2538 option_max_undo
= 256;
2539 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2541 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2544 edit
->redo_stack
= t
;
2545 edit
->redo_stack_size
<<= 1;
2546 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2550 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2552 if (edit
->redo_stack_bottom
!= sp
2553 && spm1
!= edit
->redo_stack_bottom
2554 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2557 if (edit
->redo_stack
[spm1
] < 0)
2559 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2562 if (edit
->redo_stack
[spm1
] > -1000000000)
2564 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2565 edit
->redo_stack
[spm1
]--;
2572 d
= edit
->redo_stack
[spm1
];
2576 return; /* --> no need to push multiple do-nothings */
2577 edit
->redo_stack
[sp
] = -2;
2578 goto redo_check_bottom
;
2582 edit
->redo_stack
[sp
] = c
;
2585 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2587 /* if the sp wraps round and catches the redo_stack_bottom then erase
2588 * the first set of actions on the stack to make space - by moving
2589 * redo_stack_bottom forward one "key press" */
2590 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2591 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2592 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2595 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2597 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2598 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2601 * If a single key produced enough pushes to wrap all the way round then
2602 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2603 * The stack is then initialised:
2606 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2607 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2608 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2612 /* --------------------------------------------------------------------------------------------- */
2614 Basic low level single character buffer alterations and movements at the cursor.
2615 Returns char passed over, inserted or removed.
2619 edit_insert (WEdit
* edit
, int c
)
2621 /* check if file has grown to large */
2622 if (edit
->last_byte
>= SIZE_LIMIT
)
2625 /* first we must update the position of the display window */
2626 if (edit
->curs1
< edit
->start_display
)
2628 edit
->start_display
++;
2633 /* Mark file as modified, unless the file hasn't been fully loaded */
2634 if (edit
->loading_done
)
2636 edit_modification (edit
);
2639 /* now we must update some info on the file and check if a redraw is required */
2642 if (edit
->book_mark
)
2643 book_mark_inc (edit
, edit
->curs_line
);
2645 edit
->total_lines
++;
2646 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2649 /* save the reverse command onto the undo stack */
2650 /* ordinary char and not space */
2652 edit_push_undo_action (edit
, BACKSPACE
);
2654 edit_push_undo_action (edit
, BACKSPACE_BR
);
2655 /* update markers */
2656 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2657 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2658 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2660 /* add a new buffer if we've reached the end of the last one */
2661 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2662 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2664 /* perform the insertion */
2665 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2666 = (unsigned char) c
;
2668 /* update file length */
2671 /* update cursor position */
2675 /* --------------------------------------------------------------------------------------------- */
2676 /** same as edit_insert and move left */
2679 edit_insert_ahead (WEdit
* edit
, int c
)
2681 if (edit
->last_byte
>= SIZE_LIMIT
)
2684 if (edit
->curs1
< edit
->start_display
)
2686 edit
->start_display
++;
2690 edit_modification (edit
);
2693 if (edit
->book_mark
)
2694 book_mark_inc (edit
, edit
->curs_line
);
2695 edit
->total_lines
++;
2696 edit
->force
|= REDRAW_AFTER_CURSOR
;
2698 /* ordinary char and not space */
2700 edit_push_undo_action (edit
, DELCHAR
);
2702 edit_push_undo_action (edit
, DELCHAR_BR
);
2704 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2705 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2706 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2708 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2709 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2710 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2711 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2718 /* --------------------------------------------------------------------------------------------- */
2721 edit_delete (WEdit
* edit
, const int byte_delete
)
2731 /* if byte_delete = 1 then delete only one byte not multibyte char */
2732 if (edit
->utf8
&& byte_delete
== 0)
2734 edit_get_utf (edit
, edit
->curs1
, &cw
);
2739 if (edit
->mark2
!= edit
->mark1
)
2740 edit_push_markers (edit
);
2742 for (i
= 1; i
<= cw
; i
++)
2744 if (edit
->mark1
> edit
->curs1
)
2747 edit
->end_mark_curs
--;
2749 if (edit
->mark2
> edit
->curs1
)
2751 if (edit
->last_get_rule
> edit
->curs1
)
2752 edit
->last_get_rule
--;
2754 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2756 1) & M_EDIT_BUF_SIZE
) - 1];
2758 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2760 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2761 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2765 edit_push_undo_action (edit
, p
+ 256);
2768 edit_modification (edit
);
2771 if (edit
->book_mark
)
2772 book_mark_dec (edit
, edit
->curs_line
);
2773 edit
->total_lines
--;
2774 edit
->force
|= REDRAW_AFTER_CURSOR
;
2776 if (edit
->curs1
< edit
->start_display
)
2778 edit
->start_display
--;
2786 /* --------------------------------------------------------------------------------------------- */
2789 edit_backspace (WEdit
* edit
, const int byte_delete
)
2795 if (edit
->curs1
== 0)
2800 if (edit
->mark2
!= edit
->mark1
)
2801 edit_push_markers (edit
);
2803 if (edit
->utf8
&& byte_delete
== 0)
2805 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
2809 for (i
= 1; i
<= cw
; i
++)
2811 if (edit
->mark1
>= edit
->curs1
)
2814 edit
->end_mark_curs
--;
2816 if (edit
->mark2
>= edit
->curs1
)
2818 if (edit
->last_get_rule
>= edit
->curs1
)
2819 edit
->last_get_rule
--;
2821 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
2822 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
2823 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2825 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2826 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2830 edit_push_undo_action (edit
, p
);
2832 edit_modification (edit
);
2835 if (edit
->book_mark
)
2836 book_mark_dec (edit
, edit
->curs_line
);
2838 edit
->total_lines
--;
2839 edit
->force
|= REDRAW_AFTER_CURSOR
;
2842 if (edit
->curs1
< edit
->start_display
)
2844 edit
->start_display
--;
2852 /* --------------------------------------------------------------------------------------------- */
2853 /** moves the cursor right or left: increment positive or negative respectively */
2856 edit_cursor_move (WEdit
* edit
, long increment
)
2858 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2863 for (; increment
< 0; increment
++)
2868 edit_push_undo_action (edit
, CURS_RIGHT
);
2870 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2871 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2872 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2873 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2874 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2876 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2877 1) & M_EDIT_BUF_SIZE
];
2878 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2880 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2881 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2887 edit
->force
|= REDRAW_LINE_BELOW
;
2892 else if (increment
> 0)
2894 for (; increment
> 0; increment
--)
2899 edit_push_undo_action (edit
, CURS_LEFT
);
2901 c
= edit_get_byte (edit
, edit
->curs1
);
2902 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2903 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2904 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2906 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2908 1) & M_EDIT_BUF_SIZE
) - 1];
2909 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2911 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2912 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2918 edit
->force
|= REDRAW_LINE_ABOVE
;
2924 /* These functions return positions relative to lines */
2926 /* --------------------------------------------------------------------------------------------- */
2927 /** returns index of last char on line + 1 */
2930 edit_eol (WEdit
* edit
, long current
)
2932 if (current
>= edit
->last_byte
)
2933 return edit
->last_byte
;
2936 if (edit_get_byte (edit
, current
) == '\n')
2941 /* --------------------------------------------------------------------------------------------- */
2942 /** returns index of first char on line */
2945 edit_bol (WEdit
* edit
, long current
)
2951 if (edit_get_byte (edit
, current
- 1) == '\n')
2956 /* --------------------------------------------------------------------------------------------- */
2959 edit_count_lines (WEdit
* edit
, long current
, long upto
)
2962 if (upto
> edit
->last_byte
)
2963 upto
= edit
->last_byte
;
2966 while (current
< upto
)
2967 if (edit_get_byte (edit
, current
++) == '\n')
2972 /* --------------------------------------------------------------------------------------------- */
2973 /* If lines is zero this returns the count of lines from current to upto. */
2974 /* If upto is zero returns index of lines forward current. */
2977 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
2981 return edit_count_lines (edit
, current
, upto
);
2990 next
= edit_eol (edit
, current
) + 1;
2991 if (next
> edit
->last_byte
)
3000 /* --------------------------------------------------------------------------------------------- */
3001 /** Returns offset of 'lines' lines up from current */
3004 edit_move_backward (WEdit
* edit
, long current
, long lines
)
3008 current
= edit_bol (edit
, current
);
3009 while ((lines
--) && current
!= 0)
3010 current
= edit_bol (edit
, current
- 1);
3014 /* --------------------------------------------------------------------------------------------- */
3015 /* If cols is zero this returns the count of columns from current to upto. */
3016 /* If upto is zero returns index of cols across from current. */
3019 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
3030 q
= edit
->last_byte
+ 2;
3032 for (col
= 0, p
= current
; p
< q
; p
++)
3044 orig_c
= c
= edit_get_byte (edit
, p
);
3052 utf_ch
= edit_get_utf (edit
, p
, &cw
);
3053 if (mc_global
.utf8_display
)
3057 if (g_unichar_iswide (utf_ch
))
3060 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
3064 c
= convert_to_display_c (c
);
3068 col
+= TAB_SIZE
- col
% TAB_SIZE
;
3076 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!mc_global
.utf8_display
&& !edit
->utf8
)))
3077 /* '\r' is shown as ^M, so we must advance 2 characters */
3078 /* Caret notation for control characters */
3086 /* --------------------------------------------------------------------------------------------- */
3087 /** returns the current column position of the cursor */
3090 edit_get_col (WEdit
* edit
)
3092 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3095 /* --------------------------------------------------------------------------------------------- */
3096 /* Scrolling functions */
3097 /* --------------------------------------------------------------------------------------------- */
3100 edit_update_curs_row (WEdit
* edit
)
3102 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
3105 /* --------------------------------------------------------------------------------------------- */
3108 edit_update_curs_col (WEdit
* edit
)
3110 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3113 /* --------------------------------------------------------------------------------------------- */
3116 edit_get_curs_col (const WEdit
* edit
)
3118 return edit
->curs_col
;
3121 /* --------------------------------------------------------------------------------------------- */
3122 /** moves the display start position up by i lines */
3125 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
3127 unsigned long lines_above
= edit
->start_line
;
3128 if (i
> lines_above
)
3132 edit
->start_line
-= i
;
3133 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
3134 edit
->force
|= REDRAW_PAGE
;
3135 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3137 edit_update_curs_row (edit
);
3141 /* --------------------------------------------------------------------------------------------- */
3142 /** returns 1 if could scroll, 0 otherwise */
3145 edit_scroll_downward (WEdit
* edit
, int i
)
3148 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->widget
.lines
- 1);
3149 if (lines_below
> 0)
3151 if (i
> lines_below
)
3153 edit
->start_line
+= i
;
3154 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
3155 edit
->force
|= REDRAW_PAGE
;
3156 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3158 edit_update_curs_row (edit
);
3161 /* --------------------------------------------------------------------------------------------- */
3164 edit_scroll_right (WEdit
* edit
, int i
)
3166 edit
->force
|= REDRAW_PAGE
;
3167 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3168 edit
->start_col
-= i
;
3171 /* --------------------------------------------------------------------------------------------- */
3174 edit_scroll_left (WEdit
* edit
, int i
)
3176 if (edit
->start_col
)
3178 edit
->start_col
+= i
;
3179 if (edit
->start_col
> 0)
3180 edit
->start_col
= 0;
3181 edit
->force
|= REDRAW_PAGE
;
3182 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3186 /* --------------------------------------------------------------------------------------------- */
3187 /* high level cursor movement commands */
3188 /* --------------------------------------------------------------------------------------------- */
3191 edit_move_to_prev_col (WEdit
* edit
, long p
)
3193 int prev
= edit
->prev_col
;
3194 int over
= edit
->over_col
;
3195 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
3197 if (option_cursor_beyond_eol
)
3199 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
3200 edit_eol (edit
, edit
->curs1
));
3202 if (line_len
< prev
+ edit
->over_col
)
3204 edit
->over_col
= prev
+ over
- line_len
;
3205 edit
->prev_col
= line_len
;
3206 edit
->curs_col
= line_len
;
3210 edit
->curs_col
= prev
+ over
;
3211 edit
->prev_col
= edit
->curs_col
;
3218 if (is_in_indent (edit
) && option_fake_half_tabs
)
3220 edit_update_curs_col (edit
);
3222 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
3224 int q
= edit
->curs_col
;
3225 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
3226 p
= edit_bol (edit
, edit
->curs1
);
3227 edit_cursor_move (edit
,
3228 edit_move_forward3 (edit
, p
, edit
->curs_col
,
3230 if (!left_of_four_spaces (edit
))
3231 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3237 /* --------------------------------------------------------------------------------------------- */
3238 /** check whether line in editor is blank or not
3240 * @param edit editor object
3241 * @param line number of line
3243 * @return TRUE if line in blank, FALSE otherwise
3247 line_is_blank (WEdit
* edit
, long line
)
3249 return is_blank (edit
, edit_find_line (edit
, line
));
3252 /* --------------------------------------------------------------------------------------------- */
3253 /** move cursor to line 'line' */
3256 edit_move_to_line (WEdit
* e
, long line
)
3258 if (line
< e
->curs_line
)
3259 edit_move_up (e
, e
->curs_line
- line
, 0);
3261 edit_move_down (e
, line
- e
->curs_line
, 0);
3262 edit_scroll_screen_over_cursor (e
);
3265 /* --------------------------------------------------------------------------------------------- */
3266 /** scroll window so that first visible line is 'line' */
3269 edit_move_display (WEdit
* e
, long line
)
3271 if (line
< e
->start_line
)
3272 edit_scroll_upward (e
, e
->start_line
- line
);
3274 edit_scroll_downward (e
, line
- e
->start_line
);
3277 /* --------------------------------------------------------------------------------------------- */
3278 /** save markers onto undo stack */
3281 edit_push_markers (WEdit
* edit
)
3283 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3284 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3285 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3288 /* --------------------------------------------------------------------------------------------- */
3291 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
3300 /* --------------------------------------------------------------------------------------------- */
3301 /** highlight marker toggle */
3304 edit_mark_cmd (WEdit
* edit
, int unmark
)
3306 edit_push_markers (edit
);
3309 edit_set_markers (edit
, 0, 0, 0, 0);
3310 edit
->force
|= REDRAW_PAGE
;
3314 if (edit
->mark2
>= 0)
3316 edit
->end_mark_curs
= -1;
3317 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3318 edit
->curs_col
+ edit
->over_col
);
3319 edit
->force
|= REDRAW_PAGE
;
3323 edit
->end_mark_curs
= edit
->curs1
;
3324 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3325 edit
->curs_col
+ edit
->over_col
);
3330 /* --------------------------------------------------------------------------------------------- */
3331 /** highlight the word under cursor */
3334 edit_mark_current_word_cmd (WEdit
* edit
)
3338 for (pos
= edit
->curs1
; pos
!= 0; pos
--)
3342 c1
= edit_get_byte (edit
, pos
);
3343 c2
= edit_get_byte (edit
, pos
- 1);
3344 if (!isspace (c1
) && isspace (c2
))
3346 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3351 for (; pos
< edit
->last_byte
; pos
++)
3355 c1
= edit_get_byte (edit
, pos
);
3356 c2
= edit_get_byte (edit
, pos
+ 1);
3357 if (!isspace (c1
) && isspace (c2
))
3359 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3362 edit
->mark2
= min (pos
+ 1, edit
->last_byte
);
3364 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3367 /* --------------------------------------------------------------------------------------------- */
3370 edit_mark_current_line_cmd (WEdit
* edit
)
3372 long pos
= edit
->curs1
;
3374 edit
->mark1
= edit_bol (edit
, pos
);
3375 edit
->mark2
= edit_eol (edit
, pos
);
3377 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3380 /* --------------------------------------------------------------------------------------------- */
3383 edit_delete_line (WEdit
* edit
)
3386 * Delete right part of the line.
3387 * Note that edit_get_byte() returns '\n' when byte position is
3390 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3392 (void) edit_delete (edit
, 1);
3397 * Note that edit_delete() will not corrupt anything if called while
3398 * cursor position is EOF.
3400 (void) edit_delete (edit
, 1);
3403 * Delete left part of the line.
3404 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3406 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3408 (void) edit_backspace (edit
, 1);
3412 /* --------------------------------------------------------------------------------------------- */
3415 edit_indent_width (WEdit
* edit
, long p
)
3418 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
3420 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
3423 /* --------------------------------------------------------------------------------------------- */
3426 edit_insert_indent (WEdit
* edit
, int indent
)
3428 if (!option_fill_tabs_with_spaces
)
3430 while (indent
>= TAB_SIZE
)
3432 edit_insert (edit
, '\t');
3436 while (indent
-- > 0)
3437 edit_insert (edit
, ' ');
3440 /* --------------------------------------------------------------------------------------------- */
3443 edit_push_key_press (WEdit
* edit
)
3445 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3446 if (edit
->mark2
== -1)
3448 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3449 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3453 /* --------------------------------------------------------------------------------------------- */
3456 edit_find_bracket (WEdit
* edit
)
3458 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3459 if (last_bracket
!= edit
->bracket
)
3460 edit
->force
|= REDRAW_PAGE
;
3461 last_bracket
= edit
->bracket
;
3464 /* --------------------------------------------------------------------------------------------- */
3466 * This executes a command as though the user initiated it through a key
3467 * press. Callback with WIDGET_KEY as a message calls this after
3468 * translating the key press. This function can be used to pass any
3469 * command to the editor. Note that the screen wouldn't update
3470 * automatically. Either of command or char_for_insertion must be
3471 * passed as -1. Commands are executed, and char_for_insertion is
3472 * inserted at the cursor.
3476 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3478 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3480 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3483 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3486 if (macro_index
!= -1)
3488 edit
->force
|= REDRAW_COMPLETELY
;
3489 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3491 edit_store_macro_cmd (edit
);
3495 else if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3497 edit_repeat_macro_cmd (edit
);
3503 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3505 record_macro_buf
[macro_index
].action
= command
;
3506 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3508 /* record the beginning of a set of editing actions initiated by a key press */
3509 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3510 edit_push_key_press (edit
);
3512 edit_execute_cmd (edit
, command
, char_for_insertion
);
3513 if (edit
->column_highlight
)
3514 edit
->force
|= REDRAW_PAGE
;
3517 /* --------------------------------------------------------------------------------------------- */
3519 This executes a command at a lower level than macro recording.
3520 It also does not push a key_press onto the undo stack. This means
3521 that if it is called many times, a single undo command will undo
3522 all of them. It also does not check for the Undo command.
3525 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3527 if (command
== CK_WindowFullscreen
)
3529 edit_toggle_fullscreen (edit
);
3533 /* handle window state */
3534 if (edit_handle_move_resize (edit
, command
))
3537 edit
->force
|= REDRAW_LINE
;
3539 /* The next key press will unhighlight the found string, so update
3541 if (edit
->found_len
|| edit
->column_highlight
)
3542 edit
->force
|= REDRAW_PAGE
;
3546 /* a mark command with shift-arrow */
3549 case CK_MarkToWordBegin
:
3550 case CK_MarkToWordEnd
:
3556 case CK_MarkPageDown
:
3557 case CK_MarkToFileBegin
:
3558 case CK_MarkToFileEnd
:
3559 case CK_MarkToPageBegin
:
3560 case CK_MarkToPageEnd
:
3561 case CK_MarkScrollUp
:
3562 case CK_MarkScrollDown
:
3563 case CK_MarkParagraphUp
:
3564 case CK_MarkParagraphDown
:
3565 /* a mark command with alt-arrow */
3566 case CK_MarkColumnPageUp
:
3567 case CK_MarkColumnPageDown
:
3568 case CK_MarkColumnLeft
:
3569 case CK_MarkColumnRight
:
3570 case CK_MarkColumnUp
:
3571 case CK_MarkColumnDown
:
3572 case CK_MarkColumnScrollUp
:
3573 case CK_MarkColumnScrollDown
:
3574 case CK_MarkColumnParagraphUp
:
3575 case CK_MarkColumnParagraphDown
:
3576 edit
->column_highlight
= 0;
3577 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3579 edit_mark_cmd (edit
, 1); /* clear */
3580 edit_mark_cmd (edit
, 0); /* marking on */
3582 edit
->highlight
= 1;
3585 /* any other command */
3587 if (edit
->highlight
)
3588 edit_mark_cmd (edit
, 0); /* clear */
3589 edit
->highlight
= 0;
3592 /* first check for undo */
3593 if (command
== CK_Undo
)
3595 edit
->redo_stack_reset
= 0;
3596 edit_group_undo (edit
);
3597 edit
->found_len
= 0;
3598 edit
->prev_col
= edit_get_col (edit
);
3599 edit
->search_start
= edit
->curs1
;
3602 /* check for redo */
3603 if (command
== CK_Redo
)
3605 edit
->redo_stack_reset
= 0;
3606 edit_do_redo (edit
);
3607 edit
->found_len
= 0;
3608 edit
->prev_col
= edit_get_col (edit
);
3609 edit
->search_start
= edit
->curs1
;
3613 edit
->redo_stack_reset
= 1;
3615 /* An ordinary key press */
3616 if (char_for_insertion
>= 0)
3618 /* if non persistent selection and text selected */
3619 if (!option_persistent_selections
)
3621 if (edit
->mark1
!= edit
->mark2
)
3622 edit_block_delete_cmd (edit
);
3624 if (edit
->overwrite
)
3626 /* remove char only one time, after input first byte, multibyte chars */
3627 if ((!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3628 && edit_get_byte (edit
, edit
->curs1
) != '\n')
3629 edit_delete (edit
, 0);
3631 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3632 edit_insert_over (edit
);
3634 if (char_for_insertion
> 255 && !mc_global
.utf8_display
)
3636 unsigned char str
[6 + 1];
3640 res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3650 while (str
[i
] != 0 && i
<= 6)
3652 char_for_insertion
= str
[i
];
3653 edit_insert (edit
, char_for_insertion
);
3659 edit_insert (edit
, char_for_insertion
);
3661 if (option_auto_para_formatting
)
3663 format_paragraph (edit
, 0);
3664 edit
->force
|= REDRAW_PAGE
;
3667 check_and_wrap_line (edit
);
3668 edit
->found_len
= 0;
3669 edit
->prev_col
= edit_get_col (edit
);
3670 edit
->search_start
= edit
->curs1
;
3671 edit_find_bracket (edit
);
3677 case CK_TopOnScreen
:
3678 case CK_BottomOnScreen
:
3691 if (edit
->mark2
>= 0)
3693 if (!option_persistent_selections
)
3695 if (edit
->column_highlight
)
3696 edit_push_undo_action (edit
, COLUMN_ON
);
3697 edit
->column_highlight
= 0;
3698 edit_mark_cmd (edit
, 1);
3705 case CK_TopOnScreen
:
3706 case CK_BottomOnScreen
:
3707 case CK_MarkToPageBegin
:
3708 case CK_MarkToPageEnd
:
3713 case CK_MarkToWordBegin
:
3714 case CK_MarkToWordEnd
:
3717 case CK_MarkColumnUp
:
3718 case CK_MarkColumnDown
:
3719 if (edit
->mark2
== -1)
3720 break; /*marking is following the cursor: may need to highlight a whole line */
3725 edit
->force
|= REDRAW_CHAR_ONLY
;
3728 /* basic cursor key commands */
3732 /* if non persistent selection and text selected */
3733 if (!option_persistent_selections
)
3735 if (edit
->mark1
!= edit
->mark2
)
3737 edit_block_delete_cmd (edit
);
3741 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3746 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3748 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3749 edit_backspace (edit
, 1);
3754 if (option_fake_half_tabs
)
3757 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3759 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3760 edit_backspace (edit
, 1);
3765 edit_backspace (edit
, 0);
3768 /* if non persistent selection and text selected */
3769 if (!option_persistent_selections
)
3771 if (edit
->mark1
!= edit
->mark2
)
3773 edit_block_delete_cmd (edit
);
3778 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3779 edit_insert_over (edit
);
3781 if (option_fake_half_tabs
)
3784 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3786 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3787 edit_delete (edit
, 1);
3791 edit_delete (edit
, 0);
3793 case CK_DeleteToWordBegin
:
3795 edit_left_delete_word (edit
);
3797 case CK_DeleteToWordEnd
:
3798 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3799 edit_insert_over (edit
);
3801 edit_right_delete_word (edit
);
3804 edit_delete_line (edit
);
3806 case CK_DeleteToHome
:
3807 edit_delete_to_line_begin (edit
);
3809 case CK_DeleteToEnd
:
3810 edit_delete_to_line_end (edit
);
3814 if (option_auto_para_formatting
)
3816 edit_double_newline (edit
);
3817 if (option_return_does_auto_indent
)
3818 edit_auto_indent (edit
);
3819 format_paragraph (edit
, 0);
3823 edit_insert (edit
, '\n');
3824 if (option_return_does_auto_indent
)
3826 edit_auto_indent (edit
);
3831 edit_insert (edit
, '\n');
3834 case CK_MarkColumnPageUp
:
3835 edit
->column_highlight
= 1;
3838 edit_move_up (edit
, edit
->widget
.lines
- 1, 1);
3840 case CK_MarkColumnPageDown
:
3841 edit
->column_highlight
= 1;
3843 case CK_MarkPageDown
:
3844 edit_move_down (edit
, edit
->widget
.lines
- 1, 1);
3846 case CK_MarkColumnLeft
:
3847 edit
->column_highlight
= 1;
3850 if (option_fake_half_tabs
)
3852 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3854 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3857 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3858 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3862 edit_left_char_move_cmd (edit
);
3864 case CK_MarkColumnRight
:
3865 edit
->column_highlight
= 1;
3868 if (option_fake_half_tabs
)
3870 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3872 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3873 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3877 edit_right_char_move_cmd (edit
);
3879 case CK_TopOnScreen
:
3880 case CK_MarkToPageBegin
:
3881 edit_begin_page (edit
);
3883 case CK_BottomOnScreen
:
3884 case CK_MarkToPageEnd
:
3885 edit_end_page (edit
);
3888 case CK_MarkToWordBegin
:
3890 edit_left_word_move_cmd (edit
);
3893 case CK_MarkToWordEnd
:
3895 edit_right_word_move_cmd (edit
);
3897 case CK_MarkColumnUp
:
3898 edit
->column_highlight
= 1;
3901 edit_move_up (edit
, 1, 0);
3903 case CK_MarkColumnDown
:
3904 edit
->column_highlight
= 1;
3907 edit_move_down (edit
, 1, 0);
3909 case CK_MarkColumnParagraphUp
:
3910 edit
->column_highlight
= 1;
3911 case CK_ParagraphUp
:
3912 case CK_MarkParagraphUp
:
3913 edit_move_up_paragraph (edit
, 0);
3915 case CK_MarkColumnParagraphDown
:
3916 edit
->column_highlight
= 1;
3917 case CK_ParagraphDown
:
3918 case CK_MarkParagraphDown
:
3919 edit_move_down_paragraph (edit
, 0);
3921 case CK_MarkColumnScrollUp
:
3922 edit
->column_highlight
= 1;
3924 case CK_MarkScrollUp
:
3925 edit_move_up (edit
, 1, 1);
3927 case CK_MarkColumnScrollDown
:
3928 edit
->column_highlight
= 1;
3930 case CK_MarkScrollDown
:
3931 edit_move_down (edit
, 1, 1);
3935 edit_cursor_to_bol (edit
);
3939 edit_cursor_to_eol (edit
);
3942 /* if text marked shift block */
3943 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3945 if (edit
->mark2
< 0)
3946 edit_mark_cmd (edit
, 0);
3947 edit_move_block_to_right (edit
);
3951 if (option_cursor_beyond_eol
)
3952 edit_insert_over (edit
);
3953 edit_tab_cmd (edit
);
3954 if (option_auto_para_formatting
)
3956 format_paragraph (edit
, 0);
3957 edit
->force
|= REDRAW_PAGE
;
3961 check_and_wrap_line (edit
);
3966 case CK_InsertOverwrite
:
3967 edit
->overwrite
= !edit
->overwrite
;
3971 if (edit
->mark2
>= 0)
3973 if (edit
->column_highlight
)
3974 edit_push_undo_action (edit
, COLUMN_ON
);
3975 edit
->column_highlight
= 0;
3977 edit_mark_cmd (edit
, 0);
3980 if (!edit
->column_highlight
)
3981 edit_push_undo_action (edit
, COLUMN_OFF
);
3982 edit
->column_highlight
= 1;
3983 edit_mark_cmd (edit
, 0);
3986 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3987 edit
->force
|= REDRAW_PAGE
;
3990 if (edit
->column_highlight
)
3991 edit_push_undo_action (edit
, COLUMN_ON
);
3992 edit
->column_highlight
= 0;
3993 edit_mark_cmd (edit
, 1);
3996 if (edit
->column_highlight
)
3997 edit_push_undo_action (edit
, COLUMN_ON
);
3998 edit
->column_highlight
= 0;
3999 edit_mark_current_word_cmd (edit
);
4002 if (edit
->column_highlight
)
4003 edit_push_undo_action (edit
, COLUMN_ON
);
4004 edit
->column_highlight
= 0;
4005 edit_mark_current_line_cmd (edit
);
4009 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
4010 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
4011 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4013 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4015 case CK_BookmarkFlush
:
4016 book_mark_flush (edit
, BOOK_MARK_COLOR
);
4017 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
4018 edit
->force
|= REDRAW_PAGE
;
4020 case CK_BookmarkNext
:
4021 if (edit
->book_mark
)
4023 struct _book_mark
*p
;
4024 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4028 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4029 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4030 edit_move_to_line (edit
, p
->line
);
4034 case CK_BookmarkPrev
:
4035 if (edit
->book_mark
)
4037 struct _book_mark
*p
;
4038 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4039 while (p
->line
== edit
->curs_line
)
4044 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4045 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4046 edit_move_to_line (edit
, p
->line
);
4052 case CK_MarkToFileBegin
:
4053 edit_move_to_top (edit
);
4056 case CK_MarkToFileEnd
:
4057 edit_move_to_bottom (edit
);
4061 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4062 edit_insert_over (edit
);
4063 edit_block_copy_cmd (edit
);
4066 edit_block_delete_cmd (edit
);
4069 edit_block_move_cmd (edit
);
4072 case CK_BlockShiftLeft
:
4073 if (edit
->mark1
!= edit
->mark2
)
4074 edit_move_block_to_left (edit
);
4076 case CK_BlockShiftRight
:
4077 if (edit
->mark1
!= edit
->mark2
)
4078 edit_move_block_to_right (edit
);
4081 edit_copy_to_X_buf_cmd (edit
);
4084 edit_cut_to_X_buf_cmd (edit
);
4087 /* if non persistent selection and text selected */
4088 if (!option_persistent_selections
)
4090 if (edit
->mark1
!= edit
->mark2
)
4091 edit_block_delete_cmd (edit
);
4093 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4094 edit_insert_over (edit
);
4095 edit_paste_from_X_buf_cmd (edit
);
4098 edit_paste_from_history (edit
);
4102 edit_save_as_cmd (edit
);
4105 edit_save_confirm_cmd (edit
);
4108 edit_save_block_cmd (edit
);
4111 edit_insert_file_cmd (edit
);
4115 edit_load_back_cmd (edit
);
4118 edit_load_forward_cmd (edit
);
4121 case CK_SyntaxChoose
:
4122 edit_syntax_dialog (edit
);
4126 edit_search_cmd (edit
, FALSE
);
4128 case CK_SearchContinue
:
4129 edit_search_cmd (edit
, TRUE
);
4132 edit_replace_cmd (edit
, 0);
4134 case CK_ReplaceContinue
:
4135 edit_replace_cmd (edit
, 1);
4138 /* if text marked shift block */
4139 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
4141 edit_move_block_to_left (edit
);
4145 edit_complete_word_cmd (edit
);
4149 edit_get_match_keyword_cmd (edit
);
4152 case CK_SpellCheckCurrentWord
:
4153 edit_suggest_current_word (edit
);
4156 edit_spellcheck_file (edit
);
4158 case CK_SpellCheckSelectLang
:
4159 edit_set_spell_lang ();
4165 /* fool gcc to prevent a Y2K warning */
4166 char time_format
[] = "_c";
4167 time_format
[0] = '%';
4169 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4170 edit_print_string (edit
, s
);
4171 edit
->force
|= REDRAW_PAGE
;
4176 edit_goto_cmd (edit
);
4178 case CK_ParagraphFormat
:
4179 format_paragraph (edit
, 1);
4180 edit
->force
|= REDRAW_PAGE
;
4182 case CK_MacroDelete
:
4183 edit_delete_macro_cmd (edit
);
4185 case CK_MatchBracket
:
4186 edit_goto_matching_bracket (edit
);
4189 user_menu (edit
, NULL
, -1);
4192 edit_sort_cmd (edit
);
4194 case CK_ExternalCommand
:
4195 edit_ext_cmd (edit
);
4198 edit_mail_dialog (edit
);
4201 case CK_SelectCodepage
:
4202 edit_select_codepage_cmd (edit
);
4205 case CK_InsertLiteral
:
4206 edit_insert_literal_cmd (edit
);
4208 case CK_MacroStartStopRecord
:
4209 edit_begin_end_macro_cmd (edit
);
4211 case CK_RepeatStartStopRecord
:
4212 edit_begin_end_repeat_cmd (edit
);
4214 case CK_ExtendedKeyMap
:
4215 edit
->extmod
= TRUE
;
4222 if ((command
/ CK_PipeBlock (0)) == 1)
4223 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
4225 /* keys which must set the col position, and the search vars */
4229 case CK_SearchContinue
:
4231 case CK_ReplaceContinue
:
4233 edit
->prev_col
= edit_get_col (edit
);
4237 case CK_MarkColumnUp
:
4240 case CK_MarkColumnDown
:
4243 case CK_MarkColumnPageUp
:
4245 case CK_MarkPageDown
:
4246 case CK_MarkColumnPageDown
:
4248 case CK_MarkToFileBegin
:
4250 case CK_MarkToFileEnd
:
4251 case CK_ParagraphUp
:
4252 case CK_MarkParagraphUp
:
4253 case CK_MarkColumnParagraphUp
:
4254 case CK_ParagraphDown
:
4255 case CK_MarkParagraphDown
:
4256 case CK_MarkColumnParagraphDown
:
4258 case CK_MarkScrollUp
:
4259 case CK_MarkColumnScrollUp
:
4261 case CK_MarkScrollDown
:
4262 case CK_MarkColumnScrollDown
:
4263 edit
->search_start
= edit
->curs1
;
4264 edit
->found_len
= 0;
4267 edit
->found_len
= 0;
4268 edit
->prev_col
= edit_get_col (edit
);
4269 edit
->search_start
= edit
->curs1
;
4271 edit_find_bracket (edit
);
4273 if (option_auto_para_formatting
)
4279 case CK_DeleteToWordBegin
:
4280 case CK_DeleteToWordEnd
:
4281 case CK_DeleteToHome
:
4282 case CK_DeleteToEnd
:
4283 format_paragraph (edit
, 0);
4284 edit
->force
|= REDRAW_PAGE
;
4289 /* --------------------------------------------------------------------------------------------- */
4292 edit_stack_init (void)
4294 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4296 edit_history_moveto
[edit_stack_iterator
].filename_vpath
= NULL
;
4297 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4300 edit_stack_iterator
= 0;
4303 /* --------------------------------------------------------------------------------------------- */
4306 edit_stack_free (void)
4308 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4309 vfs_path_free (edit_history_moveto
[edit_stack_iterator
].filename_vpath
);
4312 /* --------------------------------------------------------------------------------------------- */
4316 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
4318 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4321 /* --------------------------------------------------------------------------------------------- */
4325 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
4327 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4330 /* --------------------------------------------------------------------------------------------- */