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
)
258 edit
->curs2
= edit
->last_byte
;
259 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
261 vpath
= vfs_path_from_str (filename
);
262 file
= mc_open (vpath
, O_RDONLY
| O_BINARY
);
263 vfs_path_free (vpath
);
268 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
269 edit_error_dialog (_("Error"), errmsg
);
274 if (!edit
->buffers2
[buf2
])
275 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
280 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
281 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
284 for (buf
= buf2
- 1; buf
>= 0; buf
--)
286 /* edit->buffers2[0] is already allocated */
287 if (!edit
->buffers2
[buf
])
288 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
289 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
297 char *err_str
= g_strdup_printf (_("Error reading %s"), filename
);
298 edit_error_dialog (_("Error"), err_str
);
305 /* --------------------------------------------------------------------------------------------- */
306 /** Return index of the filter or -1 is there is no appropriate filter */
309 edit_find_filter (const char *filename
)
313 if (filename
== NULL
)
316 l
= strlen (filename
);
317 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
319 e
= strlen (all_filters
[i
].extension
);
321 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
327 /* --------------------------------------------------------------------------------------------- */
330 edit_get_filter (const char *filename
)
333 char *p
, *quoted_name
;
335 i
= edit_find_filter (filename
);
339 quoted_name
= name_quote (filename
, 0);
340 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
341 g_free (quoted_name
);
345 /* --------------------------------------------------------------------------------------------- */
348 edit_insert_stream (WEdit
* edit
, FILE * f
)
352 while ((c
= fgetc (f
)) >= 0)
354 edit_insert (edit
, c
);
360 /* --------------------------------------------------------------------------------------------- */
361 /** Open file and create it if necessary. Return 0 for success, 1 for error. */
364 check_file_access (WEdit
* edit
, const char *filename
, struct stat
*st
)
367 gchar
*errmsg
= NULL
;
370 /* Try opening an existing file */
371 vpath
= vfs_path_from_str (filename
);
372 file
= mc_open (vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
377 * Try creating the file. O_EXCL prevents following broken links
378 * and opening existing files.
380 file
= mc_open (vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
383 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
388 /* New file, delete it if it's not modified or saved */
389 edit
->delete_file
= 1;
393 /* Check what we have opened */
394 if (mc_fstat (file
, st
) < 0)
396 errmsg
= g_strdup_printf (_("Cannot get size/permissions for %s"), filename
);
400 /* We want to open regular files only */
401 if (!S_ISREG (st
->st_mode
))
403 errmsg
= g_strdup_printf (_("\"%s\" is not a regular file"), filename
);
408 * Don't delete non-empty files.
409 * O_EXCL should prevent it, but let's be on the safe side.
412 edit
->delete_file
= 0;
414 if (st
->st_size
>= SIZE_LIMIT
)
415 errmsg
= g_strdup_printf (_("File \"%s\" is too large"), filename
);
418 (void) mc_close (file
);
419 vfs_path_free (vpath
);
423 edit_error_dialog (_("Error"), errmsg
);
430 /* --------------------------------------------------------------------------------------------- */
432 * Open the file and load it into the buffers, either directly or using
433 * a filter. Return 0 on success, 1 on error.
435 * Fast loading (edit_load_file_fast) is used when the file size is
436 * known. In this case the data is read into the buffers by blocks.
437 * If the file size is not known, the data is loaded byte by byte in
442 edit_load_file (WEdit
* edit
)
445 vfs_path_t
*vpath
= vfs_path_from_str (edit
->filename
);
447 /* Cannot do fast load if a filter is used */
448 if (edit_find_filter (edit
->filename
) >= 0)
452 * VFS may report file size incorrectly, and slow load is not a big
453 * deal considering overhead in VFS.
455 if (!vfs_file_is_local (vpath
))
457 vfs_path_free (vpath
);
460 * FIXME: line end translation should disable fast loading as well
461 * Consider doing fseek() to the end and ftell() for the real size.
466 /* If we are dealing with a real file, check that it exists */
467 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
472 /* nothing to load */
476 edit_init_buffers (edit
);
480 edit
->last_byte
= edit
->stat1
.st_size
;
481 edit_load_file_fast (edit
, edit
->filename
);
482 /* If fast load was used, the number of lines wasn't calculated */
483 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
490 edit
->undo_stack_disable
= 1;
491 if (edit_insert_file (edit
, edit
->filename
) < 0)
496 edit
->undo_stack_disable
= 0;
503 /* --------------------------------------------------------------------------------------------- */
504 /** Restore saved cursor position in the file */
507 edit_load_position (WEdit
* edit
)
514 if (!edit
->filename
|| !*edit
->filename
)
517 vpath
= vfs_path_from_str (edit
->filename
);
518 filename
= vfs_path_to_str (vpath
);
519 load_file_position (filename
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
520 vfs_path_free (vpath
);
525 edit_move_to_line (edit
, line
- 1);
526 edit
->prev_col
= column
;
530 edit_cursor_move (edit
, offset
);
531 line
= edit
->curs_line
;
532 edit
->search_start
= edit
->curs1
;
535 book_mark_restore (edit
, BOOK_MARK_COLOR
);
537 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
538 edit_move_display (edit
, line
- (edit
->widget
.lines
/ 2));
541 /* --------------------------------------------------------------------------------------------- */
542 /** Save cursor position in the file */
545 edit_save_position (WEdit
* edit
)
550 if (edit
->filename
== NULL
|| *edit
->filename
== '\0')
553 vpath
= vfs_path_from_str (edit
->filename
);
554 filename
= vfs_path_to_str (vpath
);
556 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
557 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
,
558 edit
->serialized_bookmarks
);
559 edit
->serialized_bookmarks
= NULL
;
562 vfs_path_free (vpath
);
565 /* --------------------------------------------------------------------------------------------- */
566 /** Clean the WEdit stricture except the widget part */
569 edit_purge_widget (WEdit
* edit
)
571 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
572 char *start
= (char *) edit
+ sizeof (Widget
);
573 memset (start
, 0, len
);
576 /* --------------------------------------------------------------------------------------------- */
579 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
580 then the file should be as it was when he loaded up. Then set edit->modified to 0.
584 edit_pop_undo_action (WEdit
* edit
)
587 unsigned long sp
= edit
->undo_stack_pointer
;
589 if (sp
== edit
->undo_stack_bottom
)
592 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
593 c
= edit
->undo_stack
[sp
];
596 /* edit->undo_stack[sp] = '@'; */
597 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
601 if (sp
== edit
->undo_stack_bottom
)
604 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
605 if (edit
->undo_stack
[sp
] == -2)
607 /* edit->undo_stack[sp] = '@'; */
608 edit
->undo_stack_pointer
= sp
;
611 edit
->undo_stack
[sp
]++;
617 edit_pop_redo_action (WEdit
* edit
)
620 unsigned long sp
= edit
->redo_stack_pointer
;
622 if (sp
== edit
->redo_stack_bottom
)
625 sp
= (sp
- 1) & edit
->redo_stack_size_mask
;
626 c
= edit
->redo_stack
[sp
];
629 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
633 if (sp
== edit
->redo_stack_bottom
)
636 c
= edit
->redo_stack
[(sp
- 1) & edit
->redo_stack_size_mask
];
637 if (edit
->redo_stack
[sp
] == -2)
638 edit
->redo_stack_pointer
= sp
;
640 edit
->redo_stack
[sp
]++;
646 get_prev_undo_action (WEdit
* edit
)
649 unsigned long sp
= edit
->undo_stack_pointer
;
651 if (sp
== edit
->undo_stack_bottom
)
654 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
655 c
= edit
->undo_stack
[sp
];
659 if (sp
== edit
->undo_stack_bottom
)
662 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
666 /* --------------------------------------------------------------------------------------------- */
667 /** is called whenever a modification is made by one of the four routines below */
670 edit_modification (WEdit
* edit
)
672 edit
->caches_valid
= 0;
674 /* raise lock when file modified */
675 if (!edit
->modified
&& !edit
->delete_file
)
676 edit
->locked
= edit_lock_file (edit
);
680 /* --------------------------------------------------------------------------------------------- */
683 edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
685 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
688 if (byte_index
>= edit
->curs1
)
692 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
693 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
694 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
697 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
698 (byte_index
& M_EDIT_BUF_SIZE
));
701 /* --------------------------------------------------------------------------------------------- */
704 edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
707 gchar utf8_buf
[3 * UTF8_CHAR_LEN
+ 1];
709 gchar
*cursor_buf_ptr
;
711 if (byte_index
> (edit
->curs1
+ edit
->curs2
) || byte_index
<= 0)
717 for (i
= 0; i
< (3 * UTF8_CHAR_LEN
); i
++)
718 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
- (2 * UTF8_CHAR_LEN
));
719 utf8_buf
[3 * UTF8_CHAR_LEN
] = '\0';
721 cursor_buf_ptr
= utf8_buf
+ (2 * UTF8_CHAR_LEN
);
722 str
= g_utf8_find_prev_char (utf8_buf
, cursor_buf_ptr
);
724 if (str
== NULL
|| g_utf8_next_char (str
) != cursor_buf_ptr
)
727 return *(cursor_buf_ptr
- 1);
731 res
= g_utf8_get_char_validated (str
, -1);
736 return *(cursor_buf_ptr
- 1);
740 *char_width
= cursor_buf_ptr
- str
;
746 /* --------------------------------------------------------------------------------------------- */
749 edit_backspace (WEdit
* edit
, const int byte_delete
)
760 if (edit
->mark2
!= edit
->mark1
)
761 edit_push_markers (edit
);
763 if (edit
->utf8
&& byte_delete
== 0)
765 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
769 for (i
= 1; i
<= cw
; i
++)
771 if (edit
->mark1
>= edit
->curs1
)
774 edit
->end_mark_curs
--;
776 if (edit
->mark2
>= edit
->curs1
)
778 if (edit
->last_get_rule
>= edit
->curs1
)
779 edit
->last_get_rule
--;
781 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
782 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
783 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
785 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
786 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
790 edit_push_undo_action (edit
, p
);
792 edit_modification (edit
);
796 book_mark_dec (edit
, edit
->curs_line
);
799 edit
->force
|= REDRAW_AFTER_CURSOR
;
802 if (edit
->curs1
< edit
->start_display
)
804 edit
->start_display
--;
812 /* --------------------------------------------------------------------------------------------- */
813 /* high level cursor movement commands */
814 /* --------------------------------------------------------------------------------------------- */
817 is_in_indent (WEdit
* edit
)
819 long p
= edit_bol (edit
, edit
->curs1
);
820 while (p
< edit
->curs1
)
821 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
826 /* --------------------------------------------------------------------------------------------- */
829 is_blank (WEdit
* edit
, long offset
)
833 s
= edit_bol (edit
, offset
);
834 f
= edit_eol (edit
, offset
) - 1;
837 c
= edit_get_byte (edit
, s
++);
845 /* --------------------------------------------------------------------------------------------- */
846 /** returns the offset of line i */
849 edit_find_line (WEdit
* edit
, int line
)
853 if (!edit
->caches_valid
)
855 for (i
= 0; i
< N_LINE_CACHES
; i
++)
856 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
857 /* three offsets that we *know* are line 0 at 0 and these two: */
858 edit
->line_numbers
[1] = edit
->curs_line
;
859 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
860 edit
->line_numbers
[2] = edit
->total_lines
;
861 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
862 edit
->caches_valid
= 1;
864 if (line
>= edit
->total_lines
)
865 return edit
->line_offsets
[2];
868 /* find the closest known point */
869 for (i
= 0; i
< N_LINE_CACHES
; i
++)
872 n
= abs (edit
->line_numbers
[i
] - line
);
880 return edit
->line_offsets
[j
]; /* know the offset exactly */
881 if (m
== 1 && j
>= 3)
882 i
= j
; /* one line different - caller might be looping, so stay in this cache */
884 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
885 if (line
> edit
->line_numbers
[j
])
886 edit
->line_offsets
[i
] =
887 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
889 edit
->line_offsets
[i
] =
890 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
891 edit
->line_numbers
[i
] = line
;
892 return edit
->line_offsets
[i
];
895 /* --------------------------------------------------------------------------------------------- */
896 /** moves up until a blank line is reached, or until just
897 before a non-blank line is reached */
900 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
903 if (edit
->curs_line
> 1)
905 if (line_is_blank (edit
, edit
->curs_line
))
907 if (line_is_blank (edit
, edit
->curs_line
- 1))
909 for (i
= edit
->curs_line
- 1; i
; i
--)
910 if (!line_is_blank (edit
, i
))
918 for (i
= edit
->curs_line
- 1; i
; i
--)
919 if (line_is_blank (edit
, i
))
925 for (i
= edit
->curs_line
- 1; i
; i
--)
926 if (line_is_blank (edit
, i
))
930 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
933 /* --------------------------------------------------------------------------------------------- */
934 /** moves down until a blank line is reached, or until just
935 before a non-blank line is reached */
938 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
941 if (edit
->curs_line
>= edit
->total_lines
- 1)
943 i
= edit
->total_lines
;
947 if (line_is_blank (edit
, edit
->curs_line
))
949 if (line_is_blank (edit
, edit
->curs_line
+ 1))
951 for (i
= edit
->curs_line
+ 1; i
; i
++)
952 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
960 for (i
= edit
->curs_line
+ 1; i
; i
++)
961 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
967 for (i
= edit
->curs_line
+ 1; i
; i
++)
968 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
972 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
975 /* --------------------------------------------------------------------------------------------- */
978 edit_begin_page (WEdit
* edit
)
980 edit_update_curs_row (edit
);
981 edit_move_up (edit
, edit
->curs_row
, 0);
984 /* --------------------------------------------------------------------------------------------- */
987 edit_end_page (WEdit
* edit
)
989 edit_update_curs_row (edit
);
990 edit_move_down (edit
, edit
->widget
.lines
- edit
->curs_row
- 1, 0);
994 /* --------------------------------------------------------------------------------------------- */
995 /** goto beginning of text */
998 edit_move_to_top (WEdit
* edit
)
1000 if (edit
->curs_line
)
1002 edit_cursor_move (edit
, -edit
->curs1
);
1003 edit_move_to_prev_col (edit
, 0);
1004 edit
->force
|= REDRAW_PAGE
;
1005 edit
->search_start
= 0;
1006 edit_update_curs_row (edit
);
1011 /* --------------------------------------------------------------------------------------------- */
1012 /** goto end of text */
1015 edit_move_to_bottom (WEdit
* edit
)
1017 if (edit
->curs_line
< edit
->total_lines
)
1019 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
1020 edit
->start_display
= edit
->last_byte
;
1021 edit
->start_line
= edit
->total_lines
;
1022 edit_scroll_upward (edit
, edit
->widget
.lines
- 1);
1023 edit
->force
|= REDRAW_PAGE
;
1027 /* --------------------------------------------------------------------------------------------- */
1028 /** goto beginning of line */
1031 edit_cursor_to_bol (WEdit
* edit
)
1033 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1034 edit
->search_start
= edit
->curs1
;
1035 edit
->prev_col
= edit_get_col (edit
);
1039 /* --------------------------------------------------------------------------------------------- */
1040 /** goto end of line */
1043 edit_cursor_to_eol (WEdit
* edit
)
1045 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1046 edit
->search_start
= edit
->curs1
;
1047 edit
->prev_col
= edit_get_col (edit
);
1051 /* --------------------------------------------------------------------------------------------- */
1053 static unsigned long
1058 const char option_chars_move_whole_word
[] =
1059 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1065 if (*option_chars_move_whole_word
== '!')
1067 return 0x80000000UL
;
1069 if (g_ascii_isupper ((gchar
) c
))
1071 else if (g_ascii_islower ((gchar
) c
))
1073 else if (g_ascii_isalpha (c
))
1075 else if (isdigit (c
))
1077 else if (isspace (c
))
1079 q
= strchr (option_chars_move_whole_word
, c
);
1081 return 0xFFFFFFFFUL
;
1084 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1089 while ((q
= strchr (q
+ 1, c
)));
1093 /* --------------------------------------------------------------------------------------------- */
1096 edit_left_word_move (WEdit
* edit
, int s
)
1101 if (edit
->column_highlight
1102 && edit
->mark1
!= edit
->mark2
1103 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1105 edit_cursor_move (edit
, -1);
1108 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1109 c2
= edit_get_byte (edit
, edit
->curs1
);
1110 if (c1
== '\n' || c2
== '\n')
1112 if (!(my_type_of (c1
) & my_type_of (c2
)))
1114 if (isspace (c1
) && !isspace (c2
))
1117 if (!isspace (c1
) && isspace (c2
))
1122 /* --------------------------------------------------------------------------------------------- */
1125 edit_left_word_move_cmd (WEdit
* edit
)
1127 edit_left_word_move (edit
, 0);
1128 edit
->force
|= REDRAW_PAGE
;
1131 /* --------------------------------------------------------------------------------------------- */
1134 edit_right_word_move (WEdit
* edit
, int s
)
1139 if (edit
->column_highlight
1140 && edit
->mark1
!= edit
->mark2
1141 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
1143 edit_cursor_move (edit
, 1);
1144 if (edit
->curs1
>= edit
->last_byte
)
1146 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1147 c2
= edit_get_byte (edit
, edit
->curs1
);
1148 if (c1
== '\n' || c2
== '\n')
1150 if (!(my_type_of (c1
) & my_type_of (c2
)))
1152 if (isspace (c1
) && !isspace (c2
))
1155 if (!isspace (c1
) && isspace (c2
))
1160 /* --------------------------------------------------------------------------------------------- */
1163 edit_right_word_move_cmd (WEdit
* edit
)
1165 edit_right_word_move (edit
, 0);
1166 edit
->force
|= REDRAW_PAGE
;
1169 /* --------------------------------------------------------------------------------------------- */
1172 edit_right_char_move_cmd (WEdit
* edit
)
1178 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
1184 c
= edit_get_byte (edit
, edit
->curs1
);
1186 if (option_cursor_beyond_eol
&& c
== '\n')
1192 edit_cursor_move (edit
, cw
);
1196 /* --------------------------------------------------------------------------------------------- */
1199 edit_left_char_move_cmd (WEdit
* edit
)
1202 if (edit
->column_highlight
1203 && option_cursor_beyond_eol
1204 && edit
->mark1
!= edit
->mark2
1205 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1209 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1213 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1219 edit_cursor_move (edit
, -cw
);
1223 /* --------------------------------------------------------------------------------------------- */
1224 /** Up or down cursor moving.
1225 direction = TRUE - move up
1230 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
1233 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
1242 edit
->force
|= REDRAW_PAGE
;
1246 edit_scroll_upward (edit
, i
);
1248 edit_scroll_downward (edit
, i
);
1250 p
= edit_bol (edit
, edit
->curs1
);
1252 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
1254 edit_cursor_move (edit
, p
- edit
->curs1
);
1256 edit_move_to_prev_col (edit
, p
);
1258 /* search start of current multibyte char (like CJK) */
1259 if (edit
->curs1
+ 1 < edit
->last_byte
)
1261 edit_right_char_move_cmd (edit
);
1262 edit_left_char_move_cmd (edit
);
1265 edit
->search_start
= edit
->curs1
;
1266 edit
->found_len
= 0;
1269 /* --------------------------------------------------------------------------------------------- */
1272 edit_right_delete_word (WEdit
* edit
)
1277 if (edit
->curs1
>= edit
->last_byte
)
1279 c1
= edit_delete (edit
, 1);
1280 c2
= edit_get_byte (edit
, edit
->curs1
);
1281 if (c1
== '\n' || c2
== '\n')
1283 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1285 if (!(my_type_of (c1
) & my_type_of (c2
)))
1290 /* --------------------------------------------------------------------------------------------- */
1293 edit_left_delete_word (WEdit
* edit
)
1298 if (edit
->curs1
<= 0)
1300 c1
= edit_backspace (edit
, 1);
1301 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1302 if (c1
== '\n' || c2
== '\n')
1304 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1306 if (!(my_type_of (c1
) & my_type_of (c2
)))
1311 /* --------------------------------------------------------------------------------------------- */
1313 the start column position is not recorded, and hence does not
1314 undo as it happed. But who would notice.
1318 edit_do_undo (WEdit
* edit
)
1323 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1325 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1332 edit_cursor_move (edit
, 1);
1335 edit_cursor_move (edit
, -1);
1339 edit_backspace (edit
, 1);
1343 edit_delete (edit
, 1);
1346 edit
->column_highlight
= 1;
1349 edit
->column_highlight
= 0;
1352 if (ac
>= 256 && ac
< 512)
1353 edit_insert_ahead (edit
, ac
- 256);
1354 if (ac
>= 0 && ac
< 256)
1355 edit_insert (edit
, ac
);
1357 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1359 edit
->mark1
= ac
- MARK_1
;
1360 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1362 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1364 edit
->mark2
= ac
- MARK_2
;
1365 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1367 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1369 edit
->end_mark_curs
= ac
- MARK_CURS
;
1372 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1375 if (edit
->start_display
> ac
- KEY_PRESS
)
1377 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1378 edit
->force
|= REDRAW_PAGE
;
1380 else if (edit
->start_display
< ac
- KEY_PRESS
)
1382 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1383 edit
->force
|= REDRAW_PAGE
;
1385 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1386 edit_update_curs_row (edit
);
1389 edit
->undo_stack_disable
= 0;
1393 edit_do_redo (WEdit
* edit
)
1398 if (edit
->redo_stack_reset
)
1402 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1409 edit_cursor_move (edit
, 1);
1412 edit_cursor_move (edit
, -1);
1415 edit_backspace (edit
, 1);
1418 edit_delete (edit
, 1);
1421 edit
->column_highlight
= 1;
1424 edit
->column_highlight
= 0;
1427 if (ac
>= 256 && ac
< 512)
1428 edit_insert_ahead (edit
, ac
- 256);
1429 if (ac
>= 0 && ac
< 256)
1430 edit_insert (edit
, ac
);
1432 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1434 edit
->mark1
= ac
- MARK_1
;
1435 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1437 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1439 edit
->mark2
= ac
- MARK_2
;
1440 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1442 /* more than one pop usually means something big */
1444 edit
->force
|= REDRAW_PAGE
;
1447 if (edit
->start_display
> ac
- KEY_PRESS
)
1449 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1450 edit
->force
|= REDRAW_PAGE
;
1452 else if (edit
->start_display
< ac
- KEY_PRESS
)
1454 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1455 edit
->force
|= REDRAW_PAGE
;
1457 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1458 edit_update_curs_row (edit
);
1464 edit_group_undo (WEdit
* edit
)
1466 long ac
= KEY_PRESS
;
1467 long cur_ac
= KEY_PRESS
;
1468 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1470 cur_ac
= get_prev_undo_action (edit
);
1471 edit_do_undo (edit
);
1472 ac
= get_prev_undo_action (edit
);
1473 /* exit from cycle if option_group_undo is not set,
1474 * and make single UNDO operation
1476 if (!option_group_undo
)
1481 /* --------------------------------------------------------------------------------------------- */
1484 edit_delete_to_line_end (WEdit
* edit
)
1486 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
1490 edit_delete (edit
, 1);
1494 /* --------------------------------------------------------------------------------------------- */
1497 edit_delete_to_line_begin (WEdit
* edit
)
1499 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
1503 edit_backspace (edit
, 1);
1507 /* --------------------------------------------------------------------------------------------- */
1510 is_aligned_on_a_tab (WEdit
* edit
)
1512 edit_update_curs_col (edit
);
1513 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
1514 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
1517 /* --------------------------------------------------------------------------------------------- */
1520 right_of_four_spaces (WEdit
* edit
)
1523 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1524 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1526 return is_aligned_on_a_tab (edit
);
1530 /* --------------------------------------------------------------------------------------------- */
1533 left_of_four_spaces (WEdit
* edit
)
1536 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1537 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1539 return is_aligned_on_a_tab (edit
);
1543 /* --------------------------------------------------------------------------------------------- */
1546 edit_auto_indent (WEdit
* edit
)
1551 /* use the previous line as a template */
1552 p
= edit_move_backward (edit
, p
, 1);
1553 /* copy the leading whitespace of the line */
1555 { /* no range check - the line _is_ \n-terminated */
1556 c
= edit_get_byte (edit
, p
++);
1557 if (c
!= ' ' && c
!= '\t')
1559 edit_insert (edit
, c
);
1563 /* --------------------------------------------------------------------------------------------- */
1566 edit_double_newline (WEdit
* edit
)
1568 edit_insert (edit
, '\n');
1569 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1571 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1573 edit
->force
|= REDRAW_PAGE
;
1574 edit_insert (edit
, '\n');
1578 /* --------------------------------------------------------------------------------------------- */
1581 insert_spaces_tab (WEdit
* edit
, gboolean half
)
1585 edit_update_curs_col (edit
);
1586 i
= option_tab_spacing
* space_width
;
1589 i
= ((edit
->curs_col
/ i
) + 1) * i
- edit
->curs_col
;
1592 edit_insert (edit
, ' ');
1597 /* --------------------------------------------------------------------------------------------- */
1600 edit_tab_cmd (WEdit
* edit
)
1604 if (option_fake_half_tabs
)
1606 if (is_in_indent (edit
))
1608 /*insert a half tab (usually four spaces) unless there is a
1609 half tab already behind, then delete it and insert a
1611 if (option_fill_tabs_with_spaces
|| !right_of_four_spaces (edit
))
1612 insert_spaces_tab (edit
, TRUE
);
1615 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1616 edit_backspace (edit
, 1);
1617 edit_insert (edit
, '\t');
1622 if (option_fill_tabs_with_spaces
)
1623 insert_spaces_tab (edit
, FALSE
);
1625 edit_insert (edit
, '\t');
1628 /* --------------------------------------------------------------------------------------------- */
1631 check_and_wrap_line (WEdit
* edit
)
1634 if (!option_typewriter_wrap
)
1636 edit_update_curs_col (edit
);
1637 if (edit
->curs_col
< option_word_wrap_line_length
)
1643 c
= edit_get_byte (edit
, curs
);
1644 if (c
== '\n' || curs
<= 0)
1646 edit_insert (edit
, '\n');
1649 if (c
== ' ' || c
== '\t')
1651 int current
= edit
->curs1
;
1652 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
1653 edit_insert (edit
, '\n');
1654 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
1660 /* --------------------------------------------------------------------------------------------- */
1661 /** this find the matching bracket in either direction, and sets edit->bracket */
1664 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
1666 const char *const b
= "{}{[][()(", *p
;
1667 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
1668 unsigned long j
= 0;
1670 edit_update_curs_row (edit
);
1671 c
= edit_get_byte (edit
, edit
->curs1
);
1674 if (!furthest_bracket_search
)
1675 furthest_bracket_search
--;
1676 /* not on a bracket at all */
1679 /* the matching bracket */
1681 /* going left or right? */
1682 if (strchr ("{[(", c
))
1684 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
1686 /* out of buffer? */
1687 if (q
>= edit
->last_byte
|| q
< 0)
1689 a
= edit_get_byte (edit
, q
);
1690 /* don't want to eat CPU */
1691 if (j
++ > furthest_bracket_search
)
1693 /* out of screen? */
1696 if (q
< edit
->start_display
)
1698 /* count lines if searching downward */
1699 if (inc
> 0 && a
== '\n')
1700 if (n
++ >= edit
->widget
.lines
- edit
->curs_row
) /* out of screen */
1703 /* count bracket depth */
1704 i
+= (a
== c
) - (a
== d
);
1705 /* return if bracket depth is zero */
1713 /* --------------------------------------------------------------------------------------------- */
1716 edit_goto_matching_bracket (WEdit
* edit
)
1720 q
= edit_get_bracket (edit
, 0, 0);
1723 edit
->bracket
= edit
->curs1
;
1724 edit
->force
|= REDRAW_PAGE
;
1725 edit_cursor_move (edit
, q
- edit
->curs1
);
1729 /* --------------------------------------------------------------------------------------------- */
1732 edit_move_block_to_right (WEdit
* edit
)
1734 long start_mark
, end_mark
;
1735 long cur_bol
, start_bol
;
1737 if (eval_marks (edit
, &start_mark
, &end_mark
))
1740 start_bol
= edit_bol (edit
, start_mark
);
1741 cur_bol
= edit_bol (edit
, end_mark
- 1);
1745 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1746 if (option_fill_tabs_with_spaces
)
1747 insert_spaces_tab (edit
, option_fake_half_tabs
);
1749 edit_insert (edit
, '\t');
1750 edit_cursor_move (edit
, edit_bol (edit
, cur_bol
) - edit
->curs1
);
1755 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1757 while (cur_bol
>= start_bol
);
1759 edit
->force
|= REDRAW_PAGE
;
1762 /* --------------------------------------------------------------------------------------------- */
1765 edit_move_block_to_left (WEdit
* edit
)
1767 long start_mark
, end_mark
;
1768 long cur_bol
, start_bol
;
1771 if (eval_marks (edit
, &start_mark
, &end_mark
))
1774 start_bol
= edit_bol (edit
, start_mark
);
1775 cur_bol
= edit_bol (edit
, end_mark
- 1);
1782 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1784 if (option_fake_half_tabs
)
1785 del_tab_width
= HALF_TAB_SIZE
;
1787 del_tab_width
= option_tab_spacing
;
1789 next_char
= edit_get_byte (edit
, edit
->curs1
);
1790 if (next_char
== '\t')
1791 edit_delete (edit
, 1);
1792 else if (next_char
== ' ')
1793 for (i
= 1; i
<= del_tab_width
; i
++)
1795 if (next_char
== ' ')
1796 edit_delete (edit
, 1);
1797 next_char
= edit_get_byte (edit
, edit
->curs1
);
1803 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1805 while (cur_bol
>= start_bol
);
1807 edit
->force
|= REDRAW_PAGE
;
1810 /* --------------------------------------------------------------------------------------------- */
1812 * prints at the cursor
1813 * @returns the number of chars printed
1817 edit_print_string (WEdit
* e
, const char *s
)
1821 while (s
[i
] != '\0')
1822 edit_execute_cmd (e
, CK_InsertChar
, (unsigned char) s
[i
++]);
1823 e
->force
|= REDRAW_COMPLETELY
;
1824 edit_update_screen (e
);
1828 /* --------------------------------------------------------------------------------------------- */
1829 /*** public functions ****************************************************************************/
1830 /* --------------------------------------------------------------------------------------------- */
1832 /** User edit menu, like user menu (F2) but only in editor. */
1835 user_menu (WEdit
* edit
, const char *menu_file
, int selected_entry
)
1840 long start_mark
, end_mark
;
1842 vfs_path_t
*block_file_vpath
;
1844 block_file
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
1845 block_file_vpath
= vfs_path_from_str (block_file
);
1847 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
1849 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1851 /* run shell scripts from menu */
1852 if (user_menu_cmd (edit
, menu_file
, selected_entry
)
1853 && (mc_stat (block_file_vpath
, &status
) == 0) && (status
.st_size
!= 0))
1858 /* i.e. we have marked block */
1860 rc
= edit_block_delete_cmd (edit
);
1866 ins_len
= edit_insert_file (edit
, block_file
);
1867 if (nomark
== 0 && ins_len
> 0)
1868 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1870 /* truncate block file */
1871 fd
= fopen (block_file
, "w");
1875 edit_cursor_move (edit
, curs
- edit
->curs1
);
1876 edit_refresh_cmd (edit
);
1877 edit
->force
|= REDRAW_COMPLETELY
;
1879 g_free (block_file
);
1880 vfs_path_free (block_file_vpath
);
1883 /* --------------------------------------------------------------------------------------------- */
1886 edit_get_byte (WEdit
* edit
, long byte_index
)
1889 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1892 if (byte_index
>= edit
->curs1
)
1894 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1895 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
1899 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
1903 /* --------------------------------------------------------------------------------------------- */
1906 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1911 gchar
*next_ch
= NULL
;
1913 gchar utf8_buf
[UTF8_CHAR_LEN
+ 1];
1915 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1921 str
= edit_get_byte_ptr (edit
, byte_index
);
1929 res
= g_utf8_get_char_validated (str
, -1);
1933 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1935 for (i
= 0; i
< UTF8_CHAR_LEN
; i
++)
1936 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
);
1937 utf8_buf
[UTF8_CHAR_LEN
] = '\0';
1939 res
= g_utf8_get_char_validated (str
, -1);
1950 /* Calculate UTF-8 char width */
1951 next_ch
= g_utf8_next_char (str
);
1954 width
= next_ch
- str
;
1962 *char_width
= width
;
1966 /* --------------------------------------------------------------------------------------------- */
1969 edit_get_write_filter (const vfs_path_t
* write_name_vpath
, const char *filename
)
1972 char *p
, *writename
;
1973 vfs_path_element_t
*path_element
;
1975 i
= edit_find_filter (filename
);
1979 path_element
= vfs_path_get_by_index (write_name_vpath
, -1);
1980 writename
= name_quote (path_element
->path
, 0);
1981 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
1986 /* --------------------------------------------------------------------------------------------- */
1989 edit_write_stream (WEdit
* edit
, FILE * f
)
1993 if (edit
->lb
== LB_ASIS
)
1995 for (i
= 0; i
< edit
->last_byte
; i
++)
1996 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
2001 /* change line breaks */
2002 for (i
= 0; i
< edit
->last_byte
; i
++)
2004 unsigned char c
= edit_get_byte (edit
, i
);
2006 if (!(c
== '\n' || c
== '\r'))
2008 /* not line break */
2009 if (fputc (c
, f
) < 0)
2013 { /* (c == '\n' || c == '\r') */
2014 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
2018 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
2019 /* put one line break unconditionally */
2020 if (fputc ('\n', f
) < 0)
2023 i
++; /* 2 chars are processed */
2025 if (c
== '\r' && c1
== '\n')
2026 /* Windows line break; go to the next char */
2029 if (c
== '\r' && c1
== '\r')
2031 /* two Macintosh line breaks; put second line break */
2032 if (fputc ('\n', f
) < 0)
2037 if (fputc (c1
, f
) < 0)
2041 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
2042 /* put one line break unconditionally */
2043 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
2046 if (c
== '\r' && c1
== '\n')
2047 /* Windows line break; go to the next char */
2051 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
2052 /* put one line break unconditionally */
2053 if (fputc ('\r', f
) < 0)
2056 i
++; /* 2 chars are processed */
2058 if (c
== '\r' && c1
== '\n')
2059 /* Windows line break; go to the next char */
2062 if (c
== '\n' && c1
== '\n')
2064 /* two Windows line breaks; put second line break */
2065 if (fputc ('\r', f
) < 0)
2070 if (fputc (c1
, f
) < 0)
2073 case LB_ASIS
: /* default without changes */
2079 return edit
->last_byte
;
2082 /* --------------------------------------------------------------------------------------------- */
2083 /** inserts a file at the cursor, returns count of inserted bytes on success */
2085 edit_insert_file (WEdit
* edit
, const char *filename
)
2091 vpath
= vfs_path_from_str (filename
);
2092 p
= edit_get_filter (filename
);
2096 long current
= edit
->curs1
;
2098 f
= (FILE *) popen (p
, "r");
2101 edit_insert_stream (edit
, f
);
2102 ins_len
= edit
->curs1
- current
;
2103 edit_cursor_move (edit
, -ins_len
);
2108 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
2109 edit_error_dialog (_("Error"), errmsg
);
2119 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
2120 edit_error_dialog (_("Error"), errmsg
);
2128 int i
, file
, blocklen
;
2129 long current
= edit
->curs1
;
2130 int vertical_insertion
= 0;
2133 file
= mc_open (vpath
, O_RDONLY
| O_BINARY
);
2139 buf
= g_malloc0 (TEMP_BUF_LEN
);
2140 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2143 /* if contain signature VERTICAL_MAGIC then it vertical block */
2144 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2145 vertical_insertion
= 1;
2147 mc_lseek (file
, 0, SEEK_SET
);
2150 if (vertical_insertion
)
2155 blocklen
= edit_insert_column_of_text_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
2156 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2157 /* highlight inserted text then not persistent blocks */
2158 if (!option_persistent_selections
)
2160 if (!edit
->column_highlight
)
2161 edit_push_undo_action (edit
, COLUMN_OFF
);
2162 edit
->column_highlight
= 1;
2167 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2169 for (i
= 0; i
< blocklen
; i
++)
2170 edit_insert (edit
, buf
[i
]);
2172 /* highlight inserted text then not persistent blocks */
2173 if (!option_persistent_selections
&& edit
->modified
)
2175 edit_set_markers (edit
, edit
->curs1
, current
, 0, 0);
2176 if (edit
->column_highlight
)
2177 edit_push_undo_action (edit
, COLUMN_ON
);
2178 edit
->column_highlight
= 0;
2182 edit
->force
|= REDRAW_PAGE
;
2183 ins_len
= edit
->curs1
- current
;
2184 edit_cursor_move (edit
, -ins_len
);
2193 vfs_path_free (vpath
);
2197 /* --------------------------------------------------------------------------------------------- */
2199 * Fill in the edit structure. Return NULL on failure. Pass edit as
2200 * NULL to allocate a new structure.
2202 * If line is 0, try to restore saved position. Otherwise put the
2203 * cursor on that line and show it in the middle of the screen.
2207 edit_init (WEdit
* edit
, int y
, int x
, int lines
, int cols
, const char *filename
, long line
)
2209 gboolean to_free
= FALSE
;
2211 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2212 if (option_line_state
)
2213 option_line_state_width
= LINE_STATE_WIDTH
;
2215 option_line_state_width
= 0;
2221 * Expand option_whole_chars_search by national letters using
2225 static char option_whole_chars_search_buf
[256];
2227 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
2230 size_t len
= str_term_width1 (option_whole_chars_search
);
2232 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
2234 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
2236 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
2238 option_whole_chars_search_buf
[len
++] = i
;
2242 option_whole_chars_search_buf
[len
] = 0;
2243 option_whole_chars_search
= option_whole_chars_search_buf
;
2245 #endif /* ENABLE_NLS */
2246 edit
= g_malloc0 (sizeof (WEdit
));
2247 edit
->search
= NULL
;
2251 edit_purge_widget (edit
);
2254 edit
->widget
.lines
= lines
;
2255 edit
->widget
.cols
= cols
;
2257 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2258 edit
->stat1
.st_uid
= getuid ();
2259 edit
->stat1
.st_gid
= getgid ();
2260 edit
->stat1
.st_mtime
= 0;
2264 edit
->force
|= REDRAW_PAGE
;
2265 edit_set_filename (edit
, filename
);
2267 edit
->undo_stack_size
= START_STACK_SIZE
;
2268 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2269 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2271 edit
->redo_stack_size
= START_STACK_SIZE
;
2272 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2273 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2276 edit
->converter
= str_cnv_from_term
;
2277 edit_set_codeset (edit
);
2279 if (edit_load_file (edit
))
2281 /* edit_load_file already gives an error message */
2287 edit
->loading_done
= 1;
2290 edit_load_syntax (edit
, NULL
, NULL
);
2293 edit_get_syntax_color (edit
, -1, &color
);
2296 /* load saved cursor position */
2297 if ((line
== 0) && option_save_position
)
2298 edit_load_position (edit
);
2303 edit_move_display (edit
, line
- 1);
2304 edit_move_to_line (edit
, line
- 1);
2307 edit_load_macro_cmd (edit
);
2311 /* --------------------------------------------------------------------------------------------- */
2312 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2315 edit_clean (WEdit
* edit
)
2322 /* a stale lock, remove it */
2324 edit
->locked
= edit_unlock_file (edit
);
2326 /* save cursor position */
2327 if (option_save_position
)
2328 edit_save_position (edit
);
2329 else if (edit
->serialized_bookmarks
!= NULL
)
2330 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2332 /* File specified on the mcedit command line and never saved */
2333 if (edit
->delete_file
)
2334 unlink (edit
->filename
);
2336 edit_free_syntax_rules (edit
);
2337 book_mark_flush (edit
, -1);
2338 for (; j
<= MAXBUFF
; j
++)
2340 g_free (edit
->buffers1
[j
]);
2341 g_free (edit
->buffers2
[j
]);
2344 g_free (edit
->undo_stack
);
2345 g_free (edit
->redo_stack
);
2346 g_free (edit
->filename
);
2348 mc_search_free (edit
->search
);
2349 edit
->search
= NULL
;
2351 if (edit
->converter
!= str_cnv_from_term
)
2352 str_close_conv (edit
->converter
);
2354 edit_purge_widget (edit
);
2359 /* --------------------------------------------------------------------------------------------- */
2360 /** returns 1 on success */
2363 edit_renew (WEdit
* edit
)
2365 int y
= edit
->widget
.y
;
2366 int x
= edit
->widget
.x
;
2367 int lines
= edit
->widget
.lines
;
2368 int columns
= edit
->widget
.cols
;
2371 return (edit_init (edit
, y
, x
, lines
, columns
, "", 0) != NULL
);
2374 /* --------------------------------------------------------------------------------------------- */
2376 * Load a new file into the editor. If it fails, preserve the old file.
2377 * To do it, allocate a new widget, initialize it and, if the new file
2378 * was loaded, copy the data to the old widget.
2379 * Return 1 on success, 0 on failure.
2383 edit_reload (WEdit
* edit
, const char *filename
)
2386 int y
= edit
->widget
.y
;
2387 int x
= edit
->widget
.x
;
2388 int lines
= edit
->widget
.lines
;
2389 int columns
= edit
->widget
.cols
;
2391 e
= g_malloc0 (sizeof (WEdit
));
2392 e
->widget
= edit
->widget
;
2393 if (edit_init (e
, y
, x
, lines
, columns
, filename
, 0) == NULL
)
2399 memcpy (edit
, e
, sizeof (WEdit
));
2404 /* --------------------------------------------------------------------------------------------- */
2406 * Load a new file into the editor and set line. If it fails, preserve the old file.
2407 * To do it, allocate a new widget, initialize it and, if the new file
2408 * was loaded, copy the data to the old widget.
2409 * Return 1 on success, 0 on failure.
2413 edit_reload_line (WEdit
* edit
, const char *filename
, long line
)
2416 int y
= edit
->widget
.y
;
2417 int x
= edit
->widget
.x
;
2418 int lines
= edit
->widget
.lines
;
2419 int columns
= edit
->widget
.cols
;
2421 e
= g_malloc0 (sizeof (WEdit
));
2422 e
->widget
= edit
->widget
;
2423 if (edit_init (e
, y
, x
, lines
, columns
, filename
, line
) == NULL
)
2429 memcpy (edit
, e
, sizeof (WEdit
));
2434 /* --------------------------------------------------------------------------------------------- */
2437 edit_set_codeset (WEdit
* edit
)
2443 get_codepage_id (mc_global
.source_codepage
>=
2444 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2449 conv
= str_crt_conv_from (cp_id
);
2450 if (conv
!= INVALID_CONV
)
2452 if (edit
->converter
!= str_cnv_from_term
)
2453 str_close_conv (edit
->converter
);
2454 edit
->converter
= conv
;
2459 edit
->utf8
= str_isutf8 (cp_id
);
2466 /* --------------------------------------------------------------------------------------------- */
2468 Recording stack for undo:
2469 The following is an implementation of a compressed stack. Identical
2470 pushes are recorded by a negative prefix indicating the number of times the
2471 same char was pushed. This saves space for repeated curs-left or curs-right
2488 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2489 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2490 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2491 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2494 The only way the cursor moves or the buffer is changed is through the routines:
2495 insert, backspace, insert_ahead, delete, and cursor_move.
2496 These record the reverse undo movements onto the stack each time they are
2499 Each key press results in a set of actions (insert; delete ...). So each time
2500 a key is pressed the current position of start_display is pushed as
2501 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2502 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2503 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2508 edit_push_undo_action (WEdit
* edit
, long c
, ...)
2510 unsigned long sp
= edit
->undo_stack_pointer
;
2514 /* first enlarge the stack if necessary */
2515 if (sp
> edit
->undo_stack_size
- 10)
2517 if (option_max_undo
< 256)
2518 option_max_undo
= 256;
2519 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2521 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2524 edit
->undo_stack
= t
;
2525 edit
->undo_stack_size
<<= 1;
2526 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2530 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2531 if (edit
->undo_stack_disable
)
2533 edit_push_redo_action (edit
, KEY_PRESS
);
2534 edit_push_redo_action (edit
, c
);
2537 else if (edit
->redo_stack_reset
)
2539 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2542 if (edit
->undo_stack_bottom
!= sp
2543 && spm1
!= edit
->undo_stack_bottom
2544 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2547 if (edit
->undo_stack
[spm1
] < 0)
2549 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2552 if (edit
->undo_stack
[spm1
] > -1000000000)
2554 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2556 edit
->undo_stack
[spm1
]--;
2564 d
= edit
->undo_stack
[spm1
];
2568 return; /* --> no need to push multiple do-nothings */
2569 edit
->undo_stack
[sp
] = -2;
2574 edit
->undo_stack
[sp
] = c
;
2577 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2579 /* if the sp wraps round and catches the undo_stack_bottom then erase
2580 * the first set of actions on the stack to make space - by moving
2581 * undo_stack_bottom forward one "key press" */
2582 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2583 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2584 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2587 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2589 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2590 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2592 /*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: */
2593 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2594 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2596 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2601 edit_push_redo_action (WEdit
* edit
, long c
, ...)
2603 unsigned long sp
= edit
->redo_stack_pointer
;
2606 /* first enlarge the stack if necessary */
2607 if (sp
> edit
->redo_stack_size
- 10)
2609 if (option_max_undo
< 256)
2610 option_max_undo
= 256;
2611 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2613 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2616 edit
->redo_stack
= t
;
2617 edit
->redo_stack_size
<<= 1;
2618 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2622 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2624 if (edit
->redo_stack_bottom
!= sp
2625 && spm1
!= edit
->redo_stack_bottom
2626 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2629 if (edit
->redo_stack
[spm1
] < 0)
2631 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2634 if (edit
->redo_stack
[spm1
] > -1000000000)
2636 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2637 edit
->redo_stack
[spm1
]--;
2644 d
= edit
->redo_stack
[spm1
];
2648 return; /* --> no need to push multiple do-nothings */
2649 edit
->redo_stack
[sp
] = -2;
2650 goto redo_check_bottom
;
2654 edit
->redo_stack
[sp
] = c
;
2657 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2659 /* if the sp wraps round and catches the redo_stack_bottom then erase
2660 * the first set of actions on the stack to make space - by moving
2661 * redo_stack_bottom forward one "key press" */
2662 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2663 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2664 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2667 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2669 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2670 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2673 * If a single key produced enough pushes to wrap all the way round then
2674 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2675 * The stack is then initialised:
2678 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2679 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2680 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2684 /* --------------------------------------------------------------------------------------------- */
2686 Basic low level single character buffer alterations and movements at the cursor.
2687 Returns char passed over, inserted or removed.
2691 edit_insert (WEdit
* edit
, int c
)
2693 /* check if file has grown to large */
2694 if (edit
->last_byte
>= SIZE_LIMIT
)
2697 /* first we must update the position of the display window */
2698 if (edit
->curs1
< edit
->start_display
)
2700 edit
->start_display
++;
2705 /* Mark file as modified, unless the file hasn't been fully loaded */
2706 if (edit
->loading_done
)
2708 edit_modification (edit
);
2711 /* now we must update some info on the file and check if a redraw is required */
2714 if (edit
->book_mark
)
2715 book_mark_inc (edit
, edit
->curs_line
);
2717 edit
->total_lines
++;
2718 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2721 /* save the reverse command onto the undo stack */
2722 /* ordinary char and not space */
2724 edit_push_undo_action (edit
, BACKSPACE
);
2726 edit_push_undo_action (edit
, BACKSPACE_BR
);
2727 /* update markers */
2728 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2729 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2730 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2732 /* add a new buffer if we've reached the end of the last one */
2733 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2734 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2736 /* perform the insertion */
2737 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2738 = (unsigned char) c
;
2740 /* update file length */
2743 /* update cursor position */
2747 /* --------------------------------------------------------------------------------------------- */
2748 /** same as edit_insert and move left */
2751 edit_insert_ahead (WEdit
* edit
, int c
)
2753 if (edit
->last_byte
>= SIZE_LIMIT
)
2756 if (edit
->curs1
< edit
->start_display
)
2758 edit
->start_display
++;
2762 edit_modification (edit
);
2765 if (edit
->book_mark
)
2766 book_mark_inc (edit
, edit
->curs_line
);
2767 edit
->total_lines
++;
2768 edit
->force
|= REDRAW_AFTER_CURSOR
;
2770 /* ordinary char and not space */
2772 edit_push_undo_action (edit
, DELCHAR
);
2774 edit_push_undo_action (edit
, DELCHAR_BR
);
2776 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2777 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2778 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2780 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2781 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2782 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2783 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2790 /* --------------------------------------------------------------------------------------------- */
2793 edit_delete (WEdit
* edit
, const int byte_delete
)
2803 /* if byte_delete = 1 then delete only one byte not multibyte char */
2804 if (edit
->utf8
&& byte_delete
== 0)
2806 edit_get_utf (edit
, edit
->curs1
, &cw
);
2811 if (edit
->mark2
!= edit
->mark1
)
2812 edit_push_markers (edit
);
2814 for (i
= 1; i
<= cw
; i
++)
2816 if (edit
->mark1
> edit
->curs1
)
2819 edit
->end_mark_curs
--;
2821 if (edit
->mark2
> edit
->curs1
)
2823 if (edit
->last_get_rule
> edit
->curs1
)
2824 edit
->last_get_rule
--;
2826 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2828 1) & M_EDIT_BUF_SIZE
) - 1];
2830 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2832 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2833 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2837 edit_push_undo_action (edit
, p
+ 256);
2840 edit_modification (edit
);
2843 if (edit
->book_mark
)
2844 book_mark_dec (edit
, edit
->curs_line
);
2845 edit
->total_lines
--;
2846 edit
->force
|= REDRAW_AFTER_CURSOR
;
2848 if (edit
->curs1
< edit
->start_display
)
2850 edit
->start_display
--;
2858 /* --------------------------------------------------------------------------------------------- */
2859 /** moves the cursor right or left: increment positive or negative respectively */
2862 edit_cursor_move (WEdit
* edit
, long increment
)
2864 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2869 for (; increment
< 0; increment
++)
2874 edit_push_undo_action (edit
, CURS_RIGHT
);
2876 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2877 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2878 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2879 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2880 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2882 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2883 1) & M_EDIT_BUF_SIZE
];
2884 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2886 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2887 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2893 edit
->force
|= REDRAW_LINE_BELOW
;
2898 else if (increment
> 0)
2900 for (; increment
> 0; increment
--)
2905 edit_push_undo_action (edit
, CURS_LEFT
);
2907 c
= edit_get_byte (edit
, edit
->curs1
);
2908 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2909 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2910 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2912 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2914 1) & M_EDIT_BUF_SIZE
) - 1];
2915 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2917 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2918 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2924 edit
->force
|= REDRAW_LINE_ABOVE
;
2930 /* These functions return positions relative to lines */
2932 /* --------------------------------------------------------------------------------------------- */
2933 /** returns index of last char on line + 1 */
2936 edit_eol (WEdit
* edit
, long current
)
2938 if (current
>= edit
->last_byte
)
2939 return edit
->last_byte
;
2942 if (edit_get_byte (edit
, current
) == '\n')
2947 /* --------------------------------------------------------------------------------------------- */
2948 /** returns index of first char on line */
2951 edit_bol (WEdit
* edit
, long current
)
2957 if (edit_get_byte (edit
, current
- 1) == '\n')
2962 /* --------------------------------------------------------------------------------------------- */
2965 edit_count_lines (WEdit
* edit
, long current
, long upto
)
2968 if (upto
> edit
->last_byte
)
2969 upto
= edit
->last_byte
;
2972 while (current
< upto
)
2973 if (edit_get_byte (edit
, current
++) == '\n')
2978 /* --------------------------------------------------------------------------------------------- */
2979 /* If lines is zero this returns the count of lines from current to upto. */
2980 /* If upto is zero returns index of lines forward current. */
2983 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
2987 return edit_count_lines (edit
, current
, upto
);
2996 next
= edit_eol (edit
, current
) + 1;
2997 if (next
> edit
->last_byte
)
3006 /* --------------------------------------------------------------------------------------------- */
3007 /** Returns offset of 'lines' lines up from current */
3010 edit_move_backward (WEdit
* edit
, long current
, long lines
)
3014 current
= edit_bol (edit
, current
);
3015 while ((lines
--) && current
!= 0)
3016 current
= edit_bol (edit
, current
- 1);
3020 /* --------------------------------------------------------------------------------------------- */
3021 /* If cols is zero this returns the count of columns from current to upto. */
3022 /* If upto is zero returns index of cols across from current. */
3025 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
3036 q
= edit
->last_byte
+ 2;
3038 for (col
= 0, p
= current
; p
< q
; p
++)
3050 orig_c
= c
= edit_get_byte (edit
, p
);
3058 utf_ch
= edit_get_utf (edit
, p
, &cw
);
3059 if (mc_global
.utf8_display
)
3063 if (g_unichar_iswide (utf_ch
))
3066 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
3070 c
= convert_to_display_c (c
);
3074 col
+= TAB_SIZE
- col
% TAB_SIZE
;
3082 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!mc_global
.utf8_display
&& !edit
->utf8
)))
3083 /* '\r' is shown as ^M, so we must advance 2 characters */
3084 /* Caret notation for control characters */
3092 /* --------------------------------------------------------------------------------------------- */
3093 /** returns the current column position of the cursor */
3096 edit_get_col (WEdit
* edit
)
3098 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3101 /* --------------------------------------------------------------------------------------------- */
3102 /* Scrolling functions */
3103 /* --------------------------------------------------------------------------------------------- */
3106 edit_update_curs_row (WEdit
* edit
)
3108 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
3111 /* --------------------------------------------------------------------------------------------- */
3114 edit_update_curs_col (WEdit
* edit
)
3116 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3119 /* --------------------------------------------------------------------------------------------- */
3122 edit_get_curs_col (const WEdit
* edit
)
3124 return edit
->curs_col
;
3127 /* --------------------------------------------------------------------------------------------- */
3128 /** moves the display start position up by i lines */
3131 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
3133 unsigned long lines_above
= edit
->start_line
;
3134 if (i
> lines_above
)
3138 edit
->start_line
-= i
;
3139 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
3140 edit
->force
|= REDRAW_PAGE
;
3141 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3143 edit_update_curs_row (edit
);
3147 /* --------------------------------------------------------------------------------------------- */
3148 /** returns 1 if could scroll, 0 otherwise */
3151 edit_scroll_downward (WEdit
* edit
, int i
)
3154 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->widget
.lines
- 1);
3155 if (lines_below
> 0)
3157 if (i
> lines_below
)
3159 edit
->start_line
+= i
;
3160 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
3161 edit
->force
|= REDRAW_PAGE
;
3162 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3164 edit_update_curs_row (edit
);
3167 /* --------------------------------------------------------------------------------------------- */
3170 edit_scroll_right (WEdit
* edit
, int i
)
3172 edit
->force
|= REDRAW_PAGE
;
3173 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3174 edit
->start_col
-= i
;
3177 /* --------------------------------------------------------------------------------------------- */
3180 edit_scroll_left (WEdit
* edit
, int i
)
3182 if (edit
->start_col
)
3184 edit
->start_col
+= i
;
3185 if (edit
->start_col
> 0)
3186 edit
->start_col
= 0;
3187 edit
->force
|= REDRAW_PAGE
;
3188 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3192 /* --------------------------------------------------------------------------------------------- */
3193 /* high level cursor movement commands */
3194 /* --------------------------------------------------------------------------------------------- */
3197 edit_move_to_prev_col (WEdit
* edit
, long p
)
3199 int prev
= edit
->prev_col
;
3200 int over
= edit
->over_col
;
3201 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
3203 if (option_cursor_beyond_eol
)
3205 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
3206 edit_eol (edit
, edit
->curs1
));
3208 if (line_len
< prev
+ edit
->over_col
)
3210 edit
->over_col
= prev
+ over
- line_len
;
3211 edit
->prev_col
= line_len
;
3212 edit
->curs_col
= line_len
;
3216 edit
->curs_col
= prev
+ over
;
3217 edit
->prev_col
= edit
->curs_col
;
3224 if (is_in_indent (edit
) && option_fake_half_tabs
)
3226 edit_update_curs_col (edit
);
3228 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
3230 int q
= edit
->curs_col
;
3231 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
3232 p
= edit_bol (edit
, edit
->curs1
);
3233 edit_cursor_move (edit
,
3234 edit_move_forward3 (edit
, p
, edit
->curs_col
,
3236 if (!left_of_four_spaces (edit
))
3237 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3243 /* --------------------------------------------------------------------------------------------- */
3246 line_is_blank (WEdit
* edit
, long line
)
3248 return is_blank (edit
, edit_find_line (edit
, line
));
3251 /* --------------------------------------------------------------------------------------------- */
3252 /** move cursor to line 'line' */
3255 edit_move_to_line (WEdit
* e
, long line
)
3257 if (line
< e
->curs_line
)
3258 edit_move_up (e
, e
->curs_line
- line
, 0);
3260 edit_move_down (e
, line
- e
->curs_line
, 0);
3261 edit_scroll_screen_over_cursor (e
);
3264 /* --------------------------------------------------------------------------------------------- */
3265 /** scroll window so that first visible line is 'line' */
3268 edit_move_display (WEdit
* e
, long line
)
3270 if (line
< e
->start_line
)
3271 edit_scroll_upward (e
, e
->start_line
- line
);
3273 edit_scroll_downward (e
, line
- e
->start_line
);
3276 /* --------------------------------------------------------------------------------------------- */
3277 /** save markers onto undo stack */
3280 edit_push_markers (WEdit
* edit
)
3282 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3283 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3284 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3287 /* --------------------------------------------------------------------------------------------- */
3290 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
3299 /* --------------------------------------------------------------------------------------------- */
3300 /** highlight marker toggle */
3303 edit_mark_cmd (WEdit
* edit
, int unmark
)
3305 edit_push_markers (edit
);
3308 edit_set_markers (edit
, 0, 0, 0, 0);
3309 edit
->force
|= REDRAW_PAGE
;
3313 if (edit
->mark2
>= 0)
3315 edit
->end_mark_curs
= -1;
3316 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3317 edit
->curs_col
+ edit
->over_col
);
3318 edit
->force
|= REDRAW_PAGE
;
3322 edit
->end_mark_curs
= edit
->curs1
;
3323 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3324 edit
->curs_col
+ edit
->over_col
);
3329 /* --------------------------------------------------------------------------------------------- */
3330 /** highlight the word under cursor */
3333 edit_mark_current_word_cmd (WEdit
* edit
)
3337 for (pos
= edit
->curs1
; pos
!= 0; pos
--)
3341 c1
= edit_get_byte (edit
, pos
);
3342 c2
= edit_get_byte (edit
, pos
- 1);
3343 if (!isspace (c1
) && isspace (c2
))
3345 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3350 for (; pos
< edit
->last_byte
; pos
++)
3354 c1
= edit_get_byte (edit
, pos
);
3355 c2
= edit_get_byte (edit
, pos
+ 1);
3356 if (!isspace (c1
) && isspace (c2
))
3358 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3361 edit
->mark2
= min (pos
+ 1, edit
->last_byte
);
3363 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3366 /* --------------------------------------------------------------------------------------------- */
3369 edit_mark_current_line_cmd (WEdit
* edit
)
3371 long pos
= edit
->curs1
;
3373 edit
->mark1
= edit_bol (edit
, pos
);
3374 edit
->mark2
= edit_eol (edit
, pos
);
3376 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3379 /* --------------------------------------------------------------------------------------------- */
3382 edit_delete_line (WEdit
* edit
)
3385 * Delete right part of the line.
3386 * Note that edit_get_byte() returns '\n' when byte position is
3389 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3391 (void) edit_delete (edit
, 1);
3396 * Note that edit_delete() will not corrupt anything if called while
3397 * cursor position is EOF.
3399 (void) edit_delete (edit
, 1);
3402 * Delete left part of the line.
3403 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3405 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3407 (void) edit_backspace (edit
, 1);
3411 /* --------------------------------------------------------------------------------------------- */
3414 edit_indent_width (WEdit
* edit
, long p
)
3417 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
3419 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
3422 /* --------------------------------------------------------------------------------------------- */
3425 edit_insert_indent (WEdit
* edit
, int indent
)
3427 if (!option_fill_tabs_with_spaces
)
3429 while (indent
>= TAB_SIZE
)
3431 edit_insert (edit
, '\t');
3435 while (indent
-- > 0)
3436 edit_insert (edit
, ' ');
3439 /* --------------------------------------------------------------------------------------------- */
3442 edit_push_key_press (WEdit
* edit
)
3444 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3445 if (edit
->mark2
== -1)
3447 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3448 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3452 /* --------------------------------------------------------------------------------------------- */
3455 edit_find_bracket (WEdit
* edit
)
3457 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3458 if (last_bracket
!= edit
->bracket
)
3459 edit
->force
|= REDRAW_PAGE
;
3460 last_bracket
= edit
->bracket
;
3463 /* --------------------------------------------------------------------------------------------- */
3465 * This executes a command as though the user initiated it through a key
3466 * press. Callback with WIDGET_KEY as a message calls this after
3467 * translating the key press. This function can be used to pass any
3468 * command to the editor. Note that the screen wouldn't update
3469 * automatically. Either of command or char_for_insertion must be
3470 * passed as -1. Commands are executed, and char_for_insertion is
3471 * inserted at the cursor.
3475 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3477 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3479 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3482 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3485 if (macro_index
!= -1)
3487 edit
->force
|= REDRAW_COMPLETELY
;
3488 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3490 edit_store_macro_cmd (edit
);
3494 else if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3496 edit_repeat_macro_cmd (edit
);
3502 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3504 record_macro_buf
[macro_index
].action
= command
;
3505 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3507 /* record the beginning of a set of editing actions initiated by a key press */
3508 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3509 edit_push_key_press (edit
);
3511 edit_execute_cmd (edit
, command
, char_for_insertion
);
3512 if (edit
->column_highlight
)
3513 edit
->force
|= REDRAW_PAGE
;
3516 /* --------------------------------------------------------------------------------------------- */
3518 This executes a command at a lower level than macro recording.
3519 It also does not push a key_press onto the undo stack. This means
3520 that if it is called many times, a single undo command will undo
3521 all of them. It also does not check for the Undo command.
3524 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3526 edit
->force
|= REDRAW_LINE
;
3528 /* The next key press will unhighlight the found string, so update
3530 if (edit
->found_len
|| edit
->column_highlight
)
3531 edit
->force
|= REDRAW_PAGE
;
3535 /* a mark command with shift-arrow */
3538 case CK_MarkToWordBegin
:
3539 case CK_MarkToWordEnd
:
3545 case CK_MarkPageDown
:
3546 case CK_MarkToFileBegin
:
3547 case CK_MarkToFileEnd
:
3548 case CK_MarkToPageBegin
:
3549 case CK_MarkToPageEnd
:
3550 case CK_MarkScrollUp
:
3551 case CK_MarkScrollDown
:
3552 case CK_MarkParagraphUp
:
3553 case CK_MarkParagraphDown
:
3554 /* a mark command with alt-arrow */
3555 case CK_MarkColumnPageUp
:
3556 case CK_MarkColumnPageDown
:
3557 case CK_MarkColumnLeft
:
3558 case CK_MarkColumnRight
:
3559 case CK_MarkColumnUp
:
3560 case CK_MarkColumnDown
:
3561 case CK_MarkColumnScrollUp
:
3562 case CK_MarkColumnScrollDown
:
3563 case CK_MarkColumnParagraphUp
:
3564 case CK_MarkColumnParagraphDown
:
3565 edit
->column_highlight
= 0;
3566 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3568 edit_mark_cmd (edit
, 1); /* clear */
3569 edit_mark_cmd (edit
, 0); /* marking on */
3571 edit
->highlight
= 1;
3574 /* any other command */
3576 if (edit
->highlight
)
3577 edit_mark_cmd (edit
, 0); /* clear */
3578 edit
->highlight
= 0;
3581 /* first check for undo */
3582 if (command
== CK_Undo
)
3584 edit
->redo_stack_reset
= 0;
3585 edit_group_undo (edit
);
3586 edit
->found_len
= 0;
3587 edit
->prev_col
= edit_get_col (edit
);
3588 edit
->search_start
= edit
->curs1
;
3591 /* check for redo */
3592 if (command
== CK_Redo
)
3594 edit
->redo_stack_reset
= 0;
3595 edit_do_redo (edit
);
3596 edit
->found_len
= 0;
3597 edit
->prev_col
= edit_get_col (edit
);
3598 edit
->search_start
= edit
->curs1
;
3602 edit
->redo_stack_reset
= 1;
3604 /* An ordinary key press */
3605 if (char_for_insertion
>= 0)
3607 /* if non persistent selection and text selected */
3608 if (!option_persistent_selections
)
3610 if (edit
->mark1
!= edit
->mark2
)
3611 edit_block_delete_cmd (edit
);
3613 if (edit
->overwrite
)
3615 /* remove char only one time, after input first byte, multibyte chars */
3616 if ((!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3617 && edit_get_byte (edit
, edit
->curs1
) != '\n')
3618 edit_delete (edit
, 0);
3620 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3621 edit_insert_over (edit
);
3623 if (char_for_insertion
> 255 && !mc_global
.utf8_display
)
3625 unsigned char str
[6 + 1];
3629 res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3639 while (str
[i
] != 0 && i
<= 6)
3641 char_for_insertion
= str
[i
];
3642 edit_insert (edit
, char_for_insertion
);
3648 edit_insert (edit
, char_for_insertion
);
3650 if (option_auto_para_formatting
)
3652 format_paragraph (edit
, 0);
3653 edit
->force
|= REDRAW_PAGE
;
3656 check_and_wrap_line (edit
);
3657 edit
->found_len
= 0;
3658 edit
->prev_col
= edit_get_col (edit
);
3659 edit
->search_start
= edit
->curs1
;
3660 edit_find_bracket (edit
);
3666 case CK_TopOnScreen
:
3667 case CK_BottomOnScreen
:
3680 if (edit
->mark2
>= 0)
3682 if (!option_persistent_selections
)
3684 if (edit
->column_highlight
)
3685 edit_push_undo_action (edit
, COLUMN_ON
);
3686 edit
->column_highlight
= 0;
3687 edit_mark_cmd (edit
, 1);
3694 case CK_TopOnScreen
:
3695 case CK_BottomOnScreen
:
3696 case CK_MarkToPageBegin
:
3697 case CK_MarkToPageEnd
:
3702 case CK_MarkToWordBegin
:
3703 case CK_MarkToWordEnd
:
3706 case CK_MarkColumnUp
:
3707 case CK_MarkColumnDown
:
3708 if (edit
->mark2
== -1)
3709 break; /*marking is following the cursor: may need to highlight a whole line */
3714 edit
->force
|= REDRAW_CHAR_ONLY
;
3717 /* basic cursor key commands */
3721 /* if non persistent selection and text selected */
3722 if (!option_persistent_selections
)
3724 if (edit
->mark1
!= edit
->mark2
)
3726 edit_block_delete_cmd (edit
);
3730 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3735 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3737 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3738 edit_backspace (edit
, 1);
3743 if (option_fake_half_tabs
)
3746 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3748 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3749 edit_backspace (edit
, 1);
3754 edit_backspace (edit
, 0);
3757 /* if non persistent selection and text selected */
3758 if (!option_persistent_selections
)
3760 if (edit
->mark1
!= edit
->mark2
)
3762 edit_block_delete_cmd (edit
);
3767 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3768 edit_insert_over (edit
);
3770 if (option_fake_half_tabs
)
3773 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3775 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3776 edit_delete (edit
, 1);
3780 edit_delete (edit
, 0);
3782 case CK_DeleteToWordBegin
:
3784 edit_left_delete_word (edit
);
3786 case CK_DeleteToWordEnd
:
3787 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3788 edit_insert_over (edit
);
3790 edit_right_delete_word (edit
);
3793 edit_delete_line (edit
);
3795 case CK_DeleteToHome
:
3796 edit_delete_to_line_begin (edit
);
3798 case CK_DeleteToEnd
:
3799 edit_delete_to_line_end (edit
);
3803 if (option_auto_para_formatting
)
3805 edit_double_newline (edit
);
3806 if (option_return_does_auto_indent
)
3807 edit_auto_indent (edit
);
3808 format_paragraph (edit
, 0);
3812 edit_insert (edit
, '\n');
3813 if (option_return_does_auto_indent
)
3815 edit_auto_indent (edit
);
3820 edit_insert (edit
, '\n');
3823 case CK_MarkColumnPageUp
:
3824 edit
->column_highlight
= 1;
3827 edit_move_up (edit
, edit
->widget
.lines
- 1, 1);
3829 case CK_MarkColumnPageDown
:
3830 edit
->column_highlight
= 1;
3832 case CK_MarkPageDown
:
3833 edit_move_down (edit
, edit
->widget
.lines
- 1, 1);
3835 case CK_MarkColumnLeft
:
3836 edit
->column_highlight
= 1;
3839 if (option_fake_half_tabs
)
3841 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3843 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3846 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3847 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3851 edit_left_char_move_cmd (edit
);
3853 case CK_MarkColumnRight
:
3854 edit
->column_highlight
= 1;
3857 if (option_fake_half_tabs
)
3859 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3861 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3862 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3866 edit_right_char_move_cmd (edit
);
3868 case CK_TopOnScreen
:
3869 case CK_MarkToPageBegin
:
3870 edit_begin_page (edit
);
3872 case CK_BottomOnScreen
:
3873 case CK_MarkToPageEnd
:
3874 edit_end_page (edit
);
3877 case CK_MarkToWordBegin
:
3879 edit_left_word_move_cmd (edit
);
3882 case CK_MarkToWordEnd
:
3884 edit_right_word_move_cmd (edit
);
3886 case CK_MarkColumnUp
:
3887 edit
->column_highlight
= 1;
3890 edit_move_up (edit
, 1, 0);
3892 case CK_MarkColumnDown
:
3893 edit
->column_highlight
= 1;
3896 edit_move_down (edit
, 1, 0);
3898 case CK_MarkColumnParagraphUp
:
3899 edit
->column_highlight
= 1;
3900 case CK_ParagraphUp
:
3901 case CK_MarkParagraphUp
:
3902 edit_move_up_paragraph (edit
, 0);
3904 case CK_MarkColumnParagraphDown
:
3905 edit
->column_highlight
= 1;
3906 case CK_ParagraphDown
:
3907 case CK_MarkParagraphDown
:
3908 edit_move_down_paragraph (edit
, 0);
3910 case CK_MarkColumnScrollUp
:
3911 edit
->column_highlight
= 1;
3913 case CK_MarkScrollUp
:
3914 edit_move_up (edit
, 1, 1);
3916 case CK_MarkColumnScrollDown
:
3917 edit
->column_highlight
= 1;
3919 case CK_MarkScrollDown
:
3920 edit_move_down (edit
, 1, 1);
3924 edit_cursor_to_bol (edit
);
3928 edit_cursor_to_eol (edit
);
3931 /* if text marked shift block */
3932 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3934 if (edit
->mark2
< 0)
3935 edit_mark_cmd (edit
, 0);
3936 edit_move_block_to_right (edit
);
3940 if (option_cursor_beyond_eol
)
3941 edit_insert_over (edit
);
3942 edit_tab_cmd (edit
);
3943 if (option_auto_para_formatting
)
3945 format_paragraph (edit
, 0);
3946 edit
->force
|= REDRAW_PAGE
;
3950 check_and_wrap_line (edit
);
3955 case CK_InsertOverwrite
:
3956 edit
->overwrite
= !edit
->overwrite
;
3960 if (edit
->mark2
>= 0)
3962 if (edit
->column_highlight
)
3963 edit_push_undo_action (edit
, COLUMN_ON
);
3964 edit
->column_highlight
= 0;
3966 edit_mark_cmd (edit
, 0);
3969 if (!edit
->column_highlight
)
3970 edit_push_undo_action (edit
, COLUMN_OFF
);
3971 edit
->column_highlight
= 1;
3972 edit_mark_cmd (edit
, 0);
3975 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3976 edit
->force
|= REDRAW_PAGE
;
3979 if (edit
->column_highlight
)
3980 edit_push_undo_action (edit
, COLUMN_ON
);
3981 edit
->column_highlight
= 0;
3982 edit_mark_cmd (edit
, 1);
3985 if (edit
->column_highlight
)
3986 edit_push_undo_action (edit
, COLUMN_ON
);
3987 edit
->column_highlight
= 0;
3988 edit_mark_current_word_cmd (edit
);
3991 if (edit
->column_highlight
)
3992 edit_push_undo_action (edit
, COLUMN_ON
);
3993 edit
->column_highlight
= 0;
3994 edit_mark_current_line_cmd (edit
);
3997 case CK_ShowNumbers
:
3998 option_line_state
= !option_line_state
;
3999 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
4000 edit
->force
|= REDRAW_PAGE
;
4004 show_right_margin
= !show_right_margin
;
4005 edit
->force
|= REDRAW_PAGE
;
4009 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
4010 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
4011 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4013 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4015 case CK_BookmarkFlush
:
4016 book_mark_flush (edit
, BOOK_MARK_COLOR
);
4017 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
4018 edit
->force
|= REDRAW_PAGE
;
4020 case CK_BookmarkNext
:
4021 if (edit
->book_mark
)
4023 struct _book_mark
*p
;
4024 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4028 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4029 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4030 edit_move_to_line (edit
, p
->line
);
4034 case CK_BookmarkPrev
:
4035 if (edit
->book_mark
)
4037 struct _book_mark
*p
;
4038 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4039 while (p
->line
== edit
->curs_line
)
4044 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4045 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4046 edit_move_to_line (edit
, p
->line
);
4052 case CK_MarkToFileBegin
:
4053 edit_move_to_top (edit
);
4056 case CK_MarkToFileEnd
:
4057 edit_move_to_bottom (edit
);
4061 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4062 edit_insert_over (edit
);
4063 edit_block_copy_cmd (edit
);
4066 edit_block_delete_cmd (edit
);
4069 edit_block_move_cmd (edit
);
4072 case CK_BlockShiftLeft
:
4073 if (edit
->mark1
!= edit
->mark2
)
4074 edit_move_block_to_left (edit
);
4076 case CK_BlockShiftRight
:
4077 if (edit
->mark1
!= edit
->mark2
)
4078 edit_move_block_to_right (edit
);
4081 edit_copy_to_X_buf_cmd (edit
);
4084 edit_cut_to_X_buf_cmd (edit
);
4087 /* if non persistent selection and text selected */
4088 if (!option_persistent_selections
)
4090 if (edit
->mark1
!= edit
->mark2
)
4091 edit_block_delete_cmd (edit
);
4093 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4094 edit_insert_over (edit
);
4095 edit_paste_from_X_buf_cmd (edit
);
4098 edit_paste_from_history (edit
);
4102 edit_save_as_cmd (edit
);
4105 edit_save_confirm_cmd (edit
);
4108 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
4111 edit_save_block_cmd (edit
);
4114 edit_insert_file_cmd (edit
);
4118 edit_load_back_cmd (edit
);
4121 edit_load_forward_cmd (edit
);
4124 case CK_EditSyntaxFile
:
4125 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
4127 case CK_SyntaxChoose
:
4128 edit_syntax_dialog (edit
);
4131 case CK_EditUserMenu
:
4132 edit_load_cmd (edit
, EDIT_FILE_MENU
);
4135 case CK_SyntaxOnOff
:
4136 option_syntax_highlighting
^= 1;
4137 if (option_syntax_highlighting
== 1)
4138 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
4139 edit
->force
|= REDRAW_PAGE
;
4143 enable_show_tabs_tws
^= 1;
4144 edit
->force
|= REDRAW_PAGE
;
4148 edit_search_cmd (edit
, FALSE
);
4150 case CK_SearchContinue
:
4151 edit_search_cmd (edit
, TRUE
);
4154 edit_replace_cmd (edit
, 0);
4156 case CK_ReplaceContinue
:
4157 edit_replace_cmd (edit
, 1);
4160 /* if text marked shift block */
4161 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
4163 edit_move_block_to_left (edit
);
4167 edit_complete_word_cmd (edit
);
4171 edit_get_match_keyword_cmd (edit
);
4174 dlg_stop (edit
->widget
.owner
);
4177 edit_new_cmd (edit
);
4180 edit_help_cmd (edit
);
4183 edit_refresh_cmd (edit
);
4195 edit_options_dialog (edit
);
4197 case CK_OptionsSaveMode
:
4198 menu_save_mode_cmd ();
4203 /* fool gcc to prevent a Y2K warning */
4204 char time_format
[] = "_c";
4205 time_format
[0] = '%';
4207 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4208 edit_print_string (edit
, s
);
4209 edit
->force
|= REDRAW_PAGE
;
4214 edit_goto_cmd (edit
);
4216 case CK_ParagraphFormat
:
4217 format_paragraph (edit
, 1);
4218 edit
->force
|= REDRAW_PAGE
;
4220 case CK_MacroDelete
:
4221 edit_delete_macro_cmd (edit
);
4223 case CK_MatchBracket
:
4224 edit_goto_matching_bracket (edit
);
4227 user_menu (edit
, NULL
, -1);
4230 edit_sort_cmd (edit
);
4232 case CK_ExternalCommand
:
4233 edit_ext_cmd (edit
);
4236 edit_mail_dialog (edit
);
4242 case CK_SelectCodepage
:
4243 edit_select_codepage_cmd (edit
);
4246 case CK_InsertLiteral
:
4247 edit_insert_literal_cmd (edit
);
4249 case CK_MacroStartStopRecord
:
4250 edit_begin_end_macro_cmd (edit
);
4252 case CK_RepeatStartStopRecord
:
4253 edit_begin_end_repeat_cmd (edit
);
4255 case CK_ExtendedKeyMap
:
4256 edit
->extmod
= TRUE
;
4263 if ((command
/ CK_PipeBlock (0)) == 1)
4264 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
4266 /* keys which must set the col position, and the search vars */
4270 case CK_SearchContinue
:
4272 case CK_ReplaceContinue
:
4274 edit
->prev_col
= edit_get_col (edit
);
4278 case CK_MarkColumnUp
:
4281 case CK_MarkColumnDown
:
4284 case CK_MarkColumnPageUp
:
4286 case CK_MarkPageDown
:
4287 case CK_MarkColumnPageDown
:
4289 case CK_MarkToFileBegin
:
4291 case CK_MarkToFileEnd
:
4292 case CK_ParagraphUp
:
4293 case CK_MarkParagraphUp
:
4294 case CK_MarkColumnParagraphUp
:
4295 case CK_ParagraphDown
:
4296 case CK_MarkParagraphDown
:
4297 case CK_MarkColumnParagraphDown
:
4299 case CK_MarkScrollUp
:
4300 case CK_MarkColumnScrollUp
:
4302 case CK_MarkScrollDown
:
4303 case CK_MarkColumnScrollDown
:
4304 edit
->search_start
= edit
->curs1
;
4305 edit
->found_len
= 0;
4308 edit
->found_len
= 0;
4309 edit
->prev_col
= edit_get_col (edit
);
4310 edit
->search_start
= edit
->curs1
;
4312 edit_find_bracket (edit
);
4314 if (option_auto_para_formatting
)
4320 case CK_DeleteToWordBegin
:
4321 case CK_DeleteToWordEnd
:
4322 case CK_DeleteToHome
:
4323 case CK_DeleteToEnd
:
4324 format_paragraph (edit
, 0);
4325 edit
->force
|= REDRAW_PAGE
;
4330 /* --------------------------------------------------------------------------------------------- */
4333 edit_stack_init (void)
4335 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4337 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
4338 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4341 edit_stack_iterator
= 0;
4344 /* --------------------------------------------------------------------------------------------- */
4347 edit_stack_free (void)
4349 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4350 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
4353 /* --------------------------------------------------------------------------------------------- */
4357 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
4359 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4362 /* --------------------------------------------------------------------------------------------- */
4366 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
4368 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4371 /* --------------------------------------------------------------------------------------------- */
4374 edit_unlock_file (WEdit
* edit
)
4379 fullpath
= mc_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4380 ret
= unlock_file (fullpath
);
4386 /* --------------------------------------------------------------------------------------------- */
4389 edit_lock_file (WEdit
* edit
)
4394 fullpath
= mc_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4395 ret
= lock_file (fullpath
);
4401 /* --------------------------------------------------------------------------------------------- */