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
;
2324 edit_highlight_deinit ();
2326 if (edit
->converter
!= str_cnv_from_term
)
2327 str_close_conv (edit
->converter
);
2329 edit_purge_widget (edit
);
2334 /* --------------------------------------------------------------------------------------------- */
2335 /** returns 1 on success */
2338 edit_renew (WEdit
* edit
)
2340 int y
= edit
->widget
.y
;
2341 int x
= edit
->widget
.x
;
2342 int lines
= edit
->widget
.lines
;
2343 int columns
= edit
->widget
.cols
;
2346 return (edit_init (edit
, y
, x
, lines
, columns
, "", 0) != NULL
);
2349 /* --------------------------------------------------------------------------------------------- */
2351 * Load a new file into the editor. If it fails, preserve the old file.
2352 * To do it, allocate a new widget, initialize it and, if the new file
2353 * was loaded, copy the data to the old widget.
2354 * Return 1 on success, 0 on failure.
2358 edit_reload (WEdit
* edit
, const char *filename
)
2361 int y
= edit
->widget
.y
;
2362 int x
= edit
->widget
.x
;
2363 int lines
= edit
->widget
.lines
;
2364 int columns
= edit
->widget
.cols
;
2366 e
= g_malloc0 (sizeof (WEdit
));
2367 e
->widget
= edit
->widget
;
2368 if (edit_init (e
, y
, x
, lines
, columns
, filename
, 0) == NULL
)
2374 memcpy (edit
, e
, sizeof (WEdit
));
2379 /* --------------------------------------------------------------------------------------------- */
2381 * Load a new file into the editor and set line. If it fails, preserve the old file.
2382 * To do it, allocate a new widget, initialize it and, if the new file
2383 * was loaded, copy the data to the old widget.
2384 * Return 1 on success, 0 on failure.
2388 edit_reload_line (WEdit
* edit
, const char *filename
, long line
)
2391 int y
= edit
->widget
.y
;
2392 int x
= edit
->widget
.x
;
2393 int lines
= edit
->widget
.lines
;
2394 int columns
= edit
->widget
.cols
;
2396 e
= g_malloc0 (sizeof (WEdit
));
2397 e
->widget
= edit
->widget
;
2398 if (edit_init (e
, y
, x
, lines
, columns
, filename
, line
) == NULL
)
2404 memcpy (edit
, e
, sizeof (WEdit
));
2409 /* --------------------------------------------------------------------------------------------- */
2412 edit_set_codeset (WEdit
* edit
)
2418 get_codepage_id (mc_global
.source_codepage
>=
2419 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2424 conv
= str_crt_conv_from (cp_id
);
2425 if (conv
!= INVALID_CONV
)
2427 if (edit
->converter
!= str_cnv_from_term
)
2428 str_close_conv (edit
->converter
);
2429 edit
->converter
= conv
;
2434 edit
->utf8
= str_isutf8 (cp_id
);
2441 /* --------------------------------------------------------------------------------------------- */
2443 Recording stack for undo:
2444 The following is an implementation of a compressed stack. Identical
2445 pushes are recorded by a negative prefix indicating the number of times the
2446 same char was pushed. This saves space for repeated curs-left or curs-right
2463 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2464 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2465 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2466 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2469 The only way the cursor moves or the buffer is changed is through the routines:
2470 insert, backspace, insert_ahead, delete, and cursor_move.
2471 These record the reverse undo movements onto the stack each time they are
2474 Each key press results in a set of actions (insert; delete ...). So each time
2475 a key is pressed the current position of start_display is pushed as
2476 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2477 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2478 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2483 edit_push_undo_action (WEdit
* edit
, long c
, ...)
2485 unsigned long sp
= edit
->undo_stack_pointer
;
2489 /* first enlarge the stack if necessary */
2490 if (sp
> edit
->undo_stack_size
- 10)
2492 if (option_max_undo
< 256)
2493 option_max_undo
= 256;
2494 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2496 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2499 edit
->undo_stack
= t
;
2500 edit
->undo_stack_size
<<= 1;
2501 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2505 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2506 if (edit
->undo_stack_disable
)
2508 edit_push_redo_action (edit
, KEY_PRESS
);
2509 edit_push_redo_action (edit
, c
);
2512 else if (edit
->redo_stack_reset
)
2514 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2517 if (edit
->undo_stack_bottom
!= sp
2518 && spm1
!= edit
->undo_stack_bottom
2519 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2522 if (edit
->undo_stack
[spm1
] < 0)
2524 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2527 if (edit
->undo_stack
[spm1
] > -1000000000)
2529 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2531 edit
->undo_stack
[spm1
]--;
2539 d
= edit
->undo_stack
[spm1
];
2543 return; /* --> no need to push multiple do-nothings */
2544 edit
->undo_stack
[sp
] = -2;
2549 edit
->undo_stack
[sp
] = c
;
2552 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2554 /* if the sp wraps round and catches the undo_stack_bottom then erase
2555 * the first set of actions on the stack to make space - by moving
2556 * undo_stack_bottom forward one "key press" */
2557 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2558 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2559 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2562 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2564 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2565 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2567 /*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: */
2568 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2569 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2571 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2576 edit_push_redo_action (WEdit
* edit
, long c
, ...)
2578 unsigned long sp
= edit
->redo_stack_pointer
;
2581 /* first enlarge the stack if necessary */
2582 if (sp
> edit
->redo_stack_size
- 10)
2584 if (option_max_undo
< 256)
2585 option_max_undo
= 256;
2586 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2588 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2591 edit
->redo_stack
= t
;
2592 edit
->redo_stack_size
<<= 1;
2593 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2597 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2599 if (edit
->redo_stack_bottom
!= sp
2600 && spm1
!= edit
->redo_stack_bottom
2601 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2604 if (edit
->redo_stack
[spm1
] < 0)
2606 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2609 if (edit
->redo_stack
[spm1
] > -1000000000)
2611 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2612 edit
->redo_stack
[spm1
]--;
2619 d
= edit
->redo_stack
[spm1
];
2623 return; /* --> no need to push multiple do-nothings */
2624 edit
->redo_stack
[sp
] = -2;
2625 goto redo_check_bottom
;
2629 edit
->redo_stack
[sp
] = c
;
2632 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2634 /* if the sp wraps round and catches the redo_stack_bottom then erase
2635 * the first set of actions on the stack to make space - by moving
2636 * redo_stack_bottom forward one "key press" */
2637 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2638 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2639 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2642 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2644 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2645 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2648 * If a single key produced enough pushes to wrap all the way round then
2649 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2650 * The stack is then initialised:
2653 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2654 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2655 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2659 /* --------------------------------------------------------------------------------------------- */
2661 Basic low level single character buffer alterations and movements at the cursor.
2662 Returns char passed over, inserted or removed.
2666 edit_insert (WEdit
* edit
, int c
)
2668 /* check if file has grown to large */
2669 if (edit
->last_byte
>= SIZE_LIMIT
)
2672 /* first we must update the position of the display window */
2673 if (edit
->curs1
< edit
->start_display
)
2675 edit
->start_display
++;
2680 /* Mark file as modified, unless the file hasn't been fully loaded */
2681 if (edit
->loading_done
)
2683 edit_modification (edit
);
2686 /* now we must update some info on the file and check if a redraw is required */
2689 if (edit
->book_mark
)
2690 book_mark_inc (edit
, edit
->curs_line
);
2692 edit
->total_lines
++;
2693 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2696 /* save the reverse command onto the undo stack */
2697 /* ordinary char and not space */
2699 edit_push_undo_action (edit
, BACKSPACE
);
2701 edit_push_undo_action (edit
, BACKSPACE_BR
);
2702 /* update markers */
2703 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2704 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2705 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2707 /* add a new buffer if we've reached the end of the last one */
2708 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2709 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2711 /* perform the insertion */
2712 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2713 = (unsigned char) c
;
2715 /* update file length */
2718 /* update cursor position */
2722 /* --------------------------------------------------------------------------------------------- */
2723 /** same as edit_insert and move left */
2726 edit_insert_ahead (WEdit
* edit
, int c
)
2728 if (edit
->last_byte
>= SIZE_LIMIT
)
2731 if (edit
->curs1
< edit
->start_display
)
2733 edit
->start_display
++;
2737 edit_modification (edit
);
2740 if (edit
->book_mark
)
2741 book_mark_inc (edit
, edit
->curs_line
);
2742 edit
->total_lines
++;
2743 edit
->force
|= REDRAW_AFTER_CURSOR
;
2745 /* ordinary char and not space */
2747 edit_push_undo_action (edit
, DELCHAR
);
2749 edit_push_undo_action (edit
, DELCHAR_BR
);
2751 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2752 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2753 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2755 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2756 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2757 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2758 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2765 /* --------------------------------------------------------------------------------------------- */
2768 edit_delete (WEdit
* edit
, const int byte_delete
)
2778 /* if byte_delete = 1 then delete only one byte not multibyte char */
2779 if (edit
->utf8
&& byte_delete
== 0)
2781 edit_get_utf (edit
, edit
->curs1
, &cw
);
2786 if (edit
->mark2
!= edit
->mark1
)
2787 edit_push_markers (edit
);
2789 for (i
= 1; i
<= cw
; i
++)
2791 if (edit
->mark1
> edit
->curs1
)
2794 edit
->end_mark_curs
--;
2796 if (edit
->mark2
> edit
->curs1
)
2798 if (edit
->last_get_rule
> edit
->curs1
)
2799 edit
->last_get_rule
--;
2801 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2803 1) & M_EDIT_BUF_SIZE
) - 1];
2805 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2807 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2808 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2812 edit_push_undo_action (edit
, p
+ 256);
2815 edit_modification (edit
);
2818 if (edit
->book_mark
)
2819 book_mark_dec (edit
, edit
->curs_line
);
2820 edit
->total_lines
--;
2821 edit
->force
|= REDRAW_AFTER_CURSOR
;
2823 if (edit
->curs1
< edit
->start_display
)
2825 edit
->start_display
--;
2833 /* --------------------------------------------------------------------------------------------- */
2834 /** moves the cursor right or left: increment positive or negative respectively */
2837 edit_cursor_move (WEdit
* edit
, long increment
)
2839 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2844 for (; increment
< 0; increment
++)
2849 edit_push_undo_action (edit
, CURS_RIGHT
);
2851 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2852 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2853 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2854 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2855 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2857 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2858 1) & M_EDIT_BUF_SIZE
];
2859 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2861 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2862 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2868 edit
->force
|= REDRAW_LINE_BELOW
;
2873 else if (increment
> 0)
2875 for (; increment
> 0; increment
--)
2880 edit_push_undo_action (edit
, CURS_LEFT
);
2882 c
= edit_get_byte (edit
, edit
->curs1
);
2883 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2884 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2885 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2887 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2889 1) & M_EDIT_BUF_SIZE
) - 1];
2890 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2892 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2893 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2899 edit
->force
|= REDRAW_LINE_ABOVE
;
2905 /* These functions return positions relative to lines */
2907 /* --------------------------------------------------------------------------------------------- */
2908 /** returns index of last char on line + 1 */
2911 edit_eol (WEdit
* edit
, long current
)
2913 if (current
>= edit
->last_byte
)
2914 return edit
->last_byte
;
2917 if (edit_get_byte (edit
, current
) == '\n')
2922 /* --------------------------------------------------------------------------------------------- */
2923 /** returns index of first char on line */
2926 edit_bol (WEdit
* edit
, long current
)
2932 if (edit_get_byte (edit
, current
- 1) == '\n')
2937 /* --------------------------------------------------------------------------------------------- */
2940 edit_count_lines (WEdit
* edit
, long current
, long upto
)
2943 if (upto
> edit
->last_byte
)
2944 upto
= edit
->last_byte
;
2947 while (current
< upto
)
2948 if (edit_get_byte (edit
, current
++) == '\n')
2953 /* --------------------------------------------------------------------------------------------- */
2954 /* If lines is zero this returns the count of lines from current to upto. */
2955 /* If upto is zero returns index of lines forward current. */
2958 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
2962 return edit_count_lines (edit
, current
, upto
);
2971 next
= edit_eol (edit
, current
) + 1;
2972 if (next
> edit
->last_byte
)
2981 /* --------------------------------------------------------------------------------------------- */
2982 /** Returns offset of 'lines' lines up from current */
2985 edit_move_backward (WEdit
* edit
, long current
, long lines
)
2989 current
= edit_bol (edit
, current
);
2990 while ((lines
--) && current
!= 0)
2991 current
= edit_bol (edit
, current
- 1);
2995 /* --------------------------------------------------------------------------------------------- */
2996 /* If cols is zero this returns the count of columns from current to upto. */
2997 /* If upto is zero returns index of cols across from current. */
3000 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
3011 q
= edit
->last_byte
+ 2;
3013 for (col
= 0, p
= current
; p
< q
; p
++)
3025 orig_c
= c
= edit_get_byte (edit
, p
);
3033 utf_ch
= edit_get_utf (edit
, p
, &cw
);
3034 if (mc_global
.utf8_display
)
3038 if (g_unichar_iswide (utf_ch
))
3041 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
3045 c
= convert_to_display_c (c
);
3049 col
+= TAB_SIZE
- col
% TAB_SIZE
;
3057 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!mc_global
.utf8_display
&& !edit
->utf8
)))
3058 /* '\r' is shown as ^M, so we must advance 2 characters */
3059 /* Caret notation for control characters */
3067 /* --------------------------------------------------------------------------------------------- */
3068 /** returns the current column position of the cursor */
3071 edit_get_col (WEdit
* edit
)
3073 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3076 /* --------------------------------------------------------------------------------------------- */
3077 /* Scrolling functions */
3078 /* --------------------------------------------------------------------------------------------- */
3081 edit_update_curs_row (WEdit
* edit
)
3083 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
3086 /* --------------------------------------------------------------------------------------------- */
3089 edit_update_curs_col (WEdit
* edit
)
3091 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3094 /* --------------------------------------------------------------------------------------------- */
3097 edit_get_curs_col (const WEdit
* edit
)
3099 return edit
->curs_col
;
3102 /* --------------------------------------------------------------------------------------------- */
3103 /** moves the display start position up by i lines */
3106 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
3108 unsigned long lines_above
= edit
->start_line
;
3109 if (i
> lines_above
)
3113 edit
->start_line
-= i
;
3114 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
3115 edit
->force
|= REDRAW_PAGE
;
3116 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3118 edit_update_curs_row (edit
);
3122 /* --------------------------------------------------------------------------------------------- */
3123 /** returns 1 if could scroll, 0 otherwise */
3126 edit_scroll_downward (WEdit
* edit
, int i
)
3129 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->widget
.lines
- 1);
3130 if (lines_below
> 0)
3132 if (i
> lines_below
)
3134 edit
->start_line
+= i
;
3135 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
3136 edit
->force
|= REDRAW_PAGE
;
3137 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3139 edit_update_curs_row (edit
);
3142 /* --------------------------------------------------------------------------------------------- */
3145 edit_scroll_right (WEdit
* edit
, int i
)
3147 edit
->force
|= REDRAW_PAGE
;
3148 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3149 edit
->start_col
-= i
;
3152 /* --------------------------------------------------------------------------------------------- */
3155 edit_scroll_left (WEdit
* edit
, int i
)
3157 if (edit
->start_col
)
3159 edit
->start_col
+= i
;
3160 if (edit
->start_col
> 0)
3161 edit
->start_col
= 0;
3162 edit
->force
|= REDRAW_PAGE
;
3163 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3167 /* --------------------------------------------------------------------------------------------- */
3168 /* high level cursor movement commands */
3169 /* --------------------------------------------------------------------------------------------- */
3172 edit_move_to_prev_col (WEdit
* edit
, long p
)
3174 int prev
= edit
->prev_col
;
3175 int over
= edit
->over_col
;
3176 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
3178 if (option_cursor_beyond_eol
)
3180 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
3181 edit_eol (edit
, edit
->curs1
));
3183 if (line_len
< prev
+ edit
->over_col
)
3185 edit
->over_col
= prev
+ over
- line_len
;
3186 edit
->prev_col
= line_len
;
3187 edit
->curs_col
= line_len
;
3191 edit
->curs_col
= prev
+ over
;
3192 edit
->prev_col
= edit
->curs_col
;
3199 if (is_in_indent (edit
) && option_fake_half_tabs
)
3201 edit_update_curs_col (edit
);
3203 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
3205 int q
= edit
->curs_col
;
3206 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
3207 p
= edit_bol (edit
, edit
->curs1
);
3208 edit_cursor_move (edit
,
3209 edit_move_forward3 (edit
, p
, edit
->curs_col
,
3211 if (!left_of_four_spaces (edit
))
3212 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3218 /* --------------------------------------------------------------------------------------------- */
3221 line_is_blank (WEdit
* edit
, long line
)
3223 return is_blank (edit
, edit_find_line (edit
, line
));
3226 /* --------------------------------------------------------------------------------------------- */
3227 /** move cursor to line 'line' */
3230 edit_move_to_line (WEdit
* e
, long line
)
3232 if (line
< e
->curs_line
)
3233 edit_move_up (e
, e
->curs_line
- line
, 0);
3235 edit_move_down (e
, line
- e
->curs_line
, 0);
3236 edit_scroll_screen_over_cursor (e
);
3239 /* --------------------------------------------------------------------------------------------- */
3240 /** scroll window so that first visible line is 'line' */
3243 edit_move_display (WEdit
* e
, long line
)
3245 if (line
< e
->start_line
)
3246 edit_scroll_upward (e
, e
->start_line
- line
);
3248 edit_scroll_downward (e
, line
- e
->start_line
);
3251 /* --------------------------------------------------------------------------------------------- */
3252 /** save markers onto undo stack */
3255 edit_push_markers (WEdit
* edit
)
3257 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3258 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3259 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3262 /* --------------------------------------------------------------------------------------------- */
3265 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
3274 /* --------------------------------------------------------------------------------------------- */
3275 /** highlight marker toggle */
3278 edit_mark_cmd (WEdit
* edit
, int unmark
)
3280 edit_push_markers (edit
);
3283 edit_set_markers (edit
, 0, 0, 0, 0);
3284 edit
->force
|= REDRAW_PAGE
;
3288 if (edit
->mark2
>= 0)
3290 edit
->end_mark_curs
= -1;
3291 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3292 edit
->curs_col
+ edit
->over_col
);
3293 edit
->force
|= REDRAW_PAGE
;
3297 edit
->end_mark_curs
= edit
->curs1
;
3298 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3299 edit
->curs_col
+ edit
->over_col
);
3304 /* --------------------------------------------------------------------------------------------- */
3305 /** highlight the word under cursor */
3308 edit_mark_current_word_cmd (WEdit
* edit
)
3312 for (pos
= edit
->curs1
; pos
!= 0; pos
--)
3316 c1
= edit_get_byte (edit
, pos
);
3317 c2
= edit_get_byte (edit
, pos
- 1);
3318 if (!isspace (c1
) && isspace (c2
))
3320 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3325 for (; pos
< edit
->last_byte
; pos
++)
3329 c1
= edit_get_byte (edit
, pos
);
3330 c2
= edit_get_byte (edit
, pos
+ 1);
3331 if (!isspace (c1
) && isspace (c2
))
3333 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3336 edit
->mark2
= min (pos
+ 1, edit
->last_byte
);
3338 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3341 /* --------------------------------------------------------------------------------------------- */
3344 edit_mark_current_line_cmd (WEdit
* edit
)
3346 long pos
= edit
->curs1
;
3348 edit
->mark1
= edit_bol (edit
, pos
);
3349 edit
->mark2
= edit_eol (edit
, pos
);
3351 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3354 /* --------------------------------------------------------------------------------------------- */
3357 edit_delete_line (WEdit
* edit
)
3360 * Delete right part of the line.
3361 * Note that edit_get_byte() returns '\n' when byte position is
3364 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3366 (void) edit_delete (edit
, 1);
3371 * Note that edit_delete() will not corrupt anything if called while
3372 * cursor position is EOF.
3374 (void) edit_delete (edit
, 1);
3377 * Delete left part of the line.
3378 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3380 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3382 (void) edit_backspace (edit
, 1);
3386 /* --------------------------------------------------------------------------------------------- */
3389 edit_indent_width (WEdit
* edit
, long p
)
3392 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
3394 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
3397 /* --------------------------------------------------------------------------------------------- */
3400 edit_insert_indent (WEdit
* edit
, int indent
)
3402 if (!option_fill_tabs_with_spaces
)
3404 while (indent
>= TAB_SIZE
)
3406 edit_insert (edit
, '\t');
3410 while (indent
-- > 0)
3411 edit_insert (edit
, ' ');
3414 /* --------------------------------------------------------------------------------------------- */
3417 edit_push_key_press (WEdit
* edit
)
3419 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3420 if (edit
->mark2
== -1)
3422 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3423 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3427 /* --------------------------------------------------------------------------------------------- */
3430 edit_find_bracket (WEdit
* edit
)
3432 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3433 if (last_bracket
!= edit
->bracket
)
3434 edit
->force
|= REDRAW_PAGE
;
3435 last_bracket
= edit
->bracket
;
3438 /* --------------------------------------------------------------------------------------------- */
3440 * This executes a command as though the user initiated it through a key
3441 * press. Callback with WIDGET_KEY as a message calls this after
3442 * translating the key press. This function can be used to pass any
3443 * command to the editor. Note that the screen wouldn't update
3444 * automatically. Either of command or char_for_insertion must be
3445 * passed as -1. Commands are executed, and char_for_insertion is
3446 * inserted at the cursor.
3450 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3452 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3454 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3457 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3460 if (macro_index
!= -1)
3462 edit
->force
|= REDRAW_COMPLETELY
;
3463 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3465 edit_store_macro_cmd (edit
);
3469 else if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3471 edit_repeat_macro_cmd (edit
);
3477 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3479 record_macro_buf
[macro_index
].action
= command
;
3480 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3482 /* record the beginning of a set of editing actions initiated by a key press */
3483 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3484 edit_push_key_press (edit
);
3486 edit_execute_cmd (edit
, command
, char_for_insertion
);
3487 if (edit
->column_highlight
)
3488 edit
->force
|= REDRAW_PAGE
;
3491 /* --------------------------------------------------------------------------------------------- */
3493 This executes a command at a lower level than macro recording.
3494 It also does not push a key_press onto the undo stack. This means
3495 that if it is called many times, a single undo command will undo
3496 all of them. It also does not check for the Undo command.
3499 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3501 edit
->force
|= REDRAW_LINE
;
3503 /* The next key press will unhighlight the found string, so update
3505 if (edit
->found_len
|| edit
->column_highlight
)
3506 edit
->force
|= REDRAW_PAGE
;
3510 /* a mark command with shift-arrow */
3513 case CK_MarkToWordBegin
:
3514 case CK_MarkToWordEnd
:
3520 case CK_MarkPageDown
:
3521 case CK_MarkToFileBegin
:
3522 case CK_MarkToFileEnd
:
3523 case CK_MarkToPageBegin
:
3524 case CK_MarkToPageEnd
:
3525 case CK_MarkScrollUp
:
3526 case CK_MarkScrollDown
:
3527 case CK_MarkParagraphUp
:
3528 case CK_MarkParagraphDown
:
3529 /* a mark command with alt-arrow */
3530 case CK_MarkColumnPageUp
:
3531 case CK_MarkColumnPageDown
:
3532 case CK_MarkColumnLeft
:
3533 case CK_MarkColumnRight
:
3534 case CK_MarkColumnUp
:
3535 case CK_MarkColumnDown
:
3536 case CK_MarkColumnScrollUp
:
3537 case CK_MarkColumnScrollDown
:
3538 case CK_MarkColumnParagraphUp
:
3539 case CK_MarkColumnParagraphDown
:
3540 edit
->column_highlight
= 0;
3541 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3543 edit_mark_cmd (edit
, 1); /* clear */
3544 edit_mark_cmd (edit
, 0); /* marking on */
3546 edit
->highlight
= 1;
3549 /* any other command */
3551 if (edit
->highlight
)
3552 edit_mark_cmd (edit
, 0); /* clear */
3553 edit
->highlight
= 0;
3556 /* first check for undo */
3557 if (command
== CK_Undo
)
3559 edit
->redo_stack_reset
= 0;
3560 edit_group_undo (edit
);
3561 edit
->found_len
= 0;
3562 edit
->prev_col
= edit_get_col (edit
);
3563 edit
->search_start
= edit
->curs1
;
3566 /* check for redo */
3567 if (command
== CK_Redo
)
3569 edit
->redo_stack_reset
= 0;
3570 edit_do_redo (edit
);
3571 edit
->found_len
= 0;
3572 edit
->prev_col
= edit_get_col (edit
);
3573 edit
->search_start
= edit
->curs1
;
3577 edit
->redo_stack_reset
= 1;
3579 /* An ordinary key press */
3580 if (char_for_insertion
>= 0)
3582 /* if non persistent selection and text selected */
3583 if (!option_persistent_selections
)
3585 if (edit
->mark1
!= edit
->mark2
)
3586 edit_block_delete_cmd (edit
);
3588 if (edit
->overwrite
)
3590 /* remove char only one time, after input first byte, multibyte chars */
3591 if ((!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3592 && edit_get_byte (edit
, edit
->curs1
) != '\n')
3593 edit_delete (edit
, 0);
3595 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3596 edit_insert_over (edit
);
3598 if (char_for_insertion
> 255 && mc_global
.utf8_display
== 0)
3600 unsigned char str
[6 + 1];
3602 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3612 while (str
[i
] != 0 && i
<= 6)
3614 char_for_insertion
= str
[i
];
3615 edit_insert (edit
, char_for_insertion
);
3621 edit_insert (edit
, char_for_insertion
);
3623 if (option_auto_para_formatting
)
3625 format_paragraph (edit
, 0);
3626 edit
->force
|= REDRAW_PAGE
;
3629 check_and_wrap_line (edit
);
3630 edit
->found_len
= 0;
3631 edit
->prev_col
= edit_get_col (edit
);
3632 edit
->search_start
= edit
->curs1
;
3633 edit_find_bracket (edit
);
3639 case CK_TopOnScreen
:
3640 case CK_BottomOnScreen
:
3653 if (edit
->mark2
>= 0)
3655 if (!option_persistent_selections
)
3657 if (edit
->column_highlight
)
3658 edit_push_undo_action (edit
, COLUMN_ON
);
3659 edit
->column_highlight
= 0;
3660 edit_mark_cmd (edit
, 1);
3667 case CK_TopOnScreen
:
3668 case CK_BottomOnScreen
:
3669 case CK_MarkToPageBegin
:
3670 case CK_MarkToPageEnd
:
3675 case CK_MarkToWordBegin
:
3676 case CK_MarkToWordEnd
:
3679 case CK_MarkColumnUp
:
3680 case CK_MarkColumnDown
:
3681 if (edit
->mark2
== -1)
3682 break; /*marking is following the cursor: may need to highlight a whole line */
3687 edit
->force
|= REDRAW_CHAR_ONLY
;
3690 /* basic cursor key commands */
3694 /* if non persistent selection and text selected */
3695 if (!option_persistent_selections
)
3697 if (edit
->mark1
!= edit
->mark2
)
3699 edit_block_delete_cmd (edit
);
3703 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3708 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3710 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3711 edit_backspace (edit
, 1);
3716 if (option_fake_half_tabs
)
3719 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3721 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3722 edit_backspace (edit
, 1);
3727 edit_backspace (edit
, 0);
3730 /* if non persistent selection and text selected */
3731 if (!option_persistent_selections
)
3733 if (edit
->mark1
!= edit
->mark2
)
3735 edit_block_delete_cmd (edit
);
3740 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3741 edit_insert_over (edit
);
3743 if (option_fake_half_tabs
)
3746 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3748 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3749 edit_delete (edit
, 1);
3753 edit_delete (edit
, 0);
3755 case CK_DeleteToWordBegin
:
3757 edit_left_delete_word (edit
);
3759 case CK_DeleteToWordEnd
:
3760 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3761 edit_insert_over (edit
);
3763 edit_right_delete_word (edit
);
3766 edit_delete_line (edit
);
3768 case CK_DeleteToHome
:
3769 edit_delete_to_line_begin (edit
);
3771 case CK_DeleteToEnd
:
3772 edit_delete_to_line_end (edit
);
3776 if (option_auto_para_formatting
)
3778 edit_double_newline (edit
);
3779 if (option_return_does_auto_indent
)
3780 edit_auto_indent (edit
);
3781 format_paragraph (edit
, 0);
3785 edit_insert (edit
, '\n');
3786 if (option_return_does_auto_indent
)
3788 edit_auto_indent (edit
);
3793 edit_insert (edit
, '\n');
3796 case CK_MarkColumnPageUp
:
3797 edit
->column_highlight
= 1;
3800 edit_move_up (edit
, edit
->widget
.lines
- 1, 1);
3802 case CK_MarkColumnPageDown
:
3803 edit
->column_highlight
= 1;
3805 case CK_MarkPageDown
:
3806 edit_move_down (edit
, edit
->widget
.lines
- 1, 1);
3808 case CK_MarkColumnLeft
:
3809 edit
->column_highlight
= 1;
3812 if (option_fake_half_tabs
)
3814 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3816 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3819 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3820 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3824 edit_left_char_move_cmd (edit
);
3826 case CK_MarkColumnRight
:
3827 edit
->column_highlight
= 1;
3830 if (option_fake_half_tabs
)
3832 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3834 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3835 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3839 edit_right_char_move_cmd (edit
);
3841 case CK_TopOnScreen
:
3842 case CK_MarkToPageBegin
:
3843 edit_begin_page (edit
);
3845 case CK_BottomOnScreen
:
3846 case CK_MarkToPageEnd
:
3847 edit_end_page (edit
);
3850 case CK_MarkToWordBegin
:
3852 edit_left_word_move_cmd (edit
);
3855 case CK_MarkToWordEnd
:
3857 edit_right_word_move_cmd (edit
);
3859 case CK_MarkColumnUp
:
3860 edit
->column_highlight
= 1;
3863 edit_move_up (edit
, 1, 0);
3865 case CK_MarkColumnDown
:
3866 edit
->column_highlight
= 1;
3869 edit_move_down (edit
, 1, 0);
3871 case CK_MarkColumnParagraphUp
:
3872 edit
->column_highlight
= 1;
3873 case CK_ParagraphUp
:
3874 case CK_MarkParagraphUp
:
3875 edit_move_up_paragraph (edit
, 0);
3877 case CK_MarkColumnParagraphDown
:
3878 edit
->column_highlight
= 1;
3879 case CK_ParagraphDown
:
3880 case CK_MarkParagraphDown
:
3881 edit_move_down_paragraph (edit
, 0);
3883 case CK_MarkColumnScrollUp
:
3884 edit
->column_highlight
= 1;
3886 case CK_MarkScrollUp
:
3887 edit_move_up (edit
, 1, 1);
3889 case CK_MarkColumnScrollDown
:
3890 edit
->column_highlight
= 1;
3892 case CK_MarkScrollDown
:
3893 edit_move_down (edit
, 1, 1);
3897 edit_cursor_to_bol (edit
);
3901 edit_cursor_to_eol (edit
);
3904 /* if text marked shift block */
3905 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3907 if (edit
->mark2
< 0)
3908 edit_mark_cmd (edit
, 0);
3909 edit_move_block_to_right (edit
);
3913 if (option_cursor_beyond_eol
)
3914 edit_insert_over (edit
);
3915 edit_tab_cmd (edit
);
3916 if (option_auto_para_formatting
)
3918 format_paragraph (edit
, 0);
3919 edit
->force
|= REDRAW_PAGE
;
3923 check_and_wrap_line (edit
);
3928 case CK_InsertOverwrite
:
3929 edit
->overwrite
= !edit
->overwrite
;
3933 if (edit
->mark2
>= 0)
3935 if (edit
->column_highlight
)
3936 edit_push_undo_action (edit
, COLUMN_ON
);
3937 edit
->column_highlight
= 0;
3939 edit_mark_cmd (edit
, 0);
3942 if (!edit
->column_highlight
)
3943 edit_push_undo_action (edit
, COLUMN_OFF
);
3944 edit
->column_highlight
= 1;
3945 edit_mark_cmd (edit
, 0);
3948 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3949 edit
->force
|= REDRAW_PAGE
;
3952 if (edit
->column_highlight
)
3953 edit_push_undo_action (edit
, COLUMN_ON
);
3954 edit
->column_highlight
= 0;
3955 edit_mark_cmd (edit
, 1);
3958 if (edit
->column_highlight
)
3959 edit_push_undo_action (edit
, COLUMN_ON
);
3960 edit
->column_highlight
= 0;
3961 edit_mark_current_word_cmd (edit
);
3964 if (edit
->column_highlight
)
3965 edit_push_undo_action (edit
, COLUMN_ON
);
3966 edit
->column_highlight
= 0;
3967 edit_mark_current_line_cmd (edit
);
3970 case CK_ShowNumbers
:
3971 option_line_state
= !option_line_state
;
3972 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
3973 edit
->force
|= REDRAW_PAGE
;
3977 show_right_margin
= !show_right_margin
;
3978 edit
->force
|= REDRAW_PAGE
;
3981 case CK_HighlightOccurences
:
3982 edit_highlight_occurences (edit
);
3983 edit
->force
|= REDRAW_PAGE
;
3986 case CK_UnhighlightOccurences
:
3987 edit_unhighlight_occurences (edit
);
3988 edit
->force
|= REDRAW_PAGE
;
3992 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
3993 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
3994 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3996 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3998 case CK_BookmarkFlush
:
3999 book_mark_flush (edit
, BOOK_MARK_COLOR
);
4000 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
4001 edit
->force
|= REDRAW_PAGE
;
4003 case CK_BookmarkNext
:
4004 if (edit
->book_mark
)
4006 struct _book_mark
*p
;
4007 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4011 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4012 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4013 edit_move_to_line (edit
, p
->line
);
4017 case CK_BookmarkPrev
:
4018 if (edit
->book_mark
)
4020 struct _book_mark
*p
;
4021 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4022 while (p
->line
== edit
->curs_line
)
4027 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4028 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4029 edit_move_to_line (edit
, p
->line
);
4035 case CK_MarkToFileBegin
:
4036 edit_move_to_top (edit
);
4039 case CK_MarkToFileEnd
:
4040 edit_move_to_bottom (edit
);
4044 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4045 edit_insert_over (edit
);
4046 edit_block_copy_cmd (edit
);
4049 edit_block_delete_cmd (edit
);
4052 edit_block_move_cmd (edit
);
4055 case CK_BlockShiftLeft
:
4056 if (edit
->mark1
!= edit
->mark2
)
4057 edit_move_block_to_left (edit
);
4059 case CK_BlockShiftRight
:
4060 if (edit
->mark1
!= edit
->mark2
)
4061 edit_move_block_to_right (edit
);
4064 edit_copy_to_X_buf_cmd (edit
);
4067 edit_cut_to_X_buf_cmd (edit
);
4070 /* if non persistent selection and text selected */
4071 if (!option_persistent_selections
)
4073 if (edit
->mark1
!= edit
->mark2
)
4074 edit_block_delete_cmd (edit
);
4076 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4077 edit_insert_over (edit
);
4078 edit_paste_from_X_buf_cmd (edit
);
4081 edit_paste_from_history (edit
);
4085 edit_save_as_cmd (edit
);
4088 edit_save_confirm_cmd (edit
);
4091 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
4094 edit_save_block_cmd (edit
);
4097 edit_insert_file_cmd (edit
);
4101 edit_load_back_cmd (edit
);
4104 edit_load_forward_cmd (edit
);
4107 case CK_EditSyntaxFile
:
4108 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
4110 case CK_SyntaxChoose
:
4111 edit_syntax_dialog (edit
);
4114 case CK_EditUserMenu
:
4115 edit_load_cmd (edit
, EDIT_FILE_MENU
);
4118 case CK_SyntaxOnOff
:
4119 option_syntax_highlighting
^= 1;
4120 if (option_syntax_highlighting
== 1)
4121 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
4122 edit
->force
|= REDRAW_PAGE
;
4126 enable_show_tabs_tws
^= 1;
4127 edit
->force
|= REDRAW_PAGE
;
4131 edit_search_cmd (edit
, FALSE
);
4133 case CK_SearchContinue
:
4134 edit_search_cmd (edit
, TRUE
);
4137 edit_replace_cmd (edit
, 0);
4139 case CK_ReplaceContinue
:
4140 edit_replace_cmd (edit
, 1);
4143 /* if text marked shift block */
4144 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
4146 edit_move_block_to_left (edit
);
4150 edit_complete_word_cmd (edit
);
4154 edit_get_match_keyword_cmd (edit
);
4157 dlg_stop (edit
->widget
.owner
);
4160 edit_new_cmd (edit
);
4163 edit_help_cmd (edit
);
4166 edit_refresh_cmd (edit
);
4178 edit_options_dialog (edit
);
4180 case CK_OptionsSaveMode
:
4181 menu_save_mode_cmd ();
4186 /* fool gcc to prevent a Y2K warning */
4187 char time_format
[] = "_c";
4188 time_format
[0] = '%';
4190 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4191 edit_print_string (edit
, s
);
4192 edit
->force
|= REDRAW_PAGE
;
4197 edit_goto_cmd (edit
);
4199 case CK_ParagraphFormat
:
4200 format_paragraph (edit
, 1);
4201 edit
->force
|= REDRAW_PAGE
;
4203 case CK_MacroDelete
:
4204 edit_delete_macro_cmd (edit
);
4206 case CK_MatchBracket
:
4207 edit_goto_matching_bracket (edit
);
4210 user_menu (edit
, NULL
, -1);
4213 edit_sort_cmd (edit
);
4215 case CK_ExternalCommand
:
4216 edit_ext_cmd (edit
);
4219 edit_mail_dialog (edit
);
4225 case CK_SelectCodepage
:
4226 edit_select_codepage_cmd (edit
);
4229 case CK_InsertLiteral
:
4230 edit_insert_literal_cmd (edit
);
4232 case CK_MacroStartStopRecord
:
4233 edit_begin_end_macro_cmd (edit
);
4235 case CK_RepeatStartStopRecord
:
4236 edit_begin_end_repeat_cmd (edit
);
4238 case CK_ExtendedKeyMap
:
4239 edit
->extmod
= TRUE
;
4246 if ((command
/ CK_PipeBlock (0)) == 1)
4247 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
4249 /* keys which must set the col position, and the search vars */
4253 case CK_SearchContinue
:
4255 case CK_ReplaceContinue
:
4257 edit
->prev_col
= edit_get_col (edit
);
4261 case CK_MarkColumnUp
:
4264 case CK_MarkColumnDown
:
4267 case CK_MarkColumnPageUp
:
4269 case CK_MarkPageDown
:
4270 case CK_MarkColumnPageDown
:
4272 case CK_MarkToFileBegin
:
4274 case CK_MarkToFileEnd
:
4275 case CK_ParagraphUp
:
4276 case CK_MarkParagraphUp
:
4277 case CK_MarkColumnParagraphUp
:
4278 case CK_ParagraphDown
:
4279 case CK_MarkParagraphDown
:
4280 case CK_MarkColumnParagraphDown
:
4282 case CK_MarkScrollUp
:
4283 case CK_MarkColumnScrollUp
:
4285 case CK_MarkScrollDown
:
4286 case CK_MarkColumnScrollDown
:
4287 edit
->search_start
= edit
->curs1
;
4288 edit
->found_len
= 0;
4291 edit
->found_len
= 0;
4292 edit
->prev_col
= edit_get_col (edit
);
4293 edit
->search_start
= edit
->curs1
;
4295 edit_find_bracket (edit
);
4297 if (option_auto_para_formatting
)
4303 case CK_DeleteToWordBegin
:
4304 case CK_DeleteToWordEnd
:
4305 case CK_DeleteToHome
:
4306 case CK_DeleteToEnd
:
4307 format_paragraph (edit
, 0);
4308 edit
->force
|= REDRAW_PAGE
;
4313 /* --------------------------------------------------------------------------------------------- */
4316 edit_stack_init (void)
4318 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4320 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
4321 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4324 edit_stack_iterator
= 0;
4327 /* --------------------------------------------------------------------------------------------- */
4330 edit_stack_free (void)
4332 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4333 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
4336 /* --------------------------------------------------------------------------------------------- */
4340 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
4342 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4345 /* --------------------------------------------------------------------------------------------- */
4349 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
4351 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4354 /* --------------------------------------------------------------------------------------------- */
4357 edit_unlock_file (WEdit
* edit
)
4362 fullpath
= mc_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4363 ret
= unlock_file (fullpath
);
4369 /* --------------------------------------------------------------------------------------------- */
4372 edit_lock_file (WEdit
* edit
)
4377 fullpath
= mc_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4378 ret
= lock_file (fullpath
);
4384 /* --------------------------------------------------------------------------------------------- */