2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2008, 2009, 2010, 2011, 2012
6 The Free Software Foundation, Inc.
10 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
11 Andrew Borodin <aborodin@vmail.ru> 2012.
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 * \brief Source: editor low level data handling and cursor fundamentals
38 #include <sys/types.h>
47 #include "lib/global.h"
49 #include "lib/tty/color.h"
50 #include "lib/tty/tty.h" /* attrset() */
51 #include "lib/tty/key.h" /* is_idle() */
52 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
53 #include "lib/vfs/vfs.h"
54 #include "lib/strutil.h" /* utf string functions */
55 #include "lib/util.h" /* load_file_position(), save_file_position() */
56 #include "lib/timefmt.h" /* time formatting */
58 #include "lib/widget.h"
61 #include "lib/charsets.h" /* get_codepage_id */
64 #include "src/filemanager/cmd.h" /* view_other_cmd() */
65 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
67 #include "src/setup.h" /* option_tab_spacing */
68 #include "src/main.h" /* macro_index */
69 #include "src/learn.h" /* learn_keys */
70 #include "src/keybind-defaults.h"
72 #include "edit-impl.h"
73 #include "editwidget.h"
75 /*** global variables ****************************************************************************/
77 int option_word_wrap_line_length
= DEFAULT_WRAP_LINE_LENGTH
;
78 int option_typewriter_wrap
= 0;
79 int option_auto_para_formatting
= 0;
80 int option_fill_tabs_with_spaces
= 0;
81 int option_return_does_auto_indent
= 1;
82 int option_backspace_through_tabs
= 0;
83 int option_fake_half_tabs
= 1;
84 int option_save_mode
= EDIT_QUICK_SAVE
;
85 int option_save_position
= 1;
86 int option_max_undo
= 32768;
87 int option_persistent_selections
= 1;
88 int option_cursor_beyond_eol
= 0;
89 int option_line_state
= 0;
90 int option_line_state_width
= 0;
92 int option_edit_right_extreme
= 0;
93 int option_edit_left_extreme
= 0;
94 int option_edit_top_extreme
= 0;
95 int option_edit_bottom_extreme
= 0;
96 int enable_show_tabs_tws
= 1;
97 int option_check_nl_at_eof
= 0;
98 int option_group_undo
= 0;
99 int show_right_margin
= 0;
101 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
102 char *option_backup_ext
= NULL
;
104 unsigned int edit_stack_iterator
= 0;
105 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
106 /* magic sequense for say than block is vertical */
107 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
109 /*** file scope macro definitions ****************************************************************/
111 #define TEMP_BUF_LEN 1024
113 #define space_width 1
115 /*** file scope type declarations ****************************************************************/
117 /*** file scope variables ************************************************************************/
119 /* detecting an error on save is easy: just check if every byte has been written. */
120 /* detecting an error on read, is not so easy 'cos there is not way to tell
121 whether you read everything or not. */
122 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
123 static const struct edit_filters
125 const char *read
, *write
, *extension
;
129 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
130 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
131 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
132 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
133 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
137 static long last_bracket
= -1;
139 /*** file scope functions ************************************************************************/
140 /* --------------------------------------------------------------------------------------------- */
144 * here's a quick sketch of the layout: (don't run this through indent.)
146 * (b1 is buffers1 and b2 is buffers2)
149 * \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
150 * ______________________________________|______________________________________
152 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
153 * |-> |-> |-> |-> |-> |-> |
155 * _<------------------------->|<----------------->_
156 * WEdit->curs2 | WEdit->curs1
161 * file end|||file beginning
170 /* --------------------------------------------------------------------------------------------- */
172 static int left_of_four_spaces (WEdit
* edit
);
174 /* --------------------------------------------------------------------------------------------- */
179 const char *header
= N_("About");
180 const char *button_name
= N_("&OK");
181 const char *const version
= "MCEdit " VERSION
;
182 char text
[BUF_LARGE
];
184 int win_len
, version_len
, button_len
;
191 button_name
= _(button_name
);
194 button_len
= str_term_width1 (button_name
) + 5;
195 version_len
= str_term_width1 (version
);
197 g_snprintf (text
, sizeof (text
),
198 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
199 " A user friendly text editor\n"
200 " written for the Midnight Commander"));
202 win_len
= str_term_width1 (header
);
203 win_len
= max (win_len
, version_len
);
204 win_len
= max (win_len
, button_len
);
206 /* count width and height of text */
207 str_msg_term_size (text
, &lines
, &cols
);
209 cols
= max (win_len
, cols
) + 6;
212 about_dlg
= create_dlg (TRUE
, 0, 0, lines
, cols
, dialog_colors
, NULL
,
213 "[Internal File Editor]", header
, DLG_CENTER
| DLG_TRYUP
);
215 add_widget (about_dlg
, label_new (3, (cols
- version_len
) / 2, version
));
216 add_widget (about_dlg
, label_new (5, 3, text
));
217 add_widget (about_dlg
, button_new (lines
- 3, (cols
- button_len
) / 2,
218 B_ENTER
, NORMAL_BUTTON
, button_name
, NULL
));
221 destroy_dlg (about_dlg
);
224 /* --------------------------------------------------------------------------------------------- */
226 * Initialize the buffers for an empty files.
230 edit_init_buffers (WEdit
* edit
)
234 for (j
= 0; j
<= MAXBUFF
; j
++)
236 edit
->buffers1
[j
] = NULL
;
237 edit
->buffers2
[j
] = NULL
;
242 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
245 /* --------------------------------------------------------------------------------------------- */
248 * Load file OR text into buffers. Set cursor to the beginning of file.
250 * @returns FALSE on error.
254 edit_load_file_fast (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
258 gboolean ret
= FALSE
;
260 edit
->curs2
= edit
->last_byte
;
261 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
263 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
266 gchar
*errmsg
, *filename
;
268 filename
= vfs_path_to_str (filename_vpath
);
269 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
271 edit_error_dialog (_("Error"), errmsg
);
276 if (!edit
->buffers2
[buf2
])
277 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
282 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
283 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
286 for (buf
= buf2
- 1; buf
>= 0; buf
--)
288 /* edit->buffers2[0] is already allocated */
289 if (!edit
->buffers2
[buf
])
290 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
291 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
300 gchar
*errmsg
, *filename
;
302 filename
= vfs_path_to_str (filename_vpath
);
303 errmsg
= g_strdup_printf (_("Error reading %s"), filename
);
305 edit_error_dialog (_("Error"), errmsg
);
312 /* --------------------------------------------------------------------------------------------- */
313 /** Return index of the filter or -1 is there is no appropriate filter */
316 edit_find_filter (const vfs_path_t
* filename_vpath
)
321 if (filename_vpath
== NULL
)
324 filename
= vfs_path_to_str (filename_vpath
);
325 l
= strlen (filename
);
326 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
328 e
= strlen (all_filters
[i
].extension
);
330 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
340 /* --------------------------------------------------------------------------------------------- */
343 edit_get_filter (const vfs_path_t
* filename_vpath
)
346 char *p
, *quoted_name
, *filename
;
348 i
= edit_find_filter (filename_vpath
);
352 filename
= vfs_path_to_str (filename_vpath
);
353 quoted_name
= name_quote (filename
, 0);
355 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
356 g_free (quoted_name
);
360 /* --------------------------------------------------------------------------------------------- */
363 edit_insert_stream (WEdit
* edit
, FILE * f
)
367 while ((c
= fgetc (f
)) >= 0)
369 edit_insert (edit
, c
);
375 /* --------------------------------------------------------------------------------------------- */
377 * Open file and create it if necessary.
379 * @param edit editor object
380 * @param filename_vpath file name
381 * @param st buffer for store stat info
382 * @returns TRUE for success, FALSE for error.
386 check_file_access (WEdit
* edit
, const vfs_path_t
* filename_vpath
, struct stat
*st
)
389 gchar
*errmsg
= NULL
;
391 /* Try opening an existing file */
392 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
396 * Try creating the file. O_EXCL prevents following broken links
397 * and opening existing files.
399 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
404 filename
= vfs_path_to_str (filename_vpath
);
405 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
410 /* New file, delete it if it's not modified or saved */
411 edit
->delete_file
= 1;
414 /* Check what we have opened */
415 if (mc_fstat (file
, st
) < 0)
419 filename
= vfs_path_to_str (filename_vpath
);
420 errmsg
= g_strdup_printf (_("Cannot get size/permissions for %s"), filename
);
425 /* We want to open regular files only */
426 if (!S_ISREG (st
->st_mode
))
430 filename
= vfs_path_to_str (filename_vpath
);
431 errmsg
= g_strdup_printf (_("\"%s\" is not a regular file"), filename
);
437 * Don't delete non-empty files.
438 * O_EXCL should prevent it, but let's be on the safe side.
441 edit
->delete_file
= 0;
443 if (st
->st_size
>= SIZE_LIMIT
)
447 filename
= vfs_path_to_str (filename_vpath
);
448 errmsg
= g_strdup_printf (_("File \"%s\" is too large"), filename
);
453 (void) mc_close (file
);
457 edit_error_dialog (_("Error"), errmsg
);
464 /* --------------------------------------------------------------------------------------------- */
467 * Open the file and load it into the buffers, either directly or using
468 * a filter. Return TRUE on success, FALSE on error.
470 * Fast loading (edit_load_file_fast) is used when the file size is
471 * known. In this case the data is read into the buffers by blocks.
472 * If the file size is not known, the data is loaded byte by byte in
475 * @param edit editor object
476 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
479 edit_load_file (WEdit
* edit
)
481 gboolean fast_load
= TRUE
;
483 /* Cannot do fast load if a filter is used */
484 if (edit_find_filter (edit
->filename_vpath
) >= 0)
488 * FIXME: line end translation should disable fast loading as well
489 * Consider doing fseek() to the end and ftell() for the real size.
491 if (edit
->filename_vpath
!= NULL
)
494 * VFS may report file size incorrectly, and slow load is not a big
495 * deal considering overhead in VFS.
497 if (!vfs_file_is_local (edit
->filename_vpath
))
500 /* If we are dealing with a real file, check that it exists */
501 if (!check_file_access (edit
, edit
->filename_vpath
, &edit
->stat1
))
506 /* nothing to load */
510 edit_init_buffers (edit
);
514 edit
->last_byte
= edit
->stat1
.st_size
;
515 edit_load_file_fast (edit
, edit
->filename_vpath
);
516 /* If fast load was used, the number of lines wasn't calculated */
517 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
522 if (edit
->filename_vpath
!= NULL
523 && *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) != '\0')
525 edit
->undo_stack_disable
= 1;
526 if (edit_insert_file (edit
, edit
->filename_vpath
) < 0)
531 edit
->undo_stack_disable
= 0;
538 /* --------------------------------------------------------------------------------------------- */
539 /** Restore saved cursor position in the file */
542 edit_load_position (WEdit
* edit
)
547 if (edit
->filename_vpath
== NULL
548 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
551 load_file_position (edit
->filename_vpath
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
555 edit_move_to_line (edit
, line
- 1);
556 edit
->prev_col
= column
;
560 edit_cursor_move (edit
, offset
);
561 line
= edit
->curs_line
;
562 edit
->search_start
= edit
->curs1
;
565 book_mark_restore (edit
, BOOK_MARK_COLOR
);
567 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
568 edit_move_display (edit
, line
- (edit
->widget
.lines
/ 2));
571 /* --------------------------------------------------------------------------------------------- */
572 /** Save cursor position in the file */
575 edit_save_position (WEdit
* edit
)
577 if (edit
->filename_vpath
== NULL
578 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
581 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
582 save_file_position (edit
->filename_vpath
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
,
583 edit
->serialized_bookmarks
);
584 edit
->serialized_bookmarks
= NULL
;
587 /* --------------------------------------------------------------------------------------------- */
588 /** Clean the WEdit stricture except the widget part */
591 edit_purge_widget (WEdit
* edit
)
593 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
594 char *start
= (char *) edit
+ sizeof (Widget
);
595 memset (start
, 0, len
);
598 /* --------------------------------------------------------------------------------------------- */
601 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
602 then the file should be as it was when he loaded up. Then set edit->modified to 0.
606 edit_pop_undo_action (WEdit
* edit
)
609 unsigned long sp
= edit
->undo_stack_pointer
;
611 if (sp
== edit
->undo_stack_bottom
)
614 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
615 c
= edit
->undo_stack
[sp
];
618 /* edit->undo_stack[sp] = '@'; */
619 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
623 if (sp
== edit
->undo_stack_bottom
)
626 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
627 if (edit
->undo_stack
[sp
] == -2)
629 /* edit->undo_stack[sp] = '@'; */
630 edit
->undo_stack_pointer
= sp
;
633 edit
->undo_stack
[sp
]++;
639 edit_pop_redo_action (WEdit
* edit
)
642 unsigned long sp
= edit
->redo_stack_pointer
;
644 if (sp
== edit
->redo_stack_bottom
)
647 sp
= (sp
- 1) & edit
->redo_stack_size_mask
;
648 c
= edit
->redo_stack
[sp
];
651 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
655 if (sp
== edit
->redo_stack_bottom
)
658 c
= edit
->redo_stack
[(sp
- 1) & edit
->redo_stack_size_mask
];
659 if (edit
->redo_stack
[sp
] == -2)
660 edit
->redo_stack_pointer
= sp
;
662 edit
->redo_stack
[sp
]++;
668 get_prev_undo_action (WEdit
* edit
)
671 unsigned long sp
= edit
->undo_stack_pointer
;
673 if (sp
== edit
->undo_stack_bottom
)
676 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
677 c
= edit
->undo_stack
[sp
];
681 if (sp
== edit
->undo_stack_bottom
)
684 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
688 /* --------------------------------------------------------------------------------------------- */
689 /** is called whenever a modification is made by one of the four routines below */
692 edit_modification (WEdit
* edit
)
694 edit
->caches_valid
= 0;
696 /* raise lock when file modified */
697 if (!edit
->modified
&& !edit
->delete_file
)
698 edit
->locked
= lock_file (edit
->filename_vpath
);
702 /* --------------------------------------------------------------------------------------------- */
705 edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
707 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
710 if (byte_index
>= edit
->curs1
)
714 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
715 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
716 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
719 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
720 (byte_index
& M_EDIT_BUF_SIZE
));
723 /* --------------------------------------------------------------------------------------------- */
726 edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
729 gchar utf8_buf
[3 * UTF8_CHAR_LEN
+ 1];
731 gchar
*cursor_buf_ptr
;
733 if (byte_index
> (edit
->curs1
+ edit
->curs2
) || byte_index
<= 0)
739 for (i
= 0; i
< (3 * UTF8_CHAR_LEN
); i
++)
740 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
- (2 * UTF8_CHAR_LEN
));
741 utf8_buf
[3 * UTF8_CHAR_LEN
] = '\0';
743 cursor_buf_ptr
= utf8_buf
+ (2 * UTF8_CHAR_LEN
);
744 str
= g_utf8_find_prev_char (utf8_buf
, cursor_buf_ptr
);
746 if (str
== NULL
|| g_utf8_next_char (str
) != cursor_buf_ptr
)
749 return *(cursor_buf_ptr
- 1);
753 res
= g_utf8_get_char_validated (str
, -1);
758 return *(cursor_buf_ptr
- 1);
762 *char_width
= cursor_buf_ptr
- str
;
768 /* --------------------------------------------------------------------------------------------- */
771 edit_backspace (WEdit
* edit
, const int byte_delete
)
782 if (edit
->mark2
!= edit
->mark1
)
783 edit_push_markers (edit
);
785 if (edit
->utf8
&& byte_delete
== 0)
787 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
791 for (i
= 1; i
<= cw
; i
++)
793 if (edit
->mark1
>= edit
->curs1
)
796 edit
->end_mark_curs
--;
798 if (edit
->mark2
>= edit
->curs1
)
800 if (edit
->last_get_rule
>= edit
->curs1
)
801 edit
->last_get_rule
--;
803 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
804 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
805 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
807 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
808 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
812 edit_push_undo_action (edit
, p
);
814 edit_modification (edit
);
818 book_mark_dec (edit
, edit
->curs_line
);
821 edit
->force
|= REDRAW_AFTER_CURSOR
;
824 if (edit
->curs1
< edit
->start_display
)
826 edit
->start_display
--;
834 /* --------------------------------------------------------------------------------------------- */
835 /* high level cursor movement commands */
836 /* --------------------------------------------------------------------------------------------- */
839 is_in_indent (WEdit
* edit
)
841 long p
= edit_bol (edit
, edit
->curs1
);
842 while (p
< edit
->curs1
)
843 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
848 /* --------------------------------------------------------------------------------------------- */
849 /** check whether line in editor is blank or not
851 * @param edit editor object
852 * @param offset position in file
854 * @return TRUE if line in blank, FALSE otherwise
858 is_blank (WEdit
* edit
, long offset
)
863 s
= edit_bol (edit
, offset
);
864 f
= edit_eol (edit
, offset
) - 1;
867 c
= edit_get_byte (edit
, s
++);
874 /* --------------------------------------------------------------------------------------------- */
875 /** returns the offset of line i */
878 edit_find_line (WEdit
* edit
, int line
)
882 if (!edit
->caches_valid
)
884 for (i
= 0; i
< N_LINE_CACHES
; i
++)
885 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
886 /* three offsets that we *know* are line 0 at 0 and these two: */
887 edit
->line_numbers
[1] = edit
->curs_line
;
888 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
889 edit
->line_numbers
[2] = edit
->total_lines
;
890 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
891 edit
->caches_valid
= 1;
893 if (line
>= edit
->total_lines
)
894 return edit
->line_offsets
[2];
897 /* find the closest known point */
898 for (i
= 0; i
< N_LINE_CACHES
; i
++)
901 n
= abs (edit
->line_numbers
[i
] - line
);
909 return edit
->line_offsets
[j
]; /* know the offset exactly */
910 if (m
== 1 && j
>= 3)
911 i
= j
; /* one line different - caller might be looping, so stay in this cache */
913 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
914 if (line
> edit
->line_numbers
[j
])
915 edit
->line_offsets
[i
] =
916 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
918 edit
->line_offsets
[i
] =
919 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
920 edit
->line_numbers
[i
] = line
;
921 return edit
->line_offsets
[i
];
924 /* --------------------------------------------------------------------------------------------- */
925 /** moves up until a blank line is reached, or until just
926 before a non-blank line is reached */
929 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
932 if (edit
->curs_line
> 1)
934 if (line_is_blank (edit
, edit
->curs_line
))
936 if (line_is_blank (edit
, edit
->curs_line
- 1))
938 for (i
= edit
->curs_line
- 1; i
; i
--)
939 if (!line_is_blank (edit
, i
))
947 for (i
= edit
->curs_line
- 1; i
; i
--)
948 if (line_is_blank (edit
, i
))
954 for (i
= edit
->curs_line
- 1; i
; i
--)
955 if (line_is_blank (edit
, i
))
959 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
962 /* --------------------------------------------------------------------------------------------- */
963 /** moves down until a blank line is reached, or until just
964 before a non-blank line is reached */
967 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
970 if (edit
->curs_line
>= edit
->total_lines
- 1)
972 i
= edit
->total_lines
;
976 if (line_is_blank (edit
, edit
->curs_line
))
978 if (line_is_blank (edit
, edit
->curs_line
+ 1))
980 for (i
= edit
->curs_line
+ 1; i
; i
++)
981 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
989 for (i
= edit
->curs_line
+ 1; i
; i
++)
990 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
996 for (i
= edit
->curs_line
+ 1; i
; i
++)
997 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1001 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
1004 /* --------------------------------------------------------------------------------------------- */
1007 edit_begin_page (WEdit
* edit
)
1009 edit_update_curs_row (edit
);
1010 edit_move_up (edit
, edit
->curs_row
, 0);
1013 /* --------------------------------------------------------------------------------------------- */
1016 edit_end_page (WEdit
* edit
)
1018 edit_update_curs_row (edit
);
1019 edit_move_down (edit
, edit
->widget
.lines
- edit
->curs_row
- 1, 0);
1023 /* --------------------------------------------------------------------------------------------- */
1024 /** goto beginning of text */
1027 edit_move_to_top (WEdit
* edit
)
1029 if (edit
->curs_line
)
1031 edit_cursor_move (edit
, -edit
->curs1
);
1032 edit_move_to_prev_col (edit
, 0);
1033 edit
->force
|= REDRAW_PAGE
;
1034 edit
->search_start
= 0;
1035 edit_update_curs_row (edit
);
1040 /* --------------------------------------------------------------------------------------------- */
1041 /** goto end of text */
1044 edit_move_to_bottom (WEdit
* edit
)
1046 if (edit
->curs_line
< edit
->total_lines
)
1048 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
1049 edit
->start_display
= edit
->last_byte
;
1050 edit
->start_line
= edit
->total_lines
;
1051 edit_scroll_upward (edit
, edit
->widget
.lines
- 1);
1052 edit
->force
|= REDRAW_PAGE
;
1056 /* --------------------------------------------------------------------------------------------- */
1057 /** goto beginning of line */
1060 edit_cursor_to_bol (WEdit
* edit
)
1062 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1063 edit
->search_start
= edit
->curs1
;
1064 edit
->prev_col
= edit_get_col (edit
);
1068 /* --------------------------------------------------------------------------------------------- */
1069 /** goto end of line */
1072 edit_cursor_to_eol (WEdit
* edit
)
1074 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1075 edit
->search_start
= edit
->curs1
;
1076 edit
->prev_col
= edit_get_col (edit
);
1080 /* --------------------------------------------------------------------------------------------- */
1082 static unsigned long
1087 const char option_chars_move_whole_word
[] =
1088 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1094 if (*option_chars_move_whole_word
== '!')
1096 return 0x80000000UL
;
1098 if (g_ascii_isupper ((gchar
) c
))
1100 else if (g_ascii_islower ((gchar
) c
))
1102 else if (g_ascii_isalpha (c
))
1104 else if (isdigit (c
))
1106 else if (isspace (c
))
1108 q
= strchr (option_chars_move_whole_word
, c
);
1110 return 0xFFFFFFFFUL
;
1113 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1118 while ((q
= strchr (q
+ 1, c
)));
1122 /* --------------------------------------------------------------------------------------------- */
1125 edit_left_word_move (WEdit
* edit
, int s
)
1130 if (edit
->column_highlight
1131 && edit
->mark1
!= edit
->mark2
1132 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1134 edit_cursor_move (edit
, -1);
1137 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1138 c2
= edit_get_byte (edit
, edit
->curs1
);
1139 if (c1
== '\n' || c2
== '\n')
1141 if (!(my_type_of (c1
) & my_type_of (c2
)))
1143 if (isspace (c1
) && !isspace (c2
))
1146 if (!isspace (c1
) && isspace (c2
))
1151 /* --------------------------------------------------------------------------------------------- */
1154 edit_left_word_move_cmd (WEdit
* edit
)
1156 edit_left_word_move (edit
, 0);
1157 edit
->force
|= REDRAW_PAGE
;
1160 /* --------------------------------------------------------------------------------------------- */
1163 edit_right_word_move (WEdit
* edit
, int s
)
1168 if (edit
->column_highlight
1169 && edit
->mark1
!= edit
->mark2
1170 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
1172 edit_cursor_move (edit
, 1);
1173 if (edit
->curs1
>= edit
->last_byte
)
1175 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1176 c2
= edit_get_byte (edit
, edit
->curs1
);
1177 if (c1
== '\n' || c2
== '\n')
1179 if (!(my_type_of (c1
) & my_type_of (c2
)))
1181 if (isspace (c1
) && !isspace (c2
))
1184 if (!isspace (c1
) && isspace (c2
))
1189 /* --------------------------------------------------------------------------------------------- */
1192 edit_right_word_move_cmd (WEdit
* edit
)
1194 edit_right_word_move (edit
, 0);
1195 edit
->force
|= REDRAW_PAGE
;
1198 /* --------------------------------------------------------------------------------------------- */
1201 edit_right_char_move_cmd (WEdit
* edit
)
1207 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
1213 c
= edit_get_byte (edit
, edit
->curs1
);
1215 if (option_cursor_beyond_eol
&& c
== '\n')
1221 edit_cursor_move (edit
, cw
);
1225 /* --------------------------------------------------------------------------------------------- */
1228 edit_left_char_move_cmd (WEdit
* edit
)
1231 if (edit
->column_highlight
1232 && option_cursor_beyond_eol
1233 && edit
->mark1
!= edit
->mark2
1234 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1238 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1242 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1248 edit_cursor_move (edit
, -cw
);
1252 /* --------------------------------------------------------------------------------------------- */
1253 /** Up or down cursor moving.
1254 direction = TRUE - move up
1259 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
1262 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
1271 edit
->force
|= REDRAW_PAGE
;
1275 edit_scroll_upward (edit
, i
);
1277 edit_scroll_downward (edit
, i
);
1279 p
= edit_bol (edit
, edit
->curs1
);
1281 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
1283 edit_cursor_move (edit
, p
- edit
->curs1
);
1285 edit_move_to_prev_col (edit
, p
);
1287 /* search start of current multibyte char (like CJK) */
1288 if (edit
->curs1
+ 1 < edit
->last_byte
)
1290 edit_right_char_move_cmd (edit
);
1291 edit_left_char_move_cmd (edit
);
1294 edit
->search_start
= edit
->curs1
;
1295 edit
->found_len
= 0;
1298 /* --------------------------------------------------------------------------------------------- */
1301 edit_right_delete_word (WEdit
* edit
)
1306 if (edit
->curs1
>= edit
->last_byte
)
1308 c1
= edit_delete (edit
, 1);
1309 c2
= edit_get_byte (edit
, edit
->curs1
);
1310 if (c1
== '\n' || c2
== '\n')
1312 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1314 if (!(my_type_of (c1
) & my_type_of (c2
)))
1319 /* --------------------------------------------------------------------------------------------- */
1322 edit_left_delete_word (WEdit
* edit
)
1327 if (edit
->curs1
<= 0)
1329 c1
= edit_backspace (edit
, 1);
1330 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1331 if (c1
== '\n' || c2
== '\n')
1333 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1335 if (!(my_type_of (c1
) & my_type_of (c2
)))
1340 /* --------------------------------------------------------------------------------------------- */
1342 the start column position is not recorded, and hence does not
1343 undo as it happed. But who would notice.
1347 edit_do_undo (WEdit
* edit
)
1352 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1354 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1361 edit_cursor_move (edit
, 1);
1364 edit_cursor_move (edit
, -1);
1368 edit_backspace (edit
, 1);
1372 edit_delete (edit
, 1);
1375 edit
->column_highlight
= 1;
1378 edit
->column_highlight
= 0;
1381 if (ac
>= 256 && ac
< 512)
1382 edit_insert_ahead (edit
, ac
- 256);
1383 if (ac
>= 0 && ac
< 256)
1384 edit_insert (edit
, ac
);
1386 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1388 edit
->mark1
= ac
- MARK_1
;
1389 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1391 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1393 edit
->mark2
= ac
- MARK_2
;
1394 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1396 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1398 edit
->end_mark_curs
= ac
- MARK_CURS
;
1401 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1404 if (edit
->start_display
> ac
- KEY_PRESS
)
1406 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1407 edit
->force
|= REDRAW_PAGE
;
1409 else if (edit
->start_display
< ac
- KEY_PRESS
)
1411 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1412 edit
->force
|= REDRAW_PAGE
;
1414 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1415 edit_update_curs_row (edit
);
1418 edit
->undo_stack_disable
= 0;
1422 edit_do_redo (WEdit
* edit
)
1427 if (edit
->redo_stack_reset
)
1431 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1438 edit_cursor_move (edit
, 1);
1441 edit_cursor_move (edit
, -1);
1444 edit_backspace (edit
, 1);
1447 edit_delete (edit
, 1);
1450 edit
->column_highlight
= 1;
1453 edit
->column_highlight
= 0;
1456 if (ac
>= 256 && ac
< 512)
1457 edit_insert_ahead (edit
, ac
- 256);
1458 if (ac
>= 0 && ac
< 256)
1459 edit_insert (edit
, ac
);
1461 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1463 edit
->mark1
= ac
- MARK_1
;
1464 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1466 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1468 edit
->mark2
= ac
- MARK_2
;
1469 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1471 /* more than one pop usually means something big */
1473 edit
->force
|= REDRAW_PAGE
;
1476 if (edit
->start_display
> ac
- KEY_PRESS
)
1478 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1479 edit
->force
|= REDRAW_PAGE
;
1481 else if (edit
->start_display
< ac
- KEY_PRESS
)
1483 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1484 edit
->force
|= REDRAW_PAGE
;
1486 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1487 edit_update_curs_row (edit
);
1493 edit_group_undo (WEdit
* edit
)
1495 long ac
= KEY_PRESS
;
1496 long cur_ac
= KEY_PRESS
;
1497 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1499 cur_ac
= get_prev_undo_action (edit
);
1500 edit_do_undo (edit
);
1501 ac
= get_prev_undo_action (edit
);
1502 /* exit from cycle if option_group_undo is not set,
1503 * and make single UNDO operation
1505 if (!option_group_undo
)
1510 /* --------------------------------------------------------------------------------------------- */
1513 edit_delete_to_line_end (WEdit
* edit
)
1515 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
1519 edit_delete (edit
, 1);
1523 /* --------------------------------------------------------------------------------------------- */
1526 edit_delete_to_line_begin (WEdit
* edit
)
1528 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
1532 edit_backspace (edit
, 1);
1536 /* --------------------------------------------------------------------------------------------- */
1539 is_aligned_on_a_tab (WEdit
* edit
)
1541 edit_update_curs_col (edit
);
1542 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
1543 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
1546 /* --------------------------------------------------------------------------------------------- */
1549 right_of_four_spaces (WEdit
* edit
)
1552 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1553 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1555 return is_aligned_on_a_tab (edit
);
1559 /* --------------------------------------------------------------------------------------------- */
1562 left_of_four_spaces (WEdit
* edit
)
1565 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1566 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1568 return is_aligned_on_a_tab (edit
);
1572 /* --------------------------------------------------------------------------------------------- */
1575 edit_auto_indent (WEdit
* edit
)
1580 /* use the previous line as a template */
1581 p
= edit_move_backward (edit
, p
, 1);
1582 /* copy the leading whitespace of the line */
1584 { /* no range check - the line _is_ \n-terminated */
1585 c
= edit_get_byte (edit
, p
++);
1586 if (c
!= ' ' && c
!= '\t')
1588 edit_insert (edit
, c
);
1592 /* --------------------------------------------------------------------------------------------- */
1595 edit_double_newline (WEdit
* edit
)
1597 edit_insert (edit
, '\n');
1598 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1600 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1602 edit
->force
|= REDRAW_PAGE
;
1603 edit_insert (edit
, '\n');
1607 /* --------------------------------------------------------------------------------------------- */
1610 insert_spaces_tab (WEdit
* edit
, gboolean half
)
1614 edit_update_curs_col (edit
);
1615 i
= option_tab_spacing
* space_width
;
1618 i
= ((edit
->curs_col
/ i
) + 1) * i
- edit
->curs_col
;
1621 edit_insert (edit
, ' ');
1626 /* --------------------------------------------------------------------------------------------- */
1629 edit_tab_cmd (WEdit
* edit
)
1633 if (option_fake_half_tabs
)
1635 if (is_in_indent (edit
))
1637 /*insert a half tab (usually four spaces) unless there is a
1638 half tab already behind, then delete it and insert a
1640 if (option_fill_tabs_with_spaces
|| !right_of_four_spaces (edit
))
1641 insert_spaces_tab (edit
, TRUE
);
1644 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1645 edit_backspace (edit
, 1);
1646 edit_insert (edit
, '\t');
1651 if (option_fill_tabs_with_spaces
)
1652 insert_spaces_tab (edit
, FALSE
);
1654 edit_insert (edit
, '\t');
1657 /* --------------------------------------------------------------------------------------------- */
1660 check_and_wrap_line (WEdit
* edit
)
1663 if (!option_typewriter_wrap
)
1665 edit_update_curs_col (edit
);
1666 if (edit
->curs_col
< option_word_wrap_line_length
)
1672 c
= edit_get_byte (edit
, curs
);
1673 if (c
== '\n' || curs
<= 0)
1675 edit_insert (edit
, '\n');
1678 if (c
== ' ' || c
== '\t')
1680 int current
= edit
->curs1
;
1681 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
1682 edit_insert (edit
, '\n');
1683 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
1689 /* --------------------------------------------------------------------------------------------- */
1690 /** this find the matching bracket in either direction, and sets edit->bracket */
1693 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
1695 const char *const b
= "{}{[][()(", *p
;
1696 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
1697 unsigned long j
= 0;
1699 edit_update_curs_row (edit
);
1700 c
= edit_get_byte (edit
, edit
->curs1
);
1703 if (!furthest_bracket_search
)
1704 furthest_bracket_search
--;
1705 /* not on a bracket at all */
1708 /* the matching bracket */
1710 /* going left or right? */
1711 if (strchr ("{[(", c
))
1713 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
1715 /* out of buffer? */
1716 if (q
>= edit
->last_byte
|| q
< 0)
1718 a
= edit_get_byte (edit
, q
);
1719 /* don't want to eat CPU */
1720 if (j
++ > furthest_bracket_search
)
1722 /* out of screen? */
1725 if (q
< edit
->start_display
)
1727 /* count lines if searching downward */
1728 if (inc
> 0 && a
== '\n')
1729 if (n
++ >= edit
->widget
.lines
- edit
->curs_row
) /* out of screen */
1732 /* count bracket depth */
1733 i
+= (a
== c
) - (a
== d
);
1734 /* return if bracket depth is zero */
1742 /* --------------------------------------------------------------------------------------------- */
1745 edit_goto_matching_bracket (WEdit
* edit
)
1749 q
= edit_get_bracket (edit
, 0, 0);
1752 edit
->bracket
= edit
->curs1
;
1753 edit
->force
|= REDRAW_PAGE
;
1754 edit_cursor_move (edit
, q
- edit
->curs1
);
1758 /* --------------------------------------------------------------------------------------------- */
1761 edit_move_block_to_right (WEdit
* edit
)
1763 long start_mark
, end_mark
;
1764 long cur_bol
, start_bol
;
1766 if (eval_marks (edit
, &start_mark
, &end_mark
))
1769 start_bol
= edit_bol (edit
, start_mark
);
1770 cur_bol
= edit_bol (edit
, end_mark
- 1);
1774 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1775 if (option_fill_tabs_with_spaces
)
1776 insert_spaces_tab (edit
, option_fake_half_tabs
);
1778 edit_insert (edit
, '\t');
1779 edit_cursor_move (edit
, edit_bol (edit
, cur_bol
) - edit
->curs1
);
1784 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1786 while (cur_bol
>= start_bol
);
1788 edit
->force
|= REDRAW_PAGE
;
1791 /* --------------------------------------------------------------------------------------------- */
1794 edit_move_block_to_left (WEdit
* edit
)
1796 long start_mark
, end_mark
;
1797 long cur_bol
, start_bol
;
1800 if (eval_marks (edit
, &start_mark
, &end_mark
))
1803 start_bol
= edit_bol (edit
, start_mark
);
1804 cur_bol
= edit_bol (edit
, end_mark
- 1);
1811 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1813 if (option_fake_half_tabs
)
1814 del_tab_width
= HALF_TAB_SIZE
;
1816 del_tab_width
= option_tab_spacing
;
1818 next_char
= edit_get_byte (edit
, edit
->curs1
);
1819 if (next_char
== '\t')
1820 edit_delete (edit
, 1);
1821 else if (next_char
== ' ')
1822 for (i
= 1; i
<= del_tab_width
; i
++)
1824 if (next_char
== ' ')
1825 edit_delete (edit
, 1);
1826 next_char
= edit_get_byte (edit
, edit
->curs1
);
1832 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1834 while (cur_bol
>= start_bol
);
1836 edit
->force
|= REDRAW_PAGE
;
1839 /* --------------------------------------------------------------------------------------------- */
1841 * prints at the cursor
1842 * @returns the number of chars printed
1846 edit_print_string (WEdit
* e
, const char *s
)
1850 while (s
[i
] != '\0')
1851 edit_execute_cmd (e
, CK_InsertChar
, (unsigned char) s
[i
++]);
1852 e
->force
|= REDRAW_COMPLETELY
;
1853 edit_update_screen (e
);
1857 /* --------------------------------------------------------------------------------------------- */
1858 /*** public functions ****************************************************************************/
1859 /* --------------------------------------------------------------------------------------------- */
1861 /** User edit menu, like user menu (F2) but only in editor. */
1864 user_menu (WEdit
* edit
, const char *menu_file
, int selected_entry
)
1869 long start_mark
, end_mark
;
1871 vfs_path_t
*block_file_vpath
;
1873 block_file
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
1874 block_file_vpath
= vfs_path_from_str (block_file
);
1876 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
1878 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1880 /* run shell scripts from menu */
1881 if (user_menu_cmd (edit
, menu_file
, selected_entry
)
1882 && (mc_stat (block_file_vpath
, &status
) == 0) && (status
.st_size
!= 0))
1887 /* i.e. we have marked block */
1889 rc
= edit_block_delete_cmd (edit
);
1895 ins_len
= edit_insert_file (edit
, block_file_vpath
);
1896 if (nomark
== 0 && ins_len
> 0)
1897 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1899 /* truncate block file */
1900 fd
= fopen (block_file
, "w");
1904 edit_cursor_move (edit
, curs
- edit
->curs1
);
1905 edit_refresh_cmd (edit
);
1906 edit
->force
|= REDRAW_COMPLETELY
;
1908 g_free (block_file
);
1909 vfs_path_free (block_file_vpath
);
1912 /* --------------------------------------------------------------------------------------------- */
1915 edit_get_byte (WEdit
* edit
, long byte_index
)
1918 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1921 if (byte_index
>= edit
->curs1
)
1923 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1924 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
1928 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
1932 /* --------------------------------------------------------------------------------------------- */
1935 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1940 gchar
*next_ch
= NULL
;
1942 gchar utf8_buf
[UTF8_CHAR_LEN
+ 1];
1944 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1950 str
= edit_get_byte_ptr (edit
, byte_index
);
1958 res
= g_utf8_get_char_validated (str
, -1);
1962 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1964 for (i
= 0; i
< UTF8_CHAR_LEN
; i
++)
1965 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
);
1966 utf8_buf
[UTF8_CHAR_LEN
] = '\0';
1968 res
= g_utf8_get_char_validated (str
, -1);
1979 /* Calculate UTF-8 char width */
1980 next_ch
= g_utf8_next_char (str
);
1983 width
= next_ch
- str
;
1991 *char_width
= width
;
1995 /* --------------------------------------------------------------------------------------------- */
1998 edit_get_write_filter (const vfs_path_t
* write_name_vpath
, const vfs_path_t
* filename_vpath
)
2001 char *p
, *writename
;
2002 const vfs_path_element_t
*path_element
;
2004 i
= edit_find_filter (filename_vpath
);
2008 path_element
= vfs_path_get_by_index (write_name_vpath
, -1);
2009 writename
= name_quote (path_element
->path
, 0);
2010 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
2015 /* --------------------------------------------------------------------------------------------- */
2018 edit_write_stream (WEdit
* edit
, FILE * f
)
2022 if (edit
->lb
== LB_ASIS
)
2024 for (i
= 0; i
< edit
->last_byte
; i
++)
2025 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
2030 /* change line breaks */
2031 for (i
= 0; i
< edit
->last_byte
; i
++)
2033 unsigned char c
= edit_get_byte (edit
, i
);
2035 if (!(c
== '\n' || c
== '\r'))
2037 /* not line break */
2038 if (fputc (c
, f
) < 0)
2042 { /* (c == '\n' || c == '\r') */
2043 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
2047 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
2048 /* put one line break unconditionally */
2049 if (fputc ('\n', f
) < 0)
2052 i
++; /* 2 chars are processed */
2054 if (c
== '\r' && c1
== '\n')
2055 /* Windows line break; go to the next char */
2058 if (c
== '\r' && c1
== '\r')
2060 /* two Macintosh line breaks; put second line break */
2061 if (fputc ('\n', f
) < 0)
2066 if (fputc (c1
, f
) < 0)
2070 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
2071 /* put one line break unconditionally */
2072 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
2075 if (c
== '\r' && c1
== '\n')
2076 /* Windows line break; go to the next char */
2080 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
2081 /* put one line break unconditionally */
2082 if (fputc ('\r', f
) < 0)
2085 i
++; /* 2 chars are processed */
2087 if (c
== '\r' && c1
== '\n')
2088 /* Windows line break; go to the next char */
2091 if (c
== '\n' && c1
== '\n')
2093 /* two Windows line breaks; put second line break */
2094 if (fputc ('\r', f
) < 0)
2099 if (fputc (c1
, f
) < 0)
2102 case LB_ASIS
: /* default without changes */
2108 return edit
->last_byte
;
2111 /* --------------------------------------------------------------------------------------------- */
2112 /** inserts a file at the cursor, returns count of inserted bytes on success */
2114 edit_insert_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
2119 p
= edit_get_filter (filename_vpath
);
2123 long current
= edit
->curs1
;
2125 f
= (FILE *) popen (p
, "r");
2128 edit_insert_stream (edit
, f
);
2129 ins_len
= edit
->curs1
- current
;
2130 edit_cursor_move (edit
, -ins_len
);
2135 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
2136 edit_error_dialog (_("Error"), errmsg
);
2145 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
2146 edit_error_dialog (_("Error"), errmsg
);
2154 int i
, file
, blocklen
;
2155 long current
= edit
->curs1
;
2156 int vertical_insertion
= 0;
2159 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
2163 buf
= g_malloc0 (TEMP_BUF_LEN
);
2164 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2167 /* if contain signature VERTICAL_MAGIC then it vertical block */
2168 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2169 vertical_insertion
= 1;
2171 mc_lseek (file
, 0, SEEK_SET
);
2174 if (vertical_insertion
)
2179 blocklen
= edit_insert_column_of_text_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
2180 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2181 /* highlight inserted text then not persistent blocks */
2182 if (!option_persistent_selections
)
2184 if (!edit
->column_highlight
)
2185 edit_push_undo_action (edit
, COLUMN_OFF
);
2186 edit
->column_highlight
= 1;
2191 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2193 for (i
= 0; i
< blocklen
; i
++)
2194 edit_insert (edit
, buf
[i
]);
2196 /* highlight inserted text then not persistent blocks */
2197 if (!option_persistent_selections
&& edit
->modified
)
2199 edit_set_markers (edit
, edit
->curs1
, current
, 0, 0);
2200 if (edit
->column_highlight
)
2201 edit_push_undo_action (edit
, COLUMN_ON
);
2202 edit
->column_highlight
= 0;
2206 edit
->force
|= REDRAW_PAGE
;
2207 ins_len
= edit
->curs1
- current
;
2208 edit_cursor_move (edit
, -ins_len
);
2218 /* --------------------------------------------------------------------------------------------- */
2220 * Fill in the edit structure. Return NULL on failure. Pass edit as
2221 * NULL to allocate a new structure.
2223 * If line is 0, try to restore saved position. Otherwise put the
2224 * cursor on that line and show it in the middle of the screen.
2228 edit_init (WEdit
* edit
, int y
, int x
, int lines
, int cols
, const vfs_path_t
* filename_vpath
,
2231 gboolean to_free
= FALSE
;
2233 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2234 if (option_line_state
)
2235 option_line_state_width
= LINE_STATE_WIDTH
;
2237 option_line_state_width
= 0;
2243 * Expand option_whole_chars_search by national letters using
2247 static char option_whole_chars_search_buf
[256];
2249 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
2252 size_t len
= str_term_width1 (option_whole_chars_search
);
2254 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
2256 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
2258 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
2260 option_whole_chars_search_buf
[len
++] = i
;
2264 option_whole_chars_search_buf
[len
] = 0;
2265 option_whole_chars_search
= option_whole_chars_search_buf
;
2267 #endif /* ENABLE_NLS */
2268 edit
= g_malloc0 (sizeof (WEdit
));
2269 edit
->search
= NULL
;
2273 edit_purge_widget (edit
);
2276 edit
->widget
.lines
= lines
;
2277 edit
->widget
.cols
= cols
;
2279 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2280 edit
->stat1
.st_uid
= getuid ();
2281 edit
->stat1
.st_gid
= getgid ();
2282 edit
->stat1
.st_mtime
= 0;
2286 edit
->force
|= REDRAW_PAGE
;
2287 edit_set_filename (edit
, filename_vpath
);
2289 edit
->undo_stack_size
= START_STACK_SIZE
;
2290 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2291 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2293 edit
->redo_stack_size
= START_STACK_SIZE
;
2294 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2295 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2298 edit
->converter
= str_cnv_from_term
;
2299 edit_set_codeset (edit
);
2301 if (!edit_load_file (edit
))
2303 /* edit_load_file already gives an error message */
2309 edit
->loading_done
= 1;
2312 edit_load_syntax (edit
, NULL
, NULL
);
2315 edit_get_syntax_color (edit
, -1, &color
);
2318 /* load saved cursor position */
2319 if ((line
== 0) && option_save_position
)
2320 edit_load_position (edit
);
2325 edit_move_display (edit
, line
- 1);
2326 edit_move_to_line (edit
, line
- 1);
2329 edit_load_macro_cmd (edit
);
2333 /* --------------------------------------------------------------------------------------------- */
2335 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2337 edit_clean (WEdit
* edit
)
2344 /* a stale lock, remove it */
2346 edit
->locked
= unlock_file (edit
->filename_vpath
);
2348 /* save cursor position */
2349 if (option_save_position
)
2350 edit_save_position (edit
);
2351 else if (edit
->serialized_bookmarks
!= NULL
)
2352 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2354 /* File specified on the mcedit command line and never saved */
2355 if (edit
->delete_file
)
2356 unlink (vfs_path_get_last_path_str (edit
->filename_vpath
));
2358 edit_free_syntax_rules (edit
);
2359 book_mark_flush (edit
, -1);
2360 for (; j
<= MAXBUFF
; j
++)
2362 g_free (edit
->buffers1
[j
]);
2363 g_free (edit
->buffers2
[j
]);
2366 g_free (edit
->undo_stack
);
2367 g_free (edit
->redo_stack
);
2368 vfs_path_free (edit
->filename_vpath
);
2369 vfs_path_free (edit
->dir_vpath
);
2370 mc_search_free (edit
->search
);
2371 edit
->search
= NULL
;
2373 if (edit
->converter
!= str_cnv_from_term
)
2374 str_close_conv (edit
->converter
);
2376 edit_purge_widget (edit
);
2381 /* --------------------------------------------------------------------------------------------- */
2383 /** returns TRUE on success */
2385 edit_renew (WEdit
* edit
)
2387 int y
= edit
->widget
.y
;
2388 int x
= edit
->widget
.x
;
2389 int lines
= edit
->widget
.lines
;
2390 int columns
= edit
->widget
.cols
;
2393 return (edit_init (edit
, y
, x
, lines
, columns
, NULL
, 0) != NULL
);
2396 /* --------------------------------------------------------------------------------------------- */
2399 * Load a new file into the editor and set line. If it fails, preserve the old file.
2400 * To do it, allocate a new widget, initialize it and, if the new file
2401 * was loaded, copy the data to the old widget.
2403 * @returns TRUE on success, FALSE on failure.
2406 edit_reload_line (WEdit
* edit
, const vfs_path_t
* filename_vpath
, long line
)
2409 int y
= edit
->widget
.y
;
2410 int x
= edit
->widget
.x
;
2411 int lines
= edit
->widget
.lines
;
2412 int columns
= edit
->widget
.cols
;
2414 e
= g_malloc0 (sizeof (WEdit
));
2415 e
->widget
= edit
->widget
;
2417 if (edit_init (e
, y
, x
, lines
, columns
, filename_vpath
, line
) == NULL
)
2424 memcpy (edit
, e
, sizeof (WEdit
));
2430 /* --------------------------------------------------------------------------------------------- */
2433 edit_set_codeset (WEdit
* edit
)
2439 get_codepage_id (mc_global
.source_codepage
>=
2440 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2445 conv
= str_crt_conv_from (cp_id
);
2446 if (conv
!= INVALID_CONV
)
2448 if (edit
->converter
!= str_cnv_from_term
)
2449 str_close_conv (edit
->converter
);
2450 edit
->converter
= conv
;
2455 edit
->utf8
= str_isutf8 (cp_id
);
2462 /* --------------------------------------------------------------------------------------------- */
2464 Recording stack for undo:
2465 The following is an implementation of a compressed stack. Identical
2466 pushes are recorded by a negative prefix indicating the number of times the
2467 same char was pushed. This saves space for repeated curs-left or curs-right
2484 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2485 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2486 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2487 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2490 The only way the cursor moves or the buffer is changed is through the routines:
2491 insert, backspace, insert_ahead, delete, and cursor_move.
2492 These record the reverse undo movements onto the stack each time they are
2495 Each key press results in a set of actions (insert; delete ...). So each time
2496 a key is pressed the current position of start_display is pushed as
2497 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2498 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2499 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2504 edit_push_undo_action (WEdit
* edit
, long c
, ...)
2506 unsigned long sp
= edit
->undo_stack_pointer
;
2510 /* first enlarge the stack if necessary */
2511 if (sp
> edit
->undo_stack_size
- 10)
2513 if (option_max_undo
< 256)
2514 option_max_undo
= 256;
2515 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2517 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2520 edit
->undo_stack
= t
;
2521 edit
->undo_stack_size
<<= 1;
2522 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2526 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2527 if (edit
->undo_stack_disable
)
2529 edit_push_redo_action (edit
, KEY_PRESS
);
2530 edit_push_redo_action (edit
, c
);
2533 else if (edit
->redo_stack_reset
)
2535 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2538 if (edit
->undo_stack_bottom
!= sp
2539 && spm1
!= edit
->undo_stack_bottom
2540 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2543 if (edit
->undo_stack
[spm1
] < 0)
2545 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2548 if (edit
->undo_stack
[spm1
] > -1000000000)
2550 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2552 edit
->undo_stack
[spm1
]--;
2560 d
= edit
->undo_stack
[spm1
];
2564 return; /* --> no need to push multiple do-nothings */
2565 edit
->undo_stack
[sp
] = -2;
2570 edit
->undo_stack
[sp
] = c
;
2573 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2575 /* if the sp wraps round and catches the undo_stack_bottom then erase
2576 * the first set of actions on the stack to make space - by moving
2577 * undo_stack_bottom forward one "key press" */
2578 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2579 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2580 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2583 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2585 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2586 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2588 /*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: */
2589 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2590 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2592 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2597 edit_push_redo_action (WEdit
* edit
, long c
, ...)
2599 unsigned long sp
= edit
->redo_stack_pointer
;
2602 /* first enlarge the stack if necessary */
2603 if (sp
> edit
->redo_stack_size
- 10)
2605 if (option_max_undo
< 256)
2606 option_max_undo
= 256;
2607 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2609 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2612 edit
->redo_stack
= t
;
2613 edit
->redo_stack_size
<<= 1;
2614 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2618 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2620 if (edit
->redo_stack_bottom
!= sp
2621 && spm1
!= edit
->redo_stack_bottom
2622 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2625 if (edit
->redo_stack
[spm1
] < 0)
2627 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2630 if (edit
->redo_stack
[spm1
] > -1000000000)
2632 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2633 edit
->redo_stack
[spm1
]--;
2640 d
= edit
->redo_stack
[spm1
];
2644 return; /* --> no need to push multiple do-nothings */
2645 edit
->redo_stack
[sp
] = -2;
2646 goto redo_check_bottom
;
2650 edit
->redo_stack
[sp
] = c
;
2653 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2655 /* if the sp wraps round and catches the redo_stack_bottom then erase
2656 * the first set of actions on the stack to make space - by moving
2657 * redo_stack_bottom forward one "key press" */
2658 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2659 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2660 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2663 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2665 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2666 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2669 * If a single key produced enough pushes to wrap all the way round then
2670 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2671 * The stack is then initialised:
2674 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2675 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2676 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2680 /* --------------------------------------------------------------------------------------------- */
2682 Basic low level single character buffer alterations and movements at the cursor.
2683 Returns char passed over, inserted or removed.
2687 edit_insert (WEdit
* edit
, int c
)
2689 /* check if file has grown to large */
2690 if (edit
->last_byte
>= SIZE_LIMIT
)
2693 /* first we must update the position of the display window */
2694 if (edit
->curs1
< edit
->start_display
)
2696 edit
->start_display
++;
2701 /* Mark file as modified, unless the file hasn't been fully loaded */
2702 if (edit
->loading_done
)
2704 edit_modification (edit
);
2707 /* now we must update some info on the file and check if a redraw is required */
2710 if (edit
->book_mark
)
2711 book_mark_inc (edit
, edit
->curs_line
);
2713 edit
->total_lines
++;
2714 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2717 /* save the reverse command onto the undo stack */
2718 /* ordinary char and not space */
2720 edit_push_undo_action (edit
, BACKSPACE
);
2722 edit_push_undo_action (edit
, BACKSPACE_BR
);
2723 /* update markers */
2724 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2725 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2726 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2728 /* add a new buffer if we've reached the end of the last one */
2729 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2730 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2732 /* perform the insertion */
2733 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2734 = (unsigned char) c
;
2736 /* update file length */
2739 /* update cursor position */
2743 /* --------------------------------------------------------------------------------------------- */
2744 /** same as edit_insert and move left */
2747 edit_insert_ahead (WEdit
* edit
, int c
)
2749 if (edit
->last_byte
>= SIZE_LIMIT
)
2752 if (edit
->curs1
< edit
->start_display
)
2754 edit
->start_display
++;
2758 edit_modification (edit
);
2761 if (edit
->book_mark
)
2762 book_mark_inc (edit
, edit
->curs_line
);
2763 edit
->total_lines
++;
2764 edit
->force
|= REDRAW_AFTER_CURSOR
;
2766 /* ordinary char and not space */
2768 edit_push_undo_action (edit
, DELCHAR
);
2770 edit_push_undo_action (edit
, DELCHAR_BR
);
2772 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2773 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2774 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2776 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2777 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2778 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2779 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2786 /* --------------------------------------------------------------------------------------------- */
2789 edit_delete (WEdit
* edit
, const int byte_delete
)
2799 /* if byte_delete = 1 then delete only one byte not multibyte char */
2800 if (edit
->utf8
&& byte_delete
== 0)
2802 edit_get_utf (edit
, edit
->curs1
, &cw
);
2807 if (edit
->mark2
!= edit
->mark1
)
2808 edit_push_markers (edit
);
2810 for (i
= 1; i
<= cw
; i
++)
2812 if (edit
->mark1
> edit
->curs1
)
2815 edit
->end_mark_curs
--;
2817 if (edit
->mark2
> edit
->curs1
)
2819 if (edit
->last_get_rule
> edit
->curs1
)
2820 edit
->last_get_rule
--;
2822 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2824 1) & M_EDIT_BUF_SIZE
) - 1];
2826 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2828 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2829 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2833 edit_push_undo_action (edit
, p
+ 256);
2836 edit_modification (edit
);
2839 if (edit
->book_mark
)
2840 book_mark_dec (edit
, edit
->curs_line
);
2841 edit
->total_lines
--;
2842 edit
->force
|= REDRAW_AFTER_CURSOR
;
2844 if (edit
->curs1
< edit
->start_display
)
2846 edit
->start_display
--;
2854 /* --------------------------------------------------------------------------------------------- */
2855 /** moves the cursor right or left: increment positive or negative respectively */
2858 edit_cursor_move (WEdit
* edit
, long increment
)
2860 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2865 for (; increment
< 0; increment
++)
2870 edit_push_undo_action (edit
, CURS_RIGHT
);
2872 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2873 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2874 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2875 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2876 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2878 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2879 1) & M_EDIT_BUF_SIZE
];
2880 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2882 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2883 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2889 edit
->force
|= REDRAW_LINE_BELOW
;
2894 else if (increment
> 0)
2896 for (; increment
> 0; increment
--)
2901 edit_push_undo_action (edit
, CURS_LEFT
);
2903 c
= edit_get_byte (edit
, edit
->curs1
);
2904 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2905 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2906 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2908 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2910 1) & M_EDIT_BUF_SIZE
) - 1];
2911 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2913 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2914 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2920 edit
->force
|= REDRAW_LINE_ABOVE
;
2926 /* These functions return positions relative to lines */
2928 /* --------------------------------------------------------------------------------------------- */
2929 /** returns index of last char on line + 1 */
2932 edit_eol (WEdit
* edit
, long current
)
2934 if (current
>= edit
->last_byte
)
2935 return edit
->last_byte
;
2938 if (edit_get_byte (edit
, current
) == '\n')
2943 /* --------------------------------------------------------------------------------------------- */
2944 /** returns index of first char on line */
2947 edit_bol (WEdit
* edit
, long current
)
2953 if (edit_get_byte (edit
, current
- 1) == '\n')
2958 /* --------------------------------------------------------------------------------------------- */
2961 edit_count_lines (WEdit
* edit
, long current
, long upto
)
2964 if (upto
> edit
->last_byte
)
2965 upto
= edit
->last_byte
;
2968 while (current
< upto
)
2969 if (edit_get_byte (edit
, current
++) == '\n')
2974 /* --------------------------------------------------------------------------------------------- */
2975 /* If lines is zero this returns the count of lines from current to upto. */
2976 /* If upto is zero returns index of lines forward current. */
2979 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
2983 return edit_count_lines (edit
, current
, upto
);
2992 next
= edit_eol (edit
, current
) + 1;
2993 if (next
> edit
->last_byte
)
3002 /* --------------------------------------------------------------------------------------------- */
3003 /** Returns offset of 'lines' lines up from current */
3006 edit_move_backward (WEdit
* edit
, long current
, long lines
)
3010 current
= edit_bol (edit
, current
);
3011 while ((lines
--) && current
!= 0)
3012 current
= edit_bol (edit
, current
- 1);
3016 /* --------------------------------------------------------------------------------------------- */
3017 /* If cols is zero this returns the count of columns from current to upto. */
3018 /* If upto is zero returns index of cols across from current. */
3021 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
3032 q
= edit
->last_byte
+ 2;
3034 for (col
= 0, p
= current
; p
< q
; p
++)
3046 orig_c
= c
= edit_get_byte (edit
, p
);
3054 utf_ch
= edit_get_utf (edit
, p
, &cw
);
3055 if (mc_global
.utf8_display
)
3059 if (g_unichar_iswide (utf_ch
))
3062 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
3066 c
= convert_to_display_c (c
);
3070 col
+= TAB_SIZE
- col
% TAB_SIZE
;
3078 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!mc_global
.utf8_display
&& !edit
->utf8
)))
3079 /* '\r' is shown as ^M, so we must advance 2 characters */
3080 /* Caret notation for control characters */
3088 /* --------------------------------------------------------------------------------------------- */
3089 /** returns the current column position of the cursor */
3092 edit_get_col (WEdit
* edit
)
3094 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3097 /* --------------------------------------------------------------------------------------------- */
3098 /* Scrolling functions */
3099 /* --------------------------------------------------------------------------------------------- */
3102 edit_update_curs_row (WEdit
* edit
)
3104 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
3107 /* --------------------------------------------------------------------------------------------- */
3110 edit_update_curs_col (WEdit
* edit
)
3112 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3115 /* --------------------------------------------------------------------------------------------- */
3118 edit_get_curs_col (const WEdit
* edit
)
3120 return edit
->curs_col
;
3123 /* --------------------------------------------------------------------------------------------- */
3124 /** moves the display start position up by i lines */
3127 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
3129 unsigned long lines_above
= edit
->start_line
;
3130 if (i
> lines_above
)
3134 edit
->start_line
-= i
;
3135 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
3136 edit
->force
|= REDRAW_PAGE
;
3137 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3139 edit_update_curs_row (edit
);
3143 /* --------------------------------------------------------------------------------------------- */
3144 /** returns 1 if could scroll, 0 otherwise */
3147 edit_scroll_downward (WEdit
* edit
, int i
)
3150 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->widget
.lines
- 1);
3151 if (lines_below
> 0)
3153 if (i
> lines_below
)
3155 edit
->start_line
+= i
;
3156 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
3157 edit
->force
|= REDRAW_PAGE
;
3158 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3160 edit_update_curs_row (edit
);
3163 /* --------------------------------------------------------------------------------------------- */
3166 edit_scroll_right (WEdit
* edit
, int i
)
3168 edit
->force
|= REDRAW_PAGE
;
3169 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3170 edit
->start_col
-= i
;
3173 /* --------------------------------------------------------------------------------------------- */
3176 edit_scroll_left (WEdit
* edit
, int i
)
3178 if (edit
->start_col
)
3180 edit
->start_col
+= i
;
3181 if (edit
->start_col
> 0)
3182 edit
->start_col
= 0;
3183 edit
->force
|= REDRAW_PAGE
;
3184 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3188 /* --------------------------------------------------------------------------------------------- */
3189 /* high level cursor movement commands */
3190 /* --------------------------------------------------------------------------------------------- */
3193 edit_move_to_prev_col (WEdit
* edit
, long p
)
3195 int prev
= edit
->prev_col
;
3196 int over
= edit
->over_col
;
3197 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
3199 if (option_cursor_beyond_eol
)
3201 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
3202 edit_eol (edit
, edit
->curs1
));
3204 if (line_len
< prev
+ edit
->over_col
)
3206 edit
->over_col
= prev
+ over
- line_len
;
3207 edit
->prev_col
= line_len
;
3208 edit
->curs_col
= line_len
;
3212 edit
->curs_col
= prev
+ over
;
3213 edit
->prev_col
= edit
->curs_col
;
3220 if (is_in_indent (edit
) && option_fake_half_tabs
)
3222 edit_update_curs_col (edit
);
3224 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
3226 int q
= edit
->curs_col
;
3227 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
3228 p
= edit_bol (edit
, edit
->curs1
);
3229 edit_cursor_move (edit
,
3230 edit_move_forward3 (edit
, p
, edit
->curs_col
,
3232 if (!left_of_four_spaces (edit
))
3233 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3239 /* --------------------------------------------------------------------------------------------- */
3240 /** check whether line in editor is blank or not
3242 * @param edit editor object
3243 * @param line number of line
3245 * @return TRUE if line in blank, FALSE otherwise
3249 line_is_blank (WEdit
* edit
, long line
)
3251 return is_blank (edit
, edit_find_line (edit
, line
));
3254 /* --------------------------------------------------------------------------------------------- */
3255 /** move cursor to line 'line' */
3258 edit_move_to_line (WEdit
* e
, long line
)
3260 if (line
< e
->curs_line
)
3261 edit_move_up (e
, e
->curs_line
- line
, 0);
3263 edit_move_down (e
, line
- e
->curs_line
, 0);
3264 edit_scroll_screen_over_cursor (e
);
3267 /* --------------------------------------------------------------------------------------------- */
3268 /** scroll window so that first visible line is 'line' */
3271 edit_move_display (WEdit
* e
, long line
)
3273 if (line
< e
->start_line
)
3274 edit_scroll_upward (e
, e
->start_line
- line
);
3276 edit_scroll_downward (e
, line
- e
->start_line
);
3279 /* --------------------------------------------------------------------------------------------- */
3280 /** save markers onto undo stack */
3283 edit_push_markers (WEdit
* edit
)
3285 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3286 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3287 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3290 /* --------------------------------------------------------------------------------------------- */
3293 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
3302 /* --------------------------------------------------------------------------------------------- */
3303 /** highlight marker toggle */
3306 edit_mark_cmd (WEdit
* edit
, int unmark
)
3308 edit_push_markers (edit
);
3311 edit_set_markers (edit
, 0, 0, 0, 0);
3312 edit
->force
|= REDRAW_PAGE
;
3316 if (edit
->mark2
>= 0)
3318 edit
->end_mark_curs
= -1;
3319 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3320 edit
->curs_col
+ edit
->over_col
);
3321 edit
->force
|= REDRAW_PAGE
;
3325 edit
->end_mark_curs
= edit
->curs1
;
3326 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3327 edit
->curs_col
+ edit
->over_col
);
3332 /* --------------------------------------------------------------------------------------------- */
3333 /** highlight the word under cursor */
3336 edit_mark_current_word_cmd (WEdit
* edit
)
3340 for (pos
= edit
->curs1
; pos
!= 0; pos
--)
3344 c1
= edit_get_byte (edit
, pos
);
3345 c2
= edit_get_byte (edit
, pos
- 1);
3346 if (!isspace (c1
) && isspace (c2
))
3348 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3353 for (; pos
< edit
->last_byte
; pos
++)
3357 c1
= edit_get_byte (edit
, pos
);
3358 c2
= edit_get_byte (edit
, pos
+ 1);
3359 if (!isspace (c1
) && isspace (c2
))
3361 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3364 edit
->mark2
= min (pos
+ 1, edit
->last_byte
);
3366 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3369 /* --------------------------------------------------------------------------------------------- */
3372 edit_mark_current_line_cmd (WEdit
* edit
)
3374 long pos
= edit
->curs1
;
3376 edit
->mark1
= edit_bol (edit
, pos
);
3377 edit
->mark2
= edit_eol (edit
, pos
);
3379 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3382 /* --------------------------------------------------------------------------------------------- */
3385 edit_delete_line (WEdit
* edit
)
3388 * Delete right part of the line.
3389 * Note that edit_get_byte() returns '\n' when byte position is
3392 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3394 (void) edit_delete (edit
, 1);
3399 * Note that edit_delete() will not corrupt anything if called while
3400 * cursor position is EOF.
3402 (void) edit_delete (edit
, 1);
3405 * Delete left part of the line.
3406 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3408 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3410 (void) edit_backspace (edit
, 1);
3414 /* --------------------------------------------------------------------------------------------- */
3417 edit_indent_width (WEdit
* edit
, long p
)
3420 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
3422 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
3425 /* --------------------------------------------------------------------------------------------- */
3428 edit_insert_indent (WEdit
* edit
, int indent
)
3430 if (!option_fill_tabs_with_spaces
)
3432 while (indent
>= TAB_SIZE
)
3434 edit_insert (edit
, '\t');
3438 while (indent
-- > 0)
3439 edit_insert (edit
, ' ');
3442 /* --------------------------------------------------------------------------------------------- */
3445 edit_push_key_press (WEdit
* edit
)
3447 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3448 if (edit
->mark2
== -1)
3450 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3451 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3455 /* --------------------------------------------------------------------------------------------- */
3458 edit_find_bracket (WEdit
* edit
)
3460 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3461 if (last_bracket
!= edit
->bracket
)
3462 edit
->force
|= REDRAW_PAGE
;
3463 last_bracket
= edit
->bracket
;
3466 /* --------------------------------------------------------------------------------------------- */
3468 * This executes a command as though the user initiated it through a key
3469 * press. Callback with WIDGET_KEY as a message calls this after
3470 * translating the key press. This function can be used to pass any
3471 * command to the editor. Note that the screen wouldn't update
3472 * automatically. Either of command or char_for_insertion must be
3473 * passed as -1. Commands are executed, and char_for_insertion is
3474 * inserted at the cursor.
3478 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3480 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3482 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3485 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3488 if (macro_index
!= -1)
3490 edit
->force
|= REDRAW_COMPLETELY
;
3491 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3493 edit_store_macro_cmd (edit
);
3497 else if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3499 edit_repeat_macro_cmd (edit
);
3505 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3507 record_macro_buf
[macro_index
].action
= command
;
3508 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3510 /* record the beginning of a set of editing actions initiated by a key press */
3511 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3512 edit_push_key_press (edit
);
3514 edit_execute_cmd (edit
, command
, char_for_insertion
);
3515 if (edit
->column_highlight
)
3516 edit
->force
|= REDRAW_PAGE
;
3519 /* --------------------------------------------------------------------------------------------- */
3521 This executes a command at a lower level than macro recording.
3522 It also does not push a key_press onto the undo stack. This means
3523 that if it is called many times, a single undo command will undo
3524 all of them. It also does not check for the Undo command.
3527 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3529 edit
->force
|= REDRAW_LINE
;
3531 /* The next key press will unhighlight the found string, so update
3533 if (edit
->found_len
|| edit
->column_highlight
)
3534 edit
->force
|= REDRAW_PAGE
;
3538 /* a mark command with shift-arrow */
3541 case CK_MarkToWordBegin
:
3542 case CK_MarkToWordEnd
:
3548 case CK_MarkPageDown
:
3549 case CK_MarkToFileBegin
:
3550 case CK_MarkToFileEnd
:
3551 case CK_MarkToPageBegin
:
3552 case CK_MarkToPageEnd
:
3553 case CK_MarkScrollUp
:
3554 case CK_MarkScrollDown
:
3555 case CK_MarkParagraphUp
:
3556 case CK_MarkParagraphDown
:
3557 /* a mark command with alt-arrow */
3558 case CK_MarkColumnPageUp
:
3559 case CK_MarkColumnPageDown
:
3560 case CK_MarkColumnLeft
:
3561 case CK_MarkColumnRight
:
3562 case CK_MarkColumnUp
:
3563 case CK_MarkColumnDown
:
3564 case CK_MarkColumnScrollUp
:
3565 case CK_MarkColumnScrollDown
:
3566 case CK_MarkColumnParagraphUp
:
3567 case CK_MarkColumnParagraphDown
:
3568 edit
->column_highlight
= 0;
3569 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3571 edit_mark_cmd (edit
, 1); /* clear */
3572 edit_mark_cmd (edit
, 0); /* marking on */
3574 edit
->highlight
= 1;
3577 /* any other command */
3579 if (edit
->highlight
)
3580 edit_mark_cmd (edit
, 0); /* clear */
3581 edit
->highlight
= 0;
3584 /* first check for undo */
3585 if (command
== CK_Undo
)
3587 edit
->redo_stack_reset
= 0;
3588 edit_group_undo (edit
);
3589 edit
->found_len
= 0;
3590 edit
->prev_col
= edit_get_col (edit
);
3591 edit
->search_start
= edit
->curs1
;
3594 /* check for redo */
3595 if (command
== CK_Redo
)
3597 edit
->redo_stack_reset
= 0;
3598 edit_do_redo (edit
);
3599 edit
->found_len
= 0;
3600 edit
->prev_col
= edit_get_col (edit
);
3601 edit
->search_start
= edit
->curs1
;
3605 edit
->redo_stack_reset
= 1;
3607 /* An ordinary key press */
3608 if (char_for_insertion
>= 0)
3610 /* if non persistent selection and text selected */
3611 if (!option_persistent_selections
)
3613 if (edit
->mark1
!= edit
->mark2
)
3614 edit_block_delete_cmd (edit
);
3616 if (edit
->overwrite
)
3618 /* remove char only one time, after input first byte, multibyte chars */
3619 if ((!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3620 && edit_get_byte (edit
, edit
->curs1
) != '\n')
3621 edit_delete (edit
, 0);
3623 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3624 edit_insert_over (edit
);
3626 if (char_for_insertion
> 255 && !mc_global
.utf8_display
)
3628 unsigned char str
[6 + 1];
3632 res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3642 while (str
[i
] != 0 && i
<= 6)
3644 char_for_insertion
= str
[i
];
3645 edit_insert (edit
, char_for_insertion
);
3651 edit_insert (edit
, char_for_insertion
);
3653 if (option_auto_para_formatting
)
3655 format_paragraph (edit
, 0);
3656 edit
->force
|= REDRAW_PAGE
;
3659 check_and_wrap_line (edit
);
3660 edit
->found_len
= 0;
3661 edit
->prev_col
= edit_get_col (edit
);
3662 edit
->search_start
= edit
->curs1
;
3663 edit_find_bracket (edit
);
3669 case CK_TopOnScreen
:
3670 case CK_BottomOnScreen
:
3683 if (edit
->mark2
>= 0)
3685 if (!option_persistent_selections
)
3687 if (edit
->column_highlight
)
3688 edit_push_undo_action (edit
, COLUMN_ON
);
3689 edit
->column_highlight
= 0;
3690 edit_mark_cmd (edit
, 1);
3697 case CK_TopOnScreen
:
3698 case CK_BottomOnScreen
:
3699 case CK_MarkToPageBegin
:
3700 case CK_MarkToPageEnd
:
3705 case CK_MarkToWordBegin
:
3706 case CK_MarkToWordEnd
:
3709 case CK_MarkColumnUp
:
3710 case CK_MarkColumnDown
:
3711 if (edit
->mark2
== -1)
3712 break; /*marking is following the cursor: may need to highlight a whole line */
3717 edit
->force
|= REDRAW_CHAR_ONLY
;
3720 /* basic cursor key commands */
3724 /* if non persistent selection and text selected */
3725 if (!option_persistent_selections
)
3727 if (edit
->mark1
!= edit
->mark2
)
3729 edit_block_delete_cmd (edit
);
3733 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3738 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3740 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3741 edit_backspace (edit
, 1);
3746 if (option_fake_half_tabs
)
3749 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3751 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3752 edit_backspace (edit
, 1);
3757 edit_backspace (edit
, 0);
3760 /* if non persistent selection and text selected */
3761 if (!option_persistent_selections
)
3763 if (edit
->mark1
!= edit
->mark2
)
3765 edit_block_delete_cmd (edit
);
3770 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3771 edit_insert_over (edit
);
3773 if (option_fake_half_tabs
)
3776 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3778 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3779 edit_delete (edit
, 1);
3783 edit_delete (edit
, 0);
3785 case CK_DeleteToWordBegin
:
3787 edit_left_delete_word (edit
);
3789 case CK_DeleteToWordEnd
:
3790 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3791 edit_insert_over (edit
);
3793 edit_right_delete_word (edit
);
3796 edit_delete_line (edit
);
3798 case CK_DeleteToHome
:
3799 edit_delete_to_line_begin (edit
);
3801 case CK_DeleteToEnd
:
3802 edit_delete_to_line_end (edit
);
3806 if (option_auto_para_formatting
)
3808 edit_double_newline (edit
);
3809 if (option_return_does_auto_indent
)
3810 edit_auto_indent (edit
);
3811 format_paragraph (edit
, 0);
3815 edit_insert (edit
, '\n');
3816 if (option_return_does_auto_indent
)
3818 edit_auto_indent (edit
);
3823 edit_insert (edit
, '\n');
3826 case CK_MarkColumnPageUp
:
3827 edit
->column_highlight
= 1;
3830 edit_move_up (edit
, edit
->widget
.lines
- 1, 1);
3832 case CK_MarkColumnPageDown
:
3833 edit
->column_highlight
= 1;
3835 case CK_MarkPageDown
:
3836 edit_move_down (edit
, edit
->widget
.lines
- 1, 1);
3838 case CK_MarkColumnLeft
:
3839 edit
->column_highlight
= 1;
3842 if (option_fake_half_tabs
)
3844 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3846 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3849 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3850 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3854 edit_left_char_move_cmd (edit
);
3856 case CK_MarkColumnRight
:
3857 edit
->column_highlight
= 1;
3860 if (option_fake_half_tabs
)
3862 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3864 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3865 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3869 edit_right_char_move_cmd (edit
);
3871 case CK_TopOnScreen
:
3872 case CK_MarkToPageBegin
:
3873 edit_begin_page (edit
);
3875 case CK_BottomOnScreen
:
3876 case CK_MarkToPageEnd
:
3877 edit_end_page (edit
);
3880 case CK_MarkToWordBegin
:
3882 edit_left_word_move_cmd (edit
);
3885 case CK_MarkToWordEnd
:
3887 edit_right_word_move_cmd (edit
);
3889 case CK_MarkColumnUp
:
3890 edit
->column_highlight
= 1;
3893 edit_move_up (edit
, 1, 0);
3895 case CK_MarkColumnDown
:
3896 edit
->column_highlight
= 1;
3899 edit_move_down (edit
, 1, 0);
3901 case CK_MarkColumnParagraphUp
:
3902 edit
->column_highlight
= 1;
3903 case CK_ParagraphUp
:
3904 case CK_MarkParagraphUp
:
3905 edit_move_up_paragraph (edit
, 0);
3907 case CK_MarkColumnParagraphDown
:
3908 edit
->column_highlight
= 1;
3909 case CK_ParagraphDown
:
3910 case CK_MarkParagraphDown
:
3911 edit_move_down_paragraph (edit
, 0);
3913 case CK_MarkColumnScrollUp
:
3914 edit
->column_highlight
= 1;
3916 case CK_MarkScrollUp
:
3917 edit_move_up (edit
, 1, 1);
3919 case CK_MarkColumnScrollDown
:
3920 edit
->column_highlight
= 1;
3922 case CK_MarkScrollDown
:
3923 edit_move_down (edit
, 1, 1);
3927 edit_cursor_to_bol (edit
);
3931 edit_cursor_to_eol (edit
);
3934 /* if text marked shift block */
3935 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3937 if (edit
->mark2
< 0)
3938 edit_mark_cmd (edit
, 0);
3939 edit_move_block_to_right (edit
);
3943 if (option_cursor_beyond_eol
)
3944 edit_insert_over (edit
);
3945 edit_tab_cmd (edit
);
3946 if (option_auto_para_formatting
)
3948 format_paragraph (edit
, 0);
3949 edit
->force
|= REDRAW_PAGE
;
3953 check_and_wrap_line (edit
);
3958 case CK_InsertOverwrite
:
3959 edit
->overwrite
= !edit
->overwrite
;
3963 if (edit
->mark2
>= 0)
3965 if (edit
->column_highlight
)
3966 edit_push_undo_action (edit
, COLUMN_ON
);
3967 edit
->column_highlight
= 0;
3969 edit_mark_cmd (edit
, 0);
3972 if (!edit
->column_highlight
)
3973 edit_push_undo_action (edit
, COLUMN_OFF
);
3974 edit
->column_highlight
= 1;
3975 edit_mark_cmd (edit
, 0);
3978 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3979 edit
->force
|= REDRAW_PAGE
;
3982 if (edit
->column_highlight
)
3983 edit_push_undo_action (edit
, COLUMN_ON
);
3984 edit
->column_highlight
= 0;
3985 edit_mark_cmd (edit
, 1);
3988 if (edit
->column_highlight
)
3989 edit_push_undo_action (edit
, COLUMN_ON
);
3990 edit
->column_highlight
= 0;
3991 edit_mark_current_word_cmd (edit
);
3994 if (edit
->column_highlight
)
3995 edit_push_undo_action (edit
, COLUMN_ON
);
3996 edit
->column_highlight
= 0;
3997 edit_mark_current_line_cmd (edit
);
4000 case CK_ShowNumbers
:
4001 option_line_state
= !option_line_state
;
4002 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
4003 edit
->force
|= REDRAW_PAGE
;
4007 show_right_margin
= !show_right_margin
;
4008 edit
->force
|= REDRAW_PAGE
;
4012 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
4013 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
4014 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4016 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4018 case CK_BookmarkFlush
:
4019 book_mark_flush (edit
, BOOK_MARK_COLOR
);
4020 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
4021 edit
->force
|= REDRAW_PAGE
;
4023 case CK_BookmarkNext
:
4024 if (edit
->book_mark
)
4026 struct _book_mark
*p
;
4027 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4031 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4032 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4033 edit_move_to_line (edit
, p
->line
);
4037 case CK_BookmarkPrev
:
4038 if (edit
->book_mark
)
4040 struct _book_mark
*p
;
4041 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4042 while (p
->line
== edit
->curs_line
)
4047 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4048 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4049 edit_move_to_line (edit
, p
->line
);
4055 case CK_MarkToFileBegin
:
4056 edit_move_to_top (edit
);
4059 case CK_MarkToFileEnd
:
4060 edit_move_to_bottom (edit
);
4064 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4065 edit_insert_over (edit
);
4066 edit_block_copy_cmd (edit
);
4069 edit_block_delete_cmd (edit
);
4072 edit_block_move_cmd (edit
);
4075 case CK_BlockShiftLeft
:
4076 if (edit
->mark1
!= edit
->mark2
)
4077 edit_move_block_to_left (edit
);
4079 case CK_BlockShiftRight
:
4080 if (edit
->mark1
!= edit
->mark2
)
4081 edit_move_block_to_right (edit
);
4084 edit_copy_to_X_buf_cmd (edit
);
4087 edit_cut_to_X_buf_cmd (edit
);
4090 /* if non persistent selection and text selected */
4091 if (!option_persistent_selections
)
4093 if (edit
->mark1
!= edit
->mark2
)
4094 edit_block_delete_cmd (edit
);
4096 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4097 edit_insert_over (edit
);
4098 edit_paste_from_X_buf_cmd (edit
);
4101 edit_paste_from_history (edit
);
4105 edit_save_as_cmd (edit
);
4108 edit_save_confirm_cmd (edit
);
4111 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
4114 edit_save_block_cmd (edit
);
4117 edit_insert_file_cmd (edit
);
4121 edit_load_back_cmd (edit
);
4124 edit_load_forward_cmd (edit
);
4127 case CK_EditSyntaxFile
:
4128 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
4130 case CK_SyntaxChoose
:
4131 edit_syntax_dialog (edit
);
4134 case CK_EditUserMenu
:
4135 edit_load_cmd (edit
, EDIT_FILE_MENU
);
4138 case CK_SyntaxOnOff
:
4139 option_syntax_highlighting
^= 1;
4140 if (option_syntax_highlighting
== 1)
4141 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
4142 edit
->force
|= REDRAW_PAGE
;
4146 enable_show_tabs_tws
^= 1;
4147 edit
->force
|= REDRAW_PAGE
;
4151 edit_search_cmd (edit
, FALSE
);
4153 case CK_SearchContinue
:
4154 edit_search_cmd (edit
, TRUE
);
4157 edit_replace_cmd (edit
, 0);
4159 case CK_ReplaceContinue
:
4160 edit_replace_cmd (edit
, 1);
4163 /* if text marked shift block */
4164 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
4166 edit_move_block_to_left (edit
);
4170 edit_complete_word_cmd (edit
);
4174 edit_get_match_keyword_cmd (edit
);
4177 dlg_stop (edit
->widget
.owner
);
4180 edit_new_cmd (edit
);
4183 edit_help_cmd (edit
);
4186 edit_refresh_cmd (edit
);
4198 edit_options_dialog (edit
);
4200 case CK_OptionsSaveMode
:
4201 menu_save_mode_cmd ();
4206 /* fool gcc to prevent a Y2K warning */
4207 char time_format
[] = "_c";
4208 time_format
[0] = '%';
4210 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4211 edit_print_string (edit
, s
);
4212 edit
->force
|= REDRAW_PAGE
;
4217 edit_goto_cmd (edit
);
4219 case CK_ParagraphFormat
:
4220 format_paragraph (edit
, 1);
4221 edit
->force
|= REDRAW_PAGE
;
4223 case CK_MacroDelete
:
4224 edit_delete_macro_cmd (edit
);
4226 case CK_MatchBracket
:
4227 edit_goto_matching_bracket (edit
);
4230 user_menu (edit
, NULL
, -1);
4233 edit_sort_cmd (edit
);
4235 case CK_ExternalCommand
:
4236 edit_ext_cmd (edit
);
4239 edit_mail_dialog (edit
);
4245 case CK_SelectCodepage
:
4246 edit_select_codepage_cmd (edit
);
4249 case CK_InsertLiteral
:
4250 edit_insert_literal_cmd (edit
);
4252 case CK_MacroStartStopRecord
:
4253 edit_begin_end_macro_cmd (edit
);
4255 case CK_RepeatStartStopRecord
:
4256 edit_begin_end_repeat_cmd (edit
);
4258 case CK_ExtendedKeyMap
:
4259 edit
->extmod
= TRUE
;
4266 if ((command
/ CK_PipeBlock (0)) == 1)
4267 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
4269 /* keys which must set the col position, and the search vars */
4273 case CK_SearchContinue
:
4275 case CK_ReplaceContinue
:
4277 edit
->prev_col
= edit_get_col (edit
);
4281 case CK_MarkColumnUp
:
4284 case CK_MarkColumnDown
:
4287 case CK_MarkColumnPageUp
:
4289 case CK_MarkPageDown
:
4290 case CK_MarkColumnPageDown
:
4292 case CK_MarkToFileBegin
:
4294 case CK_MarkToFileEnd
:
4295 case CK_ParagraphUp
:
4296 case CK_MarkParagraphUp
:
4297 case CK_MarkColumnParagraphUp
:
4298 case CK_ParagraphDown
:
4299 case CK_MarkParagraphDown
:
4300 case CK_MarkColumnParagraphDown
:
4302 case CK_MarkScrollUp
:
4303 case CK_MarkColumnScrollUp
:
4305 case CK_MarkScrollDown
:
4306 case CK_MarkColumnScrollDown
:
4307 edit
->search_start
= edit
->curs1
;
4308 edit
->found_len
= 0;
4311 edit
->found_len
= 0;
4312 edit
->prev_col
= edit_get_col (edit
);
4313 edit
->search_start
= edit
->curs1
;
4315 edit_find_bracket (edit
);
4317 if (option_auto_para_formatting
)
4323 case CK_DeleteToWordBegin
:
4324 case CK_DeleteToWordEnd
:
4325 case CK_DeleteToHome
:
4326 case CK_DeleteToEnd
:
4327 format_paragraph (edit
, 0);
4328 edit
->force
|= REDRAW_PAGE
;
4333 /* --------------------------------------------------------------------------------------------- */
4336 edit_stack_init (void)
4338 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4340 edit_history_moveto
[edit_stack_iterator
].filename_vpath
= NULL
;
4341 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4344 edit_stack_iterator
= 0;
4347 /* --------------------------------------------------------------------------------------------- */
4350 edit_stack_free (void)
4352 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4353 vfs_path_free (edit_history_moveto
[edit_stack_iterator
].filename_vpath
);
4356 /* --------------------------------------------------------------------------------------------- */
4360 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
4362 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4365 /* --------------------------------------------------------------------------------------------- */
4369 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
4371 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4374 /* --------------------------------------------------------------------------------------------- */