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 char *write_name
, const char *filename
)
1972 char *p
, *writename
;
1974 i
= edit_find_filter (filename
);
1978 writename
= name_quote (write_name
, 0);
1979 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
1984 /* --------------------------------------------------------------------------------------------- */
1987 edit_write_stream (WEdit
* edit
, FILE * f
)
1991 if (edit
->lb
== LB_ASIS
)
1993 for (i
= 0; i
< edit
->last_byte
; i
++)
1994 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
1999 /* change line breaks */
2000 for (i
= 0; i
< edit
->last_byte
; i
++)
2002 unsigned char c
= edit_get_byte (edit
, i
);
2004 if (!(c
== '\n' || c
== '\r'))
2006 /* not line break */
2007 if (fputc (c
, f
) < 0)
2011 { /* (c == '\n' || c == '\r') */
2012 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
2016 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
2017 /* put one line break unconditionally */
2018 if (fputc ('\n', f
) < 0)
2021 i
++; /* 2 chars are processed */
2023 if (c
== '\r' && c1
== '\n')
2024 /* Windows line break; go to the next char */
2027 if (c
== '\r' && c1
== '\r')
2029 /* two Macintosh line breaks; put second line break */
2030 if (fputc ('\n', f
) < 0)
2035 if (fputc (c1
, f
) < 0)
2039 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
2040 /* put one line break unconditionally */
2041 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
2044 if (c
== '\r' && c1
== '\n')
2045 /* Windows line break; go to the next char */
2049 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
2050 /* put one line break unconditionally */
2051 if (fputc ('\r', f
) < 0)
2054 i
++; /* 2 chars are processed */
2056 if (c
== '\r' && c1
== '\n')
2057 /* Windows line break; go to the next char */
2060 if (c
== '\n' && c1
== '\n')
2062 /* two Windows line breaks; put second line break */
2063 if (fputc ('\r', f
) < 0)
2068 if (fputc (c1
, f
) < 0)
2071 case LB_ASIS
: /* default without changes */
2077 return edit
->last_byte
;
2080 /* --------------------------------------------------------------------------------------------- */
2081 /** inserts a file at the cursor, returns count of inserted bytes on success */
2083 edit_insert_file (WEdit
* edit
, const char *filename
)
2089 vpath
= vfs_path_from_str (filename
);
2090 p
= edit_get_filter (filename
);
2094 long current
= edit
->curs1
;
2096 f
= (FILE *) popen (p
, "r");
2099 edit_insert_stream (edit
, f
);
2100 ins_len
= edit
->curs1
- current
;
2101 edit_cursor_move (edit
, -ins_len
);
2106 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
2107 edit_error_dialog (_("Error"), errmsg
);
2117 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
2118 edit_error_dialog (_("Error"), errmsg
);
2126 int i
, file
, blocklen
;
2127 long current
= edit
->curs1
;
2128 int vertical_insertion
= 0;
2131 file
= mc_open (vpath
, O_RDONLY
| O_BINARY
);
2137 buf
= g_malloc0 (TEMP_BUF_LEN
);
2138 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2141 /* if contain signature VERTICAL_MAGIC then it vertical block */
2142 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2143 vertical_insertion
= 1;
2145 mc_lseek (file
, 0, SEEK_SET
);
2148 if (vertical_insertion
)
2153 blocklen
= edit_insert_column_of_text_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
2154 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2155 /* highlight inserted text then not persistent blocks */
2156 if (!option_persistent_selections
)
2158 if (!edit
->column_highlight
)
2159 edit_push_undo_action (edit
, COLUMN_OFF
);
2160 edit
->column_highlight
= 1;
2165 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2167 for (i
= 0; i
< blocklen
; i
++)
2168 edit_insert (edit
, buf
[i
]);
2170 /* highlight inserted text then not persistent blocks */
2171 if (!option_persistent_selections
&& edit
->modified
)
2173 edit_set_markers (edit
, edit
->curs1
, current
, 0, 0);
2174 if (edit
->column_highlight
)
2175 edit_push_undo_action (edit
, COLUMN_ON
);
2176 edit
->column_highlight
= 0;
2180 edit
->force
|= REDRAW_PAGE
;
2181 ins_len
= edit
->curs1
- current
;
2182 edit_cursor_move (edit
, -ins_len
);
2191 vfs_path_free (vpath
);
2195 /* --------------------------------------------------------------------------------------------- */
2197 * Fill in the edit structure. Return NULL on failure. Pass edit as
2198 * NULL to allocate a new structure.
2200 * If line is 0, try to restore saved position. Otherwise put the
2201 * cursor on that line and show it in the middle of the screen.
2205 edit_init (WEdit
* edit
, int y
, int x
, int lines
, int cols
, const char *filename
, long line
)
2207 gboolean to_free
= FALSE
;
2209 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2210 if (option_line_state
)
2211 option_line_state_width
= LINE_STATE_WIDTH
;
2213 option_line_state_width
= 0;
2219 * Expand option_whole_chars_search by national letters using
2223 static char option_whole_chars_search_buf
[256];
2225 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
2228 size_t len
= str_term_width1 (option_whole_chars_search
);
2230 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
2232 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
2234 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
2236 option_whole_chars_search_buf
[len
++] = i
;
2240 option_whole_chars_search_buf
[len
] = 0;
2241 option_whole_chars_search
= option_whole_chars_search_buf
;
2243 #endif /* ENABLE_NLS */
2244 edit
= g_malloc0 (sizeof (WEdit
));
2245 edit
->search
= NULL
;
2249 edit_purge_widget (edit
);
2252 edit
->widget
.lines
= lines
;
2253 edit
->widget
.cols
= cols
;
2255 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2256 edit
->stat1
.st_uid
= getuid ();
2257 edit
->stat1
.st_gid
= getgid ();
2258 edit
->stat1
.st_mtime
= 0;
2262 edit
->force
|= REDRAW_PAGE
;
2263 edit_set_filename (edit
, filename
);
2265 edit
->undo_stack_size
= START_STACK_SIZE
;
2266 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2267 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2269 edit
->redo_stack_size
= START_STACK_SIZE
;
2270 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2271 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2274 edit
->converter
= str_cnv_from_term
;
2275 edit_set_codeset (edit
);
2277 if (edit_load_file (edit
))
2279 /* edit_load_file already gives an error message */
2285 edit
->loading_done
= 1;
2288 edit_load_syntax (edit
, NULL
, NULL
);
2291 edit_get_syntax_color (edit
, -1, &color
);
2294 /* load saved cursor position */
2295 if ((line
== 0) && option_save_position
)
2296 edit_load_position (edit
);
2301 edit_move_display (edit
, line
- 1);
2302 edit_move_to_line (edit
, line
- 1);
2305 edit_load_macro_cmd (edit
);
2309 /* --------------------------------------------------------------------------------------------- */
2310 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2313 edit_clean (WEdit
* edit
)
2320 /* a stale lock, remove it */
2322 edit
->locked
= edit_unlock_file (edit
);
2324 /* save cursor position */
2325 if (option_save_position
)
2326 edit_save_position (edit
);
2327 else if (edit
->serialized_bookmarks
!= NULL
)
2328 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2330 /* File specified on the mcedit command line and never saved */
2331 if (edit
->delete_file
)
2332 unlink (edit
->filename
);
2334 edit_free_syntax_rules (edit
);
2335 book_mark_flush (edit
, -1);
2336 for (; j
<= MAXBUFF
; j
++)
2338 g_free (edit
->buffers1
[j
]);
2339 g_free (edit
->buffers2
[j
]);
2342 g_free (edit
->undo_stack
);
2343 g_free (edit
->redo_stack
);
2344 g_free (edit
->filename
);
2346 mc_search_free (edit
->search
);
2347 edit
->search
= NULL
;
2349 if (edit
->converter
!= str_cnv_from_term
)
2350 str_close_conv (edit
->converter
);
2352 edit_purge_widget (edit
);
2357 /* --------------------------------------------------------------------------------------------- */
2358 /** returns 1 on success */
2361 edit_renew (WEdit
* edit
)
2363 int y
= edit
->widget
.y
;
2364 int x
= edit
->widget
.x
;
2365 int lines
= edit
->widget
.lines
;
2366 int columns
= edit
->widget
.cols
;
2369 return (edit_init (edit
, y
, x
, lines
, columns
, "", 0) != NULL
);
2372 /* --------------------------------------------------------------------------------------------- */
2374 * Load a new file into the editor. If it fails, preserve the old file.
2375 * To do it, allocate a new widget, initialize it and, if the new file
2376 * was loaded, copy the data to the old widget.
2377 * Return 1 on success, 0 on failure.
2381 edit_reload (WEdit
* edit
, const char *filename
)
2384 int y
= edit
->widget
.y
;
2385 int x
= edit
->widget
.x
;
2386 int lines
= edit
->widget
.lines
;
2387 int columns
= edit
->widget
.cols
;
2389 e
= g_malloc0 (sizeof (WEdit
));
2390 e
->widget
= edit
->widget
;
2391 if (edit_init (e
, y
, x
, lines
, columns
, filename
, 0) == NULL
)
2397 memcpy (edit
, e
, sizeof (WEdit
));
2402 /* --------------------------------------------------------------------------------------------- */
2404 * Load a new file into the editor and set line. If it fails, preserve the old file.
2405 * To do it, allocate a new widget, initialize it and, if the new file
2406 * was loaded, copy the data to the old widget.
2407 * Return 1 on success, 0 on failure.
2411 edit_reload_line (WEdit
* edit
, const char *filename
, long line
)
2414 int y
= edit
->widget
.y
;
2415 int x
= edit
->widget
.x
;
2416 int lines
= edit
->widget
.lines
;
2417 int columns
= edit
->widget
.cols
;
2419 e
= g_malloc0 (sizeof (WEdit
));
2420 e
->widget
= edit
->widget
;
2421 if (edit_init (e
, y
, x
, lines
, columns
, filename
, line
) == NULL
)
2427 memcpy (edit
, e
, sizeof (WEdit
));
2432 /* --------------------------------------------------------------------------------------------- */
2435 edit_set_codeset (WEdit
* edit
)
2441 get_codepage_id (mc_global
.source_codepage
>=
2442 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2447 conv
= str_crt_conv_from (cp_id
);
2448 if (conv
!= INVALID_CONV
)
2450 if (edit
->converter
!= str_cnv_from_term
)
2451 str_close_conv (edit
->converter
);
2452 edit
->converter
= conv
;
2457 edit
->utf8
= str_isutf8 (cp_id
);
2464 /* --------------------------------------------------------------------------------------------- */
2466 Recording stack for undo:
2467 The following is an implementation of a compressed stack. Identical
2468 pushes are recorded by a negative prefix indicating the number of times the
2469 same char was pushed. This saves space for repeated curs-left or curs-right
2486 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2487 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2488 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2489 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2492 The only way the cursor moves or the buffer is changed is through the routines:
2493 insert, backspace, insert_ahead, delete, and cursor_move.
2494 These record the reverse undo movements onto the stack each time they are
2497 Each key press results in a set of actions (insert; delete ...). So each time
2498 a key is pressed the current position of start_display is pushed as
2499 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2500 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2501 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2506 edit_push_undo_action (WEdit
* edit
, long c
, ...)
2508 unsigned long sp
= edit
->undo_stack_pointer
;
2512 /* first enlarge the stack if necessary */
2513 if (sp
> edit
->undo_stack_size
- 10)
2515 if (option_max_undo
< 256)
2516 option_max_undo
= 256;
2517 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2519 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2522 edit
->undo_stack
= t
;
2523 edit
->undo_stack_size
<<= 1;
2524 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2528 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2529 if (edit
->undo_stack_disable
)
2531 edit_push_redo_action (edit
, KEY_PRESS
);
2532 edit_push_redo_action (edit
, c
);
2535 else if (edit
->redo_stack_reset
)
2537 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2540 if (edit
->undo_stack_bottom
!= sp
2541 && spm1
!= edit
->undo_stack_bottom
2542 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2545 if (edit
->undo_stack
[spm1
] < 0)
2547 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2550 if (edit
->undo_stack
[spm1
] > -1000000000)
2552 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2554 edit
->undo_stack
[spm1
]--;
2562 d
= edit
->undo_stack
[spm1
];
2566 return; /* --> no need to push multiple do-nothings */
2567 edit
->undo_stack
[sp
] = -2;
2572 edit
->undo_stack
[sp
] = c
;
2575 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2577 /* if the sp wraps round and catches the undo_stack_bottom then erase
2578 * the first set of actions on the stack to make space - by moving
2579 * undo_stack_bottom forward one "key press" */
2580 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2581 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2582 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2585 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2587 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2588 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2590 /*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: */
2591 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2592 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2594 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2599 edit_push_redo_action (WEdit
* edit
, long c
, ...)
2601 unsigned long sp
= edit
->redo_stack_pointer
;
2604 /* first enlarge the stack if necessary */
2605 if (sp
> edit
->redo_stack_size
- 10)
2607 if (option_max_undo
< 256)
2608 option_max_undo
= 256;
2609 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2611 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2614 edit
->redo_stack
= t
;
2615 edit
->redo_stack_size
<<= 1;
2616 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2620 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2622 if (edit
->redo_stack_bottom
!= sp
2623 && spm1
!= edit
->redo_stack_bottom
2624 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2627 if (edit
->redo_stack
[spm1
] < 0)
2629 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2632 if (edit
->redo_stack
[spm1
] > -1000000000)
2634 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2635 edit
->redo_stack
[spm1
]--;
2642 d
= edit
->redo_stack
[spm1
];
2646 return; /* --> no need to push multiple do-nothings */
2647 edit
->redo_stack
[sp
] = -2;
2648 goto redo_check_bottom
;
2652 edit
->redo_stack
[sp
] = c
;
2655 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2657 /* if the sp wraps round and catches the redo_stack_bottom then erase
2658 * the first set of actions on the stack to make space - by moving
2659 * redo_stack_bottom forward one "key press" */
2660 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2661 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2662 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2665 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2667 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2668 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2671 * If a single key produced enough pushes to wrap all the way round then
2672 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2673 * The stack is then initialised:
2676 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2677 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2678 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2682 /* --------------------------------------------------------------------------------------------- */
2684 Basic low level single character buffer alterations and movements at the cursor.
2685 Returns char passed over, inserted or removed.
2689 edit_insert (WEdit
* edit
, int c
)
2691 /* check if file has grown to large */
2692 if (edit
->last_byte
>= SIZE_LIMIT
)
2695 /* first we must update the position of the display window */
2696 if (edit
->curs1
< edit
->start_display
)
2698 edit
->start_display
++;
2703 /* Mark file as modified, unless the file hasn't been fully loaded */
2704 if (edit
->loading_done
)
2706 edit_modification (edit
);
2709 /* now we must update some info on the file and check if a redraw is required */
2712 if (edit
->book_mark
)
2713 book_mark_inc (edit
, edit
->curs_line
);
2715 edit
->total_lines
++;
2716 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2719 /* save the reverse command onto the undo stack */
2720 /* ordinary char and not space */
2722 edit_push_undo_action (edit
, BACKSPACE
);
2724 edit_push_undo_action (edit
, BACKSPACE_BR
);
2725 /* update markers */
2726 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2727 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2728 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2730 /* add a new buffer if we've reached the end of the last one */
2731 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2732 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2734 /* perform the insertion */
2735 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2736 = (unsigned char) c
;
2738 /* update file length */
2741 /* update cursor position */
2745 /* --------------------------------------------------------------------------------------------- */
2746 /** same as edit_insert and move left */
2749 edit_insert_ahead (WEdit
* edit
, int c
)
2751 if (edit
->last_byte
>= SIZE_LIMIT
)
2754 if (edit
->curs1
< edit
->start_display
)
2756 edit
->start_display
++;
2760 edit_modification (edit
);
2763 if (edit
->book_mark
)
2764 book_mark_inc (edit
, edit
->curs_line
);
2765 edit
->total_lines
++;
2766 edit
->force
|= REDRAW_AFTER_CURSOR
;
2768 /* ordinary char and not space */
2770 edit_push_undo_action (edit
, DELCHAR
);
2772 edit_push_undo_action (edit
, DELCHAR_BR
);
2774 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2775 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2776 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2778 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2779 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2780 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2781 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2788 /* --------------------------------------------------------------------------------------------- */
2791 edit_delete (WEdit
* edit
, const int byte_delete
)
2801 /* if byte_delete = 1 then delete only one byte not multibyte char */
2802 if (edit
->utf8
&& byte_delete
== 0)
2804 edit_get_utf (edit
, edit
->curs1
, &cw
);
2809 if (edit
->mark2
!= edit
->mark1
)
2810 edit_push_markers (edit
);
2812 for (i
= 1; i
<= cw
; i
++)
2814 if (edit
->mark1
> edit
->curs1
)
2817 edit
->end_mark_curs
--;
2819 if (edit
->mark2
> edit
->curs1
)
2821 if (edit
->last_get_rule
> edit
->curs1
)
2822 edit
->last_get_rule
--;
2824 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2826 1) & M_EDIT_BUF_SIZE
) - 1];
2828 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2830 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2831 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2835 edit_push_undo_action (edit
, p
+ 256);
2838 edit_modification (edit
);
2841 if (edit
->book_mark
)
2842 book_mark_dec (edit
, edit
->curs_line
);
2843 edit
->total_lines
--;
2844 edit
->force
|= REDRAW_AFTER_CURSOR
;
2846 if (edit
->curs1
< edit
->start_display
)
2848 edit
->start_display
--;
2856 /* --------------------------------------------------------------------------------------------- */
2857 /** moves the cursor right or left: increment positive or negative respectively */
2860 edit_cursor_move (WEdit
* edit
, long increment
)
2862 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2867 for (; increment
< 0; increment
++)
2872 edit_push_undo_action (edit
, CURS_RIGHT
);
2874 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2875 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2876 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2877 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2878 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2880 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2881 1) & M_EDIT_BUF_SIZE
];
2882 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2884 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2885 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2891 edit
->force
|= REDRAW_LINE_BELOW
;
2896 else if (increment
> 0)
2898 for (; increment
> 0; increment
--)
2903 edit_push_undo_action (edit
, CURS_LEFT
);
2905 c
= edit_get_byte (edit
, edit
->curs1
);
2906 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2907 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2908 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2910 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2912 1) & M_EDIT_BUF_SIZE
) - 1];
2913 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2915 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2916 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2922 edit
->force
|= REDRAW_LINE_ABOVE
;
2928 /* These functions return positions relative to lines */
2930 /* --------------------------------------------------------------------------------------------- */
2931 /** returns index of last char on line + 1 */
2934 edit_eol (WEdit
* edit
, long current
)
2936 if (current
>= edit
->last_byte
)
2937 return edit
->last_byte
;
2940 if (edit_get_byte (edit
, current
) == '\n')
2945 /* --------------------------------------------------------------------------------------------- */
2946 /** returns index of first char on line */
2949 edit_bol (WEdit
* edit
, long current
)
2955 if (edit_get_byte (edit
, current
- 1) == '\n')
2960 /* --------------------------------------------------------------------------------------------- */
2963 edit_count_lines (WEdit
* edit
, long current
, long upto
)
2966 if (upto
> edit
->last_byte
)
2967 upto
= edit
->last_byte
;
2970 while (current
< upto
)
2971 if (edit_get_byte (edit
, current
++) == '\n')
2976 /* --------------------------------------------------------------------------------------------- */
2977 /* If lines is zero this returns the count of lines from current to upto. */
2978 /* If upto is zero returns index of lines forward current. */
2981 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
2985 return edit_count_lines (edit
, current
, upto
);
2994 next
= edit_eol (edit
, current
) + 1;
2995 if (next
> edit
->last_byte
)
3004 /* --------------------------------------------------------------------------------------------- */
3005 /** Returns offset of 'lines' lines up from current */
3008 edit_move_backward (WEdit
* edit
, long current
, long lines
)
3012 current
= edit_bol (edit
, current
);
3013 while ((lines
--) && current
!= 0)
3014 current
= edit_bol (edit
, current
- 1);
3018 /* --------------------------------------------------------------------------------------------- */
3019 /* If cols is zero this returns the count of columns from current to upto. */
3020 /* If upto is zero returns index of cols across from current. */
3023 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
3034 q
= edit
->last_byte
+ 2;
3036 for (col
= 0, p
= current
; p
< q
; p
++)
3048 orig_c
= c
= edit_get_byte (edit
, p
);
3056 utf_ch
= edit_get_utf (edit
, p
, &cw
);
3057 if (mc_global
.utf8_display
)
3061 if (g_unichar_iswide (utf_ch
))
3064 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
3068 c
= convert_to_display_c (c
);
3072 col
+= TAB_SIZE
- col
% TAB_SIZE
;
3080 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!mc_global
.utf8_display
&& !edit
->utf8
)))
3081 /* '\r' is shown as ^M, so we must advance 2 characters */
3082 /* Caret notation for control characters */
3090 /* --------------------------------------------------------------------------------------------- */
3091 /** returns the current column position of the cursor */
3094 edit_get_col (WEdit
* edit
)
3096 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3099 /* --------------------------------------------------------------------------------------------- */
3100 /* Scrolling functions */
3101 /* --------------------------------------------------------------------------------------------- */
3104 edit_update_curs_row (WEdit
* edit
)
3106 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
3109 /* --------------------------------------------------------------------------------------------- */
3112 edit_update_curs_col (WEdit
* edit
)
3114 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3117 /* --------------------------------------------------------------------------------------------- */
3120 edit_get_curs_col (const WEdit
* edit
)
3122 return edit
->curs_col
;
3125 /* --------------------------------------------------------------------------------------------- */
3126 /** moves the display start position up by i lines */
3129 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
3131 unsigned long lines_above
= edit
->start_line
;
3132 if (i
> lines_above
)
3136 edit
->start_line
-= i
;
3137 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
3138 edit
->force
|= REDRAW_PAGE
;
3139 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3141 edit_update_curs_row (edit
);
3145 /* --------------------------------------------------------------------------------------------- */
3146 /** returns 1 if could scroll, 0 otherwise */
3149 edit_scroll_downward (WEdit
* edit
, int i
)
3152 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->widget
.lines
- 1);
3153 if (lines_below
> 0)
3155 if (i
> lines_below
)
3157 edit
->start_line
+= i
;
3158 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
3159 edit
->force
|= REDRAW_PAGE
;
3160 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3162 edit_update_curs_row (edit
);
3165 /* --------------------------------------------------------------------------------------------- */
3168 edit_scroll_right (WEdit
* edit
, int i
)
3170 edit
->force
|= REDRAW_PAGE
;
3171 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3172 edit
->start_col
-= i
;
3175 /* --------------------------------------------------------------------------------------------- */
3178 edit_scroll_left (WEdit
* edit
, int i
)
3180 if (edit
->start_col
)
3182 edit
->start_col
+= i
;
3183 if (edit
->start_col
> 0)
3184 edit
->start_col
= 0;
3185 edit
->force
|= REDRAW_PAGE
;
3186 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3190 /* --------------------------------------------------------------------------------------------- */
3191 /* high level cursor movement commands */
3192 /* --------------------------------------------------------------------------------------------- */
3195 edit_move_to_prev_col (WEdit
* edit
, long p
)
3197 int prev
= edit
->prev_col
;
3198 int over
= edit
->over_col
;
3199 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
3201 if (option_cursor_beyond_eol
)
3203 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
3204 edit_eol (edit
, edit
->curs1
));
3206 if (line_len
< prev
+ edit
->over_col
)
3208 edit
->over_col
= prev
+ over
- line_len
;
3209 edit
->prev_col
= line_len
;
3210 edit
->curs_col
= line_len
;
3214 edit
->curs_col
= prev
+ over
;
3215 edit
->prev_col
= edit
->curs_col
;
3222 if (is_in_indent (edit
) && option_fake_half_tabs
)
3224 edit_update_curs_col (edit
);
3226 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
3228 int q
= edit
->curs_col
;
3229 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
3230 p
= edit_bol (edit
, edit
->curs1
);
3231 edit_cursor_move (edit
,
3232 edit_move_forward3 (edit
, p
, edit
->curs_col
,
3234 if (!left_of_four_spaces (edit
))
3235 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3241 /* --------------------------------------------------------------------------------------------- */
3244 line_is_blank (WEdit
* edit
, long line
)
3246 return is_blank (edit
, edit_find_line (edit
, line
));
3249 /* --------------------------------------------------------------------------------------------- */
3250 /** move cursor to line 'line' */
3253 edit_move_to_line (WEdit
* e
, long line
)
3255 if (line
< e
->curs_line
)
3256 edit_move_up (e
, e
->curs_line
- line
, 0);
3258 edit_move_down (e
, line
- e
->curs_line
, 0);
3259 edit_scroll_screen_over_cursor (e
);
3262 /* --------------------------------------------------------------------------------------------- */
3263 /** scroll window so that first visible line is 'line' */
3266 edit_move_display (WEdit
* e
, long line
)
3268 if (line
< e
->start_line
)
3269 edit_scroll_upward (e
, e
->start_line
- line
);
3271 edit_scroll_downward (e
, line
- e
->start_line
);
3274 /* --------------------------------------------------------------------------------------------- */
3275 /** save markers onto undo stack */
3278 edit_push_markers (WEdit
* edit
)
3280 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3281 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3282 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3285 /* --------------------------------------------------------------------------------------------- */
3288 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
3297 /* --------------------------------------------------------------------------------------------- */
3298 /** highlight marker toggle */
3301 edit_mark_cmd (WEdit
* edit
, int unmark
)
3303 edit_push_markers (edit
);
3306 edit_set_markers (edit
, 0, 0, 0, 0);
3307 edit
->force
|= REDRAW_PAGE
;
3311 if (edit
->mark2
>= 0)
3313 edit
->end_mark_curs
= -1;
3314 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3315 edit
->curs_col
+ edit
->over_col
);
3316 edit
->force
|= REDRAW_PAGE
;
3320 edit
->end_mark_curs
= edit
->curs1
;
3321 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3322 edit
->curs_col
+ edit
->over_col
);
3327 /* --------------------------------------------------------------------------------------------- */
3328 /** highlight the word under cursor */
3331 edit_mark_current_word_cmd (WEdit
* edit
)
3335 for (pos
= edit
->curs1
; pos
!= 0; pos
--)
3339 c1
= edit_get_byte (edit
, pos
);
3340 c2
= edit_get_byte (edit
, pos
- 1);
3341 if (!isspace (c1
) && isspace (c2
))
3343 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3348 for (; pos
< edit
->last_byte
; pos
++)
3352 c1
= edit_get_byte (edit
, pos
);
3353 c2
= edit_get_byte (edit
, pos
+ 1);
3354 if (!isspace (c1
) && isspace (c2
))
3356 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3359 edit
->mark2
= min (pos
+ 1, edit
->last_byte
);
3361 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3364 /* --------------------------------------------------------------------------------------------- */
3367 edit_mark_current_line_cmd (WEdit
* edit
)
3369 long pos
= edit
->curs1
;
3371 edit
->mark1
= edit_bol (edit
, pos
);
3372 edit
->mark2
= edit_eol (edit
, pos
);
3374 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3377 /* --------------------------------------------------------------------------------------------- */
3380 edit_delete_line (WEdit
* edit
)
3383 * Delete right part of the line.
3384 * Note that edit_get_byte() returns '\n' when byte position is
3387 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3389 (void) edit_delete (edit
, 1);
3394 * Note that edit_delete() will not corrupt anything if called while
3395 * cursor position is EOF.
3397 (void) edit_delete (edit
, 1);
3400 * Delete left part of the line.
3401 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3403 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3405 (void) edit_backspace (edit
, 1);
3409 /* --------------------------------------------------------------------------------------------- */
3412 edit_indent_width (WEdit
* edit
, long p
)
3415 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
3417 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
3420 /* --------------------------------------------------------------------------------------------- */
3423 edit_insert_indent (WEdit
* edit
, int indent
)
3425 if (!option_fill_tabs_with_spaces
)
3427 while (indent
>= TAB_SIZE
)
3429 edit_insert (edit
, '\t');
3433 while (indent
-- > 0)
3434 edit_insert (edit
, ' ');
3437 /* --------------------------------------------------------------------------------------------- */
3440 edit_push_key_press (WEdit
* edit
)
3442 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3443 if (edit
->mark2
== -1)
3445 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3446 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3450 /* --------------------------------------------------------------------------------------------- */
3453 edit_find_bracket (WEdit
* edit
)
3455 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3456 if (last_bracket
!= edit
->bracket
)
3457 edit
->force
|= REDRAW_PAGE
;
3458 last_bracket
= edit
->bracket
;
3461 /* --------------------------------------------------------------------------------------------- */
3463 * This executes a command as though the user initiated it through a key
3464 * press. Callback with WIDGET_KEY as a message calls this after
3465 * translating the key press. This function can be used to pass any
3466 * command to the editor. Note that the screen wouldn't update
3467 * automatically. Either of command or char_for_insertion must be
3468 * passed as -1. Commands are executed, and char_for_insertion is
3469 * inserted at the cursor.
3473 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3475 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3477 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3480 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3483 if (macro_index
!= -1)
3485 edit
->force
|= REDRAW_COMPLETELY
;
3486 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3488 edit_store_macro_cmd (edit
);
3492 else if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3494 edit_repeat_macro_cmd (edit
);
3500 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3502 record_macro_buf
[macro_index
].action
= command
;
3503 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3505 /* record the beginning of a set of editing actions initiated by a key press */
3506 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3507 edit_push_key_press (edit
);
3509 edit_execute_cmd (edit
, command
, char_for_insertion
);
3510 if (edit
->column_highlight
)
3511 edit
->force
|= REDRAW_PAGE
;
3514 /* --------------------------------------------------------------------------------------------- */
3516 This executes a command at a lower level than macro recording.
3517 It also does not push a key_press onto the undo stack. This means
3518 that if it is called many times, a single undo command will undo
3519 all of them. It also does not check for the Undo command.
3522 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3524 edit
->force
|= REDRAW_LINE
;
3526 /* The next key press will unhighlight the found string, so update
3528 if (edit
->found_len
|| edit
->column_highlight
)
3529 edit
->force
|= REDRAW_PAGE
;
3533 /* a mark command with shift-arrow */
3536 case CK_MarkToWordBegin
:
3537 case CK_MarkToWordEnd
:
3543 case CK_MarkPageDown
:
3544 case CK_MarkToFileBegin
:
3545 case CK_MarkToFileEnd
:
3546 case CK_MarkToPageBegin
:
3547 case CK_MarkToPageEnd
:
3548 case CK_MarkScrollUp
:
3549 case CK_MarkScrollDown
:
3550 case CK_MarkParagraphUp
:
3551 case CK_MarkParagraphDown
:
3552 /* a mark command with alt-arrow */
3553 case CK_MarkColumnPageUp
:
3554 case CK_MarkColumnPageDown
:
3555 case CK_MarkColumnLeft
:
3556 case CK_MarkColumnRight
:
3557 case CK_MarkColumnUp
:
3558 case CK_MarkColumnDown
:
3559 case CK_MarkColumnScrollUp
:
3560 case CK_MarkColumnScrollDown
:
3561 case CK_MarkColumnParagraphUp
:
3562 case CK_MarkColumnParagraphDown
:
3563 edit
->column_highlight
= 0;
3564 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3566 edit_mark_cmd (edit
, 1); /* clear */
3567 edit_mark_cmd (edit
, 0); /* marking on */
3569 edit
->highlight
= 1;
3572 /* any other command */
3574 if (edit
->highlight
)
3575 edit_mark_cmd (edit
, 0); /* clear */
3576 edit
->highlight
= 0;
3579 /* first check for undo */
3580 if (command
== CK_Undo
)
3582 edit
->redo_stack_reset
= 0;
3583 edit_group_undo (edit
);
3584 edit
->found_len
= 0;
3585 edit
->prev_col
= edit_get_col (edit
);
3586 edit
->search_start
= edit
->curs1
;
3589 /* check for redo */
3590 if (command
== CK_Redo
)
3592 edit
->redo_stack_reset
= 0;
3593 edit_do_redo (edit
);
3594 edit
->found_len
= 0;
3595 edit
->prev_col
= edit_get_col (edit
);
3596 edit
->search_start
= edit
->curs1
;
3600 edit
->redo_stack_reset
= 1;
3602 /* An ordinary key press */
3603 if (char_for_insertion
>= 0)
3605 /* if non persistent selection and text selected */
3606 if (!option_persistent_selections
)
3608 if (edit
->mark1
!= edit
->mark2
)
3609 edit_block_delete_cmd (edit
);
3611 if (edit
->overwrite
)
3613 /* remove char only one time, after input first byte, multibyte chars */
3614 if ((!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3615 && edit_get_byte (edit
, edit
->curs1
) != '\n')
3616 edit_delete (edit
, 0);
3618 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3619 edit_insert_over (edit
);
3621 if (char_for_insertion
> 255 && !mc_global
.utf8_display
)
3623 unsigned char str
[6 + 1];
3627 res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3637 while (str
[i
] != 0 && i
<= 6)
3639 char_for_insertion
= str
[i
];
3640 edit_insert (edit
, char_for_insertion
);
3646 edit_insert (edit
, char_for_insertion
);
3648 if (option_auto_para_formatting
)
3650 format_paragraph (edit
, 0);
3651 edit
->force
|= REDRAW_PAGE
;
3654 check_and_wrap_line (edit
);
3655 edit
->found_len
= 0;
3656 edit
->prev_col
= edit_get_col (edit
);
3657 edit
->search_start
= edit
->curs1
;
3658 edit_find_bracket (edit
);
3664 case CK_TopOnScreen
:
3665 case CK_BottomOnScreen
:
3678 if (edit
->mark2
>= 0)
3680 if (!option_persistent_selections
)
3682 if (edit
->column_highlight
)
3683 edit_push_undo_action (edit
, COLUMN_ON
);
3684 edit
->column_highlight
= 0;
3685 edit_mark_cmd (edit
, 1);
3692 case CK_TopOnScreen
:
3693 case CK_BottomOnScreen
:
3694 case CK_MarkToPageBegin
:
3695 case CK_MarkToPageEnd
:
3700 case CK_MarkToWordBegin
:
3701 case CK_MarkToWordEnd
:
3704 case CK_MarkColumnUp
:
3705 case CK_MarkColumnDown
:
3706 if (edit
->mark2
== -1)
3707 break; /*marking is following the cursor: may need to highlight a whole line */
3712 edit
->force
|= REDRAW_CHAR_ONLY
;
3715 /* basic cursor key commands */
3719 /* if non persistent selection and text selected */
3720 if (!option_persistent_selections
)
3722 if (edit
->mark1
!= edit
->mark2
)
3724 edit_block_delete_cmd (edit
);
3728 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3733 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3735 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3736 edit_backspace (edit
, 1);
3741 if (option_fake_half_tabs
)
3744 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3746 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3747 edit_backspace (edit
, 1);
3752 edit_backspace (edit
, 0);
3755 /* if non persistent selection and text selected */
3756 if (!option_persistent_selections
)
3758 if (edit
->mark1
!= edit
->mark2
)
3760 edit_block_delete_cmd (edit
);
3765 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3766 edit_insert_over (edit
);
3768 if (option_fake_half_tabs
)
3771 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3773 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3774 edit_delete (edit
, 1);
3778 edit_delete (edit
, 0);
3780 case CK_DeleteToWordBegin
:
3782 edit_left_delete_word (edit
);
3784 case CK_DeleteToWordEnd
:
3785 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3786 edit_insert_over (edit
);
3788 edit_right_delete_word (edit
);
3791 edit_delete_line (edit
);
3793 case CK_DeleteToHome
:
3794 edit_delete_to_line_begin (edit
);
3796 case CK_DeleteToEnd
:
3797 edit_delete_to_line_end (edit
);
3801 if (option_auto_para_formatting
)
3803 edit_double_newline (edit
);
3804 if (option_return_does_auto_indent
)
3805 edit_auto_indent (edit
);
3806 format_paragraph (edit
, 0);
3810 edit_insert (edit
, '\n');
3811 if (option_return_does_auto_indent
)
3813 edit_auto_indent (edit
);
3818 edit_insert (edit
, '\n');
3821 case CK_MarkColumnPageUp
:
3822 edit
->column_highlight
= 1;
3825 edit_move_up (edit
, edit
->widget
.lines
- 1, 1);
3827 case CK_MarkColumnPageDown
:
3828 edit
->column_highlight
= 1;
3830 case CK_MarkPageDown
:
3831 edit_move_down (edit
, edit
->widget
.lines
- 1, 1);
3833 case CK_MarkColumnLeft
:
3834 edit
->column_highlight
= 1;
3837 if (option_fake_half_tabs
)
3839 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3841 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3844 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3845 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3849 edit_left_char_move_cmd (edit
);
3851 case CK_MarkColumnRight
:
3852 edit
->column_highlight
= 1;
3855 if (option_fake_half_tabs
)
3857 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3859 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3860 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3864 edit_right_char_move_cmd (edit
);
3866 case CK_TopOnScreen
:
3867 case CK_MarkToPageBegin
:
3868 edit_begin_page (edit
);
3870 case CK_BottomOnScreen
:
3871 case CK_MarkToPageEnd
:
3872 edit_end_page (edit
);
3875 case CK_MarkToWordBegin
:
3877 edit_left_word_move_cmd (edit
);
3880 case CK_MarkToWordEnd
:
3882 edit_right_word_move_cmd (edit
);
3884 case CK_MarkColumnUp
:
3885 edit
->column_highlight
= 1;
3888 edit_move_up (edit
, 1, 0);
3890 case CK_MarkColumnDown
:
3891 edit
->column_highlight
= 1;
3894 edit_move_down (edit
, 1, 0);
3896 case CK_MarkColumnParagraphUp
:
3897 edit
->column_highlight
= 1;
3898 case CK_ParagraphUp
:
3899 case CK_MarkParagraphUp
:
3900 edit_move_up_paragraph (edit
, 0);
3902 case CK_MarkColumnParagraphDown
:
3903 edit
->column_highlight
= 1;
3904 case CK_ParagraphDown
:
3905 case CK_MarkParagraphDown
:
3906 edit_move_down_paragraph (edit
, 0);
3908 case CK_MarkColumnScrollUp
:
3909 edit
->column_highlight
= 1;
3911 case CK_MarkScrollUp
:
3912 edit_move_up (edit
, 1, 1);
3914 case CK_MarkColumnScrollDown
:
3915 edit
->column_highlight
= 1;
3917 case CK_MarkScrollDown
:
3918 edit_move_down (edit
, 1, 1);
3922 edit_cursor_to_bol (edit
);
3926 edit_cursor_to_eol (edit
);
3929 /* if text marked shift block */
3930 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3932 if (edit
->mark2
< 0)
3933 edit_mark_cmd (edit
, 0);
3934 edit_move_block_to_right (edit
);
3938 if (option_cursor_beyond_eol
)
3939 edit_insert_over (edit
);
3940 edit_tab_cmd (edit
);
3941 if (option_auto_para_formatting
)
3943 format_paragraph (edit
, 0);
3944 edit
->force
|= REDRAW_PAGE
;
3948 check_and_wrap_line (edit
);
3953 case CK_InsertOverwrite
:
3954 edit
->overwrite
= !edit
->overwrite
;
3958 if (edit
->mark2
>= 0)
3960 if (edit
->column_highlight
)
3961 edit_push_undo_action (edit
, COLUMN_ON
);
3962 edit
->column_highlight
= 0;
3964 edit_mark_cmd (edit
, 0);
3967 if (!edit
->column_highlight
)
3968 edit_push_undo_action (edit
, COLUMN_OFF
);
3969 edit
->column_highlight
= 1;
3970 edit_mark_cmd (edit
, 0);
3973 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3974 edit
->force
|= REDRAW_PAGE
;
3977 if (edit
->column_highlight
)
3978 edit_push_undo_action (edit
, COLUMN_ON
);
3979 edit
->column_highlight
= 0;
3980 edit_mark_cmd (edit
, 1);
3983 if (edit
->column_highlight
)
3984 edit_push_undo_action (edit
, COLUMN_ON
);
3985 edit
->column_highlight
= 0;
3986 edit_mark_current_word_cmd (edit
);
3989 if (edit
->column_highlight
)
3990 edit_push_undo_action (edit
, COLUMN_ON
);
3991 edit
->column_highlight
= 0;
3992 edit_mark_current_line_cmd (edit
);
3995 case CK_ShowNumbers
:
3996 option_line_state
= !option_line_state
;
3997 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
3998 edit
->force
|= REDRAW_PAGE
;
4002 show_right_margin
= !show_right_margin
;
4003 edit
->force
|= REDRAW_PAGE
;
4007 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
4008 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
4009 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4011 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4013 case CK_BookmarkFlush
:
4014 book_mark_flush (edit
, BOOK_MARK_COLOR
);
4015 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
4016 edit
->force
|= REDRAW_PAGE
;
4018 case CK_BookmarkNext
:
4019 if (edit
->book_mark
)
4021 struct _book_mark
*p
;
4022 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4026 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4027 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4028 edit_move_to_line (edit
, p
->line
);
4032 case CK_BookmarkPrev
:
4033 if (edit
->book_mark
)
4035 struct _book_mark
*p
;
4036 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4037 while (p
->line
== edit
->curs_line
)
4042 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4043 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4044 edit_move_to_line (edit
, p
->line
);
4050 case CK_MarkToFileBegin
:
4051 edit_move_to_top (edit
);
4054 case CK_MarkToFileEnd
:
4055 edit_move_to_bottom (edit
);
4059 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4060 edit_insert_over (edit
);
4061 edit_block_copy_cmd (edit
);
4064 edit_block_delete_cmd (edit
);
4067 edit_block_move_cmd (edit
);
4070 case CK_BlockShiftLeft
:
4071 if (edit
->mark1
!= edit
->mark2
)
4072 edit_move_block_to_left (edit
);
4074 case CK_BlockShiftRight
:
4075 if (edit
->mark1
!= edit
->mark2
)
4076 edit_move_block_to_right (edit
);
4079 edit_copy_to_X_buf_cmd (edit
);
4082 edit_cut_to_X_buf_cmd (edit
);
4085 /* if non persistent selection and text selected */
4086 if (!option_persistent_selections
)
4088 if (edit
->mark1
!= edit
->mark2
)
4089 edit_block_delete_cmd (edit
);
4091 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4092 edit_insert_over (edit
);
4093 edit_paste_from_X_buf_cmd (edit
);
4096 edit_paste_from_history (edit
);
4100 edit_save_as_cmd (edit
);
4103 edit_save_confirm_cmd (edit
);
4106 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
4109 edit_save_block_cmd (edit
);
4112 edit_insert_file_cmd (edit
);
4116 edit_load_back_cmd (edit
);
4119 edit_load_forward_cmd (edit
);
4122 case CK_EditSyntaxFile
:
4123 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
4125 case CK_SyntaxChoose
:
4126 edit_syntax_dialog (edit
);
4129 case CK_EditUserMenu
:
4130 edit_load_cmd (edit
, EDIT_FILE_MENU
);
4133 case CK_SyntaxOnOff
:
4134 option_syntax_highlighting
^= 1;
4135 if (option_syntax_highlighting
== 1)
4136 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
4137 edit
->force
|= REDRAW_PAGE
;
4141 enable_show_tabs_tws
^= 1;
4142 edit
->force
|= REDRAW_PAGE
;
4146 edit_search_cmd (edit
, FALSE
);
4148 case CK_SearchContinue
:
4149 edit_search_cmd (edit
, TRUE
);
4152 edit_replace_cmd (edit
, 0);
4154 case CK_ReplaceContinue
:
4155 edit_replace_cmd (edit
, 1);
4158 /* if text marked shift block */
4159 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
4161 edit_move_block_to_left (edit
);
4165 edit_complete_word_cmd (edit
);
4169 edit_get_match_keyword_cmd (edit
);
4172 dlg_stop (edit
->widget
.owner
);
4175 edit_new_cmd (edit
);
4178 edit_help_cmd (edit
);
4181 edit_refresh_cmd (edit
);
4193 edit_options_dialog (edit
);
4195 case CK_OptionsSaveMode
:
4196 menu_save_mode_cmd ();
4201 /* fool gcc to prevent a Y2K warning */
4202 char time_format
[] = "_c";
4203 time_format
[0] = '%';
4205 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4206 edit_print_string (edit
, s
);
4207 edit
->force
|= REDRAW_PAGE
;
4212 edit_goto_cmd (edit
);
4214 case CK_ParagraphFormat
:
4215 format_paragraph (edit
, 1);
4216 edit
->force
|= REDRAW_PAGE
;
4218 case CK_MacroDelete
:
4219 edit_delete_macro_cmd (edit
);
4221 case CK_MatchBracket
:
4222 edit_goto_matching_bracket (edit
);
4225 user_menu (edit
, NULL
, -1);
4228 edit_sort_cmd (edit
);
4230 case CK_ExternalCommand
:
4231 edit_ext_cmd (edit
);
4234 edit_mail_dialog (edit
);
4240 case CK_SelectCodepage
:
4241 edit_select_codepage_cmd (edit
);
4244 case CK_InsertLiteral
:
4245 edit_insert_literal_cmd (edit
);
4247 case CK_MacroStartStopRecord
:
4248 edit_begin_end_macro_cmd (edit
);
4250 case CK_RepeatStartStopRecord
:
4251 edit_begin_end_repeat_cmd (edit
);
4253 case CK_ExtendedKeyMap
:
4254 edit
->extmod
= TRUE
;
4261 if ((command
/ CK_PipeBlock (0)) == 1)
4262 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
4264 /* keys which must set the col position, and the search vars */
4268 case CK_SearchContinue
:
4270 case CK_ReplaceContinue
:
4272 edit
->prev_col
= edit_get_col (edit
);
4276 case CK_MarkColumnUp
:
4279 case CK_MarkColumnDown
:
4282 case CK_MarkColumnPageUp
:
4284 case CK_MarkPageDown
:
4285 case CK_MarkColumnPageDown
:
4287 case CK_MarkToFileBegin
:
4289 case CK_MarkToFileEnd
:
4290 case CK_ParagraphUp
:
4291 case CK_MarkParagraphUp
:
4292 case CK_MarkColumnParagraphUp
:
4293 case CK_ParagraphDown
:
4294 case CK_MarkParagraphDown
:
4295 case CK_MarkColumnParagraphDown
:
4297 case CK_MarkScrollUp
:
4298 case CK_MarkColumnScrollUp
:
4300 case CK_MarkScrollDown
:
4301 case CK_MarkColumnScrollDown
:
4302 edit
->search_start
= edit
->curs1
;
4303 edit
->found_len
= 0;
4306 edit
->found_len
= 0;
4307 edit
->prev_col
= edit_get_col (edit
);
4308 edit
->search_start
= edit
->curs1
;
4310 edit_find_bracket (edit
);
4312 if (option_auto_para_formatting
)
4318 case CK_DeleteToWordBegin
:
4319 case CK_DeleteToWordEnd
:
4320 case CK_DeleteToHome
:
4321 case CK_DeleteToEnd
:
4322 format_paragraph (edit
, 0);
4323 edit
->force
|= REDRAW_PAGE
;
4328 /* --------------------------------------------------------------------------------------------- */
4331 edit_stack_init (void)
4333 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4335 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
4336 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4339 edit_stack_iterator
= 0;
4342 /* --------------------------------------------------------------------------------------------- */
4345 edit_stack_free (void)
4347 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4348 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
4351 /* --------------------------------------------------------------------------------------------- */
4355 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
4357 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4360 /* --------------------------------------------------------------------------------------------- */
4364 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
4366 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4369 /* --------------------------------------------------------------------------------------------- */
4372 edit_unlock_file (WEdit
* edit
)
4377 fullpath
= mc_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4378 ret
= unlock_file (fullpath
);
4384 /* --------------------------------------------------------------------------------------------- */
4387 edit_lock_file (WEdit
* edit
)
4392 fullpath
= mc_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4393 ret
= lock_file (fullpath
);
4399 /* --------------------------------------------------------------------------------------------- */