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/usermenu.h" /* user_menu_cmd() */
66 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/main.h" /* macro_index */
68 #include "src/learn.h" /* learn_keys */
69 #include "src/keybind-defaults.h"
71 #include "edit-impl.h"
72 #include "editwidget.h"
77 /*** global variables ****************************************************************************/
79 int option_word_wrap_line_length
= DEFAULT_WRAP_LINE_LENGTH
;
80 int option_typewriter_wrap
= 0;
81 int option_auto_para_formatting
= 0;
82 int option_fill_tabs_with_spaces
= 0;
83 int option_return_does_auto_indent
= 1;
84 int option_backspace_through_tabs
= 0;
85 int option_fake_half_tabs
= 1;
86 int option_save_mode
= EDIT_QUICK_SAVE
;
87 int option_save_position
= 1;
88 int option_max_undo
= 32768;
89 int option_persistent_selections
= 1;
90 int option_cursor_beyond_eol
= 0;
91 int option_line_state
= 0;
92 int option_line_state_width
= 0;
93 int option_autodetect_lb
= 0;
95 int option_edit_right_extreme
= 0;
96 int option_edit_left_extreme
= 0;
97 int option_edit_top_extreme
= 0;
98 int option_edit_bottom_extreme
= 0;
99 int enable_show_tabs_tws
= 1;
100 int option_check_nl_at_eof
= 0;
101 int option_group_undo
= 0;
102 int show_right_margin
= 0;
104 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
105 char *option_backup_ext
= NULL
;
107 int edit_stack_iterator
= 0;
108 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
109 /* magic sequense for say than block is vertical */
110 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
112 /*** file scope macro definitions ****************************************************************/
114 #define TEMP_BUF_LEN 1024
116 #define space_width 1
118 #define DETECT_LB_TYPE_BUFLEN BUF_MEDIUM
120 /*** file scope type declarations ****************************************************************/
122 /*** file scope variables ************************************************************************/
124 /* detecting an error on save is easy: just check if every byte has been written. */
125 /* detecting an error on read, is not so easy 'cos there is not way to tell
126 whether you read everything or not. */
127 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
128 static const struct edit_filters
130 const char *read
, *write
, *extension
;
134 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
135 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
136 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
137 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
138 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
142 static long last_bracket
= -1;
144 /*** file scope functions ************************************************************************/
145 /* --------------------------------------------------------------------------------------------- */
149 * here's a quick sketch of the layout: (don't run this through indent.)
151 * (b1 is buffers1 and b2 is buffers2)
154 * \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
155 * ______________________________________|______________________________________
157 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
158 * |-> |-> |-> |-> |-> |-> |
160 * _<------------------------->|<----------------->_
161 * WEdit->curs2 | WEdit->curs1
166 * file end|||file beginning
175 /* --------------------------------------------------------------------------------------------- */
177 static int left_of_four_spaces (WEdit
* edit
);
179 /* --------------------------------------------------------------------------------------------- */
181 * Initialize the buffers for an empty files.
185 edit_init_buffers (WEdit
* edit
)
189 for (j
= 0; j
<= MAXBUFF
; j
++)
191 edit
->buffers1
[j
] = NULL
;
192 edit
->buffers2
[j
] = NULL
;
197 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
200 /* --------------------------------------------------------------------------------------------- */
203 * Load file OR text into buffers. Set cursor to the beginning of file.
205 * @returns FALSE on error.
209 edit_load_file_fast (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
213 gboolean ret
= FALSE
;
215 edit
->curs2
= edit
->last_byte
;
216 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
218 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
221 gchar
*errmsg
, *filename
;
223 filename
= vfs_path_to_str (filename_vpath
);
224 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
226 edit_error_dialog (_("Error"), errmsg
);
231 if (!edit
->buffers2
[buf2
])
232 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
237 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
238 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
241 for (buf
= buf2
- 1; buf
>= 0; buf
--)
243 /* edit->buffers2[0] is already allocated */
244 if (!edit
->buffers2
[buf
])
245 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
246 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
255 gchar
*errmsg
, *filename
;
257 filename
= vfs_path_to_str (filename_vpath
);
258 errmsg
= g_strdup_printf (_("Error reading %s"), filename
);
260 edit_error_dialog (_("Error"), errmsg
);
267 /* --------------------------------------------------------------------------------------------- */
268 /** Return index of the filter or -1 is there is no appropriate filter */
271 edit_find_filter (const vfs_path_t
* filename_vpath
)
276 if (filename_vpath
== NULL
)
279 filename
= vfs_path_to_str (filename_vpath
);
280 l
= strlen (filename
);
281 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
283 e
= strlen (all_filters
[i
].extension
);
285 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
295 /* --------------------------------------------------------------------------------------------- */
298 edit_get_filter (const vfs_path_t
* filename_vpath
)
301 char *p
, *quoted_name
, *filename
;
303 i
= edit_find_filter (filename_vpath
);
307 filename
= vfs_path_to_str (filename_vpath
);
308 quoted_name
= name_quote (filename
, 0);
310 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
311 g_free (quoted_name
);
315 /* --------------------------------------------------------------------------------------------- */
318 edit_insert_stream (WEdit
* edit
, FILE * f
)
322 while ((c
= fgetc (f
)) >= 0)
324 edit_insert (edit
, c
);
330 /* --------------------------------------------------------------------------------------------- */
332 * Open file and create it if necessary.
334 * @param edit editor object
335 * @param filename_vpath file name
336 * @param st buffer for store stat info
337 * @returns TRUE for success, FALSE for error.
341 check_file_access (WEdit
* edit
, const vfs_path_t
* filename_vpath
, struct stat
*st
)
344 gchar
*errmsg
= NULL
;
346 /* Try opening an existing file */
347 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
351 * Try creating the file. O_EXCL prevents following broken links
352 * and opening existing files.
354 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
359 filename
= vfs_path_to_str (filename_vpath
);
360 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
365 /* New file, delete it if it's not modified or saved */
366 edit
->delete_file
= 1;
369 /* Check what we have opened */
370 if (mc_fstat (file
, st
) < 0)
374 filename
= vfs_path_to_str (filename_vpath
);
375 errmsg
= g_strdup_printf (_("Cannot get size/permissions for %s"), filename
);
380 /* We want to open regular files only */
381 if (!S_ISREG (st
->st_mode
))
385 filename
= vfs_path_to_str (filename_vpath
);
386 errmsg
= g_strdup_printf (_("\"%s\" is not a regular file"), filename
);
392 * Don't delete non-empty files.
393 * O_EXCL should prevent it, but let's be on the safe side.
396 edit
->delete_file
= 0;
398 if (st
->st_size
>= SIZE_LIMIT
)
402 filename
= vfs_path_to_str (filename_vpath
);
403 errmsg
= g_strdup_printf (_("File \"%s\" is too large"), filename
);
408 (void) mc_close (file
);
412 edit_error_dialog (_("Error"), errmsg
);
419 /* --------------------------------------------------------------------------------------------- */
422 * detect type of line breaks
425 /* --------------------------------------------------------------------------------------------- */
428 detect_lb_type (const vfs_path_t
*filename_vpath
)
430 char buf
[BUF_MEDIUM
];
433 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
437 sz
= mc_read (file
, buf
, sizeof (buf
) - 1);
443 buf
[(size_t) sz
] = '\0';
444 if (strstr (buf
, "\r\n") != NULL
)
446 if (strchr (buf
, '\r') != NULL
)
451 /* --------------------------------------------------------------------------------------------- */
453 * Open the file and load it into the buffers, either directly or using
454 * a filter. Return TRUE on success, FALSE on error.
456 * Fast loading (edit_load_file_fast) is used when the file size is
457 * known. In this case the data is read into the buffers by blocks.
458 * If the file size is not known, the data is loaded byte by byte in
461 * @param edit editor object
462 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
465 edit_load_file (WEdit
* edit
)
467 gboolean fast_load
= TRUE
;
468 LineBreaks lb_type
= LB_ASIS
;
470 /* Cannot do fast load if a filter is used */
471 if (edit_find_filter (edit
->filename_vpath
) >= 0)
475 * FIXME: line end translation should disable fast loading as well
476 * Consider doing fseek() to the end and ftell() for the real size.
478 if (edit
->filename_vpath
!= NULL
)
481 * VFS may report file size incorrectly, and slow load is not a big
482 * deal considering overhead in VFS.
484 if (!vfs_file_is_local (edit
->filename_vpath
))
487 /* If we are dealing with a real file, check that it exists */
488 if (!check_file_access (edit
, edit
->filename_vpath
, &edit
->stat1
))
493 if (option_autodetect_lb
)
494 lb_type
= detect_lb_type (edit
->filename_vpath
);
496 if (lb_type
!= LB_ASIS
&& lb_type
!= LB_UNIX
)
501 /* nothing to load */
505 edit_init_buffers (edit
);
509 edit
->last_byte
= edit
->stat1
.st_size
;
510 edit_load_file_fast (edit
, edit
->filename_vpath
);
511 /* If fast load was used, the number of lines wasn't calculated */
512 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
517 if (edit
->filename_vpath
!= NULL
518 && *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) != '\0')
520 edit
->undo_stack_disable
= 1;
521 if (edit_insert_file (edit
, edit
->filename_vpath
, lb_type
) < 0)
526 edit_set_markers (edit
, 0, 0, 0, 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
= 0;
692 /* raise lock when file modified */
693 if (!edit
->modified
&& !edit
->delete_file
)
694 edit
->locked
= lock_file (edit
->filename_vpath
);
698 /* --------------------------------------------------------------------------------------------- */
701 edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
703 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
706 if (byte_index
>= edit
->curs1
)
710 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
711 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
712 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
715 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
716 (byte_index
& M_EDIT_BUF_SIZE
));
719 /* --------------------------------------------------------------------------------------------- */
722 edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
725 gchar utf8_buf
[3 * UTF8_CHAR_LEN
+ 1];
727 gchar
*cursor_buf_ptr
;
729 if (byte_index
> (edit
->curs1
+ edit
->curs2
) || byte_index
<= 0)
735 for (i
= 0; i
< (3 * UTF8_CHAR_LEN
); i
++)
736 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
- (2 * UTF8_CHAR_LEN
));
737 utf8_buf
[3 * UTF8_CHAR_LEN
] = '\0';
739 cursor_buf_ptr
= utf8_buf
+ (2 * UTF8_CHAR_LEN
);
740 str
= g_utf8_find_prev_char (utf8_buf
, cursor_buf_ptr
);
742 if (str
== NULL
|| g_utf8_next_char (str
) != cursor_buf_ptr
)
745 return *(cursor_buf_ptr
- 1);
749 res
= g_utf8_get_char_validated (str
, -1);
754 return *(cursor_buf_ptr
- 1);
758 *char_width
= cursor_buf_ptr
- str
;
764 /* --------------------------------------------------------------------------------------------- */
765 /* high level cursor movement commands */
766 /* --------------------------------------------------------------------------------------------- */
769 is_in_indent (WEdit
* edit
)
771 long p
= edit_bol (edit
, edit
->curs1
);
772 while (p
< edit
->curs1
)
773 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
778 /* --------------------------------------------------------------------------------------------- */
781 is_blank (WEdit
* edit
, long offset
)
785 s
= edit_bol (edit
, offset
);
786 f
= edit_eol (edit
, offset
) - 1;
789 c
= edit_get_byte (edit
, s
++);
797 /* --------------------------------------------------------------------------------------------- */
798 /** returns the offset of line i */
801 edit_find_line (WEdit
* edit
, int line
)
805 if (!edit
->caches_valid
)
807 for (i
= 0; i
< N_LINE_CACHES
; i
++)
808 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
809 /* three offsets that we *know* are line 0 at 0 and these two: */
810 edit
->line_numbers
[1] = edit
->curs_line
;
811 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
812 edit
->line_numbers
[2] = edit
->total_lines
;
813 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
814 edit
->caches_valid
= 1;
816 if (line
>= edit
->total_lines
)
817 return edit
->line_offsets
[2];
820 /* find the closest known point */
821 for (i
= 0; i
< N_LINE_CACHES
; i
++)
824 n
= abs (edit
->line_numbers
[i
] - line
);
832 return edit
->line_offsets
[j
]; /* know the offset exactly */
833 if (m
== 1 && j
>= 3)
834 i
= j
; /* one line different - caller might be looping, so stay in this cache */
836 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
837 if (line
> edit
->line_numbers
[j
])
838 edit
->line_offsets
[i
] =
839 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
841 edit
->line_offsets
[i
] =
842 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
843 edit
->line_numbers
[i
] = line
;
844 return edit
->line_offsets
[i
];
847 /* --------------------------------------------------------------------------------------------- */
848 /** moves up until a blank line is reached, or until just
849 before a non-blank line is reached */
852 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
855 if (edit
->curs_line
> 1)
857 if (line_is_blank (edit
, edit
->curs_line
))
859 if (line_is_blank (edit
, edit
->curs_line
- 1))
861 for (i
= edit
->curs_line
- 1; i
; i
--)
862 if (!line_is_blank (edit
, i
))
870 for (i
= edit
->curs_line
- 1; i
; i
--)
871 if (line_is_blank (edit
, i
))
877 for (i
= edit
->curs_line
- 1; i
; i
--)
878 if (line_is_blank (edit
, i
))
882 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
885 /* --------------------------------------------------------------------------------------------- */
886 /** moves down until a blank line is reached, or until just
887 before a non-blank line is reached */
890 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
893 if (edit
->curs_line
>= edit
->total_lines
- 1)
895 i
= edit
->total_lines
;
899 if (line_is_blank (edit
, edit
->curs_line
))
901 if (line_is_blank (edit
, edit
->curs_line
+ 1))
903 for (i
= edit
->curs_line
+ 1; i
; i
++)
904 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
912 for (i
= edit
->curs_line
+ 1; i
; i
++)
913 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
919 for (i
= edit
->curs_line
+ 1; i
; i
++)
920 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
924 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
927 /* --------------------------------------------------------------------------------------------- */
930 edit_begin_page (WEdit
* edit
)
932 edit_update_curs_row (edit
);
933 edit_move_up (edit
, edit
->curs_row
, 0);
936 /* --------------------------------------------------------------------------------------------- */
939 edit_end_page (WEdit
* edit
)
941 edit_update_curs_row (edit
);
942 edit_move_down (edit
, edit
->widget
.lines
- edit
->curs_row
- 1, 0);
946 /* --------------------------------------------------------------------------------------------- */
947 /** goto beginning of text */
950 edit_move_to_top (WEdit
* edit
)
954 edit_cursor_move (edit
, -edit
->curs1
);
955 edit_move_to_prev_col (edit
, 0);
956 edit
->force
|= REDRAW_PAGE
;
957 edit
->search_start
= 0;
958 edit_update_curs_row (edit
);
963 /* --------------------------------------------------------------------------------------------- */
964 /** goto end of text */
967 edit_move_to_bottom (WEdit
* edit
)
969 if (edit
->curs_line
< edit
->total_lines
)
971 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
972 edit
->start_display
= edit
->last_byte
;
973 edit
->start_line
= edit
->total_lines
;
974 edit_scroll_upward (edit
, edit
->widget
.lines
- 1);
975 edit
->force
|= REDRAW_PAGE
;
979 /* --------------------------------------------------------------------------------------------- */
980 /** goto beginning of line */
983 edit_cursor_to_bol (WEdit
* edit
)
985 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
986 edit
->search_start
= edit
->curs1
;
987 edit
->prev_col
= edit_get_col (edit
);
991 /* --------------------------------------------------------------------------------------------- */
992 /** goto end of line */
995 edit_cursor_to_eol (WEdit
* edit
)
997 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
998 edit
->search_start
= edit
->curs1
;
999 edit
->prev_col
= edit_get_col (edit
);
1003 /* --------------------------------------------------------------------------------------------- */
1005 static unsigned long
1010 const char option_chars_move_whole_word
[] =
1011 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1017 if (*option_chars_move_whole_word
== '!')
1019 return 0x80000000UL
;
1021 if (g_ascii_isupper ((gchar
) c
))
1023 else if (g_ascii_islower ((gchar
) c
))
1025 else if (g_ascii_isalpha (c
))
1027 else if (isdigit (c
))
1029 else if (isspace (c
))
1031 q
= strchr (option_chars_move_whole_word
, c
);
1033 return 0xFFFFFFFFUL
;
1036 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1041 while ((q
= strchr (q
+ 1, c
)));
1045 /* --------------------------------------------------------------------------------------------- */
1048 edit_left_word_move (WEdit
* edit
, int s
)
1053 if (edit
->column_highlight
1054 && edit
->mark1
!= edit
->mark2
1055 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1057 edit_cursor_move (edit
, -1);
1060 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1061 c2
= edit_get_byte (edit
, edit
->curs1
);
1062 if (c1
== '\n' || c2
== '\n')
1064 if (!(my_type_of (c1
) & my_type_of (c2
)))
1066 if (isspace (c1
) && !isspace (c2
))
1069 if (!isspace (c1
) && isspace (c2
))
1074 /* --------------------------------------------------------------------------------------------- */
1077 edit_left_word_move_cmd (WEdit
* edit
)
1079 edit_left_word_move (edit
, 0);
1080 edit
->force
|= REDRAW_PAGE
;
1083 /* --------------------------------------------------------------------------------------------- */
1086 edit_right_word_move (WEdit
* edit
, int s
)
1091 if (edit
->column_highlight
1092 && edit
->mark1
!= edit
->mark2
1093 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
1095 edit_cursor_move (edit
, 1);
1096 if (edit
->curs1
>= edit
->last_byte
)
1098 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1099 c2
= edit_get_byte (edit
, edit
->curs1
);
1100 if (c1
== '\n' || c2
== '\n')
1102 if (!(my_type_of (c1
) & my_type_of (c2
)))
1104 if (isspace (c1
) && !isspace (c2
))
1107 if (!isspace (c1
) && isspace (c2
))
1112 /* --------------------------------------------------------------------------------------------- */
1115 edit_right_word_move_cmd (WEdit
* edit
)
1117 edit_right_word_move (edit
, 0);
1118 edit
->force
|= REDRAW_PAGE
;
1121 /* --------------------------------------------------------------------------------------------- */
1124 edit_right_char_move_cmd (WEdit
* edit
)
1130 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
1136 c
= edit_get_byte (edit
, edit
->curs1
);
1138 if (option_cursor_beyond_eol
&& c
== '\n')
1144 edit_cursor_move (edit
, cw
);
1148 /* --------------------------------------------------------------------------------------------- */
1151 edit_left_char_move_cmd (WEdit
* edit
)
1154 if (edit
->column_highlight
1155 && option_cursor_beyond_eol
1156 && edit
->mark1
!= edit
->mark2
1157 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1161 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1165 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1171 edit_cursor_move (edit
, -cw
);
1175 /* --------------------------------------------------------------------------------------------- */
1176 /** Up or down cursor moving.
1177 direction = TRUE - move up
1182 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
1185 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
1194 edit
->force
|= REDRAW_PAGE
;
1198 edit_scroll_upward (edit
, i
);
1200 edit_scroll_downward (edit
, i
);
1202 p
= edit_bol (edit
, edit
->curs1
);
1204 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
1206 edit_cursor_move (edit
, p
- edit
->curs1
);
1208 edit_move_to_prev_col (edit
, p
);
1210 /* search start of current multibyte char (like CJK) */
1211 if (edit
->curs1
+ 1 < edit
->last_byte
)
1213 edit_right_char_move_cmd (edit
);
1214 edit_left_char_move_cmd (edit
);
1217 edit
->search_start
= edit
->curs1
;
1218 edit
->found_len
= 0;
1221 /* --------------------------------------------------------------------------------------------- */
1224 edit_right_delete_word (WEdit
* edit
)
1229 if (edit
->curs1
>= edit
->last_byte
)
1231 c1
= edit_delete (edit
, 1);
1232 c2
= edit_get_byte (edit
, edit
->curs1
);
1233 if (c1
== '\n' || c2
== '\n')
1235 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1237 if (!(my_type_of (c1
) & my_type_of (c2
)))
1242 /* --------------------------------------------------------------------------------------------- */
1245 edit_left_delete_word (WEdit
* edit
)
1250 if (edit
->curs1
<= 0)
1252 c1
= edit_backspace (edit
, 1);
1253 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1254 if (c1
== '\n' || c2
== '\n')
1256 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1258 if (!(my_type_of (c1
) & my_type_of (c2
)))
1263 /* --------------------------------------------------------------------------------------------- */
1265 the start column position is not recorded, and hence does not
1266 undo as it happed. But who would notice.
1270 edit_do_undo (WEdit
* edit
)
1275 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1277 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1284 edit_cursor_move (edit
, 1);
1287 edit_cursor_move (edit
, -1);
1291 edit_backspace (edit
, 1);
1295 edit_delete (edit
, 1);
1298 edit
->column_highlight
= 1;
1301 edit
->column_highlight
= 0;
1304 if (ac
>= 256 && ac
< 512)
1305 edit_insert_ahead (edit
, ac
- 256);
1306 if (ac
>= 0 && ac
< 256)
1307 edit_insert (edit
, ac
);
1309 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1311 edit
->mark1
= ac
- MARK_1
;
1312 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1314 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1316 edit
->mark2
= ac
- MARK_2
;
1317 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1319 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1321 edit
->end_mark_curs
= ac
- MARK_CURS
;
1324 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1327 if (edit
->start_display
> ac
- KEY_PRESS
)
1329 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1330 edit
->force
|= REDRAW_PAGE
;
1332 else if (edit
->start_display
< ac
- KEY_PRESS
)
1334 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1335 edit
->force
|= REDRAW_PAGE
;
1337 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1338 edit_update_curs_row (edit
);
1341 edit
->undo_stack_disable
= 0;
1345 edit_do_redo (WEdit
* edit
)
1350 if (edit
->redo_stack_reset
)
1354 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1361 edit_cursor_move (edit
, 1);
1364 edit_cursor_move (edit
, -1);
1367 edit_backspace (edit
, 1);
1370 edit_delete (edit
, 1);
1373 edit
->column_highlight
= 1;
1376 edit
->column_highlight
= 0;
1379 if (ac
>= 256 && ac
< 512)
1380 edit_insert_ahead (edit
, ac
- 256);
1381 if (ac
>= 0 && ac
< 256)
1382 edit_insert (edit
, ac
);
1384 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1386 edit
->mark1
= ac
- MARK_1
;
1387 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1389 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1391 edit
->mark2
= ac
- MARK_2
;
1392 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1394 /* more than one pop usually means something big */
1396 edit
->force
|= REDRAW_PAGE
;
1399 if (edit
->start_display
> ac
- KEY_PRESS
)
1401 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1402 edit
->force
|= REDRAW_PAGE
;
1404 else if (edit
->start_display
< ac
- KEY_PRESS
)
1406 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1407 edit
->force
|= REDRAW_PAGE
;
1409 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1410 edit_update_curs_row (edit
);
1416 edit_group_undo (WEdit
* edit
)
1418 long ac
= KEY_PRESS
;
1419 long cur_ac
= KEY_PRESS
;
1420 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1422 cur_ac
= get_prev_undo_action (edit
);
1423 edit_do_undo (edit
);
1424 ac
= get_prev_undo_action (edit
);
1425 /* exit from cycle if option_group_undo is not set,
1426 * and make single UNDO operation
1428 if (!option_group_undo
)
1433 /* --------------------------------------------------------------------------------------------- */
1436 edit_delete_to_line_end (WEdit
* edit
)
1438 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
1442 edit_delete (edit
, 1);
1446 /* --------------------------------------------------------------------------------------------- */
1449 edit_delete_to_line_begin (WEdit
* edit
)
1451 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
1455 edit_backspace (edit
, 1);
1459 /* --------------------------------------------------------------------------------------------- */
1462 is_aligned_on_a_tab (WEdit
* edit
)
1464 edit_update_curs_col (edit
);
1465 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
1466 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
1469 /* --------------------------------------------------------------------------------------------- */
1472 right_of_four_spaces (WEdit
* edit
)
1475 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1476 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1478 return is_aligned_on_a_tab (edit
);
1482 /* --------------------------------------------------------------------------------------------- */
1485 left_of_four_spaces (WEdit
* edit
)
1488 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1489 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1491 return is_aligned_on_a_tab (edit
);
1495 /* --------------------------------------------------------------------------------------------- */
1498 edit_auto_indent (WEdit
* edit
)
1503 /* use the previous line as a template */
1504 p
= edit_move_backward (edit
, p
, 1);
1505 /* copy the leading whitespace of the line */
1507 { /* no range check - the line _is_ \n-terminated */
1508 c
= edit_get_byte (edit
, p
++);
1509 if (c
!= ' ' && c
!= '\t')
1511 edit_insert (edit
, c
);
1515 /* --------------------------------------------------------------------------------------------- */
1518 edit_double_newline (WEdit
* edit
)
1520 edit_insert (edit
, '\n');
1521 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1523 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1525 edit
->force
|= REDRAW_PAGE
;
1526 edit_insert (edit
, '\n');
1530 /* --------------------------------------------------------------------------------------------- */
1533 insert_spaces_tab (WEdit
* edit
, gboolean half
)
1537 edit_update_curs_col (edit
);
1538 i
= option_tab_spacing
* space_width
;
1541 i
= ((edit
->curs_col
/ i
) + 1) * i
- edit
->curs_col
;
1544 edit_insert (edit
, ' ');
1549 /* --------------------------------------------------------------------------------------------- */
1552 edit_tab_cmd (WEdit
* edit
)
1556 if (option_fake_half_tabs
)
1558 if (is_in_indent (edit
))
1560 /*insert a half tab (usually four spaces) unless there is a
1561 half tab already behind, then delete it and insert a
1563 if (option_fill_tabs_with_spaces
|| !right_of_four_spaces (edit
))
1564 insert_spaces_tab (edit
, TRUE
);
1567 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1568 edit_backspace (edit
, 1);
1569 edit_insert (edit
, '\t');
1574 if (option_fill_tabs_with_spaces
)
1575 insert_spaces_tab (edit
, FALSE
);
1577 edit_insert (edit
, '\t');
1580 /* --------------------------------------------------------------------------------------------- */
1583 check_and_wrap_line (WEdit
* edit
)
1586 if (!option_typewriter_wrap
)
1588 edit_update_curs_col (edit
);
1589 if (edit
->curs_col
< option_word_wrap_line_length
)
1595 c
= edit_get_byte (edit
, curs
);
1596 if (c
== '\n' || curs
<= 0)
1598 edit_insert (edit
, '\n');
1601 if (c
== ' ' || c
== '\t')
1603 int current
= edit
->curs1
;
1604 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
1605 edit_insert (edit
, '\n');
1606 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
1612 /* --------------------------------------------------------------------------------------------- */
1613 /** this find the matching bracket in either direction, and sets edit->bracket */
1616 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
1618 const char *const b
= "{}{[][()(", *p
;
1619 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
1620 unsigned long j
= 0;
1622 edit_update_curs_row (edit
);
1623 c
= edit_get_byte (edit
, edit
->curs1
);
1626 if (!furthest_bracket_search
)
1627 furthest_bracket_search
--;
1628 /* not on a bracket at all */
1631 /* the matching bracket */
1633 /* going left or right? */
1634 if (strchr ("{[(", c
))
1636 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
1638 /* out of buffer? */
1639 if (q
>= edit
->last_byte
|| q
< 0)
1641 a
= edit_get_byte (edit
, q
);
1642 /* don't want to eat CPU */
1643 if (j
++ > furthest_bracket_search
)
1645 /* out of screen? */
1648 if (q
< edit
->start_display
)
1650 /* count lines if searching downward */
1651 if (inc
> 0 && a
== '\n')
1652 if (n
++ >= edit
->widget
.lines
- edit
->curs_row
) /* out of screen */
1655 /* count bracket depth */
1656 i
+= (a
== c
) - (a
== d
);
1657 /* return if bracket depth is zero */
1665 /* --------------------------------------------------------------------------------------------- */
1668 edit_goto_matching_bracket (WEdit
* edit
)
1672 q
= edit_get_bracket (edit
, 0, 0);
1675 edit
->bracket
= edit
->curs1
;
1676 edit
->force
|= REDRAW_PAGE
;
1677 edit_cursor_move (edit
, q
- edit
->curs1
);
1681 /* --------------------------------------------------------------------------------------------- */
1684 edit_move_block_to_right (WEdit
* edit
)
1686 long start_mark
, end_mark
;
1687 long cur_bol
, start_bol
;
1689 if (eval_marks (edit
, &start_mark
, &end_mark
))
1692 start_bol
= edit_bol (edit
, start_mark
);
1693 cur_bol
= edit_bol (edit
, end_mark
- 1);
1697 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1698 if (option_fill_tabs_with_spaces
)
1699 insert_spaces_tab (edit
, option_fake_half_tabs
);
1701 edit_insert (edit
, '\t');
1702 edit_cursor_move (edit
, edit_bol (edit
, cur_bol
) - edit
->curs1
);
1707 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1709 while (cur_bol
>= start_bol
);
1711 edit
->force
|= REDRAW_PAGE
;
1714 /* --------------------------------------------------------------------------------------------- */
1717 edit_move_block_to_left (WEdit
* edit
)
1719 long start_mark
, end_mark
;
1720 long cur_bol
, start_bol
;
1723 if (eval_marks (edit
, &start_mark
, &end_mark
))
1726 start_bol
= edit_bol (edit
, start_mark
);
1727 cur_bol
= edit_bol (edit
, end_mark
- 1);
1734 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1736 if (option_fake_half_tabs
)
1737 del_tab_width
= HALF_TAB_SIZE
;
1739 del_tab_width
= option_tab_spacing
;
1741 next_char
= edit_get_byte (edit
, edit
->curs1
);
1742 if (next_char
== '\t')
1743 edit_delete (edit
, 1);
1744 else if (next_char
== ' ')
1745 for (i
= 1; i
<= del_tab_width
; i
++)
1747 if (next_char
== ' ')
1748 edit_delete (edit
, 1);
1749 next_char
= edit_get_byte (edit
, edit
->curs1
);
1755 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1757 while (cur_bol
>= start_bol
);
1759 edit
->force
|= REDRAW_PAGE
;
1762 /* --------------------------------------------------------------------------------------------- */
1764 * prints at the cursor
1765 * @returns the number of chars printed
1769 edit_print_string (WEdit
* e
, const char *s
)
1773 while (s
[i
] != '\0')
1774 edit_execute_cmd (e
, CK_InsertChar
, (unsigned char) s
[i
++]);
1775 e
->force
|= REDRAW_COMPLETELY
;
1776 edit_update_screen (e
);
1780 /* --------------------------------------------------------------------------------------------- */
1781 /*** public functions ****************************************************************************/
1782 /* --------------------------------------------------------------------------------------------- */
1784 /** User edit menu, like user menu (F2) but only in editor. */
1787 user_menu (WEdit
* edit
, const char *menu_file
, int selected_entry
)
1792 long start_mark
, end_mark
;
1794 vfs_path_t
*block_file_vpath
;
1796 block_file
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
1797 block_file_vpath
= vfs_path_from_str (block_file
);
1799 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
1801 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1803 /* run shell scripts from menu */
1804 if (user_menu_cmd (edit
, menu_file
, selected_entry
)
1805 && (mc_stat (block_file_vpath
, &status
) == 0) && (status
.st_size
!= 0))
1810 /* i.e. we have marked block */
1812 rc
= edit_block_delete_cmd (edit
);
1818 ins_len
= edit_insert_file (edit
, block_file_vpath
, LB_ASIS
);
1819 if (nomark
== 0 && ins_len
> 0)
1820 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1822 /* truncate block file */
1823 fd
= fopen (block_file
, "w");
1827 g_free (block_file
);
1828 vfs_path_free (block_file_vpath
);
1830 edit_cursor_move (edit
, curs
- edit
->curs1
);
1831 edit
->force
|= REDRAW_PAGE
;
1832 send_message ((Widget
*) edit
, WIDGET_DRAW
, 0);
1835 /* --------------------------------------------------------------------------------------------- */
1838 edit_get_byte (WEdit
* edit
, long byte_index
)
1841 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1844 if (byte_index
>= edit
->curs1
)
1846 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1847 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
1851 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
1855 /* --------------------------------------------------------------------------------------------- */
1858 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1863 gchar
*next_ch
= NULL
;
1865 gchar utf8_buf
[UTF8_CHAR_LEN
+ 1];
1867 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1873 str
= edit_get_byte_ptr (edit
, byte_index
);
1881 res
= g_utf8_get_char_validated (str
, -1);
1885 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1887 for (i
= 0; i
< UTF8_CHAR_LEN
; i
++)
1888 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
);
1889 utf8_buf
[UTF8_CHAR_LEN
] = '\0';
1891 res
= g_utf8_get_char_validated (str
, -1);
1902 /* Calculate UTF-8 char width */
1903 next_ch
= g_utf8_next_char (str
);
1906 width
= next_ch
- str
;
1914 *char_width
= width
;
1918 /* --------------------------------------------------------------------------------------------- */
1921 edit_get_write_filter (const vfs_path_t
* write_name_vpath
, const vfs_path_t
* filename_vpath
)
1924 char *p
, *writename
;
1925 const vfs_path_element_t
*path_element
;
1927 i
= edit_find_filter (filename_vpath
);
1931 path_element
= vfs_path_get_by_index (write_name_vpath
, -1);
1932 writename
= name_quote (path_element
->path
, 0);
1933 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
1938 /* --------------------------------------------------------------------------------------------- */
1941 edit_write_stream (WEdit
* edit
, FILE * f
)
1945 if (edit
->lb
== LB_ASIS
)
1947 for (i
= 0; i
< edit
->last_byte
; i
++)
1948 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
1953 /* change line breaks */
1954 for (i
= 0; i
< edit
->last_byte
; i
++)
1956 unsigned char c
= edit_get_byte (edit
, i
);
1958 if (!(c
== '\n' || c
== '\r'))
1960 /* not line break */
1961 if (fputc (c
, f
) < 0)
1965 { /* (c == '\n' || c == '\r') */
1966 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
1970 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
1971 /* put one line break unconditionally */
1972 if (fputc ('\n', f
) < 0)
1975 i
++; /* 2 chars are processed */
1977 if (c
== '\r' && c1
== '\n')
1978 /* Windows line break; go to the next char */
1981 if (c
== '\r' && c1
== '\r')
1983 /* two Macintosh line breaks; put second line break */
1984 if (fputc ('\n', f
) < 0)
1989 if (fputc (c1
, f
) < 0)
1993 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
1994 /* put one line break unconditionally */
1995 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
1998 if (c
== '\r' && c1
== '\n')
1999 /* Windows line break; go to the next char */
2003 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
2004 /* put one line break unconditionally */
2005 if (fputc ('\r', f
) < 0)
2008 i
++; /* 2 chars are processed */
2010 if (c
== '\r' && c1
== '\n')
2011 /* Windows line break; go to the next char */
2014 if (c
== '\n' && c1
== '\n')
2016 /* two Windows line breaks; put second line break */
2017 if (fputc ('\r', f
) < 0)
2022 if (fputc (c1
, f
) < 0)
2025 case LB_ASIS
: /* default without changes */
2031 return edit
->last_byte
;
2034 /* --------------------------------------------------------------------------------------------- */
2037 is_break_char (char c
)
2039 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
2042 /* --------------------------------------------------------------------------------------------- */
2045 edit_get_word_from_pos (WEdit
* edit
, long start_pos
, long *start
, gsize
* len
, gsize
* cut
)
2049 GString
*match_expr
;
2050 unsigned char *bufpos
;
2053 for (word_start
= start_pos
; word_start
!= 0; word_start
--, cut_len
++)
2055 c1
= edit_get_byte (edit
, word_start
);
2056 c2
= edit_get_byte (edit
, word_start
- 1);
2058 if (is_break_char (c1
) != is_break_char (c2
) || c1
== '\n' || c2
== '\n')
2062 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
2063 match_expr
= g_string_sized_new (16);
2067 c1
= edit_get_byte (edit
, word_start
+ match_expr
->len
);
2068 c2
= edit_get_byte (edit
, word_start
+ match_expr
->len
+ 1);
2069 g_string_append_c (match_expr
, c1
);
2071 while (!(is_break_char (c1
) != is_break_char (c2
) || c1
== '\n' || c2
== '\n'));
2073 *len
= match_expr
->len
;
2074 *start
= word_start
;
2077 return g_string_free (match_expr
, FALSE
);
2080 /* --------------------------------------------------------------------------------------------- */
2081 /** inserts a file at the cursor, returns count of inserted bytes on success */
2084 edit_insert_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
, LineBreaks lb_type
)
2089 p
= edit_get_filter (filename_vpath
);
2093 long current
= edit
->curs1
;
2095 f
= (FILE *) popen (p
, "r");
2098 edit_insert_stream (edit
, f
);
2099 ins_len
= edit
->curs1
- current
;
2100 edit_cursor_move (edit
, -ins_len
);
2105 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
2106 edit_error_dialog (_("Error"), errmsg
);
2115 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
2116 edit_error_dialog (_("Error"), errmsg
);
2124 int i
, file
, blocklen
;
2125 long current
= edit
->curs1
;
2126 int vertical_insertion
= 0;
2129 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
2133 buf
= g_malloc0 (TEMP_BUF_LEN
);
2134 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2137 /* if contain signature VERTICAL_MAGIC then it vertical block */
2138 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2139 vertical_insertion
= 1;
2141 mc_lseek (file
, 0, SEEK_SET
);
2144 if (vertical_insertion
)
2149 blocklen
= edit_insert_column_of_text_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
2150 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2151 /* highlight inserted text then not persistent blocks */
2152 if (!option_persistent_selections
)
2154 if (!edit
->column_highlight
)
2155 edit_push_undo_action (edit
, COLUMN_OFF
);
2156 edit
->column_highlight
= 1;
2161 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2163 for (i
= 0; i
< blocklen
; i
++)
2167 if (lb_type
== LB_MAC
)
2168 edit_insert (edit
, '\n');
2169 else if (lb_type
== LB_WIN
)
2172 edit_insert (edit
, '\r');
2175 edit_insert (edit
, buf
[i
]);
2178 /* highlight inserted text then not persistent blocks */
2179 if (!option_persistent_selections
&& edit
->modified
)
2181 edit_set_markers (edit
, edit
->curs1
, current
, 0, 0);
2182 if (edit
->column_highlight
)
2183 edit_push_undo_action (edit
, COLUMN_ON
);
2184 edit
->column_highlight
= 0;
2188 edit
->force
|= REDRAW_PAGE
;
2189 ins_len
= edit
->curs1
- current
;
2190 edit_cursor_move (edit
, -ins_len
);
2200 /* --------------------------------------------------------------------------------------------- */
2202 * Fill in the edit structure. Return NULL on failure. Pass edit as
2203 * NULL to allocate a new structure.
2205 * If line is 0, try to restore saved position. Otherwise put the
2206 * cursor on that line and show it in the middle of the screen.
2210 edit_init (WEdit
* edit
, int y
, int x
, int lines
, int cols
, const vfs_path_t
* filename_vpath
,
2213 gboolean to_free
= FALSE
;
2215 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2216 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
2219 edit_purge_widget (edit
);
2224 * Expand option_whole_chars_search by national letters using
2228 static char option_whole_chars_search_buf
[256];
2230 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
2233 size_t len
= str_term_width1 (option_whole_chars_search
);
2235 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
2237 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
2239 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
2241 option_whole_chars_search_buf
[len
++] = i
;
2245 option_whole_chars_search_buf
[len
] = 0;
2246 option_whole_chars_search
= option_whole_chars_search_buf
;
2248 #endif /* ENABLE_NLS */
2249 edit
= g_malloc0 (sizeof (WEdit
));
2253 edit
->drag_state
= MCEDIT_DRAG_NORMAL
;
2256 edit
->widget
.lines
= lines
;
2257 edit
->widget
.cols
= cols
;
2258 edit_save_size (edit
);
2259 edit
->fullscreen
= TRUE
;
2261 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2262 edit
->stat1
.st_uid
= getuid ();
2263 edit
->stat1
.st_gid
= getgid ();
2264 edit
->stat1
.st_mtime
= 0;
2268 edit
->force
|= REDRAW_PAGE
;
2270 /* set file name before load file */
2271 edit_set_filename (edit
, filename_vpath
);
2273 edit
->undo_stack_size
= START_STACK_SIZE
;
2274 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2275 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2277 edit
->redo_stack_size
= START_STACK_SIZE
;
2278 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2279 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2281 edit
->highlight
= 0;
2283 edit
->converter
= str_cnv_from_term
;
2284 edit_set_codeset (edit
);
2286 if (!edit_load_file (edit
))
2288 /* edit_load_file already gives an error message */
2294 edit
->loading_done
= 1;
2297 edit_load_syntax (edit
, NULL
, NULL
);
2300 edit_get_syntax_color (edit
, -1, &color
);
2303 /* load saved cursor position */
2304 if ((line
== 0) && option_save_position
)
2305 edit_load_position (edit
);
2310 edit_move_display (edit
, line
- 1);
2311 edit_move_to_line (edit
, line
- 1);
2314 edit_load_macro_cmd (edit
);
2319 /* --------------------------------------------------------------------------------------------- */
2321 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2323 edit_clean (WEdit
* edit
)
2330 /* a stale lock, remove it */
2332 edit
->locked
= unlock_file (edit
->filename_vpath
);
2334 /* save cursor position */
2335 if (option_save_position
)
2336 edit_save_position (edit
);
2337 else if (edit
->serialized_bookmarks
!= NULL
)
2338 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2340 /* File specified on the mcedit command line and never saved */
2341 if (edit
->delete_file
)
2342 unlink (vfs_path_get_last_path_str (edit
->filename_vpath
));
2344 edit_free_syntax_rules (edit
);
2345 book_mark_flush (edit
, -1);
2346 for (; j
<= MAXBUFF
; j
++)
2348 g_free (edit
->buffers1
[j
]);
2349 g_free (edit
->buffers2
[j
]);
2352 g_free (edit
->undo_stack
);
2353 g_free (edit
->redo_stack
);
2354 vfs_path_free (edit
->filename_vpath
);
2355 vfs_path_free (edit
->dir_vpath
);
2356 mc_search_free (edit
->search
);
2357 edit
->search
= NULL
;
2359 if (edit
->converter
!= str_cnv_from_term
)
2360 str_close_conv (edit
->converter
);
2362 edit_purge_widget (edit
);
2367 /* --------------------------------------------------------------------------------------------- */
2370 * Load a new file into the editor and set line. If it fails, preserve the old file.
2371 * To do it, allocate a new widget, initialize it and, if the new file
2372 * was loaded, copy the data to the old widget.
2374 * @returns TRUE on success, FALSE on failure.
2377 edit_reload_line (WEdit
* edit
, const vfs_path_t
* filename_vpath
, long line
)
2380 int y
= edit
->widget
.y
;
2381 int x
= edit
->widget
.x
;
2382 int lines
= edit
->widget
.lines
;
2383 int columns
= edit
->widget
.cols
;
2385 e
= g_malloc0 (sizeof (WEdit
));
2386 e
->widget
= edit
->widget
;
2388 if (edit_init (e
, y
, x
, lines
, columns
, filename_vpath
, line
) == NULL
)
2395 memcpy (edit
, e
, sizeof (WEdit
));
2401 /* --------------------------------------------------------------------------------------------- */
2404 edit_set_codeset (WEdit
* edit
)
2410 get_codepage_id (mc_global
.source_codepage
>=
2411 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2416 conv
= str_crt_conv_from (cp_id
);
2417 if (conv
!= INVALID_CONV
)
2419 if (edit
->converter
!= str_cnv_from_term
)
2420 str_close_conv (edit
->converter
);
2421 edit
->converter
= conv
;
2426 edit
->utf8
= str_isutf8 (cp_id
);
2433 /* --------------------------------------------------------------------------------------------- */
2435 Recording stack for undo:
2436 The following is an implementation of a compressed stack. Identical
2437 pushes are recorded by a negative prefix indicating the number of times the
2438 same char was pushed. This saves space for repeated curs-left or curs-right
2455 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2456 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2457 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2458 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2461 The only way the cursor moves or the buffer is changed is through the routines:
2462 insert, backspace, insert_ahead, delete, and cursor_move.
2463 These record the reverse undo movements onto the stack each time they are
2466 Each key press results in a set of actions (insert; delete ...). So each time
2467 a key is pressed the current position of start_display is pushed as
2468 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2469 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2470 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2475 edit_push_undo_action (WEdit
* edit
, long c
, ...)
2477 unsigned long sp
= edit
->undo_stack_pointer
;
2481 /* first enlarge the stack if necessary */
2482 if (sp
> edit
->undo_stack_size
- 10)
2484 if (option_max_undo
< 256)
2485 option_max_undo
= 256;
2486 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2488 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2491 edit
->undo_stack
= t
;
2492 edit
->undo_stack_size
<<= 1;
2493 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2497 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2498 if (edit
->undo_stack_disable
)
2500 edit_push_redo_action (edit
, KEY_PRESS
);
2501 edit_push_redo_action (edit
, c
);
2504 else if (edit
->redo_stack_reset
)
2506 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2509 if (edit
->undo_stack_bottom
!= sp
2510 && spm1
!= edit
->undo_stack_bottom
2511 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2514 if (edit
->undo_stack
[spm1
] < 0)
2516 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2519 if (edit
->undo_stack
[spm1
] > -1000000000)
2521 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2523 edit
->undo_stack
[spm1
]--;
2531 d
= edit
->undo_stack
[spm1
];
2535 return; /* --> no need to push multiple do-nothings */
2536 edit
->undo_stack
[sp
] = -2;
2541 edit
->undo_stack
[sp
] = c
;
2544 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2546 /* if the sp wraps round and catches the undo_stack_bottom then erase
2547 * the first set of actions on the stack to make space - by moving
2548 * undo_stack_bottom forward one "key press" */
2549 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2550 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2551 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2554 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2556 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2557 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2559 /*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: */
2560 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2561 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2563 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2568 edit_push_redo_action (WEdit
* edit
, long c
, ...)
2570 unsigned long sp
= edit
->redo_stack_pointer
;
2573 /* first enlarge the stack if necessary */
2574 if (sp
> edit
->redo_stack_size
- 10)
2576 if (option_max_undo
< 256)
2577 option_max_undo
= 256;
2578 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2580 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2583 edit
->redo_stack
= t
;
2584 edit
->redo_stack_size
<<= 1;
2585 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2589 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2591 if (edit
->redo_stack_bottom
!= sp
2592 && spm1
!= edit
->redo_stack_bottom
2593 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2596 if (edit
->redo_stack
[spm1
] < 0)
2598 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2601 if (edit
->redo_stack
[spm1
] > -1000000000)
2603 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2604 edit
->redo_stack
[spm1
]--;
2611 d
= edit
->redo_stack
[spm1
];
2615 return; /* --> no need to push multiple do-nothings */
2616 edit
->redo_stack
[sp
] = -2;
2617 goto redo_check_bottom
;
2621 edit
->redo_stack
[sp
] = c
;
2624 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2626 /* if the sp wraps round and catches the redo_stack_bottom then erase
2627 * the first set of actions on the stack to make space - by moving
2628 * redo_stack_bottom forward one "key press" */
2629 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2630 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2631 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2634 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2636 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2637 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2640 * If a single key produced enough pushes to wrap all the way round then
2641 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2642 * The stack is then initialised:
2645 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2646 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2647 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2651 /* --------------------------------------------------------------------------------------------- */
2653 Basic low level single character buffer alterations and movements at the cursor.
2654 Returns char passed over, inserted or removed.
2658 edit_insert (WEdit
* edit
, int c
)
2660 /* check if file has grown to large */
2661 if (edit
->last_byte
>= SIZE_LIMIT
)
2664 /* first we must update the position of the display window */
2665 if (edit
->curs1
< edit
->start_display
)
2667 edit
->start_display
++;
2672 /* Mark file as modified, unless the file hasn't been fully loaded */
2673 if (edit
->loading_done
)
2675 edit_modification (edit
);
2678 /* now we must update some info on the file and check if a redraw is required */
2681 if (edit
->book_mark
)
2682 book_mark_inc (edit
, edit
->curs_line
);
2684 edit
->total_lines
++;
2685 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2688 /* save the reverse command onto the undo stack */
2689 /* ordinary char and not space */
2691 edit_push_undo_action (edit
, BACKSPACE
);
2693 edit_push_undo_action (edit
, BACKSPACE_BR
);
2694 /* update markers */
2695 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2696 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2697 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2699 /* add a new buffer if we've reached the end of the last one */
2700 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2701 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2703 /* perform the insertion */
2704 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2705 = (unsigned char) c
;
2707 /* update file length */
2710 /* update cursor position */
2714 /* --------------------------------------------------------------------------------------------- */
2715 /** same as edit_insert and move left */
2718 edit_insert_ahead (WEdit
* edit
, int c
)
2720 if (edit
->last_byte
>= SIZE_LIMIT
)
2723 if (edit
->curs1
< edit
->start_display
)
2725 edit
->start_display
++;
2729 edit_modification (edit
);
2732 if (edit
->book_mark
)
2733 book_mark_inc (edit
, edit
->curs_line
);
2734 edit
->total_lines
++;
2735 edit
->force
|= REDRAW_AFTER_CURSOR
;
2737 /* ordinary char and not space */
2739 edit_push_undo_action (edit
, DELCHAR
);
2741 edit_push_undo_action (edit
, DELCHAR_BR
);
2743 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2744 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2745 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2747 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2748 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2749 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2750 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2757 /* --------------------------------------------------------------------------------------------- */
2760 edit_delete (WEdit
* edit
, const int byte_delete
)
2770 /* if byte_delete = 1 then delete only one byte not multibyte char */
2771 if (edit
->utf8
&& byte_delete
== 0)
2773 edit_get_utf (edit
, edit
->curs1
, &cw
);
2778 if (edit
->mark2
!= edit
->mark1
)
2779 edit_push_markers (edit
);
2781 for (i
= 1; i
<= cw
; i
++)
2783 if (edit
->mark1
> edit
->curs1
)
2786 edit
->end_mark_curs
--;
2788 if (edit
->mark2
> edit
->curs1
)
2790 if (edit
->last_get_rule
> edit
->curs1
)
2791 edit
->last_get_rule
--;
2793 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2795 1) & M_EDIT_BUF_SIZE
) - 1];
2797 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2799 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2800 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2804 edit_push_undo_action (edit
, p
+ 256);
2807 edit_modification (edit
);
2810 if (edit
->book_mark
)
2811 book_mark_dec (edit
, edit
->curs_line
);
2812 edit
->total_lines
--;
2813 edit
->force
|= REDRAW_AFTER_CURSOR
;
2815 if (edit
->curs1
< edit
->start_display
)
2817 edit
->start_display
--;
2825 /* --------------------------------------------------------------------------------------------- */
2828 edit_backspace (WEdit
* edit
, const int byte_delete
)
2834 if (edit
->curs1
== 0)
2839 if (edit
->mark2
!= edit
->mark1
)
2840 edit_push_markers (edit
);
2842 if (edit
->utf8
&& byte_delete
== 0)
2844 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
2848 for (i
= 1; i
<= cw
; i
++)
2850 if (edit
->mark1
>= edit
->curs1
)
2853 edit
->end_mark_curs
--;
2855 if (edit
->mark2
>= edit
->curs1
)
2857 if (edit
->last_get_rule
>= edit
->curs1
)
2858 edit
->last_get_rule
--;
2860 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
2861 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
2862 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2864 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2865 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2869 edit_push_undo_action (edit
, p
);
2871 edit_modification (edit
);
2874 if (edit
->book_mark
)
2875 book_mark_dec (edit
, edit
->curs_line
);
2877 edit
->total_lines
--;
2878 edit
->force
|= REDRAW_AFTER_CURSOR
;
2881 if (edit
->curs1
< edit
->start_display
)
2883 edit
->start_display
--;
2891 /* --------------------------------------------------------------------------------------------- */
2892 /** moves the cursor right or left: increment positive or negative respectively */
2895 edit_cursor_move (WEdit
* edit
, long increment
)
2897 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2902 for (; increment
< 0; increment
++)
2907 edit_push_undo_action (edit
, CURS_RIGHT
);
2909 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2910 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2911 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2912 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2913 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2915 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2916 1) & M_EDIT_BUF_SIZE
];
2917 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2919 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2920 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2926 edit
->force
|= REDRAW_LINE_BELOW
;
2931 else if (increment
> 0)
2933 for (; increment
> 0; increment
--)
2938 edit_push_undo_action (edit
, CURS_LEFT
);
2940 c
= edit_get_byte (edit
, edit
->curs1
);
2941 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2942 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2943 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2945 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2947 1) & M_EDIT_BUF_SIZE
) - 1];
2948 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2950 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2951 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2957 edit
->force
|= REDRAW_LINE_ABOVE
;
2963 /* These functions return positions relative to lines */
2965 /* --------------------------------------------------------------------------------------------- */
2966 /** returns index of last char on line + 1 */
2969 edit_eol (WEdit
* edit
, long current
)
2971 if (current
>= edit
->last_byte
)
2972 return edit
->last_byte
;
2975 if (edit_get_byte (edit
, current
) == '\n')
2980 /* --------------------------------------------------------------------------------------------- */
2981 /** returns index of first char on line */
2984 edit_bol (WEdit
* edit
, long current
)
2990 if (edit_get_byte (edit
, current
- 1) == '\n')
2995 /* --------------------------------------------------------------------------------------------- */
2998 edit_count_lines (WEdit
* edit
, long current
, long upto
)
3001 if (upto
> edit
->last_byte
)
3002 upto
= edit
->last_byte
;
3005 while (current
< upto
)
3006 if (edit_get_byte (edit
, current
++) == '\n')
3011 /* --------------------------------------------------------------------------------------------- */
3012 /* If lines is zero this returns the count of lines from current to upto. */
3013 /* If upto is zero returns index of lines forward current. */
3016 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
3020 return edit_count_lines (edit
, current
, upto
);
3029 next
= edit_eol (edit
, current
) + 1;
3030 if (next
> edit
->last_byte
)
3039 /* --------------------------------------------------------------------------------------------- */
3040 /** Returns offset of 'lines' lines up from current */
3043 edit_move_backward (WEdit
* edit
, long current
, long lines
)
3047 current
= edit_bol (edit
, current
);
3048 while ((lines
--) && current
!= 0)
3049 current
= edit_bol (edit
, current
- 1);
3053 /* --------------------------------------------------------------------------------------------- */
3054 /* If cols is zero this returns the count of columns from current to upto. */
3055 /* If upto is zero returns index of cols across from current. */
3058 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
3069 q
= edit
->last_byte
+ 2;
3071 for (col
= 0, p
= current
; p
< q
; p
++)
3083 orig_c
= c
= edit_get_byte (edit
, p
);
3091 utf_ch
= edit_get_utf (edit
, p
, &cw
);
3092 if (mc_global
.utf8_display
)
3096 if (g_unichar_iswide (utf_ch
))
3099 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
3103 c
= convert_to_display_c (c
);
3107 col
+= TAB_SIZE
- col
% TAB_SIZE
;
3115 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!mc_global
.utf8_display
&& !edit
->utf8
)))
3116 /* '\r' is shown as ^M, so we must advance 2 characters */
3117 /* Caret notation for control characters */
3125 /* --------------------------------------------------------------------------------------------- */
3126 /** returns the current column position of the cursor */
3129 edit_get_col (WEdit
* edit
)
3131 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3134 /* --------------------------------------------------------------------------------------------- */
3135 /* Scrolling functions */
3136 /* --------------------------------------------------------------------------------------------- */
3139 edit_update_curs_row (WEdit
* edit
)
3141 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
3144 /* --------------------------------------------------------------------------------------------- */
3147 edit_update_curs_col (WEdit
* edit
)
3149 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3152 /* --------------------------------------------------------------------------------------------- */
3155 edit_get_curs_col (const WEdit
* edit
)
3157 return edit
->curs_col
;
3160 /* --------------------------------------------------------------------------------------------- */
3161 /** moves the display start position up by i lines */
3164 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
3166 unsigned long lines_above
= edit
->start_line
;
3167 if (i
> lines_above
)
3171 edit
->start_line
-= i
;
3172 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
3173 edit
->force
|= REDRAW_PAGE
;
3174 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3176 edit_update_curs_row (edit
);
3180 /* --------------------------------------------------------------------------------------------- */
3181 /** returns 1 if could scroll, 0 otherwise */
3184 edit_scroll_downward (WEdit
* edit
, int i
)
3187 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->widget
.lines
- 1);
3188 if (lines_below
> 0)
3190 if (i
> lines_below
)
3192 edit
->start_line
+= i
;
3193 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
3194 edit
->force
|= REDRAW_PAGE
;
3195 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3197 edit_update_curs_row (edit
);
3200 /* --------------------------------------------------------------------------------------------- */
3203 edit_scroll_right (WEdit
* edit
, int i
)
3205 edit
->force
|= REDRAW_PAGE
;
3206 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3207 edit
->start_col
-= i
;
3210 /* --------------------------------------------------------------------------------------------- */
3213 edit_scroll_left (WEdit
* edit
, int i
)
3215 if (edit
->start_col
)
3217 edit
->start_col
+= i
;
3218 if (edit
->start_col
> 0)
3219 edit
->start_col
= 0;
3220 edit
->force
|= REDRAW_PAGE
;
3221 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3225 /* --------------------------------------------------------------------------------------------- */
3226 /* high level cursor movement commands */
3227 /* --------------------------------------------------------------------------------------------- */
3230 edit_move_to_prev_col (WEdit
* edit
, long p
)
3232 int prev
= edit
->prev_col
;
3233 int over
= edit
->over_col
;
3234 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
3236 if (option_cursor_beyond_eol
)
3238 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
3239 edit_eol (edit
, edit
->curs1
));
3241 if (line_len
< prev
+ edit
->over_col
)
3243 edit
->over_col
= prev
+ over
- line_len
;
3244 edit
->prev_col
= line_len
;
3245 edit
->curs_col
= line_len
;
3249 edit
->curs_col
= prev
+ over
;
3250 edit
->prev_col
= edit
->curs_col
;
3257 if (is_in_indent (edit
) && option_fake_half_tabs
)
3259 edit_update_curs_col (edit
);
3261 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
3263 int q
= edit
->curs_col
;
3264 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
3265 p
= edit_bol (edit
, edit
->curs1
);
3266 edit_cursor_move (edit
,
3267 edit_move_forward3 (edit
, p
, edit
->curs_col
,
3269 if (!left_of_four_spaces (edit
))
3270 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3276 /* --------------------------------------------------------------------------------------------- */
3279 line_is_blank (WEdit
* edit
, long line
)
3281 return is_blank (edit
, edit_find_line (edit
, line
));
3284 /* --------------------------------------------------------------------------------------------- */
3285 /** move cursor to line 'line' */
3288 edit_move_to_line (WEdit
* e
, long line
)
3290 if (line
< e
->curs_line
)
3291 edit_move_up (e
, e
->curs_line
- line
, 0);
3293 edit_move_down (e
, line
- e
->curs_line
, 0);
3294 edit_scroll_screen_over_cursor (e
);
3297 /* --------------------------------------------------------------------------------------------- */
3298 /** scroll window so that first visible line is 'line' */
3301 edit_move_display (WEdit
* e
, long line
)
3303 if (line
< e
->start_line
)
3304 edit_scroll_upward (e
, e
->start_line
- line
);
3306 edit_scroll_downward (e
, line
- e
->start_line
);
3309 /* --------------------------------------------------------------------------------------------- */
3310 /** save markers onto undo stack */
3313 edit_push_markers (WEdit
* edit
)
3315 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3316 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3317 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3320 /* --------------------------------------------------------------------------------------------- */
3323 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
3332 /* --------------------------------------------------------------------------------------------- */
3333 /** highlight marker toggle */
3336 edit_mark_cmd (WEdit
* edit
, int unmark
)
3338 edit_push_markers (edit
);
3341 edit_set_markers (edit
, 0, 0, 0, 0);
3342 edit
->force
|= REDRAW_PAGE
;
3346 if (edit
->mark2
>= 0)
3348 edit
->end_mark_curs
= -1;
3349 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3350 edit
->curs_col
+ edit
->over_col
);
3351 edit
->force
|= REDRAW_PAGE
;
3355 edit
->end_mark_curs
= edit
->curs1
;
3356 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3357 edit
->curs_col
+ edit
->over_col
);
3362 /* --------------------------------------------------------------------------------------------- */
3363 /** highlight the word under cursor */
3366 edit_mark_current_word_cmd (WEdit
* edit
)
3370 for (pos
= edit
->curs1
; pos
!= 0; pos
--)
3374 c1
= edit_get_byte (edit
, pos
);
3375 c2
= edit_get_byte (edit
, pos
- 1);
3376 if (!isspace (c1
) && isspace (c2
))
3378 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3383 for (; pos
< edit
->last_byte
; pos
++)
3387 c1
= edit_get_byte (edit
, pos
);
3388 c2
= edit_get_byte (edit
, pos
+ 1);
3389 if (!isspace (c1
) && isspace (c2
))
3391 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3394 edit
->mark2
= min (pos
+ 1, edit
->last_byte
);
3396 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3399 /* --------------------------------------------------------------------------------------------- */
3402 edit_mark_current_line_cmd (WEdit
* edit
)
3404 long pos
= edit
->curs1
;
3406 edit
->mark1
= edit_bol (edit
, pos
);
3407 edit
->mark2
= edit_eol (edit
, pos
);
3409 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3412 /* --------------------------------------------------------------------------------------------- */
3415 edit_delete_line (WEdit
* edit
)
3418 * Delete right part of the line.
3419 * Note that edit_get_byte() returns '\n' when byte position is
3422 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3424 (void) edit_delete (edit
, 1);
3429 * Note that edit_delete() will not corrupt anything if called while
3430 * cursor position is EOF.
3432 (void) edit_delete (edit
, 1);
3435 * Delete left part of the line.
3436 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3438 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3440 (void) edit_backspace (edit
, 1);
3444 /* --------------------------------------------------------------------------------------------- */
3447 edit_indent_width (WEdit
* edit
, long p
)
3450 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
3452 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
3455 /* --------------------------------------------------------------------------------------------- */
3458 edit_insert_indent (WEdit
* edit
, int indent
)
3460 if (!option_fill_tabs_with_spaces
)
3462 while (indent
>= TAB_SIZE
)
3464 edit_insert (edit
, '\t');
3468 while (indent
-- > 0)
3469 edit_insert (edit
, ' ');
3472 /* --------------------------------------------------------------------------------------------- */
3475 edit_push_key_press (WEdit
* edit
)
3477 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3478 if (edit
->mark2
== -1)
3480 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3481 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3485 /* --------------------------------------------------------------------------------------------- */
3488 edit_find_bracket (WEdit
* edit
)
3490 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3491 if (last_bracket
!= edit
->bracket
)
3492 edit
->force
|= REDRAW_PAGE
;
3493 last_bracket
= edit
->bracket
;
3496 /* --------------------------------------------------------------------------------------------- */
3498 * This executes a command as though the user initiated it through a key
3499 * press. Callback with WIDGET_KEY as a message calls this after
3500 * translating the key press. This function can be used to pass any
3501 * command to the editor. Note that the screen wouldn't update
3502 * automatically. Either of command or char_for_insertion must be
3503 * passed as -1. Commands are executed, and char_for_insertion is
3504 * inserted at the cursor.
3508 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3510 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3512 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3515 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3518 if (macro_index
!= -1)
3520 edit
->force
|= REDRAW_COMPLETELY
;
3521 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3523 edit_store_macro_cmd (edit
);
3527 else if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3529 edit_repeat_macro_cmd (edit
);
3535 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3537 record_macro_buf
[macro_index
].action
= command
;
3538 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3540 /* record the beginning of a set of editing actions initiated by a key press */
3541 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3542 edit_push_key_press (edit
);
3544 edit_execute_cmd (edit
, command
, char_for_insertion
);
3545 if (edit
->column_highlight
)
3546 edit
->force
|= REDRAW_PAGE
;
3549 /* --------------------------------------------------------------------------------------------- */
3551 This executes a command at a lower level than macro recording.
3552 It also does not push a key_press onto the undo stack. This means
3553 that if it is called many times, a single undo command will undo
3554 all of them. It also does not check for the Undo command.
3557 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3559 if (command
== CK_WindowFullscreen
)
3561 edit_toggle_fullscreen (edit
);
3565 /* handle window state */
3566 if (edit_handle_move_resize (edit
, command
))
3569 edit
->force
|= REDRAW_LINE
;
3571 /* The next key press will unhighlight the found string, so update
3573 if (edit
->found_len
|| edit
->column_highlight
)
3574 edit
->force
|= REDRAW_PAGE
;
3578 /* a mark command with shift-arrow */
3581 case CK_MarkToWordBegin
:
3582 case CK_MarkToWordEnd
:
3588 case CK_MarkPageDown
:
3589 case CK_MarkToFileBegin
:
3590 case CK_MarkToFileEnd
:
3591 case CK_MarkToPageBegin
:
3592 case CK_MarkToPageEnd
:
3593 case CK_MarkScrollUp
:
3594 case CK_MarkScrollDown
:
3595 case CK_MarkParagraphUp
:
3596 case CK_MarkParagraphDown
:
3597 /* a mark command with alt-arrow */
3598 case CK_MarkColumnPageUp
:
3599 case CK_MarkColumnPageDown
:
3600 case CK_MarkColumnLeft
:
3601 case CK_MarkColumnRight
:
3602 case CK_MarkColumnUp
:
3603 case CK_MarkColumnDown
:
3604 case CK_MarkColumnScrollUp
:
3605 case CK_MarkColumnScrollDown
:
3606 case CK_MarkColumnParagraphUp
:
3607 case CK_MarkColumnParagraphDown
:
3608 edit
->column_highlight
= 0;
3609 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3611 edit_mark_cmd (edit
, 1); /* clear */
3612 edit_mark_cmd (edit
, 0); /* marking on */
3614 edit
->highlight
= 1;
3617 /* any other command */
3619 if (edit
->highlight
)
3620 edit_mark_cmd (edit
, 0); /* clear */
3621 edit
->highlight
= 0;
3624 /* first check for undo */
3625 if (command
== CK_Undo
)
3627 edit
->redo_stack_reset
= 0;
3628 edit_group_undo (edit
);
3629 edit
->found_len
= 0;
3630 edit
->prev_col
= edit_get_col (edit
);
3631 edit
->search_start
= edit
->curs1
;
3634 /* check for redo */
3635 if (command
== CK_Redo
)
3637 edit
->redo_stack_reset
= 0;
3638 edit_do_redo (edit
);
3639 edit
->found_len
= 0;
3640 edit
->prev_col
= edit_get_col (edit
);
3641 edit
->search_start
= edit
->curs1
;
3645 edit
->redo_stack_reset
= 1;
3647 /* An ordinary key press */
3648 if (char_for_insertion
>= 0)
3650 /* if non persistent selection and text selected */
3651 if (!option_persistent_selections
)
3653 if (edit
->mark1
!= edit
->mark2
)
3654 edit_block_delete_cmd (edit
);
3656 if (edit
->overwrite
)
3658 /* remove char only one time, after input first byte, multibyte chars */
3659 if ((!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3660 && edit_get_byte (edit
, edit
->curs1
) != '\n')
3661 edit_delete (edit
, 0);
3663 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3664 edit_insert_over (edit
);
3666 if (char_for_insertion
> 255 && !mc_global
.utf8_display
)
3668 unsigned char str
[6 + 1];
3672 res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3682 while (str
[i
] != 0 && i
<= 6)
3684 char_for_insertion
= str
[i
];
3685 edit_insert (edit
, char_for_insertion
);
3691 edit_insert (edit
, char_for_insertion
);
3693 if (option_auto_para_formatting
)
3695 format_paragraph (edit
, 0);
3696 edit
->force
|= REDRAW_PAGE
;
3699 check_and_wrap_line (edit
);
3700 edit
->found_len
= 0;
3701 edit
->prev_col
= edit_get_col (edit
);
3702 edit
->search_start
= edit
->curs1
;
3703 edit_find_bracket (edit
);
3709 case CK_TopOnScreen
:
3710 case CK_BottomOnScreen
:
3723 if (edit
->mark2
>= 0)
3725 if (!option_persistent_selections
)
3727 if (edit
->column_highlight
)
3728 edit_push_undo_action (edit
, COLUMN_ON
);
3729 edit
->column_highlight
= 0;
3730 edit_mark_cmd (edit
, 1);
3737 case CK_TopOnScreen
:
3738 case CK_BottomOnScreen
:
3739 case CK_MarkToPageBegin
:
3740 case CK_MarkToPageEnd
:
3745 case CK_MarkToWordBegin
:
3746 case CK_MarkToWordEnd
:
3749 case CK_MarkColumnUp
:
3750 case CK_MarkColumnDown
:
3751 if (edit
->mark2
== -1)
3752 break; /*marking is following the cursor: may need to highlight a whole line */
3757 edit
->force
|= REDRAW_CHAR_ONLY
;
3760 /* basic cursor key commands */
3764 /* if non persistent selection and text selected */
3765 if (!option_persistent_selections
)
3767 if (edit
->mark1
!= edit
->mark2
)
3769 edit_block_delete_cmd (edit
);
3773 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3778 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3780 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3781 edit_backspace (edit
, 1);
3786 if (option_fake_half_tabs
)
3789 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3791 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3792 edit_backspace (edit
, 1);
3797 edit_backspace (edit
, 0);
3800 /* if non persistent selection and text selected */
3801 if (!option_persistent_selections
)
3803 if (edit
->mark1
!= edit
->mark2
)
3805 edit_block_delete_cmd (edit
);
3810 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3811 edit_insert_over (edit
);
3813 if (option_fake_half_tabs
)
3816 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3818 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3819 edit_delete (edit
, 1);
3823 edit_delete (edit
, 0);
3825 case CK_DeleteToWordBegin
:
3827 edit_left_delete_word (edit
);
3829 case CK_DeleteToWordEnd
:
3830 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3831 edit_insert_over (edit
);
3833 edit_right_delete_word (edit
);
3836 edit_delete_line (edit
);
3838 case CK_DeleteToHome
:
3839 edit_delete_to_line_begin (edit
);
3841 case CK_DeleteToEnd
:
3842 edit_delete_to_line_end (edit
);
3846 if (option_auto_para_formatting
)
3848 edit_double_newline (edit
);
3849 if (option_return_does_auto_indent
)
3850 edit_auto_indent (edit
);
3851 format_paragraph (edit
, 0);
3855 edit_insert (edit
, '\n');
3856 if (option_return_does_auto_indent
)
3858 edit_auto_indent (edit
);
3863 edit_insert (edit
, '\n');
3866 case CK_MarkColumnPageUp
:
3867 edit
->column_highlight
= 1;
3870 edit_move_up (edit
, edit
->widget
.lines
- 1, 1);
3872 case CK_MarkColumnPageDown
:
3873 edit
->column_highlight
= 1;
3875 case CK_MarkPageDown
:
3876 edit_move_down (edit
, edit
->widget
.lines
- 1, 1);
3878 case CK_MarkColumnLeft
:
3879 edit
->column_highlight
= 1;
3882 if (option_fake_half_tabs
)
3884 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3886 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3889 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3890 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3894 edit_left_char_move_cmd (edit
);
3896 case CK_MarkColumnRight
:
3897 edit
->column_highlight
= 1;
3900 if (option_fake_half_tabs
)
3902 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3904 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3905 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3909 edit_right_char_move_cmd (edit
);
3911 case CK_TopOnScreen
:
3912 case CK_MarkToPageBegin
:
3913 edit_begin_page (edit
);
3915 case CK_BottomOnScreen
:
3916 case CK_MarkToPageEnd
:
3917 edit_end_page (edit
);
3920 case CK_MarkToWordBegin
:
3922 edit_left_word_move_cmd (edit
);
3925 case CK_MarkToWordEnd
:
3927 edit_right_word_move_cmd (edit
);
3929 case CK_MarkColumnUp
:
3930 edit
->column_highlight
= 1;
3933 edit_move_up (edit
, 1, 0);
3935 case CK_MarkColumnDown
:
3936 edit
->column_highlight
= 1;
3939 edit_move_down (edit
, 1, 0);
3941 case CK_MarkColumnParagraphUp
:
3942 edit
->column_highlight
= 1;
3943 case CK_ParagraphUp
:
3944 case CK_MarkParagraphUp
:
3945 edit_move_up_paragraph (edit
, 0);
3947 case CK_MarkColumnParagraphDown
:
3948 edit
->column_highlight
= 1;
3949 case CK_ParagraphDown
:
3950 case CK_MarkParagraphDown
:
3951 edit_move_down_paragraph (edit
, 0);
3953 case CK_MarkColumnScrollUp
:
3954 edit
->column_highlight
= 1;
3956 case CK_MarkScrollUp
:
3957 edit_move_up (edit
, 1, 1);
3959 case CK_MarkColumnScrollDown
:
3960 edit
->column_highlight
= 1;
3962 case CK_MarkScrollDown
:
3963 edit_move_down (edit
, 1, 1);
3967 edit_cursor_to_bol (edit
);
3971 edit_cursor_to_eol (edit
);
3974 /* if text marked shift block */
3975 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3977 if (edit
->mark2
< 0)
3978 edit_mark_cmd (edit
, 0);
3979 edit_move_block_to_right (edit
);
3983 if (option_cursor_beyond_eol
)
3984 edit_insert_over (edit
);
3985 edit_tab_cmd (edit
);
3986 if (option_auto_para_formatting
)
3988 format_paragraph (edit
, 0);
3989 edit
->force
|= REDRAW_PAGE
;
3993 check_and_wrap_line (edit
);
3998 case CK_InsertOverwrite
:
3999 edit
->overwrite
= !edit
->overwrite
;
4003 if (edit
->mark2
>= 0)
4005 if (edit
->column_highlight
)
4006 edit_push_undo_action (edit
, COLUMN_ON
);
4007 edit
->column_highlight
= 0;
4009 edit_mark_cmd (edit
, 0);
4012 if (!edit
->column_highlight
)
4013 edit_push_undo_action (edit
, COLUMN_OFF
);
4014 edit
->column_highlight
= 1;
4015 edit_mark_cmd (edit
, 0);
4018 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
4019 edit
->force
|= REDRAW_PAGE
;
4022 if (edit
->column_highlight
)
4023 edit_push_undo_action (edit
, COLUMN_ON
);
4024 edit
->column_highlight
= 0;
4025 edit_mark_cmd (edit
, 1);
4028 if (edit
->column_highlight
)
4029 edit_push_undo_action (edit
, COLUMN_ON
);
4030 edit
->column_highlight
= 0;
4031 edit_mark_current_word_cmd (edit
);
4034 if (edit
->column_highlight
)
4035 edit_push_undo_action (edit
, COLUMN_ON
);
4036 edit
->column_highlight
= 0;
4037 edit_mark_current_line_cmd (edit
);
4041 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
4042 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
4043 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4045 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
4047 case CK_BookmarkFlush
:
4048 book_mark_flush (edit
, BOOK_MARK_COLOR
);
4049 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
4050 edit
->force
|= REDRAW_PAGE
;
4052 case CK_BookmarkNext
:
4053 if (edit
->book_mark
)
4055 struct _book_mark
*p
;
4056 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4060 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4061 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4062 edit_move_to_line (edit
, p
->line
);
4066 case CK_BookmarkPrev
:
4067 if (edit
->book_mark
)
4069 struct _book_mark
*p
;
4070 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
4071 while (p
->line
== edit
->curs_line
)
4076 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
4077 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
4078 edit_move_to_line (edit
, p
->line
);
4084 case CK_MarkToFileBegin
:
4085 edit_move_to_top (edit
);
4088 case CK_MarkToFileEnd
:
4089 edit_move_to_bottom (edit
);
4093 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4094 edit_insert_over (edit
);
4095 edit_block_copy_cmd (edit
);
4098 edit_block_delete_cmd (edit
);
4101 edit_block_move_cmd (edit
);
4104 case CK_BlockShiftLeft
:
4105 if (edit
->mark1
!= edit
->mark2
)
4106 edit_move_block_to_left (edit
);
4108 case CK_BlockShiftRight
:
4109 if (edit
->mark1
!= edit
->mark2
)
4110 edit_move_block_to_right (edit
);
4113 edit_copy_to_X_buf_cmd (edit
);
4116 edit_cut_to_X_buf_cmd (edit
);
4119 /* if non persistent selection and text selected */
4120 if (!option_persistent_selections
)
4122 if (edit
->mark1
!= edit
->mark2
)
4123 edit_block_delete_cmd (edit
);
4125 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4126 edit_insert_over (edit
);
4127 edit_paste_from_X_buf_cmd (edit
);
4130 edit_paste_from_history (edit
);
4134 edit_save_as_cmd (edit
);
4137 edit_save_confirm_cmd (edit
);
4140 edit_save_block_cmd (edit
);
4143 edit_insert_file_cmd (edit
);
4147 edit_load_back_cmd (edit
);
4150 edit_load_forward_cmd (edit
);
4153 case CK_SyntaxChoose
:
4154 edit_syntax_dialog (edit
);
4158 edit_search_cmd (edit
, FALSE
);
4160 case CK_SearchContinue
:
4161 edit_search_cmd (edit
, TRUE
);
4164 edit_replace_cmd (edit
, 0);
4166 case CK_ReplaceContinue
:
4167 edit_replace_cmd (edit
, 1);
4170 /* if text marked shift block */
4171 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
4173 edit_move_block_to_left (edit
);
4177 edit_complete_word_cmd (edit
);
4181 edit_get_match_keyword_cmd (edit
);
4184 case CK_SpellCheckCurrentWord
:
4185 edit_suggest_current_word (edit
);
4188 edit_spellcheck_file (edit
);
4190 case CK_SpellCheckSelectLang
:
4191 edit_set_spell_lang ();
4197 /* fool gcc to prevent a Y2K warning */
4198 char time_format
[] = "_c";
4199 time_format
[0] = '%';
4201 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4202 edit_print_string (edit
, s
);
4203 edit
->force
|= REDRAW_PAGE
;
4208 edit_goto_cmd (edit
);
4210 case CK_ParagraphFormat
:
4211 format_paragraph (edit
, 1);
4212 edit
->force
|= REDRAW_PAGE
;
4214 case CK_MacroDelete
:
4215 edit_delete_macro_cmd (edit
);
4217 case CK_MatchBracket
:
4218 edit_goto_matching_bracket (edit
);
4221 user_menu (edit
, NULL
, -1);
4224 edit_sort_cmd (edit
);
4226 case CK_ExternalCommand
:
4227 edit_ext_cmd (edit
);
4230 edit_mail_dialog (edit
);
4233 case CK_SelectCodepage
:
4234 edit_select_codepage_cmd (edit
);
4237 case CK_InsertLiteral
:
4238 edit_insert_literal_cmd (edit
);
4240 case CK_MacroStartStopRecord
:
4241 edit_begin_end_macro_cmd (edit
);
4243 case CK_RepeatStartStopRecord
:
4244 edit_begin_end_repeat_cmd (edit
);
4246 case CK_ExtendedKeyMap
:
4247 edit
->extmod
= TRUE
;
4254 if ((command
/ CK_PipeBlock (0)) == 1)
4255 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
4257 /* keys which must set the col position, and the search vars */
4261 case CK_SearchContinue
:
4263 case CK_ReplaceContinue
:
4265 edit
->prev_col
= edit_get_col (edit
);
4269 case CK_MarkColumnUp
:
4272 case CK_MarkColumnDown
:
4275 case CK_MarkColumnPageUp
:
4277 case CK_MarkPageDown
:
4278 case CK_MarkColumnPageDown
:
4280 case CK_MarkToFileBegin
:
4282 case CK_MarkToFileEnd
:
4283 case CK_ParagraphUp
:
4284 case CK_MarkParagraphUp
:
4285 case CK_MarkColumnParagraphUp
:
4286 case CK_ParagraphDown
:
4287 case CK_MarkParagraphDown
:
4288 case CK_MarkColumnParagraphDown
:
4290 case CK_MarkScrollUp
:
4291 case CK_MarkColumnScrollUp
:
4293 case CK_MarkScrollDown
:
4294 case CK_MarkColumnScrollDown
:
4295 edit
->search_start
= edit
->curs1
;
4296 edit
->found_len
= 0;
4299 edit
->found_len
= 0;
4300 edit
->prev_col
= edit_get_col (edit
);
4301 edit
->search_start
= edit
->curs1
;
4303 edit_find_bracket (edit
);
4305 if (option_auto_para_formatting
)
4311 case CK_DeleteToWordBegin
:
4312 case CK_DeleteToWordEnd
:
4313 case CK_DeleteToHome
:
4314 case CK_DeleteToEnd
:
4315 format_paragraph (edit
, 0);
4316 edit
->force
|= REDRAW_PAGE
;
4321 /* --------------------------------------------------------------------------------------------- */
4324 edit_stack_init (void)
4326 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4328 edit_history_moveto
[edit_stack_iterator
].filename_vpath
= NULL
;
4329 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4332 edit_stack_iterator
= 0;
4335 /* --------------------------------------------------------------------------------------------- */
4338 edit_stack_free (void)
4340 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4341 vfs_path_free (edit_history_moveto
[edit_stack_iterator
].filename_vpath
);
4344 /* --------------------------------------------------------------------------------------------- */
4348 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
4350 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4353 /* --------------------------------------------------------------------------------------------- */
4357 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
4359 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4362 /* --------------------------------------------------------------------------------------------- */