2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2008, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
10 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 * \brief Source: editor low level data handling and cursor fundamentals
37 #include <sys/types.h>
46 #include "lib/global.h"
48 #include "lib/tty/color.h"
49 #include "lib/tty/tty.h" /* attrset() */
50 #include "lib/tty/key.h" /* is_idle() */
51 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
52 #include "lib/vfs/vfs.h"
53 #include "lib/strutil.h" /* utf string functions */
54 #include "lib/util.h" /* load_file_position(), save_file_position() */
55 #include "lib/timefmt.h" /* time formatting */
57 #include "lib/widget.h"
60 #include "lib/charsets.h" /* get_codepage_id */
63 #include "src/filemanager/cmd.h" /* view_other_cmd() */
64 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
66 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/main.h" /* macro_index */
68 #include "src/learn.h" /* learn_keys */
69 #include "src/keybind-defaults.h"
71 #include "edit-impl.h"
72 #include "edit-widget.h"
74 /*** global variables ****************************************************************************/
76 int option_word_wrap_line_length
= DEFAULT_WRAP_LINE_LENGTH
;
77 int option_typewriter_wrap
= 0;
78 int option_auto_para_formatting
= 0;
79 int option_fill_tabs_with_spaces
= 0;
80 int option_return_does_auto_indent
= 1;
81 int option_backspace_through_tabs
= 0;
82 int option_fake_half_tabs
= 1;
83 int option_save_mode
= EDIT_QUICK_SAVE
;
84 int option_save_position
= 1;
85 int option_max_undo
= 32768;
86 int option_persistent_selections
= 1;
87 int option_cursor_beyond_eol
= 0;
88 int option_line_state
= 0;
89 int option_line_state_width
= 0;
91 int option_edit_right_extreme
= 0;
92 int option_edit_left_extreme
= 0;
93 int option_edit_top_extreme
= 0;
94 int option_edit_bottom_extreme
= 0;
95 int enable_show_tabs_tws
= 1;
96 int option_check_nl_at_eof
= 0;
97 int option_group_undo
= 0;
98 int show_right_margin
= 0;
100 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
101 char *option_backup_ext
= NULL
;
103 int edit_stack_iterator
= 0;
104 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
105 /* magic sequense for say than block is vertical */
106 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
108 /*** file scope macro definitions ****************************************************************/
110 #define TEMP_BUF_LEN 1024
112 #define space_width 1
114 /*** file scope type declarations ****************************************************************/
116 /*** file scope variables ************************************************************************/
118 /* detecting an error on save is easy: just check if every byte has been written. */
119 /* detecting an error on read, is not so easy 'cos there is not way to tell
120 whether you read everything or not. */
121 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
122 static const struct edit_filters
124 const char *read
, *write
, *extension
;
128 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
129 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
130 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
131 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
132 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
136 static long last_bracket
= -1;
138 /*** file scope functions ************************************************************************/
139 /* --------------------------------------------------------------------------------------------- */
143 * here's a quick sketch of the layout: (don't run this through indent.)
145 * (b1 is buffers1 and b2 is buffers2)
148 * \0\0\0\0\0m e _ f i l e . \nf i n . \n|T h i s _ i s _ s o\0\0\0\0\0\0\0\0\0
149 * ______________________________________|______________________________________
151 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
152 * |-> |-> |-> |-> |-> |-> |
154 * _<------------------------->|<----------------->_
155 * WEdit->curs2 | WEdit->curs1
160 * file end|||file beginning
169 /* --------------------------------------------------------------------------------------------- */
171 static int left_of_four_spaces (WEdit
* edit
);
173 /* --------------------------------------------------------------------------------------------- */
178 const char *header
= N_("About");
179 const char *button_name
= N_("&OK");
180 const char *const version
= "MCEdit " VERSION
;
181 char text
[BUF_LARGE
];
183 int win_len
, version_len
, button_len
;
190 button_name
= _(button_name
);
193 button_len
= str_term_width1 (button_name
) + 5;
194 version_len
= str_term_width1 (version
);
196 g_snprintf (text
, sizeof (text
),
197 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
198 " A user friendly text editor\n"
199 " written for the Midnight Commander"));
201 win_len
= str_term_width1 (header
);
202 win_len
= max (win_len
, version_len
);
203 win_len
= max (win_len
, button_len
);
205 /* count width and height of text */
206 str_msg_term_size (text
, &lines
, &cols
);
208 cols
= max (win_len
, cols
) + 6;
211 about_dlg
= create_dlg (TRUE
, 0, 0, lines
, cols
, dialog_colors
, NULL
, NULL
,
212 "[Internal File Editor]", header
, DLG_CENTER
| DLG_TRYUP
);
214 add_widget (about_dlg
, label_new (3, (cols
- version_len
) / 2, version
));
215 add_widget (about_dlg
, label_new (5, 3, text
));
216 add_widget (about_dlg
, button_new (lines
- 3, (cols
- button_len
) / 2,
217 B_ENTER
, NORMAL_BUTTON
, button_name
, NULL
));
220 destroy_dlg (about_dlg
);
223 /* --------------------------------------------------------------------------------------------- */
225 * Initialize the buffers for an empty files.
229 edit_init_buffers (WEdit
* edit
)
233 for (j
= 0; j
<= MAXBUFF
; j
++)
235 edit
->buffers1
[j
] = NULL
;
236 edit
->buffers2
[j
] = NULL
;
241 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
244 /* --------------------------------------------------------------------------------------------- */
246 * Load file OR text into buffers. Set cursor to the beginning of file.
247 * @returns 1 on error.
251 edit_load_file_fast (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
257 edit
->curs2
= edit
->last_byte
;
258 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
260 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
263 gchar
*errmsg
, *filename
;
265 filename
= vfs_path_to_str (filename_vpath
);
266 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
268 edit_error_dialog (_("Error"), errmsg
);
273 if (!edit
->buffers2
[buf2
])
274 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
279 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
280 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
283 for (buf
= buf2
- 1; buf
>= 0; buf
--)
285 /* edit->buffers2[0] is already allocated */
286 if (!edit
->buffers2
[buf
])
287 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
288 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
297 gchar
*errmsg
, *filename
;
299 filename
= vfs_path_to_str (filename_vpath
);
300 errmsg
= g_strdup_printf (_("Error reading %s"), filename
);
302 edit_error_dialog (_("Error"), errmsg
);
309 /* --------------------------------------------------------------------------------------------- */
310 /** Return index of the filter or -1 is there is no appropriate filter */
313 edit_find_filter (const vfs_path_t
* filename_vpath
)
318 if (filename_vpath
== NULL
)
321 filename
= vfs_path_to_str (filename_vpath
);
322 l
= strlen (filename
);
323 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
325 e
= strlen (all_filters
[i
].extension
);
327 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
337 /* --------------------------------------------------------------------------------------------- */
340 edit_get_filter (const vfs_path_t
* filename_vpath
)
343 char *p
, *quoted_name
, *filename
;
345 i
= edit_find_filter (filename_vpath
);
349 filename
= vfs_path_to_str (filename_vpath
);
350 quoted_name
= name_quote (filename
, 0);
352 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
353 g_free (quoted_name
);
357 /* --------------------------------------------------------------------------------------------- */
360 edit_insert_stream (WEdit
* edit
, FILE * f
)
364 while ((c
= fgetc (f
)) >= 0)
366 edit_insert (edit
, c
);
372 /* --------------------------------------------------------------------------------------------- */
373 /** Open file and create it if necessary. Return 0 for success, 1 for error. */
376 check_file_access (WEdit
* edit
, const vfs_path_t
* filename_vpath
, struct stat
*st
)
379 gchar
*errmsg
= NULL
;
381 /* Try opening an existing file */
382 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
386 * Try creating the file. O_EXCL prevents following broken links
387 * and opening existing files.
389 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
394 filename
= vfs_path_to_str (filename_vpath
);
395 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
401 /* New file, delete it if it's not modified or saved */
402 edit
->delete_file
= 1;
406 /* Check what we have opened */
407 if (mc_fstat (file
, st
) < 0)
411 filename
= vfs_path_to_str (filename_vpath
);
412 errmsg
= g_strdup_printf (_("Cannot get size/permissions for %s"), filename
);
417 /* We want to open regular files only */
418 if (!S_ISREG (st
->st_mode
))
422 filename
= vfs_path_to_str (filename_vpath
);
423 errmsg
= g_strdup_printf (_("\"%s\" is not a regular file"), filename
);
429 * Don't delete non-empty files.
430 * O_EXCL should prevent it, but let's be on the safe side.
433 edit
->delete_file
= 0;
435 if (st
->st_size
>= SIZE_LIMIT
)
439 filename
= vfs_path_to_str (filename_vpath
);
440 errmsg
= g_strdup_printf (_("File \"%s\" is too large"), filename
);
445 (void) mc_close (file
);
449 edit_error_dialog (_("Error"), errmsg
);
456 /* --------------------------------------------------------------------------------------------- */
458 * Open the file and load it into the buffers, either directly or using
459 * a filter. Return 0 on success, 1 on error.
461 * Fast loading (edit_load_file_fast) is used when the file size is
462 * known. In this case the data is read into the buffers by blocks.
463 * If the file size is not known, the data is loaded byte by byte in
468 edit_load_file (WEdit
* edit
)
472 /* Cannot do fast load if a filter is used */
473 if (edit_find_filter (edit
->filename_vpath
) >= 0)
478 * FIXME: line end translation should disable fast loading as well
479 * Consider doing fseek() to the end and ftell() for the real size.
482 if (edit
->filename_vpath
!= NULL
)
486 * VFS may report file size incorrectly, and slow load is not a big
487 * deal considering overhead in VFS.
489 if (!vfs_file_is_local (edit
->filename_vpath
))
492 /* If we are dealing with a real file, check that it exists */
493 if (check_file_access (edit
, edit
->filename_vpath
, &edit
->stat1
))
498 /* nothing to load */
502 edit_init_buffers (edit
);
506 edit
->last_byte
= edit
->stat1
.st_size
;
507 edit_load_file_fast (edit
, edit
->filename_vpath
);
508 /* If fast load was used, the number of lines wasn't calculated */
509 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
514 if (edit
->filename_vpath
!= NULL
515 && *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) != '\0')
517 edit
->undo_stack_disable
= 1;
518 if (edit_insert_file (edit
, edit
->filename_vpath
) < 0)
523 edit
->undo_stack_disable
= 0;
530 /* --------------------------------------------------------------------------------------------- */
531 /** Restore saved cursor position in the file */
534 edit_load_position (WEdit
* edit
)
539 if (edit
->filename_vpath
== NULL
540 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
543 load_file_position (edit
->filename_vpath
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
547 edit_move_to_line (edit
, line
- 1);
548 edit
->prev_col
= column
;
552 edit_cursor_move (edit
, offset
);
553 line
= edit
->curs_line
;
554 edit
->search_start
= edit
->curs1
;
557 book_mark_restore (edit
, BOOK_MARK_COLOR
);
559 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
560 edit_move_display (edit
, line
- (edit
->widget
.lines
/ 2));
563 /* --------------------------------------------------------------------------------------------- */
564 /** Save cursor position in the file */
567 edit_save_position (WEdit
* edit
)
569 if (edit
->filename_vpath
== NULL
570 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
573 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
574 save_file_position (edit
->filename_vpath
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
,
575 edit
->serialized_bookmarks
);
576 edit
->serialized_bookmarks
= NULL
;
579 /* --------------------------------------------------------------------------------------------- */
580 /** Clean the WEdit stricture except the widget part */
583 edit_purge_widget (WEdit
* edit
)
585 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
586 char *start
= (char *) edit
+ sizeof (Widget
);
587 memset (start
, 0, len
);
590 /* --------------------------------------------------------------------------------------------- */
593 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
594 then the file should be as it was when he loaded up. Then set edit->modified to 0.
598 edit_pop_undo_action (WEdit
* edit
)
601 unsigned long sp
= edit
->undo_stack_pointer
;
603 if (sp
== edit
->undo_stack_bottom
)
606 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
607 c
= edit
->undo_stack
[sp
];
610 /* edit->undo_stack[sp] = '@'; */
611 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
615 if (sp
== edit
->undo_stack_bottom
)
618 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
619 if (edit
->undo_stack
[sp
] == -2)
621 /* edit->undo_stack[sp] = '@'; */
622 edit
->undo_stack_pointer
= sp
;
625 edit
->undo_stack
[sp
]++;
631 edit_pop_redo_action (WEdit
* edit
)
634 unsigned long sp
= edit
->redo_stack_pointer
;
636 if (sp
== edit
->redo_stack_bottom
)
639 sp
= (sp
- 1) & edit
->redo_stack_size_mask
;
640 c
= edit
->redo_stack
[sp
];
643 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
647 if (sp
== edit
->redo_stack_bottom
)
650 c
= edit
->redo_stack
[(sp
- 1) & edit
->redo_stack_size_mask
];
651 if (edit
->redo_stack
[sp
] == -2)
652 edit
->redo_stack_pointer
= sp
;
654 edit
->redo_stack
[sp
]++;
660 get_prev_undo_action (WEdit
* edit
)
663 unsigned long sp
= edit
->undo_stack_pointer
;
665 if (sp
== edit
->undo_stack_bottom
)
668 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
669 c
= edit
->undo_stack
[sp
];
673 if (sp
== edit
->undo_stack_bottom
)
676 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
680 /* --------------------------------------------------------------------------------------------- */
681 /** is called whenever a modification is made by one of the four routines below */
684 edit_modification (WEdit
* edit
)
686 edit
->caches_valid
= 0;
688 /* raise lock when file modified */
689 if (!edit
->modified
&& !edit
->delete_file
)
690 edit
->locked
= lock_file (edit
->filename_vpath
);
694 /* --------------------------------------------------------------------------------------------- */
697 edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
699 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
702 if (byte_index
>= edit
->curs1
)
706 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
707 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
708 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
711 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
712 (byte_index
& M_EDIT_BUF_SIZE
));
715 /* --------------------------------------------------------------------------------------------- */
718 edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
721 gchar utf8_buf
[3 * UTF8_CHAR_LEN
+ 1];
723 gchar
*cursor_buf_ptr
;
725 if (byte_index
> (edit
->curs1
+ edit
->curs2
) || byte_index
<= 0)
731 for (i
= 0; i
< (3 * UTF8_CHAR_LEN
); i
++)
732 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
- (2 * UTF8_CHAR_LEN
));
733 utf8_buf
[3 * UTF8_CHAR_LEN
] = '\0';
735 cursor_buf_ptr
= utf8_buf
+ (2 * UTF8_CHAR_LEN
);
736 str
= g_utf8_find_prev_char (utf8_buf
, cursor_buf_ptr
);
738 if (str
== NULL
|| g_utf8_next_char (str
) != cursor_buf_ptr
)
741 return *(cursor_buf_ptr
- 1);
745 res
= g_utf8_get_char_validated (str
, -1);
750 return *(cursor_buf_ptr
- 1);
754 *char_width
= cursor_buf_ptr
- str
;
760 /* --------------------------------------------------------------------------------------------- */
763 edit_backspace (WEdit
* edit
, const int byte_delete
)
774 if (edit
->mark2
!= edit
->mark1
)
775 edit_push_markers (edit
);
777 if (edit
->utf8
&& byte_delete
== 0)
779 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
783 for (i
= 1; i
<= cw
; i
++)
785 if (edit
->mark1
>= edit
->curs1
)
788 edit
->end_mark_curs
--;
790 if (edit
->mark2
>= edit
->curs1
)
792 if (edit
->last_get_rule
>= edit
->curs1
)
793 edit
->last_get_rule
--;
795 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
796 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
797 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
799 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
800 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
804 edit_push_undo_action (edit
, p
);
806 edit_modification (edit
);
810 book_mark_dec (edit
, edit
->curs_line
);
813 edit
->force
|= REDRAW_AFTER_CURSOR
;
816 if (edit
->curs1
< edit
->start_display
)
818 edit
->start_display
--;
826 /* --------------------------------------------------------------------------------------------- */
827 /* high level cursor movement commands */
828 /* --------------------------------------------------------------------------------------------- */
831 is_in_indent (WEdit
* edit
)
833 long p
= edit_bol (edit
, edit
->curs1
);
834 while (p
< edit
->curs1
)
835 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
840 /* --------------------------------------------------------------------------------------------- */
843 is_blank (WEdit
* edit
, long offset
)
847 s
= edit_bol (edit
, offset
);
848 f
= edit_eol (edit
, offset
) - 1;
851 c
= edit_get_byte (edit
, s
++);
859 /* --------------------------------------------------------------------------------------------- */
860 /** returns the offset of line i */
863 edit_find_line (WEdit
* edit
, int line
)
867 if (!edit
->caches_valid
)
869 for (i
= 0; i
< N_LINE_CACHES
; i
++)
870 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
871 /* three offsets that we *know* are line 0 at 0 and these two: */
872 edit
->line_numbers
[1] = edit
->curs_line
;
873 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
874 edit
->line_numbers
[2] = edit
->total_lines
;
875 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
876 edit
->caches_valid
= 1;
878 if (line
>= edit
->total_lines
)
879 return edit
->line_offsets
[2];
882 /* find the closest known point */
883 for (i
= 0; i
< N_LINE_CACHES
; i
++)
886 n
= abs (edit
->line_numbers
[i
] - line
);
894 return edit
->line_offsets
[j
]; /* know the offset exactly */
895 if (m
== 1 && j
>= 3)
896 i
= j
; /* one line different - caller might be looping, so stay in this cache */
898 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
899 if (line
> edit
->line_numbers
[j
])
900 edit
->line_offsets
[i
] =
901 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
903 edit
->line_offsets
[i
] =
904 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
905 edit
->line_numbers
[i
] = line
;
906 return edit
->line_offsets
[i
];
909 /* --------------------------------------------------------------------------------------------- */
910 /** moves up until a blank line is reached, or until just
911 before a non-blank line is reached */
914 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
917 if (edit
->curs_line
> 1)
919 if (line_is_blank (edit
, edit
->curs_line
))
921 if (line_is_blank (edit
, edit
->curs_line
- 1))
923 for (i
= edit
->curs_line
- 1; i
; i
--)
924 if (!line_is_blank (edit
, i
))
932 for (i
= edit
->curs_line
- 1; i
; i
--)
933 if (line_is_blank (edit
, i
))
939 for (i
= edit
->curs_line
- 1; i
; i
--)
940 if (line_is_blank (edit
, i
))
944 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
947 /* --------------------------------------------------------------------------------------------- */
948 /** moves down until a blank line is reached, or until just
949 before a non-blank line is reached */
952 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
955 if (edit
->curs_line
>= edit
->total_lines
- 1)
957 i
= edit
->total_lines
;
961 if (line_is_blank (edit
, edit
->curs_line
))
963 if (line_is_blank (edit
, edit
->curs_line
+ 1))
965 for (i
= edit
->curs_line
+ 1; i
; i
++)
966 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
974 for (i
= edit
->curs_line
+ 1; i
; i
++)
975 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
981 for (i
= edit
->curs_line
+ 1; i
; i
++)
982 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
986 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
989 /* --------------------------------------------------------------------------------------------- */
992 edit_begin_page (WEdit
* edit
)
994 edit_update_curs_row (edit
);
995 edit_move_up (edit
, edit
->curs_row
, 0);
998 /* --------------------------------------------------------------------------------------------- */
1001 edit_end_page (WEdit
* edit
)
1003 edit_update_curs_row (edit
);
1004 edit_move_down (edit
, edit
->widget
.lines
- edit
->curs_row
- 1, 0);
1008 /* --------------------------------------------------------------------------------------------- */
1009 /** goto beginning of text */
1012 edit_move_to_top (WEdit
* edit
)
1014 if (edit
->curs_line
)
1016 edit_cursor_move (edit
, -edit
->curs1
);
1017 edit_move_to_prev_col (edit
, 0);
1018 edit
->force
|= REDRAW_PAGE
;
1019 edit
->search_start
= 0;
1020 edit_update_curs_row (edit
);
1025 /* --------------------------------------------------------------------------------------------- */
1026 /** goto end of text */
1029 edit_move_to_bottom (WEdit
* edit
)
1031 if (edit
->curs_line
< edit
->total_lines
)
1033 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
1034 edit
->start_display
= edit
->last_byte
;
1035 edit
->start_line
= edit
->total_lines
;
1036 edit_scroll_upward (edit
, edit
->widget
.lines
- 1);
1037 edit
->force
|= REDRAW_PAGE
;
1041 /* --------------------------------------------------------------------------------------------- */
1042 /** goto beginning of line */
1045 edit_cursor_to_bol (WEdit
* edit
)
1047 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1048 edit
->search_start
= edit
->curs1
;
1049 edit
->prev_col
= edit_get_col (edit
);
1053 /* --------------------------------------------------------------------------------------------- */
1054 /** goto end of line */
1057 edit_cursor_to_eol (WEdit
* edit
)
1059 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1060 edit
->search_start
= edit
->curs1
;
1061 edit
->prev_col
= edit_get_col (edit
);
1065 /* --------------------------------------------------------------------------------------------- */
1067 static unsigned long
1072 const char option_chars_move_whole_word
[] =
1073 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1079 if (*option_chars_move_whole_word
== '!')
1081 return 0x80000000UL
;
1083 if (g_ascii_isupper ((gchar
) c
))
1085 else if (g_ascii_islower ((gchar
) c
))
1087 else if (g_ascii_isalpha (c
))
1089 else if (isdigit (c
))
1091 else if (isspace (c
))
1093 q
= strchr (option_chars_move_whole_word
, c
);
1095 return 0xFFFFFFFFUL
;
1098 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1103 while ((q
= strchr (q
+ 1, c
)));
1107 /* --------------------------------------------------------------------------------------------- */
1110 edit_left_word_move (WEdit
* edit
, int s
)
1115 if (edit
->column_highlight
1116 && edit
->mark1
!= edit
->mark2
1117 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1119 edit_cursor_move (edit
, -1);
1122 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1123 c2
= edit_get_byte (edit
, edit
->curs1
);
1124 if (c1
== '\n' || c2
== '\n')
1126 if (!(my_type_of (c1
) & my_type_of (c2
)))
1128 if (isspace (c1
) && !isspace (c2
))
1131 if (!isspace (c1
) && isspace (c2
))
1136 /* --------------------------------------------------------------------------------------------- */
1139 edit_left_word_move_cmd (WEdit
* edit
)
1141 edit_left_word_move (edit
, 0);
1142 edit
->force
|= REDRAW_PAGE
;
1145 /* --------------------------------------------------------------------------------------------- */
1148 edit_right_word_move (WEdit
* edit
, int s
)
1153 if (edit
->column_highlight
1154 && edit
->mark1
!= edit
->mark2
1155 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
1157 edit_cursor_move (edit
, 1);
1158 if (edit
->curs1
>= edit
->last_byte
)
1160 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1161 c2
= edit_get_byte (edit
, edit
->curs1
);
1162 if (c1
== '\n' || c2
== '\n')
1164 if (!(my_type_of (c1
) & my_type_of (c2
)))
1166 if (isspace (c1
) && !isspace (c2
))
1169 if (!isspace (c1
) && isspace (c2
))
1174 /* --------------------------------------------------------------------------------------------- */
1177 edit_right_word_move_cmd (WEdit
* edit
)
1179 edit_right_word_move (edit
, 0);
1180 edit
->force
|= REDRAW_PAGE
;
1183 /* --------------------------------------------------------------------------------------------- */
1186 edit_right_char_move_cmd (WEdit
* edit
)
1192 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
1198 c
= edit_get_byte (edit
, edit
->curs1
);
1200 if (option_cursor_beyond_eol
&& c
== '\n')
1206 edit_cursor_move (edit
, cw
);
1210 /* --------------------------------------------------------------------------------------------- */
1213 edit_left_char_move_cmd (WEdit
* edit
)
1216 if (edit
->column_highlight
1217 && option_cursor_beyond_eol
1218 && edit
->mark1
!= edit
->mark2
1219 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1223 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1227 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1233 edit_cursor_move (edit
, -cw
);
1237 /* --------------------------------------------------------------------------------------------- */
1238 /** Up or down cursor moving.
1239 direction = TRUE - move up
1244 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
1247 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
1256 edit
->force
|= REDRAW_PAGE
;
1260 edit_scroll_upward (edit
, i
);
1262 edit_scroll_downward (edit
, i
);
1264 p
= edit_bol (edit
, edit
->curs1
);
1266 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
1268 edit_cursor_move (edit
, p
- edit
->curs1
);
1270 edit_move_to_prev_col (edit
, p
);
1272 /* search start of current multibyte char (like CJK) */
1273 if (edit
->curs1
+ 1 < edit
->last_byte
)
1275 edit_right_char_move_cmd (edit
);
1276 edit_left_char_move_cmd (edit
);
1279 edit
->search_start
= edit
->curs1
;
1280 edit
->found_len
= 0;
1283 /* --------------------------------------------------------------------------------------------- */
1286 edit_right_delete_word (WEdit
* edit
)
1291 if (edit
->curs1
>= edit
->last_byte
)
1293 c1
= edit_delete (edit
, 1);
1294 c2
= edit_get_byte (edit
, edit
->curs1
);
1295 if (c1
== '\n' || c2
== '\n')
1297 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1299 if (!(my_type_of (c1
) & my_type_of (c2
)))
1304 /* --------------------------------------------------------------------------------------------- */
1307 edit_left_delete_word (WEdit
* edit
)
1312 if (edit
->curs1
<= 0)
1314 c1
= edit_backspace (edit
, 1);
1315 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1316 if (c1
== '\n' || c2
== '\n')
1318 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1320 if (!(my_type_of (c1
) & my_type_of (c2
)))
1325 /* --------------------------------------------------------------------------------------------- */
1327 the start column position is not recorded, and hence does not
1328 undo as it happed. But who would notice.
1332 edit_do_undo (WEdit
* edit
)
1337 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1339 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1346 edit_cursor_move (edit
, 1);
1349 edit_cursor_move (edit
, -1);
1353 edit_backspace (edit
, 1);
1357 edit_delete (edit
, 1);
1360 edit
->column_highlight
= 1;
1363 edit
->column_highlight
= 0;
1366 if (ac
>= 256 && ac
< 512)
1367 edit_insert_ahead (edit
, ac
- 256);
1368 if (ac
>= 0 && ac
< 256)
1369 edit_insert (edit
, ac
);
1371 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1373 edit
->mark1
= ac
- MARK_1
;
1374 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1376 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1378 edit
->mark2
= ac
- MARK_2
;
1379 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1381 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1383 edit
->end_mark_curs
= ac
- MARK_CURS
;
1386 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1389 if (edit
->start_display
> ac
- KEY_PRESS
)
1391 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1392 edit
->force
|= REDRAW_PAGE
;
1394 else if (edit
->start_display
< ac
- KEY_PRESS
)
1396 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1397 edit
->force
|= REDRAW_PAGE
;
1399 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1400 edit_update_curs_row (edit
);
1403 edit
->undo_stack_disable
= 0;
1407 edit_do_redo (WEdit
* edit
)
1412 if (edit
->redo_stack_reset
)
1416 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1423 edit_cursor_move (edit
, 1);
1426 edit_cursor_move (edit
, -1);
1429 edit_backspace (edit
, 1);
1432 edit_delete (edit
, 1);
1435 edit
->column_highlight
= 1;
1438 edit
->column_highlight
= 0;
1441 if (ac
>= 256 && ac
< 512)
1442 edit_insert_ahead (edit
, ac
- 256);
1443 if (ac
>= 0 && ac
< 256)
1444 edit_insert (edit
, ac
);
1446 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1448 edit
->mark1
= ac
- MARK_1
;
1449 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1451 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1453 edit
->mark2
= ac
- MARK_2
;
1454 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1456 /* more than one pop usually means something big */
1458 edit
->force
|= REDRAW_PAGE
;
1461 if (edit
->start_display
> ac
- KEY_PRESS
)
1463 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1464 edit
->force
|= REDRAW_PAGE
;
1466 else if (edit
->start_display
< ac
- KEY_PRESS
)
1468 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1469 edit
->force
|= REDRAW_PAGE
;
1471 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1472 edit_update_curs_row (edit
);
1478 edit_group_undo (WEdit
* edit
)
1480 long ac
= KEY_PRESS
;
1481 long cur_ac
= KEY_PRESS
;
1482 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1484 cur_ac
= get_prev_undo_action (edit
);
1485 edit_do_undo (edit
);
1486 ac
= get_prev_undo_action (edit
);
1487 /* exit from cycle if option_group_undo is not set,
1488 * and make single UNDO operation
1490 if (!option_group_undo
)
1495 /* --------------------------------------------------------------------------------------------- */
1498 edit_delete_to_line_end (WEdit
* edit
)
1500 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
1504 edit_delete (edit
, 1);
1508 /* --------------------------------------------------------------------------------------------- */
1511 edit_delete_to_line_begin (WEdit
* edit
)
1513 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
1517 edit_backspace (edit
, 1);
1521 /* --------------------------------------------------------------------------------------------- */
1524 is_aligned_on_a_tab (WEdit
* edit
)
1526 edit_update_curs_col (edit
);
1527 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
1528 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
1531 /* --------------------------------------------------------------------------------------------- */
1534 right_of_four_spaces (WEdit
* edit
)
1537 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1538 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1540 return is_aligned_on_a_tab (edit
);
1544 /* --------------------------------------------------------------------------------------------- */
1547 left_of_four_spaces (WEdit
* edit
)
1550 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1551 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1553 return is_aligned_on_a_tab (edit
);
1557 /* --------------------------------------------------------------------------------------------- */
1560 edit_auto_indent (WEdit
* edit
)
1565 /* use the previous line as a template */
1566 p
= edit_move_backward (edit
, p
, 1);
1567 /* copy the leading whitespace of the line */
1569 { /* no range check - the line _is_ \n-terminated */
1570 c
= edit_get_byte (edit
, p
++);
1571 if (c
!= ' ' && c
!= '\t')
1573 edit_insert (edit
, c
);
1577 /* --------------------------------------------------------------------------------------------- */
1580 edit_double_newline (WEdit
* edit
)
1582 edit_insert (edit
, '\n');
1583 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1585 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1587 edit
->force
|= REDRAW_PAGE
;
1588 edit_insert (edit
, '\n');
1592 /* --------------------------------------------------------------------------------------------- */
1595 insert_spaces_tab (WEdit
* edit
, gboolean half
)
1599 edit_update_curs_col (edit
);
1600 i
= option_tab_spacing
* space_width
;
1603 i
= ((edit
->curs_col
/ i
) + 1) * i
- edit
->curs_col
;
1606 edit_insert (edit
, ' ');
1611 /* --------------------------------------------------------------------------------------------- */
1614 edit_tab_cmd (WEdit
* edit
)
1618 if (option_fake_half_tabs
)
1620 if (is_in_indent (edit
))
1622 /*insert a half tab (usually four spaces) unless there is a
1623 half tab already behind, then delete it and insert a
1625 if (option_fill_tabs_with_spaces
|| !right_of_four_spaces (edit
))
1626 insert_spaces_tab (edit
, TRUE
);
1629 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1630 edit_backspace (edit
, 1);
1631 edit_insert (edit
, '\t');
1636 if (option_fill_tabs_with_spaces
)
1637 insert_spaces_tab (edit
, FALSE
);
1639 edit_insert (edit
, '\t');
1642 /* --------------------------------------------------------------------------------------------- */
1645 check_and_wrap_line (WEdit
* edit
)
1648 if (!option_typewriter_wrap
)
1650 edit_update_curs_col (edit
);
1651 if (edit
->curs_col
< option_word_wrap_line_length
)
1657 c
= edit_get_byte (edit
, curs
);
1658 if (c
== '\n' || curs
<= 0)
1660 edit_insert (edit
, '\n');
1663 if (c
== ' ' || c
== '\t')
1665 int current
= edit
->curs1
;
1666 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
1667 edit_insert (edit
, '\n');
1668 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
1674 /* --------------------------------------------------------------------------------------------- */
1675 /** this find the matching bracket in either direction, and sets edit->bracket */
1678 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
1680 const char *const b
= "{}{[][()(", *p
;
1681 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
1682 unsigned long j
= 0;
1684 edit_update_curs_row (edit
);
1685 c
= edit_get_byte (edit
, edit
->curs1
);
1688 if (!furthest_bracket_search
)
1689 furthest_bracket_search
--;
1690 /* not on a bracket at all */
1693 /* the matching bracket */
1695 /* going left or right? */
1696 if (strchr ("{[(", c
))
1698 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
1700 /* out of buffer? */
1701 if (q
>= edit
->last_byte
|| q
< 0)
1703 a
= edit_get_byte (edit
, q
);
1704 /* don't want to eat CPU */
1705 if (j
++ > furthest_bracket_search
)
1707 /* out of screen? */
1710 if (q
< edit
->start_display
)
1712 /* count lines if searching downward */
1713 if (inc
> 0 && a
== '\n')
1714 if (n
++ >= edit
->widget
.lines
- edit
->curs_row
) /* out of screen */
1717 /* count bracket depth */
1718 i
+= (a
== c
) - (a
== d
);
1719 /* return if bracket depth is zero */
1727 /* --------------------------------------------------------------------------------------------- */
1730 edit_goto_matching_bracket (WEdit
* edit
)
1734 q
= edit_get_bracket (edit
, 0, 0);
1737 edit
->bracket
= edit
->curs1
;
1738 edit
->force
|= REDRAW_PAGE
;
1739 edit_cursor_move (edit
, q
- edit
->curs1
);
1743 /* --------------------------------------------------------------------------------------------- */
1746 edit_move_block_to_right (WEdit
* edit
)
1748 long start_mark
, end_mark
;
1749 long cur_bol
, start_bol
;
1751 if (eval_marks (edit
, &start_mark
, &end_mark
))
1754 start_bol
= edit_bol (edit
, start_mark
);
1755 cur_bol
= edit_bol (edit
, end_mark
- 1);
1759 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1760 if (option_fill_tabs_with_spaces
)
1761 insert_spaces_tab (edit
, option_fake_half_tabs
);
1763 edit_insert (edit
, '\t');
1764 edit_cursor_move (edit
, edit_bol (edit
, cur_bol
) - edit
->curs1
);
1769 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1771 while (cur_bol
>= start_bol
);
1773 edit
->force
|= REDRAW_PAGE
;
1776 /* --------------------------------------------------------------------------------------------- */
1779 edit_move_block_to_left (WEdit
* edit
)
1781 long start_mark
, end_mark
;
1782 long cur_bol
, start_bol
;
1785 if (eval_marks (edit
, &start_mark
, &end_mark
))
1788 start_bol
= edit_bol (edit
, start_mark
);
1789 cur_bol
= edit_bol (edit
, end_mark
- 1);
1796 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1798 if (option_fake_half_tabs
)
1799 del_tab_width
= HALF_TAB_SIZE
;
1801 del_tab_width
= option_tab_spacing
;
1803 next_char
= edit_get_byte (edit
, edit
->curs1
);
1804 if (next_char
== '\t')
1805 edit_delete (edit
, 1);
1806 else if (next_char
== ' ')
1807 for (i
= 1; i
<= del_tab_width
; i
++)
1809 if (next_char
== ' ')
1810 edit_delete (edit
, 1);
1811 next_char
= edit_get_byte (edit
, edit
->curs1
);
1817 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1819 while (cur_bol
>= start_bol
);
1821 edit
->force
|= REDRAW_PAGE
;
1824 /* --------------------------------------------------------------------------------------------- */
1826 * prints at the cursor
1827 * @returns the number of chars printed
1831 edit_print_string (WEdit
* e
, const char *s
)
1835 while (s
[i
] != '\0')
1836 edit_execute_cmd (e
, CK_InsertChar
, (unsigned char) s
[i
++]);
1837 e
->force
|= REDRAW_COMPLETELY
;
1838 edit_update_screen (e
);
1842 /* --------------------------------------------------------------------------------------------- */
1843 /*** public functions ****************************************************************************/
1844 /* --------------------------------------------------------------------------------------------- */
1846 /** User edit menu, like user menu (F2) but only in editor. */
1849 user_menu (WEdit
* edit
, const char *menu_file
, int selected_entry
)
1854 long start_mark
, end_mark
;
1856 vfs_path_t
*block_file_vpath
;
1858 block_file
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
1859 block_file_vpath
= vfs_path_from_str (block_file
);
1861 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
1863 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1865 /* run shell scripts from menu */
1866 if (user_menu_cmd (edit
, menu_file
, selected_entry
)
1867 && (mc_stat (block_file_vpath
, &status
) == 0) && (status
.st_size
!= 0))
1872 /* i.e. we have marked block */
1874 rc
= edit_block_delete_cmd (edit
);
1880 ins_len
= edit_insert_file (edit
, block_file_vpath
);
1881 if (nomark
== 0 && ins_len
> 0)
1882 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1884 /* truncate block file */
1885 fd
= fopen (block_file
, "w");
1889 edit_cursor_move (edit
, curs
- edit
->curs1
);
1890 edit_refresh_cmd (edit
);
1891 edit
->force
|= REDRAW_COMPLETELY
;
1893 g_free (block_file
);
1894 vfs_path_free (block_file_vpath
);
1897 /* --------------------------------------------------------------------------------------------- */
1900 edit_get_byte (WEdit
* edit
, long byte_index
)
1903 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1906 if (byte_index
>= edit
->curs1
)
1908 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1909 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
1913 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
1917 /* --------------------------------------------------------------------------------------------- */
1920 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1925 gchar
*next_ch
= NULL
;
1927 gchar utf8_buf
[UTF8_CHAR_LEN
+ 1];
1929 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1935 str
= edit_get_byte_ptr (edit
, byte_index
);
1943 res
= g_utf8_get_char_validated (str
, -1);
1947 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1949 for (i
= 0; i
< UTF8_CHAR_LEN
; i
++)
1950 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
);
1951 utf8_buf
[UTF8_CHAR_LEN
] = '\0';
1953 res
= g_utf8_get_char_validated (str
, -1);
1964 /* Calculate UTF-8 char width */
1965 next_ch
= g_utf8_next_char (str
);
1968 width
= next_ch
- str
;
1976 *char_width
= width
;
1980 /* --------------------------------------------------------------------------------------------- */
1983 edit_get_write_filter (const vfs_path_t
* write_name_vpath
, const vfs_path_t
* filename_vpath
)
1986 char *p
, *writename
;
1987 const vfs_path_element_t
*path_element
;
1989 i
= edit_find_filter (filename_vpath
);
1993 path_element
= vfs_path_get_by_index (write_name_vpath
, -1);
1994 writename
= name_quote (path_element
->path
, 0);
1995 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
2000 /* --------------------------------------------------------------------------------------------- */
2003 edit_write_stream (WEdit
* edit
, FILE * f
)
2007 if (edit
->lb
== LB_ASIS
)
2009 for (i
= 0; i
< edit
->last_byte
; i
++)
2010 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
2015 /* change line breaks */
2016 for (i
= 0; i
< edit
->last_byte
; i
++)
2018 unsigned char c
= edit_get_byte (edit
, i
);
2020 if (!(c
== '\n' || c
== '\r'))
2022 /* not line break */
2023 if (fputc (c
, f
) < 0)
2027 { /* (c == '\n' || c == '\r') */
2028 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
2032 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
2033 /* put one line break unconditionally */
2034 if (fputc ('\n', f
) < 0)
2037 i
++; /* 2 chars are processed */
2039 if (c
== '\r' && c1
== '\n')
2040 /* Windows line break; go to the next char */
2043 if (c
== '\r' && c1
== '\r')
2045 /* two Macintosh line breaks; put second line break */
2046 if (fputc ('\n', f
) < 0)
2051 if (fputc (c1
, f
) < 0)
2055 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
2056 /* put one line break unconditionally */
2057 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
2060 if (c
== '\r' && c1
== '\n')
2061 /* Windows line break; go to the next char */
2065 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
2066 /* put one line break unconditionally */
2067 if (fputc ('\r', f
) < 0)
2070 i
++; /* 2 chars are processed */
2072 if (c
== '\r' && c1
== '\n')
2073 /* Windows line break; go to the next char */
2076 if (c
== '\n' && c1
== '\n')
2078 /* two Windows line breaks; put second line break */
2079 if (fputc ('\r', f
) < 0)
2084 if (fputc (c1
, f
) < 0)
2087 case LB_ASIS
: /* default without changes */
2093 return edit
->last_byte
;
2096 /* --------------------------------------------------------------------------------------------- */
2097 /** inserts a file at the cursor, returns count of inserted bytes on success */
2099 edit_insert_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
2104 p
= edit_get_filter (filename_vpath
);
2108 long current
= edit
->curs1
;
2110 f
= (FILE *) popen (p
, "r");
2113 edit_insert_stream (edit
, f
);
2114 ins_len
= edit
->curs1
- current
;
2115 edit_cursor_move (edit
, -ins_len
);
2120 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
2121 edit_error_dialog (_("Error"), errmsg
);
2130 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
2131 edit_error_dialog (_("Error"), errmsg
);
2139 int i
, file
, blocklen
;
2140 long current
= edit
->curs1
;
2141 int vertical_insertion
= 0;
2144 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
2148 buf
= g_malloc0 (TEMP_BUF_LEN
);
2149 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2152 /* if contain signature VERTICAL_MAGIC then it vertical block */
2153 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2154 vertical_insertion
= 1;
2156 mc_lseek (file
, 0, SEEK_SET
);
2159 if (vertical_insertion
)
2164 blocklen
= edit_insert_column_of_text_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
2165 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2166 /* highlight inserted text then not persistent blocks */
2167 if (!option_persistent_selections
)
2169 if (!edit
->column_highlight
)
2170 edit_push_undo_action (edit
, COLUMN_OFF
);
2171 edit
->column_highlight
= 1;
2176 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2178 for (i
= 0; i
< blocklen
; i
++)
2179 edit_insert (edit
, buf
[i
]);
2181 /* highlight inserted text then not persistent blocks */
2182 if (!option_persistent_selections
&& edit
->modified
)
2184 edit_set_markers (edit
, edit
->curs1
, current
, 0, 0);
2185 if (edit
->column_highlight
)
2186 edit_push_undo_action (edit
, COLUMN_ON
);
2187 edit
->column_highlight
= 0;
2191 edit
->force
|= REDRAW_PAGE
;
2192 ins_len
= edit
->curs1
- current
;
2193 edit_cursor_move (edit
, -ins_len
);
2203 /* --------------------------------------------------------------------------------------------- */
2205 * Fill in the edit structure. Return NULL on failure. Pass edit as
2206 * NULL to allocate a new structure.
2208 * If line is 0, try to restore saved position. Otherwise put the
2209 * cursor on that line and show it in the middle of the screen.
2213 edit_init (WEdit
* edit
, int y
, int x
, int lines
, int cols
, const vfs_path_t
* filename_vpath
,
2216 gboolean to_free
= FALSE
;
2218 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2219 if (option_line_state
)
2220 option_line_state_width
= LINE_STATE_WIDTH
;
2222 option_line_state_width
= 0;
2228 * Expand option_whole_chars_search by national letters using
2232 static char option_whole_chars_search_buf
[256];
2234 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
2237 size_t len
= str_term_width1 (option_whole_chars_search
);
2239 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
2241 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
2243 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
2245 option_whole_chars_search_buf
[len
++] = i
;
2249 option_whole_chars_search_buf
[len
] = 0;
2250 option_whole_chars_search
= option_whole_chars_search_buf
;
2252 #endif /* ENABLE_NLS */
2253 edit
= g_malloc0 (sizeof (WEdit
));
2254 edit
->search
= NULL
;
2258 edit_purge_widget (edit
);
2261 edit
->widget
.lines
= lines
;
2262 edit
->widget
.cols
= cols
;
2264 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2265 edit
->stat1
.st_uid
= getuid ();
2266 edit
->stat1
.st_gid
= getgid ();
2267 edit
->stat1
.st_mtime
= 0;
2271 edit
->force
|= REDRAW_PAGE
;
2272 edit_set_filename (edit
, filename_vpath
);
2274 edit
->undo_stack_size
= START_STACK_SIZE
;
2275 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2276 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2278 edit
->redo_stack_size
= START_STACK_SIZE
;
2279 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2280 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2283 edit
->converter
= str_cnv_from_term
;
2284 edit_set_codeset (edit
);
2286 if (edit_load_file (edit
))
2288 /* edit_load_file already gives an error message */
2294 edit
->loading_done
= 1;
2297 edit_load_syntax (edit
, NULL
, NULL
);
2300 edit_get_syntax_color (edit
, -1, &color
);
2303 /* load saved cursor position */
2304 if ((line
== 0) && option_save_position
)
2305 edit_load_position (edit
);
2310 edit_move_display (edit
, line
- 1);
2311 edit_move_to_line (edit
, line
- 1);
2314 edit_load_macro_cmd (edit
);
2318 /* --------------------------------------------------------------------------------------------- */
2319 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2322 edit_clean (WEdit
* edit
)
2329 /* a stale lock, remove it */
2331 edit
->locked
= unlock_file (edit
->filename_vpath
);
2333 /* save cursor position */
2334 if (option_save_position
)
2335 edit_save_position (edit
);
2336 else if (edit
->serialized_bookmarks
!= NULL
)
2337 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2339 /* File specified on the mcedit command line and never saved */
2340 if (edit
->delete_file
)
2341 unlink (vfs_path_get_last_path_str (edit
->filename_vpath
));
2343 edit_free_syntax_rules (edit
);
2344 book_mark_flush (edit
, -1);
2345 for (; j
<= MAXBUFF
; j
++)
2347 g_free (edit
->buffers1
[j
]);
2348 g_free (edit
->buffers2
[j
]);
2351 g_free (edit
->undo_stack
);
2352 g_free (edit
->redo_stack
);
2353 vfs_path_free (edit
->filename_vpath
);
2354 vfs_path_free (edit
->dir_vpath
);
2355 mc_search_free (edit
->search
);
2356 edit
->search
= NULL
;
2358 if (edit
->converter
!= str_cnv_from_term
)
2359 str_close_conv (edit
->converter
);
2361 edit_purge_widget (edit
);
2366 /* --------------------------------------------------------------------------------------------- */
2367 /** returns 1 on success */
2370 edit_renew (WEdit
* edit
)
2372 int y
= edit
->widget
.y
;
2373 int x
= edit
->widget
.x
;
2374 int lines
= edit
->widget
.lines
;
2375 int columns
= edit
->widget
.cols
;
2378 return (edit_init (edit
, y
, x
, lines
, columns
, NULL
, 0) != NULL
);
2381 /* --------------------------------------------------------------------------------------------- */
2383 * Load a new file into the editor. If it fails, preserve the old file.
2384 * To do it, allocate a new widget, initialize it and, if the new file
2385 * was loaded, copy the data to the old widget.
2386 * Return 1 on success, 0 on failure.
2390 edit_reload (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
2393 int y
= edit
->widget
.y
;
2394 int x
= edit
->widget
.x
;
2395 int lines
= edit
->widget
.lines
;
2396 int columns
= edit
->widget
.cols
;
2398 e
= g_malloc0 (sizeof (WEdit
));
2399 e
->widget
= edit
->widget
;
2400 if (edit_init (e
, y
, x
, lines
, columns
, filename_vpath
, 0) == NULL
)
2406 memcpy (edit
, e
, sizeof (WEdit
));
2411 /* --------------------------------------------------------------------------------------------- */
2413 * Load a new file into the editor and set line. If it fails, preserve the old file.
2414 * To do it, allocate a new widget, initialize it and, if the new file
2415 * was loaded, copy the data to the old widget.
2416 * Return 1 on success, 0 on failure.
2420 edit_reload_line (WEdit
* edit
, const vfs_path_t
* filename_vpath
, long line
)
2423 int y
= edit
->widget
.y
;
2424 int x
= edit
->widget
.x
;
2425 int lines
= edit
->widget
.lines
;
2426 int columns
= edit
->widget
.cols
;
2428 e
= g_malloc0 (sizeof (WEdit
));
2429 e
->widget
= edit
->widget
;
2430 if (edit_init (e
, y
, x
, lines
, columns
, filename_vpath
, line
) == NULL
)
2436 memcpy (edit
, e
, sizeof (WEdit
));
2441 /* --------------------------------------------------------------------------------------------- */
2444 edit_set_codeset (WEdit
* edit
)
2450 get_codepage_id (mc_global
.source_codepage
>=
2451 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2456 conv
= str_crt_conv_from (cp_id
);
2457 if (conv
!= INVALID_CONV
)
2459 if (edit
->converter
!= str_cnv_from_term
)
2460 str_close_conv (edit
->converter
);
2461 edit
->converter
= conv
;
2466 edit
->utf8
= str_isutf8 (cp_id
);
2473 /* --------------------------------------------------------------------------------------------- */
2475 Recording stack for undo:
2476 The following is an implementation of a compressed stack. Identical
2477 pushes are recorded by a negative prefix indicating the number of times the
2478 same char was pushed. This saves space for repeated curs-left or curs-right
2495 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2496 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2497 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2498 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2501 The only way the cursor moves or the buffer is changed is through the routines:
2502 insert, backspace, insert_ahead, delete, and cursor_move.
2503 These record the reverse undo movements onto the stack each time they are
2506 Each key press results in a set of actions (insert; delete ...). So each time
2507 a key is pressed the current position of start_display is pushed as
2508 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2509 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2510 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2515 edit_push_undo_action (WEdit
* edit
, long c
, ...)
2517 unsigned long sp
= edit
->undo_stack_pointer
;
2521 /* first enlarge the stack if necessary */
2522 if (sp
> edit
->undo_stack_size
- 10)
2524 if (option_max_undo
< 256)
2525 option_max_undo
= 256;
2526 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2528 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2531 edit
->undo_stack
= t
;
2532 edit
->undo_stack_size
<<= 1;
2533 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2537 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2538 if (edit
->undo_stack_disable
)
2540 edit_push_redo_action (edit
, KEY_PRESS
);
2541 edit_push_redo_action (edit
, c
);
2544 else if (edit
->redo_stack_reset
)
2546 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2549 if (edit
->undo_stack_bottom
!= sp
2550 && spm1
!= edit
->undo_stack_bottom
2551 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2554 if (edit
->undo_stack
[spm1
] < 0)
2556 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2559 if (edit
->undo_stack
[spm1
] > -1000000000)
2561 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2563 edit
->undo_stack
[spm1
]--;
2571 d
= edit
->undo_stack
[spm1
];
2575 return; /* --> no need to push multiple do-nothings */
2576 edit
->undo_stack
[sp
] = -2;
2581 edit
->undo_stack
[sp
] = c
;
2584 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2586 /* if the sp wraps round and catches the undo_stack_bottom then erase
2587 * the first set of actions on the stack to make space - by moving
2588 * undo_stack_bottom forward one "key press" */
2589 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2590 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2591 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2594 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2596 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2597 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2599 /*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: */
2600 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2601 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2603 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2608 edit_push_redo_action (WEdit
* edit
, long c
, ...)
2610 unsigned long sp
= edit
->redo_stack_pointer
;
2613 /* first enlarge the stack if necessary */
2614 if (sp
> edit
->redo_stack_size
- 10)
2616 if (option_max_undo
< 256)
2617 option_max_undo
= 256;
2618 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2620 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2623 edit
->redo_stack
= t
;
2624 edit
->redo_stack_size
<<= 1;
2625 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2629 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2631 if (edit
->redo_stack_bottom
!= sp
2632 && spm1
!= edit
->redo_stack_bottom
2633 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2636 if (edit
->redo_stack
[spm1
] < 0)
2638 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2641 if (edit
->redo_stack
[spm1
] > -1000000000)
2643 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2644 edit
->redo_stack
[spm1
]--;
2651 d
= edit
->redo_stack
[spm1
];
2655 return; /* --> no need to push multiple do-nothings */
2656 edit
->redo_stack
[sp
] = -2;
2657 goto redo_check_bottom
;
2661 edit
->redo_stack
[sp
] = c
;
2664 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2666 /* if the sp wraps round and catches the redo_stack_bottom then erase
2667 * the first set of actions on the stack to make space - by moving
2668 * redo_stack_bottom forward one "key press" */
2669 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2670 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2671 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2674 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2676 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2677 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2680 * If a single key produced enough pushes to wrap all the way round then
2681 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2682 * The stack is then initialised:
2685 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2686 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2687 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2691 /* --------------------------------------------------------------------------------------------- */
2693 Basic low level single character buffer alterations and movements at the cursor.
2694 Returns char passed over, inserted or removed.
2698 edit_insert (WEdit
* edit
, int c
)
2700 /* check if file has grown to large */
2701 if (edit
->last_byte
>= SIZE_LIMIT
)
2704 /* first we must update the position of the display window */
2705 if (edit
->curs1
< edit
->start_display
)
2707 edit
->start_display
++;
2712 /* Mark file as modified, unless the file hasn't been fully loaded */
2713 if (edit
->loading_done
)
2715 edit_modification (edit
);
2718 /* now we must update some info on the file and check if a redraw is required */
2721 if (edit
->book_mark
)
2722 book_mark_inc (edit
, edit
->curs_line
);
2724 edit
->total_lines
++;
2725 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2728 /* save the reverse command onto the undo stack */
2729 /* ordinary char and not space */
2731 edit_push_undo_action (edit
, BACKSPACE
);
2733 edit_push_undo_action (edit
, BACKSPACE_BR
);
2734 /* update markers */
2735 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2736 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2737 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2739 /* add a new buffer if we've reached the end of the last one */
2740 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2741 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2743 /* perform the insertion */
2744 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2745 = (unsigned char) c
;
2747 /* update file length */
2750 /* update cursor position */
2754 /* --------------------------------------------------------------------------------------------- */
2755 /** same as edit_insert and move left */
2758 edit_insert_ahead (WEdit
* edit
, int c
)
2760 if (edit
->last_byte
>= SIZE_LIMIT
)
2763 if (edit
->curs1
< edit
->start_display
)
2765 edit
->start_display
++;
2769 edit_modification (edit
);
2772 if (edit
->book_mark
)
2773 book_mark_inc (edit
, edit
->curs_line
);
2774 edit
->total_lines
++;
2775 edit
->force
|= REDRAW_AFTER_CURSOR
;
2777 /* ordinary char and not space */
2779 edit_push_undo_action (edit
, DELCHAR
);
2781 edit_push_undo_action (edit
, DELCHAR_BR
);
2783 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2784 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2785 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2787 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2788 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2789 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2790 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2797 /* --------------------------------------------------------------------------------------------- */
2800 edit_delete (WEdit
* edit
, const int byte_delete
)
2810 /* if byte_delete = 1 then delete only one byte not multibyte char */
2811 if (edit
->utf8
&& byte_delete
== 0)
2813 edit_get_utf (edit
, edit
->curs1
, &cw
);
2818 if (edit
->mark2
!= edit
->mark1
)
2819 edit_push_markers (edit
);
2821 for (i
= 1; i
<= cw
; i
++)
2823 if (edit
->mark1
> edit
->curs1
)
2826 edit
->end_mark_curs
--;
2828 if (edit
->mark2
> edit
->curs1
)
2830 if (edit
->last_get_rule
> edit
->curs1
)
2831 edit
->last_get_rule
--;
2833 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2835 1) & M_EDIT_BUF_SIZE
) - 1];
2837 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2839 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2840 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2844 edit_push_undo_action (edit
, p
+ 256);
2847 edit_modification (edit
);
2850 if (edit
->book_mark
)
2851 book_mark_dec (edit
, edit
->curs_line
);
2852 edit
->total_lines
--;
2853 edit
->force
|= REDRAW_AFTER_CURSOR
;
2855 if (edit
->curs1
< edit
->start_display
)
2857 edit
->start_display
--;
2865 /* --------------------------------------------------------------------------------------------- */
2866 /** moves the cursor right or left: increment positive or negative respectively */
2869 edit_cursor_move (WEdit
* edit
, long increment
)
2871 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2876 for (; increment
< 0; increment
++)
2881 edit_push_undo_action (edit
, CURS_RIGHT
);
2883 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2884 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2885 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2886 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2887 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2889 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2890 1) & M_EDIT_BUF_SIZE
];
2891 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2893 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2894 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2900 edit
->force
|= REDRAW_LINE_BELOW
;
2905 else if (increment
> 0)
2907 for (; increment
> 0; increment
--)
2912 edit_push_undo_action (edit
, CURS_LEFT
);
2914 c
= edit_get_byte (edit
, edit
->curs1
);
2915 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2916 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2917 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2919 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2921 1) & M_EDIT_BUF_SIZE
) - 1];
2922 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2924 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2925 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2931 edit
->force
|= REDRAW_LINE_ABOVE
;
2937 /* These functions return positions relative to lines */
2939 /* --------------------------------------------------------------------------------------------- */
2940 /** returns index of last char on line + 1 */
2943 edit_eol (WEdit
* edit
, long current
)
2945 if (current
>= edit
->last_byte
)
2946 return edit
->last_byte
;
2949 if (edit_get_byte (edit
, current
) == '\n')
2954 /* --------------------------------------------------------------------------------------------- */
2955 /** returns index of first char on line */
2958 edit_bol (WEdit
* edit
, long current
)
2964 if (edit_get_byte (edit
, current
- 1) == '\n')
2969 /* --------------------------------------------------------------------------------------------- */
2972 edit_count_lines (WEdit
* edit
, long current
, long upto
)
2975 if (upto
> edit
->last_byte
)
2976 upto
= edit
->last_byte
;
2979 while (current
< upto
)
2980 if (edit_get_byte (edit
, current
++) == '\n')
2985 /* --------------------------------------------------------------------------------------------- */
2986 /* If lines is zero this returns the count of lines from current to upto. */
2987 /* If upto is zero returns index of lines forward current. */
2990 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
2994 return edit_count_lines (edit
, current
, upto
);
3003 next
= edit_eol (edit
, current
) + 1;
3004 if (next
> edit
->last_byte
)
3013 /* --------------------------------------------------------------------------------------------- */
3014 /** Returns offset of 'lines' lines up from current */
3017 edit_move_backward (WEdit
* edit
, long current
, long lines
)
3021 current
= edit_bol (edit
, current
);
3022 while ((lines
--) && current
!= 0)
3023 current
= edit_bol (edit
, current
- 1);
3027 /* --------------------------------------------------------------------------------------------- */
3028 /* If cols is zero this returns the count of columns from current to upto. */
3029 /* If upto is zero returns index of cols across from current. */
3032 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
3043 q
= edit
->last_byte
+ 2;
3045 for (col
= 0, p
= current
; p
< q
; p
++)
3057 orig_c
= c
= edit_get_byte (edit
, p
);
3065 utf_ch
= edit_get_utf (edit
, p
, &cw
);
3066 if (mc_global
.utf8_display
)
3070 if (g_unichar_iswide (utf_ch
))
3073 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
3077 c
= convert_to_display_c (c
);
3081 col
+= TAB_SIZE
- col
% TAB_SIZE
;
3089 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!mc_global
.utf8_display
&& !edit
->utf8
)))
3090 /* '\r' is shown as ^M, so we must advance 2 characters */
3091 /* Caret notation for control characters */
3099 /* --------------------------------------------------------------------------------------------- */
3100 /** returns the current column position of the cursor */
3103 edit_get_col (WEdit
* edit
)
3105 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3108 /* --------------------------------------------------------------------------------------------- */
3109 /* Scrolling functions */
3110 /* --------------------------------------------------------------------------------------------- */
3113 edit_update_curs_row (WEdit
* edit
)
3115 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
3118 /* --------------------------------------------------------------------------------------------- */
3121 edit_update_curs_col (WEdit
* edit
)
3123 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3126 /* --------------------------------------------------------------------------------------------- */
3129 edit_get_curs_col (const WEdit
* edit
)
3131 return edit
->curs_col
;
3134 /* --------------------------------------------------------------------------------------------- */
3135 /** moves the display start position up by i lines */
3138 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
3140 unsigned long lines_above
= edit
->start_line
;
3141 if (i
> lines_above
)
3145 edit
->start_line
-= i
;
3146 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
3147 edit
->force
|= REDRAW_PAGE
;
3148 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3150 edit_update_curs_row (edit
);
3154 /* --------------------------------------------------------------------------------------------- */
3155 /** returns 1 if could scroll, 0 otherwise */
3158 edit_scroll_downward (WEdit
* edit
, int i
)
3161 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->widget
.lines
- 1);
3162 if (lines_below
> 0)
3164 if (i
> lines_below
)
3166 edit
->start_line
+= i
;
3167 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
3168 edit
->force
|= REDRAW_PAGE
;
3169 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3171 edit_update_curs_row (edit
);
3174 /* --------------------------------------------------------------------------------------------- */
3177 edit_scroll_right (WEdit
* edit
, int i
)
3179 edit
->force
|= REDRAW_PAGE
;
3180 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3181 edit
->start_col
-= i
;
3184 /* --------------------------------------------------------------------------------------------- */
3187 edit_scroll_left (WEdit
* edit
, int i
)
3189 if (edit
->start_col
)
3191 edit
->start_col
+= i
;
3192 if (edit
->start_col
> 0)
3193 edit
->start_col
= 0;
3194 edit
->force
|= REDRAW_PAGE
;
3195 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3199 /* --------------------------------------------------------------------------------------------- */
3200 /* high level cursor movement commands */
3201 /* --------------------------------------------------------------------------------------------- */
3204 edit_move_to_prev_col (WEdit
* edit
, long p
)
3206 int prev
= edit
->prev_col
;
3207 int over
= edit
->over_col
;
3208 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
3210 if (option_cursor_beyond_eol
)
3212 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
3213 edit_eol (edit
, edit
->curs1
));
3215 if (line_len
< prev
+ edit
->over_col
)
3217 edit
->over_col
= prev
+ over
- line_len
;
3218 edit
->prev_col
= line_len
;
3219 edit
->curs_col
= line_len
;
3223 edit
->curs_col
= prev
+ over
;
3224 edit
->prev_col
= edit
->curs_col
;
3231 if (is_in_indent (edit
) && option_fake_half_tabs
)
3233 edit_update_curs_col (edit
);
3235 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
3237 int q
= edit
->curs_col
;
3238 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
3239 p
= edit_bol (edit
, edit
->curs1
);
3240 edit_cursor_move (edit
,
3241 edit_move_forward3 (edit
, p
, edit
->curs_col
,
3243 if (!left_of_four_spaces (edit
))
3244 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3250 /* --------------------------------------------------------------------------------------------- */
3253 line_is_blank (WEdit
* edit
, long line
)
3255 return is_blank (edit
, edit_find_line (edit
, line
));
3258 /* --------------------------------------------------------------------------------------------- */
3259 /** move cursor to line 'line' */
3262 edit_move_to_line (WEdit
* e
, long line
)
3264 if (line
< e
->curs_line
)
3265 edit_move_up (e
, e
->curs_line
- line
, 0);
3267 edit_move_down (e
, line
- e
->curs_line
, 0);
3268 edit_scroll_screen_over_cursor (e
);
3271 /* --------------------------------------------------------------------------------------------- */
3272 /** scroll window so that first visible line is 'line' */
3275 edit_move_display (WEdit
* e
, long line
)
3277 if (line
< e
->start_line
)
3278 edit_scroll_upward (e
, e
->start_line
- line
);
3280 edit_scroll_downward (e
, line
- e
->start_line
);
3283 /* --------------------------------------------------------------------------------------------- */
3284 /** save markers onto undo stack */
3287 edit_push_markers (WEdit
* edit
)
3289 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3290 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3291 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3294 /* --------------------------------------------------------------------------------------------- */
3297 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
3306 /* --------------------------------------------------------------------------------------------- */
3307 /** highlight marker toggle */
3310 edit_mark_cmd (WEdit
* edit
, int unmark
)
3312 edit_push_markers (edit
);
3315 edit_set_markers (edit
, 0, 0, 0, 0);
3316 edit
->force
|= REDRAW_PAGE
;
3320 if (edit
->mark2
>= 0)
3322 edit
->end_mark_curs
= -1;
3323 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3324 edit
->curs_col
+ edit
->over_col
);
3325 edit
->force
|= REDRAW_PAGE
;
3329 edit
->end_mark_curs
= edit
->curs1
;
3330 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3331 edit
->curs_col
+ edit
->over_col
);
3336 /* --------------------------------------------------------------------------------------------- */
3337 /** highlight the word under cursor */
3340 edit_mark_current_word_cmd (WEdit
* edit
)
3344 for (pos
= edit
->curs1
; pos
!= 0; pos
--)
3348 c1
= edit_get_byte (edit
, pos
);
3349 c2
= edit_get_byte (edit
, pos
- 1);
3350 if (!isspace (c1
) && isspace (c2
))
3352 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3357 for (; pos
< edit
->last_byte
; pos
++)
3361 c1
= edit_get_byte (edit
, pos
);
3362 c2
= edit_get_byte (edit
, pos
+ 1);
3363 if (!isspace (c1
) && isspace (c2
))
3365 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3368 edit
->mark2
= min (pos
+ 1, edit
->last_byte
);
3370 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3373 /* --------------------------------------------------------------------------------------------- */
3376 edit_mark_current_line_cmd (WEdit
* edit
)
3378 long pos
= edit
->curs1
;
3380 edit
->mark1
= edit_bol (edit
, pos
);
3381 edit
->mark2
= edit_eol (edit
, pos
);
3383 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3386 /* --------------------------------------------------------------------------------------------- */
3389 edit_delete_line (WEdit
* edit
)
3392 * Delete right part of the line.
3393 * Note that edit_get_byte() returns '\n' when byte position is
3396 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3398 (void) edit_delete (edit
, 1);
3403 * Note that edit_delete() will not corrupt anything if called while
3404 * cursor position is EOF.
3406 (void) edit_delete (edit
, 1);
3409 * Delete left part of the line.
3410 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3412 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3414 (void) edit_backspace (edit
, 1);
3418 /* --------------------------------------------------------------------------------------------- */
3421 edit_indent_width (WEdit
* edit
, long p
)
3424 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
3426 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
3429 /* --------------------------------------------------------------------------------------------- */
3432 edit_insert_indent (WEdit
* edit
, int indent
)
3434 if (!option_fill_tabs_with_spaces
)
3436 while (indent
>= TAB_SIZE
)
3438 edit_insert (edit
, '\t');
3442 while (indent
-- > 0)
3443 edit_insert (edit
, ' ');
3446 /* --------------------------------------------------------------------------------------------- */
3449 edit_push_key_press (WEdit
* edit
)
3451 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3452 if (edit
->mark2
== -1)
3454 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3455 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3459 /* --------------------------------------------------------------------------------------------- */
3462 edit_find_bracket (WEdit
* edit
)
3464 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3465 if (last_bracket
!= edit
->bracket
)
3466 edit
->force
|= REDRAW_PAGE
;
3467 last_bracket
= edit
->bracket
;
3470 /* --------------------------------------------------------------------------------------------- */
3472 * This executes a command as though the user initiated it through a key
3473 * press. Callback with WIDGET_KEY as a message calls this after
3474 * translating the key press. This function can be used to pass any
3475 * command to the editor. Note that the screen wouldn't update
3476 * automatically. Either of command or char_for_insertion must be
3477 * passed as -1. Commands are executed, and char_for_insertion is
3478 * inserted at the cursor.
3482 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3484 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3486 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3489 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3492 if (macro_index
!= -1)
3494 edit
->force
|= REDRAW_COMPLETELY
;
3495 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3497 edit_store_macro_cmd (edit
);
3501 else if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3503 edit_repeat_macro_cmd (edit
);
3509 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3511 record_macro_buf
[macro_index
].action
= command
;
3512 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3514 /* record the beginning of a set of editing actions initiated by a key press */
3515 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3516 edit_push_key_press (edit
);
3518 edit_execute_cmd (edit
, command
, char_for_insertion
);
3519 if (edit
->column_highlight
)
3520 edit
->force
|= REDRAW_PAGE
;
3523 /* --------------------------------------------------------------------------------------------- */
3525 This executes a command at a lower level than macro recording.
3526 It also does not push a key_press onto the undo stack. This means
3527 that if it is called many times, a single undo command will undo
3528 all of them. It also does not check for the Undo command.
3531 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3533 edit
->force
|= REDRAW_LINE
;
3535 /* The next key press will unhighlight the found string, so update
3537 if (edit
->found_len
|| edit
->column_highlight
)
3538 edit
->force
|= REDRAW_PAGE
;
3542 /* a mark command with shift-arrow */
3545 case CK_MarkToWordBegin
:
3546 case CK_MarkToWordEnd
:
3552 case CK_MarkPageDown
:
3553 case CK_MarkToFileBegin
:
3554 case CK_MarkToFileEnd
:
3555 case CK_MarkToPageBegin
:
3556 case CK_MarkToPageEnd
:
3557 case CK_MarkScrollUp
:
3558 case CK_MarkScrollDown
:
3559 case CK_MarkParagraphUp
:
3560 case CK_MarkParagraphDown
:
3561 /* a mark command with alt-arrow */
3562 case CK_MarkColumnPageUp
:
3563 case CK_MarkColumnPageDown
:
3564 case CK_MarkColumnLeft
:
3565 case CK_MarkColumnRight
:
3566 case CK_MarkColumnUp
:
3567 case CK_MarkColumnDown
:
3568 case CK_MarkColumnScrollUp
:
3569 case CK_MarkColumnScrollDown
:
3570 case CK_MarkColumnParagraphUp
:
3571 case CK_MarkColumnParagraphDown
:
3572 edit
->column_highlight
= 0;
3573 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3575 edit_mark_cmd (edit
, 1); /* clear */
3576 edit_mark_cmd (edit
, 0); /* marking on */
3578 edit
->highlight
= 1;
3581 /* any other command */
3583 if (edit
->highlight
)
3584 edit_mark_cmd (edit
, 0); /* clear */
3585 edit
->highlight
= 0;
3588 /* first check for undo */
3589 if (command
== CK_Undo
)
3591 edit
->redo_stack_reset
= 0;
3592 edit_group_undo (edit
);
3593 edit
->found_len
= 0;
3594 edit
->prev_col
= edit_get_col (edit
);
3595 edit
->search_start
= edit
->curs1
;
3598 /* check for redo */
3599 if (command
== CK_Redo
)
3601 edit
->redo_stack_reset
= 0;
3602 edit_do_redo (edit
);
3603 edit
->found_len
= 0;
3604 edit
->prev_col
= edit_get_col (edit
);
3605 edit
->search_start
= edit
->curs1
;
3609 edit
->redo_stack_reset
= 1;
3611 /* An ordinary key press */
3612 if (char_for_insertion
>= 0)
3614 /* if non persistent selection and text selected */
3615 if (!option_persistent_selections
)
3617 if (edit
->mark1
!= edit
->mark2
)
3618 edit_block_delete_cmd (edit
);
3620 if (edit
->overwrite
)
3622 /* remove char only one time, after input first byte, multibyte chars */
3623 if ((!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3624 && edit_get_byte (edit
, edit
->curs1
) != '\n')
3625 edit_delete (edit
, 0);
3627 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3628 edit_insert_over (edit
);
3630 if (char_for_insertion
> 255 && !mc_global
.utf8_display
)
3632 unsigned char str
[6 + 1];
3636 res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3646 while (str
[i
] != 0 && i
<= 6)
3648 char_for_insertion
= str
[i
];
3649 edit_insert (edit
, char_for_insertion
);
3655 edit_insert (edit
, char_for_insertion
);
3657 if (option_auto_para_formatting
)
3659 format_paragraph (edit
, 0);
3660 edit
->force
|= REDRAW_PAGE
;
3663 check_and_wrap_line (edit
);
3664 edit
->found_len
= 0;
3665 edit
->prev_col
= edit_get_col (edit
);
3666 edit
->search_start
= edit
->curs1
;
3667 edit_find_bracket (edit
);
3673 case CK_TopOnScreen
:
3674 case CK_BottomOnScreen
:
3687 if (edit
->mark2
>= 0)
3689 if (!option_persistent_selections
)
3691 if (edit
->column_highlight
)
3692 edit_push_undo_action (edit
, COLUMN_ON
);
3693 edit
->column_highlight
= 0;
3694 edit_mark_cmd (edit
, 1);
3701 case CK_TopOnScreen
:
3702 case CK_BottomOnScreen
:
3703 case CK_MarkToPageBegin
:
3704 case CK_MarkToPageEnd
:
3709 case CK_MarkToWordBegin
:
3710 case CK_MarkToWordEnd
:
3713 case CK_MarkColumnUp
:
3714 case CK_MarkColumnDown
:
3715 if (edit
->mark2
== -1)
3716 break; /*marking is following the cursor: may need to highlight a whole line */
3721 edit
->force
|= REDRAW_CHAR_ONLY
;
3724 /* basic cursor key commands */
3728 /* if non persistent selection and text selected */
3729 if (!option_persistent_selections
)
3731 if (edit
->mark1
!= edit
->mark2
)
3733 edit_block_delete_cmd (edit
);
3737 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3742 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3744 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3745 edit_backspace (edit
, 1);
3750 if (option_fake_half_tabs
)
3753 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3755 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3756 edit_backspace (edit
, 1);
3761 edit_backspace (edit
, 0);
3764 /* if non persistent selection and text selected */
3765 if (!option_persistent_selections
)
3767 if (edit
->mark1
!= edit
->mark2
)
3769 edit_block_delete_cmd (edit
);
3774 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3775 edit_insert_over (edit
);
3777 if (option_fake_half_tabs
)
3780 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3782 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3783 edit_delete (edit
, 1);
3787 edit_delete (edit
, 0);
3789 case CK_DeleteToWordBegin
:
3791 edit_left_delete_word (edit
);
3793 case CK_DeleteToWordEnd
:
3794 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3795 edit_insert_over (edit
);
3797 edit_right_delete_word (edit
);
3800 edit_delete_line (edit
);
3802 case CK_DeleteToHome
:
3803 edit_delete_to_line_begin (edit
);
3805 case CK_DeleteToEnd
:
3806 edit_delete_to_line_end (edit
);
3810 if (option_auto_para_formatting
)
3812 edit_double_newline (edit
);
3813 if (option_return_does_auto_indent
)
3814 edit_auto_indent (edit
);
3815 format_paragraph (edit
, 0);
3819 edit_insert (edit
, '\n');
3820 if (option_return_does_auto_indent
)
3822 edit_auto_indent (edit
);
3827 edit_insert (edit
, '\n');
3830 case CK_MarkColumnPageUp
:
3831 edit
->column_highlight
= 1;
3834 edit_move_up (edit
, edit
->widget
.lines
- 1, 1);
3836 case CK_MarkColumnPageDown
:
3837 edit
->column_highlight
= 1;
3839 case CK_MarkPageDown
:
3840 edit_move_down (edit
, edit
->widget
.lines
- 1, 1);
3842 case CK_MarkColumnLeft
:
3843 edit
->column_highlight
= 1;
3846 if (option_fake_half_tabs
)
3848 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3850 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3853 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3854 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3858 edit_left_char_move_cmd (edit
);
3860 case CK_MarkColumnRight
:
3861 edit
->column_highlight
= 1;
3864 if (option_fake_half_tabs
)
3866 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3868 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3869 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3873 edit_right_char_move_cmd (edit
);
3875 case CK_TopOnScreen
:
3876 case CK_MarkToPageBegin
:
3877 edit_begin_page (edit
);
3879 case CK_BottomOnScreen
:
3880 case CK_MarkToPageEnd
:
3881 edit_end_page (edit
);
3884 case CK_MarkToWordBegin
:
3886 edit_left_word_move_cmd (edit
);
3889 case CK_MarkToWordEnd
:
3891 edit_right_word_move_cmd (edit
);
3893 case CK_MarkColumnUp
:
3894 edit
->column_highlight
= 1;
3897 edit_move_up (edit
, 1, 0);
3899 case CK_MarkColumnDown
:
3900 edit
->column_highlight
= 1;
3903 edit_move_down (edit
, 1, 0);
3905 case CK_MarkColumnParagraphUp
:
3906 edit
->column_highlight
= 1;
3907 case CK_ParagraphUp
:
3908 case CK_MarkParagraphUp
:
3909 edit_move_up_paragraph (edit
, 0);
3911 case CK_MarkColumnParagraphDown
:
3912 edit
->column_highlight
= 1;
3913 case CK_ParagraphDown
:
3914 case CK_MarkParagraphDown
:
3915 edit_move_down_paragraph (edit
, 0);
3917 case CK_MarkColumnScrollUp
:
3918 edit
->column_highlight
= 1;
3920 case CK_MarkScrollUp
:
3921 edit_move_up (edit
, 1, 1);
3923 case CK_MarkColumnScrollDown
:
3924 edit
->column_highlight
= 1;
3926 case CK_MarkScrollDown
:
3927 edit_move_down (edit
, 1, 1);
3931 edit_cursor_to_bol (edit
);
3935 edit_cursor_to_eol (edit
);
3938 /* if text marked shift block */
3939 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3941 if (edit
->mark2
< 0)
3942 edit_mark_cmd (edit
, 0);
3943 edit_move_block_to_right (edit
);
3947 if (option_cursor_beyond_eol
)
3948 edit_insert_over (edit
);
3949 edit_tab_cmd (edit
);
3950 if (option_auto_para_formatting
)
3952 format_paragraph (edit
, 0);
3953 edit
->force
|= REDRAW_PAGE
;
3957 check_and_wrap_line (edit
);
3962 case CK_InsertOverwrite
:
3963 edit
->overwrite
= !edit
->overwrite
;
3967 if (edit
->mark2
>= 0)
3969 if (edit
->column_highlight
)
3970 edit_push_undo_action (edit
, COLUMN_ON
);
3971 edit
->column_highlight
= 0;
3973 edit_mark_cmd (edit
, 0);
3976 if (!edit
->column_highlight
)
3977 edit_push_undo_action (edit
, COLUMN_OFF
);
3978 edit
->column_highlight
= 1;
3979 edit_mark_cmd (edit
, 0);
3982 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3983 edit
->force
|= REDRAW_PAGE
;
3986 if (edit
->column_highlight
)
3987 edit_push_undo_action (edit
, COLUMN_ON
);
3988 edit
->column_highlight
= 0;
3989 edit_mark_cmd (edit
, 1);
3992 if (edit
->column_highlight
)
3993 edit_push_undo_action (edit
, COLUMN_ON
);
3994 edit
->column_highlight
= 0;
3995 edit_mark_current_word_cmd (edit
);
3998 if (edit
->column_highlight
)
3999 edit_push_undo_action (edit
, COLUMN_ON
);
4000 edit
->column_highlight
= 0;
4001 edit_mark_current_line_cmd (edit
);
4004 case CK_ShowNumbers
:
4005 option_line_state
= !option_line_state
;
4006 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
4007 edit
->force
|= REDRAW_PAGE
;
4011 show_right_margin
= !show_right_margin
;
4012 edit
->force
|= REDRAW_PAGE
;
4016 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
4017 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
4018 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4020 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4022 case CK_BookmarkFlush
:
4023 book_mark_flush (edit
, BOOK_MARK_COLOR
);
4024 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
4025 edit
->force
|= REDRAW_PAGE
;
4027 case CK_BookmarkNext
:
4028 if (edit
->book_mark
)
4030 struct _book_mark
*p
;
4031 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4035 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4036 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4037 edit_move_to_line (edit
, p
->line
);
4041 case CK_BookmarkPrev
:
4042 if (edit
->book_mark
)
4044 struct _book_mark
*p
;
4045 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4046 while (p
->line
== edit
->curs_line
)
4051 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4052 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4053 edit_move_to_line (edit
, p
->line
);
4059 case CK_MarkToFileBegin
:
4060 edit_move_to_top (edit
);
4063 case CK_MarkToFileEnd
:
4064 edit_move_to_bottom (edit
);
4068 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4069 edit_insert_over (edit
);
4070 edit_block_copy_cmd (edit
);
4073 edit_block_delete_cmd (edit
);
4076 edit_block_move_cmd (edit
);
4079 case CK_BlockShiftLeft
:
4080 if (edit
->mark1
!= edit
->mark2
)
4081 edit_move_block_to_left (edit
);
4083 case CK_BlockShiftRight
:
4084 if (edit
->mark1
!= edit
->mark2
)
4085 edit_move_block_to_right (edit
);
4088 edit_copy_to_X_buf_cmd (edit
);
4091 edit_cut_to_X_buf_cmd (edit
);
4094 /* if non persistent selection and text selected */
4095 if (!option_persistent_selections
)
4097 if (edit
->mark1
!= edit
->mark2
)
4098 edit_block_delete_cmd (edit
);
4100 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4101 edit_insert_over (edit
);
4102 edit_paste_from_X_buf_cmd (edit
);
4105 edit_paste_from_history (edit
);
4109 edit_save_as_cmd (edit
);
4112 edit_save_confirm_cmd (edit
);
4115 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
4118 edit_save_block_cmd (edit
);
4121 edit_insert_file_cmd (edit
);
4125 edit_load_back_cmd (edit
);
4128 edit_load_forward_cmd (edit
);
4131 case CK_EditSyntaxFile
:
4132 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
4134 case CK_SyntaxChoose
:
4135 edit_syntax_dialog (edit
);
4138 case CK_EditUserMenu
:
4139 edit_load_cmd (edit
, EDIT_FILE_MENU
);
4142 case CK_SyntaxOnOff
:
4143 option_syntax_highlighting
^= 1;
4144 if (option_syntax_highlighting
== 1)
4145 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
4146 edit
->force
|= REDRAW_PAGE
;
4150 enable_show_tabs_tws
^= 1;
4151 edit
->force
|= REDRAW_PAGE
;
4155 edit_search_cmd (edit
, FALSE
);
4157 case CK_SearchContinue
:
4158 edit_search_cmd (edit
, TRUE
);
4161 edit_replace_cmd (edit
, 0);
4163 case CK_ReplaceContinue
:
4164 edit_replace_cmd (edit
, 1);
4167 /* if text marked shift block */
4168 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
4170 edit_move_block_to_left (edit
);
4174 edit_complete_word_cmd (edit
);
4178 edit_get_match_keyword_cmd (edit
);
4181 dlg_stop (edit
->widget
.owner
);
4184 edit_new_cmd (edit
);
4187 edit_help_cmd (edit
);
4190 edit_refresh_cmd (edit
);
4202 edit_options_dialog (edit
);
4204 case CK_OptionsSaveMode
:
4205 menu_save_mode_cmd ();
4210 /* fool gcc to prevent a Y2K warning */
4211 char time_format
[] = "_c";
4212 time_format
[0] = '%';
4214 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4215 edit_print_string (edit
, s
);
4216 edit
->force
|= REDRAW_PAGE
;
4221 edit_goto_cmd (edit
);
4223 case CK_ParagraphFormat
:
4224 format_paragraph (edit
, 1);
4225 edit
->force
|= REDRAW_PAGE
;
4227 case CK_MacroDelete
:
4228 edit_delete_macro_cmd (edit
);
4230 case CK_MatchBracket
:
4231 edit_goto_matching_bracket (edit
);
4234 user_menu (edit
, NULL
, -1);
4237 edit_sort_cmd (edit
);
4239 case CK_ExternalCommand
:
4240 edit_ext_cmd (edit
);
4243 edit_mail_dialog (edit
);
4249 case CK_SelectCodepage
:
4250 edit_select_codepage_cmd (edit
);
4253 case CK_InsertLiteral
:
4254 edit_insert_literal_cmd (edit
);
4256 case CK_MacroStartStopRecord
:
4257 edit_begin_end_macro_cmd (edit
);
4259 case CK_RepeatStartStopRecord
:
4260 edit_begin_end_repeat_cmd (edit
);
4262 case CK_ExtendedKeyMap
:
4263 edit
->extmod
= TRUE
;
4270 if ((command
/ CK_PipeBlock (0)) == 1)
4271 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
4273 /* keys which must set the col position, and the search vars */
4277 case CK_SearchContinue
:
4279 case CK_ReplaceContinue
:
4281 edit
->prev_col
= edit_get_col (edit
);
4285 case CK_MarkColumnUp
:
4288 case CK_MarkColumnDown
:
4291 case CK_MarkColumnPageUp
:
4293 case CK_MarkPageDown
:
4294 case CK_MarkColumnPageDown
:
4296 case CK_MarkToFileBegin
:
4298 case CK_MarkToFileEnd
:
4299 case CK_ParagraphUp
:
4300 case CK_MarkParagraphUp
:
4301 case CK_MarkColumnParagraphUp
:
4302 case CK_ParagraphDown
:
4303 case CK_MarkParagraphDown
:
4304 case CK_MarkColumnParagraphDown
:
4306 case CK_MarkScrollUp
:
4307 case CK_MarkColumnScrollUp
:
4309 case CK_MarkScrollDown
:
4310 case CK_MarkColumnScrollDown
:
4311 edit
->search_start
= edit
->curs1
;
4312 edit
->found_len
= 0;
4315 edit
->found_len
= 0;
4316 edit
->prev_col
= edit_get_col (edit
);
4317 edit
->search_start
= edit
->curs1
;
4319 edit_find_bracket (edit
);
4321 if (option_auto_para_formatting
)
4327 case CK_DeleteToWordBegin
:
4328 case CK_DeleteToWordEnd
:
4329 case CK_DeleteToHome
:
4330 case CK_DeleteToEnd
:
4331 format_paragraph (edit
, 0);
4332 edit
->force
|= REDRAW_PAGE
;
4337 /* --------------------------------------------------------------------------------------------- */
4340 edit_stack_init (void)
4342 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4344 edit_history_moveto
[edit_stack_iterator
].filename_vpath
= NULL
;
4345 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4348 edit_stack_iterator
= 0;
4351 /* --------------------------------------------------------------------------------------------- */
4354 edit_stack_free (void)
4356 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4357 vfs_path_free (edit_history_moveto
[edit_stack_iterator
].filename_vpath
);
4360 /* --------------------------------------------------------------------------------------------- */
4364 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
4366 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4369 /* --------------------------------------------------------------------------------------------- */
4373 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
4375 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4378 /* --------------------------------------------------------------------------------------------- */