2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2008, 2009, 2010, 2011, 2012
6 The Free Software Foundation, Inc.
10 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
11 Andrew Borodin <aborodin@vmail.ru> 2012.
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 * \brief Source: editor low level data handling and cursor fundamentals
38 #include <sys/types.h>
47 #include "lib/global.h"
49 #include "lib/tty/color.h"
50 #include "lib/tty/tty.h" /* attrset() */
51 #include "lib/tty/key.h" /* is_idle() */
52 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
53 #include "lib/vfs/vfs.h"
54 #include "lib/strutil.h" /* utf string functions */
55 #include "lib/util.h" /* load_file_position(), save_file_position() */
56 #include "lib/timefmt.h" /* time formatting */
58 #include "lib/widget.h"
61 #include "lib/charsets.h" /* get_codepage_id */
64 #include "src/filemanager/cmd.h" /* view_other_cmd() */
65 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
67 #include "src/setup.h" /* option_tab_spacing */
68 #include "src/main.h" /* macro_index */
69 #include "src/learn.h" /* learn_keys */
70 #include "src/keybind-defaults.h"
72 #include "edit-impl.h"
73 #include "editwidget.h"
75 /*** global variables ****************************************************************************/
77 int option_word_wrap_line_length
= DEFAULT_WRAP_LINE_LENGTH
;
78 int option_typewriter_wrap
= 0;
79 int option_auto_para_formatting
= 0;
80 int option_fill_tabs_with_spaces
= 0;
81 int option_return_does_auto_indent
= 1;
82 int option_backspace_through_tabs
= 0;
83 int option_fake_half_tabs
= 1;
84 int option_save_mode
= EDIT_QUICK_SAVE
;
85 int option_save_position
= 1;
86 int option_max_undo
= 32768;
87 int option_persistent_selections
= 1;
88 int option_cursor_beyond_eol
= 0;
89 int option_line_state
= 0;
90 int option_line_state_width
= 0;
92 int option_edit_right_extreme
= 0;
93 int option_edit_left_extreme
= 0;
94 int option_edit_top_extreme
= 0;
95 int option_edit_bottom_extreme
= 0;
96 int enable_show_tabs_tws
= 1;
97 int option_check_nl_at_eof
= 0;
98 int option_group_undo
= 0;
99 int show_right_margin
= 0;
101 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
102 char *option_backup_ext
= NULL
;
104 unsigned int edit_stack_iterator
= 0;
105 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
106 /* magic sequense for say than block is vertical */
107 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
109 /*** file scope macro definitions ****************************************************************/
111 #define TEMP_BUF_LEN 1024
113 #define space_width 1
115 /*** file scope type declarations ****************************************************************/
117 /*** file scope variables ************************************************************************/
119 /* detecting an error on save is easy: just check if every byte has been written. */
120 /* detecting an error on read, is not so easy 'cos there is not way to tell
121 whether you read everything or not. */
122 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
123 static const struct edit_filters
125 const char *read
, *write
, *extension
;
129 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
130 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
131 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
132 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
133 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
137 static off_t last_bracket
= -1;
139 /*** file scope functions ************************************************************************/
140 /* --------------------------------------------------------------------------------------------- */
144 * here's a quick sketch of the layout: (don't run this through indent.)
146 * (b1 is buffers1 and b2 is buffers2)
149 * \0\0\0\0\0m e _ f i l e . \nf i n . \n|T h i s _ i s _ s o\0\0\0\0\0\0\0\0\0
150 * ______________________________________|______________________________________
152 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
153 * |-> |-> |-> |-> |-> |-> |
155 * _<------------------------->|<----------------->_
156 * WEdit->curs2 | WEdit->curs1
161 * file end|||file beginning
170 /* --------------------------------------------------------------------------------------------- */
175 const char *header
= N_("About");
176 const char *button_name
= N_("&OK");
177 const char *const version
= "MCEdit " VERSION
;
178 char text
[BUF_LARGE
];
180 int win_len
, version_len
, button_len
;
187 button_name
= _(button_name
);
190 button_len
= str_term_width1 (button_name
) + 5;
191 version_len
= str_term_width1 (version
);
193 g_snprintf (text
, sizeof (text
),
194 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
195 " A user friendly text editor\n"
196 " written for the Midnight Commander"));
198 win_len
= str_term_width1 (header
);
199 win_len
= max (win_len
, version_len
);
200 win_len
= max (win_len
, button_len
);
202 /* count width and height of text */
203 str_msg_term_size (text
, &lines
, &cols
);
205 cols
= max (win_len
, cols
) + 6;
208 about_dlg
= create_dlg (TRUE
, 0, 0, lines
, cols
, dialog_colors
, NULL
,
209 "[Internal File Editor]", header
, DLG_CENTER
| DLG_TRYUP
);
211 add_widget (about_dlg
, label_new (3, (cols
- version_len
) / 2, version
));
212 add_widget (about_dlg
, label_new (5, 3, text
));
213 add_widget (about_dlg
, button_new (lines
- 3, (cols
- button_len
) / 2,
214 B_ENTER
, NORMAL_BUTTON
, button_name
, NULL
));
217 destroy_dlg (about_dlg
);
220 /* --------------------------------------------------------------------------------------------- */
222 * Initialize the buffers for an empty files.
226 edit_init_buffers (WEdit
* edit
)
230 for (j
= 0; j
<= MAXBUFF
; j
++)
232 edit
->buffers1
[j
] = NULL
;
233 edit
->buffers2
[j
] = NULL
;
238 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
241 /* --------------------------------------------------------------------------------------------- */
244 * Load file OR text into buffers. Set cursor to the beginning of file.
246 * @returns FALSE on error.
250 edit_load_file_fast (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
254 gboolean ret
= FALSE
;
256 edit
->curs2
= edit
->last_byte
;
257 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
259 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
262 gchar
*errmsg
, *filename
;
264 filename
= vfs_path_to_str (filename_vpath
);
265 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
267 edit_error_dialog (_("Error"), errmsg
);
272 if (!edit
->buffers2
[buf2
])
273 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
278 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
279 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
282 for (buf
= buf2
- 1; buf
>= 0; buf
--)
284 /* edit->buffers2[0] is already allocated */
285 if (!edit
->buffers2
[buf
])
286 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
287 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
296 gchar
*errmsg
, *filename
;
298 filename
= vfs_path_to_str (filename_vpath
);
299 errmsg
= g_strdup_printf (_("Error reading %s"), filename
);
301 edit_error_dialog (_("Error"), errmsg
);
308 /* --------------------------------------------------------------------------------------------- */
309 /** Return index of the filter or -1 is there is no appropriate filter */
312 edit_find_filter (const vfs_path_t
* filename_vpath
)
317 if (filename_vpath
== NULL
)
320 filename
= vfs_path_to_str (filename_vpath
);
321 l
= strlen (filename
);
322 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
324 e
= strlen (all_filters
[i
].extension
);
326 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
336 /* --------------------------------------------------------------------------------------------- */
339 edit_get_filter (const vfs_path_t
* filename_vpath
)
342 char *p
, *quoted_name
, *filename
;
344 i
= edit_find_filter (filename_vpath
);
348 filename
= vfs_path_to_str (filename_vpath
);
349 quoted_name
= name_quote (filename
, 0);
351 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
352 g_free (quoted_name
);
356 /* --------------------------------------------------------------------------------------------- */
359 edit_insert_stream (WEdit
* edit
, FILE * f
)
363 while ((c
= fgetc (f
)) >= 0)
365 edit_insert (edit
, c
);
371 /* --------------------------------------------------------------------------------------------- */
373 * Open file and create it if necessary.
375 * @param edit editor object
376 * @param filename_vpath file name
377 * @param st buffer for store stat info
378 * @returns TRUE for success, FALSE for error.
382 check_file_access (WEdit
* edit
, const vfs_path_t
* filename_vpath
, struct stat
*st
)
385 gchar
*errmsg
= NULL
;
387 /* Try opening an existing file */
388 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
392 * Try creating the file. O_EXCL prevents following broken links
393 * and opening existing files.
395 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
400 filename
= vfs_path_to_str (filename_vpath
);
401 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
406 /* New file, delete it if it's not modified or saved */
407 edit
->delete_file
= 1;
410 /* Check what we have opened */
411 if (mc_fstat (file
, st
) < 0)
415 filename
= vfs_path_to_str (filename_vpath
);
416 errmsg
= g_strdup_printf (_("Cannot get size/permissions for %s"), filename
);
421 /* We want to open regular files only */
422 if (!S_ISREG (st
->st_mode
))
426 filename
= vfs_path_to_str (filename_vpath
);
427 errmsg
= g_strdup_printf (_("\"%s\" is not a regular file"), filename
);
433 * Don't delete non-empty files.
434 * O_EXCL should prevent it, but let's be on the safe side.
437 edit
->delete_file
= 0;
439 if (st
->st_size
>= SIZE_LIMIT
)
443 filename
= vfs_path_to_str (filename_vpath
);
444 errmsg
= g_strdup_printf (_("File \"%s\" is too large"), filename
);
449 (void) mc_close (file
);
453 edit_error_dialog (_("Error"), errmsg
);
460 /* --------------------------------------------------------------------------------------------- */
463 * Open the file and load it into the buffers, either directly or using
464 * a filter. Return TRUE on success, FALSE on error.
466 * Fast loading (edit_load_file_fast) is used when the file size is
467 * known. In this case the data is read into the buffers by blocks.
468 * If the file size is not known, the data is loaded byte by byte in
471 * @param edit editor object
472 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
475 edit_load_file (WEdit
* edit
)
477 gboolean fast_load
= TRUE
;
479 /* Cannot do fast load if a filter is used */
480 if (edit_find_filter (edit
->filename_vpath
) >= 0)
484 * FIXME: line end translation should disable fast loading as well
485 * Consider doing fseek() to the end and ftell() for the real size.
487 if (edit
->filename_vpath
!= NULL
)
490 * VFS may report file size incorrectly, and slow load is not a big
491 * deal considering overhead in VFS.
493 if (!vfs_file_is_local (edit
->filename_vpath
))
496 /* If we are dealing with a real file, check that it exists */
497 if (!check_file_access (edit
, edit
->filename_vpath
, &edit
->stat1
))
502 /* nothing to load */
506 edit_init_buffers (edit
);
510 edit
->last_byte
= edit
->stat1
.st_size
;
511 edit_load_file_fast (edit
, edit
->filename_vpath
);
512 /* If fast load was used, the number of lines wasn't calculated */
513 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
518 if (edit
->filename_vpath
!= NULL
519 && *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) != '\0')
521 edit
->undo_stack_disable
= 1;
522 if (edit_insert_file (edit
, edit
->filename_vpath
) < 0)
527 edit
->undo_stack_disable
= 0;
534 /* --------------------------------------------------------------------------------------------- */
535 /** Restore saved cursor position in the file */
538 edit_load_position (WEdit
* edit
)
543 if (edit
->filename_vpath
== NULL
544 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
547 load_file_position (edit
->filename_vpath
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
551 edit_move_to_line (edit
, line
- 1);
552 edit
->prev_col
= column
;
556 edit_cursor_move (edit
, offset
);
557 line
= edit
->curs_line
;
558 edit
->search_start
= edit
->curs1
;
561 book_mark_restore (edit
, BOOK_MARK_COLOR
);
563 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
564 edit_move_display (edit
, line
- (edit
->widget
.lines
/ 2));
567 /* --------------------------------------------------------------------------------------------- */
568 /** Save cursor position in the file */
571 edit_save_position (WEdit
* edit
)
573 if (edit
->filename_vpath
== NULL
574 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
577 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
578 save_file_position (edit
->filename_vpath
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
,
579 edit
->serialized_bookmarks
);
580 edit
->serialized_bookmarks
= NULL
;
583 /* --------------------------------------------------------------------------------------------- */
584 /** Clean the WEdit stricture except the widget part */
587 edit_purge_widget (WEdit
* edit
)
589 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
590 char *start
= (char *) edit
+ sizeof (Widget
);
591 memset (start
, 0, len
);
594 /* --------------------------------------------------------------------------------------------- */
597 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
598 then the file should be as it was when he loaded up. Then set edit->modified to 0.
602 edit_pop_undo_action (WEdit
* edit
)
605 unsigned long sp
= edit
->undo_stack_pointer
;
607 if (sp
== edit
->undo_stack_bottom
)
610 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
611 c
= edit
->undo_stack
[sp
];
614 /* edit->undo_stack[sp] = '@'; */
615 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
619 if (sp
== edit
->undo_stack_bottom
)
622 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
623 if (edit
->undo_stack
[sp
] == -2)
625 /* edit->undo_stack[sp] = '@'; */
626 edit
->undo_stack_pointer
= sp
;
629 edit
->undo_stack
[sp
]++;
635 edit_pop_redo_action (WEdit
* edit
)
638 unsigned long sp
= edit
->redo_stack_pointer
;
640 if (sp
== edit
->redo_stack_bottom
)
643 sp
= (sp
- 1) & edit
->redo_stack_size_mask
;
644 c
= edit
->redo_stack
[sp
];
647 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
651 if (sp
== edit
->redo_stack_bottom
)
654 c
= edit
->redo_stack
[(sp
- 1) & edit
->redo_stack_size_mask
];
655 if (edit
->redo_stack
[sp
] == -2)
656 edit
->redo_stack_pointer
= sp
;
658 edit
->redo_stack
[sp
]++;
664 get_prev_undo_action (WEdit
* edit
)
667 unsigned long sp
= edit
->undo_stack_pointer
;
669 if (sp
== edit
->undo_stack_bottom
)
672 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
673 c
= edit
->undo_stack
[sp
];
677 if (sp
== edit
->undo_stack_bottom
)
680 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
684 /* --------------------------------------------------------------------------------------------- */
685 /** is called whenever a modification is made by one of the four routines below */
688 edit_modification (WEdit
* edit
)
690 edit
->caches_valid
= FALSE
;
692 /* raise lock when file modified */
693 if (!edit
->modified
&& !edit
->delete_file
)
694 edit
->locked
= lock_file (edit
->filename_vpath
);
698 /* --------------------------------------------------------------------------------------------- */
702 edit_get_byte_ptr (WEdit
* edit
, off_t byte_index
)
704 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
707 if (byte_index
>= edit
->curs1
)
711 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
712 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
713 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
716 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
717 (byte_index
& M_EDIT_BUF_SIZE
));
721 /* --------------------------------------------------------------------------------------------- */
725 edit_get_prev_utf (WEdit
* edit
, off_t byte_index
, int *char_width
)
728 gchar utf8_buf
[3 * UTF8_CHAR_LEN
+ 1];
730 gchar
*cursor_buf_ptr
;
732 if (byte_index
> (edit
->curs1
+ edit
->curs2
) || byte_index
<= 0)
738 for (i
= 0; i
< (3 * UTF8_CHAR_LEN
); i
++)
739 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
- (2 * UTF8_CHAR_LEN
));
740 utf8_buf
[3 * UTF8_CHAR_LEN
] = '\0';
742 cursor_buf_ptr
= utf8_buf
+ (2 * UTF8_CHAR_LEN
);
743 str
= g_utf8_find_prev_char (utf8_buf
, cursor_buf_ptr
);
745 if (str
== NULL
|| g_utf8_next_char (str
) != cursor_buf_ptr
)
748 return *(cursor_buf_ptr
- 1);
752 res
= g_utf8_get_char_validated (str
, -1);
757 return *(cursor_buf_ptr
- 1);
761 *char_width
= cursor_buf_ptr
- str
;
768 /* --------------------------------------------------------------------------------------------- */
771 edit_backspace (WEdit
* edit
, const int byte_delete
)
777 if (edit
->curs1
== 0)
780 if (edit
->mark2
!= edit
->mark1
)
781 edit_push_markers (edit
);
784 if (edit
->utf8
&& byte_delete
== 0)
786 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
794 for (i
= 1; i
<= cw
; i
++)
796 if (edit
->mark1
>= edit
->curs1
)
799 edit
->end_mark_curs
--;
801 if (edit
->mark2
>= edit
->curs1
)
803 if (edit
->last_get_rule
>= edit
->curs1
)
804 edit
->last_get_rule
--;
806 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
807 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
808 if (((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
) == 0)
810 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
811 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
815 edit_push_undo_action (edit
, p
);
817 edit_modification (edit
);
820 book_mark_dec (edit
, edit
->curs_line
);
823 edit
->force
|= REDRAW_AFTER_CURSOR
;
826 if (edit
->curs1
< edit
->start_display
)
828 edit
->start_display
--;
836 /* --------------------------------------------------------------------------------------------- */
837 /* high level cursor movement commands */
838 /* --------------------------------------------------------------------------------------------- */
839 /** check whether cursor is in indent part of line
841 * @param edit editor object
843 * @return TRUE if cursor is in indent, FALSE otherwise
847 is_in_indent (WEdit
* edit
)
851 for (p
= edit_bol (edit
, edit
->curs1
); p
< edit
->curs1
; p
++)
852 if (strchr (" \t", edit_get_byte (edit
, p
)) == NULL
)
858 /* --------------------------------------------------------------------------------------------- */
859 /** check whether line in editor is blank or not
861 * @param edit editor object
862 * @param offset position in file
864 * @return TRUE if line in blank, FALSE otherwise
868 is_blank (WEdit
* edit
, off_t offset
)
873 s
= edit_bol (edit
, offset
);
874 f
= edit_eol (edit
, offset
) - 1;
877 c
= edit_get_byte (edit
, s
++);
884 /* --------------------------------------------------------------------------------------------- */
885 /** returns the offset of line i */
888 edit_find_line (WEdit
* edit
, long line
)
891 long m
= 2000000000; /* what is the magic number? */
893 if (!edit
->caches_valid
)
895 memset (edit
->line_numbers
, 0, sizeof (edit
->line_numbers
));
896 memset (edit
->line_offsets
, 0, sizeof (edit
->line_offsets
));
897 /* three offsets that we *know* are line 0 at 0 and these two: */
898 edit
->line_numbers
[1] = edit
->curs_line
;
899 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
900 edit
->line_numbers
[2] = edit
->total_lines
;
901 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
902 edit
->caches_valid
= TRUE
;
904 if (line
>= edit
->total_lines
)
905 return edit
->line_offsets
[2];
908 /* find the closest known point */
909 for (i
= 0; i
< N_LINE_CACHES
; i
++)
913 n
= abs (edit
->line_numbers
[i
] - line
);
921 return edit
->line_offsets
[j
]; /* know the offset exactly */
922 if (m
== 1 && j
>= 3)
923 i
= j
; /* one line different - caller might be looping, so stay in this cache */
925 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
926 if (line
> edit
->line_numbers
[j
])
927 edit
->line_offsets
[i
] =
928 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
930 edit
->line_offsets
[i
] =
931 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
932 edit
->line_numbers
[i
] = line
;
933 return edit
->line_offsets
[i
];
936 /* --------------------------------------------------------------------------------------------- */
937 /** moves up until a blank line is reached, or until just
938 before a non-blank line is reached */
941 edit_move_up_paragraph (WEdit
* edit
, gboolean do_scroll
)
945 if (edit
->curs_line
> 1)
947 if (!line_is_blank (edit
, edit
->curs_line
))
949 for (i
= edit
->curs_line
- 1; i
!= 0; i
--)
950 if (line_is_blank (edit
, i
))
953 else if (line_is_blank (edit
, edit
->curs_line
- 1))
955 for (i
= edit
->curs_line
- 1; i
!= 0; i
--)
956 if (!line_is_blank (edit
, i
))
964 for (i
= edit
->curs_line
- 1; i
!= 0; i
--)
965 if (line_is_blank (edit
, i
))
969 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
972 /* --------------------------------------------------------------------------------------------- */
973 /** moves down until a blank line is reached, or until just
974 before a non-blank line is reached */
977 edit_move_down_paragraph (WEdit
* edit
, gboolean do_scroll
)
981 if (edit
->curs_line
>= edit
->total_lines
- 1)
982 i
= edit
->total_lines
;
983 else if (!line_is_blank (edit
, edit
->curs_line
))
985 for (i
= edit
->curs_line
+ 1; i
; i
++)
986 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
989 else if (line_is_blank (edit
, edit
->curs_line
+ 1))
991 for (i
= edit
->curs_line
+ 1; i
; i
++)
992 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
1000 for (i
= edit
->curs_line
+ 1; i
; i
++)
1001 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1005 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
1008 /* --------------------------------------------------------------------------------------------- */
1011 edit_begin_page (WEdit
* edit
)
1013 edit_update_curs_row (edit
);
1014 edit_move_up (edit
, edit
->curs_row
, 0);
1017 /* --------------------------------------------------------------------------------------------- */
1020 edit_end_page (WEdit
* edit
)
1022 edit_update_curs_row (edit
);
1023 edit_move_down (edit
, edit
->widget
.lines
- edit
->curs_row
- 1, 0);
1027 /* --------------------------------------------------------------------------------------------- */
1028 /** goto beginning of text */
1031 edit_move_to_top (WEdit
* edit
)
1033 if (edit
->curs_line
)
1035 edit_cursor_move (edit
, -edit
->curs1
);
1036 edit_move_to_prev_col (edit
, 0);
1037 edit
->force
|= REDRAW_PAGE
;
1038 edit
->search_start
= 0;
1039 edit_update_curs_row (edit
);
1044 /* --------------------------------------------------------------------------------------------- */
1045 /** goto end of text */
1048 edit_move_to_bottom (WEdit
* edit
)
1050 if (edit
->curs_line
< edit
->total_lines
)
1052 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
1053 edit
->start_display
= edit
->last_byte
;
1054 edit
->start_line
= edit
->total_lines
;
1055 edit_scroll_upward (edit
, edit
->widget
.lines
- 1);
1056 edit
->force
|= REDRAW_PAGE
;
1060 /* --------------------------------------------------------------------------------------------- */
1061 /** goto beginning of line */
1064 edit_cursor_to_bol (WEdit
* edit
)
1066 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1067 edit
->search_start
= edit
->curs1
;
1068 edit
->prev_col
= edit_get_col (edit
);
1072 /* --------------------------------------------------------------------------------------------- */
1073 /** goto end of line */
1076 edit_cursor_to_eol (WEdit
* edit
)
1078 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1079 edit
->search_start
= edit
->curs1
;
1080 edit
->prev_col
= edit_get_col (edit
);
1084 /* --------------------------------------------------------------------------------------------- */
1086 static unsigned long
1091 const char option_chars_move_whole_word
[] =
1092 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1098 if (*option_chars_move_whole_word
== '!')
1100 return 0x80000000UL
;
1102 if (g_ascii_isupper ((gchar
) c
))
1104 else if (g_ascii_islower ((gchar
) c
))
1106 else if (g_ascii_isalpha (c
))
1108 else if (isdigit (c
))
1110 else if (isspace (c
))
1112 q
= strchr (option_chars_move_whole_word
, c
);
1114 return 0xFFFFFFFFUL
;
1117 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1122 while ((q
= strchr (q
+ 1, c
)));
1126 /* --------------------------------------------------------------------------------------------- */
1129 edit_left_word_move (WEdit
* edit
, int s
)
1134 if (edit
->column_highlight
1135 && edit
->mark1
!= edit
->mark2
1136 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1138 edit_cursor_move (edit
, -1);
1141 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1142 c2
= edit_get_byte (edit
, edit
->curs1
);
1143 if (c1
== '\n' || c2
== '\n')
1145 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1147 if (isspace (c1
) && !isspace (c2
))
1150 if (!isspace (c1
) && isspace (c2
))
1155 /* --------------------------------------------------------------------------------------------- */
1158 edit_left_word_move_cmd (WEdit
* edit
)
1160 edit_left_word_move (edit
, 0);
1161 edit
->force
|= REDRAW_PAGE
;
1164 /* --------------------------------------------------------------------------------------------- */
1167 edit_right_word_move (WEdit
* edit
, int s
)
1172 if (edit
->column_highlight
1173 && edit
->mark1
!= edit
->mark2
1174 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
1176 edit_cursor_move (edit
, 1);
1177 if (edit
->curs1
>= edit
->last_byte
)
1179 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1180 c2
= edit_get_byte (edit
, edit
->curs1
);
1181 if (c1
== '\n' || c2
== '\n')
1183 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1185 if (isspace (c1
) && !isspace (c2
))
1188 if (!isspace (c1
) && isspace (c2
))
1193 /* --------------------------------------------------------------------------------------------- */
1196 edit_right_word_move_cmd (WEdit
* edit
)
1198 edit_right_word_move (edit
, 0);
1199 edit
->force
|= REDRAW_PAGE
;
1202 /* --------------------------------------------------------------------------------------------- */
1205 edit_right_char_move_cmd (WEdit
* edit
)
1213 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
1220 c
= edit_get_byte (edit
, edit
->curs1
);
1222 if (option_cursor_beyond_eol
&& c
== '\n')
1228 edit_cursor_move (edit
, cw
);
1232 /* --------------------------------------------------------------------------------------------- */
1235 edit_left_char_move_cmd (WEdit
* edit
)
1239 if (edit
->column_highlight
1240 && option_cursor_beyond_eol
1241 && edit
->mark1
!= edit
->mark2
1242 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1248 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1253 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1259 edit_cursor_move (edit
, -cw
);
1263 /* --------------------------------------------------------------------------------------------- */
1264 /** Up or down cursor moving.
1265 direction = TRUE - move up
1270 edit_move_updown (WEdit
* edit
, long lines
, gboolean do_scroll
, gboolean direction
)
1273 long l
= direction
? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
1282 edit
->force
|= REDRAW_PAGE
;
1286 edit_scroll_upward (edit
, lines
);
1288 edit_scroll_downward (edit
, lines
);
1290 p
= edit_bol (edit
, edit
->curs1
);
1292 p
= direction
? edit_move_backward (edit
, p
, lines
) : edit_move_forward (edit
, p
, lines
, 0);
1294 edit_cursor_move (edit
, p
- edit
->curs1
);
1296 edit_move_to_prev_col (edit
, p
);
1298 /* search start of current multibyte char (like CJK) */
1299 if (edit
->curs1
+ 1 < edit
->last_byte
)
1301 edit_right_char_move_cmd (edit
);
1302 edit_left_char_move_cmd (edit
);
1305 edit
->search_start
= edit
->curs1
;
1306 edit
->found_len
= 0;
1309 /* --------------------------------------------------------------------------------------------- */
1312 edit_right_delete_word (WEdit
* edit
)
1317 if (edit
->curs1
>= edit
->last_byte
)
1319 c1
= edit_delete (edit
, 1);
1320 c2
= edit_get_byte (edit
, edit
->curs1
);
1321 if (c1
== '\n' || c2
== '\n')
1323 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1325 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1330 /* --------------------------------------------------------------------------------------------- */
1333 edit_left_delete_word (WEdit
* edit
)
1338 if (edit
->curs1
<= 0)
1340 c1
= edit_backspace (edit
, 1);
1341 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1342 if (c1
== '\n' || c2
== '\n')
1344 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1346 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1351 /* --------------------------------------------------------------------------------------------- */
1353 the start column position is not recorded, and hence does not
1354 undo as it happed. But who would notice.
1358 edit_do_undo (WEdit
* edit
)
1363 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1365 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1372 edit_cursor_move (edit
, 1);
1375 edit_cursor_move (edit
, -1);
1379 edit_backspace (edit
, 1);
1383 edit_delete (edit
, 1);
1386 edit
->column_highlight
= 1;
1389 edit
->column_highlight
= 0;
1392 if (ac
>= 256 && ac
< 512)
1393 edit_insert_ahead (edit
, ac
- 256);
1394 if (ac
>= 0 && ac
< 256)
1395 edit_insert (edit
, ac
);
1397 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1399 edit
->mark1
= ac
- MARK_1
;
1401 (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1403 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1405 edit
->mark2
= ac
- MARK_2
;
1407 (long) 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;
1434 /* --------------------------------------------------------------------------------------------- */
1437 edit_do_redo (WEdit
* edit
)
1442 if (edit
->redo_stack_reset
)
1446 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1453 edit_cursor_move (edit
, 1);
1456 edit_cursor_move (edit
, -1);
1459 edit_backspace (edit
, 1);
1462 edit_delete (edit
, 1);
1465 edit
->column_highlight
= 1;
1468 edit
->column_highlight
= 0;
1471 if (ac
>= 256 && ac
< 512)
1472 edit_insert_ahead (edit
, ac
- 256);
1473 if (ac
>= 0 && ac
< 256)
1474 edit_insert (edit
, ac
);
1476 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1478 edit
->mark1
= ac
- MARK_1
;
1480 (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1482 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1484 edit
->mark2
= ac
- MARK_2
;
1486 (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1488 /* more than one pop usually means something big */
1490 edit
->force
|= REDRAW_PAGE
;
1493 if (edit
->start_display
> ac
- KEY_PRESS
)
1495 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1496 edit
->force
|= REDRAW_PAGE
;
1498 else if (edit
->start_display
< ac
- KEY_PRESS
)
1500 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1501 edit
->force
|= REDRAW_PAGE
;
1503 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1504 edit_update_curs_row (edit
);
1510 /* --------------------------------------------------------------------------------------------- */
1513 edit_group_undo (WEdit
* edit
)
1515 long ac
= KEY_PRESS
;
1516 long cur_ac
= KEY_PRESS
;
1517 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1519 cur_ac
= get_prev_undo_action (edit
);
1520 edit_do_undo (edit
);
1521 ac
= get_prev_undo_action (edit
);
1522 /* exit from cycle if option_group_undo is not set,
1523 * and make single UNDO operation
1525 if (!option_group_undo
)
1530 /* --------------------------------------------------------------------------------------------- */
1533 edit_delete_to_line_end (WEdit
* edit
)
1535 while (edit_get_byte (edit
, edit
->curs1
) != '\n' && edit
->curs2
!= 0)
1536 edit_delete (edit
, 1);
1539 /* --------------------------------------------------------------------------------------------- */
1542 edit_delete_to_line_begin (WEdit
* edit
)
1544 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
!= 0)
1545 edit_backspace (edit
, 1);
1548 /* --------------------------------------------------------------------------------------------- */
1551 is_aligned_on_a_tab (WEdit
* edit
)
1555 edit_update_curs_col (edit
);
1556 curs_col
= edit
->curs_col
% (TAB_SIZE
* space_width
);
1557 return (curs_col
== 0 || curs_col
== (HALF_TAB_SIZE
* space_width
));
1560 /* --------------------------------------------------------------------------------------------- */
1563 right_of_four_spaces (WEdit
* edit
)
1567 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1568 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1570 return (ch
== ' ' && is_aligned_on_a_tab (edit
));
1573 /* --------------------------------------------------------------------------------------------- */
1576 left_of_four_spaces (WEdit
* edit
)
1580 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1581 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1583 return (ch
== ' ' && is_aligned_on_a_tab (edit
));
1586 /* --------------------------------------------------------------------------------------------- */
1589 edit_auto_indent (WEdit
* edit
)
1594 /* use the previous line as a template */
1595 p
= edit_move_backward (edit
, p
, 1);
1596 /* copy the leading whitespace of the line */
1598 { /* no range check - the line _is_ \n-terminated */
1599 c
= edit_get_byte (edit
, p
++);
1600 if (c
!= ' ' && c
!= '\t')
1602 edit_insert (edit
, c
);
1606 /* --------------------------------------------------------------------------------------------- */
1609 edit_double_newline (WEdit
* edit
)
1611 edit_insert (edit
, '\n');
1612 if (edit_get_byte (edit
, edit
->curs1
) == '\n' || edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1614 edit
->force
|= REDRAW_PAGE
;
1615 edit_insert (edit
, '\n');
1619 /* --------------------------------------------------------------------------------------------- */
1622 insert_spaces_tab (WEdit
* edit
, gboolean half
)
1626 edit_update_curs_col (edit
);
1627 i
= option_tab_spacing
* space_width
;
1630 i
= ((edit
->curs_col
/ i
) + 1) * i
- edit
->curs_col
;
1633 edit_insert (edit
, ' ');
1638 /* --------------------------------------------------------------------------------------------- */
1641 edit_tab_cmd (WEdit
* edit
)
1643 if (option_fake_half_tabs
&& is_in_indent (edit
))
1645 /*insert a half tab (usually four spaces) unless there is a
1646 half tab already behind, then delete it and insert a
1648 if (option_fill_tabs_with_spaces
|| !right_of_four_spaces (edit
))
1649 insert_spaces_tab (edit
, TRUE
);
1654 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1655 edit_backspace (edit
, 1);
1656 edit_insert (edit
, '\t');
1659 else if (option_fill_tabs_with_spaces
)
1660 insert_spaces_tab (edit
, FALSE
);
1662 edit_insert (edit
, '\t');
1665 /* --------------------------------------------------------------------------------------------- */
1668 check_and_wrap_line (WEdit
* edit
)
1673 if (!option_typewriter_wrap
)
1675 edit_update_curs_col (edit
);
1676 if (edit
->curs_col
< option_word_wrap_line_length
)
1682 c
= edit_get_byte (edit
, curs
);
1683 if (c
== '\n' || curs
<= 0)
1685 edit_insert (edit
, '\n');
1688 if (c
== ' ' || c
== '\t')
1690 off_t current
= edit
->curs1
;
1691 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
1692 edit_insert (edit
, '\n');
1693 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
1699 /* --------------------------------------------------------------------------------------------- */
1700 /** this find the matching bracket in either direction, and sets edit->bracket
1702 * @param edit editor object
1703 * @param in_screen seach only on the current screen
1704 * @param furthest_bracket_search count of the bytes for search
1706 * @return position of the found bracket (-1 if no match)
1710 edit_get_bracket (WEdit
* edit
, gboolean in_screen
, unsigned long furthest_bracket_search
)
1712 const char *const b
= "{}{[][()(", *p
;
1713 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
1714 unsigned long j
= 0;
1716 edit_update_curs_row (edit
);
1717 c
= edit_get_byte (edit
, edit
->curs1
);
1720 if (!furthest_bracket_search
)
1721 furthest_bracket_search
--;
1722 /* not on a bracket at all */
1725 /* the matching bracket */
1727 /* going left or right? */
1728 if (strchr ("{[(", c
))
1730 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
1732 /* out of buffer? */
1733 if (q
>= edit
->last_byte
|| q
< 0)
1735 a
= edit_get_byte (edit
, q
);
1736 /* don't want to eat CPU */
1737 if (j
++ > furthest_bracket_search
)
1739 /* out of screen? */
1742 if (q
< edit
->start_display
)
1744 /* count lines if searching downward */
1745 if (inc
> 0 && a
== '\n')
1746 if (n
++ >= edit
->widget
.lines
- edit
->curs_row
) /* out of screen */
1749 /* count bracket depth */
1750 i
+= (a
== c
) - (a
== d
);
1751 /* return if bracket depth is zero */
1759 /* --------------------------------------------------------------------------------------------- */
1762 edit_goto_matching_bracket (WEdit
* edit
)
1766 q
= edit_get_bracket (edit
, 0, 0);
1769 edit
->bracket
= edit
->curs1
;
1770 edit
->force
|= REDRAW_PAGE
;
1771 edit_cursor_move (edit
, q
- edit
->curs1
);
1775 /* --------------------------------------------------------------------------------------------- */
1778 edit_move_block_to_right (WEdit
* edit
)
1780 off_t start_mark
, end_mark
;
1781 long cur_bol
, start_bol
;
1783 if (eval_marks (edit
, &start_mark
, &end_mark
))
1786 start_bol
= edit_bol (edit
, start_mark
);
1787 cur_bol
= edit_bol (edit
, end_mark
- 1);
1791 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1792 if (!line_is_blank (edit
, edit
->curs_line
))
1794 if (option_fill_tabs_with_spaces
)
1795 insert_spaces_tab (edit
, option_fake_half_tabs
);
1797 edit_insert (edit
, '\t');
1798 edit_cursor_move (edit
, edit_bol (edit
, cur_bol
) - edit
->curs1
);
1804 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1806 while (cur_bol
>= start_bol
);
1808 edit
->force
|= REDRAW_PAGE
;
1811 /* --------------------------------------------------------------------------------------------- */
1814 edit_move_block_to_left (WEdit
* edit
)
1816 off_t start_mark
, end_mark
;
1817 off_t cur_bol
, start_bol
;
1820 if (eval_marks (edit
, &start_mark
, &end_mark
))
1823 start_bol
= edit_bol (edit
, start_mark
);
1824 cur_bol
= edit_bol (edit
, end_mark
- 1);
1831 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1833 if (option_fake_half_tabs
)
1834 del_tab_width
= HALF_TAB_SIZE
;
1836 del_tab_width
= option_tab_spacing
;
1838 next_char
= edit_get_byte (edit
, edit
->curs1
);
1839 if (next_char
== '\t')
1840 edit_delete (edit
, 1);
1841 else if (next_char
== ' ')
1842 for (i
= 1; i
<= del_tab_width
; i
++)
1844 if (next_char
== ' ')
1845 edit_delete (edit
, 1);
1846 next_char
= edit_get_byte (edit
, edit
->curs1
);
1852 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1854 while (cur_bol
>= start_bol
);
1856 edit
->force
|= REDRAW_PAGE
;
1859 /* --------------------------------------------------------------------------------------------- */
1861 * prints at the cursor
1862 * @returns the number of chars printed
1866 edit_print_string (WEdit
* e
, const char *s
)
1870 while (s
[i
] != '\0')
1871 edit_execute_cmd (e
, CK_InsertChar
, (unsigned char) s
[i
++]);
1872 e
->force
|= REDRAW_COMPLETELY
;
1873 edit_update_screen (e
);
1877 /* --------------------------------------------------------------------------------------------- */
1878 /*** public functions ****************************************************************************/
1879 /* --------------------------------------------------------------------------------------------- */
1881 /** User edit menu, like user menu (F2) but only in editor. */
1884 user_menu (WEdit
* edit
, const char *menu_file
, int selected_entry
)
1889 off_t start_mark
, end_mark
;
1891 vfs_path_t
*block_file_vpath
;
1893 block_file
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
1894 block_file_vpath
= vfs_path_from_str (block_file
);
1896 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
1898 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1900 /* run shell scripts from menu */
1901 if (user_menu_cmd (edit
, menu_file
, selected_entry
)
1902 && (mc_stat (block_file_vpath
, &status
) == 0) && (status
.st_size
!= 0))
1907 /* i.e. we have marked block */
1909 rc
= edit_block_delete_cmd (edit
);
1915 ins_len
= edit_insert_file (edit
, block_file_vpath
);
1916 if (nomark
== 0 && ins_len
> 0)
1917 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1919 /* truncate block file */
1920 fd
= fopen (block_file
, "w");
1924 edit_cursor_move (edit
, curs
- edit
->curs1
);
1925 edit_refresh_cmd (edit
);
1926 edit
->force
|= REDRAW_COMPLETELY
;
1928 g_free (block_file
);
1929 vfs_path_free (block_file_vpath
);
1932 /* --------------------------------------------------------------------------------------------- */
1935 edit_get_byte (WEdit
* edit
, off_t byte_index
)
1939 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1942 if (byte_index
>= edit
->curs1
)
1944 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1945 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
1948 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
1951 /* --------------------------------------------------------------------------------------------- */
1955 edit_get_utf (WEdit
* edit
, off_t byte_index
, int *char_width
)
1960 gchar
*next_ch
= NULL
;
1962 gchar utf8_buf
[UTF8_CHAR_LEN
+ 1];
1964 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1970 str
= edit_get_byte_ptr (edit
, byte_index
);
1978 res
= g_utf8_get_char_validated (str
, -1);
1982 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1984 for (i
= 0; i
< UTF8_CHAR_LEN
; i
++)
1985 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
);
1986 utf8_buf
[UTF8_CHAR_LEN
] = '\0';
1988 res
= g_utf8_get_char_validated (str
, -1);
1999 /* Calculate UTF-8 char width */
2000 next_ch
= g_utf8_next_char (str
);
2003 width
= next_ch
- str
;
2011 *char_width
= width
;
2016 /* --------------------------------------------------------------------------------------------- */
2019 edit_get_write_filter (const vfs_path_t
* write_name_vpath
, const vfs_path_t
* filename_vpath
)
2022 char *p
, *writename
;
2023 const vfs_path_element_t
*path_element
;
2025 i
= edit_find_filter (filename_vpath
);
2029 path_element
= vfs_path_get_by_index (write_name_vpath
, -1);
2030 writename
= name_quote (path_element
->path
, 0);
2031 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
2036 /* --------------------------------------------------------------------------------------------- */
2038 * @param edit editor object
2039 * @param f value of stream file
2040 * @returns the length of the file
2044 edit_write_stream (WEdit
* edit
, FILE * f
)
2048 if (edit
->lb
== LB_ASIS
)
2050 for (i
= 0; i
< edit
->last_byte
; i
++)
2051 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
2056 /* change line breaks */
2057 for (i
= 0; i
< edit
->last_byte
; i
++)
2059 unsigned char c
= edit_get_byte (edit
, i
);
2061 if (!(c
== '\n' || c
== '\r'))
2063 /* not line break */
2064 if (fputc (c
, f
) < 0)
2068 { /* (c == '\n' || c == '\r') */
2069 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
2073 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
2074 /* put one line break unconditionally */
2075 if (fputc ('\n', f
) < 0)
2078 i
++; /* 2 chars are processed */
2080 if (c
== '\r' && c1
== '\n')
2081 /* Windows line break; go to the next char */
2084 if (c
== '\r' && c1
== '\r')
2086 /* two Macintosh line breaks; put second line break */
2087 if (fputc ('\n', f
) < 0)
2092 if (fputc (c1
, f
) < 0)
2096 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
2097 /* put one line break unconditionally */
2098 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
2101 if (c
== '\r' && c1
== '\n')
2102 /* Windows line break; go to the next char */
2106 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
2107 /* put one line break unconditionally */
2108 if (fputc ('\r', f
) < 0)
2111 i
++; /* 2 chars are processed */
2113 if (c
== '\r' && c1
== '\n')
2114 /* Windows line break; go to the next char */
2117 if (c
== '\n' && c1
== '\n')
2119 /* two Windows line breaks; put second line break */
2120 if (fputc ('\r', f
) < 0)
2125 if (fputc (c1
, f
) < 0)
2128 case LB_ASIS
: /* default without changes */
2134 return edit
->last_byte
;
2137 /* --------------------------------------------------------------------------------------------- */
2138 /** inserts a file at the cursor, returns count of inserted bytes on success */
2140 edit_insert_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
2145 p
= edit_get_filter (filename_vpath
);
2149 off_t current
= edit
->curs1
;
2151 f
= (FILE *) popen (p
, "r");
2154 edit_insert_stream (edit
, f
);
2155 ins_len
= edit
->curs1
- current
;
2156 edit_cursor_move (edit
, -ins_len
);
2161 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
2162 edit_error_dialog (_("Error"), errmsg
);
2171 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
2172 edit_error_dialog (_("Error"), errmsg
);
2182 off_t current
= edit
->curs1
;
2183 int vertical_insertion
= 0;
2186 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
2190 buf
= g_malloc0 (TEMP_BUF_LEN
);
2191 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2194 /* if contain signature VERTICAL_MAGIC then it vertical block */
2195 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2196 vertical_insertion
= 1;
2198 mc_lseek (file
, 0, SEEK_SET
);
2201 if (vertical_insertion
)
2206 blocklen
= edit_insert_column_of_text_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
2207 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2208 /* highlight inserted text then not persistent blocks */
2209 if (!option_persistent_selections
)
2211 if (!edit
->column_highlight
)
2212 edit_push_undo_action (edit
, COLUMN_OFF
);
2213 edit
->column_highlight
= 1;
2220 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2222 for (i
= 0; i
< blocklen
; i
++)
2223 edit_insert (edit
, buf
[i
]);
2225 /* highlight inserted text then not persistent blocks */
2226 if (!option_persistent_selections
&& edit
->modified
)
2228 edit_set_markers (edit
, edit
->curs1
, current
, 0, 0);
2229 if (edit
->column_highlight
)
2230 edit_push_undo_action (edit
, COLUMN_ON
);
2231 edit
->column_highlight
= 0;
2235 edit
->force
|= REDRAW_PAGE
;
2236 ins_len
= edit
->curs1
- current
;
2237 edit_cursor_move (edit
, -ins_len
);
2247 /* --------------------------------------------------------------------------------------------- */
2249 * Fill in the edit structure. Return NULL on failure. Pass edit as
2250 * NULL to allocate a new structure.
2252 * If line is 0, try to restore saved position. Otherwise put the
2253 * cursor on that line and show it in the middle of the screen.
2257 edit_init (WEdit
* edit
, int y
, int x
, int lines
, int cols
, const vfs_path_t
* filename_vpath
,
2260 gboolean to_free
= FALSE
;
2262 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2263 if (option_line_state
)
2264 option_line_state_width
= LINE_STATE_WIDTH
;
2266 option_line_state_width
= 0;
2272 * Expand option_whole_chars_search by national letters using
2276 static char option_whole_chars_search_buf
[256];
2278 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
2281 size_t len
= str_term_width1 (option_whole_chars_search
);
2283 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
2285 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
2287 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
2289 option_whole_chars_search_buf
[len
++] = i
;
2293 option_whole_chars_search_buf
[len
] = 0;
2294 option_whole_chars_search
= option_whole_chars_search_buf
;
2296 #endif /* ENABLE_NLS */
2297 edit
= g_malloc0 (sizeof (WEdit
));
2298 edit
->search
= NULL
;
2302 edit_purge_widget (edit
);
2305 edit
->widget
.lines
= lines
;
2306 edit
->widget
.cols
= cols
;
2308 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2309 edit
->stat1
.st_uid
= getuid ();
2310 edit
->stat1
.st_gid
= getgid ();
2311 edit
->stat1
.st_mtime
= 0;
2315 edit
->force
|= REDRAW_PAGE
;
2316 edit_set_filename (edit
, filename_vpath
);
2318 edit
->undo_stack_size
= START_STACK_SIZE
;
2319 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2320 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2322 edit
->redo_stack_size
= START_STACK_SIZE
;
2323 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2324 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2328 edit
->converter
= str_cnv_from_term
;
2329 edit_set_codeset (edit
);
2332 if (!edit_load_file (edit
))
2334 /* edit_load_file already gives an error message */
2340 edit
->loading_done
= 1;
2343 edit_load_syntax (edit
, NULL
, NULL
);
2346 edit_get_syntax_color (edit
, -1, &color
);
2349 /* load saved cursor position */
2350 if ((line
== 0) && option_save_position
)
2351 edit_load_position (edit
);
2356 edit_move_display (edit
, line
- 1);
2357 edit_move_to_line (edit
, line
- 1);
2360 edit_load_macro_cmd (edit
);
2364 /* --------------------------------------------------------------------------------------------- */
2366 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2368 edit_clean (WEdit
* edit
)
2375 /* a stale lock, remove it */
2377 edit
->locked
= unlock_file (edit
->filename_vpath
);
2379 /* save cursor position */
2380 if (option_save_position
)
2381 edit_save_position (edit
);
2382 else if (edit
->serialized_bookmarks
!= NULL
)
2383 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2385 /* File specified on the mcedit command line and never saved */
2386 if (edit
->delete_file
)
2387 unlink (vfs_path_get_last_path_str (edit
->filename_vpath
));
2389 edit_free_syntax_rules (edit
);
2390 book_mark_flush (edit
, -1);
2391 for (; j
<= MAXBUFF
; j
++)
2393 g_free (edit
->buffers1
[j
]);
2394 g_free (edit
->buffers2
[j
]);
2397 g_free (edit
->undo_stack
);
2398 g_free (edit
->redo_stack
);
2399 vfs_path_free (edit
->filename_vpath
);
2400 vfs_path_free (edit
->dir_vpath
);
2401 mc_search_free (edit
->search
);
2402 edit
->search
= NULL
;
2405 if (edit
->converter
!= str_cnv_from_term
)
2406 str_close_conv (edit
->converter
);
2409 edit_purge_widget (edit
);
2414 /* --------------------------------------------------------------------------------------------- */
2416 /** returns TRUE on success */
2418 edit_renew (WEdit
* edit
)
2420 int y
= edit
->widget
.y
;
2421 int x
= edit
->widget
.x
;
2422 int lines
= edit
->widget
.lines
;
2423 int columns
= edit
->widget
.cols
;
2426 return (edit_init (edit
, y
, x
, lines
, columns
, NULL
, 0) != NULL
);
2429 /* --------------------------------------------------------------------------------------------- */
2432 * Load a new file into the editor and set line. If it fails, preserve the old file.
2433 * To do it, allocate a new widget, initialize it and, if the new file
2434 * was loaded, copy the data to the old widget.
2436 * @returns TRUE on success, FALSE on failure.
2439 edit_reload_line (WEdit
* edit
, const vfs_path_t
* filename_vpath
, long line
)
2442 int y
= edit
->widget
.y
;
2443 int x
= edit
->widget
.x
;
2444 int lines
= edit
->widget
.lines
;
2445 int columns
= edit
->widget
.cols
;
2447 e
= g_malloc0 (sizeof (WEdit
));
2448 e
->widget
= edit
->widget
;
2450 if (edit_init (e
, y
, x
, lines
, columns
, filename_vpath
, line
) == NULL
)
2457 memcpy (edit
, e
, sizeof (WEdit
));
2463 /* --------------------------------------------------------------------------------------------- */
2467 edit_set_codeset (WEdit
* edit
)
2472 get_codepage_id (mc_global
.source_codepage
>=
2473 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2478 conv
= str_crt_conv_from (cp_id
);
2479 if (conv
!= INVALID_CONV
)
2481 if (edit
->converter
!= str_cnv_from_term
)
2482 str_close_conv (edit
->converter
);
2483 edit
->converter
= conv
;
2488 edit
->utf8
= str_isutf8 (cp_id
);
2492 /* --------------------------------------------------------------------------------------------- */
2494 Recording stack for undo:
2495 The following is an implementation of a compressed stack. Identical
2496 pushes are recorded by a negative prefix indicating the number of times the
2497 same char was pushed. This saves space for repeated curs-left or curs-right
2514 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2515 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2516 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2517 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2520 The only way the cursor moves or the buffer is changed is through the routines:
2521 insert, backspace, insert_ahead, delete, and cursor_move.
2522 These record the reverse undo movements onto the stack each time they are
2525 Each key press results in a set of actions (insert; delete ...). So each time
2526 a key is pressed the current position of start_display is pushed as
2527 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2528 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2529 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2534 edit_push_undo_action (WEdit
* edit
, long c
, ...)
2536 unsigned long sp
= edit
->undo_stack_pointer
;
2540 /* first enlarge the stack if necessary */
2541 if (sp
> edit
->undo_stack_size
- 10)
2543 if (option_max_undo
< 256)
2544 option_max_undo
= 256;
2545 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2547 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2550 edit
->undo_stack
= t
;
2551 edit
->undo_stack_size
<<= 1;
2552 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2556 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2557 if (edit
->undo_stack_disable
)
2559 edit_push_redo_action (edit
, KEY_PRESS
);
2560 edit_push_redo_action (edit
, c
);
2563 if (edit
->redo_stack_reset
)
2564 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2566 if (edit
->undo_stack_bottom
!= sp
2567 && spm1
!= edit
->undo_stack_bottom
2568 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2572 if (edit
->undo_stack
[spm1
] < 0)
2574 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2575 if (d
== c
&& edit
->undo_stack
[spm1
] > -1000000000)
2577 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2578 edit
->undo_stack
[spm1
]--;
2584 d
= edit
->undo_stack
[spm1
];
2588 return; /* --> no need to push multiple do-nothings */
2589 edit
->undo_stack
[sp
] = -2;
2594 edit
->undo_stack
[sp
] = c
;
2597 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2599 /* if the sp wraps round and catches the undo_stack_bottom then erase
2600 * the first set of actions on the stack to make space - by moving
2601 * undo_stack_bottom forward one "key press" */
2602 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2603 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2604 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2607 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2609 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2610 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2612 /*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: */
2613 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2614 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2616 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2620 /* --------------------------------------------------------------------------------------------- */
2623 edit_push_redo_action (WEdit
* edit
, long c
, ...)
2625 unsigned long sp
= edit
->redo_stack_pointer
;
2629 /* first enlarge the stack if necessary */
2630 if (sp
> edit
->redo_stack_size
- 10)
2632 if (option_max_undo
< 256)
2633 option_max_undo
= 256;
2634 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2636 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2639 edit
->redo_stack
= t
;
2640 edit
->redo_stack_size
<<= 1;
2641 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2645 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2647 if (edit
->redo_stack_bottom
!= sp
2648 && spm1
!= edit
->redo_stack_bottom
2649 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2653 if (edit
->redo_stack
[spm1
] < 0)
2655 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2656 if (d
== c
&& edit
->redo_stack
[spm1
] > -1000000000)
2658 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2659 edit
->redo_stack
[spm1
]--;
2665 d
= edit
->redo_stack
[spm1
];
2669 return; /* --> no need to push multiple do-nothings */
2670 edit
->redo_stack
[sp
] = -2;
2671 goto redo_check_bottom
;
2675 edit
->redo_stack
[sp
] = c
;
2678 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2680 /* if the sp wraps round and catches the redo_stack_bottom then erase
2681 * the first set of actions on the stack to make space - by moving
2682 * redo_stack_bottom forward one "key press" */
2683 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2684 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2685 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2688 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2690 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2691 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2694 * If a single key produced enough pushes to wrap all the way round then
2695 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2696 * The stack is then initialised:
2699 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2700 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2701 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2705 /* --------------------------------------------------------------------------------------------- */
2707 Basic low level single character buffer alterations and movements at the cursor.
2708 Returns char passed over, inserted or removed.
2712 edit_insert (WEdit
* edit
, int c
)
2714 /* check if file has grown to large */
2715 if (edit
->last_byte
>= SIZE_LIMIT
)
2718 /* first we must update the position of the display window */
2719 if (edit
->curs1
< edit
->start_display
)
2721 edit
->start_display
++;
2726 /* Mark file as modified, unless the file hasn't been fully loaded */
2727 if (edit
->loading_done
)
2728 edit_modification (edit
);
2730 /* now we must update some info on the file and check if a redraw is required */
2733 book_mark_inc (edit
, edit
->curs_line
);
2735 edit
->total_lines
++;
2736 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2739 /* save the reverse command onto the undo stack */
2740 /* ordinary char and not space */
2742 edit_push_undo_action (edit
, BACKSPACE
);
2744 edit_push_undo_action (edit
, BACKSPACE_BR
);
2745 /* update markers */
2746 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2747 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2748 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2750 /* add a new buffer if we've reached the end of the last one */
2751 if ((edit
->curs1
& M_EDIT_BUF_SIZE
) == 0)
2752 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2754 /* perform the insertion */
2755 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2756 = (unsigned char) c
;
2758 /* update file length */
2761 /* update cursor position */
2765 /* --------------------------------------------------------------------------------------------- */
2766 /** same as edit_insert and move left */
2769 edit_insert_ahead (WEdit
* edit
, int c
)
2771 if (edit
->last_byte
>= SIZE_LIMIT
)
2774 if (edit
->curs1
< edit
->start_display
)
2776 edit
->start_display
++;
2780 edit_modification (edit
);
2783 book_mark_inc (edit
, edit
->curs_line
);
2784 edit
->total_lines
++;
2785 edit
->force
|= REDRAW_AFTER_CURSOR
;
2787 /* ordinary char and not space */
2789 edit_push_undo_action (edit
, DELCHAR
);
2791 edit_push_undo_action (edit
, DELCHAR_BR
);
2793 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2794 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2795 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2797 if (((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
) == 0)
2798 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2799 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2800 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2806 /* --------------------------------------------------------------------------------------------- */
2809 edit_delete (WEdit
* edit
, const int byte_delete
)
2815 if (edit
->curs2
== 0)
2819 /* if byte_delete = 1 then delete only one byte not multibyte char */
2820 if (edit
->utf8
&& byte_delete
== 0)
2822 edit_get_utf (edit
, edit
->curs1
, &cw
);
2830 if (edit
->mark2
!= edit
->mark1
)
2831 edit_push_markers (edit
);
2833 for (i
= 1; i
<= cw
; i
++)
2835 if (edit
->mark1
> edit
->curs1
)
2838 edit
->end_mark_curs
--;
2840 if (edit
->mark2
> edit
->curs1
)
2842 if (edit
->last_get_rule
> edit
->curs1
)
2843 edit
->last_get_rule
--;
2845 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2847 1) & M_EDIT_BUF_SIZE
) - 1];
2849 if ((edit
->curs2
& M_EDIT_BUF_SIZE
) == 0)
2851 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2852 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2856 edit_push_undo_action (edit
, p
+ 256);
2859 edit_modification (edit
);
2862 book_mark_dec (edit
, edit
->curs_line
);
2863 edit
->total_lines
--;
2864 edit
->force
|= REDRAW_AFTER_CURSOR
;
2866 if (edit
->curs1
< edit
->start_display
)
2868 edit
->start_display
--;
2876 /* --------------------------------------------------------------------------------------------- */
2877 /** moves the cursor right or left: increment positive or negative respectively */
2880 edit_cursor_move (WEdit
* edit
, off_t increment
)
2882 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2887 for (; increment
< 0; increment
++)
2889 if (edit
->curs1
== 0)
2892 edit_push_undo_action (edit
, CURS_RIGHT
);
2894 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2895 if (((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
) == 0)
2896 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2897 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2898 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2900 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2901 1) & M_EDIT_BUF_SIZE
];
2902 if (((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
) == 0)
2904 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2905 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2911 edit
->force
|= REDRAW_LINE_BELOW
;
2918 for (; increment
> 0; increment
--)
2920 if (edit
->curs2
== 0)
2923 edit_push_undo_action (edit
, CURS_LEFT
);
2925 c
= edit_get_byte (edit
, edit
->curs1
);
2926 if ((edit
->curs1
& M_EDIT_BUF_SIZE
) == 0)
2927 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2928 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2930 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2932 1) & M_EDIT_BUF_SIZE
) - 1];
2933 if ((edit
->curs2
& M_EDIT_BUF_SIZE
) == 0)
2935 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2936 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2942 edit
->force
|= REDRAW_LINE_ABOVE
;
2948 /* These functions return positions relative to lines */
2950 /* --------------------------------------------------------------------------------------------- */
2951 /** returns index of last char on line + 1 */
2954 edit_eol (WEdit
* edit
, off_t current
)
2956 if (current
>= edit
->last_byte
)
2957 return edit
->last_byte
;
2959 for (; edit_get_byte (edit
, current
) != '\n'; current
++)
2965 /* --------------------------------------------------------------------------------------------- */
2966 /** returns index of first char on line */
2969 edit_bol (WEdit
* edit
, off_t current
)
2974 for (; edit_get_byte (edit
, current
- 1) != '\n'; current
--)
2980 /* --------------------------------------------------------------------------------------------- */
2983 edit_count_lines (WEdit
* edit
, off_t current
, off_t upto
)
2986 if (upto
> edit
->last_byte
)
2987 upto
= edit
->last_byte
;
2990 while (current
< upto
)
2991 if (edit_get_byte (edit
, current
++) == '\n')
2996 /* --------------------------------------------------------------------------------------------- */
2997 /* If lines is zero this returns the count of lines from current to upto. */
2998 /* If upto is zero returns index of lines forward current. */
3001 edit_move_forward (WEdit
* edit
, off_t current
, long lines
, off_t upto
)
3005 return (off_t
) edit_count_lines (edit
, current
, upto
);
3013 while (lines
-- != 0)
3015 next
= edit_eol (edit
, current
) + 1;
3016 if (next
> edit
->last_byte
)
3024 /* --------------------------------------------------------------------------------------------- */
3025 /** Returns offset of 'lines' lines up from current */
3028 edit_move_backward (WEdit
* edit
, off_t current
, long lines
)
3032 current
= edit_bol (edit
, current
);
3033 while (lines
-- != 0 && current
!= 0)
3034 current
= edit_bol (edit
, current
- 1);
3038 /* --------------------------------------------------------------------------------------------- */
3039 /* If cols is zero this returns the count of columns from current to upto. */
3040 /* If upto is zero returns index of cols across from current. */
3043 edit_move_forward3 (WEdit
* edit
, off_t current
, long cols
, off_t upto
)
3054 q
= edit
->last_byte
+ 2;
3056 for (col
= 0, p
= current
; p
< q
; p
++)
3068 orig_c
= c
= edit_get_byte (edit
, p
);
3076 utf_ch
= edit_get_utf (edit
, p
, &cw
);
3077 if (mc_global
.utf8_display
)
3081 if (g_unichar_iswide (utf_ch
))
3084 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
3088 c
= convert_to_display_c (c
);
3092 return (upto
!= 0 ? (off_t
) col
: p
);
3094 col
+= TAB_SIZE
- col
% TAB_SIZE
;
3095 else if ((c
< 32 || c
== 127) && (orig_c
== c
3097 || (!mc_global
.utf8_display
&& !edit
->utf8
)
3100 /* '\r' is shown as ^M, so we must advance 2 characters */
3101 /* Caret notation for control characters */
3109 /* --------------------------------------------------------------------------------------------- */
3110 /** returns the current column position of the cursor */
3113 edit_get_col (WEdit
* edit
)
3115 return (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3118 /* --------------------------------------------------------------------------------------------- */
3119 /* Scrolling functions */
3120 /* --------------------------------------------------------------------------------------------- */
3123 edit_update_curs_row (WEdit
* edit
)
3125 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
3128 /* --------------------------------------------------------------------------------------------- */
3131 edit_update_curs_col (WEdit
* edit
)
3133 edit
->curs_col
= (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3136 /* --------------------------------------------------------------------------------------------- */
3139 edit_get_curs_col (const WEdit
* edit
)
3141 return edit
->curs_col
;
3144 /* --------------------------------------------------------------------------------------------- */
3145 /** moves the display start position up by i lines */
3148 edit_scroll_upward (WEdit
* edit
, long i
)
3150 long lines_above
= edit
->start_line
;
3152 if (i
> lines_above
)
3156 edit
->start_line
-= i
;
3157 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
3158 edit
->force
|= REDRAW_PAGE
;
3159 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3161 edit_update_curs_row (edit
);
3165 /* --------------------------------------------------------------------------------------------- */
3168 edit_scroll_downward (WEdit
* edit
, long i
)
3172 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->widget
.lines
- 1);
3173 if (lines_below
> 0)
3175 if (i
> lines_below
)
3177 edit
->start_line
+= i
;
3178 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
3179 edit
->force
|= REDRAW_PAGE
;
3180 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3182 edit_update_curs_row (edit
);
3185 /* --------------------------------------------------------------------------------------------- */
3188 edit_scroll_right (WEdit
* edit
, long i
)
3190 edit
->force
|= REDRAW_PAGE
;
3191 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3192 edit
->start_col
-= i
;
3195 /* --------------------------------------------------------------------------------------------- */
3198 edit_scroll_left (WEdit
* edit
, long i
)
3200 if (edit
->start_col
)
3202 edit
->start_col
+= i
;
3203 if (edit
->start_col
> 0)
3204 edit
->start_col
= 0;
3205 edit
->force
|= REDRAW_PAGE
;
3206 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3210 /* --------------------------------------------------------------------------------------------- */
3211 /* high level cursor movement commands */
3212 /* --------------------------------------------------------------------------------------------- */
3215 edit_move_to_prev_col (WEdit
* edit
, off_t p
)
3217 long prev
= edit
->prev_col
;
3218 long over
= edit
->over_col
;
3220 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
3222 if (option_cursor_beyond_eol
)
3226 line_len
= (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
3227 edit_eol (edit
, edit
->curs1
));
3228 if (line_len
< prev
+ edit
->over_col
)
3230 edit
->over_col
= prev
+ over
- line_len
;
3231 edit
->prev_col
= line_len
;
3232 edit
->curs_col
= line_len
;
3236 edit
->curs_col
= prev
+ over
;
3237 edit
->prev_col
= edit
->curs_col
;
3244 if (option_fake_half_tabs
&& is_in_indent (edit
))
3246 edit_update_curs_col (edit
);
3247 if (space_width
!= 0 && edit
->curs_col
% (HALF_TAB_SIZE
* space_width
) != 0)
3252 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
3253 p
= edit_bol (edit
, edit
->curs1
);
3254 edit_cursor_move (edit
,
3255 edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->curs1
);
3256 if (!left_of_four_spaces (edit
))
3257 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3263 /* --------------------------------------------------------------------------------------------- */
3264 /** check whether line in editor is blank or not
3266 * @param edit editor object
3267 * @param line number of line
3269 * @return TRUE if line in blank, FALSE otherwise
3273 line_is_blank (WEdit
* edit
, long line
)
3275 return is_blank (edit
, edit_find_line (edit
, line
));
3278 /* --------------------------------------------------------------------------------------------- */
3279 /** move cursor to line 'line' */
3282 edit_move_to_line (WEdit
* e
, long line
)
3284 if (line
< e
->curs_line
)
3285 edit_move_up (e
, e
->curs_line
- line
, 0);
3287 edit_move_down (e
, line
- e
->curs_line
, 0);
3288 edit_scroll_screen_over_cursor (e
);
3291 /* --------------------------------------------------------------------------------------------- */
3292 /** scroll window so that first visible line is 'line' */
3295 edit_move_display (WEdit
* e
, long line
)
3297 if (line
< e
->start_line
)
3298 edit_scroll_upward (e
, e
->start_line
- line
);
3300 edit_scroll_downward (e
, line
- e
->start_line
);
3303 /* --------------------------------------------------------------------------------------------- */
3304 /** save markers onto undo stack */
3307 edit_push_markers (WEdit
* edit
)
3309 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3310 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3311 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3314 /* --------------------------------------------------------------------------------------------- */
3317 edit_set_markers (WEdit
* edit
, off_t m1
, off_t m2
, long c1
, long c2
)
3325 /* --------------------------------------------------------------------------------------------- */
3326 /** highlight marker toggle */
3329 edit_mark_cmd (WEdit
* edit
, gboolean unmark
)
3331 edit_push_markers (edit
);
3334 edit_set_markers (edit
, 0, 0, 0, 0);
3335 edit
->force
|= REDRAW_PAGE
;
3337 else if (edit
->mark2
>= 0)
3339 edit
->end_mark_curs
= -1;
3340 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3341 edit
->curs_col
+ edit
->over_col
);
3342 edit
->force
|= REDRAW_PAGE
;
3346 edit
->end_mark_curs
= edit
->curs1
;
3347 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3348 edit
->curs_col
+ edit
->over_col
);
3352 /* --------------------------------------------------------------------------------------------- */
3353 /** highlight the word under cursor */
3356 edit_mark_current_word_cmd (WEdit
* edit
)
3360 for (pos
= edit
->curs1
; pos
!= 0; pos
--)
3364 c1
= edit_get_byte (edit
, pos
);
3365 c2
= edit_get_byte (edit
, pos
- 1);
3366 if (!isspace (c1
) && isspace (c2
))
3368 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3373 for (; pos
< edit
->last_byte
; pos
++)
3377 c1
= edit_get_byte (edit
, pos
);
3378 c2
= edit_get_byte (edit
, pos
+ 1);
3379 if (!isspace (c1
) && isspace (c2
))
3381 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3384 edit
->mark2
= min (pos
+ 1, edit
->last_byte
);
3386 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3389 /* --------------------------------------------------------------------------------------------- */
3392 edit_mark_current_line_cmd (WEdit
* edit
)
3394 long pos
= edit
->curs1
;
3396 edit
->mark1
= edit_bol (edit
, pos
);
3397 edit
->mark2
= edit_eol (edit
, pos
);
3399 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3402 /* --------------------------------------------------------------------------------------------- */
3405 edit_delete_line (WEdit
* edit
)
3408 * Delete right part of the line.
3409 * Note that edit_get_byte() returns '\n' when byte position is
3412 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3413 (void) edit_delete (edit
, 1);
3417 * Note that edit_delete() will not corrupt anything if called while
3418 * cursor position is EOF.
3420 (void) edit_delete (edit
, 1);
3423 * Delete left part of the line.
3424 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3426 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3427 (void) edit_backspace (edit
, 1);
3430 /* --------------------------------------------------------------------------------------------- */
3433 edit_indent_width (WEdit
* edit
, off_t p
)
3437 /* move to the end of the leading whitespace of the line */
3438 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1)
3440 /* count the number of columns of indentation */
3441 return (long) edit_move_forward3 (edit
, p
, 0, q
);
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 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
, TRUE
); /* clear */
3591 edit_mark_cmd (edit
, FALSE
); /* marking on */
3593 edit
->highlight
= 1;
3596 /* any other command */
3598 if (edit
->highlight
)
3599 edit_mark_cmd (edit
, FALSE
); /* 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
&& edit
->mark1
!= edit
->mark2
)
3631 edit_block_delete_cmd (edit
);
3633 if (edit
->overwrite
)
3635 /* remove char only one time, after input first byte, multibyte chars */
3637 if (!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3639 if (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
)
3647 unsigned char str
[6 + 1];
3651 res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3661 while (str
[i
] != 0 && i
<= 6)
3663 char_for_insertion
= str
[i
];
3664 edit_insert (edit
, char_for_insertion
);
3670 edit_insert (edit
, char_for_insertion
);
3672 if (option_auto_para_formatting
)
3674 format_paragraph (edit
, 0);
3675 edit
->force
|= REDRAW_PAGE
;
3678 check_and_wrap_line (edit
);
3679 edit
->found_len
= 0;
3680 edit
->prev_col
= edit_get_col (edit
);
3681 edit
->search_start
= edit
->curs1
;
3682 edit_find_bracket (edit
);
3688 case CK_TopOnScreen
:
3689 case CK_BottomOnScreen
:
3702 if (!option_persistent_selections
&& edit
->mark2
>= 0)
3704 if (edit
->column_highlight
)
3705 edit_push_undo_action (edit
, COLUMN_ON
);
3706 edit
->column_highlight
= 0;
3707 edit_mark_cmd (edit
, TRUE
);
3713 case CK_TopOnScreen
:
3714 case CK_BottomOnScreen
:
3715 case CK_MarkToPageBegin
:
3716 case CK_MarkToPageEnd
:
3721 case CK_MarkToWordBegin
:
3722 case CK_MarkToWordEnd
:
3725 case CK_MarkColumnUp
:
3726 case CK_MarkColumnDown
:
3727 if (edit
->mark2
== -1)
3728 break; /*marking is following the cursor: may need to highlight a whole line */
3733 edit
->force
|= REDRAW_CHAR_ONLY
;
3736 /* basic cursor key commands */
3740 /* if non persistent selection and text selected */
3741 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3742 edit_block_delete_cmd (edit
);
3743 else if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3745 else if (option_backspace_through_tabs
&& is_in_indent (edit
))
3746 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3747 edit_backspace (edit
, 1);
3748 else if (option_fake_half_tabs
&& is_in_indent (edit
) && right_of_four_spaces (edit
))
3752 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3753 edit_backspace (edit
, 1);
3756 edit_backspace (edit
, 0);
3759 /* if non persistent selection and text selected */
3760 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3761 edit_block_delete_cmd (edit
);
3764 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3765 edit_insert_over (edit
);
3767 if (option_fake_half_tabs
&& is_in_indent (edit
) && left_of_four_spaces (edit
))
3771 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3772 edit_delete (edit
, 1);
3775 edit_delete (edit
, 0);
3778 case CK_DeleteToWordBegin
:
3780 edit_left_delete_word (edit
);
3782 case CK_DeleteToWordEnd
:
3783 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3784 edit_insert_over (edit
);
3786 edit_right_delete_word (edit
);
3789 edit_delete_line (edit
);
3791 case CK_DeleteToHome
:
3792 edit_delete_to_line_begin (edit
);
3794 case CK_DeleteToEnd
:
3795 edit_delete_to_line_end (edit
);
3799 if (option_auto_para_formatting
)
3801 edit_double_newline (edit
);
3802 if (option_return_does_auto_indent
)
3803 edit_auto_indent (edit
);
3804 format_paragraph (edit
, 0);
3808 edit_insert (edit
, '\n');
3809 if (option_return_does_auto_indent
)
3810 edit_auto_indent (edit
);
3814 edit_insert (edit
, '\n');
3817 case CK_MarkColumnPageUp
:
3818 edit
->column_highlight
= 1;
3821 edit_move_up (edit
, edit
->widget
.lines
- 1, 1);
3823 case CK_MarkColumnPageDown
:
3824 edit
->column_highlight
= 1;
3826 case CK_MarkPageDown
:
3827 edit_move_down (edit
, edit
->widget
.lines
- 1, 1);
3829 case CK_MarkColumnLeft
:
3830 edit
->column_highlight
= 1;
3833 if (option_fake_half_tabs
&& is_in_indent (edit
) && right_of_four_spaces (edit
))
3835 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3838 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3839 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3842 edit_left_char_move_cmd (edit
);
3844 case CK_MarkColumnRight
:
3845 edit
->column_highlight
= 1;
3848 if (option_fake_half_tabs
&& is_in_indent (edit
) && left_of_four_spaces (edit
))
3850 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3851 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3854 edit_right_char_move_cmd (edit
);
3856 case CK_TopOnScreen
:
3857 case CK_MarkToPageBegin
:
3858 edit_begin_page (edit
);
3860 case CK_BottomOnScreen
:
3861 case CK_MarkToPageEnd
:
3862 edit_end_page (edit
);
3865 case CK_MarkToWordBegin
:
3867 edit_left_word_move_cmd (edit
);
3870 case CK_MarkToWordEnd
:
3872 edit_right_word_move_cmd (edit
);
3874 case CK_MarkColumnUp
:
3875 edit
->column_highlight
= 1;
3878 edit_move_up (edit
, 1, 0);
3880 case CK_MarkColumnDown
:
3881 edit
->column_highlight
= 1;
3884 edit_move_down (edit
, 1, 0);
3886 case CK_MarkColumnParagraphUp
:
3887 edit
->column_highlight
= 1;
3888 case CK_ParagraphUp
:
3889 case CK_MarkParagraphUp
:
3890 edit_move_up_paragraph (edit
, 0);
3892 case CK_MarkColumnParagraphDown
:
3893 edit
->column_highlight
= 1;
3894 case CK_ParagraphDown
:
3895 case CK_MarkParagraphDown
:
3896 edit_move_down_paragraph (edit
, 0);
3898 case CK_MarkColumnScrollUp
:
3899 edit
->column_highlight
= 1;
3901 case CK_MarkScrollUp
:
3902 edit_move_up (edit
, 1, 1);
3904 case CK_MarkColumnScrollDown
:
3905 edit
->column_highlight
= 1;
3907 case CK_MarkScrollDown
:
3908 edit_move_down (edit
, 1, 1);
3912 edit_cursor_to_bol (edit
);
3916 edit_cursor_to_eol (edit
);
3919 /* if text marked shift block */
3920 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3922 if (edit
->mark2
< 0)
3923 edit_mark_cmd (edit
, FALSE
);
3924 edit_move_block_to_right (edit
);
3928 if (option_cursor_beyond_eol
)
3929 edit_insert_over (edit
);
3930 edit_tab_cmd (edit
);
3931 if (option_auto_para_formatting
)
3933 format_paragraph (edit
, 0);
3934 edit
->force
|= REDRAW_PAGE
;
3937 check_and_wrap_line (edit
);
3941 case CK_InsertOverwrite
:
3942 edit
->overwrite
= !edit
->overwrite
;
3946 if (edit
->mark2
>= 0)
3948 if (edit
->column_highlight
)
3949 edit_push_undo_action (edit
, COLUMN_ON
);
3950 edit
->column_highlight
= 0;
3952 edit_mark_cmd (edit
, FALSE
);
3955 if (!edit
->column_highlight
)
3956 edit_push_undo_action (edit
, COLUMN_OFF
);
3957 edit
->column_highlight
= 1;
3958 edit_mark_cmd (edit
, FALSE
);
3961 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3962 edit
->force
|= REDRAW_PAGE
;
3965 if (edit
->column_highlight
)
3966 edit_push_undo_action (edit
, COLUMN_ON
);
3967 edit
->column_highlight
= 0;
3968 edit_mark_cmd (edit
, TRUE
);
3971 if (edit
->column_highlight
)
3972 edit_push_undo_action (edit
, COLUMN_ON
);
3973 edit
->column_highlight
= 0;
3974 edit_mark_current_word_cmd (edit
);
3977 if (edit
->column_highlight
)
3978 edit_push_undo_action (edit
, COLUMN_ON
);
3979 edit
->column_highlight
= 0;
3980 edit_mark_current_line_cmd (edit
);
3983 case CK_ShowNumbers
:
3984 option_line_state
= !option_line_state
;
3985 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
3986 edit
->force
|= REDRAW_PAGE
;
3990 show_right_margin
= !show_right_margin
;
3991 edit
->force
|= REDRAW_PAGE
;
3995 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
3996 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
3997 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3999 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4001 case CK_BookmarkFlush
:
4002 book_mark_flush (edit
, BOOK_MARK_COLOR
);
4003 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
4004 edit
->force
|= REDRAW_PAGE
;
4006 case CK_BookmarkNext
:
4007 if (edit
->book_mark
!= NULL
)
4009 struct _book_mark
*p
;
4011 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4012 if (p
->next
!= NULL
)
4015 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4016 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4017 edit_move_to_line (edit
, p
->line
);
4021 case CK_BookmarkPrev
:
4022 if (edit
->book_mark
!= NULL
)
4024 struct _book_mark
*p
;
4026 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4027 while (p
->line
== edit
->curs_line
)
4028 if (p
->prev
!= NULL
)
4032 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4033 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4034 edit_move_to_line (edit
, p
->line
);
4040 case CK_MarkToFileBegin
:
4041 edit_move_to_top (edit
);
4044 case CK_MarkToFileEnd
:
4045 edit_move_to_bottom (edit
);
4049 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4050 edit_insert_over (edit
);
4051 edit_block_copy_cmd (edit
);
4054 edit_block_delete_cmd (edit
);
4057 edit_block_move_cmd (edit
);
4060 case CK_BlockShiftLeft
:
4061 if (edit
->mark1
!= edit
->mark2
)
4062 edit_move_block_to_left (edit
);
4064 case CK_BlockShiftRight
:
4065 if (edit
->mark1
!= edit
->mark2
)
4066 edit_move_block_to_right (edit
);
4069 edit_copy_to_X_buf_cmd (edit
);
4072 edit_cut_to_X_buf_cmd (edit
);
4075 /* if non persistent selection and text selected */
4076 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
4077 edit_block_delete_cmd (edit
);
4078 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4079 edit_insert_over (edit
);
4080 edit_paste_from_X_buf_cmd (edit
);
4083 edit_paste_from_history (edit
);
4087 edit_save_as_cmd (edit
);
4090 edit_save_confirm_cmd (edit
);
4093 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
4096 edit_save_block_cmd (edit
);
4099 edit_insert_file_cmd (edit
);
4103 edit_load_back_cmd (edit
);
4106 edit_load_forward_cmd (edit
);
4109 case CK_EditSyntaxFile
:
4110 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
4112 case CK_SyntaxChoose
:
4113 edit_syntax_dialog (edit
);
4116 case CK_EditUserMenu
:
4117 edit_load_cmd (edit
, EDIT_FILE_MENU
);
4120 case CK_SyntaxOnOff
:
4121 option_syntax_highlighting
^= 1;
4122 if (option_syntax_highlighting
== 1)
4123 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
4124 edit
->force
|= REDRAW_PAGE
;
4128 enable_show_tabs_tws
^= 1;
4129 edit
->force
|= REDRAW_PAGE
;
4133 edit_search_cmd (edit
, FALSE
);
4135 case CK_SearchContinue
:
4136 edit_search_cmd (edit
, TRUE
);
4139 edit_replace_cmd (edit
, 0);
4141 case CK_ReplaceContinue
:
4142 edit_replace_cmd (edit
, 1);
4145 /* if text marked shift block */
4146 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
4147 edit_move_block_to_left (edit
);
4149 edit_complete_word_cmd (edit
);
4152 edit_get_match_keyword_cmd (edit
);
4155 dlg_stop (edit
->widget
.owner
);
4158 edit_new_cmd (edit
);
4161 edit_help_cmd (edit
);
4164 edit_refresh_cmd (edit
);
4176 edit_options_dialog (edit
);
4178 case CK_OptionsSaveMode
:
4179 menu_save_mode_cmd ();
4184 /* fool gcc to prevent a Y2K warning */
4185 char time_format
[] = "_c";
4186 time_format
[0] = '%';
4188 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4189 edit_print_string (edit
, s
);
4190 edit
->force
|= REDRAW_PAGE
;
4194 edit_goto_cmd (edit
);
4196 case CK_ParagraphFormat
:
4197 format_paragraph (edit
, 1);
4198 edit
->force
|= REDRAW_PAGE
;
4200 case CK_MacroDelete
:
4201 edit_delete_macro_cmd (edit
);
4203 case CK_MatchBracket
:
4204 edit_goto_matching_bracket (edit
);
4207 user_menu (edit
, NULL
, -1);
4210 edit_sort_cmd (edit
);
4212 case CK_ExternalCommand
:
4213 edit_ext_cmd (edit
);
4216 edit_mail_dialog (edit
);
4222 case CK_SelectCodepage
:
4223 edit_select_codepage_cmd (edit
);
4226 case CK_InsertLiteral
:
4227 edit_insert_literal_cmd (edit
);
4229 case CK_MacroStartStopRecord
:
4230 edit_begin_end_macro_cmd (edit
);
4232 case CK_RepeatStartStopRecord
:
4233 edit_begin_end_repeat_cmd (edit
);
4235 case CK_ExtendedKeyMap
:
4236 edit
->extmod
= TRUE
;
4243 if ((command
/ CK_PipeBlock (0)) == 1)
4244 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
4246 /* keys which must set the col position, and the search vars */
4250 case CK_SearchContinue
:
4252 case CK_ReplaceContinue
:
4254 edit
->prev_col
= edit_get_col (edit
);
4258 case CK_MarkColumnUp
:
4261 case CK_MarkColumnDown
:
4264 case CK_MarkColumnPageUp
:
4266 case CK_MarkPageDown
:
4267 case CK_MarkColumnPageDown
:
4269 case CK_MarkToFileBegin
:
4271 case CK_MarkToFileEnd
:
4272 case CK_ParagraphUp
:
4273 case CK_MarkParagraphUp
:
4274 case CK_MarkColumnParagraphUp
:
4275 case CK_ParagraphDown
:
4276 case CK_MarkParagraphDown
:
4277 case CK_MarkColumnParagraphDown
:
4279 case CK_MarkScrollUp
:
4280 case CK_MarkColumnScrollUp
:
4282 case CK_MarkScrollDown
:
4283 case CK_MarkColumnScrollDown
:
4284 edit
->search_start
= edit
->curs1
;
4285 edit
->found_len
= 0;
4288 edit
->found_len
= 0;
4289 edit
->prev_col
= edit_get_col (edit
);
4290 edit
->search_start
= edit
->curs1
;
4292 edit_find_bracket (edit
);
4294 if (option_auto_para_formatting
)
4300 case CK_DeleteToWordBegin
:
4301 case CK_DeleteToWordEnd
:
4302 case CK_DeleteToHome
:
4303 case CK_DeleteToEnd
:
4304 format_paragraph (edit
, 0);
4305 edit
->force
|= REDRAW_PAGE
;
4310 /* --------------------------------------------------------------------------------------------- */
4313 edit_stack_init (void)
4315 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4317 edit_history_moveto
[edit_stack_iterator
].filename_vpath
= NULL
;
4318 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4321 edit_stack_iterator
= 0;
4324 /* --------------------------------------------------------------------------------------------- */
4327 edit_stack_free (void)
4329 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4330 vfs_path_free (edit_history_moveto
[edit_stack_iterator
].filename_vpath
);
4333 /* --------------------------------------------------------------------------------------------- */
4337 edit_move_up (WEdit
* edit
, long i
, gboolean do_scroll
)
4339 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4342 /* --------------------------------------------------------------------------------------------- */
4346 edit_move_down (WEdit
* edit
, long i
, gboolean do_scroll
)
4348 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4351 /* --------------------------------------------------------------------------------------------- */