mcedit: rename functions:
[midnight-commander.git] / src / editor / editcmd.c
blobb4667c36a639d661354fb93fa864a49708669c0b
1 /*
2 Editor high level editing commands
4 Copyright (C) 1996-2016
5 Free Software Foundation, Inc.
7 Written by:
8 Paul Sheer, 1996, 1997
9 Andrew Borodin <aborodin@vmail.ru>, 2012-2014
10 Ilia Maslakov <il.smind@gmail.com>, 2012
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file
29 * \brief Source: editor high level editing commands
30 * \author Paul Sheer
31 * \date 1996, 1997
34 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
36 #include <config.h>
38 #ifdef HAVE_ASSERT_H
39 #include <assert.h>
40 #endif
41 #include <ctype.h>
43 #include <stdio.h>
44 #include <stdarg.h>
45 #include <sys/types.h>
46 #include <unistd.h>
47 #include <string.h>
48 #include <errno.h>
49 #include <sys/stat.h>
50 #include <stdlib.h>
52 #include "lib/global.h"
53 #include "lib/tty/tty.h"
54 #include "lib/tty/key.h" /* XCTRL */
55 #include "lib/mcconfig.h"
56 #include "lib/skin.h"
57 #include "lib/strutil.h" /* utf string functions */
58 #include "lib/fileloc.h"
59 #include "lib/lock.h"
60 #include "lib/util.h" /* tilde_expand() */
61 #include "lib/vfs/vfs.h"
62 #include "lib/widget.h"
63 #include "lib/event.h" /* mc_event_raise() */
64 #ifdef HAVE_CHARSET
65 #include "lib/charsets.h"
66 #endif
68 #include "src/history.h"
69 #include "src/setup.h" /* option_tab_spacing */
70 #ifdef HAVE_CHARSET
71 #include "src/selcodepage.h"
72 #endif
73 #include "src/keybind-defaults.h"
74 #include "src/util.h" /* check_for_default() */
76 #include "edit-impl.h"
77 #include "editwidget.h"
78 #include "editcmd_dialogs.h"
79 #ifdef HAVE_ASPELL
80 #include "spell.h"
81 #include "spell_dialogs.h"
82 #endif
83 #include "etags.h"
85 /*** global variables ****************************************************************************/
87 /* search and replace: */
88 int search_create_bookmark = FALSE;
90 /* queries on a save */
91 int edit_confirm_save = 1;
93 /* whether we need to drop selection on copy to buffer */
94 int option_drop_selection_on_copy = 1;
96 /*** file scope macro definitions ****************************************************************/
98 #define space_width 1
100 #define TEMP_BUF_LEN 1024
102 #define INPUT_INDEX 9
104 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
105 (and the above) routines to work properly - paul */
107 #define is_digit(x) ((x) >= '0' && (x) <= '9')
109 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
111 /*** file scope type declarations ****************************************************************/
113 typedef struct
115 simple_status_msg_t status_msg; /* base class */
117 gboolean first;
118 WEdit *edit;
119 off_t offset;
120 } edit_search_status_msg_t;
122 /*** file scope variables ************************************************************************/
124 static unsigned long edit_save_mode_radio_id, edit_save_mode_input_id;
126 /* --------------------------------------------------------------------------------------------- */
127 /*** file scope functions ************************************************************************/
128 /* --------------------------------------------------------------------------------------------- */
130 static int
131 edit_search_status_update_cb (status_msg_t * sm)
133 simple_status_msg_t *ssm = SIMPLE_STATUS_MSG (sm);
134 edit_search_status_msg_t *esm = (edit_search_status_msg_t *) sm;
135 Widget *wd = WIDGET (sm->dlg);
137 if (verbose)
138 label_set_textv (ssm->label, _("Searching %s: %3d%%"), esm->edit->last_search_string,
139 edit_buffer_calc_percent (&esm->edit->buffer, esm->offset));
140 else
141 label_set_textv (ssm->label, _("Searching %s"), esm->edit->last_search_string);
143 if (esm->first)
145 int wd_width;
146 Widget *lw = WIDGET (ssm->label);
148 wd_width = max (wd->cols, lw->cols + 6);
149 widget_set_size (wd, wd->y, wd->x, wd->lines, wd_width);
150 widget_set_size (lw, lw->y, wd->x + (wd->cols - lw->cols) / 2, lw->lines, lw->cols);
151 esm->first = FALSE;
154 return status_msg_common_update (sm);
157 /* --------------------------------------------------------------------------------------------- */
159 static cb_ret_t
160 edit_save_mode_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
162 switch (msg)
164 case MSG_NOTIFY:
165 if (sender != NULL && sender->id == edit_save_mode_radio_id)
167 Widget *ww;
169 ww = dlg_find_by_id (DIALOG (w), edit_save_mode_input_id);
170 widget_disable (ww, RADIO (sender)->sel != 2);
171 return MSG_HANDLED;
174 return MSG_NOT_HANDLED;
176 default:
177 return dlg_default_callback (w, sender, msg, parm, data);
181 /* --------------------------------------------------------------------------------------------- */
183 /* If 0 (quick save) then a) create/truncate <filename> file,
184 b) save to <filename>;
185 if 1 (safe save) then a) save to <tempnam>,
186 b) rename <tempnam> to <filename>;
187 if 2 (do backups) then a) save to <tempnam>,
188 b) rename <filename> to <filename.backup_ext>,
189 c) rename <tempnam> to <filename>. */
191 /* returns 0 on error, -1 on abort */
193 static int
194 edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath)
196 char *p;
197 gchar *tmp;
198 off_t filelen = 0;
199 int this_save_mode, rv, fd = -1;
200 vfs_path_t *real_filename_vpath;
201 vfs_path_t *savename_vpath = NULL;
202 const char *start_filename;
203 const vfs_path_element_t *vpath_element;
204 struct stat sb;
206 vpath_element = vfs_path_get_by_index (filename_vpath, 0);
207 if (vpath_element == NULL)
208 return 0;
210 start_filename = vpath_element->path;
211 if (*start_filename == '\0')
212 return 0;
214 if (!IS_PATH_SEP (*start_filename) && edit->dir_vpath != NULL)
215 real_filename_vpath = vfs_path_append_vpath_new (edit->dir_vpath, filename_vpath, NULL);
216 else
217 real_filename_vpath = vfs_path_clone (filename_vpath);
219 this_save_mode = option_save_mode;
220 if (this_save_mode != EDIT_QUICK_SAVE)
222 if (!vfs_file_is_local (real_filename_vpath)
223 || (fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY)) == -1)
226 * The file does not exists yet, so no safe save or
227 * backup are necessary.
229 this_save_mode = EDIT_QUICK_SAVE;
231 if (fd != -1)
232 mc_close (fd);
235 rv = mc_stat (real_filename_vpath, &sb);
236 if (rv == 0)
238 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt && sb.st_nlink > 1)
240 rv = edit_query_dialog3 (_("Warning"),
241 _("File has hard-links. Detach before saving?"),
242 _("&Yes"), _("&No"), _("&Cancel"));
243 switch (rv)
245 case 0:
246 this_save_mode = EDIT_SAFE_SAVE;
247 /* fallthrough */
248 case 1:
249 edit->skip_detach_prompt = 1;
250 break;
251 default:
252 vfs_path_free (real_filename_vpath);
253 return -1;
257 /* Prevent overwriting changes from other editor sessions. */
258 if (edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
260 /* The default action is "Cancel". */
261 query_set_sel (1);
263 rv = edit_query_dialog2 (_("Warning"),
264 _("The file has been modified in the meantime. Save anyway?"),
265 _("&Yes"), _("&Cancel"));
266 if (rv != 0)
268 vfs_path_free (real_filename_vpath);
269 return -1;
274 if (this_save_mode != EDIT_QUICK_SAVE)
276 char *savedir, *saveprefix;
278 savedir = vfs_path_tokens_get (real_filename_vpath, 0, -1);
279 if (savedir == NULL)
280 savedir = g_strdup (".");
282 /* Token-related function never return leading slash, so we need add it manually */
283 saveprefix = mc_build_filename (PATH_SEP_STR, savedir, "cooledit", NULL);
284 g_free (savedir);
285 fd = mc_mkstemps (&savename_vpath, saveprefix, NULL);
286 g_free (saveprefix);
287 if (savename_vpath == NULL)
289 vfs_path_free (real_filename_vpath);
290 return 0;
292 /* FIXME:
293 * Close for now because mc_mkstemps use pure open system call
294 * to create temporary file and it needs to be reopened by
295 * VFS-aware mc_open().
297 close (fd);
299 else
300 savename_vpath = vfs_path_clone (real_filename_vpath);
302 (void) mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
303 (void) mc_chmod (savename_vpath, edit->stat1.st_mode);
305 fd = mc_open (savename_vpath, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
306 if (fd == -1)
307 goto error_save;
309 /* pipe save */
310 p = edit_get_write_filter (savename_vpath, real_filename_vpath);
311 if (p != NULL)
313 FILE *file;
315 mc_close (fd);
316 file = (FILE *) popen (p, "w");
318 if (file)
320 filelen = edit_write_stream (edit, file);
321 #if 1
322 pclose (file);
323 #else
324 if (pclose (file) != 0)
326 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
327 edit_error_dialog (_("Error"), tmp);
328 g_free (tmp);
329 g_free (p);
330 goto error_save;
332 #endif
334 else
336 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
337 edit_error_dialog (_("Error"), get_sys_error (tmp));
338 g_free (p);
339 g_free (tmp);
340 goto error_save;
342 g_free (p);
344 else if (edit->lb == LB_ASIS)
345 { /* do not change line breaks */
346 filelen = edit_buffer_write_file (&edit->buffer, fd);
348 if (filelen != edit->buffer.size)
350 mc_close (fd);
351 goto error_save;
353 if (mc_close (fd) != 0)
354 goto error_save;
355 /* Update the file information, especially the mtime. */
356 if (mc_stat (savename_vpath, &edit->stat1) == -1)
357 goto error_save;
359 else
360 { /* change line breaks */
361 FILE *file;
362 const vfs_path_element_t *path_element;
364 mc_close (fd);
366 path_element = vfs_path_get_by_index (savename_vpath, -1);
367 file = (FILE *) fopen (path_element->path, "w");
368 if (file != NULL)
370 filelen = edit_write_stream (edit, file);
371 fclose (file);
373 else
375 char *msg;
377 msg = g_strdup_printf (_("Cannot open file for writing: %s"), path_element->path);
378 edit_error_dialog (_("Error"), msg);
379 g_free (msg);
380 goto error_save;
384 if (filelen != edit->buffer.size)
385 goto error_save;
387 if (this_save_mode == EDIT_DO_BACKUP)
389 char *tmp_store_filename;
390 vfs_path_element_t *last_vpath_element;
391 vfs_path_t *tmp_vpath;
392 gboolean ok;
394 #ifdef HAVE_ASSERT_H
395 assert (option_backup_ext != NULL);
396 #endif
397 /* add backup extension to the path */
398 tmp_vpath = vfs_path_clone (real_filename_vpath);
399 last_vpath_element = (vfs_path_element_t *) vfs_path_get_by_index (tmp_vpath, -1);
400 tmp_store_filename = last_vpath_element->path;
401 last_vpath_element->path = g_strdup_printf ("%s%s", tmp_store_filename, option_backup_ext);
402 g_free (tmp_store_filename);
404 ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
405 vfs_path_free (tmp_vpath);
406 if (!ok)
407 goto error_save;
410 if (this_save_mode != EDIT_QUICK_SAVE)
411 if (mc_rename (savename_vpath, real_filename_vpath) == -1)
412 goto error_save;
414 vfs_path_free (real_filename_vpath);
415 vfs_path_free (savename_vpath);
416 return 1;
417 error_save:
418 /* FIXME: Is this safe ?
419 * if (this_save_mode != EDIT_QUICK_SAVE)
420 * mc_unlink (savename);
422 vfs_path_free (real_filename_vpath);
423 vfs_path_free (savename_vpath);
424 return 0;
427 /* --------------------------------------------------------------------------------------------- */
429 static gboolean
430 edit_check_newline (const edit_buffer_t * buf)
432 return !(option_check_nl_at_eof && buf->size > 0
433 && edit_buffer_get_byte (buf, buf->size - 1) != '\n'
434 && edit_query_dialog2 (_("Warning"),
435 _("The file you are saving does not end with a newline."),
436 _("C&ontinue"), _("&Cancel")) != 0);
439 /* --------------------------------------------------------------------------------------------- */
441 static vfs_path_t *
442 edit_get_save_file_as (WEdit * edit)
444 static LineBreaks cur_lb = LB_ASIS;
445 char *filename_res;
446 vfs_path_t *ret_vpath = NULL;
448 const char *lb_names[LB_NAMES] = {
449 N_("&Do not change"),
450 N_("&Unix format (LF)"),
451 N_("&Windows/DOS format (CR LF)"),
452 N_("&Macintosh format (CR)")
455 quick_widget_t quick_widgets[] = {
456 /* *INDENT-OFF* */
457 QUICK_LABELED_INPUT (N_("Enter file name:"), input_label_above,
458 vfs_path_as_str (edit->filename_vpath), "save-as",
459 &filename_res, NULL, FALSE, FALSE, INPUT_COMPLETE_FILENAMES),
460 QUICK_SEPARATOR (TRUE),
461 QUICK_LABEL (N_("Change line breaks to:"), NULL),
462 QUICK_RADIO (LB_NAMES, lb_names, (int *) &cur_lb, NULL),
463 QUICK_BUTTONS_OK_CANCEL,
464 QUICK_END
465 /* *INDENT-ON* */
468 quick_dialog_t qdlg = {
469 -1, -1, 64,
470 N_("Save As"), "[Save File As]",
471 quick_widgets, NULL, NULL
474 if (quick_dialog (&qdlg) != B_CANCEL)
476 char *fname;
478 edit->lb = cur_lb;
479 fname = tilde_expand (filename_res);
480 g_free (filename_res);
481 ret_vpath = vfs_path_from_str (fname);
482 g_free (fname);
485 return ret_vpath;
488 /* --------------------------------------------------------------------------------------------- */
490 /** returns TRUE on success */
492 static gboolean
493 edit_save_cmd (WEdit * edit)
495 int res, save_lock = 0;
497 if (!edit->locked && !edit->delete_file)
498 save_lock = lock_file (edit->filename_vpath);
499 res = edit_save_file (edit, edit->filename_vpath);
501 /* Maintain modify (not save) lock on failure */
502 if ((res > 0 && edit->locked) || save_lock)
503 edit->locked = unlock_file (edit->filename_vpath);
505 /* On failure try 'save as', it does locking on its own */
506 if (res == 0)
507 return edit_save_as_cmd (edit);
508 edit->force |= REDRAW_COMPLETELY;
509 if (res > 0)
511 edit->delete_file = 0;
512 edit->modified = 0;
515 return TRUE;
518 /* --------------------------------------------------------------------------------------------- */
520 * Load file content
522 * @param h screen the owner of editor window
523 * @param vpath vfs file path
524 * @return TRUE if file content was successfully loaded, FALSE otherwise
527 static inline gboolean
528 edit_load_file_from_filename (WDialog * h, const vfs_path_t * vpath)
530 Widget *w = WIDGET (h);
532 return edit_add_window (h, w->y + 1, w->x, w->lines - 2, w->cols, vpath, 0);
535 /* --------------------------------------------------------------------------------------------- */
537 static void
538 edit_delete_column_of_text (WEdit * edit)
540 off_t p, q, r;
541 off_t m1, m2;
542 off_t n;
543 long b, c, d;
545 eval_marks (edit, &m1, &m2);
546 n = edit_buffer_get_forward_offset (&edit->buffer, m1, 0, m2) + 1;
547 c = (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, m1), 0, m1);
548 d = (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, m2), 0, m2);
549 b = max (min (c, d), min (edit->column1, edit->column2));
550 c = max (c, max (edit->column1, edit->column2));
552 while (n--)
554 r = edit_buffer_get_current_bol (&edit->buffer);
555 p = edit_move_forward3 (edit, r, b, 0);
556 q = edit_move_forward3 (edit, r, c, 0);
557 if (p < m1)
558 p = m1;
559 if (q > m2)
560 q = m2;
561 edit_cursor_move (edit, p - edit->buffer.curs1);
562 while (q > p)
564 /* delete line between margins */
565 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
566 edit_delete (edit, TRUE);
567 q--;
569 if (n)
570 /* move to next line except on the last delete */
571 edit_cursor_move (edit,
572 edit_buffer_get_forward_offset (&edit->buffer, edit->buffer.curs1, 1,
573 0) - edit->buffer.curs1);
577 /* --------------------------------------------------------------------------------------------- */
578 /** if success return 0 */
580 static int
581 edit_block_delete (WEdit * edit)
583 off_t start_mark, end_mark;
584 off_t curs_pos;
585 long curs_line, c1, c2;
587 if (!eval_marks (edit, &start_mark, &end_mark))
588 return 0;
590 if (edit->column_highlight && edit->mark2 < 0)
591 edit_mark_cmd (edit, FALSE);
592 if ((end_mark - start_mark) > option_max_undo / 2)
594 /* Warning message with a query to continue or cancel the operation */
595 if (edit_query_dialog2
596 (_("Warning"),
598 ("Block is large, you may not be able to undo this action"),
599 _("C&ontinue"), _("&Cancel")))
601 return 1;
604 c1 = min (edit->column1, edit->column2);
605 c2 = max (edit->column1, edit->column2);
606 edit->column1 = c1;
607 edit->column2 = c2;
609 edit_push_markers (edit);
611 curs_line = edit->buffer.curs_line;
613 curs_pos = edit->curs_col + edit->over_col;
615 /* move cursor to start of selection */
616 edit_cursor_move (edit, start_mark - edit->buffer.curs1);
617 edit_scroll_screen_over_cursor (edit);
619 if (start_mark < end_mark)
621 if (edit->column_highlight)
623 off_t line_width;
625 if (edit->mark2 < 0)
626 edit_mark_cmd (edit, FALSE);
627 edit_delete_column_of_text (edit);
628 /* move cursor to the saved position */
629 edit_move_to_line (edit, curs_line);
630 /* calculate line width and cursor position before cut */
631 line_width = edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), 0,
632 edit_buffer_get_current_eol (&edit->buffer));
633 if (option_cursor_beyond_eol && curs_pos > line_width)
634 edit->over_col = curs_pos - line_width;
636 else
638 off_t count;
640 for (count = start_mark; count < end_mark; count++)
641 edit_delete (edit, TRUE);
644 edit_set_markers (edit, 0, 0, 0, 0);
645 edit->force |= REDRAW_PAGE;
646 return 0;
649 /* --------------------------------------------------------------------------------------------- */
651 * Get EOL symbol for searching.
653 * @param edit editor object
654 * @return EOL symbol
657 static inline char
658 edit_search_get_current_end_line_char (const WEdit * edit)
660 switch (edit->lb)
662 case LB_MAC:
663 return '\r';
664 default:
665 return '\n';
669 /* --------------------------------------------------------------------------------------------- */
671 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
673 * @param search search object
674 * @return result of checks.
677 static edit_search_line_t
678 edit_get_search_line_type (mc_search_t * search)
680 edit_search_line_t search_line_type = 0;
682 if (search->search_type != MC_SEARCH_T_REGEX)
683 return search_line_type;
685 if (*search->original == '^')
686 search_line_type |= AT_START_LINE;
688 if (search->original[search->original_len - 1] == '$')
689 search_line_type |= AT_END_LINE;
690 return search_line_type;
693 /* --------------------------------------------------------------------------------------------- */
695 * Calculating the start position of next line.
697 * @param buf editor buffer object
698 * @param current_pos current position
699 * @param max_pos max position
700 * @param end_string_symbol end of line symbol
701 * @return start position of next line
704 static off_t
705 edit_calculate_start_of_next_line (const edit_buffer_t * buf, off_t current_pos, off_t max_pos,
706 char end_string_symbol)
708 off_t i;
710 for (i = current_pos; i < max_pos; i++)
712 current_pos++;
713 if (edit_buffer_get_byte (buf, i) == end_string_symbol)
714 break;
717 return current_pos;
720 /* --------------------------------------------------------------------------------------------- */
722 * Calculating the end position of previous line.
724 * @param buf editor buffer object
725 * @param current_pos current position
726 * @param end_string_symbol end of line symbol
727 * @return end position of previous line
730 static off_t
731 edit_calculate_end_of_previous_line (const edit_buffer_t * buf, off_t current_pos,
732 char end_string_symbol)
734 off_t i;
736 for (i = current_pos - 1; i >= 0; i--)
737 if (edit_buffer_get_byte (buf, i) == end_string_symbol)
738 break;
740 return i;
743 /* --------------------------------------------------------------------------------------------- */
745 * Calculating the start position of previous line.
747 * @param buf editor buffer object
748 * @param current_pos current position
749 * @param end_string_symbol end of line symbol
750 * @return start position of previous line
753 static inline off_t
754 edit_calculate_start_of_previous_line (const edit_buffer_t * buf, off_t current_pos,
755 char end_string_symbol)
757 current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
758 current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
760 return (current_pos + 1);
763 /* --------------------------------------------------------------------------------------------- */
765 * Calculating the start position of current line.
767 * @param buf editor buffer object
768 * @param current_pos current position
769 * @param end_string_symbol end of line symbol
770 * @return start position of current line
773 static inline off_t
774 edit_calculate_start_of_current_line (const edit_buffer_t * buf, off_t current_pos,
775 char end_string_symbol)
777 current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
779 return (current_pos + 1);
782 /* --------------------------------------------------------------------------------------------- */
784 * Fixing (if needed) search start position if 'only in selection' option present.
786 * @param edit editor object
789 static void
790 edit_search_fix_search_start_if_selection (WEdit * edit)
792 off_t start_mark = 0;
793 off_t end_mark = 0;
795 if (!edit_search_options.only_in_selection)
796 return;
798 if (!eval_marks (edit, &start_mark, &end_mark))
799 return;
801 if (edit_search_options.backwards)
803 if (edit->search_start > end_mark || edit->search_start <= start_mark)
804 edit->search_start = end_mark;
806 else
808 if (edit->search_start < start_mark || edit->search_start >= end_mark)
809 edit->search_start = start_mark;
813 /* --------------------------------------------------------------------------------------------- */
815 static gboolean
816 editcmd_find (edit_search_status_msg_t * esm, gsize * len)
818 WEdit *edit = esm->edit;
819 off_t search_start = edit->search_start;
820 off_t search_end;
821 off_t start_mark = 0;
822 off_t end_mark = edit->buffer.size;
823 char end_string_symbol;
825 end_string_symbol = edit_search_get_current_end_line_char (edit);
827 /* prepare for search */
828 if (edit_search_options.only_in_selection)
830 if (!eval_marks (edit, &start_mark, &end_mark))
832 edit->search->error = MC_SEARCH_E_NOTFOUND;
833 edit->search->error_str = g_strdup (_(STR_E_NOTFOUND));
834 return FALSE;
837 /* fix the start and the end of search block positions */
838 if ((edit->search_line_type & AT_START_LINE) != 0
839 && (start_mark != 0
840 || edit_buffer_get_byte (&edit->buffer, start_mark - 1) != end_string_symbol))
842 start_mark =
843 edit_calculate_start_of_next_line (&edit->buffer, start_mark, edit->buffer.size,
844 end_string_symbol);
846 if ((edit->search_line_type & AT_END_LINE) != 0
847 && (end_mark - 1 != edit->buffer.size
848 || edit_buffer_get_byte (&edit->buffer, end_mark) != end_string_symbol))
849 end_mark =
850 edit_calculate_end_of_previous_line (&edit->buffer, end_mark, end_string_symbol);
851 if (start_mark >= end_mark)
853 edit->search->error = MC_SEARCH_E_NOTFOUND;
854 edit->search->error_str = g_strdup (_(STR_E_NOTFOUND));
855 return FALSE;
858 else
860 if (edit_search_options.backwards)
861 end_mark = max (1, edit->buffer.curs1) - 1;
864 /* search */
865 if (edit_search_options.backwards)
867 /* backward search */
868 search_end = end_mark;
870 if ((edit->search_line_type & AT_START_LINE) != 0)
871 search_start =
872 edit_calculate_start_of_current_line (&edit->buffer, search_start,
873 end_string_symbol);
875 while (search_start >= start_mark)
877 if (search_end > (off_t) (search_start + edit->search->original_len)
878 && mc_search_is_fixed_search_str (edit->search))
880 search_end = search_start + edit->search->original_len;
882 if (mc_search_run (edit->search, (void *) esm, search_start, search_end, len)
883 && edit->search->normal_offset == search_start)
885 return TRUE;
889 if ((edit->search_line_type & AT_START_LINE) != 0)
890 search_start =
891 edit_calculate_start_of_previous_line (&edit->buffer, search_start,
892 end_string_symbol);
893 else
894 search_start--;
896 edit->search->error_str = g_strdup (_(STR_E_NOTFOUND));
898 else
900 /* forward search */
901 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
902 search_start =
903 edit_calculate_start_of_next_line (&edit->buffer, search_start, end_mark,
904 end_string_symbol);
905 return mc_search_run (edit->search, (void *) esm, search_start, end_mark, len);
907 return FALSE;
910 /* --------------------------------------------------------------------------------------------- */
912 static char *
913 edit_replace_cmd__conv_to_display (const char *str)
915 #ifdef HAVE_CHARSET
916 GString *tmp;
918 tmp = str_convert_to_display (str);
919 if (tmp != NULL)
921 if (tmp->len != 0)
922 return g_string_free (tmp, FALSE);
923 g_string_free (tmp, TRUE);
925 #endif
926 return g_strdup (str);
929 /* --------------------------------------------------------------------------------------------- */
931 static char *
932 edit_replace_cmd__conv_to_input (char *str)
934 #ifdef HAVE_CHARSET
935 GString *tmp;
937 tmp = str_convert_to_input (str);
938 if (tmp != NULL)
940 if (tmp->len != 0)
941 return g_string_free (tmp, FALSE);
942 g_string_free (tmp, TRUE);
944 #endif
945 return g_strdup (str);
948 /* --------------------------------------------------------------------------------------------- */
950 static void
951 edit_do_search (WEdit * edit)
953 edit_search_status_msg_t esm;
954 gsize len = 0;
956 if (edit->search == NULL)
957 edit->search_start = edit->buffer.curs1;
959 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
961 esm.first = TRUE;
962 esm.edit = edit;
963 esm.offset = edit->search_start;
965 status_msg_init (STATUS_MSG (&esm), _("Search"), 1.0, simple_status_msg_init_cb,
966 edit_search_status_update_cb, NULL);
968 if (search_create_bookmark)
970 int found = 0, books = 0;
971 long l = 0, l_last = -1;
972 long q = 0;
974 search_create_bookmark = FALSE;
975 book_mark_flush (edit, -1);
977 while (mc_search_run (edit->search, (void *) &esm, q, edit->buffer.size, &len))
979 if (found == 0)
980 edit->search_start = edit->search->normal_offset;
981 found++;
982 l += edit_buffer_count_lines (&edit->buffer, q, edit->search->normal_offset);
983 if (l != l_last)
985 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
986 books++;
988 l_last = l;
989 q = edit->search->normal_offset + 1;
992 if (found == 0)
993 edit_error_dialog (_("Search"), _(STR_E_NOTFOUND));
994 else
995 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
997 else
999 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
1000 && edit_search_options.backwards)
1001 edit->search_start--;
1003 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
1004 && !edit_search_options.backwards)
1005 edit->search_start++;
1007 if (editcmd_find (&esm, &len))
1009 edit->found_start = edit->search_start = edit->search->normal_offset;
1010 edit->found_len = len;
1011 edit->over_col = 0;
1012 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
1013 edit_scroll_screen_over_cursor (edit);
1014 if (edit_search_options.backwards)
1015 edit->search_start--;
1016 else
1017 edit->search_start++;
1019 else
1021 edit->search_start = edit->buffer.curs1;
1022 if (edit->search->error_str != NULL)
1023 edit_query_dialog (_("Search"), edit->search->error_str);
1027 status_msg_deinit (STATUS_MSG (&esm));
1029 edit->force |= REDRAW_COMPLETELY;
1030 edit_scroll_screen_over_cursor (edit);
1033 /* --------------------------------------------------------------------------------------------- */
1035 static void
1036 edit_search (WEdit * edit)
1038 if (editcmd_dialog_search_show (edit))
1040 edit->search_line_type = edit_get_search_line_type (edit->search);
1041 edit_search_fix_search_start_if_selection (edit);
1042 edit_do_search (edit);
1046 /* --------------------------------------------------------------------------------------------- */
1047 /** Return a null terminated length of text. Result must be g_free'd */
1049 static unsigned char *
1050 edit_get_block (WEdit * edit, off_t start, off_t finish, off_t * l)
1052 unsigned char *s, *r;
1054 r = s = g_malloc0 (finish - start + 1);
1055 if (edit->column_highlight)
1057 *l = 0;
1058 /* copy from buffer, excluding chars that are out of the column 'margins' */
1059 while (start < finish)
1061 int c;
1062 off_t x;
1064 x = edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, start), 0, start);
1065 c = edit_buffer_get_byte (&edit->buffer, start);
1066 if ((x >= edit->column1 && x < edit->column2)
1067 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1069 *s++ = c;
1070 (*l)++;
1072 start++;
1075 else
1077 *l = finish - start;
1078 while (start < finish)
1079 *s++ = edit_buffer_get_byte (&edit->buffer, start++);
1081 *s = '\0';
1082 return r;
1085 /* --------------------------------------------------------------------------------------------- */
1086 /** copies a block to clipboard file */
1088 static gboolean
1089 edit_save_block_to_clip_file (WEdit * edit, off_t start, off_t finish)
1091 gboolean ret;
1092 gchar *tmp;
1094 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1095 ret = edit_save_block (edit, tmp, start, finish);
1096 g_free (tmp);
1097 return ret;
1100 /* --------------------------------------------------------------------------------------------- */
1102 static void
1103 pipe_mail (const edit_buffer_t * buf, char *to, char *subject, char *cc)
1105 FILE *p = 0;
1106 char *s;
1108 to = name_quote (to, FALSE);
1109 subject = name_quote (subject, FALSE);
1110 cc = name_quote (cc, FALSE);
1111 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1112 g_free (to);
1113 g_free (subject);
1114 g_free (cc);
1116 if (s != NULL)
1118 p = popen (s, "w");
1119 g_free (s);
1122 if (p != NULL)
1124 off_t i;
1126 for (i = 0; i < buf->size; i++)
1127 fputc (edit_buffer_get_byte (buf, i), p);
1128 pclose (p);
1132 /* --------------------------------------------------------------------------------------------- */
1133 /** find first character of current word */
1135 static gboolean
1136 edit_find_word_start (const edit_buffer_t * buf, off_t * word_start, gsize * word_len)
1138 int c;
1139 off_t i;
1141 /* return if at begin of file */
1142 if (buf->curs1 <= 0)
1143 return FALSE;
1145 c = edit_buffer_get_previous_byte (buf);
1146 /* return if not at end or in word */
1147 if (is_break_char (c))
1148 return FALSE;
1150 /* search start of word to be completed */
1151 for (i = 1;; i++)
1153 int last;
1155 last = c;
1156 c = edit_buffer_get_byte (buf, buf->curs1 - i - 1);
1158 if (is_break_char (c))
1160 /* return if word starts with digit */
1161 if (isdigit (last))
1162 return FALSE;
1164 break;
1168 /* success */
1169 *word_start = buf->curs1 - i; /* start found */
1170 *word_len = (gsize) i;
1171 return TRUE;
1174 /* --------------------------------------------------------------------------------------------- */
1176 * Get current word under cursor
1178 * @param esm status message window
1179 * @param srch mc_search object
1180 * @param word_start start word position
1182 * @return newly allocated string or NULL if no any words under cursor
1185 static char *
1186 edit_collect_completions_get_current_word (edit_search_status_msg_t * esm, mc_search_t * srch,
1187 off_t word_start)
1189 WEdit *edit = esm->edit;
1190 gsize len = 0;
1191 off_t i;
1192 GString *temp;
1194 if (!mc_search_run (srch, (void *) esm, word_start, edit->buffer.size, &len))
1195 return NULL;
1197 temp = g_string_sized_new (len);
1199 for (i = 0; i < (off_t) len; i++)
1201 int chr;
1203 chr = edit_buffer_get_byte (&edit->buffer, word_start + i);
1204 if (!isspace (chr))
1205 g_string_append_c (temp, chr);
1208 return g_string_free (temp, temp->len == 0);
1211 /* --------------------------------------------------------------------------------------------- */
1212 /** collect the possible completions */
1214 static gsize
1215 edit_collect_completions (WEdit * edit, off_t word_start, gsize word_len,
1216 char *match_expr, GString ** compl, gsize * num)
1218 gsize len = 0;
1219 gsize max_len = 0;
1220 gsize i;
1221 int skip;
1222 GString *temp;
1223 mc_search_t *srch;
1224 off_t last_byte, start = -1;
1225 char *current_word;
1226 gboolean entire_file;
1227 edit_search_status_msg_t esm;
1229 #ifdef HAVE_CHARSET
1230 srch = mc_search_new (match_expr, cp_source);
1231 #else
1232 srch = mc_search_new (match_expr, NULL);
1233 #endif
1234 if (srch == NULL)
1235 return 0;
1237 entire_file =
1238 mc_config_get_bool (mc_main_config, CONFIG_APP_SECTION,
1239 "editor_wordcompletion_collect_entire_file", 0);
1241 last_byte = entire_file ? edit->buffer.size : word_start;
1243 srch->search_type = MC_SEARCH_T_REGEX;
1244 srch->is_case_sensitive = TRUE;
1245 srch->search_fn = edit_search_cmd_callback;
1246 srch->update_fn = edit_search_update_callback;
1248 esm.first = TRUE;
1249 esm.edit = edit;
1250 esm.offset = entire_file ? 0 : word_start;
1252 status_msg_init (STATUS_MSG (&esm), _("Collect completions"), 1.0, simple_status_msg_init_cb,
1253 edit_search_status_update_cb, NULL);
1255 current_word = edit_collect_completions_get_current_word (&esm, srch, word_start);
1257 temp = g_string_new ("");
1259 /* collect max MAX_WORD_COMPLETIONS completions */
1260 while (mc_search_run (srch, (void *) &esm, start + 1, last_byte, &len))
1262 g_string_set_size (temp, 0);
1263 start = srch->normal_offset;
1265 /* add matched completion if not yet added */
1266 for (i = 0; i < len; i++)
1268 skip = edit_buffer_get_byte (&edit->buffer, start + i);
1269 if (isspace (skip))
1270 continue;
1272 /* skip current word */
1273 if (start + (off_t) i == word_start)
1274 break;
1276 g_string_append_c (temp, skip);
1279 if (temp->len == 0)
1280 continue;
1282 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1283 continue;
1285 skip = 0;
1287 for (i = 0; i < *num; i++)
1289 if (strncmp
1290 ((char *) &compl[i]->str[word_len],
1291 (char *) &temp->str[word_len], max (len, compl[i]->len) - word_len) == 0)
1293 GString *this = compl[i];
1294 for (++i; i < *num; i++)
1295 compl[i - 1] = compl[i];
1296 compl[*num - 1] = this;
1297 skip = 1;
1298 break; /* skip it, already added */
1301 if (skip != 0)
1302 continue;
1304 if (*num == MAX_WORD_COMPLETIONS)
1306 g_string_free (compl[0], TRUE);
1307 for (i = 1; i < *num; i++)
1308 compl[i - 1] = compl[i];
1309 (*num)--;
1311 #ifdef HAVE_CHARSET
1313 GString *recoded;
1314 recoded = str_convert_to_display (temp->str);
1316 if (recoded && recoded->len)
1317 g_string_assign (temp, recoded->str);
1319 g_string_free (recoded, TRUE);
1321 #endif
1322 compl[(*num)++] = g_string_new_len (temp->str, temp->len);
1323 start += len;
1325 /* note the maximal length needed for the completion dialog */
1326 if (len > max_len)
1327 max_len = len;
1330 status_msg_deinit (STATUS_MSG (&esm));
1331 mc_search_free (srch);
1332 g_string_free (temp, TRUE);
1333 g_free (current_word);
1335 return max_len;
1338 /* --------------------------------------------------------------------------------------------- */
1340 static void
1341 edit_insert_column_of_text (WEdit * edit, unsigned char *data, off_t size, long width,
1342 off_t * start_pos, off_t * end_pos, long *col1, long *col2)
1344 off_t i, cursor;
1345 long col;
1347 cursor = edit->buffer.curs1;
1348 col = edit_get_col (edit);
1350 for (i = 0; i < size; i++)
1352 if (data[i] != '\n')
1353 edit_insert (edit, data[i]);
1354 else
1355 { /* fill in and move to next line */
1356 long l;
1357 off_t p;
1359 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
1361 for (l = width - (edit_get_col (edit) - col); l > 0; l -= space_width)
1362 edit_insert (edit, ' ');
1364 for (p = edit->buffer.curs1;; p++)
1366 if (p == edit->buffer.size)
1368 edit_cursor_move (edit, edit->buffer.size - edit->buffer.curs1);
1369 edit_insert_ahead (edit, '\n');
1370 p++;
1371 break;
1373 if (edit_buffer_get_byte (&edit->buffer, p) == '\n')
1375 p++;
1376 break;
1379 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->buffer.curs1);
1381 for (l = col - edit_get_col (edit); l >= space_width; l -= space_width)
1382 edit_insert (edit, ' ');
1386 *col1 = col;
1387 *col2 = col + width;
1388 *start_pos = cursor;
1389 *end_pos = edit->buffer.curs1;
1390 edit_cursor_move (edit, cursor - edit->buffer.curs1);
1393 /* --------------------------------------------------------------------------------------------- */
1395 static int
1396 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1398 const macros_t *m1 = (const macros_t *) macro1;
1399 const macros_t *m2 = (const macros_t *) macro2;
1401 return m1->hotkey - m2->hotkey;
1404 /* --------------------------------------------------------------------------------------------- */
1406 static void
1407 edit_macro_sort_by_hotkey (void)
1409 if (macros_list != NULL && macros_list->len != 0)
1410 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1413 /* --------------------------------------------------------------------------------------------- */
1415 static gboolean
1416 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1418 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1419 macros_t *result;
1420 macros_t search_macro;
1422 (void) edit;
1424 search_macro.hotkey = hotkey;
1425 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1426 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1428 if (result != NULL && result->macro != NULL)
1430 *indx = (result - array_start);
1431 *macros = result;
1432 return TRUE;
1434 *indx = 0;
1435 return FALSE;
1438 /* --------------------------------------------------------------------------------------------- */
1439 /** returns FALSE on error */
1441 static gboolean
1442 edit_delete_macro (WEdit * edit, int hotkey)
1444 mc_config_t *macros_config = NULL;
1445 const char *section_name = "editor";
1446 gchar *macros_fname;
1447 guint indx;
1448 char *skeyname;
1449 const macros_t *macros = NULL;
1451 /* clear array of actions for current hotkey */
1452 while (edit_get_macro (edit, hotkey, &macros, &indx))
1454 if (macros->macro != NULL)
1455 g_array_free (macros->macro, TRUE);
1456 macros = NULL;
1457 g_array_remove_index (macros_list, indx);
1458 edit_macro_sort_by_hotkey ();
1461 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1462 macros_config = mc_config_init (macros_fname, FALSE);
1463 g_free (macros_fname);
1465 if (macros_config == NULL)
1466 return FALSE;
1468 skeyname = lookup_key_by_code (hotkey);
1469 while (mc_config_del_key (macros_config, section_name, skeyname))
1471 g_free (skeyname);
1472 mc_config_save_file (macros_config, NULL);
1473 mc_config_deinit (macros_config);
1474 return TRUE;
1477 /* --------------------------------------------------------------------------------------------- */
1479 * Callback for the iteration of objects in the 'editors' array.
1480 * Toggle syntax highlighting in editor object.
1482 * @param data probably WEdit object
1483 * @param user_data unused
1486 static void
1487 edit_syntax_onoff_cb (void *data, void *user_data)
1489 (void) user_data;
1491 if (edit_widget_is_editor (CONST_WIDGET (data)))
1493 WEdit *edit = (WEdit *) data;
1495 if (option_syntax_highlighting)
1496 edit_load_syntax (edit, NULL, edit->syntax_type);
1497 edit->force |= REDRAW_PAGE;
1501 /* --------------------------------------------------------------------------------------------- */
1503 * Callback for the iteration of objects in the 'editors' array.
1504 * Redraw editor object.
1506 * @param data probably WEdit object
1507 * @param user_data unused
1510 static void
1511 edit_redraw_page_cb (void *data, void *user_data)
1513 (void) user_data;
1515 if (edit_widget_is_editor (CONST_WIDGET (data)))
1516 ((WEdit *) data)->force |= REDRAW_PAGE;
1519 /* --------------------------------------------------------------------------------------------- */
1521 * Insert autocompleted word into editor.
1523 * @param edit editor object
1524 * @param completion word for completion
1525 * @param word_len offset from beginning for insert
1528 static void
1529 edit_complete_word_insert_recoded_completion (WEdit * edit, char *completion, gsize word_len)
1531 #ifdef HAVE_CHARSET
1532 GString *temp;
1534 temp = str_convert_to_input (completion);
1536 for (completion = temp->str + word_len; *completion != '\0'; completion++)
1537 edit_insert (edit, *completion);
1538 g_string_free (temp, TRUE);
1539 #else
1540 for (completion += word_len; *completion != '\0'; completion++)
1541 edit_insert (edit, *completion);
1542 #endif
1545 /* --------------------------------------------------------------------------------------------- */
1546 /*** public functions ****************************************************************************/
1547 /* --------------------------------------------------------------------------------------------- */
1549 void
1550 edit_refresh_cmd (void)
1552 clr_scr ();
1553 repaint_screen ();
1554 tty_keypad (TRUE);
1557 /* --------------------------------------------------------------------------------------------- */
1559 * Toggle syntax highlighting in all editor windows.
1561 * @param h root widget for all windows
1564 void
1565 edit_syntax_onoff_cmd (WDialog * h)
1567 option_syntax_highlighting = !option_syntax_highlighting;
1568 g_list_foreach (h->widgets, edit_syntax_onoff_cb, NULL);
1569 dlg_redraw (h);
1572 /* --------------------------------------------------------------------------------------------- */
1574 * Toggle tabs showing in all editor windows.
1576 * @param h root widget for all windows
1579 void
1580 edit_show_tabs_tws_cmd (WDialog * h)
1582 enable_show_tabs_tws = !enable_show_tabs_tws;
1583 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1584 dlg_redraw (h);
1587 /* --------------------------------------------------------------------------------------------- */
1589 * Toggle right margin showing in all editor windows.
1591 * @param h root widget for all windows
1594 void
1595 edit_show_margin_cmd (WDialog * h)
1597 show_right_margin = !show_right_margin;
1598 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1599 dlg_redraw (h);
1602 /* --------------------------------------------------------------------------------------------- */
1604 * Toggle line numbers showing in all editor windows.
1606 * @param h root widget for all windows
1609 void
1610 edit_show_numbers_cmd (WDialog * h)
1612 option_line_state = !option_line_state;
1613 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
1614 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1615 dlg_redraw (h);
1618 /* --------------------------------------------------------------------------------------------- */
1620 void
1621 edit_save_mode_cmd (void)
1623 char *str_result = NULL;
1625 const char *str[] = {
1626 N_("&Quick save"),
1627 N_("&Safe save"),
1628 N_("&Do backups with following extension:")
1631 #ifdef HAVE_ASSERT_H
1632 assert (option_backup_ext != NULL);
1633 #endif
1635 #ifdef ENABLE_NLS
1636 size_t i;
1638 for (i = 0; i < 3; i++)
1639 str[i] = _(str[i]);
1640 #endif
1643 quick_widget_t quick_widgets[] = {
1644 /* *INDENT-OFF* */
1645 QUICK_RADIO (3, str, &option_save_mode, &edit_save_mode_radio_id),
1646 QUICK_INPUT (option_backup_ext, "edit-backup-ext", &str_result,
1647 &edit_save_mode_input_id, FALSE, FALSE, INPUT_COMPLETE_NONE),
1648 QUICK_SEPARATOR (TRUE),
1649 QUICK_CHECKBOX (N_("Check &POSIX new line"), &option_check_nl_at_eof, NULL),
1650 QUICK_BUTTONS_OK_CANCEL,
1651 QUICK_END
1652 /* *INDENT-ON* */
1655 quick_dialog_t qdlg = {
1656 -1, -1, 38,
1657 N_("Edit Save Mode"), "[Edit Save Mode]",
1658 quick_widgets, edit_save_mode_callback, NULL
1661 if (quick_dialog (&qdlg) != B_CANCEL)
1663 g_free (option_backup_ext);
1664 option_backup_ext = str_result;
1669 /* --------------------------------------------------------------------------------------------- */
1671 void
1672 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1674 vfs_path_free (edit->filename_vpath);
1675 edit->filename_vpath = vfs_path_clone (name_vpath);
1677 if (edit->dir_vpath == NULL)
1678 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1681 /* --------------------------------------------------------------------------------------------- */
1682 /* Here we want to warn the users of overwriting an existing file,
1683 but only if they have made a change to the filename */
1684 /* returns TRUE on success */
1685 gboolean
1686 edit_save_as_cmd (WEdit * edit)
1688 /* This heads the 'Save As' dialog box */
1689 vfs_path_t *exp_vpath;
1690 int save_lock = 0;
1691 int different_filename = 0;
1693 if (!edit_check_newline (&edit->buffer))
1694 return FALSE;
1696 exp_vpath = edit_get_save_file_as (edit);
1697 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1699 if (exp_vpath != NULL)
1701 if (vfs_path_len (exp_vpath) == 0)
1702 goto ret;
1703 else
1705 int rv;
1707 if (!vfs_path_equal (edit->filename_vpath, exp_vpath))
1709 int file;
1710 struct stat sb;
1712 if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1714 edit_error_dialog (_("Save as"),
1715 get_sys_error (_
1716 ("Cannot save: destination is not a regular file")));
1717 goto ret;
1720 different_filename = 1;
1721 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1723 if (file != -1)
1725 /* the file exists */
1726 mc_close (file);
1727 /* Overwrite the current file or cancel the operation */
1728 if (edit_query_dialog2
1729 (_("Warning"),
1730 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1731 goto ret;
1733 else
1735 edit->stat1.st_mode |= S_IWUSR;
1737 save_lock = lock_file (exp_vpath);
1739 else
1741 /* filenames equal, check if already locked */
1742 if (!edit->locked && !edit->delete_file)
1743 save_lock = lock_file (exp_vpath);
1746 if (different_filename)
1749 * Allow user to write into saved (under another name) file
1750 * even if original file had r/o user permissions.
1752 edit->stat1.st_mode |= S_IWRITE;
1755 rv = edit_save_file (edit, exp_vpath);
1756 switch (rv)
1758 case 1:
1759 /* Successful, so unlock both files */
1760 if (different_filename)
1762 if (save_lock)
1763 unlock_file (exp_vpath);
1764 if (edit->locked)
1765 edit->locked = unlock_file (edit->filename_vpath);
1767 else
1769 if (edit->locked || save_lock)
1770 edit->locked = unlock_file (edit->filename_vpath);
1773 edit_set_filename (edit, exp_vpath);
1774 if (edit->lb != LB_ASIS)
1775 edit_reload (edit, exp_vpath);
1776 edit->modified = 0;
1777 edit->delete_file = 0;
1778 if (different_filename)
1779 edit_load_syntax (edit, NULL, edit->syntax_type);
1780 vfs_path_free (exp_vpath);
1781 edit->force |= REDRAW_COMPLETELY;
1782 return TRUE;
1783 default:
1784 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1785 /* fallthrough */
1786 case -1:
1787 /* Failed, so maintain modify (not save) lock */
1788 if (save_lock)
1789 unlock_file (exp_vpath);
1790 break;
1795 ret:
1796 vfs_path_free (exp_vpath);
1797 edit->force |= REDRAW_COMPLETELY;
1798 return FALSE;
1801 /* {{{ Macro stuff starts here */
1802 /* --------------------------------------------------------------------------------------------- */
1804 void
1805 edit_delete_macro_cmd (WEdit * edit)
1807 int hotkey;
1809 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE);
1811 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1812 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1815 /* --------------------------------------------------------------------------------------------- */
1817 /** returns FALSE on error */
1818 gboolean
1819 edit_execute_macro (WEdit * edit, int hotkey)
1821 gboolean res = FALSE;
1823 if (hotkey != 0)
1825 const macros_t *macros;
1826 guint indx;
1828 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1829 macros->macro != NULL && macros->macro->len != 0)
1831 guint i;
1833 edit->force |= REDRAW_PAGE;
1835 for (i = 0; i < macros->macro->len; i++)
1837 const macro_action_t *m_act;
1839 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1840 edit_execute_cmd (edit, m_act->action, m_act->ch);
1841 res = TRUE;
1846 return res;
1849 /* --------------------------------------------------------------------------------------------- */
1851 /** returns FALSE on error */
1852 gboolean
1853 edit_store_macro_cmd (WEdit * edit)
1855 int i;
1856 int hotkey;
1857 GString *marcros_string;
1858 mc_config_t *macros_config = NULL;
1859 const char *section_name = "editor";
1860 gchar *macros_fname;
1861 GArray *macros; /* current macro */
1862 int tmp_act;
1863 gboolean have_macro = FALSE;
1864 char *skeyname = NULL;
1866 hotkey =
1867 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE);
1868 if (hotkey == ESC_CHAR)
1869 return FALSE;
1871 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1873 /* return FALSE if try assign macro into restricted hotkeys */
1874 if (tmp_act == CK_MacroStartRecord
1875 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1876 return FALSE;
1878 edit_delete_macro (edit, hotkey);
1880 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1881 macros_config = mc_config_init (macros_fname, FALSE);
1882 g_free (macros_fname);
1884 if (macros_config == NULL)
1885 return FALSE;
1887 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1889 marcros_string = g_string_sized_new (250);
1890 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1892 skeyname = lookup_key_by_code (hotkey);
1894 for (i = 0; i < macro_index; i++)
1896 macro_action_t m_act;
1897 const char *action_name;
1899 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1901 if (action_name == NULL)
1902 break;
1904 m_act.action = record_macro_buf[i].action;
1905 m_act.ch = record_macro_buf[i].ch;
1906 g_array_append_val (macros, m_act);
1907 have_macro = TRUE;
1908 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1909 (int) record_macro_buf[i].ch);
1911 if (have_macro)
1913 macros_t macro;
1914 macro.hotkey = hotkey;
1915 macro.macro = macros;
1916 g_array_append_val (macros_list, macro);
1917 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1919 else
1920 mc_config_del_key (macros_config, section_name, skeyname);
1922 g_free (skeyname);
1923 edit_macro_sort_by_hotkey ();
1925 g_string_free (marcros_string, TRUE);
1926 mc_config_save_file (macros_config, NULL);
1927 mc_config_deinit (macros_config);
1928 return TRUE;
1931 /* --------------------------------------------------------------------------------------------- */
1933 gboolean
1934 edit_repeat_macro_cmd (WEdit * edit)
1936 int i, j;
1937 char *f;
1938 long count_repeat;
1939 char *error = NULL;
1941 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL,
1942 INPUT_COMPLETE_NONE);
1943 if (f == NULL || *f == '\0')
1945 g_free (f);
1946 return FALSE;
1949 count_repeat = strtol (f, &error, 0);
1951 if (*error != '\0')
1953 g_free (f);
1954 return FALSE;
1957 g_free (f);
1959 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1960 edit->force |= REDRAW_PAGE;
1962 for (j = 0; j < count_repeat; j++)
1963 for (i = 0; i < macro_index; i++)
1964 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1965 edit_update_screen (edit);
1966 return TRUE;
1969 /* --------------------------------------------------------------------------------------------- */
1970 /** return FALSE on error */
1972 gboolean
1973 edit_load_macro_cmd (WEdit * edit)
1975 mc_config_t *macros_config = NULL;
1976 gchar **profile_keys, **keys;
1977 gchar **values, **curr_values;
1978 const char *section_name = "editor";
1979 gchar *macros_fname;
1981 (void) edit;
1983 if (macros_list == NULL || macros_list->len != 0)
1984 return FALSE;
1986 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1987 macros_config = mc_config_init (macros_fname, TRUE);
1988 g_free (macros_fname);
1990 if (macros_config == NULL)
1991 return FALSE;
1993 keys = mc_config_get_keys (macros_config, section_name, NULL);
1995 for (profile_keys = keys; *profile_keys != NULL; profile_keys++)
1997 int hotkey;
1998 gboolean have_macro = FALSE;
1999 GArray *macros;
2000 macros_t macro;
2002 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
2003 values = mc_config_get_string_list (macros_config, section_name, *profile_keys, NULL);
2004 hotkey = lookup_key (*profile_keys, NULL);
2006 for (curr_values = values; *curr_values != NULL && *curr_values[0] != '\0'; curr_values++)
2008 char **macro_pair = NULL;
2010 macro_pair = g_strsplit (*curr_values, ":", 2);
2011 if (macro_pair != NULL)
2013 macro_action_t m_act;
2014 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
2015 m_act.action = 0;
2016 else
2018 m_act.action = keybind_lookup_action (macro_pair[0]);
2019 MC_PTR_FREE (macro_pair[0]);
2021 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
2022 m_act.ch = -1;
2023 else
2025 m_act.ch = strtol (macro_pair[1], NULL, 0);
2026 MC_PTR_FREE (macro_pair[1]);
2028 if (m_act.action != 0)
2030 /* a shell command */
2031 if ((m_act.action / CK_PipeBlock (0)) == 1)
2033 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
2034 m_act.ch = -1;
2036 g_array_append_val (macros, m_act);
2037 have_macro = TRUE;
2039 g_strfreev (macro_pair);
2040 macro_pair = NULL;
2043 if (have_macro)
2045 macro.hotkey = hotkey;
2046 macro.macro = macros;
2047 g_array_append_val (macros_list, macro);
2049 g_strfreev (values);
2051 g_strfreev (keys);
2052 mc_config_deinit (macros_config);
2053 edit_macro_sort_by_hotkey ();
2054 return TRUE;
2057 /* }}} Macro stuff end here */
2059 /* --------------------------------------------------------------------------------------------- */
2060 /** returns TRUE on success */
2062 gboolean
2063 edit_save_confirm_cmd (WEdit * edit)
2065 if (edit->filename_vpath == NULL)
2066 return edit_save_as_cmd (edit);
2068 if (!edit_check_newline (&edit->buffer))
2069 return FALSE;
2071 if (edit_confirm_save)
2073 char *f;
2074 gboolean ok;
2076 f = g_strdup_printf (_("Confirm save file: \"%s\""),
2077 vfs_path_as_str (edit->filename_vpath));
2078 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2079 g_free (f);
2080 if (!ok)
2081 return FALSE;
2083 return edit_save_cmd (edit);
2086 /* --------------------------------------------------------------------------------------------- */
2088 * Ask file to edit and load it.
2090 * @return TRUE on success or cancel of ask.
2093 gboolean
2094 edit_load_cmd (WDialog * h)
2096 char *exp;
2097 gboolean ret = TRUE; /* possible cancel */
2099 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2100 MC_HISTORY_EDIT_LOAD, INPUT_LAST_TEXT,
2101 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_CD);
2103 if (exp != NULL && *exp != '\0')
2105 vfs_path_t *exp_vpath;
2107 exp_vpath = vfs_path_from_str (exp);
2108 ret = edit_load_file_from_filename (h, exp_vpath);
2109 vfs_path_free (exp_vpath);
2112 g_free (exp);
2114 return ret;
2117 /* --------------------------------------------------------------------------------------------- */
2119 * Load syntax file to edit.
2121 * @return TRUE on success
2124 gboolean
2125 edit_load_syntax_file (WDialog * h)
2127 vfs_path_t *extdir_vpath;
2128 int dir = 0;
2129 gboolean ret = FALSE;
2131 if (geteuid () == 0)
2132 dir = query_dialog (_("Syntax file edit"),
2133 _("Which syntax file you want to edit?"), D_NORMAL, 2,
2134 _("&User"), _("&System wide"));
2136 extdir_vpath =
2137 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
2138 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
2140 vfs_path_free (extdir_vpath);
2141 extdir_vpath =
2142 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
2145 if (dir == 0)
2147 vfs_path_t *user_syntax_file_vpath;
2149 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
2150 check_for_default (extdir_vpath, user_syntax_file_vpath);
2151 ret = edit_load_file_from_filename (h, user_syntax_file_vpath);
2152 vfs_path_free (user_syntax_file_vpath);
2154 else if (dir == 1)
2155 ret = edit_load_file_from_filename (h, extdir_vpath);
2157 vfs_path_free (extdir_vpath);
2159 return ret;
2162 /* --------------------------------------------------------------------------------------------- */
2164 * Load menu file to edit.
2166 * @return TRUE on success
2169 gboolean
2170 edit_load_menu_file (WDialog * h)
2172 vfs_path_t *buffer_vpath;
2173 vfs_path_t *menufile_vpath;
2174 int dir;
2175 gboolean ret;
2177 query_set_sel (1);
2178 dir = query_dialog (_("Menu edit"),
2179 _("Which menu file do you want to edit?"), D_NORMAL,
2180 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2182 menufile_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2183 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
2185 vfs_path_free (menufile_vpath);
2186 menufile_vpath = vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2189 switch (dir)
2191 case 0:
2192 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
2193 check_for_default (menufile_vpath, buffer_vpath);
2194 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
2195 break;
2197 case 1:
2198 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
2199 check_for_default (menufile_vpath, buffer_vpath);
2200 break;
2202 case 2:
2203 buffer_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2204 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
2206 vfs_path_free (buffer_vpath);
2207 buffer_vpath =
2208 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2210 break;
2212 default:
2213 vfs_path_free (menufile_vpath);
2214 return FALSE;
2217 ret = edit_load_file_from_filename (h, buffer_vpath);
2219 vfs_path_free (buffer_vpath);
2220 vfs_path_free (menufile_vpath);
2222 return ret;
2225 /* --------------------------------------------------------------------------------------------- */
2227 * Close window with opened file.
2229 * @return TRUE if file was closed.
2232 gboolean
2233 edit_close_cmd (WEdit * edit)
2235 gboolean ret;
2237 ret = (edit != NULL) && edit_ok_to_exit (edit);
2239 if (ret)
2241 WDialog *h = WIDGET (edit)->owner;
2243 if (edit->locked != 0)
2244 unlock_file (edit->filename_vpath);
2246 del_widget (edit);
2248 if (edit_widget_is_editor (CONST_WIDGET (h->current->data)))
2249 edit = (WEdit *) h->current->data;
2250 else
2252 edit = find_editor (h);
2253 if (edit != NULL)
2254 dlg_set_top_widget (edit);
2258 if (edit != NULL)
2259 edit->force |= REDRAW_COMPLETELY;
2261 return ret;
2264 /* --------------------------------------------------------------------------------------------- */
2266 if mark2 is -1 then marking is from mark1 to the cursor.
2267 Otherwise its between the markers. This handles this.
2268 Returns FALSE if no text is marked.
2271 gboolean
2272 eval_marks (WEdit * edit, off_t * start_mark, off_t * end_mark)
2274 long end_mark_curs;
2276 if (edit->mark1 == edit->mark2)
2278 *start_mark = *end_mark = 0;
2279 edit->column2 = edit->column1 = 0;
2280 return FALSE;
2283 if (edit->end_mark_curs < 0)
2284 end_mark_curs = edit->buffer.curs1;
2285 else
2286 end_mark_curs = edit->end_mark_curs;
2288 if (edit->mark2 >= 0)
2290 *start_mark = min (edit->mark1, edit->mark2);
2291 *end_mark = max (edit->mark1, edit->mark2);
2293 else
2295 *start_mark = min (edit->mark1, end_mark_curs);
2296 *end_mark = max (edit->mark1, end_mark_curs);
2297 edit->column2 = edit->curs_col + edit->over_col;
2300 if (edit->column_highlight
2301 && ((edit->mark1 > end_mark_curs && edit->column1 < edit->column2)
2302 || (edit->mark1 < end_mark_curs && edit->column1 > edit->column2)))
2304 off_t start_bol, start_eol;
2305 off_t end_bol, end_eol;
2306 long col1, col2;
2307 off_t diff1, diff2;
2309 start_bol = edit_buffer_get_bol (&edit->buffer, *start_mark);
2310 start_eol = edit_buffer_get_eol (&edit->buffer, start_bol - 1) + 1;
2311 end_bol = edit_buffer_get_bol (&edit->buffer, *end_mark);
2312 end_eol = edit_buffer_get_eol (&edit->buffer, *end_mark);
2313 col1 = min (edit->column1, edit->column2);
2314 col2 = max (edit->column1, edit->column2);
2316 diff1 = edit_move_forward3 (edit, start_bol, col2, 0) -
2317 edit_move_forward3 (edit, start_bol, col1, 0);
2318 diff2 = edit_move_forward3 (edit, end_bol, col2, 0) -
2319 edit_move_forward3 (edit, end_bol, col1, 0);
2321 *start_mark -= diff1;
2322 *end_mark += diff2;
2323 *start_mark = max (*start_mark, start_eol);
2324 *end_mark = min (*end_mark, end_eol);
2326 return TRUE;
2329 /* --------------------------------------------------------------------------------------------- */
2331 void
2332 edit_block_copy_cmd (WEdit * edit)
2334 off_t start_mark, end_mark, current = edit->buffer.curs1;
2335 off_t mark1 = 0, mark2 = 0;
2336 long c1 = 0, c2 = 0;
2337 off_t size;
2338 unsigned char *copy_buf;
2340 edit_update_curs_col (edit);
2341 if (!eval_marks (edit, &start_mark, &end_mark))
2342 return;
2344 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2346 /* all that gets pushed are deletes hence little space is used on the stack */
2348 edit_push_markers (edit);
2350 if (edit->column_highlight)
2352 long col_delta;
2354 col_delta = labs (edit->column2 - edit->column1);
2355 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2357 else
2359 int size_orig = size;
2361 while (size-- != 0)
2362 edit_insert_ahead (edit, copy_buf[size]);
2364 /* Place cursor at the end of text selection */
2365 if (option_cursor_after_inserted_block)
2366 edit_cursor_move (edit, size_orig);
2369 g_free (copy_buf);
2370 edit_scroll_screen_over_cursor (edit);
2372 if (edit->column_highlight)
2373 edit_set_markers (edit, edit->buffer.curs1, mark2, c1, c2);
2374 else if (start_mark < current && end_mark > current)
2375 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2377 edit->force |= REDRAW_PAGE;
2381 /* --------------------------------------------------------------------------------------------- */
2383 void
2384 edit_block_move_cmd (WEdit * edit)
2386 off_t current;
2387 unsigned char *copy_buf = NULL;
2388 off_t start_mark, end_mark;
2390 if (!eval_marks (edit, &start_mark, &end_mark))
2391 return;
2393 if (!edit->column_highlight && edit->buffer.curs1 > start_mark && edit->buffer.curs1 < end_mark)
2394 return;
2396 if (edit->mark2 < 0)
2397 edit_mark_cmd (edit, FALSE);
2398 edit_push_markers (edit);
2400 if (edit->column_highlight)
2402 off_t mark1, mark2;
2403 off_t size;
2404 long c1, c2, b_width;
2405 long x, x2;
2407 c1 = min (edit->column1, edit->column2);
2408 c2 = max (edit->column1, edit->column2);
2409 b_width = c2 - c1;
2411 edit_update_curs_col (edit);
2413 x = edit->curs_col;
2414 x2 = x + edit->over_col;
2416 /* do nothing when cursor inside first line of selected area */
2417 if ((edit_buffer_get_eol (&edit->buffer, edit->buffer.curs1) ==
2418 edit_buffer_get_eol (&edit->buffer, start_mark)) && x2 > c1 && x2 <= c2)
2419 return;
2421 if (edit->buffer.curs1 > start_mark
2422 && edit->buffer.curs1 < edit_buffer_get_eol (&edit->buffer, end_mark))
2424 if (x > c2)
2425 x -= b_width;
2426 else if (x > c1 && x <= c2)
2427 x = c1;
2429 /* save current selection into buffer */
2430 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2432 /* remove current selection */
2433 edit_block_delete_cmd (edit);
2435 edit->over_col = max (0, edit->over_col - b_width);
2436 /* calculate the cursor pos after delete block */
2437 current = edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), x, 0);
2438 edit_cursor_move (edit, current - edit->buffer.curs1);
2439 edit_scroll_screen_over_cursor (edit);
2441 /* add TWS if need before block insertion */
2442 if (option_cursor_beyond_eol && edit->over_col > 0)
2443 edit_insert_over (edit);
2445 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2446 edit_set_markers (edit, mark1, mark2, c1, c2);
2448 else
2450 off_t count, count_orig;
2452 current = edit->buffer.curs1;
2453 copy_buf = g_malloc0 (end_mark - start_mark);
2454 edit_cursor_move (edit, start_mark - edit->buffer.curs1);
2455 edit_scroll_screen_over_cursor (edit);
2457 for (count = start_mark; count < end_mark; count++)
2458 copy_buf[end_mark - count - 1] = edit_delete (edit, TRUE);
2460 edit_scroll_screen_over_cursor (edit);
2461 edit_cursor_move (edit,
2462 current - edit->buffer.curs1 -
2463 (((current - edit->buffer.curs1) > 0) ? end_mark - start_mark : 0));
2464 edit_scroll_screen_over_cursor (edit);
2465 count_orig = count;
2466 while (count-- > start_mark)
2467 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2469 edit_set_markers (edit, edit->buffer.curs1, edit->buffer.curs1 + end_mark - start_mark, 0,
2472 /* Place cursor at the end of text selection */
2473 if (option_cursor_after_inserted_block)
2474 edit_cursor_move (edit, count_orig - start_mark);
2477 edit_scroll_screen_over_cursor (edit);
2478 g_free (copy_buf);
2479 edit->force |= REDRAW_PAGE;
2482 /* --------------------------------------------------------------------------------------------- */
2483 /** returns 1 if canceelled by user */
2486 edit_block_delete_cmd (WEdit * edit)
2488 off_t start_mark, end_mark;
2490 if (eval_marks (edit, &start_mark, &end_mark))
2491 return edit_block_delete (edit);
2493 edit_delete_line (edit);
2494 return 0;
2497 /* --------------------------------------------------------------------------------------------- */
2498 /** call with edit = 0 before shutdown to close memory leaks */
2500 void
2501 edit_replace_cmd (WEdit * edit, int again)
2503 /* 1 = search string, 2 = replace with */
2504 static char *saved1 = NULL; /* saved default[123] */
2505 static char *saved2 = NULL;
2506 char *input1 = NULL; /* user input from the dialog */
2507 char *input2 = NULL;
2508 GString *input2_str = NULL;
2509 char *disp1 = NULL;
2510 char *disp2 = NULL;
2511 long times_replaced = 0;
2512 gboolean once_found = FALSE;
2513 edit_search_status_msg_t esm;
2515 if (edit == NULL)
2517 MC_PTR_FREE (saved1);
2518 MC_PTR_FREE (saved2);
2519 return;
2522 edit->force |= REDRAW_COMPLETELY;
2524 if (again && !saved1 && !saved2)
2525 again = 0;
2527 if (again)
2529 input1 = g_strdup (saved1 ? saved1 : "");
2530 input2 = g_strdup (saved2 ? saved2 : "");
2532 else
2534 char *tmp_inp1, *tmp_inp2;
2536 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : "");
2537 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : "");
2539 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2541 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2543 g_free (disp1);
2544 g_free (disp2);
2546 if (input1 == NULL || *input1 == '\0')
2548 edit->force = REDRAW_COMPLETELY;
2549 goto cleanup;
2552 tmp_inp1 = input1;
2553 tmp_inp2 = input2;
2554 input1 = edit_replace_cmd__conv_to_input (input1);
2555 input2 = edit_replace_cmd__conv_to_input (input2);
2556 g_free (tmp_inp1);
2557 g_free (tmp_inp2);
2559 g_free (saved1), saved1 = g_strdup (input1);
2560 g_free (saved2), saved2 = g_strdup (input2);
2562 mc_search_free (edit->search);
2563 edit->search = NULL;
2566 input2_str = g_string_new (input2);
2568 if (edit->search == NULL)
2570 #ifdef HAVE_CHARSET
2571 edit->search = mc_search_new (input1, cp_source);
2572 #else
2573 edit->search = mc_search_new (input1, NULL);
2574 #endif
2575 if (edit->search == NULL)
2577 edit->search_start = edit->buffer.curs1;
2578 goto cleanup;
2580 edit->search->search_type = edit_search_options.type;
2581 #ifdef HAVE_CHARSET
2582 edit->search->is_all_charsets = edit_search_options.all_codepages;
2583 #endif
2584 edit->search->is_case_sensitive = edit_search_options.case_sens;
2585 edit->search->whole_words = edit_search_options.whole_words;
2586 edit->search->search_fn = edit_search_cmd_callback;
2587 edit->search->update_fn = edit_search_update_callback;
2588 edit->search_line_type = edit_get_search_line_type (edit->search);
2589 edit_search_fix_search_start_if_selection (edit);
2592 if (edit->found_len && edit->search_start == edit->found_start + 1
2593 && edit_search_options.backwards)
2594 edit->search_start--;
2596 if (edit->found_len && edit->search_start == edit->found_start - 1
2597 && !edit_search_options.backwards)
2598 edit->search_start++;
2600 esm.first = TRUE;
2601 esm.edit = edit;
2602 esm.offset = edit->search_start;
2604 status_msg_init (STATUS_MSG (&esm), _("Search"), 1.0, simple_status_msg_init_cb,
2605 edit_search_status_update_cb, NULL);
2609 gsize len = 0;
2611 if (!editcmd_find (&esm, &len))
2613 if (!(edit->search->error == MC_SEARCH_E_OK ||
2614 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2616 edit_query_dialog (_("Search"), edit->search->error_str);
2618 break;
2620 once_found = TRUE;
2622 edit->search_start = edit->search->normal_offset;
2623 /*returns negative on not found or error in pattern */
2625 if ((edit->search_start >= 0) && (edit->search_start < edit->buffer.size))
2627 gsize i;
2628 GString *repl_str;
2630 edit->found_start = edit->search_start;
2631 i = edit->found_len = len;
2633 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
2634 edit_scroll_screen_over_cursor (edit);
2636 if (edit->replace_mode == 0)
2638 long l;
2639 int prompt;
2641 l = edit->curs_row - WIDGET (edit)->lines / 3;
2642 if (l > 0)
2643 edit_scroll_downward (edit, l);
2644 if (l < 0)
2645 edit_scroll_upward (edit, -l);
2647 edit_scroll_screen_over_cursor (edit);
2648 edit->force |= REDRAW_PAGE;
2649 edit_render_keypress (edit);
2651 /*so that undo stops at each query */
2652 edit_push_key_press (edit);
2653 /* and prompt 2/3 down */
2654 disp1 = edit_replace_cmd__conv_to_display (saved1);
2655 disp2 = edit_replace_cmd__conv_to_display (saved2);
2656 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2657 g_free (disp1);
2658 g_free (disp2);
2660 if (prompt == B_REPLACE_ALL)
2661 edit->replace_mode = 1;
2662 else if (prompt == B_SKIP_REPLACE)
2664 if (edit_search_options.backwards)
2665 edit->search_start--;
2666 else
2667 edit->search_start++;
2668 continue; /* loop */
2670 else if (prompt == B_CANCEL)
2672 edit->replace_mode = -1;
2673 break; /* loop */
2677 repl_str = mc_search_prepare_replace_str (edit->search, input2_str);
2679 if (edit->search->error != MC_SEARCH_E_OK)
2681 edit_error_dialog (_("Replace"), edit->search->error_str);
2682 g_string_free (repl_str, TRUE);
2683 break;
2686 /* delete then insert new */
2687 for (i = 0; i < len; i++)
2688 edit_delete (edit, TRUE);
2690 for (i = 0; i < repl_str->len; i++)
2691 edit_insert (edit, repl_str->str[i]);
2693 edit->found_len = repl_str->len;
2694 g_string_free (repl_str, TRUE);
2695 times_replaced++;
2697 /* so that we don't find the same string again */
2698 if (edit_search_options.backwards)
2700 edit->search_start--;
2702 else
2704 edit->search_start += edit->found_len + (len == 0 ? 1 : 0);
2706 if (edit->search_start >= edit->buffer.size)
2707 break;
2710 edit_scroll_screen_over_cursor (edit);
2712 else
2714 /* try and find from right here for next search */
2715 edit->search_start = edit->buffer.curs1;
2716 edit_update_curs_col (edit);
2718 edit->force |= REDRAW_PAGE;
2719 edit_render_keypress (edit);
2721 if (times_replaced == 0)
2722 query_dialog (_("Replace"), _(STR_E_NOTFOUND), D_NORMAL, 1, _("&OK"));
2723 break;
2726 while (edit->replace_mode >= 0);
2728 status_msg_deinit (STATUS_MSG (&esm));
2729 edit_scroll_screen_over_cursor (edit);
2730 edit->force |= REDRAW_COMPLETELY;
2731 edit_render_keypress (edit);
2733 if ((edit->replace_mode == 1) && (times_replaced != 0))
2734 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2736 cleanup:
2737 g_free (input1);
2738 g_free (input2);
2739 if (input2_str != NULL)
2740 g_string_free (input2_str, TRUE);
2743 /* --------------------------------------------------------------------------------------------- */
2745 mc_search_cbret_t
2746 edit_search_cmd_callback (const void *user_data, gsize char_offset, int *current_char)
2748 WEdit *edit = ((const edit_search_status_msg_t *) user_data)->edit;
2750 *current_char = edit_buffer_get_byte (&edit->buffer, (off_t) char_offset);
2751 return MC_SEARCH_CB_OK;
2754 /* --------------------------------------------------------------------------------------------- */
2756 mc_search_cbret_t
2757 edit_search_update_callback (const void *user_data, gsize char_offset)
2759 status_msg_t *sm = STATUS_MSG (user_data);
2761 ((edit_search_status_msg_t *) sm)->offset = (off_t) char_offset;
2763 return (sm->update (sm) == B_CANCEL ? MC_SEARCH_CB_ABORT : MC_SEARCH_CB_OK);
2766 /* --------------------------------------------------------------------------------------------- */
2768 void
2769 edit_search_cmd (WEdit * edit, gboolean again)
2772 if (edit == NULL)
2773 return;
2775 if (!again)
2776 edit_search (edit);
2777 else if (edit->last_search_string != NULL)
2778 edit_do_search (edit);
2779 else
2781 /* find last search string in history */
2782 GList *history;
2784 history = history_get (MC_HISTORY_SHARED_SEARCH);
2785 if (history != NULL && history->data != NULL)
2787 edit->last_search_string = (char *) history->data;
2788 history->data = NULL;
2789 history = g_list_first (history);
2790 g_list_free_full (history, g_free);
2792 #ifdef HAVE_CHARSET
2793 edit->search = mc_search_new (edit->last_search_string, cp_source);
2794 #else
2795 edit->search = mc_search_new (edit->last_search_string, NULL);
2796 #endif
2797 if (edit->search == NULL)
2799 /* if not... then ask for an expression */
2800 MC_PTR_FREE (edit->last_search_string);
2801 edit_search (edit);
2803 else
2805 edit->search->search_type = edit_search_options.type;
2806 #ifdef HAVE_CHARSET
2807 edit->search->is_all_charsets = edit_search_options.all_codepages;
2808 #endif
2809 edit->search->is_case_sensitive = edit_search_options.case_sens;
2810 edit->search->whole_words = edit_search_options.whole_words;
2811 edit->search->search_fn = edit_search_cmd_callback;
2812 edit->search->update_fn = edit_search_update_callback;
2813 edit->search_line_type = edit_get_search_line_type (edit->search);
2814 edit_do_search (edit);
2817 else
2819 /* if not... then ask for an expression */
2820 MC_PTR_FREE (edit->last_search_string);
2821 edit_search (edit);
2827 /* --------------------------------------------------------------------------------------------- */
2829 * Check if it's OK to close the file. If there are unsaved changes, ask user.
2831 * @return TRUE if it's OK to exit, FALSE to continue editing.
2834 gboolean
2835 edit_ok_to_exit (WEdit * edit)
2837 const char *fname = N_("[NoName]");
2838 char *msg;
2839 int act;
2841 if (!edit->modified)
2842 return TRUE;
2844 if (edit->filename_vpath != NULL)
2845 fname = vfs_path_as_str (edit->filename_vpath);
2846 #ifdef ENABLE_NLS
2847 else
2848 fname = _(fname);
2849 #endif
2851 if (!mc_global.midnight_shutdown)
2853 query_set_sel (2);
2855 msg = g_strdup_printf (_("File %s was modified.\nSave before close?"), fname);
2856 act = edit_query_dialog3 (_("Close file"), msg, _("&Yes"), _("&No"), _("&Cancel"));
2858 else
2860 msg = g_strdup_printf (_("Midnight Commander is being shut down.\nSave modified file %s?"),
2861 fname);
2862 act = edit_query_dialog2 (_("Quit"), msg, _("&Yes"), _("&No"));
2864 /* Esc is No */
2865 if (act == -1)
2866 act = 1;
2869 g_free (msg);
2871 switch (act)
2873 case 0: /* Yes */
2874 if (!mc_global.midnight_shutdown && !edit_check_newline (&edit->buffer))
2875 return FALSE;
2876 edit_push_markers (edit);
2877 edit_set_markers (edit, 0, 0, 0, 0);
2878 if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2879 return mc_global.midnight_shutdown;
2880 break;
2881 case 1: /* No */
2882 default:
2883 break;
2884 case 2: /* Cancel quit */
2885 case -1: /* Esc */
2886 return FALSE;
2889 return TRUE;
2892 /* --------------------------------------------------------------------------------------------- */
2893 /** save block, returns TRUE on success */
2895 gboolean
2896 edit_save_block (WEdit * edit, const char *filename, off_t start, off_t finish)
2898 int file;
2899 off_t len = 1;
2900 vfs_path_t *vpath;
2902 vpath = vfs_path_from_str (filename);
2903 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2904 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2905 vfs_path_free (vpath);
2906 if (file == -1)
2907 return FALSE;
2909 if (edit->column_highlight)
2911 int r;
2913 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2914 if (r > 0)
2916 unsigned char *block, *p;
2918 p = block = edit_get_block (edit, start, finish, &len);
2919 while (len)
2921 r = mc_write (file, p, len);
2922 if (r < 0)
2923 break;
2924 p += r;
2925 len -= r;
2927 g_free (block);
2930 else
2932 unsigned char *buf;
2933 off_t i = start;
2934 off_t end;
2936 len = finish - start;
2937 buf = g_malloc0 (TEMP_BUF_LEN);
2938 while (start != finish)
2940 end = min (finish, start + TEMP_BUF_LEN);
2941 for (; i < end; i++)
2942 buf[i - start] = edit_buffer_get_byte (&edit->buffer, i);
2943 len -= mc_write (file, (char *) buf, end - start);
2944 start = end;
2946 g_free (buf);
2948 mc_close (file);
2950 return (len == 0);
2953 /* --------------------------------------------------------------------------------------------- */
2955 void
2956 edit_paste_from_history (WEdit * edit)
2958 (void) edit;
2959 edit_error_dialog (_("Error"), _("This function is not implemented"));
2962 /* --------------------------------------------------------------------------------------------- */
2964 gboolean
2965 edit_copy_to_X_buf_cmd (WEdit * edit)
2967 off_t start_mark, end_mark;
2969 if (!eval_marks (edit, &start_mark, &end_mark))
2970 return TRUE;
2972 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2974 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2975 return FALSE;
2977 /* try use external clipboard utility */
2978 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2980 if (option_drop_selection_on_copy)
2981 edit_mark_cmd (edit, TRUE);
2983 return TRUE;
2986 /* --------------------------------------------------------------------------------------------- */
2988 gboolean
2989 edit_cut_to_X_buf_cmd (WEdit * edit)
2991 off_t start_mark, end_mark;
2993 if (!eval_marks (edit, &start_mark, &end_mark))
2994 return TRUE;
2996 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2998 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2999 return FALSE;
3001 /* try use external clipboard utility */
3002 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
3004 edit_block_delete_cmd (edit);
3005 edit_mark_cmd (edit, TRUE);
3007 return TRUE;
3010 /* --------------------------------------------------------------------------------------------- */
3012 gboolean
3013 edit_paste_from_X_buf_cmd (WEdit * edit)
3015 vfs_path_t *tmp;
3016 gboolean ret;
3018 /* try use external clipboard utility */
3019 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
3020 tmp = mc_config_get_full_vpath (EDIT_CLIP_FILE);
3021 ret = (edit_insert_file (edit, tmp) >= 0);
3022 vfs_path_free (tmp);
3024 return ret;
3027 /* --------------------------------------------------------------------------------------------- */
3029 * Ask user for the line and go to that line.
3030 * Negative numbers mean line from the end (i.e. -1 is the last line).
3033 void
3034 edit_goto_cmd (WEdit * edit)
3036 static gboolean first_run = TRUE;
3038 char *f;
3039 long l;
3040 char *error;
3042 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE,
3043 first_run ? NULL : INPUT_LAST_TEXT, INPUT_COMPLETE_NONE);
3044 if (f == NULL || *f == '\0')
3046 g_free (f);
3047 return;
3050 l = strtol (f, &error, 0);
3051 if (*error != '\0')
3053 g_free (f);
3054 return;
3057 if (l < 0)
3058 l = edit->buffer.lines + l + 2;
3059 edit_move_display (edit, l - WIDGET (edit)->lines / 2 - 1);
3060 edit_move_to_line (edit, l - 1);
3061 edit->force |= REDRAW_COMPLETELY;
3062 g_free (f);
3064 first_run = FALSE;
3068 /* --------------------------------------------------------------------------------------------- */
3069 /** Return TRUE on success */
3071 gboolean
3072 edit_save_block_cmd (WEdit * edit)
3074 off_t start_mark, end_mark;
3075 char *exp, *tmp;
3076 gboolean ret = FALSE;
3078 if (!eval_marks (edit, &start_mark, &end_mark))
3079 return TRUE;
3081 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3082 exp =
3083 input_expand_dialog (_("Save block"), _("Enter file name:"),
3084 MC_HISTORY_EDIT_SAVE_BLOCK, tmp, INPUT_COMPLETE_FILENAMES);
3085 g_free (tmp);
3086 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3088 if (exp != NULL && *exp != '\0')
3090 if (edit_save_block (edit, exp, start_mark, end_mark))
3091 ret = TRUE;
3092 else
3093 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3095 edit->force |= REDRAW_COMPLETELY;
3098 g_free (exp);
3100 return ret;
3104 /* --------------------------------------------------------------------------------------------- */
3106 /** returns TRUE on success */
3107 gboolean
3108 edit_insert_file_cmd (WEdit * edit)
3110 char *tmp;
3111 char *exp;
3112 gboolean ret = FALSE;
3114 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3115 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3116 MC_HISTORY_EDIT_INSERT_FILE, tmp, INPUT_COMPLETE_FILENAMES);
3117 g_free (tmp);
3119 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3121 if (exp != NULL && *exp != '\0')
3123 vfs_path_t *exp_vpath;
3125 exp_vpath = vfs_path_from_str (exp);
3126 ret = (edit_insert_file (edit, exp_vpath) >= 0);
3127 vfs_path_free (exp_vpath);
3129 if (!ret)
3130 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3133 g_free (exp);
3135 edit->force |= REDRAW_COMPLETELY;
3136 return ret;
3139 /* --------------------------------------------------------------------------------------------- */
3140 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3143 edit_sort_cmd (WEdit * edit)
3145 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3146 off_t start_mark, end_mark;
3147 int e;
3149 if (!eval_marks (edit, &start_mark, &end_mark))
3151 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3152 return 0;
3155 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
3156 edit_save_block (edit, tmp, start_mark, end_mark);
3157 g_free (tmp);
3159 exp = input_dialog (_("Run sort"),
3160 _("Enter sort options (see manpage) separated by whitespace:"),
3161 MC_HISTORY_EDIT_SORT, INPUT_LAST_TEXT, INPUT_COMPLETE_NONE);
3163 if (exp == NULL)
3164 return 1;
3166 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
3167 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
3168 tmp =
3169 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3170 " > ", tmp_edit_temp_name, (char *) NULL);
3171 g_free (tmp_edit_temp_name);
3172 g_free (tmp_edit_block_name);
3173 g_free (exp);
3175 e = system (tmp);
3176 g_free (tmp);
3177 if (e != 0)
3179 if (e == -1 || e == 127)
3180 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3181 else
3183 char q[8];
3185 sprintf (q, "%d ", e);
3186 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3187 edit_error_dialog (_("Sort"), tmp);
3188 g_free (tmp);
3190 return -1;
3193 edit->force |= REDRAW_COMPLETELY;
3195 if (edit_block_delete_cmd (edit))
3196 return 1;
3199 vfs_path_t *tmp_vpath;
3201 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3202 edit_insert_file (edit, tmp_vpath);
3203 vfs_path_free (tmp_vpath);
3205 return 0;
3208 /* --------------------------------------------------------------------------------------------- */
3210 * Ask user for a command, execute it and paste its output back to the
3211 * editor.
3215 edit_ext_cmd (WEdit * edit)
3217 char *exp, *tmp, *tmp_edit_temp_file;
3218 int e;
3220 exp =
3221 input_dialog (_("Paste output of external command"),
3222 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, INPUT_LAST_TEXT,
3223 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_VARIABLES | INPUT_COMPLETE_USERNAMES
3224 | INPUT_COMPLETE_HOSTNAMES | INPUT_COMPLETE_CD | INPUT_COMPLETE_COMMANDS |
3225 INPUT_COMPLETE_SHELL_ESC);
3227 if (!exp)
3228 return 1;
3230 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3231 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3232 g_free (tmp_edit_temp_file);
3233 e = system (tmp);
3234 g_free (tmp);
3235 g_free (exp);
3237 if (e)
3239 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3240 return -1;
3243 edit->force |= REDRAW_COMPLETELY;
3246 vfs_path_t *tmp_vpath;
3248 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3249 edit_insert_file (edit, tmp_vpath);
3250 vfs_path_free (tmp_vpath);
3252 return 0;
3255 /* --------------------------------------------------------------------------------------------- */
3256 /** if block is 1, a block must be highlighted and the shell command
3257 processes it. If block is 0 the shell command is a straight system
3258 command, that just produces some output which is to be inserted */
3260 void
3261 edit_block_process_cmd (WEdit * edit, int macro_number)
3263 char *fname;
3264 char *macros_fname = NULL;
3266 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3267 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3268 user_menu (edit, macros_fname, 0);
3269 g_free (fname);
3270 g_free (macros_fname);
3271 edit->force |= REDRAW_COMPLETELY;
3274 /* --------------------------------------------------------------------------------------------- */
3276 void
3277 edit_mail_dialog (WEdit * edit)
3279 char *mail_to, *mail_subject, *mail_cc;
3281 quick_widget_t quick_widgets[] = {
3282 /* *INDENT-OFF* */
3283 QUICK_LABEL (N_("mail -s <subject> -c <cc> <to>"), NULL),
3284 QUICK_LABELED_INPUT (N_("To"), input_label_above,
3285 INPUT_LAST_TEXT, "mail-dlg-input-3",
3286 &mail_to, NULL, FALSE, FALSE, INPUT_COMPLETE_USERNAMES),
3287 QUICK_LABELED_INPUT (N_("Subject"), input_label_above,
3288 INPUT_LAST_TEXT, "mail-dlg-input-2",
3289 &mail_subject, NULL, FALSE, FALSE, INPUT_COMPLETE_NONE),
3290 QUICK_LABELED_INPUT (N_("Copies to"), input_label_above,
3291 INPUT_LAST_TEXT, "mail-dlg-input",
3292 &mail_cc, NULL, FALSE, FALSE, INPUT_COMPLETE_USERNAMES),
3293 QUICK_BUTTONS_OK_CANCEL,
3294 QUICK_END
3295 /* *INDENT-ON* */
3298 quick_dialog_t qdlg = {
3299 -1, -1, 50,
3300 N_("Mail"), "[Input Line Keys]",
3301 quick_widgets, NULL, NULL
3304 if (quick_dialog (&qdlg) != B_CANCEL)
3306 pipe_mail (&edit->buffer, mail_to, mail_subject, mail_cc);
3307 g_free (mail_to);
3308 g_free (mail_subject);
3309 g_free (mail_cc);
3313 /* --------------------------------------------------------------------------------------------- */
3315 /*******************/
3316 /* Word Completion */
3317 /*******************/
3320 * Complete current word using regular expression search
3321 * backwards beginning at the current cursor position.
3324 void
3325 edit_complete_word_cmd (WEdit * edit)
3327 gsize i, max_len, word_len = 0, num_compl = 0;
3328 off_t word_start = 0;
3329 GString *match_expr;
3330 GString *compl[MAX_WORD_COMPLETIONS]; /* completions */
3332 /* search start of word to be completed */
3333 if (!edit_find_word_start (&edit->buffer, &word_start, &word_len))
3334 return;
3336 /* prepare match expression */
3337 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3338 match_expr = g_string_new ("(^|\\s+|\\b)");
3339 for (i = 0; i < word_len; i++)
3340 g_string_append_c (match_expr, edit_buffer_get_byte (&edit->buffer, word_start + i));
3341 g_string_append (match_expr,
3342 "[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+");
3344 /* collect the possible completions */
3345 /* start search from begin to end of file */
3346 max_len =
3347 edit_collect_completions (edit, word_start, word_len, match_expr->str, (GString **) & compl,
3348 &num_compl);
3350 if (num_compl > 0)
3352 /* insert completed word if there is only one match */
3353 if (num_compl == 1)
3354 edit_complete_word_insert_recoded_completion (edit, compl[0]->str, word_len);
3355 /* more than one possible completion => ask the user */
3356 else
3358 char *curr_compl;
3360 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3361 /* !!! pressed again the selection dialog pops up, but that !!! */
3362 /* !!! seems to require a further internal state !!! */
3363 /*tty_beep (); */
3365 /* let the user select the preferred completion */
3366 curr_compl = editcmd_dialog_completion_show (edit, max_len,
3367 (GString **) & compl, num_compl);
3369 if (curr_compl != NULL)
3371 edit_complete_word_insert_recoded_completion (edit, curr_compl, word_len);
3372 g_free (curr_compl);
3377 g_string_free (match_expr, TRUE);
3378 /* release memory before return */
3379 for (i = 0; i < num_compl; i++)
3380 g_string_free (compl[i], TRUE);
3383 /* --------------------------------------------------------------------------------------------- */
3385 #ifdef HAVE_CHARSET
3386 void
3387 edit_select_codepage_cmd (WEdit * edit)
3389 if (do_select_codepage ())
3390 edit_set_codeset (edit);
3392 edit->force = REDRAW_PAGE;
3393 widget_redraw (WIDGET (edit));
3395 #endif
3397 /* --------------------------------------------------------------------------------------------- */
3399 void
3400 edit_insert_literal_cmd (WEdit * edit)
3402 int char_for_insertion;
3404 char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3405 _("Press any key:"), FALSE);
3406 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3409 /* --------------------------------------------------------------------------------------------- */
3411 void
3412 edit_begin_end_macro_cmd (WEdit * edit)
3414 /* edit is a pointer to the widget */
3415 if (edit != NULL)
3417 long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3418 edit_execute_key_command (edit, command, -1);
3422 /* --------------------------------------------------------------------------------------------- */
3424 void
3425 edit_begin_end_repeat_cmd (WEdit * edit)
3427 /* edit is a pointer to the widget */
3428 if (edit != NULL)
3430 long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3431 edit_execute_key_command (edit, command, -1);
3435 /* --------------------------------------------------------------------------------------------- */
3437 gboolean
3438 edit_load_forward_cmd (WEdit * edit)
3440 if (edit->modified
3441 && edit_query_dialog2 (_("Warning"),
3442 _("Current text was modified without a file save.\n"
3443 "Continue discards these changes"), _("C&ontinue"),
3444 _("&Cancel")) == 1)
3446 edit->force |= REDRAW_COMPLETELY;
3447 return TRUE;
3450 if (edit_stack_iterator + 1 >= MAX_HISTORY_MOVETO)
3451 return FALSE;
3453 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3454 return FALSE;
3456 edit_stack_iterator++;
3457 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3458 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3459 edit_history_moveto[edit_stack_iterator].line);
3461 return FALSE;
3464 /* --------------------------------------------------------------------------------------------- */
3466 gboolean
3467 edit_load_back_cmd (WEdit * edit)
3469 if (edit->modified
3470 && edit_query_dialog2 (_("Warning"),
3471 _("Current text was modified without a file save.\n"
3472 "Continue discards these changes"), _("C&ontinue"),
3473 _("&Cancel")) == 1)
3475 edit->force |= REDRAW_COMPLETELY;
3476 return TRUE;
3479 /* we are in the bottom of the stack, NO WAY! */
3480 if (edit_stack_iterator == 0)
3481 return FALSE;
3483 edit_stack_iterator--;
3484 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3485 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3486 edit_history_moveto[edit_stack_iterator].line);
3488 return FALSE;
3491 /* --------------------------------------------------------------------------------------------- */
3493 void
3494 edit_get_match_keyword_cmd (WEdit * edit)
3496 gsize word_len = 0, max_len = 0;
3497 int num_def = 0;
3498 gsize i;
3499 off_t word_start = 0;
3500 GString *match_expr;
3501 char *path = NULL;
3502 char *ptr = NULL;
3503 char *tagfile = NULL;
3505 etags_hash_t def_hash[MAX_DEFINITIONS];
3507 for (i = 0; i < MAX_DEFINITIONS; i++)
3509 def_hash[i].filename = NULL;
3512 /* search start of word to be completed */
3513 if (!edit_find_word_start (&edit->buffer, &word_start, &word_len))
3514 return;
3516 /* prepare match expression */
3517 match_expr = g_string_sized_new (word_len);
3518 for (i = 0; i < word_len; i++)
3519 g_string_append_c (match_expr, edit_buffer_get_byte (&edit->buffer, word_start + i));
3521 ptr = g_get_current_dir ();
3522 path = g_strconcat (ptr, PATH_SEP_STR, (char *) NULL);
3523 g_free (ptr);
3525 /* Recursive search file 'TAGS' in parent dirs */
3528 ptr = g_path_get_dirname (path);
3529 g_free (path);
3530 path = ptr;
3531 g_free (tagfile);
3532 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3533 if (exist_file (tagfile))
3534 break;
3536 while (strcmp (path, PATH_SEP_STR) != 0);
3538 if (tagfile)
3540 num_def =
3541 etags_set_definition_hash (tagfile, path, match_expr->str, (etags_hash_t *) & def_hash);
3542 g_free (tagfile);
3544 g_free (path);
3546 max_len = MAX_WIDTH_DEF_DIALOG;
3547 word_len = 0;
3548 if (num_def > 0)
3550 editcmd_dialog_select_definition_show (edit, match_expr->str, max_len, word_len,
3551 (etags_hash_t *) & def_hash, num_def);
3553 g_string_free (match_expr, TRUE);
3556 /* --------------------------------------------------------------------------------------------- */
3558 #ifdef HAVE_ASPELL
3560 edit_suggest_current_word (WEdit * edit)
3562 gsize cut_len = 0;
3563 gsize word_len = 0;
3564 off_t word_start = 0;
3565 int retval = B_SKIP_WORD;
3566 GString *match_word;
3568 /* search start of word to spell check */
3569 match_word = edit_buffer_get_word_from_pos (&edit->buffer, edit->buffer.curs1, &word_start,
3570 &cut_len);
3571 word_len = match_word->len;
3573 #ifdef HAVE_CHARSET
3574 if (mc_global.source_codepage >= 0 && (mc_global.source_codepage != mc_global.display_codepage))
3576 GString *tmp_word;
3578 tmp_word = str_convert_to_display (match_word->str);
3579 g_string_free (match_word, TRUE);
3580 match_word = tmp_word;
3582 #endif
3583 if (!aspell_check (match_word->str, (int) word_len))
3585 GArray *suggest;
3586 unsigned int res;
3587 guint i;
3589 suggest = g_array_new (TRUE, FALSE, sizeof (char *));
3591 res = aspell_suggest (suggest, match_word->str, (int) word_len);
3592 if (res != 0)
3594 char *new_word = NULL;
3596 edit->found_start = word_start;
3597 edit->found_len = word_len;
3598 edit->force |= REDRAW_PAGE;
3599 edit_scroll_screen_over_cursor (edit);
3600 edit_render_keypress (edit);
3602 retval = spell_dialog_spell_suggest_show (edit, match_word->str, &new_word, suggest);
3603 edit_cursor_move (edit, word_len - cut_len);
3605 if (retval == B_ENTER && new_word != NULL)
3607 char *cp_word;
3609 #ifdef HAVE_CHARSET
3610 if (mc_global.source_codepage >= 0 &&
3611 (mc_global.source_codepage != mc_global.display_codepage))
3613 GString *tmp_word;
3615 tmp_word = str_convert_to_input (new_word);
3616 g_free (new_word);
3617 new_word = g_string_free (tmp_word, FALSE);
3619 #endif
3620 cp_word = new_word;
3621 for (i = 0; i < word_len; i++)
3622 edit_backspace (edit, TRUE);
3623 for (; *new_word; new_word++)
3624 edit_insert (edit, *new_word);
3625 g_free (cp_word);
3627 else if (retval == B_ADD_WORD && match_word != NULL)
3628 aspell_add_to_dict (match_word->str, (int) word_len);
3632 for (i = 0; i < suggest->len; i++)
3634 char *cur_sugg_word;
3636 cur_sugg_word = g_array_index (suggest, char *, i);
3637 g_free (cur_sugg_word);
3639 g_array_free (suggest, TRUE);
3640 edit->found_start = 0;
3641 edit->found_len = 0;
3643 g_string_free (match_word, TRUE);
3644 return retval;
3647 /* --------------------------------------------------------------------------------------------- */
3649 void
3650 edit_spellcheck_file (WEdit * edit)
3652 if (edit->buffer.curs_line > 0)
3654 edit_cursor_move (edit, -edit->buffer.curs1);
3655 edit_move_to_prev_col (edit, 0);
3656 edit_update_curs_row (edit);
3661 int c1, c2;
3663 c2 = edit_buffer_get_current_byte (&edit->buffer);
3667 if (edit->buffer.curs1 >= edit->buffer.size)
3668 return;
3670 c1 = c2;
3671 edit_cursor_move (edit, 1);
3672 c2 = edit_buffer_get_current_byte (&edit->buffer);
3674 while (is_break_char (c1) || is_break_char (c2));
3676 while (edit_suggest_current_word (edit) != B_CANCEL);
3679 /* --------------------------------------------------------------------------------------------- */
3681 void
3682 edit_set_spell_lang (void)
3684 GArray *lang_list;
3686 lang_list = g_array_new (TRUE, FALSE, sizeof (char *));
3687 if (aspell_get_lang_list (lang_list) != 0)
3689 char *lang;
3691 lang = spell_dialog_lang_list_show (lang_list);
3692 if (lang != NULL)
3694 (void) aspell_set_lang (lang);
3695 g_free (lang);
3698 aspell_array_clean (lang_list);
3700 #endif /* HAVE_ASPELL */
3702 /* --------------------------------------------------------------------------------------------- */