Merge branch '2991_label_redraw'
[midnight-commander.git] / src / editor / editcmd.c
blob8affddad8be499eb7c2245cb9be005d7a694761d
1 /*
2 Editor high level editing commands
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2011, 2012, 2013
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer, 1996, 1997
10 Andrew Borodin <aborodin@vmail.ru>, 2012, 2013
11 Ilia Maslakov <il.smind@gmail.com>, 2012
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /** \file
30 * \brief Source: editor high level editing commands
31 * \author Paul Sheer
32 * \date 1996, 1997
35 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
37 #include <config.h>
39 #ifdef HAVE_ASSERT_H
40 #include <assert.h>
41 #endif
42 #include <ctype.h>
44 #include <stdio.h>
45 #include <stdarg.h>
46 #include <sys/types.h>
47 #include <unistd.h>
48 #include <string.h>
49 #include <errno.h>
50 #include <sys/stat.h>
51 #include <stdlib.h>
52 #include <fcntl.h>
54 #include "lib/global.h"
55 #include "lib/tty/tty.h"
56 #include "lib/tty/key.h" /* XCTRL */
57 #include "lib/mcconfig.h"
58 #include "lib/skin.h"
59 #include "lib/strutil.h" /* utf string functions */
60 #include "lib/lock.h"
61 #include "lib/util.h" /* tilde_expand() */
62 #include "lib/vfs/vfs.h"
63 #include "lib/widget.h"
64 #include "lib/event.h" /* mc_event_raise() */
65 #ifdef HAVE_CHARSET
66 #include "lib/charsets.h"
67 #endif
69 #include "src/history.h"
70 #include "src/setup.h" /* option_tab_spacing */
71 #ifdef HAVE_CHARSET
72 #include "src/selcodepage.h"
73 #endif
74 #include "src/keybind-defaults.h"
75 #include "src/util.h" /* check_for_default() */
76 #include "src/filemanager/layout.h" /* mc_refresh() */
78 #include "edit-impl.h"
79 #include "editwidget.h"
80 #include "editcmd_dialogs.h"
81 #ifdef HAVE_ASPELL
82 #include "spell.h"
83 #include "spell_dialogs.h"
84 #endif
85 #include "etags.h"
87 /*** global variables ****************************************************************************/
89 /* search and replace: */
90 int search_create_bookmark = FALSE;
92 /* queries on a save */
93 int edit_confirm_save = 1;
95 /*** file scope macro definitions ****************************************************************/
97 #define space_width 1
99 #define TEMP_BUF_LEN 1024
101 #define INPUT_INDEX 9
103 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
104 (and the above) routines to work properly - paul */
106 #define is_digit(x) ((x) >= '0' && (x) <= '9')
108 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
110 /*** file scope type declarations ****************************************************************/
112 /*** file scope variables ************************************************************************/
114 static unsigned long edit_save_mode_radio_id, edit_save_mode_input_id;
116 /* --------------------------------------------------------------------------------------------- */
117 /*** file scope functions ************************************************************************/
118 /* --------------------------------------------------------------------------------------------- */
120 static cb_ret_t
121 edit_save_mode_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
123 switch (msg)
125 case MSG_ACTION:
126 if (sender != NULL && sender->id == edit_save_mode_radio_id)
128 Widget *ww;
130 ww = dlg_find_by_id (DIALOG (w), edit_save_mode_input_id);
131 widget_disable (ww, RADIO (sender)->sel != 2);
132 return MSG_HANDLED;
135 return MSG_NOT_HANDLED;
137 default:
138 return dlg_default_callback (w, sender, msg, parm, data);
142 /* --------------------------------------------------------------------------------------------- */
144 /* If 0 (quick save) then a) create/truncate <filename> file,
145 b) save to <filename>;
146 if 1 (safe save) then a) save to <tempnam>,
147 b) rename <tempnam> to <filename>;
148 if 2 (do backups) then a) save to <tempnam>,
149 b) rename <filename> to <filename.backup_ext>,
150 c) rename <tempnam> to <filename>. */
152 /* returns 0 on error, -1 on abort */
154 static int
155 edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath)
157 char *p;
158 gchar *tmp;
159 off_t filelen = 0;
160 int this_save_mode, fd = -1;
161 vfs_path_t *real_filename_vpath;
162 vfs_path_t *savename_vpath = NULL;
163 const char *start_filename;
164 const vfs_path_element_t *vpath_element;
166 vpath_element = vfs_path_get_by_index (filename_vpath, 0);
167 if (vpath_element == NULL)
168 return 0;
170 start_filename = vpath_element->path;
171 if (*start_filename == '\0')
172 return 0;
174 if (*start_filename != PATH_SEP && edit->dir_vpath != NULL)
176 real_filename_vpath = vfs_path_append_vpath_new (edit->dir_vpath, filename_vpath, NULL);
178 else
180 real_filename_vpath = vfs_path_clone (filename_vpath);
183 this_save_mode = option_save_mode;
184 if (this_save_mode != EDIT_QUICK_SAVE)
186 if (!vfs_file_is_local (real_filename_vpath)
187 || (fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY)) == -1)
190 * The file does not exists yet, so no safe save or
191 * backup are necessary.
193 this_save_mode = EDIT_QUICK_SAVE;
195 if (fd != -1)
196 mc_close (fd);
199 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt)
201 int rv;
202 struct stat sb;
204 rv = mc_stat (real_filename_vpath, &sb);
205 if (rv == 0 && sb.st_nlink > 1)
207 rv = edit_query_dialog3 (_("Warning"),
208 _("File has hard-links. Detach before saving?"),
209 _("&Yes"), _("&No"), _("&Cancel"));
210 switch (rv)
212 case 0:
213 this_save_mode = EDIT_SAFE_SAVE;
214 /* fallthrough */
215 case 1:
216 edit->skip_detach_prompt = 1;
217 break;
218 default:
219 vfs_path_free (real_filename_vpath);
220 return -1;
224 /* Prevent overwriting changes from other editor sessions. */
225 if (rv == 0 && edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
227 /* The default action is "Cancel". */
228 query_set_sel (1);
230 rv = edit_query_dialog2 (_("Warning"),
231 _("The file has been modified in the meantime. Save anyway?"),
232 _("&Yes"), _("&Cancel"));
233 if (rv != 0)
235 vfs_path_free (real_filename_vpath);
236 return -1;
241 if (this_save_mode != EDIT_QUICK_SAVE)
243 char *savedir, *saveprefix;
245 savedir = vfs_path_tokens_get (real_filename_vpath, 0, -1);
246 if (savedir == NULL)
247 savedir = g_strdup (".");
249 /* Token-related function never return leading slash, so we need add it manually */
250 saveprefix = mc_build_filename ("/", savedir, "cooledit", NULL);
251 g_free (savedir);
252 fd = mc_mkstemps (&savename_vpath, saveprefix, NULL);
253 g_free (saveprefix);
254 if (savename_vpath == NULL)
256 vfs_path_free (real_filename_vpath);
257 return 0;
259 /* FIXME:
260 * Close for now because mc_mkstemps use pure open system call
261 * to create temporary file and it needs to be reopened by
262 * VFS-aware mc_open().
264 close (fd);
266 else
267 savename_vpath = vfs_path_clone (real_filename_vpath);
269 (void) mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
270 (void) mc_chmod (savename_vpath, edit->stat1.st_mode);
272 fd = mc_open (savename_vpath, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
273 if (fd == -1)
274 goto error_save;
276 /* pipe save */
277 p = edit_get_write_filter (savename_vpath, real_filename_vpath);
278 if (p != NULL)
280 FILE *file;
282 mc_close (fd);
283 file = (FILE *) popen (p, "w");
285 if (file)
287 filelen = edit_write_stream (edit, file);
288 #if 1
289 pclose (file);
290 #else
291 if (pclose (file) != 0)
293 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
294 edit_error_dialog (_("Error"), tmp);
295 g_free (tmp);
296 g_free (p);
297 goto error_save;
299 #endif
301 else
303 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
304 edit_error_dialog (_("Error"), get_sys_error (tmp));
305 g_free (p);
306 g_free (tmp);
307 goto error_save;
309 g_free (p);
311 else if (edit->lb == LB_ASIS)
312 { /* do not change line breaks */
313 off_t buf;
314 buf = 0;
315 filelen = edit->last_byte;
316 while (buf <= (edit->curs1 >> S_EDIT_BUF_SIZE) - 1)
318 if (mc_write (fd, (char *) edit->buffers1[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
320 mc_close (fd);
321 goto error_save;
323 buf++;
325 if (mc_write
326 (fd, (char *) edit->buffers1[buf],
327 edit->curs1 & M_EDIT_BUF_SIZE) != (edit->curs1 & M_EDIT_BUF_SIZE))
329 filelen = -1;
331 else if (edit->curs2)
333 edit->curs2--;
334 buf = (edit->curs2 >> S_EDIT_BUF_SIZE);
335 if (mc_write
336 (fd,
337 (char *) edit->buffers2[buf] + EDIT_BUF_SIZE -
338 (edit->curs2 & M_EDIT_BUF_SIZE) - 1,
339 1 + (edit->curs2 & M_EDIT_BUF_SIZE)) != 1 + (edit->curs2 & M_EDIT_BUF_SIZE))
341 filelen = -1;
343 else
345 while (--buf >= 0)
347 if (mc_write (fd, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
349 filelen = -1;
350 break;
354 edit->curs2++;
356 if (mc_close (fd))
357 goto error_save;
359 /* Update the file information, especially the mtime. */
360 if (mc_stat (savename_vpath, &edit->stat1) == -1)
361 goto error_save;
363 else
364 { /* change line breaks */
365 FILE *file;
366 const vfs_path_element_t *path_element;
368 mc_close (fd);
370 path_element = vfs_path_get_by_index (savename_vpath, -1);
371 file = (FILE *) fopen (path_element->path, "w");
372 if (file != NULL)
374 filelen = edit_write_stream (edit, file);
375 fclose (file);
377 else
379 char *msg;
381 msg = g_strdup_printf (_("Cannot open file for writing: %s"), path_element->path);
382 edit_error_dialog (_("Error"), msg);
383 g_free (msg);
384 goto error_save;
388 if (filelen != edit->last_byte)
389 goto error_save;
391 if (this_save_mode == EDIT_DO_BACKUP)
393 char *tmp_store_filename;
394 vfs_path_element_t *last_vpath_element;
395 vfs_path_t *tmp_vpath;
396 gboolean ok;
398 #ifdef HAVE_ASSERT_H
399 assert (option_backup_ext != NULL);
400 #endif
401 /* add backup extention to the path */
402 tmp_vpath = vfs_path_clone (real_filename_vpath);
403 last_vpath_element = (vfs_path_element_t *) vfs_path_get_by_index (tmp_vpath, -1);
404 tmp_store_filename = last_vpath_element->path;
405 last_vpath_element->path = g_strdup_printf ("%s%s", tmp_store_filename, option_backup_ext);
406 g_free (tmp_store_filename);
408 ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
409 vfs_path_free (tmp_vpath);
410 if (!ok)
411 goto error_save;
414 if (this_save_mode != EDIT_QUICK_SAVE)
415 if (mc_rename (savename_vpath, real_filename_vpath) == -1)
416 goto error_save;
418 vfs_path_free (real_filename_vpath);
419 vfs_path_free (savename_vpath);
420 return 1;
421 error_save:
422 /* FIXME: Is this safe ?
423 * if (this_save_mode != EDIT_QUICK_SAVE)
424 * mc_unlink (savename);
426 vfs_path_free (real_filename_vpath);
427 vfs_path_free (savename_vpath);
428 return 0;
431 /* --------------------------------------------------------------------------------------------- */
433 static gboolean
434 edit_check_newline (WEdit * edit)
436 return !(option_check_nl_at_eof && edit->last_byte > 0
437 && edit_get_byte (edit, edit->last_byte - 1) != '\n'
438 && edit_query_dialog2 (_("Warning"),
439 _("The file you are saving is not finished with a newline"),
440 _("C&ontinue"), _("&Cancel")));
443 /* --------------------------------------------------------------------------------------------- */
445 static vfs_path_t *
446 edit_get_save_file_as (WEdit * edit)
448 static LineBreaks cur_lb = LB_ASIS;
449 char *filename;
450 char *filename_res;
451 vfs_path_t *ret_vpath = NULL;
453 const char *lb_names[LB_NAMES] = {
454 N_("&Do not change"),
455 N_("&Unix format (LF)"),
456 N_("&Windows/DOS format (CR LF)"),
457 N_("&Macintosh format (CR)")
460 filename = vfs_path_to_str (edit->filename_vpath);
463 quick_widget_t quick_widgets[] = {
464 /* *INDENT-OFF* */
465 QUICK_LABELED_INPUT (N_("Enter file name:"), input_label_above, filename, "save-as",
466 &filename_res, NULL, FALSE, FALSE, INPUT_COMPLETE_FILENAMES),
467 QUICK_SEPARATOR (TRUE),
468 QUICK_LABEL (N_("Change line breaks to:"), NULL),
469 QUICK_RADIO (LB_NAMES, lb_names, (int *) &cur_lb, NULL),
470 QUICK_BUTTONS_OK_CANCEL,
471 QUICK_END
472 /* *INDENT-ON* */
475 quick_dialog_t qdlg = {
476 -1, -1, 64,
477 N_("Save As"), "[Save File As]",
478 quick_widgets, NULL, NULL
481 if (quick_dialog (&qdlg) != B_CANCEL)
483 char *fname;
485 edit->lb = cur_lb;
486 fname = tilde_expand (filename_res);
487 g_free (filename_res);
488 ret_vpath = vfs_path_from_str (fname);
489 g_free (fname);
493 g_free (filename);
495 return ret_vpath;
498 /* --------------------------------------------------------------------------------------------- */
500 /** returns TRUE on success */
502 static gboolean
503 edit_save_cmd (WEdit * edit)
505 int res, save_lock = 0;
507 if (!edit->locked && !edit->delete_file)
508 save_lock = lock_file (edit->filename_vpath);
509 res = edit_save_file (edit, edit->filename_vpath);
511 /* Maintain modify (not save) lock on failure */
512 if ((res > 0 && edit->locked) || save_lock)
513 edit->locked = unlock_file (edit->filename_vpath);
515 /* On failure try 'save as', it does locking on its own */
516 if (res == 0)
517 return edit_save_as_cmd (edit);
518 edit->force |= REDRAW_COMPLETELY;
519 if (res > 0)
521 edit->delete_file = 0;
522 edit->modified = 0;
525 return TRUE;
528 /* --------------------------------------------------------------------------------------------- */
530 * Load file content
532 * @param h screen the owner of editor window
533 * @param vpath vfs file path
534 * @return TRUE if file content was successfully loaded, FALSE otherwise
537 static inline gboolean
538 edit_load_file_from_filename (WDialog * h, const vfs_path_t * vpath)
540 Widget *w = WIDGET (h);
542 return edit_add_window (h, w->y + 1, w->x, w->lines - 2, w->cols, vpath, 0);
545 /* --------------------------------------------------------------------------------------------- */
547 static void
548 edit_delete_column_of_text (WEdit * edit)
550 off_t p, q, r;
551 off_t m1, m2;
552 off_t n;
553 long b, c, d;
555 eval_marks (edit, &m1, &m2);
556 n = edit_move_forward (edit, m1, 0, m2) + 1;
557 c = (long) edit_move_forward3 (edit, edit_bol (edit, m1), 0, m1);
558 d = (long) edit_move_forward3 (edit, edit_bol (edit, m2), 0, m2);
559 b = max (min (c, d), min (edit->column1, edit->column2));
560 c = max (c, max (edit->column1, edit->column2));
562 while (n--)
564 r = edit_bol (edit, edit->curs1);
565 p = edit_move_forward3 (edit, r, b, 0);
566 q = edit_move_forward3 (edit, r, c, 0);
567 if (p < m1)
568 p = m1;
569 if (q > m2)
570 q = m2;
571 edit_cursor_move (edit, p - edit->curs1);
572 while (q > p)
574 /* delete line between margins */
575 if (edit_get_byte (edit, edit->curs1) != '\n')
576 edit_delete (edit, TRUE);
577 q--;
579 if (n)
580 /* move to next line except on the last delete */
581 edit_cursor_move (edit, edit_move_forward (edit, edit->curs1, 1, 0) - edit->curs1);
585 /* --------------------------------------------------------------------------------------------- */
586 /** if success return 0 */
588 static int
589 edit_block_delete (WEdit * edit)
591 off_t start_mark, end_mark;
592 off_t curs_pos;
593 long curs_line, c1, c2;
595 if (eval_marks (edit, &start_mark, &end_mark))
596 return 0;
597 if (edit->column_highlight && edit->mark2 < 0)
598 edit_mark_cmd (edit, FALSE);
599 if ((end_mark - start_mark) > option_max_undo / 2)
601 /* Warning message with a query to continue or cancel the operation */
602 if (edit_query_dialog2
603 (_("Warning"),
605 ("Block is large, you may not be able to undo this action"),
606 _("C&ontinue"), _("&Cancel")))
608 return 1;
611 c1 = min (edit->column1, edit->column2);
612 c2 = max (edit->column1, edit->column2);
613 edit->column1 = c1;
614 edit->column2 = c2;
616 edit_push_markers (edit);
618 curs_line = edit->curs_line;
620 curs_pos = edit->curs_col + edit->over_col;
622 /* move cursor to start of selection */
623 edit_cursor_move (edit, start_mark - edit->curs1);
624 edit_scroll_screen_over_cursor (edit);
626 if (start_mark < end_mark)
628 if (edit->column_highlight)
630 off_t line_width;
632 if (edit->mark2 < 0)
633 edit_mark_cmd (edit, FALSE);
634 edit_delete_column_of_text (edit);
635 /* move cursor to the saved position */
636 edit_move_to_line (edit, curs_line);
637 /* calculate line width and cursor position before cut */
638 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
639 edit_eol (edit, edit->curs1));
640 if (option_cursor_beyond_eol && curs_pos > line_width)
641 edit->over_col = curs_pos - line_width;
643 else
645 off_t count;
647 for (count = start_mark; count < end_mark; count++)
648 edit_delete (edit, TRUE);
651 edit_set_markers (edit, 0, 0, 0, 0);
652 edit->force |= REDRAW_PAGE;
653 return 0;
656 /* --------------------------------------------------------------------------------------------- */
658 * Get EOL symbol for searching.
660 * @param edit editor object
661 * @return EOL symbol
664 static inline char
665 edit_search_get_current_end_line_char (const WEdit * edit)
667 switch (edit->lb)
669 case LB_MAC:
670 return '\r';
671 default:
672 return '\n';
676 /* --------------------------------------------------------------------------------------------- */
678 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
680 * @param search search object
681 * @return result of checks.
684 static edit_search_line_t
685 edit_get_search_line_type (mc_search_t * search)
687 edit_search_line_t search_line_type = 0;
689 if (search->search_type != MC_SEARCH_T_REGEX)
690 return search_line_type;
692 if (*search->original == '^')
693 search_line_type |= AT_START_LINE;
695 if (search->original[search->original_len - 1] == '$')
696 search_line_type |= AT_END_LINE;
697 return search_line_type;
700 /* --------------------------------------------------------------------------------------------- */
702 * Calculating the start position of next line.
704 * @param edit editor object
705 * @param current_pos current position
706 * @param max_pos max position
707 * @param end_string_symbol end of line symbol
708 * @return start position of next line
711 static off_t
712 edit_calculate_start_of_next_line (WEdit * edit, off_t current_pos, off_t max_pos,
713 char end_string_symbol)
715 off_t i;
717 for (i = current_pos; i < max_pos; i++)
719 current_pos++;
720 if (edit_get_byte (edit, i) == end_string_symbol)
721 break;
724 return current_pos;
727 /* --------------------------------------------------------------------------------------------- */
729 * Calculating the end position of previous line.
731 * @param edit editor object
732 * @param current_pos current position
733 * @param end_string_symbol end of line symbol
734 * @return end position of previous line
737 static off_t
738 edit_calculate_end_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
740 off_t i;
742 for (i = current_pos - 1; i >= 0; i--)
743 if (edit_get_byte (edit, i) == end_string_symbol)
744 break;
746 return i;
749 /* --------------------------------------------------------------------------------------------- */
751 * Calculating the start position of previous line.
753 * @param edit editor object
754 * @param current_pos current position
755 * @param end_string_symbol end of line symbol
756 * @return start position of previous line
759 static inline off_t
760 edit_calculate_start_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
762 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
763 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
765 return (current_pos + 1);
768 /* --------------------------------------------------------------------------------------------- */
770 * Calculating the start position of current line.
772 * @param edit editor object
773 * @param current_pos current position
774 * @param end_string_symbol end of line symbol
775 * @return start position of current line
778 static inline off_t
779 edit_calculate_start_of_current_line (WEdit * edit, off_t current_pos, char end_string_symbol)
781 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
783 return (current_pos + 1);
786 /* --------------------------------------------------------------------------------------------- */
788 * Fixing (if needed) search start position if 'only in selection' option present.
790 * @param edit editor object
793 static void
794 edit_search_fix_search_start_if_selection (WEdit * edit)
796 off_t start_mark = 0;
797 off_t end_mark = 0;
799 if (!edit_search_options.only_in_selection)
800 return;
802 if (eval_marks (edit, &start_mark, &end_mark) != 0)
803 return;
805 if (edit_search_options.backwards)
807 if (edit->search_start > end_mark || edit->search_start <= start_mark)
808 edit->search_start = end_mark;
810 else
812 if (edit->search_start < start_mark || edit->search_start >= end_mark)
813 edit->search_start = start_mark;
817 /* --------------------------------------------------------------------------------------------- */
819 static gboolean
820 editcmd_find (WEdit * edit, gsize * len)
822 off_t search_start = edit->search_start;
823 off_t search_end;
824 off_t start_mark = 0;
825 off_t end_mark = edit->last_byte;
826 int mark_res = 0;
827 char end_string_symbol;
829 end_string_symbol = edit_search_get_current_end_line_char (edit);
831 /* prepare for search */
832 if (edit_search_options.only_in_selection)
834 mark_res = eval_marks (edit, &start_mark, &end_mark);
835 if (mark_res != 0)
837 edit->search->error = MC_SEARCH_E_NOTFOUND;
838 edit->search->error_str = g_strdup (_("Search string not found"));
839 return FALSE;
842 /* fix the start and the end of search block positions */
843 if ((edit->search_line_type & AT_START_LINE) != 0
844 && (start_mark != 0 || edit_get_byte (edit, start_mark - 1) != end_string_symbol))
846 start_mark =
847 edit_calculate_start_of_next_line (edit, start_mark, edit->last_byte,
848 end_string_symbol);
850 if ((edit->search_line_type & AT_END_LINE) != 0
851 && (end_mark - 1 != edit->last_byte
852 || edit_get_byte (edit, end_mark) != end_string_symbol))
854 end_mark = edit_calculate_end_of_previous_line (edit, end_mark, end_string_symbol);
856 if (start_mark >= end_mark)
858 edit->search->error = MC_SEARCH_E_NOTFOUND;
859 edit->search->error_str = g_strdup (_("Search string not found"));
860 return FALSE;
863 else
865 if (edit_search_options.backwards)
866 end_mark = max (1, edit->curs1) - 1;
869 /* search */
870 if (edit_search_options.backwards)
872 /* backward search */
873 search_end = end_mark;
875 if ((edit->search_line_type & AT_START_LINE) != 0)
876 search_start =
877 edit_calculate_start_of_current_line (edit, search_start, end_string_symbol);
879 while (search_start >= start_mark)
881 if (search_end > (off_t) (search_start + edit->search->original_len)
882 && mc_search_is_fixed_search_str (edit->search))
884 search_end = search_start + edit->search->original_len;
886 if (mc_search_run (edit->search, (void *) edit, search_start, search_end, len)
887 && edit->search->normal_offset == search_start)
889 return TRUE;
893 if ((edit->search_line_type & AT_START_LINE) != 0)
894 search_start =
895 edit_calculate_start_of_previous_line (edit, search_start, end_string_symbol);
896 else
897 search_start--;
899 edit->search->error_str = g_strdup (_("Search string not found"));
901 else
903 /* forward search */
904 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
905 search_start =
906 edit_calculate_start_of_next_line (edit, search_start, end_mark, end_string_symbol);
907 return mc_search_run (edit->search, (void *) edit, search_start, end_mark, len);
909 return FALSE;
912 /* --------------------------------------------------------------------------------------------- */
914 static char *
915 edit_replace_cmd__conv_to_display (char *str)
917 #ifdef HAVE_CHARSET
918 GString *tmp;
920 tmp = str_convert_to_display (str);
921 if (tmp != NULL)
923 if (tmp->len != 0)
924 return g_string_free (tmp, FALSE);
925 g_string_free (tmp, TRUE);
927 #endif
928 return g_strdup (str);
931 /* --------------------------------------------------------------------------------------------- */
933 static char *
934 edit_replace_cmd__conv_to_input (char *str)
936 #ifdef HAVE_CHARSET
937 GString *tmp;
939 tmp = str_convert_to_input (str);
940 if (tmp != NULL)
942 if (tmp->len != 0)
943 return g_string_free (tmp, FALSE);
944 g_string_free (tmp, TRUE);
946 #endif
947 return g_strdup (str);
950 /* --------------------------------------------------------------------------------------------- */
952 static void
953 edit_do_search (WEdit * edit)
955 gsize len = 0;
957 if (edit->search == NULL)
958 edit->search_start = edit->curs1;
960 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
962 if (search_create_bookmark)
964 int found = 0, books = 0;
965 long l = 0, l_last = -1;
966 long q = 0;
968 search_create_bookmark = FALSE;
969 book_mark_flush (edit, -1);
971 while (mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len))
973 if (found == 0)
974 edit->search_start = edit->search->normal_offset;
975 found++;
976 l += edit_count_lines (edit, q, edit->search->normal_offset);
977 if (l != l_last)
979 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
980 books++;
982 l_last = l;
983 q = edit->search->normal_offset + 1;
986 if (found == 0)
987 edit_error_dialog (_("Search"), _("Search string not found"));
988 else
989 edit_cursor_move (edit, edit->search_start - edit->curs1);
991 else
993 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
994 && edit_search_options.backwards)
995 edit->search_start--;
997 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
998 && !edit_search_options.backwards)
999 edit->search_start++;
1001 if (editcmd_find (edit, &len))
1003 edit->found_start = edit->search_start = edit->search->normal_offset;
1004 edit->found_len = len;
1005 edit->over_col = 0;
1006 edit_cursor_move (edit, edit->search_start - edit->curs1);
1007 edit_scroll_screen_over_cursor (edit);
1008 if (edit_search_options.backwards)
1009 edit->search_start--;
1010 else
1011 edit->search_start++;
1013 else
1015 edit->search_start = edit->curs1;
1016 if (edit->search->error_str != NULL)
1017 edit_error_dialog (_("Search"), edit->search->error_str);
1021 edit->force |= REDRAW_COMPLETELY;
1022 edit_scroll_screen_over_cursor (edit);
1025 /* --------------------------------------------------------------------------------------------- */
1027 static void
1028 edit_search (WEdit * edit)
1030 if (editcmd_dialog_search_show (edit))
1032 edit->search_line_type = edit_get_search_line_type (edit->search);
1033 edit_search_fix_search_start_if_selection (edit);
1034 edit_do_search (edit);
1038 /* --------------------------------------------------------------------------------------------- */
1039 /** Return a null terminated length of text. Result must be g_free'd */
1041 static unsigned char *
1042 edit_get_block (WEdit * edit, off_t start, off_t finish, off_t * l)
1044 unsigned char *s, *r;
1046 r = s = g_malloc0 (finish - start + 1);
1047 if (edit->column_highlight)
1049 *l = 0;
1050 /* copy from buffer, excluding chars that are out of the column 'margins' */
1051 while (start < finish)
1053 int c;
1054 off_t x;
1056 x = edit_move_forward3 (edit, edit_bol (edit, start), 0, start);
1057 c = edit_get_byte (edit, start);
1058 if ((x >= edit->column1 && x < edit->column2)
1059 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1061 *s++ = c;
1062 (*l)++;
1064 start++;
1067 else
1069 *l = finish - start;
1070 while (start < finish)
1071 *s++ = edit_get_byte (edit, start++);
1073 *s = '\0';
1074 return r;
1077 /* --------------------------------------------------------------------------------------------- */
1078 /** copies a block to clipboard file */
1080 static gboolean
1081 edit_save_block_to_clip_file (WEdit * edit, off_t start, off_t finish)
1083 gboolean ret;
1084 gchar *tmp;
1086 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1087 ret = edit_save_block (edit, tmp, start, finish);
1088 g_free (tmp);
1089 return ret;
1092 /* --------------------------------------------------------------------------------------------- */
1094 static void
1095 pipe_mail (WEdit * edit, char *to, char *subject, char *cc)
1097 FILE *p = 0;
1098 char *s;
1100 to = name_quote (to, 0);
1101 subject = name_quote (subject, 0);
1102 cc = name_quote (cc, 0);
1103 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1104 g_free (to);
1105 g_free (subject);
1106 g_free (cc);
1108 if (s)
1110 p = popen (s, "w");
1111 g_free (s);
1114 if (p)
1116 off_t i;
1117 for (i = 0; i < edit->last_byte; i++)
1118 fputc (edit_get_byte (edit, i), p);
1119 pclose (p);
1123 /* --------------------------------------------------------------------------------------------- */
1124 /** find first character of current word */
1126 static gboolean
1127 edit_find_word_start (WEdit * edit, off_t * word_start, gsize * word_len)
1129 int c, last;
1130 off_t i;
1132 /* return if at begin of file */
1133 if (edit->curs1 <= 0)
1134 return FALSE;
1136 c = edit_get_byte (edit, edit->curs1 - 1);
1137 /* return if not at end or in word */
1138 if (is_break_char (c))
1139 return FALSE;
1141 /* search start of word to be completed */
1142 for (i = 2;; i++)
1144 /* return if at begin of file */
1145 if (edit->curs1 < i)
1146 return FALSE;
1148 last = c;
1149 c = edit_get_byte (edit, edit->curs1 - i);
1151 if (is_break_char (c))
1153 /* return if word starts with digit */
1154 if (isdigit (last))
1155 return FALSE;
1157 *word_start = edit->curs1 - (i - 1); /* start found */
1158 *word_len = (gsize) (i - 1);
1159 break;
1162 /* success */
1163 return TRUE;
1166 /* --------------------------------------------------------------------------------------------- */
1168 * Get current word under cursor
1170 * @param edit editor object
1171 * @param srch mc_search object
1172 * @param word_start start word position
1174 * @return newly allocated string or NULL if no any words under cursor
1177 static char *
1178 edit_collect_completions_get_current_word (WEdit * edit, mc_search_t * srch, off_t word_start)
1180 gsize len = 0;
1181 off_t i;
1182 GString *temp;
1184 if (!mc_search_run (srch, (void *) edit, word_start, edit->last_byte, &len))
1185 return NULL;
1187 temp = g_string_sized_new (len);
1189 for (i = 0; i < (off_t) len; i++)
1191 int chr;
1193 chr = edit_get_byte (edit, word_start + i);
1194 if (!isspace (chr))
1195 g_string_append_c (temp, chr);
1198 return g_string_free (temp, temp->len == 0);
1201 /* --------------------------------------------------------------------------------------------- */
1202 /** collect the possible completions */
1204 static gsize
1205 edit_collect_completions (WEdit * edit, off_t word_start, gsize word_len,
1206 char *match_expr, GString ** compl, gsize * num)
1208 gsize len = 0;
1209 gsize max_len = 0;
1210 gsize i;
1211 int skip;
1212 GString *temp;
1213 mc_search_t *srch;
1214 off_t last_byte, start = -1;
1215 char *current_word;
1217 srch = mc_search_new (match_expr, -1);
1218 if (srch == NULL)
1219 return 0;
1221 if (mc_config_get_bool
1222 (mc_main_config, CONFIG_APP_SECTION, "editor_wordcompletion_collect_entire_file", 0))
1224 last_byte = edit->last_byte;
1226 else
1228 last_byte = word_start;
1231 srch->search_type = MC_SEARCH_T_REGEX;
1232 srch->is_case_sensitive = TRUE;
1233 srch->search_fn = edit_search_cmd_callback;
1235 current_word = edit_collect_completions_get_current_word (edit, srch, word_start);
1237 temp = g_string_new ("");
1239 /* collect max MAX_WORD_COMPLETIONS completions */
1240 while (mc_search_run (srch, (void *) edit, start + 1, last_byte, &len))
1242 g_string_set_size (temp, 0);
1243 start = srch->normal_offset;
1245 /* add matched completion if not yet added */
1246 for (i = 0; i < len; i++)
1248 skip = edit_get_byte (edit, start + i);
1249 if (isspace (skip))
1250 continue;
1252 /* skip current word */
1253 if (start + (off_t) i == word_start)
1254 break;
1256 g_string_append_c (temp, skip);
1259 if (temp->len == 0)
1260 continue;
1262 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1263 continue;
1265 skip = 0;
1267 for (i = 0; i < *num; i++)
1269 if (strncmp
1270 ((char *) &compl[i]->str[word_len],
1271 (char *) &temp->str[word_len], max (len, compl[i]->len) - word_len) == 0)
1273 GString *this = compl[i];
1274 for (++i; i < *num; i++)
1275 compl[i - 1] = compl[i];
1276 compl[*num - 1] = this;
1277 skip = 1;
1278 break; /* skip it, already added */
1281 if (skip != 0)
1282 continue;
1284 if (*num == MAX_WORD_COMPLETIONS)
1286 g_string_free (compl[0], TRUE);
1287 for (i = 1; i < *num; i++)
1288 compl[i - 1] = compl[i];
1289 (*num)--;
1291 #ifdef HAVE_CHARSET
1293 GString *recoded;
1294 recoded = str_convert_to_display (temp->str);
1296 if (recoded && recoded->len)
1297 g_string_assign (temp, recoded->str);
1299 g_string_free (recoded, TRUE);
1301 #endif
1302 compl[(*num)++] = g_string_new_len (temp->str, temp->len);
1303 start += len;
1305 /* note the maximal length needed for the completion dialog */
1306 if (len > max_len)
1307 max_len = len;
1310 mc_search_free (srch);
1311 g_string_free (temp, TRUE);
1312 g_free (current_word);
1314 return max_len;
1317 /* --------------------------------------------------------------------------------------------- */
1319 static void
1320 edit_insert_column_of_text (WEdit * edit, unsigned char *data, off_t size, long width,
1321 off_t * start_pos, off_t * end_pos, long *col1, long *col2)
1323 off_t i, cursor;
1324 long col;
1326 cursor = edit->curs1;
1327 col = edit_get_col (edit);
1329 for (i = 0; i < size; i++)
1331 if (data[i] != '\n')
1332 edit_insert (edit, data[i]);
1333 else
1334 { /* fill in and move to next line */
1335 long l;
1336 off_t p;
1338 if (edit_get_byte (edit, edit->curs1) != '\n')
1340 for (l = width - (edit_get_col (edit) - col); l > 0; l -= space_width)
1341 edit_insert (edit, ' ');
1343 for (p = edit->curs1;; p++)
1345 if (p == edit->last_byte)
1347 edit_cursor_move (edit, edit->last_byte - edit->curs1);
1348 edit_insert_ahead (edit, '\n');
1349 p++;
1350 break;
1352 if (edit_get_byte (edit, p) == '\n')
1354 p++;
1355 break;
1358 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
1360 for (l = col - edit_get_col (edit); l >= space_width; l -= space_width)
1361 edit_insert (edit, ' ');
1365 *col1 = col;
1366 *col2 = col + width;
1367 *start_pos = cursor;
1368 *end_pos = edit->curs1;
1369 edit_cursor_move (edit, cursor - edit->curs1);
1372 /* --------------------------------------------------------------------------------------------- */
1374 static int
1375 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1377 const macros_t *m1 = (const macros_t *) macro1;
1378 const macros_t *m2 = (const macros_t *) macro2;
1380 return m1->hotkey - m2->hotkey;
1383 /* --------------------------------------------------------------------------------------------- */
1385 static void
1386 edit_macro_sort_by_hotkey (void)
1388 if (macros_list != NULL && macros_list->len != 0)
1389 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1392 /* --------------------------------------------------------------------------------------------- */
1394 static gboolean
1395 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1397 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1398 macros_t *result;
1399 macros_t search_macro;
1401 (void) edit;
1403 search_macro.hotkey = hotkey;
1404 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1405 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1407 if (result != NULL && result->macro != NULL)
1409 *indx = (result - array_start);
1410 *macros = result;
1411 return TRUE;
1413 *indx = 0;
1414 return FALSE;
1417 /* --------------------------------------------------------------------------------------------- */
1418 /** returns FALSE on error */
1420 static gboolean
1421 edit_delete_macro (WEdit * edit, int hotkey)
1423 mc_config_t *macros_config = NULL;
1424 const char *section_name = "editor";
1425 gchar *macros_fname;
1426 guint indx;
1427 char *skeyname;
1428 const macros_t *macros = NULL;
1430 /* clear array of actions for current hotkey */
1431 while (edit_get_macro (edit, hotkey, &macros, &indx))
1433 if (macros->macro != NULL)
1434 g_array_free (macros->macro, TRUE);
1435 macros = NULL;
1436 g_array_remove_index (macros_list, indx);
1437 edit_macro_sort_by_hotkey ();
1440 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1441 macros_config = mc_config_init (macros_fname, FALSE);
1442 g_free (macros_fname);
1444 if (macros_config == NULL)
1445 return FALSE;
1447 skeyname = lookup_key_by_code (hotkey);
1448 while (mc_config_del_key (macros_config, section_name, skeyname))
1450 g_free (skeyname);
1451 mc_config_save_file (macros_config, NULL);
1452 mc_config_deinit (macros_config);
1453 return TRUE;
1456 /* --------------------------------------------------------------------------------------------- */
1458 * Callback for the iteration of objects in the 'editors' array.
1459 * Toggle syntax highlighting in editor object.
1461 * @param data probably WEdit object
1462 * @param user_data unused
1465 static void
1466 edit_syntax_onoff_cb (void *data, void *user_data)
1468 (void) user_data;
1470 if (edit_widget_is_editor ((const Widget *) data))
1472 WEdit *edit = (WEdit *) data;
1474 if (option_syntax_highlighting)
1475 edit_load_syntax (edit, NULL, edit->syntax_type);
1476 edit->force |= REDRAW_PAGE;
1480 /* --------------------------------------------------------------------------------------------- */
1482 * Callback for the iteration of objects in the 'editors' array.
1483 * Redraw editor object.
1485 * @param data probably WEdit object
1486 * @param user_data unused
1489 static void
1490 edit_redraw_page_cb (void *data, void *user_data)
1492 (void) user_data;
1494 if (edit_widget_is_editor ((const Widget *) data))
1495 ((WEdit *) data)->force |= REDRAW_PAGE;
1498 /* --------------------------------------------------------------------------------------------- */
1500 * Insert autocompleted word into editor.
1502 * @param edit editor object
1503 * @param completion word for completion
1504 * @param word_len offset from begining for insert
1507 static void
1508 edit_complete_word_insert_recoded_completion (WEdit * edit, char *completion, gsize word_len)
1510 #ifdef HAVE_CHARSET
1511 GString *temp;
1513 temp = str_convert_to_input (completion);
1515 for (completion = temp->str + word_len; *completion != '\0'; completion++)
1516 edit_insert (edit, *completion);
1517 g_string_free (temp, TRUE);
1518 #else
1519 for (completion += word_len; *completion != '\0'; completion++)
1520 edit_insert (edit, *completion);
1521 #endif
1524 /* --------------------------------------------------------------------------------------------- */
1525 /*** public functions ****************************************************************************/
1526 /* --------------------------------------------------------------------------------------------- */
1528 void
1529 edit_refresh_cmd (void)
1531 clr_scr ();
1532 repaint_screen ();
1533 tty_keypad (TRUE);
1536 /* --------------------------------------------------------------------------------------------- */
1538 * Toggle syntax highlighting in all editor windows.
1540 * @param h root widget for all windows
1543 void
1544 edit_syntax_onoff_cmd (WDialog * h)
1546 option_syntax_highlighting = !option_syntax_highlighting;
1547 g_list_foreach (h->widgets, edit_syntax_onoff_cb, NULL);
1548 dlg_redraw (h);
1551 /* --------------------------------------------------------------------------------------------- */
1553 * Toggle tabs showing in all editor windows.
1555 * @param h root widget for all windows
1558 void
1559 edit_show_tabs_tws_cmd (WDialog * h)
1561 enable_show_tabs_tws = !enable_show_tabs_tws;
1562 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1563 dlg_redraw (h);
1566 /* --------------------------------------------------------------------------------------------- */
1568 * Toggle right margin showing in all editor windows.
1570 * @param h root widget for all windows
1573 void
1574 edit_show_margin_cmd (WDialog * h)
1576 show_right_margin = !show_right_margin;
1577 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1578 dlg_redraw (h);
1581 /* --------------------------------------------------------------------------------------------- */
1583 * Toggle line numbers showing in all editor windows.
1585 * @param h root widget for all windows
1588 void
1589 edit_show_numbers_cmd (WDialog * h)
1591 option_line_state = !option_line_state;
1592 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
1593 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1594 dlg_redraw (h);
1597 /* --------------------------------------------------------------------------------------------- */
1599 void
1600 edit_save_mode_cmd (void)
1602 char *str_result;
1604 const char *str[] = {
1605 N_("&Quick save"),
1606 N_("&Safe save"),
1607 N_("&Do backups with following extension:")
1610 #ifdef HAVE_ASSERT_H
1611 assert (option_backup_ext != NULL);
1612 #endif
1614 #ifdef ENABLE_NLS
1615 size_t i;
1617 for (i = 0; i < 3; i++)
1618 str[i] = _(str[i]);
1619 #endif
1622 quick_widget_t quick_widgets[] = {
1623 /* *INDENT-OFF* */
1624 QUICK_RADIO (3, str, &option_save_mode, &edit_save_mode_radio_id),
1625 QUICK_INPUT (option_backup_ext, "edit-backup-ext", &str_result,
1626 &edit_save_mode_input_id, FALSE, FALSE, INPUT_COMPLETE_NONE),
1627 QUICK_SEPARATOR (TRUE),
1628 QUICK_CHECKBOX (N_("Check &POSIX new line"), &option_check_nl_at_eof, NULL),
1629 QUICK_BUTTONS_OK_CANCEL,
1630 QUICK_END
1631 /* *INDENT-ON* */
1634 quick_dialog_t qdlg = {
1635 -1, -1, 38,
1636 N_("Edit Save Mode"), "[Edit Save Mode]",
1637 quick_widgets, edit_save_mode_callback, NULL
1640 if (quick_dialog (&qdlg) != B_CANCEL)
1642 g_free (option_backup_ext);
1643 option_backup_ext = str_result;
1648 /* --------------------------------------------------------------------------------------------- */
1650 void
1651 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1653 vfs_path_free (edit->filename_vpath);
1654 edit->filename_vpath = vfs_path_clone (name_vpath);
1656 if (edit->dir_vpath == NULL)
1657 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1660 /* --------------------------------------------------------------------------------------------- */
1661 /* Here we want to warn the users of overwriting an existing file,
1662 but only if they have made a change to the filename */
1663 /* returns TRUE on success */
1664 gboolean
1665 edit_save_as_cmd (WEdit * edit)
1667 /* This heads the 'Save As' dialog box */
1668 vfs_path_t *exp_vpath;
1669 int save_lock = 0;
1670 int different_filename = 0;
1672 if (!edit_check_newline (edit))
1673 return FALSE;
1675 exp_vpath = edit_get_save_file_as (edit);
1676 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1678 if (exp_vpath != NULL)
1680 if (vfs_path_len (exp_vpath) == 0)
1681 goto ret;
1682 else
1684 int rv;
1686 if (!vfs_path_equal (edit->filename_vpath, exp_vpath))
1688 int file;
1689 struct stat sb;
1691 if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1693 edit_error_dialog (_("Save as"),
1694 get_sys_error (_
1695 ("Cannot save: destination is not a regular file")));
1696 goto ret;
1699 different_filename = 1;
1700 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1702 if (file != -1)
1704 /* the file exists */
1705 mc_close (file);
1706 /* Overwrite the current file or cancel the operation */
1707 if (edit_query_dialog2
1708 (_("Warning"),
1709 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1710 goto ret;
1712 else
1714 edit->stat1.st_mode |= S_IWUSR;
1716 save_lock = lock_file (exp_vpath);
1718 else
1720 /* filenames equal, check if already locked */
1721 if (!edit->locked && !edit->delete_file)
1722 save_lock = lock_file (exp_vpath);
1725 if (different_filename)
1728 * Allow user to write into saved (under another name) file
1729 * even if original file had r/o user permissions.
1731 edit->stat1.st_mode |= S_IWRITE;
1734 rv = edit_save_file (edit, exp_vpath);
1735 switch (rv)
1737 case 1:
1738 /* Succesful, so unlock both files */
1739 if (different_filename)
1741 if (save_lock)
1742 unlock_file (exp_vpath);
1743 if (edit->locked)
1744 edit->locked = unlock_file (edit->filename_vpath);
1746 else
1748 if (edit->locked || save_lock)
1749 edit->locked = unlock_file (edit->filename_vpath);
1752 edit_set_filename (edit, exp_vpath);
1753 if (edit->lb != LB_ASIS)
1754 edit_reload (edit, exp_vpath);
1755 edit->modified = 0;
1756 edit->delete_file = 0;
1757 if (different_filename)
1758 edit_load_syntax (edit, NULL, edit->syntax_type);
1759 vfs_path_free (exp_vpath);
1760 edit->force |= REDRAW_COMPLETELY;
1761 return TRUE;
1762 default:
1763 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1764 /* fallthrough */
1765 case -1:
1766 /* Failed, so maintain modify (not save) lock */
1767 if (save_lock)
1768 unlock_file (exp_vpath);
1769 break;
1774 ret:
1775 vfs_path_free (exp_vpath);
1776 edit->force |= REDRAW_COMPLETELY;
1777 return FALSE;
1780 /* {{{ Macro stuff starts here */
1781 /* --------------------------------------------------------------------------------------------- */
1783 void
1784 edit_delete_macro_cmd (WEdit * edit)
1786 int hotkey;
1788 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE);
1790 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1791 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1794 /* --------------------------------------------------------------------------------------------- */
1796 /** returns FALSE on error */
1797 gboolean
1798 edit_execute_macro (WEdit * edit, int hotkey)
1800 gboolean res = FALSE;
1802 if (hotkey != 0)
1804 const macros_t *macros;
1805 guint indx;
1807 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1808 macros->macro != NULL && macros->macro->len != 0)
1810 guint i;
1812 edit->force |= REDRAW_PAGE;
1814 for (i = 0; i < macros->macro->len; i++)
1816 const macro_action_t *m_act;
1818 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1819 edit_execute_cmd (edit, m_act->action, m_act->ch);
1820 res = TRUE;
1825 return res;
1828 /* --------------------------------------------------------------------------------------------- */
1830 /** returns FALSE on error */
1831 gboolean
1832 edit_store_macro_cmd (WEdit * edit)
1834 int i;
1835 int hotkey;
1836 GString *marcros_string;
1837 mc_config_t *macros_config = NULL;
1838 const char *section_name = "editor";
1839 gchar *macros_fname;
1840 GArray *macros; /* current macro */
1841 int tmp_act;
1842 gboolean have_macro = FALSE;
1843 char *skeyname = NULL;
1845 hotkey =
1846 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE);
1847 if (hotkey == ESC_CHAR)
1848 return FALSE;
1850 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1852 /* return FALSE if try assign macro into restricted hotkeys */
1853 if (tmp_act == CK_MacroStartRecord
1854 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1855 return FALSE;
1857 edit_delete_macro (edit, hotkey);
1859 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1860 macros_config = mc_config_init (macros_fname, FALSE);
1861 g_free (macros_fname);
1863 if (macros_config == NULL)
1864 return FALSE;
1866 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1868 marcros_string = g_string_sized_new (250);
1869 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1871 skeyname = lookup_key_by_code (hotkey);
1873 for (i = 0; i < macro_index; i++)
1875 macro_action_t m_act;
1876 const char *action_name;
1878 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1880 if (action_name == NULL)
1881 break;
1883 m_act.action = record_macro_buf[i].action;
1884 m_act.ch = record_macro_buf[i].ch;
1885 g_array_append_val (macros, m_act);
1886 have_macro = TRUE;
1887 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1888 (int) record_macro_buf[i].ch);
1890 if (have_macro)
1892 macros_t macro;
1893 macro.hotkey = hotkey;
1894 macro.macro = macros;
1895 g_array_append_val (macros_list, macro);
1896 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1898 else
1899 mc_config_del_key (macros_config, section_name, skeyname);
1901 g_free (skeyname);
1902 edit_macro_sort_by_hotkey ();
1904 g_string_free (marcros_string, TRUE);
1905 mc_config_save_file (macros_config, NULL);
1906 mc_config_deinit (macros_config);
1907 return TRUE;
1910 /* --------------------------------------------------------------------------------------------- */
1912 gboolean
1913 edit_repeat_macro_cmd (WEdit * edit)
1915 int i, j;
1916 char *f;
1917 long count_repeat;
1918 char *error = NULL;
1920 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL,
1921 INPUT_COMPLETE_NONE);
1922 if (f == NULL || *f == '\0')
1924 g_free (f);
1925 return FALSE;
1928 count_repeat = strtol (f, &error, 0);
1930 if (*error != '\0')
1932 g_free (f);
1933 return FALSE;
1936 g_free (f);
1938 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1939 edit->force |= REDRAW_PAGE;
1941 for (j = 0; j < count_repeat; j++)
1942 for (i = 0; i < macro_index; i++)
1943 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1944 edit_update_screen (edit);
1945 return TRUE;
1948 /* --------------------------------------------------------------------------------------------- */
1949 /** return FALSE on error */
1951 gboolean
1952 edit_load_macro_cmd (WEdit * edit)
1954 mc_config_t *macros_config = NULL;
1955 gchar **profile_keys, **keys;
1956 gchar **values, **curr_values;
1957 gsize len, values_len;
1958 const char *section_name = "editor";
1959 gchar *macros_fname;
1960 int hotkey;
1962 (void) edit;
1964 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1965 macros_config = mc_config_init (macros_fname, TRUE);
1966 g_free (macros_fname);
1968 if (macros_config == NULL || macros_list == NULL || macros_list->len != 0)
1969 return FALSE;
1971 profile_keys = keys = mc_config_get_keys (macros_config, section_name, &len);
1972 while (*profile_keys != NULL)
1974 gboolean have_macro;
1975 GArray *macros;
1976 macros_t macro;
1978 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1980 curr_values = values = mc_config_get_string_list (macros_config, section_name,
1981 *profile_keys, &values_len);
1982 hotkey = lookup_key (*profile_keys, NULL);
1983 have_macro = FALSE;
1985 while (*curr_values != NULL && *curr_values[0] != '\0')
1987 char **macro_pair = NULL;
1989 macro_pair = g_strsplit (*curr_values, ":", 2);
1991 if (macro_pair != NULL)
1993 macro_action_t m_act;
1994 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
1995 m_act.action = 0;
1996 else
1998 m_act.action = keybind_lookup_action (macro_pair[0]);
1999 g_free (macro_pair[0]);
2000 macro_pair[0] = NULL;
2002 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
2003 m_act.ch = -1;
2004 else
2006 m_act.ch = strtol (macro_pair[1], NULL, 0);
2007 g_free (macro_pair[1]);
2008 macro_pair[1] = NULL;
2010 if (m_act.action != 0)
2012 /* a shell command */
2013 if ((m_act.action / CK_PipeBlock (0)) == 1)
2015 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
2016 m_act.ch = -1;
2018 g_array_append_val (macros, m_act);
2019 have_macro = TRUE;
2021 g_strfreev (macro_pair);
2022 macro_pair = NULL;
2024 curr_values++;
2026 if (have_macro)
2028 macro.hotkey = hotkey;
2029 macro.macro = macros;
2030 g_array_append_val (macros_list, macro);
2032 profile_keys++;
2033 g_strfreev (values);
2035 g_strfreev (keys);
2036 mc_config_deinit (macros_config);
2037 edit_macro_sort_by_hotkey ();
2038 return TRUE;
2041 /* }}} Macro stuff end here */
2043 /* --------------------------------------------------------------------------------------------- */
2044 /** returns TRUE on success */
2046 gboolean
2047 edit_save_confirm_cmd (WEdit * edit)
2049 char *f = NULL;
2051 if (edit->filename_vpath == NULL)
2052 return edit_save_as_cmd (edit);
2054 if (!edit_check_newline (edit))
2055 return FALSE;
2057 if (edit_confirm_save)
2059 char *filename;
2060 gboolean ok;
2062 filename = vfs_path_to_str (edit->filename_vpath);
2063 f = g_strdup_printf (_("Confirm save file: \"%s\""), filename);
2064 g_free (filename);
2065 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2066 g_free (f);
2067 if (!ok)
2068 return FALSE;
2070 return edit_save_cmd (edit);
2073 /* --------------------------------------------------------------------------------------------- */
2075 * Ask file to edit and load it.
2077 * @return TRUE on success or cancel of ask.
2080 gboolean
2081 edit_load_cmd (WDialog * h)
2083 char *exp;
2084 gboolean ret = TRUE; /* possible cancel */
2086 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2087 MC_HISTORY_EDIT_LOAD, INPUT_LAST_TEXT,
2088 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_CD);
2090 if (exp != NULL && *exp != '\0')
2092 vfs_path_t *exp_vpath;
2094 exp_vpath = vfs_path_from_str (exp);
2095 ret = edit_load_file_from_filename (h, exp_vpath);
2096 vfs_path_free (exp_vpath);
2099 g_free (exp);
2101 return ret;
2104 /* --------------------------------------------------------------------------------------------- */
2106 * Load syntax file to edit.
2108 * @return TRUE on success
2111 gboolean
2112 edit_load_syntax_file (WDialog * h)
2114 vfs_path_t *extdir_vpath;
2115 int dir = 0;
2116 gboolean ret = FALSE;
2118 if (geteuid () == 0)
2119 dir = query_dialog (_("Syntax file edit"),
2120 _("Which syntax file you want to edit?"), D_NORMAL, 2,
2121 _("&User"), _("&System wide"));
2123 extdir_vpath =
2124 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
2125 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
2127 vfs_path_free (extdir_vpath);
2128 extdir_vpath =
2129 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
2132 if (dir == 0)
2134 vfs_path_t *user_syntax_file_vpath;
2136 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
2137 check_for_default (extdir_vpath, user_syntax_file_vpath);
2138 ret = edit_load_file_from_filename (h, user_syntax_file_vpath);
2139 vfs_path_free (user_syntax_file_vpath);
2141 else if (dir == 1)
2142 ret = edit_load_file_from_filename (h, extdir_vpath);
2144 vfs_path_free (extdir_vpath);
2146 return ret;
2149 /* --------------------------------------------------------------------------------------------- */
2151 * Load menu file to edit.
2153 * @return TRUE on success
2156 gboolean
2157 edit_load_menu_file (WDialog * h)
2159 vfs_path_t *buffer_vpath;
2160 vfs_path_t *menufile_vpath;
2161 int dir;
2162 gboolean ret;
2164 dir = query_dialog (_("Menu edit"),
2165 _("Which menu file do you want to edit?"), D_NORMAL,
2166 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2168 menufile_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2169 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
2171 vfs_path_free (menufile_vpath);
2172 menufile_vpath = vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2175 switch (dir)
2177 case 0:
2178 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
2179 check_for_default (menufile_vpath, buffer_vpath);
2180 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
2181 break;
2183 case 1:
2184 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
2185 check_for_default (menufile_vpath, buffer_vpath);
2186 break;
2188 case 2:
2189 buffer_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2190 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
2192 vfs_path_free (buffer_vpath);
2193 buffer_vpath =
2194 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2196 break;
2198 default:
2199 vfs_path_free (menufile_vpath);
2200 return FALSE;
2203 ret = edit_load_file_from_filename (h, buffer_vpath);
2205 vfs_path_free (buffer_vpath);
2206 vfs_path_free (menufile_vpath);
2208 return ret;
2211 /* --------------------------------------------------------------------------------------------- */
2213 * Close window with opened file.
2215 * @return TRUE if file was closed.
2218 gboolean
2219 edit_close_cmd (WEdit * edit)
2221 gboolean ret;
2223 ret = (edit != NULL) && edit_ok_to_exit (edit);
2225 if (ret)
2227 WDialog *h = WIDGET (edit)->owner;
2229 if (edit->locked != 0)
2230 unlock_file (edit->filename_vpath);
2232 del_widget (edit);
2234 if (edit_widget_is_editor (WIDGET (h->current->data)))
2235 edit = (WEdit *) h->current->data;
2236 else
2238 edit = find_editor (h);
2239 if (edit != NULL)
2240 dlg_set_top_widget (edit);
2244 if (edit != NULL)
2245 edit->force |= REDRAW_COMPLETELY;
2247 return ret;
2250 /* --------------------------------------------------------------------------------------------- */
2252 if mark2 is -1 then marking is from mark1 to the cursor.
2253 Otherwise its between the markers. This handles this.
2254 Returns 1 if no text is marked.
2258 eval_marks (WEdit * edit, off_t * start_mark, off_t * end_mark)
2260 if (edit->mark1 != edit->mark2)
2262 off_t start_bol, start_eol;
2263 off_t end_bol, end_eol;
2264 off_t diff1, diff2;
2265 long col1, col2;
2266 long end_mark_curs;
2268 if (edit->end_mark_curs < 0)
2269 end_mark_curs = edit->curs1;
2270 else
2271 end_mark_curs = edit->end_mark_curs;
2273 if (edit->mark2 >= 0)
2275 *start_mark = min (edit->mark1, edit->mark2);
2276 *end_mark = max (edit->mark1, edit->mark2);
2278 else
2280 *start_mark = min (edit->mark1, end_mark_curs);
2281 *end_mark = max (edit->mark1, end_mark_curs);
2282 edit->column2 = edit->curs_col + edit->over_col;
2285 if (edit->column_highlight
2286 && (((edit->mark1 > end_mark_curs) && (edit->column1 < edit->column2))
2287 || ((edit->mark1 < end_mark_curs) && (edit->column1 > edit->column2))))
2289 start_bol = edit_bol (edit, *start_mark);
2290 start_eol = edit_eol (edit, start_bol - 1) + 1;
2291 end_bol = edit_bol (edit, *end_mark);
2292 end_eol = edit_eol (edit, *end_mark);
2293 col1 = min (edit->column1, edit->column2);
2294 col2 = max (edit->column1, edit->column2);
2296 diff1 = edit_move_forward3 (edit, start_bol, col2, 0) -
2297 edit_move_forward3 (edit, start_bol, col1, 0);
2298 diff2 = edit_move_forward3 (edit, end_bol, col2, 0) -
2299 edit_move_forward3 (edit, end_bol, col1, 0);
2301 *start_mark -= diff1;
2302 *end_mark += diff2;
2303 *start_mark = max (*start_mark, start_eol);
2304 *end_mark = min (*end_mark, end_eol);
2306 return 0;
2308 else
2310 *start_mark = *end_mark = 0;
2311 edit->column2 = edit->column1 = 0;
2312 return 1;
2316 /* --------------------------------------------------------------------------------------------- */
2318 void
2319 edit_block_copy_cmd (WEdit * edit)
2321 off_t start_mark, end_mark, current = edit->curs1;
2322 long col_delta = 0;
2323 off_t mark1, mark2;
2324 long c1, c2;
2325 off_t size;
2326 unsigned char *copy_buf;
2328 edit_update_curs_col (edit);
2329 if (eval_marks (edit, &start_mark, &end_mark))
2330 return;
2332 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2334 /* all that gets pushed are deletes hence little space is used on the stack */
2336 edit_push_markers (edit);
2338 if (edit->column_highlight)
2340 col_delta = abs (edit->column2 - edit->column1);
2341 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2343 else
2345 int size_orig = size;
2347 while (size-- != 0)
2348 edit_insert_ahead (edit, copy_buf[size]);
2350 /* Place cursor at the end of text selection */
2351 if (option_cursor_after_inserted_block)
2352 edit_cursor_move (edit, size_orig);
2355 g_free (copy_buf);
2356 edit_scroll_screen_over_cursor (edit);
2358 if (edit->column_highlight)
2359 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2360 else if (start_mark < current && end_mark > current)
2361 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2363 edit->force |= REDRAW_PAGE;
2367 /* --------------------------------------------------------------------------------------------- */
2369 void
2370 edit_block_move_cmd (WEdit * edit)
2372 off_t current;
2373 unsigned char *copy_buf = NULL;
2374 off_t start_mark, end_mark;
2376 if (eval_marks (edit, &start_mark, &end_mark))
2377 return;
2379 if (!edit->column_highlight && edit->curs1 > start_mark && edit->curs1 < end_mark)
2380 return;
2382 if (edit->mark2 < 0)
2383 edit_mark_cmd (edit, FALSE);
2384 edit_push_markers (edit);
2386 if (edit->column_highlight)
2388 off_t mark1, mark2;
2389 off_t size;
2390 long c1, c2, b_width;
2391 long x, x2;
2393 c1 = min (edit->column1, edit->column2);
2394 c2 = max (edit->column1, edit->column2);
2395 b_width = c2 - c1;
2397 edit_update_curs_col (edit);
2399 x = edit->curs_col;
2400 x2 = x + edit->over_col;
2402 /* do nothing when cursor inside first line of selected area */
2403 if ((edit_eol (edit, edit->curs1) == edit_eol (edit, start_mark)) && x2 > c1 && x2 <= c2)
2404 return;
2406 if (edit->curs1 > start_mark && edit->curs1 < edit_eol (edit, end_mark))
2408 if (x > c2)
2409 x -= b_width;
2410 else if (x > c1 && x <= c2)
2411 x = c1;
2413 /* save current selection into buffer */
2414 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2416 /* remove current selection */
2417 edit_block_delete_cmd (edit);
2419 edit->over_col = max (0, edit->over_col - b_width);
2420 /* calculate the cursor pos after delete block */
2421 current = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), x, 0);
2422 edit_cursor_move (edit, current - edit->curs1);
2423 edit_scroll_screen_over_cursor (edit);
2425 /* add TWS if need before block insertion */
2426 if (option_cursor_beyond_eol && edit->over_col > 0)
2427 edit_insert_over (edit);
2429 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2430 edit_set_markers (edit, mark1, mark2, c1, c2);
2432 else
2434 off_t count, count_orig;
2436 current = edit->curs1;
2437 copy_buf = g_malloc0 (end_mark - start_mark);
2438 edit_cursor_move (edit, start_mark - edit->curs1);
2439 edit_scroll_screen_over_cursor (edit);
2441 for (count = start_mark; count < end_mark; count++)
2442 copy_buf[end_mark - count - 1] = edit_delete (edit, TRUE);
2444 edit_scroll_screen_over_cursor (edit);
2445 edit_cursor_move (edit,
2446 current - edit->curs1 -
2447 (((current - edit->curs1) > 0) ? end_mark - start_mark : 0));
2448 edit_scroll_screen_over_cursor (edit);
2449 count_orig = count;
2450 while (count-- > start_mark)
2451 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2453 edit_set_markers (edit, edit->curs1, edit->curs1 + end_mark - start_mark, 0, 0);
2455 /* Place cursor at the end of text selection */
2456 if (option_cursor_after_inserted_block)
2457 edit_cursor_move (edit, count_orig - start_mark);
2460 edit_scroll_screen_over_cursor (edit);
2461 g_free (copy_buf);
2462 edit->force |= REDRAW_PAGE;
2465 /* --------------------------------------------------------------------------------------------- */
2466 /** returns 1 if canceelled by user */
2469 edit_block_delete_cmd (WEdit * edit)
2471 off_t start_mark, end_mark;
2472 if (eval_marks (edit, &start_mark, &end_mark))
2474 edit_delete_line (edit);
2475 return 0;
2477 return edit_block_delete (edit);
2480 /* --------------------------------------------------------------------------------------------- */
2481 /** call with edit = 0 before shutdown to close memory leaks */
2483 void
2484 edit_replace_cmd (WEdit * edit, int again)
2486 /* 1 = search string, 2 = replace with */
2487 static char *saved1 = NULL; /* saved default[123] */
2488 static char *saved2 = NULL;
2489 char *input1 = NULL; /* user input from the dialog */
2490 char *input2 = NULL;
2491 GString *input2_str = NULL;
2492 char *disp1 = NULL;
2493 char *disp2 = NULL;
2494 long times_replaced = 0;
2495 gboolean once_found = FALSE;
2497 if (!edit)
2499 g_free (saved1), saved1 = NULL;
2500 g_free (saved2), saved2 = NULL;
2501 return;
2504 edit->force |= REDRAW_COMPLETELY;
2506 if (again && !saved1 && !saved2)
2507 again = 0;
2509 if (again)
2511 input1 = g_strdup (saved1 ? saved1 : "");
2512 input2 = g_strdup (saved2 ? saved2 : "");
2514 else
2516 char *tmp_inp1, *tmp_inp2;
2518 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
2519 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
2521 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2523 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2525 g_free (disp1);
2526 g_free (disp2);
2528 if (input1 == NULL || *input1 == '\0')
2530 edit->force = REDRAW_COMPLETELY;
2531 goto cleanup;
2534 tmp_inp1 = input1;
2535 tmp_inp2 = input2;
2536 input1 = edit_replace_cmd__conv_to_input (input1);
2537 input2 = edit_replace_cmd__conv_to_input (input2);
2538 g_free (tmp_inp1);
2539 g_free (tmp_inp2);
2541 g_free (saved1), saved1 = g_strdup (input1);
2542 g_free (saved2), saved2 = g_strdup (input2);
2544 mc_search_free (edit->search);
2545 edit->search = NULL;
2548 input2_str = g_string_new (input2);
2550 if (!edit->search)
2552 edit->search = mc_search_new (input1, -1);
2553 if (edit->search == NULL)
2555 edit->search_start = edit->curs1;
2556 goto cleanup;
2558 edit->search->search_type = edit_search_options.type;
2559 edit->search->is_all_charsets = edit_search_options.all_codepages;
2560 edit->search->is_case_sensitive = edit_search_options.case_sens;
2561 edit->search->whole_words = edit_search_options.whole_words;
2562 edit->search->search_fn = edit_search_cmd_callback;
2563 edit->search_line_type = edit_get_search_line_type (edit->search);
2564 edit_search_fix_search_start_if_selection (edit);
2567 if (edit->found_len && edit->search_start == edit->found_start + 1
2568 && edit_search_options.backwards)
2569 edit->search_start--;
2571 if (edit->found_len && edit->search_start == edit->found_start - 1
2572 && !edit_search_options.backwards)
2573 edit->search_start++;
2577 gsize len = 0;
2579 if (!editcmd_find (edit, &len))
2581 if (!(edit->search->error == MC_SEARCH_E_OK ||
2582 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2584 edit_error_dialog (_("Search"), edit->search->error_str);
2586 break;
2588 once_found = TRUE;
2590 edit->search_start = edit->search->normal_offset;
2591 /*returns negative on not found or error in pattern */
2593 if ((edit->search_start >= 0) && (edit->search_start < edit->last_byte))
2595 gsize i;
2596 GString *repl_str;
2598 edit->found_start = edit->search_start;
2599 i = edit->found_len = len;
2601 edit_cursor_move (edit, edit->search_start - edit->curs1);
2602 edit_scroll_screen_over_cursor (edit);
2604 if (edit->replace_mode == 0)
2606 long l;
2607 int prompt;
2609 l = edit->curs_row - WIDGET (edit)->lines / 3;
2610 if (l > 0)
2611 edit_scroll_downward (edit, l);
2612 if (l < 0)
2613 edit_scroll_upward (edit, -l);
2615 edit_scroll_screen_over_cursor (edit);
2616 edit->force |= REDRAW_PAGE;
2617 edit_render_keypress (edit);
2619 /*so that undo stops at each query */
2620 edit_push_key_press (edit);
2621 /* and prompt 2/3 down */
2622 disp1 = edit_replace_cmd__conv_to_display (saved1);
2623 disp2 = edit_replace_cmd__conv_to_display (saved2);
2624 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2625 g_free (disp1);
2626 g_free (disp2);
2628 if (prompt == B_REPLACE_ALL)
2629 edit->replace_mode = 1;
2630 else if (prompt == B_SKIP_REPLACE)
2632 if (edit_search_options.backwards)
2633 edit->search_start--;
2634 else
2635 edit->search_start++;
2636 continue; /* loop */
2638 else if (prompt == B_CANCEL)
2640 edit->replace_mode = -1;
2641 break; /* loop */
2645 repl_str = mc_search_prepare_replace_str (edit->search, input2_str);
2647 if (edit->search->error != MC_SEARCH_E_OK)
2649 edit_error_dialog (_("Replace"), edit->search->error_str);
2650 g_string_free (repl_str, TRUE);
2651 break;
2654 /* delete then insert new */
2655 for (i = 0; i < len; i++)
2656 edit_delete (edit, TRUE);
2658 for (i = 0; i < repl_str->len; i++)
2659 edit_insert (edit, repl_str->str[i]);
2661 edit->found_len = repl_str->len;
2662 g_string_free (repl_str, TRUE);
2663 times_replaced++;
2665 /* so that we don't find the same string again */
2666 if (edit_search_options.backwards)
2668 edit->search_start--;
2670 else
2672 edit->search_start += edit->found_len + (len == 0 ? 1 : 0);
2674 if (edit->search_start >= edit->last_byte)
2675 break;
2678 edit_scroll_screen_over_cursor (edit);
2680 else
2682 /* try and find from right here for next search */
2683 edit->search_start = edit->curs1;
2684 edit_update_curs_col (edit);
2686 edit->force |= REDRAW_PAGE;
2687 edit_render_keypress (edit);
2689 if (times_replaced == 0)
2690 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL, 1, _("&OK"));
2691 break;
2694 while (edit->replace_mode >= 0);
2696 edit_scroll_screen_over_cursor (edit);
2697 edit->force |= REDRAW_COMPLETELY;
2698 edit_render_keypress (edit);
2700 if ((edit->replace_mode == 1) && (times_replaced != 0))
2701 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2703 cleanup:
2704 g_free (input1);
2705 g_free (input2);
2706 if (input2_str != NULL)
2707 g_string_free (input2_str, TRUE);
2710 /* --------------------------------------------------------------------------------------------- */
2712 mc_search_cbret_t
2713 edit_search_cmd_callback (const void *user_data, gsize char_offset, int *current_char)
2715 *current_char = edit_get_byte ((WEdit *) user_data, (off_t) char_offset);
2716 return MC_SEARCH_CB_OK;
2719 /* --------------------------------------------------------------------------------------------- */
2721 void
2722 edit_search_cmd (WEdit * edit, gboolean again)
2725 if (edit == NULL)
2726 return;
2728 if (!again)
2729 edit_search (edit);
2730 else if (edit->last_search_string != NULL)
2731 edit_do_search (edit);
2732 else
2734 /* find last search string in history */
2735 GList *history;
2737 history = history_get (MC_HISTORY_SHARED_SEARCH);
2738 if (history != NULL && history->data != NULL)
2740 edit->last_search_string = (char *) history->data;
2741 history->data = NULL;
2742 history = g_list_first (history);
2743 g_list_foreach (history, (GFunc) g_free, NULL);
2744 g_list_free (history);
2746 edit->search = mc_search_new (edit->last_search_string, -1);
2747 if (edit->search == NULL)
2749 /* if not... then ask for an expression */
2750 g_free (edit->last_search_string);
2751 edit->last_search_string = NULL;
2752 edit_search (edit);
2754 else
2756 edit->search->search_type = edit_search_options.type;
2757 edit->search->is_all_charsets = edit_search_options.all_codepages;
2758 edit->search->is_case_sensitive = edit_search_options.case_sens;
2759 edit->search->whole_words = edit_search_options.whole_words;
2760 edit->search->search_fn = edit_search_cmd_callback;
2761 edit->search_line_type = edit_get_search_line_type (edit->search);
2762 edit_do_search (edit);
2765 else
2767 /* if not... then ask for an expression */
2768 g_free (edit->last_search_string);
2769 edit->last_search_string = NULL;
2770 edit_search (edit);
2776 /* --------------------------------------------------------------------------------------------- */
2778 * Check if it's OK to close the file. If there are unsaved changes, ask user.
2780 * @return TRUE if it's OK to exit, FALSE to continue editing.
2783 gboolean
2784 edit_ok_to_exit (WEdit * edit)
2786 char *fname = (char *) N_("[NoName]");
2787 char *msg;
2788 int act;
2790 if (!edit->modified)
2791 return TRUE;
2793 if (edit->filename_vpath != NULL)
2794 fname = vfs_path_to_str (edit->filename_vpath);
2795 #ifdef ENABLE_NLS
2796 else
2797 fname = g_strdup (_(fname));
2798 #else
2799 else
2800 fname = g_strdup (fname);
2801 #endif
2803 if (!mc_global.midnight_shutdown)
2805 if (!edit_check_newline (edit))
2807 g_free (fname);
2808 return FALSE;
2811 query_set_sel (2);
2813 msg = g_strdup_printf (_("File %s was modified.\nSave before close?"), fname);
2814 act = edit_query_dialog3 (_("Close file"), msg, _("&Yes"), _("&No"), _("&Cancel"));
2816 else
2818 msg = g_strdup_printf (_("Midnight Commander is being shut down.\nSave modified file %s?"),
2819 fname);
2820 act = edit_query_dialog2 (_("Quit"), msg, _("&Yes"), _("&No"));
2822 /* Esc is No */
2823 if (act == -1)
2824 act = 1;
2827 g_free (msg);
2828 g_free (fname);
2830 switch (act)
2832 case 0: /* Yes */
2833 edit_push_markers (edit);
2834 edit_set_markers (edit, 0, 0, 0, 0);
2835 if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2836 return mc_global.midnight_shutdown;
2837 break;
2838 case 1: /* No */
2839 break;
2840 case 2: /* Cancel quit */
2841 case -1: /* Esc */
2842 return FALSE;
2845 return TRUE;
2848 /* --------------------------------------------------------------------------------------------- */
2849 /** save block, returns TRUE on success */
2851 gboolean
2852 edit_save_block (WEdit * edit, const char *filename, off_t start, off_t finish)
2854 int file;
2855 off_t len = 1;
2856 vfs_path_t *vpath;
2858 vpath = vfs_path_from_str (filename);
2859 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2860 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2861 vfs_path_free (vpath);
2862 if (file == -1)
2863 return FALSE;
2865 if (edit->column_highlight)
2867 int r;
2869 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2870 if (r > 0)
2872 unsigned char *block, *p;
2874 p = block = edit_get_block (edit, start, finish, &len);
2875 while (len)
2877 r = mc_write (file, p, len);
2878 if (r < 0)
2879 break;
2880 p += r;
2881 len -= r;
2883 g_free (block);
2886 else
2888 unsigned char *buf;
2889 off_t i = start;
2890 off_t end;
2892 len = finish - start;
2893 buf = g_malloc0 (TEMP_BUF_LEN);
2894 while (start != finish)
2896 end = min (finish, start + TEMP_BUF_LEN);
2897 for (; i < end; i++)
2898 buf[i - start] = edit_get_byte (edit, i);
2899 len -= mc_write (file, (char *) buf, end - start);
2900 start = end;
2902 g_free (buf);
2904 mc_close (file);
2906 return (len == 0);
2909 /* --------------------------------------------------------------------------------------------- */
2911 void
2912 edit_paste_from_history (WEdit * edit)
2914 (void) edit;
2915 edit_error_dialog (_("Error"), _("This function is not implemented"));
2918 /* --------------------------------------------------------------------------------------------- */
2920 gboolean
2921 edit_copy_to_X_buf_cmd (WEdit * edit)
2923 off_t start_mark, end_mark;
2925 if (eval_marks (edit, &start_mark, &end_mark))
2926 return TRUE;
2927 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2929 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2930 return FALSE;
2932 /* try use external clipboard utility */
2933 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2935 return TRUE;
2938 /* --------------------------------------------------------------------------------------------- */
2940 gboolean
2941 edit_cut_to_X_buf_cmd (WEdit * edit)
2943 off_t start_mark, end_mark;
2945 if (eval_marks (edit, &start_mark, &end_mark))
2946 return TRUE;
2947 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2949 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2950 return FALSE;
2952 /* try use external clipboard utility */
2953 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2955 edit_block_delete_cmd (edit);
2956 edit_mark_cmd (edit, TRUE);
2958 return TRUE;
2961 /* --------------------------------------------------------------------------------------------- */
2963 gboolean
2964 edit_paste_from_X_buf_cmd (WEdit * edit)
2966 vfs_path_t *tmp;
2967 gboolean ret;
2969 /* try use external clipboard utility */
2970 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
2971 tmp = mc_config_get_full_vpath (EDIT_CLIP_FILE);
2972 ret = (edit_insert_file (edit, tmp) >= 0);
2973 vfs_path_free (tmp);
2975 return ret;
2978 /* --------------------------------------------------------------------------------------------- */
2980 * Ask user for the line and go to that line.
2981 * Negative numbers mean line from the end (i.e. -1 is the last line).
2984 void
2985 edit_goto_cmd (WEdit * edit)
2987 char *f;
2988 static long line = 0; /* line as typed, saved as default */
2989 long l;
2990 char *error;
2991 char s[32];
2993 g_snprintf (s, sizeof (s), "%ld", line);
2994 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE, line ? s : "",
2995 INPUT_COMPLETE_NONE);
2996 if (!f)
2997 return;
2999 if (!*f)
3001 g_free (f);
3002 return;
3005 l = strtol (f, &error, 0);
3006 if (*error)
3008 g_free (f);
3009 return;
3012 line = l;
3013 if (l < 0)
3014 l = edit->total_lines + l + 2;
3015 edit_move_display (edit, l - WIDGET (edit)->lines / 2 - 1);
3016 edit_move_to_line (edit, l - 1);
3017 edit->force |= REDRAW_COMPLETELY;
3018 g_free (f);
3022 /* --------------------------------------------------------------------------------------------- */
3023 /** Return TRUE on success */
3025 gboolean
3026 edit_save_block_cmd (WEdit * edit)
3028 off_t start_mark, end_mark;
3029 char *exp, *tmp;
3030 gboolean ret = FALSE;
3032 if (eval_marks (edit, &start_mark, &end_mark))
3033 return TRUE;
3035 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3036 exp =
3037 input_expand_dialog (_("Save block"), _("Enter file name:"),
3038 MC_HISTORY_EDIT_SAVE_BLOCK, tmp, INPUT_COMPLETE_FILENAMES);
3039 g_free (tmp);
3040 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3042 if (exp != NULL && *exp != '\0')
3044 if (edit_save_block (edit, exp, start_mark, end_mark))
3045 ret = TRUE;
3046 else
3047 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3049 edit->force |= REDRAW_COMPLETELY;
3052 g_free (exp);
3054 return ret;
3058 /* --------------------------------------------------------------------------------------------- */
3060 /** returns TRUE on success */
3061 gboolean
3062 edit_insert_file_cmd (WEdit * edit)
3064 char *tmp;
3065 char *exp;
3066 gboolean ret = FALSE;
3068 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3069 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3070 MC_HISTORY_EDIT_INSERT_FILE, tmp, INPUT_COMPLETE_FILENAMES);
3071 g_free (tmp);
3073 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3075 if (exp != NULL && *exp != '\0')
3077 vfs_path_t *exp_vpath;
3079 exp_vpath = vfs_path_from_str (exp);
3080 ret = (edit_insert_file (edit, exp_vpath) >= 0);
3081 vfs_path_free (exp_vpath);
3083 if (!ret)
3084 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3087 g_free (exp);
3089 edit->force |= REDRAW_COMPLETELY;
3090 return ret;
3093 /* --------------------------------------------------------------------------------------------- */
3094 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3097 edit_sort_cmd (WEdit * edit)
3099 static char *old = 0;
3100 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3101 off_t start_mark, end_mark;
3102 int e;
3104 if (eval_marks (edit, &start_mark, &end_mark))
3106 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3107 return 0;
3110 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
3111 edit_save_block (edit, tmp, start_mark, end_mark);
3112 g_free (tmp);
3114 exp = input_dialog (_("Run sort"),
3115 _("Enter sort options (see manpage) separated by whitespace:"),
3116 MC_HISTORY_EDIT_SORT, (old != NULL) ? old : "", INPUT_COMPLETE_NONE);
3118 if (!exp)
3119 return 1;
3120 g_free (old);
3121 old = exp;
3122 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
3123 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
3124 tmp =
3125 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3126 " > ", tmp_edit_temp_name, (char *) NULL);
3127 g_free (tmp_edit_temp_name);
3128 g_free (tmp_edit_block_name);
3130 e = system (tmp);
3131 g_free (tmp);
3132 if (e)
3134 if (e == -1 || e == 127)
3136 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3138 else
3140 char q[8];
3141 sprintf (q, "%d ", e);
3142 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3143 edit_error_dialog (_("Sort"), tmp);
3144 g_free (tmp);
3146 return -1;
3149 edit->force |= REDRAW_COMPLETELY;
3151 if (edit_block_delete_cmd (edit))
3152 return 1;
3155 vfs_path_t *tmp_vpath;
3157 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3158 edit_insert_file (edit, tmp_vpath);
3159 vfs_path_free (tmp_vpath);
3161 return 0;
3164 /* --------------------------------------------------------------------------------------------- */
3166 * Ask user for a command, execute it and paste its output back to the
3167 * editor.
3171 edit_ext_cmd (WEdit * edit)
3173 char *exp, *tmp, *tmp_edit_temp_file;
3174 int e;
3176 exp =
3177 input_dialog (_("Paste output of external command"),
3178 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, NULL,
3179 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_VARIABLES | INPUT_COMPLETE_USERNAMES
3180 | INPUT_COMPLETE_HOSTNAMES | INPUT_COMPLETE_CD | INPUT_COMPLETE_COMMANDS |
3181 INPUT_COMPLETE_SHELL_ESC);
3183 if (!exp)
3184 return 1;
3186 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3187 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3188 g_free (tmp_edit_temp_file);
3189 e = system (tmp);
3190 g_free (tmp);
3191 g_free (exp);
3193 if (e)
3195 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3196 return -1;
3199 edit->force |= REDRAW_COMPLETELY;
3202 vfs_path_t *tmp_vpath;
3204 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3205 edit_insert_file (edit, tmp_vpath);
3206 vfs_path_free (tmp_vpath);
3208 return 0;
3211 /* --------------------------------------------------------------------------------------------- */
3212 /** if block is 1, a block must be highlighted and the shell command
3213 processes it. If block is 0 the shell command is a straight system
3214 command, that just produces some output which is to be inserted */
3216 void
3217 edit_block_process_cmd (WEdit * edit, int macro_number)
3219 char *fname;
3220 char *macros_fname = NULL;
3222 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3223 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3224 user_menu (edit, macros_fname, 0);
3225 g_free (fname);
3226 g_free (macros_fname);
3227 edit->force |= REDRAW_COMPLETELY;
3230 /* --------------------------------------------------------------------------------------------- */
3232 void
3233 edit_mail_dialog (WEdit * edit)
3235 char *tmail_to;
3236 char *tmail_subject;
3237 char *tmail_cc;
3239 static char *mail_cc_last = 0;
3240 static char *mail_subject_last = 0;
3241 static char *mail_to_last = 0;
3244 quick_widget_t quick_widgets[] = {
3245 /* *INDENT-OFF* */
3246 QUICK_LABEL (N_("mail -s <subject> -c <cc> <to>"), NULL),
3247 QUICK_LABELED_INPUT (N_("To"), input_label_above,
3248 mail_to_last != NULL ? mail_to_last : "", "mail-dlg-input-3",
3249 &tmail_to, NULL, FALSE, FALSE, INPUT_COMPLETE_USERNAMES),
3250 QUICK_LABELED_INPUT (N_("Subject"), input_label_above,
3251 mail_subject_last != NULL ? mail_subject_last : "", "mail-dlg-input-2",
3252 &tmail_subject, NULL, FALSE, FALSE, INPUT_COMPLETE_NONE),
3253 QUICK_LABELED_INPUT (N_("Copies to"), input_label_above,
3254 mail_cc_last != NULL ? mail_cc_last : "", "mail-dlg-input",
3255 &tmail_cc, NULL, FALSE, FALSE, INPUT_COMPLETE_USERNAMES),
3256 QUICK_BUTTONS_OK_CANCEL,
3257 QUICK_END
3258 /* *INDENT-ON* */
3261 quick_dialog_t qdlg = {
3262 -1, -1, 50,
3263 N_("Mail"), "[Input Line Keys]",
3264 quick_widgets, NULL, NULL
3267 if (quick_dialog (&qdlg) != B_CANCEL)
3269 g_free (mail_cc_last);
3270 g_free (mail_subject_last);
3271 g_free (mail_to_last);
3272 mail_cc_last = tmail_cc;
3273 mail_subject_last = tmail_subject;
3274 mail_to_last = tmail_to;
3275 pipe_mail (edit, mail_to_last, mail_subject_last, mail_cc_last);
3279 /* --------------------------------------------------------------------------------------------- */
3281 /*******************/
3282 /* Word Completion */
3283 /*******************/
3286 * Complete current word using regular expression search
3287 * backwards beginning at the current cursor position.
3290 void
3291 edit_complete_word_cmd (WEdit * edit)
3293 gsize i, max_len, word_len = 0, num_compl = 0;
3294 off_t word_start = 0;
3295 GString *match_expr;
3296 GString *compl[MAX_WORD_COMPLETIONS]; /* completions */
3298 /* search start of word to be completed */
3299 if (!edit_find_word_start (edit, &word_start, &word_len))
3300 return;
3302 /* prepare match expression */
3303 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3304 match_expr = g_string_new ("(^|\\s+|\\b)");
3305 for (i = 0; i < word_len; i++)
3306 g_string_append_c (match_expr, edit_get_byte (edit, word_start + i));
3307 g_string_append (match_expr,
3308 "[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+");
3310 /* collect the possible completions */
3311 /* start search from begin to end of file */
3312 max_len =
3313 edit_collect_completions (edit, word_start, word_len, match_expr->str, (GString **) & compl,
3314 &num_compl);
3316 if (num_compl > 0)
3318 /* insert completed word if there is only one match */
3319 if (num_compl == 1)
3320 edit_complete_word_insert_recoded_completion (edit, compl[0]->str, word_len);
3321 /* more than one possible completion => ask the user */
3322 else
3324 char *curr_compl;
3326 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3327 /* !!! pressed again the selection dialog pops up, but that !!! */
3328 /* !!! seems to require a further internal state !!! */
3329 /*tty_beep (); */
3331 /* let the user select the preferred completion */
3332 curr_compl = editcmd_dialog_completion_show (edit, max_len,
3333 (GString **) & compl, num_compl);
3335 if (curr_compl != NULL)
3337 edit_complete_word_insert_recoded_completion (edit, curr_compl, word_len);
3338 g_free (curr_compl);
3343 g_string_free (match_expr, TRUE);
3344 /* release memory before return */
3345 for (i = 0; i < num_compl; i++)
3346 g_string_free (compl[i], TRUE);
3349 /* --------------------------------------------------------------------------------------------- */
3351 #ifdef HAVE_CHARSET
3352 void
3353 edit_select_codepage_cmd (WEdit * edit)
3355 if (do_select_codepage ())
3356 edit_set_codeset (edit);
3358 edit->force = REDRAW_PAGE;
3359 widget_redraw (WIDGET (edit));
3361 #endif
3363 /* --------------------------------------------------------------------------------------------- */
3365 void
3366 edit_insert_literal_cmd (WEdit * edit)
3368 int char_for_insertion;
3370 char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3371 _("Press any key:"), FALSE);
3372 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3375 /* --------------------------------------------------------------------------------------------- */
3377 void
3378 edit_begin_end_macro_cmd (WEdit * edit)
3380 /* edit is a pointer to the widget */
3381 if (edit != NULL)
3383 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3384 edit_execute_key_command (edit, command, -1);
3388 /* --------------------------------------------------------------------------------------------- */
3390 void
3391 edit_begin_end_repeat_cmd (WEdit * edit)
3393 /* edit is a pointer to the widget */
3394 if (edit != NULL)
3396 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3397 edit_execute_key_command (edit, command, -1);
3401 /* --------------------------------------------------------------------------------------------- */
3403 gboolean
3404 edit_load_forward_cmd (WEdit * edit)
3406 if (edit->modified
3407 && edit_query_dialog2 (_("Warning"),
3408 _("Current text was modified without a file save.\n"
3409 "Continue discards these changes"), _("C&ontinue"),
3410 _("&Cancel")) == 1)
3412 edit->force |= REDRAW_COMPLETELY;
3413 return TRUE;
3416 if (edit_stack_iterator + 1 >= MAX_HISTORY_MOVETO)
3417 return FALSE;
3419 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3420 return FALSE;
3422 edit_stack_iterator++;
3423 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3424 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3425 edit_history_moveto[edit_stack_iterator].line);
3427 return FALSE;
3430 /* --------------------------------------------------------------------------------------------- */
3432 gboolean
3433 edit_load_back_cmd (WEdit * edit)
3435 if (edit->modified
3436 && edit_query_dialog2 (_("Warning"),
3437 _("Current text was modified without a file save.\n"
3438 "Continue discards these changes"), _("C&ontinue"),
3439 _("&Cancel")) == 1)
3441 edit->force |= REDRAW_COMPLETELY;
3442 return TRUE;
3445 /* we are in the bottom of the stack, NO WAY! */
3446 if (edit_stack_iterator == 0)
3447 return FALSE;
3449 edit_stack_iterator--;
3450 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3451 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3452 edit_history_moveto[edit_stack_iterator].line);
3454 return FALSE;
3457 /* --------------------------------------------------------------------------------------------- */
3459 void
3460 edit_get_match_keyword_cmd (WEdit * edit)
3462 gsize word_len = 0, max_len = 0;
3463 int num_def = 0;
3464 gsize i;
3465 off_t word_start = 0;
3466 GString *match_expr;
3467 char *path = NULL;
3468 char *ptr = NULL;
3469 char *tagfile = NULL;
3471 etags_hash_t def_hash[MAX_DEFINITIONS];
3473 for (i = 0; i < MAX_DEFINITIONS; i++)
3475 def_hash[i].filename = NULL;
3478 /* search start of word to be completed */
3479 if (!edit_find_word_start (edit, &word_start, &word_len))
3480 return;
3482 /* prepare match expression */
3483 match_expr = g_string_sized_new (word_len);
3484 for (i = 0; i < word_len; i++)
3485 g_string_append_c (match_expr, edit_get_byte (edit, word_start + i));
3487 ptr = g_get_current_dir ();
3488 path = g_strconcat (ptr, G_DIR_SEPARATOR_S, (char *) NULL);
3489 g_free (ptr);
3491 /* Recursive search file 'TAGS' in parent dirs */
3494 ptr = g_path_get_dirname (path);
3495 g_free (path);
3496 path = ptr;
3497 g_free (tagfile);
3498 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3499 if (exist_file (tagfile))
3500 break;
3502 while (strcmp (path, G_DIR_SEPARATOR_S) != 0);
3504 if (tagfile)
3506 num_def =
3507 etags_set_definition_hash (tagfile, path, match_expr->str, (etags_hash_t *) & def_hash);
3508 g_free (tagfile);
3510 g_free (path);
3512 max_len = MAX_WIDTH_DEF_DIALOG;
3513 word_len = 0;
3514 if (num_def > 0)
3516 editcmd_dialog_select_definition_show (edit, match_expr->str, max_len, word_len,
3517 (etags_hash_t *) & def_hash, num_def);
3519 g_string_free (match_expr, TRUE);
3522 /* --------------------------------------------------------------------------------------------- */
3524 #ifdef HAVE_ASPELL
3526 edit_suggest_current_word (WEdit * edit)
3528 gsize cut_len = 0;
3529 gsize word_len = 0;
3530 off_t word_start = 0;
3531 int retval = B_SKIP_WORD;
3532 char *match_word;
3534 /* search start of word to spell check */
3535 match_word = edit_get_word_from_pos (edit, edit->curs1, &word_start, &word_len, &cut_len);
3537 #ifdef HAVE_CHARSET
3538 if (mc_global.source_codepage >= 0 && (mc_global.source_codepage != mc_global.display_codepage))
3540 GString *tmp_word;
3542 tmp_word = str_convert_to_display (match_word);
3543 g_free (match_word);
3544 match_word = g_string_free (tmp_word, FALSE);
3546 #endif
3547 if (!aspell_check (match_word, (int) word_len))
3549 GArray *suggest;
3550 unsigned int res;
3552 suggest = g_array_new (TRUE, FALSE, sizeof (char *));
3554 res = aspell_suggest (suggest, match_word, (int) word_len);
3555 if (res != 0)
3557 char *new_word = NULL;
3559 edit->found_start = word_start;
3560 edit->found_len = word_len;
3561 edit->force |= REDRAW_PAGE;
3562 edit_scroll_screen_over_cursor (edit);
3563 edit_render_keypress (edit);
3565 retval = spell_dialog_spell_suggest_show (edit, match_word, &new_word, suggest);
3566 edit_cursor_move (edit, word_len - cut_len);
3568 if (retval == B_ENTER && new_word != NULL)
3570 guint i;
3571 char *cp_word;
3573 #ifdef HAVE_CHARSET
3574 if (mc_global.source_codepage >= 0 &&
3575 (mc_global.source_codepage != mc_global.display_codepage))
3577 GString *tmp_word;
3579 tmp_word = str_convert_to_input (new_word);
3580 g_free (new_word);
3581 new_word = g_string_free (tmp_word, FALSE);
3583 #endif
3584 cp_word = new_word;
3585 for (i = 0; i < word_len; i++)
3586 edit_backspace (edit, TRUE);
3587 for (; *new_word; new_word++)
3588 edit_insert (edit, *new_word);
3589 g_free (cp_word);
3591 else if (retval == B_ADD_WORD && match_word != NULL)
3592 aspell_add_to_dict (match_word, (int) word_len);
3595 g_array_free (suggest, TRUE);
3596 edit->found_start = 0;
3597 edit->found_len = 0;
3599 g_free (match_word);
3600 return retval;
3603 /* --------------------------------------------------------------------------------------------- */
3605 void
3606 edit_spellcheck_file (WEdit * edit)
3608 if (edit->curs_line > 0)
3610 edit_cursor_move (edit, -edit->curs1);
3611 edit_move_to_prev_col (edit, 0);
3612 edit_update_curs_row (edit);
3617 int c1, c2;
3619 c2 = edit_get_byte (edit, edit->curs1);
3623 if (edit->curs1 >= edit->last_byte)
3624 return;
3626 c1 = c2;
3627 edit_cursor_move (edit, 1);
3628 c2 = edit_get_byte (edit, edit->curs1);
3630 while (is_break_char (c1) || is_break_char (c2));
3632 while (edit_suggest_current_word (edit) != B_CANCEL);
3635 /* --------------------------------------------------------------------------------------------- */
3637 void
3638 edit_set_spell_lang (void)
3640 GArray *lang_list;
3642 lang_list = g_array_new (TRUE, FALSE, sizeof (char *));
3643 if (aspell_get_lang_list (lang_list) != 0)
3645 char *lang;
3647 lang = spell_dialog_lang_list_show (lang_list);
3648 if (lang != NULL)
3649 (void) aspell_set_lang (lang);
3651 aspell_array_clean (lang_list);
3653 #endif /* HAVE_ASPELL */
3655 /* --------------------------------------------------------------------------------------------- */