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"
74 /*** global variables ****************************************************************************/
76 int option_word_wrap_line_length
= DEFAULT_WRAP_LINE_LENGTH
;
77 int option_typewriter_wrap
= 0;
78 int option_auto_para_formatting
= 0;
79 int option_fill_tabs_with_spaces
= 0;
80 int option_return_does_auto_indent
= 1;
81 int option_backspace_through_tabs
= 0;
82 int option_fake_half_tabs
= 1;
83 int option_save_mode
= EDIT_QUICK_SAVE
;
84 int option_save_position
= 1;
85 int option_max_undo
= 32768;
86 int option_persistent_selections
= 1;
87 int option_cursor_beyond_eol
= 0;
88 int option_line_state
= 0;
89 int option_line_state_width
= 0;
91 int option_edit_right_extreme
= 0;
92 int option_edit_left_extreme
= 0;
93 int option_edit_top_extreme
= 0;
94 int option_edit_bottom_extreme
= 0;
95 int enable_show_tabs_tws
= 1;
96 int option_check_nl_at_eof
= 0;
97 int option_group_undo
= 0;
98 int show_right_margin
= 0;
100 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
101 char *option_backup_ext
= NULL
;
103 int edit_stack_iterator
= 0;
104 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
105 /* magic sequense for say than block is vertical */
106 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
108 /*** file scope macro definitions ****************************************************************/
110 #define TEMP_BUF_LEN 1024
112 #define space_width 1
114 /*** file scope type declarations ****************************************************************/
116 /*** file scope variables ************************************************************************/
118 /* detecting an error on save is easy: just check if every byte has been written. */
119 /* detecting an error on read, is not so easy 'cos there is not way to tell
120 whether you read everything or not. */
121 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
122 static const struct edit_filters
124 const char *read
, *write
, *extension
;
128 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
129 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
130 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
131 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
132 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
136 static long last_bracket
= -1;
138 /*** file scope functions ************************************************************************/
139 /* --------------------------------------------------------------------------------------------- */
143 * here's a quick sketch of the layout: (don't run this through indent.)
145 * (b1 is buffers1 and b2 is buffers2)
148 * \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
149 * ______________________________________|______________________________________
151 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
152 * |-> |-> |-> |-> |-> |-> |
154 * _<------------------------->|<----------------->_
155 * WEdit->curs2 | WEdit->curs1
160 * file end|||file beginning
169 /* --------------------------------------------------------------------------------------------- */
171 static int left_of_four_spaces (WEdit
* edit
);
173 /* --------------------------------------------------------------------------------------------- */
175 * Initialize the buffers for an empty files.
179 edit_init_buffers (WEdit
* edit
)
183 for (j
= 0; j
<= MAXBUFF
; j
++)
185 edit
->buffers1
[j
] = NULL
;
186 edit
->buffers2
[j
] = NULL
;
191 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
194 /* --------------------------------------------------------------------------------------------- */
197 * Load file OR text into buffers. Set cursor to the beginning of file.
199 * @returns FALSE on error.
203 edit_load_file_fast (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
207 gboolean ret
= FALSE
;
209 edit
->curs2
= edit
->last_byte
;
210 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
212 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
215 gchar
*errmsg
, *filename
;
217 filename
= vfs_path_to_str (filename_vpath
);
218 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
220 edit_error_dialog (_("Error"), errmsg
);
225 if (!edit
->buffers2
[buf2
])
226 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
231 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
232 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
235 for (buf
= buf2
- 1; buf
>= 0; buf
--)
237 /* edit->buffers2[0] is already allocated */
238 if (!edit
->buffers2
[buf
])
239 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
240 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
249 gchar
*errmsg
, *filename
;
251 filename
= vfs_path_to_str (filename_vpath
);
252 errmsg
= g_strdup_printf (_("Error reading %s"), filename
);
254 edit_error_dialog (_("Error"), errmsg
);
261 /* --------------------------------------------------------------------------------------------- */
262 /** Return index of the filter or -1 is there is no appropriate filter */
265 edit_find_filter (const vfs_path_t
* filename_vpath
)
270 if (filename_vpath
== NULL
)
273 filename
= vfs_path_to_str (filename_vpath
);
274 l
= strlen (filename
);
275 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
277 e
= strlen (all_filters
[i
].extension
);
279 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
289 /* --------------------------------------------------------------------------------------------- */
292 edit_get_filter (const vfs_path_t
* filename_vpath
)
295 char *p
, *quoted_name
, *filename
;
297 i
= edit_find_filter (filename_vpath
);
301 filename
= vfs_path_to_str (filename_vpath
);
302 quoted_name
= name_quote (filename
, 0);
304 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
305 g_free (quoted_name
);
309 /* --------------------------------------------------------------------------------------------- */
312 edit_insert_stream (WEdit
* edit
, FILE * f
)
316 while ((c
= fgetc (f
)) >= 0)
318 edit_insert (edit
, c
);
324 /* --------------------------------------------------------------------------------------------- */
326 * Open file and create it if necessary.
328 * @param edit editor object
329 * @param filename_vpath file name
330 * @param st buffer for store stat info
331 * @returns TRUE for success, FALSE for error.
335 check_file_access (WEdit
* edit
, const vfs_path_t
* filename_vpath
, struct stat
*st
)
338 gchar
*errmsg
= NULL
;
340 /* Try opening an existing file */
341 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
345 * Try creating the file. O_EXCL prevents following broken links
346 * and opening existing files.
348 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
353 filename
= vfs_path_to_str (filename_vpath
);
354 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
359 /* New file, delete it if it's not modified or saved */
360 edit
->delete_file
= 1;
363 /* Check what we have opened */
364 if (mc_fstat (file
, st
) < 0)
368 filename
= vfs_path_to_str (filename_vpath
);
369 errmsg
= g_strdup_printf (_("Cannot get size/permissions for %s"), filename
);
374 /* We want to open regular files only */
375 if (!S_ISREG (st
->st_mode
))
379 filename
= vfs_path_to_str (filename_vpath
);
380 errmsg
= g_strdup_printf (_("\"%s\" is not a regular file"), filename
);
386 * Don't delete non-empty files.
387 * O_EXCL should prevent it, but let's be on the safe side.
390 edit
->delete_file
= 0;
392 if (st
->st_size
>= SIZE_LIMIT
)
396 filename
= vfs_path_to_str (filename_vpath
);
397 errmsg
= g_strdup_printf (_("File \"%s\" is too large"), filename
);
402 (void) mc_close (file
);
406 edit_error_dialog (_("Error"), errmsg
);
413 /* --------------------------------------------------------------------------------------------- */
416 * Open the file and load it into the buffers, either directly or using
417 * a filter. Return TRUE on success, FALSE on error.
419 * Fast loading (edit_load_file_fast) is used when the file size is
420 * known. In this case the data is read into the buffers by blocks.
421 * If the file size is not known, the data is loaded byte by byte in
424 * @param edit editor object
425 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
428 edit_load_file (WEdit
* edit
)
430 gboolean fast_load
= TRUE
;
432 /* Cannot do fast load if a filter is used */
433 if (edit_find_filter (edit
->filename_vpath
) >= 0)
437 * FIXME: line end translation should disable fast loading as well
438 * Consider doing fseek() to the end and ftell() for the real size.
440 if (edit
->filename_vpath
!= NULL
)
443 * VFS may report file size incorrectly, and slow load is not a big
444 * deal considering overhead in VFS.
446 if (!vfs_file_is_local (edit
->filename_vpath
))
449 /* If we are dealing with a real file, check that it exists */
450 if (!check_file_access (edit
, edit
->filename_vpath
, &edit
->stat1
))
458 /* nothing to load */
462 edit_init_buffers (edit
);
466 edit
->last_byte
= edit
->stat1
.st_size
;
467 edit_load_file_fast (edit
, edit
->filename_vpath
);
468 /* If fast load was used, the number of lines wasn't calculated */
469 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
474 if (edit
->filename_vpath
!= NULL
475 && *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) != '\0')
477 edit
->undo_stack_disable
= 1;
478 if (edit_insert_file (edit
, edit
->filename_vpath
) < 0)
483 edit
->undo_stack_disable
= 0;
490 /* --------------------------------------------------------------------------------------------- */
491 /** Restore saved cursor position in the file */
494 edit_load_position (WEdit
* edit
)
499 if (edit
->filename_vpath
== NULL
500 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
503 load_file_position (edit
->filename_vpath
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
507 edit_move_to_line (edit
, line
- 1);
508 edit
->prev_col
= column
;
512 edit_cursor_move (edit
, offset
);
513 line
= edit
->curs_line
;
514 edit
->search_start
= edit
->curs1
;
517 book_mark_restore (edit
, BOOK_MARK_COLOR
);
519 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
520 edit_move_display (edit
, line
- (edit
->widget
.lines
/ 2));
523 /* --------------------------------------------------------------------------------------------- */
524 /** Save cursor position in the file */
527 edit_save_position (WEdit
* edit
)
529 if (edit
->filename_vpath
== NULL
530 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
533 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
534 save_file_position (edit
->filename_vpath
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
,
535 edit
->serialized_bookmarks
);
536 edit
->serialized_bookmarks
= NULL
;
539 /* --------------------------------------------------------------------------------------------- */
540 /** Clean the WEdit stricture except the widget part */
543 edit_purge_widget (WEdit
* edit
)
545 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
546 char *start
= (char *) edit
+ sizeof (Widget
);
547 memset (start
, 0, len
);
550 /* --------------------------------------------------------------------------------------------- */
553 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
554 then the file should be as it was when he loaded up. Then set edit->modified to 0.
558 edit_pop_undo_action (WEdit
* edit
)
561 unsigned long sp
= edit
->undo_stack_pointer
;
563 if (sp
== edit
->undo_stack_bottom
)
566 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
567 c
= edit
->undo_stack
[sp
];
570 /* edit->undo_stack[sp] = '@'; */
571 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
575 if (sp
== edit
->undo_stack_bottom
)
578 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
579 if (edit
->undo_stack
[sp
] == -2)
581 /* edit->undo_stack[sp] = '@'; */
582 edit
->undo_stack_pointer
= sp
;
585 edit
->undo_stack
[sp
]++;
591 edit_pop_redo_action (WEdit
* edit
)
594 unsigned long sp
= edit
->redo_stack_pointer
;
596 if (sp
== edit
->redo_stack_bottom
)
599 sp
= (sp
- 1) & edit
->redo_stack_size_mask
;
600 c
= edit
->redo_stack
[sp
];
603 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
607 if (sp
== edit
->redo_stack_bottom
)
610 c
= edit
->redo_stack
[(sp
- 1) & edit
->redo_stack_size_mask
];
611 if (edit
->redo_stack
[sp
] == -2)
612 edit
->redo_stack_pointer
= sp
;
614 edit
->redo_stack
[sp
]++;
620 get_prev_undo_action (WEdit
* edit
)
623 unsigned long sp
= edit
->undo_stack_pointer
;
625 if (sp
== edit
->undo_stack_bottom
)
628 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
629 c
= edit
->undo_stack
[sp
];
633 if (sp
== edit
->undo_stack_bottom
)
636 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
640 /* --------------------------------------------------------------------------------------------- */
641 /** is called whenever a modification is made by one of the four routines below */
644 edit_modification (WEdit
* edit
)
646 edit
->caches_valid
= 0;
648 /* raise lock when file modified */
649 if (!edit
->modified
&& !edit
->delete_file
)
650 edit
->locked
= lock_file (edit
->filename_vpath
);
654 /* --------------------------------------------------------------------------------------------- */
657 edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
659 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
662 if (byte_index
>= edit
->curs1
)
666 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
667 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
668 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
671 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
672 (byte_index
& M_EDIT_BUF_SIZE
));
675 /* --------------------------------------------------------------------------------------------- */
678 edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
681 gchar utf8_buf
[3 * UTF8_CHAR_LEN
+ 1];
683 gchar
*cursor_buf_ptr
;
685 if (byte_index
> (edit
->curs1
+ edit
->curs2
) || byte_index
<= 0)
691 for (i
= 0; i
< (3 * UTF8_CHAR_LEN
); i
++)
692 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
- (2 * UTF8_CHAR_LEN
));
693 utf8_buf
[3 * UTF8_CHAR_LEN
] = '\0';
695 cursor_buf_ptr
= utf8_buf
+ (2 * UTF8_CHAR_LEN
);
696 str
= g_utf8_find_prev_char (utf8_buf
, cursor_buf_ptr
);
698 if (str
== NULL
|| g_utf8_next_char (str
) != cursor_buf_ptr
)
701 return *(cursor_buf_ptr
- 1);
705 res
= g_utf8_get_char_validated (str
, -1);
710 return *(cursor_buf_ptr
- 1);
714 *char_width
= cursor_buf_ptr
- str
;
720 /* --------------------------------------------------------------------------------------------- */
723 edit_backspace (WEdit
* edit
, const int byte_delete
)
734 if (edit
->mark2
!= edit
->mark1
)
735 edit_push_markers (edit
);
737 if (edit
->utf8
&& byte_delete
== 0)
739 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
743 for (i
= 1; i
<= cw
; i
++)
745 if (edit
->mark1
>= edit
->curs1
)
748 edit
->end_mark_curs
--;
750 if (edit
->mark2
>= edit
->curs1
)
752 if (edit
->last_get_rule
>= edit
->curs1
)
753 edit
->last_get_rule
--;
755 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
756 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
757 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
759 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
760 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
764 edit_push_undo_action (edit
, p
);
766 edit_modification (edit
);
770 book_mark_dec (edit
, edit
->curs_line
);
773 edit
->force
|= REDRAW_AFTER_CURSOR
;
776 if (edit
->curs1
< edit
->start_display
)
778 edit
->start_display
--;
786 /* --------------------------------------------------------------------------------------------- */
787 /* high level cursor movement commands */
788 /* --------------------------------------------------------------------------------------------- */
791 is_in_indent (WEdit
* edit
)
793 long p
= edit_bol (edit
, edit
->curs1
);
794 while (p
< edit
->curs1
)
795 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
800 /* --------------------------------------------------------------------------------------------- */
803 is_blank (WEdit
* edit
, long offset
)
807 s
= edit_bol (edit
, offset
);
808 f
= edit_eol (edit
, offset
) - 1;
811 c
= edit_get_byte (edit
, s
++);
819 /* --------------------------------------------------------------------------------------------- */
820 /** returns the offset of line i */
823 edit_find_line (WEdit
* edit
, int line
)
827 if (!edit
->caches_valid
)
829 for (i
= 0; i
< N_LINE_CACHES
; i
++)
830 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
831 /* three offsets that we *know* are line 0 at 0 and these two: */
832 edit
->line_numbers
[1] = edit
->curs_line
;
833 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
834 edit
->line_numbers
[2] = edit
->total_lines
;
835 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
836 edit
->caches_valid
= 1;
838 if (line
>= edit
->total_lines
)
839 return edit
->line_offsets
[2];
842 /* find the closest known point */
843 for (i
= 0; i
< N_LINE_CACHES
; i
++)
846 n
= abs (edit
->line_numbers
[i
] - line
);
854 return edit
->line_offsets
[j
]; /* know the offset exactly */
855 if (m
== 1 && j
>= 3)
856 i
= j
; /* one line different - caller might be looping, so stay in this cache */
858 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
859 if (line
> edit
->line_numbers
[j
])
860 edit
->line_offsets
[i
] =
861 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
863 edit
->line_offsets
[i
] =
864 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
865 edit
->line_numbers
[i
] = line
;
866 return edit
->line_offsets
[i
];
869 /* --------------------------------------------------------------------------------------------- */
870 /** moves up until a blank line is reached, or until just
871 before a non-blank line is reached */
874 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
877 if (edit
->curs_line
> 1)
879 if (line_is_blank (edit
, edit
->curs_line
))
881 if (line_is_blank (edit
, edit
->curs_line
- 1))
883 for (i
= edit
->curs_line
- 1; i
; i
--)
884 if (!line_is_blank (edit
, i
))
892 for (i
= edit
->curs_line
- 1; i
; i
--)
893 if (line_is_blank (edit
, i
))
899 for (i
= edit
->curs_line
- 1; i
; i
--)
900 if (line_is_blank (edit
, i
))
904 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
907 /* --------------------------------------------------------------------------------------------- */
908 /** moves down until a blank line is reached, or until just
909 before a non-blank line is reached */
912 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
915 if (edit
->curs_line
>= edit
->total_lines
- 1)
917 i
= edit
->total_lines
;
921 if (line_is_blank (edit
, edit
->curs_line
))
923 if (line_is_blank (edit
, edit
->curs_line
+ 1))
925 for (i
= edit
->curs_line
+ 1; i
; i
++)
926 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
934 for (i
= edit
->curs_line
+ 1; i
; i
++)
935 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
941 for (i
= edit
->curs_line
+ 1; i
; i
++)
942 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
946 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
949 /* --------------------------------------------------------------------------------------------- */
952 edit_begin_page (WEdit
* edit
)
954 edit_update_curs_row (edit
);
955 edit_move_up (edit
, edit
->curs_row
, 0);
958 /* --------------------------------------------------------------------------------------------- */
961 edit_end_page (WEdit
* edit
)
963 edit_update_curs_row (edit
);
964 edit_move_down (edit
, edit
->widget
.lines
- edit
->curs_row
- 1, 0);
968 /* --------------------------------------------------------------------------------------------- */
969 /** goto beginning of text */
972 edit_move_to_top (WEdit
* edit
)
976 edit_cursor_move (edit
, -edit
->curs1
);
977 edit_move_to_prev_col (edit
, 0);
978 edit
->force
|= REDRAW_PAGE
;
979 edit
->search_start
= 0;
980 edit_update_curs_row (edit
);
985 /* --------------------------------------------------------------------------------------------- */
986 /** goto end of text */
989 edit_move_to_bottom (WEdit
* edit
)
991 if (edit
->curs_line
< edit
->total_lines
)
993 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
994 edit
->start_display
= edit
->last_byte
;
995 edit
->start_line
= edit
->total_lines
;
996 edit_scroll_upward (edit
, edit
->widget
.lines
- 1);
997 edit
->force
|= REDRAW_PAGE
;
1001 /* --------------------------------------------------------------------------------------------- */
1002 /** goto beginning of line */
1005 edit_cursor_to_bol (WEdit
* edit
)
1007 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1008 edit
->search_start
= edit
->curs1
;
1009 edit
->prev_col
= edit_get_col (edit
);
1013 /* --------------------------------------------------------------------------------------------- */
1014 /** goto end of line */
1017 edit_cursor_to_eol (WEdit
* edit
)
1019 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1020 edit
->search_start
= edit
->curs1
;
1021 edit
->prev_col
= edit_get_col (edit
);
1025 /* --------------------------------------------------------------------------------------------- */
1027 static unsigned long
1032 const char option_chars_move_whole_word
[] =
1033 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1039 if (*option_chars_move_whole_word
== '!')
1041 return 0x80000000UL
;
1043 if (g_ascii_isupper ((gchar
) c
))
1045 else if (g_ascii_islower ((gchar
) c
))
1047 else if (g_ascii_isalpha (c
))
1049 else if (isdigit (c
))
1051 else if (isspace (c
))
1053 q
= strchr (option_chars_move_whole_word
, c
);
1055 return 0xFFFFFFFFUL
;
1058 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1063 while ((q
= strchr (q
+ 1, c
)));
1067 /* --------------------------------------------------------------------------------------------- */
1070 edit_left_word_move (WEdit
* edit
, int s
)
1075 if (edit
->column_highlight
1076 && edit
->mark1
!= edit
->mark2
1077 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1079 edit_cursor_move (edit
, -1);
1082 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1083 c2
= edit_get_byte (edit
, edit
->curs1
);
1084 if (c1
== '\n' || c2
== '\n')
1086 if (!(my_type_of (c1
) & my_type_of (c2
)))
1088 if (isspace (c1
) && !isspace (c2
))
1091 if (!isspace (c1
) && isspace (c2
))
1096 /* --------------------------------------------------------------------------------------------- */
1099 edit_left_word_move_cmd (WEdit
* edit
)
1101 edit_left_word_move (edit
, 0);
1102 edit
->force
|= REDRAW_PAGE
;
1105 /* --------------------------------------------------------------------------------------------- */
1108 edit_right_word_move (WEdit
* edit
, int s
)
1113 if (edit
->column_highlight
1114 && edit
->mark1
!= edit
->mark2
1115 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
1117 edit_cursor_move (edit
, 1);
1118 if (edit
->curs1
>= edit
->last_byte
)
1120 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1121 c2
= edit_get_byte (edit
, edit
->curs1
);
1122 if (c1
== '\n' || c2
== '\n')
1124 if (!(my_type_of (c1
) & my_type_of (c2
)))
1126 if (isspace (c1
) && !isspace (c2
))
1129 if (!isspace (c1
) && isspace (c2
))
1134 /* --------------------------------------------------------------------------------------------- */
1137 edit_right_word_move_cmd (WEdit
* edit
)
1139 edit_right_word_move (edit
, 0);
1140 edit
->force
|= REDRAW_PAGE
;
1143 /* --------------------------------------------------------------------------------------------- */
1146 edit_right_char_move_cmd (WEdit
* edit
)
1152 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
1158 c
= edit_get_byte (edit
, edit
->curs1
);
1160 if (option_cursor_beyond_eol
&& c
== '\n')
1166 edit_cursor_move (edit
, cw
);
1170 /* --------------------------------------------------------------------------------------------- */
1173 edit_left_char_move_cmd (WEdit
* edit
)
1176 if (edit
->column_highlight
1177 && option_cursor_beyond_eol
1178 && edit
->mark1
!= edit
->mark2
1179 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1183 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1187 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1193 edit_cursor_move (edit
, -cw
);
1197 /* --------------------------------------------------------------------------------------------- */
1198 /** Up or down cursor moving.
1199 direction = TRUE - move up
1204 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
1207 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
1216 edit
->force
|= REDRAW_PAGE
;
1220 edit_scroll_upward (edit
, i
);
1222 edit_scroll_downward (edit
, i
);
1224 p
= edit_bol (edit
, edit
->curs1
);
1226 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
1228 edit_cursor_move (edit
, p
- edit
->curs1
);
1230 edit_move_to_prev_col (edit
, p
);
1232 /* search start of current multibyte char (like CJK) */
1233 if (edit
->curs1
+ 1 < edit
->last_byte
)
1235 edit_right_char_move_cmd (edit
);
1236 edit_left_char_move_cmd (edit
);
1239 edit
->search_start
= edit
->curs1
;
1240 edit
->found_len
= 0;
1243 /* --------------------------------------------------------------------------------------------- */
1246 edit_right_delete_word (WEdit
* edit
)
1251 if (edit
->curs1
>= edit
->last_byte
)
1253 c1
= edit_delete (edit
, 1);
1254 c2
= edit_get_byte (edit
, edit
->curs1
);
1255 if (c1
== '\n' || c2
== '\n')
1257 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1259 if (!(my_type_of (c1
) & my_type_of (c2
)))
1264 /* --------------------------------------------------------------------------------------------- */
1267 edit_left_delete_word (WEdit
* edit
)
1272 if (edit
->curs1
<= 0)
1274 c1
= edit_backspace (edit
, 1);
1275 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1276 if (c1
== '\n' || c2
== '\n')
1278 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1280 if (!(my_type_of (c1
) & my_type_of (c2
)))
1285 /* --------------------------------------------------------------------------------------------- */
1287 the start column position is not recorded, and hence does not
1288 undo as it happed. But who would notice.
1292 edit_do_undo (WEdit
* edit
)
1297 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1299 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1306 edit_cursor_move (edit
, 1);
1309 edit_cursor_move (edit
, -1);
1313 edit_backspace (edit
, 1);
1317 edit_delete (edit
, 1);
1320 edit
->column_highlight
= 1;
1323 edit
->column_highlight
= 0;
1326 if (ac
>= 256 && ac
< 512)
1327 edit_insert_ahead (edit
, ac
- 256);
1328 if (ac
>= 0 && ac
< 256)
1329 edit_insert (edit
, ac
);
1331 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1333 edit
->mark1
= ac
- MARK_1
;
1334 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1336 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1338 edit
->mark2
= ac
- MARK_2
;
1339 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1341 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1343 edit
->end_mark_curs
= ac
- MARK_CURS
;
1346 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1349 if (edit
->start_display
> ac
- KEY_PRESS
)
1351 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1352 edit
->force
|= REDRAW_PAGE
;
1354 else if (edit
->start_display
< ac
- KEY_PRESS
)
1356 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1357 edit
->force
|= REDRAW_PAGE
;
1359 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1360 edit_update_curs_row (edit
);
1363 edit
->undo_stack_disable
= 0;
1367 edit_do_redo (WEdit
* edit
)
1372 if (edit
->redo_stack_reset
)
1376 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1383 edit_cursor_move (edit
, 1);
1386 edit_cursor_move (edit
, -1);
1389 edit_backspace (edit
, 1);
1392 edit_delete (edit
, 1);
1395 edit
->column_highlight
= 1;
1398 edit
->column_highlight
= 0;
1401 if (ac
>= 256 && ac
< 512)
1402 edit_insert_ahead (edit
, ac
- 256);
1403 if (ac
>= 0 && ac
< 256)
1404 edit_insert (edit
, ac
);
1406 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1408 edit
->mark1
= ac
- MARK_1
;
1409 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1411 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1413 edit
->mark2
= ac
- MARK_2
;
1414 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1416 /* more than one pop usually means something big */
1418 edit
->force
|= REDRAW_PAGE
;
1421 if (edit
->start_display
> ac
- KEY_PRESS
)
1423 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1424 edit
->force
|= REDRAW_PAGE
;
1426 else if (edit
->start_display
< ac
- KEY_PRESS
)
1428 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1429 edit
->force
|= REDRAW_PAGE
;
1431 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1432 edit_update_curs_row (edit
);
1438 edit_group_undo (WEdit
* edit
)
1440 long ac
= KEY_PRESS
;
1441 long cur_ac
= KEY_PRESS
;
1442 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1444 cur_ac
= get_prev_undo_action (edit
);
1445 edit_do_undo (edit
);
1446 ac
= get_prev_undo_action (edit
);
1447 /* exit from cycle if option_group_undo is not set,
1448 * and make single UNDO operation
1450 if (!option_group_undo
)
1455 /* --------------------------------------------------------------------------------------------- */
1458 edit_delete_to_line_end (WEdit
* edit
)
1460 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
1464 edit_delete (edit
, 1);
1468 /* --------------------------------------------------------------------------------------------- */
1471 edit_delete_to_line_begin (WEdit
* edit
)
1473 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
1477 edit_backspace (edit
, 1);
1481 /* --------------------------------------------------------------------------------------------- */
1484 is_aligned_on_a_tab (WEdit
* edit
)
1486 edit_update_curs_col (edit
);
1487 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
1488 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
1491 /* --------------------------------------------------------------------------------------------- */
1494 right_of_four_spaces (WEdit
* edit
)
1497 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1498 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1500 return is_aligned_on_a_tab (edit
);
1504 /* --------------------------------------------------------------------------------------------- */
1507 left_of_four_spaces (WEdit
* edit
)
1510 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1511 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1513 return is_aligned_on_a_tab (edit
);
1517 /* --------------------------------------------------------------------------------------------- */
1520 edit_auto_indent (WEdit
* edit
)
1525 /* use the previous line as a template */
1526 p
= edit_move_backward (edit
, p
, 1);
1527 /* copy the leading whitespace of the line */
1529 { /* no range check - the line _is_ \n-terminated */
1530 c
= edit_get_byte (edit
, p
++);
1531 if (c
!= ' ' && c
!= '\t')
1533 edit_insert (edit
, c
);
1537 /* --------------------------------------------------------------------------------------------- */
1540 edit_double_newline (WEdit
* edit
)
1542 edit_insert (edit
, '\n');
1543 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1545 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1547 edit
->force
|= REDRAW_PAGE
;
1548 edit_insert (edit
, '\n');
1552 /* --------------------------------------------------------------------------------------------- */
1555 insert_spaces_tab (WEdit
* edit
, gboolean half
)
1559 edit_update_curs_col (edit
);
1560 i
= option_tab_spacing
* space_width
;
1563 i
= ((edit
->curs_col
/ i
) + 1) * i
- edit
->curs_col
;
1566 edit_insert (edit
, ' ');
1571 /* --------------------------------------------------------------------------------------------- */
1574 edit_tab_cmd (WEdit
* edit
)
1578 if (option_fake_half_tabs
)
1580 if (is_in_indent (edit
))
1582 /*insert a half tab (usually four spaces) unless there is a
1583 half tab already behind, then delete it and insert a
1585 if (option_fill_tabs_with_spaces
|| !right_of_four_spaces (edit
))
1586 insert_spaces_tab (edit
, TRUE
);
1589 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1590 edit_backspace (edit
, 1);
1591 edit_insert (edit
, '\t');
1596 if (option_fill_tabs_with_spaces
)
1597 insert_spaces_tab (edit
, FALSE
);
1599 edit_insert (edit
, '\t');
1602 /* --------------------------------------------------------------------------------------------- */
1605 check_and_wrap_line (WEdit
* edit
)
1608 if (!option_typewriter_wrap
)
1610 edit_update_curs_col (edit
);
1611 if (edit
->curs_col
< option_word_wrap_line_length
)
1617 c
= edit_get_byte (edit
, curs
);
1618 if (c
== '\n' || curs
<= 0)
1620 edit_insert (edit
, '\n');
1623 if (c
== ' ' || c
== '\t')
1625 int current
= edit
->curs1
;
1626 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
1627 edit_insert (edit
, '\n');
1628 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
1634 /* --------------------------------------------------------------------------------------------- */
1635 /** this find the matching bracket in either direction, and sets edit->bracket */
1638 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
1640 const char *const b
= "{}{[][()(", *p
;
1641 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
1642 unsigned long j
= 0;
1644 edit_update_curs_row (edit
);
1645 c
= edit_get_byte (edit
, edit
->curs1
);
1648 if (!furthest_bracket_search
)
1649 furthest_bracket_search
--;
1650 /* not on a bracket at all */
1653 /* the matching bracket */
1655 /* going left or right? */
1656 if (strchr ("{[(", c
))
1658 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
1660 /* out of buffer? */
1661 if (q
>= edit
->last_byte
|| q
< 0)
1663 a
= edit_get_byte (edit
, q
);
1664 /* don't want to eat CPU */
1665 if (j
++ > furthest_bracket_search
)
1667 /* out of screen? */
1670 if (q
< edit
->start_display
)
1672 /* count lines if searching downward */
1673 if (inc
> 0 && a
== '\n')
1674 if (n
++ >= edit
->widget
.lines
- edit
->curs_row
) /* out of screen */
1677 /* count bracket depth */
1678 i
+= (a
== c
) - (a
== d
);
1679 /* return if bracket depth is zero */
1687 /* --------------------------------------------------------------------------------------------- */
1690 edit_goto_matching_bracket (WEdit
* edit
)
1694 q
= edit_get_bracket (edit
, 0, 0);
1697 edit
->bracket
= edit
->curs1
;
1698 edit
->force
|= REDRAW_PAGE
;
1699 edit_cursor_move (edit
, q
- edit
->curs1
);
1703 /* --------------------------------------------------------------------------------------------- */
1706 edit_move_block_to_right (WEdit
* edit
)
1708 long start_mark
, end_mark
;
1709 long cur_bol
, start_bol
;
1711 if (eval_marks (edit
, &start_mark
, &end_mark
))
1714 start_bol
= edit_bol (edit
, start_mark
);
1715 cur_bol
= edit_bol (edit
, end_mark
- 1);
1719 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1720 if (option_fill_tabs_with_spaces
)
1721 insert_spaces_tab (edit
, option_fake_half_tabs
);
1723 edit_insert (edit
, '\t');
1724 edit_cursor_move (edit
, edit_bol (edit
, cur_bol
) - edit
->curs1
);
1729 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1731 while (cur_bol
>= start_bol
);
1733 edit
->force
|= REDRAW_PAGE
;
1736 /* --------------------------------------------------------------------------------------------- */
1739 edit_move_block_to_left (WEdit
* edit
)
1741 long start_mark
, end_mark
;
1742 long cur_bol
, start_bol
;
1745 if (eval_marks (edit
, &start_mark
, &end_mark
))
1748 start_bol
= edit_bol (edit
, start_mark
);
1749 cur_bol
= edit_bol (edit
, end_mark
- 1);
1756 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
1758 if (option_fake_half_tabs
)
1759 del_tab_width
= HALF_TAB_SIZE
;
1761 del_tab_width
= option_tab_spacing
;
1763 next_char
= edit_get_byte (edit
, edit
->curs1
);
1764 if (next_char
== '\t')
1765 edit_delete (edit
, 1);
1766 else if (next_char
== ' ')
1767 for (i
= 1; i
<= del_tab_width
; i
++)
1769 if (next_char
== ' ')
1770 edit_delete (edit
, 1);
1771 next_char
= edit_get_byte (edit
, edit
->curs1
);
1777 cur_bol
= edit_bol (edit
, cur_bol
- 1);
1779 while (cur_bol
>= start_bol
);
1781 edit
->force
|= REDRAW_PAGE
;
1784 /* --------------------------------------------------------------------------------------------- */
1786 * prints at the cursor
1787 * @returns the number of chars printed
1791 edit_print_string (WEdit
* e
, const char *s
)
1795 while (s
[i
] != '\0')
1796 edit_execute_cmd (e
, CK_InsertChar
, (unsigned char) s
[i
++]);
1797 e
->force
|= REDRAW_COMPLETELY
;
1798 edit_update_screen (e
);
1802 /* --------------------------------------------------------------------------------------------- */
1803 /*** public functions ****************************************************************************/
1804 /* --------------------------------------------------------------------------------------------- */
1806 /** User edit menu, like user menu (F2) but only in editor. */
1809 user_menu (WEdit
* edit
, const char *menu_file
, int selected_entry
)
1814 long start_mark
, end_mark
;
1816 vfs_path_t
*block_file_vpath
;
1818 block_file
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
1819 block_file_vpath
= vfs_path_from_str (block_file
);
1821 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
1823 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1825 /* run shell scripts from menu */
1826 if (user_menu_cmd (edit
, menu_file
, selected_entry
)
1827 && (mc_stat (block_file_vpath
, &status
) == 0) && (status
.st_size
!= 0))
1832 /* i.e. we have marked block */
1834 rc
= edit_block_delete_cmd (edit
);
1840 ins_len
= edit_insert_file (edit
, block_file_vpath
);
1841 if (nomark
== 0 && ins_len
> 0)
1842 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1844 /* truncate block file */
1845 fd
= fopen (block_file
, "w");
1849 g_free (block_file
);
1850 vfs_path_free (block_file_vpath
);
1852 edit_cursor_move (edit
, curs
- edit
->curs1
);
1853 edit
->force
|= REDRAW_PAGE
;
1854 send_message ((Widget
*) edit
, WIDGET_DRAW
, 0);
1857 /* --------------------------------------------------------------------------------------------- */
1860 edit_get_byte (WEdit
* edit
, long byte_index
)
1863 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1866 if (byte_index
>= edit
->curs1
)
1868 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1869 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
1873 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
1877 /* --------------------------------------------------------------------------------------------- */
1880 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1885 gchar
*next_ch
= NULL
;
1887 gchar utf8_buf
[UTF8_CHAR_LEN
+ 1];
1889 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1895 str
= edit_get_byte_ptr (edit
, byte_index
);
1903 res
= g_utf8_get_char_validated (str
, -1);
1907 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1909 for (i
= 0; i
< UTF8_CHAR_LEN
; i
++)
1910 utf8_buf
[i
] = edit_get_byte (edit
, byte_index
+ i
);
1911 utf8_buf
[UTF8_CHAR_LEN
] = '\0';
1913 res
= g_utf8_get_char_validated (str
, -1);
1924 /* Calculate UTF-8 char width */
1925 next_ch
= g_utf8_next_char (str
);
1928 width
= next_ch
- str
;
1936 *char_width
= width
;
1940 /* --------------------------------------------------------------------------------------------- */
1943 edit_get_write_filter (const vfs_path_t
* write_name_vpath
, const vfs_path_t
* filename_vpath
)
1946 char *p
, *writename
;
1947 const vfs_path_element_t
*path_element
;
1949 i
= edit_find_filter (filename_vpath
);
1953 path_element
= vfs_path_get_by_index (write_name_vpath
, -1);
1954 writename
= name_quote (path_element
->path
, 0);
1955 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
1960 /* --------------------------------------------------------------------------------------------- */
1963 edit_write_stream (WEdit
* edit
, FILE * f
)
1967 if (edit
->lb
== LB_ASIS
)
1969 for (i
= 0; i
< edit
->last_byte
; i
++)
1970 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
1975 /* change line breaks */
1976 for (i
= 0; i
< edit
->last_byte
; i
++)
1978 unsigned char c
= edit_get_byte (edit
, i
);
1980 if (!(c
== '\n' || c
== '\r'))
1982 /* not line break */
1983 if (fputc (c
, f
) < 0)
1987 { /* (c == '\n' || c == '\r') */
1988 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
1992 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
1993 /* put one line break unconditionally */
1994 if (fputc ('\n', f
) < 0)
1997 i
++; /* 2 chars are processed */
1999 if (c
== '\r' && c1
== '\n')
2000 /* Windows line break; go to the next char */
2003 if (c
== '\r' && c1
== '\r')
2005 /* two Macintosh line breaks; put second line break */
2006 if (fputc ('\n', f
) < 0)
2011 if (fputc (c1
, f
) < 0)
2015 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
2016 /* put one line break unconditionally */
2017 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
2020 if (c
== '\r' && c1
== '\n')
2021 /* Windows line break; go to the next char */
2025 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
2026 /* put one line break unconditionally */
2027 if (fputc ('\r', f
) < 0)
2030 i
++; /* 2 chars are processed */
2032 if (c
== '\r' && c1
== '\n')
2033 /* Windows line break; go to the next char */
2036 if (c
== '\n' && c1
== '\n')
2038 /* two Windows line breaks; put second line break */
2039 if (fputc ('\r', f
) < 0)
2044 if (fputc (c1
, f
) < 0)
2047 case LB_ASIS
: /* default without changes */
2053 return edit
->last_byte
;
2056 /* --------------------------------------------------------------------------------------------- */
2057 /** inserts a file at the cursor, returns count of inserted bytes on success */
2059 edit_insert_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
2064 p
= edit_get_filter (filename_vpath
);
2068 long current
= edit
->curs1
;
2070 f
= (FILE *) popen (p
, "r");
2073 edit_insert_stream (edit
, f
);
2074 ins_len
= edit
->curs1
- current
;
2075 edit_cursor_move (edit
, -ins_len
);
2080 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
2081 edit_error_dialog (_("Error"), errmsg
);
2090 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
2091 edit_error_dialog (_("Error"), errmsg
);
2099 int i
, file
, blocklen
;
2100 long current
= edit
->curs1
;
2101 int vertical_insertion
= 0;
2104 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
2108 buf
= g_malloc0 (TEMP_BUF_LEN
);
2109 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2112 /* if contain signature VERTICAL_MAGIC then it vertical block */
2113 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2114 vertical_insertion
= 1;
2116 mc_lseek (file
, 0, SEEK_SET
);
2119 if (vertical_insertion
)
2124 blocklen
= edit_insert_column_of_text_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
2125 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2126 /* highlight inserted text then not persistent blocks */
2127 if (!option_persistent_selections
)
2129 if (!edit
->column_highlight
)
2130 edit_push_undo_action (edit
, COLUMN_OFF
);
2131 edit
->column_highlight
= 1;
2136 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2138 for (i
= 0; i
< blocklen
; i
++)
2139 edit_insert (edit
, buf
[i
]);
2141 /* highlight inserted text then not persistent blocks */
2142 if (!option_persistent_selections
&& edit
->modified
)
2144 edit_set_markers (edit
, edit
->curs1
, current
, 0, 0);
2145 if (edit
->column_highlight
)
2146 edit_push_undo_action (edit
, COLUMN_ON
);
2147 edit
->column_highlight
= 0;
2151 edit
->force
|= REDRAW_PAGE
;
2152 ins_len
= edit
->curs1
- current
;
2153 edit_cursor_move (edit
, -ins_len
);
2163 /* --------------------------------------------------------------------------------------------- */
2165 * Fill in the edit structure. Return NULL on failure. Pass edit as
2166 * NULL to allocate a new structure.
2168 * If line is 0, try to restore saved position. Otherwise put the
2169 * cursor on that line and show it in the middle of the screen.
2173 edit_init (WEdit
* edit
, int y
, int x
, int lines
, int cols
, const vfs_path_t
* filename_vpath
,
2176 gboolean to_free
= FALSE
;
2178 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2179 if (option_line_state
)
2180 option_line_state_width
= LINE_STATE_WIDTH
;
2182 option_line_state_width
= 0;
2185 edit_purge_widget (edit
);
2190 * Expand option_whole_chars_search by national letters using
2194 static char option_whole_chars_search_buf
[256];
2196 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
2199 size_t len
= str_term_width1 (option_whole_chars_search
);
2201 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
2203 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
2205 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
2207 option_whole_chars_search_buf
[len
++] = i
;
2211 option_whole_chars_search_buf
[len
] = 0;
2212 option_whole_chars_search
= option_whole_chars_search_buf
;
2214 #endif /* ENABLE_NLS */
2215 edit
= g_malloc0 (sizeof (WEdit
));
2219 edit
->drag_state
= MCEDIT_DRAG_NORMAL
;
2222 edit
->widget
.lines
= lines
;
2223 edit
->widget
.cols
= cols
;
2224 edit_save_size (edit
);
2226 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2227 edit
->stat1
.st_uid
= getuid ();
2228 edit
->stat1
.st_gid
= getgid ();
2229 edit
->stat1
.st_mtime
= 0;
2233 edit
->force
|= REDRAW_PAGE
;
2235 /* set file name before load file */
2236 edit_set_filename (edit
, filename_vpath
);
2238 edit
->undo_stack_size
= START_STACK_SIZE
;
2239 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2240 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2242 edit
->redo_stack_size
= START_STACK_SIZE
;
2243 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2244 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2247 edit
->converter
= str_cnv_from_term
;
2248 edit_set_codeset (edit
);
2250 if (!edit_load_file (edit
))
2252 /* edit_load_file already gives an error message */
2258 edit
->loading_done
= 1;
2261 edit_load_syntax (edit
, NULL
, NULL
);
2264 edit_get_syntax_color (edit
, -1, &color
);
2267 /* load saved cursor position */
2268 if ((line
== 0) && option_save_position
)
2269 edit_load_position (edit
);
2274 edit_move_display (edit
, line
- 1);
2275 edit_move_to_line (edit
, line
- 1);
2278 edit_load_macro_cmd (edit
);
2282 /* --------------------------------------------------------------------------------------------- */
2284 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2286 edit_clean (WEdit
* edit
)
2293 /* a stale lock, remove it */
2295 edit
->locked
= unlock_file (edit
->filename_vpath
);
2297 /* save cursor position */
2298 if (option_save_position
)
2299 edit_save_position (edit
);
2300 else if (edit
->serialized_bookmarks
!= NULL
)
2301 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2303 /* File specified on the mcedit command line and never saved */
2304 if (edit
->delete_file
)
2305 unlink (vfs_path_get_last_path_str (edit
->filename_vpath
));
2307 edit_free_syntax_rules (edit
);
2308 book_mark_flush (edit
, -1);
2309 for (; j
<= MAXBUFF
; j
++)
2311 g_free (edit
->buffers1
[j
]);
2312 g_free (edit
->buffers2
[j
]);
2315 g_free (edit
->undo_stack
);
2316 g_free (edit
->redo_stack
);
2317 vfs_path_free (edit
->filename_vpath
);
2318 vfs_path_free (edit
->dir_vpath
);
2319 mc_search_free (edit
->search
);
2320 edit
->search
= NULL
;
2322 if (edit
->converter
!= str_cnv_from_term
)
2323 str_close_conv (edit
->converter
);
2325 edit_purge_widget (edit
);
2330 /* --------------------------------------------------------------------------------------------- */
2333 * Load a new file into the editor and set line. If it fails, preserve the old file.
2334 * To do it, allocate a new widget, initialize it and, if the new file
2335 * was loaded, copy the data to the old widget.
2337 * @returns TRUE on success, FALSE on failure.
2340 edit_reload_line (WEdit
* edit
, const vfs_path_t
* filename_vpath
, long line
)
2343 int y
= edit
->widget
.y
;
2344 int x
= edit
->widget
.x
;
2345 int lines
= edit
->widget
.lines
;
2346 int columns
= edit
->widget
.cols
;
2348 e
= g_malloc0 (sizeof (WEdit
));
2349 e
->widget
= edit
->widget
;
2351 if (edit_init (e
, y
, x
, lines
, columns
, filename_vpath
, line
) == NULL
)
2358 memcpy (edit
, e
, sizeof (WEdit
));
2364 /* --------------------------------------------------------------------------------------------- */
2367 edit_set_codeset (WEdit
* edit
)
2373 get_codepage_id (mc_global
.source_codepage
>=
2374 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2379 conv
= str_crt_conv_from (cp_id
);
2380 if (conv
!= INVALID_CONV
)
2382 if (edit
->converter
!= str_cnv_from_term
)
2383 str_close_conv (edit
->converter
);
2384 edit
->converter
= conv
;
2389 edit
->utf8
= str_isutf8 (cp_id
);
2396 /* --------------------------------------------------------------------------------------------- */
2398 Recording stack for undo:
2399 The following is an implementation of a compressed stack. Identical
2400 pushes are recorded by a negative prefix indicating the number of times the
2401 same char was pushed. This saves space for repeated curs-left or curs-right
2418 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2419 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2420 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2421 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2424 The only way the cursor moves or the buffer is changed is through the routines:
2425 insert, backspace, insert_ahead, delete, and cursor_move.
2426 These record the reverse undo movements onto the stack each time they are
2429 Each key press results in a set of actions (insert; delete ...). So each time
2430 a key is pressed the current position of start_display is pushed as
2431 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2432 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2433 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2438 edit_push_undo_action (WEdit
* edit
, long c
, ...)
2440 unsigned long sp
= edit
->undo_stack_pointer
;
2444 /* first enlarge the stack if necessary */
2445 if (sp
> edit
->undo_stack_size
- 10)
2447 if (option_max_undo
< 256)
2448 option_max_undo
= 256;
2449 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2451 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2454 edit
->undo_stack
= t
;
2455 edit
->undo_stack_size
<<= 1;
2456 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2460 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2461 if (edit
->undo_stack_disable
)
2463 edit_push_redo_action (edit
, KEY_PRESS
);
2464 edit_push_redo_action (edit
, c
);
2467 else if (edit
->redo_stack_reset
)
2469 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2472 if (edit
->undo_stack_bottom
!= sp
2473 && spm1
!= edit
->undo_stack_bottom
2474 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2477 if (edit
->undo_stack
[spm1
] < 0)
2479 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2482 if (edit
->undo_stack
[spm1
] > -1000000000)
2484 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2486 edit
->undo_stack
[spm1
]--;
2494 d
= edit
->undo_stack
[spm1
];
2498 return; /* --> no need to push multiple do-nothings */
2499 edit
->undo_stack
[sp
] = -2;
2504 edit
->undo_stack
[sp
] = c
;
2507 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2509 /* if the sp wraps round and catches the undo_stack_bottom then erase
2510 * the first set of actions on the stack to make space - by moving
2511 * undo_stack_bottom forward one "key press" */
2512 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2513 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2514 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2517 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2519 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2520 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2522 /*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: */
2523 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2524 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2526 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2531 edit_push_redo_action (WEdit
* edit
, long c
, ...)
2533 unsigned long sp
= edit
->redo_stack_pointer
;
2536 /* first enlarge the stack if necessary */
2537 if (sp
> edit
->redo_stack_size
- 10)
2539 if (option_max_undo
< 256)
2540 option_max_undo
= 256;
2541 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2543 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2546 edit
->redo_stack
= t
;
2547 edit
->redo_stack_size
<<= 1;
2548 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2552 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2554 if (edit
->redo_stack_bottom
!= sp
2555 && spm1
!= edit
->redo_stack_bottom
2556 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2559 if (edit
->redo_stack
[spm1
] < 0)
2561 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2564 if (edit
->redo_stack
[spm1
] > -1000000000)
2566 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2567 edit
->redo_stack
[spm1
]--;
2574 d
= edit
->redo_stack
[spm1
];
2578 return; /* --> no need to push multiple do-nothings */
2579 edit
->redo_stack
[sp
] = -2;
2580 goto redo_check_bottom
;
2584 edit
->redo_stack
[sp
] = c
;
2587 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2589 /* if the sp wraps round and catches the redo_stack_bottom then erase
2590 * the first set of actions on the stack to make space - by moving
2591 * redo_stack_bottom forward one "key press" */
2592 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2593 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2594 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2597 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2599 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2600 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2603 * If a single key produced enough pushes to wrap all the way round then
2604 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2605 * The stack is then initialised:
2608 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2609 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2610 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2614 /* --------------------------------------------------------------------------------------------- */
2616 Basic low level single character buffer alterations and movements at the cursor.
2617 Returns char passed over, inserted or removed.
2621 edit_insert (WEdit
* edit
, int c
)
2623 /* check if file has grown to large */
2624 if (edit
->last_byte
>= SIZE_LIMIT
)
2627 /* first we must update the position of the display window */
2628 if (edit
->curs1
< edit
->start_display
)
2630 edit
->start_display
++;
2635 /* Mark file as modified, unless the file hasn't been fully loaded */
2636 if (edit
->loading_done
)
2638 edit_modification (edit
);
2641 /* now we must update some info on the file and check if a redraw is required */
2644 if (edit
->book_mark
)
2645 book_mark_inc (edit
, edit
->curs_line
);
2647 edit
->total_lines
++;
2648 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2651 /* save the reverse command onto the undo stack */
2652 /* ordinary char and not space */
2654 edit_push_undo_action (edit
, BACKSPACE
);
2656 edit_push_undo_action (edit
, BACKSPACE_BR
);
2657 /* update markers */
2658 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2659 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2660 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2662 /* add a new buffer if we've reached the end of the last one */
2663 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2664 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2666 /* perform the insertion */
2667 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2668 = (unsigned char) c
;
2670 /* update file length */
2673 /* update cursor position */
2677 /* --------------------------------------------------------------------------------------------- */
2678 /** same as edit_insert and move left */
2681 edit_insert_ahead (WEdit
* edit
, int c
)
2683 if (edit
->last_byte
>= SIZE_LIMIT
)
2686 if (edit
->curs1
< edit
->start_display
)
2688 edit
->start_display
++;
2692 edit_modification (edit
);
2695 if (edit
->book_mark
)
2696 book_mark_inc (edit
, edit
->curs_line
);
2697 edit
->total_lines
++;
2698 edit
->force
|= REDRAW_AFTER_CURSOR
;
2700 /* ordinary char and not space */
2702 edit_push_undo_action (edit
, DELCHAR
);
2704 edit_push_undo_action (edit
, DELCHAR_BR
);
2706 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2707 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2708 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2710 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2711 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2712 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2713 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2720 /* --------------------------------------------------------------------------------------------- */
2723 edit_delete (WEdit
* edit
, const int byte_delete
)
2733 /* if byte_delete = 1 then delete only one byte not multibyte char */
2734 if (edit
->utf8
&& byte_delete
== 0)
2736 edit_get_utf (edit
, edit
->curs1
, &cw
);
2741 if (edit
->mark2
!= edit
->mark1
)
2742 edit_push_markers (edit
);
2744 for (i
= 1; i
<= cw
; i
++)
2746 if (edit
->mark1
> edit
->curs1
)
2749 edit
->end_mark_curs
--;
2751 if (edit
->mark2
> edit
->curs1
)
2753 if (edit
->last_get_rule
> edit
->curs1
)
2754 edit
->last_get_rule
--;
2756 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2758 1) & M_EDIT_BUF_SIZE
) - 1];
2760 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2762 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2763 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2767 edit_push_undo_action (edit
, p
+ 256);
2770 edit_modification (edit
);
2773 if (edit
->book_mark
)
2774 book_mark_dec (edit
, edit
->curs_line
);
2775 edit
->total_lines
--;
2776 edit
->force
|= REDRAW_AFTER_CURSOR
;
2778 if (edit
->curs1
< edit
->start_display
)
2780 edit
->start_display
--;
2788 /* --------------------------------------------------------------------------------------------- */
2789 /** moves the cursor right or left: increment positive or negative respectively */
2792 edit_cursor_move (WEdit
* edit
, long increment
)
2794 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2799 for (; increment
< 0; increment
++)
2804 edit_push_undo_action (edit
, CURS_RIGHT
);
2806 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2807 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2808 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2809 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2810 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2812 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2813 1) & M_EDIT_BUF_SIZE
];
2814 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2816 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2817 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2823 edit
->force
|= REDRAW_LINE_BELOW
;
2828 else if (increment
> 0)
2830 for (; increment
> 0; increment
--)
2835 edit_push_undo_action (edit
, CURS_LEFT
);
2837 c
= edit_get_byte (edit
, edit
->curs1
);
2838 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2839 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2840 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2842 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2844 1) & M_EDIT_BUF_SIZE
) - 1];
2845 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2847 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2848 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2854 edit
->force
|= REDRAW_LINE_ABOVE
;
2860 /* These functions return positions relative to lines */
2862 /* --------------------------------------------------------------------------------------------- */
2863 /** returns index of last char on line + 1 */
2866 edit_eol (WEdit
* edit
, long current
)
2868 if (current
>= edit
->last_byte
)
2869 return edit
->last_byte
;
2872 if (edit_get_byte (edit
, current
) == '\n')
2877 /* --------------------------------------------------------------------------------------------- */
2878 /** returns index of first char on line */
2881 edit_bol (WEdit
* edit
, long current
)
2887 if (edit_get_byte (edit
, current
- 1) == '\n')
2892 /* --------------------------------------------------------------------------------------------- */
2895 edit_count_lines (WEdit
* edit
, long current
, long upto
)
2898 if (upto
> edit
->last_byte
)
2899 upto
= edit
->last_byte
;
2902 while (current
< upto
)
2903 if (edit_get_byte (edit
, current
++) == '\n')
2908 /* --------------------------------------------------------------------------------------------- */
2909 /* If lines is zero this returns the count of lines from current to upto. */
2910 /* If upto is zero returns index of lines forward current. */
2913 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
2917 return edit_count_lines (edit
, current
, upto
);
2926 next
= edit_eol (edit
, current
) + 1;
2927 if (next
> edit
->last_byte
)
2936 /* --------------------------------------------------------------------------------------------- */
2937 /** Returns offset of 'lines' lines up from current */
2940 edit_move_backward (WEdit
* edit
, long current
, long lines
)
2944 current
= edit_bol (edit
, current
);
2945 while ((lines
--) && current
!= 0)
2946 current
= edit_bol (edit
, current
- 1);
2950 /* --------------------------------------------------------------------------------------------- */
2951 /* If cols is zero this returns the count of columns from current to upto. */
2952 /* If upto is zero returns index of cols across from current. */
2955 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
2966 q
= edit
->last_byte
+ 2;
2968 for (col
= 0, p
= current
; p
< q
; p
++)
2980 orig_c
= c
= edit_get_byte (edit
, p
);
2988 utf_ch
= edit_get_utf (edit
, p
, &cw
);
2989 if (mc_global
.utf8_display
)
2993 if (g_unichar_iswide (utf_ch
))
2996 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
3000 c
= convert_to_display_c (c
);
3004 col
+= TAB_SIZE
- col
% TAB_SIZE
;
3012 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!mc_global
.utf8_display
&& !edit
->utf8
)))
3013 /* '\r' is shown as ^M, so we must advance 2 characters */
3014 /* Caret notation for control characters */
3022 /* --------------------------------------------------------------------------------------------- */
3023 /** returns the current column position of the cursor */
3026 edit_get_col (WEdit
* edit
)
3028 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3031 /* --------------------------------------------------------------------------------------------- */
3032 /* Scrolling functions */
3033 /* --------------------------------------------------------------------------------------------- */
3036 edit_update_curs_row (WEdit
* edit
)
3038 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
3041 /* --------------------------------------------------------------------------------------------- */
3044 edit_update_curs_col (WEdit
* edit
)
3046 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3049 /* --------------------------------------------------------------------------------------------- */
3052 edit_get_curs_col (const WEdit
* edit
)
3054 return edit
->curs_col
;
3057 /* --------------------------------------------------------------------------------------------- */
3058 /** moves the display start position up by i lines */
3061 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
3063 unsigned long lines_above
= edit
->start_line
;
3064 if (i
> lines_above
)
3068 edit
->start_line
-= i
;
3069 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
3070 edit
->force
|= REDRAW_PAGE
;
3071 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3073 edit_update_curs_row (edit
);
3077 /* --------------------------------------------------------------------------------------------- */
3078 /** returns 1 if could scroll, 0 otherwise */
3081 edit_scroll_downward (WEdit
* edit
, int i
)
3084 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->widget
.lines
- 1);
3085 if (lines_below
> 0)
3087 if (i
> lines_below
)
3089 edit
->start_line
+= i
;
3090 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
3091 edit
->force
|= REDRAW_PAGE
;
3092 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3094 edit_update_curs_row (edit
);
3097 /* --------------------------------------------------------------------------------------------- */
3100 edit_scroll_right (WEdit
* edit
, int i
)
3102 edit
->force
|= REDRAW_PAGE
;
3103 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3104 edit
->start_col
-= i
;
3107 /* --------------------------------------------------------------------------------------------- */
3110 edit_scroll_left (WEdit
* edit
, int i
)
3112 if (edit
->start_col
)
3114 edit
->start_col
+= i
;
3115 if (edit
->start_col
> 0)
3116 edit
->start_col
= 0;
3117 edit
->force
|= REDRAW_PAGE
;
3118 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3122 /* --------------------------------------------------------------------------------------------- */
3123 /* high level cursor movement commands */
3124 /* --------------------------------------------------------------------------------------------- */
3127 edit_move_to_prev_col (WEdit
* edit
, long p
)
3129 int prev
= edit
->prev_col
;
3130 int over
= edit
->over_col
;
3131 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
3133 if (option_cursor_beyond_eol
)
3135 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
3136 edit_eol (edit
, edit
->curs1
));
3138 if (line_len
< prev
+ edit
->over_col
)
3140 edit
->over_col
= prev
+ over
- line_len
;
3141 edit
->prev_col
= line_len
;
3142 edit
->curs_col
= line_len
;
3146 edit
->curs_col
= prev
+ over
;
3147 edit
->prev_col
= edit
->curs_col
;
3154 if (is_in_indent (edit
) && option_fake_half_tabs
)
3156 edit_update_curs_col (edit
);
3158 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
3160 int q
= edit
->curs_col
;
3161 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
3162 p
= edit_bol (edit
, edit
->curs1
);
3163 edit_cursor_move (edit
,
3164 edit_move_forward3 (edit
, p
, edit
->curs_col
,
3166 if (!left_of_four_spaces (edit
))
3167 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3173 /* --------------------------------------------------------------------------------------------- */
3176 line_is_blank (WEdit
* edit
, long line
)
3178 return is_blank (edit
, edit_find_line (edit
, line
));
3181 /* --------------------------------------------------------------------------------------------- */
3182 /** move cursor to line 'line' */
3185 edit_move_to_line (WEdit
* e
, long line
)
3187 if (line
< e
->curs_line
)
3188 edit_move_up (e
, e
->curs_line
- line
, 0);
3190 edit_move_down (e
, line
- e
->curs_line
, 0);
3191 edit_scroll_screen_over_cursor (e
);
3194 /* --------------------------------------------------------------------------------------------- */
3195 /** scroll window so that first visible line is 'line' */
3198 edit_move_display (WEdit
* e
, long line
)
3200 if (line
< e
->start_line
)
3201 edit_scroll_upward (e
, e
->start_line
- line
);
3203 edit_scroll_downward (e
, line
- e
->start_line
);
3206 /* --------------------------------------------------------------------------------------------- */
3207 /** save markers onto undo stack */
3210 edit_push_markers (WEdit
* edit
)
3212 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3213 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3214 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3217 /* --------------------------------------------------------------------------------------------- */
3220 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
3229 /* --------------------------------------------------------------------------------------------- */
3230 /** highlight marker toggle */
3233 edit_mark_cmd (WEdit
* edit
, int unmark
)
3235 edit_push_markers (edit
);
3238 edit_set_markers (edit
, 0, 0, 0, 0);
3239 edit
->force
|= REDRAW_PAGE
;
3243 if (edit
->mark2
>= 0)
3245 edit
->end_mark_curs
= -1;
3246 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3247 edit
->curs_col
+ edit
->over_col
);
3248 edit
->force
|= REDRAW_PAGE
;
3252 edit
->end_mark_curs
= edit
->curs1
;
3253 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3254 edit
->curs_col
+ edit
->over_col
);
3259 /* --------------------------------------------------------------------------------------------- */
3260 /** highlight the word under cursor */
3263 edit_mark_current_word_cmd (WEdit
* edit
)
3267 for (pos
= edit
->curs1
; pos
!= 0; pos
--)
3271 c1
= edit_get_byte (edit
, pos
);
3272 c2
= edit_get_byte (edit
, pos
- 1);
3273 if (!isspace (c1
) && isspace (c2
))
3275 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3280 for (; pos
< edit
->last_byte
; pos
++)
3284 c1
= edit_get_byte (edit
, pos
);
3285 c2
= edit_get_byte (edit
, pos
+ 1);
3286 if (!isspace (c1
) && isspace (c2
))
3288 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3291 edit
->mark2
= min (pos
+ 1, edit
->last_byte
);
3293 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3296 /* --------------------------------------------------------------------------------------------- */
3299 edit_mark_current_line_cmd (WEdit
* edit
)
3301 long pos
= edit
->curs1
;
3303 edit
->mark1
= edit_bol (edit
, pos
);
3304 edit
->mark2
= edit_eol (edit
, pos
);
3306 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3309 /* --------------------------------------------------------------------------------------------- */
3312 edit_delete_line (WEdit
* edit
)
3315 * Delete right part of the line.
3316 * Note that edit_get_byte() returns '\n' when byte position is
3319 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3321 (void) edit_delete (edit
, 1);
3326 * Note that edit_delete() will not corrupt anything if called while
3327 * cursor position is EOF.
3329 (void) edit_delete (edit
, 1);
3332 * Delete left part of the line.
3333 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3335 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3337 (void) edit_backspace (edit
, 1);
3341 /* --------------------------------------------------------------------------------------------- */
3344 edit_indent_width (WEdit
* edit
, long p
)
3347 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
3349 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
3352 /* --------------------------------------------------------------------------------------------- */
3355 edit_insert_indent (WEdit
* edit
, int indent
)
3357 if (!option_fill_tabs_with_spaces
)
3359 while (indent
>= TAB_SIZE
)
3361 edit_insert (edit
, '\t');
3365 while (indent
-- > 0)
3366 edit_insert (edit
, ' ');
3369 /* --------------------------------------------------------------------------------------------- */
3372 edit_push_key_press (WEdit
* edit
)
3374 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3375 if (edit
->mark2
== -1)
3377 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3378 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3382 /* --------------------------------------------------------------------------------------------- */
3385 edit_find_bracket (WEdit
* edit
)
3387 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3388 if (last_bracket
!= edit
->bracket
)
3389 edit
->force
|= REDRAW_PAGE
;
3390 last_bracket
= edit
->bracket
;
3393 /* --------------------------------------------------------------------------------------------- */
3395 * This executes a command as though the user initiated it through a key
3396 * press. Callback with WIDGET_KEY as a message calls this after
3397 * translating the key press. This function can be used to pass any
3398 * command to the editor. Note that the screen wouldn't update
3399 * automatically. Either of command or char_for_insertion must be
3400 * passed as -1. Commands are executed, and char_for_insertion is
3401 * inserted at the cursor.
3405 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3407 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3409 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3412 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3415 if (macro_index
!= -1)
3417 edit
->force
|= REDRAW_COMPLETELY
;
3418 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3420 edit_store_macro_cmd (edit
);
3424 else if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3426 edit_repeat_macro_cmd (edit
);
3432 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3434 record_macro_buf
[macro_index
].action
= command
;
3435 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3437 /* record the beginning of a set of editing actions initiated by a key press */
3438 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3439 edit_push_key_press (edit
);
3441 edit_execute_cmd (edit
, command
, char_for_insertion
);
3442 if (edit
->column_highlight
)
3443 edit
->force
|= REDRAW_PAGE
;
3446 /* --------------------------------------------------------------------------------------------- */
3448 This executes a command at a lower level than macro recording.
3449 It also does not push a key_press onto the undo stack. This means
3450 that if it is called many times, a single undo command will undo
3451 all of them. It also does not check for the Undo command.
3454 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3456 /* handle window state */
3457 if (edit_handle_move_resize (edit
, command
))
3460 edit
->force
|= REDRAW_LINE
;
3462 /* The next key press will unhighlight the found string, so update
3464 if (edit
->found_len
|| edit
->column_highlight
)
3465 edit
->force
|= REDRAW_PAGE
;
3469 /* a mark command with shift-arrow */
3472 case CK_MarkToWordBegin
:
3473 case CK_MarkToWordEnd
:
3479 case CK_MarkPageDown
:
3480 case CK_MarkToFileBegin
:
3481 case CK_MarkToFileEnd
:
3482 case CK_MarkToPageBegin
:
3483 case CK_MarkToPageEnd
:
3484 case CK_MarkScrollUp
:
3485 case CK_MarkScrollDown
:
3486 case CK_MarkParagraphUp
:
3487 case CK_MarkParagraphDown
:
3488 /* a mark command with alt-arrow */
3489 case CK_MarkColumnPageUp
:
3490 case CK_MarkColumnPageDown
:
3491 case CK_MarkColumnLeft
:
3492 case CK_MarkColumnRight
:
3493 case CK_MarkColumnUp
:
3494 case CK_MarkColumnDown
:
3495 case CK_MarkColumnScrollUp
:
3496 case CK_MarkColumnScrollDown
:
3497 case CK_MarkColumnParagraphUp
:
3498 case CK_MarkColumnParagraphDown
:
3499 edit
->column_highlight
= 0;
3500 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3502 edit_mark_cmd (edit
, 1); /* clear */
3503 edit_mark_cmd (edit
, 0); /* marking on */
3505 edit
->highlight
= 1;
3508 /* any other command */
3510 if (edit
->highlight
)
3511 edit_mark_cmd (edit
, 0); /* clear */
3512 edit
->highlight
= 0;
3515 /* first check for undo */
3516 if (command
== CK_Undo
)
3518 edit
->redo_stack_reset
= 0;
3519 edit_group_undo (edit
);
3520 edit
->found_len
= 0;
3521 edit
->prev_col
= edit_get_col (edit
);
3522 edit
->search_start
= edit
->curs1
;
3525 /* check for redo */
3526 if (command
== CK_Redo
)
3528 edit
->redo_stack_reset
= 0;
3529 edit_do_redo (edit
);
3530 edit
->found_len
= 0;
3531 edit
->prev_col
= edit_get_col (edit
);
3532 edit
->search_start
= edit
->curs1
;
3536 edit
->redo_stack_reset
= 1;
3538 /* An ordinary key press */
3539 if (char_for_insertion
>= 0)
3541 /* if non persistent selection and text selected */
3542 if (!option_persistent_selections
)
3544 if (edit
->mark1
!= edit
->mark2
)
3545 edit_block_delete_cmd (edit
);
3547 if (edit
->overwrite
)
3549 /* remove char only one time, after input first byte, multibyte chars */
3550 if ((!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3551 && edit_get_byte (edit
, edit
->curs1
) != '\n')
3552 edit_delete (edit
, 0);
3554 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3555 edit_insert_over (edit
);
3557 if (char_for_insertion
> 255 && !mc_global
.utf8_display
)
3559 unsigned char str
[6 + 1];
3563 res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3573 while (str
[i
] != 0 && i
<= 6)
3575 char_for_insertion
= str
[i
];
3576 edit_insert (edit
, char_for_insertion
);
3582 edit_insert (edit
, char_for_insertion
);
3584 if (option_auto_para_formatting
)
3586 format_paragraph (edit
, 0);
3587 edit
->force
|= REDRAW_PAGE
;
3590 check_and_wrap_line (edit
);
3591 edit
->found_len
= 0;
3592 edit
->prev_col
= edit_get_col (edit
);
3593 edit
->search_start
= edit
->curs1
;
3594 edit_find_bracket (edit
);
3600 case CK_TopOnScreen
:
3601 case CK_BottomOnScreen
:
3614 if (edit
->mark2
>= 0)
3616 if (!option_persistent_selections
)
3618 if (edit
->column_highlight
)
3619 edit_push_undo_action (edit
, COLUMN_ON
);
3620 edit
->column_highlight
= 0;
3621 edit_mark_cmd (edit
, 1);
3628 case CK_TopOnScreen
:
3629 case CK_BottomOnScreen
:
3630 case CK_MarkToPageBegin
:
3631 case CK_MarkToPageEnd
:
3636 case CK_MarkToWordBegin
:
3637 case CK_MarkToWordEnd
:
3640 case CK_MarkColumnUp
:
3641 case CK_MarkColumnDown
:
3642 if (edit
->mark2
== -1)
3643 break; /*marking is following the cursor: may need to highlight a whole line */
3648 edit
->force
|= REDRAW_CHAR_ONLY
;
3651 /* basic cursor key commands */
3655 /* if non persistent selection and text selected */
3656 if (!option_persistent_selections
)
3658 if (edit
->mark1
!= edit
->mark2
)
3660 edit_block_delete_cmd (edit
);
3664 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3669 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3671 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3672 edit_backspace (edit
, 1);
3677 if (option_fake_half_tabs
)
3680 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3682 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3683 edit_backspace (edit
, 1);
3688 edit_backspace (edit
, 0);
3691 /* if non persistent selection and text selected */
3692 if (!option_persistent_selections
)
3694 if (edit
->mark1
!= edit
->mark2
)
3696 edit_block_delete_cmd (edit
);
3701 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3702 edit_insert_over (edit
);
3704 if (option_fake_half_tabs
)
3707 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3709 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3710 edit_delete (edit
, 1);
3714 edit_delete (edit
, 0);
3716 case CK_DeleteToWordBegin
:
3718 edit_left_delete_word (edit
);
3720 case CK_DeleteToWordEnd
:
3721 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3722 edit_insert_over (edit
);
3724 edit_right_delete_word (edit
);
3727 edit_delete_line (edit
);
3729 case CK_DeleteToHome
:
3730 edit_delete_to_line_begin (edit
);
3732 case CK_DeleteToEnd
:
3733 edit_delete_to_line_end (edit
);
3737 if (option_auto_para_formatting
)
3739 edit_double_newline (edit
);
3740 if (option_return_does_auto_indent
)
3741 edit_auto_indent (edit
);
3742 format_paragraph (edit
, 0);
3746 edit_insert (edit
, '\n');
3747 if (option_return_does_auto_indent
)
3749 edit_auto_indent (edit
);
3754 edit_insert (edit
, '\n');
3757 case CK_MarkColumnPageUp
:
3758 edit
->column_highlight
= 1;
3761 edit_move_up (edit
, edit
->widget
.lines
- 1, 1);
3763 case CK_MarkColumnPageDown
:
3764 edit
->column_highlight
= 1;
3766 case CK_MarkPageDown
:
3767 edit_move_down (edit
, edit
->widget
.lines
- 1, 1);
3769 case CK_MarkColumnLeft
:
3770 edit
->column_highlight
= 1;
3773 if (option_fake_half_tabs
)
3775 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3777 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3780 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3781 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3785 edit_left_char_move_cmd (edit
);
3787 case CK_MarkColumnRight
:
3788 edit
->column_highlight
= 1;
3791 if (option_fake_half_tabs
)
3793 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3795 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3796 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3800 edit_right_char_move_cmd (edit
);
3802 case CK_TopOnScreen
:
3803 case CK_MarkToPageBegin
:
3804 edit_begin_page (edit
);
3806 case CK_BottomOnScreen
:
3807 case CK_MarkToPageEnd
:
3808 edit_end_page (edit
);
3811 case CK_MarkToWordBegin
:
3813 edit_left_word_move_cmd (edit
);
3816 case CK_MarkToWordEnd
:
3818 edit_right_word_move_cmd (edit
);
3820 case CK_MarkColumnUp
:
3821 edit
->column_highlight
= 1;
3824 edit_move_up (edit
, 1, 0);
3826 case CK_MarkColumnDown
:
3827 edit
->column_highlight
= 1;
3830 edit_move_down (edit
, 1, 0);
3832 case CK_MarkColumnParagraphUp
:
3833 edit
->column_highlight
= 1;
3834 case CK_ParagraphUp
:
3835 case CK_MarkParagraphUp
:
3836 edit_move_up_paragraph (edit
, 0);
3838 case CK_MarkColumnParagraphDown
:
3839 edit
->column_highlight
= 1;
3840 case CK_ParagraphDown
:
3841 case CK_MarkParagraphDown
:
3842 edit_move_down_paragraph (edit
, 0);
3844 case CK_MarkColumnScrollUp
:
3845 edit
->column_highlight
= 1;
3847 case CK_MarkScrollUp
:
3848 edit_move_up (edit
, 1, 1);
3850 case CK_MarkColumnScrollDown
:
3851 edit
->column_highlight
= 1;
3853 case CK_MarkScrollDown
:
3854 edit_move_down (edit
, 1, 1);
3858 edit_cursor_to_bol (edit
);
3862 edit_cursor_to_eol (edit
);
3865 /* if text marked shift block */
3866 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3868 if (edit
->mark2
< 0)
3869 edit_mark_cmd (edit
, 0);
3870 edit_move_block_to_right (edit
);
3874 if (option_cursor_beyond_eol
)
3875 edit_insert_over (edit
);
3876 edit_tab_cmd (edit
);
3877 if (option_auto_para_formatting
)
3879 format_paragraph (edit
, 0);
3880 edit
->force
|= REDRAW_PAGE
;
3884 check_and_wrap_line (edit
);
3889 case CK_InsertOverwrite
:
3890 edit
->overwrite
= !edit
->overwrite
;
3894 if (edit
->mark2
>= 0)
3896 if (edit
->column_highlight
)
3897 edit_push_undo_action (edit
, COLUMN_ON
);
3898 edit
->column_highlight
= 0;
3900 edit_mark_cmd (edit
, 0);
3903 if (!edit
->column_highlight
)
3904 edit_push_undo_action (edit
, COLUMN_OFF
);
3905 edit
->column_highlight
= 1;
3906 edit_mark_cmd (edit
, 0);
3909 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3910 edit
->force
|= REDRAW_PAGE
;
3913 if (edit
->column_highlight
)
3914 edit_push_undo_action (edit
, COLUMN_ON
);
3915 edit
->column_highlight
= 0;
3916 edit_mark_cmd (edit
, 1);
3919 if (edit
->column_highlight
)
3920 edit_push_undo_action (edit
, COLUMN_ON
);
3921 edit
->column_highlight
= 0;
3922 edit_mark_current_word_cmd (edit
);
3925 if (edit
->column_highlight
)
3926 edit_push_undo_action (edit
, COLUMN_ON
);
3927 edit
->column_highlight
= 0;
3928 edit_mark_current_line_cmd (edit
);
3931 case CK_ShowNumbers
:
3932 option_line_state
= !option_line_state
;
3933 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
3934 edit
->force
|= REDRAW_PAGE
;
3938 show_right_margin
= !show_right_margin
;
3939 edit
->force
|= REDRAW_PAGE
;
3943 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
3944 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
3945 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3947 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3949 case CK_BookmarkFlush
:
3950 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3951 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3952 edit
->force
|= REDRAW_PAGE
;
3954 case CK_BookmarkNext
:
3955 if (edit
->book_mark
)
3957 struct _book_mark
*p
;
3958 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3962 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
3963 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
3964 edit_move_to_line (edit
, p
->line
);
3968 case CK_BookmarkPrev
:
3969 if (edit
->book_mark
)
3971 struct _book_mark
*p
;
3972 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3973 while (p
->line
== edit
->curs_line
)
3978 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
|| p
->line
< edit
->start_line
)
3979 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
3980 edit_move_to_line (edit
, p
->line
);
3986 case CK_MarkToFileBegin
:
3987 edit_move_to_top (edit
);
3990 case CK_MarkToFileEnd
:
3991 edit_move_to_bottom (edit
);
3995 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3996 edit_insert_over (edit
);
3997 edit_block_copy_cmd (edit
);
4000 edit_block_delete_cmd (edit
);
4003 edit_block_move_cmd (edit
);
4006 case CK_BlockShiftLeft
:
4007 if (edit
->mark1
!= edit
->mark2
)
4008 edit_move_block_to_left (edit
);
4010 case CK_BlockShiftRight
:
4011 if (edit
->mark1
!= edit
->mark2
)
4012 edit_move_block_to_right (edit
);
4015 edit_copy_to_X_buf_cmd (edit
);
4018 edit_cut_to_X_buf_cmd (edit
);
4021 /* if non persistent selection and text selected */
4022 if (!option_persistent_selections
)
4024 if (edit
->mark1
!= edit
->mark2
)
4025 edit_block_delete_cmd (edit
);
4027 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
4028 edit_insert_over (edit
);
4029 edit_paste_from_X_buf_cmd (edit
);
4032 edit_paste_from_history (edit
);
4036 edit_save_as_cmd (edit
);
4039 edit_save_confirm_cmd (edit
);
4042 edit_save_block_cmd (edit
);
4045 edit_insert_file_cmd (edit
);
4049 edit_load_back_cmd (edit
);
4052 edit_load_forward_cmd (edit
);
4055 case CK_SyntaxChoose
:
4056 edit_syntax_dialog (edit
);
4059 case CK_SyntaxOnOff
:
4060 option_syntax_highlighting
^= 1;
4061 if (option_syntax_highlighting
== 1)
4062 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
4063 edit
->force
|= REDRAW_PAGE
;
4067 enable_show_tabs_tws
^= 1;
4068 edit
->force
|= REDRAW_PAGE
;
4072 edit_search_cmd (edit
, FALSE
);
4074 case CK_SearchContinue
:
4075 edit_search_cmd (edit
, TRUE
);
4078 edit_replace_cmd (edit
, 0);
4080 case CK_ReplaceContinue
:
4081 edit_replace_cmd (edit
, 1);
4084 /* if text marked shift block */
4085 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
4087 edit_move_block_to_left (edit
);
4091 edit_complete_word_cmd (edit
);
4095 edit_get_match_keyword_cmd (edit
);
4100 /* fool gcc to prevent a Y2K warning */
4101 char time_format
[] = "_c";
4102 time_format
[0] = '%';
4104 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4105 edit_print_string (edit
, s
);
4106 edit
->force
|= REDRAW_PAGE
;
4111 edit_goto_cmd (edit
);
4113 case CK_ParagraphFormat
:
4114 format_paragraph (edit
, 1);
4115 edit
->force
|= REDRAW_PAGE
;
4117 case CK_MacroDelete
:
4118 edit_delete_macro_cmd (edit
);
4120 case CK_MatchBracket
:
4121 edit_goto_matching_bracket (edit
);
4124 user_menu (edit
, NULL
, -1);
4127 edit_sort_cmd (edit
);
4129 case CK_ExternalCommand
:
4130 edit_ext_cmd (edit
);
4133 edit_mail_dialog (edit
);
4136 case CK_SelectCodepage
:
4137 edit_select_codepage_cmd (edit
);
4140 case CK_InsertLiteral
:
4141 edit_insert_literal_cmd (edit
);
4143 case CK_MacroStartStopRecord
:
4144 edit_begin_end_macro_cmd (edit
);
4146 case CK_RepeatStartStopRecord
:
4147 edit_begin_end_repeat_cmd (edit
);
4149 case CK_ExtendedKeyMap
:
4150 edit
->extmod
= TRUE
;
4157 if ((command
/ CK_PipeBlock (0)) == 1)
4158 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
4160 /* keys which must set the col position, and the search vars */
4164 case CK_SearchContinue
:
4166 case CK_ReplaceContinue
:
4168 edit
->prev_col
= edit_get_col (edit
);
4172 case CK_MarkColumnUp
:
4175 case CK_MarkColumnDown
:
4178 case CK_MarkColumnPageUp
:
4180 case CK_MarkPageDown
:
4181 case CK_MarkColumnPageDown
:
4183 case CK_MarkToFileBegin
:
4185 case CK_MarkToFileEnd
:
4186 case CK_ParagraphUp
:
4187 case CK_MarkParagraphUp
:
4188 case CK_MarkColumnParagraphUp
:
4189 case CK_ParagraphDown
:
4190 case CK_MarkParagraphDown
:
4191 case CK_MarkColumnParagraphDown
:
4193 case CK_MarkScrollUp
:
4194 case CK_MarkColumnScrollUp
:
4196 case CK_MarkScrollDown
:
4197 case CK_MarkColumnScrollDown
:
4198 edit
->search_start
= edit
->curs1
;
4199 edit
->found_len
= 0;
4202 edit
->found_len
= 0;
4203 edit
->prev_col
= edit_get_col (edit
);
4204 edit
->search_start
= edit
->curs1
;
4206 edit_find_bracket (edit
);
4208 if (option_auto_para_formatting
)
4214 case CK_DeleteToWordBegin
:
4215 case CK_DeleteToWordEnd
:
4216 case CK_DeleteToHome
:
4217 case CK_DeleteToEnd
:
4218 format_paragraph (edit
, 0);
4219 edit
->force
|= REDRAW_PAGE
;
4224 /* --------------------------------------------------------------------------------------------- */
4227 edit_stack_init (void)
4229 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4231 edit_history_moveto
[edit_stack_iterator
].filename_vpath
= NULL
;
4232 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4235 edit_stack_iterator
= 0;
4238 /* --------------------------------------------------------------------------------------------- */
4241 edit_stack_free (void)
4243 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4244 vfs_path_free (edit_history_moveto
[edit_stack_iterator
].filename_vpath
);
4247 /* --------------------------------------------------------------------------------------------- */
4251 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
4253 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4256 /* --------------------------------------------------------------------------------------------- */
4260 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
4262 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4265 /* --------------------------------------------------------------------------------------------- */