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
6 The Free Software Foundation, Inc.
10 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 * \brief Source: editor low level data handling and cursor fundamentals
37 #include <sys/types.h>
46 #include "lib/global.h"
48 #include "lib/tty/color.h"
49 #include "lib/tty/tty.h" /* attrset() */
50 #include "lib/tty/key.h" /* is_idle() */
51 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
52 #include "lib/vfs/vfs.h"
53 #include "lib/strutil.h" /* utf string functions */
54 #include "lib/util.h" /* load_file_position(), save_file_position() */
55 #include "lib/timefmt.h" /* time formatting */
57 #include "lib/widget.h"
60 #include "lib/charsets.h" /* get_codepage_id */
63 #include "src/filemanager/cmd.h" /* view_other_cmd() */
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 "edit-widget.h"
74 /*** global variables ****************************************************************************/
76 int option_word_wrap_line_length
= DEFAULT_WRAP_LINE_LENGTH
;
77 int option_typewriter_wrap
= 0;
78 int option_auto_para_formatting
= 0;
79 int option_fill_tabs_with_spaces
= 0;
80 int option_return_does_auto_indent
= 1;
81 int option_backspace_through_tabs
= 0;
82 int option_fake_half_tabs
= 1;
83 int option_save_mode
= EDIT_QUICK_SAVE
;
84 int option_save_position
= 1;
85 int option_max_undo
= 32768;
86 int option_persistent_selections
= 1;
87 int option_cursor_beyond_eol
= 0;
88 int option_line_state
= 0;
89 int option_line_state_width
= 0;
91 int option_edit_right_extreme
= 0;
92 int option_edit_left_extreme
= 0;
93 int option_edit_top_extreme
= 0;
94 int option_edit_bottom_extreme
= 0;
95 int enable_show_tabs_tws
= 1;
96 int option_check_nl_at_eof
= 0;
97 int option_group_undo
= 0;
98 int show_right_margin
= 0;
100 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
101 char *option_backup_ext
= NULL
;
103 int edit_stack_iterator
= 0;
104 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
105 /* magic sequense for say than block is vertical */
106 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
108 /*** file scope macro definitions ****************************************************************/
110 #define TEMP_BUF_LEN 1024
112 #define space_width 1
114 /*** file scope type declarations ****************************************************************/
116 /*** file scope variables ************************************************************************/
118 /* detecting an error on save is easy: just check if every byte has been written. */
119 /* detecting an error on read, is not so easy 'cos there is not way to tell
120 whether you read everything or not. */
121 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
122 static const struct edit_filters
124 const char *read
, *write
, *extension
;
128 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
129 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
130 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
131 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
132 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
136 static long last_bracket
= -1;
138 /*** file scope functions ************************************************************************/
139 /* --------------------------------------------------------------------------------------------- */
143 * here's a quick sketch of the layout: (don't run this through indent.)
145 * (b1 is buffers1 and b2 is buffers2)
148 * \0\0\0\0\0m e _ f i l e . \nf i n . \n|T h i s _ i s _ s o\0\0\0\0\0\0\0\0\0
149 * ______________________________________|______________________________________
151 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
152 * |-> |-> |-> |-> |-> |-> |
154 * _<------------------------->|<----------------->_
155 * WEdit->curs2 | WEdit->curs1
160 * file end|||file beginning
169 /* --------------------------------------------------------------------------------------------- */
171 static int left_of_four_spaces (WEdit
* edit
);
173 /* --------------------------------------------------------------------------------------------- */
178 const char *header
= N_("About");
179 const char *button_name
= N_("&OK");
180 const char *const version
= "MCEdit " VERSION
;
181 char text
[BUF_LARGE
];
183 int win_len
, version_len
, button_len
;
190 button_name
= _(button_name
);
193 button_len
= str_term_width1 (button_name
) + 5;
194 version_len
= str_term_width1 (version
);
196 g_snprintf (text
, sizeof (text
),
197 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
198 " A user friendly text editor\n"
199 " written for the Midnight Commander"));
201 win_len
= str_term_width1 (header
);
202 win_len
= max (win_len
, version_len
);
203 win_len
= max (win_len
, button_len
);
205 /* count width and height of text */
206 str_msg_term_size (text
, &lines
, &cols
);
208 cols
= max (win_len
, cols
) + 6;
211 about_dlg
= create_dlg (TRUE
, 0, 0, lines
, cols
, dialog_colors
, NULL
,
212 "[Internal File Editor]", header
, DLG_CENTER
| DLG_TRYUP
);
214 add_widget (about_dlg
, label_new (3, (cols
- version_len
) / 2, version
));
215 add_widget (about_dlg
, label_new (5, 3, text
));
216 add_widget (about_dlg
, button_new (lines
- 3, (cols
- button_len
) / 2,
217 B_ENTER
, NORMAL_BUTTON
, button_name
, NULL
));
220 destroy_dlg (about_dlg
);
223 /* --------------------------------------------------------------------------------------------- */
225 * Initialize the buffers for an empty files.
229 edit_init_buffers (WEdit
* edit
)
233 for (j
= 0; j
<= MAXBUFF
; j
++)
235 edit
->buffers1
[j
] = NULL
;
236 edit
->buffers2
[j
] = NULL
;
241 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
244 /* --------------------------------------------------------------------------------------------- */
246 * Load file OR text into buffers. Set cursor to the beginning of file.
247 * @returns 1 on error.
251 edit_load_file_fast (WEdit
* edit
, const char *filename
)
257 edit
->curs2
= edit
->last_byte
;
258 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
260 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
265 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
266 edit_error_dialog (_("Error"), errmsg
);
271 if (!edit
->buffers2
[buf2
])
272 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
277 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
278 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
281 for (buf
= buf2
- 1; buf
>= 0; buf
--)
283 /* edit->buffers2[0] is already allocated */
284 if (!edit
->buffers2
[buf
])
285 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
286 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
294 char *err_str
= g_strdup_printf (_("Error reading %s"), filename
);
295 edit_error_dialog (_("Error"), err_str
);
302 /* --------------------------------------------------------------------------------------------- */
303 /** Return index of the filter or -1 is there is no appropriate filter */
306 edit_find_filter (const char *filename
)
310 if (filename
== NULL
)
313 l
= strlen (filename
);
314 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
316 e
= strlen (all_filters
[i
].extension
);
318 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
324 /* --------------------------------------------------------------------------------------------- */
327 edit_get_filter (const char *filename
)
330 char *p
, *quoted_name
;
332 i
= edit_find_filter (filename
);
336 quoted_name
= name_quote (filename
, 0);
337 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
338 g_free (quoted_name
);
342 /* --------------------------------------------------------------------------------------------- */
345 edit_insert_stream (WEdit
* edit
, FILE * f
)
349 while ((c
= fgetc (f
)) >= 0)
351 edit_insert (edit
, c
);
357 /* --------------------------------------------------------------------------------------------- */
358 /** Open file and create it if necessary. Return 0 for success, 1 for error. */
361 check_file_access (WEdit
* edit
, const char *filename
, struct stat
*st
)
364 gchar
*errmsg
= NULL
;
366 /* Try opening an existing file */
367 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
372 * Try creating the file. O_EXCL prevents following broken links
373 * and opening existing files.
375 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
378 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
383 /* New file, delete it if it's not modified or saved */
384 edit
->delete_file
= 1;
388 /* Check what we have opened */
389 if (mc_fstat (file
, st
) < 0)
391 errmsg
= g_strdup_printf (_("Cannot get size/permissions for %s"), filename
);
395 /* We want to open regular files only */
396 if (!S_ISREG (st
->st_mode
))
398 errmsg
= g_strdup_printf (_("\"%s\" is not a regular file"), filename
);
403 * Don't delete non-empty files.
404 * O_EXCL should prevent it, but let's be on the safe side.
407 edit
->delete_file
= 0;
409 if (st
->st_size
>= SIZE_LIMIT
)
410 errmsg
= g_strdup_printf (_("File \"%s\" is too large"), filename
);
413 (void) mc_close (file
);
417 edit_error_dialog (_("Error"), errmsg
);
424 /* --------------------------------------------------------------------------------------------- */
426 * Open the file and load it into the buffers, either directly or using
427 * a filter. Return 0 on success, 1 on error.
429 * Fast loading (edit_load_file_fast) is used when the file size is
430 * known. In this case the data is read into the buffers by blocks.
431 * If the file size is not known, the data is loaded byte by byte in
436 edit_load_file (WEdit
* edit
)
439 vfs_path_t
*vpath
= vfs_path_from_str (edit
->filename
);
441 /* Cannot do fast load if a filter is used */
442 if (edit_find_filter (edit
->filename
) >= 0)
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 (vpath
))
451 vfs_path_free (vpath
);
454 * FIXME: line end translation should disable fast loading as well
455 * Consider doing fseek() to the end and ftell() for the real size.
460 /* If we are dealing with a real file, check that it exists */
461 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
466 /* nothing to load */
470 edit_init_buffers (edit
);
474 edit
->last_byte
= edit
->stat1
.st_size
;
475 edit_load_file_fast (edit
, edit
->filename
);
476 /* If fast load was used, the number of lines wasn't calculated */
477 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
484 edit
->undo_stack_disable
= 1;
485 if (edit_insert_file (edit
, edit
->filename
) < 0)
490 edit
->undo_stack_disable
= 0;
497 /* --------------------------------------------------------------------------------------------- */
498 /** Restore saved cursor position in the file */
501 edit_load_position (WEdit
* edit
)
508 if (!edit
->filename
|| !*edit
->filename
)
511 vpath
= vfs_path_from_str (edit
->filename
);
512 filename
= vfs_path_to_str (vpath
);
513 load_file_position (filename
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
514 vfs_path_free (vpath
);
519 edit_move_to_line (edit
, line
- 1);
520 edit
->prev_col
= column
;
524 edit_cursor_move (edit
, offset
);
525 line
= edit
->curs_line
;
526 edit
->search_start
= edit
->curs1
;
529 book_mark_restore (edit
, BOOK_MARK_COLOR
);
531 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
532 edit_move_display (edit
, line
- (edit
->widget
.lines
/ 2));
535 /* --------------------------------------------------------------------------------------------- */
536 /** Save cursor position in the file */
539 edit_save_position (WEdit
* edit
)
544 if (edit
->filename
== NULL
|| *edit
->filename
== '\0')
547 vpath
= vfs_path_from_str (edit
->filename
);
548 filename
= vfs_path_to_str (vpath
);
550 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
551 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
,
552 edit
->serialized_bookmarks
);
553 edit
->serialized_bookmarks
= NULL
;
556 vfs_path_free (vpath
);
559 /* --------------------------------------------------------------------------------------------- */
560 /** Clean the WEdit stricture except the widget part */
563 edit_purge_widget (WEdit
* edit
)
565 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
566 char *start
= (char *) edit
+ sizeof (Widget
);
567 memset (start
, 0, len
);
570 /* --------------------------------------------------------------------------------------------- */
573 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
574 then the file should be as it was when he loaded up. Then set edit->modified to 0.
578 edit_pop_undo_action (WEdit
* edit
)
581 unsigned long sp
= edit
->undo_stack_pointer
;
583 if (sp
== edit
->undo_stack_bottom
)
586 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
587 c
= edit
->undo_stack
[sp
];
590 /* edit->undo_stack[sp] = '@'; */
591 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
595 if (sp
== edit
->undo_stack_bottom
)
598 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
599 if (edit
->undo_stack
[sp
] == -2)
601 /* edit->undo_stack[sp] = '@'; */
602 edit
->undo_stack_pointer
= sp
;
605 edit
->undo_stack
[sp
]++;
611 edit_pop_redo_action (WEdit
* edit
)
614 unsigned long sp
= edit
->redo_stack_pointer
;
616 if (sp
== edit
->redo_stack_bottom
)
619 sp
= (sp
- 1) & edit
->redo_stack_size_mask
;
620 c
= edit
->redo_stack
[sp
];
623 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
627 if (sp
== edit
->redo_stack_bottom
)
630 c
= edit
->redo_stack
[(sp
- 1) & edit
->redo_stack_size_mask
];
631 if (edit
->redo_stack
[sp
] == -2)
632 edit
->redo_stack_pointer
= sp
;
634 edit
->redo_stack
[sp
]++;
640 get_prev_undo_action (WEdit
* edit
)
643 unsigned long sp
= edit
->undo_stack_pointer
;
645 if (sp
== edit
->undo_stack_bottom
)
648 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
649 c
= edit
->undo_stack
[sp
];
653 if (sp
== edit
->undo_stack_bottom
)
656 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
660 /* --------------------------------------------------------------------------------------------- */
661 /** is called whenever a modification is made by one of the four routines below */
664 edit_modification (WEdit
* edit
)
666 edit
->caches_valid
= 0;
668 /* raise lock when file modified */
669 if (!edit
->modified
&& !edit
->delete_file
)
670 edit
->locked
= edit_lock_file (edit
);
674 /* --------------------------------------------------------------------------------------------- */
677 edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
679 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
682 if (byte_index
>= edit
->curs1
)
686 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
687 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
688 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
691 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
692 (byte_index
& M_EDIT_BUF_SIZE
));
695 /* --------------------------------------------------------------------------------------------- */
698 edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
701 gchar utf8_buf
[3 * UTF8_CHAR_LEN
+ 1];
703 gchar
*cursor_buf_ptr
;
705 if (byte_index
> (edit
->curs1
+ edit
->curs2
) || byte_index
<= 0)
711 for (i
= 0; i
< (3 * UTF8_CHAR_LEN
); i
++)
712 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
- (2 * UTF8_CHAR_LEN
));
713 utf8_buf
[3 * UTF8_CHAR_LEN
] = '\0';
715 cursor_buf_ptr
= utf8_buf
+ (2 * UTF8_CHAR_LEN
);
716 str
= g_utf8_find_prev_char (utf8_buf
, cursor_buf_ptr
);
718 if (str
== NULL
|| g_utf8_next_char (str
) != cursor_buf_ptr
)
721 return *(cursor_buf_ptr
- 1);
725 res
= g_utf8_get_char_validated (str
, -1);
730 return *(cursor_buf_ptr
- 1);
734 *char_width
= cursor_buf_ptr
- str
;
740 /* --------------------------------------------------------------------------------------------- */
743 edit_backspace (WEdit
* edit
, const int byte_delete
)
754 if (edit
->mark2
!= edit
->mark1
)
755 edit_push_markers (edit
);
757 if (edit
->utf8
&& byte_delete
== 0)
759 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
763 for (i
= 1; i
<= cw
; i
++)
765 if (edit
->mark1
>= edit
->curs1
)
768 edit
->end_mark_curs
--;
770 if (edit
->mark2
>= edit
->curs1
)
772 if (edit
->last_get_rule
>= edit
->curs1
)
773 edit
->last_get_rule
--;
775 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
776 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
777 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
779 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
780 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
784 edit_push_undo_action (edit
, p
);
786 edit_modification (edit
);
790 book_mark_dec (edit
, edit
->curs_line
);
793 edit
->force
|= REDRAW_AFTER_CURSOR
;
796 if (edit
->curs1
< edit
->start_display
)
798 edit
->start_display
--;
806 /* --------------------------------------------------------------------------------------------- */
807 /* high level cursor movement commands */
808 /* --------------------------------------------------------------------------------------------- */
811 is_in_indent (WEdit
* edit
)
813 long p
= edit_bol (edit
, edit
->curs1
);
814 while (p
< edit
->curs1
)
815 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
820 /* --------------------------------------------------------------------------------------------- */
823 is_blank (WEdit
* edit
, long offset
)
827 s
= edit_bol (edit
, offset
);
828 f
= edit_eol (edit
, offset
) - 1;
831 c
= edit_get_byte (edit
, s
++);
839 /* --------------------------------------------------------------------------------------------- */
840 /** returns the offset of line i */
843 edit_find_line (WEdit
* edit
, int line
)
847 if (!edit
->caches_valid
)
849 for (i
= 0; i
< N_LINE_CACHES
; i
++)
850 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
851 /* three offsets that we *know* are line 0 at 0 and these two: */
852 edit
->line_numbers
[1] = edit
->curs_line
;
853 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
854 edit
->line_numbers
[2] = edit
->total_lines
;
855 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
856 edit
->caches_valid
= 1;
858 if (line
>= edit
->total_lines
)
859 return edit
->line_offsets
[2];
862 /* find the closest known point */
863 for (i
= 0; i
< N_LINE_CACHES
; i
++)
866 n
= abs (edit
->line_numbers
[i
] - line
);
874 return edit
->line_offsets
[j
]; /* know the offset exactly */
875 if (m
== 1 && j
>= 3)
876 i
= j
; /* one line different - caller might be looping, so stay in this cache */
878 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
879 if (line
> edit
->line_numbers
[j
])
880 edit
->line_offsets
[i
] =
881 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
883 edit
->line_offsets
[i
] =
884 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
885 edit
->line_numbers
[i
] = line
;
886 return edit
->line_offsets
[i
];
889 /* --------------------------------------------------------------------------------------------- */
890 /** moves up until a blank line is reached, or until just
891 before a non-blank line is reached */
894 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
897 if (edit
->curs_line
> 1)
899 if (line_is_blank (edit
, edit
->curs_line
))
901 if (line_is_blank (edit
, edit
->curs_line
- 1))
903 for (i
= edit
->curs_line
- 1; i
; i
--)
904 if (!line_is_blank (edit
, i
))
912 for (i
= edit
->curs_line
- 1; i
; i
--)
913 if (line_is_blank (edit
, i
))
919 for (i
= edit
->curs_line
- 1; i
; i
--)
920 if (line_is_blank (edit
, i
))
924 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
927 /* --------------------------------------------------------------------------------------------- */
928 /** moves down until a blank line is reached, or until just
929 before a non-blank line is reached */
932 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
935 if (edit
->curs_line
>= edit
->total_lines
- 1)
937 i
= edit
->total_lines
;
941 if (line_is_blank (edit
, edit
->curs_line
))
943 if (line_is_blank (edit
, edit
->curs_line
+ 1))
945 for (i
= edit
->curs_line
+ 1; i
; i
++)
946 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
954 for (i
= edit
->curs_line
+ 1; i
; i
++)
955 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
961 for (i
= edit
->curs_line
+ 1; i
; i
++)
962 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
966 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
969 /* --------------------------------------------------------------------------------------------- */
972 edit_begin_page (WEdit
* edit
)
974 edit_update_curs_row (edit
);
975 edit_move_up (edit
, edit
->curs_row
, 0);
978 /* --------------------------------------------------------------------------------------------- */
981 edit_end_page (WEdit
* edit
)
983 edit_update_curs_row (edit
);
984 edit_move_down (edit
, edit
->widget
.lines
- edit
->curs_row
- 1, 0);
988 /* --------------------------------------------------------------------------------------------- */
989 /** goto beginning of text */
992 edit_move_to_top (WEdit
* edit
)
996 edit_cursor_move (edit
, -edit
->curs1
);
997 edit_move_to_prev_col (edit
, 0);
998 edit
->force
|= REDRAW_PAGE
;
999 edit
->search_start
= 0;
1000 edit_update_curs_row (edit
);
1005 /* --------------------------------------------------------------------------------------------- */
1006 /** goto end of text */
1009 edit_move_to_bottom (WEdit
* edit
)
1011 if (edit
->curs_line
< edit
->total_lines
)
1013 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
1014 edit
->start_display
= edit
->last_byte
;
1015 edit
->start_line
= edit
->total_lines
;
1016 edit_scroll_upward (edit
, edit
->widget
.lines
- 1);
1017 edit
->force
|= REDRAW_PAGE
;
1021 /* --------------------------------------------------------------------------------------------- */
1022 /** goto beginning of line */
1025 edit_cursor_to_bol (WEdit
* edit
)
1027 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1028 edit
->search_start
= edit
->curs1
;
1029 edit
->prev_col
= edit_get_col (edit
);
1033 /* --------------------------------------------------------------------------------------------- */
1034 /** goto end of line */
1037 edit_cursor_to_eol (WEdit
* edit
)
1039 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1040 edit
->search_start
= edit
->curs1
;
1041 edit
->prev_col
= edit_get_col (edit
);
1045 /* --------------------------------------------------------------------------------------------- */
1047 static unsigned long
1052 const char option_chars_move_whole_word
[] =
1053 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1059 if (*option_chars_move_whole_word
== '!')
1061 return 0x80000000UL
;
1063 if (g_ascii_isupper ((gchar
) c
))
1065 else if (g_ascii_islower ((gchar
) c
))
1067 else if (g_ascii_isalpha (c
))
1069 else if (isdigit (c
))
1071 else if (isspace (c
))
1073 q
= strchr (option_chars_move_whole_word
, c
);
1075 return 0xFFFFFFFFUL
;
1078 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1083 while ((q
= strchr (q
+ 1, c
)));
1087 /* --------------------------------------------------------------------------------------------- */
1090 edit_left_word_move (WEdit
* edit
, int s
)
1095 if (edit
->column_highlight
1096 && edit
->mark1
!= edit
->mark2
1097 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1099 edit_cursor_move (edit
, -1);
1102 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1103 c2
= edit_get_byte (edit
, edit
->curs1
);
1104 if (c1
== '\n' || c2
== '\n')
1106 if (!(my_type_of (c1
) & my_type_of (c2
)))
1108 if (isspace (c1
) && !isspace (c2
))
1111 if (!isspace (c1
) && isspace (c2
))
1116 /* --------------------------------------------------------------------------------------------- */
1119 edit_left_word_move_cmd (WEdit
* edit
)
1121 edit_left_word_move (edit
, 0);
1122 edit
->force
|= REDRAW_PAGE
;
1125 /* --------------------------------------------------------------------------------------------- */
1128 edit_right_word_move (WEdit
* edit
, int s
)
1133 if (edit
->column_highlight
1134 && edit
->mark1
!= edit
->mark2
1135 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
1137 edit_cursor_move (edit
, 1);
1138 if (edit
->curs1
>= edit
->last_byte
)
1140 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1141 c2
= edit_get_byte (edit
, edit
->curs1
);
1142 if (c1
== '\n' || c2
== '\n')
1144 if (!(my_type_of (c1
) & my_type_of (c2
)))
1146 if (isspace (c1
) && !isspace (c2
))
1149 if (!isspace (c1
) && isspace (c2
))
1154 /* --------------------------------------------------------------------------------------------- */
1157 edit_right_word_move_cmd (WEdit
* edit
)
1159 edit_right_word_move (edit
, 0);
1160 edit
->force
|= REDRAW_PAGE
;
1163 /* --------------------------------------------------------------------------------------------- */
1166 edit_right_char_move_cmd (WEdit
* edit
)
1172 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
1178 c
= edit_get_byte (edit
, edit
->curs1
);
1180 if (option_cursor_beyond_eol
&& c
== '\n')
1186 edit_cursor_move (edit
, cw
);
1190 /* --------------------------------------------------------------------------------------------- */
1193 edit_left_char_move_cmd (WEdit
* edit
)
1196 if (edit
->column_highlight
1197 && option_cursor_beyond_eol
1198 && edit
->mark1
!= edit
->mark2
1199 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1203 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1207 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1213 edit_cursor_move (edit
, -cw
);
1217 /* --------------------------------------------------------------------------------------------- */
1218 /** Up or down cursor moving.
1219 direction = TRUE - move up
1224 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
1227 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
1236 edit
->force
|= REDRAW_PAGE
;
1240 edit_scroll_upward (edit
, i
);
1242 edit_scroll_downward (edit
, i
);
1244 p
= edit_bol (edit
, edit
->curs1
);
1246 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
1248 edit_cursor_move (edit
, p
- edit
->curs1
);
1250 edit_move_to_prev_col (edit
, p
);
1252 /* search start of current multibyte char (like CJK) */
1253 if (edit
->curs1
+ 1 < edit
->last_byte
)
1255 edit_right_char_move_cmd (edit
);
1256 edit_left_char_move_cmd (edit
);
1259 edit
->search_start
= edit
->curs1
;
1260 edit
->found_len
= 0;
1263 /* --------------------------------------------------------------------------------------------- */
1266 edit_right_delete_word (WEdit
* edit
)
1271 if (edit
->curs1
>= edit
->last_byte
)
1273 c1
= edit_delete (edit
, 1);
1274 c2
= edit_get_byte (edit
, edit
->curs1
);
1275 if (c1
== '\n' || c2
== '\n')
1277 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1279 if (!(my_type_of (c1
) & my_type_of (c2
)))
1284 /* --------------------------------------------------------------------------------------------- */
1287 edit_left_delete_word (WEdit
* edit
)
1292 if (edit
->curs1
<= 0)
1294 c1
= edit_backspace (edit
, 1);
1295 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1296 if (c1
== '\n' || c2
== '\n')
1298 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1300 if (!(my_type_of (c1
) & my_type_of (c2
)))
1305 /* --------------------------------------------------------------------------------------------- */
1307 the start column position is not recorded, and hence does not
1308 undo as it happed. But who would notice.
1312 edit_do_undo (WEdit
* edit
)
1317 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1319 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1326 edit_cursor_move (edit
, 1);
1329 edit_cursor_move (edit
, -1);
1333 edit_backspace (edit
, 1);
1337 edit_delete (edit
, 1);
1340 edit
->column_highlight
= 1;
1343 edit
->column_highlight
= 0;
1346 if (ac
>= 256 && ac
< 512)
1347 edit_insert_ahead (edit
, ac
- 256);
1348 if (ac
>= 0 && ac
< 256)
1349 edit_insert (edit
, ac
);
1351 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1353 edit
->mark1
= ac
- MARK_1
;
1354 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1356 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1358 edit
->mark2
= ac
- MARK_2
;
1359 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1361 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1363 edit
->end_mark_curs
= ac
- MARK_CURS
;
1366 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1369 if (edit
->start_display
> ac
- KEY_PRESS
)
1371 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1372 edit
->force
|= REDRAW_PAGE
;
1374 else if (edit
->start_display
< ac
- KEY_PRESS
)
1376 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1377 edit
->force
|= REDRAW_PAGE
;
1379 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1380 edit_update_curs_row (edit
);
1383 edit
->undo_stack_disable
= 0;
1387 edit_do_redo (WEdit
* edit
)
1392 if (edit
->redo_stack_reset
)
1396 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1403 edit_cursor_move (edit
, 1);
1406 edit_cursor_move (edit
, -1);
1409 edit_backspace (edit
, 1);
1412 edit_delete (edit
, 1);
1415 edit
->column_highlight
= 1;
1418 edit
->column_highlight
= 0;
1421 if (ac
>= 256 && ac
< 512)
1422 edit_insert_ahead (edit
, ac
- 256);
1423 if (ac
>= 0 && ac
< 256)
1424 edit_insert (edit
, ac
);
1426 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1428 edit
->mark1
= ac
- MARK_1
;
1429 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1431 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1433 edit
->mark2
= ac
- MARK_2
;
1434 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1436 /* more than one pop usually means something big */
1438 edit
->force
|= REDRAW_PAGE
;
1441 if (edit
->start_display
> ac
- KEY_PRESS
)
1443 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1444 edit
->force
|= REDRAW_PAGE
;
1446 else if (edit
->start_display
< ac
- KEY_PRESS
)
1448 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1449 edit
->force
|= REDRAW_PAGE
;
1451 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1452 edit_update_curs_row (edit
);
1458 edit_group_undo (WEdit
* edit
)
1460 long ac
= KEY_PRESS
;
1461 long cur_ac
= KEY_PRESS
;
1462 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1464 cur_ac
= get_prev_undo_action (edit
);
1465 edit_do_undo (edit
);
1466 ac
= get_prev_undo_action (edit
);
1467 /* exit from cycle if option_group_undo is not set,
1468 * and make single UNDO operation
1470 if (!option_group_undo
)
1475 /* --------------------------------------------------------------------------------------------- */
1478 edit_delete_to_line_end (WEdit
* edit
)
1480 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
1484 edit_delete (edit
, 1);
1488 /* --------------------------------------------------------------------------------------------- */
1491 edit_delete_to_line_begin (WEdit
* edit
)
1493 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
1497 edit_backspace (edit
, 1);
1501 /* --------------------------------------------------------------------------------------------- */
1504 is_aligned_on_a_tab (WEdit
* edit
)
1506 edit_update_curs_col (edit
);
1507 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
1508 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
1511 /* --------------------------------------------------------------------------------------------- */
1514 right_of_four_spaces (WEdit
* edit
)
1517 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1518 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1520 return is_aligned_on_a_tab (edit
);
1524 /* --------------------------------------------------------------------------------------------- */
1527 left_of_four_spaces (WEdit
* edit
)
1530 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1531 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1533 return is_aligned_on_a_tab (edit
);
1537 /* --------------------------------------------------------------------------------------------- */
1540 edit_auto_indent (WEdit
* edit
)
1545 /* use the previous line as a template */
1546 p
= edit_move_backward (edit
, p
, 1);
1547 /* copy the leading whitespace of the line */
1549 { /* no range check - the line _is_ \n-terminated */
1550 c
= edit_get_byte (edit
, p
++);
1551 if (c
!= ' ' && c
!= '\t')
1553 edit_insert (edit
, c
);
1557 /* --------------------------------------------------------------------------------------------- */
1560 edit_double_newline (WEdit
* edit
)
1562 edit_insert (edit
, '\n');
1563 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1565 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1567 edit
->force
|= REDRAW_PAGE
;
1568 edit_insert (edit
, '\n');
1572 /* --------------------------------------------------------------------------------------------- */
1575 insert_spaces_tab (WEdit
* edit
, gboolean half
)
1579 edit_update_curs_col (edit
);
1580 i
= option_tab_spacing
* space_width
;
1583 i
= ((edit
->curs_col
/ i
) + 1) * i
- edit
->curs_col
;
1586 edit_insert (edit
, ' ');
1591 /* --------------------------------------------------------------------------------------------- */
1594 edit_tab_cmd (WEdit
* edit
)
1598 if (option_fake_half_tabs
)
1600 if (is_in_indent (edit
))
1602 /*insert a half tab (usually four spaces) unless there is a
1603 half tab already behind, then delete it and insert a
1605 if (option_fill_tabs_with_spaces
|| !right_of_four_spaces (edit
))
1606 insert_spaces_tab (edit
, TRUE
);
1609 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1610 edit_backspace (edit
, 1);
1611 edit_insert (edit
, '\t');
1616 if (option_fill_tabs_with_spaces
)
1617 insert_spaces_tab (edit
, FALSE
);
1619 edit_insert (edit
, '\t');
1622 /* --------------------------------------------------------------------------------------------- */
1625 check_and_wrap_line (WEdit
* edit
)
1628 if (!option_typewriter_wrap
)
1630 edit_update_curs_col (edit
);
1631 if (edit
->curs_col
< option_word_wrap_line_length
)
1637 c
= edit_get_byte (edit
, curs
);
1638 if (c
== '\n' || curs
<= 0)
1640 edit_insert (edit
, '\n');
1643 if (c
== ' ' || c
== '\t')
1645 int current
= edit
->curs1
;
1646 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
1647 edit_insert (edit
, '\n');
1648 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
1654 /* --------------------------------------------------------------------------------------------- */
1655 /** this find the matching bracket in either direction, and sets edit->bracket */
1658 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
1660 const char *const b
= "{}{[][()(", *p
;
1661 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
1662 unsigned long j
= 0;
1664 edit_update_curs_row (edit
);
1665 c
= edit_get_byte (edit
, edit
->curs1
);
1668 if (!furthest_bracket_search
)
1669 furthest_bracket_search
--;
1670 /* not on a bracket at all */
1673 /* the matching bracket */
1675 /* going left or right? */
1676 if (strchr ("{[(", c
))
1678 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
1680 /* out of buffer? */
1681 if (q
>= edit
->last_byte
|| q
< 0)
1683 a
= edit_get_byte (edit
, q
);
1684 /* don't want to eat CPU */
1685 if (j
++ > furthest_bracket_search
)
1687 /* out of screen? */
1690 if (q
< edit
->start_display
)
1692 /* count lines if searching downward */
1693 if (inc
> 0 && a
== '\n')
1694 if (n
++ >= edit
->widget
.lines
- edit
->curs_row
) /* out of screen */
1697 /* count bracket depth */
1698 i
+= (a
== c
) - (a
== d
);
1699 /* return if bracket depth is zero */
1707 /* --------------------------------------------------------------------------------------------- */
1710 edit_goto_matching_bracket (WEdit
* edit
)
1714 q
= edit_get_bracket (edit
, 0, 0);
1717 edit
->bracket
= edit
->curs1
;
1718 edit
->force
|= REDRAW_PAGE
;
1719 edit_cursor_move (edit
, q
- edit
->curs1
);
1723 /* --------------------------------------------------------------------------------------------- */
1726 edit_move_block_to_right (WEdit
* edit
)
1728 long start_mark
, end_mark
;
1729 long cur_bol
, start_bol
;
1731 if (eval_marks (edit
, &start_mark
, &end_mark
))
1734 start_bol
= edit_bol (edit
, start_mark
);
1735 cur_bol
= edit_bol (edit
, end_mark
- 1);
1739 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1740 if (option_fill_tabs_with_spaces
)
1741 insert_spaces_tab (edit
, option_fake_half_tabs
);
1743 edit_insert (edit
, '\t');
1744 edit_cursor_move (edit
, edit_bol (edit
, cur_bol
) - edit
->curs1
);
1749 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1751 while (cur_bol
>= start_bol
);
1753 edit
->force
|= REDRAW_PAGE
;
1756 /* --------------------------------------------------------------------------------------------- */
1759 edit_move_block_to_left (WEdit
* edit
)
1761 long start_mark
, end_mark
;
1762 long cur_bol
, start_bol
;
1765 if (eval_marks (edit
, &start_mark
, &end_mark
))
1768 start_bol
= edit_bol (edit
, start_mark
);
1769 cur_bol
= edit_bol (edit
, end_mark
- 1);
1776 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1778 if (option_fake_half_tabs
)
1779 del_tab_width
= HALF_TAB_SIZE
;
1781 del_tab_width
= option_tab_spacing
;
1783 next_char
= edit_get_byte (edit
, edit
->curs1
);
1784 if (next_char
== '\t')
1785 edit_delete (edit
, 1);
1786 else if (next_char
== ' ')
1787 for (i
= 1; i
<= del_tab_width
; i
++)
1789 if (next_char
== ' ')
1790 edit_delete (edit
, 1);
1791 next_char
= edit_get_byte (edit
, edit
->curs1
);
1797 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1799 while (cur_bol
>= start_bol
);
1801 edit
->force
|= REDRAW_PAGE
;
1804 /* --------------------------------------------------------------------------------------------- */
1806 * prints at the cursor
1807 * @returns the number of chars printed
1811 edit_print_string (WEdit
* e
, const char *s
)
1815 while (s
[i
] != '\0')
1816 edit_execute_cmd (e
, CK_InsertChar
, (unsigned char) s
[i
++]);
1817 e
->force
|= REDRAW_COMPLETELY
;
1818 edit_update_screen (e
);
1822 /* --------------------------------------------------------------------------------------------- */
1823 /*** public functions ****************************************************************************/
1824 /* --------------------------------------------------------------------------------------------- */
1826 /** User edit menu, like user menu (F2) but only in editor. */
1829 user_menu (WEdit
* edit
, const char *menu_file
, int selected_entry
)
1834 long start_mark
, end_mark
;
1837 block_file
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
1839 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
1841 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1843 /* run shell scripts from menu */
1844 if (user_menu_cmd (edit
, menu_file
, selected_entry
)
1845 && (mc_stat (block_file
, &status
) == 0) && (status
.st_size
!= 0))
1850 /* i.e. we have marked block */
1852 rc
= edit_block_delete_cmd (edit
);
1858 ins_len
= edit_insert_file (edit
, block_file
);
1859 if (nomark
== 0 && ins_len
> 0)
1860 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1862 /* truncate block file */
1863 fd
= fopen (block_file
, "w");
1867 edit_cursor_move (edit
, curs
- edit
->curs1
);
1868 edit_refresh_cmd (edit
);
1869 edit
->force
|= REDRAW_COMPLETELY
;
1871 g_free (block_file
);
1874 /* --------------------------------------------------------------------------------------------- */
1877 edit_get_byte (WEdit
* edit
, long byte_index
)
1880 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1883 if (byte_index
>= edit
->curs1
)
1885 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1886 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
1890 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
1894 /* --------------------------------------------------------------------------------------------- */
1897 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1902 gchar
*next_ch
= NULL
;
1904 gchar utf8_buf
[UTF8_CHAR_LEN
+ 1];
1906 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1912 str
= edit_get_byte_ptr (edit
, byte_index
);
1920 res
= g_utf8_get_char_validated (str
, -1);
1924 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1926 for (i
= 0; i
< UTF8_CHAR_LEN
; i
++)
1927 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
);
1928 utf8_buf
[UTF8_CHAR_LEN
] = '\0';
1930 res
= g_utf8_get_char_validated (str
, -1);
1941 /* Calculate UTF-8 char width */
1942 next_ch
= g_utf8_next_char (str
);
1945 width
= next_ch
- str
;
1953 *char_width
= width
;
1957 /* --------------------------------------------------------------------------------------------- */
1960 edit_get_write_filter (const char *write_name
, const char *filename
)
1963 char *p
, *writename
;
1965 i
= edit_find_filter (filename
);
1969 writename
= name_quote (write_name
, 0);
1970 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
1975 /* --------------------------------------------------------------------------------------------- */
1978 edit_write_stream (WEdit
* edit
, FILE * f
)
1982 if (edit
->lb
== LB_ASIS
)
1984 for (i
= 0; i
< edit
->last_byte
; i
++)
1985 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
1990 /* change line breaks */
1991 for (i
= 0; i
< edit
->last_byte
; i
++)
1993 unsigned char c
= edit_get_byte (edit
, i
);
1995 if (!(c
== '\n' || c
== '\r'))
1997 /* not line break */
1998 if (fputc (c
, f
) < 0)
2002 { /* (c == '\n' || c == '\r') */
2003 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
2007 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
2008 /* put one line break unconditionally */
2009 if (fputc ('\n', f
) < 0)
2012 i
++; /* 2 chars are processed */
2014 if (c
== '\r' && c1
== '\n')
2015 /* Windows line break; go to the next char */
2018 if (c
== '\r' && c1
== '\r')
2020 /* two Macintosh line breaks; put second line break */
2021 if (fputc ('\n', f
) < 0)
2026 if (fputc (c1
, f
) < 0)
2030 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
2031 /* put one line break unconditionally */
2032 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
2035 if (c
== '\r' && c1
== '\n')
2036 /* Windows line break; go to the next char */
2040 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
2041 /* put one line break unconditionally */
2042 if (fputc ('\r', f
) < 0)
2045 i
++; /* 2 chars are processed */
2047 if (c
== '\r' && c1
== '\n')
2048 /* Windows line break; go to the next char */
2051 if (c
== '\n' && c1
== '\n')
2053 /* two Windows line breaks; put second line break */
2054 if (fputc ('\r', f
) < 0)
2059 if (fputc (c1
, f
) < 0)
2062 case LB_ASIS
: /* default without changes */
2068 return edit
->last_byte
;
2071 /* --------------------------------------------------------------------------------------------- */
2072 /** inserts a file at the cursor, returns count of inserted bytes on success */
2074 edit_insert_file (WEdit
* edit
, const char *filename
)
2079 p
= edit_get_filter (filename
);
2083 long current
= edit
->curs1
;
2084 f
= (FILE *) popen (p
, "r");
2087 edit_insert_stream (edit
, f
);
2088 ins_len
= edit
->curs1
- current
;
2089 edit_cursor_move (edit
, current
- edit
->curs1
);
2093 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
2094 edit_error_dialog (_("Error"), errmsg
);
2103 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
2104 edit_error_dialog (_("Error"), errmsg
);
2113 int i
, file
, blocklen
;
2114 long current
= edit
->curs1
;
2115 int vertical_insertion
= 0;
2117 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
2120 buf
= g_malloc0 (TEMP_BUF_LEN
);
2121 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2124 /* if contain signature VERTICAL_MAGIC then it vertical block */
2125 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2126 vertical_insertion
= 1;
2128 mc_lseek (file
, 0, SEEK_SET
);
2130 if (vertical_insertion
)
2134 blocklen
= edit_insert_column_of_text_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
2135 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2136 /* highlight inserted text then not persistent blocks */
2137 if (!option_persistent_selections
)
2139 if (!edit
->column_highlight
)
2140 edit_push_undo_action (edit
, COLUMN_OFF
);
2141 edit
->column_highlight
= 1;
2146 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2148 for (i
= 0; i
< blocklen
; i
++)
2149 edit_insert (edit
, buf
[i
]);
2151 /* highlight inserted text then not persistent blocks */
2152 if (!option_persistent_selections
&& edit
->modified
)
2154 edit_set_markers (edit
, edit
->curs1
, current
, 0, 0);
2155 if (edit
->column_highlight
)
2156 edit_push_undo_action (edit
, COLUMN_ON
);
2157 edit
->column_highlight
= 0;
2160 edit
->force
|= REDRAW_PAGE
;
2161 ins_len
= edit
->curs1
- current
;
2162 edit_cursor_move (edit
, current
- edit
->curs1
);
2171 /* --------------------------------------------------------------------------------------------- */
2173 * Fill in the edit structure. Return NULL on failure. Pass edit as
2174 * NULL to allocate a new structure.
2176 * If line is 0, try to restore saved position. Otherwise put the
2177 * cursor on that line and show it in the middle of the screen.
2181 edit_init (WEdit
* edit
, int y
, int x
, int lines
, int cols
, const char *filename
, long line
)
2183 gboolean to_free
= FALSE
;
2185 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2186 if (option_line_state
)
2187 option_line_state_width
= LINE_STATE_WIDTH
;
2189 option_line_state_width
= 0;
2195 * Expand option_whole_chars_search by national letters using
2199 static char option_whole_chars_search_buf
[256];
2201 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
2204 size_t len
= str_term_width1 (option_whole_chars_search
);
2206 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
2208 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
2210 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
2212 option_whole_chars_search_buf
[len
++] = i
;
2216 option_whole_chars_search_buf
[len
] = 0;
2217 option_whole_chars_search
= option_whole_chars_search_buf
;
2219 #endif /* ENABLE_NLS */
2220 edit
= g_malloc0 (sizeof (WEdit
));
2221 edit
->search
= NULL
;
2225 edit_purge_widget (edit
);
2228 edit
->widget
.lines
= lines
;
2229 edit
->widget
.cols
= cols
;
2231 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2232 edit
->stat1
.st_uid
= getuid ();
2233 edit
->stat1
.st_gid
= getgid ();
2234 edit
->stat1
.st_mtime
= 0;
2238 edit
->force
|= REDRAW_PAGE
;
2239 edit_set_filename (edit
, filename
);
2241 edit
->undo_stack_size
= START_STACK_SIZE
;
2242 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2243 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2245 edit
->redo_stack_size
= START_STACK_SIZE
;
2246 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2247 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2250 edit
->converter
= str_cnv_from_term
;
2251 edit_set_codeset (edit
);
2253 if (edit_load_file (edit
))
2255 /* edit_load_file already gives an error message */
2261 edit
->loading_done
= 1;
2264 edit_load_syntax (edit
, NULL
, NULL
);
2267 edit_get_syntax_color (edit
, -1, &color
);
2270 /* load saved cursor position */
2271 if ((line
== 0) && option_save_position
)
2272 edit_load_position (edit
);
2277 edit_move_display (edit
, line
- 1);
2278 edit_move_to_line (edit
, line
- 1);
2281 edit_load_macro_cmd (edit
);
2285 /* --------------------------------------------------------------------------------------------- */
2286 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2289 edit_clean (WEdit
* edit
)
2296 /* a stale lock, remove it */
2298 edit
->locked
= edit_unlock_file (edit
);
2300 /* save cursor position */
2301 if (option_save_position
)
2302 edit_save_position (edit
);
2303 else if (edit
->serialized_bookmarks
!= NULL
)
2304 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2306 /* File specified on the mcedit command line and never saved */
2307 if (edit
->delete_file
)
2308 unlink (edit
->filename
);
2310 edit_free_syntax_rules (edit
);
2311 book_mark_flush (edit
, -1);
2312 for (; j
<= MAXBUFF
; j
++)
2314 g_free (edit
->buffers1
[j
]);
2315 g_free (edit
->buffers2
[j
]);
2318 g_free (edit
->undo_stack
);
2319 g_free (edit
->redo_stack
);
2320 g_free (edit
->filename
);
2322 mc_search_free (edit
->search
);
2323 edit
->search
= NULL
;
2325 if (edit
->converter
!= str_cnv_from_term
)
2326 str_close_conv (edit
->converter
);
2328 edit_purge_widget (edit
);
2333 /* --------------------------------------------------------------------------------------------- */
2334 /** returns 1 on success */
2337 edit_renew (WEdit
* edit
)
2339 int y
= edit
->widget
.y
;
2340 int x
= edit
->widget
.x
;
2341 int lines
= edit
->widget
.lines
;
2342 int columns
= edit
->widget
.cols
;
2345 return (edit_init (edit
, y
, x
, lines
, columns
, "", 0) != NULL
);
2348 /* --------------------------------------------------------------------------------------------- */
2350 * Load a new file into the editor. If it fails, preserve the old file.
2351 * To do it, allocate a new widget, initialize it and, if the new file
2352 * was loaded, copy the data to the old widget.
2353 * Return 1 on success, 0 on failure.
2357 edit_reload (WEdit
* edit
, const char *filename
)
2360 int y
= edit
->widget
.y
;
2361 int x
= edit
->widget
.x
;
2362 int lines
= edit
->widget
.lines
;
2363 int columns
= edit
->widget
.cols
;
2365 e
= g_malloc0 (sizeof (WEdit
));
2366 e
->widget
= edit
->widget
;
2367 if (edit_init (e
, y
, x
, lines
, columns
, filename
, 0) == NULL
)
2373 memcpy (edit
, e
, sizeof (WEdit
));
2378 /* --------------------------------------------------------------------------------------------- */
2380 * Load a new file into the editor and set line. If it fails, preserve the old file.
2381 * To do it, allocate a new widget, initialize it and, if the new file
2382 * was loaded, copy the data to the old widget.
2383 * Return 1 on success, 0 on failure.
2387 edit_reload_line (WEdit
* edit
, const char *filename
, long line
)
2390 int y
= edit
->widget
.y
;
2391 int x
= edit
->widget
.x
;
2392 int lines
= edit
->widget
.lines
;
2393 int columns
= edit
->widget
.cols
;
2395 e
= g_malloc0 (sizeof (WEdit
));
2396 e
->widget
= edit
->widget
;
2397 if (edit_init (e
, y
, x
, lines
, columns
, filename
, line
) == NULL
)
2403 memcpy (edit
, e
, sizeof (WEdit
));
2408 /* --------------------------------------------------------------------------------------------- */
2411 edit_set_codeset (WEdit
* edit
)
2417 get_codepage_id (mc_global
.source_codepage
>=
2418 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2423 conv
= str_crt_conv_from (cp_id
);
2424 if (conv
!= INVALID_CONV
)
2426 if (edit
->converter
!= str_cnv_from_term
)
2427 str_close_conv (edit
->converter
);
2428 edit
->converter
= conv
;
2433 edit
->utf8
= str_isutf8 (cp_id
);
2440 /* --------------------------------------------------------------------------------------------- */
2442 Recording stack for undo:
2443 The following is an implementation of a compressed stack. Identical
2444 pushes are recorded by a negative prefix indicating the number of times the
2445 same char was pushed. This saves space for repeated curs-left or curs-right
2462 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2463 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2464 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2465 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2468 The only way the cursor moves or the buffer is changed is through the routines:
2469 insert, backspace, insert_ahead, delete, and cursor_move.
2470 These record the reverse undo movements onto the stack each time they are
2473 Each key press results in a set of actions (insert; delete ...). So each time
2474 a key is pressed the current position of start_display is pushed as
2475 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2476 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2477 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2482 edit_push_undo_action (WEdit
* edit
, long c
, ...)
2484 unsigned long sp
= edit
->undo_stack_pointer
;
2488 /* first enlarge the stack if necessary */
2489 if (sp
> edit
->undo_stack_size
- 10)
2491 if (option_max_undo
< 256)
2492 option_max_undo
= 256;
2493 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2495 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2498 edit
->undo_stack
= t
;
2499 edit
->undo_stack_size
<<= 1;
2500 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2504 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2505 if (edit
->undo_stack_disable
)
2507 edit_push_redo_action (edit
, KEY_PRESS
);
2508 edit_push_redo_action (edit
, c
);
2511 else if (edit
->redo_stack_reset
)
2513 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2516 if (edit
->undo_stack_bottom
!= sp
2517 && spm1
!= edit
->undo_stack_bottom
2518 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2521 if (edit
->undo_stack
[spm1
] < 0)
2523 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2526 if (edit
->undo_stack
[spm1
] > -1000000000)
2528 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2530 edit
->undo_stack
[spm1
]--;
2538 d
= edit
->undo_stack
[spm1
];
2542 return; /* --> no need to push multiple do-nothings */
2543 edit
->undo_stack
[sp
] = -2;
2548 edit
->undo_stack
[sp
] = c
;
2551 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2553 /* if the sp wraps round and catches the undo_stack_bottom then erase
2554 * the first set of actions on the stack to make space - by moving
2555 * undo_stack_bottom forward one "key press" */
2556 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2557 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2558 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2561 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2563 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2564 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2566 /*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: */
2567 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2568 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2570 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2575 edit_push_redo_action (WEdit
* edit
, long c
, ...)
2577 unsigned long sp
= edit
->redo_stack_pointer
;
2580 /* first enlarge the stack if necessary */
2581 if (sp
> edit
->redo_stack_size
- 10)
2583 if (option_max_undo
< 256)
2584 option_max_undo
= 256;
2585 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2587 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2590 edit
->redo_stack
= t
;
2591 edit
->redo_stack_size
<<= 1;
2592 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2596 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2598 if (edit
->redo_stack_bottom
!= sp
2599 && spm1
!= edit
->redo_stack_bottom
2600 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2603 if (edit
->redo_stack
[spm1
] < 0)
2605 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2608 if (edit
->redo_stack
[spm1
] > -1000000000)
2610 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2611 edit
->redo_stack
[spm1
]--;
2618 d
= edit
->redo_stack
[spm1
];
2622 return; /* --> no need to push multiple do-nothings */
2623 edit
->redo_stack
[sp
] = -2;
2624 goto redo_check_bottom
;
2628 edit
->redo_stack
[sp
] = c
;
2631 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2633 /* if the sp wraps round and catches the redo_stack_bottom then erase
2634 * the first set of actions on the stack to make space - by moving
2635 * redo_stack_bottom forward one "key press" */
2636 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2637 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2638 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2641 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2643 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2644 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2647 * If a single key produced enough pushes to wrap all the way round then
2648 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2649 * The stack is then initialised:
2652 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2653 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2654 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2658 /* --------------------------------------------------------------------------------------------- */
2660 Basic low level single character buffer alterations and movements at the cursor.
2661 Returns char passed over, inserted or removed.
2665 edit_insert (WEdit
* edit
, int c
)
2667 /* check if file has grown to large */
2668 if (edit
->last_byte
>= SIZE_LIMIT
)
2671 /* first we must update the position of the display window */
2672 if (edit
->curs1
< edit
->start_display
)
2674 edit
->start_display
++;
2679 /* Mark file as modified, unless the file hasn't been fully loaded */
2680 if (edit
->loading_done
)
2682 edit_modification (edit
);
2685 /* now we must update some info on the file and check if a redraw is required */
2688 if (edit
->book_mark
)
2689 book_mark_inc (edit
, edit
->curs_line
);
2691 edit
->total_lines
++;
2692 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2695 /* save the reverse command onto the undo stack */
2696 /* ordinary char and not space */
2698 edit_push_undo_action (edit
, BACKSPACE
);
2700 edit_push_undo_action (edit
, BACKSPACE_BR
);
2701 /* update markers */
2702 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2703 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2704 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2706 /* add a new buffer if we've reached the end of the last one */
2707 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2708 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2710 /* perform the insertion */
2711 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2712 = (unsigned char) c
;
2714 /* update file length */
2717 /* update cursor position */
2721 /* --------------------------------------------------------------------------------------------- */
2722 /** same as edit_insert and move left */
2725 edit_insert_ahead (WEdit
* edit
, int c
)
2727 if (edit
->last_byte
>= SIZE_LIMIT
)
2730 if (edit
->curs1
< edit
->start_display
)
2732 edit
->start_display
++;
2736 edit_modification (edit
);
2739 if (edit
->book_mark
)
2740 book_mark_inc (edit
, edit
->curs_line
);
2741 edit
->total_lines
++;
2742 edit
->force
|= REDRAW_AFTER_CURSOR
;
2744 /* ordinary char and not space */
2746 edit_push_undo_action (edit
, DELCHAR
);
2748 edit_push_undo_action (edit
, DELCHAR_BR
);
2750 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2751 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2752 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2754 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2755 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2756 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2757 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2764 /* --------------------------------------------------------------------------------------------- */
2767 edit_delete (WEdit
* edit
, const int byte_delete
)
2777 /* if byte_delete = 1 then delete only one byte not multibyte char */
2778 if (edit
->utf8
&& byte_delete
== 0)
2780 edit_get_utf (edit
, edit
->curs1
, &cw
);
2785 if (edit
->mark2
!= edit
->mark1
)
2786 edit_push_markers (edit
);
2788 for (i
= 1; i
<= cw
; i
++)
2790 if (edit
->mark1
> edit
->curs1
)
2793 edit
->end_mark_curs
--;
2795 if (edit
->mark2
> edit
->curs1
)
2797 if (edit
->last_get_rule
> edit
->curs1
)
2798 edit
->last_get_rule
--;
2800 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2802 1) & M_EDIT_BUF_SIZE
) - 1];
2804 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2806 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2807 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2811 edit_push_undo_action (edit
, p
+ 256);
2814 edit_modification (edit
);
2817 if (edit
->book_mark
)
2818 book_mark_dec (edit
, edit
->curs_line
);
2819 edit
->total_lines
--;
2820 edit
->force
|= REDRAW_AFTER_CURSOR
;
2822 if (edit
->curs1
< edit
->start_display
)
2824 edit
->start_display
--;
2832 /* --------------------------------------------------------------------------------------------- */
2833 /** moves the cursor right or left: increment positive or negative respectively */
2836 edit_cursor_move (WEdit
* edit
, long increment
)
2838 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2843 for (; increment
< 0; increment
++)
2848 edit_push_undo_action (edit
, CURS_RIGHT
);
2850 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2851 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2852 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2853 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2854 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2856 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2857 1) & M_EDIT_BUF_SIZE
];
2858 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2860 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2861 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2867 edit
->force
|= REDRAW_LINE_BELOW
;
2872 else if (increment
> 0)
2874 for (; increment
> 0; increment
--)
2879 edit_push_undo_action (edit
, CURS_LEFT
);
2881 c
= edit_get_byte (edit
, edit
->curs1
);
2882 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2883 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2884 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2886 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2888 1) & M_EDIT_BUF_SIZE
) - 1];
2889 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2891 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2892 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2898 edit
->force
|= REDRAW_LINE_ABOVE
;
2904 /* These functions return positions relative to lines */
2906 /* --------------------------------------------------------------------------------------------- */
2907 /** returns index of last char on line + 1 */
2910 edit_eol (WEdit
* edit
, long current
)
2912 if (current
>= edit
->last_byte
)
2913 return edit
->last_byte
;
2916 if (edit_get_byte (edit
, current
) == '\n')
2921 /* --------------------------------------------------------------------------------------------- */
2922 /** returns index of first char on line */
2925 edit_bol (WEdit
* edit
, long current
)
2931 if (edit_get_byte (edit
, current
- 1) == '\n')
2936 /* --------------------------------------------------------------------------------------------- */
2939 edit_count_lines (WEdit
* edit
, long current
, long upto
)
2942 if (upto
> edit
->last_byte
)
2943 upto
= edit
->last_byte
;
2946 while (current
< upto
)
2947 if (edit_get_byte (edit
, current
++) == '\n')
2952 /* --------------------------------------------------------------------------------------------- */
2953 /* If lines is zero this returns the count of lines from current to upto. */
2954 /* If upto is zero returns index of lines forward current. */
2957 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
2961 return edit_count_lines (edit
, current
, upto
);
2970 next
= edit_eol (edit
, current
) + 1;
2971 if (next
> edit
->last_byte
)
2980 /* --------------------------------------------------------------------------------------------- */
2981 /** Returns offset of 'lines' lines up from current */
2984 edit_move_backward (WEdit
* edit
, long current
, long lines
)
2988 current
= edit_bol (edit
, current
);
2989 while ((lines
--) && current
!= 0)
2990 current
= edit_bol (edit
, current
- 1);
2994 /* --------------------------------------------------------------------------------------------- */
2995 /* If cols is zero this returns the count of columns from current to upto. */
2996 /* If upto is zero returns index of cols across from current. */
2999 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
3010 q
= edit
->last_byte
+ 2;
3012 for (col
= 0, p
= current
; p
< q
; p
++)
3024 orig_c
= c
= edit_get_byte (edit
, p
);
3032 utf_ch
= edit_get_utf (edit
, p
, &cw
);
3033 if (mc_global
.utf8_display
)
3037 if (g_unichar_iswide (utf_ch
))
3040 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
3044 c
= convert_to_display_c (c
);
3048 col
+= TAB_SIZE
- col
% TAB_SIZE
;
3056 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!mc_global
.utf8_display
&& !edit
->utf8
)))
3057 /* '\r' is shown as ^M, so we must advance 2 characters */
3058 /* Caret notation for control characters */
3066 /* --------------------------------------------------------------------------------------------- */
3067 /** returns the current column position of the cursor */
3070 edit_get_col (WEdit
* edit
)
3072 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3075 /* --------------------------------------------------------------------------------------------- */
3076 /* Scrolling functions */
3077 /* --------------------------------------------------------------------------------------------- */
3080 edit_update_curs_row (WEdit
* edit
)
3082 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
3085 /* --------------------------------------------------------------------------------------------- */
3088 edit_update_curs_col (WEdit
* edit
)
3090 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3093 /* --------------------------------------------------------------------------------------------- */
3096 edit_get_curs_col (const WEdit
* edit
)
3098 return edit
->curs_col
;
3101 /* --------------------------------------------------------------------------------------------- */
3102 /** moves the display start position up by i lines */
3105 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
3107 unsigned long lines_above
= edit
->start_line
;
3108 if (i
> lines_above
)
3112 edit
->start_line
-= i
;
3113 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
3114 edit
->force
|= REDRAW_PAGE
;
3115 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3117 edit_update_curs_row (edit
);
3121 /* --------------------------------------------------------------------------------------------- */
3122 /** returns 1 if could scroll, 0 otherwise */
3125 edit_scroll_downward (WEdit
* edit
, int i
)
3128 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->widget
.lines
- 1);
3129 if (lines_below
> 0)
3131 if (i
> lines_below
)
3133 edit
->start_line
+= i
;
3134 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
3135 edit
->force
|= REDRAW_PAGE
;
3136 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3138 edit_update_curs_row (edit
);
3141 /* --------------------------------------------------------------------------------------------- */
3144 edit_scroll_right (WEdit
* edit
, int i
)
3146 edit
->force
|= REDRAW_PAGE
;
3147 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3148 edit
->start_col
-= i
;
3151 /* --------------------------------------------------------------------------------------------- */
3154 edit_scroll_left (WEdit
* edit
, int i
)
3156 if (edit
->start_col
)
3158 edit
->start_col
+= i
;
3159 if (edit
->start_col
> 0)
3160 edit
->start_col
= 0;
3161 edit
->force
|= REDRAW_PAGE
;
3162 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3166 /* --------------------------------------------------------------------------------------------- */
3167 /* high level cursor movement commands */
3168 /* --------------------------------------------------------------------------------------------- */
3171 edit_move_to_prev_col (WEdit
* edit
, long p
)
3173 int prev
= edit
->prev_col
;
3174 int over
= edit
->over_col
;
3175 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
3177 if (option_cursor_beyond_eol
)
3179 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
3180 edit_eol (edit
, edit
->curs1
));
3182 if (line_len
< prev
+ edit
->over_col
)
3184 edit
->over_col
= prev
+ over
- line_len
;
3185 edit
->prev_col
= line_len
;
3186 edit
->curs_col
= line_len
;
3190 edit
->curs_col
= prev
+ over
;
3191 edit
->prev_col
= edit
->curs_col
;
3198 if (is_in_indent (edit
) && option_fake_half_tabs
)
3200 edit_update_curs_col (edit
);
3202 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
3204 int q
= edit
->curs_col
;
3205 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
3206 p
= edit_bol (edit
, edit
->curs1
);
3207 edit_cursor_move (edit
,
3208 edit_move_forward3 (edit
, p
, edit
->curs_col
,
3210 if (!left_of_four_spaces (edit
))
3211 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3217 /* --------------------------------------------------------------------------------------------- */
3220 line_is_blank (WEdit
* edit
, long line
)
3222 return is_blank (edit
, edit_find_line (edit
, line
));
3225 /* --------------------------------------------------------------------------------------------- */
3226 /** move cursor to line 'line' */
3229 edit_move_to_line (WEdit
* e
, long line
)
3231 if (line
< e
->curs_line
)
3232 edit_move_up (e
, e
->curs_line
- line
, 0);
3234 edit_move_down (e
, line
- e
->curs_line
, 0);
3235 edit_scroll_screen_over_cursor (e
);
3238 /* --------------------------------------------------------------------------------------------- */
3239 /** scroll window so that first visible line is 'line' */
3242 edit_move_display (WEdit
* e
, long line
)
3244 if (line
< e
->start_line
)
3245 edit_scroll_upward (e
, e
->start_line
- line
);
3247 edit_scroll_downward (e
, line
- e
->start_line
);
3250 /* --------------------------------------------------------------------------------------------- */
3251 /** save markers onto undo stack */
3254 edit_push_markers (WEdit
* edit
)
3256 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3257 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3258 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3261 /* --------------------------------------------------------------------------------------------- */
3264 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
3273 /* --------------------------------------------------------------------------------------------- */
3274 /** highlight marker toggle */
3277 edit_mark_cmd (WEdit
* edit
, int unmark
)
3279 edit_push_markers (edit
);
3282 edit_set_markers (edit
, 0, 0, 0, 0);
3283 edit
->force
|= REDRAW_PAGE
;
3287 if (edit
->mark2
>= 0)
3289 edit
->end_mark_curs
= -1;
3290 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3291 edit
->curs_col
+ edit
->over_col
);
3292 edit
->force
|= REDRAW_PAGE
;
3296 edit
->end_mark_curs
= edit
->curs1
;
3297 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3298 edit
->curs_col
+ edit
->over_col
);
3303 /* --------------------------------------------------------------------------------------------- */
3304 /** highlight the word under cursor */
3307 edit_mark_current_word_cmd (WEdit
* edit
)
3311 for (pos
= edit
->curs1
; pos
!= 0; pos
--)
3315 c1
= edit_get_byte (edit
, pos
);
3316 c2
= edit_get_byte (edit
, pos
- 1);
3317 if (!isspace (c1
) && isspace (c2
))
3319 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3324 for (; pos
< edit
->last_byte
; pos
++)
3328 c1
= edit_get_byte (edit
, pos
);
3329 c2
= edit_get_byte (edit
, pos
+ 1);
3330 if (!isspace (c1
) && isspace (c2
))
3332 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3335 edit
->mark2
= min (pos
+ 1, edit
->last_byte
);
3337 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3340 /* --------------------------------------------------------------------------------------------- */
3343 edit_mark_current_line_cmd (WEdit
* edit
)
3345 long pos
= edit
->curs1
;
3347 edit
->mark1
= edit_bol (edit
, pos
);
3348 edit
->mark2
= edit_eol (edit
, pos
);
3350 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3353 /* --------------------------------------------------------------------------------------------- */
3356 edit_delete_line (WEdit
* edit
)
3359 * Delete right part of the line.
3360 * Note that edit_get_byte() returns '\n' when byte position is
3363 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3365 (void) edit_delete (edit
, 1);
3370 * Note that edit_delete() will not corrupt anything if called while
3371 * cursor position is EOF.
3373 (void) edit_delete (edit
, 1);
3376 * Delete left part of the line.
3377 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3379 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3381 (void) edit_backspace (edit
, 1);
3385 /* --------------------------------------------------------------------------------------------- */
3388 edit_indent_width (WEdit
* edit
, long p
)
3391 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
3393 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
3396 /* --------------------------------------------------------------------------------------------- */
3399 edit_insert_indent (WEdit
* edit
, int indent
)
3401 if (!option_fill_tabs_with_spaces
)
3403 while (indent
>= TAB_SIZE
)
3405 edit_insert (edit
, '\t');
3409 while (indent
-- > 0)
3410 edit_insert (edit
, ' ');
3413 /* --------------------------------------------------------------------------------------------- */
3416 edit_push_key_press (WEdit
* edit
)
3418 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3419 if (edit
->mark2
== -1)
3421 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3422 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3426 /* --------------------------------------------------------------------------------------------- */
3429 edit_find_bracket (WEdit
* edit
)
3431 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3432 if (last_bracket
!= edit
->bracket
)
3433 edit
->force
|= REDRAW_PAGE
;
3434 last_bracket
= edit
->bracket
;
3437 /* --------------------------------------------------------------------------------------------- */
3439 * This executes a command as though the user initiated it through a key
3440 * press. Callback with WIDGET_KEY as a message calls this after
3441 * translating the key press. This function can be used to pass any
3442 * command to the editor. Note that the screen wouldn't update
3443 * automatically. Either of command or char_for_insertion must be
3444 * passed as -1. Commands are executed, and char_for_insertion is
3445 * inserted at the cursor.
3449 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3451 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3453 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3456 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3459 if (macro_index
!= -1)
3461 edit
->force
|= REDRAW_COMPLETELY
;
3462 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3464 edit_store_macro_cmd (edit
);
3468 else if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3470 edit_repeat_macro_cmd (edit
);
3476 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3478 record_macro_buf
[macro_index
].action
= command
;
3479 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3481 /* record the beginning of a set of editing actions initiated by a key press */
3482 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3483 edit_push_key_press (edit
);
3485 edit_execute_cmd (edit
, command
, char_for_insertion
);
3486 if (edit
->column_highlight
)
3487 edit
->force
|= REDRAW_PAGE
;
3490 /* --------------------------------------------------------------------------------------------- */
3492 This executes a command at a lower level than macro recording.
3493 It also does not push a key_press onto the undo stack. This means
3494 that if it is called many times, a single undo command will undo
3495 all of them. It also does not check for the Undo command.
3498 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3500 edit
->force
|= REDRAW_LINE
;
3502 /* The next key press will unhighlight the found string, so update
3504 if (edit
->found_len
|| edit
->column_highlight
)
3505 edit
->force
|= REDRAW_PAGE
;
3509 /* a mark command with shift-arrow */
3512 case CK_MarkToWordBegin
:
3513 case CK_MarkToWordEnd
:
3519 case CK_MarkPageDown
:
3520 case CK_MarkToFileBegin
:
3521 case CK_MarkToFileEnd
:
3522 case CK_MarkToPageBegin
:
3523 case CK_MarkToPageEnd
:
3524 case CK_MarkScrollUp
:
3525 case CK_MarkScrollDown
:
3526 case CK_MarkParagraphUp
:
3527 case CK_MarkParagraphDown
:
3528 /* a mark command with alt-arrow */
3529 case CK_MarkColumnPageUp
:
3530 case CK_MarkColumnPageDown
:
3531 case CK_MarkColumnLeft
:
3532 case CK_MarkColumnRight
:
3533 case CK_MarkColumnUp
:
3534 case CK_MarkColumnDown
:
3535 case CK_MarkColumnScrollUp
:
3536 case CK_MarkColumnScrollDown
:
3537 case CK_MarkColumnParagraphUp
:
3538 case CK_MarkColumnParagraphDown
:
3539 edit
->column_highlight
= 0;
3540 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3542 edit_mark_cmd (edit
, 1); /* clear */
3543 edit_mark_cmd (edit
, 0); /* marking on */
3545 edit
->highlight
= 1;
3548 /* any other command */
3550 if (edit
->highlight
)
3551 edit_mark_cmd (edit
, 0); /* clear */
3552 edit
->highlight
= 0;
3555 /* first check for undo */
3556 if (command
== CK_Undo
)
3558 edit
->redo_stack_reset
= 0;
3559 edit_group_undo (edit
);
3560 edit
->found_len
= 0;
3561 edit
->prev_col
= edit_get_col (edit
);
3562 edit
->search_start
= edit
->curs1
;
3565 /* check for redo */
3566 if (command
== CK_Redo
)
3568 edit
->redo_stack_reset
= 0;
3569 edit_do_redo (edit
);
3570 edit
->found_len
= 0;
3571 edit
->prev_col
= edit_get_col (edit
);
3572 edit
->search_start
= edit
->curs1
;
3576 edit
->redo_stack_reset
= 1;
3578 /* An ordinary key press */
3579 if (char_for_insertion
>= 0)
3581 /* if non persistent selection and text selected */
3582 if (!option_persistent_selections
)
3584 if (edit
->mark1
!= edit
->mark2
)
3585 edit_block_delete_cmd (edit
);
3587 if (edit
->overwrite
)
3589 /* remove char only one time, after input first byte, multibyte chars */
3590 if ((!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3591 && edit_get_byte (edit
, edit
->curs1
) != '\n')
3592 edit_delete (edit
, 0);
3594 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3595 edit_insert_over (edit
);
3597 if (char_for_insertion
> 255 && mc_global
.utf8_display
== 0)
3599 unsigned char str
[6 + 1];
3601 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3611 while (str
[i
] != 0 && i
<= 6)
3613 char_for_insertion
= str
[i
];
3614 edit_insert (edit
, char_for_insertion
);
3620 edit_insert (edit
, char_for_insertion
);
3622 if (option_auto_para_formatting
)
3624 format_paragraph (edit
, 0);
3625 edit
->force
|= REDRAW_PAGE
;
3628 check_and_wrap_line (edit
);
3629 edit
->found_len
= 0;
3630 edit
->prev_col
= edit_get_col (edit
);
3631 edit
->search_start
= edit
->curs1
;
3632 edit_find_bracket (edit
);
3638 case CK_TopOnScreen
:
3639 case CK_BottomOnScreen
:
3652 if (edit
->mark2
>= 0)
3654 if (!option_persistent_selections
)
3656 if (edit
->column_highlight
)
3657 edit_push_undo_action (edit
, COLUMN_ON
);
3658 edit
->column_highlight
= 0;
3659 edit_mark_cmd (edit
, 1);
3666 case CK_TopOnScreen
:
3667 case CK_BottomOnScreen
:
3668 case CK_MarkToPageBegin
:
3669 case CK_MarkToPageEnd
:
3674 case CK_MarkToWordBegin
:
3675 case CK_MarkToWordEnd
:
3678 case CK_MarkColumnUp
:
3679 case CK_MarkColumnDown
:
3680 if (edit
->mark2
== -1)
3681 break; /*marking is following the cursor: may need to highlight a whole line */
3686 edit
->force
|= REDRAW_CHAR_ONLY
;
3689 /* basic cursor key commands */
3693 /* if non persistent selection and text selected */
3694 if (!option_persistent_selections
)
3696 if (edit
->mark1
!= edit
->mark2
)
3698 edit_block_delete_cmd (edit
);
3702 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3707 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3709 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3710 edit_backspace (edit
, 1);
3715 if (option_fake_half_tabs
)
3718 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3720 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3721 edit_backspace (edit
, 1);
3726 edit_backspace (edit
, 0);
3729 /* if non persistent selection and text selected */
3730 if (!option_persistent_selections
)
3732 if (edit
->mark1
!= edit
->mark2
)
3734 edit_block_delete_cmd (edit
);
3739 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3740 edit_insert_over (edit
);
3742 if (option_fake_half_tabs
)
3745 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3747 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3748 edit_delete (edit
, 1);
3752 edit_delete (edit
, 0);
3754 case CK_DeleteToWordBegin
:
3756 edit_left_delete_word (edit
);
3758 case CK_DeleteToWordEnd
:
3759 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3760 edit_insert_over (edit
);
3762 edit_right_delete_word (edit
);
3765 edit_delete_line (edit
);
3767 case CK_DeleteToHome
:
3768 edit_delete_to_line_begin (edit
);
3770 case CK_DeleteToEnd
:
3771 edit_delete_to_line_end (edit
);
3775 if (option_auto_para_formatting
)
3777 edit_double_newline (edit
);
3778 if (option_return_does_auto_indent
)
3779 edit_auto_indent (edit
);
3780 format_paragraph (edit
, 0);
3784 edit_insert (edit
, '\n');
3785 if (option_return_does_auto_indent
)
3787 edit_auto_indent (edit
);
3792 edit_insert (edit
, '\n');
3795 case CK_MarkColumnPageUp
:
3796 edit
->column_highlight
= 1;
3799 edit_move_up (edit
, edit
->widget
.lines
- 1, 1);
3801 case CK_MarkColumnPageDown
:
3802 edit
->column_highlight
= 1;
3804 case CK_MarkPageDown
:
3805 edit_move_down (edit
, edit
->widget
.lines
- 1, 1);
3807 case CK_MarkColumnLeft
:
3808 edit
->column_highlight
= 1;
3811 if (option_fake_half_tabs
)
3813 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3815 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3818 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3819 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3823 edit_left_char_move_cmd (edit
);
3825 case CK_MarkColumnRight
:
3826 edit
->column_highlight
= 1;
3829 if (option_fake_half_tabs
)
3831 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3833 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3834 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3838 edit_right_char_move_cmd (edit
);
3840 case CK_TopOnScreen
:
3841 case CK_MarkToPageBegin
:
3842 edit_begin_page (edit
);
3844 case CK_BottomOnScreen
:
3845 case CK_MarkToPageEnd
:
3846 edit_end_page (edit
);
3849 case CK_MarkToWordBegin
:
3851 edit_left_word_move_cmd (edit
);
3854 case CK_MarkToWordEnd
:
3856 edit_right_word_move_cmd (edit
);
3858 case CK_MarkColumnUp
:
3859 edit
->column_highlight
= 1;
3862 edit_move_up (edit
, 1, 0);
3864 case CK_MarkColumnDown
:
3865 edit
->column_highlight
= 1;
3868 edit_move_down (edit
, 1, 0);
3870 case CK_MarkColumnParagraphUp
:
3871 edit
->column_highlight
= 1;
3872 case CK_ParagraphUp
:
3873 case CK_MarkParagraphUp
:
3874 edit_move_up_paragraph (edit
, 0);
3876 case CK_MarkColumnParagraphDown
:
3877 edit
->column_highlight
= 1;
3878 case CK_ParagraphDown
:
3879 case CK_MarkParagraphDown
:
3880 edit_move_down_paragraph (edit
, 0);
3882 case CK_MarkColumnScrollUp
:
3883 edit
->column_highlight
= 1;
3885 case CK_MarkScrollUp
:
3886 edit_move_up (edit
, 1, 1);
3888 case CK_MarkColumnScrollDown
:
3889 edit
->column_highlight
= 1;
3891 case CK_MarkScrollDown
:
3892 edit_move_down (edit
, 1, 1);
3896 edit_cursor_to_bol (edit
);
3900 edit_cursor_to_eol (edit
);
3903 /* if text marked shift block */
3904 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3906 if (edit
->mark2
< 0)
3907 edit_mark_cmd (edit
, 0);
3908 edit_move_block_to_right (edit
);
3912 if (option_cursor_beyond_eol
)
3913 edit_insert_over (edit
);
3914 edit_tab_cmd (edit
);
3915 if (option_auto_para_formatting
)
3917 format_paragraph (edit
, 0);
3918 edit
->force
|= REDRAW_PAGE
;
3922 check_and_wrap_line (edit
);
3927 case CK_InsertOverwrite
:
3928 edit
->overwrite
= !edit
->overwrite
;
3932 if (edit
->mark2
>= 0)
3934 if (edit
->column_highlight
)
3935 edit_push_undo_action (edit
, COLUMN_ON
);
3936 edit
->column_highlight
= 0;
3938 edit_mark_cmd (edit
, 0);
3941 if (!edit
->column_highlight
)
3942 edit_push_undo_action (edit
, COLUMN_OFF
);
3943 edit
->column_highlight
= 1;
3944 edit_mark_cmd (edit
, 0);
3947 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3948 edit
->force
|= REDRAW_PAGE
;
3951 if (edit
->column_highlight
)
3952 edit_push_undo_action (edit
, COLUMN_ON
);
3953 edit
->column_highlight
= 0;
3954 edit_mark_cmd (edit
, 1);
3957 if (edit
->column_highlight
)
3958 edit_push_undo_action (edit
, COLUMN_ON
);
3959 edit
->column_highlight
= 0;
3960 edit_mark_current_word_cmd (edit
);
3963 if (edit
->column_highlight
)
3964 edit_push_undo_action (edit
, COLUMN_ON
);
3965 edit
->column_highlight
= 0;
3966 edit_mark_current_line_cmd (edit
);
3969 case CK_ShowNumbers
:
3970 option_line_state
= !option_line_state
;
3971 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
3972 edit
->force
|= REDRAW_PAGE
;
3976 show_right_margin
= !show_right_margin
;
3977 edit
->force
|= REDRAW_PAGE
;
3981 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
3982 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
3983 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3985 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3987 case CK_BookmarkFlush
:
3988 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3989 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3990 edit
->force
|= REDRAW_PAGE
;
3992 case CK_BookmarkNext
:
3993 if (edit
->book_mark
)
3995 struct _book_mark
*p
;
3996 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4000 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4001 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4002 edit_move_to_line (edit
, p
->line
);
4006 case CK_BookmarkPrev
:
4007 if (edit
->book_mark
)
4009 struct _book_mark
*p
;
4010 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4011 while (p
->line
== edit
->curs_line
)
4016 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4017 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4018 edit_move_to_line (edit
, p
->line
);
4024 case CK_MarkToFileBegin
:
4025 edit_move_to_top (edit
);
4028 case CK_MarkToFileEnd
:
4029 edit_move_to_bottom (edit
);
4033 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4034 edit_insert_over (edit
);
4035 edit_block_copy_cmd (edit
);
4038 edit_block_delete_cmd (edit
);
4041 edit_block_move_cmd (edit
);
4044 case CK_BlockShiftLeft
:
4045 if (edit
->mark1
!= edit
->mark2
)
4046 edit_move_block_to_left (edit
);
4048 case CK_BlockShiftRight
:
4049 if (edit
->mark1
!= edit
->mark2
)
4050 edit_move_block_to_right (edit
);
4053 edit_copy_to_X_buf_cmd (edit
);
4056 edit_cut_to_X_buf_cmd (edit
);
4059 /* if non persistent selection and text selected */
4060 if (!option_persistent_selections
)
4062 if (edit
->mark1
!= edit
->mark2
)
4063 edit_block_delete_cmd (edit
);
4065 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4066 edit_insert_over (edit
);
4067 edit_paste_from_X_buf_cmd (edit
);
4070 edit_paste_from_history (edit
);
4074 edit_save_as_cmd (edit
);
4077 edit_save_confirm_cmd (edit
);
4080 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
4083 edit_save_block_cmd (edit
);
4086 edit_insert_file_cmd (edit
);
4090 edit_load_back_cmd (edit
);
4093 edit_load_forward_cmd (edit
);
4096 case CK_EditSyntaxFile
:
4097 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
4099 case CK_SyntaxChoose
:
4100 edit_syntax_dialog (edit
);
4103 case CK_EditUserMenu
:
4104 edit_load_cmd (edit
, EDIT_FILE_MENU
);
4107 case CK_SyntaxOnOff
:
4108 option_syntax_highlighting
^= 1;
4109 if (option_syntax_highlighting
== 1)
4110 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
4111 edit
->force
|= REDRAW_PAGE
;
4115 enable_show_tabs_tws
^= 1;
4116 edit
->force
|= REDRAW_PAGE
;
4120 edit_search_cmd (edit
, FALSE
);
4122 case CK_SearchContinue
:
4123 edit_search_cmd (edit
, TRUE
);
4126 edit_replace_cmd (edit
, 0);
4128 case CK_ReplaceContinue
:
4129 edit_replace_cmd (edit
, 1);
4132 /* if text marked shift block */
4133 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
4135 edit_move_block_to_left (edit
);
4139 edit_complete_word_cmd (edit
);
4143 edit_get_match_keyword_cmd (edit
);
4146 dlg_stop (edit
->widget
.owner
);
4149 edit_new_cmd (edit
);
4152 edit_help_cmd (edit
);
4155 edit_refresh_cmd (edit
);
4167 edit_options_dialog (edit
);
4169 case CK_OptionsSaveMode
:
4170 menu_save_mode_cmd ();
4175 /* fool gcc to prevent a Y2K warning */
4176 char time_format
[] = "_c";
4177 time_format
[0] = '%';
4179 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4180 edit_print_string (edit
, s
);
4181 edit
->force
|= REDRAW_PAGE
;
4186 edit_goto_cmd (edit
);
4188 case CK_ParagraphFormat
:
4189 format_paragraph (edit
, 1);
4190 edit
->force
|= REDRAW_PAGE
;
4192 case CK_MacroDelete
:
4193 edit_delete_macro_cmd (edit
);
4195 case CK_MatchBracket
:
4196 edit_goto_matching_bracket (edit
);
4199 user_menu (edit
, NULL
, -1);
4202 edit_sort_cmd (edit
);
4204 case CK_ExternalCommand
:
4205 edit_ext_cmd (edit
);
4208 edit_mail_dialog (edit
);
4214 case CK_SelectCodepage
:
4215 edit_select_codepage_cmd (edit
);
4218 case CK_InsertLiteral
:
4219 edit_insert_literal_cmd (edit
);
4221 case CK_MacroStartStopRecord
:
4222 edit_begin_end_macro_cmd (edit
);
4224 case CK_RepeatStartStopRecord
:
4225 edit_begin_end_repeat_cmd (edit
);
4227 case CK_ExtendedKeyMap
:
4228 edit
->extmod
= TRUE
;
4235 if ((command
/ CK_PipeBlock (0)) == 1)
4236 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
4238 /* keys which must set the col position, and the search vars */
4242 case CK_SearchContinue
:
4244 case CK_ReplaceContinue
:
4246 edit
->prev_col
= edit_get_col (edit
);
4250 case CK_MarkColumnUp
:
4253 case CK_MarkColumnDown
:
4256 case CK_MarkColumnPageUp
:
4258 case CK_MarkPageDown
:
4259 case CK_MarkColumnPageDown
:
4261 case CK_MarkToFileBegin
:
4263 case CK_MarkToFileEnd
:
4264 case CK_ParagraphUp
:
4265 case CK_MarkParagraphUp
:
4266 case CK_MarkColumnParagraphUp
:
4267 case CK_ParagraphDown
:
4268 case CK_MarkParagraphDown
:
4269 case CK_MarkColumnParagraphDown
:
4271 case CK_MarkScrollUp
:
4272 case CK_MarkColumnScrollUp
:
4274 case CK_MarkScrollDown
:
4275 case CK_MarkColumnScrollDown
:
4276 edit
->search_start
= edit
->curs1
;
4277 edit
->found_len
= 0;
4280 edit
->found_len
= 0;
4281 edit
->prev_col
= edit_get_col (edit
);
4282 edit
->search_start
= edit
->curs1
;
4284 edit_find_bracket (edit
);
4286 if (option_auto_para_formatting
)
4292 case CK_DeleteToWordBegin
:
4293 case CK_DeleteToWordEnd
:
4294 case CK_DeleteToHome
:
4295 case CK_DeleteToEnd
:
4296 format_paragraph (edit
, 0);
4297 edit
->force
|= REDRAW_PAGE
;
4302 /* --------------------------------------------------------------------------------------------- */
4305 edit_stack_init (void)
4307 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4309 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
4310 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4313 edit_stack_iterator
= 0;
4316 /* --------------------------------------------------------------------------------------------- */
4319 edit_stack_free (void)
4321 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4322 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
4325 /* --------------------------------------------------------------------------------------------- */
4329 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
4331 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4334 /* --------------------------------------------------------------------------------------------- */
4338 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
4340 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4343 /* --------------------------------------------------------------------------------------------- */
4346 edit_unlock_file (WEdit
* edit
)
4351 fullpath
= mc_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4352 ret
= unlock_file (fullpath
);
4358 /* --------------------------------------------------------------------------------------------- */
4361 edit_lock_file (WEdit
* edit
)
4366 fullpath
= mc_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4367 ret
= lock_file (fullpath
);
4373 /* --------------------------------------------------------------------------------------------- */