Ticket #2761: save file on top of existing directory changes dir's permissions
[midnight-commander.git] / src / editor / editcmd.c
blobd0c665f35716533c56207d91b7eec6dbccbaa0de
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 #ifdef HAVE_ASSERT_H
38 #include <assert.h>
39 #endif
40 #include <ctype.h>
42 #include <stdio.h>
43 #include <stdarg.h>
44 #include <sys/types.h>
45 #include <unistd.h>
46 #include <string.h>
47 #include <errno.h>
48 #include <sys/stat.h>
49 #include <stdlib.h>
50 #include <fcntl.h>
52 #include "lib/global.h"
53 #include "lib/tty/tty.h"
54 #include "lib/tty/key.h" /* XCTRL */
55 #include "lib/mcconfig.h"
56 #include "lib/skin.h"
57 #include "lib/strutil.h" /* utf string functions */
58 #include "lib/lock.h"
59 #include "lib/util.h" /* tilde_expand() */
60 #include "lib/vfs/vfs.h"
61 #include "lib/widget.h"
62 #include "lib/charsets.h"
63 #include "lib/event.h" /* mc_event_raise() */
65 #include "src/history.h"
66 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/main.h" /* mactos_t */
68 #include "src/selcodepage.h"
69 #include "src/keybind-defaults.h"
70 #include "src/util.h" /* check_for_default() */
71 #include "src/filemanager/layout.h" /* mc_refresh() */
73 #include "edit-impl.h"
74 #include "edit-widget.h"
75 #include "editcmd_dialogs.h"
76 #include "etags.h"
78 /*** global variables ****************************************************************************/
80 /* search and replace: */
81 int search_create_bookmark = FALSE;
83 /* queries on a save */
84 int edit_confirm_save = 1;
86 /*** file scope macro definitions ****************************************************************/
88 #define space_width 1
90 #define TEMP_BUF_LEN 1024
92 #define INPUT_INDEX 9
94 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
95 (and the above) routines to work properly - paul */
97 #define is_digit(x) ((x) >= '0' && (x) <= '9')
99 #define MAIL_DLG_HEIGHT 12
101 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
103 /*** file scope type declarations ****************************************************************/
105 /*** file scope variables ************************************************************************/
107 /*** file scope functions ************************************************************************/
108 /* --------------------------------------------------------------------------------------------- */
110 /* If 0 (quick save) then a) create/truncate <filename> file,
111 b) save to <filename>;
112 if 1 (safe save) then a) save to <tempnam>,
113 b) rename <tempnam> to <filename>;
114 if 2 (do backups) then a) save to <tempnam>,
115 b) rename <filename> to <filename.backup_ext>,
116 c) rename <tempnam> to <filename>. */
118 /* returns 0 on error, -1 on abort */
120 static int
121 edit_save_file (WEdit * edit, const char *filename)
123 char *p;
124 gchar *tmp;
125 long filelen = 0;
126 char *savename = 0;
127 gchar *real_filename;
128 int this_save_mode, fd = -1;
130 if (!filename)
131 return 0;
132 if (!*filename)
133 return 0;
135 if (*filename != PATH_SEP && edit->dir)
137 real_filename = concat_dir_and_file (edit->dir, filename);
139 else
141 real_filename = g_strdup (filename);
144 this_save_mode = option_save_mode;
145 if (this_save_mode != EDIT_QUICK_SAVE)
147 vfs_path_t *vpath = vfs_path_from_str (real_filename);
148 if (!vfs_file_is_local (vpath) || (fd = mc_open (real_filename, O_RDONLY | O_BINARY)) == -1)
151 * The file does not exists yet, so no safe save or
152 * backup are necessary.
154 this_save_mode = EDIT_QUICK_SAVE;
156 vfs_path_free (vpath);
157 if (fd != -1)
158 mc_close (fd);
161 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt)
163 int rv;
164 struct stat sb;
166 rv = mc_stat (real_filename, &sb);
167 if (rv == 0 && sb.st_nlink > 1)
169 rv = edit_query_dialog3 (_("Warning"),
170 _("File has hard-links. Detach before saving?"),
171 _("&Yes"), _("&No"), _("&Cancel"));
172 switch (rv)
174 case 0:
175 this_save_mode = EDIT_SAFE_SAVE;
176 /* fallthrough */
177 case 1:
178 edit->skip_detach_prompt = 1;
179 break;
180 default:
181 g_free (real_filename);
182 return -1;
186 /* Prevent overwriting changes from other editor sessions. */
187 if (rv == 0 && edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
190 /* The default action is "Cancel". */
191 query_set_sel (1);
193 rv = edit_query_dialog2 (_("Warning"),
194 _("The file has been modified in the meantime. Save anyway?"),
195 _("&Yes"), _("&Cancel"));
196 if (rv != 0)
198 g_free (real_filename);
199 return -1;
204 if (this_save_mode != EDIT_QUICK_SAVE)
206 char *savedir, *saveprefix;
207 const char *slashpos;
208 slashpos = strrchr (real_filename, PATH_SEP);
209 if (slashpos)
211 savedir = g_strdup (real_filename);
212 savedir[slashpos - real_filename + 1] = '\0';
214 else
215 savedir = g_strdup (".");
216 saveprefix = concat_dir_and_file (savedir, "cooledit");
217 g_free (savedir);
218 fd = mc_mkstemps (&savename, saveprefix, NULL);
219 g_free (saveprefix);
220 if (!savename)
222 g_free (real_filename);
223 return 0;
225 /* FIXME:
226 * Close for now because mc_mkstemps use pure open system call
227 * to create temporary file and it needs to be reopened by
228 * VFS-aware mc_open().
230 close (fd);
232 else
233 savename = g_strdup (real_filename);
235 int ret;
236 ret = mc_chown (savename, edit->stat1.st_uid, edit->stat1.st_gid);
237 ret = mc_chmod (savename, edit->stat1.st_mode);
240 fd = mc_open (savename, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
241 if (fd == -1)
242 goto error_save;
244 /* pipe save */
245 p = edit_get_write_filter (savename, real_filename);
246 if (p != NULL)
248 FILE *file;
250 mc_close (fd);
251 file = (FILE *) popen (p, "w");
253 if (file)
255 filelen = edit_write_stream (edit, file);
256 #if 1
257 pclose (file);
258 #else
259 if (pclose (file) != 0)
261 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
262 edit_error_dialog (_("Error"), tmp);
263 g_free (tmp);
264 g_free (p);
265 goto error_save;
267 #endif
269 else
271 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
272 edit_error_dialog (_("Error"), get_sys_error (tmp));
273 g_free (p);
274 g_free (tmp);
275 goto error_save;
277 g_free (p);
279 else if (edit->lb == LB_ASIS)
280 { /* do not change line breaks */
281 long buf;
282 buf = 0;
283 filelen = edit->last_byte;
284 while (buf <= (edit->curs1 >> S_EDIT_BUF_SIZE) - 1)
286 if (mc_write (fd, (char *) edit->buffers1[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
288 mc_close (fd);
289 goto error_save;
291 buf++;
293 if (mc_write
294 (fd, (char *) edit->buffers1[buf],
295 edit->curs1 & M_EDIT_BUF_SIZE) != (edit->curs1 & M_EDIT_BUF_SIZE))
297 filelen = -1;
299 else if (edit->curs2)
301 edit->curs2--;
302 buf = (edit->curs2 >> S_EDIT_BUF_SIZE);
303 if (mc_write
304 (fd,
305 (char *) edit->buffers2[buf] + EDIT_BUF_SIZE -
306 (edit->curs2 & M_EDIT_BUF_SIZE) - 1,
307 1 + (edit->curs2 & M_EDIT_BUF_SIZE)) != 1 + (edit->curs2 & M_EDIT_BUF_SIZE))
309 filelen = -1;
311 else
313 while (--buf >= 0)
315 if (mc_write (fd, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
317 filelen = -1;
318 break;
322 edit->curs2++;
324 if (mc_close (fd))
325 goto error_save;
327 /* Update the file information, especially the mtime. */
328 if (mc_stat (savename, &edit->stat1) == -1)
329 goto error_save;
331 else
332 { /* change line breaks */
333 FILE *file;
335 mc_close (fd);
337 file = (FILE *) fopen (savename, "w");
339 if (file)
341 filelen = edit_write_stream (edit, file);
342 fclose (file);
344 else
346 char *msg;
348 msg = g_strdup_printf (_("Cannot open file for writing: %s"), savename);
349 edit_error_dialog (_("Error"), msg);
350 g_free (msg);
351 goto error_save;
355 if (filelen != edit->last_byte)
356 goto error_save;
358 if (this_save_mode == EDIT_DO_BACKUP)
360 #ifdef HAVE_ASSERT_H
361 assert (option_backup_ext != NULL);
362 #endif
363 tmp = g_strconcat (real_filename, option_backup_ext, (char *) NULL);
364 if (mc_rename (real_filename, tmp) == -1)
366 g_free (tmp);
367 goto error_save;
371 if (this_save_mode != EDIT_QUICK_SAVE)
372 if (mc_rename (savename, real_filename) == -1)
373 goto error_save;
374 g_free (savename);
375 g_free (real_filename);
376 return 1;
377 error_save:
378 /* FIXME: Is this safe ?
379 * if (this_save_mode != EDIT_QUICK_SAVE)
380 * mc_unlink (savename);
382 g_free (real_filename);
383 g_free (savename);
384 return 0;
387 /* --------------------------------------------------------------------------------------------- */
389 static gboolean
390 edit_check_newline (WEdit * edit)
392 return !(option_check_nl_at_eof && edit->last_byte > 0
393 && edit_get_byte (edit, edit->last_byte - 1) != '\n'
394 && edit_query_dialog2 (_("Warning"),
395 _("The file you are saving is not finished with a newline"),
396 _("C&ontinue"), _("&Cancel")));
399 /* --------------------------------------------------------------------------------------------- */
401 static char *
402 edit_get_save_file_as (WEdit * edit)
404 #define DLG_WIDTH 64
405 #define DLG_HEIGHT 14
407 static LineBreaks cur_lb = LB_ASIS;
409 char *filename = edit->filename;
411 const char *lb_names[LB_NAMES] = {
412 N_("&Do not change"),
413 N_("&Unix format (LF)"),
414 N_("&Windows/DOS format (CR LF)"),
415 N_("&Macintosh format (CR)")
418 QuickWidget quick_widgets[] = {
419 QUICK_BUTTON (6, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
420 QUICK_BUTTON (2, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
421 QUICK_RADIO (5, DLG_WIDTH, DLG_HEIGHT - 8, DLG_HEIGHT, LB_NAMES, lb_names, (int *) &cur_lb),
422 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 9, DLG_HEIGHT, N_("Change line breaks to:")),
423 QUICK_INPUT (3, DLG_WIDTH, DLG_HEIGHT - 11, DLG_HEIGHT, filename, DLG_WIDTH - 6, 0,
424 "save-as", &filename),
425 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 12, DLG_HEIGHT, N_("Enter file name:")),
426 QUICK_END
429 QuickDialog Quick_options = {
430 DLG_WIDTH, DLG_HEIGHT, -1, -1,
431 N_("Save As"), "[Save File As]",
432 quick_widgets, NULL, FALSE
435 if (quick_dialog (&Quick_options) != B_CANCEL)
437 char *fname;
439 edit->lb = cur_lb;
440 fname = tilde_expand (filename);
441 g_free (filename);
442 return fname;
445 return NULL;
447 #undef DLG_WIDTH
448 #undef DLG_HEIGHT
451 /** returns 1 on success */
453 static int
454 edit_save_cmd (WEdit * edit)
456 int res, save_lock = 0;
458 if (!edit->locked && !edit->delete_file)
459 save_lock = edit_lock_file (edit);
460 res = edit_save_file (edit, edit->filename);
462 /* Maintain modify (not save) lock on failure */
463 if ((res > 0 && edit->locked) || save_lock)
464 edit->locked = edit_unlock_file (edit);
466 /* On failure try 'save as', it does locking on its own */
467 if (!res)
468 return edit_save_as_cmd (edit);
469 edit->force |= REDRAW_COMPLETELY;
470 if (res > 0)
472 edit->delete_file = 0;
473 edit->modified = 0;
476 return 1;
479 /* --------------------------------------------------------------------------------------------- */
480 /** returns 1 on error */
482 static int
483 edit_load_file_from_filename (WEdit * edit, char *exp)
485 int prev_locked = edit->locked;
486 char *prev_filename = g_strdup (edit->filename);
488 if (!edit_reload (edit, exp))
490 g_free (prev_filename);
491 return 1;
494 if (prev_locked)
496 char *fullpath;
498 fullpath = mc_build_filename (edit->dir, prev_filename, (char *) NULL);
499 unlock_file (fullpath);
500 g_free (fullpath);
502 g_free (prev_filename);
503 return 0;
506 /* --------------------------------------------------------------------------------------------- */
508 static void
509 edit_load_syntax_file (WEdit * edit)
511 char *extdir;
512 int dir = 0;
514 if (geteuid () == 0)
516 dir = query_dialog (_("Syntax file edit"),
517 _("Which syntax file you want to edit?"), D_NORMAL, 2,
518 _("&User"), _("&System Wide"));
521 extdir = g_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
522 if (!exist_file (extdir))
524 g_free (extdir);
525 extdir = g_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
528 if (dir == 0)
530 char *buffer;
532 buffer = mc_config_get_full_path (EDIT_SYNTAX_FILE);
533 check_for_default (extdir, buffer);
534 edit_load_file_from_filename (edit, buffer);
535 g_free (buffer);
537 else if (dir == 1)
538 edit_load_file_from_filename (edit, extdir);
540 g_free (extdir);
543 /* --------------------------------------------------------------------------------------------- */
545 static void
546 edit_load_menu_file (WEdit * edit)
548 char *buffer;
549 char *menufile;
550 int dir = 0;
552 dir = query_dialog (_("Menu edit"),
553 _("Which menu file do you want to edit?"), D_NORMAL,
554 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
556 menufile = concat_dir_and_file (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU);
558 if (!exist_file (menufile))
560 g_free (menufile);
561 menufile = concat_dir_and_file (mc_global.share_data_dir, EDIT_GLOBAL_MENU);
564 switch (dir)
566 case 0:
567 buffer = g_strdup (EDIT_LOCAL_MENU);
568 check_for_default (menufile, buffer);
569 chmod (buffer, 0600);
570 break;
572 case 1:
573 buffer = mc_config_get_full_path (EDIT_HOME_MENU);
574 check_for_default (menufile, buffer);
575 break;
577 case 2:
578 buffer = concat_dir_and_file (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU);
579 if (!exist_file (buffer))
581 g_free (buffer);
582 buffer = concat_dir_and_file (mc_global.share_data_dir, EDIT_GLOBAL_MENU);
584 break;
586 default:
587 g_free (menufile);
588 return;
591 edit_load_file_from_filename (edit, buffer);
593 g_free (buffer);
594 g_free (menufile);
597 /* --------------------------------------------------------------------------------------------- */
599 static void
600 edit_delete_column_of_text (WEdit * edit)
602 long p, q, r, m1, m2;
603 long b, c, d, n;
605 eval_marks (edit, &m1, &m2);
606 n = edit_move_forward (edit, m1, 0, m2) + 1;
607 c = edit_move_forward3 (edit, edit_bol (edit, m1), 0, m1);
608 d = edit_move_forward3 (edit, edit_bol (edit, m2), 0, m2);
609 b = max (min (c, d), min (edit->column1, edit->column2));
610 c = max (c, max (edit->column1, edit->column2));
612 while (n--)
614 r = edit_bol (edit, edit->curs1);
615 p = edit_move_forward3 (edit, r, b, 0);
616 q = edit_move_forward3 (edit, r, c, 0);
617 if (p < m1)
618 p = m1;
619 if (q > m2)
620 q = m2;
621 edit_cursor_move (edit, p - edit->curs1);
622 while (q > p)
624 /* delete line between margins */
625 if (edit_get_byte (edit, edit->curs1) != '\n')
626 edit_delete (edit, 1);
627 q--;
629 if (n)
630 /* move to next line except on the last delete */
631 edit_cursor_move (edit, edit_move_forward (edit, edit->curs1, 1, 0) - edit->curs1);
635 /* --------------------------------------------------------------------------------------------- */
636 /** if success return 0 */
638 static int
639 edit_block_delete (WEdit * edit)
641 long count;
642 long start_mark, end_mark;
643 int curs_pos, line_width;
644 long curs_line, c1, c2;
646 if (eval_marks (edit, &start_mark, &end_mark))
647 return 0;
648 if (edit->column_highlight && edit->mark2 < 0)
649 edit_mark_cmd (edit, 0);
650 if ((end_mark - start_mark) > option_max_undo / 2)
652 /* Warning message with a query to continue or cancel the operation */
653 if (edit_query_dialog2
654 (_("Warning"),
656 ("Block is large, you may not be able to undo this action"),
657 _("C&ontinue"), _("&Cancel")))
659 return 1;
662 c1 = min (edit->column1, edit->column2);
663 c2 = max (edit->column1, edit->column2);
664 edit->column1 = c1;
665 edit->column2 = c2;
667 edit_push_markers (edit);
669 curs_line = edit->curs_line;
671 /* calculate line width and cursor position before cut */
672 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
673 edit_eol (edit, edit->curs1));
674 curs_pos = edit->curs_col + edit->over_col;
676 /* move cursor to start of selection */
677 edit_cursor_move (edit, start_mark - edit->curs1);
678 edit_scroll_screen_over_cursor (edit);
679 count = start_mark;
680 if (start_mark < end_mark)
682 if (edit->column_highlight)
684 if (edit->mark2 < 0)
685 edit_mark_cmd (edit, 0);
686 edit_delete_column_of_text (edit);
687 /* move cursor to the saved position */
688 edit_move_to_line (edit, curs_line);
689 /* calculate line width after cut */
690 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
691 edit_eol (edit, edit->curs1));
692 if (option_cursor_beyond_eol && curs_pos > line_width)
693 edit->over_col = curs_pos - line_width;
695 else
697 while (count < end_mark)
699 edit_delete (edit, 1);
700 count++;
704 edit_set_markers (edit, 0, 0, 0, 0);
705 edit->force |= REDRAW_PAGE;
706 return 0;
709 /* --------------------------------------------------------------------------------------------- */
711 static gboolean
712 editcmd_find (WEdit * edit, gsize * len)
714 off_t search_start = edit->search_start;
715 off_t search_end;
716 long start_mark = 0;
717 long end_mark = edit->last_byte;
718 int mark_res = 0;
720 if (edit_search_options.only_in_selection)
722 mark_res = eval_marks (edit, &start_mark, &end_mark);
723 if (mark_res != 0)
725 edit->search->error = MC_SEARCH_E_NOTFOUND;
726 edit->search->error_str = g_strdup (_("Search string not found"));
727 return FALSE;
729 if (edit_search_options.backwards)
731 if (search_start > end_mark || search_start <= start_mark)
733 search_start = end_mark;
736 else
738 if (search_start < start_mark || search_start >= end_mark)
740 search_start = start_mark;
744 else
746 if (edit_search_options.backwards)
747 end_mark = max (1, edit->curs1) - 1;
749 if (edit_search_options.backwards)
751 search_end = end_mark;
752 while ((int) search_start >= start_mark)
754 if (search_end > (off_t) (search_start + edit->search->original_len) &&
755 mc_search_is_fixed_search_str (edit->search))
757 search_end = search_start + edit->search->original_len;
759 if (mc_search_run (edit->search, (void *) edit, search_start, search_end, len)
760 && edit->search->normal_offset == search_start)
762 return TRUE;
764 search_start--;
766 edit->search->error_str = g_strdup (_("Search string not found"));
768 else
770 return mc_search_run (edit->search, (void *) edit, search_start, end_mark, len);
772 return FALSE;
775 /* --------------------------------------------------------------------------------------------- */
777 static char *
778 edit_replace_cmd__conv_to_display (char *str)
780 #ifdef HAVE_CHARSET
781 GString *tmp;
783 tmp = str_convert_to_display (str);
784 if (tmp != NULL)
786 if (tmp->len != 0)
787 return g_string_free (tmp, FALSE);
788 g_string_free (tmp, TRUE);
790 #endif
791 return g_strdup (str);
794 /* --------------------------------------------------------------------------------------------- */
796 static char *
797 edit_replace_cmd__conv_to_input (char *str)
799 #ifdef HAVE_CHARSET
800 GString *tmp;
802 tmp = str_convert_to_input (str);
803 if (tmp != NULL)
805 if (tmp->len != 0)
806 return g_string_free (tmp, FALSE);
807 g_string_free (tmp, TRUE);
809 #endif
810 return g_strdup (str);
813 /* --------------------------------------------------------------------------------------------- */
815 static void
816 edit_do_search (WEdit * edit)
818 gsize len = 0;
820 if (edit->search == NULL)
821 edit->search_start = edit->curs1;
823 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
825 if (search_create_bookmark)
827 int found = 0, books = 0;
828 long l = 0, l_last = -1;
829 long q = 0;
831 search_create_bookmark = FALSE;
832 book_mark_flush (edit, -1);
834 while (TRUE)
836 if (!mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len))
837 break;
838 if (found == 0)
839 edit->search_start = edit->search->normal_offset;
840 found++;
841 l += edit_count_lines (edit, q, edit->search->normal_offset);
842 if (l != l_last)
844 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
845 books++;
847 l_last = l;
848 q = edit->search->normal_offset + 1;
851 if (found == 0)
852 edit_error_dialog (_("Search"), _("Search string not found"));
853 else
854 edit_cursor_move (edit, edit->search_start - edit->curs1);
856 else
858 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
859 && edit_search_options.backwards)
860 edit->search_start--;
862 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
863 && !edit_search_options.backwards)
864 edit->search_start++;
866 if (editcmd_find (edit, &len))
868 edit->found_start = edit->search_start = edit->search->normal_offset;
869 edit->found_len = len;
870 edit->over_col = 0;
871 edit_cursor_move (edit, edit->search_start - edit->curs1);
872 edit_scroll_screen_over_cursor (edit);
873 if (edit_search_options.backwards)
874 edit->search_start--;
875 else
876 edit->search_start++;
878 else
880 edit->search_start = edit->curs1;
881 if (edit->search->error_str != NULL)
882 edit_error_dialog (_("Search"), edit->search->error_str);
886 edit->force |= REDRAW_COMPLETELY;
887 edit_scroll_screen_over_cursor (edit);
890 /* --------------------------------------------------------------------------------------------- */
892 static void
893 edit_search (WEdit * edit)
895 if (editcmd_dialog_search_show (edit))
896 edit_do_search (edit);
899 /* --------------------------------------------------------------------------------------------- */
900 /** Return a null terminated length of text. Result must be g_free'd */
902 static unsigned char *
903 edit_get_block (WEdit * edit, long start, long finish, int *l)
905 unsigned char *s, *r;
906 r = s = g_malloc0 (finish - start + 1);
907 if (edit->column_highlight)
909 *l = 0;
910 /* copy from buffer, excluding chars that are out of the column 'margins' */
911 while (start < finish)
913 int c;
914 long x;
915 x = edit_move_forward3 (edit, edit_bol (edit, start), 0, start);
916 c = edit_get_byte (edit, start);
917 if ((x >= edit->column1 && x < edit->column2)
918 || (x >= edit->column2 && x < edit->column1) || c == '\n')
920 *s++ = c;
921 (*l)++;
923 start++;
926 else
928 *l = finish - start;
929 while (start < finish)
930 *s++ = edit_get_byte (edit, start++);
932 *s = 0;
933 return r;
936 /* --------------------------------------------------------------------------------------------- */
937 /** copies a block to clipboard file */
939 static int
940 edit_save_block_to_clip_file (WEdit * edit, long start, long finish)
942 int ret;
943 gchar *tmp;
944 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
945 ret = edit_save_block (edit, tmp, start, finish);
946 g_free (tmp);
947 return ret;
950 /* --------------------------------------------------------------------------------------------- */
952 static void
953 pipe_mail (WEdit * edit, char *to, char *subject, char *cc)
955 FILE *p = 0;
956 char *s;
958 to = name_quote (to, 0);
959 subject = name_quote (subject, 0);
960 cc = name_quote (cc, 0);
961 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
962 g_free (to);
963 g_free (subject);
964 g_free (cc);
966 if (s)
968 p = popen (s, "w");
969 g_free (s);
972 if (p)
974 long i;
975 for (i = 0; i < edit->last_byte; i++)
976 fputc (edit_get_byte (edit, i), p);
977 pclose (p);
981 /* --------------------------------------------------------------------------------------------- */
983 static gboolean
984 is_break_char (char c)
986 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
989 /* --------------------------------------------------------------------------------------------- */
990 /** find first character of current word */
992 static int
993 edit_find_word_start (WEdit * edit, long *word_start, gsize * word_len)
995 int c, last;
996 gsize i;
998 /* return if at begin of file */
999 if (edit->curs1 <= 0)
1000 return 0;
1002 c = (unsigned char) edit_get_byte (edit, edit->curs1 - 1);
1003 /* return if not at end or in word */
1004 if (is_break_char (c))
1005 return 0;
1007 /* search start of word to be completed */
1008 for (i = 2;; i++)
1010 /* return if at begin of file */
1011 if ((gsize) edit->curs1 < i)
1012 return 0;
1014 last = c;
1015 c = (unsigned char) edit_get_byte (edit, edit->curs1 - i);
1017 if (is_break_char (c))
1019 /* return if word starts with digit */
1020 if (isdigit (last))
1021 return 0;
1023 *word_start = edit->curs1 - (i - 1); /* start found */
1024 *word_len = i - 1;
1025 break;
1028 /* success */
1029 return 1;
1032 /* --------------------------------------------------------------------------------------------- */
1034 * Get current word under cursor
1036 * @param edit editor object
1037 * @param srch mc_search object
1038 * @param word_start start word position
1040 * @return newly allocated string or NULL if no any words under cursor
1043 static char *
1044 edit_collect_completions_get_current_word (WEdit * edit, mc_search_t * srch, long word_start)
1046 gsize len = 0, i;
1047 GString *temp;
1049 if (!mc_search_run (srch, (void *) edit, word_start, edit->last_byte, &len))
1050 return NULL;
1052 temp = g_string_sized_new (len);
1054 for (i = 0; i < len; i++)
1056 int chr;
1058 chr = edit_get_byte (edit, word_start + i);
1059 if (!isspace (chr))
1060 g_string_append_c (temp, chr);
1063 return g_string_free (temp, temp->len == 0);
1066 /* --------------------------------------------------------------------------------------------- */
1067 /** collect the possible completions */
1069 static gsize
1070 edit_collect_completions (WEdit * edit, long word_start, gsize word_len,
1071 char *match_expr, struct selection *compl, gsize * num)
1073 gsize len = 0;
1074 gsize max_len = 0;
1075 gsize i;
1076 int skip;
1077 GString *temp;
1078 mc_search_t *srch;
1079 long last_byte, start = -1;
1080 char *current_word;
1082 srch = mc_search_new (match_expr, -1);
1083 if (srch == NULL)
1084 return 0;
1086 if (mc_config_get_bool
1087 (mc_main_config, CONFIG_APP_SECTION, "editor_wordcompletion_collect_entire_file", 0))
1089 last_byte = edit->last_byte;
1091 else
1093 last_byte = word_start;
1096 srch->search_type = MC_SEARCH_T_REGEX;
1097 srch->is_case_sensitive = TRUE;
1098 srch->search_fn = edit_search_cmd_callback;
1100 current_word = edit_collect_completions_get_current_word (edit, srch, word_start);
1102 temp = g_string_new ("");
1104 /* collect max MAX_WORD_COMPLETIONS completions */
1105 while (mc_search_run (srch, (void *) edit, start + 1, last_byte, &len))
1107 g_string_set_size (temp, 0);
1108 start = srch->normal_offset;
1110 /* add matched completion if not yet added */
1111 for (i = 0; i < len; i++)
1113 skip = edit_get_byte (edit, start + i);
1114 if (isspace (skip))
1115 continue;
1117 /* skip current word */
1118 if (start + (long) i == word_start)
1119 break;
1121 g_string_append_c (temp, skip);
1124 if (temp->len == 0)
1125 continue;
1127 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1128 continue;
1130 skip = 0;
1132 for (i = 0; i < *num; i++)
1134 if (strncmp
1135 ((char *) &compl[i].text[word_len],
1136 (char *) &temp->str[word_len], max (len, compl[i].len) - word_len) == 0)
1138 struct selection this = compl[i];
1139 for (++i; i < *num; i++)
1141 compl[i - 1] = compl[i];
1143 compl[*num - 1] = this;
1144 skip = 1;
1145 break; /* skip it, already added */
1148 if (skip != 0)
1149 continue;
1151 if (*num == MAX_WORD_COMPLETIONS)
1153 g_free (compl[0].text);
1154 for (i = 1; i < *num; i++)
1156 compl[i - 1] = compl[i];
1158 (*num)--;
1160 #ifdef HAVE_CHARSET
1162 GString *recoded;
1163 recoded = str_convert_to_display (temp->str);
1165 if (recoded && recoded->len)
1166 g_string_assign (temp, recoded->str);
1168 g_string_free (recoded, TRUE);
1170 #endif
1171 compl[*num].text = g_strdup (temp->str);
1172 compl[*num].len = temp->len;
1173 (*num)++;
1174 start += len;
1176 /* note the maximal length needed for the completion dialog */
1177 if (len > max_len)
1178 max_len = len;
1181 mc_search_free (srch);
1182 g_string_free (temp, TRUE);
1183 g_free (current_word);
1185 return max_len;
1188 /* --------------------------------------------------------------------------------------------- */
1190 static void
1191 edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width,
1192 long *start_pos, long *end_pos, int *col1, int *col2)
1194 long cursor;
1195 int i, col;
1197 cursor = edit->curs1;
1198 col = edit_get_col (edit);
1200 for (i = 0; i < size; i++)
1202 if (data[i] != '\n')
1203 edit_insert (edit, data[i]);
1204 else
1205 { /* fill in and move to next line */
1206 int l;
1207 long p;
1209 if (edit_get_byte (edit, edit->curs1) != '\n')
1211 l = width - (edit_get_col (edit) - col);
1212 while (l > 0)
1214 edit_insert (edit, ' ');
1215 l -= space_width;
1218 for (p = edit->curs1;; p++)
1220 if (p == edit->last_byte)
1222 edit_cursor_move (edit, edit->last_byte - edit->curs1);
1223 edit_insert_ahead (edit, '\n');
1224 p++;
1225 break;
1227 if (edit_get_byte (edit, p) == '\n')
1229 p++;
1230 break;
1233 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
1234 l = col - edit_get_col (edit);
1235 while (l >= space_width)
1237 edit_insert (edit, ' ');
1238 l -= space_width;
1243 *col1 = col;
1244 *col2 = col + width;
1245 *start_pos = cursor;
1246 *end_pos = edit->curs1;
1247 edit_cursor_move (edit, cursor - edit->curs1);
1250 /* --------------------------------------------------------------------------------------------- */
1252 static int
1253 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1255 const macros_t *m1 = (const macros_t *) macro1;
1256 const macros_t *m2 = (const macros_t *) macro2;
1258 return m1->hotkey - m2->hotkey;
1261 /* --------------------------------------------------------------------------------------------- */
1263 static void
1264 edit_macro_sort_by_hotkey (void)
1266 if (macros_list != NULL && macros_list->len != 0)
1267 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1270 /* --------------------------------------------------------------------------------------------- */
1272 static gboolean
1273 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1275 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1276 macros_t *result;
1277 macros_t search_macro;
1279 (void) edit;
1281 search_macro.hotkey = hotkey;
1282 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1283 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1285 if (result != NULL && result->macro != NULL)
1287 *indx = (result - array_start);
1288 *macros = result;
1289 return TRUE;
1291 *indx = 0;
1292 return FALSE;
1295 /* --------------------------------------------------------------------------------------------- */
1296 /** returns FALSE on error */
1298 static gboolean
1299 edit_delete_macro (WEdit * edit, int hotkey)
1301 mc_config_t *macros_config = NULL;
1302 const char *section_name = "editor";
1303 gchar *macros_fname;
1304 guint indx;
1305 char *skeyname;
1306 const macros_t *macros = NULL;
1308 /* clear array of actions for current hotkey */
1309 while (edit_get_macro (edit, hotkey, &macros, &indx))
1311 if (macros->macro != NULL)
1312 g_array_free (macros->macro, TRUE);
1313 macros = NULL;
1314 g_array_remove_index (macros_list, indx);
1315 edit_macro_sort_by_hotkey ();
1318 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1319 macros_config = mc_config_init (macros_fname);
1320 g_free (macros_fname);
1322 if (macros_config == NULL)
1323 return FALSE;
1325 skeyname = lookup_key_by_code (hotkey);
1326 while (mc_config_del_key (macros_config, section_name, skeyname))
1328 g_free (skeyname);
1329 mc_config_save_file (macros_config, NULL);
1330 mc_config_deinit (macros_config);
1331 return TRUE;
1335 /* --------------------------------------------------------------------------------------------- */
1336 /*** public functions ****************************************************************************/
1337 /* --------------------------------------------------------------------------------------------- */
1339 void
1340 edit_help_cmd (WEdit * edit)
1342 ev_help_t event_data = { NULL, "[Internal File Editor]" };
1343 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
1345 edit->force |= REDRAW_COMPLETELY;
1348 /* --------------------------------------------------------------------------------------------- */
1350 void
1351 edit_refresh_cmd (WEdit * edit)
1353 #ifdef HAVE_SLANG
1354 int color;
1356 edit_get_syntax_color (edit, -1, &color);
1357 tty_touch_screen ();
1358 mc_refresh ();
1359 #else
1360 (void) edit;
1362 clr_scr ();
1363 repaint_screen ();
1364 #endif /* !HAVE_SLANG */
1365 tty_keypad (TRUE);
1368 /* --------------------------------------------------------------------------------------------- */
1370 void
1371 menu_save_mode_cmd (void)
1373 /* diaog sizes */
1374 const int DLG_X = 38;
1375 const int DLG_Y = 13;
1377 char *str_result;
1379 const char *str[] = {
1380 N_("&Quick save"),
1381 N_("&Safe save"),
1382 N_("&Do backups with following extension:")
1385 QuickWidget widgets[] = {
1386 /* 0 */
1387 QUICK_BUTTON (18, DLG_X, DLG_Y - 3, DLG_Y, N_("&Cancel"), B_CANCEL, NULL),
1388 /* 1 */
1389 QUICK_BUTTON (6, DLG_X, DLG_Y - 3, DLG_Y, N_("&OK"), B_ENTER, NULL),
1390 /* 2 */
1391 QUICK_CHECKBOX (4, DLG_X, 8, DLG_Y, N_("Check &POSIX new line"), &option_check_nl_at_eof),
1392 /* 3 */
1393 QUICK_INPUT (8, DLG_X, 6, DLG_Y, option_backup_ext, 9, 0, "edit-backup-ext", &str_result),
1394 /* 4 */
1395 QUICK_RADIO (4, DLG_X, 3, DLG_Y, 3, str, &option_save_mode),
1396 QUICK_END
1399 QuickDialog dialog = {
1400 DLG_X, DLG_Y, -1, -1, N_("Edit Save Mode"),
1401 "[Edit Save Mode]", widgets, NULL, FALSE
1404 size_t i;
1405 size_t maxlen = 0;
1406 size_t w0, w1, b_len, w3;
1408 #ifdef HAVE_ASSERT_H
1409 assert (option_backup_ext != NULL);
1410 #endif
1412 /* OK/Cancel buttons */
1413 w0 = str_term_width1 (_(widgets[0].u.button.text)) + 3;
1414 w1 = str_term_width1 (_(widgets[1].u.button.text)) + 5; /* default button */
1415 b_len = w0 + w1 + 3;
1417 maxlen = max (b_len, (size_t) str_term_width1 (_(dialog.title)) + 2);
1419 w3 = 0;
1420 for (i = 0; i < 3; i++)
1422 #ifdef ENABLE_NLS
1423 str[i] = _(str[i]);
1424 #endif
1425 w3 = max (w3, (size_t) str_term_width1 (str[i]));
1428 maxlen = max (maxlen, w3 + 4);
1430 dialog.xlen = min ((size_t) COLS, maxlen + 8);
1432 widgets[3].u.input.len = w3;
1433 widgets[1].relative_x = (dialog.xlen - b_len) / 2;
1434 widgets[0].relative_x = widgets[1].relative_x + w0 + 2;
1436 for (i = 0; i < sizeof (widgets) / sizeof (widgets[0]); i++)
1437 widgets[i].x_divisions = dialog.xlen;
1439 if (quick_dialog (&dialog) != B_CANCEL)
1441 g_free (option_backup_ext);
1442 option_backup_ext = str_result;
1446 /* --------------------------------------------------------------------------------------------- */
1448 void
1449 edit_set_filename (WEdit * edit, const char *name)
1451 g_free (edit->filename);
1453 if (name == NULL)
1454 name = "";
1456 edit->filename = tilde_expand (name);
1457 if (edit->dir == NULL && !g_path_is_absolute (name))
1458 edit->dir = vfs_get_current_dir ();
1461 /* --------------------------------------------------------------------------------------------- */
1462 /* Here we want to warn the users of overwriting an existing file,
1463 but only if they have made a change to the filename */
1464 /* returns 1 on success */
1466 edit_save_as_cmd (WEdit * edit)
1468 /* This heads the 'Save As' dialog box */
1469 char *exp;
1470 int save_lock = 0;
1471 int different_filename = 0;
1473 if (!edit_check_newline (edit))
1474 return 0;
1476 exp = edit_get_save_file_as (edit);
1477 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1479 if (exp)
1481 if (!*exp)
1483 g_free (exp);
1484 edit->force |= REDRAW_COMPLETELY;
1485 return 0;
1487 else
1489 int rv;
1490 if (strcmp (edit->filename, exp))
1492 int file;
1493 struct stat sb;
1495 if (mc_stat (exp, &sb) == 0 && !S_ISREG (sb.st_mode))
1497 edit_error_dialog (_("Save as"),
1498 get_sys_error (_
1499 ("Cannot save: destination is not a regular file")));
1500 g_free (exp);
1501 edit->force |= REDRAW_COMPLETELY;
1502 return 0;
1505 different_filename = 1;
1506 file = mc_open (exp, O_RDONLY | O_BINARY);
1507 if (file != -1)
1509 /* the file exists */
1510 mc_close (file);
1511 /* Overwrite the current file or cancel the operation */
1512 if (edit_query_dialog2
1513 (_("Warning"),
1514 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1516 edit->force |= REDRAW_COMPLETELY;
1517 g_free (exp);
1518 return 0;
1521 else
1523 edit->stat1.st_mode |= S_IWUSR;
1525 save_lock = lock_file (exp);
1527 else
1529 /* filenames equal, check if already locked */
1530 if (!edit->locked && !edit->delete_file)
1531 save_lock = lock_file (exp);
1534 if (different_filename)
1537 * Allow user to write into saved (under another name) file
1538 * even if original file had r/o user permissions.
1540 edit->stat1.st_mode |= S_IWRITE;
1543 rv = edit_save_file (edit, exp);
1544 switch (rv)
1546 case 1:
1547 /* Succesful, so unlock both files */
1548 if (different_filename)
1550 if (save_lock)
1551 unlock_file (exp);
1552 if (edit->locked)
1553 edit->locked = edit_unlock_file (edit);
1555 else
1557 if (edit->locked || save_lock)
1558 edit->locked = edit_unlock_file (edit);
1561 edit_set_filename (edit, exp);
1562 if (edit->lb != LB_ASIS)
1563 edit_reload (edit, exp);
1564 g_free (exp);
1565 edit->modified = 0;
1566 edit->delete_file = 0;
1567 if (different_filename)
1568 edit_load_syntax (edit, NULL, edit->syntax_type);
1569 edit->force |= REDRAW_COMPLETELY;
1570 return 1;
1571 default:
1572 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1573 /* fallthrough */
1574 case -1:
1575 /* Failed, so maintain modify (not save) lock */
1576 if (save_lock)
1577 unlock_file (exp);
1578 g_free (exp);
1579 edit->force |= REDRAW_COMPLETELY;
1580 return 0;
1584 edit->force |= REDRAW_COMPLETELY;
1585 return 0;
1588 /* {{{ Macro stuff starts here */
1589 /* --------------------------------------------------------------------------------------------- */
1591 void
1592 edit_delete_macro_cmd (WEdit * edit)
1594 int hotkey;
1596 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1598 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1599 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1602 /* --------------------------------------------------------------------------------------------- */
1604 /** returns FALSE on error */
1605 gboolean
1606 edit_execute_macro (WEdit * edit, int hotkey)
1608 gboolean res = FALSE;
1610 if (hotkey != 0)
1612 const macros_t *macros;
1613 guint indx;
1615 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1616 macros->macro != NULL && macros->macro->len != 0)
1618 guint i;
1620 edit->force |= REDRAW_PAGE;
1622 for (i = 0; i < macros->macro->len; i++)
1624 const macro_action_t *m_act;
1626 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1627 edit_execute_cmd (edit, m_act->action, m_act->ch);
1628 res = TRUE;
1633 return res;
1636 /* --------------------------------------------------------------------------------------------- */
1638 /** returns FALSE on error */
1639 gboolean
1640 edit_store_macro_cmd (WEdit * edit)
1642 int i;
1643 int hotkey;
1644 GString *marcros_string;
1645 mc_config_t *macros_config = NULL;
1646 const char *section_name = "editor";
1647 gchar *macros_fname;
1648 GArray *macros; /* current macro */
1649 int tmp_act;
1650 gboolean have_macro = FALSE;
1651 char *skeyname = NULL;
1653 hotkey = editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1654 if (hotkey == ESC_CHAR)
1655 return FALSE;
1657 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1659 /* return FALSE if try assign macro into restricted hotkeys */
1660 if (tmp_act == CK_MacroStartRecord
1661 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1662 return FALSE;
1664 edit_delete_macro (edit, hotkey);
1666 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1667 macros_config = mc_config_init (macros_fname);
1668 g_free (macros_fname);
1670 if (macros_config == NULL)
1671 return FALSE;
1673 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1675 marcros_string = g_string_sized_new (250);
1676 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1678 skeyname = lookup_key_by_code (hotkey);
1680 for (i = 0; i < macro_index; i++)
1682 macro_action_t m_act;
1683 const char *action_name;
1685 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1687 if (action_name == NULL)
1688 break;
1690 m_act.action = record_macro_buf[i].action;
1691 m_act.ch = record_macro_buf[i].ch;
1692 g_array_append_val (macros, m_act);
1693 have_macro = TRUE;
1694 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1695 (int) record_macro_buf[i].ch);
1697 if (have_macro)
1699 macros_t macro;
1700 macro.hotkey = hotkey;
1701 macro.macro = macros;
1702 g_array_append_val (macros_list, macro);
1703 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1705 else
1706 mc_config_del_key (macros_config, section_name, skeyname);
1708 g_free (skeyname);
1709 edit_macro_sort_by_hotkey ();
1711 g_string_free (marcros_string, TRUE);
1712 mc_config_save_file (macros_config, NULL);
1713 mc_config_deinit (macros_config);
1714 return TRUE;
1717 /* --------------------------------------------------------------------------------------------- */
1719 gboolean
1720 edit_repeat_macro_cmd (WEdit * edit)
1722 int i, j;
1723 char *f;
1724 long count_repeat;
1725 char *error = NULL;
1727 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL);
1728 if (f == NULL || *f == '\0')
1730 g_free (f);
1731 return FALSE;
1734 count_repeat = strtol (f, &error, 0);
1736 if (*error != '\0')
1738 g_free (f);
1739 return FALSE;
1742 g_free (f);
1744 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1745 edit->force |= REDRAW_PAGE;
1747 for (j = 0; j < count_repeat; j++)
1748 for (i = 0; i < macro_index; i++)
1749 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1750 edit_update_screen (edit);
1751 return TRUE;
1754 /* --------------------------------------------------------------------------------------------- */
1755 /** return FALSE on error */
1757 gboolean
1758 edit_load_macro_cmd (WEdit * edit)
1760 mc_config_t *macros_config = NULL;
1761 gchar **profile_keys, **keys;
1762 gchar **values, **curr_values;
1763 gsize len, values_len;
1764 const char *section_name = "editor";
1765 gchar *macros_fname;
1766 int hotkey;
1768 (void) edit;
1770 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1771 macros_config = mc_config_init (macros_fname);
1772 g_free (macros_fname);
1774 if (macros_config == NULL)
1775 return FALSE;
1777 profile_keys = keys = mc_config_get_keys (macros_config, section_name, &len);
1778 while (*profile_keys != NULL)
1780 gboolean have_macro;
1781 GArray *macros;
1782 macros_t macro;
1784 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1786 curr_values = values = mc_config_get_string_list (macros_config, section_name,
1787 *profile_keys, &values_len);
1788 hotkey = lookup_key (*profile_keys, NULL);
1789 have_macro = FALSE;
1791 while (*curr_values != NULL && *curr_values[0] != '\0')
1793 char **macro_pair = NULL;
1795 macro_pair = g_strsplit (*curr_values, ":", 2);
1797 if (macro_pair != NULL)
1799 macro_action_t m_act;
1800 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
1801 m_act.action = 0;
1802 else
1804 m_act.action = keybind_lookup_action (macro_pair[0]);
1805 g_free (macro_pair[0]);
1806 macro_pair[0] = NULL;
1808 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
1809 m_act.ch = -1;
1810 else
1812 m_act.ch = strtol (macro_pair[1], NULL, 0);
1813 g_free (macro_pair[1]);
1814 macro_pair[1] = NULL;
1816 if (m_act.action != 0)
1818 /* a shell command */
1819 if ((m_act.action / CK_PipeBlock (0)) == 1)
1821 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
1822 m_act.ch = -1;
1824 g_array_append_val (macros, m_act);
1825 have_macro = TRUE;
1827 g_strfreev (macro_pair);
1828 macro_pair = NULL;
1830 curr_values++;
1832 if (have_macro)
1834 macro.hotkey = hotkey;
1835 macro.macro = macros;
1836 g_array_append_val (macros_list, macro);
1838 profile_keys++;
1839 g_strfreev (values);
1841 g_strfreev (keys);
1842 mc_config_deinit (macros_config);
1843 edit_macro_sort_by_hotkey ();
1844 return TRUE;
1847 /* }}} Macro stuff end here */
1849 /* --------------------------------------------------------------------------------------------- */
1850 /** returns 1 on success */
1853 edit_save_confirm_cmd (WEdit * edit)
1855 gchar *f = NULL;
1857 if (!edit_check_newline (edit))
1858 return 0;
1860 if (edit_confirm_save)
1862 f = g_strdup_printf (_("Confirm save file: \"%s\""), edit->filename);
1863 if (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")))
1865 g_free (f);
1866 return 0;
1868 g_free (f);
1870 return edit_save_cmd (edit);
1873 /* --------------------------------------------------------------------------------------------- */
1874 /** returns 1 on success */
1877 edit_new_cmd (WEdit * edit)
1879 if (edit->modified)
1881 if (edit_query_dialog2
1882 (_("Warning"),
1884 ("Current text was modified without a file save.\nContinue discards these changes"),
1885 _("C&ontinue"), _("&Cancel")))
1887 edit->force |= REDRAW_COMPLETELY;
1888 return 0;
1891 edit->force |= REDRAW_COMPLETELY;
1893 return edit_renew (edit); /* if this gives an error, something has really screwed up */
1896 /* --------------------------------------------------------------------------------------------- */
1899 edit_load_cmd (WEdit * edit, edit_current_file_t what)
1901 char *exp;
1903 if (edit->modified
1904 && (edit_query_dialog2
1905 (_("Warning"),
1906 _("Current text was modified without a file save.\n"
1907 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")) == 1))
1909 edit->force |= REDRAW_COMPLETELY;
1910 return 0;
1913 switch (what)
1915 case EDIT_FILE_COMMON:
1916 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
1917 MC_HISTORY_EDIT_LOAD, edit->filename);
1919 if (exp)
1921 if (*exp)
1922 edit_load_file_from_filename (edit, exp);
1923 g_free (exp);
1925 break;
1927 case EDIT_FILE_SYNTAX:
1928 edit_load_syntax_file (edit);
1929 break;
1931 case EDIT_FILE_MENU:
1932 edit_load_menu_file (edit);
1933 break;
1935 default:
1936 break;
1939 edit->force |= REDRAW_COMPLETELY;
1940 return 0;
1943 /* --------------------------------------------------------------------------------------------- */
1945 if mark2 is -1 then marking is from mark1 to the cursor.
1946 Otherwise its between the markers. This handles this.
1947 Returns 1 if no text is marked.
1951 eval_marks (WEdit * edit, long *start_mark, long *end_mark)
1953 if (edit->mark1 != edit->mark2)
1955 long start_bol, start_eol;
1956 long end_bol, end_eol;
1957 long col1, col2;
1958 long diff1, diff2;
1959 long end_mark_curs;
1961 if (edit->end_mark_curs < 0)
1962 end_mark_curs = edit->curs1;
1963 else
1964 end_mark_curs = edit->end_mark_curs;
1966 if (edit->mark2 >= 0)
1968 *start_mark = min (edit->mark1, edit->mark2);
1969 *end_mark = max (edit->mark1, edit->mark2);
1971 else
1973 *start_mark = min (edit->mark1, end_mark_curs);
1974 *end_mark = max (edit->mark1, end_mark_curs);
1975 edit->column2 = edit->curs_col + edit->over_col;
1978 if (edit->column_highlight
1979 && (((edit->mark1 > end_mark_curs) && (edit->column1 < edit->column2))
1980 || ((edit->mark1 < end_mark_curs) && (edit->column1 > edit->column2))))
1982 start_bol = edit_bol (edit, *start_mark);
1983 start_eol = edit_eol (edit, start_bol - 1) + 1;
1984 end_bol = edit_bol (edit, *end_mark);
1985 end_eol = edit_eol (edit, *end_mark);
1986 col1 = min (edit->column1, edit->column2);
1987 col2 = max (edit->column1, edit->column2);
1989 diff1 =
1990 edit_move_forward3 (edit, start_bol, col2, 0) - edit_move_forward3 (edit, start_bol,
1991 col1, 0);
1992 diff2 =
1993 edit_move_forward3 (edit, end_bol, col2, 0) - edit_move_forward3 (edit, end_bol,
1994 col1, 0);
1996 *start_mark -= diff1;
1997 *end_mark += diff2;
1998 *start_mark = max (*start_mark, start_eol);
1999 *end_mark = min (*end_mark, end_eol);
2001 return 0;
2003 else
2005 *start_mark = *end_mark = 0;
2006 edit->column2 = edit->column1 = 0;
2007 return 1;
2011 /* --------------------------------------------------------------------------------------------- */
2013 void
2014 edit_insert_over (WEdit * edit)
2016 int i;
2018 for (i = 0; i < edit->over_col; i++)
2020 edit_insert (edit, ' ');
2022 edit->over_col = 0;
2025 /* --------------------------------------------------------------------------------------------- */
2028 edit_insert_column_of_text_from_file (WEdit * edit, int file,
2029 long *start_pos, long *end_pos, int *col1, int *col2)
2031 long cursor;
2032 int col;
2033 int blocklen = -1, width = 0;
2034 unsigned char *data;
2036 cursor = edit->curs1;
2037 col = edit_get_col (edit);
2038 data = g_malloc0 (TEMP_BUF_LEN);
2040 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
2042 int i;
2043 for (width = 0; width < blocklen; width++)
2045 if (data[width] == '\n')
2046 break;
2048 for (i = 0; i < blocklen; i++)
2050 if (data[i] == '\n')
2051 { /* fill in and move to next line */
2052 int l;
2053 long p;
2054 if (edit_get_byte (edit, edit->curs1) != '\n')
2056 l = width - (edit_get_col (edit) - col);
2057 while (l > 0)
2059 edit_insert (edit, ' ');
2060 l -= space_width;
2063 for (p = edit->curs1;; p++)
2065 if (p == edit->last_byte)
2067 edit_cursor_move (edit, edit->last_byte - edit->curs1);
2068 edit_insert_ahead (edit, '\n');
2069 p++;
2070 break;
2072 if (edit_get_byte (edit, p) == '\n')
2074 p++;
2075 break;
2078 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
2079 l = col - edit_get_col (edit);
2080 while (l >= space_width)
2082 edit_insert (edit, ' ');
2083 l -= space_width;
2085 continue;
2087 edit_insert (edit, data[i]);
2090 *col1 = col;
2091 *col2 = col + width;
2092 *start_pos = cursor;
2093 *end_pos = edit->curs1;
2094 edit_cursor_move (edit, cursor - edit->curs1);
2095 g_free (data);
2097 return blocklen;
2100 /* --------------------------------------------------------------------------------------------- */
2102 void
2103 edit_block_copy_cmd (WEdit * edit)
2105 long start_mark, end_mark, current = edit->curs1;
2106 long col_delta = 0;
2107 long mark1, mark2;
2108 int c1, c2;
2109 int size;
2110 unsigned char *copy_buf;
2112 edit_update_curs_col (edit);
2113 if (eval_marks (edit, &start_mark, &end_mark))
2114 return;
2116 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2118 /* all that gets pushed are deletes hence little space is used on the stack */
2120 edit_push_markers (edit);
2122 if (edit->column_highlight)
2124 col_delta = abs (edit->column2 - edit->column1);
2125 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2127 else
2129 while (size--)
2130 edit_insert_ahead (edit, copy_buf[size]);
2133 g_free (copy_buf);
2134 edit_scroll_screen_over_cursor (edit);
2136 if (edit->column_highlight)
2137 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2138 else if (start_mark < current && end_mark > current)
2139 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2141 edit->force |= REDRAW_PAGE;
2145 /* --------------------------------------------------------------------------------------------- */
2147 void
2148 edit_block_move_cmd (WEdit * edit)
2150 long current;
2151 unsigned char *copy_buf = NULL;
2152 long start_mark, end_mark;
2153 long line;
2155 if (eval_marks (edit, &start_mark, &end_mark))
2156 return;
2158 line = edit->curs_line;
2159 if (edit->mark2 < 0)
2160 edit_mark_cmd (edit, 0);
2161 edit_push_markers (edit);
2163 if (edit->column_highlight)
2165 long mark1, mark2;
2166 int size;
2167 int b_width = 0;
2168 int c1, c2;
2169 int x, x2;
2171 c1 = min (edit->column1, edit->column2);
2172 c2 = max (edit->column1, edit->column2);
2173 b_width = (c2 - c1);
2175 edit_update_curs_col (edit);
2177 x = edit->curs_col;
2178 x2 = x + edit->over_col;
2180 /* do nothing when cursor inside first line of selected area */
2181 if ((edit_eol (edit, edit->curs1) == edit_eol (edit, start_mark)) && (x2 > c1 && x2 <= c2))
2182 return;
2184 if (edit->curs1 > start_mark && edit->curs1 < edit_eol (edit, end_mark))
2186 if (x > c2)
2187 x -= b_width;
2188 else if (x > c1 && x <= c2)
2189 x = c1;
2191 /* save current selection into buffer */
2192 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2194 /* remove current selection */
2195 edit_block_delete_cmd (edit);
2197 edit->over_col = max (0, edit->over_col - b_width);
2198 /* calculate the cursor pos after delete block */
2199 current = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), x, 0);
2200 edit_cursor_move (edit, current - edit->curs1);
2201 edit_scroll_screen_over_cursor (edit);
2203 /* add TWS if need before block insertion */
2204 if (option_cursor_beyond_eol && edit->over_col > 0)
2205 edit_insert_over (edit);
2207 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2208 edit_set_markers (edit, mark1, mark2, c1, c2);
2210 else
2212 long count;
2214 current = edit->curs1;
2215 copy_buf = g_malloc0 (end_mark - start_mark);
2216 edit_cursor_move (edit, start_mark - edit->curs1);
2217 edit_scroll_screen_over_cursor (edit);
2218 count = start_mark;
2219 while (count < end_mark)
2221 copy_buf[end_mark - count - 1] = edit_delete (edit, 1);
2222 count++;
2224 edit_scroll_screen_over_cursor (edit);
2225 edit_cursor_move (edit,
2226 current - edit->curs1 -
2227 (((current - edit->curs1) > 0) ? end_mark - start_mark : 0));
2228 edit_scroll_screen_over_cursor (edit);
2229 while (count-- > start_mark)
2230 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2231 edit_set_markers (edit, edit->curs1, edit->curs1 + end_mark - start_mark, 0, 0);
2234 edit_scroll_screen_over_cursor (edit);
2235 g_free (copy_buf);
2236 edit->force |= REDRAW_PAGE;
2239 /* --------------------------------------------------------------------------------------------- */
2240 /** returns 1 if canceelled by user */
2243 edit_block_delete_cmd (WEdit * edit)
2245 long start_mark, end_mark;
2246 if (eval_marks (edit, &start_mark, &end_mark))
2248 edit_delete_line (edit);
2249 return 0;
2251 return edit_block_delete (edit);
2254 /* --------------------------------------------------------------------------------------------- */
2255 /** call with edit = 0 before shutdown to close memory leaks */
2257 void
2258 edit_replace_cmd (WEdit * edit, int again)
2260 /* 1 = search string, 2 = replace with */
2261 static char *saved1 = NULL; /* saved default[123] */
2262 static char *saved2 = NULL;
2263 char *input1 = NULL; /* user input from the dialog */
2264 char *input2 = NULL;
2265 char *disp1 = NULL;
2266 char *disp2 = NULL;
2267 long times_replaced = 0;
2268 gboolean once_found = FALSE;
2270 if (!edit)
2272 g_free (saved1), saved1 = NULL;
2273 g_free (saved2), saved2 = NULL;
2274 return;
2277 edit->force |= REDRAW_COMPLETELY;
2279 if (again && !saved1 && !saved2)
2280 again = 0;
2282 if (again)
2284 input1 = g_strdup (saved1 ? saved1 : "");
2285 input2 = g_strdup (saved2 ? saved2 : "");
2287 else
2289 char *tmp_inp1, *tmp_inp2;
2290 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
2291 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
2293 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2295 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2297 g_free (disp1);
2298 g_free (disp2);
2300 if (input1 == NULL || *input1 == '\0')
2302 edit->force = REDRAW_COMPLETELY;
2303 goto cleanup;
2306 tmp_inp1 = input1;
2307 tmp_inp2 = input2;
2308 input1 = edit_replace_cmd__conv_to_input (input1);
2309 input2 = edit_replace_cmd__conv_to_input (input2);
2310 g_free (tmp_inp1);
2311 g_free (tmp_inp2);
2313 g_free (saved1), saved1 = g_strdup (input1);
2314 g_free (saved2), saved2 = g_strdup (input2);
2316 mc_search_free (edit->search);
2317 edit->search = NULL;
2320 if (!edit->search)
2322 edit->search = mc_search_new (input1, -1);
2323 if (edit->search == NULL)
2325 edit->search_start = edit->curs1;
2326 goto cleanup;
2328 edit->search->search_type = edit_search_options.type;
2329 edit->search->is_all_charsets = edit_search_options.all_codepages;
2330 edit->search->is_case_sensitive = edit_search_options.case_sens;
2331 edit->search->whole_words = edit_search_options.whole_words;
2332 edit->search->search_fn = edit_search_cmd_callback;
2335 if (edit->found_len && edit->search_start == edit->found_start + 1
2336 && edit_search_options.backwards)
2337 edit->search_start--;
2339 if (edit->found_len && edit->search_start == edit->found_start - 1
2340 && !edit_search_options.backwards)
2341 edit->search_start++;
2345 gsize len = 0;
2347 if (!editcmd_find (edit, &len))
2349 if (!(edit->search->error == MC_SEARCH_E_OK ||
2350 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2352 edit_error_dialog (_("Search"), edit->search->error_str);
2354 break;
2356 once_found = TRUE;
2358 edit->search_start = edit->search->normal_offset;
2359 /*returns negative on not found or error in pattern */
2361 if ((edit->search_start >= 0) && (edit->search_start < edit->last_byte))
2363 gsize i;
2364 GString *tmp_str, *repl_str;
2366 edit->found_start = edit->search_start;
2367 i = edit->found_len = len;
2369 edit_cursor_move (edit, edit->search_start - edit->curs1);
2370 edit_scroll_screen_over_cursor (edit);
2372 if (edit->replace_mode == 0)
2374 int l;
2375 int prompt;
2377 l = edit->curs_row - edit->widget.lines / 3;
2378 if (l > 0)
2379 edit_scroll_downward (edit, l);
2380 if (l < 0)
2381 edit_scroll_upward (edit, -l);
2383 edit_scroll_screen_over_cursor (edit);
2384 edit->force |= REDRAW_PAGE;
2385 edit_render_keypress (edit);
2387 /*so that undo stops at each query */
2388 edit_push_key_press (edit);
2389 /* and prompt 2/3 down */
2390 disp1 = edit_replace_cmd__conv_to_display (saved1);
2391 disp2 = edit_replace_cmd__conv_to_display (saved2);
2392 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2393 g_free (disp1);
2394 g_free (disp2);
2396 if (prompt == B_REPLACE_ALL)
2397 edit->replace_mode = 1;
2398 else if (prompt == B_SKIP_REPLACE)
2400 if (edit_search_options.backwards)
2401 edit->search_start--;
2402 else
2403 edit->search_start++;
2404 continue; /* loop */
2406 else if (prompt == B_CANCEL)
2408 edit->replace_mode = -1;
2409 break; /* loop */
2413 /* don't process string each time */
2414 tmp_str = g_string_new (input2);
2415 repl_str = mc_search_prepare_replace_str (edit->search, tmp_str);
2416 g_string_free (tmp_str, TRUE);
2418 if (edit->search->error != MC_SEARCH_E_OK)
2420 edit_error_dialog (_("Replace"), edit->search->error_str);
2421 g_string_free (repl_str, TRUE);
2422 break;
2425 /* delete then insert new */
2426 for (i = 0; i < len; i++)
2427 edit_delete (edit, 1);
2429 for (i = 0; i < repl_str->len; i++)
2430 edit_insert (edit, repl_str->str[i]);
2432 edit->found_len = repl_str->len;
2433 g_string_free (repl_str, TRUE);
2434 times_replaced++;
2436 /* so that we don't find the same string again */
2437 if (edit_search_options.backwards)
2438 edit->search_start--;
2439 else
2441 edit->search_start += edit->found_len;
2443 if (edit->search_start >= edit->last_byte)
2444 break;
2447 edit_scroll_screen_over_cursor (edit);
2449 else
2451 /* try and find from right here for next search */
2452 edit->search_start = edit->curs1;
2453 edit_update_curs_col (edit);
2455 edit->force |= REDRAW_PAGE;
2456 edit_render_keypress (edit);
2458 if (times_replaced == 0)
2459 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL, 1, _("&OK"));
2460 break;
2463 while (edit->replace_mode >= 0);
2465 edit_scroll_screen_over_cursor (edit);
2466 edit->force |= REDRAW_COMPLETELY;
2467 edit_render_keypress (edit);
2469 if ((edit->replace_mode == 1) && (times_replaced != 0))
2470 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2472 cleanup:
2473 g_free (input1);
2474 g_free (input2);
2477 /* --------------------------------------------------------------------------------------------- */
2480 edit_search_cmd_callback (const void *user_data, gsize char_offset)
2482 return edit_get_byte ((WEdit *) user_data, (long) char_offset);
2485 /* --------------------------------------------------------------------------------------------- */
2487 void
2488 edit_search_cmd (WEdit * edit, gboolean again)
2490 if (edit == NULL)
2491 return;
2493 if (!again)
2494 edit_search (edit);
2495 else if (edit->last_search_string != NULL)
2496 edit_do_search (edit);
2497 else
2499 /* find last search string in history */
2500 GList *history;
2502 history = history_get (MC_HISTORY_SHARED_SEARCH);
2503 if (history != NULL && history->data != NULL)
2505 edit->last_search_string = (char *) history->data;
2506 history->data = NULL;
2507 history = g_list_first (history);
2508 g_list_foreach (history, (GFunc) g_free, NULL);
2509 g_list_free (history);
2511 edit->search = mc_search_new (edit->last_search_string, -1);
2512 if (edit->search == NULL)
2514 /* if not... then ask for an expression */
2515 g_free (edit->last_search_string);
2516 edit->last_search_string = NULL;
2517 edit_search (edit);
2519 else
2521 edit->search->search_type = edit_search_options.type;
2522 edit->search->is_all_charsets = edit_search_options.all_codepages;
2523 edit->search->is_case_sensitive = edit_search_options.case_sens;
2524 edit->search->whole_words = edit_search_options.whole_words;
2525 edit->search->search_fn = edit_search_cmd_callback;
2526 edit_do_search (edit);
2529 else
2531 /* if not... then ask for an expression */
2532 g_free (edit->last_search_string);
2533 edit->last_search_string = NULL;
2534 edit_search (edit);
2540 /* --------------------------------------------------------------------------------------------- */
2542 * Check if it's OK to close the editor. If there are unsaved changes,
2543 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2546 gboolean
2547 edit_ok_to_exit (WEdit * edit)
2549 int act;
2551 if (!edit->modified)
2552 return TRUE;
2554 if (!mc_global.midnight_shutdown)
2556 if (!edit_check_newline (edit))
2557 return FALSE;
2559 query_set_sel (2);
2560 act = edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2561 _("&Yes"), _("&No"), _("&Cancel quit"));
2563 else
2565 act =
2566 edit_query_dialog2 (_("Quit"),
2567 _("Midnight Commander is being shut down.\nSave modified file?"),
2568 _("&Yes"), _("&No"));
2570 /* Esc is No */
2571 if (act == -1)
2572 act = 1;
2575 switch (act)
2577 case 0: /* Yes */
2578 edit_push_markers (edit);
2579 edit_set_markers (edit, 0, 0, 0, 0);
2580 if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2581 return mc_global.midnight_shutdown;
2582 break;
2583 case 1: /* No */
2584 break;
2585 case 2: /* Cancel quit */
2586 case -1: /* Esc */
2587 return FALSE;
2590 return TRUE;
2593 /* --------------------------------------------------------------------------------------------- */
2594 /** save block, returns 1 on success */
2597 edit_save_block (WEdit * edit, const char *filename, long start, long finish)
2599 int len, file;
2601 file = mc_open (filename, O_CREAT | O_WRONLY | O_TRUNC,
2602 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2603 if (file == -1)
2604 return 0;
2606 if (edit->column_highlight)
2608 int r;
2609 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2610 if (r > 0)
2612 unsigned char *block, *p;
2613 p = block = edit_get_block (edit, start, finish, &len);
2614 while (len)
2616 r = mc_write (file, p, len);
2617 if (r < 0)
2618 break;
2619 p += r;
2620 len -= r;
2622 g_free (block);
2625 else
2627 unsigned char *buf;
2628 int i = start, end;
2629 len = finish - start;
2630 buf = g_malloc0 (TEMP_BUF_LEN);
2631 while (start != finish)
2633 end = min (finish, start + TEMP_BUF_LEN);
2634 for (; i < end; i++)
2635 buf[i - start] = edit_get_byte (edit, i);
2636 len -= mc_write (file, (char *) buf, end - start);
2637 start = end;
2639 g_free (buf);
2641 mc_close (file);
2642 if (len)
2643 return 0;
2644 return 1;
2647 /* --------------------------------------------------------------------------------------------- */
2649 void
2650 edit_paste_from_history (WEdit * edit)
2652 (void) edit;
2653 edit_error_dialog (_("Error"), _("This function is not implemented"));
2656 /* --------------------------------------------------------------------------------------------- */
2659 edit_copy_to_X_buf_cmd (WEdit * edit)
2661 long start_mark, end_mark;
2662 if (eval_marks (edit, &start_mark, &end_mark))
2663 return 0;
2664 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2666 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2667 return 1;
2669 /* try use external clipboard utility */
2670 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2672 return 0;
2675 /* --------------------------------------------------------------------------------------------- */
2678 edit_cut_to_X_buf_cmd (WEdit * edit)
2680 long start_mark, end_mark;
2681 if (eval_marks (edit, &start_mark, &end_mark))
2682 return 0;
2683 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2685 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2686 return 1;
2688 /* try use external clipboard utility */
2689 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2691 edit_block_delete_cmd (edit);
2692 edit_mark_cmd (edit, 1);
2693 return 0;
2696 /* --------------------------------------------------------------------------------------------- */
2698 void
2699 edit_paste_from_X_buf_cmd (WEdit * edit)
2701 gchar *tmp;
2702 /* try use external clipboard utility */
2703 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
2704 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
2705 edit_insert_file (edit, tmp);
2706 g_free (tmp);
2710 /* --------------------------------------------------------------------------------------------- */
2712 * Ask user for the line and go to that line.
2713 * Negative numbers mean line from the end (i.e. -1 is the last line).
2716 void
2717 edit_goto_cmd (WEdit * edit)
2719 char *f;
2720 static long line = 0; /* line as typed, saved as default */
2721 long l;
2722 char *error;
2723 char s[32];
2725 g_snprintf (s, sizeof (s), "%ld", line);
2726 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE, line ? s : "");
2727 if (!f)
2728 return;
2730 if (!*f)
2732 g_free (f);
2733 return;
2736 l = strtol (f, &error, 0);
2737 if (*error)
2739 g_free (f);
2740 return;
2743 line = l;
2744 if (l < 0)
2745 l = edit->total_lines + l + 2;
2746 edit_move_display (edit, l - edit->widget.lines / 2 - 1);
2747 edit_move_to_line (edit, l - 1);
2748 edit->force |= REDRAW_COMPLETELY;
2749 g_free (f);
2753 /* --------------------------------------------------------------------------------------------- */
2754 /** Return 1 on success */
2757 edit_save_block_cmd (WEdit * edit)
2759 long start_mark, end_mark;
2760 char *exp, *tmp;
2762 if (eval_marks (edit, &start_mark, &end_mark))
2763 return 1;
2765 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
2766 exp =
2767 input_expand_dialog (_("Save block"), _("Enter file name:"),
2768 MC_HISTORY_EDIT_SAVE_BLOCK, tmp);
2769 g_free (tmp);
2770 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2771 if (exp)
2773 if (!*exp)
2775 g_free (exp);
2776 return 0;
2778 else
2780 if (edit_save_block (edit, exp, start_mark, end_mark))
2782 g_free (exp);
2783 edit->force |= REDRAW_COMPLETELY;
2784 return 1;
2786 else
2788 g_free (exp);
2789 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
2793 edit->force |= REDRAW_COMPLETELY;
2794 return 0;
2798 /* --------------------------------------------------------------------------------------------- */
2799 /** returns TRUE on success */
2801 gboolean
2802 edit_insert_file_cmd (WEdit * edit)
2804 gchar *tmp;
2805 char *exp;
2806 gboolean ret = FALSE;
2808 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
2809 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
2810 MC_HISTORY_EDIT_INSERT_FILE, tmp);
2811 g_free (tmp);
2813 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2815 if (exp != NULL && *exp != '\0')
2817 ret = (edit_insert_file (edit, exp) >= 0);
2818 if (!ret)
2819 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
2822 g_free (exp);
2824 edit->force |= REDRAW_COMPLETELY;
2825 return ret;
2828 /* --------------------------------------------------------------------------------------------- */
2829 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
2832 edit_sort_cmd (WEdit * edit)
2834 static char *old = 0;
2835 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
2836 long start_mark, end_mark;
2837 int e;
2839 if (eval_marks (edit, &start_mark, &end_mark))
2841 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
2842 return 0;
2845 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
2846 edit_save_block (edit, tmp, start_mark, end_mark);
2847 g_free (tmp);
2849 exp = input_dialog (_("Run sort"),
2850 _("Enter sort options (see manpage) separated by whitespace:"),
2851 MC_HISTORY_EDIT_SORT, (old != NULL) ? old : "");
2853 if (!exp)
2854 return 1;
2855 g_free (old);
2856 old = exp;
2857 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
2858 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
2859 tmp =
2860 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
2861 " > ", tmp_edit_temp_name, (char *) NULL);
2862 g_free (tmp_edit_temp_name);
2863 g_free (tmp_edit_block_name);
2865 e = system (tmp);
2866 g_free (tmp);
2867 if (e)
2869 if (e == -1 || e == 127)
2871 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
2873 else
2875 char q[8];
2876 sprintf (q, "%d ", e);
2877 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
2878 edit_error_dialog (_("Sort"), tmp);
2879 g_free (tmp);
2881 return -1;
2884 edit->force |= REDRAW_COMPLETELY;
2886 if (edit_block_delete_cmd (edit))
2887 return 1;
2888 tmp = mc_config_get_full_path (EDIT_TEMP_FILE);
2889 edit_insert_file (edit, tmp);
2890 g_free (tmp);
2891 return 0;
2894 /* --------------------------------------------------------------------------------------------- */
2896 * Ask user for a command, execute it and paste its output back to the
2897 * editor.
2901 edit_ext_cmd (WEdit * edit)
2903 char *exp, *tmp, *tmp_edit_temp_file;
2904 int e;
2906 exp =
2907 input_dialog (_("Paste output of external command"),
2908 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, NULL);
2910 if (!exp)
2911 return 1;
2913 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
2914 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
2915 g_free (tmp_edit_temp_file);
2916 e = system (tmp);
2917 g_free (tmp);
2918 g_free (exp);
2920 if (e)
2922 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
2923 return -1;
2926 edit->force |= REDRAW_COMPLETELY;
2927 tmp = mc_config_get_full_path (EDIT_TEMP_FILE);
2928 edit_insert_file (edit, tmp);
2929 g_free (tmp);
2930 return 0;
2933 /* --------------------------------------------------------------------------------------------- */
2934 /** if block is 1, a block must be highlighted and the shell command
2935 processes it. If block is 0 the shell command is a straight system
2936 command, that just produces some output which is to be inserted */
2938 void
2939 edit_block_process_cmd (WEdit * edit, int macro_number)
2941 char *fname;
2942 char *macros_fname = NULL;
2944 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
2945 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
2946 user_menu (edit, macros_fname, 0);
2947 g_free (fname);
2948 g_free (macros_fname);
2949 edit->force |= REDRAW_COMPLETELY;
2952 /* --------------------------------------------------------------------------------------------- */
2954 void
2955 edit_mail_dialog (WEdit * edit)
2957 char *tmail_to;
2958 char *tmail_subject;
2959 char *tmail_cc;
2961 static char *mail_cc_last = 0;
2962 static char *mail_subject_last = 0;
2963 static char *mail_to_last = 0;
2965 QuickWidget quick_widgets[] = {
2966 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
2967 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
2968 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input", &tmail_cc),
2969 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT, N_("Copies to")),
2970 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-2",
2971 &tmail_subject),
2972 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT, N_("Subject")),
2973 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-3", &tmail_to),
2974 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT, N_("To")),
2975 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT, N_("mail -s <subject> -c <cc> <to>")),
2976 QUICK_END
2979 QuickDialog Quick_input = {
2980 50, MAIL_DLG_HEIGHT, -1, -1, N_("Mail"),
2981 "[Input Line Keys]", quick_widgets, NULL, FALSE
2984 quick_widgets[2].u.input.text = mail_cc_last ? mail_cc_last : "";
2985 quick_widgets[4].u.input.text = mail_subject_last ? mail_subject_last : "";
2986 quick_widgets[6].u.input.text = mail_to_last ? mail_to_last : "";
2988 if (quick_dialog (&Quick_input) != B_CANCEL)
2990 g_free (mail_cc_last);
2991 g_free (mail_subject_last);
2992 g_free (mail_to_last);
2993 mail_cc_last = tmail_cc;
2994 mail_subject_last = tmail_subject;
2995 mail_to_last = tmail_to;
2996 pipe_mail (edit, mail_to_last, mail_subject_last, mail_cc_last);
3001 /*******************/
3002 /* Word Completion */
3003 /*******************/
3005 /* --------------------------------------------------------------------------------------------- */
3007 * Complete current word using regular expression search
3008 * backwards beginning at the current cursor position.
3011 void
3012 edit_complete_word_cmd (WEdit * edit)
3014 gsize i, max_len, word_len = 0, num_compl = 0;
3015 long word_start = 0;
3016 unsigned char *bufpos;
3017 char *match_expr;
3018 struct selection compl[MAX_WORD_COMPLETIONS]; /* completions */
3020 /* search start of word to be completed */
3021 if (!edit_find_word_start (edit, &word_start, &word_len))
3022 return;
3024 /* prepare match expression */
3025 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3027 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3028 match_expr =
3029 g_strdup_printf
3030 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3031 (int) word_len, bufpos);
3033 /* collect the possible completions */
3034 /* start search from begin to end of file */
3035 max_len =
3036 edit_collect_completions (edit, word_start, word_len, match_expr,
3037 (struct selection *) &compl, &num_compl);
3039 if (num_compl > 0)
3041 /* insert completed word if there is only one match */
3042 if (num_compl == 1)
3044 for (i = word_len; i < compl[0].len; i++)
3045 edit_insert (edit, *(compl[0].text + i));
3047 /* more than one possible completion => ask the user */
3048 else
3050 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3051 /* !!! pressed again the selection dialog pops up, but that !!! */
3052 /* !!! seems to require a further internal state !!! */
3053 /*tty_beep (); */
3055 /* let the user select the preferred completion */
3056 editcmd_dialog_completion_show (edit, max_len, word_len,
3057 (struct selection *) &compl, num_compl);
3061 g_free (match_expr);
3062 /* release memory before return */
3063 for (i = 0; i < num_compl; i++)
3064 g_free (compl[i].text);
3067 /* --------------------------------------------------------------------------------------------- */
3069 void
3070 edit_select_codepage_cmd (WEdit * edit)
3072 #ifdef HAVE_CHARSET
3073 if (do_select_codepage ())
3074 edit_set_codeset (edit);
3076 edit->force = REDRAW_COMPLETELY;
3077 edit_refresh_cmd (edit);
3078 #else
3079 (void) edit;
3080 #endif
3083 /* --------------------------------------------------------------------------------------------- */
3085 void
3086 edit_insert_literal_cmd (WEdit * edit)
3088 int char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3089 _("Press any key:"), 0);
3090 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3093 /* --------------------------------------------------------------------------------------------- */
3095 void
3096 edit_begin_end_macro_cmd (WEdit * edit)
3098 /* edit is a pointer to the widget */
3099 if (edit != NULL)
3101 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3102 edit_execute_key_command (edit, command, -1);
3106 /* --------------------------------------------------------------------------------------------- */
3108 void
3109 edit_begin_end_repeat_cmd (WEdit * edit)
3111 /* edit is a pointer to the widget */
3112 if (edit != NULL)
3114 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3115 edit_execute_key_command (edit, command, -1);
3119 /* --------------------------------------------------------------------------------------------- */
3122 edit_load_forward_cmd (WEdit * edit)
3124 if (edit->modified)
3126 if (edit_query_dialog2
3127 (_("Warning"),
3128 _("Current text was modified without a file save\n"
3129 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3131 edit->force |= REDRAW_COMPLETELY;
3132 return 0;
3135 if (edit_stack_iterator + 1 < MAX_HISTORY_MOVETO)
3137 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3139 return 1;
3141 edit_stack_iterator++;
3142 if (edit_history_moveto[edit_stack_iterator].filename)
3144 edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename,
3145 edit_history_moveto[edit_stack_iterator].line);
3146 return 0;
3148 else
3150 return 1;
3153 else
3155 return 1;
3159 /* --------------------------------------------------------------------------------------------- */
3162 edit_load_back_cmd (WEdit * edit)
3164 if (edit->modified)
3166 if (edit_query_dialog2
3167 (_("Warning"),
3168 _("Current text was modified without a file save\n"
3169 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3171 edit->force |= REDRAW_COMPLETELY;
3172 return 0;
3175 if (edit_stack_iterator > 0)
3177 edit_stack_iterator--;
3178 if (edit_history_moveto[edit_stack_iterator].filename)
3180 edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename,
3181 edit_history_moveto[edit_stack_iterator].line);
3182 return 0;
3184 else
3186 return 1;
3189 else
3191 return 1;
3195 /* --------------------------------------------------------------------------------------------- */
3197 void
3198 edit_get_match_keyword_cmd (WEdit * edit)
3200 gsize word_len = 0, max_len = 0;
3201 int num_def = 0;
3202 int i;
3203 long word_start = 0;
3204 unsigned char *bufpos;
3205 char *match_expr;
3206 char *path = NULL;
3207 char *ptr = NULL;
3208 char *tagfile = NULL;
3210 etags_hash_t def_hash[MAX_DEFINITIONS];
3212 for (i = 0; i < MAX_DEFINITIONS; i++)
3214 def_hash[i].filename = NULL;
3217 /* search start of word to be completed */
3218 if (!edit_find_word_start (edit, &word_start, &word_len))
3219 return;
3221 /* prepare match expression */
3222 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3223 match_expr = g_strdup_printf ("%.*s", (int) word_len, bufpos);
3225 ptr = g_get_current_dir ();
3226 path = g_strconcat (ptr, G_DIR_SEPARATOR_S, (char *) NULL);
3227 g_free (ptr);
3229 /* Recursive search file 'TAGS' in parent dirs */
3232 ptr = g_path_get_dirname (path);
3233 g_free (path);
3234 path = ptr;
3235 g_free (tagfile);
3236 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3237 if (exist_file (tagfile))
3238 break;
3240 while (strcmp (path, G_DIR_SEPARATOR_S) != 0);
3242 if (tagfile)
3244 num_def =
3245 etags_set_definition_hash (tagfile, path, match_expr, (etags_hash_t *) & def_hash);
3246 g_free (tagfile);
3248 g_free (path);
3250 max_len = MAX_WIDTH_DEF_DIALOG;
3251 word_len = 0;
3252 if (num_def > 0)
3254 editcmd_dialog_select_definition_show (edit, match_expr, max_len, word_len,
3255 (etags_hash_t *) & def_hash, num_def);
3257 g_free (match_expr);
3260 /* --------------------------------------------------------------------------------------------- */