1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 Authors: Paul Sheer 1996, 1997
7 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 * \brief Source: editor low level data handling and cursor fundamentals
34 #include <sys/types.h>
43 #include "lib/global.h"
45 #include "lib/tty/color.h"
46 #include "lib/tty/tty.h" /* attrset() */
47 #include "lib/tty/key.h" /* is_idle() */
48 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
49 #include "lib/vfs/vfs.h"
50 #include "lib/strutil.h" /* utf string functions */
51 #include "lib/util.h" /* load_file_position(), save_file_position() */
52 #include "lib/timefmt.h" /* time formatting */
54 #include "lib/widget.h"
57 #include "lib/charsets.h" /* get_codepage_id */
60 #include "src/filemanager/cmd.h" /* view_other_cmd() */
61 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
63 #include "src/setup.h" /* option_tab_spacing */
64 #include "src/learn.h" /* learn_keys */
65 #include "src/keybind-defaults.h"
67 #include "edit-impl.h"
68 #include "edit-widget.h"
70 /*** global variables ****************************************************************************/
72 int option_word_wrap_line_length
= DEFAULT_WRAP_LINE_LENGTH
;
73 int option_typewriter_wrap
= 0;
74 int option_auto_para_formatting
= 0;
75 int option_fill_tabs_with_spaces
= 0;
76 int option_return_does_auto_indent
= 1;
77 int option_backspace_through_tabs
= 0;
78 int option_fake_half_tabs
= 1;
79 int option_save_mode
= EDIT_QUICK_SAVE
;
80 int option_save_position
= 1;
81 int option_max_undo
= 32768;
82 int option_persistent_selections
= 1;
83 int option_cursor_beyond_eol
= 0;
84 int option_line_state
= 0;
85 int option_line_state_width
= 0;
87 int option_edit_right_extreme
= 0;
88 int option_edit_left_extreme
= 0;
89 int option_edit_top_extreme
= 0;
90 int option_edit_bottom_extreme
= 0;
91 int enable_show_tabs_tws
= 1;
92 int option_check_nl_at_eof
= 0;
93 int option_group_undo
= 0;
94 int show_right_margin
= 0;
96 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
97 char *option_backup_ext
= NULL
;
99 int edit_stack_iterator
= 0;
100 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
101 /* magic sequense for say than block is vertical */
102 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
104 /*** file scope macro definitions ****************************************************************/
106 #define TEMP_BUF_LEN 1024
108 #define space_width 1
110 /*** file scope type declarations ****************************************************************/
112 /*** file scope variables ************************************************************************/
114 /* detecting an error on save is easy: just check if every byte has been written. */
115 /* detecting an error on read, is not so easy 'cos there is not way to tell
116 whether you read everything or not. */
117 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
118 static const struct edit_filters
120 const char *read
, *write
, *extension
;
124 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
125 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
126 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
127 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
128 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
132 static long last_bracket
= -1;
134 /*** file scope functions ************************************************************************/
135 /* --------------------------------------------------------------------------------------------- */
139 * here's a quick sketch of the layout: (don't run this through indent.)
141 * (b1 is buffers1 and b2 is buffers2)
144 * \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
145 * ______________________________________|______________________________________
147 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
148 * |-> |-> |-> |-> |-> |-> |
150 * _<------------------------->|<----------------->_
151 * WEdit->curs2 | WEdit->curs1
156 * file end|||file beginning
165 /* --------------------------------------------------------------------------------------------- */
167 static int left_of_four_spaces (WEdit
* edit
);
169 /* --------------------------------------------------------------------------------------------- */
174 const char *header
= N_("About");
175 const char *button_name
= N_("&OK");
176 const char *const version
= "MCEdit " VERSION
;
177 char text
[BUF_LARGE
];
179 int win_len
, version_len
, button_len
;
186 button_name
= _(button_name
);
189 button_len
= str_term_width1 (button_name
) + 5;
190 version_len
= str_term_width1 (version
);
192 g_snprintf (text
, sizeof (text
),
193 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
194 " A user friendly text editor\n"
195 " written for the Midnight Commander"));
197 win_len
= str_term_width1 (header
);
198 win_len
= max (win_len
, version_len
);
199 win_len
= max (win_len
, button_len
);
201 /* count width and height of text */
202 str_msg_term_size (text
, &lines
, &cols
);
204 cols
= max (win_len
, cols
) + 6;
207 about_dlg
= create_dlg (TRUE
, 0, 0, lines
, cols
, dialog_colors
, NULL
,
208 "[Internal File Editor]", header
, DLG_CENTER
| DLG_TRYUP
);
210 add_widget (about_dlg
, label_new (3, (cols
- version_len
) / 2, version
));
211 add_widget (about_dlg
, label_new (5, 3, text
));
212 add_widget (about_dlg
, button_new (lines
- 3, (cols
- button_len
) / 2,
213 B_ENTER
, NORMAL_BUTTON
, button_name
, NULL
));
216 destroy_dlg (about_dlg
);
219 /* --------------------------------------------------------------------------------------------- */
221 * Initialize the buffers for an empty files.
225 edit_init_buffers (WEdit
* edit
)
229 for (j
= 0; j
<= MAXBUFF
; j
++)
231 edit
->buffers1
[j
] = NULL
;
232 edit
->buffers2
[j
] = NULL
;
237 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
240 /* --------------------------------------------------------------------------------------------- */
242 * Load file OR text into buffers. Set cursor to the beginning of file.
243 * @returns 1 on error.
247 edit_load_file_fast (WEdit
* edit
, const char *filename
)
253 edit
->curs2
= edit
->last_byte
;
254 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
256 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
261 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
262 edit_error_dialog (_("Error"), errmsg
);
267 if (!edit
->buffers2
[buf2
])
268 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
273 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
274 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
277 for (buf
= buf2
- 1; buf
>= 0; buf
--)
279 /* edit->buffers2[0] is already allocated */
280 if (!edit
->buffers2
[buf
])
281 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
282 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
290 char *err_str
= g_strdup_printf (_("Error reading %s"), filename
);
291 edit_error_dialog (_("Error"), err_str
);
298 /* --------------------------------------------------------------------------------------------- */
299 /** Return index of the filter or -1 is there is no appropriate filter */
302 edit_find_filter (const char *filename
)
306 if (filename
== NULL
)
309 l
= strlen (filename
);
310 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
312 e
= strlen (all_filters
[i
].extension
);
314 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
320 /* --------------------------------------------------------------------------------------------- */
323 edit_get_filter (const char *filename
)
326 char *p
, *quoted_name
;
328 i
= edit_find_filter (filename
);
332 quoted_name
= name_quote (filename
, 0);
333 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
334 g_free (quoted_name
);
338 /* --------------------------------------------------------------------------------------------- */
341 edit_insert_stream (WEdit
* edit
, FILE * f
)
345 while ((c
= fgetc (f
)) >= 0)
347 edit_insert (edit
, c
);
353 /* --------------------------------------------------------------------------------------------- */
354 /** Open file and create it if necessary. Return 0 for success, 1 for error. */
357 check_file_access (WEdit
* edit
, const char *filename
, struct stat
*st
)
360 gchar
*errmsg
= NULL
;
362 /* Try opening an existing file */
363 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
368 * Try creating the file. O_EXCL prevents following broken links
369 * and opening existing files.
371 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
374 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
379 /* New file, delete it if it's not modified or saved */
380 edit
->delete_file
= 1;
384 /* Check what we have opened */
385 if (mc_fstat (file
, st
) < 0)
387 errmsg
= g_strdup_printf (_("Cannot get size/permissions for %s"), filename
);
391 /* We want to open regular files only */
392 if (!S_ISREG (st
->st_mode
))
394 errmsg
= g_strdup_printf (_("\"%s\" is not a regular file"), filename
);
399 * Don't delete non-empty files.
400 * O_EXCL should prevent it, but let's be on the safe side.
403 edit
->delete_file
= 0;
405 if (st
->st_size
>= SIZE_LIMIT
)
406 errmsg
= g_strdup_printf (_("File \"%s\" is too large"), filename
);
409 (void) mc_close (file
);
413 edit_error_dialog (_("Error"), errmsg
);
420 /* --------------------------------------------------------------------------------------------- */
422 * Open the file and load it into the buffers, either directly or using
423 * a filter. Return 0 on success, 1 on error.
425 * Fast loading (edit_load_file_fast) is used when the file size is
426 * known. In this case the data is read into the buffers by blocks.
427 * If the file size is not known, the data is loaded byte by byte in
432 edit_load_file (WEdit
* edit
)
435 vfs_path_t
*vpath
= vfs_path_from_str (edit
->filename
);
437 /* Cannot do fast load if a filter is used */
438 if (edit_find_filter (edit
->filename
) >= 0)
442 * VFS may report file size incorrectly, and slow load is not a big
443 * deal considering overhead in VFS.
445 if (!vfs_file_is_local (vpath
))
447 vfs_path_free (vpath
);
450 * FIXME: line end translation should disable fast loading as well
451 * Consider doing fseek() to the end and ftell() for the real size.
456 /* If we are dealing with a real file, check that it exists */
457 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
462 /* nothing to load */
466 edit_init_buffers (edit
);
470 edit
->last_byte
= edit
->stat1
.st_size
;
471 edit_load_file_fast (edit
, edit
->filename
);
472 /* If fast load was used, the number of lines wasn't calculated */
473 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
480 edit
->undo_stack_disable
= 1;
481 if (edit_insert_file (edit
, edit
->filename
) == 0)
486 edit
->undo_stack_disable
= 0;
493 /* --------------------------------------------------------------------------------------------- */
494 /** Restore saved cursor position in the file */
497 edit_load_position (WEdit
* edit
)
504 if (!edit
->filename
|| !*edit
->filename
)
507 vpath
= vfs_path_from_str (edit
->filename
);
508 filename
= vfs_path_to_str (vpath
);
509 load_file_position (filename
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
510 vfs_path_free (vpath
);
515 edit_move_to_line (edit
, line
- 1);
516 edit
->prev_col
= column
;
520 edit_cursor_move (edit
, offset
);
521 line
= edit
->curs_line
;
522 edit
->search_start
= edit
->curs1
;
525 book_mark_restore (edit
, BOOK_MARK_COLOR
);
527 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
528 edit_move_display (edit
, line
- (edit
->widget
.lines
/ 2));
531 /* --------------------------------------------------------------------------------------------- */
532 /** Save cursor position in the file */
535 edit_save_position (WEdit
* edit
)
540 if (edit
->filename
== NULL
|| *edit
->filename
== '\0')
543 vpath
= vfs_path_from_str (edit
->filename
);
544 filename
= vfs_path_to_str (vpath
);
546 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
547 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
,
548 edit
->serialized_bookmarks
);
549 edit
->serialized_bookmarks
= NULL
;
552 vfs_path_free (vpath
);
555 /* --------------------------------------------------------------------------------------------- */
556 /** Clean the WEdit stricture except the widget part */
559 edit_purge_widget (WEdit
* edit
)
561 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
562 char *start
= (char *) edit
+ sizeof (Widget
);
563 memset (start
, 0, len
);
566 /* --------------------------------------------------------------------------------------------- */
569 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
570 then the file should be as it was when he loaded up. Then set edit->modified to 0.
574 edit_pop_undo_action (WEdit
* edit
)
577 unsigned long sp
= edit
->undo_stack_pointer
;
579 if (sp
== edit
->undo_stack_bottom
)
582 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
583 c
= edit
->undo_stack
[sp
];
586 /* edit->undo_stack[sp] = '@'; */
587 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
591 if (sp
== edit
->undo_stack_bottom
)
594 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
595 if (edit
->undo_stack
[sp
] == -2)
597 /* edit->undo_stack[sp] = '@'; */
598 edit
->undo_stack_pointer
= sp
;
601 edit
->undo_stack
[sp
]++;
607 edit_pop_redo_action (WEdit
* edit
)
610 unsigned long sp
= edit
->redo_stack_pointer
;
612 if (sp
== edit
->redo_stack_bottom
)
615 sp
= (sp
- 1) & edit
->redo_stack_size_mask
;
616 c
= edit
->redo_stack
[sp
];
619 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
623 if (sp
== edit
->redo_stack_bottom
)
626 c
= edit
->redo_stack
[(sp
- 1) & edit
->redo_stack_size_mask
];
627 if (edit
->redo_stack
[sp
] == -2)
628 edit
->redo_stack_pointer
= sp
;
630 edit
->redo_stack
[sp
]++;
636 get_prev_undo_action (WEdit
* edit
)
639 unsigned long sp
= edit
->undo_stack_pointer
;
641 if (sp
== edit
->undo_stack_bottom
)
644 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
645 c
= edit
->undo_stack
[sp
];
649 if (sp
== edit
->undo_stack_bottom
)
652 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
656 /* --------------------------------------------------------------------------------------------- */
657 /** is called whenever a modification is made by one of the four routines below */
660 edit_modification (WEdit
* edit
)
662 edit
->caches_valid
= 0;
664 /* raise lock when file modified */
665 if (!edit
->modified
&& !edit
->delete_file
)
666 edit
->locked
= edit_lock_file (edit
);
670 /* --------------------------------------------------------------------------------------------- */
673 edit_backspace (WEdit
* edit
, const int byte_delete
)
684 if (edit
->mark2
!= edit
->mark1
)
685 edit_push_markers (edit
);
687 if (edit
->utf8
&& byte_delete
== 0)
689 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
693 for (i
= 1; i
<= cw
; i
++)
695 if (edit
->mark1
>= edit
->curs1
)
698 edit
->end_mark_curs
--;
700 if (edit
->mark2
>= edit
->curs1
)
702 if (edit
->last_get_rule
>= edit
->curs1
)
703 edit
->last_get_rule
--;
705 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
706 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
707 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
709 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
710 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
714 edit_push_undo_action (edit
, p
);
716 edit_modification (edit
);
720 book_mark_dec (edit
, edit
->curs_line
);
723 edit
->force
|= REDRAW_AFTER_CURSOR
;
726 if (edit
->curs1
< edit
->start_display
)
728 edit
->start_display
--;
736 /* --------------------------------------------------------------------------------------------- */
737 /* high level cursor movement commands */
738 /* --------------------------------------------------------------------------------------------- */
741 is_in_indent (WEdit
* edit
)
743 long p
= edit_bol (edit
, edit
->curs1
);
744 while (p
< edit
->curs1
)
745 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
750 /* --------------------------------------------------------------------------------------------- */
753 is_blank (WEdit
* edit
, long offset
)
757 s
= edit_bol (edit
, offset
);
758 f
= edit_eol (edit
, offset
) - 1;
761 c
= edit_get_byte (edit
, s
++);
769 /* --------------------------------------------------------------------------------------------- */
770 /** returns the offset of line i */
773 edit_find_line (WEdit
* edit
, int line
)
777 if (!edit
->caches_valid
)
779 for (i
= 0; i
< N_LINE_CACHES
; i
++)
780 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
781 /* three offsets that we *know* are line 0 at 0 and these two: */
782 edit
->line_numbers
[1] = edit
->curs_line
;
783 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
784 edit
->line_numbers
[2] = edit
->total_lines
;
785 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
786 edit
->caches_valid
= 1;
788 if (line
>= edit
->total_lines
)
789 return edit
->line_offsets
[2];
792 /* find the closest known point */
793 for (i
= 0; i
< N_LINE_CACHES
; i
++)
796 n
= abs (edit
->line_numbers
[i
] - line
);
804 return edit
->line_offsets
[j
]; /* know the offset exactly */
805 if (m
== 1 && j
>= 3)
806 i
= j
; /* one line different - caller might be looping, so stay in this cache */
808 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
809 if (line
> edit
->line_numbers
[j
])
810 edit
->line_offsets
[i
] =
811 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
813 edit
->line_offsets
[i
] =
814 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
815 edit
->line_numbers
[i
] = line
;
816 return edit
->line_offsets
[i
];
819 /* --------------------------------------------------------------------------------------------- */
820 /** moves up until a blank line is reached, or until just
821 before a non-blank line is reached */
824 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
827 if (edit
->curs_line
> 1)
829 if (line_is_blank (edit
, edit
->curs_line
))
831 if (line_is_blank (edit
, edit
->curs_line
- 1))
833 for (i
= edit
->curs_line
- 1; i
; i
--)
834 if (!line_is_blank (edit
, i
))
842 for (i
= edit
->curs_line
- 1; i
; i
--)
843 if (line_is_blank (edit
, i
))
849 for (i
= edit
->curs_line
- 1; i
; i
--)
850 if (line_is_blank (edit
, i
))
854 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
857 /* --------------------------------------------------------------------------------------------- */
858 /** moves down until a blank line is reached, or until just
859 before a non-blank line is reached */
862 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
865 if (edit
->curs_line
>= edit
->total_lines
- 1)
867 i
= edit
->total_lines
;
871 if (line_is_blank (edit
, edit
->curs_line
))
873 if (line_is_blank (edit
, edit
->curs_line
+ 1))
875 for (i
= edit
->curs_line
+ 1; i
; i
++)
876 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
884 for (i
= edit
->curs_line
+ 1; i
; i
++)
885 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
891 for (i
= edit
->curs_line
+ 1; i
; i
++)
892 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
896 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
899 /* --------------------------------------------------------------------------------------------- */
902 edit_begin_page (WEdit
* edit
)
904 edit_update_curs_row (edit
);
905 edit_move_up (edit
, edit
->curs_row
, 0);
908 /* --------------------------------------------------------------------------------------------- */
911 edit_end_page (WEdit
* edit
)
913 edit_update_curs_row (edit
);
914 edit_move_down (edit
, edit
->widget
.lines
- edit
->curs_row
- 1, 0);
918 /* --------------------------------------------------------------------------------------------- */
919 /** goto beginning of text */
922 edit_move_to_top (WEdit
* edit
)
926 edit_cursor_move (edit
, -edit
->curs1
);
927 edit_move_to_prev_col (edit
, 0);
928 edit
->force
|= REDRAW_PAGE
;
929 edit
->search_start
= 0;
930 edit_update_curs_row (edit
);
935 /* --------------------------------------------------------------------------------------------- */
936 /** goto end of text */
939 edit_move_to_bottom (WEdit
* edit
)
941 if (edit
->curs_line
< edit
->total_lines
)
943 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
944 edit
->start_display
= edit
->last_byte
;
945 edit
->start_line
= edit
->total_lines
;
946 edit_scroll_upward (edit
, edit
->widget
.lines
- 1);
947 edit
->force
|= REDRAW_PAGE
;
951 /* --------------------------------------------------------------------------------------------- */
952 /** goto beginning of line */
955 edit_cursor_to_bol (WEdit
* edit
)
957 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
958 edit
->search_start
= edit
->curs1
;
959 edit
->prev_col
= edit_get_col (edit
);
963 /* --------------------------------------------------------------------------------------------- */
964 /** goto end of line */
967 edit_cursor_to_eol (WEdit
* edit
)
969 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
970 edit
->search_start
= edit
->curs1
;
971 edit
->prev_col
= edit_get_col (edit
);
975 /* --------------------------------------------------------------------------------------------- */
982 const char option_chars_move_whole_word
[] =
983 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
989 if (*option_chars_move_whole_word
== '!')
993 if (g_ascii_isupper ((gchar
) c
))
995 else if (g_ascii_islower ((gchar
) c
))
997 else if (g_ascii_isalpha (c
))
999 else if (isdigit (c
))
1001 else if (isspace (c
))
1003 q
= strchr (option_chars_move_whole_word
, c
);
1005 return 0xFFFFFFFFUL
;
1008 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1013 while ((q
= strchr (q
+ 1, c
)));
1017 /* --------------------------------------------------------------------------------------------- */
1020 edit_left_word_move (WEdit
* edit
, int s
)
1025 if (edit
->column_highlight
1026 && edit
->mark1
!= edit
->mark2
1027 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1029 edit_cursor_move (edit
, -1);
1032 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1033 c2
= edit_get_byte (edit
, edit
->curs1
);
1034 if (c1
== '\n' || c2
== '\n')
1036 if (!(my_type_of (c1
) & my_type_of (c2
)))
1038 if (isspace (c1
) && !isspace (c2
))
1041 if (!isspace (c1
) && isspace (c2
))
1046 /* --------------------------------------------------------------------------------------------- */
1049 edit_left_word_move_cmd (WEdit
* edit
)
1051 edit_left_word_move (edit
, 0);
1052 edit
->force
|= REDRAW_PAGE
;
1055 /* --------------------------------------------------------------------------------------------- */
1058 edit_right_word_move (WEdit
* edit
, int s
)
1063 if (edit
->column_highlight
1064 && edit
->mark1
!= edit
->mark2
1065 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
1067 edit_cursor_move (edit
, 1);
1068 if (edit
->curs1
>= edit
->last_byte
)
1070 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1071 c2
= edit_get_byte (edit
, edit
->curs1
);
1072 if (c1
== '\n' || c2
== '\n')
1074 if (!(my_type_of (c1
) & my_type_of (c2
)))
1076 if (isspace (c1
) && !isspace (c2
))
1079 if (!isspace (c1
) && isspace (c2
))
1084 /* --------------------------------------------------------------------------------------------- */
1087 edit_right_word_move_cmd (WEdit
* edit
)
1089 edit_right_word_move (edit
, 0);
1090 edit
->force
|= REDRAW_PAGE
;
1093 /* --------------------------------------------------------------------------------------------- */
1096 edit_right_char_move_cmd (WEdit
* edit
)
1102 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
1108 c
= edit_get_byte (edit
, edit
->curs1
);
1110 if (option_cursor_beyond_eol
&& c
== '\n')
1116 edit_cursor_move (edit
, cw
);
1120 /* --------------------------------------------------------------------------------------------- */
1123 edit_left_char_move_cmd (WEdit
* edit
)
1126 if (edit
->column_highlight
1127 && option_cursor_beyond_eol
1128 && edit
->mark1
!= edit
->mark2
1129 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1133 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1137 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1143 edit_cursor_move (edit
, -cw
);
1147 /* --------------------------------------------------------------------------------------------- */
1148 /** Up or down cursor moving.
1149 direction = TRUE - move up
1154 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
1157 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
1166 edit
->force
|= REDRAW_PAGE
;
1170 edit_scroll_upward (edit
, i
);
1172 edit_scroll_downward (edit
, i
);
1174 p
= edit_bol (edit
, edit
->curs1
);
1176 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
1178 edit_cursor_move (edit
, p
- edit
->curs1
);
1180 edit_move_to_prev_col (edit
, p
);
1182 /* search start of current multibyte char (like CJK) */
1183 if (edit
->curs1
+ 1 < edit
->last_byte
)
1185 edit_right_char_move_cmd (edit
);
1186 edit_left_char_move_cmd (edit
);
1189 edit
->search_start
= edit
->curs1
;
1190 edit
->found_len
= 0;
1193 /* --------------------------------------------------------------------------------------------- */
1196 edit_right_delete_word (WEdit
* edit
)
1201 if (edit
->curs1
>= edit
->last_byte
)
1203 c1
= edit_delete (edit
, 1);
1204 c2
= edit_get_byte (edit
, edit
->curs1
);
1205 if (c1
== '\n' || c2
== '\n')
1207 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1209 if (!(my_type_of (c1
) & my_type_of (c2
)))
1214 /* --------------------------------------------------------------------------------------------- */
1217 edit_left_delete_word (WEdit
* edit
)
1222 if (edit
->curs1
<= 0)
1224 c1
= edit_backspace (edit
, 1);
1225 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1226 if (c1
== '\n' || c2
== '\n')
1228 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1230 if (!(my_type_of (c1
) & my_type_of (c2
)))
1235 /* --------------------------------------------------------------------------------------------- */
1237 the start column position is not recorded, and hence does not
1238 undo as it happed. But who would notice.
1242 edit_do_undo (WEdit
* edit
)
1247 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1249 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1256 edit_cursor_move (edit
, 1);
1259 edit_cursor_move (edit
, -1);
1263 edit_backspace (edit
, 1);
1267 edit_delete (edit
, 1);
1270 edit
->column_highlight
= 1;
1273 edit
->column_highlight
= 0;
1276 if (ac
>= 256 && ac
< 512)
1277 edit_insert_ahead (edit
, ac
- 256);
1278 if (ac
>= 0 && ac
< 256)
1279 edit_insert (edit
, ac
);
1281 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1283 edit
->mark1
= ac
- MARK_1
;
1284 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1286 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1288 edit
->mark2
= ac
- MARK_2
;
1289 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1291 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1293 edit
->end_mark_curs
= ac
- MARK_CURS
;
1296 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1299 if (edit
->start_display
> ac
- KEY_PRESS
)
1301 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1302 edit
->force
|= REDRAW_PAGE
;
1304 else if (edit
->start_display
< ac
- KEY_PRESS
)
1306 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1307 edit
->force
|= REDRAW_PAGE
;
1309 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1310 edit_update_curs_row (edit
);
1313 edit
->undo_stack_disable
= 0;
1317 edit_do_redo (WEdit
* edit
)
1322 if (edit
->redo_stack_reset
)
1326 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1333 edit_cursor_move (edit
, 1);
1336 edit_cursor_move (edit
, -1);
1339 edit_backspace (edit
, 1);
1342 edit_delete (edit
, 1);
1345 edit
->column_highlight
= 1;
1348 edit
->column_highlight
= 0;
1351 if (ac
>= 256 && ac
< 512)
1352 edit_insert_ahead (edit
, ac
- 256);
1353 if (ac
>= 0 && ac
< 256)
1354 edit_insert (edit
, ac
);
1356 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1358 edit
->mark1
= ac
- MARK_1
;
1359 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1361 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1363 edit
->mark2
= ac
- MARK_2
;
1364 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1366 /* more than one pop usually means something big */
1368 edit
->force
|= REDRAW_PAGE
;
1371 if (edit
->start_display
> ac
- KEY_PRESS
)
1373 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1374 edit
->force
|= REDRAW_PAGE
;
1376 else if (edit
->start_display
< ac
- KEY_PRESS
)
1378 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1379 edit
->force
|= REDRAW_PAGE
;
1381 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1382 edit_update_curs_row (edit
);
1388 edit_group_undo (WEdit
* edit
)
1390 long ac
= KEY_PRESS
;
1391 long cur_ac
= KEY_PRESS
;
1392 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1394 cur_ac
= get_prev_undo_action (edit
);
1395 edit_do_undo (edit
);
1396 ac
= get_prev_undo_action (edit
);
1397 /* exit from cycle if option_group_undo is not set,
1398 * and make single UNDO operation
1400 if (!option_group_undo
)
1405 /* --------------------------------------------------------------------------------------------- */
1408 edit_delete_to_line_end (WEdit
* edit
)
1410 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
1414 edit_delete (edit
, 1);
1418 /* --------------------------------------------------------------------------------------------- */
1421 edit_delete_to_line_begin (WEdit
* edit
)
1423 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
1427 edit_backspace (edit
, 1);
1431 /* --------------------------------------------------------------------------------------------- */
1434 is_aligned_on_a_tab (WEdit
* edit
)
1436 edit_update_curs_col (edit
);
1437 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
1438 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
1441 /* --------------------------------------------------------------------------------------------- */
1444 right_of_four_spaces (WEdit
* edit
)
1447 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1448 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1450 return is_aligned_on_a_tab (edit
);
1454 /* --------------------------------------------------------------------------------------------- */
1457 left_of_four_spaces (WEdit
* edit
)
1460 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1461 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1463 return is_aligned_on_a_tab (edit
);
1467 /* --------------------------------------------------------------------------------------------- */
1470 edit_auto_indent (WEdit
* edit
)
1475 /* use the previous line as a template */
1476 p
= edit_move_backward (edit
, p
, 1);
1477 /* copy the leading whitespace of the line */
1479 { /* no range check - the line _is_ \n-terminated */
1480 c
= edit_get_byte (edit
, p
++);
1481 if (c
!= ' ' && c
!= '\t')
1483 edit_insert (edit
, c
);
1487 /* --------------------------------------------------------------------------------------------- */
1490 edit_double_newline (WEdit
* edit
)
1492 edit_insert (edit
, '\n');
1493 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1495 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1497 edit
->force
|= REDRAW_PAGE
;
1498 edit_insert (edit
, '\n');
1501 /* --------------------------------------------------------------------------------------------- */
1504 edit_tab_cmd (WEdit
* edit
)
1508 if (option_fake_half_tabs
)
1510 if (is_in_indent (edit
))
1512 /*insert a half tab (usually four spaces) unless there is a
1513 half tab already behind, then delete it and insert a
1515 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
))
1517 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1518 edit_backspace (edit
, 1);
1519 edit_insert (edit
, '\t');
1523 insert_spaces_tab (edit
, 1);
1528 if (option_fill_tabs_with_spaces
)
1530 insert_spaces_tab (edit
, 0);
1534 edit_insert (edit
, '\t');
1538 /* --------------------------------------------------------------------------------------------- */
1541 check_and_wrap_line (WEdit
* edit
)
1544 if (!option_typewriter_wrap
)
1546 edit_update_curs_col (edit
);
1547 if (edit
->curs_col
< option_word_wrap_line_length
)
1553 c
= edit_get_byte (edit
, curs
);
1554 if (c
== '\n' || curs
<= 0)
1556 edit_insert (edit
, '\n');
1559 if (c
== ' ' || c
== '\t')
1561 int current
= edit
->curs1
;
1562 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
1563 edit_insert (edit
, '\n');
1564 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
1570 /* --------------------------------------------------------------------------------------------- */
1571 /** this find the matching bracket in either direction, and sets edit->bracket */
1574 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
1576 const char *const b
= "{}{[][()(", *p
;
1577 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
1578 unsigned long j
= 0;
1580 edit_update_curs_row (edit
);
1581 c
= edit_get_byte (edit
, edit
->curs1
);
1584 if (!furthest_bracket_search
)
1585 furthest_bracket_search
--;
1586 /* not on a bracket at all */
1589 /* the matching bracket */
1591 /* going left or right? */
1592 if (strchr ("{[(", c
))
1594 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
1596 /* out of buffer? */
1597 if (q
>= edit
->last_byte
|| q
< 0)
1599 a
= edit_get_byte (edit
, q
);
1600 /* don't want to eat CPU */
1601 if (j
++ > furthest_bracket_search
)
1603 /* out of screen? */
1606 if (q
< edit
->start_display
)
1608 /* count lines if searching downward */
1609 if (inc
> 0 && a
== '\n')
1610 if (n
++ >= edit
->widget
.lines
- edit
->curs_row
) /* out of screen */
1613 /* count bracket depth */
1614 i
+= (a
== c
) - (a
== d
);
1615 /* return if bracket depth is zero */
1623 /* --------------------------------------------------------------------------------------------- */
1626 edit_goto_matching_bracket (WEdit
* edit
)
1630 q
= edit_get_bracket (edit
, 0, 0);
1633 edit
->bracket
= edit
->curs1
;
1634 edit
->force
|= REDRAW_PAGE
;
1635 edit_cursor_move (edit
, q
- edit
->curs1
);
1639 /* --------------------------------------------------------------------------------------------- */
1640 /*** public functions ****************************************************************************/
1641 /* --------------------------------------------------------------------------------------------- */
1643 /** User edit menu, like user menu (F2) but only in editor. */
1646 user_menu (WEdit
* edit
, const char *menu_file
, int selected_entry
)
1651 long start_mark
, end_mark
;
1654 block_file
= concat_dir_and_file (mc_config_get_cache_path (), EDIT_BLOCK_FILE
);
1656 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
1658 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1660 /* run shell scripts from menu */
1661 if (user_menu_cmd (edit
, menu_file
, selected_entry
)
1662 && (mc_stat (block_file
, &status
) == 0) && (status
.st_size
!= 0))
1667 /* i.e. we have marked block */
1669 rc
= edit_block_delete_cmd (edit
);
1675 ins_len
= edit_insert_file (edit
, block_file
);
1676 if (nomark
== 0 && ins_len
> 0)
1677 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1679 /* truncate block file */
1680 fd
= fopen (block_file
, "w");
1684 edit_cursor_move (edit
, curs
- edit
->curs1
);
1685 edit_refresh_cmd (edit
);
1686 edit
->force
|= REDRAW_COMPLETELY
;
1688 g_free (block_file
);
1691 /* --------------------------------------------------------------------------------------------- */
1694 edit_get_byte (WEdit
* edit
, long byte_index
)
1697 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1700 if (byte_index
>= edit
->curs1
)
1702 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1703 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
1707 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
1711 /* --------------------------------------------------------------------------------------------- */
1714 edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
1717 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1720 if (byte_index
>= edit
->curs1
)
1722 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1723 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
1724 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
1728 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
1729 (byte_index
& M_EDIT_BUF_SIZE
));
1733 /* --------------------------------------------------------------------------------------------- */
1736 edit_get_buf_ptr (WEdit
* edit
, long byte_index
)
1740 if (byte_index
>= (edit
->curs1
+ edit
->curs2
))
1746 if (byte_index
>= edit
->curs1
)
1748 p
= edit
->curs1
+ edit
->curs2
- 1;
1749 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
1750 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
1754 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] + (0 & M_EDIT_BUF_SIZE
));
1758 /* --------------------------------------------------------------------------------------------- */
1761 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1766 gchar
*next_ch
= NULL
;
1769 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1775 str
= edit_get_byte_ptr (edit
, byte_index
);
1783 res
= g_utf8_get_char_validated (str
, -1);
1793 /* Calculate UTF-8 char width */
1794 next_ch
= g_utf8_next_char (str
);
1797 width
= next_ch
- str
;
1805 *char_width
= width
;
1809 /* --------------------------------------------------------------------------------------------- */
1812 edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1814 gchar
*str
, *buf
= NULL
;
1817 gchar
*next_ch
= NULL
;
1823 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1829 ch
= edit_get_utf (edit
, byte_index
, &width
);
1833 *char_width
= width
;
1837 str
= edit_get_byte_ptr (edit
, byte_index
);
1838 buf
= edit_get_buf_ptr (edit
, byte_index
);
1839 if (str
== NULL
|| buf
== NULL
)
1844 /* get prev utf8 char */
1846 str
= g_utf8_find_prev_char (buf
, str
);
1854 res
= g_utf8_get_char_validated (str
, -1);
1864 /* Calculate UTF-8 char width */
1865 next_ch
= g_utf8_next_char (str
);
1868 width
= next_ch
- str
;
1876 *char_width
= width
;
1880 /* --------------------------------------------------------------------------------------------- */
1883 edit_get_write_filter (const char *write_name
, const char *filename
)
1886 char *p
, *writename
;
1888 i
= edit_find_filter (filename
);
1892 writename
= name_quote (write_name
, 0);
1893 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
1898 /* --------------------------------------------------------------------------------------------- */
1901 edit_write_stream (WEdit
* edit
, FILE * f
)
1905 if (edit
->lb
== LB_ASIS
)
1907 for (i
= 0; i
< edit
->last_byte
; i
++)
1908 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
1913 /* change line breaks */
1914 for (i
= 0; i
< edit
->last_byte
; i
++)
1916 unsigned char c
= edit_get_byte (edit
, i
);
1918 if (!(c
== '\n' || c
== '\r'))
1920 /* not line break */
1921 if (fputc (c
, f
) < 0)
1925 { /* (c == '\n' || c == '\r') */
1926 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
1930 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
1931 /* put one line break unconditionally */
1932 if (fputc ('\n', f
) < 0)
1935 i
++; /* 2 chars are processed */
1937 if (c
== '\r' && c1
== '\n')
1938 /* Windows line break; go to the next char */
1941 if (c
== '\r' && c1
== '\r')
1943 /* two Macintosh line breaks; put second line break */
1944 if (fputc ('\n', f
) < 0)
1949 if (fputc (c1
, f
) < 0)
1953 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
1954 /* put one line break unconditionally */
1955 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
1958 if (c
== '\r' && c1
== '\n')
1959 /* Windows line break; go to the next char */
1963 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
1964 /* put one line break unconditionally */
1965 if (fputc ('\r', f
) < 0)
1968 i
++; /* 2 chars are processed */
1970 if (c
== '\r' && c1
== '\n')
1971 /* Windows line break; go to the next char */
1974 if (c
== '\n' && c1
== '\n')
1976 /* two Windows line breaks; put second line break */
1977 if (fputc ('\r', f
) < 0)
1982 if (fputc (c1
, f
) < 0)
1985 case LB_ASIS
: /* default without changes */
1991 return edit
->last_byte
;
1994 /* --------------------------------------------------------------------------------------------- */
1995 /** inserts a file at the cursor, returns count of inserted bytes on success */
1997 edit_insert_file (WEdit
* edit
, const char *filename
)
2002 p
= edit_get_filter (filename
);
2006 long current
= edit
->curs1
;
2007 f
= (FILE *) popen (p
, "r");
2010 edit_insert_stream (edit
, f
);
2011 ins_len
= edit
->curs1
- current
;
2012 edit_cursor_move (edit
, current
- edit
->curs1
);
2016 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
2017 edit_error_dialog (_("Error"), errmsg
);
2026 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
2027 edit_error_dialog (_("Error"), errmsg
);
2036 int i
, file
, blocklen
;
2037 long current
= edit
->curs1
;
2038 int vertical_insertion
= 0;
2040 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
2043 buf
= g_malloc0 (TEMP_BUF_LEN
);
2044 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2047 /* if contain signature VERTICAL_MAGIC then it vertical block */
2048 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2049 vertical_insertion
= 1;
2051 mc_lseek (file
, 0, SEEK_SET
);
2053 if (vertical_insertion
)
2057 blocklen
= edit_insert_column_of_text_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
2058 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2059 /* highlight inserted text then not persistent blocks */
2060 if (!option_persistent_selections
)
2062 if (!edit
->column_highlight
)
2063 edit_push_undo_action (edit
, COLUMN_OFF
);
2064 edit
->column_highlight
= 1;
2069 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2071 for (i
= 0; i
< blocklen
; i
++)
2072 edit_insert (edit
, buf
[i
]);
2074 /* highlight inserted text then not persistent blocks */
2075 if (!option_persistent_selections
&& edit
->modified
)
2077 edit_set_markers (edit
, edit
->curs1
, current
, 0, 0);
2078 if (edit
->column_highlight
)
2079 edit_push_undo_action (edit
, COLUMN_ON
);
2080 edit
->column_highlight
= 0;
2083 edit
->force
|= REDRAW_PAGE
;
2084 ins_len
= edit
->curs1
- current
;
2085 edit_cursor_move (edit
, current
- edit
->curs1
);
2094 /* --------------------------------------------------------------------------------------------- */
2096 * Fill in the edit structure. Return NULL on failure. Pass edit as
2097 * NULL to allocate a new structure.
2099 * If line is 0, try to restore saved position. Otherwise put the
2100 * cursor on that line and show it in the middle of the screen.
2104 edit_init (WEdit
* edit
, int y
, int x
, int lines
, int cols
, const char *filename
, long line
)
2106 gboolean to_free
= FALSE
;
2108 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2109 if (option_line_state
)
2110 option_line_state_width
= LINE_STATE_WIDTH
;
2112 option_line_state_width
= 0;
2118 * Expand option_whole_chars_search by national letters using
2122 static char option_whole_chars_search_buf
[256];
2124 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
2127 size_t len
= str_term_width1 (option_whole_chars_search
);
2129 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
2131 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
2133 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
2135 option_whole_chars_search_buf
[len
++] = i
;
2139 option_whole_chars_search_buf
[len
] = 0;
2140 option_whole_chars_search
= option_whole_chars_search_buf
;
2142 #endif /* ENABLE_NLS */
2143 edit
= g_malloc0 (sizeof (WEdit
));
2144 edit
->search
= NULL
;
2148 edit_purge_widget (edit
);
2151 edit
->widget
.lines
= lines
;
2152 edit
->widget
.cols
= cols
;
2154 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2155 edit
->stat1
.st_uid
= getuid ();
2156 edit
->stat1
.st_gid
= getgid ();
2157 edit
->stat1
.st_mtime
= 0;
2161 edit
->force
|= REDRAW_PAGE
;
2162 edit_set_filename (edit
, filename
);
2164 edit
->undo_stack_size
= START_STACK_SIZE
;
2165 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2166 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2168 edit
->redo_stack_size
= START_STACK_SIZE
;
2169 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2170 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2173 edit
->converter
= str_cnv_from_term
;
2174 edit_set_codeset (edit
);
2176 if (edit_load_file (edit
))
2178 /* edit_load_file already gives an error message */
2184 edit
->loading_done
= 1;
2187 edit_load_syntax (edit
, NULL
, NULL
);
2190 edit_get_syntax_color (edit
, -1, &color
);
2193 /* load saved cursor position */
2194 if ((line
== 0) && option_save_position
)
2195 edit_load_position (edit
);
2200 edit_move_display (edit
, line
- 1);
2201 edit_move_to_line (edit
, line
- 1);
2204 edit_load_macro_cmd (edit
);
2208 /* --------------------------------------------------------------------------------------------- */
2209 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2212 edit_clean (WEdit
* edit
)
2219 /* a stale lock, remove it */
2221 edit
->locked
= edit_unlock_file (edit
);
2223 /* save cursor position */
2224 if (option_save_position
)
2225 edit_save_position (edit
);
2226 else if (edit
->serialized_bookmarks
!= NULL
)
2227 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2229 /* File specified on the mcedit command line and never saved */
2230 if (edit
->delete_file
)
2231 unlink (edit
->filename
);
2233 edit_free_syntax_rules (edit
);
2234 book_mark_flush (edit
, -1);
2235 for (; j
<= MAXBUFF
; j
++)
2237 g_free (edit
->buffers1
[j
]);
2238 g_free (edit
->buffers2
[j
]);
2241 g_free (edit
->undo_stack
);
2242 g_free (edit
->redo_stack
);
2243 g_free (edit
->filename
);
2245 mc_search_free (edit
->search
);
2246 edit
->search
= NULL
;
2248 if (edit
->converter
!= str_cnv_from_term
)
2249 str_close_conv (edit
->converter
);
2251 edit_purge_widget (edit
);
2256 /* --------------------------------------------------------------------------------------------- */
2257 /** returns 1 on success */
2260 edit_renew (WEdit
* edit
)
2262 int y
= edit
->widget
.y
;
2263 int x
= edit
->widget
.x
;
2264 int lines
= edit
->widget
.lines
;
2265 int columns
= edit
->widget
.cols
;
2268 return (edit_init (edit
, y
, x
, lines
, columns
, "", 0) != NULL
);
2271 /* --------------------------------------------------------------------------------------------- */
2273 * Load a new file into the editor. If it fails, preserve the old file.
2274 * To do it, allocate a new widget, initialize it and, if the new file
2275 * was loaded, copy the data to the old widget.
2276 * Return 1 on success, 0 on failure.
2280 edit_reload (WEdit
* edit
, const char *filename
)
2283 int y
= edit
->widget
.y
;
2284 int x
= edit
->widget
.x
;
2285 int lines
= edit
->widget
.lines
;
2286 int columns
= edit
->widget
.cols
;
2288 e
= g_malloc0 (sizeof (WEdit
));
2289 e
->widget
= edit
->widget
;
2290 if (edit_init (e
, y
, x
, lines
, columns
, filename
, 0) == NULL
)
2296 memcpy (edit
, e
, sizeof (WEdit
));
2301 /* --------------------------------------------------------------------------------------------- */
2303 * Load a new file into the editor and set line. If it fails, preserve the old file.
2304 * To do it, allocate a new widget, initialize it and, if the new file
2305 * was loaded, copy the data to the old widget.
2306 * Return 1 on success, 0 on failure.
2310 edit_reload_line (WEdit
* edit
, const char *filename
, long line
)
2313 int y
= edit
->widget
.y
;
2314 int x
= edit
->widget
.x
;
2315 int lines
= edit
->widget
.lines
;
2316 int columns
= edit
->widget
.cols
;
2318 e
= g_malloc0 (sizeof (WEdit
));
2319 e
->widget
= edit
->widget
;
2320 if (edit_init (e
, y
, x
, lines
, columns
, filename
, line
) == NULL
)
2326 memcpy (edit
, e
, sizeof (WEdit
));
2331 /* --------------------------------------------------------------------------------------------- */
2334 edit_set_codeset (WEdit
* edit
)
2340 get_codepage_id (mc_global
.source_codepage
>=
2341 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2346 conv
= str_crt_conv_from (cp_id
);
2347 if (conv
!= INVALID_CONV
)
2349 if (edit
->converter
!= str_cnv_from_term
)
2350 str_close_conv (edit
->converter
);
2351 edit
->converter
= conv
;
2356 edit
->utf8
= str_isutf8 (cp_id
);
2363 /* --------------------------------------------------------------------------------------------- */
2365 Recording stack for undo:
2366 The following is an implementation of a compressed stack. Identical
2367 pushes are recorded by a negative prefix indicating the number of times the
2368 same char was pushed. This saves space for repeated curs-left or curs-right
2385 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2386 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2387 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2388 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2391 The only way the cursor moves or the buffer is changed is through the routines:
2392 insert, backspace, insert_ahead, delete, and cursor_move.
2393 These record the reverse undo movements onto the stack each time they are
2396 Each key press results in a set of actions (insert; delete ...). So each time
2397 a key is pressed the current position of start_display is pushed as
2398 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2399 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2400 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2405 edit_push_undo_action (WEdit
* edit
, long c
, ...)
2407 unsigned long sp
= edit
->undo_stack_pointer
;
2411 /* first enlarge the stack if necessary */
2412 if (sp
> edit
->undo_stack_size
- 10)
2414 if (option_max_undo
< 256)
2415 option_max_undo
= 256;
2416 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2418 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2421 edit
->undo_stack
= t
;
2422 edit
->undo_stack_size
<<= 1;
2423 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2427 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2428 if (edit
->undo_stack_disable
)
2430 edit_push_redo_action (edit
, KEY_PRESS
);
2431 edit_push_redo_action (edit
, c
);
2434 else if (edit
->redo_stack_reset
)
2436 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2439 if (edit
->undo_stack_bottom
!= sp
2440 && spm1
!= edit
->undo_stack_bottom
2441 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2444 if (edit
->undo_stack
[spm1
] < 0)
2446 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2449 if (edit
->undo_stack
[spm1
] > -1000000000)
2451 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2453 edit
->undo_stack
[spm1
]--;
2461 d
= edit
->undo_stack
[spm1
];
2465 return; /* --> no need to push multiple do-nothings */
2466 edit
->undo_stack
[sp
] = -2;
2471 edit
->undo_stack
[sp
] = c
;
2474 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2476 /* if the sp wraps round and catches the undo_stack_bottom then erase
2477 * the first set of actions on the stack to make space - by moving
2478 * undo_stack_bottom forward one "key press" */
2479 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2480 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2481 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2484 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2486 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2487 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2489 /*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: */
2490 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2491 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2493 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2498 edit_push_redo_action (WEdit
* edit
, long c
, ...)
2500 unsigned long sp
= edit
->redo_stack_pointer
;
2503 /* first enlarge the stack if necessary */
2504 if (sp
> edit
->redo_stack_size
- 10)
2506 if (option_max_undo
< 256)
2507 option_max_undo
= 256;
2508 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2510 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2513 edit
->redo_stack
= t
;
2514 edit
->redo_stack_size
<<= 1;
2515 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2519 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2521 if (edit
->redo_stack_bottom
!= sp
2522 && spm1
!= edit
->redo_stack_bottom
2523 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2526 if (edit
->redo_stack
[spm1
] < 0)
2528 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2531 if (edit
->redo_stack
[spm1
] > -1000000000)
2533 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2534 edit
->redo_stack
[spm1
]--;
2541 d
= edit
->redo_stack
[spm1
];
2545 return; /* --> no need to push multiple do-nothings */
2546 edit
->redo_stack
[sp
] = -2;
2547 goto redo_check_bottom
;
2551 edit
->redo_stack
[sp
] = c
;
2554 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2556 /* if the sp wraps round and catches the redo_stack_bottom then erase
2557 * the first set of actions on the stack to make space - by moving
2558 * redo_stack_bottom forward one "key press" */
2559 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2560 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2561 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2564 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2566 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2567 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2570 * If a single key produced enough pushes to wrap all the way round then
2571 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2572 * The stack is then initialised:
2575 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2576 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2577 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2581 /* --------------------------------------------------------------------------------------------- */
2583 Basic low level single character buffer alterations and movements at the cursor.
2584 Returns char passed over, inserted or removed.
2588 edit_insert (WEdit
* edit
, int c
)
2590 /* check if file has grown to large */
2591 if (edit
->last_byte
>= SIZE_LIMIT
)
2594 /* first we must update the position of the display window */
2595 if (edit
->curs1
< edit
->start_display
)
2597 edit
->start_display
++;
2602 /* Mark file as modified, unless the file hasn't been fully loaded */
2603 if (edit
->loading_done
)
2605 edit_modification (edit
);
2608 /* now we must update some info on the file and check if a redraw is required */
2611 if (edit
->book_mark
)
2612 book_mark_inc (edit
, edit
->curs_line
);
2614 edit
->total_lines
++;
2615 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2618 /* save the reverse command onto the undo stack */
2619 /* ordinary char and not space */
2621 edit_push_undo_action (edit
, BACKSPACE
);
2623 edit_push_undo_action (edit
, BACKSPACE_BR
);
2624 /* update markers */
2625 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2626 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2627 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2629 /* add a new buffer if we've reached the end of the last one */
2630 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2631 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2633 /* perform the insertion */
2634 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2635 = (unsigned char) c
;
2637 /* update file length */
2640 /* update cursor position */
2644 /* --------------------------------------------------------------------------------------------- */
2645 /** same as edit_insert and move left */
2648 edit_insert_ahead (WEdit
* edit
, int c
)
2650 if (edit
->last_byte
>= SIZE_LIMIT
)
2653 if (edit
->curs1
< edit
->start_display
)
2655 edit
->start_display
++;
2659 edit_modification (edit
);
2662 if (edit
->book_mark
)
2663 book_mark_inc (edit
, edit
->curs_line
);
2664 edit
->total_lines
++;
2665 edit
->force
|= REDRAW_AFTER_CURSOR
;
2667 /* ordinary char and not space */
2669 edit_push_undo_action (edit
, DELCHAR
);
2671 edit_push_undo_action (edit
, DELCHAR_BR
);
2673 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2674 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2675 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2677 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2678 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2679 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2680 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2687 /* --------------------------------------------------------------------------------------------- */
2690 edit_delete (WEdit
* edit
, const int byte_delete
)
2700 /* if byte_delete = 1 then delete only one byte not multibyte char */
2701 if (edit
->utf8
&& byte_delete
== 0)
2703 edit_get_utf (edit
, edit
->curs1
, &cw
);
2708 if (edit
->mark2
!= edit
->mark1
)
2709 edit_push_markers (edit
);
2711 for (i
= 1; i
<= cw
; i
++)
2713 if (edit
->mark1
> edit
->curs1
)
2716 edit
->end_mark_curs
--;
2718 if (edit
->mark2
> edit
->curs1
)
2720 if (edit
->last_get_rule
> edit
->curs1
)
2721 edit
->last_get_rule
--;
2723 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2725 1) & M_EDIT_BUF_SIZE
) - 1];
2727 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2729 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2730 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2734 edit_push_undo_action (edit
, p
+ 256);
2737 edit_modification (edit
);
2740 if (edit
->book_mark
)
2741 book_mark_dec (edit
, edit
->curs_line
);
2742 edit
->total_lines
--;
2743 edit
->force
|= REDRAW_AFTER_CURSOR
;
2745 if (edit
->curs1
< edit
->start_display
)
2747 edit
->start_display
--;
2755 /* --------------------------------------------------------------------------------------------- */
2756 /** moves the cursor right or left: increment positive or negative respectively */
2759 edit_cursor_move (WEdit
* edit
, long increment
)
2761 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2766 for (; increment
< 0; increment
++)
2771 edit_push_undo_action (edit
, CURS_RIGHT
);
2773 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2774 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2775 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2776 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2777 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2779 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2780 1) & M_EDIT_BUF_SIZE
];
2781 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2783 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2784 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2790 edit
->force
|= REDRAW_LINE_BELOW
;
2795 else if (increment
> 0)
2797 for (; increment
> 0; increment
--)
2802 edit_push_undo_action (edit
, CURS_LEFT
);
2804 c
= edit_get_byte (edit
, edit
->curs1
);
2805 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2806 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2807 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2809 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2811 1) & M_EDIT_BUF_SIZE
) - 1];
2812 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2814 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2815 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2821 edit
->force
|= REDRAW_LINE_ABOVE
;
2827 /* These functions return positions relative to lines */
2829 /* --------------------------------------------------------------------------------------------- */
2830 /** returns index of last char on line + 1 */
2833 edit_eol (WEdit
* edit
, long current
)
2835 if (current
>= edit
->last_byte
)
2836 return edit
->last_byte
;
2839 if (edit_get_byte (edit
, current
) == '\n')
2844 /* --------------------------------------------------------------------------------------------- */
2845 /** returns index of first char on line */
2848 edit_bol (WEdit
* edit
, long current
)
2854 if (edit_get_byte (edit
, current
- 1) == '\n')
2859 /* --------------------------------------------------------------------------------------------- */
2862 edit_count_lines (WEdit
* edit
, long current
, long upto
)
2865 if (upto
> edit
->last_byte
)
2866 upto
= edit
->last_byte
;
2869 while (current
< upto
)
2870 if (edit_get_byte (edit
, current
++) == '\n')
2875 /* --------------------------------------------------------------------------------------------- */
2876 /* If lines is zero this returns the count of lines from current to upto. */
2877 /* If upto is zero returns index of lines forward current. */
2880 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
2884 return edit_count_lines (edit
, current
, upto
);
2893 next
= edit_eol (edit
, current
) + 1;
2894 if (next
> edit
->last_byte
)
2903 /* --------------------------------------------------------------------------------------------- */
2904 /** Returns offset of 'lines' lines up from current */
2907 edit_move_backward (WEdit
* edit
, long current
, long lines
)
2911 current
= edit_bol (edit
, current
);
2912 while ((lines
--) && current
!= 0)
2913 current
= edit_bol (edit
, current
- 1);
2917 /* --------------------------------------------------------------------------------------------- */
2918 /* If cols is zero this returns the count of columns from current to upto. */
2919 /* If upto is zero returns index of cols across from current. */
2922 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
2933 q
= edit
->last_byte
+ 2;
2935 for (col
= 0, p
= current
; p
< q
; p
++)
2947 orig_c
= c
= edit_get_byte (edit
, p
);
2955 utf_ch
= edit_get_utf (edit
, p
, &cw
);
2956 if (mc_global
.utf8_display
)
2960 if (g_unichar_iswide (utf_ch
))
2963 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
2967 c
= convert_to_display_c (c
);
2971 col
+= TAB_SIZE
- col
% TAB_SIZE
;
2979 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!mc_global
.utf8_display
&& !edit
->utf8
)))
2980 /* '\r' is shown as ^M, so we must advance 2 characters */
2981 /* Caret notation for control characters */
2989 /* --------------------------------------------------------------------------------------------- */
2990 /** returns the current column position of the cursor */
2993 edit_get_col (WEdit
* edit
)
2995 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
2998 /* --------------------------------------------------------------------------------------------- */
2999 /* Scrolling functions */
3000 /* --------------------------------------------------------------------------------------------- */
3003 edit_update_curs_row (WEdit
* edit
)
3005 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
3008 /* --------------------------------------------------------------------------------------------- */
3011 edit_update_curs_col (WEdit
* edit
)
3013 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3016 /* --------------------------------------------------------------------------------------------- */
3019 edit_get_curs_col (const WEdit
* edit
)
3021 return edit
->curs_col
;
3024 /* --------------------------------------------------------------------------------------------- */
3025 /** moves the display start position up by i lines */
3028 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
3030 unsigned long lines_above
= edit
->start_line
;
3031 if (i
> lines_above
)
3035 edit
->start_line
-= i
;
3036 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
3037 edit
->force
|= REDRAW_PAGE
;
3038 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3040 edit_update_curs_row (edit
);
3044 /* --------------------------------------------------------------------------------------------- */
3045 /** returns 1 if could scroll, 0 otherwise */
3048 edit_scroll_downward (WEdit
* edit
, int i
)
3051 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->widget
.lines
- 1);
3052 if (lines_below
> 0)
3054 if (i
> lines_below
)
3056 edit
->start_line
+= i
;
3057 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
3058 edit
->force
|= REDRAW_PAGE
;
3059 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3061 edit_update_curs_row (edit
);
3064 /* --------------------------------------------------------------------------------------------- */
3067 edit_scroll_right (WEdit
* edit
, int i
)
3069 edit
->force
|= REDRAW_PAGE
;
3070 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3071 edit
->start_col
-= i
;
3074 /* --------------------------------------------------------------------------------------------- */
3077 edit_scroll_left (WEdit
* edit
, int i
)
3079 if (edit
->start_col
)
3081 edit
->start_col
+= i
;
3082 if (edit
->start_col
> 0)
3083 edit
->start_col
= 0;
3084 edit
->force
|= REDRAW_PAGE
;
3085 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3089 /* --------------------------------------------------------------------------------------------- */
3090 /* high level cursor movement commands */
3091 /* --------------------------------------------------------------------------------------------- */
3094 edit_move_to_prev_col (WEdit
* edit
, long p
)
3096 int prev
= edit
->prev_col
;
3097 int over
= edit
->over_col
;
3098 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
3100 if (option_cursor_beyond_eol
)
3102 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
3103 edit_eol (edit
, edit
->curs1
));
3105 if (line_len
< prev
+ edit
->over_col
)
3107 edit
->over_col
= prev
+ over
- line_len
;
3108 edit
->prev_col
= line_len
;
3109 edit
->curs_col
= line_len
;
3113 edit
->curs_col
= prev
+ over
;
3114 edit
->prev_col
= edit
->curs_col
;
3121 if (is_in_indent (edit
) && option_fake_half_tabs
)
3123 edit_update_curs_col (edit
);
3125 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
3127 int q
= edit
->curs_col
;
3128 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
3129 p
= edit_bol (edit
, edit
->curs1
);
3130 edit_cursor_move (edit
,
3131 edit_move_forward3 (edit
, p
, edit
->curs_col
,
3133 if (!left_of_four_spaces (edit
))
3134 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3140 /* --------------------------------------------------------------------------------------------- */
3143 line_is_blank (WEdit
* edit
, long line
)
3145 return is_blank (edit
, edit_find_line (edit
, line
));
3148 /* --------------------------------------------------------------------------------------------- */
3149 /** move cursor to line 'line' */
3152 edit_move_to_line (WEdit
* e
, long line
)
3154 if (line
< e
->curs_line
)
3155 edit_move_up (e
, e
->curs_line
- line
, 0);
3157 edit_move_down (e
, line
- e
->curs_line
, 0);
3158 edit_scroll_screen_over_cursor (e
);
3161 /* --------------------------------------------------------------------------------------------- */
3162 /** scroll window so that first visible line is 'line' */
3165 edit_move_display (WEdit
* e
, long line
)
3167 if (line
< e
->start_line
)
3168 edit_scroll_upward (e
, e
->start_line
- line
);
3170 edit_scroll_downward (e
, line
- e
->start_line
);
3173 /* --------------------------------------------------------------------------------------------- */
3174 /** save markers onto undo stack */
3177 edit_push_markers (WEdit
* edit
)
3179 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3180 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3181 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3184 /* --------------------------------------------------------------------------------------------- */
3187 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
3196 /* --------------------------------------------------------------------------------------------- */
3197 /** highlight marker toggle */
3200 edit_mark_cmd (WEdit
* edit
, int unmark
)
3202 edit_push_markers (edit
);
3205 edit_set_markers (edit
, 0, 0, 0, 0);
3206 edit
->force
|= REDRAW_PAGE
;
3210 if (edit
->mark2
>= 0)
3212 edit
->end_mark_curs
= -1;
3213 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3214 edit
->curs_col
+ edit
->over_col
);
3215 edit
->force
|= REDRAW_PAGE
;
3219 edit
->end_mark_curs
= edit
->curs1
;
3220 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3221 edit
->curs_col
+ edit
->over_col
);
3226 /* --------------------------------------------------------------------------------------------- */
3227 /** highlight the word under cursor */
3230 edit_mark_current_word_cmd (WEdit
* edit
)
3234 for (pos
= edit
->curs1
; pos
!= 0; pos
--)
3238 c1
= edit_get_byte (edit
, pos
);
3239 c2
= edit_get_byte (edit
, pos
- 1);
3240 if (!isspace (c1
) && isspace (c2
))
3242 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3247 for (; pos
< edit
->last_byte
; pos
++)
3251 c1
= edit_get_byte (edit
, pos
);
3252 c2
= edit_get_byte (edit
, pos
+ 1);
3253 if (!isspace (c1
) && isspace (c2
))
3255 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3258 edit
->mark2
= min (pos
+ 1, edit
->last_byte
);
3260 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3263 /* --------------------------------------------------------------------------------------------- */
3266 edit_mark_current_line_cmd (WEdit
* edit
)
3268 long pos
= edit
->curs1
;
3270 edit
->mark1
= edit_bol (edit
, pos
);
3271 edit
->mark2
= edit_eol (edit
, pos
);
3273 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3276 /* --------------------------------------------------------------------------------------------- */
3279 edit_delete_line (WEdit
* edit
)
3282 * Delete right part of the line.
3283 * Note that edit_get_byte() returns '\n' when byte position is
3286 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3288 (void) edit_delete (edit
, 1);
3293 * Note that edit_delete() will not corrupt anything if called while
3294 * cursor position is EOF.
3296 (void) edit_delete (edit
, 1);
3299 * Delete left part of the line.
3300 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3302 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3304 (void) edit_backspace (edit
, 1);
3308 /* --------------------------------------------------------------------------------------------- */
3311 insert_spaces_tab (WEdit
* edit
, int half
)
3314 edit_update_curs_col (edit
);
3315 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) +
3316 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
3319 edit_insert (edit
, ' ');
3324 /* --------------------------------------------------------------------------------------------- */
3327 edit_indent_width (WEdit
* edit
, long p
)
3330 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
3332 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
3335 /* --------------------------------------------------------------------------------------------- */
3338 edit_insert_indent (WEdit
* edit
, int indent
)
3340 if (!option_fill_tabs_with_spaces
)
3342 while (indent
>= TAB_SIZE
)
3344 edit_insert (edit
, '\t');
3348 while (indent
-- > 0)
3349 edit_insert (edit
, ' ');
3352 /* --------------------------------------------------------------------------------------------- */
3355 edit_push_key_press (WEdit
* edit
)
3357 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3358 if (edit
->mark2
== -1)
3360 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3361 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3365 /* --------------------------------------------------------------------------------------------- */
3368 edit_find_bracket (WEdit
* edit
)
3370 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3371 if (last_bracket
!= edit
->bracket
)
3372 edit
->force
|= REDRAW_PAGE
;
3373 last_bracket
= edit
->bracket
;
3376 /* --------------------------------------------------------------------------------------------- */
3378 * This executes a command as though the user initiated it through a key
3379 * press. Callback with WIDGET_KEY as a message calls this after
3380 * translating the key press. This function can be used to pass any
3381 * command to the editor. Note that the screen wouldn't update
3382 * automatically. Either of command or char_for_insertion must be
3383 * passed as -1. Commands are executed, and char_for_insertion is
3384 * inserted at the cursor.
3388 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3390 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3392 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3395 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3398 if (macro_index
!= -1)
3400 edit
->force
|= REDRAW_COMPLETELY
;
3401 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3403 edit_store_macro_cmd (edit
);
3407 else if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3409 edit_repeat_macro_cmd (edit
);
3415 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3417 record_macro_buf
[macro_index
].action
= command
;
3418 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3420 /* record the beginning of a set of editing actions initiated by a key press */
3421 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3422 edit_push_key_press (edit
);
3424 edit_execute_cmd (edit
, command
, char_for_insertion
);
3425 if (edit
->column_highlight
)
3426 edit
->force
|= REDRAW_PAGE
;
3429 /* --------------------------------------------------------------------------------------------- */
3431 This executes a command at a lower level than macro recording.
3432 It also does not push a key_press onto the undo stack. This means
3433 that if it is called many times, a single undo command will undo
3434 all of them. It also does not check for the Undo command.
3437 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3439 edit
->force
|= REDRAW_LINE
;
3441 /* The next key press will unhighlight the found string, so update
3443 if (edit
->found_len
|| edit
->column_highlight
)
3444 edit
->force
|= REDRAW_PAGE
;
3448 /* a mark command with shift-arrow */
3451 case CK_MarkToWordBegin
:
3452 case CK_MarkToWordEnd
:
3458 case CK_MarkPageDown
:
3459 case CK_MarkToFileBegin
:
3460 case CK_MarkToFileEnd
:
3461 case CK_MarkToPageBegin
:
3462 case CK_MarkToPageEnd
:
3463 case CK_MarkScrollUp
:
3464 case CK_MarkScrollDown
:
3465 case CK_MarkParagraphUp
:
3466 case CK_MarkParagraphDown
:
3467 /* a mark command with alt-arrow */
3468 case CK_MarkColumnPageUp
:
3469 case CK_MarkColumnPageDown
:
3470 case CK_MarkColumnLeft
:
3471 case CK_MarkColumnRight
:
3472 case CK_MarkColumnUp
:
3473 case CK_MarkColumnDown
:
3474 case CK_MarkColumnScrollUp
:
3475 case CK_MarkColumnScrollDown
:
3476 case CK_MarkColumnParagraphUp
:
3477 case CK_MarkColumnParagraphDown
:
3478 edit
->column_highlight
= 0;
3479 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3481 edit_mark_cmd (edit
, 1); /* clear */
3482 edit_mark_cmd (edit
, 0); /* marking on */
3484 edit
->highlight
= 1;
3487 /* any other command */
3489 if (edit
->highlight
)
3490 edit_mark_cmd (edit
, 0); /* clear */
3491 edit
->highlight
= 0;
3494 /* first check for undo */
3495 if (command
== CK_Undo
)
3497 edit
->redo_stack_reset
= 0;
3498 edit_group_undo (edit
);
3499 edit
->found_len
= 0;
3500 edit
->prev_col
= edit_get_col (edit
);
3501 edit
->search_start
= edit
->curs1
;
3504 /* check for redo */
3505 if (command
== CK_Redo
)
3507 edit
->redo_stack_reset
= 0;
3508 edit_do_redo (edit
);
3509 edit
->found_len
= 0;
3510 edit
->prev_col
= edit_get_col (edit
);
3511 edit
->search_start
= edit
->curs1
;
3515 edit
->redo_stack_reset
= 1;
3517 /* An ordinary key press */
3518 if (char_for_insertion
>= 0)
3520 /* if non persistent selection and text selected */
3521 if (!option_persistent_selections
)
3523 if (edit
->mark1
!= edit
->mark2
)
3524 edit_block_delete_cmd (edit
);
3526 if (edit
->overwrite
)
3528 /* remove char only one time, after input first byte, multibyte chars */
3529 if ((!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3530 && edit_get_byte (edit
, edit
->curs1
) != '\n')
3531 edit_delete (edit
, 0);
3533 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3534 edit_insert_over (edit
);
3536 if (char_for_insertion
> 255 && mc_global
.utf8_display
== 0)
3538 unsigned char str
[6 + 1];
3540 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3550 while (str
[i
] != 0 && i
<= 6)
3552 char_for_insertion
= str
[i
];
3553 edit_insert (edit
, char_for_insertion
);
3559 edit_insert (edit
, char_for_insertion
);
3561 if (option_auto_para_formatting
)
3563 format_paragraph (edit
, 0);
3564 edit
->force
|= REDRAW_PAGE
;
3567 check_and_wrap_line (edit
);
3568 edit
->found_len
= 0;
3569 edit
->prev_col
= edit_get_col (edit
);
3570 edit
->search_start
= edit
->curs1
;
3571 edit_find_bracket (edit
);
3577 case CK_TopOnScreen
:
3578 case CK_BottomOnScreen
:
3579 case CK_MarkToPageBegin
:
3580 case CK_MarkToPageEnd
:
3587 if (edit
->mark2
>= 0)
3589 if (!option_persistent_selections
)
3591 if (edit
->column_highlight
)
3592 edit_push_undo_action (edit
, COLUMN_ON
);
3593 edit
->column_highlight
= 0;
3594 edit_mark_cmd (edit
, 1);
3601 case CK_TopOnScreen
:
3602 case CK_BottomOnScreen
:
3603 case CK_MarkToPageBegin
:
3604 case CK_MarkToPageEnd
:
3609 case CK_MarkToWordBegin
:
3610 case CK_MarkToWordEnd
:
3613 case CK_MarkColumnUp
:
3614 case CK_MarkColumnDown
:
3615 if (edit
->mark2
== -1)
3616 break; /*marking is following the cursor: may need to highlight a whole line */
3621 edit
->force
|= REDRAW_CHAR_ONLY
;
3624 /* basic cursor key commands */
3628 /* if non persistent selection and text selected */
3629 if (!option_persistent_selections
)
3631 if (edit
->mark1
!= edit
->mark2
)
3633 edit_block_delete_cmd (edit
);
3637 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3642 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3644 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3645 edit_backspace (edit
, 1);
3650 if (option_fake_half_tabs
)
3653 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3655 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3656 edit_backspace (edit
, 1);
3661 edit_backspace (edit
, 0);
3664 /* if non persistent selection and text selected */
3665 if (!option_persistent_selections
)
3667 if (edit
->mark1
!= edit
->mark2
)
3669 edit_block_delete_cmd (edit
);
3674 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3675 edit_insert_over (edit
);
3677 if (option_fake_half_tabs
)
3680 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3682 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3683 edit_delete (edit
, 1);
3687 edit_delete (edit
, 0);
3689 case CK_DeleteToWordBegin
:
3691 edit_left_delete_word (edit
);
3693 case CK_DeleteToWordEnd
:
3694 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3695 edit_insert_over (edit
);
3697 edit_right_delete_word (edit
);
3700 edit_delete_line (edit
);
3702 case CK_DeleteToHome
:
3703 edit_delete_to_line_begin (edit
);
3705 case CK_DeleteToEnd
:
3706 edit_delete_to_line_end (edit
);
3710 if (option_auto_para_formatting
)
3712 edit_double_newline (edit
);
3713 if (option_return_does_auto_indent
)
3714 edit_auto_indent (edit
);
3715 format_paragraph (edit
, 0);
3719 edit_insert (edit
, '\n');
3720 if (option_return_does_auto_indent
)
3722 edit_auto_indent (edit
);
3727 edit_insert (edit
, '\n');
3730 case CK_MarkColumnPageUp
:
3731 edit
->column_highlight
= 1;
3734 edit_move_up (edit
, edit
->widget
.lines
- 1, 1);
3736 case CK_MarkColumnPageDown
:
3737 edit
->column_highlight
= 1;
3739 case CK_MarkPageDown
:
3740 edit_move_down (edit
, edit
->widget
.lines
- 1, 1);
3742 case CK_MarkColumnLeft
:
3743 edit
->column_highlight
= 1;
3746 if (option_fake_half_tabs
)
3748 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3750 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3753 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3754 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3758 edit_left_char_move_cmd (edit
);
3760 case CK_MarkColumnRight
:
3761 edit
->column_highlight
= 1;
3764 if (option_fake_half_tabs
)
3766 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3768 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3769 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3773 edit_right_char_move_cmd (edit
);
3775 case CK_TopOnScreen
:
3776 case CK_MarkToPageBegin
:
3777 edit_begin_page (edit
);
3779 case CK_BottomOnScreen
:
3780 case CK_MarkToPageEnd
:
3781 edit_end_page (edit
);
3784 case CK_MarkToWordBegin
:
3786 edit_left_word_move_cmd (edit
);
3789 case CK_MarkToWordEnd
:
3791 edit_right_word_move_cmd (edit
);
3793 case CK_MarkColumnUp
:
3794 edit
->column_highlight
= 1;
3797 edit_move_up (edit
, 1, 0);
3799 case CK_MarkColumnDown
:
3800 edit
->column_highlight
= 1;
3803 edit_move_down (edit
, 1, 0);
3805 case CK_MarkColumnParagraphUp
:
3806 edit
->column_highlight
= 1;
3807 case CK_ParagraphUp
:
3808 case CK_MarkParagraphUp
:
3809 edit_move_up_paragraph (edit
, 0);
3811 case CK_MarkColumnParagraphDown
:
3812 edit
->column_highlight
= 1;
3813 case CK_ParagraphDown
:
3814 case CK_MarkParagraphDown
:
3815 edit_move_down_paragraph (edit
, 0);
3817 case CK_MarkColumnScrollUp
:
3818 edit
->column_highlight
= 1;
3820 case CK_MarkScrollUp
:
3821 edit_move_up (edit
, 1, 1);
3823 case CK_MarkColumnScrollDown
:
3824 edit
->column_highlight
= 1;
3826 case CK_MarkScrollDown
:
3827 edit_move_down (edit
, 1, 1);
3831 edit_cursor_to_bol (edit
);
3835 edit_cursor_to_eol (edit
);
3838 /* if text marked shift block */
3839 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3841 if (edit
->mark2
< 0)
3842 edit_mark_cmd (edit
, 0);
3843 edit_move_block_to_right (edit
);
3847 if (option_cursor_beyond_eol
)
3848 edit_insert_over (edit
);
3849 edit_tab_cmd (edit
);
3850 if (option_auto_para_formatting
)
3852 format_paragraph (edit
, 0);
3853 edit
->force
|= REDRAW_PAGE
;
3857 check_and_wrap_line (edit
);
3862 case CK_InsertOverwrite
:
3863 edit
->overwrite
= !edit
->overwrite
;
3867 if (edit
->mark2
>= 0)
3869 if (edit
->column_highlight
)
3870 edit_push_undo_action (edit
, COLUMN_ON
);
3871 edit
->column_highlight
= 0;
3873 edit_mark_cmd (edit
, 0);
3876 if (!edit
->column_highlight
)
3877 edit_push_undo_action (edit
, COLUMN_OFF
);
3878 edit
->column_highlight
= 1;
3879 edit_mark_cmd (edit
, 0);
3882 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3883 edit
->force
|= REDRAW_PAGE
;
3886 if (edit
->column_highlight
)
3887 edit_push_undo_action (edit
, COLUMN_ON
);
3888 edit
->column_highlight
= 0;
3889 edit_mark_cmd (edit
, 1);
3892 if (edit
->column_highlight
)
3893 edit_push_undo_action (edit
, COLUMN_ON
);
3894 edit
->column_highlight
= 0;
3895 edit_mark_current_word_cmd (edit
);
3898 if (edit
->column_highlight
)
3899 edit_push_undo_action (edit
, COLUMN_ON
);
3900 edit
->column_highlight
= 0;
3901 edit_mark_current_line_cmd (edit
);
3904 case CK_ShowNumbers
:
3905 option_line_state
= !option_line_state
;
3906 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
3907 edit
->force
|= REDRAW_PAGE
;
3911 show_right_margin
= !show_right_margin
;
3912 edit
->force
|= REDRAW_PAGE
;
3916 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
3917 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
3918 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3920 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3922 case CK_BookmarkFlush
:
3923 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3924 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3925 edit
->force
|= REDRAW_PAGE
;
3927 case CK_BookmarkNext
:
3928 if (edit
->book_mark
)
3930 struct _book_mark
*p
;
3931 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3935 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
3936 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
3937 edit_move_to_line (edit
, p
->line
);
3941 case CK_BookmarkPrev
:
3942 if (edit
->book_mark
)
3944 struct _book_mark
*p
;
3945 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3946 while (p
->line
== edit
->curs_line
)
3951 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
3952 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
3953 edit_move_to_line (edit
, p
->line
);
3959 case CK_MarkToFileBegin
:
3960 edit_move_to_top (edit
);
3963 case CK_MarkToFileEnd
:
3964 edit_move_to_bottom (edit
);
3968 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3969 edit_insert_over (edit
);
3970 edit_block_copy_cmd (edit
);
3973 edit_block_delete_cmd (edit
);
3976 edit_block_move_cmd (edit
);
3979 case CK_BlockShiftLeft
:
3980 if (edit
->mark1
!= edit
->mark2
)
3981 edit_move_block_to_left (edit
);
3983 case CK_BlockShiftRight
:
3984 if (edit
->mark1
!= edit
->mark2
)
3985 edit_move_block_to_right (edit
);
3988 edit_copy_to_X_buf_cmd (edit
);
3991 edit_cut_to_X_buf_cmd (edit
);
3994 /* if non persistent selection and text selected */
3995 if (!option_persistent_selections
)
3997 if (edit
->mark1
!= edit
->mark2
)
3998 edit_block_delete_cmd (edit
);
4000 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4001 edit_insert_over (edit
);
4002 edit_paste_from_X_buf_cmd (edit
);
4005 edit_paste_from_history (edit
);
4009 edit_save_as_cmd (edit
);
4012 edit_save_confirm_cmd (edit
);
4015 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
4018 edit_save_block_cmd (edit
);
4021 edit_insert_file_cmd (edit
);
4025 edit_load_back_cmd (edit
);
4028 edit_load_forward_cmd (edit
);
4031 case CK_EditSyntaxFile
:
4032 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
4034 case CK_SyntaxChoose
:
4035 edit_syntax_dialog (edit
);
4038 case CK_EditUserMenu
:
4039 edit_load_cmd (edit
, EDIT_FILE_MENU
);
4042 case CK_SyntaxOnOff
:
4043 option_syntax_highlighting
^= 1;
4044 if (option_syntax_highlighting
== 1)
4045 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
4046 edit
->force
|= REDRAW_PAGE
;
4050 enable_show_tabs_tws
^= 1;
4051 edit
->force
|= REDRAW_PAGE
;
4055 edit_search_cmd (edit
, FALSE
);
4057 case CK_SearchContinue
:
4058 edit_search_cmd (edit
, TRUE
);
4061 edit_replace_cmd (edit
, 0);
4063 case CK_ReplaceContinue
:
4064 edit_replace_cmd (edit
, 1);
4067 /* if text marked shift block */
4068 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
4070 edit_move_block_to_left (edit
);
4074 edit_complete_word_cmd (edit
);
4078 edit_get_match_keyword_cmd (edit
);
4081 dlg_stop (edit
->widget
.owner
);
4084 edit_new_cmd (edit
);
4087 edit_help_cmd (edit
);
4090 edit_refresh_cmd (edit
);
4102 edit_options_dialog (edit
);
4104 case CK_OptionsSaveMode
:
4105 menu_save_mode_cmd ();
4110 /* fool gcc to prevent a Y2K warning */
4111 char time_format
[] = "_c";
4112 time_format
[0] = '%';
4114 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4115 edit_print_string (edit
, s
);
4116 edit
->force
|= REDRAW_PAGE
;
4121 edit_goto_cmd (edit
);
4123 case CK_ParagraphFormat
:
4124 format_paragraph (edit
, 1);
4125 edit
->force
|= REDRAW_PAGE
;
4127 case CK_MacroDelete
:
4128 edit_delete_macro_cmd (edit
);
4130 case CK_MatchBracket
:
4131 edit_goto_matching_bracket (edit
);
4134 user_menu (edit
, NULL
, -1);
4137 edit_sort_cmd (edit
);
4139 case CK_ExternalCommand
:
4140 edit_ext_cmd (edit
);
4143 edit_mail_dialog (edit
);
4149 case CK_SelectCodepage
:
4150 edit_select_codepage_cmd (edit
);
4153 case CK_InsertLiteral
:
4154 edit_insert_literal_cmd (edit
);
4156 case CK_MacroStartStopRecord
:
4157 edit_begin_end_macro_cmd (edit
);
4159 case CK_RepeatStartStopRecord
:
4160 edit_begin_end_repeat_cmd (edit
);
4162 case CK_ExtendedKeyMap
:
4163 edit
->extmod
= TRUE
;
4170 if ((command
/ CK_PipeBlock (0)) == 1)
4171 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
4173 /* keys which must set the col position, and the search vars */
4177 case CK_SearchContinue
:
4179 case CK_ReplaceContinue
:
4181 edit
->prev_col
= edit_get_col (edit
);
4185 case CK_MarkColumnUp
:
4188 case CK_MarkColumnDown
:
4191 case CK_MarkColumnPageUp
:
4193 case CK_MarkPageDown
:
4194 case CK_MarkColumnPageDown
:
4196 case CK_MarkToFileBegin
:
4198 case CK_MarkToFileEnd
:
4199 case CK_ParagraphUp
:
4200 case CK_MarkParagraphUp
:
4201 case CK_MarkColumnParagraphUp
:
4202 case CK_ParagraphDown
:
4203 case CK_MarkParagraphDown
:
4204 case CK_MarkColumnParagraphDown
:
4206 case CK_MarkScrollUp
:
4207 case CK_MarkColumnScrollUp
:
4209 case CK_MarkScrollDown
:
4210 case CK_MarkColumnScrollDown
:
4211 edit
->search_start
= edit
->curs1
;
4212 edit
->found_len
= 0;
4215 edit
->found_len
= 0;
4216 edit
->prev_col
= edit_get_col (edit
);
4217 edit
->search_start
= edit
->curs1
;
4219 edit_find_bracket (edit
);
4221 if (option_auto_para_formatting
)
4227 case CK_DeleteToWordBegin
:
4228 case CK_DeleteToWordEnd
:
4229 case CK_DeleteToHome
:
4230 case CK_DeleteToEnd
:
4231 format_paragraph (edit
, 0);
4232 edit
->force
|= REDRAW_PAGE
;
4237 /* --------------------------------------------------------------------------------------------- */
4240 edit_stack_init (void)
4242 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4244 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
4245 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4248 edit_stack_iterator
= 0;
4251 /* --------------------------------------------------------------------------------------------- */
4254 edit_stack_free (void)
4256 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4257 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
4260 /* --------------------------------------------------------------------------------------------- */
4264 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
4266 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4269 /* --------------------------------------------------------------------------------------------- */
4273 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
4275 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4278 /* --------------------------------------------------------------------------------------------- */
4281 edit_unlock_file (WEdit
* edit
)
4286 fullpath
= mc_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4287 ret
= unlock_file (fullpath
);
4293 /* --------------------------------------------------------------------------------------------- */
4296 edit_lock_file (WEdit
* edit
)
4301 fullpath
= mc_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4302 ret
= lock_file (fullpath
);
4308 /* --------------------------------------------------------------------------------------------- */