cppcheck: reduce variable scope.
[midnight-commander.git] / src / editor / editcmd.c
blob8c6b9206f6964c33dbe137e90945630cf1eccd9b
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() */
77 #include "edit-impl.h"
78 #include "editwidget.h"
79 #include "editcmd_dialogs.h"
80 #ifdef HAVE_ASPELL
81 #include "spell.h"
82 #include "spell_dialogs.h"
83 #endif
84 #include "etags.h"
86 /*** global variables ****************************************************************************/
88 /* search and replace: */
89 int search_create_bookmark = FALSE;
91 /* queries on a save */
92 int edit_confirm_save = 1;
94 /*** file scope macro definitions ****************************************************************/
96 #define space_width 1
98 #define TEMP_BUF_LEN 1024
100 #define INPUT_INDEX 9
102 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
103 (and the above) routines to work properly - paul */
105 #define is_digit(x) ((x) >= '0' && (x) <= '9')
107 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
109 /*** file scope type declarations ****************************************************************/
111 /*** file scope variables ************************************************************************/
113 static unsigned long edit_save_mode_radio_id, edit_save_mode_input_id;
115 /* --------------------------------------------------------------------------------------------- */
116 /*** file scope functions ************************************************************************/
117 /* --------------------------------------------------------------------------------------------- */
119 static cb_ret_t
120 edit_save_mode_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
122 switch (msg)
124 case MSG_ACTION:
125 if (sender != NULL && sender->id == edit_save_mode_radio_id)
127 Widget *ww;
129 ww = dlg_find_by_id (DIALOG (w), edit_save_mode_input_id);
130 widget_disable (ww, RADIO (sender)->sel != 2);
131 return MSG_HANDLED;
134 return MSG_NOT_HANDLED;
136 default:
137 return dlg_default_callback (w, sender, msg, parm, data);
141 /* --------------------------------------------------------------------------------------------- */
143 /* If 0 (quick save) then a) create/truncate <filename> file,
144 b) save to <filename>;
145 if 1 (safe save) then a) save to <tempnam>,
146 b) rename <tempnam> to <filename>;
147 if 2 (do backups) then a) save to <tempnam>,
148 b) rename <filename> to <filename.backup_ext>,
149 c) rename <tempnam> to <filename>. */
151 /* returns 0 on error, -1 on abort */
153 static int
154 edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath)
156 char *p;
157 gchar *tmp;
158 off_t filelen = 0;
159 int this_save_mode, fd = -1;
160 vfs_path_t *real_filename_vpath;
161 vfs_path_t *savename_vpath = NULL;
162 const char *start_filename;
163 const vfs_path_element_t *vpath_element;
165 vpath_element = vfs_path_get_by_index (filename_vpath, 0);
166 if (vpath_element == NULL)
167 return 0;
169 start_filename = vpath_element->path;
170 if (*start_filename == '\0')
171 return 0;
173 if (*start_filename != PATH_SEP && edit->dir_vpath != NULL)
175 real_filename_vpath = vfs_path_append_vpath_new (edit->dir_vpath, filename_vpath, NULL);
177 else
179 real_filename_vpath = vfs_path_clone (filename_vpath);
182 this_save_mode = option_save_mode;
183 if (this_save_mode != EDIT_QUICK_SAVE)
185 if (!vfs_file_is_local (real_filename_vpath)
186 || (fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY)) == -1)
189 * The file does not exists yet, so no safe save or
190 * backup are necessary.
192 this_save_mode = EDIT_QUICK_SAVE;
194 if (fd != -1)
195 mc_close (fd);
198 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt)
200 int rv;
201 struct stat sb;
203 rv = mc_stat (real_filename_vpath, &sb);
204 if (rv == 0 && sb.st_nlink > 1)
206 rv = edit_query_dialog3 (_("Warning"),
207 _("File has hard-links. Detach before saving?"),
208 _("&Yes"), _("&No"), _("&Cancel"));
209 switch (rv)
211 case 0:
212 this_save_mode = EDIT_SAFE_SAVE;
213 /* fallthrough */
214 case 1:
215 edit->skip_detach_prompt = 1;
216 break;
217 default:
218 vfs_path_free (real_filename_vpath);
219 return -1;
223 /* Prevent overwriting changes from other editor sessions. */
224 if (rv == 0 && edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
226 /* The default action is "Cancel". */
227 query_set_sel (1);
229 rv = edit_query_dialog2 (_("Warning"),
230 _("The file has been modified in the meantime. Save anyway?"),
231 _("&Yes"), _("&Cancel"));
232 if (rv != 0)
234 vfs_path_free (real_filename_vpath);
235 return -1;
240 if (this_save_mode != EDIT_QUICK_SAVE)
242 char *savedir, *saveprefix;
244 savedir = vfs_path_tokens_get (real_filename_vpath, 0, -1);
245 if (savedir == NULL)
246 savedir = g_strdup (".");
248 /* Token-related function never return leading slash, so we need add it manually */
249 saveprefix = mc_build_filename ("/", savedir, "cooledit", NULL);
250 g_free (savedir);
251 fd = mc_mkstemps (&savename_vpath, saveprefix, NULL);
252 g_free (saveprefix);
253 if (savename_vpath == NULL)
255 vfs_path_free (real_filename_vpath);
256 return 0;
258 /* FIXME:
259 * Close for now because mc_mkstemps use pure open system call
260 * to create temporary file and it needs to be reopened by
261 * VFS-aware mc_open().
263 close (fd);
265 else
266 savename_vpath = vfs_path_clone (real_filename_vpath);
268 (void) mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
269 (void) mc_chmod (savename_vpath, edit->stat1.st_mode);
271 fd = mc_open (savename_vpath, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
272 if (fd == -1)
273 goto error_save;
275 /* pipe save */
276 p = edit_get_write_filter (savename_vpath, real_filename_vpath);
277 if (p != NULL)
279 FILE *file;
281 mc_close (fd);
282 file = (FILE *) popen (p, "w");
284 if (file)
286 filelen = edit_write_stream (edit, file);
287 #if 1
288 pclose (file);
289 #else
290 if (pclose (file) != 0)
292 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
293 edit_error_dialog (_("Error"), tmp);
294 g_free (tmp);
295 g_free (p);
296 goto error_save;
298 #endif
300 else
302 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
303 edit_error_dialog (_("Error"), get_sys_error (tmp));
304 g_free (p);
305 g_free (tmp);
306 goto error_save;
308 g_free (p);
310 else if (edit->lb == LB_ASIS)
311 { /* do not change line breaks */
312 filelen = edit_buffer_write_file (&edit->buffer, fd);
314 if (filelen != edit->buffer.size)
316 mc_close (fd);
317 goto error_save;
319 if (mc_close (fd) != 0)
320 goto error_save;
321 /* Update the file information, especially the mtime. */
322 if (mc_stat (savename_vpath, &edit->stat1) == -1)
323 goto error_save;
325 else
326 { /* change line breaks */
327 FILE *file;
328 const vfs_path_element_t *path_element;
330 mc_close (fd);
332 path_element = vfs_path_get_by_index (savename_vpath, -1);
333 file = (FILE *) fopen (path_element->path, "w");
334 if (file != NULL)
336 filelen = edit_write_stream (edit, file);
337 fclose (file);
339 else
341 char *msg;
343 msg = g_strdup_printf (_("Cannot open file for writing: %s"), path_element->path);
344 edit_error_dialog (_("Error"), msg);
345 g_free (msg);
346 goto error_save;
350 if (filelen != edit->buffer.size)
351 goto error_save;
353 if (this_save_mode == EDIT_DO_BACKUP)
355 char *tmp_store_filename;
356 vfs_path_element_t *last_vpath_element;
357 vfs_path_t *tmp_vpath;
358 gboolean ok;
360 #ifdef HAVE_ASSERT_H
361 assert (option_backup_ext != NULL);
362 #endif
363 /* add backup extension to the path */
364 tmp_vpath = vfs_path_clone (real_filename_vpath);
365 last_vpath_element = (vfs_path_element_t *) vfs_path_get_by_index (tmp_vpath, -1);
366 tmp_store_filename = last_vpath_element->path;
367 last_vpath_element->path = g_strdup_printf ("%s%s", tmp_store_filename, option_backup_ext);
368 g_free (tmp_store_filename);
370 ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
371 vfs_path_free (tmp_vpath);
372 if (!ok)
373 goto error_save;
376 if (this_save_mode != EDIT_QUICK_SAVE)
377 if (mc_rename (savename_vpath, real_filename_vpath) == -1)
378 goto error_save;
380 vfs_path_free (real_filename_vpath);
381 vfs_path_free (savename_vpath);
382 return 1;
383 error_save:
384 /* FIXME: Is this safe ?
385 * if (this_save_mode != EDIT_QUICK_SAVE)
386 * mc_unlink (savename);
388 vfs_path_free (real_filename_vpath);
389 vfs_path_free (savename_vpath);
390 return 0;
393 /* --------------------------------------------------------------------------------------------- */
395 static gboolean
396 edit_check_newline (const edit_buffer_t * buf)
398 return !(option_check_nl_at_eof && buf->size > 0
399 && edit_buffer_get_byte (buf, buf->size - 1) != '\n'
400 && edit_query_dialog2 (_("Warning"),
401 _("The file you are saving is not finished with a newline"),
402 _("C&ontinue"), _("&Cancel")) != 0);
405 /* --------------------------------------------------------------------------------------------- */
407 static vfs_path_t *
408 edit_get_save_file_as (WEdit * edit)
410 static LineBreaks cur_lb = LB_ASIS;
411 char *filename_res;
412 vfs_path_t *ret_vpath = NULL;
414 const char *lb_names[LB_NAMES] = {
415 N_("&Do not change"),
416 N_("&Unix format (LF)"),
417 N_("&Windows/DOS format (CR LF)"),
418 N_("&Macintosh format (CR)")
421 quick_widget_t quick_widgets[] = {
422 /* *INDENT-OFF* */
423 QUICK_LABELED_INPUT (N_("Enter file name:"), input_label_above,
424 vfs_path_as_str (edit->filename_vpath), "save-as",
425 &filename_res, NULL, FALSE, FALSE, INPUT_COMPLETE_FILENAMES),
426 QUICK_SEPARATOR (TRUE),
427 QUICK_LABEL (N_("Change line breaks to:"), NULL),
428 QUICK_RADIO (LB_NAMES, lb_names, (int *) &cur_lb, NULL),
429 QUICK_BUTTONS_OK_CANCEL,
430 QUICK_END
431 /* *INDENT-ON* */
434 quick_dialog_t qdlg = {
435 -1, -1, 64,
436 N_("Save As"), "[Save File As]",
437 quick_widgets, NULL, NULL
440 if (quick_dialog (&qdlg) != B_CANCEL)
442 char *fname;
444 edit->lb = cur_lb;
445 fname = tilde_expand (filename_res);
446 g_free (filename_res);
447 ret_vpath = vfs_path_from_str (fname);
448 g_free (fname);
451 return ret_vpath;
454 /* --------------------------------------------------------------------------------------------- */
456 /** returns TRUE on success */
458 static gboolean
459 edit_save_cmd (WEdit * edit)
461 int res, save_lock = 0;
463 if (!edit->locked && !edit->delete_file)
464 save_lock = lock_file (edit->filename_vpath);
465 res = edit_save_file (edit, edit->filename_vpath);
467 /* Maintain modify (not save) lock on failure */
468 if ((res > 0 && edit->locked) || save_lock)
469 edit->locked = unlock_file (edit->filename_vpath);
471 /* On failure try 'save as', it does locking on its own */
472 if (res == 0)
473 return edit_save_as_cmd (edit);
474 edit->force |= REDRAW_COMPLETELY;
475 if (res > 0)
477 edit->delete_file = 0;
478 edit->modified = 0;
481 return TRUE;
484 /* --------------------------------------------------------------------------------------------- */
486 * Load file content
488 * @param h screen the owner of editor window
489 * @param vpath vfs file path
490 * @return TRUE if file content was successfully loaded, FALSE otherwise
493 static inline gboolean
494 edit_load_file_from_filename (WDialog * h, const vfs_path_t * vpath)
496 Widget *w = WIDGET (h);
498 return edit_add_window (h, w->y + 1, w->x, w->lines - 2, w->cols, vpath, 0);
501 /* --------------------------------------------------------------------------------------------- */
503 static void
504 edit_delete_column_of_text (WEdit * edit)
506 off_t p, q, r;
507 off_t m1, m2;
508 off_t n;
509 long b, c, d;
511 eval_marks (edit, &m1, &m2);
512 n = edit_buffer_move_forward (&edit->buffer, m1, 0, m2) + 1;
513 c = (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, m1), 0, m1);
514 d = (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, m2), 0, m2);
515 b = max (min (c, d), min (edit->column1, edit->column2));
516 c = max (c, max (edit->column1, edit->column2));
518 while (n--)
520 r = edit_buffer_get_current_bol (&edit->buffer);
521 p = edit_move_forward3 (edit, r, b, 0);
522 q = edit_move_forward3 (edit, r, c, 0);
523 if (p < m1)
524 p = m1;
525 if (q > m2)
526 q = m2;
527 edit_cursor_move (edit, p - edit->buffer.curs1);
528 while (q > p)
530 /* delete line between margins */
531 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
532 edit_delete (edit, TRUE);
533 q--;
535 if (n)
536 /* move to next line except on the last delete */
537 edit_cursor_move (edit,
538 edit_buffer_move_forward (&edit->buffer, edit->buffer.curs1, 1,
539 0) - edit->buffer.curs1);
543 /* --------------------------------------------------------------------------------------------- */
544 /** if success return 0 */
546 static int
547 edit_block_delete (WEdit * edit)
549 off_t start_mark, end_mark;
550 off_t curs_pos;
551 long curs_line, c1, c2;
553 if (!eval_marks (edit, &start_mark, &end_mark))
554 return 0;
556 if (edit->column_highlight && edit->mark2 < 0)
557 edit_mark_cmd (edit, FALSE);
558 if ((end_mark - start_mark) > option_max_undo / 2)
560 /* Warning message with a query to continue or cancel the operation */
561 if (edit_query_dialog2
562 (_("Warning"),
564 ("Block is large, you may not be able to undo this action"),
565 _("C&ontinue"), _("&Cancel")))
567 return 1;
570 c1 = min (edit->column1, edit->column2);
571 c2 = max (edit->column1, edit->column2);
572 edit->column1 = c1;
573 edit->column2 = c2;
575 edit_push_markers (edit);
577 curs_line = edit->buffer.curs_line;
579 curs_pos = edit->curs_col + edit->over_col;
581 /* move cursor to start of selection */
582 edit_cursor_move (edit, start_mark - edit->buffer.curs1);
583 edit_scroll_screen_over_cursor (edit);
585 if (start_mark < end_mark)
587 if (edit->column_highlight)
589 off_t line_width;
591 if (edit->mark2 < 0)
592 edit_mark_cmd (edit, FALSE);
593 edit_delete_column_of_text (edit);
594 /* move cursor to the saved position */
595 edit_move_to_line (edit, curs_line);
596 /* calculate line width and cursor position before cut */
597 line_width = edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), 0,
598 edit_buffer_get_current_eol (&edit->buffer));
599 if (option_cursor_beyond_eol && curs_pos > line_width)
600 edit->over_col = curs_pos - line_width;
602 else
604 off_t count;
606 for (count = start_mark; count < end_mark; count++)
607 edit_delete (edit, TRUE);
610 edit_set_markers (edit, 0, 0, 0, 0);
611 edit->force |= REDRAW_PAGE;
612 return 0;
615 /* --------------------------------------------------------------------------------------------- */
617 * Get EOL symbol for searching.
619 * @param edit editor object
620 * @return EOL symbol
623 static inline char
624 edit_search_get_current_end_line_char (const WEdit * edit)
626 switch (edit->lb)
628 case LB_MAC:
629 return '\r';
630 default:
631 return '\n';
635 /* --------------------------------------------------------------------------------------------- */
637 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
639 * @param search search object
640 * @return result of checks.
643 static edit_search_line_t
644 edit_get_search_line_type (mc_search_t * search)
646 edit_search_line_t search_line_type = 0;
648 if (search->search_type != MC_SEARCH_T_REGEX)
649 return search_line_type;
651 if (*search->original == '^')
652 search_line_type |= AT_START_LINE;
654 if (search->original[search->original_len - 1] == '$')
655 search_line_type |= AT_END_LINE;
656 return search_line_type;
659 /* --------------------------------------------------------------------------------------------- */
661 * Calculating the start position of next line.
663 * @param buf editor buffer object
664 * @param current_pos current position
665 * @param max_pos max position
666 * @param end_string_symbol end of line symbol
667 * @return start position of next line
670 static off_t
671 edit_calculate_start_of_next_line (const edit_buffer_t * buf, off_t current_pos, off_t max_pos,
672 char end_string_symbol)
674 off_t i;
676 for (i = current_pos; i < max_pos; i++)
678 current_pos++;
679 if (edit_buffer_get_byte (buf, i) == end_string_symbol)
680 break;
683 return current_pos;
686 /* --------------------------------------------------------------------------------------------- */
688 * Calculating the end position of previous line.
690 * @param buf editor buffer object
691 * @param current_pos current position
692 * @param end_string_symbol end of line symbol
693 * @return end position of previous line
696 static off_t
697 edit_calculate_end_of_previous_line (const edit_buffer_t * buf, off_t current_pos,
698 char end_string_symbol)
700 off_t i;
702 for (i = current_pos - 1; i >= 0; i--)
703 if (edit_buffer_get_byte (buf, i) == end_string_symbol)
704 break;
706 return i;
709 /* --------------------------------------------------------------------------------------------- */
711 * Calculating the start position of previous line.
713 * @param buf editor buffer object
714 * @param current_pos current position
715 * @param end_string_symbol end of line symbol
716 * @return start position of previous line
719 static inline off_t
720 edit_calculate_start_of_previous_line (const edit_buffer_t * buf, off_t current_pos,
721 char end_string_symbol)
723 current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
724 current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
726 return (current_pos + 1);
729 /* --------------------------------------------------------------------------------------------- */
731 * Calculating the start position of current line.
733 * @param buf editor buffer object
734 * @param current_pos current position
735 * @param end_string_symbol end of line symbol
736 * @return start position of current line
739 static inline off_t
740 edit_calculate_start_of_current_line (const edit_buffer_t * buf, off_t current_pos,
741 char end_string_symbol)
743 current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
745 return (current_pos + 1);
748 /* --------------------------------------------------------------------------------------------- */
750 * Fixing (if needed) search start position if 'only in selection' option present.
752 * @param edit editor object
755 static void
756 edit_search_fix_search_start_if_selection (WEdit * edit)
758 off_t start_mark = 0;
759 off_t end_mark = 0;
761 if (!edit_search_options.only_in_selection)
762 return;
764 if (!eval_marks (edit, &start_mark, &end_mark) != 0)
765 return;
767 if (edit_search_options.backwards)
769 if (edit->search_start > end_mark || edit->search_start <= start_mark)
770 edit->search_start = end_mark;
772 else
774 if (edit->search_start < start_mark || edit->search_start >= end_mark)
775 edit->search_start = start_mark;
779 /* --------------------------------------------------------------------------------------------- */
781 static gboolean
782 editcmd_find (WEdit * edit, gsize * len)
784 off_t search_start = edit->search_start;
785 off_t search_end;
786 off_t start_mark = 0;
787 off_t end_mark = edit->buffer.size;
788 char end_string_symbol;
790 end_string_symbol = edit_search_get_current_end_line_char (edit);
792 /* prepare for search */
793 if (edit_search_options.only_in_selection)
795 if (!eval_marks (edit, &start_mark, &end_mark))
797 edit->search->error = MC_SEARCH_E_NOTFOUND;
798 edit->search->error_str = g_strdup (_("Search string not found"));
799 return FALSE;
802 /* fix the start and the end of search block positions */
803 if ((edit->search_line_type & AT_START_LINE) != 0
804 && (start_mark != 0
805 || edit_buffer_get_byte (&edit->buffer, start_mark - 1) != end_string_symbol))
807 start_mark =
808 edit_calculate_start_of_next_line (&edit->buffer, start_mark, edit->buffer.size,
809 end_string_symbol);
811 if ((edit->search_line_type & AT_END_LINE) != 0
812 && (end_mark - 1 != edit->buffer.size
813 || edit_buffer_get_byte (&edit->buffer, end_mark) != end_string_symbol))
814 end_mark =
815 edit_calculate_end_of_previous_line (&edit->buffer, end_mark, end_string_symbol);
816 if (start_mark >= end_mark)
818 edit->search->error = MC_SEARCH_E_NOTFOUND;
819 edit->search->error_str = g_strdup (_("Search string not found"));
820 return FALSE;
823 else
825 if (edit_search_options.backwards)
826 end_mark = max (1, edit->buffer.curs1) - 1;
829 /* search */
830 if (edit_search_options.backwards)
832 /* backward search */
833 search_end = end_mark;
835 if ((edit->search_line_type & AT_START_LINE) != 0)
836 search_start =
837 edit_calculate_start_of_current_line (&edit->buffer, search_start,
838 end_string_symbol);
840 while (search_start >= start_mark)
842 if (search_end > (off_t) (search_start + edit->search->original_len)
843 && mc_search_is_fixed_search_str (edit->search))
845 search_end = search_start + edit->search->original_len;
847 if (mc_search_run (edit->search, (void *) edit, search_start, search_end, len)
848 && edit->search->normal_offset == search_start)
850 return TRUE;
854 if ((edit->search_line_type & AT_START_LINE) != 0)
855 search_start =
856 edit_calculate_start_of_previous_line (&edit->buffer, search_start,
857 end_string_symbol);
858 else
859 search_start--;
861 edit->search->error_str = g_strdup (_("Search string not found"));
863 else
865 /* forward search */
866 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
867 search_start =
868 edit_calculate_start_of_next_line (&edit->buffer, search_start, end_mark,
869 end_string_symbol);
870 return mc_search_run (edit->search, (void *) edit, search_start, end_mark, len);
872 return FALSE;
875 /* --------------------------------------------------------------------------------------------- */
877 static char *
878 edit_replace_cmd__conv_to_display (char *str)
880 #ifdef HAVE_CHARSET
881 GString *tmp;
883 tmp = str_convert_to_display (str);
884 if (tmp != NULL)
886 if (tmp->len != 0)
887 return g_string_free (tmp, FALSE);
888 g_string_free (tmp, TRUE);
890 #endif
891 return g_strdup (str);
894 /* --------------------------------------------------------------------------------------------- */
896 static char *
897 edit_replace_cmd__conv_to_input (char *str)
899 #ifdef HAVE_CHARSET
900 GString *tmp;
902 tmp = str_convert_to_input (str);
903 if (tmp != NULL)
905 if (tmp->len != 0)
906 return g_string_free (tmp, FALSE);
907 g_string_free (tmp, TRUE);
909 #endif
910 return g_strdup (str);
913 /* --------------------------------------------------------------------------------------------- */
915 static void
916 edit_do_search (WEdit * edit)
918 gsize len = 0;
920 if (edit->search == NULL)
921 edit->search_start = edit->buffer.curs1;
923 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
925 if (search_create_bookmark)
927 int found = 0, books = 0;
928 long l = 0, l_last = -1;
929 long q = 0;
931 search_create_bookmark = FALSE;
932 book_mark_flush (edit, -1);
934 while (mc_search_run (edit->search, (void *) edit, q, edit->buffer.size, &len))
936 if (found == 0)
937 edit->search_start = edit->search->normal_offset;
938 found++;
939 l += edit_buffer_count_lines (&edit->buffer, q, edit->search->normal_offset);
940 if (l != l_last)
942 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
943 books++;
945 l_last = l;
946 q = edit->search->normal_offset + 1;
949 if (found == 0)
950 edit_error_dialog (_("Search"), _("Search string not found"));
951 else
952 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
954 else
956 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
957 && edit_search_options.backwards)
958 edit->search_start--;
960 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
961 && !edit_search_options.backwards)
962 edit->search_start++;
964 if (editcmd_find (edit, &len))
966 edit->found_start = edit->search_start = edit->search->normal_offset;
967 edit->found_len = len;
968 edit->over_col = 0;
969 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
970 edit_scroll_screen_over_cursor (edit);
971 if (edit_search_options.backwards)
972 edit->search_start--;
973 else
974 edit->search_start++;
976 else
978 edit->search_start = edit->buffer.curs1;
979 if (edit->search->error_str != NULL)
980 edit_error_dialog (_("Search"), edit->search->error_str);
984 edit->force |= REDRAW_COMPLETELY;
985 edit_scroll_screen_over_cursor (edit);
988 /* --------------------------------------------------------------------------------------------- */
990 static void
991 edit_search (WEdit * edit)
993 if (editcmd_dialog_search_show (edit))
995 edit->search_line_type = edit_get_search_line_type (edit->search);
996 edit_search_fix_search_start_if_selection (edit);
997 edit_do_search (edit);
1001 /* --------------------------------------------------------------------------------------------- */
1002 /** Return a null terminated length of text. Result must be g_free'd */
1004 static unsigned char *
1005 edit_get_block (WEdit * edit, off_t start, off_t finish, off_t * l)
1007 unsigned char *s, *r;
1009 r = s = g_malloc0 (finish - start + 1);
1010 if (edit->column_highlight)
1012 *l = 0;
1013 /* copy from buffer, excluding chars that are out of the column 'margins' */
1014 while (start < finish)
1016 int c;
1017 off_t x;
1019 x = edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, start), 0, start);
1020 c = edit_buffer_get_byte (&edit->buffer, start);
1021 if ((x >= edit->column1 && x < edit->column2)
1022 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1024 *s++ = c;
1025 (*l)++;
1027 start++;
1030 else
1032 *l = finish - start;
1033 while (start < finish)
1034 *s++ = edit_buffer_get_byte (&edit->buffer, start++);
1036 *s = '\0';
1037 return r;
1040 /* --------------------------------------------------------------------------------------------- */
1041 /** copies a block to clipboard file */
1043 static gboolean
1044 edit_save_block_to_clip_file (WEdit * edit, off_t start, off_t finish)
1046 gboolean ret;
1047 gchar *tmp;
1049 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1050 ret = edit_save_block (edit, tmp, start, finish);
1051 g_free (tmp);
1052 return ret;
1055 /* --------------------------------------------------------------------------------------------- */
1057 static void
1058 pipe_mail (const edit_buffer_t * buf, char *to, char *subject, char *cc)
1060 FILE *p = 0;
1061 char *s;
1063 to = name_quote (to, 0);
1064 subject = name_quote (subject, 0);
1065 cc = name_quote (cc, 0);
1066 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1067 g_free (to);
1068 g_free (subject);
1069 g_free (cc);
1071 if (s != NULL)
1073 p = popen (s, "w");
1074 g_free (s);
1077 if (p != NULL)
1079 off_t i;
1081 for (i = 0; i < buf->size; i++)
1082 fputc (edit_buffer_get_byte (buf, i), p);
1083 pclose (p);
1087 /* --------------------------------------------------------------------------------------------- */
1088 /** find first character of current word */
1090 static gboolean
1091 edit_find_word_start (const edit_buffer_t * buf, off_t * word_start, gsize * word_len)
1093 int c;
1094 off_t i;
1096 /* return if at begin of file */
1097 if (buf->curs1 <= 0)
1098 return FALSE;
1100 c = edit_buffer_get_previous_byte (buf);
1101 /* return if not at end or in word */
1102 if (is_break_char (c))
1103 return FALSE;
1105 /* search start of word to be completed */
1106 for (i = 1;; i++)
1108 int last;
1110 last = c;
1111 c = edit_buffer_get_byte (buf, buf->curs1 - i - 1);
1113 if (is_break_char (c))
1115 /* return if word starts with digit */
1116 if (isdigit (last))
1117 return FALSE;
1119 break;
1123 /* success */
1124 *word_start = buf->curs1 - i; /* start found */
1125 *word_len = (gsize) i;
1126 return TRUE;
1129 /* --------------------------------------------------------------------------------------------- */
1131 * Get current word under cursor
1133 * @param edit editor object
1134 * @param srch mc_search object
1135 * @param word_start start word position
1137 * @return newly allocated string or NULL if no any words under cursor
1140 static char *
1141 edit_collect_completions_get_current_word (WEdit * edit, mc_search_t * srch, off_t word_start)
1143 gsize len = 0;
1144 off_t i;
1145 GString *temp;
1147 if (!mc_search_run (srch, (void *) edit, word_start, edit->buffer.size, &len))
1148 return NULL;
1150 temp = g_string_sized_new (len);
1152 for (i = 0; i < (off_t) len; i++)
1154 int chr;
1156 chr = edit_buffer_get_byte (&edit->buffer, word_start + i);
1157 if (!isspace (chr))
1158 g_string_append_c (temp, chr);
1161 return g_string_free (temp, temp->len == 0);
1164 /* --------------------------------------------------------------------------------------------- */
1165 /** collect the possible completions */
1167 static gsize
1168 edit_collect_completions (WEdit * edit, off_t word_start, gsize word_len,
1169 char *match_expr, GString ** compl, gsize * num)
1171 gsize len = 0;
1172 gsize max_len = 0;
1173 gsize i;
1174 int skip;
1175 GString *temp;
1176 mc_search_t *srch;
1177 off_t last_byte, start = -1;
1178 char *current_word;
1180 #ifdef HAVE_CHARSET
1181 srch = mc_search_new (match_expr, -1, cp_source);
1182 #else
1183 srch = mc_search_new (match_expr, -1, NULL);
1184 #endif
1185 if (srch == NULL)
1186 return 0;
1188 if (mc_config_get_bool
1189 (mc_main_config, CONFIG_APP_SECTION, "editor_wordcompletion_collect_entire_file", 0))
1191 last_byte = edit->buffer.size;
1193 else
1195 last_byte = word_start;
1198 srch->search_type = MC_SEARCH_T_REGEX;
1199 srch->is_case_sensitive = TRUE;
1200 srch->search_fn = edit_search_cmd_callback;
1202 current_word = edit_collect_completions_get_current_word (edit, srch, word_start);
1204 temp = g_string_new ("");
1206 /* collect max MAX_WORD_COMPLETIONS completions */
1207 while (mc_search_run (srch, (void *) edit, start + 1, last_byte, &len))
1209 g_string_set_size (temp, 0);
1210 start = srch->normal_offset;
1212 /* add matched completion if not yet added */
1213 for (i = 0; i < len; i++)
1215 skip = edit_buffer_get_byte (&edit->buffer, start + i);
1216 if (isspace (skip))
1217 continue;
1219 /* skip current word */
1220 if (start + (off_t) i == word_start)
1221 break;
1223 g_string_append_c (temp, skip);
1226 if (temp->len == 0)
1227 continue;
1229 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1230 continue;
1232 skip = 0;
1234 for (i = 0; i < *num; i++)
1236 if (strncmp
1237 ((char *) &compl[i]->str[word_len],
1238 (char *) &temp->str[word_len], max (len, compl[i]->len) - word_len) == 0)
1240 GString *this = compl[i];
1241 for (++i; i < *num; i++)
1242 compl[i - 1] = compl[i];
1243 compl[*num - 1] = this;
1244 skip = 1;
1245 break; /* skip it, already added */
1248 if (skip != 0)
1249 continue;
1251 if (*num == MAX_WORD_COMPLETIONS)
1253 g_string_free (compl[0], TRUE);
1254 for (i = 1; i < *num; i++)
1255 compl[i - 1] = compl[i];
1256 (*num)--;
1258 #ifdef HAVE_CHARSET
1260 GString *recoded;
1261 recoded = str_convert_to_display (temp->str);
1263 if (recoded && recoded->len)
1264 g_string_assign (temp, recoded->str);
1266 g_string_free (recoded, TRUE);
1268 #endif
1269 compl[(*num)++] = g_string_new_len (temp->str, temp->len);
1270 start += len;
1272 /* note the maximal length needed for the completion dialog */
1273 if (len > max_len)
1274 max_len = len;
1277 mc_search_free (srch);
1278 g_string_free (temp, TRUE);
1279 g_free (current_word);
1281 return max_len;
1284 /* --------------------------------------------------------------------------------------------- */
1286 static void
1287 edit_insert_column_of_text (WEdit * edit, unsigned char *data, off_t size, long width,
1288 off_t * start_pos, off_t * end_pos, long *col1, long *col2)
1290 off_t i, cursor;
1291 long col;
1293 cursor = edit->buffer.curs1;
1294 col = edit_get_col (edit);
1296 for (i = 0; i < size; i++)
1298 if (data[i] != '\n')
1299 edit_insert (edit, data[i]);
1300 else
1301 { /* fill in and move to next line */
1302 long l;
1303 off_t p;
1305 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
1307 for (l = width - (edit_get_col (edit) - col); l > 0; l -= space_width)
1308 edit_insert (edit, ' ');
1310 for (p = edit->buffer.curs1;; p++)
1312 if (p == edit->buffer.size)
1314 edit_cursor_move (edit, edit->buffer.size - edit->buffer.curs1);
1315 edit_insert_ahead (edit, '\n');
1316 p++;
1317 break;
1319 if (edit_buffer_get_byte (&edit->buffer, p) == '\n')
1321 p++;
1322 break;
1325 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->buffer.curs1);
1327 for (l = col - edit_get_col (edit); l >= space_width; l -= space_width)
1328 edit_insert (edit, ' ');
1332 *col1 = col;
1333 *col2 = col + width;
1334 *start_pos = cursor;
1335 *end_pos = edit->buffer.curs1;
1336 edit_cursor_move (edit, cursor - edit->buffer.curs1);
1339 /* --------------------------------------------------------------------------------------------- */
1341 static int
1342 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1344 const macros_t *m1 = (const macros_t *) macro1;
1345 const macros_t *m2 = (const macros_t *) macro2;
1347 return m1->hotkey - m2->hotkey;
1350 /* --------------------------------------------------------------------------------------------- */
1352 static void
1353 edit_macro_sort_by_hotkey (void)
1355 if (macros_list != NULL && macros_list->len != 0)
1356 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1359 /* --------------------------------------------------------------------------------------------- */
1361 static gboolean
1362 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1364 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1365 macros_t *result;
1366 macros_t search_macro;
1368 (void) edit;
1370 search_macro.hotkey = hotkey;
1371 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1372 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1374 if (result != NULL && result->macro != NULL)
1376 *indx = (result - array_start);
1377 *macros = result;
1378 return TRUE;
1380 *indx = 0;
1381 return FALSE;
1384 /* --------------------------------------------------------------------------------------------- */
1385 /** returns FALSE on error */
1387 static gboolean
1388 edit_delete_macro (WEdit * edit, int hotkey)
1390 mc_config_t *macros_config = NULL;
1391 const char *section_name = "editor";
1392 gchar *macros_fname;
1393 guint indx;
1394 char *skeyname;
1395 const macros_t *macros = NULL;
1397 /* clear array of actions for current hotkey */
1398 while (edit_get_macro (edit, hotkey, &macros, &indx))
1400 if (macros->macro != NULL)
1401 g_array_free (macros->macro, TRUE);
1402 macros = NULL;
1403 g_array_remove_index (macros_list, indx);
1404 edit_macro_sort_by_hotkey ();
1407 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1408 macros_config = mc_config_init (macros_fname, FALSE);
1409 g_free (macros_fname);
1411 if (macros_config == NULL)
1412 return FALSE;
1414 skeyname = lookup_key_by_code (hotkey);
1415 while (mc_config_del_key (macros_config, section_name, skeyname))
1417 g_free (skeyname);
1418 mc_config_save_file (macros_config, NULL);
1419 mc_config_deinit (macros_config);
1420 return TRUE;
1423 /* --------------------------------------------------------------------------------------------- */
1425 * Callback for the iteration of objects in the 'editors' array.
1426 * Toggle syntax highlighting in editor object.
1428 * @param data probably WEdit object
1429 * @param user_data unused
1432 static void
1433 edit_syntax_onoff_cb (void *data, void *user_data)
1435 (void) user_data;
1437 if (edit_widget_is_editor ((const Widget *) data))
1439 WEdit *edit = (WEdit *) data;
1441 if (option_syntax_highlighting)
1442 edit_load_syntax (edit, NULL, edit->syntax_type);
1443 edit->force |= REDRAW_PAGE;
1447 /* --------------------------------------------------------------------------------------------- */
1449 * Callback for the iteration of objects in the 'editors' array.
1450 * Redraw editor object.
1452 * @param data probably WEdit object
1453 * @param user_data unused
1456 static void
1457 edit_redraw_page_cb (void *data, void *user_data)
1459 (void) user_data;
1461 if (edit_widget_is_editor ((const Widget *) data))
1462 ((WEdit *) data)->force |= REDRAW_PAGE;
1465 /* --------------------------------------------------------------------------------------------- */
1467 * Insert autocompleted word into editor.
1469 * @param edit editor object
1470 * @param completion word for completion
1471 * @param word_len offset from beginning for insert
1474 static void
1475 edit_complete_word_insert_recoded_completion (WEdit * edit, char *completion, gsize word_len)
1477 #ifdef HAVE_CHARSET
1478 GString *temp;
1480 temp = str_convert_to_input (completion);
1482 for (completion = temp->str + word_len; *completion != '\0'; completion++)
1483 edit_insert (edit, *completion);
1484 g_string_free (temp, TRUE);
1485 #else
1486 for (completion += word_len; *completion != '\0'; completion++)
1487 edit_insert (edit, *completion);
1488 #endif
1491 /* --------------------------------------------------------------------------------------------- */
1492 /*** public functions ****************************************************************************/
1493 /* --------------------------------------------------------------------------------------------- */
1495 void
1496 edit_refresh_cmd (void)
1498 clr_scr ();
1499 repaint_screen ();
1500 tty_keypad (TRUE);
1503 /* --------------------------------------------------------------------------------------------- */
1505 * Toggle syntax highlighting in all editor windows.
1507 * @param h root widget for all windows
1510 void
1511 edit_syntax_onoff_cmd (WDialog * h)
1513 option_syntax_highlighting = !option_syntax_highlighting;
1514 g_list_foreach (h->widgets, edit_syntax_onoff_cb, NULL);
1515 dlg_redraw (h);
1518 /* --------------------------------------------------------------------------------------------- */
1520 * Toggle tabs showing in all editor windows.
1522 * @param h root widget for all windows
1525 void
1526 edit_show_tabs_tws_cmd (WDialog * h)
1528 enable_show_tabs_tws = !enable_show_tabs_tws;
1529 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1530 dlg_redraw (h);
1533 /* --------------------------------------------------------------------------------------------- */
1535 * Toggle right margin showing in all editor windows.
1537 * @param h root widget for all windows
1540 void
1541 edit_show_margin_cmd (WDialog * h)
1543 show_right_margin = !show_right_margin;
1544 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1545 dlg_redraw (h);
1548 /* --------------------------------------------------------------------------------------------- */
1550 * Toggle line numbers showing in all editor windows.
1552 * @param h root widget for all windows
1555 void
1556 edit_show_numbers_cmd (WDialog * h)
1558 option_line_state = !option_line_state;
1559 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
1560 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1561 dlg_redraw (h);
1564 /* --------------------------------------------------------------------------------------------- */
1566 void
1567 edit_save_mode_cmd (void)
1569 char *str_result = NULL;
1571 const char *str[] = {
1572 N_("&Quick save"),
1573 N_("&Safe save"),
1574 N_("&Do backups with following extension:")
1577 #ifdef HAVE_ASSERT_H
1578 assert (option_backup_ext != NULL);
1579 #endif
1581 #ifdef ENABLE_NLS
1582 size_t i;
1584 for (i = 0; i < 3; i++)
1585 str[i] = _(str[i]);
1586 #endif
1589 quick_widget_t quick_widgets[] = {
1590 /* *INDENT-OFF* */
1591 QUICK_RADIO (3, str, &option_save_mode, &edit_save_mode_radio_id),
1592 QUICK_INPUT (option_backup_ext, "edit-backup-ext", &str_result,
1593 &edit_save_mode_input_id, FALSE, FALSE, INPUT_COMPLETE_NONE),
1594 QUICK_SEPARATOR (TRUE),
1595 QUICK_CHECKBOX (N_("Check &POSIX new line"), &option_check_nl_at_eof, NULL),
1596 QUICK_BUTTONS_OK_CANCEL,
1597 QUICK_END
1598 /* *INDENT-ON* */
1601 quick_dialog_t qdlg = {
1602 -1, -1, 38,
1603 N_("Edit Save Mode"), "[Edit Save Mode]",
1604 quick_widgets, edit_save_mode_callback, NULL
1607 if (quick_dialog (&qdlg) != B_CANCEL)
1609 g_free (option_backup_ext);
1610 option_backup_ext = str_result;
1615 /* --------------------------------------------------------------------------------------------- */
1617 void
1618 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1620 vfs_path_free (edit->filename_vpath);
1621 edit->filename_vpath = vfs_path_clone (name_vpath);
1623 if (edit->dir_vpath == NULL)
1624 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1627 /* --------------------------------------------------------------------------------------------- */
1628 /* Here we want to warn the users of overwriting an existing file,
1629 but only if they have made a change to the filename */
1630 /* returns TRUE on success */
1631 gboolean
1632 edit_save_as_cmd (WEdit * edit)
1634 /* This heads the 'Save As' dialog box */
1635 vfs_path_t *exp_vpath;
1636 int save_lock = 0;
1637 int different_filename = 0;
1639 if (!edit_check_newline (&edit->buffer))
1640 return FALSE;
1642 exp_vpath = edit_get_save_file_as (edit);
1643 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1645 if (exp_vpath != NULL)
1647 if (vfs_path_len (exp_vpath) == 0)
1648 goto ret;
1649 else
1651 int rv;
1653 if (!vfs_path_equal (edit->filename_vpath, exp_vpath))
1655 int file;
1656 struct stat sb;
1658 if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1660 edit_error_dialog (_("Save as"),
1661 get_sys_error (_
1662 ("Cannot save: destination is not a regular file")));
1663 goto ret;
1666 different_filename = 1;
1667 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1669 if (file != -1)
1671 /* the file exists */
1672 mc_close (file);
1673 /* Overwrite the current file or cancel the operation */
1674 if (edit_query_dialog2
1675 (_("Warning"),
1676 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1677 goto ret;
1679 else
1681 edit->stat1.st_mode |= S_IWUSR;
1683 save_lock = lock_file (exp_vpath);
1685 else
1687 /* filenames equal, check if already locked */
1688 if (!edit->locked && !edit->delete_file)
1689 save_lock = lock_file (exp_vpath);
1692 if (different_filename)
1695 * Allow user to write into saved (under another name) file
1696 * even if original file had r/o user permissions.
1698 edit->stat1.st_mode |= S_IWRITE;
1701 rv = edit_save_file (edit, exp_vpath);
1702 switch (rv)
1704 case 1:
1705 /* Successful, so unlock both files */
1706 if (different_filename)
1708 if (save_lock)
1709 unlock_file (exp_vpath);
1710 if (edit->locked)
1711 edit->locked = unlock_file (edit->filename_vpath);
1713 else
1715 if (edit->locked || save_lock)
1716 edit->locked = unlock_file (edit->filename_vpath);
1719 edit_set_filename (edit, exp_vpath);
1720 if (edit->lb != LB_ASIS)
1721 edit_reload (edit, exp_vpath);
1722 edit->modified = 0;
1723 edit->delete_file = 0;
1724 if (different_filename)
1725 edit_load_syntax (edit, NULL, edit->syntax_type);
1726 vfs_path_free (exp_vpath);
1727 edit->force |= REDRAW_COMPLETELY;
1728 return TRUE;
1729 default:
1730 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1731 /* fallthrough */
1732 case -1:
1733 /* Failed, so maintain modify (not save) lock */
1734 if (save_lock)
1735 unlock_file (exp_vpath);
1736 break;
1741 ret:
1742 vfs_path_free (exp_vpath);
1743 edit->force |= REDRAW_COMPLETELY;
1744 return FALSE;
1747 /* {{{ Macro stuff starts here */
1748 /* --------------------------------------------------------------------------------------------- */
1750 void
1751 edit_delete_macro_cmd (WEdit * edit)
1753 int hotkey;
1755 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE);
1757 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1758 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1761 /* --------------------------------------------------------------------------------------------- */
1763 /** returns FALSE on error */
1764 gboolean
1765 edit_execute_macro (WEdit * edit, int hotkey)
1767 gboolean res = FALSE;
1769 if (hotkey != 0)
1771 const macros_t *macros;
1772 guint indx;
1774 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1775 macros->macro != NULL && macros->macro->len != 0)
1777 guint i;
1779 edit->force |= REDRAW_PAGE;
1781 for (i = 0; i < macros->macro->len; i++)
1783 const macro_action_t *m_act;
1785 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1786 edit_execute_cmd (edit, m_act->action, m_act->ch);
1787 res = TRUE;
1792 return res;
1795 /* --------------------------------------------------------------------------------------------- */
1797 /** returns FALSE on error */
1798 gboolean
1799 edit_store_macro_cmd (WEdit * edit)
1801 int i;
1802 int hotkey;
1803 GString *marcros_string;
1804 mc_config_t *macros_config = NULL;
1805 const char *section_name = "editor";
1806 gchar *macros_fname;
1807 GArray *macros; /* current macro */
1808 int tmp_act;
1809 gboolean have_macro = FALSE;
1810 char *skeyname = NULL;
1812 hotkey =
1813 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE);
1814 if (hotkey == ESC_CHAR)
1815 return FALSE;
1817 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1819 /* return FALSE if try assign macro into restricted hotkeys */
1820 if (tmp_act == CK_MacroStartRecord
1821 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1822 return FALSE;
1824 edit_delete_macro (edit, hotkey);
1826 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1827 macros_config = mc_config_init (macros_fname, FALSE);
1828 g_free (macros_fname);
1830 if (macros_config == NULL)
1831 return FALSE;
1833 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1835 marcros_string = g_string_sized_new (250);
1836 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1838 skeyname = lookup_key_by_code (hotkey);
1840 for (i = 0; i < macro_index; i++)
1842 macro_action_t m_act;
1843 const char *action_name;
1845 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1847 if (action_name == NULL)
1848 break;
1850 m_act.action = record_macro_buf[i].action;
1851 m_act.ch = record_macro_buf[i].ch;
1852 g_array_append_val (macros, m_act);
1853 have_macro = TRUE;
1854 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1855 (int) record_macro_buf[i].ch);
1857 if (have_macro)
1859 macros_t macro;
1860 macro.hotkey = hotkey;
1861 macro.macro = macros;
1862 g_array_append_val (macros_list, macro);
1863 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1865 else
1866 mc_config_del_key (macros_config, section_name, skeyname);
1868 g_free (skeyname);
1869 edit_macro_sort_by_hotkey ();
1871 g_string_free (marcros_string, TRUE);
1872 mc_config_save_file (macros_config, NULL);
1873 mc_config_deinit (macros_config);
1874 return TRUE;
1877 /* --------------------------------------------------------------------------------------------- */
1879 gboolean
1880 edit_repeat_macro_cmd (WEdit * edit)
1882 int i, j;
1883 char *f;
1884 long count_repeat;
1885 char *error = NULL;
1887 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL,
1888 INPUT_COMPLETE_NONE);
1889 if (f == NULL || *f == '\0')
1891 g_free (f);
1892 return FALSE;
1895 count_repeat = strtol (f, &error, 0);
1897 if (*error != '\0')
1899 g_free (f);
1900 return FALSE;
1903 g_free (f);
1905 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1906 edit->force |= REDRAW_PAGE;
1908 for (j = 0; j < count_repeat; j++)
1909 for (i = 0; i < macro_index; i++)
1910 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1911 edit_update_screen (edit);
1912 return TRUE;
1915 /* --------------------------------------------------------------------------------------------- */
1916 /** return FALSE on error */
1918 gboolean
1919 edit_load_macro_cmd (WEdit * edit)
1921 mc_config_t *macros_config = NULL;
1922 gchar **profile_keys, **keys;
1923 gchar **values, **curr_values;
1924 gsize len, values_len;
1925 const char *section_name = "editor";
1926 gchar *macros_fname;
1928 (void) edit;
1930 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1931 macros_config = mc_config_init (macros_fname, TRUE);
1932 g_free (macros_fname);
1934 if (macros_config == NULL || macros_list == NULL || macros_list->len != 0)
1935 return FALSE;
1937 keys = mc_config_get_keys (macros_config, section_name, &len);
1938 for (profile_keys = keys; *profile_keys != NULL; profile_keys++)
1940 int hotkey;
1941 gboolean have_macro = FALSE;
1942 GArray *macros;
1943 macros_t macro;
1945 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1946 values =
1947 mc_config_get_string_list (macros_config, section_name, *profile_keys, &values_len);
1948 hotkey = lookup_key (*profile_keys, NULL);
1950 for (curr_values = values; *curr_values != NULL && *curr_values[0] != '\0'; curr_values++)
1952 char **macro_pair = NULL;
1954 macro_pair = g_strsplit (*curr_values, ":", 2);
1955 if (macro_pair != NULL)
1957 macro_action_t m_act;
1958 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
1959 m_act.action = 0;
1960 else
1962 m_act.action = keybind_lookup_action (macro_pair[0]);
1963 g_free (macro_pair[0]);
1964 macro_pair[0] = NULL;
1966 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
1967 m_act.ch = -1;
1968 else
1970 m_act.ch = strtol (macro_pair[1], NULL, 0);
1971 g_free (macro_pair[1]);
1972 macro_pair[1] = NULL;
1974 if (m_act.action != 0)
1976 /* a shell command */
1977 if ((m_act.action / CK_PipeBlock (0)) == 1)
1979 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
1980 m_act.ch = -1;
1982 g_array_append_val (macros, m_act);
1983 have_macro = TRUE;
1985 g_strfreev (macro_pair);
1986 macro_pair = NULL;
1989 if (have_macro)
1991 macro.hotkey = hotkey;
1992 macro.macro = macros;
1993 g_array_append_val (macros_list, macro);
1995 g_strfreev (values);
1997 g_strfreev (keys);
1998 mc_config_deinit (macros_config);
1999 edit_macro_sort_by_hotkey ();
2000 return TRUE;
2003 /* }}} Macro stuff end here */
2005 /* --------------------------------------------------------------------------------------------- */
2006 /** returns TRUE on success */
2008 gboolean
2009 edit_save_confirm_cmd (WEdit * edit)
2011 if (edit->filename_vpath == NULL)
2012 return edit_save_as_cmd (edit);
2014 if (!edit_check_newline (&edit->buffer))
2015 return FALSE;
2017 if (edit_confirm_save)
2019 char *f;
2020 gboolean ok;
2022 f = g_strdup_printf (_("Confirm save file: \"%s\""),
2023 vfs_path_as_str (edit->filename_vpath));
2024 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2025 g_free (f);
2026 if (!ok)
2027 return FALSE;
2029 return edit_save_cmd (edit);
2032 /* --------------------------------------------------------------------------------------------- */
2034 * Ask file to edit and load it.
2036 * @return TRUE on success or cancel of ask.
2039 gboolean
2040 edit_load_cmd (WDialog * h)
2042 char *exp;
2043 gboolean ret = TRUE; /* possible cancel */
2045 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2046 MC_HISTORY_EDIT_LOAD, INPUT_LAST_TEXT,
2047 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_CD);
2049 if (exp != NULL && *exp != '\0')
2051 vfs_path_t *exp_vpath;
2053 exp_vpath = vfs_path_from_str (exp);
2054 ret = edit_load_file_from_filename (h, exp_vpath);
2055 vfs_path_free (exp_vpath);
2058 g_free (exp);
2060 return ret;
2063 /* --------------------------------------------------------------------------------------------- */
2065 * Load syntax file to edit.
2067 * @return TRUE on success
2070 gboolean
2071 edit_load_syntax_file (WDialog * h)
2073 vfs_path_t *extdir_vpath;
2074 int dir = 0;
2075 gboolean ret = FALSE;
2077 if (geteuid () == 0)
2078 dir = query_dialog (_("Syntax file edit"),
2079 _("Which syntax file you want to edit?"), D_NORMAL, 2,
2080 _("&User"), _("&System wide"));
2082 extdir_vpath =
2083 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
2084 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
2086 vfs_path_free (extdir_vpath);
2087 extdir_vpath =
2088 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
2091 if (dir == 0)
2093 vfs_path_t *user_syntax_file_vpath;
2095 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
2096 check_for_default (extdir_vpath, user_syntax_file_vpath);
2097 ret = edit_load_file_from_filename (h, user_syntax_file_vpath);
2098 vfs_path_free (user_syntax_file_vpath);
2100 else if (dir == 1)
2101 ret = edit_load_file_from_filename (h, extdir_vpath);
2103 vfs_path_free (extdir_vpath);
2105 return ret;
2108 /* --------------------------------------------------------------------------------------------- */
2110 * Load menu file to edit.
2112 * @return TRUE on success
2115 gboolean
2116 edit_load_menu_file (WDialog * h)
2118 vfs_path_t *buffer_vpath;
2119 vfs_path_t *menufile_vpath;
2120 int dir;
2121 gboolean ret;
2123 dir = query_dialog (_("Menu edit"),
2124 _("Which menu file do you want to edit?"), D_NORMAL,
2125 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2127 menufile_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2128 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
2130 vfs_path_free (menufile_vpath);
2131 menufile_vpath = vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2134 switch (dir)
2136 case 0:
2137 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
2138 check_for_default (menufile_vpath, buffer_vpath);
2139 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
2140 break;
2142 case 1:
2143 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
2144 check_for_default (menufile_vpath, buffer_vpath);
2145 break;
2147 case 2:
2148 buffer_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2149 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
2151 vfs_path_free (buffer_vpath);
2152 buffer_vpath =
2153 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2155 break;
2157 default:
2158 vfs_path_free (menufile_vpath);
2159 return FALSE;
2162 ret = edit_load_file_from_filename (h, buffer_vpath);
2164 vfs_path_free (buffer_vpath);
2165 vfs_path_free (menufile_vpath);
2167 return ret;
2170 /* --------------------------------------------------------------------------------------------- */
2172 * Close window with opened file.
2174 * @return TRUE if file was closed.
2177 gboolean
2178 edit_close_cmd (WEdit * edit)
2180 gboolean ret;
2182 ret = (edit != NULL) && edit_ok_to_exit (edit);
2184 if (ret)
2186 WDialog *h = WIDGET (edit)->owner;
2188 if (edit->locked != 0)
2189 unlock_file (edit->filename_vpath);
2191 del_widget (edit);
2193 if (edit_widget_is_editor (WIDGET (h->current->data)))
2194 edit = (WEdit *) h->current->data;
2195 else
2197 edit = find_editor (h);
2198 if (edit != NULL)
2199 dlg_set_top_widget (edit);
2203 if (edit != NULL)
2204 edit->force |= REDRAW_COMPLETELY;
2206 return ret;
2209 /* --------------------------------------------------------------------------------------------- */
2211 if mark2 is -1 then marking is from mark1 to the cursor.
2212 Otherwise its between the markers. This handles this.
2213 Returns FALSE if no text is marked.
2216 gboolean
2217 eval_marks (WEdit * edit, off_t * start_mark, off_t * end_mark)
2219 long end_mark_curs;
2221 if (edit->mark1 == edit->mark2)
2223 *start_mark = *end_mark = 0;
2224 edit->column2 = edit->column1 = 0;
2225 return FALSE;
2228 if (edit->end_mark_curs < 0)
2229 end_mark_curs = edit->buffer.curs1;
2230 else
2231 end_mark_curs = edit->end_mark_curs;
2233 if (edit->mark2 >= 0)
2235 *start_mark = min (edit->mark1, edit->mark2);
2236 *end_mark = max (edit->mark1, edit->mark2);
2238 else
2240 *start_mark = min (edit->mark1, end_mark_curs);
2241 *end_mark = max (edit->mark1, end_mark_curs);
2242 edit->column2 = edit->curs_col + edit->over_col;
2245 if (edit->column_highlight
2246 && ((edit->mark1 > end_mark_curs && edit->column1 < edit->column2)
2247 || (edit->mark1 < end_mark_curs && edit->column1 > edit->column2)))
2249 off_t start_bol, start_eol;
2250 off_t end_bol, end_eol;
2251 long col1, col2;
2252 off_t diff1, diff2;
2254 start_bol = edit_buffer_get_bol (&edit->buffer, *start_mark);
2255 start_eol = edit_buffer_get_eol (&edit->buffer, start_bol - 1) + 1;
2256 end_bol = edit_buffer_get_bol (&edit->buffer, *end_mark);
2257 end_eol = edit_buffer_get_eol (&edit->buffer, *end_mark);
2258 col1 = min (edit->column1, edit->column2);
2259 col2 = max (edit->column1, edit->column2);
2261 diff1 = edit_move_forward3 (edit, start_bol, col2, 0) -
2262 edit_move_forward3 (edit, start_bol, col1, 0);
2263 diff2 = edit_move_forward3 (edit, end_bol, col2, 0) -
2264 edit_move_forward3 (edit, end_bol, col1, 0);
2266 *start_mark -= diff1;
2267 *end_mark += diff2;
2268 *start_mark = max (*start_mark, start_eol);
2269 *end_mark = min (*end_mark, end_eol);
2271 return TRUE;
2274 /* --------------------------------------------------------------------------------------------- */
2276 void
2277 edit_block_copy_cmd (WEdit * edit)
2279 off_t start_mark, end_mark, current = edit->buffer.curs1;
2280 off_t mark1, mark2;
2281 long c1, c2;
2282 off_t size;
2283 unsigned char *copy_buf;
2285 edit_update_curs_col (edit);
2286 if (!eval_marks (edit, &start_mark, &end_mark))
2287 return;
2289 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2291 /* all that gets pushed are deletes hence little space is used on the stack */
2293 edit_push_markers (edit);
2295 if (edit->column_highlight)
2297 long col_delta;
2299 col_delta = abs (edit->column2 - edit->column1);
2300 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2302 else
2304 int size_orig = size;
2306 while (size-- != 0)
2307 edit_insert_ahead (edit, copy_buf[size]);
2309 /* Place cursor at the end of text selection */
2310 if (option_cursor_after_inserted_block)
2311 edit_cursor_move (edit, size_orig);
2314 g_free (copy_buf);
2315 edit_scroll_screen_over_cursor (edit);
2317 if (edit->column_highlight)
2318 edit_set_markers (edit, edit->buffer.curs1, mark2, c1, c2);
2319 else if (start_mark < current && end_mark > current)
2320 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2322 edit->force |= REDRAW_PAGE;
2326 /* --------------------------------------------------------------------------------------------- */
2328 void
2329 edit_block_move_cmd (WEdit * edit)
2331 off_t current;
2332 unsigned char *copy_buf = NULL;
2333 off_t start_mark, end_mark;
2335 if (!eval_marks (edit, &start_mark, &end_mark))
2336 return;
2338 if (!edit->column_highlight && edit->buffer.curs1 > start_mark && edit->buffer.curs1 < end_mark)
2339 return;
2341 if (edit->mark2 < 0)
2342 edit_mark_cmd (edit, FALSE);
2343 edit_push_markers (edit);
2345 if (edit->column_highlight)
2347 off_t mark1, mark2;
2348 off_t size;
2349 long c1, c2, b_width;
2350 long x, x2;
2352 c1 = min (edit->column1, edit->column2);
2353 c2 = max (edit->column1, edit->column2);
2354 b_width = c2 - c1;
2356 edit_update_curs_col (edit);
2358 x = edit->curs_col;
2359 x2 = x + edit->over_col;
2361 /* do nothing when cursor inside first line of selected area */
2362 if ((edit_buffer_get_eol (&edit->buffer, edit->buffer.curs1) ==
2363 edit_buffer_get_eol (&edit->buffer, start_mark)) && x2 > c1 && x2 <= c2)
2364 return;
2366 if (edit->buffer.curs1 > start_mark
2367 && edit->buffer.curs1 < edit_buffer_get_eol (&edit->buffer, end_mark))
2369 if (x > c2)
2370 x -= b_width;
2371 else if (x > c1 && x <= c2)
2372 x = c1;
2374 /* save current selection into buffer */
2375 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2377 /* remove current selection */
2378 edit_block_delete_cmd (edit);
2380 edit->over_col = max (0, edit->over_col - b_width);
2381 /* calculate the cursor pos after delete block */
2382 current = edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), x, 0);
2383 edit_cursor_move (edit, current - edit->buffer.curs1);
2384 edit_scroll_screen_over_cursor (edit);
2386 /* add TWS if need before block insertion */
2387 if (option_cursor_beyond_eol && edit->over_col > 0)
2388 edit_insert_over (edit);
2390 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2391 edit_set_markers (edit, mark1, mark2, c1, c2);
2393 else
2395 off_t count, count_orig;
2397 current = edit->buffer.curs1;
2398 copy_buf = g_malloc0 (end_mark - start_mark);
2399 edit_cursor_move (edit, start_mark - edit->buffer.curs1);
2400 edit_scroll_screen_over_cursor (edit);
2402 for (count = start_mark; count < end_mark; count++)
2403 copy_buf[end_mark - count - 1] = edit_delete (edit, TRUE);
2405 edit_scroll_screen_over_cursor (edit);
2406 edit_cursor_move (edit,
2407 current - edit->buffer.curs1 -
2408 (((current - edit->buffer.curs1) > 0) ? end_mark - start_mark : 0));
2409 edit_scroll_screen_over_cursor (edit);
2410 count_orig = count;
2411 while (count-- > start_mark)
2412 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2414 edit_set_markers (edit, edit->buffer.curs1, edit->buffer.curs1 + end_mark - start_mark, 0,
2417 /* Place cursor at the end of text selection */
2418 if (option_cursor_after_inserted_block)
2419 edit_cursor_move (edit, count_orig - start_mark);
2422 edit_scroll_screen_over_cursor (edit);
2423 g_free (copy_buf);
2424 edit->force |= REDRAW_PAGE;
2427 /* --------------------------------------------------------------------------------------------- */
2428 /** returns 1 if canceelled by user */
2431 edit_block_delete_cmd (WEdit * edit)
2433 off_t start_mark, end_mark;
2435 if (eval_marks (edit, &start_mark, &end_mark))
2436 return edit_block_delete (edit);
2438 edit_delete_line (edit);
2439 return 0;
2442 /* --------------------------------------------------------------------------------------------- */
2443 /** call with edit = 0 before shutdown to close memory leaks */
2445 void
2446 edit_replace_cmd (WEdit * edit, int again)
2448 /* 1 = search string, 2 = replace with */
2449 static char *saved1 = NULL; /* saved default[123] */
2450 static char *saved2 = NULL;
2451 char *input1 = NULL; /* user input from the dialog */
2452 char *input2 = NULL;
2453 GString *input2_str = NULL;
2454 char *disp1 = NULL;
2455 char *disp2 = NULL;
2456 long times_replaced = 0;
2457 gboolean once_found = FALSE;
2459 if (!edit)
2461 g_free (saved1), saved1 = NULL;
2462 g_free (saved2), saved2 = NULL;
2463 return;
2466 edit->force |= REDRAW_COMPLETELY;
2468 if (again && !saved1 && !saved2)
2469 again = 0;
2471 if (again)
2473 input1 = g_strdup (saved1 ? saved1 : "");
2474 input2 = g_strdup (saved2 ? saved2 : "");
2476 else
2478 char *tmp_inp1, *tmp_inp2;
2480 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
2481 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
2483 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2485 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2487 g_free (disp1);
2488 g_free (disp2);
2490 if (input1 == NULL || *input1 == '\0')
2492 edit->force = REDRAW_COMPLETELY;
2493 goto cleanup;
2496 tmp_inp1 = input1;
2497 tmp_inp2 = input2;
2498 input1 = edit_replace_cmd__conv_to_input (input1);
2499 input2 = edit_replace_cmd__conv_to_input (input2);
2500 g_free (tmp_inp1);
2501 g_free (tmp_inp2);
2503 g_free (saved1), saved1 = g_strdup (input1);
2504 g_free (saved2), saved2 = g_strdup (input2);
2506 mc_search_free (edit->search);
2507 edit->search = NULL;
2510 input2_str = g_string_new (input2);
2512 if (edit->search == NULL)
2514 #ifdef HAVE_CHARSET
2515 edit->search = mc_search_new (input1, -1, cp_source);
2516 #else
2517 edit->search = mc_search_new (input1, -1, NULL);
2518 #endif
2519 if (edit->search == NULL)
2521 edit->search_start = edit->buffer.curs1;
2522 goto cleanup;
2524 edit->search->search_type = edit_search_options.type;
2525 #ifdef HAVE_CHARSET
2526 edit->search->is_all_charsets = edit_search_options.all_codepages;
2527 #endif
2528 edit->search->is_case_sensitive = edit_search_options.case_sens;
2529 edit->search->whole_words = edit_search_options.whole_words;
2530 edit->search->search_fn = edit_search_cmd_callback;
2531 edit->search_line_type = edit_get_search_line_type (edit->search);
2532 edit_search_fix_search_start_if_selection (edit);
2535 if (edit->found_len && edit->search_start == edit->found_start + 1
2536 && edit_search_options.backwards)
2537 edit->search_start--;
2539 if (edit->found_len && edit->search_start == edit->found_start - 1
2540 && !edit_search_options.backwards)
2541 edit->search_start++;
2545 gsize len = 0;
2547 if (!editcmd_find (edit, &len))
2549 if (!(edit->search->error == MC_SEARCH_E_OK ||
2550 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2552 edit_error_dialog (_("Search"), edit->search->error_str);
2554 break;
2556 once_found = TRUE;
2558 edit->search_start = edit->search->normal_offset;
2559 /*returns negative on not found or error in pattern */
2561 if ((edit->search_start >= 0) && (edit->search_start < edit->buffer.size))
2563 gsize i;
2564 GString *repl_str;
2566 edit->found_start = edit->search_start;
2567 i = edit->found_len = len;
2569 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
2570 edit_scroll_screen_over_cursor (edit);
2572 if (edit->replace_mode == 0)
2574 long l;
2575 int prompt;
2577 l = edit->curs_row - WIDGET (edit)->lines / 3;
2578 if (l > 0)
2579 edit_scroll_downward (edit, l);
2580 if (l < 0)
2581 edit_scroll_upward (edit, -l);
2583 edit_scroll_screen_over_cursor (edit);
2584 edit->force |= REDRAW_PAGE;
2585 edit_render_keypress (edit);
2587 /*so that undo stops at each query */
2588 edit_push_key_press (edit);
2589 /* and prompt 2/3 down */
2590 disp1 = edit_replace_cmd__conv_to_display (saved1);
2591 disp2 = edit_replace_cmd__conv_to_display (saved2);
2592 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2593 g_free (disp1);
2594 g_free (disp2);
2596 if (prompt == B_REPLACE_ALL)
2597 edit->replace_mode = 1;
2598 else if (prompt == B_SKIP_REPLACE)
2600 if (edit_search_options.backwards)
2601 edit->search_start--;
2602 else
2603 edit->search_start++;
2604 continue; /* loop */
2606 else if (prompt == B_CANCEL)
2608 edit->replace_mode = -1;
2609 break; /* loop */
2613 repl_str = mc_search_prepare_replace_str (edit->search, input2_str);
2615 if (edit->search->error != MC_SEARCH_E_OK)
2617 edit_error_dialog (_("Replace"), edit->search->error_str);
2618 g_string_free (repl_str, TRUE);
2619 break;
2622 /* delete then insert new */
2623 for (i = 0; i < len; i++)
2624 edit_delete (edit, TRUE);
2626 for (i = 0; i < repl_str->len; i++)
2627 edit_insert (edit, repl_str->str[i]);
2629 edit->found_len = repl_str->len;
2630 g_string_free (repl_str, TRUE);
2631 times_replaced++;
2633 /* so that we don't find the same string again */
2634 if (edit_search_options.backwards)
2636 edit->search_start--;
2638 else
2640 edit->search_start += edit->found_len + (len == 0 ? 1 : 0);
2642 if (edit->search_start >= edit->buffer.size)
2643 break;
2646 edit_scroll_screen_over_cursor (edit);
2648 else
2650 /* try and find from right here for next search */
2651 edit->search_start = edit->buffer.curs1;
2652 edit_update_curs_col (edit);
2654 edit->force |= REDRAW_PAGE;
2655 edit_render_keypress (edit);
2657 if (times_replaced == 0)
2658 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL, 1, _("&OK"));
2659 break;
2662 while (edit->replace_mode >= 0);
2664 edit_scroll_screen_over_cursor (edit);
2665 edit->force |= REDRAW_COMPLETELY;
2666 edit_render_keypress (edit);
2668 if ((edit->replace_mode == 1) && (times_replaced != 0))
2669 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2671 cleanup:
2672 g_free (input1);
2673 g_free (input2);
2674 if (input2_str != NULL)
2675 g_string_free (input2_str, TRUE);
2678 /* --------------------------------------------------------------------------------------------- */
2680 mc_search_cbret_t
2681 edit_search_cmd_callback (const void *user_data, gsize char_offset, int *current_char)
2683 *current_char = edit_buffer_get_byte (&((WEdit *) user_data)->buffer, (off_t) char_offset);
2684 return MC_SEARCH_CB_OK;
2687 /* --------------------------------------------------------------------------------------------- */
2689 void
2690 edit_search_cmd (WEdit * edit, gboolean again)
2693 if (edit == NULL)
2694 return;
2696 if (!again)
2697 edit_search (edit);
2698 else if (edit->last_search_string != NULL)
2699 edit_do_search (edit);
2700 else
2702 /* find last search string in history */
2703 GList *history;
2705 history = history_get (MC_HISTORY_SHARED_SEARCH);
2706 if (history != NULL && history->data != NULL)
2708 edit->last_search_string = (char *) history->data;
2709 history->data = NULL;
2710 history = g_list_first (history);
2711 g_list_foreach (history, (GFunc) g_free, NULL);
2712 g_list_free (history);
2714 #ifdef HAVE_CHARSET
2715 edit->search = mc_search_new (edit->last_search_string, -1, cp_source);
2716 #else
2717 edit->search = mc_search_new (edit->last_search_string, -1, NULL);
2718 #endif
2719 if (edit->search == NULL)
2721 /* if not... then ask for an expression */
2722 g_free (edit->last_search_string);
2723 edit->last_search_string = NULL;
2724 edit_search (edit);
2726 else
2728 edit->search->search_type = edit_search_options.type;
2729 #ifdef HAVE_CHARSET
2730 edit->search->is_all_charsets = edit_search_options.all_codepages;
2731 #endif
2732 edit->search->is_case_sensitive = edit_search_options.case_sens;
2733 edit->search->whole_words = edit_search_options.whole_words;
2734 edit->search->search_fn = edit_search_cmd_callback;
2735 edit->search_line_type = edit_get_search_line_type (edit->search);
2736 edit_do_search (edit);
2739 else
2741 /* if not... then ask for an expression */
2742 g_free (edit->last_search_string);
2743 edit->last_search_string = NULL;
2744 edit_search (edit);
2750 /* --------------------------------------------------------------------------------------------- */
2752 * Check if it's OK to close the file. If there are unsaved changes, ask user.
2754 * @return TRUE if it's OK to exit, FALSE to continue editing.
2757 gboolean
2758 edit_ok_to_exit (WEdit * edit)
2760 const char *fname = N_("[NoName]");
2761 char *msg;
2762 int act;
2764 if (!edit->modified)
2765 return TRUE;
2767 if (edit->filename_vpath != NULL)
2768 fname = vfs_path_as_str (edit->filename_vpath);
2769 #ifdef ENABLE_NLS
2770 else
2771 fname = _(fname);
2772 #endif
2774 if (!mc_global.midnight_shutdown)
2776 if (!edit_check_newline (&edit->buffer))
2777 return FALSE;
2779 query_set_sel (2);
2781 msg = g_strdup_printf (_("File %s was modified.\nSave before close?"), fname);
2782 act = edit_query_dialog3 (_("Close file"), msg, _("&Yes"), _("&No"), _("&Cancel"));
2784 else
2786 msg = g_strdup_printf (_("Midnight Commander is being shut down.\nSave modified file %s?"),
2787 fname);
2788 act = edit_query_dialog2 (_("Quit"), msg, _("&Yes"), _("&No"));
2790 /* Esc is No */
2791 if (act == -1)
2792 act = 1;
2795 g_free (msg);
2797 switch (act)
2799 case 0: /* Yes */
2800 edit_push_markers (edit);
2801 edit_set_markers (edit, 0, 0, 0, 0);
2802 if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2803 return mc_global.midnight_shutdown;
2804 break;
2805 case 1: /* No */
2806 break;
2807 case 2: /* Cancel quit */
2808 case -1: /* Esc */
2809 return FALSE;
2812 return TRUE;
2815 /* --------------------------------------------------------------------------------------------- */
2816 /** save block, returns TRUE on success */
2818 gboolean
2819 edit_save_block (WEdit * edit, const char *filename, off_t start, off_t finish)
2821 int file;
2822 off_t len = 1;
2823 vfs_path_t *vpath;
2825 vpath = vfs_path_from_str (filename);
2826 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2827 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2828 vfs_path_free (vpath);
2829 if (file == -1)
2830 return FALSE;
2832 if (edit->column_highlight)
2834 int r;
2836 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2837 if (r > 0)
2839 unsigned char *block, *p;
2841 p = block = edit_get_block (edit, start, finish, &len);
2842 while (len)
2844 r = mc_write (file, p, len);
2845 if (r < 0)
2846 break;
2847 p += r;
2848 len -= r;
2850 g_free (block);
2853 else
2855 unsigned char *buf;
2856 off_t i = start;
2857 off_t end;
2859 len = finish - start;
2860 buf = g_malloc0 (TEMP_BUF_LEN);
2861 while (start != finish)
2863 end = min (finish, start + TEMP_BUF_LEN);
2864 for (; i < end; i++)
2865 buf[i - start] = edit_buffer_get_byte (&edit->buffer, i);
2866 len -= mc_write (file, (char *) buf, end - start);
2867 start = end;
2869 g_free (buf);
2871 mc_close (file);
2873 return (len == 0);
2876 /* --------------------------------------------------------------------------------------------- */
2878 void
2879 edit_paste_from_history (WEdit * edit)
2881 (void) edit;
2882 edit_error_dialog (_("Error"), _("This function is not implemented"));
2885 /* --------------------------------------------------------------------------------------------- */
2887 gboolean
2888 edit_copy_to_X_buf_cmd (WEdit * edit)
2890 off_t start_mark, end_mark;
2892 if (!eval_marks (edit, &start_mark, &end_mark))
2893 return TRUE;
2895 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2897 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2898 return FALSE;
2900 /* try use external clipboard utility */
2901 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2903 return TRUE;
2906 /* --------------------------------------------------------------------------------------------- */
2908 gboolean
2909 edit_cut_to_X_buf_cmd (WEdit * edit)
2911 off_t start_mark, end_mark;
2913 if (!eval_marks (edit, &start_mark, &end_mark))
2914 return TRUE;
2916 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2918 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2919 return FALSE;
2921 /* try use external clipboard utility */
2922 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2924 edit_block_delete_cmd (edit);
2925 edit_mark_cmd (edit, TRUE);
2927 return TRUE;
2930 /* --------------------------------------------------------------------------------------------- */
2932 gboolean
2933 edit_paste_from_X_buf_cmd (WEdit * edit)
2935 vfs_path_t *tmp;
2936 gboolean ret;
2938 /* try use external clipboard utility */
2939 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
2940 tmp = mc_config_get_full_vpath (EDIT_CLIP_FILE);
2941 ret = (edit_insert_file (edit, tmp) >= 0);
2942 vfs_path_free (tmp);
2944 return ret;
2947 /* --------------------------------------------------------------------------------------------- */
2949 * Ask user for the line and go to that line.
2950 * Negative numbers mean line from the end (i.e. -1 is the last line).
2953 void
2954 edit_goto_cmd (WEdit * edit)
2956 char *f;
2957 static long line = 0; /* line as typed, saved as default */
2958 long l;
2959 char *error;
2960 char s[32];
2962 g_snprintf (s, sizeof (s), "%ld", line);
2963 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE, line ? s : "",
2964 INPUT_COMPLETE_NONE);
2965 if (!f)
2966 return;
2968 if (!*f)
2970 g_free (f);
2971 return;
2974 l = strtol (f, &error, 0);
2975 if (*error)
2977 g_free (f);
2978 return;
2981 line = l;
2982 if (l < 0)
2983 l = edit->buffer.lines + l + 2;
2984 edit_move_display (edit, l - WIDGET (edit)->lines / 2 - 1);
2985 edit_move_to_line (edit, l - 1);
2986 edit->force |= REDRAW_COMPLETELY;
2987 g_free (f);
2991 /* --------------------------------------------------------------------------------------------- */
2992 /** Return TRUE on success */
2994 gboolean
2995 edit_save_block_cmd (WEdit * edit)
2997 off_t start_mark, end_mark;
2998 char *exp, *tmp;
2999 gboolean ret = FALSE;
3001 if (!eval_marks (edit, &start_mark, &end_mark))
3002 return TRUE;
3004 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3005 exp =
3006 input_expand_dialog (_("Save block"), _("Enter file name:"),
3007 MC_HISTORY_EDIT_SAVE_BLOCK, tmp, INPUT_COMPLETE_FILENAMES);
3008 g_free (tmp);
3009 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3011 if (exp != NULL && *exp != '\0')
3013 if (edit_save_block (edit, exp, start_mark, end_mark))
3014 ret = TRUE;
3015 else
3016 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3018 edit->force |= REDRAW_COMPLETELY;
3021 g_free (exp);
3023 return ret;
3027 /* --------------------------------------------------------------------------------------------- */
3029 /** returns TRUE on success */
3030 gboolean
3031 edit_insert_file_cmd (WEdit * edit)
3033 char *tmp;
3034 char *exp;
3035 gboolean ret = FALSE;
3037 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3038 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3039 MC_HISTORY_EDIT_INSERT_FILE, tmp, INPUT_COMPLETE_FILENAMES);
3040 g_free (tmp);
3042 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3044 if (exp != NULL && *exp != '\0')
3046 vfs_path_t *exp_vpath;
3048 exp_vpath = vfs_path_from_str (exp);
3049 ret = (edit_insert_file (edit, exp_vpath) >= 0);
3050 vfs_path_free (exp_vpath);
3052 if (!ret)
3053 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3056 g_free (exp);
3058 edit->force |= REDRAW_COMPLETELY;
3059 return ret;
3062 /* --------------------------------------------------------------------------------------------- */
3063 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3066 edit_sort_cmd (WEdit * edit)
3068 static char *old = 0;
3069 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3070 off_t start_mark, end_mark;
3071 int e;
3073 if (!eval_marks (edit, &start_mark, &end_mark))
3075 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3076 return 0;
3079 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
3080 edit_save_block (edit, tmp, start_mark, end_mark);
3081 g_free (tmp);
3083 exp = input_dialog (_("Run sort"),
3084 _("Enter sort options (see manpage) separated by whitespace:"),
3085 MC_HISTORY_EDIT_SORT, (old != NULL) ? old : "", INPUT_COMPLETE_NONE);
3087 if (!exp)
3088 return 1;
3089 g_free (old);
3090 old = exp;
3091 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
3092 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
3093 tmp =
3094 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3095 " > ", tmp_edit_temp_name, (char *) NULL);
3096 g_free (tmp_edit_temp_name);
3097 g_free (tmp_edit_block_name);
3099 e = system (tmp);
3100 g_free (tmp);
3101 if (e)
3103 if (e == -1 || e == 127)
3105 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3107 else
3109 char q[8];
3110 sprintf (q, "%d ", e);
3111 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3112 edit_error_dialog (_("Sort"), tmp);
3113 g_free (tmp);
3115 return -1;
3118 edit->force |= REDRAW_COMPLETELY;
3120 if (edit_block_delete_cmd (edit))
3121 return 1;
3124 vfs_path_t *tmp_vpath;
3126 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3127 edit_insert_file (edit, tmp_vpath);
3128 vfs_path_free (tmp_vpath);
3130 return 0;
3133 /* --------------------------------------------------------------------------------------------- */
3135 * Ask user for a command, execute it and paste its output back to the
3136 * editor.
3140 edit_ext_cmd (WEdit * edit)
3142 char *exp, *tmp, *tmp_edit_temp_file;
3143 int e;
3145 exp =
3146 input_dialog (_("Paste output of external command"),
3147 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, NULL,
3148 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_VARIABLES | INPUT_COMPLETE_USERNAMES
3149 | INPUT_COMPLETE_HOSTNAMES | INPUT_COMPLETE_CD | INPUT_COMPLETE_COMMANDS |
3150 INPUT_COMPLETE_SHELL_ESC);
3152 if (!exp)
3153 return 1;
3155 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3156 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3157 g_free (tmp_edit_temp_file);
3158 e = system (tmp);
3159 g_free (tmp);
3160 g_free (exp);
3162 if (e)
3164 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3165 return -1;
3168 edit->force |= REDRAW_COMPLETELY;
3171 vfs_path_t *tmp_vpath;
3173 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3174 edit_insert_file (edit, tmp_vpath);
3175 vfs_path_free (tmp_vpath);
3177 return 0;
3180 /* --------------------------------------------------------------------------------------------- */
3181 /** if block is 1, a block must be highlighted and the shell command
3182 processes it. If block is 0 the shell command is a straight system
3183 command, that just produces some output which is to be inserted */
3185 void
3186 edit_block_process_cmd (WEdit * edit, int macro_number)
3188 char *fname;
3189 char *macros_fname = NULL;
3191 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3192 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3193 user_menu (edit, macros_fname, 0);
3194 g_free (fname);
3195 g_free (macros_fname);
3196 edit->force |= REDRAW_COMPLETELY;
3199 /* --------------------------------------------------------------------------------------------- */
3201 void
3202 edit_mail_dialog (WEdit * edit)
3204 char *tmail_to = NULL;
3205 char *tmail_subject = NULL;
3206 char *tmail_cc = NULL;
3208 static char *mail_cc_last = 0;
3209 static char *mail_subject_last = 0;
3210 static char *mail_to_last = 0;
3213 quick_widget_t quick_widgets[] = {
3214 /* *INDENT-OFF* */
3215 QUICK_LABEL (N_("mail -s <subject> -c <cc> <to>"), NULL),
3216 QUICK_LABELED_INPUT (N_("To"), input_label_above,
3217 mail_to_last != NULL ? mail_to_last : "", "mail-dlg-input-3",
3218 &tmail_to, NULL, FALSE, FALSE, INPUT_COMPLETE_USERNAMES),
3219 QUICK_LABELED_INPUT (N_("Subject"), input_label_above,
3220 mail_subject_last != NULL ? mail_subject_last : "", "mail-dlg-input-2",
3221 &tmail_subject, NULL, FALSE, FALSE, INPUT_COMPLETE_NONE),
3222 QUICK_LABELED_INPUT (N_("Copies to"), input_label_above,
3223 mail_cc_last != NULL ? mail_cc_last : "", "mail-dlg-input",
3224 &tmail_cc, NULL, FALSE, FALSE, INPUT_COMPLETE_USERNAMES),
3225 QUICK_BUTTONS_OK_CANCEL,
3226 QUICK_END
3227 /* *INDENT-ON* */
3230 quick_dialog_t qdlg = {
3231 -1, -1, 50,
3232 N_("Mail"), "[Input Line Keys]",
3233 quick_widgets, NULL, NULL
3236 if (quick_dialog (&qdlg) != B_CANCEL)
3238 g_free (mail_cc_last);
3239 g_free (mail_subject_last);
3240 g_free (mail_to_last);
3241 mail_cc_last = tmail_cc;
3242 mail_subject_last = tmail_subject;
3243 mail_to_last = tmail_to;
3244 pipe_mail (&edit->buffer, mail_to_last, mail_subject_last, mail_cc_last);
3248 /* --------------------------------------------------------------------------------------------- */
3250 /*******************/
3251 /* Word Completion */
3252 /*******************/
3255 * Complete current word using regular expression search
3256 * backwards beginning at the current cursor position.
3259 void
3260 edit_complete_word_cmd (WEdit * edit)
3262 gsize i, max_len, word_len = 0, num_compl = 0;
3263 off_t word_start = 0;
3264 GString *match_expr;
3265 GString *compl[MAX_WORD_COMPLETIONS]; /* completions */
3267 /* search start of word to be completed */
3268 if (!edit_find_word_start (&edit->buffer, &word_start, &word_len))
3269 return;
3271 /* prepare match expression */
3272 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3273 match_expr = g_string_new ("(^|\\s+|\\b)");
3274 for (i = 0; i < word_len; i++)
3275 g_string_append_c (match_expr, edit_buffer_get_byte (&edit->buffer, word_start + i));
3276 g_string_append (match_expr,
3277 "[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+");
3279 /* collect the possible completions */
3280 /* start search from begin to end of file */
3281 max_len =
3282 edit_collect_completions (edit, word_start, word_len, match_expr->str, (GString **) & compl,
3283 &num_compl);
3285 if (num_compl > 0)
3287 /* insert completed word if there is only one match */
3288 if (num_compl == 1)
3289 edit_complete_word_insert_recoded_completion (edit, compl[0]->str, word_len);
3290 /* more than one possible completion => ask the user */
3291 else
3293 char *curr_compl;
3295 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3296 /* !!! pressed again the selection dialog pops up, but that !!! */
3297 /* !!! seems to require a further internal state !!! */
3298 /*tty_beep (); */
3300 /* let the user select the preferred completion */
3301 curr_compl = editcmd_dialog_completion_show (edit, max_len,
3302 (GString **) & compl, num_compl);
3304 if (curr_compl != NULL)
3306 edit_complete_word_insert_recoded_completion (edit, curr_compl, word_len);
3307 g_free (curr_compl);
3312 g_string_free (match_expr, TRUE);
3313 /* release memory before return */
3314 for (i = 0; i < num_compl; i++)
3315 g_string_free (compl[i], TRUE);
3318 /* --------------------------------------------------------------------------------------------- */
3320 #ifdef HAVE_CHARSET
3321 void
3322 edit_select_codepage_cmd (WEdit * edit)
3324 if (do_select_codepage ())
3325 edit_set_codeset (edit);
3327 edit->force = REDRAW_PAGE;
3328 widget_redraw (WIDGET (edit));
3330 #endif
3332 /* --------------------------------------------------------------------------------------------- */
3334 void
3335 edit_insert_literal_cmd (WEdit * edit)
3337 int char_for_insertion;
3339 char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3340 _("Press any key:"), FALSE);
3341 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3344 /* --------------------------------------------------------------------------------------------- */
3346 void
3347 edit_begin_end_macro_cmd (WEdit * edit)
3349 /* edit is a pointer to the widget */
3350 if (edit != NULL)
3352 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3353 edit_execute_key_command (edit, command, -1);
3357 /* --------------------------------------------------------------------------------------------- */
3359 void
3360 edit_begin_end_repeat_cmd (WEdit * edit)
3362 /* edit is a pointer to the widget */
3363 if (edit != NULL)
3365 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3366 edit_execute_key_command (edit, command, -1);
3370 /* --------------------------------------------------------------------------------------------- */
3372 gboolean
3373 edit_load_forward_cmd (WEdit * edit)
3375 if (edit->modified
3376 && edit_query_dialog2 (_("Warning"),
3377 _("Current text was modified without a file save.\n"
3378 "Continue discards these changes"), _("C&ontinue"),
3379 _("&Cancel")) == 1)
3381 edit->force |= REDRAW_COMPLETELY;
3382 return TRUE;
3385 if (edit_stack_iterator + 1 >= MAX_HISTORY_MOVETO)
3386 return FALSE;
3388 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3389 return FALSE;
3391 edit_stack_iterator++;
3392 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3393 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3394 edit_history_moveto[edit_stack_iterator].line);
3396 return FALSE;
3399 /* --------------------------------------------------------------------------------------------- */
3401 gboolean
3402 edit_load_back_cmd (WEdit * edit)
3404 if (edit->modified
3405 && edit_query_dialog2 (_("Warning"),
3406 _("Current text was modified without a file save.\n"
3407 "Continue discards these changes"), _("C&ontinue"),
3408 _("&Cancel")) == 1)
3410 edit->force |= REDRAW_COMPLETELY;
3411 return TRUE;
3414 /* we are in the bottom of the stack, NO WAY! */
3415 if (edit_stack_iterator == 0)
3416 return FALSE;
3418 edit_stack_iterator--;
3419 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3420 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3421 edit_history_moveto[edit_stack_iterator].line);
3423 return FALSE;
3426 /* --------------------------------------------------------------------------------------------- */
3428 void
3429 edit_get_match_keyword_cmd (WEdit * edit)
3431 gsize word_len = 0, max_len = 0;
3432 int num_def = 0;
3433 gsize i;
3434 off_t word_start = 0;
3435 GString *match_expr;
3436 char *path = NULL;
3437 char *ptr = NULL;
3438 char *tagfile = NULL;
3440 etags_hash_t def_hash[MAX_DEFINITIONS];
3442 for (i = 0; i < MAX_DEFINITIONS; i++)
3444 def_hash[i].filename = NULL;
3447 /* search start of word to be completed */
3448 if (!edit_find_word_start (&edit->buffer, &word_start, &word_len))
3449 return;
3451 /* prepare match expression */
3452 match_expr = g_string_sized_new (word_len);
3453 for (i = 0; i < word_len; i++)
3454 g_string_append_c (match_expr, edit_buffer_get_byte (&edit->buffer, word_start + i));
3456 ptr = g_get_current_dir ();
3457 path = g_strconcat (ptr, G_DIR_SEPARATOR_S, (char *) NULL);
3458 g_free (ptr);
3460 /* Recursive search file 'TAGS' in parent dirs */
3463 ptr = g_path_get_dirname (path);
3464 g_free (path);
3465 path = ptr;
3466 g_free (tagfile);
3467 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3468 if (exist_file (tagfile))
3469 break;
3471 while (strcmp (path, G_DIR_SEPARATOR_S) != 0);
3473 if (tagfile)
3475 num_def =
3476 etags_set_definition_hash (tagfile, path, match_expr->str, (etags_hash_t *) & def_hash);
3477 g_free (tagfile);
3479 g_free (path);
3481 max_len = MAX_WIDTH_DEF_DIALOG;
3482 word_len = 0;
3483 if (num_def > 0)
3485 editcmd_dialog_select_definition_show (edit, match_expr->str, max_len, word_len,
3486 (etags_hash_t *) & def_hash, num_def);
3488 g_string_free (match_expr, TRUE);
3491 /* --------------------------------------------------------------------------------------------- */
3493 #ifdef HAVE_ASPELL
3495 edit_suggest_current_word (WEdit * edit)
3497 gsize cut_len = 0;
3498 gsize word_len = 0;
3499 off_t word_start = 0;
3500 int retval = B_SKIP_WORD;
3501 GString *match_word;
3503 /* search start of word to spell check */
3504 match_word = edit_buffer_get_word_from_pos (&edit->buffer, edit->buffer.curs1, &word_start,
3505 &cut_len);
3506 word_len = match_word->len;
3508 #ifdef HAVE_CHARSET
3509 if (mc_global.source_codepage >= 0 && (mc_global.source_codepage != mc_global.display_codepage))
3511 GString *tmp_word;
3513 tmp_word = str_convert_to_display (match_word->str);
3514 g_string_free (match_word, TRUE);
3515 match_word = tmp_word;
3517 #endif
3518 if (!aspell_check (match_word->str, (int) word_len))
3520 GArray *suggest;
3521 unsigned int res;
3523 suggest = g_array_new (TRUE, FALSE, sizeof (char *));
3525 res = aspell_suggest (suggest, match_word->str, (int) word_len);
3526 if (res != 0)
3528 char *new_word = NULL;
3530 edit->found_start = word_start;
3531 edit->found_len = word_len;
3532 edit->force |= REDRAW_PAGE;
3533 edit_scroll_screen_over_cursor (edit);
3534 edit_render_keypress (edit);
3536 retval = spell_dialog_spell_suggest_show (edit, match_word->str, &new_word, suggest);
3537 edit_cursor_move (edit, word_len - cut_len);
3539 if (retval == B_ENTER && new_word != NULL)
3541 guint i;
3542 char *cp_word;
3544 #ifdef HAVE_CHARSET
3545 if (mc_global.source_codepage >= 0 &&
3546 (mc_global.source_codepage != mc_global.display_codepage))
3548 GString *tmp_word;
3550 tmp_word = str_convert_to_input (new_word);
3551 g_free (new_word);
3552 new_word = g_string_free (tmp_word, FALSE);
3554 #endif
3555 cp_word = new_word;
3556 for (i = 0; i < word_len; i++)
3557 edit_backspace (edit, TRUE);
3558 for (; *new_word; new_word++)
3559 edit_insert (edit, *new_word);
3560 g_free (cp_word);
3562 else if (retval == B_ADD_WORD && match_word != NULL)
3563 aspell_add_to_dict (match_word->str, (int) word_len);
3566 g_array_free (suggest, TRUE);
3567 edit->found_start = 0;
3568 edit->found_len = 0;
3570 g_string_free (match_word, TRUE);
3571 return retval;
3574 /* --------------------------------------------------------------------------------------------- */
3576 void
3577 edit_spellcheck_file (WEdit * edit)
3579 if (edit->buffer.curs_line > 0)
3581 edit_cursor_move (edit, -edit->buffer.curs1);
3582 edit_move_to_prev_col (edit, 0);
3583 edit_update_curs_row (edit);
3588 int c1, c2;
3590 c2 = edit_buffer_get_current_byte (&edit->buffer);
3594 if (edit->buffer.curs1 >= edit->buffer.size)
3595 return;
3597 c1 = c2;
3598 edit_cursor_move (edit, 1);
3599 c2 = edit_buffer_get_current_byte (&edit->buffer);
3601 while (is_break_char (c1) || is_break_char (c2));
3603 while (edit_suggest_current_word (edit) != B_CANCEL);
3606 /* --------------------------------------------------------------------------------------------- */
3608 void
3609 edit_set_spell_lang (void)
3611 GArray *lang_list;
3613 lang_list = g_array_new (TRUE, FALSE, sizeof (char *));
3614 if (aspell_get_lang_list (lang_list) != 0)
3616 char *lang;
3618 lang = spell_dialog_lang_list_show (lang_list);
3619 if (lang != NULL)
3620 (void) aspell_set_lang (lang);
3622 aspell_array_clean (lang_list);
3624 #endif /* HAVE_ASPELL */
3626 /* --------------------------------------------------------------------------------------------- */