2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2008, 2009, 2010, 2011, 2012
6 The Free Software Foundation, Inc.
10 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
11 Andrew Borodin <aborodin@vmail.ru> 2012.
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 * \brief Source: editor low level data handling and cursor fundamentals
38 #include <sys/types.h>
47 #include "lib/global.h"
49 #include "lib/tty/color.h"
50 #include "lib/tty/tty.h" /* attrset() */
51 #include "lib/tty/key.h" /* is_idle() */
52 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
53 #include "lib/vfs/vfs.h"
54 #include "lib/strutil.h" /* utf string functions */
55 #include "lib/util.h" /* load_file_position(), save_file_position() */
56 #include "lib/timefmt.h" /* time formatting */
58 #include "lib/widget.h"
61 #include "lib/charsets.h" /* get_codepage_id */
64 #include "src/filemanager/cmd.h" /* view_other_cmd() */
65 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
67 #include "src/setup.h" /* option_tab_spacing */
68 #include "src/main.h" /* macro_index */
69 #include "src/learn.h" /* learn_keys */
70 #include "src/keybind-defaults.h"
72 #include "edit-impl.h"
73 #include "editwidget.h"
75 /*** global variables ****************************************************************************/
77 int option_word_wrap_line_length
= DEFAULT_WRAP_LINE_LENGTH
;
78 int option_typewriter_wrap
= 0;
79 int option_auto_para_formatting
= 0;
80 int option_fill_tabs_with_spaces
= 0;
81 int option_return_does_auto_indent
= 1;
82 int option_backspace_through_tabs
= 0;
83 int option_fake_half_tabs
= 1;
84 int option_save_mode
= EDIT_QUICK_SAVE
;
85 int option_save_position
= 1;
86 int option_max_undo
= 32768;
87 int option_persistent_selections
= 1;
88 int option_cursor_beyond_eol
= 0;
89 int option_line_state
= 0;
90 int option_line_state_width
= 0;
92 int option_edit_right_extreme
= 0;
93 int option_edit_left_extreme
= 0;
94 int option_edit_top_extreme
= 0;
95 int option_edit_bottom_extreme
= 0;
96 int enable_show_tabs_tws
= 1;
97 int option_check_nl_at_eof
= 0;
98 int option_group_undo
= 0;
99 int show_right_margin
= 0;
101 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
102 char *option_backup_ext
= NULL
;
104 int edit_stack_iterator
= 0;
105 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
106 /* magic sequense for say than block is vertical */
107 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
109 /*** file scope macro definitions ****************************************************************/
111 #define TEMP_BUF_LEN 1024
113 #define space_width 1
115 /*** file scope type declarations ****************************************************************/
117 /*** file scope variables ************************************************************************/
119 /* detecting an error on save is easy: just check if every byte has been written. */
120 /* detecting an error on read, is not so easy 'cos there is not way to tell
121 whether you read everything or not. */
122 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
123 static const struct edit_filters
125 const char *read
, *write
, *extension
;
129 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
130 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
131 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
132 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
133 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
137 static long last_bracket
= -1;
139 /*** file scope functions ************************************************************************/
140 /* --------------------------------------------------------------------------------------------- */
144 * here's a quick sketch of the layout: (don't run this through indent.)
146 * (b1 is buffers1 and b2 is buffers2)
149 * \0\0\0\0\0m e _ f i l e . \nf i n . \n|T h i s _ i s _ s o\0\0\0\0\0\0\0\0\0
150 * ______________________________________|______________________________________
152 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
153 * |-> |-> |-> |-> |-> |-> |
155 * _<------------------------->|<----------------->_
156 * WEdit->curs2 | WEdit->curs1
161 * file end|||file beginning
170 /* --------------------------------------------------------------------------------------------- */
172 static int left_of_four_spaces (WEdit
* edit
);
174 /* --------------------------------------------------------------------------------------------- */
179 const char *header
= N_("About");
180 const char *button_name
= N_("&OK");
181 const char *const version
= "MCEdit " VERSION
;
182 char text
[BUF_LARGE
];
184 int win_len
, version_len
, button_len
;
191 button_name
= _(button_name
);
194 button_len
= str_term_width1 (button_name
) + 5;
195 version_len
= str_term_width1 (version
);
197 g_snprintf (text
, sizeof (text
),
198 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
199 " A user friendly text editor\n"
200 " written for the Midnight Commander"));
202 win_len
= str_term_width1 (header
);
203 win_len
= max (win_len
, version_len
);
204 win_len
= max (win_len
, button_len
);
206 /* count width and height of text */
207 str_msg_term_size (text
, &lines
, &cols
);
209 cols
= max (win_len
, cols
) + 6;
212 about_dlg
= create_dlg (TRUE
, 0, 0, lines
, cols
, dialog_colors
, NULL
, NULL
,
213 "[Internal File Editor]", header
, DLG_CENTER
| DLG_TRYUP
);
215 add_widget (about_dlg
, label_new (3, (cols
- version_len
) / 2, version
));
216 add_widget (about_dlg
, label_new (5, 3, text
));
217 add_widget (about_dlg
, button_new (lines
- 3, (cols
- button_len
) / 2,
218 B_ENTER
, NORMAL_BUTTON
, button_name
, NULL
));
221 destroy_dlg (about_dlg
);
224 /* --------------------------------------------------------------------------------------------- */
226 * Initialize the buffers for an empty files.
230 edit_init_buffers (WEdit
* edit
)
234 for (j
= 0; j
<= MAXBUFF
; j
++)
236 edit
->buffers1
[j
] = NULL
;
237 edit
->buffers2
[j
] = NULL
;
242 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
245 /* --------------------------------------------------------------------------------------------- */
248 * Load file OR text into buffers. Set cursor to the beginning of file.
250 * @returns FALSE on error.
254 edit_load_file_fast (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
258 gboolean ret
= FALSE
;
260 edit
->curs2
= edit
->last_byte
;
261 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
263 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
266 gchar
*errmsg
, *filename
;
268 filename
= vfs_path_to_str (filename_vpath
);
269 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
271 edit_error_dialog (_("Error"), errmsg
);
276 if (!edit
->buffers2
[buf2
])
277 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
282 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
283 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
286 for (buf
= buf2
- 1; buf
>= 0; buf
--)
288 /* edit->buffers2[0] is already allocated */
289 if (!edit
->buffers2
[buf
])
290 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
291 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
300 gchar
*errmsg
, *filename
;
302 filename
= vfs_path_to_str (filename_vpath
);
303 errmsg
= g_strdup_printf (_("Error reading %s"), filename
);
305 edit_error_dialog (_("Error"), errmsg
);
312 /* --------------------------------------------------------------------------------------------- */
313 /** Return index of the filter or -1 is there is no appropriate filter */
316 edit_find_filter (const vfs_path_t
* filename_vpath
)
321 if (filename_vpath
== NULL
)
324 filename
= vfs_path_to_str (filename_vpath
);
325 l
= strlen (filename
);
326 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
328 e
= strlen (all_filters
[i
].extension
);
330 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
340 /* --------------------------------------------------------------------------------------------- */
343 edit_get_filter (const vfs_path_t
* filename_vpath
)
346 char *p
, *quoted_name
, *filename
;
348 i
= edit_find_filter (filename_vpath
);
352 filename
= vfs_path_to_str (filename_vpath
);
353 quoted_name
= name_quote (filename
, 0);
355 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
356 g_free (quoted_name
);
360 /* --------------------------------------------------------------------------------------------- */
363 edit_insert_stream (WEdit
* edit
, FILE * f
)
367 while ((c
= fgetc (f
)) >= 0)
369 edit_insert (edit
, c
);
375 /* --------------------------------------------------------------------------------------------- */
377 * Open file and create it if necessary.
379 * @param edit editor object
380 * @param filename_vpath file name
381 * @param st buffer for store stat info
382 * @returns TRUE for success, FALSE for error.
386 check_file_access (WEdit
* edit
, const vfs_path_t
* filename_vpath
, struct stat
*st
)
389 gchar
*errmsg
= NULL
;
391 /* Try opening an existing file */
392 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
396 * Try creating the file. O_EXCL prevents following broken links
397 * and opening existing files.
399 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
404 filename
= vfs_path_to_str (filename_vpath
);
405 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
410 /* New file, delete it if it's not modified or saved */
411 edit
->delete_file
= 1;
414 /* Check what we have opened */
415 if (mc_fstat (file
, st
) < 0)
419 filename
= vfs_path_to_str (filename_vpath
);
420 errmsg
= g_strdup_printf (_("Cannot get size/permissions for %s"), filename
);
425 /* We want to open regular files only */
426 if (!S_ISREG (st
->st_mode
))
430 filename
= vfs_path_to_str (filename_vpath
);
431 errmsg
= g_strdup_printf (_("\"%s\" is not a regular file"), filename
);
437 * Don't delete non-empty files.
438 * O_EXCL should prevent it, but let's be on the safe side.
441 edit
->delete_file
= 0;
443 if (st
->st_size
>= SIZE_LIMIT
)
447 filename
= vfs_path_to_str (filename_vpath
);
448 errmsg
= g_strdup_printf (_("File \"%s\" is too large"), filename
);
453 (void) mc_close (file
);
457 edit_error_dialog (_("Error"), errmsg
);
464 /* --------------------------------------------------------------------------------------------- */
467 * Open the file and load it into the buffers, either directly or using
468 * a filter. Return TRUE on success, FALSE on error.
470 * Fast loading (edit_load_file_fast) is used when the file size is
471 * known. In this case the data is read into the buffers by blocks.
472 * If the file size is not known, the data is loaded byte by byte in
475 * @param edit editor object
476 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
479 edit_load_file (WEdit
* edit
)
481 gboolean fast_load
= TRUE
;
483 /* Cannot do fast load if a filter is used */
484 if (edit_find_filter (edit
->filename_vpath
) >= 0)
488 * FIXME: line end translation should disable fast loading as well
489 * Consider doing fseek() to the end and ftell() for the real size.
491 if (edit
->filename_vpath
!= NULL
)
494 * VFS may report file size incorrectly, and slow load is not a big
495 * deal considering overhead in VFS.
497 if (!vfs_file_is_local (edit
->filename_vpath
))
500 /* If we are dealing with a real file, check that it exists */
501 if (!check_file_access (edit
, edit
->filename_vpath
, &edit
->stat1
))
506 /* nothing to load */
510 edit_init_buffers (edit
);
514 edit
->last_byte
= edit
->stat1
.st_size
;
515 edit_load_file_fast (edit
, edit
->filename_vpath
);
516 /* If fast load was used, the number of lines wasn't calculated */
517 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
522 if (edit
->filename_vpath
!= NULL
523 && *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) != '\0')
525 edit
->undo_stack_disable
= 1;
526 if (edit_insert_file (edit
, edit
->filename_vpath
) < 0)
531 edit
->undo_stack_disable
= 0;
538 /* --------------------------------------------------------------------------------------------- */
539 /** Restore saved cursor position in the file */
542 edit_load_position (WEdit
* edit
)
547 if (edit
->filename_vpath
== NULL
548 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
551 load_file_position (edit
->filename_vpath
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
555 edit_move_to_line (edit
, line
- 1);
556 edit
->prev_col
= column
;
560 edit_cursor_move (edit
, offset
);
561 line
= edit
->curs_line
;
562 edit
->search_start
= edit
->curs1
;
565 book_mark_restore (edit
, BOOK_MARK_COLOR
);
567 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
568 edit_move_display (edit
, line
- (edit
->widget
.lines
/ 2));
571 /* --------------------------------------------------------------------------------------------- */
572 /** Save cursor position in the file */
575 edit_save_position (WEdit
* edit
)
577 if (edit
->filename_vpath
== NULL
578 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
581 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
582 save_file_position (edit
->filename_vpath
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
,
583 edit
->serialized_bookmarks
);
584 edit
->serialized_bookmarks
= NULL
;
587 /* --------------------------------------------------------------------------------------------- */
588 /** Clean the WEdit stricture except the widget part */
591 edit_purge_widget (WEdit
* edit
)
593 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
594 char *start
= (char *) edit
+ sizeof (Widget
);
595 memset (start
, 0, len
);
598 /* --------------------------------------------------------------------------------------------- */
601 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
602 then the file should be as it was when he loaded up. Then set edit->modified to 0.
606 edit_pop_undo_action (WEdit
* edit
)
609 unsigned long sp
= edit
->undo_stack_pointer
;
611 if (sp
== edit
->undo_stack_bottom
)
614 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
615 c
= edit
->undo_stack
[sp
];
618 /* edit->undo_stack[sp] = '@'; */
619 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
623 if (sp
== edit
->undo_stack_bottom
)
626 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
627 if (edit
->undo_stack
[sp
] == -2)
629 /* edit->undo_stack[sp] = '@'; */
630 edit
->undo_stack_pointer
= sp
;
633 edit
->undo_stack
[sp
]++;
639 edit_pop_redo_action (WEdit
* edit
)
642 unsigned long sp
= edit
->redo_stack_pointer
;
644 if (sp
== edit
->redo_stack_bottom
)
647 sp
= (sp
- 1) & edit
->redo_stack_size_mask
;
648 c
= edit
->redo_stack
[sp
];
651 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
655 if (sp
== edit
->redo_stack_bottom
)
658 c
= edit
->redo_stack
[(sp
- 1) & edit
->redo_stack_size_mask
];
659 if (edit
->redo_stack
[sp
] == -2)
660 edit
->redo_stack_pointer
= sp
;
662 edit
->redo_stack
[sp
]++;
668 get_prev_undo_action (WEdit
* edit
)
671 unsigned long sp
= edit
->undo_stack_pointer
;
673 if (sp
== edit
->undo_stack_bottom
)
676 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
677 c
= edit
->undo_stack
[sp
];
681 if (sp
== edit
->undo_stack_bottom
)
684 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
688 /* --------------------------------------------------------------------------------------------- */
689 /** is called whenever a modification is made by one of the four routines below */
692 edit_modification (WEdit
* edit
)
694 edit
->caches_valid
= 0;
696 /* raise lock when file modified */
697 if (!edit
->modified
&& !edit
->delete_file
)
698 edit
->locked
= lock_file (edit
->filename_vpath
);
702 /* --------------------------------------------------------------------------------------------- */
705 edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
707 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
710 if (byte_index
>= edit
->curs1
)
714 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
715 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
716 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
719 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
720 (byte_index
& M_EDIT_BUF_SIZE
));
723 /* --------------------------------------------------------------------------------------------- */
726 edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
729 gchar utf8_buf
[3 * UTF8_CHAR_LEN
+ 1];
731 gchar
*cursor_buf_ptr
;
733 if (byte_index
> (edit
->curs1
+ edit
->curs2
) || byte_index
<= 0)
739 for (i
= 0; i
< (3 * UTF8_CHAR_LEN
); i
++)
740 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
- (2 * UTF8_CHAR_LEN
));
741 utf8_buf
[3 * UTF8_CHAR_LEN
] = '\0';
743 cursor_buf_ptr
= utf8_buf
+ (2 * UTF8_CHAR_LEN
);
744 str
= g_utf8_find_prev_char (utf8_buf
, cursor_buf_ptr
);
746 if (str
== NULL
|| g_utf8_next_char (str
) != cursor_buf_ptr
)
749 return *(cursor_buf_ptr
- 1);
753 res
= g_utf8_get_char_validated (str
, -1);
758 return *(cursor_buf_ptr
- 1);
762 *char_width
= cursor_buf_ptr
- str
;
768 /* --------------------------------------------------------------------------------------------- */
771 edit_backspace (WEdit
* edit
, const int byte_delete
)
782 if (edit
->mark2
!= edit
->mark1
)
783 edit_push_markers (edit
);
785 if (edit
->utf8
&& byte_delete
== 0)
787 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
791 for (i
= 1; i
<= cw
; i
++)
793 if (edit
->mark1
>= edit
->curs1
)
796 edit
->end_mark_curs
--;
798 if (edit
->mark2
>= edit
->curs1
)
800 if (edit
->last_get_rule
>= edit
->curs1
)
801 edit
->last_get_rule
--;
803 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
804 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
805 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
807 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
808 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
812 edit_push_undo_action (edit
, p
);
814 edit_modification (edit
);
818 book_mark_dec (edit
, edit
->curs_line
);
821 edit
->force
|= REDRAW_AFTER_CURSOR
;
824 if (edit
->curs1
< edit
->start_display
)
826 edit
->start_display
--;
834 /* --------------------------------------------------------------------------------------------- */
835 /* high level cursor movement commands */
836 /* --------------------------------------------------------------------------------------------- */
839 is_in_indent (WEdit
* edit
)
841 long p
= edit_bol (edit
, edit
->curs1
);
842 while (p
< edit
->curs1
)
843 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
848 /* --------------------------------------------------------------------------------------------- */
851 is_blank (WEdit
* edit
, long offset
)
855 s
= edit_bol (edit
, offset
);
856 f
= edit_eol (edit
, offset
) - 1;
859 c
= edit_get_byte (edit
, s
++);
867 /* --------------------------------------------------------------------------------------------- */
868 /** returns the offset of line i */
871 edit_find_line (WEdit
* edit
, int line
)
875 if (!edit
->caches_valid
)
877 for (i
= 0; i
< N_LINE_CACHES
; i
++)
878 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
879 /* three offsets that we *know* are line 0 at 0 and these two: */
880 edit
->line_numbers
[1] = edit
->curs_line
;
881 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
882 edit
->line_numbers
[2] = edit
->total_lines
;
883 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
884 edit
->caches_valid
= 1;
886 if (line
>= edit
->total_lines
)
887 return edit
->line_offsets
[2];
890 /* find the closest known point */
891 for (i
= 0; i
< N_LINE_CACHES
; i
++)
894 n
= abs (edit
->line_numbers
[i
] - line
);
902 return edit
->line_offsets
[j
]; /* know the offset exactly */
903 if (m
== 1 && j
>= 3)
904 i
= j
; /* one line different - caller might be looping, so stay in this cache */
906 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
907 if (line
> edit
->line_numbers
[j
])
908 edit
->line_offsets
[i
] =
909 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
911 edit
->line_offsets
[i
] =
912 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
913 edit
->line_numbers
[i
] = line
;
914 return edit
->line_offsets
[i
];
917 /* --------------------------------------------------------------------------------------------- */
918 /** moves up until a blank line is reached, or until just
919 before a non-blank line is reached */
922 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
925 if (edit
->curs_line
> 1)
927 if (line_is_blank (edit
, edit
->curs_line
))
929 if (line_is_blank (edit
, edit
->curs_line
- 1))
931 for (i
= edit
->curs_line
- 1; i
; i
--)
932 if (!line_is_blank (edit
, i
))
940 for (i
= edit
->curs_line
- 1; i
; i
--)
941 if (line_is_blank (edit
, i
))
947 for (i
= edit
->curs_line
- 1; i
; i
--)
948 if (line_is_blank (edit
, i
))
952 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
955 /* --------------------------------------------------------------------------------------------- */
956 /** moves down until a blank line is reached, or until just
957 before a non-blank line is reached */
960 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
963 if (edit
->curs_line
>= edit
->total_lines
- 1)
965 i
= edit
->total_lines
;
969 if (line_is_blank (edit
, edit
->curs_line
))
971 if (line_is_blank (edit
, edit
->curs_line
+ 1))
973 for (i
= edit
->curs_line
+ 1; i
; i
++)
974 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
982 for (i
= edit
->curs_line
+ 1; i
; i
++)
983 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
989 for (i
= edit
->curs_line
+ 1; i
; i
++)
990 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
994 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
997 /* --------------------------------------------------------------------------------------------- */
1000 edit_begin_page (WEdit
* edit
)
1002 edit_update_curs_row (edit
);
1003 edit_move_up (edit
, edit
->curs_row
, 0);
1006 /* --------------------------------------------------------------------------------------------- */
1009 edit_end_page (WEdit
* edit
)
1011 edit_update_curs_row (edit
);
1012 edit_move_down (edit
, edit
->widget
.lines
- edit
->curs_row
- 1, 0);
1016 /* --------------------------------------------------------------------------------------------- */
1017 /** goto beginning of text */
1020 edit_move_to_top (WEdit
* edit
)
1022 if (edit
->curs_line
)
1024 edit_cursor_move (edit
, -edit
->curs1
);
1025 edit_move_to_prev_col (edit
, 0);
1026 edit
->force
|= REDRAW_PAGE
;
1027 edit
->search_start
= 0;
1028 edit_update_curs_row (edit
);
1033 /* --------------------------------------------------------------------------------------------- */
1034 /** goto end of text */
1037 edit_move_to_bottom (WEdit
* edit
)
1039 if (edit
->curs_line
< edit
->total_lines
)
1041 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
1042 edit
->start_display
= edit
->last_byte
;
1043 edit
->start_line
= edit
->total_lines
;
1044 edit_scroll_upward (edit
, edit
->widget
.lines
- 1);
1045 edit
->force
|= REDRAW_PAGE
;
1049 /* --------------------------------------------------------------------------------------------- */
1050 /** goto beginning of line */
1053 edit_cursor_to_bol (WEdit
* edit
)
1055 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1056 edit
->search_start
= edit
->curs1
;
1057 edit
->prev_col
= edit_get_col (edit
);
1061 /* --------------------------------------------------------------------------------------------- */
1062 /** goto end of line */
1065 edit_cursor_to_eol (WEdit
* edit
)
1067 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1068 edit
->search_start
= edit
->curs1
;
1069 edit
->prev_col
= edit_get_col (edit
);
1073 /* --------------------------------------------------------------------------------------------- */
1075 static unsigned long
1080 const char option_chars_move_whole_word
[] =
1081 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1087 if (*option_chars_move_whole_word
== '!')
1089 return 0x80000000UL
;
1091 if (g_ascii_isupper ((gchar
) c
))
1093 else if (g_ascii_islower ((gchar
) c
))
1095 else if (g_ascii_isalpha (c
))
1097 else if (isdigit (c
))
1099 else if (isspace (c
))
1101 q
= strchr (option_chars_move_whole_word
, c
);
1103 return 0xFFFFFFFFUL
;
1106 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1111 while ((q
= strchr (q
+ 1, c
)));
1115 /* --------------------------------------------------------------------------------------------- */
1118 edit_left_word_move (WEdit
* edit
, int s
)
1123 if (edit
->column_highlight
1124 && edit
->mark1
!= edit
->mark2
1125 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1127 edit_cursor_move (edit
, -1);
1130 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1131 c2
= edit_get_byte (edit
, edit
->curs1
);
1132 if (c1
== '\n' || c2
== '\n')
1134 if (!(my_type_of (c1
) & my_type_of (c2
)))
1136 if (isspace (c1
) && !isspace (c2
))
1139 if (!isspace (c1
) && isspace (c2
))
1144 /* --------------------------------------------------------------------------------------------- */
1147 edit_left_word_move_cmd (WEdit
* edit
)
1149 edit_left_word_move (edit
, 0);
1150 edit
->force
|= REDRAW_PAGE
;
1153 /* --------------------------------------------------------------------------------------------- */
1156 edit_right_word_move (WEdit
* edit
, int s
)
1161 if (edit
->column_highlight
1162 && edit
->mark1
!= edit
->mark2
1163 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
1165 edit_cursor_move (edit
, 1);
1166 if (edit
->curs1
>= edit
->last_byte
)
1168 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1169 c2
= edit_get_byte (edit
, edit
->curs1
);
1170 if (c1
== '\n' || c2
== '\n')
1172 if (!(my_type_of (c1
) & my_type_of (c2
)))
1174 if (isspace (c1
) && !isspace (c2
))
1177 if (!isspace (c1
) && isspace (c2
))
1182 /* --------------------------------------------------------------------------------------------- */
1185 edit_right_word_move_cmd (WEdit
* edit
)
1187 edit_right_word_move (edit
, 0);
1188 edit
->force
|= REDRAW_PAGE
;
1191 /* --------------------------------------------------------------------------------------------- */
1194 edit_right_char_move_cmd (WEdit
* edit
)
1200 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
1206 c
= edit_get_byte (edit
, edit
->curs1
);
1208 if (option_cursor_beyond_eol
&& c
== '\n')
1214 edit_cursor_move (edit
, cw
);
1218 /* --------------------------------------------------------------------------------------------- */
1221 edit_left_char_move_cmd (WEdit
* edit
)
1224 if (edit
->column_highlight
1225 && option_cursor_beyond_eol
1226 && edit
->mark1
!= edit
->mark2
1227 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1231 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1235 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1241 edit_cursor_move (edit
, -cw
);
1245 /* --------------------------------------------------------------------------------------------- */
1246 /** Up or down cursor moving.
1247 direction = TRUE - move up
1252 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
1255 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
1264 edit
->force
|= REDRAW_PAGE
;
1268 edit_scroll_upward (edit
, i
);
1270 edit_scroll_downward (edit
, i
);
1272 p
= edit_bol (edit
, edit
->curs1
);
1274 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
1276 edit_cursor_move (edit
, p
- edit
->curs1
);
1278 edit_move_to_prev_col (edit
, p
);
1280 /* search start of current multibyte char (like CJK) */
1281 if (edit
->curs1
+ 1 < edit
->last_byte
)
1283 edit_right_char_move_cmd (edit
);
1284 edit_left_char_move_cmd (edit
);
1287 edit
->search_start
= edit
->curs1
;
1288 edit
->found_len
= 0;
1291 /* --------------------------------------------------------------------------------------------- */
1294 edit_right_delete_word (WEdit
* edit
)
1299 if (edit
->curs1
>= edit
->last_byte
)
1301 c1
= edit_delete (edit
, 1);
1302 c2
= edit_get_byte (edit
, edit
->curs1
);
1303 if (c1
== '\n' || c2
== '\n')
1305 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1307 if (!(my_type_of (c1
) & my_type_of (c2
)))
1312 /* --------------------------------------------------------------------------------------------- */
1315 edit_left_delete_word (WEdit
* edit
)
1320 if (edit
->curs1
<= 0)
1322 c1
= edit_backspace (edit
, 1);
1323 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1324 if (c1
== '\n' || c2
== '\n')
1326 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1328 if (!(my_type_of (c1
) & my_type_of (c2
)))
1333 /* --------------------------------------------------------------------------------------------- */
1335 the start column position is not recorded, and hence does not
1336 undo as it happed. But who would notice.
1340 edit_do_undo (WEdit
* edit
)
1345 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1347 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1354 edit_cursor_move (edit
, 1);
1357 edit_cursor_move (edit
, -1);
1361 edit_backspace (edit
, 1);
1365 edit_delete (edit
, 1);
1368 edit
->column_highlight
= 1;
1371 edit
->column_highlight
= 0;
1374 if (ac
>= 256 && ac
< 512)
1375 edit_insert_ahead (edit
, ac
- 256);
1376 if (ac
>= 0 && ac
< 256)
1377 edit_insert (edit
, ac
);
1379 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1381 edit
->mark1
= ac
- MARK_1
;
1382 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1384 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1386 edit
->mark2
= ac
- MARK_2
;
1387 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1389 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1391 edit
->end_mark_curs
= ac
- MARK_CURS
;
1394 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1397 if (edit
->start_display
> ac
- KEY_PRESS
)
1399 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1400 edit
->force
|= REDRAW_PAGE
;
1402 else if (edit
->start_display
< ac
- KEY_PRESS
)
1404 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1405 edit
->force
|= REDRAW_PAGE
;
1407 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1408 edit_update_curs_row (edit
);
1411 edit
->undo_stack_disable
= 0;
1415 edit_do_redo (WEdit
* edit
)
1420 if (edit
->redo_stack_reset
)
1424 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1431 edit_cursor_move (edit
, 1);
1434 edit_cursor_move (edit
, -1);
1437 edit_backspace (edit
, 1);
1440 edit_delete (edit
, 1);
1443 edit
->column_highlight
= 1;
1446 edit
->column_highlight
= 0;
1449 if (ac
>= 256 && ac
< 512)
1450 edit_insert_ahead (edit
, ac
- 256);
1451 if (ac
>= 0 && ac
< 256)
1452 edit_insert (edit
, ac
);
1454 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1456 edit
->mark1
= ac
- MARK_1
;
1457 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1459 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1461 edit
->mark2
= ac
- MARK_2
;
1462 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1464 /* more than one pop usually means something big */
1466 edit
->force
|= REDRAW_PAGE
;
1469 if (edit
->start_display
> ac
- KEY_PRESS
)
1471 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1472 edit
->force
|= REDRAW_PAGE
;
1474 else if (edit
->start_display
< ac
- KEY_PRESS
)
1476 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1477 edit
->force
|= REDRAW_PAGE
;
1479 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1480 edit_update_curs_row (edit
);
1486 edit_group_undo (WEdit
* edit
)
1488 long ac
= KEY_PRESS
;
1489 long cur_ac
= KEY_PRESS
;
1490 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1492 cur_ac
= get_prev_undo_action (edit
);
1493 edit_do_undo (edit
);
1494 ac
= get_prev_undo_action (edit
);
1495 /* exit from cycle if option_group_undo is not set,
1496 * and make single UNDO operation
1498 if (!option_group_undo
)
1503 /* --------------------------------------------------------------------------------------------- */
1506 edit_delete_to_line_end (WEdit
* edit
)
1508 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
1512 edit_delete (edit
, 1);
1516 /* --------------------------------------------------------------------------------------------- */
1519 edit_delete_to_line_begin (WEdit
* edit
)
1521 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
1525 edit_backspace (edit
, 1);
1529 /* --------------------------------------------------------------------------------------------- */
1532 is_aligned_on_a_tab (WEdit
* edit
)
1534 edit_update_curs_col (edit
);
1535 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
1536 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
1539 /* --------------------------------------------------------------------------------------------- */
1542 right_of_four_spaces (WEdit
* edit
)
1545 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1546 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1548 return is_aligned_on_a_tab (edit
);
1552 /* --------------------------------------------------------------------------------------------- */
1555 left_of_four_spaces (WEdit
* edit
)
1558 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1559 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1561 return is_aligned_on_a_tab (edit
);
1565 /* --------------------------------------------------------------------------------------------- */
1568 edit_auto_indent (WEdit
* edit
)
1573 /* use the previous line as a template */
1574 p
= edit_move_backward (edit
, p
, 1);
1575 /* copy the leading whitespace of the line */
1577 { /* no range check - the line _is_ \n-terminated */
1578 c
= edit_get_byte (edit
, p
++);
1579 if (c
!= ' ' && c
!= '\t')
1581 edit_insert (edit
, c
);
1585 /* --------------------------------------------------------------------------------------------- */
1588 edit_double_newline (WEdit
* edit
)
1590 edit_insert (edit
, '\n');
1591 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1593 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1595 edit
->force
|= REDRAW_PAGE
;
1596 edit_insert (edit
, '\n');
1600 /* --------------------------------------------------------------------------------------------- */
1603 insert_spaces_tab (WEdit
* edit
, gboolean half
)
1607 edit_update_curs_col (edit
);
1608 i
= option_tab_spacing
* space_width
;
1611 i
= ((edit
->curs_col
/ i
) + 1) * i
- edit
->curs_col
;
1614 edit_insert (edit
, ' ');
1619 /* --------------------------------------------------------------------------------------------- */
1622 edit_tab_cmd (WEdit
* edit
)
1626 if (option_fake_half_tabs
)
1628 if (is_in_indent (edit
))
1630 /*insert a half tab (usually four spaces) unless there is a
1631 half tab already behind, then delete it and insert a
1633 if (option_fill_tabs_with_spaces
|| !right_of_four_spaces (edit
))
1634 insert_spaces_tab (edit
, TRUE
);
1637 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1638 edit_backspace (edit
, 1);
1639 edit_insert (edit
, '\t');
1644 if (option_fill_tabs_with_spaces
)
1645 insert_spaces_tab (edit
, FALSE
);
1647 edit_insert (edit
, '\t');
1650 /* --------------------------------------------------------------------------------------------- */
1653 check_and_wrap_line (WEdit
* edit
)
1656 if (!option_typewriter_wrap
)
1658 edit_update_curs_col (edit
);
1659 if (edit
->curs_col
< option_word_wrap_line_length
)
1665 c
= edit_get_byte (edit
, curs
);
1666 if (c
== '\n' || curs
<= 0)
1668 edit_insert (edit
, '\n');
1671 if (c
== ' ' || c
== '\t')
1673 int current
= edit
->curs1
;
1674 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
1675 edit_insert (edit
, '\n');
1676 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
1682 /* --------------------------------------------------------------------------------------------- */
1683 /** this find the matching bracket in either direction, and sets edit->bracket */
1686 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
1688 const char *const b
= "{}{[][()(", *p
;
1689 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
1690 unsigned long j
= 0;
1692 edit_update_curs_row (edit
);
1693 c
= edit_get_byte (edit
, edit
->curs1
);
1696 if (!furthest_bracket_search
)
1697 furthest_bracket_search
--;
1698 /* not on a bracket at all */
1701 /* the matching bracket */
1703 /* going left or right? */
1704 if (strchr ("{[(", c
))
1706 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
1708 /* out of buffer? */
1709 if (q
>= edit
->last_byte
|| q
< 0)
1711 a
= edit_get_byte (edit
, q
);
1712 /* don't want to eat CPU */
1713 if (j
++ > furthest_bracket_search
)
1715 /* out of screen? */
1718 if (q
< edit
->start_display
)
1720 /* count lines if searching downward */
1721 if (inc
> 0 && a
== '\n')
1722 if (n
++ >= edit
->widget
.lines
- edit
->curs_row
) /* out of screen */
1725 /* count bracket depth */
1726 i
+= (a
== c
) - (a
== d
);
1727 /* return if bracket depth is zero */
1735 /* --------------------------------------------------------------------------------------------- */
1738 edit_goto_matching_bracket (WEdit
* edit
)
1742 q
= edit_get_bracket (edit
, 0, 0);
1745 edit
->bracket
= edit
->curs1
;
1746 edit
->force
|= REDRAW_PAGE
;
1747 edit_cursor_move (edit
, q
- edit
->curs1
);
1751 /* --------------------------------------------------------------------------------------------- */
1754 edit_move_block_to_right (WEdit
* edit
)
1756 long start_mark
, end_mark
;
1757 long cur_bol
, start_bol
;
1759 if (eval_marks (edit
, &start_mark
, &end_mark
))
1762 start_bol
= edit_bol (edit
, start_mark
);
1763 cur_bol
= edit_bol (edit
, end_mark
- 1);
1767 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1768 if (option_fill_tabs_with_spaces
)
1769 insert_spaces_tab (edit
, option_fake_half_tabs
);
1771 edit_insert (edit
, '\t');
1772 edit_cursor_move (edit
, edit_bol (edit
, cur_bol
) - edit
->curs1
);
1777 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1779 while (cur_bol
>= start_bol
);
1781 edit
->force
|= REDRAW_PAGE
;
1784 /* --------------------------------------------------------------------------------------------- */
1787 edit_move_block_to_left (WEdit
* edit
)
1789 long start_mark
, end_mark
;
1790 long cur_bol
, start_bol
;
1793 if (eval_marks (edit
, &start_mark
, &end_mark
))
1796 start_bol
= edit_bol (edit
, start_mark
);
1797 cur_bol
= edit_bol (edit
, end_mark
- 1);
1804 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1806 if (option_fake_half_tabs
)
1807 del_tab_width
= HALF_TAB_SIZE
;
1809 del_tab_width
= option_tab_spacing
;
1811 next_char
= edit_get_byte (edit
, edit
->curs1
);
1812 if (next_char
== '\t')
1813 edit_delete (edit
, 1);
1814 else if (next_char
== ' ')
1815 for (i
= 1; i
<= del_tab_width
; i
++)
1817 if (next_char
== ' ')
1818 edit_delete (edit
, 1);
1819 next_char
= edit_get_byte (edit
, edit
->curs1
);
1825 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1827 while (cur_bol
>= start_bol
);
1829 edit
->force
|= REDRAW_PAGE
;
1832 /* --------------------------------------------------------------------------------------------- */
1834 * prints at the cursor
1835 * @returns the number of chars printed
1839 edit_print_string (WEdit
* e
, const char *s
)
1843 while (s
[i
] != '\0')
1844 edit_execute_cmd (e
, CK_InsertChar
, (unsigned char) s
[i
++]);
1845 e
->force
|= REDRAW_COMPLETELY
;
1846 edit_update_screen (e
);
1850 /* --------------------------------------------------------------------------------------------- */
1851 /*** public functions ****************************************************************************/
1852 /* --------------------------------------------------------------------------------------------- */
1854 /** User edit menu, like user menu (F2) but only in editor. */
1857 user_menu (WEdit
* edit
, const char *menu_file
, int selected_entry
)
1862 long start_mark
, end_mark
;
1864 vfs_path_t
*block_file_vpath
;
1866 block_file
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
1867 block_file_vpath
= vfs_path_from_str (block_file
);
1869 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
1871 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1873 /* run shell scripts from menu */
1874 if (user_menu_cmd (edit
, menu_file
, selected_entry
)
1875 && (mc_stat (block_file_vpath
, &status
) == 0) && (status
.st_size
!= 0))
1880 /* i.e. we have marked block */
1882 rc
= edit_block_delete_cmd (edit
);
1888 ins_len
= edit_insert_file (edit
, block_file_vpath
);
1889 if (nomark
== 0 && ins_len
> 0)
1890 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1892 /* truncate block file */
1893 fd
= fopen (block_file
, "w");
1897 edit_cursor_move (edit
, curs
- edit
->curs1
);
1898 edit_refresh_cmd (edit
);
1899 edit
->force
|= REDRAW_COMPLETELY
;
1901 g_free (block_file
);
1902 vfs_path_free (block_file_vpath
);
1905 /* --------------------------------------------------------------------------------------------- */
1908 edit_get_byte (WEdit
* edit
, long byte_index
)
1911 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1914 if (byte_index
>= edit
->curs1
)
1916 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1917 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
1921 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
1925 /* --------------------------------------------------------------------------------------------- */
1928 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1933 gchar
*next_ch
= NULL
;
1935 gchar utf8_buf
[UTF8_CHAR_LEN
+ 1];
1937 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1943 str
= edit_get_byte_ptr (edit
, byte_index
);
1951 res
= g_utf8_get_char_validated (str
, -1);
1955 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1957 for (i
= 0; i
< UTF8_CHAR_LEN
; i
++)
1958 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
);
1959 utf8_buf
[UTF8_CHAR_LEN
] = '\0';
1961 res
= g_utf8_get_char_validated (str
, -1);
1972 /* Calculate UTF-8 char width */
1973 next_ch
= g_utf8_next_char (str
);
1976 width
= next_ch
- str
;
1984 *char_width
= width
;
1988 /* --------------------------------------------------------------------------------------------- */
1991 edit_get_write_filter (const vfs_path_t
* write_name_vpath
, const vfs_path_t
* filename_vpath
)
1994 char *p
, *writename
;
1995 const vfs_path_element_t
*path_element
;
1997 i
= edit_find_filter (filename_vpath
);
2001 path_element
= vfs_path_get_by_index (write_name_vpath
, -1);
2002 writename
= name_quote (path_element
->path
, 0);
2003 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
2008 /* --------------------------------------------------------------------------------------------- */
2011 edit_write_stream (WEdit
* edit
, FILE * f
)
2015 if (edit
->lb
== LB_ASIS
)
2017 for (i
= 0; i
< edit
->last_byte
; i
++)
2018 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
2023 /* change line breaks */
2024 for (i
= 0; i
< edit
->last_byte
; i
++)
2026 unsigned char c
= edit_get_byte (edit
, i
);
2028 if (!(c
== '\n' || c
== '\r'))
2030 /* not line break */
2031 if (fputc (c
, f
) < 0)
2035 { /* (c == '\n' || c == '\r') */
2036 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
2040 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
2041 /* put one line break unconditionally */
2042 if (fputc ('\n', f
) < 0)
2045 i
++; /* 2 chars are processed */
2047 if (c
== '\r' && c1
== '\n')
2048 /* Windows line break; go to the next char */
2051 if (c
== '\r' && c1
== '\r')
2053 /* two Macintosh line breaks; put second line break */
2054 if (fputc ('\n', f
) < 0)
2059 if (fputc (c1
, f
) < 0)
2063 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
2064 /* put one line break unconditionally */
2065 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
2068 if (c
== '\r' && c1
== '\n')
2069 /* Windows line break; go to the next char */
2073 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
2074 /* put one line break unconditionally */
2075 if (fputc ('\r', f
) < 0)
2078 i
++; /* 2 chars are processed */
2080 if (c
== '\r' && c1
== '\n')
2081 /* Windows line break; go to the next char */
2084 if (c
== '\n' && c1
== '\n')
2086 /* two Windows line breaks; put second line break */
2087 if (fputc ('\r', f
) < 0)
2092 if (fputc (c1
, f
) < 0)
2095 case LB_ASIS
: /* default without changes */
2101 return edit
->last_byte
;
2104 /* --------------------------------------------------------------------------------------------- */
2105 /** inserts a file at the cursor, returns count of inserted bytes on success */
2107 edit_insert_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
2112 p
= edit_get_filter (filename_vpath
);
2116 long current
= edit
->curs1
;
2118 f
= (FILE *) popen (p
, "r");
2121 edit_insert_stream (edit
, f
);
2122 ins_len
= edit
->curs1
- current
;
2123 edit_cursor_move (edit
, -ins_len
);
2128 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
2129 edit_error_dialog (_("Error"), errmsg
);
2138 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
2139 edit_error_dialog (_("Error"), errmsg
);
2147 int i
, file
, blocklen
;
2148 long current
= edit
->curs1
;
2149 int vertical_insertion
= 0;
2152 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
2156 buf
= g_malloc0 (TEMP_BUF_LEN
);
2157 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2160 /* if contain signature VERTICAL_MAGIC then it vertical block */
2161 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2162 vertical_insertion
= 1;
2164 mc_lseek (file
, 0, SEEK_SET
);
2167 if (vertical_insertion
)
2172 blocklen
= edit_insert_column_of_text_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
2173 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2174 /* highlight inserted text then not persistent blocks */
2175 if (!option_persistent_selections
)
2177 if (!edit
->column_highlight
)
2178 edit_push_undo_action (edit
, COLUMN_OFF
);
2179 edit
->column_highlight
= 1;
2184 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2186 for (i
= 0; i
< blocklen
; i
++)
2187 edit_insert (edit
, buf
[i
]);
2189 /* highlight inserted text then not persistent blocks */
2190 if (!option_persistent_selections
&& edit
->modified
)
2192 edit_set_markers (edit
, edit
->curs1
, current
, 0, 0);
2193 if (edit
->column_highlight
)
2194 edit_push_undo_action (edit
, COLUMN_ON
);
2195 edit
->column_highlight
= 0;
2199 edit
->force
|= REDRAW_PAGE
;
2200 ins_len
= edit
->curs1
- current
;
2201 edit_cursor_move (edit
, -ins_len
);
2211 /* --------------------------------------------------------------------------------------------- */
2213 * Fill in the edit structure. Return NULL on failure. Pass edit as
2214 * NULL to allocate a new structure.
2216 * If line is 0, try to restore saved position. Otherwise put the
2217 * cursor on that line and show it in the middle of the screen.
2221 edit_init (WEdit
* edit
, int y
, int x
, int lines
, int cols
, const vfs_path_t
* filename_vpath
,
2224 gboolean to_free
= FALSE
;
2226 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2227 if (option_line_state
)
2228 option_line_state_width
= LINE_STATE_WIDTH
;
2230 option_line_state_width
= 0;
2236 * Expand option_whole_chars_search by national letters using
2240 static char option_whole_chars_search_buf
[256];
2242 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
2245 size_t len
= str_term_width1 (option_whole_chars_search
);
2247 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
2249 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
2251 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
2253 option_whole_chars_search_buf
[len
++] = i
;
2257 option_whole_chars_search_buf
[len
] = 0;
2258 option_whole_chars_search
= option_whole_chars_search_buf
;
2260 #endif /* ENABLE_NLS */
2261 edit
= g_malloc0 (sizeof (WEdit
));
2262 edit
->search
= NULL
;
2266 edit_purge_widget (edit
);
2267 edit
->drag_state
= MCEDIT_DRAG_NORMAL
;
2270 edit
->widget
.lines
= lines
;
2271 edit
->widget
.cols
= cols
;
2272 edit_save_size (edit
);
2274 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2275 edit
->stat1
.st_uid
= getuid ();
2276 edit
->stat1
.st_gid
= getgid ();
2277 edit
->stat1
.st_mtime
= 0;
2281 edit
->force
|= REDRAW_PAGE
;
2282 edit_set_filename (edit
, filename_vpath
);
2284 edit
->undo_stack_size
= START_STACK_SIZE
;
2285 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2286 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2288 edit
->redo_stack_size
= START_STACK_SIZE
;
2289 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2290 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2293 edit
->converter
= str_cnv_from_term
;
2294 edit_set_codeset (edit
);
2296 if (!edit_load_file (edit
))
2298 /* edit_load_file already gives an error message */
2304 edit
->loading_done
= 1;
2307 edit_load_syntax (edit
, NULL
, NULL
);
2310 edit_get_syntax_color (edit
, -1, &color
);
2313 /* load saved cursor position */
2314 if ((line
== 0) && option_save_position
)
2315 edit_load_position (edit
);
2320 edit_move_display (edit
, line
- 1);
2321 edit_move_to_line (edit
, line
- 1);
2324 edit_load_macro_cmd (edit
);
2328 /* --------------------------------------------------------------------------------------------- */
2330 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2332 edit_clean (WEdit
* edit
)
2339 /* a stale lock, remove it */
2341 edit
->locked
= unlock_file (edit
->filename_vpath
);
2343 /* save cursor position */
2344 if (option_save_position
)
2345 edit_save_position (edit
);
2346 else if (edit
->serialized_bookmarks
!= NULL
)
2347 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2349 /* File specified on the mcedit command line and never saved */
2350 if (edit
->delete_file
)
2351 unlink (vfs_path_get_last_path_str (edit
->filename_vpath
));
2353 edit_free_syntax_rules (edit
);
2354 book_mark_flush (edit
, -1);
2355 for (; j
<= MAXBUFF
; j
++)
2357 g_free (edit
->buffers1
[j
]);
2358 g_free (edit
->buffers2
[j
]);
2361 g_free (edit
->undo_stack
);
2362 g_free (edit
->redo_stack
);
2363 vfs_path_free (edit
->filename_vpath
);
2364 vfs_path_free (edit
->dir_vpath
);
2365 mc_search_free (edit
->search
);
2366 edit
->search
= NULL
;
2368 if (edit
->converter
!= str_cnv_from_term
)
2369 str_close_conv (edit
->converter
);
2371 edit_purge_widget (edit
);
2376 /* --------------------------------------------------------------------------------------------- */
2378 /** returns TRUE on success */
2380 edit_renew (WEdit
* edit
)
2382 int y
= edit
->widget
.y
;
2383 int x
= edit
->widget
.x
;
2384 int lines
= edit
->widget
.lines
;
2385 int columns
= edit
->widget
.cols
;
2388 return (edit_init (edit
, y
, x
, lines
, columns
, NULL
, 0) != NULL
);
2391 /* --------------------------------------------------------------------------------------------- */
2394 * Load a new file into the editor and set line. If it fails, preserve the old file.
2395 * To do it, allocate a new widget, initialize it and, if the new file
2396 * was loaded, copy the data to the old widget.
2398 * @returns TRUE on success, FALSE on failure.
2401 edit_reload_line (WEdit
* edit
, const vfs_path_t
* filename_vpath
, long line
)
2404 int y
= edit
->widget
.y
;
2405 int x
= edit
->widget
.x
;
2406 int lines
= edit
->widget
.lines
;
2407 int columns
= edit
->widget
.cols
;
2409 e
= g_malloc0 (sizeof (WEdit
));
2410 e
->widget
= edit
->widget
;
2412 if (edit_init (e
, y
, x
, lines
, columns
, filename_vpath
, line
) == NULL
)
2419 memcpy (edit
, e
, sizeof (WEdit
));
2425 /* --------------------------------------------------------------------------------------------- */
2428 edit_set_codeset (WEdit
* edit
)
2434 get_codepage_id (mc_global
.source_codepage
>=
2435 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2440 conv
= str_crt_conv_from (cp_id
);
2441 if (conv
!= INVALID_CONV
)
2443 if (edit
->converter
!= str_cnv_from_term
)
2444 str_close_conv (edit
->converter
);
2445 edit
->converter
= conv
;
2450 edit
->utf8
= str_isutf8 (cp_id
);
2457 /* --------------------------------------------------------------------------------------------- */
2459 Recording stack for undo:
2460 The following is an implementation of a compressed stack. Identical
2461 pushes are recorded by a negative prefix indicating the number of times the
2462 same char was pushed. This saves space for repeated curs-left or curs-right
2479 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2480 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2481 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2482 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2485 The only way the cursor moves or the buffer is changed is through the routines:
2486 insert, backspace, insert_ahead, delete, and cursor_move.
2487 These record the reverse undo movements onto the stack each time they are
2490 Each key press results in a set of actions (insert; delete ...). So each time
2491 a key is pressed the current position of start_display is pushed as
2492 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2493 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2494 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2499 edit_push_undo_action (WEdit
* edit
, long c
, ...)
2501 unsigned long sp
= edit
->undo_stack_pointer
;
2505 /* first enlarge the stack if necessary */
2506 if (sp
> edit
->undo_stack_size
- 10)
2508 if (option_max_undo
< 256)
2509 option_max_undo
= 256;
2510 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2512 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2515 edit
->undo_stack
= t
;
2516 edit
->undo_stack_size
<<= 1;
2517 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2521 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2522 if (edit
->undo_stack_disable
)
2524 edit_push_redo_action (edit
, KEY_PRESS
);
2525 edit_push_redo_action (edit
, c
);
2528 else if (edit
->redo_stack_reset
)
2530 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2533 if (edit
->undo_stack_bottom
!= sp
2534 && spm1
!= edit
->undo_stack_bottom
2535 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2538 if (edit
->undo_stack
[spm1
] < 0)
2540 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2543 if (edit
->undo_stack
[spm1
] > -1000000000)
2545 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2547 edit
->undo_stack
[spm1
]--;
2555 d
= edit
->undo_stack
[spm1
];
2559 return; /* --> no need to push multiple do-nothings */
2560 edit
->undo_stack
[sp
] = -2;
2565 edit
->undo_stack
[sp
] = c
;
2568 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2570 /* if the sp wraps round and catches the undo_stack_bottom then erase
2571 * the first set of actions on the stack to make space - by moving
2572 * undo_stack_bottom forward one "key press" */
2573 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2574 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2575 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2578 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2580 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2581 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2583 /*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: */
2584 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2585 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2587 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2592 edit_push_redo_action (WEdit
* edit
, long c
, ...)
2594 unsigned long sp
= edit
->redo_stack_pointer
;
2597 /* first enlarge the stack if necessary */
2598 if (sp
> edit
->redo_stack_size
- 10)
2600 if (option_max_undo
< 256)
2601 option_max_undo
= 256;
2602 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2604 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2607 edit
->redo_stack
= t
;
2608 edit
->redo_stack_size
<<= 1;
2609 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2613 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2615 if (edit
->redo_stack_bottom
!= sp
2616 && spm1
!= edit
->redo_stack_bottom
2617 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2620 if (edit
->redo_stack
[spm1
] < 0)
2622 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2625 if (edit
->redo_stack
[spm1
] > -1000000000)
2627 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2628 edit
->redo_stack
[spm1
]--;
2635 d
= edit
->redo_stack
[spm1
];
2639 return; /* --> no need to push multiple do-nothings */
2640 edit
->redo_stack
[sp
] = -2;
2641 goto redo_check_bottom
;
2645 edit
->redo_stack
[sp
] = c
;
2648 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2650 /* if the sp wraps round and catches the redo_stack_bottom then erase
2651 * the first set of actions on the stack to make space - by moving
2652 * redo_stack_bottom forward one "key press" */
2653 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2654 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2655 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2658 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2660 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2661 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2664 * If a single key produced enough pushes to wrap all the way round then
2665 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2666 * The stack is then initialised:
2669 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2670 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2671 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2675 /* --------------------------------------------------------------------------------------------- */
2677 Basic low level single character buffer alterations and movements at the cursor.
2678 Returns char passed over, inserted or removed.
2682 edit_insert (WEdit
* edit
, int c
)
2684 /* check if file has grown to large */
2685 if (edit
->last_byte
>= SIZE_LIMIT
)
2688 /* first we must update the position of the display window */
2689 if (edit
->curs1
< edit
->start_display
)
2691 edit
->start_display
++;
2696 /* Mark file as modified, unless the file hasn't been fully loaded */
2697 if (edit
->loading_done
)
2699 edit_modification (edit
);
2702 /* now we must update some info on the file and check if a redraw is required */
2705 if (edit
->book_mark
)
2706 book_mark_inc (edit
, edit
->curs_line
);
2708 edit
->total_lines
++;
2709 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2712 /* save the reverse command onto the undo stack */
2713 /* ordinary char and not space */
2715 edit_push_undo_action (edit
, BACKSPACE
);
2717 edit_push_undo_action (edit
, BACKSPACE_BR
);
2718 /* update markers */
2719 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2720 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2721 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2723 /* add a new buffer if we've reached the end of the last one */
2724 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2725 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2727 /* perform the insertion */
2728 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2729 = (unsigned char) c
;
2731 /* update file length */
2734 /* update cursor position */
2738 /* --------------------------------------------------------------------------------------------- */
2739 /** same as edit_insert and move left */
2742 edit_insert_ahead (WEdit
* edit
, int c
)
2744 if (edit
->last_byte
>= SIZE_LIMIT
)
2747 if (edit
->curs1
< edit
->start_display
)
2749 edit
->start_display
++;
2753 edit_modification (edit
);
2756 if (edit
->book_mark
)
2757 book_mark_inc (edit
, edit
->curs_line
);
2758 edit
->total_lines
++;
2759 edit
->force
|= REDRAW_AFTER_CURSOR
;
2761 /* ordinary char and not space */
2763 edit_push_undo_action (edit
, DELCHAR
);
2765 edit_push_undo_action (edit
, DELCHAR_BR
);
2767 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2768 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2769 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2771 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2772 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2773 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2774 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2781 /* --------------------------------------------------------------------------------------------- */
2784 edit_delete (WEdit
* edit
, const int byte_delete
)
2794 /* if byte_delete = 1 then delete only one byte not multibyte char */
2795 if (edit
->utf8
&& byte_delete
== 0)
2797 edit_get_utf (edit
, edit
->curs1
, &cw
);
2802 if (edit
->mark2
!= edit
->mark1
)
2803 edit_push_markers (edit
);
2805 for (i
= 1; i
<= cw
; i
++)
2807 if (edit
->mark1
> edit
->curs1
)
2810 edit
->end_mark_curs
--;
2812 if (edit
->mark2
> edit
->curs1
)
2814 if (edit
->last_get_rule
> edit
->curs1
)
2815 edit
->last_get_rule
--;
2817 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2819 1) & M_EDIT_BUF_SIZE
) - 1];
2821 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2823 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2824 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2828 edit_push_undo_action (edit
, p
+ 256);
2831 edit_modification (edit
);
2834 if (edit
->book_mark
)
2835 book_mark_dec (edit
, edit
->curs_line
);
2836 edit
->total_lines
--;
2837 edit
->force
|= REDRAW_AFTER_CURSOR
;
2839 if (edit
->curs1
< edit
->start_display
)
2841 edit
->start_display
--;
2849 /* --------------------------------------------------------------------------------------------- */
2850 /** moves the cursor right or left: increment positive or negative respectively */
2853 edit_cursor_move (WEdit
* edit
, long increment
)
2855 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2860 for (; increment
< 0; increment
++)
2865 edit_push_undo_action (edit
, CURS_RIGHT
);
2867 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2868 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2869 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2870 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2871 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2873 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2874 1) & M_EDIT_BUF_SIZE
];
2875 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2877 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2878 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2884 edit
->force
|= REDRAW_LINE_BELOW
;
2889 else if (increment
> 0)
2891 for (; increment
> 0; increment
--)
2896 edit_push_undo_action (edit
, CURS_LEFT
);
2898 c
= edit_get_byte (edit
, edit
->curs1
);
2899 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2900 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2901 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2903 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2905 1) & M_EDIT_BUF_SIZE
) - 1];
2906 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2908 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2909 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2915 edit
->force
|= REDRAW_LINE_ABOVE
;
2921 /* These functions return positions relative to lines */
2923 /* --------------------------------------------------------------------------------------------- */
2924 /** returns index of last char on line + 1 */
2927 edit_eol (WEdit
* edit
, long current
)
2929 if (current
>= edit
->last_byte
)
2930 return edit
->last_byte
;
2933 if (edit_get_byte (edit
, current
) == '\n')
2938 /* --------------------------------------------------------------------------------------------- */
2939 /** returns index of first char on line */
2942 edit_bol (WEdit
* edit
, long current
)
2948 if (edit_get_byte (edit
, current
- 1) == '\n')
2953 /* --------------------------------------------------------------------------------------------- */
2956 edit_count_lines (WEdit
* edit
, long current
, long upto
)
2959 if (upto
> edit
->last_byte
)
2960 upto
= edit
->last_byte
;
2963 while (current
< upto
)
2964 if (edit_get_byte (edit
, current
++) == '\n')
2969 /* --------------------------------------------------------------------------------------------- */
2970 /* If lines is zero this returns the count of lines from current to upto. */
2971 /* If upto is zero returns index of lines forward current. */
2974 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
2978 return edit_count_lines (edit
, current
, upto
);
2987 next
= edit_eol (edit
, current
) + 1;
2988 if (next
> edit
->last_byte
)
2997 /* --------------------------------------------------------------------------------------------- */
2998 /** Returns offset of 'lines' lines up from current */
3001 edit_move_backward (WEdit
* edit
, long current
, long lines
)
3005 current
= edit_bol (edit
, current
);
3006 while ((lines
--) && current
!= 0)
3007 current
= edit_bol (edit
, current
- 1);
3011 /* --------------------------------------------------------------------------------------------- */
3012 /* If cols is zero this returns the count of columns from current to upto. */
3013 /* If upto is zero returns index of cols across from current. */
3016 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
3027 q
= edit
->last_byte
+ 2;
3029 for (col
= 0, p
= current
; p
< q
; p
++)
3041 orig_c
= c
= edit_get_byte (edit
, p
);
3049 utf_ch
= edit_get_utf (edit
, p
, &cw
);
3050 if (mc_global
.utf8_display
)
3054 if (g_unichar_iswide (utf_ch
))
3057 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
3061 c
= convert_to_display_c (c
);
3065 col
+= TAB_SIZE
- col
% TAB_SIZE
;
3073 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!mc_global
.utf8_display
&& !edit
->utf8
)))
3074 /* '\r' is shown as ^M, so we must advance 2 characters */
3075 /* Caret notation for control characters */
3083 /* --------------------------------------------------------------------------------------------- */
3084 /** returns the current column position of the cursor */
3087 edit_get_col (WEdit
* edit
)
3089 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3092 /* --------------------------------------------------------------------------------------------- */
3093 /* Scrolling functions */
3094 /* --------------------------------------------------------------------------------------------- */
3097 edit_update_curs_row (WEdit
* edit
)
3099 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
3102 /* --------------------------------------------------------------------------------------------- */
3105 edit_update_curs_col (WEdit
* edit
)
3107 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3110 /* --------------------------------------------------------------------------------------------- */
3113 edit_get_curs_col (const WEdit
* edit
)
3115 return edit
->curs_col
;
3118 /* --------------------------------------------------------------------------------------------- */
3119 /** moves the display start position up by i lines */
3122 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
3124 unsigned long lines_above
= edit
->start_line
;
3125 if (i
> lines_above
)
3129 edit
->start_line
-= i
;
3130 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
3131 edit
->force
|= REDRAW_PAGE
;
3132 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3134 edit_update_curs_row (edit
);
3138 /* --------------------------------------------------------------------------------------------- */
3139 /** returns 1 if could scroll, 0 otherwise */
3142 edit_scroll_downward (WEdit
* edit
, int i
)
3145 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->widget
.lines
- 1);
3146 if (lines_below
> 0)
3148 if (i
> lines_below
)
3150 edit
->start_line
+= i
;
3151 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
3152 edit
->force
|= REDRAW_PAGE
;
3153 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3155 edit_update_curs_row (edit
);
3158 /* --------------------------------------------------------------------------------------------- */
3161 edit_scroll_right (WEdit
* edit
, int i
)
3163 edit
->force
|= REDRAW_PAGE
;
3164 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3165 edit
->start_col
-= i
;
3168 /* --------------------------------------------------------------------------------------------- */
3171 edit_scroll_left (WEdit
* edit
, int i
)
3173 if (edit
->start_col
)
3175 edit
->start_col
+= i
;
3176 if (edit
->start_col
> 0)
3177 edit
->start_col
= 0;
3178 edit
->force
|= REDRAW_PAGE
;
3179 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3183 /* --------------------------------------------------------------------------------------------- */
3184 /* high level cursor movement commands */
3185 /* --------------------------------------------------------------------------------------------- */
3188 edit_move_to_prev_col (WEdit
* edit
, long p
)
3190 int prev
= edit
->prev_col
;
3191 int over
= edit
->over_col
;
3192 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
3194 if (option_cursor_beyond_eol
)
3196 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
3197 edit_eol (edit
, edit
->curs1
));
3199 if (line_len
< prev
+ edit
->over_col
)
3201 edit
->over_col
= prev
+ over
- line_len
;
3202 edit
->prev_col
= line_len
;
3203 edit
->curs_col
= line_len
;
3207 edit
->curs_col
= prev
+ over
;
3208 edit
->prev_col
= edit
->curs_col
;
3215 if (is_in_indent (edit
) && option_fake_half_tabs
)
3217 edit_update_curs_col (edit
);
3219 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
3221 int q
= edit
->curs_col
;
3222 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
3223 p
= edit_bol (edit
, edit
->curs1
);
3224 edit_cursor_move (edit
,
3225 edit_move_forward3 (edit
, p
, edit
->curs_col
,
3227 if (!left_of_four_spaces (edit
))
3228 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3234 /* --------------------------------------------------------------------------------------------- */
3237 line_is_blank (WEdit
* edit
, long line
)
3239 return is_blank (edit
, edit_find_line (edit
, line
));
3242 /* --------------------------------------------------------------------------------------------- */
3243 /** move cursor to line 'line' */
3246 edit_move_to_line (WEdit
* e
, long line
)
3248 if (line
< e
->curs_line
)
3249 edit_move_up (e
, e
->curs_line
- line
, 0);
3251 edit_move_down (e
, line
- e
->curs_line
, 0);
3252 edit_scroll_screen_over_cursor (e
);
3255 /* --------------------------------------------------------------------------------------------- */
3256 /** scroll window so that first visible line is 'line' */
3259 edit_move_display (WEdit
* e
, long line
)
3261 if (line
< e
->start_line
)
3262 edit_scroll_upward (e
, e
->start_line
- line
);
3264 edit_scroll_downward (e
, line
- e
->start_line
);
3267 /* --------------------------------------------------------------------------------------------- */
3268 /** save markers onto undo stack */
3271 edit_push_markers (WEdit
* edit
)
3273 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3274 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3275 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3278 /* --------------------------------------------------------------------------------------------- */
3281 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
3290 /* --------------------------------------------------------------------------------------------- */
3291 /** highlight marker toggle */
3294 edit_mark_cmd (WEdit
* edit
, int unmark
)
3296 edit_push_markers (edit
);
3299 edit_set_markers (edit
, 0, 0, 0, 0);
3300 edit
->force
|= REDRAW_PAGE
;
3304 if (edit
->mark2
>= 0)
3306 edit
->end_mark_curs
= -1;
3307 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3308 edit
->curs_col
+ edit
->over_col
);
3309 edit
->force
|= REDRAW_PAGE
;
3313 edit
->end_mark_curs
= edit
->curs1
;
3314 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3315 edit
->curs_col
+ edit
->over_col
);
3320 /* --------------------------------------------------------------------------------------------- */
3321 /** highlight the word under cursor */
3324 edit_mark_current_word_cmd (WEdit
* edit
)
3328 for (pos
= edit
->curs1
; pos
!= 0; pos
--)
3332 c1
= edit_get_byte (edit
, pos
);
3333 c2
= edit_get_byte (edit
, pos
- 1);
3334 if (!isspace (c1
) && isspace (c2
))
3336 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3341 for (; pos
< edit
->last_byte
; pos
++)
3345 c1
= edit_get_byte (edit
, pos
);
3346 c2
= edit_get_byte (edit
, pos
+ 1);
3347 if (!isspace (c1
) && isspace (c2
))
3349 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3352 edit
->mark2
= min (pos
+ 1, edit
->last_byte
);
3354 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3357 /* --------------------------------------------------------------------------------------------- */
3360 edit_mark_current_line_cmd (WEdit
* edit
)
3362 long pos
= edit
->curs1
;
3364 edit
->mark1
= edit_bol (edit
, pos
);
3365 edit
->mark2
= edit_eol (edit
, pos
);
3367 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3370 /* --------------------------------------------------------------------------------------------- */
3373 edit_delete_line (WEdit
* edit
)
3376 * Delete right part of the line.
3377 * Note that edit_get_byte() returns '\n' when byte position is
3380 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3382 (void) edit_delete (edit
, 1);
3387 * Note that edit_delete() will not corrupt anything if called while
3388 * cursor position is EOF.
3390 (void) edit_delete (edit
, 1);
3393 * Delete left part of the line.
3394 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3396 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3398 (void) edit_backspace (edit
, 1);
3402 /* --------------------------------------------------------------------------------------------- */
3405 edit_indent_width (WEdit
* edit
, long p
)
3408 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
3410 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
3413 /* --------------------------------------------------------------------------------------------- */
3416 edit_insert_indent (WEdit
* edit
, int indent
)
3418 if (!option_fill_tabs_with_spaces
)
3420 while (indent
>= TAB_SIZE
)
3422 edit_insert (edit
, '\t');
3426 while (indent
-- > 0)
3427 edit_insert (edit
, ' ');
3430 /* --------------------------------------------------------------------------------------------- */
3433 edit_push_key_press (WEdit
* edit
)
3435 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3436 if (edit
->mark2
== -1)
3438 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3439 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3443 /* --------------------------------------------------------------------------------------------- */
3446 edit_find_bracket (WEdit
* edit
)
3448 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3449 if (last_bracket
!= edit
->bracket
)
3450 edit
->force
|= REDRAW_PAGE
;
3451 last_bracket
= edit
->bracket
;
3454 /* --------------------------------------------------------------------------------------------- */
3456 * This executes a command as though the user initiated it through a key
3457 * press. Callback with WIDGET_KEY as a message calls this after
3458 * translating the key press. This function can be used to pass any
3459 * command to the editor. Note that the screen wouldn't update
3460 * automatically. Either of command or char_for_insertion must be
3461 * passed as -1. Commands are executed, and char_for_insertion is
3462 * inserted at the cursor.
3466 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3468 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3470 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3473 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3476 if (macro_index
!= -1)
3478 edit
->force
|= REDRAW_COMPLETELY
;
3479 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3481 edit_store_macro_cmd (edit
);
3485 else if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3487 edit_repeat_macro_cmd (edit
);
3493 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3495 record_macro_buf
[macro_index
].action
= command
;
3496 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3498 /* record the beginning of a set of editing actions initiated by a key press */
3499 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3500 edit_push_key_press (edit
);
3502 edit_execute_cmd (edit
, command
, char_for_insertion
);
3503 if (edit
->column_highlight
)
3504 edit
->force
|= REDRAW_PAGE
;
3507 /* --------------------------------------------------------------------------------------------- */
3509 This executes a command at a lower level than macro recording.
3510 It also does not push a key_press onto the undo stack. This means
3511 that if it is called many times, a single undo command will undo
3512 all of them. It also does not check for the Undo command.
3515 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3517 /* at first, handle window state */
3518 if (edit_handle_move_resize (edit
, command
))
3521 edit
->force
|= REDRAW_LINE
;
3523 /* The next key press will unhighlight the found string, so update
3525 if (edit
->found_len
|| edit
->column_highlight
)
3526 edit
->force
|= REDRAW_PAGE
;
3530 /* a mark command with shift-arrow */
3533 case CK_MarkToWordBegin
:
3534 case CK_MarkToWordEnd
:
3540 case CK_MarkPageDown
:
3541 case CK_MarkToFileBegin
:
3542 case CK_MarkToFileEnd
:
3543 case CK_MarkToPageBegin
:
3544 case CK_MarkToPageEnd
:
3545 case CK_MarkScrollUp
:
3546 case CK_MarkScrollDown
:
3547 case CK_MarkParagraphUp
:
3548 case CK_MarkParagraphDown
:
3549 /* a mark command with alt-arrow */
3550 case CK_MarkColumnPageUp
:
3551 case CK_MarkColumnPageDown
:
3552 case CK_MarkColumnLeft
:
3553 case CK_MarkColumnRight
:
3554 case CK_MarkColumnUp
:
3555 case CK_MarkColumnDown
:
3556 case CK_MarkColumnScrollUp
:
3557 case CK_MarkColumnScrollDown
:
3558 case CK_MarkColumnParagraphUp
:
3559 case CK_MarkColumnParagraphDown
:
3560 edit
->column_highlight
= 0;
3561 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3563 edit_mark_cmd (edit
, 1); /* clear */
3564 edit_mark_cmd (edit
, 0); /* marking on */
3566 edit
->highlight
= 1;
3569 /* any other command */
3571 if (edit
->highlight
)
3572 edit_mark_cmd (edit
, 0); /* clear */
3573 edit
->highlight
= 0;
3576 /* first check for undo */
3577 if (command
== CK_Undo
)
3579 edit
->redo_stack_reset
= 0;
3580 edit_group_undo (edit
);
3581 edit
->found_len
= 0;
3582 edit
->prev_col
= edit_get_col (edit
);
3583 edit
->search_start
= edit
->curs1
;
3586 /* check for redo */
3587 if (command
== CK_Redo
)
3589 edit
->redo_stack_reset
= 0;
3590 edit_do_redo (edit
);
3591 edit
->found_len
= 0;
3592 edit
->prev_col
= edit_get_col (edit
);
3593 edit
->search_start
= edit
->curs1
;
3597 edit
->redo_stack_reset
= 1;
3599 /* An ordinary key press */
3600 if (char_for_insertion
>= 0)
3602 /* if non persistent selection and text selected */
3603 if (!option_persistent_selections
)
3605 if (edit
->mark1
!= edit
->mark2
)
3606 edit_block_delete_cmd (edit
);
3608 if (edit
->overwrite
)
3610 /* remove char only one time, after input first byte, multibyte chars */
3611 if ((!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3612 && edit_get_byte (edit
, edit
->curs1
) != '\n')
3613 edit_delete (edit
, 0);
3615 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3616 edit_insert_over (edit
);
3618 if (char_for_insertion
> 255 && !mc_global
.utf8_display
)
3620 unsigned char str
[6 + 1];
3624 res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3634 while (str
[i
] != 0 && i
<= 6)
3636 char_for_insertion
= str
[i
];
3637 edit_insert (edit
, char_for_insertion
);
3643 edit_insert (edit
, char_for_insertion
);
3645 if (option_auto_para_formatting
)
3647 format_paragraph (edit
, 0);
3648 edit
->force
|= REDRAW_PAGE
;
3651 check_and_wrap_line (edit
);
3652 edit
->found_len
= 0;
3653 edit
->prev_col
= edit_get_col (edit
);
3654 edit
->search_start
= edit
->curs1
;
3655 edit_find_bracket (edit
);
3661 case CK_TopOnScreen
:
3662 case CK_BottomOnScreen
:
3675 if (edit
->mark2
>= 0)
3677 if (!option_persistent_selections
)
3679 if (edit
->column_highlight
)
3680 edit_push_undo_action (edit
, COLUMN_ON
);
3681 edit
->column_highlight
= 0;
3682 edit_mark_cmd (edit
, 1);
3689 case CK_TopOnScreen
:
3690 case CK_BottomOnScreen
:
3691 case CK_MarkToPageBegin
:
3692 case CK_MarkToPageEnd
:
3697 case CK_MarkToWordBegin
:
3698 case CK_MarkToWordEnd
:
3701 case CK_MarkColumnUp
:
3702 case CK_MarkColumnDown
:
3703 if (edit
->mark2
== -1)
3704 break; /*marking is following the cursor: may need to highlight a whole line */
3709 edit
->force
|= REDRAW_CHAR_ONLY
;
3712 /* basic cursor key commands */
3716 /* if non persistent selection and text selected */
3717 if (!option_persistent_selections
)
3719 if (edit
->mark1
!= edit
->mark2
)
3721 edit_block_delete_cmd (edit
);
3725 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3730 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3732 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3733 edit_backspace (edit
, 1);
3738 if (option_fake_half_tabs
)
3741 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3743 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3744 edit_backspace (edit
, 1);
3749 edit_backspace (edit
, 0);
3752 /* if non persistent selection and text selected */
3753 if (!option_persistent_selections
)
3755 if (edit
->mark1
!= edit
->mark2
)
3757 edit_block_delete_cmd (edit
);
3762 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3763 edit_insert_over (edit
);
3765 if (option_fake_half_tabs
)
3768 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3770 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3771 edit_delete (edit
, 1);
3775 edit_delete (edit
, 0);
3777 case CK_DeleteToWordBegin
:
3779 edit_left_delete_word (edit
);
3781 case CK_DeleteToWordEnd
:
3782 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3783 edit_insert_over (edit
);
3785 edit_right_delete_word (edit
);
3788 edit_delete_line (edit
);
3790 case CK_DeleteToHome
:
3791 edit_delete_to_line_begin (edit
);
3793 case CK_DeleteToEnd
:
3794 edit_delete_to_line_end (edit
);
3798 if (option_auto_para_formatting
)
3800 edit_double_newline (edit
);
3801 if (option_return_does_auto_indent
)
3802 edit_auto_indent (edit
);
3803 format_paragraph (edit
, 0);
3807 edit_insert (edit
, '\n');
3808 if (option_return_does_auto_indent
)
3810 edit_auto_indent (edit
);
3815 edit_insert (edit
, '\n');
3818 case CK_MarkColumnPageUp
:
3819 edit
->column_highlight
= 1;
3822 edit_move_up (edit
, edit
->widget
.lines
- 1, 1);
3824 case CK_MarkColumnPageDown
:
3825 edit
->column_highlight
= 1;
3827 case CK_MarkPageDown
:
3828 edit_move_down (edit
, edit
->widget
.lines
- 1, 1);
3830 case CK_MarkColumnLeft
:
3831 edit
->column_highlight
= 1;
3834 if (option_fake_half_tabs
)
3836 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3838 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3841 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3842 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3846 edit_left_char_move_cmd (edit
);
3848 case CK_MarkColumnRight
:
3849 edit
->column_highlight
= 1;
3852 if (option_fake_half_tabs
)
3854 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3856 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3857 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3861 edit_right_char_move_cmd (edit
);
3863 case CK_TopOnScreen
:
3864 case CK_MarkToPageBegin
:
3865 edit_begin_page (edit
);
3867 case CK_BottomOnScreen
:
3868 case CK_MarkToPageEnd
:
3869 edit_end_page (edit
);
3872 case CK_MarkToWordBegin
:
3874 edit_left_word_move_cmd (edit
);
3877 case CK_MarkToWordEnd
:
3879 edit_right_word_move_cmd (edit
);
3881 case CK_MarkColumnUp
:
3882 edit
->column_highlight
= 1;
3885 edit_move_up (edit
, 1, 0);
3887 case CK_MarkColumnDown
:
3888 edit
->column_highlight
= 1;
3891 edit_move_down (edit
, 1, 0);
3893 case CK_MarkColumnParagraphUp
:
3894 edit
->column_highlight
= 1;
3895 case CK_ParagraphUp
:
3896 case CK_MarkParagraphUp
:
3897 edit_move_up_paragraph (edit
, 0);
3899 case CK_MarkColumnParagraphDown
:
3900 edit
->column_highlight
= 1;
3901 case CK_ParagraphDown
:
3902 case CK_MarkParagraphDown
:
3903 edit_move_down_paragraph (edit
, 0);
3905 case CK_MarkColumnScrollUp
:
3906 edit
->column_highlight
= 1;
3908 case CK_MarkScrollUp
:
3909 edit_move_up (edit
, 1, 1);
3911 case CK_MarkColumnScrollDown
:
3912 edit
->column_highlight
= 1;
3914 case CK_MarkScrollDown
:
3915 edit_move_down (edit
, 1, 1);
3919 edit_cursor_to_bol (edit
);
3923 edit_cursor_to_eol (edit
);
3926 /* if text marked shift block */
3927 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3929 if (edit
->mark2
< 0)
3930 edit_mark_cmd (edit
, 0);
3931 edit_move_block_to_right (edit
);
3935 if (option_cursor_beyond_eol
)
3936 edit_insert_over (edit
);
3937 edit_tab_cmd (edit
);
3938 if (option_auto_para_formatting
)
3940 format_paragraph (edit
, 0);
3941 edit
->force
|= REDRAW_PAGE
;
3945 check_and_wrap_line (edit
);
3950 case CK_InsertOverwrite
:
3951 edit
->overwrite
= !edit
->overwrite
;
3955 if (edit
->mark2
>= 0)
3957 if (edit
->column_highlight
)
3958 edit_push_undo_action (edit
, COLUMN_ON
);
3959 edit
->column_highlight
= 0;
3961 edit_mark_cmd (edit
, 0);
3964 if (!edit
->column_highlight
)
3965 edit_push_undo_action (edit
, COLUMN_OFF
);
3966 edit
->column_highlight
= 1;
3967 edit_mark_cmd (edit
, 0);
3970 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3971 edit
->force
|= REDRAW_PAGE
;
3974 if (edit
->column_highlight
)
3975 edit_push_undo_action (edit
, COLUMN_ON
);
3976 edit
->column_highlight
= 0;
3977 edit_mark_cmd (edit
, 1);
3980 if (edit
->column_highlight
)
3981 edit_push_undo_action (edit
, COLUMN_ON
);
3982 edit
->column_highlight
= 0;
3983 edit_mark_current_word_cmd (edit
);
3986 if (edit
->column_highlight
)
3987 edit_push_undo_action (edit
, COLUMN_ON
);
3988 edit
->column_highlight
= 0;
3989 edit_mark_current_line_cmd (edit
);
3992 case CK_ShowNumbers
:
3993 option_line_state
= !option_line_state
;
3994 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
3995 edit
->force
|= REDRAW_PAGE
;
3999 show_right_margin
= !show_right_margin
;
4000 edit
->force
|= REDRAW_PAGE
;
4004 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
4005 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
4006 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4008 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4010 case CK_BookmarkFlush
:
4011 book_mark_flush (edit
, BOOK_MARK_COLOR
);
4012 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
4013 edit
->force
|= REDRAW_PAGE
;
4015 case CK_BookmarkNext
:
4016 if (edit
->book_mark
)
4018 struct _book_mark
*p
;
4019 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4023 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4024 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4025 edit_move_to_line (edit
, p
->line
);
4029 case CK_BookmarkPrev
:
4030 if (edit
->book_mark
)
4032 struct _book_mark
*p
;
4033 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4034 while (p
->line
== edit
->curs_line
)
4039 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4040 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4041 edit_move_to_line (edit
, p
->line
);
4047 case CK_MarkToFileBegin
:
4048 edit_move_to_top (edit
);
4051 case CK_MarkToFileEnd
:
4052 edit_move_to_bottom (edit
);
4056 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4057 edit_insert_over (edit
);
4058 edit_block_copy_cmd (edit
);
4061 edit_block_delete_cmd (edit
);
4064 edit_block_move_cmd (edit
);
4067 case CK_BlockShiftLeft
:
4068 if (edit
->mark1
!= edit
->mark2
)
4069 edit_move_block_to_left (edit
);
4071 case CK_BlockShiftRight
:
4072 if (edit
->mark1
!= edit
->mark2
)
4073 edit_move_block_to_right (edit
);
4076 edit_copy_to_X_buf_cmd (edit
);
4079 edit_cut_to_X_buf_cmd (edit
);
4082 /* if non persistent selection and text selected */
4083 if (!option_persistent_selections
)
4085 if (edit
->mark1
!= edit
->mark2
)
4086 edit_block_delete_cmd (edit
);
4088 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4089 edit_insert_over (edit
);
4090 edit_paste_from_X_buf_cmd (edit
);
4093 edit_paste_from_history (edit
);
4097 edit_save_as_cmd (edit
);
4100 edit_save_confirm_cmd (edit
);
4103 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
4106 edit_save_block_cmd (edit
);
4109 edit_insert_file_cmd (edit
);
4113 edit_load_back_cmd (edit
);
4116 edit_load_forward_cmd (edit
);
4119 case CK_EditSyntaxFile
:
4120 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
4122 case CK_SyntaxChoose
:
4123 edit_syntax_dialog (edit
);
4126 case CK_EditUserMenu
:
4127 edit_load_cmd (edit
, EDIT_FILE_MENU
);
4130 case CK_SyntaxOnOff
:
4131 option_syntax_highlighting
^= 1;
4132 if (option_syntax_highlighting
== 1)
4133 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
4134 edit
->force
|= REDRAW_PAGE
;
4138 enable_show_tabs_tws
^= 1;
4139 edit
->force
|= REDRAW_PAGE
;
4143 edit_search_cmd (edit
, FALSE
);
4145 case CK_SearchContinue
:
4146 edit_search_cmd (edit
, TRUE
);
4149 edit_replace_cmd (edit
, 0);
4151 case CK_ReplaceContinue
:
4152 edit_replace_cmd (edit
, 1);
4155 /* if text marked shift block */
4156 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
4158 edit_move_block_to_left (edit
);
4162 edit_complete_word_cmd (edit
);
4166 edit_get_match_keyword_cmd (edit
);
4169 edit_new_cmd (edit
);
4172 edit_help_cmd (edit
);
4175 edit_refresh_cmd (edit
);
4187 edit_options_dialog (edit
);
4189 case CK_OptionsSaveMode
:
4190 menu_save_mode_cmd ();
4195 /* fool gcc to prevent a Y2K warning */
4196 char time_format
[] = "_c";
4197 time_format
[0] = '%';
4199 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4200 edit_print_string (edit
, s
);
4201 edit
->force
|= REDRAW_PAGE
;
4206 edit_goto_cmd (edit
);
4208 case CK_ParagraphFormat
:
4209 format_paragraph (edit
, 1);
4210 edit
->force
|= REDRAW_PAGE
;
4212 case CK_MacroDelete
:
4213 edit_delete_macro_cmd (edit
);
4215 case CK_MatchBracket
:
4216 edit_goto_matching_bracket (edit
);
4219 user_menu (edit
, NULL
, -1);
4222 edit_sort_cmd (edit
);
4224 case CK_ExternalCommand
:
4225 edit_ext_cmd (edit
);
4228 edit_mail_dialog (edit
);
4234 case CK_SelectCodepage
:
4235 edit_select_codepage_cmd (edit
);
4238 case CK_InsertLiteral
:
4239 edit_insert_literal_cmd (edit
);
4241 case CK_MacroStartStopRecord
:
4242 edit_begin_end_macro_cmd (edit
);
4244 case CK_RepeatStartStopRecord
:
4245 edit_begin_end_repeat_cmd (edit
);
4247 case CK_ExtendedKeyMap
:
4248 edit
->extmod
= TRUE
;
4255 if ((command
/ CK_PipeBlock (0)) == 1)
4256 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
4258 /* keys which must set the col position, and the search vars */
4262 case CK_SearchContinue
:
4264 case CK_ReplaceContinue
:
4266 edit
->prev_col
= edit_get_col (edit
);
4270 case CK_MarkColumnUp
:
4273 case CK_MarkColumnDown
:
4276 case CK_MarkColumnPageUp
:
4278 case CK_MarkPageDown
:
4279 case CK_MarkColumnPageDown
:
4281 case CK_MarkToFileBegin
:
4283 case CK_MarkToFileEnd
:
4284 case CK_ParagraphUp
:
4285 case CK_MarkParagraphUp
:
4286 case CK_MarkColumnParagraphUp
:
4287 case CK_ParagraphDown
:
4288 case CK_MarkParagraphDown
:
4289 case CK_MarkColumnParagraphDown
:
4291 case CK_MarkScrollUp
:
4292 case CK_MarkColumnScrollUp
:
4294 case CK_MarkScrollDown
:
4295 case CK_MarkColumnScrollDown
:
4296 edit
->search_start
= edit
->curs1
;
4297 edit
->found_len
= 0;
4300 edit
->found_len
= 0;
4301 edit
->prev_col
= edit_get_col (edit
);
4302 edit
->search_start
= edit
->curs1
;
4304 edit_find_bracket (edit
);
4306 if (option_auto_para_formatting
)
4312 case CK_DeleteToWordBegin
:
4313 case CK_DeleteToWordEnd
:
4314 case CK_DeleteToHome
:
4315 case CK_DeleteToEnd
:
4316 format_paragraph (edit
, 0);
4317 edit
->force
|= REDRAW_PAGE
;
4322 /* --------------------------------------------------------------------------------------------- */
4325 edit_stack_init (void)
4327 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4329 edit_history_moveto
[edit_stack_iterator
].filename_vpath
= NULL
;
4330 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4333 edit_stack_iterator
= 0;
4336 /* --------------------------------------------------------------------------------------------- */
4339 edit_stack_free (void)
4341 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4342 vfs_path_free (edit_history_moveto
[edit_stack_iterator
].filename_vpath
);
4345 /* --------------------------------------------------------------------------------------------- */
4349 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
4351 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4354 /* --------------------------------------------------------------------------------------------- */
4358 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
4360 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4363 /* --------------------------------------------------------------------------------------------- */