(edit_insert_file_cmd): sync with new edit_insert_file()
[midnight-commander.git] / src / editor / editcmd.c
blobaf98b97eddbc5cc68a27d9abcb6f13e8a1f8a3f0
1 /*
2 Editor high level editing commands
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer, 1996, 1997
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /** \file
28 * \brief Source: editor high level editing commands
29 * \author Paul Sheer
30 * \date 1996, 1997
33 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
35 #include <config.h>
37 #include <assert.h>
38 #include <ctype.h>
40 #include <stdio.h>
41 #include <stdarg.h>
42 #include <sys/types.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <errno.h>
46 #include <sys/stat.h>
47 #include <stdlib.h>
48 #include <fcntl.h>
50 #include "lib/global.h"
51 #include "lib/tty/tty.h"
52 #include "lib/tty/key.h" /* XCTRL */
53 #include "lib/mcconfig.h"
54 #include "lib/skin.h"
55 #include "lib/strutil.h" /* utf string functions */
56 #include "lib/lock.h"
57 #include "lib/util.h" /* tilde_expand() */
58 #include "lib/vfs/vfs.h"
59 #include "lib/widget.h"
60 #include "lib/charsets.h"
61 #include "lib/event.h" /* mc_event_raise() */
63 #include "src/history.h"
64 #include "src/setup.h" /* option_tab_spacing */
65 #include "src/main.h" /* mactos_t */
66 #include "src/selcodepage.h"
67 #include "src/keybind-defaults.h"
68 #include "src/util.h" /* check_for_default() */
69 #include "src/filemanager/layout.h" /* mc_refresh() */
71 #include "edit-impl.h"
72 #include "edit-widget.h"
73 #include "editcmd_dialogs.h"
74 #include "etags.h"
76 /*** global variables ****************************************************************************/
78 /* search and replace: */
79 int search_create_bookmark = FALSE;
81 /* queries on a save */
82 int edit_confirm_save = 1;
84 /*** file scope macro definitions ****************************************************************/
86 #define space_width 1
88 #define TEMP_BUF_LEN 1024
90 #define INPUT_INDEX 9
92 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
93 (and the above) routines to work properly - paul */
95 #define is_digit(x) ((x) >= '0' && (x) <= '9')
97 #define MAIL_DLG_HEIGHT 12
99 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
101 /*** file scope type declarations ****************************************************************/
103 /*** file scope variables ************************************************************************/
105 /*** file scope functions ************************************************************************/
106 /* --------------------------------------------------------------------------------------------- */
108 /* If 0 (quick save) then a) create/truncate <filename> file,
109 b) save to <filename>;
110 if 1 (safe save) then a) save to <tempnam>,
111 b) rename <tempnam> to <filename>;
112 if 2 (do backups) then a) save to <tempnam>,
113 b) rename <filename> to <filename.backup_ext>,
114 c) rename <tempnam> to <filename>. */
116 /* returns 0 on error, -1 on abort */
118 static int
119 edit_save_file (WEdit * edit, const char *filename)
121 char *p;
122 gchar *tmp;
123 long filelen = 0;
124 char *savename = 0;
125 gchar *real_filename;
126 int this_save_mode, fd = -1;
128 if (!filename)
129 return 0;
130 if (!*filename)
131 return 0;
133 if (*filename != PATH_SEP && edit->dir)
135 real_filename = concat_dir_and_file (edit->dir, filename);
137 else
139 real_filename = g_strdup (filename);
142 this_save_mode = option_save_mode;
143 if (this_save_mode != EDIT_QUICK_SAVE)
145 vfs_path_t *vpath = vfs_path_from_str (real_filename);
146 if (!vfs_file_is_local (vpath) || (fd = mc_open (real_filename, O_RDONLY | O_BINARY)) == -1)
149 * The file does not exists yet, so no safe save or
150 * backup are necessary.
152 this_save_mode = EDIT_QUICK_SAVE;
154 vfs_path_free (vpath);
155 if (fd != -1)
156 mc_close (fd);
159 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt)
161 int rv;
162 struct stat sb;
164 rv = mc_stat (real_filename, &sb);
165 if (rv == 0 && sb.st_nlink > 1)
167 rv = edit_query_dialog3 (_("Warning"),
168 _("File has hard-links. Detach before saving?"),
169 _("&Yes"), _("&No"), _("&Cancel"));
170 switch (rv)
172 case 0:
173 this_save_mode = EDIT_SAFE_SAVE;
174 /* fallthrough */
175 case 1:
176 edit->skip_detach_prompt = 1;
177 break;
178 default:
179 g_free (real_filename);
180 return -1;
184 /* Prevent overwriting changes from other editor sessions. */
185 if (rv == 0 && edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
188 /* The default action is "Cancel". */
189 query_set_sel (1);
191 rv = edit_query_dialog2 (_("Warning"),
192 _("The file has been modified in the meantime. Save anyway?"),
193 _("&Yes"), _("&Cancel"));
194 if (rv != 0)
196 g_free (real_filename);
197 return -1;
202 if (this_save_mode != EDIT_QUICK_SAVE)
204 char *savedir, *saveprefix;
205 const char *slashpos;
206 slashpos = strrchr (real_filename, PATH_SEP);
207 if (slashpos)
209 savedir = g_strdup (real_filename);
210 savedir[slashpos - real_filename + 1] = '\0';
212 else
213 savedir = g_strdup (".");
214 saveprefix = concat_dir_and_file (savedir, "cooledit");
215 g_free (savedir);
216 fd = mc_mkstemps (&savename, saveprefix, NULL);
217 g_free (saveprefix);
218 if (!savename)
220 g_free (real_filename);
221 return 0;
223 /* FIXME:
224 * Close for now because mc_mkstemps use pure open system call
225 * to create temporary file and it needs to be reopened by
226 * VFS-aware mc_open().
228 close (fd);
230 else
231 savename = g_strdup (real_filename);
233 int ret;
234 ret = mc_chown (savename, edit->stat1.st_uid, edit->stat1.st_gid);
235 ret = mc_chmod (savename, edit->stat1.st_mode);
238 fd = mc_open (savename, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
239 if (fd == -1)
240 goto error_save;
242 /* pipe save */
243 p = edit_get_write_filter (savename, real_filename);
244 if (p != NULL)
246 FILE *file;
248 mc_close (fd);
249 file = (FILE *) popen (p, "w");
251 if (file)
253 filelen = edit_write_stream (edit, file);
254 #if 1
255 pclose (file);
256 #else
257 if (pclose (file) != 0)
259 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
260 edit_error_dialog (_("Error"), tmp);
261 g_free (tmp);
262 g_free (p);
263 goto error_save;
265 #endif
267 else
269 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
270 edit_error_dialog (_("Error"), get_sys_error (tmp));
271 g_free (p);
272 g_free (tmp);
273 goto error_save;
275 g_free (p);
277 else if (edit->lb == LB_ASIS)
278 { /* do not change line breaks */
279 long buf;
280 buf = 0;
281 filelen = edit->last_byte;
282 while (buf <= (edit->curs1 >> S_EDIT_BUF_SIZE) - 1)
284 if (mc_write (fd, (char *) edit->buffers1[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
286 mc_close (fd);
287 goto error_save;
289 buf++;
291 if (mc_write
292 (fd, (char *) edit->buffers1[buf],
293 edit->curs1 & M_EDIT_BUF_SIZE) != (edit->curs1 & M_EDIT_BUF_SIZE))
295 filelen = -1;
297 else if (edit->curs2)
299 edit->curs2--;
300 buf = (edit->curs2 >> S_EDIT_BUF_SIZE);
301 if (mc_write
302 (fd,
303 (char *) edit->buffers2[buf] + EDIT_BUF_SIZE -
304 (edit->curs2 & M_EDIT_BUF_SIZE) - 1,
305 1 + (edit->curs2 & M_EDIT_BUF_SIZE)) != 1 + (edit->curs2 & M_EDIT_BUF_SIZE))
307 filelen = -1;
309 else
311 while (--buf >= 0)
313 if (mc_write (fd, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
315 filelen = -1;
316 break;
320 edit->curs2++;
322 if (mc_close (fd))
323 goto error_save;
325 /* Update the file information, especially the mtime. */
326 if (mc_stat (savename, &edit->stat1) == -1)
327 goto error_save;
329 else
330 { /* change line breaks */
331 FILE *file;
333 mc_close (fd);
335 file = (FILE *) fopen (savename, "w");
337 if (file)
339 filelen = edit_write_stream (edit, file);
340 fclose (file);
342 else
344 char *msg;
346 msg = g_strdup_printf (_("Cannot open file for writing: %s"), savename);
347 edit_error_dialog (_("Error"), msg);
348 g_free (msg);
349 goto error_save;
353 if (filelen != edit->last_byte)
354 goto error_save;
356 if (this_save_mode == EDIT_DO_BACKUP)
358 assert (option_backup_ext != NULL);
359 tmp = g_strconcat (real_filename, option_backup_ext, (char *) NULL);
360 if (mc_rename (real_filename, tmp) == -1)
362 g_free (tmp);
363 goto error_save;
367 if (this_save_mode != EDIT_QUICK_SAVE)
368 if (mc_rename (savename, real_filename) == -1)
369 goto error_save;
370 g_free (savename);
371 g_free (real_filename);
372 return 1;
373 error_save:
374 /* FIXME: Is this safe ?
375 * if (this_save_mode != EDIT_QUICK_SAVE)
376 * mc_unlink (savename);
378 g_free (real_filename);
379 g_free (savename);
380 return 0;
383 /* --------------------------------------------------------------------------------------------- */
385 static gboolean
386 edit_check_newline (WEdit * edit)
388 return !(option_check_nl_at_eof && edit->last_byte > 0
389 && edit_get_byte (edit, edit->last_byte - 1) != '\n'
390 && edit_query_dialog2 (_("Warning"),
391 _("The file you are saving is not finished with a newline"),
392 _("C&ontinue"), _("&Cancel")));
395 /* --------------------------------------------------------------------------------------------- */
397 static char *
398 edit_get_save_file_as (WEdit * edit)
400 #define DLG_WIDTH 64
401 #define DLG_HEIGHT 14
403 static LineBreaks cur_lb = LB_ASIS;
405 char *filename = edit->filename;
407 const char *lb_names[LB_NAMES] = {
408 N_("&Do not change"),
409 N_("&Unix format (LF)"),
410 N_("&Windows/DOS format (CR LF)"),
411 N_("&Macintosh format (CR)")
414 QuickWidget quick_widgets[] = {
415 QUICK_BUTTON (6, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
416 QUICK_BUTTON (2, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
417 QUICK_RADIO (5, DLG_WIDTH, DLG_HEIGHT - 8, DLG_HEIGHT, LB_NAMES, lb_names, (int *) &cur_lb),
418 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 9, DLG_HEIGHT, N_("Change line breaks to:")),
419 QUICK_INPUT (3, DLG_WIDTH, DLG_HEIGHT - 11, DLG_HEIGHT, filename, DLG_WIDTH - 6, 0,
420 "save-as", &filename),
421 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 12, DLG_HEIGHT, N_("Enter file name:")),
422 QUICK_END
425 QuickDialog Quick_options = {
426 DLG_WIDTH, DLG_HEIGHT, -1, -1,
427 N_("Save As"), "[Save File As]",
428 quick_widgets, NULL, FALSE
431 if (quick_dialog (&Quick_options) != B_CANCEL)
433 char *fname;
435 edit->lb = cur_lb;
436 fname = tilde_expand (filename);
437 g_free (filename);
438 return fname;
441 return NULL;
443 #undef DLG_WIDTH
444 #undef DLG_HEIGHT
447 /** returns 1 on success */
449 static int
450 edit_save_cmd (WEdit * edit)
452 int res, save_lock = 0;
454 if (!edit->locked && !edit->delete_file)
455 save_lock = edit_lock_file (edit);
456 res = edit_save_file (edit, edit->filename);
458 /* Maintain modify (not save) lock on failure */
459 if ((res > 0 && edit->locked) || save_lock)
460 edit->locked = edit_unlock_file (edit);
462 /* On failure try 'save as', it does locking on its own */
463 if (!res)
464 return edit_save_as_cmd (edit);
465 edit->force |= REDRAW_COMPLETELY;
466 if (res > 0)
468 edit->delete_file = 0;
469 edit->modified = 0;
472 return 1;
475 /* --------------------------------------------------------------------------------------------- */
476 /** returns 1 on error */
478 static int
479 edit_load_file_from_filename (WEdit * edit, char *exp)
481 int prev_locked = edit->locked;
482 char *prev_filename = g_strdup (edit->filename);
484 if (!edit_reload (edit, exp))
486 g_free (prev_filename);
487 return 1;
490 if (prev_locked)
492 char *fullpath;
494 fullpath = mc_build_filename (edit->dir, prev_filename, (char *) NULL);
495 unlock_file (fullpath);
496 g_free (fullpath);
498 g_free (prev_filename);
499 return 0;
502 /* --------------------------------------------------------------------------------------------- */
504 static void
505 edit_load_syntax_file (WEdit * edit)
507 char *extdir;
508 int dir = 0;
510 if (geteuid () == 0)
512 dir = query_dialog (_("Syntax file edit"),
513 _("Which syntax file you want to edit?"), D_NORMAL, 2,
514 _("&User"), _("&System Wide"));
517 extdir = g_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
518 if (!exist_file (extdir))
520 g_free (extdir);
521 extdir = g_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
524 if (dir == 0)
526 char *buffer;
528 buffer = mc_config_get_full_path (EDIT_SYNTAX_FILE);
529 check_for_default (extdir, buffer);
530 edit_load_file_from_filename (edit, buffer);
531 g_free (buffer);
533 else if (dir == 1)
534 edit_load_file_from_filename (edit, extdir);
536 g_free (extdir);
539 /* --------------------------------------------------------------------------------------------- */
541 static void
542 edit_load_menu_file (WEdit * edit)
544 char *buffer;
545 char *menufile;
546 int dir = 0;
548 dir = query_dialog (_("Menu edit"),
549 _("Which menu file do you want to edit?"), D_NORMAL,
550 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
552 menufile = concat_dir_and_file (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU);
554 if (!exist_file (menufile))
556 g_free (menufile);
557 menufile = concat_dir_and_file (mc_global.share_data_dir, EDIT_GLOBAL_MENU);
560 switch (dir)
562 case 0:
563 buffer = g_strdup (EDIT_LOCAL_MENU);
564 check_for_default (menufile, buffer);
565 chmod (buffer, 0600);
566 break;
568 case 1:
569 buffer = mc_config_get_full_path (EDIT_HOME_MENU);
570 check_for_default (menufile, buffer);
571 break;
573 case 2:
574 buffer = concat_dir_and_file (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU);
575 if (!exist_file (buffer))
577 g_free (buffer);
578 buffer = concat_dir_and_file (mc_global.share_data_dir, EDIT_GLOBAL_MENU);
580 break;
582 default:
583 g_free (menufile);
584 return;
587 edit_load_file_from_filename (edit, buffer);
589 g_free (buffer);
590 g_free (menufile);
593 /* --------------------------------------------------------------------------------------------- */
595 static void
596 edit_delete_column_of_text (WEdit * edit)
598 long p, q, r, m1, m2;
599 long b, c, d, n;
601 eval_marks (edit, &m1, &m2);
602 n = edit_move_forward (edit, m1, 0, m2) + 1;
603 c = edit_move_forward3 (edit, edit_bol (edit, m1), 0, m1);
604 d = edit_move_forward3 (edit, edit_bol (edit, m2), 0, m2);
605 b = max (min (c, d), min (edit->column1, edit->column2));
606 c = max (c, max (edit->column1, edit->column2));
608 while (n--)
610 r = edit_bol (edit, edit->curs1);
611 p = edit_move_forward3 (edit, r, b, 0);
612 q = edit_move_forward3 (edit, r, c, 0);
613 if (p < m1)
614 p = m1;
615 if (q > m2)
616 q = m2;
617 edit_cursor_move (edit, p - edit->curs1);
618 while (q > p)
620 /* delete line between margins */
621 if (edit_get_byte (edit, edit->curs1) != '\n')
622 edit_delete (edit, 1);
623 q--;
625 if (n)
626 /* move to next line except on the last delete */
627 edit_cursor_move (edit, edit_move_forward (edit, edit->curs1, 1, 0) - edit->curs1);
631 /* --------------------------------------------------------------------------------------------- */
632 /** if success return 0 */
634 static int
635 edit_block_delete (WEdit * edit)
637 long count;
638 long start_mark, end_mark;
639 int curs_pos, line_width;
640 long curs_line, c1, c2;
642 if (eval_marks (edit, &start_mark, &end_mark))
643 return 0;
644 if (edit->column_highlight && edit->mark2 < 0)
645 edit_mark_cmd (edit, 0);
646 if ((end_mark - start_mark) > option_max_undo / 2)
648 /* Warning message with a query to continue or cancel the operation */
649 if (edit_query_dialog2
650 (_("Warning"),
652 ("Block is large, you may not be able to undo this action"),
653 _("C&ontinue"), _("&Cancel")))
655 return 1;
658 c1 = min (edit->column1, edit->column2);
659 c2 = max (edit->column1, edit->column2);
660 edit->column1 = c1;
661 edit->column2 = c2;
663 edit_push_markers (edit);
665 curs_line = edit->curs_line;
667 /* calculate line width and cursor position before cut */
668 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
669 edit_eol (edit, edit->curs1));
670 curs_pos = edit->curs_col + edit->over_col;
672 /* move cursor to start of selection */
673 edit_cursor_move (edit, start_mark - edit->curs1);
674 edit_scroll_screen_over_cursor (edit);
675 count = start_mark;
676 if (start_mark < end_mark)
678 if (edit->column_highlight)
680 if (edit->mark2 < 0)
681 edit_mark_cmd (edit, 0);
682 edit_delete_column_of_text (edit);
683 /* move cursor to the saved position */
684 edit_move_to_line (edit, curs_line);
685 /* calculate line width after cut */
686 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
687 edit_eol (edit, edit->curs1));
688 if (option_cursor_beyond_eol && curs_pos > line_width)
689 edit->over_col = curs_pos - line_width;
691 else
693 while (count < end_mark)
695 edit_delete (edit, 1);
696 count++;
700 edit_set_markers (edit, 0, 0, 0, 0);
701 edit->force |= REDRAW_PAGE;
702 return 0;
705 /* --------------------------------------------------------------------------------------------- */
707 static gboolean
708 editcmd_find (WEdit * edit, gsize * len)
710 off_t search_start = edit->search_start;
711 off_t search_end;
712 long start_mark = 0;
713 long end_mark = edit->last_byte;
714 int mark_res = 0;
716 if (edit_search_options.only_in_selection)
718 mark_res = eval_marks (edit, &start_mark, &end_mark);
719 if (mark_res != 0)
721 edit->search->error = MC_SEARCH_E_NOTFOUND;
722 edit->search->error_str = g_strdup (_("Search string not found"));
723 return FALSE;
725 if (edit_search_options.backwards)
727 if (search_start > end_mark || search_start <= start_mark)
729 search_start = end_mark;
732 else
734 if (search_start < start_mark || search_start >= end_mark)
736 search_start = start_mark;
740 else
742 if (edit_search_options.backwards)
743 end_mark = max (1, edit->curs1) - 1;
745 if (edit_search_options.backwards)
747 search_end = end_mark;
748 while ((int) search_start >= start_mark)
750 if (search_end > (off_t) (search_start + edit->search->original_len) &&
751 mc_search_is_fixed_search_str (edit->search))
753 search_end = search_start + edit->search->original_len;
755 if (mc_search_run (edit->search, (void *) edit, search_start, search_end, len)
756 && edit->search->normal_offset == search_start)
758 return TRUE;
760 search_start--;
762 edit->search->error_str = g_strdup (_("Search string not found"));
764 else
766 return mc_search_run (edit->search, (void *) edit, search_start, end_mark, len);
768 return FALSE;
771 /* --------------------------------------------------------------------------------------------- */
773 static char *
774 edit_replace_cmd__conv_to_display (char *str)
776 #ifdef HAVE_CHARSET
777 GString *tmp;
779 tmp = str_convert_to_display (str);
780 if (tmp != NULL)
782 if (tmp->len != 0)
783 return g_string_free (tmp, FALSE);
784 g_string_free (tmp, TRUE);
786 #endif
787 return g_strdup (str);
790 /* --------------------------------------------------------------------------------------------- */
792 static char *
793 edit_replace_cmd__conv_to_input (char *str)
795 #ifdef HAVE_CHARSET
796 GString *tmp;
798 tmp = str_convert_to_input (str);
799 if (tmp != NULL)
801 if (tmp->len != 0)
802 return g_string_free (tmp, FALSE);
803 g_string_free (tmp, TRUE);
805 #endif
806 return g_strdup (str);
809 /* --------------------------------------------------------------------------------------------- */
811 static void
812 edit_do_search (WEdit * edit)
814 gsize len = 0;
816 if (edit->search == NULL)
817 edit->search_start = edit->curs1;
819 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
821 if (search_create_bookmark)
823 int found = 0, books = 0;
824 long l = 0, l_last = -1;
825 long q = 0;
827 search_create_bookmark = FALSE;
828 book_mark_flush (edit, -1);
830 while (TRUE)
832 if (!mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len))
833 break;
834 if (found == 0)
835 edit->search_start = edit->search->normal_offset;
836 found++;
837 l += edit_count_lines (edit, q, edit->search->normal_offset);
838 if (l != l_last)
840 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
841 books++;
843 l_last = l;
844 q = edit->search->normal_offset + 1;
847 if (found == 0)
848 edit_error_dialog (_("Search"), _("Search string not found"));
849 else
850 edit_cursor_move (edit, edit->search_start - edit->curs1);
852 else
854 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
855 && edit_search_options.backwards)
856 edit->search_start--;
858 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
859 && !edit_search_options.backwards)
860 edit->search_start++;
862 if (editcmd_find (edit, &len))
864 edit->found_start = edit->search_start = edit->search->normal_offset;
865 edit->found_len = len;
866 edit->over_col = 0;
867 edit_cursor_move (edit, edit->search_start - edit->curs1);
868 edit_scroll_screen_over_cursor (edit);
869 if (edit_search_options.backwards)
870 edit->search_start--;
871 else
872 edit->search_start++;
874 else
876 edit->search_start = edit->curs1;
877 if (edit->search->error_str != NULL)
878 edit_error_dialog (_("Search"), edit->search->error_str);
882 edit->force |= REDRAW_COMPLETELY;
883 edit_scroll_screen_over_cursor (edit);
886 /* --------------------------------------------------------------------------------------------- */
888 static void
889 edit_search (WEdit * edit)
891 if (editcmd_dialog_search_show (edit))
892 edit_do_search (edit);
895 /* --------------------------------------------------------------------------------------------- */
896 /** Return a null terminated length of text. Result must be g_free'd */
898 static unsigned char *
899 edit_get_block (WEdit * edit, long start, long finish, int *l)
901 unsigned char *s, *r;
902 r = s = g_malloc0 (finish - start + 1);
903 if (edit->column_highlight)
905 *l = 0;
906 /* copy from buffer, excluding chars that are out of the column 'margins' */
907 while (start < finish)
909 int c;
910 long x;
911 x = edit_move_forward3 (edit, edit_bol (edit, start), 0, start);
912 c = edit_get_byte (edit, start);
913 if ((x >= edit->column1 && x < edit->column2)
914 || (x >= edit->column2 && x < edit->column1) || c == '\n')
916 *s++ = c;
917 (*l)++;
919 start++;
922 else
924 *l = finish - start;
925 while (start < finish)
926 *s++ = edit_get_byte (edit, start++);
928 *s = 0;
929 return r;
932 /* --------------------------------------------------------------------------------------------- */
933 /** copies a block to clipboard file */
935 static int
936 edit_save_block_to_clip_file (WEdit * edit, long start, long finish)
938 int ret;
939 gchar *tmp;
940 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
941 ret = edit_save_block (edit, tmp, start, finish);
942 g_free (tmp);
943 return ret;
946 /* --------------------------------------------------------------------------------------------- */
948 static void
949 pipe_mail (WEdit * edit, char *to, char *subject, char *cc)
951 FILE *p = 0;
952 char *s;
954 to = name_quote (to, 0);
955 subject = name_quote (subject, 0);
956 cc = name_quote (cc, 0);
957 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
958 g_free (to);
959 g_free (subject);
960 g_free (cc);
962 if (s)
964 p = popen (s, "w");
965 g_free (s);
968 if (p)
970 long i;
971 for (i = 0; i < edit->last_byte; i++)
972 fputc (edit_get_byte (edit, i), p);
973 pclose (p);
977 /* --------------------------------------------------------------------------------------------- */
979 static gboolean
980 is_break_char (char c)
982 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
985 /* --------------------------------------------------------------------------------------------- */
986 /** find first character of current word */
988 static int
989 edit_find_word_start (WEdit * edit, long *word_start, gsize * word_len)
991 int c, last;
992 gsize i;
994 /* return if at begin of file */
995 if (edit->curs1 <= 0)
996 return 0;
998 c = (unsigned char) edit_get_byte (edit, edit->curs1 - 1);
999 /* return if not at end or in word */
1000 if (is_break_char (c))
1001 return 0;
1003 /* search start of word to be completed */
1004 for (i = 2;; i++)
1006 /* return if at begin of file */
1007 if ((gsize) edit->curs1 < i)
1008 return 0;
1010 last = c;
1011 c = (unsigned char) edit_get_byte (edit, edit->curs1 - i);
1013 if (is_break_char (c))
1015 /* return if word starts with digit */
1016 if (isdigit (last))
1017 return 0;
1019 *word_start = edit->curs1 - (i - 1); /* start found */
1020 *word_len = i - 1;
1021 break;
1024 /* success */
1025 return 1;
1028 /* --------------------------------------------------------------------------------------------- */
1030 * Get current word under cursor
1032 * @param edit editor object
1033 * @param srch mc_search object
1034 * @param word_start start word position
1036 * @return newly allocated string or NULL if no any words under cursor
1039 static char *
1040 edit_collect_completions_get_current_word (WEdit * edit, mc_search_t * srch, long word_start)
1042 gsize len = 0, i;
1043 GString *temp;
1045 if (!mc_search_run (srch, (void *) edit, word_start, edit->last_byte, &len))
1046 return NULL;
1048 temp = g_string_sized_new (len);
1050 for (i = 0; i < len; i++)
1052 int chr;
1054 chr = edit_get_byte (edit, word_start + i);
1055 if (!isspace (chr))
1056 g_string_append_c (temp, chr);
1059 return g_string_free (temp, temp->len == 0);
1062 /* --------------------------------------------------------------------------------------------- */
1063 /** collect the possible completions */
1065 static gsize
1066 edit_collect_completions (WEdit * edit, long word_start, gsize word_len,
1067 char *match_expr, struct selection *compl, gsize * num)
1069 gsize len = 0;
1070 gsize max_len = 0;
1071 gsize i;
1072 int skip;
1073 GString *temp;
1074 mc_search_t *srch;
1075 long last_byte, start = -1;
1076 char *current_word;
1078 srch = mc_search_new (match_expr, -1);
1079 if (srch == NULL)
1080 return 0;
1082 if (mc_config_get_bool
1083 (mc_main_config, CONFIG_APP_SECTION, "editor_wordcompletion_collect_entire_file", 0))
1085 last_byte = edit->last_byte;
1087 else
1089 last_byte = word_start;
1092 srch->search_type = MC_SEARCH_T_REGEX;
1093 srch->is_case_sensitive = TRUE;
1094 srch->search_fn = edit_search_cmd_callback;
1096 current_word = edit_collect_completions_get_current_word (edit, srch, word_start);
1098 temp = g_string_new ("");
1100 /* collect max MAX_WORD_COMPLETIONS completions */
1101 while (mc_search_run (srch, (void *) edit, start + 1, last_byte, &len))
1103 g_string_set_size (temp, 0);
1104 start = srch->normal_offset;
1106 /* add matched completion if not yet added */
1107 for (i = 0; i < len; i++)
1109 skip = edit_get_byte (edit, start + i);
1110 if (isspace (skip))
1111 continue;
1113 /* skip current word */
1114 if (start + (long) i == word_start)
1115 break;
1117 g_string_append_c (temp, skip);
1120 if (temp->len == 0)
1121 continue;
1123 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1124 continue;
1126 skip = 0;
1128 for (i = 0; i < *num; i++)
1130 if (strncmp
1131 ((char *) &compl[i].text[word_len],
1132 (char *) &temp->str[word_len], max (len, compl[i].len) - word_len) == 0)
1134 struct selection this = compl[i];
1135 for (++i; i < *num; i++)
1137 compl[i - 1] = compl[i];
1139 compl[*num - 1] = this;
1140 skip = 1;
1141 break; /* skip it, already added */
1144 if (skip != 0)
1145 continue;
1147 if (*num == MAX_WORD_COMPLETIONS)
1149 g_free (compl[0].text);
1150 for (i = 1; i < *num; i++)
1152 compl[i - 1] = compl[i];
1154 (*num)--;
1156 #ifdef HAVE_CHARSET
1158 GString *recoded;
1159 recoded = str_convert_to_display (temp->str);
1161 if (recoded && recoded->len)
1162 g_string_assign (temp, recoded->str);
1164 g_string_free (recoded, TRUE);
1166 #endif
1167 compl[*num].text = g_strdup (temp->str);
1168 compl[*num].len = temp->len;
1169 (*num)++;
1170 start += len;
1172 /* note the maximal length needed for the completion dialog */
1173 if (len > max_len)
1174 max_len = len;
1177 mc_search_free (srch);
1178 g_string_free (temp, TRUE);
1179 g_free (current_word);
1181 return max_len;
1184 /* --------------------------------------------------------------------------------------------- */
1186 static void
1187 edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width,
1188 long *start_pos, long *end_pos, int *col1, int *col2)
1190 long cursor;
1191 int i, col;
1193 cursor = edit->curs1;
1194 col = edit_get_col (edit);
1196 for (i = 0; i < size; i++)
1198 if (data[i] != '\n')
1199 edit_insert (edit, data[i]);
1200 else
1201 { /* fill in and move to next line */
1202 int l;
1203 long p;
1205 if (edit_get_byte (edit, edit->curs1) != '\n')
1207 l = width - (edit_get_col (edit) - col);
1208 while (l > 0)
1210 edit_insert (edit, ' ');
1211 l -= space_width;
1214 for (p = edit->curs1;; p++)
1216 if (p == edit->last_byte)
1218 edit_cursor_move (edit, edit->last_byte - edit->curs1);
1219 edit_insert_ahead (edit, '\n');
1220 p++;
1221 break;
1223 if (edit_get_byte (edit, p) == '\n')
1225 p++;
1226 break;
1229 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
1230 l = col - edit_get_col (edit);
1231 while (l >= space_width)
1233 edit_insert (edit, ' ');
1234 l -= space_width;
1239 *col1 = col;
1240 *col2 = col + width;
1241 *start_pos = cursor;
1242 *end_pos = edit->curs1;
1243 edit_cursor_move (edit, cursor - edit->curs1);
1246 /* --------------------------------------------------------------------------------------------- */
1248 static int
1249 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1251 const macros_t *m1 = (const macros_t *) macro1;
1252 const macros_t *m2 = (const macros_t *) macro2;
1254 return m1->hotkey - m2->hotkey;
1257 /* --------------------------------------------------------------------------------------------- */
1259 static void
1260 edit_macro_sort_by_hotkey (void)
1262 if (macros_list != NULL && macros_list->len != 0)
1263 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1266 /* --------------------------------------------------------------------------------------------- */
1268 static gboolean
1269 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1271 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1272 macros_t *result;
1273 macros_t search_macro;
1275 (void) edit;
1277 search_macro.hotkey = hotkey;
1278 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1279 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1281 if (result != NULL && result->macro != NULL)
1283 *indx = (result - array_start);
1284 *macros = result;
1285 return TRUE;
1287 *indx = 0;
1288 return FALSE;
1291 /* --------------------------------------------------------------------------------------------- */
1292 /** returns FALSE on error */
1294 static gboolean
1295 edit_delete_macro (WEdit * edit, int hotkey)
1297 mc_config_t *macros_config = NULL;
1298 const char *section_name = "editor";
1299 gchar *macros_fname;
1300 guint indx;
1301 char *skeyname;
1302 const macros_t *macros = NULL;
1304 /* clear array of actions for current hotkey */
1305 while (edit_get_macro (edit, hotkey, &macros, &indx))
1307 if (macros->macro != NULL)
1308 g_array_free (macros->macro, TRUE);
1309 macros = NULL;
1310 g_array_remove_index (macros_list, indx);
1311 edit_macro_sort_by_hotkey ();
1314 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1315 macros_config = mc_config_init (macros_fname);
1316 g_free (macros_fname);
1318 if (macros_config == NULL)
1319 return FALSE;
1321 skeyname = lookup_key_by_code (hotkey);
1322 while (mc_config_del_key (macros_config, section_name, skeyname))
1324 g_free (skeyname);
1325 mc_config_save_file (macros_config, NULL);
1326 mc_config_deinit (macros_config);
1327 return TRUE;
1331 /* --------------------------------------------------------------------------------------------- */
1332 /*** public functions ****************************************************************************/
1333 /* --------------------------------------------------------------------------------------------- */
1335 void
1336 edit_help_cmd (WEdit * edit)
1338 ev_help_t event_data = { NULL, "[Internal File Editor]" };
1339 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
1341 edit->force |= REDRAW_COMPLETELY;
1344 /* --------------------------------------------------------------------------------------------- */
1346 void
1347 edit_refresh_cmd (WEdit * edit)
1349 #ifdef HAVE_SLANG
1350 int color;
1352 edit_get_syntax_color (edit, -1, &color);
1353 tty_touch_screen ();
1354 mc_refresh ();
1355 #else
1356 (void) edit;
1358 clr_scr ();
1359 repaint_screen ();
1360 #endif /* !HAVE_SLANG */
1361 tty_keypad (TRUE);
1364 /* --------------------------------------------------------------------------------------------- */
1366 void
1367 menu_save_mode_cmd (void)
1369 /* diaog sizes */
1370 const int DLG_X = 38;
1371 const int DLG_Y = 13;
1373 char *str_result;
1375 const char *str[] = {
1376 N_("&Quick save"),
1377 N_("&Safe save"),
1378 N_("&Do backups with following extension:")
1381 QuickWidget widgets[] = {
1382 /* 0 */
1383 QUICK_BUTTON (18, DLG_X, DLG_Y - 3, DLG_Y, N_("&Cancel"), B_CANCEL, NULL),
1384 /* 1 */
1385 QUICK_BUTTON (6, DLG_X, DLG_Y - 3, DLG_Y, N_("&OK"), B_ENTER, NULL),
1386 /* 2 */
1387 QUICK_CHECKBOX (4, DLG_X, 8, DLG_Y, N_("Check &POSIX new line"), &option_check_nl_at_eof),
1388 /* 3 */
1389 QUICK_INPUT (8, DLG_X, 6, DLG_Y, option_backup_ext, 9, 0, "edit-backup-ext", &str_result),
1390 /* 4 */
1391 QUICK_RADIO (4, DLG_X, 3, DLG_Y, 3, str, &option_save_mode),
1392 QUICK_END
1395 QuickDialog dialog = {
1396 DLG_X, DLG_Y, -1, -1, N_("Edit Save Mode"),
1397 "[Edit Save Mode]", widgets, NULL, FALSE
1400 size_t i;
1401 size_t maxlen = 0;
1402 size_t w0, w1, b_len, w3;
1404 assert (option_backup_ext != NULL);
1406 /* OK/Cancel buttons */
1407 w0 = str_term_width1 (_(widgets[0].u.button.text)) + 3;
1408 w1 = str_term_width1 (_(widgets[1].u.button.text)) + 5; /* default button */
1409 b_len = w0 + w1 + 3;
1411 maxlen = max (b_len, (size_t) str_term_width1 (_(dialog.title)) + 2);
1413 w3 = 0;
1414 for (i = 0; i < 3; i++)
1416 #ifdef ENABLE_NLS
1417 str[i] = _(str[i]);
1418 #endif
1419 w3 = max (w3, (size_t) str_term_width1 (str[i]));
1422 maxlen = max (maxlen, w3 + 4);
1424 dialog.xlen = min ((size_t) COLS, maxlen + 8);
1426 widgets[3].u.input.len = w3;
1427 widgets[1].relative_x = (dialog.xlen - b_len) / 2;
1428 widgets[0].relative_x = widgets[1].relative_x + w0 + 2;
1430 for (i = 0; i < sizeof (widgets) / sizeof (widgets[0]); i++)
1431 widgets[i].x_divisions = dialog.xlen;
1433 if (quick_dialog (&dialog) != B_CANCEL)
1435 g_free (option_backup_ext);
1436 option_backup_ext = str_result;
1440 /* --------------------------------------------------------------------------------------------- */
1442 void
1443 edit_set_filename (WEdit * edit, const char *name)
1445 g_free (edit->filename);
1447 if (name == NULL)
1448 name = "";
1450 edit->filename = tilde_expand (name);
1451 if (edit->dir == NULL && !g_path_is_absolute (name))
1452 edit->dir = vfs_get_current_dir ();
1455 /* --------------------------------------------------------------------------------------------- */
1456 /* Here we want to warn the users of overwriting an existing file,
1457 but only if they have made a change to the filename */
1458 /* returns 1 on success */
1460 edit_save_as_cmd (WEdit * edit)
1462 /* This heads the 'Save As' dialog box */
1463 char *exp;
1464 int save_lock = 0;
1465 int different_filename = 0;
1467 if (!edit_check_newline (edit))
1468 return 0;
1470 exp = edit_get_save_file_as (edit);
1471 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1473 if (exp)
1475 if (!*exp)
1477 g_free (exp);
1478 edit->force |= REDRAW_COMPLETELY;
1479 return 0;
1481 else
1483 int rv;
1484 if (strcmp (edit->filename, exp))
1486 int file;
1487 different_filename = 1;
1488 file = mc_open (exp, O_RDONLY | O_BINARY);
1489 if (file != -1)
1491 /* the file exists */
1492 mc_close (file);
1493 /* Overwrite the current file or cancel the operation */
1494 if (edit_query_dialog2
1495 (_("Warning"),
1496 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1498 edit->force |= REDRAW_COMPLETELY;
1499 g_free (exp);
1500 return 0;
1503 else
1505 edit->stat1.st_mode |= S_IWUSR;
1507 save_lock = lock_file (exp);
1509 else
1511 /* filenames equal, check if already locked */
1512 if (!edit->locked && !edit->delete_file)
1513 save_lock = lock_file (exp);
1516 if (different_filename)
1519 * Allow user to write into saved (under another name) file
1520 * even if original file had r/o user permissions.
1522 edit->stat1.st_mode |= S_IWRITE;
1525 rv = edit_save_file (edit, exp);
1526 switch (rv)
1528 case 1:
1529 /* Succesful, so unlock both files */
1530 if (different_filename)
1532 if (save_lock)
1533 unlock_file (exp);
1534 if (edit->locked)
1535 edit->locked = edit_unlock_file (edit);
1537 else
1539 if (edit->locked || save_lock)
1540 edit->locked = edit_unlock_file (edit);
1543 edit_set_filename (edit, exp);
1544 if (edit->lb != LB_ASIS)
1545 edit_reload (edit, exp);
1546 g_free (exp);
1547 edit->modified = 0;
1548 edit->delete_file = 0;
1549 if (different_filename)
1550 edit_load_syntax (edit, NULL, edit->syntax_type);
1551 edit->force |= REDRAW_COMPLETELY;
1552 return 1;
1553 default:
1554 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1555 /* fallthrough */
1556 case -1:
1557 /* Failed, so maintain modify (not save) lock */
1558 if (save_lock)
1559 unlock_file (exp);
1560 g_free (exp);
1561 edit->force |= REDRAW_COMPLETELY;
1562 return 0;
1566 edit->force |= REDRAW_COMPLETELY;
1567 return 0;
1570 /* {{{ Macro stuff starts here */
1571 /* --------------------------------------------------------------------------------------------- */
1573 void
1574 edit_delete_macro_cmd (WEdit * edit)
1576 int hotkey;
1578 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1580 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1581 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1584 /* --------------------------------------------------------------------------------------------- */
1586 /** returns FALSE on error */
1587 gboolean
1588 edit_execute_macro (WEdit * edit, int hotkey)
1590 gboolean res = FALSE;
1592 if (hotkey != 0)
1594 const macros_t *macros;
1595 guint indx;
1597 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1598 macros->macro != NULL && macros->macro->len != 0)
1600 guint i;
1602 edit->force |= REDRAW_PAGE;
1604 for (i = 0; i < macros->macro->len; i++)
1606 const macro_action_t *m_act;
1608 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1609 edit_execute_cmd (edit, m_act->action, m_act->ch);
1610 res = TRUE;
1614 edit_update_screen (edit);
1615 return res;
1618 /* --------------------------------------------------------------------------------------------- */
1620 /** returns FALSE on error */
1621 gboolean
1622 edit_store_macro_cmd (WEdit * edit)
1624 int i;
1625 int hotkey;
1626 GString *marcros_string;
1627 mc_config_t *macros_config = NULL;
1628 const char *section_name = "editor";
1629 gchar *macros_fname;
1630 GArray *macros; /* current macro */
1631 int tmp_act;
1632 gboolean have_macro = FALSE;
1633 char *skeyname = NULL;
1635 hotkey = editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1636 if (hotkey == ESC_CHAR)
1637 return FALSE;
1639 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1641 /* return FALSE if try assign macro into restricted hotkeys */
1642 if (tmp_act == CK_MacroStartRecord
1643 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1644 return FALSE;
1646 edit_delete_macro (edit, hotkey);
1648 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1649 macros_config = mc_config_init (macros_fname);
1650 g_free (macros_fname);
1652 if (macros_config == NULL)
1653 return FALSE;
1655 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1657 marcros_string = g_string_sized_new (250);
1658 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1660 skeyname = lookup_key_by_code (hotkey);
1662 for (i = 0; i < macro_index; i++)
1664 macro_action_t m_act;
1665 const char *action_name;
1667 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1669 if (action_name == NULL)
1670 break;
1672 m_act.action = record_macro_buf[i].action;
1673 m_act.ch = record_macro_buf[i].ch;
1674 g_array_append_val (macros, m_act);
1675 have_macro = TRUE;
1676 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1677 (int) record_macro_buf[i].ch);
1679 if (have_macro)
1681 macros_t macro;
1682 macro.hotkey = hotkey;
1683 macro.macro = macros;
1684 g_array_append_val (macros_list, macro);
1685 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1687 else
1688 mc_config_del_key (macros_config, section_name, skeyname);
1690 g_free (skeyname);
1691 edit_macro_sort_by_hotkey ();
1693 g_string_free (marcros_string, TRUE);
1694 mc_config_save_file (macros_config, NULL);
1695 mc_config_deinit (macros_config);
1696 return TRUE;
1699 /* --------------------------------------------------------------------------------------------- */
1701 gboolean
1702 edit_repeat_macro_cmd (WEdit * edit)
1704 int i, j;
1705 char *f;
1706 long count_repeat;
1707 char *error = NULL;
1709 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL);
1710 if (f == NULL || *f == '\0')
1712 g_free (f);
1713 return FALSE;
1716 count_repeat = strtol (f, &error, 0);
1718 if (*error != '\0')
1720 g_free (f);
1721 return FALSE;
1724 g_free (f);
1726 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1727 edit->force |= REDRAW_PAGE;
1729 for (j = 0; j < count_repeat; j++)
1730 for (i = 0; i < macro_index; i++)
1731 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1732 edit_update_screen (edit);
1733 return TRUE;
1736 /* --------------------------------------------------------------------------------------------- */
1737 /** return FALSE on error */
1739 gboolean
1740 edit_load_macro_cmd (WEdit * edit)
1742 mc_config_t *macros_config = NULL;
1743 gchar **profile_keys, **keys;
1744 gchar **values, **curr_values;
1745 gsize len, values_len;
1746 const char *section_name = "editor";
1747 gchar *macros_fname;
1748 int hotkey;
1750 (void) edit;
1752 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1753 macros_config = mc_config_init (macros_fname);
1754 g_free (macros_fname);
1756 if (macros_config == NULL)
1757 return FALSE;
1759 profile_keys = keys = mc_config_get_keys (macros_config, section_name, &len);
1760 while (*profile_keys != NULL)
1762 gboolean have_macro;
1763 GArray *macros;
1764 macros_t macro;
1766 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1768 curr_values = values = mc_config_get_string_list (macros_config, section_name,
1769 *profile_keys, &values_len);
1770 hotkey = lookup_key (*profile_keys, NULL);
1771 have_macro = FALSE;
1773 while (*curr_values != NULL && *curr_values[0] != '\0')
1775 char **macro_pair = NULL;
1777 macro_pair = g_strsplit (*curr_values, ":", 2);
1779 if (macro_pair != NULL)
1781 macro_action_t m_act;
1782 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
1783 m_act.action = 0;
1784 else
1786 m_act.action = keybind_lookup_action (macro_pair[0]);
1787 g_free (macro_pair[0]);
1788 macro_pair[0] = NULL;
1790 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
1791 m_act.ch = -1;
1792 else
1794 m_act.ch = strtol (macro_pair[1], NULL, 0);
1795 g_free (macro_pair[1]);
1796 macro_pair[1] = NULL;
1798 if (m_act.action != 0)
1800 /* a shell command */
1801 if ((m_act.action / CK_PipeBlock (0)) == 1)
1803 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
1804 m_act.ch = -1;
1806 g_array_append_val (macros, m_act);
1807 have_macro = TRUE;
1809 g_strfreev (macro_pair);
1810 macro_pair = NULL;
1812 curr_values++;
1814 if (have_macro)
1816 macro.hotkey = hotkey;
1817 macro.macro = macros;
1818 g_array_append_val (macros_list, macro);
1820 profile_keys++;
1821 g_strfreev (values);
1823 g_strfreev (keys);
1824 mc_config_deinit (macros_config);
1825 edit_macro_sort_by_hotkey ();
1826 return TRUE;
1829 /* }}} Macro stuff end here */
1831 /* --------------------------------------------------------------------------------------------- */
1832 /** returns 1 on success */
1835 edit_save_confirm_cmd (WEdit * edit)
1837 gchar *f = NULL;
1839 if (!edit_check_newline (edit))
1840 return 0;
1842 if (edit_confirm_save)
1844 f = g_strdup_printf (_("Confirm save file: \"%s\""), edit->filename);
1845 if (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")))
1847 g_free (f);
1848 return 0;
1850 g_free (f);
1852 return edit_save_cmd (edit);
1855 /* --------------------------------------------------------------------------------------------- */
1856 /** returns 1 on success */
1859 edit_new_cmd (WEdit * edit)
1861 if (edit->modified)
1863 if (edit_query_dialog2
1864 (_("Warning"),
1866 ("Current text was modified without a file save.\nContinue discards these changes"),
1867 _("C&ontinue"), _("&Cancel")))
1869 edit->force |= REDRAW_COMPLETELY;
1870 return 0;
1873 edit->force |= REDRAW_COMPLETELY;
1875 return edit_renew (edit); /* if this gives an error, something has really screwed up */
1878 /* --------------------------------------------------------------------------------------------- */
1881 edit_load_cmd (WEdit * edit, edit_current_file_t what)
1883 char *exp;
1885 if (edit->modified
1886 && (edit_query_dialog2
1887 (_("Warning"),
1888 _("Current text was modified without a file save.\n"
1889 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")) == 1))
1891 edit->force |= REDRAW_COMPLETELY;
1892 return 0;
1895 switch (what)
1897 case EDIT_FILE_COMMON:
1898 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
1899 MC_HISTORY_EDIT_LOAD, edit->filename);
1901 if (exp)
1903 if (*exp)
1904 edit_load_file_from_filename (edit, exp);
1905 g_free (exp);
1907 break;
1909 case EDIT_FILE_SYNTAX:
1910 edit_load_syntax_file (edit);
1911 break;
1913 case EDIT_FILE_MENU:
1914 edit_load_menu_file (edit);
1915 break;
1917 default:
1918 break;
1921 edit->force |= REDRAW_COMPLETELY;
1922 return 0;
1925 /* --------------------------------------------------------------------------------------------- */
1927 if mark2 is -1 then marking is from mark1 to the cursor.
1928 Otherwise its between the markers. This handles this.
1929 Returns 1 if no text is marked.
1933 eval_marks (WEdit * edit, long *start_mark, long *end_mark)
1935 if (edit->mark1 != edit->mark2)
1937 long start_bol, start_eol;
1938 long end_bol, end_eol;
1939 long col1, col2;
1940 long diff1, diff2;
1941 long end_mark_curs;
1943 if (edit->end_mark_curs < 0)
1944 end_mark_curs = edit->curs1;
1945 else
1946 end_mark_curs = edit->end_mark_curs;
1948 if (edit->mark2 >= 0)
1950 *start_mark = min (edit->mark1, edit->mark2);
1951 *end_mark = max (edit->mark1, edit->mark2);
1953 else
1955 *start_mark = min (edit->mark1, end_mark_curs);
1956 *end_mark = max (edit->mark1, end_mark_curs);
1957 edit->column2 = edit->curs_col + edit->over_col;
1960 if (edit->column_highlight
1961 && (((edit->mark1 > end_mark_curs) && (edit->column1 < edit->column2))
1962 || ((edit->mark1 < end_mark_curs) && (edit->column1 > edit->column2))))
1964 start_bol = edit_bol (edit, *start_mark);
1965 start_eol = edit_eol (edit, start_bol - 1) + 1;
1966 end_bol = edit_bol (edit, *end_mark);
1967 end_eol = edit_eol (edit, *end_mark);
1968 col1 = min (edit->column1, edit->column2);
1969 col2 = max (edit->column1, edit->column2);
1971 diff1 =
1972 edit_move_forward3 (edit, start_bol, col2, 0) - edit_move_forward3 (edit, start_bol,
1973 col1, 0);
1974 diff2 =
1975 edit_move_forward3 (edit, end_bol, col2, 0) - edit_move_forward3 (edit, end_bol,
1976 col1, 0);
1978 *start_mark -= diff1;
1979 *end_mark += diff2;
1980 *start_mark = max (*start_mark, start_eol);
1981 *end_mark = min (*end_mark, end_eol);
1983 return 0;
1985 else
1987 *start_mark = *end_mark = 0;
1988 edit->column2 = edit->column1 = 0;
1989 return 1;
1993 /* --------------------------------------------------------------------------------------------- */
1995 void
1996 edit_insert_over (WEdit * edit)
1998 int i;
2000 for (i = 0; i < edit->over_col; i++)
2002 edit_insert (edit, ' ');
2004 edit->over_col = 0;
2007 /* --------------------------------------------------------------------------------------------- */
2010 edit_insert_column_of_text_from_file (WEdit * edit, int file,
2011 long *start_pos, long *end_pos, int *col1, int *col2)
2013 long cursor;
2014 int col;
2015 int blocklen = -1, width = 0;
2016 unsigned char *data;
2018 cursor = edit->curs1;
2019 col = edit_get_col (edit);
2020 data = g_malloc0 (TEMP_BUF_LEN);
2022 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
2024 int i;
2025 for (width = 0; width < blocklen; width++)
2027 if (data[width] == '\n')
2028 break;
2030 for (i = 0; i < blocklen; i++)
2032 if (data[i] == '\n')
2033 { /* fill in and move to next line */
2034 int l;
2035 long p;
2036 if (edit_get_byte (edit, edit->curs1) != '\n')
2038 l = width - (edit_get_col (edit) - col);
2039 while (l > 0)
2041 edit_insert (edit, ' ');
2042 l -= space_width;
2045 for (p = edit->curs1;; p++)
2047 if (p == edit->last_byte)
2049 edit_cursor_move (edit, edit->last_byte - edit->curs1);
2050 edit_insert_ahead (edit, '\n');
2051 p++;
2052 break;
2054 if (edit_get_byte (edit, p) == '\n')
2056 p++;
2057 break;
2060 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
2061 l = col - edit_get_col (edit);
2062 while (l >= space_width)
2064 edit_insert (edit, ' ');
2065 l -= space_width;
2067 continue;
2069 edit_insert (edit, data[i]);
2072 *col1 = col;
2073 *col2 = col + width;
2074 *start_pos = cursor;
2075 *end_pos = edit->curs1;
2076 edit_cursor_move (edit, cursor - edit->curs1);
2077 g_free (data);
2079 return blocklen;
2082 /* --------------------------------------------------------------------------------------------- */
2084 void
2085 edit_block_copy_cmd (WEdit * edit)
2087 long start_mark, end_mark, current = edit->curs1;
2088 long col_delta = 0;
2089 long mark1, mark2;
2090 int c1, c2;
2091 int size;
2092 unsigned char *copy_buf;
2094 edit_update_curs_col (edit);
2095 if (eval_marks (edit, &start_mark, &end_mark))
2096 return;
2098 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2100 /* all that gets pushed are deletes hence little space is used on the stack */
2102 edit_push_markers (edit);
2104 if (edit->column_highlight)
2106 col_delta = abs (edit->column2 - edit->column1);
2107 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2109 else
2111 while (size--)
2112 edit_insert_ahead (edit, copy_buf[size]);
2115 g_free (copy_buf);
2116 edit_scroll_screen_over_cursor (edit);
2118 if (edit->column_highlight)
2119 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2120 else if (start_mark < current && end_mark > current)
2121 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2123 edit->force |= REDRAW_PAGE;
2127 /* --------------------------------------------------------------------------------------------- */
2129 void
2130 edit_block_move_cmd (WEdit * edit)
2132 long current;
2133 unsigned char *copy_buf = NULL;
2134 long start_mark, end_mark;
2135 long line;
2137 if (eval_marks (edit, &start_mark, &end_mark))
2138 return;
2140 line = edit->curs_line;
2141 if (edit->mark2 < 0)
2142 edit_mark_cmd (edit, 0);
2143 edit_push_markers (edit);
2145 if (edit->column_highlight)
2147 long mark1, mark2;
2148 int size;
2149 int b_width = 0;
2150 int c1, c2;
2151 int x, x2;
2153 c1 = min (edit->column1, edit->column2);
2154 c2 = max (edit->column1, edit->column2);
2155 b_width = (c2 - c1);
2157 edit_update_curs_col (edit);
2159 x = edit->curs_col;
2160 x2 = x + edit->over_col;
2162 /* do nothing when cursor inside first line of selected area */
2163 if ((edit_eol (edit, edit->curs1) == edit_eol (edit, start_mark)) && (x2 > c1 && x2 <= c2))
2164 return;
2166 if (edit->curs1 > start_mark && edit->curs1 < edit_eol (edit, end_mark))
2168 if (x > c2)
2169 x -= b_width;
2170 else if (x > c1 && x <= c2)
2171 x = c1;
2173 /* save current selection into buffer */
2174 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2176 /* remove current selection */
2177 edit_block_delete_cmd (edit);
2179 edit->over_col = max (0, edit->over_col - b_width);
2180 /* calculate the cursor pos after delete block */
2181 current = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), x, 0);
2182 edit_cursor_move (edit, current - edit->curs1);
2183 edit_scroll_screen_over_cursor (edit);
2185 /* add TWS if need before block insertion */
2186 if (option_cursor_beyond_eol && edit->over_col > 0)
2187 edit_insert_over (edit);
2189 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2190 edit_set_markers (edit, mark1, mark2, c1, c2);
2192 else
2194 long count;
2196 current = edit->curs1;
2197 copy_buf = g_malloc0 (end_mark - start_mark);
2198 edit_cursor_move (edit, start_mark - edit->curs1);
2199 edit_scroll_screen_over_cursor (edit);
2200 count = start_mark;
2201 while (count < end_mark)
2203 copy_buf[end_mark - count - 1] = edit_delete (edit, 1);
2204 count++;
2206 edit_scroll_screen_over_cursor (edit);
2207 edit_cursor_move (edit,
2208 current - edit->curs1 -
2209 (((current - edit->curs1) > 0) ? end_mark - start_mark : 0));
2210 edit_scroll_screen_over_cursor (edit);
2211 while (count-- > start_mark)
2212 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2213 edit_set_markers (edit, edit->curs1, edit->curs1 + end_mark - start_mark, 0, 0);
2216 edit_scroll_screen_over_cursor (edit);
2217 g_free (copy_buf);
2218 edit->force |= REDRAW_PAGE;
2221 /* --------------------------------------------------------------------------------------------- */
2222 /** returns 1 if canceelled by user */
2225 edit_block_delete_cmd (WEdit * edit)
2227 long start_mark, end_mark;
2228 if (eval_marks (edit, &start_mark, &end_mark))
2230 edit_delete_line (edit);
2231 return 0;
2233 return edit_block_delete (edit);
2236 /* --------------------------------------------------------------------------------------------- */
2237 /** call with edit = 0 before shutdown to close memory leaks */
2239 void
2240 edit_replace_cmd (WEdit * edit, int again)
2242 /* 1 = search string, 2 = replace with */
2243 static char *saved1 = NULL; /* saved default[123] */
2244 static char *saved2 = NULL;
2245 char *input1 = NULL; /* user input from the dialog */
2246 char *input2 = NULL;
2247 char *disp1 = NULL;
2248 char *disp2 = NULL;
2249 long times_replaced = 0;
2250 gboolean once_found = FALSE;
2252 if (!edit)
2254 g_free (saved1), saved1 = NULL;
2255 g_free (saved2), saved2 = NULL;
2256 return;
2259 edit->force |= REDRAW_COMPLETELY;
2261 if (again && !saved1 && !saved2)
2262 again = 0;
2264 if (again)
2266 input1 = g_strdup (saved1 ? saved1 : "");
2267 input2 = g_strdup (saved2 ? saved2 : "");
2269 else
2271 char *tmp_inp1, *tmp_inp2;
2272 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
2273 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
2275 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2277 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2279 g_free (disp1);
2280 g_free (disp2);
2282 if (input1 == NULL || *input1 == '\0')
2284 edit->force = REDRAW_COMPLETELY;
2285 goto cleanup;
2288 tmp_inp1 = input1;
2289 tmp_inp2 = input2;
2290 input1 = edit_replace_cmd__conv_to_input (input1);
2291 input2 = edit_replace_cmd__conv_to_input (input2);
2292 g_free (tmp_inp1);
2293 g_free (tmp_inp2);
2295 g_free (saved1), saved1 = g_strdup (input1);
2296 g_free (saved2), saved2 = g_strdup (input2);
2298 mc_search_free (edit->search);
2299 edit->search = NULL;
2302 if (!edit->search)
2304 edit->search = mc_search_new (input1, -1);
2305 if (edit->search == NULL)
2307 edit->search_start = edit->curs1;
2308 goto cleanup;
2310 edit->search->search_type = edit_search_options.type;
2311 edit->search->is_all_charsets = edit_search_options.all_codepages;
2312 edit->search->is_case_sensitive = edit_search_options.case_sens;
2313 edit->search->whole_words = edit_search_options.whole_words;
2314 edit->search->search_fn = edit_search_cmd_callback;
2317 if (edit->found_len && edit->search_start == edit->found_start + 1
2318 && edit_search_options.backwards)
2319 edit->search_start--;
2321 if (edit->found_len && edit->search_start == edit->found_start - 1
2322 && !edit_search_options.backwards)
2323 edit->search_start++;
2327 gsize len = 0;
2329 if (!editcmd_find (edit, &len))
2331 if (!(edit->search->error == MC_SEARCH_E_OK ||
2332 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2334 edit_error_dialog (_("Search"), edit->search->error_str);
2336 break;
2338 once_found = TRUE;
2340 edit->search_start = edit->search->normal_offset;
2341 /*returns negative on not found or error in pattern */
2343 if ((edit->search_start >= 0) && (edit->search_start < edit->last_byte))
2345 gsize i;
2346 GString *tmp_str, *repl_str;
2348 edit->found_start = edit->search_start;
2349 i = edit->found_len = len;
2351 edit_cursor_move (edit, edit->search_start - edit->curs1);
2352 edit_scroll_screen_over_cursor (edit);
2354 if (edit->replace_mode == 0)
2356 int l;
2357 int prompt;
2359 l = edit->curs_row - edit->widget.lines / 3;
2360 if (l > 0)
2361 edit_scroll_downward (edit, l);
2362 if (l < 0)
2363 edit_scroll_upward (edit, -l);
2365 edit_scroll_screen_over_cursor (edit);
2366 edit->force |= REDRAW_PAGE;
2367 edit_render_keypress (edit);
2369 /*so that undo stops at each query */
2370 edit_push_key_press (edit);
2371 /* and prompt 2/3 down */
2372 disp1 = edit_replace_cmd__conv_to_display (saved1);
2373 disp2 = edit_replace_cmd__conv_to_display (saved2);
2374 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2375 g_free (disp1);
2376 g_free (disp2);
2378 if (prompt == B_REPLACE_ALL)
2379 edit->replace_mode = 1;
2380 else if (prompt == B_SKIP_REPLACE)
2382 if (edit_search_options.backwards)
2383 edit->search_start--;
2384 else
2385 edit->search_start++;
2386 continue; /* loop */
2388 else if (prompt == B_CANCEL)
2390 edit->replace_mode = -1;
2391 break; /* loop */
2395 /* don't process string each time */
2396 tmp_str = g_string_new (input2);
2397 repl_str = mc_search_prepare_replace_str (edit->search, tmp_str);
2398 g_string_free (tmp_str, TRUE);
2400 if (edit->search->error != MC_SEARCH_E_OK)
2402 edit_error_dialog (_("Replace"), edit->search->error_str);
2403 g_string_free (repl_str, TRUE);
2404 break;
2407 /* delete then insert new */
2408 for (i = 0; i < len; i++)
2409 edit_delete (edit, 1);
2411 for (i = 0; i < repl_str->len; i++)
2412 edit_insert (edit, repl_str->str[i]);
2414 edit->found_len = repl_str->len;
2415 g_string_free (repl_str, TRUE);
2416 times_replaced++;
2418 /* so that we don't find the same string again */
2419 if (edit_search_options.backwards)
2420 edit->search_start--;
2421 else
2423 edit->search_start += edit->found_len;
2425 if (edit->search_start >= edit->last_byte)
2426 break;
2429 edit_scroll_screen_over_cursor (edit);
2431 else
2433 /* try and find from right here for next search */
2434 edit->search_start = edit->curs1;
2435 edit_update_curs_col (edit);
2437 edit->force |= REDRAW_PAGE;
2438 edit_render_keypress (edit);
2440 if (times_replaced == 0)
2441 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL, 1, _("&OK"));
2442 break;
2445 while (edit->replace_mode >= 0);
2447 edit_scroll_screen_over_cursor (edit);
2448 edit->force |= REDRAW_COMPLETELY;
2449 edit_render_keypress (edit);
2451 if ((edit->replace_mode == 1) && (times_replaced != 0))
2452 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2454 cleanup:
2455 g_free (input1);
2456 g_free (input2);
2459 /* --------------------------------------------------------------------------------------------- */
2462 edit_search_cmd_callback (const void *user_data, gsize char_offset)
2464 return edit_get_byte ((WEdit *) user_data, (long) char_offset);
2467 /* --------------------------------------------------------------------------------------------- */
2469 void
2470 edit_search_cmd (WEdit * edit, gboolean again)
2472 if (edit == NULL)
2473 return;
2475 if (!again)
2476 edit_search (edit);
2477 else if (edit->last_search_string != NULL)
2478 edit_do_search (edit);
2479 else
2481 /* find last search string in history */
2482 GList *history;
2484 history = history_get (MC_HISTORY_SHARED_SEARCH);
2485 if (history != NULL && history->data != NULL)
2487 edit->last_search_string = (char *) history->data;
2488 history->data = NULL;
2489 history = g_list_first (history);
2490 g_list_foreach (history, (GFunc) g_free, NULL);
2491 g_list_free (history);
2493 edit->search = mc_search_new (edit->last_search_string, -1);
2494 if (edit->search == NULL)
2496 /* if not... then ask for an expression */
2497 g_free (edit->last_search_string);
2498 edit->last_search_string = NULL;
2499 edit_search (edit);
2501 else
2503 edit->search->search_type = edit_search_options.type;
2504 edit->search->is_all_charsets = edit_search_options.all_codepages;
2505 edit->search->is_case_sensitive = edit_search_options.case_sens;
2506 edit->search->whole_words = edit_search_options.whole_words;
2507 edit->search->search_fn = edit_search_cmd_callback;
2508 edit_do_search (edit);
2511 else
2513 /* if not... then ask for an expression */
2514 g_free (edit->last_search_string);
2515 edit->last_search_string = NULL;
2516 edit_search (edit);
2522 /* --------------------------------------------------------------------------------------------- */
2524 * Check if it's OK to close the editor. If there are unsaved changes,
2525 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2528 gboolean
2529 edit_ok_to_exit (WEdit * edit)
2531 int act;
2533 if (!edit->modified)
2534 return TRUE;
2536 if (!mc_global.widget.midnight_shutdown)
2538 if (!edit_check_newline (edit))
2539 return FALSE;
2541 query_set_sel (2);
2542 act = edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2543 _("&Yes"), _("&No"), _("&Cancel quit"));
2545 else
2547 act =
2548 edit_query_dialog2 (_("Quit"),
2549 _("Midnight Commander is being shut down.\nSave modified file?"),
2550 _("&Yes"), _("&No"));
2552 /* Esc is No */
2553 if (act == -1)
2554 act = 1;
2557 switch (act)
2559 case 0: /* Yes */
2560 edit_push_markers (edit);
2561 edit_set_markers (edit, 0, 0, 0, 0);
2562 if (!edit_save_cmd (edit) || mc_global.widget.midnight_shutdown)
2563 return mc_global.widget.midnight_shutdown;
2564 break;
2565 case 1: /* No */
2566 break;
2567 case 2: /* Cancel quit */
2568 case -1: /* Esc */
2569 return FALSE;
2572 return TRUE;
2575 /* --------------------------------------------------------------------------------------------- */
2576 /** save block, returns 1 on success */
2579 edit_save_block (WEdit * edit, const char *filename, long start, long finish)
2581 int len, file;
2583 file = mc_open (filename, O_CREAT | O_WRONLY | O_TRUNC,
2584 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2585 if (file == -1)
2586 return 0;
2588 if (edit->column_highlight)
2590 int r;
2591 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2592 if (r > 0)
2594 unsigned char *block, *p;
2595 p = block = edit_get_block (edit, start, finish, &len);
2596 while (len)
2598 r = mc_write (file, p, len);
2599 if (r < 0)
2600 break;
2601 p += r;
2602 len -= r;
2604 g_free (block);
2607 else
2609 unsigned char *buf;
2610 int i = start, end;
2611 len = finish - start;
2612 buf = g_malloc0 (TEMP_BUF_LEN);
2613 while (start != finish)
2615 end = min (finish, start + TEMP_BUF_LEN);
2616 for (; i < end; i++)
2617 buf[i - start] = edit_get_byte (edit, i);
2618 len -= mc_write (file, (char *) buf, end - start);
2619 start = end;
2621 g_free (buf);
2623 mc_close (file);
2624 if (len)
2625 return 0;
2626 return 1;
2629 /* --------------------------------------------------------------------------------------------- */
2631 void
2632 edit_paste_from_history (WEdit * edit)
2634 (void) edit;
2635 edit_error_dialog (_("Error"), _("This function is not implemented"));
2638 /* --------------------------------------------------------------------------------------------- */
2641 edit_copy_to_X_buf_cmd (WEdit * edit)
2643 long start_mark, end_mark;
2644 if (eval_marks (edit, &start_mark, &end_mark))
2645 return 0;
2646 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2648 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2649 return 1;
2651 /* try use external clipboard utility */
2652 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2654 return 0;
2657 /* --------------------------------------------------------------------------------------------- */
2660 edit_cut_to_X_buf_cmd (WEdit * edit)
2662 long start_mark, end_mark;
2663 if (eval_marks (edit, &start_mark, &end_mark))
2664 return 0;
2665 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2667 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2668 return 1;
2670 /* try use external clipboard utility */
2671 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2673 edit_block_delete_cmd (edit);
2674 edit_mark_cmd (edit, 1);
2675 return 0;
2678 /* --------------------------------------------------------------------------------------------- */
2680 void
2681 edit_paste_from_X_buf_cmd (WEdit * edit)
2683 gchar *tmp;
2684 /* try use external clipboard utility */
2685 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
2686 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
2687 edit_insert_file (edit, tmp);
2688 g_free (tmp);
2692 /* --------------------------------------------------------------------------------------------- */
2694 * Ask user for the line and go to that line.
2695 * Negative numbers mean line from the end (i.e. -1 is the last line).
2698 void
2699 edit_goto_cmd (WEdit * edit)
2701 char *f;
2702 static long line = 0; /* line as typed, saved as default */
2703 long l;
2704 char *error;
2705 char s[32];
2707 g_snprintf (s, sizeof (s), "%ld", line);
2708 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE, line ? s : "");
2709 if (!f)
2710 return;
2712 if (!*f)
2714 g_free (f);
2715 return;
2718 l = strtol (f, &error, 0);
2719 if (*error)
2721 g_free (f);
2722 return;
2725 line = l;
2726 if (l < 0)
2727 l = edit->total_lines + l + 2;
2728 edit_move_display (edit, l - edit->widget.lines / 2 - 1);
2729 edit_move_to_line (edit, l - 1);
2730 edit->force |= REDRAW_COMPLETELY;
2731 g_free (f);
2735 /* --------------------------------------------------------------------------------------------- */
2736 /** Return 1 on success */
2739 edit_save_block_cmd (WEdit * edit)
2741 long start_mark, end_mark;
2742 char *exp, *tmp;
2744 if (eval_marks (edit, &start_mark, &end_mark))
2745 return 1;
2747 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
2748 exp =
2749 input_expand_dialog (_("Save block"), _("Enter file name:"),
2750 MC_HISTORY_EDIT_SAVE_BLOCK, tmp);
2751 g_free (tmp);
2752 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2753 if (exp)
2755 if (!*exp)
2757 g_free (exp);
2758 return 0;
2760 else
2762 if (edit_save_block (edit, exp, start_mark, end_mark))
2764 g_free (exp);
2765 edit->force |= REDRAW_COMPLETELY;
2766 return 1;
2768 else
2770 g_free (exp);
2771 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
2775 edit->force |= REDRAW_COMPLETELY;
2776 return 0;
2780 /* --------------------------------------------------------------------------------------------- */
2781 /** returns TRUE on success */
2783 gboolean
2784 edit_insert_file_cmd (WEdit * edit)
2786 gchar *tmp;
2787 char *exp;
2788 gboolean ret = FALSE;
2790 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
2791 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
2792 MC_HISTORY_EDIT_INSERT_FILE, tmp);
2793 g_free (tmp);
2795 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2797 if (exp != NULL && *exp != '\0')
2799 ret = (edit_insert_file (edit, exp) >= 0);
2800 if (!ret)
2801 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
2804 g_free (exp);
2806 edit->force |= REDRAW_COMPLETELY;
2807 return ret;
2810 /* --------------------------------------------------------------------------------------------- */
2811 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
2814 edit_sort_cmd (WEdit * edit)
2816 static char *old = 0;
2817 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
2818 long start_mark, end_mark;
2819 int e;
2821 if (eval_marks (edit, &start_mark, &end_mark))
2823 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
2824 return 0;
2827 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
2828 edit_save_block (edit, tmp, start_mark, end_mark);
2829 g_free (tmp);
2831 exp = input_dialog (_("Run sort"),
2832 _("Enter sort options (see manpage) separated by whitespace:"),
2833 MC_HISTORY_EDIT_SORT, (old != NULL) ? old : "");
2835 if (!exp)
2836 return 1;
2837 g_free (old);
2838 old = exp;
2839 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
2840 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
2841 tmp =
2842 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
2843 " > ", tmp_edit_temp_name, (char *) NULL);
2844 g_free (tmp_edit_temp_name);
2845 g_free (tmp_edit_block_name);
2847 e = system (tmp);
2848 g_free (tmp);
2849 if (e)
2851 if (e == -1 || e == 127)
2853 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
2855 else
2857 char q[8];
2858 sprintf (q, "%d ", e);
2859 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
2860 edit_error_dialog (_("Sort"), tmp);
2861 g_free (tmp);
2863 return -1;
2866 edit->force |= REDRAW_COMPLETELY;
2868 if (edit_block_delete_cmd (edit))
2869 return 1;
2870 tmp = mc_config_get_full_path (EDIT_TEMP_FILE);
2871 edit_insert_file (edit, tmp);
2872 g_free (tmp);
2873 return 0;
2876 /* --------------------------------------------------------------------------------------------- */
2878 * Ask user for a command, execute it and paste its output back to the
2879 * editor.
2883 edit_ext_cmd (WEdit * edit)
2885 char *exp, *tmp, *tmp_edit_temp_file;
2886 int e;
2888 exp =
2889 input_dialog (_("Paste output of external command"),
2890 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, NULL);
2892 if (!exp)
2893 return 1;
2895 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
2896 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
2897 g_free (tmp_edit_temp_file);
2898 e = system (tmp);
2899 g_free (tmp);
2900 g_free (exp);
2902 if (e)
2904 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
2905 return -1;
2908 edit->force |= REDRAW_COMPLETELY;
2909 tmp = mc_config_get_full_path (EDIT_TEMP_FILE);
2910 edit_insert_file (edit, tmp);
2911 g_free (tmp);
2912 return 0;
2915 /* --------------------------------------------------------------------------------------------- */
2916 /** if block is 1, a block must be highlighted and the shell command
2917 processes it. If block is 0 the shell command is a straight system
2918 command, that just produces some output which is to be inserted */
2920 void
2921 edit_block_process_cmd (WEdit * edit, int macro_number)
2923 char *fname;
2924 char *macros_fname = NULL;
2926 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
2927 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
2928 user_menu (edit, macros_fname, 0);
2929 g_free (fname);
2930 g_free (macros_fname);
2931 edit->force |= REDRAW_COMPLETELY;
2934 /* --------------------------------------------------------------------------------------------- */
2936 void
2937 edit_mail_dialog (WEdit * edit)
2939 char *tmail_to;
2940 char *tmail_subject;
2941 char *tmail_cc;
2943 static char *mail_cc_last = 0;
2944 static char *mail_subject_last = 0;
2945 static char *mail_to_last = 0;
2947 QuickWidget quick_widgets[] = {
2948 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
2949 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
2950 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input", &tmail_cc),
2951 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT, N_("Copies to")),
2952 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-2",
2953 &tmail_subject),
2954 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT, N_("Subject")),
2955 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-3", &tmail_to),
2956 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT, N_("To")),
2957 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT, N_("mail -s <subject> -c <cc> <to>")),
2958 QUICK_END
2961 QuickDialog Quick_input = {
2962 50, MAIL_DLG_HEIGHT, -1, -1, N_("Mail"),
2963 "[Input Line Keys]", quick_widgets, NULL, FALSE
2966 quick_widgets[2].u.input.text = mail_cc_last ? mail_cc_last : "";
2967 quick_widgets[4].u.input.text = mail_subject_last ? mail_subject_last : "";
2968 quick_widgets[6].u.input.text = mail_to_last ? mail_to_last : "";
2970 if (quick_dialog (&Quick_input) != B_CANCEL)
2972 g_free (mail_cc_last);
2973 g_free (mail_subject_last);
2974 g_free (mail_to_last);
2975 mail_cc_last = tmail_cc;
2976 mail_subject_last = tmail_subject;
2977 mail_to_last = tmail_to;
2978 pipe_mail (edit, mail_to_last, mail_subject_last, mail_cc_last);
2983 /*******************/
2984 /* Word Completion */
2985 /*******************/
2987 /* --------------------------------------------------------------------------------------------- */
2989 * Complete current word using regular expression search
2990 * backwards beginning at the current cursor position.
2993 void
2994 edit_complete_word_cmd (WEdit * edit)
2996 gsize i, max_len, word_len = 0, num_compl = 0;
2997 long word_start = 0;
2998 unsigned char *bufpos;
2999 char *match_expr;
3000 struct selection compl[MAX_WORD_COMPLETIONS]; /* completions */
3002 /* search start of word to be completed */
3003 if (!edit_find_word_start (edit, &word_start, &word_len))
3004 return;
3006 /* prepare match expression */
3007 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3009 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3010 match_expr =
3011 g_strdup_printf
3012 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3013 (int) word_len, bufpos);
3015 /* collect the possible completions */
3016 /* start search from begin to end of file */
3017 max_len =
3018 edit_collect_completions (edit, word_start, word_len, match_expr,
3019 (struct selection *) &compl, &num_compl);
3021 if (num_compl > 0)
3023 /* insert completed word if there is only one match */
3024 if (num_compl == 1)
3026 for (i = word_len; i < compl[0].len; i++)
3027 edit_insert (edit, *(compl[0].text + i));
3029 /* more than one possible completion => ask the user */
3030 else
3032 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3033 /* !!! pressed again the selection dialog pops up, but that !!! */
3034 /* !!! seems to require a further internal state !!! */
3035 /*tty_beep (); */
3037 /* let the user select the preferred completion */
3038 editcmd_dialog_completion_show (edit, max_len, word_len,
3039 (struct selection *) &compl, num_compl);
3043 g_free (match_expr);
3044 /* release memory before return */
3045 for (i = 0; i < num_compl; i++)
3046 g_free (compl[i].text);
3049 /* --------------------------------------------------------------------------------------------- */
3051 void
3052 edit_select_codepage_cmd (WEdit * edit)
3054 #ifdef HAVE_CHARSET
3055 if (do_select_codepage ())
3056 edit_set_codeset (edit);
3058 edit->force = REDRAW_COMPLETELY;
3059 edit_refresh_cmd (edit);
3060 #else
3061 (void) edit;
3062 #endif
3065 /* --------------------------------------------------------------------------------------------- */
3067 void
3068 edit_insert_literal_cmd (WEdit * edit)
3070 int char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3071 _("Press any key:"), 0);
3072 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3075 /* --------------------------------------------------------------------------------------------- */
3077 void
3078 edit_begin_end_macro_cmd (WEdit * edit)
3080 /* edit is a pointer to the widget */
3081 if (edit != NULL)
3083 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3084 edit_execute_key_command (edit, command, -1);
3088 /* --------------------------------------------------------------------------------------------- */
3090 void
3091 edit_begin_end_repeat_cmd (WEdit * edit)
3093 /* edit is a pointer to the widget */
3094 if (edit != NULL)
3096 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3097 edit_execute_key_command (edit, command, -1);
3101 /* --------------------------------------------------------------------------------------------- */
3104 edit_load_forward_cmd (WEdit * edit)
3106 if (edit->modified)
3108 if (edit_query_dialog2
3109 (_("Warning"),
3110 _("Current text was modified without a file save\n"
3111 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3113 edit->force |= REDRAW_COMPLETELY;
3114 return 0;
3117 if (edit_stack_iterator + 1 < MAX_HISTORY_MOVETO)
3119 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3121 return 1;
3123 edit_stack_iterator++;
3124 if (edit_history_moveto[edit_stack_iterator].filename)
3126 edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename,
3127 edit_history_moveto[edit_stack_iterator].line);
3128 return 0;
3130 else
3132 return 1;
3135 else
3137 return 1;
3141 /* --------------------------------------------------------------------------------------------- */
3144 edit_load_back_cmd (WEdit * edit)
3146 if (edit->modified)
3148 if (edit_query_dialog2
3149 (_("Warning"),
3150 _("Current text was modified without a file save\n"
3151 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3153 edit->force |= REDRAW_COMPLETELY;
3154 return 0;
3157 if (edit_stack_iterator > 0)
3159 edit_stack_iterator--;
3160 if (edit_history_moveto[edit_stack_iterator].filename)
3162 edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename,
3163 edit_history_moveto[edit_stack_iterator].line);
3164 return 0;
3166 else
3168 return 1;
3171 else
3173 return 1;
3177 /* --------------------------------------------------------------------------------------------- */
3179 void
3180 edit_get_match_keyword_cmd (WEdit * edit)
3182 gsize word_len = 0, max_len = 0;
3183 int num_def = 0;
3184 int i;
3185 long word_start = 0;
3186 unsigned char *bufpos;
3187 char *match_expr;
3188 char *path = NULL;
3189 char *ptr = NULL;
3190 char *tagfile = NULL;
3192 etags_hash_t def_hash[MAX_DEFINITIONS];
3194 for (i = 0; i < MAX_DEFINITIONS; i++)
3196 def_hash[i].filename = NULL;
3199 /* search start of word to be completed */
3200 if (!edit_find_word_start (edit, &word_start, &word_len))
3201 return;
3203 /* prepare match expression */
3204 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3205 match_expr = g_strdup_printf ("%.*s", (int) word_len, bufpos);
3207 ptr = g_get_current_dir ();
3208 path = g_strconcat (ptr, G_DIR_SEPARATOR_S, (char *) NULL);
3209 g_free (ptr);
3211 /* Recursive search file 'TAGS' in parent dirs */
3214 ptr = g_path_get_dirname (path);
3215 g_free (path);
3216 path = ptr;
3217 g_free (tagfile);
3218 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3219 if (exist_file (tagfile))
3220 break;
3222 while (strcmp (path, G_DIR_SEPARATOR_S) != 0);
3224 if (tagfile)
3226 num_def =
3227 etags_set_definition_hash (tagfile, path, match_expr, (etags_hash_t *) & def_hash);
3228 g_free (tagfile);
3230 g_free (path);
3232 max_len = MAX_WIDTH_DEF_DIALOG;
3233 word_len = 0;
3234 if (num_def > 0)
3236 editcmd_dialog_select_definition_show (edit, match_expr, max_len, word_len,
3237 (etags_hash_t *) & def_hash, num_def);
3239 g_free (match_expr);
3242 /* --------------------------------------------------------------------------------------------- */