2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2008, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
10 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 * \brief Source: editor low level data handling and cursor fundamentals
37 #include <sys/types.h>
46 #include "lib/global.h"
48 #include "lib/tty/color.h"
49 #include "lib/tty/tty.h" /* attrset() */
50 #include "lib/tty/key.h" /* is_idle() */
51 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
52 #include "lib/vfs/vfs.h"
53 #include "lib/strutil.h" /* utf string functions */
54 #include "lib/util.h" /* load_file_position(), save_file_position() */
55 #include "lib/timefmt.h" /* time formatting */
57 #include "lib/widget.h"
60 #include "lib/charsets.h" /* get_codepage_id */
63 #include "src/filemanager/cmd.h" /* view_other_cmd() */
64 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
66 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/learn.h" /* learn_keys */
68 #include "src/keybind-defaults.h"
70 #include "edit-impl.h"
71 #include "edit-widget.h"
73 /*** global variables ****************************************************************************/
75 int option_word_wrap_line_length
= DEFAULT_WRAP_LINE_LENGTH
;
76 int option_typewriter_wrap
= 0;
77 int option_auto_para_formatting
= 0;
78 int option_fill_tabs_with_spaces
= 0;
79 int option_return_does_auto_indent
= 1;
80 int option_backspace_through_tabs
= 0;
81 int option_fake_half_tabs
= 1;
82 int option_save_mode
= EDIT_QUICK_SAVE
;
83 int option_save_position
= 1;
84 int option_max_undo
= 32768;
85 int option_persistent_selections
= 1;
86 int option_cursor_beyond_eol
= 0;
87 int option_line_state
= 0;
88 int option_line_state_width
= 0;
90 int option_edit_right_extreme
= 0;
91 int option_edit_left_extreme
= 0;
92 int option_edit_top_extreme
= 0;
93 int option_edit_bottom_extreme
= 0;
94 int enable_show_tabs_tws
= 1;
95 int option_check_nl_at_eof
= 0;
96 int option_group_undo
= 0;
97 int show_right_margin
= 0;
99 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
100 char *option_backup_ext
= NULL
;
102 int edit_stack_iterator
= 0;
103 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
104 /* magic sequense for say than block is vertical */
105 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
107 /*** file scope macro definitions ****************************************************************/
109 #define TEMP_BUF_LEN 1024
111 #define space_width 1
113 /*** file scope type declarations ****************************************************************/
115 /*** file scope variables ************************************************************************/
117 /* detecting an error on save is easy: just check if every byte has been written. */
118 /* detecting an error on read, is not so easy 'cos there is not way to tell
119 whether you read everything or not. */
120 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
121 static const struct edit_filters
123 const char *read
, *write
, *extension
;
127 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
128 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
129 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
130 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
131 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
135 static long last_bracket
= -1;
137 /*** file scope functions ************************************************************************/
138 /* --------------------------------------------------------------------------------------------- */
142 * here's a quick sketch of the layout: (don't run this through indent.)
144 * (b1 is buffers1 and b2 is buffers2)
147 * \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
148 * ______________________________________|______________________________________
150 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
151 * |-> |-> |-> |-> |-> |-> |
153 * _<------------------------->|<----------------->_
154 * WEdit->curs2 | WEdit->curs1
159 * file end|||file beginning
168 /* --------------------------------------------------------------------------------------------- */
170 static int left_of_four_spaces (WEdit
* edit
);
172 /* --------------------------------------------------------------------------------------------- */
177 const char *header
= N_("About");
178 const char *button_name
= N_("&OK");
179 const char *const version
= "MCEdit " VERSION
;
180 char text
[BUF_LARGE
];
182 int win_len
, version_len
, button_len
;
189 button_name
= _(button_name
);
192 button_len
= str_term_width1 (button_name
) + 5;
193 version_len
= str_term_width1 (version
);
195 g_snprintf (text
, sizeof (text
),
196 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
197 " A user friendly text editor\n"
198 " written for the Midnight Commander"));
200 win_len
= str_term_width1 (header
);
201 win_len
= max (win_len
, version_len
);
202 win_len
= max (win_len
, button_len
);
204 /* count width and height of text */
205 str_msg_term_size (text
, &lines
, &cols
);
207 cols
= max (win_len
, cols
) + 6;
210 about_dlg
= create_dlg (TRUE
, 0, 0, lines
, cols
, dialog_colors
, NULL
,
211 "[Internal File Editor]", header
, DLG_CENTER
| DLG_TRYUP
);
213 add_widget (about_dlg
, label_new (3, (cols
- version_len
) / 2, version
));
214 add_widget (about_dlg
, label_new (5, 3, text
));
215 add_widget (about_dlg
, button_new (lines
- 3, (cols
- button_len
) / 2,
216 B_ENTER
, NORMAL_BUTTON
, button_name
, NULL
));
219 destroy_dlg (about_dlg
);
222 /* --------------------------------------------------------------------------------------------- */
224 * Initialize the buffers for an empty files.
228 edit_init_buffers (WEdit
* edit
)
232 for (j
= 0; j
<= MAXBUFF
; j
++)
234 edit
->buffers1
[j
] = NULL
;
235 edit
->buffers2
[j
] = NULL
;
240 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
243 /* --------------------------------------------------------------------------------------------- */
245 * Load file OR text into buffers. Set cursor to the beginning of file.
246 * @returns 1 on error.
250 edit_load_file_fast (WEdit
* edit
, const char *filename
)
256 edit
->curs2
= edit
->last_byte
;
257 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
259 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
264 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
265 edit_error_dialog (_("Error"), errmsg
);
270 if (!edit
->buffers2
[buf2
])
271 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
276 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
277 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
280 for (buf
= buf2
- 1; buf
>= 0; buf
--)
282 /* edit->buffers2[0] is already allocated */
283 if (!edit
->buffers2
[buf
])
284 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
285 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
293 char *err_str
= g_strdup_printf (_("Error reading %s"), filename
);
294 edit_error_dialog (_("Error"), err_str
);
301 /* --------------------------------------------------------------------------------------------- */
302 /** Return index of the filter or -1 is there is no appropriate filter */
305 edit_find_filter (const char *filename
)
309 if (filename
== NULL
)
312 l
= strlen (filename
);
313 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
315 e
= strlen (all_filters
[i
].extension
);
317 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
323 /* --------------------------------------------------------------------------------------------- */
326 edit_get_filter (const char *filename
)
329 char *p
, *quoted_name
;
331 i
= edit_find_filter (filename
);
335 quoted_name
= name_quote (filename
, 0);
336 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
337 g_free (quoted_name
);
341 /* --------------------------------------------------------------------------------------------- */
344 edit_insert_stream (WEdit
* edit
, FILE * f
)
348 while ((c
= fgetc (f
)) >= 0)
350 edit_insert (edit
, c
);
356 /* --------------------------------------------------------------------------------------------- */
357 /** Open file and create it if necessary. Return 0 for success, 1 for error. */
360 check_file_access (WEdit
* edit
, const char *filename
, struct stat
*st
)
363 gchar
*errmsg
= NULL
;
365 /* Try opening an existing file */
366 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
371 * Try creating the file. O_EXCL prevents following broken links
372 * and opening existing files.
374 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
377 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
382 /* New file, delete it if it's not modified or saved */
383 edit
->delete_file
= 1;
387 /* Check what we have opened */
388 if (mc_fstat (file
, st
) < 0)
390 errmsg
= g_strdup_printf (_("Cannot get size/permissions for %s"), filename
);
394 /* We want to open regular files only */
395 if (!S_ISREG (st
->st_mode
))
397 errmsg
= g_strdup_printf (_("\"%s\" is not a regular file"), filename
);
402 * Don't delete non-empty files.
403 * O_EXCL should prevent it, but let's be on the safe side.
406 edit
->delete_file
= 0;
408 if (st
->st_size
>= SIZE_LIMIT
)
409 errmsg
= g_strdup_printf (_("File \"%s\" is too large"), filename
);
412 (void) mc_close (file
);
416 edit_error_dialog (_("Error"), errmsg
);
423 /* --------------------------------------------------------------------------------------------- */
425 * Open the file and load it into the buffers, either directly or using
426 * a filter. Return 0 on success, 1 on error.
428 * Fast loading (edit_load_file_fast) is used when the file size is
429 * known. In this case the data is read into the buffers by blocks.
430 * If the file size is not known, the data is loaded byte by byte in
435 edit_load_file (WEdit
* edit
)
438 vfs_path_t
*vpath
= vfs_path_from_str (edit
->filename
);
440 /* Cannot do fast load if a filter is used */
441 if (edit_find_filter (edit
->filename
) >= 0)
445 * VFS may report file size incorrectly, and slow load is not a big
446 * deal considering overhead in VFS.
448 if (!vfs_file_is_local (vpath
))
450 vfs_path_free (vpath
);
453 * FIXME: line end translation should disable fast loading as well
454 * Consider doing fseek() to the end and ftell() for the real size.
459 /* If we are dealing with a real file, check that it exists */
460 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
465 /* nothing to load */
469 edit_init_buffers (edit
);
473 edit
->last_byte
= edit
->stat1
.st_size
;
474 edit_load_file_fast (edit
, edit
->filename
);
475 /* If fast load was used, the number of lines wasn't calculated */
476 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
483 edit
->undo_stack_disable
= 1;
484 if (edit_insert_file (edit
, edit
->filename
) == 0)
489 edit
->undo_stack_disable
= 0;
496 /* --------------------------------------------------------------------------------------------- */
497 /** Restore saved cursor position in the file */
500 edit_load_position (WEdit
* edit
)
507 if (!edit
->filename
|| !*edit
->filename
)
510 vpath
= vfs_path_from_str (edit
->filename
);
511 filename
= vfs_path_to_str (vpath
);
512 load_file_position (filename
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
513 vfs_path_free (vpath
);
518 edit_move_to_line (edit
, line
- 1);
519 edit
->prev_col
= column
;
523 edit_cursor_move (edit
, offset
);
524 line
= edit
->curs_line
;
525 edit
->search_start
= edit
->curs1
;
528 book_mark_restore (edit
, BOOK_MARK_COLOR
);
530 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
531 edit_move_display (edit
, line
- (edit
->widget
.lines
/ 2));
534 /* --------------------------------------------------------------------------------------------- */
535 /** Save cursor position in the file */
538 edit_save_position (WEdit
* edit
)
543 if (edit
->filename
== NULL
|| *edit
->filename
== '\0')
546 vpath
= vfs_path_from_str (edit
->filename
);
547 filename
= vfs_path_to_str (vpath
);
549 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
550 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
,
551 edit
->serialized_bookmarks
);
552 edit
->serialized_bookmarks
= NULL
;
555 vfs_path_free (vpath
);
558 /* --------------------------------------------------------------------------------------------- */
559 /** Clean the WEdit stricture except the widget part */
562 edit_purge_widget (WEdit
* edit
)
564 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
565 char *start
= (char *) edit
+ sizeof (Widget
);
566 memset (start
, 0, len
);
569 /* --------------------------------------------------------------------------------------------- */
572 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
573 then the file should be as it was when he loaded up. Then set edit->modified to 0.
577 edit_pop_undo_action (WEdit
* edit
)
580 unsigned long sp
= edit
->undo_stack_pointer
;
582 if (sp
== edit
->undo_stack_bottom
)
585 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
586 c
= edit
->undo_stack
[sp
];
589 /* edit->undo_stack[sp] = '@'; */
590 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
594 if (sp
== edit
->undo_stack_bottom
)
597 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
598 if (edit
->undo_stack
[sp
] == -2)
600 /* edit->undo_stack[sp] = '@'; */
601 edit
->undo_stack_pointer
= sp
;
604 edit
->undo_stack
[sp
]++;
610 edit_pop_redo_action (WEdit
* edit
)
613 unsigned long sp
= edit
->redo_stack_pointer
;
615 if (sp
== edit
->redo_stack_bottom
)
618 sp
= (sp
- 1) & edit
->redo_stack_size_mask
;
619 c
= edit
->redo_stack
[sp
];
622 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
626 if (sp
== edit
->redo_stack_bottom
)
629 c
= edit
->redo_stack
[(sp
- 1) & edit
->redo_stack_size_mask
];
630 if (edit
->redo_stack
[sp
] == -2)
631 edit
->redo_stack_pointer
= sp
;
633 edit
->redo_stack
[sp
]++;
639 get_prev_undo_action (WEdit
* edit
)
642 unsigned long sp
= edit
->undo_stack_pointer
;
644 if (sp
== edit
->undo_stack_bottom
)
647 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
648 c
= edit
->undo_stack
[sp
];
652 if (sp
== edit
->undo_stack_bottom
)
655 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
659 /* --------------------------------------------------------------------------------------------- */
660 /** is called whenever a modification is made by one of the four routines below */
663 edit_modification (WEdit
* edit
)
665 edit
->caches_valid
= 0;
667 /* raise lock when file modified */
668 if (!edit
->modified
&& !edit
->delete_file
)
669 edit
->locked
= edit_lock_file (edit
);
673 /* --------------------------------------------------------------------------------------------- */
676 edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
678 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
681 if (byte_index
>= edit
->curs1
)
685 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
686 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
687 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
690 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
691 (byte_index
& M_EDIT_BUF_SIZE
));
694 /* --------------------------------------------------------------------------------------------- */
697 edit_get_buf_ptr (WEdit
* edit
, long byte_index
)
699 if (byte_index
>= (edit
->curs1
+ edit
->curs2
))
705 if (byte_index
>= edit
->curs1
)
709 p
= edit
->curs1
+ edit
->curs2
- 1;
710 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
711 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
714 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] + (0 & M_EDIT_BUF_SIZE
));
717 /* --------------------------------------------------------------------------------------------- */
720 edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
722 gchar
*str
, *buf
= NULL
;
725 gchar
*next_ch
= NULL
;
731 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
737 ch
= edit_get_utf (edit
, byte_index
, &width
);
745 str
= edit_get_byte_ptr (edit
, byte_index
);
746 buf
= edit_get_buf_ptr (edit
, byte_index
);
747 if (str
== NULL
|| buf
== NULL
)
752 /* get prev utf8 char */
754 str
= g_utf8_find_prev_char (buf
, str
);
762 res
= g_utf8_get_char_validated (str
, -1);
772 /* Calculate UTF-8 char width */
773 next_ch
= g_utf8_next_char (str
);
776 width
= next_ch
- str
;
788 /* --------------------------------------------------------------------------------------------- */
791 edit_backspace (WEdit
* edit
, const int byte_delete
)
802 if (edit
->mark2
!= edit
->mark1
)
803 edit_push_markers (edit
);
805 if (edit
->utf8
&& byte_delete
== 0)
807 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
811 for (i
= 1; i
<= cw
; i
++)
813 if (edit
->mark1
>= edit
->curs1
)
816 edit
->end_mark_curs
--;
818 if (edit
->mark2
>= edit
->curs1
)
820 if (edit
->last_get_rule
>= edit
->curs1
)
821 edit
->last_get_rule
--;
823 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
824 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
825 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
827 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
828 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
832 edit_push_undo_action (edit
, p
);
834 edit_modification (edit
);
838 book_mark_dec (edit
, edit
->curs_line
);
841 edit
->force
|= REDRAW_AFTER_CURSOR
;
844 if (edit
->curs1
< edit
->start_display
)
846 edit
->start_display
--;
854 /* --------------------------------------------------------------------------------------------- */
855 /* high level cursor movement commands */
856 /* --------------------------------------------------------------------------------------------- */
859 is_in_indent (WEdit
* edit
)
861 long p
= edit_bol (edit
, edit
->curs1
);
862 while (p
< edit
->curs1
)
863 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
868 /* --------------------------------------------------------------------------------------------- */
871 is_blank (WEdit
* edit
, long offset
)
875 s
= edit_bol (edit
, offset
);
876 f
= edit_eol (edit
, offset
) - 1;
879 c
= edit_get_byte (edit
, s
++);
887 /* --------------------------------------------------------------------------------------------- */
888 /** returns the offset of line i */
891 edit_find_line (WEdit
* edit
, int line
)
895 if (!edit
->caches_valid
)
897 for (i
= 0; i
< N_LINE_CACHES
; i
++)
898 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
899 /* three offsets that we *know* are line 0 at 0 and these two: */
900 edit
->line_numbers
[1] = edit
->curs_line
;
901 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
902 edit
->line_numbers
[2] = edit
->total_lines
;
903 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
904 edit
->caches_valid
= 1;
906 if (line
>= edit
->total_lines
)
907 return edit
->line_offsets
[2];
910 /* find the closest known point */
911 for (i
= 0; i
< N_LINE_CACHES
; i
++)
914 n
= abs (edit
->line_numbers
[i
] - line
);
922 return edit
->line_offsets
[j
]; /* know the offset exactly */
923 if (m
== 1 && j
>= 3)
924 i
= j
; /* one line different - caller might be looping, so stay in this cache */
926 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
927 if (line
> edit
->line_numbers
[j
])
928 edit
->line_offsets
[i
] =
929 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
931 edit
->line_offsets
[i
] =
932 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
933 edit
->line_numbers
[i
] = line
;
934 return edit
->line_offsets
[i
];
937 /* --------------------------------------------------------------------------------------------- */
938 /** moves up until a blank line is reached, or until just
939 before a non-blank line is reached */
942 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
945 if (edit
->curs_line
> 1)
947 if (line_is_blank (edit
, edit
->curs_line
))
949 if (line_is_blank (edit
, edit
->curs_line
- 1))
951 for (i
= edit
->curs_line
- 1; i
; i
--)
952 if (!line_is_blank (edit
, i
))
960 for (i
= edit
->curs_line
- 1; i
; i
--)
961 if (line_is_blank (edit
, i
))
967 for (i
= edit
->curs_line
- 1; i
; i
--)
968 if (line_is_blank (edit
, i
))
972 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
975 /* --------------------------------------------------------------------------------------------- */
976 /** moves down until a blank line is reached, or until just
977 before a non-blank line is reached */
980 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
983 if (edit
->curs_line
>= edit
->total_lines
- 1)
985 i
= edit
->total_lines
;
989 if (line_is_blank (edit
, edit
->curs_line
))
991 if (line_is_blank (edit
, edit
->curs_line
+ 1))
993 for (i
= edit
->curs_line
+ 1; i
; i
++)
994 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
1002 for (i
= edit
->curs_line
+ 1; i
; i
++)
1003 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1009 for (i
= edit
->curs_line
+ 1; i
; i
++)
1010 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1014 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
1017 /* --------------------------------------------------------------------------------------------- */
1020 edit_begin_page (WEdit
* edit
)
1022 edit_update_curs_row (edit
);
1023 edit_move_up (edit
, edit
->curs_row
, 0);
1026 /* --------------------------------------------------------------------------------------------- */
1029 edit_end_page (WEdit
* edit
)
1031 edit_update_curs_row (edit
);
1032 edit_move_down (edit
, edit
->widget
.lines
- edit
->curs_row
- 1, 0);
1036 /* --------------------------------------------------------------------------------------------- */
1037 /** goto beginning of text */
1040 edit_move_to_top (WEdit
* edit
)
1042 if (edit
->curs_line
)
1044 edit_cursor_move (edit
, -edit
->curs1
);
1045 edit_move_to_prev_col (edit
, 0);
1046 edit
->force
|= REDRAW_PAGE
;
1047 edit
->search_start
= 0;
1048 edit_update_curs_row (edit
);
1053 /* --------------------------------------------------------------------------------------------- */
1054 /** goto end of text */
1057 edit_move_to_bottom (WEdit
* edit
)
1059 if (edit
->curs_line
< edit
->total_lines
)
1061 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
1062 edit
->start_display
= edit
->last_byte
;
1063 edit
->start_line
= edit
->total_lines
;
1064 edit_scroll_upward (edit
, edit
->widget
.lines
- 1);
1065 edit
->force
|= REDRAW_PAGE
;
1069 /* --------------------------------------------------------------------------------------------- */
1070 /** goto beginning of line */
1073 edit_cursor_to_bol (WEdit
* edit
)
1075 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1076 edit
->search_start
= edit
->curs1
;
1077 edit
->prev_col
= edit_get_col (edit
);
1081 /* --------------------------------------------------------------------------------------------- */
1082 /** goto end of line */
1085 edit_cursor_to_eol (WEdit
* edit
)
1087 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1088 edit
->search_start
= edit
->curs1
;
1089 edit
->prev_col
= edit_get_col (edit
);
1093 /* --------------------------------------------------------------------------------------------- */
1095 static unsigned long
1100 const char option_chars_move_whole_word
[] =
1101 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1107 if (*option_chars_move_whole_word
== '!')
1109 return 0x80000000UL
;
1111 if (g_ascii_isupper ((gchar
) c
))
1113 else if (g_ascii_islower ((gchar
) c
))
1115 else if (g_ascii_isalpha (c
))
1117 else if (isdigit (c
))
1119 else if (isspace (c
))
1121 q
= strchr (option_chars_move_whole_word
, c
);
1123 return 0xFFFFFFFFUL
;
1126 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1131 while ((q
= strchr (q
+ 1, c
)));
1135 /* --------------------------------------------------------------------------------------------- */
1138 edit_left_word_move (WEdit
* edit
, int s
)
1143 if (edit
->column_highlight
1144 && edit
->mark1
!= edit
->mark2
1145 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1147 edit_cursor_move (edit
, -1);
1150 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1151 c2
= edit_get_byte (edit
, edit
->curs1
);
1152 if (c1
== '\n' || c2
== '\n')
1154 if (!(my_type_of (c1
) & my_type_of (c2
)))
1156 if (isspace (c1
) && !isspace (c2
))
1159 if (!isspace (c1
) && isspace (c2
))
1164 /* --------------------------------------------------------------------------------------------- */
1167 edit_left_word_move_cmd (WEdit
* edit
)
1169 edit_left_word_move (edit
, 0);
1170 edit
->force
|= REDRAW_PAGE
;
1173 /* --------------------------------------------------------------------------------------------- */
1176 edit_right_word_move (WEdit
* edit
, int s
)
1181 if (edit
->column_highlight
1182 && edit
->mark1
!= edit
->mark2
1183 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
1185 edit_cursor_move (edit
, 1);
1186 if (edit
->curs1
>= edit
->last_byte
)
1188 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1189 c2
= edit_get_byte (edit
, edit
->curs1
);
1190 if (c1
== '\n' || c2
== '\n')
1192 if (!(my_type_of (c1
) & my_type_of (c2
)))
1194 if (isspace (c1
) && !isspace (c2
))
1197 if (!isspace (c1
) && isspace (c2
))
1202 /* --------------------------------------------------------------------------------------------- */
1205 edit_right_word_move_cmd (WEdit
* edit
)
1207 edit_right_word_move (edit
, 0);
1208 edit
->force
|= REDRAW_PAGE
;
1211 /* --------------------------------------------------------------------------------------------- */
1214 edit_right_char_move_cmd (WEdit
* edit
)
1220 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
1226 c
= edit_get_byte (edit
, edit
->curs1
);
1228 if (option_cursor_beyond_eol
&& c
== '\n')
1234 edit_cursor_move (edit
, cw
);
1238 /* --------------------------------------------------------------------------------------------- */
1241 edit_left_char_move_cmd (WEdit
* edit
)
1244 if (edit
->column_highlight
1245 && option_cursor_beyond_eol
1246 && edit
->mark1
!= edit
->mark2
1247 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1251 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1255 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1261 edit_cursor_move (edit
, -cw
);
1265 /* --------------------------------------------------------------------------------------------- */
1266 /** Up or down cursor moving.
1267 direction = TRUE - move up
1272 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
1275 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
1284 edit
->force
|= REDRAW_PAGE
;
1288 edit_scroll_upward (edit
, i
);
1290 edit_scroll_downward (edit
, i
);
1292 p
= edit_bol (edit
, edit
->curs1
);
1294 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
1296 edit_cursor_move (edit
, p
- edit
->curs1
);
1298 edit_move_to_prev_col (edit
, p
);
1300 /* search start of current multibyte char (like CJK) */
1301 if (edit
->curs1
+ 1 < edit
->last_byte
)
1303 edit_right_char_move_cmd (edit
);
1304 edit_left_char_move_cmd (edit
);
1307 edit
->search_start
= edit
->curs1
;
1308 edit
->found_len
= 0;
1311 /* --------------------------------------------------------------------------------------------- */
1314 edit_right_delete_word (WEdit
* edit
)
1319 if (edit
->curs1
>= edit
->last_byte
)
1321 c1
= edit_delete (edit
, 1);
1322 c2
= edit_get_byte (edit
, edit
->curs1
);
1323 if (c1
== '\n' || c2
== '\n')
1325 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1327 if (!(my_type_of (c1
) & my_type_of (c2
)))
1332 /* --------------------------------------------------------------------------------------------- */
1335 edit_left_delete_word (WEdit
* edit
)
1340 if (edit
->curs1
<= 0)
1342 c1
= edit_backspace (edit
, 1);
1343 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1344 if (c1
== '\n' || c2
== '\n')
1346 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1348 if (!(my_type_of (c1
) & my_type_of (c2
)))
1353 /* --------------------------------------------------------------------------------------------- */
1355 the start column position is not recorded, and hence does not
1356 undo as it happed. But who would notice.
1360 edit_do_undo (WEdit
* edit
)
1365 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1367 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1374 edit_cursor_move (edit
, 1);
1377 edit_cursor_move (edit
, -1);
1381 edit_backspace (edit
, 1);
1385 edit_delete (edit
, 1);
1388 edit
->column_highlight
= 1;
1391 edit
->column_highlight
= 0;
1394 if (ac
>= 256 && ac
< 512)
1395 edit_insert_ahead (edit
, ac
- 256);
1396 if (ac
>= 0 && ac
< 256)
1397 edit_insert (edit
, ac
);
1399 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1401 edit
->mark1
= ac
- MARK_1
;
1402 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1404 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1406 edit
->mark2
= ac
- MARK_2
;
1407 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1409 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1411 edit
->end_mark_curs
= ac
- MARK_CURS
;
1414 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1417 if (edit
->start_display
> ac
- KEY_PRESS
)
1419 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1420 edit
->force
|= REDRAW_PAGE
;
1422 else if (edit
->start_display
< ac
- KEY_PRESS
)
1424 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1425 edit
->force
|= REDRAW_PAGE
;
1427 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1428 edit_update_curs_row (edit
);
1431 edit
->undo_stack_disable
= 0;
1435 edit_do_redo (WEdit
* edit
)
1440 if (edit
->redo_stack_reset
)
1444 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1451 edit_cursor_move (edit
, 1);
1454 edit_cursor_move (edit
, -1);
1457 edit_backspace (edit
, 1);
1460 edit_delete (edit
, 1);
1463 edit
->column_highlight
= 1;
1466 edit
->column_highlight
= 0;
1469 if (ac
>= 256 && ac
< 512)
1470 edit_insert_ahead (edit
, ac
- 256);
1471 if (ac
>= 0 && ac
< 256)
1472 edit_insert (edit
, ac
);
1474 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1476 edit
->mark1
= ac
- MARK_1
;
1477 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1479 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1481 edit
->mark2
= ac
- MARK_2
;
1482 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1484 /* more than one pop usually means something big */
1486 edit
->force
|= REDRAW_PAGE
;
1489 if (edit
->start_display
> ac
- KEY_PRESS
)
1491 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1492 edit
->force
|= REDRAW_PAGE
;
1494 else if (edit
->start_display
< ac
- KEY_PRESS
)
1496 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1497 edit
->force
|= REDRAW_PAGE
;
1499 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1500 edit_update_curs_row (edit
);
1506 edit_group_undo (WEdit
* edit
)
1508 long ac
= KEY_PRESS
;
1509 long cur_ac
= KEY_PRESS
;
1510 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1512 cur_ac
= get_prev_undo_action (edit
);
1513 edit_do_undo (edit
);
1514 ac
= get_prev_undo_action (edit
);
1515 /* exit from cycle if option_group_undo is not set,
1516 * and make single UNDO operation
1518 if (!option_group_undo
)
1523 /* --------------------------------------------------------------------------------------------- */
1526 edit_delete_to_line_end (WEdit
* edit
)
1528 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
1532 edit_delete (edit
, 1);
1536 /* --------------------------------------------------------------------------------------------- */
1539 edit_delete_to_line_begin (WEdit
* edit
)
1541 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
1545 edit_backspace (edit
, 1);
1549 /* --------------------------------------------------------------------------------------------- */
1552 is_aligned_on_a_tab (WEdit
* edit
)
1554 edit_update_curs_col (edit
);
1555 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
1556 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
1559 /* --------------------------------------------------------------------------------------------- */
1562 right_of_four_spaces (WEdit
* edit
)
1565 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1566 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1568 return is_aligned_on_a_tab (edit
);
1572 /* --------------------------------------------------------------------------------------------- */
1575 left_of_four_spaces (WEdit
* edit
)
1578 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1579 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1581 return is_aligned_on_a_tab (edit
);
1585 /* --------------------------------------------------------------------------------------------- */
1588 edit_auto_indent (WEdit
* edit
)
1593 /* use the previous line as a template */
1594 p
= edit_move_backward (edit
, p
, 1);
1595 /* copy the leading whitespace of the line */
1597 { /* no range check - the line _is_ \n-terminated */
1598 c
= edit_get_byte (edit
, p
++);
1599 if (c
!= ' ' && c
!= '\t')
1601 edit_insert (edit
, c
);
1605 /* --------------------------------------------------------------------------------------------- */
1608 edit_double_newline (WEdit
* edit
)
1610 edit_insert (edit
, '\n');
1611 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1613 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1615 edit
->force
|= REDRAW_PAGE
;
1616 edit_insert (edit
, '\n');
1620 /* --------------------------------------------------------------------------------------------- */
1623 insert_spaces_tab (WEdit
* edit
, gboolean half
)
1627 edit_update_curs_col (edit
);
1628 i
= option_tab_spacing
* space_width
;
1631 i
= ((edit
->curs_col
/ i
) + 1) * i
- edit
->curs_col
;
1634 edit_insert (edit
, ' ');
1639 /* --------------------------------------------------------------------------------------------- */
1642 edit_tab_cmd (WEdit
* edit
)
1646 if (option_fake_half_tabs
)
1648 if (is_in_indent (edit
))
1650 /*insert a half tab (usually four spaces) unless there is a
1651 half tab already behind, then delete it and insert a
1653 if (option_fill_tabs_with_spaces
|| !right_of_four_spaces (edit
))
1654 insert_spaces_tab (edit
, TRUE
);
1657 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1658 edit_backspace (edit
, 1);
1659 edit_insert (edit
, '\t');
1664 if (option_fill_tabs_with_spaces
)
1665 insert_spaces_tab (edit
, FALSE
);
1667 edit_insert (edit
, '\t');
1670 /* --------------------------------------------------------------------------------------------- */
1673 check_and_wrap_line (WEdit
* edit
)
1676 if (!option_typewriter_wrap
)
1678 edit_update_curs_col (edit
);
1679 if (edit
->curs_col
< option_word_wrap_line_length
)
1685 c
= edit_get_byte (edit
, curs
);
1686 if (c
== '\n' || curs
<= 0)
1688 edit_insert (edit
, '\n');
1691 if (c
== ' ' || c
== '\t')
1693 int current
= edit
->curs1
;
1694 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
1695 edit_insert (edit
, '\n');
1696 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
1702 /* --------------------------------------------------------------------------------------------- */
1703 /** this find the matching bracket in either direction, and sets edit->bracket */
1706 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
1708 const char *const b
= "{}{[][()(", *p
;
1709 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
1710 unsigned long j
= 0;
1712 edit_update_curs_row (edit
);
1713 c
= edit_get_byte (edit
, edit
->curs1
);
1716 if (!furthest_bracket_search
)
1717 furthest_bracket_search
--;
1718 /* not on a bracket at all */
1721 /* the matching bracket */
1723 /* going left or right? */
1724 if (strchr ("{[(", c
))
1726 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
1728 /* out of buffer? */
1729 if (q
>= edit
->last_byte
|| q
< 0)
1731 a
= edit_get_byte (edit
, q
);
1732 /* don't want to eat CPU */
1733 if (j
++ > furthest_bracket_search
)
1735 /* out of screen? */
1738 if (q
< edit
->start_display
)
1740 /* count lines if searching downward */
1741 if (inc
> 0 && a
== '\n')
1742 if (n
++ >= edit
->widget
.lines
- edit
->curs_row
) /* out of screen */
1745 /* count bracket depth */
1746 i
+= (a
== c
) - (a
== d
);
1747 /* return if bracket depth is zero */
1755 /* --------------------------------------------------------------------------------------------- */
1758 edit_goto_matching_bracket (WEdit
* edit
)
1762 q
= edit_get_bracket (edit
, 0, 0);
1765 edit
->bracket
= edit
->curs1
;
1766 edit
->force
|= REDRAW_PAGE
;
1767 edit_cursor_move (edit
, q
- edit
->curs1
);
1771 /* --------------------------------------------------------------------------------------------- */
1774 edit_move_block_to_right (WEdit
* edit
)
1776 long start_mark
, end_mark
;
1777 long cur_bol
, start_bol
;
1779 if (eval_marks (edit
, &start_mark
, &end_mark
))
1782 start_bol
= edit_bol (edit
, start_mark
);
1783 cur_bol
= edit_bol (edit
, end_mark
- 1);
1787 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1788 if (option_fill_tabs_with_spaces
)
1789 insert_spaces_tab (edit
, option_fake_half_tabs
);
1791 edit_insert (edit
, '\t');
1792 edit_cursor_move (edit
, edit_bol (edit
, cur_bol
) - edit
->curs1
);
1797 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1799 while (cur_bol
>= start_bol
);
1801 edit
->force
|= REDRAW_PAGE
;
1804 /* --------------------------------------------------------------------------------------------- */
1807 edit_move_block_to_left (WEdit
* edit
)
1809 long start_mark
, end_mark
;
1810 long cur_bol
, start_bol
;
1813 if (eval_marks (edit
, &start_mark
, &end_mark
))
1816 start_bol
= edit_bol (edit
, start_mark
);
1817 cur_bol
= edit_bol (edit
, end_mark
- 1);
1824 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1826 if (option_fake_half_tabs
)
1827 del_tab_width
= HALF_TAB_SIZE
;
1829 del_tab_width
= option_tab_spacing
;
1831 next_char
= edit_get_byte (edit
, edit
->curs1
);
1832 if (next_char
== '\t')
1833 edit_delete (edit
, 1);
1834 else if (next_char
== ' ')
1835 for (i
= 1; i
<= del_tab_width
; i
++)
1837 if (next_char
== ' ')
1838 edit_delete (edit
, 1);
1839 next_char
= edit_get_byte (edit
, edit
->curs1
);
1845 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1847 while (cur_bol
>= start_bol
);
1849 edit
->force
|= REDRAW_PAGE
;
1852 /* --------------------------------------------------------------------------------------------- */
1854 * prints at the cursor
1855 * @returns the number of chars printed
1859 edit_print_string (WEdit
* e
, const char *s
)
1863 while (s
[i
] != '\0')
1864 edit_execute_cmd (e
, CK_InsertChar
, (unsigned char) s
[i
++]);
1865 e
->force
|= REDRAW_COMPLETELY
;
1866 edit_update_screen (e
);
1870 /* --------------------------------------------------------------------------------------------- */
1871 /*** public functions ****************************************************************************/
1872 /* --------------------------------------------------------------------------------------------- */
1874 /** User edit menu, like user menu (F2) but only in editor. */
1877 user_menu (WEdit
* edit
, const char *menu_file
, int selected_entry
)
1882 long start_mark
, end_mark
;
1885 block_file
= concat_dir_and_file (mc_config_get_cache_path (), EDIT_BLOCK_FILE
);
1887 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
1889 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1891 /* run shell scripts from menu */
1892 if (user_menu_cmd (edit
, menu_file
, selected_entry
)
1893 && (mc_stat (block_file
, &status
) == 0) && (status
.st_size
!= 0))
1898 /* i.e. we have marked block */
1900 rc
= edit_block_delete_cmd (edit
);
1906 ins_len
= edit_insert_file (edit
, block_file
);
1907 if (nomark
== 0 && ins_len
> 0)
1908 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1910 /* truncate block file */
1911 fd
= fopen (block_file
, "w");
1915 edit_cursor_move (edit
, curs
- edit
->curs1
);
1916 edit_refresh_cmd (edit
);
1917 edit
->force
|= REDRAW_COMPLETELY
;
1919 g_free (block_file
);
1922 /* --------------------------------------------------------------------------------------------- */
1925 edit_get_byte (WEdit
* edit
, long byte_index
)
1928 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1931 if (byte_index
>= edit
->curs1
)
1933 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1934 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
1938 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
1942 /* --------------------------------------------------------------------------------------------- */
1945 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1950 gchar
*next_ch
= NULL
;
1952 gchar utf8_buf
[UTF8_CHAR_LEN
+ 1];
1954 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1960 str
= edit_get_byte_ptr (edit
, byte_index
);
1968 res
= g_utf8_get_char_validated (str
, -1);
1972 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1974 for (i
= 0; i
< UTF8_CHAR_LEN
; i
++)
1975 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
);
1976 utf8_buf
[UTF8_CHAR_LEN
] = '\0';
1978 res
= g_utf8_get_char_validated (str
, -1);
1989 /* Calculate UTF-8 char width */
1990 next_ch
= g_utf8_next_char (str
);
1993 width
= next_ch
- str
;
2001 *char_width
= width
;
2005 /* --------------------------------------------------------------------------------------------- */
2008 edit_get_write_filter (const char *write_name
, const char *filename
)
2011 char *p
, *writename
;
2013 i
= edit_find_filter (filename
);
2017 writename
= name_quote (write_name
, 0);
2018 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
2023 /* --------------------------------------------------------------------------------------------- */
2026 edit_write_stream (WEdit
* edit
, FILE * f
)
2030 if (edit
->lb
== LB_ASIS
)
2032 for (i
= 0; i
< edit
->last_byte
; i
++)
2033 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
2038 /* change line breaks */
2039 for (i
= 0; i
< edit
->last_byte
; i
++)
2041 unsigned char c
= edit_get_byte (edit
, i
);
2043 if (!(c
== '\n' || c
== '\r'))
2045 /* not line break */
2046 if (fputc (c
, f
) < 0)
2050 { /* (c == '\n' || c == '\r') */
2051 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
2055 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
2056 /* put one line break unconditionally */
2057 if (fputc ('\n', f
) < 0)
2060 i
++; /* 2 chars are processed */
2062 if (c
== '\r' && c1
== '\n')
2063 /* Windows line break; go to the next char */
2066 if (c
== '\r' && c1
== '\r')
2068 /* two Macintosh line breaks; put second line break */
2069 if (fputc ('\n', f
) < 0)
2074 if (fputc (c1
, f
) < 0)
2078 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
2079 /* put one line break unconditionally */
2080 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
2083 if (c
== '\r' && c1
== '\n')
2084 /* Windows line break; go to the next char */
2088 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
2089 /* put one line break unconditionally */
2090 if (fputc ('\r', f
) < 0)
2093 i
++; /* 2 chars are processed */
2095 if (c
== '\r' && c1
== '\n')
2096 /* Windows line break; go to the next char */
2099 if (c
== '\n' && c1
== '\n')
2101 /* two Windows line breaks; put second line break */
2102 if (fputc ('\r', f
) < 0)
2107 if (fputc (c1
, f
) < 0)
2110 case LB_ASIS
: /* default without changes */
2116 return edit
->last_byte
;
2119 /* --------------------------------------------------------------------------------------------- */
2120 /** inserts a file at the cursor, returns count of inserted bytes on success */
2122 edit_insert_file (WEdit
* edit
, const char *filename
)
2127 p
= edit_get_filter (filename
);
2131 long current
= edit
->curs1
;
2132 f
= (FILE *) popen (p
, "r");
2135 edit_insert_stream (edit
, f
);
2136 ins_len
= edit
->curs1
- current
;
2137 edit_cursor_move (edit
, current
- edit
->curs1
);
2141 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
2142 edit_error_dialog (_("Error"), errmsg
);
2151 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
2152 edit_error_dialog (_("Error"), errmsg
);
2161 int i
, file
, blocklen
;
2162 long current
= edit
->curs1
;
2163 int vertical_insertion
= 0;
2165 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
2168 buf
= g_malloc0 (TEMP_BUF_LEN
);
2169 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2172 /* if contain signature VERTICAL_MAGIC then it vertical block */
2173 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2174 vertical_insertion
= 1;
2176 mc_lseek (file
, 0, SEEK_SET
);
2178 if (vertical_insertion
)
2182 blocklen
= edit_insert_column_of_text_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
2183 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2184 /* highlight inserted text then not persistent blocks */
2185 if (!option_persistent_selections
)
2187 if (!edit
->column_highlight
)
2188 edit_push_undo_action (edit
, COLUMN_OFF
);
2189 edit
->column_highlight
= 1;
2194 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2196 for (i
= 0; i
< blocklen
; i
++)
2197 edit_insert (edit
, buf
[i
]);
2199 /* highlight inserted text then not persistent blocks */
2200 if (!option_persistent_selections
&& edit
->modified
)
2202 edit_set_markers (edit
, edit
->curs1
, current
, 0, 0);
2203 if (edit
->column_highlight
)
2204 edit_push_undo_action (edit
, COLUMN_ON
);
2205 edit
->column_highlight
= 0;
2208 edit
->force
|= REDRAW_PAGE
;
2209 ins_len
= edit
->curs1
- current
;
2210 edit_cursor_move (edit
, current
- edit
->curs1
);
2219 /* --------------------------------------------------------------------------------------------- */
2221 * Fill in the edit structure. Return NULL on failure. Pass edit as
2222 * NULL to allocate a new structure.
2224 * If line is 0, try to restore saved position. Otherwise put the
2225 * cursor on that line and show it in the middle of the screen.
2229 edit_init (WEdit
* edit
, int y
, int x
, int lines
, int cols
, const char *filename
, long line
)
2231 gboolean to_free
= FALSE
;
2233 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2234 if (option_line_state
)
2235 option_line_state_width
= LINE_STATE_WIDTH
;
2237 option_line_state_width
= 0;
2243 * Expand option_whole_chars_search by national letters using
2247 static char option_whole_chars_search_buf
[256];
2249 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
2252 size_t len
= str_term_width1 (option_whole_chars_search
);
2254 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
2256 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
2258 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
2260 option_whole_chars_search_buf
[len
++] = i
;
2264 option_whole_chars_search_buf
[len
] = 0;
2265 option_whole_chars_search
= option_whole_chars_search_buf
;
2267 #endif /* ENABLE_NLS */
2268 edit
= g_malloc0 (sizeof (WEdit
));
2269 edit
->search
= NULL
;
2273 edit_purge_widget (edit
);
2276 edit
->widget
.lines
= lines
;
2277 edit
->widget
.cols
= cols
;
2279 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2280 edit
->stat1
.st_uid
= getuid ();
2281 edit
->stat1
.st_gid
= getgid ();
2282 edit
->stat1
.st_mtime
= 0;
2286 edit
->force
|= REDRAW_PAGE
;
2287 edit_set_filename (edit
, filename
);
2289 edit
->undo_stack_size
= START_STACK_SIZE
;
2290 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2291 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2293 edit
->redo_stack_size
= START_STACK_SIZE
;
2294 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2295 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2298 edit
->converter
= str_cnv_from_term
;
2299 edit_set_codeset (edit
);
2301 if (edit_load_file (edit
))
2303 /* edit_load_file already gives an error message */
2309 edit
->loading_done
= 1;
2312 edit_load_syntax (edit
, NULL
, NULL
);
2315 edit_get_syntax_color (edit
, -1, &color
);
2318 /* load saved cursor position */
2319 if ((line
== 0) && option_save_position
)
2320 edit_load_position (edit
);
2325 edit_move_display (edit
, line
- 1);
2326 edit_move_to_line (edit
, line
- 1);
2329 edit_load_macro_cmd (edit
);
2333 /* --------------------------------------------------------------------------------------------- */
2334 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2337 edit_clean (WEdit
* edit
)
2344 /* a stale lock, remove it */
2346 edit
->locked
= edit_unlock_file (edit
);
2348 /* save cursor position */
2349 if (option_save_position
)
2350 edit_save_position (edit
);
2351 else if (edit
->serialized_bookmarks
!= NULL
)
2352 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2354 /* File specified on the mcedit command line and never saved */
2355 if (edit
->delete_file
)
2356 unlink (edit
->filename
);
2358 edit_free_syntax_rules (edit
);
2359 book_mark_flush (edit
, -1);
2360 for (; j
<= MAXBUFF
; j
++)
2362 g_free (edit
->buffers1
[j
]);
2363 g_free (edit
->buffers2
[j
]);
2366 g_free (edit
->undo_stack
);
2367 g_free (edit
->redo_stack
);
2368 g_free (edit
->filename
);
2370 mc_search_free (edit
->search
);
2371 edit
->search
= NULL
;
2373 if (edit
->converter
!= str_cnv_from_term
)
2374 str_close_conv (edit
->converter
);
2376 edit_purge_widget (edit
);
2381 /* --------------------------------------------------------------------------------------------- */
2382 /** returns 1 on success */
2385 edit_renew (WEdit
* edit
)
2387 int y
= edit
->widget
.y
;
2388 int x
= edit
->widget
.x
;
2389 int lines
= edit
->widget
.lines
;
2390 int columns
= edit
->widget
.cols
;
2393 return (edit_init (edit
, y
, x
, lines
, columns
, "", 0) != NULL
);
2396 /* --------------------------------------------------------------------------------------------- */
2398 * Load a new file into the editor. If it fails, preserve the old file.
2399 * To do it, allocate a new widget, initialize it and, if the new file
2400 * was loaded, copy the data to the old widget.
2401 * Return 1 on success, 0 on failure.
2405 edit_reload (WEdit
* edit
, const char *filename
)
2408 int y
= edit
->widget
.y
;
2409 int x
= edit
->widget
.x
;
2410 int lines
= edit
->widget
.lines
;
2411 int columns
= edit
->widget
.cols
;
2413 e
= g_malloc0 (sizeof (WEdit
));
2414 e
->widget
= edit
->widget
;
2415 if (edit_init (e
, y
, x
, lines
, columns
, filename
, 0) == NULL
)
2421 memcpy (edit
, e
, sizeof (WEdit
));
2426 /* --------------------------------------------------------------------------------------------- */
2428 * Load a new file into the editor and set line. If it fails, preserve the old file.
2429 * To do it, allocate a new widget, initialize it and, if the new file
2430 * was loaded, copy the data to the old widget.
2431 * Return 1 on success, 0 on failure.
2435 edit_reload_line (WEdit
* edit
, const char *filename
, long line
)
2438 int y
= edit
->widget
.y
;
2439 int x
= edit
->widget
.x
;
2440 int lines
= edit
->widget
.lines
;
2441 int columns
= edit
->widget
.cols
;
2443 e
= g_malloc0 (sizeof (WEdit
));
2444 e
->widget
= edit
->widget
;
2445 if (edit_init (e
, y
, x
, lines
, columns
, filename
, line
) == NULL
)
2451 memcpy (edit
, e
, sizeof (WEdit
));
2456 /* --------------------------------------------------------------------------------------------- */
2459 edit_set_codeset (WEdit
* edit
)
2465 get_codepage_id (mc_global
.source_codepage
>=
2466 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2471 conv
= str_crt_conv_from (cp_id
);
2472 if (conv
!= INVALID_CONV
)
2474 if (edit
->converter
!= str_cnv_from_term
)
2475 str_close_conv (edit
->converter
);
2476 edit
->converter
= conv
;
2481 edit
->utf8
= str_isutf8 (cp_id
);
2488 /* --------------------------------------------------------------------------------------------- */
2490 Recording stack for undo:
2491 The following is an implementation of a compressed stack. Identical
2492 pushes are recorded by a negative prefix indicating the number of times the
2493 same char was pushed. This saves space for repeated curs-left or curs-right
2510 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2511 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2512 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2513 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2516 The only way the cursor moves or the buffer is changed is through the routines:
2517 insert, backspace, insert_ahead, delete, and cursor_move.
2518 These record the reverse undo movements onto the stack each time they are
2521 Each key press results in a set of actions (insert; delete ...). So each time
2522 a key is pressed the current position of start_display is pushed as
2523 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2524 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2525 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2530 edit_push_undo_action (WEdit
* edit
, long c
, ...)
2532 unsigned long sp
= edit
->undo_stack_pointer
;
2536 /* first enlarge the stack if necessary */
2537 if (sp
> edit
->undo_stack_size
- 10)
2539 if (option_max_undo
< 256)
2540 option_max_undo
= 256;
2541 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2543 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2546 edit
->undo_stack
= t
;
2547 edit
->undo_stack_size
<<= 1;
2548 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2552 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2553 if (edit
->undo_stack_disable
)
2555 edit_push_redo_action (edit
, KEY_PRESS
);
2556 edit_push_redo_action (edit
, c
);
2559 else if (edit
->redo_stack_reset
)
2561 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2564 if (edit
->undo_stack_bottom
!= sp
2565 && spm1
!= edit
->undo_stack_bottom
2566 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2569 if (edit
->undo_stack
[spm1
] < 0)
2571 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2574 if (edit
->undo_stack
[spm1
] > -1000000000)
2576 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2578 edit
->undo_stack
[spm1
]--;
2586 d
= edit
->undo_stack
[spm1
];
2590 return; /* --> no need to push multiple do-nothings */
2591 edit
->undo_stack
[sp
] = -2;
2596 edit
->undo_stack
[sp
] = c
;
2599 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2601 /* if the sp wraps round and catches the undo_stack_bottom then erase
2602 * the first set of actions on the stack to make space - by moving
2603 * undo_stack_bottom forward one "key press" */
2604 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2605 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2606 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2609 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2611 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2612 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2614 /*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: */
2615 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2616 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2618 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2623 edit_push_redo_action (WEdit
* edit
, long c
, ...)
2625 unsigned long sp
= edit
->redo_stack_pointer
;
2628 /* first enlarge the stack if necessary */
2629 if (sp
> edit
->redo_stack_size
- 10)
2631 if (option_max_undo
< 256)
2632 option_max_undo
= 256;
2633 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2635 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2638 edit
->redo_stack
= t
;
2639 edit
->redo_stack_size
<<= 1;
2640 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2644 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2646 if (edit
->redo_stack_bottom
!= sp
2647 && spm1
!= edit
->redo_stack_bottom
2648 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2651 if (edit
->redo_stack
[spm1
] < 0)
2653 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2656 if (edit
->redo_stack
[spm1
] > -1000000000)
2658 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2659 edit
->redo_stack
[spm1
]--;
2666 d
= edit
->redo_stack
[spm1
];
2670 return; /* --> no need to push multiple do-nothings */
2671 edit
->redo_stack
[sp
] = -2;
2672 goto redo_check_bottom
;
2676 edit
->redo_stack
[sp
] = c
;
2679 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2681 /* if the sp wraps round and catches the redo_stack_bottom then erase
2682 * the first set of actions on the stack to make space - by moving
2683 * redo_stack_bottom forward one "key press" */
2684 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2685 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2686 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2689 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2691 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2692 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2695 * If a single key produced enough pushes to wrap all the way round then
2696 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2697 * The stack is then initialised:
2700 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2701 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2702 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2706 /* --------------------------------------------------------------------------------------------- */
2708 Basic low level single character buffer alterations and movements at the cursor.
2709 Returns char passed over, inserted or removed.
2713 edit_insert (WEdit
* edit
, int c
)
2715 /* check if file has grown to large */
2716 if (edit
->last_byte
>= SIZE_LIMIT
)
2719 /* first we must update the position of the display window */
2720 if (edit
->curs1
< edit
->start_display
)
2722 edit
->start_display
++;
2727 /* Mark file as modified, unless the file hasn't been fully loaded */
2728 if (edit
->loading_done
)
2730 edit_modification (edit
);
2733 /* now we must update some info on the file and check if a redraw is required */
2736 if (edit
->book_mark
)
2737 book_mark_inc (edit
, edit
->curs_line
);
2739 edit
->total_lines
++;
2740 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2743 /* save the reverse command onto the undo stack */
2744 /* ordinary char and not space */
2746 edit_push_undo_action (edit
, BACKSPACE
);
2748 edit_push_undo_action (edit
, BACKSPACE_BR
);
2749 /* update markers */
2750 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2751 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2752 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2754 /* add a new buffer if we've reached the end of the last one */
2755 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2756 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2758 /* perform the insertion */
2759 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2760 = (unsigned char) c
;
2762 /* update file length */
2765 /* update cursor position */
2769 /* --------------------------------------------------------------------------------------------- */
2770 /** same as edit_insert and move left */
2773 edit_insert_ahead (WEdit
* edit
, int c
)
2775 if (edit
->last_byte
>= SIZE_LIMIT
)
2778 if (edit
->curs1
< edit
->start_display
)
2780 edit
->start_display
++;
2784 edit_modification (edit
);
2787 if (edit
->book_mark
)
2788 book_mark_inc (edit
, edit
->curs_line
);
2789 edit
->total_lines
++;
2790 edit
->force
|= REDRAW_AFTER_CURSOR
;
2792 /* ordinary char and not space */
2794 edit_push_undo_action (edit
, DELCHAR
);
2796 edit_push_undo_action (edit
, DELCHAR_BR
);
2798 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2799 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2800 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2802 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2803 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2804 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2805 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2812 /* --------------------------------------------------------------------------------------------- */
2815 edit_delete (WEdit
* edit
, const int byte_delete
)
2825 /* if byte_delete = 1 then delete only one byte not multibyte char */
2826 if (edit
->utf8
&& byte_delete
== 0)
2828 edit_get_utf (edit
, edit
->curs1
, &cw
);
2833 if (edit
->mark2
!= edit
->mark1
)
2834 edit_push_markers (edit
);
2836 for (i
= 1; i
<= cw
; i
++)
2838 if (edit
->mark1
> edit
->curs1
)
2841 edit
->end_mark_curs
--;
2843 if (edit
->mark2
> edit
->curs1
)
2845 if (edit
->last_get_rule
> edit
->curs1
)
2846 edit
->last_get_rule
--;
2848 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2850 1) & M_EDIT_BUF_SIZE
) - 1];
2852 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2854 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2855 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2859 edit_push_undo_action (edit
, p
+ 256);
2862 edit_modification (edit
);
2865 if (edit
->book_mark
)
2866 book_mark_dec (edit
, edit
->curs_line
);
2867 edit
->total_lines
--;
2868 edit
->force
|= REDRAW_AFTER_CURSOR
;
2870 if (edit
->curs1
< edit
->start_display
)
2872 edit
->start_display
--;
2880 /* --------------------------------------------------------------------------------------------- */
2881 /** moves the cursor right or left: increment positive or negative respectively */
2884 edit_cursor_move (WEdit
* edit
, long increment
)
2886 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2891 for (; increment
< 0; increment
++)
2896 edit_push_undo_action (edit
, CURS_RIGHT
);
2898 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2899 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2900 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2901 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2902 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2904 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2905 1) & M_EDIT_BUF_SIZE
];
2906 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2908 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2909 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2915 edit
->force
|= REDRAW_LINE_BELOW
;
2920 else if (increment
> 0)
2922 for (; increment
> 0; increment
--)
2927 edit_push_undo_action (edit
, CURS_LEFT
);
2929 c
= edit_get_byte (edit
, edit
->curs1
);
2930 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2931 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2932 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2934 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2936 1) & M_EDIT_BUF_SIZE
) - 1];
2937 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2939 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2940 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2946 edit
->force
|= REDRAW_LINE_ABOVE
;
2952 /* These functions return positions relative to lines */
2954 /* --------------------------------------------------------------------------------------------- */
2955 /** returns index of last char on line + 1 */
2958 edit_eol (WEdit
* edit
, long current
)
2960 if (current
>= edit
->last_byte
)
2961 return edit
->last_byte
;
2964 if (edit_get_byte (edit
, current
) == '\n')
2969 /* --------------------------------------------------------------------------------------------- */
2970 /** returns index of first char on line */
2973 edit_bol (WEdit
* edit
, long current
)
2979 if (edit_get_byte (edit
, current
- 1) == '\n')
2984 /* --------------------------------------------------------------------------------------------- */
2987 edit_count_lines (WEdit
* edit
, long current
, long upto
)
2990 if (upto
> edit
->last_byte
)
2991 upto
= edit
->last_byte
;
2994 while (current
< upto
)
2995 if (edit_get_byte (edit
, current
++) == '\n')
3000 /* --------------------------------------------------------------------------------------------- */
3001 /* If lines is zero this returns the count of lines from current to upto. */
3002 /* If upto is zero returns index of lines forward current. */
3005 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
3009 return edit_count_lines (edit
, current
, upto
);
3018 next
= edit_eol (edit
, current
) + 1;
3019 if (next
> edit
->last_byte
)
3028 /* --------------------------------------------------------------------------------------------- */
3029 /** Returns offset of 'lines' lines up from current */
3032 edit_move_backward (WEdit
* edit
, long current
, long lines
)
3036 current
= edit_bol (edit
, current
);
3037 while ((lines
--) && current
!= 0)
3038 current
= edit_bol (edit
, current
- 1);
3042 /* --------------------------------------------------------------------------------------------- */
3043 /* If cols is zero this returns the count of columns from current to upto. */
3044 /* If upto is zero returns index of cols across from current. */
3047 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
3058 q
= edit
->last_byte
+ 2;
3060 for (col
= 0, p
= current
; p
< q
; p
++)
3072 orig_c
= c
= edit_get_byte (edit
, p
);
3080 utf_ch
= edit_get_utf (edit
, p
, &cw
);
3081 if (mc_global
.utf8_display
)
3085 if (g_unichar_iswide (utf_ch
))
3088 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
3092 c
= convert_to_display_c (c
);
3096 col
+= TAB_SIZE
- col
% TAB_SIZE
;
3104 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!mc_global
.utf8_display
&& !edit
->utf8
)))
3105 /* '\r' is shown as ^M, so we must advance 2 characters */
3106 /* Caret notation for control characters */
3114 /* --------------------------------------------------------------------------------------------- */
3115 /** returns the current column position of the cursor */
3118 edit_get_col (WEdit
* edit
)
3120 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3123 /* --------------------------------------------------------------------------------------------- */
3124 /* Scrolling functions */
3125 /* --------------------------------------------------------------------------------------------- */
3128 edit_update_curs_row (WEdit
* edit
)
3130 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
3133 /* --------------------------------------------------------------------------------------------- */
3136 edit_update_curs_col (WEdit
* edit
)
3138 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3141 /* --------------------------------------------------------------------------------------------- */
3144 edit_get_curs_col (const WEdit
* edit
)
3146 return edit
->curs_col
;
3149 /* --------------------------------------------------------------------------------------------- */
3150 /** moves the display start position up by i lines */
3153 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
3155 unsigned long lines_above
= edit
->start_line
;
3156 if (i
> lines_above
)
3160 edit
->start_line
-= i
;
3161 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
3162 edit
->force
|= REDRAW_PAGE
;
3163 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3165 edit_update_curs_row (edit
);
3169 /* --------------------------------------------------------------------------------------------- */
3170 /** returns 1 if could scroll, 0 otherwise */
3173 edit_scroll_downward (WEdit
* edit
, int i
)
3176 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->widget
.lines
- 1);
3177 if (lines_below
> 0)
3179 if (i
> lines_below
)
3181 edit
->start_line
+= i
;
3182 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
3183 edit
->force
|= REDRAW_PAGE
;
3184 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3186 edit_update_curs_row (edit
);
3189 /* --------------------------------------------------------------------------------------------- */
3192 edit_scroll_right (WEdit
* edit
, int i
)
3194 edit
->force
|= REDRAW_PAGE
;
3195 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3196 edit
->start_col
-= i
;
3199 /* --------------------------------------------------------------------------------------------- */
3202 edit_scroll_left (WEdit
* edit
, int i
)
3204 if (edit
->start_col
)
3206 edit
->start_col
+= i
;
3207 if (edit
->start_col
> 0)
3208 edit
->start_col
= 0;
3209 edit
->force
|= REDRAW_PAGE
;
3210 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3214 /* --------------------------------------------------------------------------------------------- */
3215 /* high level cursor movement commands */
3216 /* --------------------------------------------------------------------------------------------- */
3219 edit_move_to_prev_col (WEdit
* edit
, long p
)
3221 int prev
= edit
->prev_col
;
3222 int over
= edit
->over_col
;
3223 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
3225 if (option_cursor_beyond_eol
)
3227 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
3228 edit_eol (edit
, edit
->curs1
));
3230 if (line_len
< prev
+ edit
->over_col
)
3232 edit
->over_col
= prev
+ over
- line_len
;
3233 edit
->prev_col
= line_len
;
3234 edit
->curs_col
= line_len
;
3238 edit
->curs_col
= prev
+ over
;
3239 edit
->prev_col
= edit
->curs_col
;
3246 if (is_in_indent (edit
) && option_fake_half_tabs
)
3248 edit_update_curs_col (edit
);
3250 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
3252 int q
= edit
->curs_col
;
3253 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
3254 p
= edit_bol (edit
, edit
->curs1
);
3255 edit_cursor_move (edit
,
3256 edit_move_forward3 (edit
, p
, edit
->curs_col
,
3258 if (!left_of_four_spaces (edit
))
3259 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3265 /* --------------------------------------------------------------------------------------------- */
3268 line_is_blank (WEdit
* edit
, long line
)
3270 return is_blank (edit
, edit_find_line (edit
, line
));
3273 /* --------------------------------------------------------------------------------------------- */
3274 /** move cursor to line 'line' */
3277 edit_move_to_line (WEdit
* e
, long line
)
3279 if (line
< e
->curs_line
)
3280 edit_move_up (e
, e
->curs_line
- line
, 0);
3282 edit_move_down (e
, line
- e
->curs_line
, 0);
3283 edit_scroll_screen_over_cursor (e
);
3286 /* --------------------------------------------------------------------------------------------- */
3287 /** scroll window so that first visible line is 'line' */
3290 edit_move_display (WEdit
* e
, long line
)
3292 if (line
< e
->start_line
)
3293 edit_scroll_upward (e
, e
->start_line
- line
);
3295 edit_scroll_downward (e
, line
- e
->start_line
);
3298 /* --------------------------------------------------------------------------------------------- */
3299 /** save markers onto undo stack */
3302 edit_push_markers (WEdit
* edit
)
3304 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3305 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3306 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3309 /* --------------------------------------------------------------------------------------------- */
3312 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
3321 /* --------------------------------------------------------------------------------------------- */
3322 /** highlight marker toggle */
3325 edit_mark_cmd (WEdit
* edit
, int unmark
)
3327 edit_push_markers (edit
);
3330 edit_set_markers (edit
, 0, 0, 0, 0);
3331 edit
->force
|= REDRAW_PAGE
;
3335 if (edit
->mark2
>= 0)
3337 edit
->end_mark_curs
= -1;
3338 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3339 edit
->curs_col
+ edit
->over_col
);
3340 edit
->force
|= REDRAW_PAGE
;
3344 edit
->end_mark_curs
= edit
->curs1
;
3345 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3346 edit
->curs_col
+ edit
->over_col
);
3351 /* --------------------------------------------------------------------------------------------- */
3352 /** highlight the word under cursor */
3355 edit_mark_current_word_cmd (WEdit
* edit
)
3359 for (pos
= edit
->curs1
; pos
!= 0; pos
--)
3363 c1
= edit_get_byte (edit
, pos
);
3364 c2
= edit_get_byte (edit
, pos
- 1);
3365 if (!isspace (c1
) && isspace (c2
))
3367 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3372 for (; pos
< edit
->last_byte
; pos
++)
3376 c1
= edit_get_byte (edit
, pos
);
3377 c2
= edit_get_byte (edit
, pos
+ 1);
3378 if (!isspace (c1
) && isspace (c2
))
3380 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3383 edit
->mark2
= min (pos
+ 1, edit
->last_byte
);
3385 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3388 /* --------------------------------------------------------------------------------------------- */
3391 edit_mark_current_line_cmd (WEdit
* edit
)
3393 long pos
= edit
->curs1
;
3395 edit
->mark1
= edit_bol (edit
, pos
);
3396 edit
->mark2
= edit_eol (edit
, pos
);
3398 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3401 /* --------------------------------------------------------------------------------------------- */
3404 edit_delete_line (WEdit
* edit
)
3407 * Delete right part of the line.
3408 * Note that edit_get_byte() returns '\n' when byte position is
3411 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3413 (void) edit_delete (edit
, 1);
3418 * Note that edit_delete() will not corrupt anything if called while
3419 * cursor position is EOF.
3421 (void) edit_delete (edit
, 1);
3424 * Delete left part of the line.
3425 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3427 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3429 (void) edit_backspace (edit
, 1);
3433 /* --------------------------------------------------------------------------------------------- */
3436 edit_indent_width (WEdit
* edit
, long p
)
3439 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
3441 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
3444 /* --------------------------------------------------------------------------------------------- */
3447 edit_insert_indent (WEdit
* edit
, int indent
)
3449 if (!option_fill_tabs_with_spaces
)
3451 while (indent
>= TAB_SIZE
)
3453 edit_insert (edit
, '\t');
3457 while (indent
-- > 0)
3458 edit_insert (edit
, ' ');
3461 /* --------------------------------------------------------------------------------------------- */
3464 edit_push_key_press (WEdit
* edit
)
3466 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3467 if (edit
->mark2
== -1)
3469 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3470 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3474 /* --------------------------------------------------------------------------------------------- */
3477 edit_find_bracket (WEdit
* edit
)
3479 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3480 if (last_bracket
!= edit
->bracket
)
3481 edit
->force
|= REDRAW_PAGE
;
3482 last_bracket
= edit
->bracket
;
3485 /* --------------------------------------------------------------------------------------------- */
3487 * This executes a command as though the user initiated it through a key
3488 * press. Callback with WIDGET_KEY as a message calls this after
3489 * translating the key press. This function can be used to pass any
3490 * command to the editor. Note that the screen wouldn't update
3491 * automatically. Either of command or char_for_insertion must be
3492 * passed as -1. Commands are executed, and char_for_insertion is
3493 * inserted at the cursor.
3497 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3499 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3501 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3504 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3507 if (macro_index
!= -1)
3509 edit
->force
|= REDRAW_COMPLETELY
;
3510 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3512 edit_store_macro_cmd (edit
);
3516 else if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3518 edit_repeat_macro_cmd (edit
);
3524 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3526 record_macro_buf
[macro_index
].action
= command
;
3527 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3529 /* record the beginning of a set of editing actions initiated by a key press */
3530 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3531 edit_push_key_press (edit
);
3533 edit_execute_cmd (edit
, command
, char_for_insertion
);
3534 if (edit
->column_highlight
)
3535 edit
->force
|= REDRAW_PAGE
;
3538 /* --------------------------------------------------------------------------------------------- */
3540 This executes a command at a lower level than macro recording.
3541 It also does not push a key_press onto the undo stack. This means
3542 that if it is called many times, a single undo command will undo
3543 all of them. It also does not check for the Undo command.
3546 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3548 edit
->force
|= REDRAW_LINE
;
3550 /* The next key press will unhighlight the found string, so update
3552 if (edit
->found_len
|| edit
->column_highlight
)
3553 edit
->force
|= REDRAW_PAGE
;
3557 /* a mark command with shift-arrow */
3560 case CK_MarkToWordBegin
:
3561 case CK_MarkToWordEnd
:
3567 case CK_MarkPageDown
:
3568 case CK_MarkToFileBegin
:
3569 case CK_MarkToFileEnd
:
3570 case CK_MarkToPageBegin
:
3571 case CK_MarkToPageEnd
:
3572 case CK_MarkScrollUp
:
3573 case CK_MarkScrollDown
:
3574 case CK_MarkParagraphUp
:
3575 case CK_MarkParagraphDown
:
3576 /* a mark command with alt-arrow */
3577 case CK_MarkColumnPageUp
:
3578 case CK_MarkColumnPageDown
:
3579 case CK_MarkColumnLeft
:
3580 case CK_MarkColumnRight
:
3581 case CK_MarkColumnUp
:
3582 case CK_MarkColumnDown
:
3583 case CK_MarkColumnScrollUp
:
3584 case CK_MarkColumnScrollDown
:
3585 case CK_MarkColumnParagraphUp
:
3586 case CK_MarkColumnParagraphDown
:
3587 edit
->column_highlight
= 0;
3588 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3590 edit_mark_cmd (edit
, 1); /* clear */
3591 edit_mark_cmd (edit
, 0); /* marking on */
3593 edit
->highlight
= 1;
3596 /* any other command */
3598 if (edit
->highlight
)
3599 edit_mark_cmd (edit
, 0); /* clear */
3600 edit
->highlight
= 0;
3603 /* first check for undo */
3604 if (command
== CK_Undo
)
3606 edit
->redo_stack_reset
= 0;
3607 edit_group_undo (edit
);
3608 edit
->found_len
= 0;
3609 edit
->prev_col
= edit_get_col (edit
);
3610 edit
->search_start
= edit
->curs1
;
3613 /* check for redo */
3614 if (command
== CK_Redo
)
3616 edit
->redo_stack_reset
= 0;
3617 edit_do_redo (edit
);
3618 edit
->found_len
= 0;
3619 edit
->prev_col
= edit_get_col (edit
);
3620 edit
->search_start
= edit
->curs1
;
3624 edit
->redo_stack_reset
= 1;
3626 /* An ordinary key press */
3627 if (char_for_insertion
>= 0)
3629 /* if non persistent selection and text selected */
3630 if (!option_persistent_selections
)
3632 if (edit
->mark1
!= edit
->mark2
)
3633 edit_block_delete_cmd (edit
);
3635 if (edit
->overwrite
)
3637 /* remove char only one time, after input first byte, multibyte chars */
3638 if ((!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3639 && edit_get_byte (edit
, edit
->curs1
) != '\n')
3640 edit_delete (edit
, 0);
3642 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3643 edit_insert_over (edit
);
3645 if (char_for_insertion
> 255 && mc_global
.utf8_display
== 0)
3647 unsigned char str
[6 + 1];
3649 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3659 while (str
[i
] != 0 && i
<= 6)
3661 char_for_insertion
= str
[i
];
3662 edit_insert (edit
, char_for_insertion
);
3668 edit_insert (edit
, char_for_insertion
);
3670 if (option_auto_para_formatting
)
3672 format_paragraph (edit
, 0);
3673 edit
->force
|= REDRAW_PAGE
;
3676 check_and_wrap_line (edit
);
3677 edit
->found_len
= 0;
3678 edit
->prev_col
= edit_get_col (edit
);
3679 edit
->search_start
= edit
->curs1
;
3680 edit_find_bracket (edit
);
3686 case CK_TopOnScreen
:
3687 case CK_BottomOnScreen
:
3688 case CK_MarkToPageBegin
:
3689 case CK_MarkToPageEnd
:
3696 if (edit
->mark2
>= 0)
3698 if (!option_persistent_selections
)
3700 if (edit
->column_highlight
)
3701 edit_push_undo_action (edit
, COLUMN_ON
);
3702 edit
->column_highlight
= 0;
3703 edit_mark_cmd (edit
, 1);
3710 case CK_TopOnScreen
:
3711 case CK_BottomOnScreen
:
3712 case CK_MarkToPageBegin
:
3713 case CK_MarkToPageEnd
:
3718 case CK_MarkToWordBegin
:
3719 case CK_MarkToWordEnd
:
3722 case CK_MarkColumnUp
:
3723 case CK_MarkColumnDown
:
3724 if (edit
->mark2
== -1)
3725 break; /*marking is following the cursor: may need to highlight a whole line */
3730 edit
->force
|= REDRAW_CHAR_ONLY
;
3733 /* basic cursor key commands */
3737 /* if non persistent selection and text selected */
3738 if (!option_persistent_selections
)
3740 if (edit
->mark1
!= edit
->mark2
)
3742 edit_block_delete_cmd (edit
);
3746 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3751 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3753 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3754 edit_backspace (edit
, 1);
3759 if (option_fake_half_tabs
)
3762 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3764 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3765 edit_backspace (edit
, 1);
3770 edit_backspace (edit
, 0);
3773 /* if non persistent selection and text selected */
3774 if (!option_persistent_selections
)
3776 if (edit
->mark1
!= edit
->mark2
)
3778 edit_block_delete_cmd (edit
);
3783 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3784 edit_insert_over (edit
);
3786 if (option_fake_half_tabs
)
3789 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3791 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3792 edit_delete (edit
, 1);
3796 edit_delete (edit
, 0);
3798 case CK_DeleteToWordBegin
:
3800 edit_left_delete_word (edit
);
3802 case CK_DeleteToWordEnd
:
3803 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3804 edit_insert_over (edit
);
3806 edit_right_delete_word (edit
);
3809 edit_delete_line (edit
);
3811 case CK_DeleteToHome
:
3812 edit_delete_to_line_begin (edit
);
3814 case CK_DeleteToEnd
:
3815 edit_delete_to_line_end (edit
);
3819 if (option_auto_para_formatting
)
3821 edit_double_newline (edit
);
3822 if (option_return_does_auto_indent
)
3823 edit_auto_indent (edit
);
3824 format_paragraph (edit
, 0);
3828 edit_insert (edit
, '\n');
3829 if (option_return_does_auto_indent
)
3831 edit_auto_indent (edit
);
3836 edit_insert (edit
, '\n');
3839 case CK_MarkColumnPageUp
:
3840 edit
->column_highlight
= 1;
3843 edit_move_up (edit
, edit
->widget
.lines
- 1, 1);
3845 case CK_MarkColumnPageDown
:
3846 edit
->column_highlight
= 1;
3848 case CK_MarkPageDown
:
3849 edit_move_down (edit
, edit
->widget
.lines
- 1, 1);
3851 case CK_MarkColumnLeft
:
3852 edit
->column_highlight
= 1;
3855 if (option_fake_half_tabs
)
3857 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3859 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3862 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3863 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3867 edit_left_char_move_cmd (edit
);
3869 case CK_MarkColumnRight
:
3870 edit
->column_highlight
= 1;
3873 if (option_fake_half_tabs
)
3875 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3877 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3878 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3882 edit_right_char_move_cmd (edit
);
3884 case CK_TopOnScreen
:
3885 case CK_MarkToPageBegin
:
3886 edit_begin_page (edit
);
3888 case CK_BottomOnScreen
:
3889 case CK_MarkToPageEnd
:
3890 edit_end_page (edit
);
3893 case CK_MarkToWordBegin
:
3895 edit_left_word_move_cmd (edit
);
3898 case CK_MarkToWordEnd
:
3900 edit_right_word_move_cmd (edit
);
3902 case CK_MarkColumnUp
:
3903 edit
->column_highlight
= 1;
3906 edit_move_up (edit
, 1, 0);
3908 case CK_MarkColumnDown
:
3909 edit
->column_highlight
= 1;
3912 edit_move_down (edit
, 1, 0);
3914 case CK_MarkColumnParagraphUp
:
3915 edit
->column_highlight
= 1;
3916 case CK_ParagraphUp
:
3917 case CK_MarkParagraphUp
:
3918 edit_move_up_paragraph (edit
, 0);
3920 case CK_MarkColumnParagraphDown
:
3921 edit
->column_highlight
= 1;
3922 case CK_ParagraphDown
:
3923 case CK_MarkParagraphDown
:
3924 edit_move_down_paragraph (edit
, 0);
3926 case CK_MarkColumnScrollUp
:
3927 edit
->column_highlight
= 1;
3929 case CK_MarkScrollUp
:
3930 edit_move_up (edit
, 1, 1);
3932 case CK_MarkColumnScrollDown
:
3933 edit
->column_highlight
= 1;
3935 case CK_MarkScrollDown
:
3936 edit_move_down (edit
, 1, 1);
3940 edit_cursor_to_bol (edit
);
3944 edit_cursor_to_eol (edit
);
3947 /* if text marked shift block */
3948 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3950 if (edit
->mark2
< 0)
3951 edit_mark_cmd (edit
, 0);
3952 edit_move_block_to_right (edit
);
3956 if (option_cursor_beyond_eol
)
3957 edit_insert_over (edit
);
3958 edit_tab_cmd (edit
);
3959 if (option_auto_para_formatting
)
3961 format_paragraph (edit
, 0);
3962 edit
->force
|= REDRAW_PAGE
;
3966 check_and_wrap_line (edit
);
3971 case CK_InsertOverwrite
:
3972 edit
->overwrite
= !edit
->overwrite
;
3976 if (edit
->mark2
>= 0)
3978 if (edit
->column_highlight
)
3979 edit_push_undo_action (edit
, COLUMN_ON
);
3980 edit
->column_highlight
= 0;
3982 edit_mark_cmd (edit
, 0);
3985 if (!edit
->column_highlight
)
3986 edit_push_undo_action (edit
, COLUMN_OFF
);
3987 edit
->column_highlight
= 1;
3988 edit_mark_cmd (edit
, 0);
3991 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3992 edit
->force
|= REDRAW_PAGE
;
3995 if (edit
->column_highlight
)
3996 edit_push_undo_action (edit
, COLUMN_ON
);
3997 edit
->column_highlight
= 0;
3998 edit_mark_cmd (edit
, 1);
4001 if (edit
->column_highlight
)
4002 edit_push_undo_action (edit
, COLUMN_ON
);
4003 edit
->column_highlight
= 0;
4004 edit_mark_current_word_cmd (edit
);
4007 if (edit
->column_highlight
)
4008 edit_push_undo_action (edit
, COLUMN_ON
);
4009 edit
->column_highlight
= 0;
4010 edit_mark_current_line_cmd (edit
);
4013 case CK_ShowNumbers
:
4014 option_line_state
= !option_line_state
;
4015 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
4016 edit
->force
|= REDRAW_PAGE
;
4020 show_right_margin
= !show_right_margin
;
4021 edit
->force
|= REDRAW_PAGE
;
4025 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
4026 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
4027 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4029 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4031 case CK_BookmarkFlush
:
4032 book_mark_flush (edit
, BOOK_MARK_COLOR
);
4033 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
4034 edit
->force
|= REDRAW_PAGE
;
4036 case CK_BookmarkNext
:
4037 if (edit
->book_mark
)
4039 struct _book_mark
*p
;
4040 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4044 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4045 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4046 edit_move_to_line (edit
, p
->line
);
4050 case CK_BookmarkPrev
:
4051 if (edit
->book_mark
)
4053 struct _book_mark
*p
;
4054 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4055 while (p
->line
== edit
->curs_line
)
4060 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4061 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4062 edit_move_to_line (edit
, p
->line
);
4068 case CK_MarkToFileBegin
:
4069 edit_move_to_top (edit
);
4072 case CK_MarkToFileEnd
:
4073 edit_move_to_bottom (edit
);
4077 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4078 edit_insert_over (edit
);
4079 edit_block_copy_cmd (edit
);
4082 edit_block_delete_cmd (edit
);
4085 edit_block_move_cmd (edit
);
4088 case CK_BlockShiftLeft
:
4089 if (edit
->mark1
!= edit
->mark2
)
4090 edit_move_block_to_left (edit
);
4092 case CK_BlockShiftRight
:
4093 if (edit
->mark1
!= edit
->mark2
)
4094 edit_move_block_to_right (edit
);
4097 edit_copy_to_X_buf_cmd (edit
);
4100 edit_cut_to_X_buf_cmd (edit
);
4103 /* if non persistent selection and text selected */
4104 if (!option_persistent_selections
)
4106 if (edit
->mark1
!= edit
->mark2
)
4107 edit_block_delete_cmd (edit
);
4109 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4110 edit_insert_over (edit
);
4111 edit_paste_from_X_buf_cmd (edit
);
4114 edit_paste_from_history (edit
);
4118 edit_save_as_cmd (edit
);
4121 edit_save_confirm_cmd (edit
);
4124 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
4127 edit_save_block_cmd (edit
);
4130 edit_insert_file_cmd (edit
);
4134 edit_load_back_cmd (edit
);
4137 edit_load_forward_cmd (edit
);
4140 case CK_EditSyntaxFile
:
4141 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
4143 case CK_SyntaxChoose
:
4144 edit_syntax_dialog (edit
);
4147 case CK_EditUserMenu
:
4148 edit_load_cmd (edit
, EDIT_FILE_MENU
);
4151 case CK_SyntaxOnOff
:
4152 option_syntax_highlighting
^= 1;
4153 if (option_syntax_highlighting
== 1)
4154 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
4155 edit
->force
|= REDRAW_PAGE
;
4159 enable_show_tabs_tws
^= 1;
4160 edit
->force
|= REDRAW_PAGE
;
4164 edit_search_cmd (edit
, FALSE
);
4166 case CK_SearchContinue
:
4167 edit_search_cmd (edit
, TRUE
);
4170 edit_replace_cmd (edit
, 0);
4172 case CK_ReplaceContinue
:
4173 edit_replace_cmd (edit
, 1);
4176 /* if text marked shift block */
4177 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
4179 edit_move_block_to_left (edit
);
4183 edit_complete_word_cmd (edit
);
4187 edit_get_match_keyword_cmd (edit
);
4190 dlg_stop (edit
->widget
.owner
);
4193 edit_new_cmd (edit
);
4196 edit_help_cmd (edit
);
4199 edit_refresh_cmd (edit
);
4211 edit_options_dialog (edit
);
4213 case CK_OptionsSaveMode
:
4214 menu_save_mode_cmd ();
4219 /* fool gcc to prevent a Y2K warning */
4220 char time_format
[] = "_c";
4221 time_format
[0] = '%';
4223 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4224 edit_print_string (edit
, s
);
4225 edit
->force
|= REDRAW_PAGE
;
4230 edit_goto_cmd (edit
);
4232 case CK_ParagraphFormat
:
4233 format_paragraph (edit
, 1);
4234 edit
->force
|= REDRAW_PAGE
;
4236 case CK_MacroDelete
:
4237 edit_delete_macro_cmd (edit
);
4239 case CK_MatchBracket
:
4240 edit_goto_matching_bracket (edit
);
4243 user_menu (edit
, NULL
, -1);
4246 edit_sort_cmd (edit
);
4248 case CK_ExternalCommand
:
4249 edit_ext_cmd (edit
);
4252 edit_mail_dialog (edit
);
4258 case CK_SelectCodepage
:
4259 edit_select_codepage_cmd (edit
);
4262 case CK_InsertLiteral
:
4263 edit_insert_literal_cmd (edit
);
4265 case CK_MacroStartStopRecord
:
4266 edit_begin_end_macro_cmd (edit
);
4268 case CK_RepeatStartStopRecord
:
4269 edit_begin_end_repeat_cmd (edit
);
4271 case CK_ExtendedKeyMap
:
4272 edit
->extmod
= TRUE
;
4279 if ((command
/ CK_PipeBlock (0)) == 1)
4280 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
4282 /* keys which must set the col position, and the search vars */
4286 case CK_SearchContinue
:
4288 case CK_ReplaceContinue
:
4290 edit
->prev_col
= edit_get_col (edit
);
4294 case CK_MarkColumnUp
:
4297 case CK_MarkColumnDown
:
4300 case CK_MarkColumnPageUp
:
4302 case CK_MarkPageDown
:
4303 case CK_MarkColumnPageDown
:
4305 case CK_MarkToFileBegin
:
4307 case CK_MarkToFileEnd
:
4308 case CK_ParagraphUp
:
4309 case CK_MarkParagraphUp
:
4310 case CK_MarkColumnParagraphUp
:
4311 case CK_ParagraphDown
:
4312 case CK_MarkParagraphDown
:
4313 case CK_MarkColumnParagraphDown
:
4315 case CK_MarkScrollUp
:
4316 case CK_MarkColumnScrollUp
:
4318 case CK_MarkScrollDown
:
4319 case CK_MarkColumnScrollDown
:
4320 edit
->search_start
= edit
->curs1
;
4321 edit
->found_len
= 0;
4324 edit
->found_len
= 0;
4325 edit
->prev_col
= edit_get_col (edit
);
4326 edit
->search_start
= edit
->curs1
;
4328 edit_find_bracket (edit
);
4330 if (option_auto_para_formatting
)
4336 case CK_DeleteToWordBegin
:
4337 case CK_DeleteToWordEnd
:
4338 case CK_DeleteToHome
:
4339 case CK_DeleteToEnd
:
4340 format_paragraph (edit
, 0);
4341 edit
->force
|= REDRAW_PAGE
;
4346 /* --------------------------------------------------------------------------------------------- */
4349 edit_stack_init (void)
4351 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4353 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
4354 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4357 edit_stack_iterator
= 0;
4360 /* --------------------------------------------------------------------------------------------- */
4363 edit_stack_free (void)
4365 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4366 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
4369 /* --------------------------------------------------------------------------------------------- */
4373 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
4375 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4378 /* --------------------------------------------------------------------------------------------- */
4382 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
4384 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4387 /* --------------------------------------------------------------------------------------------- */
4390 edit_unlock_file (WEdit
* edit
)
4395 fullpath
= mc_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4396 ret
= unlock_file (fullpath
);
4402 /* --------------------------------------------------------------------------------------------- */
4405 edit_lock_file (WEdit
* edit
)
4410 fullpath
= mc_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4411 ret
= lock_file (fullpath
);
4417 /* --------------------------------------------------------------------------------------------- */