Ticket #3495:(edit_ext_cmd): use INPUT_LAST_TEXT.
[midnight-commander.git] / src / editor / editcmd.c
blob565115259baff334e06cef29de45d19ebf8eb3b6
1 /*
2 Editor high level editing commands
4 Copyright (C) 1996-2015
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/lock.h"
59 #include "lib/util.h" /* tilde_expand() */
60 #include "lib/vfs/vfs.h"
61 #include "lib/widget.h"
62 #include "lib/event.h" /* mc_event_raise() */
63 #ifdef HAVE_CHARSET
64 #include "lib/charsets.h"
65 #endif
67 #include "src/history.h"
68 #include "src/setup.h" /* option_tab_spacing */
69 #ifdef HAVE_CHARSET
70 #include "src/selcodepage.h"
71 #endif
72 #include "src/keybind-defaults.h"
73 #include "src/util.h" /* check_for_default() */
75 #include "edit-impl.h"
76 #include "editwidget.h"
77 #include "editcmd_dialogs.h"
78 #ifdef HAVE_ASPELL
79 #include "spell.h"
80 #include "spell_dialogs.h"
81 #endif
82 #include "etags.h"
84 /*** global variables ****************************************************************************/
86 /* search and replace: */
87 int search_create_bookmark = FALSE;
89 /* queries on a save */
90 int edit_confirm_save = 1;
92 /* whether we need to drop selection on copy to buffer */
93 int option_drop_selection_on_copy = 1;
95 /*** file scope macro definitions ****************************************************************/
97 #define space_width 1
99 #define TEMP_BUF_LEN 1024
101 #define INPUT_INDEX 9
103 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
104 (and the above) routines to work properly - paul */
106 #define is_digit(x) ((x) >= '0' && (x) <= '9')
108 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
110 /*** file scope type declarations ****************************************************************/
112 typedef struct
114 simple_status_msg_t status_msg; /* base class */
116 gboolean first;
117 WEdit *edit;
118 off_t offset;
119 } edit_search_status_msg_t;
121 /*** file scope variables ************************************************************************/
123 static unsigned long edit_save_mode_radio_id, edit_save_mode_input_id;
125 /* --------------------------------------------------------------------------------------------- */
126 /*** file scope functions ************************************************************************/
127 /* --------------------------------------------------------------------------------------------- */
129 static int
130 edit_search_status_update_cb (status_msg_t * sm)
132 simple_status_msg_t *ssm = SIMPLE_STATUS_MSG (sm);
133 edit_search_status_msg_t *esm = (edit_search_status_msg_t *) sm;
134 Widget *wd = WIDGET (sm->dlg);
136 if (verbose)
137 label_set_textv (ssm->label, _("Searching %s: %3d%%"), esm->edit->last_search_string,
138 edit_buffer_calc_percent (&esm->edit->buffer, esm->offset));
139 else
140 label_set_textv (ssm->label, _("Searching %s"), esm->edit->last_search_string);
142 if (esm->first)
144 int wd_width;
145 Widget *lw = WIDGET (ssm->label);
147 wd_width = max (wd->cols, lw->cols + 6);
148 widget_set_size (wd, wd->y, wd->x, wd->lines, wd_width);
149 widget_set_size (lw, lw->y, wd->x + (wd->cols - lw->cols) / 2, lw->lines, lw->cols);
150 esm->first = FALSE;
153 return status_msg_common_update (sm);
156 /* --------------------------------------------------------------------------------------------- */
158 static cb_ret_t
159 edit_save_mode_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
161 switch (msg)
163 case MSG_ACTION:
164 if (sender != NULL && sender->id == edit_save_mode_radio_id)
166 Widget *ww;
168 ww = dlg_find_by_id (DIALOG (w), edit_save_mode_input_id);
169 widget_disable (ww, RADIO (sender)->sel != 2);
170 return MSG_HANDLED;
173 return MSG_NOT_HANDLED;
175 default:
176 return dlg_default_callback (w, sender, msg, parm, data);
180 /* --------------------------------------------------------------------------------------------- */
182 /* If 0 (quick save) then a) create/truncate <filename> file,
183 b) save to <filename>;
184 if 1 (safe save) then a) save to <tempnam>,
185 b) rename <tempnam> to <filename>;
186 if 2 (do backups) then a) save to <tempnam>,
187 b) rename <filename> to <filename.backup_ext>,
188 c) rename <tempnam> to <filename>. */
190 /* returns 0 on error, -1 on abort */
192 static int
193 edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath)
195 char *p;
196 gchar *tmp;
197 off_t filelen = 0;
198 int this_save_mode, rv, fd = -1;
199 vfs_path_t *real_filename_vpath;
200 vfs_path_t *savename_vpath = NULL;
201 const char *start_filename;
202 const vfs_path_element_t *vpath_element;
203 struct stat sb;
205 vpath_element = vfs_path_get_by_index (filename_vpath, 0);
206 if (vpath_element == NULL)
207 return 0;
209 start_filename = vpath_element->path;
210 if (*start_filename == '\0')
211 return 0;
213 if (!IS_PATH_SEP (*start_filename) && edit->dir_vpath != NULL)
214 real_filename_vpath = vfs_path_append_vpath_new (edit->dir_vpath, filename_vpath, NULL);
215 else
216 real_filename_vpath = vfs_path_clone (filename_vpath);
218 this_save_mode = option_save_mode;
219 if (this_save_mode != EDIT_QUICK_SAVE)
221 if (!vfs_file_is_local (real_filename_vpath)
222 || (fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY)) == -1)
225 * The file does not exists yet, so no safe save or
226 * backup are necessary.
228 this_save_mode = EDIT_QUICK_SAVE;
230 if (fd != -1)
231 mc_close (fd);
234 rv = mc_stat (real_filename_vpath, &sb);
235 if (rv == 0)
237 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt && sb.st_nlink > 1)
239 rv = edit_query_dialog3 (_("Warning"),
240 _("File has hard-links. Detach before saving?"),
241 _("&Yes"), _("&No"), _("&Cancel"));
242 switch (rv)
244 case 0:
245 this_save_mode = EDIT_SAFE_SAVE;
246 /* fallthrough */
247 case 1:
248 edit->skip_detach_prompt = 1;
249 break;
250 default:
251 vfs_path_free (real_filename_vpath);
252 return -1;
256 /* Prevent overwriting changes from other editor sessions. */
257 if (edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
259 /* The default action is "Cancel". */
260 query_set_sel (1);
262 rv = edit_query_dialog2 (_("Warning"),
263 _("The file has been modified in the meantime. Save anyway?"),
264 _("&Yes"), _("&Cancel"));
265 if (rv != 0)
267 vfs_path_free (real_filename_vpath);
268 return -1;
273 if (this_save_mode != EDIT_QUICK_SAVE)
275 char *savedir, *saveprefix;
277 savedir = vfs_path_tokens_get (real_filename_vpath, 0, -1);
278 if (savedir == NULL)
279 savedir = g_strdup (".");
281 /* Token-related function never return leading slash, so we need add it manually */
282 saveprefix = mc_build_filename (PATH_SEP_STR, savedir, "cooledit", NULL);
283 g_free (savedir);
284 fd = mc_mkstemps (&savename_vpath, saveprefix, NULL);
285 g_free (saveprefix);
286 if (savename_vpath == NULL)
288 vfs_path_free (real_filename_vpath);
289 return 0;
291 /* FIXME:
292 * Close for now because mc_mkstemps use pure open system call
293 * to create temporary file and it needs to be reopened by
294 * VFS-aware mc_open().
296 close (fd);
298 else
299 savename_vpath = vfs_path_clone (real_filename_vpath);
301 (void) mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
302 (void) mc_chmod (savename_vpath, edit->stat1.st_mode);
304 fd = mc_open (savename_vpath, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
305 if (fd == -1)
306 goto error_save;
308 /* pipe save */
309 p = edit_get_write_filter (savename_vpath, real_filename_vpath);
310 if (p != NULL)
312 FILE *file;
314 mc_close (fd);
315 file = (FILE *) popen (p, "w");
317 if (file)
319 filelen = edit_write_stream (edit, file);
320 #if 1
321 pclose (file);
322 #else
323 if (pclose (file) != 0)
325 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
326 edit_error_dialog (_("Error"), tmp);
327 g_free (tmp);
328 g_free (p);
329 goto error_save;
331 #endif
333 else
335 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
336 edit_error_dialog (_("Error"), get_sys_error (tmp));
337 g_free (p);
338 g_free (tmp);
339 goto error_save;
341 g_free (p);
343 else if (edit->lb == LB_ASIS)
344 { /* do not change line breaks */
345 filelen = edit_buffer_write_file (&edit->buffer, fd);
347 if (filelen != edit->buffer.size)
349 mc_close (fd);
350 goto error_save;
352 if (mc_close (fd) != 0)
353 goto error_save;
354 /* Update the file information, especially the mtime. */
355 if (mc_stat (savename_vpath, &edit->stat1) == -1)
356 goto error_save;
358 else
359 { /* change line breaks */
360 FILE *file;
361 const vfs_path_element_t *path_element;
363 mc_close (fd);
365 path_element = vfs_path_get_by_index (savename_vpath, -1);
366 file = (FILE *) fopen (path_element->path, "w");
367 if (file != NULL)
369 filelen = edit_write_stream (edit, file);
370 fclose (file);
372 else
374 char *msg;
376 msg = g_strdup_printf (_("Cannot open file for writing: %s"), path_element->path);
377 edit_error_dialog (_("Error"), msg);
378 g_free (msg);
379 goto error_save;
383 if (filelen != edit->buffer.size)
384 goto error_save;
386 if (this_save_mode == EDIT_DO_BACKUP)
388 char *tmp_store_filename;
389 vfs_path_element_t *last_vpath_element;
390 vfs_path_t *tmp_vpath;
391 gboolean ok;
393 #ifdef HAVE_ASSERT_H
394 assert (option_backup_ext != NULL);
395 #endif
396 /* add backup extension to the path */
397 tmp_vpath = vfs_path_clone (real_filename_vpath);
398 last_vpath_element = (vfs_path_element_t *) vfs_path_get_by_index (tmp_vpath, -1);
399 tmp_store_filename = last_vpath_element->path;
400 last_vpath_element->path = g_strdup_printf ("%s%s", tmp_store_filename, option_backup_ext);
401 g_free (tmp_store_filename);
403 ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
404 vfs_path_free (tmp_vpath);
405 if (!ok)
406 goto error_save;
409 if (this_save_mode != EDIT_QUICK_SAVE)
410 if (mc_rename (savename_vpath, real_filename_vpath) == -1)
411 goto error_save;
413 vfs_path_free (real_filename_vpath);
414 vfs_path_free (savename_vpath);
415 return 1;
416 error_save:
417 /* FIXME: Is this safe ?
418 * if (this_save_mode != EDIT_QUICK_SAVE)
419 * mc_unlink (savename);
421 vfs_path_free (real_filename_vpath);
422 vfs_path_free (savename_vpath);
423 return 0;
426 /* --------------------------------------------------------------------------------------------- */
428 static gboolean
429 edit_check_newline (const edit_buffer_t * buf)
431 return !(option_check_nl_at_eof && buf->size > 0
432 && edit_buffer_get_byte (buf, buf->size - 1) != '\n'
433 && edit_query_dialog2 (_("Warning"),
434 _("The file you are saving does not end with a newline."),
435 _("C&ontinue"), _("&Cancel")) != 0);
438 /* --------------------------------------------------------------------------------------------- */
440 static vfs_path_t *
441 edit_get_save_file_as (WEdit * edit)
443 static LineBreaks cur_lb = LB_ASIS;
444 char *filename_res;
445 vfs_path_t *ret_vpath = NULL;
447 const char *lb_names[LB_NAMES] = {
448 N_("&Do not change"),
449 N_("&Unix format (LF)"),
450 N_("&Windows/DOS format (CR LF)"),
451 N_("&Macintosh format (CR)")
454 quick_widget_t quick_widgets[] = {
455 /* *INDENT-OFF* */
456 QUICK_LABELED_INPUT (N_("Enter file name:"), input_label_above,
457 vfs_path_as_str (edit->filename_vpath), "save-as",
458 &filename_res, NULL, FALSE, FALSE, INPUT_COMPLETE_FILENAMES),
459 QUICK_SEPARATOR (TRUE),
460 QUICK_LABEL (N_("Change line breaks to:"), NULL),
461 QUICK_RADIO (LB_NAMES, lb_names, (int *) &cur_lb, NULL),
462 QUICK_BUTTONS_OK_CANCEL,
463 QUICK_END
464 /* *INDENT-ON* */
467 quick_dialog_t qdlg = {
468 -1, -1, 64,
469 N_("Save As"), "[Save File As]",
470 quick_widgets, NULL, NULL
473 if (quick_dialog (&qdlg) != B_CANCEL)
475 char *fname;
477 edit->lb = cur_lb;
478 fname = tilde_expand (filename_res);
479 g_free (filename_res);
480 ret_vpath = vfs_path_from_str (fname);
481 g_free (fname);
484 return ret_vpath;
487 /* --------------------------------------------------------------------------------------------- */
489 /** returns TRUE on success */
491 static gboolean
492 edit_save_cmd (WEdit * edit)
494 int res, save_lock = 0;
496 if (!edit->locked && !edit->delete_file)
497 save_lock = lock_file (edit->filename_vpath);
498 res = edit_save_file (edit, edit->filename_vpath);
500 /* Maintain modify (not save) lock on failure */
501 if ((res > 0 && edit->locked) || save_lock)
502 edit->locked = unlock_file (edit->filename_vpath);
504 /* On failure try 'save as', it does locking on its own */
505 if (res == 0)
506 return edit_save_as_cmd (edit);
507 edit->force |= REDRAW_COMPLETELY;
508 if (res > 0)
510 edit->delete_file = 0;
511 edit->modified = 0;
514 return TRUE;
517 /* --------------------------------------------------------------------------------------------- */
519 * Load file content
521 * @param h screen the owner of editor window
522 * @param vpath vfs file path
523 * @return TRUE if file content was successfully loaded, FALSE otherwise
526 static inline gboolean
527 edit_load_file_from_filename (WDialog * h, const vfs_path_t * vpath)
529 Widget *w = WIDGET (h);
531 return edit_add_window (h, w->y + 1, w->x, w->lines - 2, w->cols, vpath, 0);
534 /* --------------------------------------------------------------------------------------------- */
536 static void
537 edit_delete_column_of_text (WEdit * edit)
539 off_t p, q, r;
540 off_t m1, m2;
541 off_t n;
542 long b, c, d;
544 eval_marks (edit, &m1, &m2);
545 n = edit_buffer_move_forward (&edit->buffer, m1, 0, m2) + 1;
546 c = (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, m1), 0, m1);
547 d = (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, m2), 0, m2);
548 b = max (min (c, d), min (edit->column1, edit->column2));
549 c = max (c, max (edit->column1, edit->column2));
551 while (n--)
553 r = edit_buffer_get_current_bol (&edit->buffer);
554 p = edit_move_forward3 (edit, r, b, 0);
555 q = edit_move_forward3 (edit, r, c, 0);
556 if (p < m1)
557 p = m1;
558 if (q > m2)
559 q = m2;
560 edit_cursor_move (edit, p - edit->buffer.curs1);
561 while (q > p)
563 /* delete line between margins */
564 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
565 edit_delete (edit, TRUE);
566 q--;
568 if (n)
569 /* move to next line except on the last delete */
570 edit_cursor_move (edit,
571 edit_buffer_move_forward (&edit->buffer, edit->buffer.curs1, 1,
572 0) - edit->buffer.curs1);
576 /* --------------------------------------------------------------------------------------------- */
577 /** if success return 0 */
579 static int
580 edit_block_delete (WEdit * edit)
582 off_t start_mark, end_mark;
583 off_t curs_pos;
584 long curs_line, c1, c2;
586 if (!eval_marks (edit, &start_mark, &end_mark))
587 return 0;
589 if (edit->column_highlight && edit->mark2 < 0)
590 edit_mark_cmd (edit, FALSE);
591 if ((end_mark - start_mark) > option_max_undo / 2)
593 /* Warning message with a query to continue or cancel the operation */
594 if (edit_query_dialog2
595 (_("Warning"),
597 ("Block is large, you may not be able to undo this action"),
598 _("C&ontinue"), _("&Cancel")))
600 return 1;
603 c1 = min (edit->column1, edit->column2);
604 c2 = max (edit->column1, edit->column2);
605 edit->column1 = c1;
606 edit->column2 = c2;
608 edit_push_markers (edit);
610 curs_line = edit->buffer.curs_line;
612 curs_pos = edit->curs_col + edit->over_col;
614 /* move cursor to start of selection */
615 edit_cursor_move (edit, start_mark - edit->buffer.curs1);
616 edit_scroll_screen_over_cursor (edit);
618 if (start_mark < end_mark)
620 if (edit->column_highlight)
622 off_t line_width;
624 if (edit->mark2 < 0)
625 edit_mark_cmd (edit, FALSE);
626 edit_delete_column_of_text (edit);
627 /* move cursor to the saved position */
628 edit_move_to_line (edit, curs_line);
629 /* calculate line width and cursor position before cut */
630 line_width = edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), 0,
631 edit_buffer_get_current_eol (&edit->buffer));
632 if (option_cursor_beyond_eol && curs_pos > line_width)
633 edit->over_col = curs_pos - line_width;
635 else
637 off_t count;
639 for (count = start_mark; count < end_mark; count++)
640 edit_delete (edit, TRUE);
643 edit_set_markers (edit, 0, 0, 0, 0);
644 edit->force |= REDRAW_PAGE;
645 return 0;
648 /* --------------------------------------------------------------------------------------------- */
650 * Get EOL symbol for searching.
652 * @param edit editor object
653 * @return EOL symbol
656 static inline char
657 edit_search_get_current_end_line_char (const WEdit * edit)
659 switch (edit->lb)
661 case LB_MAC:
662 return '\r';
663 default:
664 return '\n';
668 /* --------------------------------------------------------------------------------------------- */
670 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
672 * @param search search object
673 * @return result of checks.
676 static edit_search_line_t
677 edit_get_search_line_type (mc_search_t * search)
679 edit_search_line_t search_line_type = 0;
681 if (search->search_type != MC_SEARCH_T_REGEX)
682 return search_line_type;
684 if (*search->original == '^')
685 search_line_type |= AT_START_LINE;
687 if (search->original[search->original_len - 1] == '$')
688 search_line_type |= AT_END_LINE;
689 return search_line_type;
692 /* --------------------------------------------------------------------------------------------- */
694 * Calculating the start position of next line.
696 * @param buf editor buffer object
697 * @param current_pos current position
698 * @param max_pos max position
699 * @param end_string_symbol end of line symbol
700 * @return start position of next line
703 static off_t
704 edit_calculate_start_of_next_line (const edit_buffer_t * buf, off_t current_pos, off_t max_pos,
705 char end_string_symbol)
707 off_t i;
709 for (i = current_pos; i < max_pos; i++)
711 current_pos++;
712 if (edit_buffer_get_byte (buf, i) == end_string_symbol)
713 break;
716 return current_pos;
719 /* --------------------------------------------------------------------------------------------- */
721 * Calculating the end position of previous line.
723 * @param buf editor buffer object
724 * @param current_pos current position
725 * @param end_string_symbol end of line symbol
726 * @return end position of previous line
729 static off_t
730 edit_calculate_end_of_previous_line (const edit_buffer_t * buf, off_t current_pos,
731 char end_string_symbol)
733 off_t i;
735 for (i = current_pos - 1; i >= 0; i--)
736 if (edit_buffer_get_byte (buf, i) == end_string_symbol)
737 break;
739 return i;
742 /* --------------------------------------------------------------------------------------------- */
744 * Calculating the start position of previous line.
746 * @param buf editor buffer object
747 * @param current_pos current position
748 * @param end_string_symbol end of line symbol
749 * @return start position of previous line
752 static inline off_t
753 edit_calculate_start_of_previous_line (const edit_buffer_t * buf, off_t current_pos,
754 char end_string_symbol)
756 current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
757 current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
759 return (current_pos + 1);
762 /* --------------------------------------------------------------------------------------------- */
764 * Calculating the start position of current line.
766 * @param buf editor buffer object
767 * @param current_pos current position
768 * @param end_string_symbol end of line symbol
769 * @return start position of current line
772 static inline off_t
773 edit_calculate_start_of_current_line (const edit_buffer_t * buf, off_t current_pos,
774 char end_string_symbol)
776 current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
778 return (current_pos + 1);
781 /* --------------------------------------------------------------------------------------------- */
783 * Fixing (if needed) search start position if 'only in selection' option present.
785 * @param edit editor object
788 static void
789 edit_search_fix_search_start_if_selection (WEdit * edit)
791 off_t start_mark = 0;
792 off_t end_mark = 0;
794 if (!edit_search_options.only_in_selection)
795 return;
797 if (!eval_marks (edit, &start_mark, &end_mark) != 0)
798 return;
800 if (edit_search_options.backwards)
802 if (edit->search_start > end_mark || edit->search_start <= start_mark)
803 edit->search_start = end_mark;
805 else
807 if (edit->search_start < start_mark || edit->search_start >= end_mark)
808 edit->search_start = start_mark;
812 /* --------------------------------------------------------------------------------------------- */
814 static gboolean
815 editcmd_find (edit_search_status_msg_t * esm, gsize * len)
817 WEdit *edit = esm->edit;
818 off_t search_start = edit->search_start;
819 off_t search_end;
820 off_t start_mark = 0;
821 off_t end_mark = edit->buffer.size;
822 char end_string_symbol;
824 end_string_symbol = edit_search_get_current_end_line_char (edit);
826 /* prepare for search */
827 if (edit_search_options.only_in_selection)
829 if (!eval_marks (edit, &start_mark, &end_mark))
831 edit->search->error = MC_SEARCH_E_NOTFOUND;
832 edit->search->error_str = g_strdup (_("Search string not found"));
833 return FALSE;
836 /* fix the start and the end of search block positions */
837 if ((edit->search_line_type & AT_START_LINE) != 0
838 && (start_mark != 0
839 || edit_buffer_get_byte (&edit->buffer, start_mark - 1) != end_string_symbol))
841 start_mark =
842 edit_calculate_start_of_next_line (&edit->buffer, start_mark, edit->buffer.size,
843 end_string_symbol);
845 if ((edit->search_line_type & AT_END_LINE) != 0
846 && (end_mark - 1 != edit->buffer.size
847 || edit_buffer_get_byte (&edit->buffer, end_mark) != end_string_symbol))
848 end_mark =
849 edit_calculate_end_of_previous_line (&edit->buffer, end_mark, end_string_symbol);
850 if (start_mark >= end_mark)
852 edit->search->error = MC_SEARCH_E_NOTFOUND;
853 edit->search->error_str = g_strdup (_("Search string not found"));
854 return FALSE;
857 else
859 if (edit_search_options.backwards)
860 end_mark = max (1, edit->buffer.curs1) - 1;
863 /* search */
864 if (edit_search_options.backwards)
866 /* backward search */
867 search_end = end_mark;
869 if ((edit->search_line_type & AT_START_LINE) != 0)
870 search_start =
871 edit_calculate_start_of_current_line (&edit->buffer, search_start,
872 end_string_symbol);
874 while (search_start >= start_mark)
876 if (search_end > (off_t) (search_start + edit->search->original_len)
877 && mc_search_is_fixed_search_str (edit->search))
879 search_end = search_start + edit->search->original_len;
881 if (mc_search_run (edit->search, (void *) esm, search_start, search_end, len)
882 && edit->search->normal_offset == search_start)
884 return TRUE;
888 if ((edit->search_line_type & AT_START_LINE) != 0)
889 search_start =
890 edit_calculate_start_of_previous_line (&edit->buffer, search_start,
891 end_string_symbol);
892 else
893 search_start--;
895 edit->search->error_str = g_strdup (_("Search string not found"));
897 else
899 /* forward search */
900 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
901 search_start =
902 edit_calculate_start_of_next_line (&edit->buffer, search_start, end_mark,
903 end_string_symbol);
904 return mc_search_run (edit->search, (void *) esm, search_start, end_mark, len);
906 return FALSE;
909 /* --------------------------------------------------------------------------------------------- */
911 static char *
912 edit_replace_cmd__conv_to_display (char *str)
914 #ifdef HAVE_CHARSET
915 GString *tmp;
917 tmp = str_convert_to_display (str);
918 if (tmp != NULL)
920 if (tmp->len != 0)
921 return g_string_free (tmp, FALSE);
922 g_string_free (tmp, TRUE);
924 #endif
925 return g_strdup (str);
928 /* --------------------------------------------------------------------------------------------- */
930 static char *
931 edit_replace_cmd__conv_to_input (char *str)
933 #ifdef HAVE_CHARSET
934 GString *tmp;
936 tmp = str_convert_to_input (str);
937 if (tmp != NULL)
939 if (tmp->len != 0)
940 return g_string_free (tmp, FALSE);
941 g_string_free (tmp, TRUE);
943 #endif
944 return g_strdup (str);
947 /* --------------------------------------------------------------------------------------------- */
949 static void
950 edit_do_search (WEdit * edit)
952 edit_search_status_msg_t esm;
953 gsize len = 0;
955 if (edit->search == NULL)
956 edit->search_start = edit->buffer.curs1;
958 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
960 esm.first = TRUE;
961 esm.edit = edit;
962 esm.offset = edit->search_start;
964 status_msg_init (STATUS_MSG (&esm), _("Search"), 1.0, simple_status_msg_init_cb,
965 edit_search_status_update_cb, NULL);
967 if (search_create_bookmark)
969 int found = 0, books = 0;
970 long l = 0, l_last = -1;
971 long q = 0;
973 search_create_bookmark = FALSE;
974 book_mark_flush (edit, -1);
976 while (mc_search_run (edit->search, (void *) &esm, q, edit->buffer.size, &len))
978 if (found == 0)
979 edit->search_start = edit->search->normal_offset;
980 found++;
981 l += edit_buffer_count_lines (&edit->buffer, q, edit->search->normal_offset);
982 if (l != l_last)
984 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
985 books++;
987 l_last = l;
988 q = edit->search->normal_offset + 1;
991 if (found == 0)
992 edit_error_dialog (_("Search"), _("Search string not found"));
993 else
994 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
996 else
998 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
999 && edit_search_options.backwards)
1000 edit->search_start--;
1002 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
1003 && !edit_search_options.backwards)
1004 edit->search_start++;
1006 if (editcmd_find (&esm, &len))
1008 edit->found_start = edit->search_start = edit->search->normal_offset;
1009 edit->found_len = len;
1010 edit->over_col = 0;
1011 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
1012 edit_scroll_screen_over_cursor (edit);
1013 if (edit_search_options.backwards)
1014 edit->search_start--;
1015 else
1016 edit->search_start++;
1018 else
1020 edit->search_start = edit->buffer.curs1;
1021 if (edit->search->error_str != NULL)
1022 edit_query_dialog (_("Search"), edit->search->error_str);
1026 status_msg_deinit (STATUS_MSG (&esm));
1028 edit->force |= REDRAW_COMPLETELY;
1029 edit_scroll_screen_over_cursor (edit);
1032 /* --------------------------------------------------------------------------------------------- */
1034 static void
1035 edit_search (WEdit * edit)
1037 if (editcmd_dialog_search_show (edit))
1039 edit->search_line_type = edit_get_search_line_type (edit->search);
1040 edit_search_fix_search_start_if_selection (edit);
1041 edit_do_search (edit);
1045 /* --------------------------------------------------------------------------------------------- */
1046 /** Return a null terminated length of text. Result must be g_free'd */
1048 static unsigned char *
1049 edit_get_block (WEdit * edit, off_t start, off_t finish, off_t * l)
1051 unsigned char *s, *r;
1053 r = s = g_malloc0 (finish - start + 1);
1054 if (edit->column_highlight)
1056 *l = 0;
1057 /* copy from buffer, excluding chars that are out of the column 'margins' */
1058 while (start < finish)
1060 int c;
1061 off_t x;
1063 x = edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, start), 0, start);
1064 c = edit_buffer_get_byte (&edit->buffer, start);
1065 if ((x >= edit->column1 && x < edit->column2)
1066 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1068 *s++ = c;
1069 (*l)++;
1071 start++;
1074 else
1076 *l = finish - start;
1077 while (start < finish)
1078 *s++ = edit_buffer_get_byte (&edit->buffer, start++);
1080 *s = '\0';
1081 return r;
1084 /* --------------------------------------------------------------------------------------------- */
1085 /** copies a block to clipboard file */
1087 static gboolean
1088 edit_save_block_to_clip_file (WEdit * edit, off_t start, off_t finish)
1090 gboolean ret;
1091 gchar *tmp;
1093 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1094 ret = edit_save_block (edit, tmp, start, finish);
1095 g_free (tmp);
1096 return ret;
1099 /* --------------------------------------------------------------------------------------------- */
1101 static void
1102 pipe_mail (const edit_buffer_t * buf, char *to, char *subject, char *cc)
1104 FILE *p = 0;
1105 char *s;
1107 to = name_quote (to, FALSE);
1108 subject = name_quote (subject, FALSE);
1109 cc = name_quote (cc, FALSE);
1110 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1111 g_free (to);
1112 g_free (subject);
1113 g_free (cc);
1115 if (s != NULL)
1117 p = popen (s, "w");
1118 g_free (s);
1121 if (p != NULL)
1123 off_t i;
1125 for (i = 0; i < buf->size; i++)
1126 fputc (edit_buffer_get_byte (buf, i), p);
1127 pclose (p);
1131 /* --------------------------------------------------------------------------------------------- */
1132 /** find first character of current word */
1134 static gboolean
1135 edit_find_word_start (const edit_buffer_t * buf, off_t * word_start, gsize * word_len)
1137 int c;
1138 off_t i;
1140 /* return if at begin of file */
1141 if (buf->curs1 <= 0)
1142 return FALSE;
1144 c = edit_buffer_get_previous_byte (buf);
1145 /* return if not at end or in word */
1146 if (is_break_char (c))
1147 return FALSE;
1149 /* search start of word to be completed */
1150 for (i = 1;; i++)
1152 int last;
1154 last = c;
1155 c = edit_buffer_get_byte (buf, buf->curs1 - i - 1);
1157 if (is_break_char (c))
1159 /* return if word starts with digit */
1160 if (isdigit (last))
1161 return FALSE;
1163 break;
1167 /* success */
1168 *word_start = buf->curs1 - i; /* start found */
1169 *word_len = (gsize) i;
1170 return TRUE;
1173 /* --------------------------------------------------------------------------------------------- */
1175 * Get current word under cursor
1177 * @param esm status message window
1178 * @param srch mc_search object
1179 * @param word_start start word position
1181 * @return newly allocated string or NULL if no any words under cursor
1184 static char *
1185 edit_collect_completions_get_current_word (edit_search_status_msg_t * esm, mc_search_t * srch,
1186 off_t word_start)
1188 WEdit *edit = esm->edit;
1189 gsize len = 0;
1190 off_t i;
1191 GString *temp;
1193 if (!mc_search_run (srch, (void *) esm, word_start, edit->buffer.size, &len))
1194 return NULL;
1196 temp = g_string_sized_new (len);
1198 for (i = 0; i < (off_t) len; i++)
1200 int chr;
1202 chr = edit_buffer_get_byte (&edit->buffer, word_start + i);
1203 if (!isspace (chr))
1204 g_string_append_c (temp, chr);
1207 return g_string_free (temp, temp->len == 0);
1210 /* --------------------------------------------------------------------------------------------- */
1211 /** collect the possible completions */
1213 static gsize
1214 edit_collect_completions (WEdit * edit, off_t word_start, gsize word_len,
1215 char *match_expr, GString ** compl, gsize * num)
1217 gsize len = 0;
1218 gsize max_len = 0;
1219 gsize i;
1220 int skip;
1221 GString *temp;
1222 mc_search_t *srch;
1223 off_t last_byte, start = -1;
1224 char *current_word;
1225 gboolean entire_file;
1226 edit_search_status_msg_t esm;
1228 #ifdef HAVE_CHARSET
1229 srch = mc_search_new (match_expr, -1, cp_source);
1230 #else
1231 srch = mc_search_new (match_expr, -1, NULL);
1232 #endif
1233 if (srch == NULL)
1234 return 0;
1236 entire_file =
1237 mc_config_get_bool (mc_main_config, CONFIG_APP_SECTION,
1238 "editor_wordcompletion_collect_entire_file", 0);
1240 last_byte = entire_file ? edit->buffer.size : word_start;
1242 srch->search_type = MC_SEARCH_T_REGEX;
1243 srch->is_case_sensitive = TRUE;
1244 srch->search_fn = edit_search_cmd_callback;
1245 srch->update_fn = edit_search_update_callback;
1247 esm.first = TRUE;
1248 esm.edit = edit;
1249 esm.offset = entire_file ? 0 : word_start;
1251 status_msg_init (STATUS_MSG (&esm), _("Collect completions"), 1.0, simple_status_msg_init_cb,
1252 edit_search_status_update_cb, NULL);
1254 current_word = edit_collect_completions_get_current_word (&esm, srch, word_start);
1256 temp = g_string_new ("");
1258 /* collect max MAX_WORD_COMPLETIONS completions */
1259 while (mc_search_run (srch, (void *) &esm, start + 1, last_byte, &len))
1261 g_string_set_size (temp, 0);
1262 start = srch->normal_offset;
1264 /* add matched completion if not yet added */
1265 for (i = 0; i < len; i++)
1267 skip = edit_buffer_get_byte (&edit->buffer, start + i);
1268 if (isspace (skip))
1269 continue;
1271 /* skip current word */
1272 if (start + (off_t) i == word_start)
1273 break;
1275 g_string_append_c (temp, skip);
1278 if (temp->len == 0)
1279 continue;
1281 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1282 continue;
1284 skip = 0;
1286 for (i = 0; i < *num; i++)
1288 if (strncmp
1289 ((char *) &compl[i]->str[word_len],
1290 (char *) &temp->str[word_len], max (len, compl[i]->len) - word_len) == 0)
1292 GString *this = compl[i];
1293 for (++i; i < *num; i++)
1294 compl[i - 1] = compl[i];
1295 compl[*num - 1] = this;
1296 skip = 1;
1297 break; /* skip it, already added */
1300 if (skip != 0)
1301 continue;
1303 if (*num == MAX_WORD_COMPLETIONS)
1305 g_string_free (compl[0], TRUE);
1306 for (i = 1; i < *num; i++)
1307 compl[i - 1] = compl[i];
1308 (*num)--;
1310 #ifdef HAVE_CHARSET
1312 GString *recoded;
1313 recoded = str_convert_to_display (temp->str);
1315 if (recoded && recoded->len)
1316 g_string_assign (temp, recoded->str);
1318 g_string_free (recoded, TRUE);
1320 #endif
1321 compl[(*num)++] = g_string_new_len (temp->str, temp->len);
1322 start += len;
1324 /* note the maximal length needed for the completion dialog */
1325 if (len > max_len)
1326 max_len = len;
1329 status_msg_deinit (STATUS_MSG (&esm));
1330 mc_search_free (srch);
1331 g_string_free (temp, TRUE);
1332 g_free (current_word);
1334 return max_len;
1337 /* --------------------------------------------------------------------------------------------- */
1339 static void
1340 edit_insert_column_of_text (WEdit * edit, unsigned char *data, off_t size, long width,
1341 off_t * start_pos, off_t * end_pos, long *col1, long *col2)
1343 off_t i, cursor;
1344 long col;
1346 cursor = edit->buffer.curs1;
1347 col = edit_get_col (edit);
1349 for (i = 0; i < size; i++)
1351 if (data[i] != '\n')
1352 edit_insert (edit, data[i]);
1353 else
1354 { /* fill in and move to next line */
1355 long l;
1356 off_t p;
1358 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
1360 for (l = width - (edit_get_col (edit) - col); l > 0; l -= space_width)
1361 edit_insert (edit, ' ');
1363 for (p = edit->buffer.curs1;; p++)
1365 if (p == edit->buffer.size)
1367 edit_cursor_move (edit, edit->buffer.size - edit->buffer.curs1);
1368 edit_insert_ahead (edit, '\n');
1369 p++;
1370 break;
1372 if (edit_buffer_get_byte (&edit->buffer, p) == '\n')
1374 p++;
1375 break;
1378 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->buffer.curs1);
1380 for (l = col - edit_get_col (edit); l >= space_width; l -= space_width)
1381 edit_insert (edit, ' ');
1385 *col1 = col;
1386 *col2 = col + width;
1387 *start_pos = cursor;
1388 *end_pos = edit->buffer.curs1;
1389 edit_cursor_move (edit, cursor - edit->buffer.curs1);
1392 /* --------------------------------------------------------------------------------------------- */
1394 static int
1395 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1397 const macros_t *m1 = (const macros_t *) macro1;
1398 const macros_t *m2 = (const macros_t *) macro2;
1400 return m1->hotkey - m2->hotkey;
1403 /* --------------------------------------------------------------------------------------------- */
1405 static void
1406 edit_macro_sort_by_hotkey (void)
1408 if (macros_list != NULL && macros_list->len != 0)
1409 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1412 /* --------------------------------------------------------------------------------------------- */
1414 static gboolean
1415 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1417 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1418 macros_t *result;
1419 macros_t search_macro;
1421 (void) edit;
1423 search_macro.hotkey = hotkey;
1424 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1425 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1427 if (result != NULL && result->macro != NULL)
1429 *indx = (result - array_start);
1430 *macros = result;
1431 return TRUE;
1433 *indx = 0;
1434 return FALSE;
1437 /* --------------------------------------------------------------------------------------------- */
1438 /** returns FALSE on error */
1440 static gboolean
1441 edit_delete_macro (WEdit * edit, int hotkey)
1443 mc_config_t *macros_config = NULL;
1444 const char *section_name = "editor";
1445 gchar *macros_fname;
1446 guint indx;
1447 char *skeyname;
1448 const macros_t *macros = NULL;
1450 /* clear array of actions for current hotkey */
1451 while (edit_get_macro (edit, hotkey, &macros, &indx))
1453 if (macros->macro != NULL)
1454 g_array_free (macros->macro, TRUE);
1455 macros = NULL;
1456 g_array_remove_index (macros_list, indx);
1457 edit_macro_sort_by_hotkey ();
1460 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1461 macros_config = mc_config_init (macros_fname, FALSE);
1462 g_free (macros_fname);
1464 if (macros_config == NULL)
1465 return FALSE;
1467 skeyname = lookup_key_by_code (hotkey);
1468 while (mc_config_del_key (macros_config, section_name, skeyname))
1470 g_free (skeyname);
1471 mc_config_save_file (macros_config, NULL);
1472 mc_config_deinit (macros_config);
1473 return TRUE;
1476 /* --------------------------------------------------------------------------------------------- */
1478 * Callback for the iteration of objects in the 'editors' array.
1479 * Toggle syntax highlighting in editor object.
1481 * @param data probably WEdit object
1482 * @param user_data unused
1485 static void
1486 edit_syntax_onoff_cb (void *data, void *user_data)
1488 (void) user_data;
1490 if (edit_widget_is_editor ((const Widget *) data))
1492 WEdit *edit = (WEdit *) data;
1494 if (option_syntax_highlighting)
1495 edit_load_syntax (edit, NULL, edit->syntax_type);
1496 edit->force |= REDRAW_PAGE;
1500 /* --------------------------------------------------------------------------------------------- */
1502 * Callback for the iteration of objects in the 'editors' array.
1503 * Redraw editor object.
1505 * @param data probably WEdit object
1506 * @param user_data unused
1509 static void
1510 edit_redraw_page_cb (void *data, void *user_data)
1512 (void) user_data;
1514 if (edit_widget_is_editor ((const Widget *) data))
1515 ((WEdit *) data)->force |= REDRAW_PAGE;
1518 /* --------------------------------------------------------------------------------------------- */
1520 * Insert autocompleted word into editor.
1522 * @param edit editor object
1523 * @param completion word for completion
1524 * @param word_len offset from beginning for insert
1527 static void
1528 edit_complete_word_insert_recoded_completion (WEdit * edit, char *completion, gsize word_len)
1530 #ifdef HAVE_CHARSET
1531 GString *temp;
1533 temp = str_convert_to_input (completion);
1535 for (completion = temp->str + word_len; *completion != '\0'; completion++)
1536 edit_insert (edit, *completion);
1537 g_string_free (temp, TRUE);
1538 #else
1539 for (completion += word_len; *completion != '\0'; completion++)
1540 edit_insert (edit, *completion);
1541 #endif
1544 /* --------------------------------------------------------------------------------------------- */
1545 /*** public functions ****************************************************************************/
1546 /* --------------------------------------------------------------------------------------------- */
1548 void
1549 edit_refresh_cmd (void)
1551 clr_scr ();
1552 repaint_screen ();
1553 tty_keypad (TRUE);
1556 /* --------------------------------------------------------------------------------------------- */
1558 * Toggle syntax highlighting in all editor windows.
1560 * @param h root widget for all windows
1563 void
1564 edit_syntax_onoff_cmd (WDialog * h)
1566 option_syntax_highlighting = !option_syntax_highlighting;
1567 g_list_foreach (h->widgets, edit_syntax_onoff_cb, NULL);
1568 dlg_redraw (h);
1571 /* --------------------------------------------------------------------------------------------- */
1573 * Toggle tabs showing in all editor windows.
1575 * @param h root widget for all windows
1578 void
1579 edit_show_tabs_tws_cmd (WDialog * h)
1581 enable_show_tabs_tws = !enable_show_tabs_tws;
1582 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1583 dlg_redraw (h);
1586 /* --------------------------------------------------------------------------------------------- */
1588 * Toggle right margin showing in all editor windows.
1590 * @param h root widget for all windows
1593 void
1594 edit_show_margin_cmd (WDialog * h)
1596 show_right_margin = !show_right_margin;
1597 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1598 dlg_redraw (h);
1601 /* --------------------------------------------------------------------------------------------- */
1603 * Toggle line numbers showing in all editor windows.
1605 * @param h root widget for all windows
1608 void
1609 edit_show_numbers_cmd (WDialog * h)
1611 option_line_state = !option_line_state;
1612 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
1613 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1614 dlg_redraw (h);
1617 /* --------------------------------------------------------------------------------------------- */
1619 void
1620 edit_save_mode_cmd (void)
1622 char *str_result = NULL;
1624 const char *str[] = {
1625 N_("&Quick save"),
1626 N_("&Safe save"),
1627 N_("&Do backups with following extension:")
1630 #ifdef HAVE_ASSERT_H
1631 assert (option_backup_ext != NULL);
1632 #endif
1634 #ifdef ENABLE_NLS
1635 size_t i;
1637 for (i = 0; i < 3; i++)
1638 str[i] = _(str[i]);
1639 #endif
1642 quick_widget_t quick_widgets[] = {
1643 /* *INDENT-OFF* */
1644 QUICK_RADIO (3, str, &option_save_mode, &edit_save_mode_radio_id),
1645 QUICK_INPUT (option_backup_ext, "edit-backup-ext", &str_result,
1646 &edit_save_mode_input_id, FALSE, FALSE, INPUT_COMPLETE_NONE),
1647 QUICK_SEPARATOR (TRUE),
1648 QUICK_CHECKBOX (N_("Check &POSIX new line"), &option_check_nl_at_eof, NULL),
1649 QUICK_BUTTONS_OK_CANCEL,
1650 QUICK_END
1651 /* *INDENT-ON* */
1654 quick_dialog_t qdlg = {
1655 -1, -1, 38,
1656 N_("Edit Save Mode"), "[Edit Save Mode]",
1657 quick_widgets, edit_save_mode_callback, NULL
1660 if (quick_dialog (&qdlg) != B_CANCEL)
1662 g_free (option_backup_ext);
1663 option_backup_ext = str_result;
1668 /* --------------------------------------------------------------------------------------------- */
1670 void
1671 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1673 vfs_path_free (edit->filename_vpath);
1674 edit->filename_vpath = vfs_path_clone (name_vpath);
1676 if (edit->dir_vpath == NULL)
1677 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1680 /* --------------------------------------------------------------------------------------------- */
1681 /* Here we want to warn the users of overwriting an existing file,
1682 but only if they have made a change to the filename */
1683 /* returns TRUE on success */
1684 gboolean
1685 edit_save_as_cmd (WEdit * edit)
1687 /* This heads the 'Save As' dialog box */
1688 vfs_path_t *exp_vpath;
1689 int save_lock = 0;
1690 int different_filename = 0;
1692 if (!edit_check_newline (&edit->buffer))
1693 return FALSE;
1695 exp_vpath = edit_get_save_file_as (edit);
1696 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1698 if (exp_vpath != NULL)
1700 if (vfs_path_len (exp_vpath) == 0)
1701 goto ret;
1702 else
1704 int rv;
1706 if (!vfs_path_equal (edit->filename_vpath, exp_vpath))
1708 int file;
1709 struct stat sb;
1711 if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1713 edit_error_dialog (_("Save as"),
1714 get_sys_error (_
1715 ("Cannot save: destination is not a regular file")));
1716 goto ret;
1719 different_filename = 1;
1720 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1722 if (file != -1)
1724 /* the file exists */
1725 mc_close (file);
1726 /* Overwrite the current file or cancel the operation */
1727 if (edit_query_dialog2
1728 (_("Warning"),
1729 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1730 goto ret;
1732 else
1734 edit->stat1.st_mode |= S_IWUSR;
1736 save_lock = lock_file (exp_vpath);
1738 else
1740 /* filenames equal, check if already locked */
1741 if (!edit->locked && !edit->delete_file)
1742 save_lock = lock_file (exp_vpath);
1745 if (different_filename)
1748 * Allow user to write into saved (under another name) file
1749 * even if original file had r/o user permissions.
1751 edit->stat1.st_mode |= S_IWRITE;
1754 rv = edit_save_file (edit, exp_vpath);
1755 switch (rv)
1757 case 1:
1758 /* Successful, so unlock both files */
1759 if (different_filename)
1761 if (save_lock)
1762 unlock_file (exp_vpath);
1763 if (edit->locked)
1764 edit->locked = unlock_file (edit->filename_vpath);
1766 else
1768 if (edit->locked || save_lock)
1769 edit->locked = unlock_file (edit->filename_vpath);
1772 edit_set_filename (edit, exp_vpath);
1773 if (edit->lb != LB_ASIS)
1774 edit_reload (edit, exp_vpath);
1775 edit->modified = 0;
1776 edit->delete_file = 0;
1777 if (different_filename)
1778 edit_load_syntax (edit, NULL, edit->syntax_type);
1779 vfs_path_free (exp_vpath);
1780 edit->force |= REDRAW_COMPLETELY;
1781 return TRUE;
1782 default:
1783 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1784 /* fallthrough */
1785 case -1:
1786 /* Failed, so maintain modify (not save) lock */
1787 if (save_lock)
1788 unlock_file (exp_vpath);
1789 break;
1794 ret:
1795 vfs_path_free (exp_vpath);
1796 edit->force |= REDRAW_COMPLETELY;
1797 return FALSE;
1800 /* {{{ Macro stuff starts here */
1801 /* --------------------------------------------------------------------------------------------- */
1803 void
1804 edit_delete_macro_cmd (WEdit * edit)
1806 int hotkey;
1808 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE);
1810 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1811 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1814 /* --------------------------------------------------------------------------------------------- */
1816 /** returns FALSE on error */
1817 gboolean
1818 edit_execute_macro (WEdit * edit, int hotkey)
1820 gboolean res = FALSE;
1822 if (hotkey != 0)
1824 const macros_t *macros;
1825 guint indx;
1827 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1828 macros->macro != NULL && macros->macro->len != 0)
1830 guint i;
1832 edit->force |= REDRAW_PAGE;
1834 for (i = 0; i < macros->macro->len; i++)
1836 const macro_action_t *m_act;
1838 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1839 edit_execute_cmd (edit, m_act->action, m_act->ch);
1840 res = TRUE;
1845 return res;
1848 /* --------------------------------------------------------------------------------------------- */
1850 /** returns FALSE on error */
1851 gboolean
1852 edit_store_macro_cmd (WEdit * edit)
1854 int i;
1855 int hotkey;
1856 GString *marcros_string;
1857 mc_config_t *macros_config = NULL;
1858 const char *section_name = "editor";
1859 gchar *macros_fname;
1860 GArray *macros; /* current macro */
1861 int tmp_act;
1862 gboolean have_macro = FALSE;
1863 char *skeyname = NULL;
1865 hotkey =
1866 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE);
1867 if (hotkey == ESC_CHAR)
1868 return FALSE;
1870 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1872 /* return FALSE if try assign macro into restricted hotkeys */
1873 if (tmp_act == CK_MacroStartRecord
1874 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1875 return FALSE;
1877 edit_delete_macro (edit, hotkey);
1879 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1880 macros_config = mc_config_init (macros_fname, FALSE);
1881 g_free (macros_fname);
1883 if (macros_config == NULL)
1884 return FALSE;
1886 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1888 marcros_string = g_string_sized_new (250);
1889 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1891 skeyname = lookup_key_by_code (hotkey);
1893 for (i = 0; i < macro_index; i++)
1895 macro_action_t m_act;
1896 const char *action_name;
1898 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1900 if (action_name == NULL)
1901 break;
1903 m_act.action = record_macro_buf[i].action;
1904 m_act.ch = record_macro_buf[i].ch;
1905 g_array_append_val (macros, m_act);
1906 have_macro = TRUE;
1907 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1908 (int) record_macro_buf[i].ch);
1910 if (have_macro)
1912 macros_t macro;
1913 macro.hotkey = hotkey;
1914 macro.macro = macros;
1915 g_array_append_val (macros_list, macro);
1916 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1918 else
1919 mc_config_del_key (macros_config, section_name, skeyname);
1921 g_free (skeyname);
1922 edit_macro_sort_by_hotkey ();
1924 g_string_free (marcros_string, TRUE);
1925 mc_config_save_file (macros_config, NULL);
1926 mc_config_deinit (macros_config);
1927 return TRUE;
1930 /* --------------------------------------------------------------------------------------------- */
1932 gboolean
1933 edit_repeat_macro_cmd (WEdit * edit)
1935 int i, j;
1936 char *f;
1937 long count_repeat;
1938 char *error = NULL;
1940 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL,
1941 INPUT_COMPLETE_NONE);
1942 if (f == NULL || *f == '\0')
1944 g_free (f);
1945 return FALSE;
1948 count_repeat = strtol (f, &error, 0);
1950 if (*error != '\0')
1952 g_free (f);
1953 return FALSE;
1956 g_free (f);
1958 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1959 edit->force |= REDRAW_PAGE;
1961 for (j = 0; j < count_repeat; j++)
1962 for (i = 0; i < macro_index; i++)
1963 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1964 edit_update_screen (edit);
1965 return TRUE;
1968 /* --------------------------------------------------------------------------------------------- */
1969 /** return FALSE on error */
1971 gboolean
1972 edit_load_macro_cmd (WEdit * edit)
1974 mc_config_t *macros_config = NULL;
1975 gchar **profile_keys, **keys;
1976 gchar **values, **curr_values;
1977 const char *section_name = "editor";
1978 gchar *macros_fname;
1980 (void) edit;
1982 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1983 macros_config = mc_config_init (macros_fname, TRUE);
1984 g_free (macros_fname);
1986 if (macros_config == NULL || macros_list == NULL || macros_list->len != 0)
1987 return FALSE;
1989 keys = mc_config_get_keys (macros_config, section_name, NULL);
1991 for (profile_keys = keys; *profile_keys != NULL; profile_keys++)
1993 int hotkey;
1994 gboolean have_macro = FALSE;
1995 GArray *macros;
1996 macros_t macro;
1998 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1999 values = mc_config_get_string_list (macros_config, section_name, *profile_keys, NULL);
2000 hotkey = lookup_key (*profile_keys, NULL);
2002 for (curr_values = values; *curr_values != NULL && *curr_values[0] != '\0'; curr_values++)
2004 char **macro_pair = NULL;
2006 macro_pair = g_strsplit (*curr_values, ":", 2);
2007 if (macro_pair != NULL)
2009 macro_action_t m_act;
2010 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
2011 m_act.action = 0;
2012 else
2014 m_act.action = keybind_lookup_action (macro_pair[0]);
2015 MC_PTR_FREE (macro_pair[0]);
2017 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
2018 m_act.ch = -1;
2019 else
2021 m_act.ch = strtol (macro_pair[1], NULL, 0);
2022 MC_PTR_FREE (macro_pair[1]);
2024 if (m_act.action != 0)
2026 /* a shell command */
2027 if ((m_act.action / CK_PipeBlock (0)) == 1)
2029 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
2030 m_act.ch = -1;
2032 g_array_append_val (macros, m_act);
2033 have_macro = TRUE;
2035 g_strfreev (macro_pair);
2036 macro_pair = NULL;
2039 if (have_macro)
2041 macro.hotkey = hotkey;
2042 macro.macro = macros;
2043 g_array_append_val (macros_list, macro);
2045 g_strfreev (values);
2047 g_strfreev (keys);
2048 mc_config_deinit (macros_config);
2049 edit_macro_sort_by_hotkey ();
2050 return TRUE;
2053 /* }}} Macro stuff end here */
2055 /* --------------------------------------------------------------------------------------------- */
2056 /** returns TRUE on success */
2058 gboolean
2059 edit_save_confirm_cmd (WEdit * edit)
2061 if (edit->filename_vpath == NULL)
2062 return edit_save_as_cmd (edit);
2064 if (!edit_check_newline (&edit->buffer))
2065 return FALSE;
2067 if (edit_confirm_save)
2069 char *f;
2070 gboolean ok;
2072 f = g_strdup_printf (_("Confirm save file: \"%s\""),
2073 vfs_path_as_str (edit->filename_vpath));
2074 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2075 g_free (f);
2076 if (!ok)
2077 return FALSE;
2079 return edit_save_cmd (edit);
2082 /* --------------------------------------------------------------------------------------------- */
2084 * Ask file to edit and load it.
2086 * @return TRUE on success or cancel of ask.
2089 gboolean
2090 edit_load_cmd (WDialog * h)
2092 char *exp;
2093 gboolean ret = TRUE; /* possible cancel */
2095 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2096 MC_HISTORY_EDIT_LOAD, INPUT_LAST_TEXT,
2097 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_CD);
2099 if (exp != NULL && *exp != '\0')
2101 vfs_path_t *exp_vpath;
2103 exp_vpath = vfs_path_from_str (exp);
2104 ret = edit_load_file_from_filename (h, exp_vpath);
2105 vfs_path_free (exp_vpath);
2108 g_free (exp);
2110 return ret;
2113 /* --------------------------------------------------------------------------------------------- */
2115 * Load syntax file to edit.
2117 * @return TRUE on success
2120 gboolean
2121 edit_load_syntax_file (WDialog * h)
2123 vfs_path_t *extdir_vpath;
2124 int dir = 0;
2125 gboolean ret = FALSE;
2127 if (geteuid () == 0)
2128 dir = query_dialog (_("Syntax file edit"),
2129 _("Which syntax file you want to edit?"), D_NORMAL, 2,
2130 _("&User"), _("&System wide"));
2132 extdir_vpath =
2133 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
2134 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
2136 vfs_path_free (extdir_vpath);
2137 extdir_vpath =
2138 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
2141 if (dir == 0)
2143 vfs_path_t *user_syntax_file_vpath;
2145 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
2146 check_for_default (extdir_vpath, user_syntax_file_vpath);
2147 ret = edit_load_file_from_filename (h, user_syntax_file_vpath);
2148 vfs_path_free (user_syntax_file_vpath);
2150 else if (dir == 1)
2151 ret = edit_load_file_from_filename (h, extdir_vpath);
2153 vfs_path_free (extdir_vpath);
2155 return ret;
2158 /* --------------------------------------------------------------------------------------------- */
2160 * Load menu file to edit.
2162 * @return TRUE on success
2165 gboolean
2166 edit_load_menu_file (WDialog * h)
2168 vfs_path_t *buffer_vpath;
2169 vfs_path_t *menufile_vpath;
2170 int dir;
2171 gboolean ret;
2173 query_set_sel (1);
2174 dir = query_dialog (_("Menu edit"),
2175 _("Which menu file do you want to edit?"), D_NORMAL,
2176 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2178 menufile_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2179 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
2181 vfs_path_free (menufile_vpath);
2182 menufile_vpath = vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2185 switch (dir)
2187 case 0:
2188 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
2189 check_for_default (menufile_vpath, buffer_vpath);
2190 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
2191 break;
2193 case 1:
2194 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
2195 check_for_default (menufile_vpath, buffer_vpath);
2196 break;
2198 case 2:
2199 buffer_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2200 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
2202 vfs_path_free (buffer_vpath);
2203 buffer_vpath =
2204 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2206 break;
2208 default:
2209 vfs_path_free (menufile_vpath);
2210 return FALSE;
2213 ret = edit_load_file_from_filename (h, buffer_vpath);
2215 vfs_path_free (buffer_vpath);
2216 vfs_path_free (menufile_vpath);
2218 return ret;
2221 /* --------------------------------------------------------------------------------------------- */
2223 * Close window with opened file.
2225 * @return TRUE if file was closed.
2228 gboolean
2229 edit_close_cmd (WEdit * edit)
2231 gboolean ret;
2233 ret = (edit != NULL) && edit_ok_to_exit (edit);
2235 if (ret)
2237 WDialog *h = WIDGET (edit)->owner;
2239 if (edit->locked != 0)
2240 unlock_file (edit->filename_vpath);
2242 del_widget (edit);
2244 if (edit_widget_is_editor (WIDGET (h->current->data)))
2245 edit = (WEdit *) h->current->data;
2246 else
2248 edit = find_editor (h);
2249 if (edit != NULL)
2250 dlg_set_top_widget (edit);
2254 if (edit != NULL)
2255 edit->force |= REDRAW_COMPLETELY;
2257 return ret;
2260 /* --------------------------------------------------------------------------------------------- */
2262 if mark2 is -1 then marking is from mark1 to the cursor.
2263 Otherwise its between the markers. This handles this.
2264 Returns FALSE if no text is marked.
2267 gboolean
2268 eval_marks (WEdit * edit, off_t * start_mark, off_t * end_mark)
2270 long end_mark_curs;
2272 if (edit->mark1 == edit->mark2)
2274 *start_mark = *end_mark = 0;
2275 edit->column2 = edit->column1 = 0;
2276 return FALSE;
2279 if (edit->end_mark_curs < 0)
2280 end_mark_curs = edit->buffer.curs1;
2281 else
2282 end_mark_curs = edit->end_mark_curs;
2284 if (edit->mark2 >= 0)
2286 *start_mark = min (edit->mark1, edit->mark2);
2287 *end_mark = max (edit->mark1, edit->mark2);
2289 else
2291 *start_mark = min (edit->mark1, end_mark_curs);
2292 *end_mark = max (edit->mark1, end_mark_curs);
2293 edit->column2 = edit->curs_col + edit->over_col;
2296 if (edit->column_highlight
2297 && ((edit->mark1 > end_mark_curs && edit->column1 < edit->column2)
2298 || (edit->mark1 < end_mark_curs && edit->column1 > edit->column2)))
2300 off_t start_bol, start_eol;
2301 off_t end_bol, end_eol;
2302 long col1, col2;
2303 off_t diff1, diff2;
2305 start_bol = edit_buffer_get_bol (&edit->buffer, *start_mark);
2306 start_eol = edit_buffer_get_eol (&edit->buffer, start_bol - 1) + 1;
2307 end_bol = edit_buffer_get_bol (&edit->buffer, *end_mark);
2308 end_eol = edit_buffer_get_eol (&edit->buffer, *end_mark);
2309 col1 = min (edit->column1, edit->column2);
2310 col2 = max (edit->column1, edit->column2);
2312 diff1 = edit_move_forward3 (edit, start_bol, col2, 0) -
2313 edit_move_forward3 (edit, start_bol, col1, 0);
2314 diff2 = edit_move_forward3 (edit, end_bol, col2, 0) -
2315 edit_move_forward3 (edit, end_bol, col1, 0);
2317 *start_mark -= diff1;
2318 *end_mark += diff2;
2319 *start_mark = max (*start_mark, start_eol);
2320 *end_mark = min (*end_mark, end_eol);
2322 return TRUE;
2325 /* --------------------------------------------------------------------------------------------- */
2327 void
2328 edit_block_copy_cmd (WEdit * edit)
2330 off_t start_mark, end_mark, current = edit->buffer.curs1;
2331 off_t mark1, mark2;
2332 long c1, c2;
2333 off_t size;
2334 unsigned char *copy_buf;
2336 edit_update_curs_col (edit);
2337 if (!eval_marks (edit, &start_mark, &end_mark))
2338 return;
2340 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2342 /* all that gets pushed are deletes hence little space is used on the stack */
2344 edit_push_markers (edit);
2346 if (edit->column_highlight)
2348 long col_delta;
2350 col_delta = labs (edit->column2 - edit->column1);
2351 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2353 else
2355 int size_orig = size;
2357 while (size-- != 0)
2358 edit_insert_ahead (edit, copy_buf[size]);
2360 /* Place cursor at the end of text selection */
2361 if (option_cursor_after_inserted_block)
2362 edit_cursor_move (edit, size_orig);
2365 g_free (copy_buf);
2366 edit_scroll_screen_over_cursor (edit);
2368 if (edit->column_highlight)
2369 edit_set_markers (edit, edit->buffer.curs1, mark2, c1, c2);
2370 else if (start_mark < current && end_mark > current)
2371 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2373 edit->force |= REDRAW_PAGE;
2377 /* --------------------------------------------------------------------------------------------- */
2379 void
2380 edit_block_move_cmd (WEdit * edit)
2382 off_t current;
2383 unsigned char *copy_buf = NULL;
2384 off_t start_mark, end_mark;
2386 if (!eval_marks (edit, &start_mark, &end_mark))
2387 return;
2389 if (!edit->column_highlight && edit->buffer.curs1 > start_mark && edit->buffer.curs1 < end_mark)
2390 return;
2392 if (edit->mark2 < 0)
2393 edit_mark_cmd (edit, FALSE);
2394 edit_push_markers (edit);
2396 if (edit->column_highlight)
2398 off_t mark1, mark2;
2399 off_t size;
2400 long c1, c2, b_width;
2401 long x, x2;
2403 c1 = min (edit->column1, edit->column2);
2404 c2 = max (edit->column1, edit->column2);
2405 b_width = c2 - c1;
2407 edit_update_curs_col (edit);
2409 x = edit->curs_col;
2410 x2 = x + edit->over_col;
2412 /* do nothing when cursor inside first line of selected area */
2413 if ((edit_buffer_get_eol (&edit->buffer, edit->buffer.curs1) ==
2414 edit_buffer_get_eol (&edit->buffer, start_mark)) && x2 > c1 && x2 <= c2)
2415 return;
2417 if (edit->buffer.curs1 > start_mark
2418 && edit->buffer.curs1 < edit_buffer_get_eol (&edit->buffer, end_mark))
2420 if (x > c2)
2421 x -= b_width;
2422 else if (x > c1 && x <= c2)
2423 x = c1;
2425 /* save current selection into buffer */
2426 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2428 /* remove current selection */
2429 edit_block_delete_cmd (edit);
2431 edit->over_col = max (0, edit->over_col - b_width);
2432 /* calculate the cursor pos after delete block */
2433 current = edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), x, 0);
2434 edit_cursor_move (edit, current - edit->buffer.curs1);
2435 edit_scroll_screen_over_cursor (edit);
2437 /* add TWS if need before block insertion */
2438 if (option_cursor_beyond_eol && edit->over_col > 0)
2439 edit_insert_over (edit);
2441 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2442 edit_set_markers (edit, mark1, mark2, c1, c2);
2444 else
2446 off_t count, count_orig;
2448 current = edit->buffer.curs1;
2449 copy_buf = g_malloc0 (end_mark - start_mark);
2450 edit_cursor_move (edit, start_mark - edit->buffer.curs1);
2451 edit_scroll_screen_over_cursor (edit);
2453 for (count = start_mark; count < end_mark; count++)
2454 copy_buf[end_mark - count - 1] = edit_delete (edit, TRUE);
2456 edit_scroll_screen_over_cursor (edit);
2457 edit_cursor_move (edit,
2458 current - edit->buffer.curs1 -
2459 (((current - edit->buffer.curs1) > 0) ? end_mark - start_mark : 0));
2460 edit_scroll_screen_over_cursor (edit);
2461 count_orig = count;
2462 while (count-- > start_mark)
2463 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2465 edit_set_markers (edit, edit->buffer.curs1, edit->buffer.curs1 + end_mark - start_mark, 0,
2468 /* Place cursor at the end of text selection */
2469 if (option_cursor_after_inserted_block)
2470 edit_cursor_move (edit, count_orig - start_mark);
2473 edit_scroll_screen_over_cursor (edit);
2474 g_free (copy_buf);
2475 edit->force |= REDRAW_PAGE;
2478 /* --------------------------------------------------------------------------------------------- */
2479 /** returns 1 if canceelled by user */
2482 edit_block_delete_cmd (WEdit * edit)
2484 off_t start_mark, end_mark;
2486 if (eval_marks (edit, &start_mark, &end_mark))
2487 return edit_block_delete (edit);
2489 edit_delete_line (edit);
2490 return 0;
2493 /* --------------------------------------------------------------------------------------------- */
2494 /** call with edit = 0 before shutdown to close memory leaks */
2496 void
2497 edit_replace_cmd (WEdit * edit, int again)
2499 /* 1 = search string, 2 = replace with */
2500 static char *saved1 = NULL; /* saved default[123] */
2501 static char *saved2 = NULL;
2502 char *input1 = NULL; /* user input from the dialog */
2503 char *input2 = NULL;
2504 GString *input2_str = NULL;
2505 char *disp1 = NULL;
2506 char *disp2 = NULL;
2507 long times_replaced = 0;
2508 gboolean once_found = FALSE;
2509 edit_search_status_msg_t esm;
2511 if (edit == NULL)
2513 MC_PTR_FREE (saved1);
2514 MC_PTR_FREE (saved2);
2515 return;
2518 edit->force |= REDRAW_COMPLETELY;
2520 if (again && !saved1 && !saved2)
2521 again = 0;
2523 if (again)
2525 input1 = g_strdup (saved1 ? saved1 : "");
2526 input2 = g_strdup (saved2 ? saved2 : "");
2528 else
2530 char *tmp_inp1, *tmp_inp2;
2532 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
2533 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
2535 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2537 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2539 g_free (disp1);
2540 g_free (disp2);
2542 if (input1 == NULL || *input1 == '\0')
2544 edit->force = REDRAW_COMPLETELY;
2545 goto cleanup;
2548 tmp_inp1 = input1;
2549 tmp_inp2 = input2;
2550 input1 = edit_replace_cmd__conv_to_input (input1);
2551 input2 = edit_replace_cmd__conv_to_input (input2);
2552 g_free (tmp_inp1);
2553 g_free (tmp_inp2);
2555 g_free (saved1), saved1 = g_strdup (input1);
2556 g_free (saved2), saved2 = g_strdup (input2);
2558 mc_search_free (edit->search);
2559 edit->search = NULL;
2562 input2_str = g_string_new (input2);
2564 if (edit->search == NULL)
2566 #ifdef HAVE_CHARSET
2567 edit->search = mc_search_new (input1, -1, cp_source);
2568 #else
2569 edit->search = mc_search_new (input1, -1, NULL);
2570 #endif
2571 if (edit->search == NULL)
2573 edit->search_start = edit->buffer.curs1;
2574 goto cleanup;
2576 edit->search->search_type = edit_search_options.type;
2577 #ifdef HAVE_CHARSET
2578 edit->search->is_all_charsets = edit_search_options.all_codepages;
2579 #endif
2580 edit->search->is_case_sensitive = edit_search_options.case_sens;
2581 edit->search->whole_words = edit_search_options.whole_words;
2582 edit->search->search_fn = edit_search_cmd_callback;
2583 edit->search->update_fn = edit_search_update_callback;
2584 edit->search_line_type = edit_get_search_line_type (edit->search);
2585 edit_search_fix_search_start_if_selection (edit);
2588 if (edit->found_len && edit->search_start == edit->found_start + 1
2589 && edit_search_options.backwards)
2590 edit->search_start--;
2592 if (edit->found_len && edit->search_start == edit->found_start - 1
2593 && !edit_search_options.backwards)
2594 edit->search_start++;
2596 esm.first = TRUE;
2597 esm.edit = edit;
2598 esm.offset = edit->search_start;
2600 status_msg_init (STATUS_MSG (&esm), _("Search"), 1.0, simple_status_msg_init_cb,
2601 edit_search_status_update_cb, NULL);
2605 gsize len = 0;
2607 if (!editcmd_find (&esm, &len))
2609 if (!(edit->search->error == MC_SEARCH_E_OK ||
2610 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2612 edit_query_dialog (_("Search"), edit->search->error_str);
2614 break;
2616 once_found = TRUE;
2618 edit->search_start = edit->search->normal_offset;
2619 /*returns negative on not found or error in pattern */
2621 if ((edit->search_start >= 0) && (edit->search_start < edit->buffer.size))
2623 gsize i;
2624 GString *repl_str;
2626 edit->found_start = edit->search_start;
2627 i = edit->found_len = len;
2629 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
2630 edit_scroll_screen_over_cursor (edit);
2632 if (edit->replace_mode == 0)
2634 long l;
2635 int prompt;
2637 l = edit->curs_row - WIDGET (edit)->lines / 3;
2638 if (l > 0)
2639 edit_scroll_downward (edit, l);
2640 if (l < 0)
2641 edit_scroll_upward (edit, -l);
2643 edit_scroll_screen_over_cursor (edit);
2644 edit->force |= REDRAW_PAGE;
2645 edit_render_keypress (edit);
2647 /*so that undo stops at each query */
2648 edit_push_key_press (edit);
2649 /* and prompt 2/3 down */
2650 disp1 = edit_replace_cmd__conv_to_display (saved1);
2651 disp2 = edit_replace_cmd__conv_to_display (saved2);
2652 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2653 g_free (disp1);
2654 g_free (disp2);
2656 if (prompt == B_REPLACE_ALL)
2657 edit->replace_mode = 1;
2658 else if (prompt == B_SKIP_REPLACE)
2660 if (edit_search_options.backwards)
2661 edit->search_start--;
2662 else
2663 edit->search_start++;
2664 continue; /* loop */
2666 else if (prompt == B_CANCEL)
2668 edit->replace_mode = -1;
2669 break; /* loop */
2673 repl_str = mc_search_prepare_replace_str (edit->search, input2_str);
2675 if (edit->search->error != MC_SEARCH_E_OK)
2677 edit_error_dialog (_("Replace"), edit->search->error_str);
2678 g_string_free (repl_str, TRUE);
2679 break;
2682 /* delete then insert new */
2683 for (i = 0; i < len; i++)
2684 edit_delete (edit, TRUE);
2686 for (i = 0; i < repl_str->len; i++)
2687 edit_insert (edit, repl_str->str[i]);
2689 edit->found_len = repl_str->len;
2690 g_string_free (repl_str, TRUE);
2691 times_replaced++;
2693 /* so that we don't find the same string again */
2694 if (edit_search_options.backwards)
2696 edit->search_start--;
2698 else
2700 edit->search_start += edit->found_len + (len == 0 ? 1 : 0);
2702 if (edit->search_start >= edit->buffer.size)
2703 break;
2706 edit_scroll_screen_over_cursor (edit);
2708 else
2710 /* try and find from right here for next search */
2711 edit->search_start = edit->buffer.curs1;
2712 edit_update_curs_col (edit);
2714 edit->force |= REDRAW_PAGE;
2715 edit_render_keypress (edit);
2717 if (times_replaced == 0)
2718 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL, 1, _("&OK"));
2719 break;
2722 while (edit->replace_mode >= 0);
2724 status_msg_deinit (STATUS_MSG (&esm));
2725 edit_scroll_screen_over_cursor (edit);
2726 edit->force |= REDRAW_COMPLETELY;
2727 edit_render_keypress (edit);
2729 if ((edit->replace_mode == 1) && (times_replaced != 0))
2730 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2732 cleanup:
2733 g_free (input1);
2734 g_free (input2);
2735 if (input2_str != NULL)
2736 g_string_free (input2_str, TRUE);
2739 /* --------------------------------------------------------------------------------------------- */
2741 mc_search_cbret_t
2742 edit_search_cmd_callback (const void *user_data, gsize char_offset, int *current_char)
2744 WEdit *edit = ((edit_search_status_msg_t *) user_data)->edit;
2746 *current_char = edit_buffer_get_byte (&edit->buffer, (off_t) char_offset);
2747 return MC_SEARCH_CB_OK;
2750 /* --------------------------------------------------------------------------------------------- */
2752 mc_search_cbret_t
2753 edit_search_update_callback (const void *user_data, gsize char_offset)
2755 status_msg_t *sm = STATUS_MSG (user_data);
2757 ((edit_search_status_msg_t *) sm)->offset = (off_t) char_offset;
2759 return (sm->update (sm) == B_CANCEL ? MC_SEARCH_CB_ABORT : MC_SEARCH_CB_OK);
2762 /* --------------------------------------------------------------------------------------------- */
2764 void
2765 edit_search_cmd (WEdit * edit, gboolean again)
2768 if (edit == NULL)
2769 return;
2771 if (!again)
2772 edit_search (edit);
2773 else if (edit->last_search_string != NULL)
2774 edit_do_search (edit);
2775 else
2777 /* find last search string in history */
2778 GList *history;
2780 history = history_get (MC_HISTORY_SHARED_SEARCH);
2781 if (history != NULL && history->data != NULL)
2783 edit->last_search_string = (char *) history->data;
2784 history->data = NULL;
2785 history = g_list_first (history);
2786 g_list_free_full (history, g_free);
2788 #ifdef HAVE_CHARSET
2789 edit->search = mc_search_new (edit->last_search_string, -1, cp_source);
2790 #else
2791 edit->search = mc_search_new (edit->last_search_string, -1, NULL);
2792 #endif
2793 if (edit->search == NULL)
2795 /* if not... then ask for an expression */
2796 MC_PTR_FREE (edit->last_search_string);
2797 edit_search (edit);
2799 else
2801 edit->search->search_type = edit_search_options.type;
2802 #ifdef HAVE_CHARSET
2803 edit->search->is_all_charsets = edit_search_options.all_codepages;
2804 #endif
2805 edit->search->is_case_sensitive = edit_search_options.case_sens;
2806 edit->search->whole_words = edit_search_options.whole_words;
2807 edit->search->search_fn = edit_search_cmd_callback;
2808 edit->search->update_fn = edit_search_update_callback;
2809 edit->search_line_type = edit_get_search_line_type (edit->search);
2810 edit_do_search (edit);
2813 else
2815 /* if not... then ask for an expression */
2816 MC_PTR_FREE (edit->last_search_string);
2817 edit_search (edit);
2823 /* --------------------------------------------------------------------------------------------- */
2825 * Check if it's OK to close the file. If there are unsaved changes, ask user.
2827 * @return TRUE if it's OK to exit, FALSE to continue editing.
2830 gboolean
2831 edit_ok_to_exit (WEdit * edit)
2833 const char *fname = N_("[NoName]");
2834 char *msg;
2835 int act;
2837 if (!edit->modified)
2838 return TRUE;
2840 if (edit->filename_vpath != NULL)
2841 fname = vfs_path_as_str (edit->filename_vpath);
2842 #ifdef ENABLE_NLS
2843 else
2844 fname = _(fname);
2845 #endif
2847 if (!mc_global.midnight_shutdown)
2849 query_set_sel (2);
2851 msg = g_strdup_printf (_("File %s was modified.\nSave before close?"), fname);
2852 act = edit_query_dialog3 (_("Close file"), msg, _("&Yes"), _("&No"), _("&Cancel"));
2854 else
2856 msg = g_strdup_printf (_("Midnight Commander is being shut down.\nSave modified file %s?"),
2857 fname);
2858 act = edit_query_dialog2 (_("Quit"), msg, _("&Yes"), _("&No"));
2860 /* Esc is No */
2861 if (act == -1)
2862 act = 1;
2865 g_free (msg);
2867 switch (act)
2869 case 0: /* Yes */
2870 if (!mc_global.midnight_shutdown && !edit_check_newline (&edit->buffer))
2871 return FALSE;
2872 edit_push_markers (edit);
2873 edit_set_markers (edit, 0, 0, 0, 0);
2874 if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2875 return mc_global.midnight_shutdown;
2876 break;
2877 case 1: /* No */
2878 default:
2879 break;
2880 case 2: /* Cancel quit */
2881 case -1: /* Esc */
2882 return FALSE;
2885 return TRUE;
2888 /* --------------------------------------------------------------------------------------------- */
2889 /** save block, returns TRUE on success */
2891 gboolean
2892 edit_save_block (WEdit * edit, const char *filename, off_t start, off_t finish)
2894 int file;
2895 off_t len = 1;
2896 vfs_path_t *vpath;
2898 vpath = vfs_path_from_str (filename);
2899 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2900 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2901 vfs_path_free (vpath);
2902 if (file == -1)
2903 return FALSE;
2905 if (edit->column_highlight)
2907 int r;
2909 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2910 if (r > 0)
2912 unsigned char *block, *p;
2914 p = block = edit_get_block (edit, start, finish, &len);
2915 while (len)
2917 r = mc_write (file, p, len);
2918 if (r < 0)
2919 break;
2920 p += r;
2921 len -= r;
2923 g_free (block);
2926 else
2928 unsigned char *buf;
2929 off_t i = start;
2930 off_t end;
2932 len = finish - start;
2933 buf = g_malloc0 (TEMP_BUF_LEN);
2934 while (start != finish)
2936 end = min (finish, start + TEMP_BUF_LEN);
2937 for (; i < end; i++)
2938 buf[i - start] = edit_buffer_get_byte (&edit->buffer, i);
2939 len -= mc_write (file, (char *) buf, end - start);
2940 start = end;
2942 g_free (buf);
2944 mc_close (file);
2946 return (len == 0);
2949 /* --------------------------------------------------------------------------------------------- */
2951 void
2952 edit_paste_from_history (WEdit * edit)
2954 (void) edit;
2955 edit_error_dialog (_("Error"), _("This function is not implemented"));
2958 /* --------------------------------------------------------------------------------------------- */
2960 gboolean
2961 edit_copy_to_X_buf_cmd (WEdit * edit)
2963 off_t start_mark, end_mark;
2965 if (!eval_marks (edit, &start_mark, &end_mark))
2966 return TRUE;
2968 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2970 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2971 return FALSE;
2973 /* try use external clipboard utility */
2974 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2976 if (option_drop_selection_on_copy)
2977 edit_mark_cmd (edit, TRUE);
2979 return TRUE;
2982 /* --------------------------------------------------------------------------------------------- */
2984 gboolean
2985 edit_cut_to_X_buf_cmd (WEdit * edit)
2987 off_t start_mark, end_mark;
2989 if (!eval_marks (edit, &start_mark, &end_mark))
2990 return TRUE;
2992 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2994 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2995 return FALSE;
2997 /* try use external clipboard utility */
2998 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
3000 edit_block_delete_cmd (edit);
3001 edit_mark_cmd (edit, TRUE);
3003 return TRUE;
3006 /* --------------------------------------------------------------------------------------------- */
3008 gboolean
3009 edit_paste_from_X_buf_cmd (WEdit * edit)
3011 vfs_path_t *tmp;
3012 gboolean ret;
3014 /* try use external clipboard utility */
3015 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
3016 tmp = mc_config_get_full_vpath (EDIT_CLIP_FILE);
3017 ret = (edit_insert_file (edit, tmp) >= 0);
3018 vfs_path_free (tmp);
3020 return ret;
3023 /* --------------------------------------------------------------------------------------------- */
3025 * Ask user for the line and go to that line.
3026 * Negative numbers mean line from the end (i.e. -1 is the last line).
3029 void
3030 edit_goto_cmd (WEdit * edit)
3032 static gboolean first_run = TRUE;
3034 char *f;
3035 long l;
3036 char *error;
3038 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE,
3039 first_run ? NULL : INPUT_LAST_TEXT, INPUT_COMPLETE_NONE);
3040 if (f == NULL || *f == '\0')
3042 g_free (f);
3043 return;
3046 l = strtol (f, &error, 0);
3047 if (*error != '\0')
3049 g_free (f);
3050 return;
3053 if (l < 0)
3054 l = edit->buffer.lines + l + 2;
3055 edit_move_display (edit, l - WIDGET (edit)->lines / 2 - 1);
3056 edit_move_to_line (edit, l - 1);
3057 edit->force |= REDRAW_COMPLETELY;
3058 g_free (f);
3060 first_run = FALSE;
3064 /* --------------------------------------------------------------------------------------------- */
3065 /** Return TRUE on success */
3067 gboolean
3068 edit_save_block_cmd (WEdit * edit)
3070 off_t start_mark, end_mark;
3071 char *exp, *tmp;
3072 gboolean ret = FALSE;
3074 if (!eval_marks (edit, &start_mark, &end_mark))
3075 return TRUE;
3077 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3078 exp =
3079 input_expand_dialog (_("Save block"), _("Enter file name:"),
3080 MC_HISTORY_EDIT_SAVE_BLOCK, tmp, INPUT_COMPLETE_FILENAMES);
3081 g_free (tmp);
3082 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3084 if (exp != NULL && *exp != '\0')
3086 if (edit_save_block (edit, exp, start_mark, end_mark))
3087 ret = TRUE;
3088 else
3089 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3091 edit->force |= REDRAW_COMPLETELY;
3094 g_free (exp);
3096 return ret;
3100 /* --------------------------------------------------------------------------------------------- */
3102 /** returns TRUE on success */
3103 gboolean
3104 edit_insert_file_cmd (WEdit * edit)
3106 char *tmp;
3107 char *exp;
3108 gboolean ret = FALSE;
3110 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3111 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3112 MC_HISTORY_EDIT_INSERT_FILE, tmp, INPUT_COMPLETE_FILENAMES);
3113 g_free (tmp);
3115 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3117 if (exp != NULL && *exp != '\0')
3119 vfs_path_t *exp_vpath;
3121 exp_vpath = vfs_path_from_str (exp);
3122 ret = (edit_insert_file (edit, exp_vpath) >= 0);
3123 vfs_path_free (exp_vpath);
3125 if (!ret)
3126 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3129 g_free (exp);
3131 edit->force |= REDRAW_COMPLETELY;
3132 return ret;
3135 /* --------------------------------------------------------------------------------------------- */
3136 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3139 edit_sort_cmd (WEdit * edit)
3141 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3142 off_t start_mark, end_mark;
3143 int e;
3145 if (!eval_marks (edit, &start_mark, &end_mark))
3147 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3148 return 0;
3151 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
3152 edit_save_block (edit, tmp, start_mark, end_mark);
3153 g_free (tmp);
3155 exp = input_dialog (_("Run sort"),
3156 _("Enter sort options (see manpage) separated by whitespace:"),
3157 MC_HISTORY_EDIT_SORT, INPUT_LAST_TEXT, INPUT_COMPLETE_NONE);
3159 if (exp == NULL)
3160 return 1;
3162 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
3163 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
3164 tmp =
3165 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3166 " > ", tmp_edit_temp_name, (char *) NULL);
3167 g_free (tmp_edit_temp_name);
3168 g_free (tmp_edit_block_name);
3170 e = system (tmp);
3171 g_free (tmp);
3172 if (e != 0)
3174 if (e == -1 || e == 127)
3175 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3176 else
3178 char q[8];
3180 sprintf (q, "%d ", e);
3181 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3182 edit_error_dialog (_("Sort"), tmp);
3183 g_free (tmp);
3185 return -1;
3188 edit->force |= REDRAW_COMPLETELY;
3190 if (edit_block_delete_cmd (edit))
3191 return 1;
3194 vfs_path_t *tmp_vpath;
3196 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3197 edit_insert_file (edit, tmp_vpath);
3198 vfs_path_free (tmp_vpath);
3200 return 0;
3203 /* --------------------------------------------------------------------------------------------- */
3205 * Ask user for a command, execute it and paste its output back to the
3206 * editor.
3210 edit_ext_cmd (WEdit * edit)
3212 char *exp, *tmp, *tmp_edit_temp_file;
3213 int e;
3215 exp =
3216 input_dialog (_("Paste output of external command"),
3217 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, INPUT_LAST_TEXT,
3218 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_VARIABLES | INPUT_COMPLETE_USERNAMES
3219 | INPUT_COMPLETE_HOSTNAMES | INPUT_COMPLETE_CD | INPUT_COMPLETE_COMMANDS |
3220 INPUT_COMPLETE_SHELL_ESC);
3222 if (!exp)
3223 return 1;
3225 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3226 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3227 g_free (tmp_edit_temp_file);
3228 e = system (tmp);
3229 g_free (tmp);
3230 g_free (exp);
3232 if (e)
3234 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3235 return -1;
3238 edit->force |= REDRAW_COMPLETELY;
3241 vfs_path_t *tmp_vpath;
3243 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3244 edit_insert_file (edit, tmp_vpath);
3245 vfs_path_free (tmp_vpath);
3247 return 0;
3250 /* --------------------------------------------------------------------------------------------- */
3251 /** if block is 1, a block must be highlighted and the shell command
3252 processes it. If block is 0 the shell command is a straight system
3253 command, that just produces some output which is to be inserted */
3255 void
3256 edit_block_process_cmd (WEdit * edit, int macro_number)
3258 char *fname;
3259 char *macros_fname = NULL;
3261 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3262 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3263 user_menu (edit, macros_fname, 0);
3264 g_free (fname);
3265 g_free (macros_fname);
3266 edit->force |= REDRAW_COMPLETELY;
3269 /* --------------------------------------------------------------------------------------------- */
3271 void
3272 edit_mail_dialog (WEdit * edit)
3274 char *mail_to, *mail_subject, *mail_cc;
3276 quick_widget_t quick_widgets[] = {
3277 /* *INDENT-OFF* */
3278 QUICK_LABEL (N_("mail -s <subject> -c <cc> <to>"), NULL),
3279 QUICK_LABELED_INPUT (N_("To"), input_label_above,
3280 INPUT_LAST_TEXT, "mail-dlg-input-3",
3281 &mail_to, NULL, FALSE, FALSE, INPUT_COMPLETE_USERNAMES),
3282 QUICK_LABELED_INPUT (N_("Subject"), input_label_above,
3283 INPUT_LAST_TEXT, "mail-dlg-input-2",
3284 &mail_subject, NULL, FALSE, FALSE, INPUT_COMPLETE_NONE),
3285 QUICK_LABELED_INPUT (N_("Copies to"), input_label_above,
3286 INPUT_LAST_TEXT, "mail-dlg-input",
3287 &mail_cc, NULL, FALSE, FALSE, INPUT_COMPLETE_USERNAMES),
3288 QUICK_BUTTONS_OK_CANCEL,
3289 QUICK_END
3290 /* *INDENT-ON* */
3293 quick_dialog_t qdlg = {
3294 -1, -1, 50,
3295 N_("Mail"), "[Input Line Keys]",
3296 quick_widgets, NULL, NULL
3299 if (quick_dialog (&qdlg) != B_CANCEL)
3301 pipe_mail (&edit->buffer, mail_to, mail_subject, mail_cc);
3302 g_free (mail_to);
3303 g_free (mail_subject);
3304 g_free (mail_cc);
3308 /* --------------------------------------------------------------------------------------------- */
3310 /*******************/
3311 /* Word Completion */
3312 /*******************/
3315 * Complete current word using regular expression search
3316 * backwards beginning at the current cursor position.
3319 void
3320 edit_complete_word_cmd (WEdit * edit)
3322 gsize i, max_len, word_len = 0, num_compl = 0;
3323 off_t word_start = 0;
3324 GString *match_expr;
3325 GString *compl[MAX_WORD_COMPLETIONS]; /* completions */
3327 /* search start of word to be completed */
3328 if (!edit_find_word_start (&edit->buffer, &word_start, &word_len))
3329 return;
3331 /* prepare match expression */
3332 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3333 match_expr = g_string_new ("(^|\\s+|\\b)");
3334 for (i = 0; i < word_len; i++)
3335 g_string_append_c (match_expr, edit_buffer_get_byte (&edit->buffer, word_start + i));
3336 g_string_append (match_expr,
3337 "[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+");
3339 /* collect the possible completions */
3340 /* start search from begin to end of file */
3341 max_len =
3342 edit_collect_completions (edit, word_start, word_len, match_expr->str, (GString **) & compl,
3343 &num_compl);
3345 if (num_compl > 0)
3347 /* insert completed word if there is only one match */
3348 if (num_compl == 1)
3349 edit_complete_word_insert_recoded_completion (edit, compl[0]->str, word_len);
3350 /* more than one possible completion => ask the user */
3351 else
3353 char *curr_compl;
3355 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3356 /* !!! pressed again the selection dialog pops up, but that !!! */
3357 /* !!! seems to require a further internal state !!! */
3358 /*tty_beep (); */
3360 /* let the user select the preferred completion */
3361 curr_compl = editcmd_dialog_completion_show (edit, max_len,
3362 (GString **) & compl, num_compl);
3364 if (curr_compl != NULL)
3366 edit_complete_word_insert_recoded_completion (edit, curr_compl, word_len);
3367 g_free (curr_compl);
3372 g_string_free (match_expr, TRUE);
3373 /* release memory before return */
3374 for (i = 0; i < num_compl; i++)
3375 g_string_free (compl[i], TRUE);
3378 /* --------------------------------------------------------------------------------------------- */
3380 #ifdef HAVE_CHARSET
3381 void
3382 edit_select_codepage_cmd (WEdit * edit)
3384 if (do_select_codepage ())
3385 edit_set_codeset (edit);
3387 edit->force = REDRAW_PAGE;
3388 widget_redraw (WIDGET (edit));
3390 #endif
3392 /* --------------------------------------------------------------------------------------------- */
3394 void
3395 edit_insert_literal_cmd (WEdit * edit)
3397 int char_for_insertion;
3399 char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3400 _("Press any key:"), FALSE);
3401 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3404 /* --------------------------------------------------------------------------------------------- */
3406 void
3407 edit_begin_end_macro_cmd (WEdit * edit)
3409 /* edit is a pointer to the widget */
3410 if (edit != NULL)
3412 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3413 edit_execute_key_command (edit, command, -1);
3417 /* --------------------------------------------------------------------------------------------- */
3419 void
3420 edit_begin_end_repeat_cmd (WEdit * edit)
3422 /* edit is a pointer to the widget */
3423 if (edit != NULL)
3425 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3426 edit_execute_key_command (edit, command, -1);
3430 /* --------------------------------------------------------------------------------------------- */
3432 gboolean
3433 edit_load_forward_cmd (WEdit * edit)
3435 if (edit->modified
3436 && edit_query_dialog2 (_("Warning"),
3437 _("Current text was modified without a file save.\n"
3438 "Continue discards these changes"), _("C&ontinue"),
3439 _("&Cancel")) == 1)
3441 edit->force |= REDRAW_COMPLETELY;
3442 return TRUE;
3445 if (edit_stack_iterator + 1 >= MAX_HISTORY_MOVETO)
3446 return FALSE;
3448 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3449 return FALSE;
3451 edit_stack_iterator++;
3452 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3453 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3454 edit_history_moveto[edit_stack_iterator].line);
3456 return FALSE;
3459 /* --------------------------------------------------------------------------------------------- */
3461 gboolean
3462 edit_load_back_cmd (WEdit * edit)
3464 if (edit->modified
3465 && edit_query_dialog2 (_("Warning"),
3466 _("Current text was modified without a file save.\n"
3467 "Continue discards these changes"), _("C&ontinue"),
3468 _("&Cancel")) == 1)
3470 edit->force |= REDRAW_COMPLETELY;
3471 return TRUE;
3474 /* we are in the bottom of the stack, NO WAY! */
3475 if (edit_stack_iterator == 0)
3476 return FALSE;
3478 edit_stack_iterator--;
3479 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3480 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3481 edit_history_moveto[edit_stack_iterator].line);
3483 return FALSE;
3486 /* --------------------------------------------------------------------------------------------- */
3488 void
3489 edit_get_match_keyword_cmd (WEdit * edit)
3491 gsize word_len = 0, max_len = 0;
3492 int num_def = 0;
3493 gsize i;
3494 off_t word_start = 0;
3495 GString *match_expr;
3496 char *path = NULL;
3497 char *ptr = NULL;
3498 char *tagfile = NULL;
3500 etags_hash_t def_hash[MAX_DEFINITIONS];
3502 for (i = 0; i < MAX_DEFINITIONS; i++)
3504 def_hash[i].filename = NULL;
3507 /* search start of word to be completed */
3508 if (!edit_find_word_start (&edit->buffer, &word_start, &word_len))
3509 return;
3511 /* prepare match expression */
3512 match_expr = g_string_sized_new (word_len);
3513 for (i = 0; i < word_len; i++)
3514 g_string_append_c (match_expr, edit_buffer_get_byte (&edit->buffer, word_start + i));
3516 ptr = g_get_current_dir ();
3517 path = g_strconcat (ptr, PATH_SEP_STR, (char *) NULL);
3518 g_free (ptr);
3520 /* Recursive search file 'TAGS' in parent dirs */
3523 ptr = g_path_get_dirname (path);
3524 g_free (path);
3525 path = ptr;
3526 g_free (tagfile);
3527 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3528 if (exist_file (tagfile))
3529 break;
3531 while (strcmp (path, PATH_SEP_STR) != 0);
3533 if (tagfile)
3535 num_def =
3536 etags_set_definition_hash (tagfile, path, match_expr->str, (etags_hash_t *) & def_hash);
3537 g_free (tagfile);
3539 g_free (path);
3541 max_len = MAX_WIDTH_DEF_DIALOG;
3542 word_len = 0;
3543 if (num_def > 0)
3545 editcmd_dialog_select_definition_show (edit, match_expr->str, max_len, word_len,
3546 (etags_hash_t *) & def_hash, num_def);
3548 g_string_free (match_expr, TRUE);
3551 /* --------------------------------------------------------------------------------------------- */
3553 #ifdef HAVE_ASPELL
3555 edit_suggest_current_word (WEdit * edit)
3557 gsize cut_len = 0;
3558 gsize word_len = 0;
3559 off_t word_start = 0;
3560 int retval = B_SKIP_WORD;
3561 GString *match_word;
3563 /* search start of word to spell check */
3564 match_word = edit_buffer_get_word_from_pos (&edit->buffer, edit->buffer.curs1, &word_start,
3565 &cut_len);
3566 word_len = match_word->len;
3568 #ifdef HAVE_CHARSET
3569 if (mc_global.source_codepage >= 0 && (mc_global.source_codepage != mc_global.display_codepage))
3571 GString *tmp_word;
3573 tmp_word = str_convert_to_display (match_word->str);
3574 g_string_free (match_word, TRUE);
3575 match_word = tmp_word;
3577 #endif
3578 if (!aspell_check (match_word->str, (int) word_len))
3580 GArray *suggest;
3581 unsigned int res;
3583 suggest = g_array_new (TRUE, FALSE, sizeof (char *));
3585 res = aspell_suggest (suggest, match_word->str, (int) word_len);
3586 if (res != 0)
3588 char *new_word = NULL;
3590 edit->found_start = word_start;
3591 edit->found_len = word_len;
3592 edit->force |= REDRAW_PAGE;
3593 edit_scroll_screen_over_cursor (edit);
3594 edit_render_keypress (edit);
3596 retval = spell_dialog_spell_suggest_show (edit, match_word->str, &new_word, suggest);
3597 edit_cursor_move (edit, word_len - cut_len);
3599 if (retval == B_ENTER && new_word != NULL)
3601 guint i;
3602 char *cp_word;
3604 #ifdef HAVE_CHARSET
3605 if (mc_global.source_codepage >= 0 &&
3606 (mc_global.source_codepage != mc_global.display_codepage))
3608 GString *tmp_word;
3610 tmp_word = str_convert_to_input (new_word);
3611 g_free (new_word);
3612 new_word = g_string_free (tmp_word, FALSE);
3614 #endif
3615 cp_word = new_word;
3616 for (i = 0; i < word_len; i++)
3617 edit_backspace (edit, TRUE);
3618 for (; *new_word; new_word++)
3619 edit_insert (edit, *new_word);
3620 g_free (cp_word);
3622 else if (retval == B_ADD_WORD && match_word != NULL)
3623 aspell_add_to_dict (match_word->str, (int) word_len);
3626 g_array_free (suggest, TRUE);
3627 edit->found_start = 0;
3628 edit->found_len = 0;
3630 g_string_free (match_word, TRUE);
3631 return retval;
3634 /* --------------------------------------------------------------------------------------------- */
3636 void
3637 edit_spellcheck_file (WEdit * edit)
3639 if (edit->buffer.curs_line > 0)
3641 edit_cursor_move (edit, -edit->buffer.curs1);
3642 edit_move_to_prev_col (edit, 0);
3643 edit_update_curs_row (edit);
3648 int c1, c2;
3650 c2 = edit_buffer_get_current_byte (&edit->buffer);
3654 if (edit->buffer.curs1 >= edit->buffer.size)
3655 return;
3657 c1 = c2;
3658 edit_cursor_move (edit, 1);
3659 c2 = edit_buffer_get_current_byte (&edit->buffer);
3661 while (is_break_char (c1) || is_break_char (c2));
3663 while (edit_suggest_current_word (edit) != B_CANCEL);
3666 /* --------------------------------------------------------------------------------------------- */
3668 void
3669 edit_set_spell_lang (void)
3671 GArray *lang_list;
3673 lang_list = g_array_new (TRUE, FALSE, sizeof (char *));
3674 if (aspell_get_lang_list (lang_list) != 0)
3676 char *lang;
3678 lang = spell_dialog_lang_list_show (lang_list);
3679 if (lang != NULL)
3680 (void) aspell_set_lang (lang);
3682 aspell_array_clean (lang_list);
3684 #endif /* HAVE_ASPELL */
3686 /* --------------------------------------------------------------------------------------------- */