Unification of widget and dialog callback functions.
[midnight-commander.git] / src / editor / editcmd.c
blob6f39e8b14ee38f8098d0fb10518fa8643e717d35
1 /*
2 Editor high level editing commands
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2011, 2012
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer, 1996, 1997
10 Andrew Borodin <aborodin@vmail.ru> 2012
11 Ilia Maslakov <il.smind@gmail.com> 2012
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /** \file
30 * \brief Source: editor high level editing commands
31 * \author Paul Sheer
32 * \date 1996, 1997
35 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
37 #include <config.h>
39 #ifdef HAVE_ASSERT_H
40 #include <assert.h>
41 #endif
42 #include <ctype.h>
44 #include <stdio.h>
45 #include <stdarg.h>
46 #include <sys/types.h>
47 #include <unistd.h>
48 #include <string.h>
49 #include <errno.h>
50 #include <sys/stat.h>
51 #include <stdlib.h>
52 #include <fcntl.h>
54 #include "lib/global.h"
55 #include "lib/tty/tty.h"
56 #include "lib/tty/key.h" /* XCTRL */
57 #include "lib/mcconfig.h"
58 #include "lib/skin.h"
59 #include "lib/strutil.h" /* utf string functions */
60 #include "lib/lock.h"
61 #include "lib/util.h" /* tilde_expand() */
62 #include "lib/vfs/vfs.h"
63 #include "lib/widget.h"
64 #include "lib/event.h" /* mc_event_raise() */
65 #ifdef HAVE_CHARSET
66 #include "lib/charsets.h"
67 #endif
69 #include "src/history.h"
70 #include "src/setup.h" /* option_tab_spacing */
71 #ifdef HAVE_CHARSET
72 #include "src/selcodepage.h"
73 #endif
74 #include "src/keybind-defaults.h"
75 #include "src/util.h" /* check_for_default() */
76 #include "src/filemanager/layout.h" /* mc_refresh() */
78 #include "edit-impl.h"
79 #include "editwidget.h"
80 #include "editcmd_dialogs.h"
81 #ifdef HAVE_ASPELL
82 #include "spell.h"
83 #include "spell_dialogs.h"
84 #endif
85 #include "etags.h"
87 /*** global variables ****************************************************************************/
89 /* search and replace: */
90 int search_create_bookmark = FALSE;
92 /* queries on a save */
93 int edit_confirm_save = 1;
95 /*** file scope macro definitions ****************************************************************/
97 #define space_width 1
99 #define TEMP_BUF_LEN 1024
101 #define INPUT_INDEX 9
103 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
104 (and the above) routines to work properly - paul */
106 #define is_digit(x) ((x) >= '0' && (x) <= '9')
108 #define MAIL_DLG_HEIGHT 12
110 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
112 /*** file scope type declarations ****************************************************************/
114 /*** file scope variables ************************************************************************/
116 /*** file scope functions ************************************************************************/
117 /* --------------------------------------------------------------------------------------------- */
119 /* If 0 (quick save) then a) create/truncate <filename> file,
120 b) save to <filename>;
121 if 1 (safe save) then a) save to <tempnam>,
122 b) rename <tempnam> to <filename>;
123 if 2 (do backups) then a) save to <tempnam>,
124 b) rename <filename> to <filename.backup_ext>,
125 c) rename <tempnam> to <filename>. */
127 /* returns 0 on error, -1 on abort */
129 static int
130 edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath)
132 char *p;
133 gchar *tmp;
134 off_t filelen = 0;
135 int this_save_mode, fd = -1;
136 vfs_path_t *real_filename_vpath;
137 vfs_path_t *savename_vpath = NULL;
138 const char *start_filename;
139 const vfs_path_element_t *vpath_element;
141 vpath_element = vfs_path_get_by_index (filename_vpath, 0);
142 if (vpath_element == NULL)
143 return 0;
145 start_filename = vpath_element->path;
146 if (*start_filename == '\0')
147 return 0;
149 if (*start_filename != PATH_SEP && edit->dir_vpath != NULL)
151 real_filename_vpath = vfs_path_append_vpath_new (edit->dir_vpath, filename_vpath, NULL);
153 else
155 real_filename_vpath = vfs_path_clone (filename_vpath);
158 this_save_mode = option_save_mode;
159 if (this_save_mode != EDIT_QUICK_SAVE)
161 if (!vfs_file_is_local (real_filename_vpath)
162 || (fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY)) == -1)
165 * The file does not exists yet, so no safe save or
166 * backup are necessary.
168 this_save_mode = EDIT_QUICK_SAVE;
170 if (fd != -1)
171 mc_close (fd);
174 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt)
176 int rv;
177 struct stat sb;
179 rv = mc_stat (real_filename_vpath, &sb);
180 if (rv == 0 && sb.st_nlink > 1)
182 rv = edit_query_dialog3 (_("Warning"),
183 _("File has hard-links. Detach before saving?"),
184 _("&Yes"), _("&No"), _("&Cancel"));
185 switch (rv)
187 case 0:
188 this_save_mode = EDIT_SAFE_SAVE;
189 /* fallthrough */
190 case 1:
191 edit->skip_detach_prompt = 1;
192 break;
193 default:
194 vfs_path_free (real_filename_vpath);
195 return -1;
199 /* Prevent overwriting changes from other editor sessions. */
200 if (rv == 0 && edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
202 /* The default action is "Cancel". */
203 query_set_sel (1);
205 rv = edit_query_dialog2 (_("Warning"),
206 _("The file has been modified in the meantime. Save anyway?"),
207 _("&Yes"), _("&Cancel"));
208 if (rv != 0)
210 vfs_path_free (real_filename_vpath);
211 return -1;
216 if (this_save_mode != EDIT_QUICK_SAVE)
218 char *savedir, *saveprefix;
220 savedir = vfs_path_tokens_get (real_filename_vpath, 0, -1);
221 if (savedir == NULL)
222 savedir = g_strdup (".");
224 /* Token-related function never return leading slash, so we need add it manually */
225 saveprefix = mc_build_filename ("/", savedir, "cooledit", NULL);
226 g_free (savedir);
227 fd = mc_mkstemps (&savename_vpath, saveprefix, NULL);
228 g_free (saveprefix);
229 if (savename_vpath == NULL)
231 vfs_path_free (real_filename_vpath);
232 return 0;
234 /* FIXME:
235 * Close for now because mc_mkstemps use pure open system call
236 * to create temporary file and it needs to be reopened by
237 * VFS-aware mc_open().
239 close (fd);
241 else
242 savename_vpath = vfs_path_clone (real_filename_vpath);
244 (void) mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
245 (void) mc_chmod (savename_vpath, edit->stat1.st_mode);
247 fd = mc_open (savename_vpath, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
248 if (fd == -1)
249 goto error_save;
251 /* pipe save */
252 p = edit_get_write_filter (savename_vpath, real_filename_vpath);
253 if (p != NULL)
255 FILE *file;
257 mc_close (fd);
258 file = (FILE *) popen (p, "w");
260 if (file)
262 filelen = edit_write_stream (edit, file);
263 #if 1
264 pclose (file);
265 #else
266 if (pclose (file) != 0)
268 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
269 edit_error_dialog (_("Error"), tmp);
270 g_free (tmp);
271 g_free (p);
272 goto error_save;
274 #endif
276 else
278 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
279 edit_error_dialog (_("Error"), get_sys_error (tmp));
280 g_free (p);
281 g_free (tmp);
282 goto error_save;
284 g_free (p);
286 else if (edit->lb == LB_ASIS)
287 { /* do not change line breaks */
288 off_t buf;
289 buf = 0;
290 filelen = edit->last_byte;
291 while (buf <= (edit->curs1 >> S_EDIT_BUF_SIZE) - 1)
293 if (mc_write (fd, (char *) edit->buffers1[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
295 mc_close (fd);
296 goto error_save;
298 buf++;
300 if (mc_write
301 (fd, (char *) edit->buffers1[buf],
302 edit->curs1 & M_EDIT_BUF_SIZE) != (edit->curs1 & M_EDIT_BUF_SIZE))
304 filelen = -1;
306 else if (edit->curs2)
308 edit->curs2--;
309 buf = (edit->curs2 >> S_EDIT_BUF_SIZE);
310 if (mc_write
311 (fd,
312 (char *) edit->buffers2[buf] + EDIT_BUF_SIZE -
313 (edit->curs2 & M_EDIT_BUF_SIZE) - 1,
314 1 + (edit->curs2 & M_EDIT_BUF_SIZE)) != 1 + (edit->curs2 & M_EDIT_BUF_SIZE))
316 filelen = -1;
318 else
320 while (--buf >= 0)
322 if (mc_write (fd, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
324 filelen = -1;
325 break;
329 edit->curs2++;
331 if (mc_close (fd))
332 goto error_save;
334 /* Update the file information, especially the mtime. */
335 if (mc_stat (savename_vpath, &edit->stat1) == -1)
336 goto error_save;
338 else
339 { /* change line breaks */
340 FILE *file;
341 const vfs_path_element_t *path_element;
343 mc_close (fd);
345 path_element = vfs_path_get_by_index (savename_vpath, -1);
346 file = (FILE *) fopen (path_element->path, "w");
347 if (file != NULL)
349 filelen = edit_write_stream (edit, file);
350 fclose (file);
352 else
354 char *msg;
356 msg = g_strdup_printf (_("Cannot open file for writing: %s"), path_element->path);
357 edit_error_dialog (_("Error"), msg);
358 g_free (msg);
359 goto error_save;
363 if (filelen != edit->last_byte)
364 goto error_save;
366 if (this_save_mode == EDIT_DO_BACKUP)
368 char *tmp_store_filename;
369 vfs_path_element_t *last_vpath_element;
370 vfs_path_t *tmp_vpath;
371 gboolean ok;
373 #ifdef HAVE_ASSERT_H
374 assert (option_backup_ext != NULL);
375 #endif
376 /* add backup extention to the path */
377 tmp_vpath = vfs_path_clone (real_filename_vpath);
378 last_vpath_element = (vfs_path_element_t *) vfs_path_get_by_index (tmp_vpath, -1);
379 tmp_store_filename = last_vpath_element->path;
380 last_vpath_element->path = g_strdup_printf ("%s%s", tmp_store_filename, option_backup_ext);
381 g_free (tmp_store_filename);
383 ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
384 vfs_path_free (tmp_vpath);
385 if (!ok)
386 goto error_save;
389 if (this_save_mode != EDIT_QUICK_SAVE)
390 if (mc_rename (savename_vpath, real_filename_vpath) == -1)
391 goto error_save;
393 vfs_path_free (real_filename_vpath);
394 vfs_path_free (savename_vpath);
395 return 1;
396 error_save:
397 /* FIXME: Is this safe ?
398 * if (this_save_mode != EDIT_QUICK_SAVE)
399 * mc_unlink (savename);
401 vfs_path_free (real_filename_vpath);
402 vfs_path_free (savename_vpath);
403 return 0;
406 /* --------------------------------------------------------------------------------------------- */
408 static gboolean
409 edit_check_newline (WEdit * edit)
411 return !(option_check_nl_at_eof && edit->last_byte > 0
412 && edit_get_byte (edit, edit->last_byte - 1) != '\n'
413 && edit_query_dialog2 (_("Warning"),
414 _("The file you are saving is not finished with a newline"),
415 _("C&ontinue"), _("&Cancel")));
418 /* --------------------------------------------------------------------------------------------- */
420 static vfs_path_t *
421 edit_get_save_file_as (WEdit * edit)
423 #define DLG_WIDTH 64
424 #define DLG_HEIGHT 14
426 static LineBreaks cur_lb = LB_ASIS;
428 char *filename = vfs_path_to_str (edit->filename_vpath);
429 char *filename_res;
431 const char *lb_names[LB_NAMES] = {
432 N_("&Do not change"),
433 N_("&Unix format (LF)"),
434 N_("&Windows/DOS format (CR LF)"),
435 N_("&Macintosh format (CR)")
438 QuickWidget quick_widgets[] = {
439 QUICK_BUTTON (6, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
440 QUICK_BUTTON (2, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
441 QUICK_RADIO (5, DLG_WIDTH, DLG_HEIGHT - 8, DLG_HEIGHT, LB_NAMES, lb_names, (int *) &cur_lb),
442 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 9, DLG_HEIGHT, N_("Change line breaks to:")),
443 QUICK_INPUT (3, DLG_WIDTH, DLG_HEIGHT - 11, DLG_HEIGHT, filename, DLG_WIDTH - 6, 0,
444 "save-as", &filename_res),
445 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 12, DLG_HEIGHT, N_("Enter file name:")),
446 QUICK_END
449 QuickDialog Quick_options = {
450 DLG_WIDTH, DLG_HEIGHT, -1, -1,
451 N_("Save As"), "[Save File As]",
452 quick_widgets, NULL, NULL, FALSE
455 if (quick_dialog (&Quick_options) != B_CANCEL)
457 char *fname;
458 vfs_path_t *ret_vpath;
460 edit->lb = cur_lb;
461 fname = tilde_expand (filename_res);
462 g_free (filename_res);
463 ret_vpath = vfs_path_from_str (fname);
464 g_free (fname);
465 return ret_vpath;
467 g_free (filename);
469 return NULL;
471 #undef DLG_WIDTH
472 #undef DLG_HEIGHT
475 /* --------------------------------------------------------------------------------------------- */
477 /** returns TRUE on success */
479 static gboolean
480 edit_save_cmd (WEdit * edit)
482 int res, save_lock = 0;
484 if (!edit->locked && !edit->delete_file)
485 save_lock = lock_file (edit->filename_vpath);
486 res = edit_save_file (edit, edit->filename_vpath);
488 /* Maintain modify (not save) lock on failure */
489 if ((res > 0 && edit->locked) || save_lock)
490 edit->locked = unlock_file (edit->filename_vpath);
492 /* On failure try 'save as', it does locking on its own */
493 if (res == 0)
494 return edit_save_as_cmd (edit);
495 edit->force |= REDRAW_COMPLETELY;
496 if (res > 0)
498 edit->delete_file = 0;
499 edit->modified = 0;
502 return TRUE;
505 /* --------------------------------------------------------------------------------------------- */
507 * Load file content
509 * @param h screen the owner of editor window
510 * @param vpath vfs file path
511 * @return TRUE if file content was successfully loaded, FALSE otherwise
514 static inline gboolean
515 edit_load_file_from_filename (Dlg_head * h, const vfs_path_t * vpath)
517 Widget *w = WIDGET (h);
519 return edit_add_window (h, w->y + 1, w->x, w->lines - 2, w->cols, vpath, 0);
522 /* --------------------------------------------------------------------------------------------- */
524 static void
525 edit_delete_column_of_text (WEdit * edit)
527 off_t p, q, r;
528 off_t m1, m2;
529 off_t n;
530 long b, c, d;
532 eval_marks (edit, &m1, &m2);
533 n = edit_move_forward (edit, m1, 0, m2) + 1;
534 c = (long) edit_move_forward3 (edit, edit_bol (edit, m1), 0, m1);
535 d = (long) edit_move_forward3 (edit, edit_bol (edit, m2), 0, m2);
536 b = max (min (c, d), min (edit->column1, edit->column2));
537 c = max (c, max (edit->column1, edit->column2));
539 while (n--)
541 r = edit_bol (edit, edit->curs1);
542 p = edit_move_forward3 (edit, r, b, 0);
543 q = edit_move_forward3 (edit, r, c, 0);
544 if (p < m1)
545 p = m1;
546 if (q > m2)
547 q = m2;
548 edit_cursor_move (edit, p - edit->curs1);
549 while (q > p)
551 /* delete line between margins */
552 if (edit_get_byte (edit, edit->curs1) != '\n')
553 edit_delete (edit, TRUE);
554 q--;
556 if (n)
557 /* move to next line except on the last delete */
558 edit_cursor_move (edit, edit_move_forward (edit, edit->curs1, 1, 0) - edit->curs1);
562 /* --------------------------------------------------------------------------------------------- */
563 /** if success return 0 */
565 static int
566 edit_block_delete (WEdit * edit)
568 off_t start_mark, end_mark;
569 off_t curs_pos;
570 long curs_line, c1, c2;
572 if (eval_marks (edit, &start_mark, &end_mark))
573 return 0;
574 if (edit->column_highlight && edit->mark2 < 0)
575 edit_mark_cmd (edit, FALSE);
576 if ((end_mark - start_mark) > option_max_undo / 2)
578 /* Warning message with a query to continue or cancel the operation */
579 if (edit_query_dialog2
580 (_("Warning"),
582 ("Block is large, you may not be able to undo this action"),
583 _("C&ontinue"), _("&Cancel")))
585 return 1;
588 c1 = min (edit->column1, edit->column2);
589 c2 = max (edit->column1, edit->column2);
590 edit->column1 = c1;
591 edit->column2 = c2;
593 edit_push_markers (edit);
595 curs_line = edit->curs_line;
597 curs_pos = edit->curs_col + edit->over_col;
599 /* move cursor to start of selection */
600 edit_cursor_move (edit, start_mark - edit->curs1);
601 edit_scroll_screen_over_cursor (edit);
603 if (start_mark < end_mark)
605 if (edit->column_highlight)
607 off_t line_width;
609 if (edit->mark2 < 0)
610 edit_mark_cmd (edit, FALSE);
611 edit_delete_column_of_text (edit);
612 /* move cursor to the saved position */
613 edit_move_to_line (edit, curs_line);
614 /* calculate line width and cursor position before cut */
615 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
616 edit_eol (edit, edit->curs1));
617 if (option_cursor_beyond_eol && curs_pos > line_width)
618 edit->over_col = curs_pos - line_width;
620 else
622 off_t count;
624 for (count = start_mark; count < end_mark; count++)
625 edit_delete (edit, TRUE);
628 edit_set_markers (edit, 0, 0, 0, 0);
629 edit->force |= REDRAW_PAGE;
630 return 0;
633 /* --------------------------------------------------------------------------------------------- */
635 * Get EOL symbol for searching.
637 * @param edit editor object
638 * @return EOL symbol
641 static inline char
642 edit_search_get_current_end_line_char (const WEdit * edit)
644 switch (edit->lb)
646 case LB_MAC:
647 return '\r';
648 default:
649 return '\n';
653 /* --------------------------------------------------------------------------------------------- */
655 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
657 * @param search search object
658 * @return result of checks.
661 static edit_search_line_t
662 edit_get_search_line_type (mc_search_t * search)
664 edit_search_line_t search_line_type = 0;
666 if (search->search_type != MC_SEARCH_T_REGEX)
667 return search_line_type;
669 if (*search->original == '^')
670 search_line_type |= AT_START_LINE;
672 if (search->original[search->original_len - 1] == '$')
673 search_line_type |= AT_END_LINE;
674 return search_line_type;
677 /* --------------------------------------------------------------------------------------------- */
679 * Calculating the start position of next line.
681 * @param edit editor object
682 * @param current_pos current position
683 * @param max_pos max position
684 * @param end_string_symbol end of line symbol
685 * @return start position of next line
688 static off_t
689 edit_calculate_start_of_next_line (WEdit * edit, off_t current_pos, off_t max_pos,
690 char end_string_symbol)
692 off_t i;
694 for (i = current_pos; i < max_pos; i++)
696 current_pos++;
697 if (edit_get_byte (edit, i) == end_string_symbol)
698 break;
701 return current_pos;
704 /* --------------------------------------------------------------------------------------------- */
706 * Calculating the end position of previous line.
708 * @param edit editor object
709 * @param current_pos current position
710 * @param end_string_symbol end of line symbol
711 * @return end position of previous line
714 static off_t
715 edit_calculate_end_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
717 off_t i;
719 for (i = current_pos - 1; i >= 0; i--)
720 if (edit_get_byte (edit, i) == end_string_symbol)
721 break;
723 return i;
726 /* --------------------------------------------------------------------------------------------- */
728 * Calculating the start position of previous line.
730 * @param edit editor object
731 * @param current_pos current position
732 * @param end_string_symbol end of line symbol
733 * @return start position of previous line
736 static inline off_t
737 edit_calculate_start_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
739 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
740 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
742 return (current_pos + 1);
745 /* --------------------------------------------------------------------------------------------- */
747 * Calculating the start position of current line.
749 * @param edit editor object
750 * @param current_pos current position
751 * @param end_string_symbol end of line symbol
752 * @return start position of current line
755 static inline off_t
756 edit_calculate_start_of_current_line (WEdit * edit, off_t current_pos, char end_string_symbol)
758 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
760 return (current_pos + 1);
763 /* --------------------------------------------------------------------------------------------- */
765 * Fixing (if needed) search start position if 'only in selection' option present.
767 * @param edit editor object
770 static void
771 edit_search_fix_search_start_if_selection (WEdit * edit)
773 off_t start_mark = 0;
774 off_t end_mark = 0;
776 if (!edit_search_options.only_in_selection)
777 return;
779 if (eval_marks (edit, &start_mark, &end_mark) != 0)
780 return;
782 if (edit_search_options.backwards)
784 if (edit->search_start > end_mark || edit->search_start <= start_mark)
785 edit->search_start = end_mark;
787 else
789 if (edit->search_start < start_mark || edit->search_start >= end_mark)
790 edit->search_start = start_mark;
794 /* --------------------------------------------------------------------------------------------- */
796 static gboolean
797 editcmd_find (WEdit * edit, gsize * len)
799 off_t search_start = edit->search_start;
800 off_t search_end;
801 off_t start_mark = 0;
802 off_t end_mark = edit->last_byte;
803 int mark_res = 0;
804 char end_string_symbol;
806 end_string_symbol = edit_search_get_current_end_line_char (edit);
808 /* prepare for search */
809 if (edit_search_options.only_in_selection)
811 mark_res = eval_marks (edit, &start_mark, &end_mark);
812 if (mark_res != 0)
814 edit->search->error = MC_SEARCH_E_NOTFOUND;
815 edit->search->error_str = g_strdup (_("Search string not found"));
816 return FALSE;
819 /* fix the start and the end of search block positions */
820 if ((edit->search_line_type & AT_START_LINE) != 0
821 && (start_mark != 0 || edit_get_byte (edit, start_mark - 1) != end_string_symbol))
823 start_mark =
824 edit_calculate_start_of_next_line (edit, start_mark, edit->last_byte,
825 end_string_symbol);
827 if ((edit->search_line_type & AT_END_LINE) != 0
828 && (end_mark - 1 != edit->last_byte
829 || edit_get_byte (edit, end_mark) != end_string_symbol))
831 end_mark = edit_calculate_end_of_previous_line (edit, end_mark, end_string_symbol);
833 if (start_mark >= end_mark)
835 edit->search->error = MC_SEARCH_E_NOTFOUND;
836 edit->search->error_str = g_strdup (_("Search string not found"));
837 return FALSE;
840 else
842 if (edit_search_options.backwards)
843 end_mark = max (1, edit->curs1) - 1;
846 /* search */
847 if (edit_search_options.backwards)
849 /* backward search */
850 search_end = end_mark;
852 if ((edit->search_line_type & AT_START_LINE) != 0)
853 search_start =
854 edit_calculate_start_of_current_line (edit, search_start, end_string_symbol);
856 while ((int) search_start >= start_mark)
858 if (search_end > (off_t) (search_start + edit->search->original_len)
859 && mc_search_is_fixed_search_str (edit->search))
861 search_end = search_start + edit->search->original_len;
863 if (mc_search_run (edit->search, (void *) edit, search_start, search_end, len)
864 && edit->search->normal_offset == search_start)
866 return TRUE;
870 if ((edit->search_line_type & AT_START_LINE) != 0)
871 search_start =
872 edit_calculate_start_of_previous_line (edit, search_start, end_string_symbol);
873 else
874 search_start--;
876 edit->search->error_str = g_strdup (_("Search string not found"));
878 else
880 /* forward search */
881 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
882 search_start =
883 edit_calculate_start_of_next_line (edit, search_start, end_mark, end_string_symbol);
884 return mc_search_run (edit->search, (void *) edit, search_start, end_mark, len);
886 return FALSE;
889 /* --------------------------------------------------------------------------------------------- */
891 static char *
892 edit_replace_cmd__conv_to_display (char *str)
894 #ifdef HAVE_CHARSET
895 GString *tmp;
897 tmp = str_convert_to_display (str);
898 if (tmp != NULL)
900 if (tmp->len != 0)
901 return g_string_free (tmp, FALSE);
902 g_string_free (tmp, TRUE);
904 #endif
905 return g_strdup (str);
908 /* --------------------------------------------------------------------------------------------- */
910 static char *
911 edit_replace_cmd__conv_to_input (char *str)
913 #ifdef HAVE_CHARSET
914 GString *tmp;
916 tmp = str_convert_to_input (str);
917 if (tmp != NULL)
919 if (tmp->len != 0)
920 return g_string_free (tmp, FALSE);
921 g_string_free (tmp, TRUE);
923 #endif
924 return g_strdup (str);
927 /* --------------------------------------------------------------------------------------------- */
929 static void
930 edit_do_search (WEdit * edit)
932 gsize len = 0;
934 if (edit->search == NULL)
935 edit->search_start = edit->curs1;
937 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
939 if (search_create_bookmark)
941 int found = 0, books = 0;
942 long l = 0, l_last = -1;
943 long q = 0;
945 search_create_bookmark = FALSE;
946 book_mark_flush (edit, -1);
948 while (mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len))
950 if (found == 0)
951 edit->search_start = edit->search->normal_offset;
952 found++;
953 l += edit_count_lines (edit, q, edit->search->normal_offset);
954 if (l != l_last)
956 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
957 books++;
959 l_last = l;
960 q = edit->search->normal_offset + 1;
963 if (found == 0)
964 edit_error_dialog (_("Search"), _("Search string not found"));
965 else
966 edit_cursor_move (edit, edit->search_start - edit->curs1);
968 else
970 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
971 && edit_search_options.backwards)
972 edit->search_start--;
974 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
975 && !edit_search_options.backwards)
976 edit->search_start++;
978 if (editcmd_find (edit, &len))
980 edit->found_start = edit->search_start = edit->search->normal_offset;
981 edit->found_len = len;
982 edit->over_col = 0;
983 edit_cursor_move (edit, edit->search_start - edit->curs1);
984 edit_scroll_screen_over_cursor (edit);
985 if (edit_search_options.backwards)
986 edit->search_start--;
987 else
988 edit->search_start++;
990 else
992 edit->search_start = edit->curs1;
993 if (edit->search->error_str != NULL)
994 edit_error_dialog (_("Search"), edit->search->error_str);
998 edit->force |= REDRAW_COMPLETELY;
999 edit_scroll_screen_over_cursor (edit);
1002 /* --------------------------------------------------------------------------------------------- */
1004 static void
1005 edit_search (WEdit * edit)
1007 if (editcmd_dialog_search_show (edit))
1009 edit->search_line_type = edit_get_search_line_type (edit->search);
1010 edit_search_fix_search_start_if_selection (edit);
1011 edit_do_search (edit);
1015 /* --------------------------------------------------------------------------------------------- */
1016 /** Return a null terminated length of text. Result must be g_free'd */
1018 static unsigned char *
1019 edit_get_block (WEdit * edit, off_t start, off_t finish, off_t * l)
1021 unsigned char *s, *r;
1023 r = s = g_malloc0 (finish - start + 1);
1024 if (edit->column_highlight)
1026 *l = 0;
1027 /* copy from buffer, excluding chars that are out of the column 'margins' */
1028 while (start < finish)
1030 int c;
1031 off_t x;
1033 x = edit_move_forward3 (edit, edit_bol (edit, start), 0, start);
1034 c = edit_get_byte (edit, start);
1035 if ((x >= edit->column1 && x < edit->column2)
1036 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1038 *s++ = c;
1039 (*l)++;
1041 start++;
1044 else
1046 *l = finish - start;
1047 while (start < finish)
1048 *s++ = edit_get_byte (edit, start++);
1050 *s = '\0';
1051 return r;
1054 /* --------------------------------------------------------------------------------------------- */
1055 /** copies a block to clipboard file */
1057 static gboolean
1058 edit_save_block_to_clip_file (WEdit * edit, off_t start, off_t finish)
1060 gboolean ret;
1061 gchar *tmp;
1063 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1064 ret = edit_save_block (edit, tmp, start, finish);
1065 g_free (tmp);
1066 return ret;
1069 /* --------------------------------------------------------------------------------------------- */
1071 static void
1072 pipe_mail (WEdit * edit, char *to, char *subject, char *cc)
1074 FILE *p = 0;
1075 char *s;
1077 to = name_quote (to, 0);
1078 subject = name_quote (subject, 0);
1079 cc = name_quote (cc, 0);
1080 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1081 g_free (to);
1082 g_free (subject);
1083 g_free (cc);
1085 if (s)
1087 p = popen (s, "w");
1088 g_free (s);
1091 if (p)
1093 off_t i;
1094 for (i = 0; i < edit->last_byte; i++)
1095 fputc (edit_get_byte (edit, i), p);
1096 pclose (p);
1100 /* --------------------------------------------------------------------------------------------- */
1101 /** find first character of current word */
1103 static gboolean
1104 edit_find_word_start (WEdit * edit, off_t * word_start, gsize * word_len)
1106 int c, last;
1107 gsize i;
1109 /* return if at begin of file */
1110 if (edit->curs1 <= 0)
1111 return FALSE;
1113 c = edit_get_byte (edit, edit->curs1 - 1);
1114 /* return if not at end or in word */
1115 if (is_break_char (c))
1116 return FALSE;
1118 /* search start of word to be completed */
1119 for (i = 2;; i++)
1121 /* return if at begin of file */
1122 if ((gsize) edit->curs1 < i)
1123 return FALSE;
1125 last = c;
1126 c = edit_get_byte (edit, edit->curs1 - i);
1128 if (is_break_char (c))
1130 /* return if word starts with digit */
1131 if (isdigit (last))
1132 return FALSE;
1134 *word_start = edit->curs1 - (i - 1); /* start found */
1135 *word_len = i - 1;
1136 break;
1139 /* success */
1140 return TRUE;
1143 /* --------------------------------------------------------------------------------------------- */
1145 * Get current word under cursor
1147 * @param edit editor object
1148 * @param srch mc_search object
1149 * @param word_start start word position
1151 * @return newly allocated string or NULL if no any words under cursor
1154 static char *
1155 edit_collect_completions_get_current_word (WEdit * edit, mc_search_t * srch, off_t word_start)
1157 gsize len = 0;
1158 off_t i;
1159 GString *temp;
1161 if (!mc_search_run (srch, (void *) edit, word_start, edit->last_byte, &len))
1162 return NULL;
1164 temp = g_string_sized_new (len);
1166 for (i = 0; i < (off_t) len; i++)
1168 int chr;
1170 chr = edit_get_byte (edit, word_start + i);
1171 if (!isspace (chr))
1172 g_string_append_c (temp, chr);
1175 return g_string_free (temp, temp->len == 0);
1178 /* --------------------------------------------------------------------------------------------- */
1179 /** collect the possible completions */
1181 static gsize
1182 edit_collect_completions (WEdit * edit, off_t word_start, gsize word_len,
1183 char *match_expr, struct selection *compl, gsize * num)
1185 gsize len = 0;
1186 gsize max_len = 0;
1187 gsize i;
1188 int skip;
1189 GString *temp;
1190 mc_search_t *srch;
1191 off_t last_byte, start = -1;
1192 char *current_word;
1194 srch = mc_search_new (match_expr, -1);
1195 if (srch == NULL)
1196 return 0;
1198 if (mc_config_get_bool
1199 (mc_main_config, CONFIG_APP_SECTION, "editor_wordcompletion_collect_entire_file", 0))
1201 last_byte = edit->last_byte;
1203 else
1205 last_byte = word_start;
1208 srch->search_type = MC_SEARCH_T_REGEX;
1209 srch->is_case_sensitive = TRUE;
1210 srch->search_fn = edit_search_cmd_callback;
1212 current_word = edit_collect_completions_get_current_word (edit, srch, word_start);
1214 temp = g_string_new ("");
1216 /* collect max MAX_WORD_COMPLETIONS completions */
1217 while (mc_search_run (srch, (void *) edit, start + 1, last_byte, &len))
1219 g_string_set_size (temp, 0);
1220 start = srch->normal_offset;
1222 /* add matched completion if not yet added */
1223 for (i = 0; i < len; i++)
1225 skip = edit_get_byte (edit, start + i);
1226 if (isspace (skip))
1227 continue;
1229 /* skip current word */
1230 if (start + (off_t) i == word_start)
1231 break;
1233 g_string_append_c (temp, skip);
1236 if (temp->len == 0)
1237 continue;
1239 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1240 continue;
1242 skip = 0;
1244 for (i = 0; i < *num; i++)
1246 if (strncmp
1247 ((char *) &compl[i].text[word_len],
1248 (char *) &temp->str[word_len], max (len, compl[i].len) - word_len) == 0)
1250 struct selection this = compl[i];
1251 for (++i; i < *num; i++)
1253 compl[i - 1] = compl[i];
1255 compl[*num - 1] = this;
1256 skip = 1;
1257 break; /* skip it, already added */
1260 if (skip != 0)
1261 continue;
1263 if (*num == MAX_WORD_COMPLETIONS)
1265 g_free (compl[0].text);
1266 for (i = 1; i < *num; i++)
1268 compl[i - 1] = compl[i];
1270 (*num)--;
1272 #ifdef HAVE_CHARSET
1274 GString *recoded;
1275 recoded = str_convert_to_display (temp->str);
1277 if (recoded && recoded->len)
1278 g_string_assign (temp, recoded->str);
1280 g_string_free (recoded, TRUE);
1282 #endif
1283 compl[*num].text = g_strndup (temp->str, temp->len);
1284 compl[*num].len = temp->len;
1285 (*num)++;
1286 start += len;
1288 /* note the maximal length needed for the completion dialog */
1289 if (len > max_len)
1290 max_len = len;
1293 mc_search_free (srch);
1294 g_string_free (temp, TRUE);
1295 g_free (current_word);
1297 return max_len;
1300 /* --------------------------------------------------------------------------------------------- */
1302 static void
1303 edit_insert_column_of_text (WEdit * edit, unsigned char *data, off_t size, long width,
1304 off_t * start_pos, off_t * end_pos, long *col1, long *col2)
1306 off_t i, cursor;
1307 long col;
1309 cursor = edit->curs1;
1310 col = edit_get_col (edit);
1312 for (i = 0; i < size; i++)
1314 if (data[i] != '\n')
1315 edit_insert (edit, data[i]);
1316 else
1317 { /* fill in and move to next line */
1318 long l;
1319 off_t p;
1321 if (edit_get_byte (edit, edit->curs1) != '\n')
1323 for (l = width - (edit_get_col (edit) - col); l > 0; l -= space_width)
1324 edit_insert (edit, ' ');
1326 for (p = edit->curs1;; p++)
1328 if (p == edit->last_byte)
1330 edit_cursor_move (edit, edit->last_byte - edit->curs1);
1331 edit_insert_ahead (edit, '\n');
1332 p++;
1333 break;
1335 if (edit_get_byte (edit, p) == '\n')
1337 p++;
1338 break;
1341 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
1343 for (l = col - edit_get_col (edit); l >= space_width; l -= space_width)
1344 edit_insert (edit, ' ');
1348 *col1 = col;
1349 *col2 = col + width;
1350 *start_pos = cursor;
1351 *end_pos = edit->curs1;
1352 edit_cursor_move (edit, cursor - edit->curs1);
1355 /* --------------------------------------------------------------------------------------------- */
1357 static int
1358 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1360 const macros_t *m1 = (const macros_t *) macro1;
1361 const macros_t *m2 = (const macros_t *) macro2;
1363 return m1->hotkey - m2->hotkey;
1366 /* --------------------------------------------------------------------------------------------- */
1368 static void
1369 edit_macro_sort_by_hotkey (void)
1371 if (macros_list != NULL && macros_list->len != 0)
1372 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1375 /* --------------------------------------------------------------------------------------------- */
1377 static gboolean
1378 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1380 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1381 macros_t *result;
1382 macros_t search_macro;
1384 (void) edit;
1386 search_macro.hotkey = hotkey;
1387 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1388 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1390 if (result != NULL && result->macro != NULL)
1392 *indx = (result - array_start);
1393 *macros = result;
1394 return TRUE;
1396 *indx = 0;
1397 return FALSE;
1400 /* --------------------------------------------------------------------------------------------- */
1401 /** returns FALSE on error */
1403 static gboolean
1404 edit_delete_macro (WEdit * edit, int hotkey)
1406 mc_config_t *macros_config = NULL;
1407 const char *section_name = "editor";
1408 gchar *macros_fname;
1409 guint indx;
1410 char *skeyname;
1411 const macros_t *macros = NULL;
1413 /* clear array of actions for current hotkey */
1414 while (edit_get_macro (edit, hotkey, &macros, &indx))
1416 if (macros->macro != NULL)
1417 g_array_free (macros->macro, TRUE);
1418 macros = NULL;
1419 g_array_remove_index (macros_list, indx);
1420 edit_macro_sort_by_hotkey ();
1423 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1424 macros_config = mc_config_init (macros_fname, FALSE);
1425 g_free (macros_fname);
1427 if (macros_config == NULL)
1428 return FALSE;
1430 skeyname = lookup_key_by_code (hotkey);
1431 while (mc_config_del_key (macros_config, section_name, skeyname))
1433 g_free (skeyname);
1434 mc_config_save_file (macros_config, NULL);
1435 mc_config_deinit (macros_config);
1436 return TRUE;
1439 /* --------------------------------------------------------------------------------------------- */
1441 * Callback for the iteration of objects in the 'editors' array.
1442 * Toggle syntax highlighting in editor object.
1444 * @param data probably WEdit object
1445 * @param user_data unused
1448 static void
1449 edit_syntax_onoff_cb (void *data, void *user_data)
1451 (void) user_data;
1453 if (edit_widget_is_editor ((const Widget *) data))
1455 WEdit *edit = (WEdit *) data;
1457 if (option_syntax_highlighting)
1458 edit_load_syntax (edit, NULL, edit->syntax_type);
1459 edit->force |= REDRAW_PAGE;
1463 /* --------------------------------------------------------------------------------------------- */
1465 * Callback for the iteration of objects in the 'editors' array.
1466 * Redraw editor object.
1468 * @param data probably WEdit object
1469 * @param user_data unused
1472 static void
1473 edit_redraw_page_cb (void *data, void *user_data)
1475 (void) user_data;
1477 if (edit_widget_is_editor ((const Widget *) data))
1478 ((WEdit *) data)->force |= REDRAW_PAGE;
1481 /* --------------------------------------------------------------------------------------------- */
1482 /*** public functions ****************************************************************************/
1483 /* --------------------------------------------------------------------------------------------- */
1485 void
1486 edit_refresh_cmd (void)
1488 clr_scr ();
1489 repaint_screen ();
1490 tty_keypad (TRUE);
1493 /* --------------------------------------------------------------------------------------------- */
1495 * Toggle syntax highlighting in all editor windows.
1497 * @param h root widget for all windows
1500 void
1501 edit_syntax_onoff_cmd (Dlg_head * h)
1503 option_syntax_highlighting = !option_syntax_highlighting;
1504 g_list_foreach (h->widgets, edit_syntax_onoff_cb, NULL);
1505 dlg_redraw (h);
1508 /* --------------------------------------------------------------------------------------------- */
1510 * Toggle tabs showing in all editor windows.
1512 * @param h root widget for all windows
1515 void
1516 edit_show_tabs_tws_cmd (Dlg_head * h)
1518 enable_show_tabs_tws = !enable_show_tabs_tws;
1519 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1520 dlg_redraw (h);
1523 /* --------------------------------------------------------------------------------------------- */
1525 * Toggle right margin showing in all editor windows.
1527 * @param h root widget for all windows
1530 void
1531 edit_show_margin_cmd (Dlg_head * h)
1533 show_right_margin = !show_right_margin;
1534 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1535 dlg_redraw (h);
1538 /* --------------------------------------------------------------------------------------------- */
1540 * Toggle line numbers showing in all editor windows.
1542 * @param h root widget for all windows
1545 void
1546 edit_show_numbers_cmd (Dlg_head * h)
1548 option_line_state = !option_line_state;
1549 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
1550 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1551 dlg_redraw (h);
1554 /* --------------------------------------------------------------------------------------------- */
1556 void
1557 edit_save_mode_cmd (void)
1559 /* diaog sizes */
1560 const int DLG_X = 38;
1561 const int DLG_Y = 13;
1563 char *str_result;
1565 const char *str[] = {
1566 N_("&Quick save"),
1567 N_("&Safe save"),
1568 N_("&Do backups with following extension:")
1571 QuickWidget widgets[] = {
1572 /* 0 */
1573 QUICK_BUTTON (18, DLG_X, DLG_Y - 3, DLG_Y, N_("&Cancel"), B_CANCEL, NULL),
1574 /* 1 */
1575 QUICK_BUTTON (6, DLG_X, DLG_Y - 3, DLG_Y, N_("&OK"), B_ENTER, NULL),
1576 /* 2 */
1577 QUICK_CHECKBOX (4, DLG_X, 8, DLG_Y, N_("Check &POSIX new line"), &option_check_nl_at_eof),
1578 /* 3 */
1579 QUICK_INPUT (8, DLG_X, 6, DLG_Y, option_backup_ext, 9, 0, "edit-backup-ext", &str_result),
1580 /* 4 */
1581 QUICK_RADIO (4, DLG_X, 3, DLG_Y, 3, str, &option_save_mode),
1582 QUICK_END
1585 QuickDialog dialog = {
1586 DLG_X, DLG_Y, -1, -1, N_("Edit Save Mode"),
1587 "[Edit Save Mode]", widgets, NULL, NULL, FALSE
1590 size_t i;
1591 size_t maxlen = 0;
1592 size_t w0, w1, b_len, w3;
1594 #ifdef HAVE_ASSERT_H
1595 assert (option_backup_ext != NULL);
1596 #endif
1598 /* OK/Cancel buttons */
1599 w0 = str_term_width1 (_(widgets[0].u.button.text)) + 3;
1600 w1 = str_term_width1 (_(widgets[1].u.button.text)) + 5; /* default button */
1601 b_len = w0 + w1 + 3;
1603 maxlen = max (b_len, (size_t) str_term_width1 (_(dialog.title)) + 2);
1605 w3 = 0;
1606 for (i = 0; i < 3; i++)
1608 #ifdef ENABLE_NLS
1609 str[i] = _(str[i]);
1610 #endif
1611 w3 = max (w3, (size_t) str_term_width1 (str[i]));
1614 maxlen = max (maxlen, w3 + 4);
1616 dialog.xlen = min ((size_t) COLS, maxlen + 8);
1618 widgets[3].u.input.len = w3;
1619 widgets[1].relative_x = (dialog.xlen - b_len) / 2;
1620 widgets[0].relative_x = widgets[1].relative_x + w0 + 2;
1622 for (i = 0; i < sizeof (widgets) / sizeof (widgets[0]); i++)
1623 widgets[i].x_divisions = dialog.xlen;
1625 if (quick_dialog (&dialog) != B_CANCEL)
1627 g_free (option_backup_ext);
1628 option_backup_ext = str_result;
1632 /* --------------------------------------------------------------------------------------------- */
1634 void
1635 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1637 vfs_path_free (edit->filename_vpath);
1638 edit->filename_vpath = vfs_path_clone (name_vpath);
1640 if (edit->dir_vpath == NULL)
1641 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1644 /* --------------------------------------------------------------------------------------------- */
1645 /* Here we want to warn the users of overwriting an existing file,
1646 but only if they have made a change to the filename */
1647 /* returns TRUE on success */
1648 gboolean
1649 edit_save_as_cmd (WEdit * edit)
1651 /* This heads the 'Save As' dialog box */
1652 vfs_path_t *exp_vpath;
1653 int save_lock = 0;
1654 int different_filename = 0;
1656 if (!edit_check_newline (edit))
1657 return FALSE;
1659 exp_vpath = edit_get_save_file_as (edit);
1660 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1662 if (exp_vpath != NULL)
1664 if (vfs_path_len (exp_vpath) == 0)
1665 goto ret;
1666 else
1668 int rv;
1670 if (vfs_path_cmp (edit->filename_vpath, exp_vpath) != 0)
1672 int file;
1673 struct stat sb;
1675 if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1677 edit_error_dialog (_("Save as"),
1678 get_sys_error (_
1679 ("Cannot save: destination is not a regular file")));
1680 goto ret;
1683 different_filename = 1;
1684 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1686 if (file != -1)
1688 /* the file exists */
1689 mc_close (file);
1690 /* Overwrite the current file or cancel the operation */
1691 if (edit_query_dialog2
1692 (_("Warning"),
1693 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1694 goto ret;
1696 else
1698 edit->stat1.st_mode |= S_IWUSR;
1700 save_lock = lock_file (exp_vpath);
1702 else
1704 /* filenames equal, check if already locked */
1705 if (!edit->locked && !edit->delete_file)
1706 save_lock = lock_file (exp_vpath);
1709 if (different_filename)
1712 * Allow user to write into saved (under another name) file
1713 * even if original file had r/o user permissions.
1715 edit->stat1.st_mode |= S_IWRITE;
1718 rv = edit_save_file (edit, exp_vpath);
1719 switch (rv)
1721 case 1:
1722 /* Succesful, so unlock both files */
1723 if (different_filename)
1725 if (save_lock)
1726 unlock_file (exp_vpath);
1727 if (edit->locked)
1728 edit->locked = unlock_file (edit->filename_vpath);
1730 else
1732 if (edit->locked || save_lock)
1733 edit->locked = unlock_file (edit->filename_vpath);
1736 edit_set_filename (edit, exp_vpath);
1737 if (edit->lb != LB_ASIS)
1738 edit_reload (edit, exp_vpath);
1739 edit->modified = 0;
1740 edit->delete_file = 0;
1741 if (different_filename)
1742 edit_load_syntax (edit, NULL, edit->syntax_type);
1743 vfs_path_free (exp_vpath);
1744 edit->force |= REDRAW_COMPLETELY;
1745 return TRUE;
1746 default:
1747 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1748 /* fallthrough */
1749 case -1:
1750 /* Failed, so maintain modify (not save) lock */
1751 if (save_lock)
1752 unlock_file (exp_vpath);
1753 break;
1758 ret:
1759 vfs_path_free (exp_vpath);
1760 edit->force |= REDRAW_COMPLETELY;
1761 return FALSE;
1764 /* {{{ Macro stuff starts here */
1765 /* --------------------------------------------------------------------------------------------- */
1767 void
1768 edit_delete_macro_cmd (WEdit * edit)
1770 int hotkey;
1772 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE);
1774 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1775 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1778 /* --------------------------------------------------------------------------------------------- */
1780 /** returns FALSE on error */
1781 gboolean
1782 edit_execute_macro (WEdit * edit, int hotkey)
1784 gboolean res = FALSE;
1786 if (hotkey != 0)
1788 const macros_t *macros;
1789 guint indx;
1791 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1792 macros->macro != NULL && macros->macro->len != 0)
1794 guint i;
1796 edit->force |= REDRAW_PAGE;
1798 for (i = 0; i < macros->macro->len; i++)
1800 const macro_action_t *m_act;
1802 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1803 edit_execute_cmd (edit, m_act->action, m_act->ch);
1804 res = TRUE;
1809 return res;
1812 /* --------------------------------------------------------------------------------------------- */
1814 /** returns FALSE on error */
1815 gboolean
1816 edit_store_macro_cmd (WEdit * edit)
1818 int i;
1819 int hotkey;
1820 GString *marcros_string;
1821 mc_config_t *macros_config = NULL;
1822 const char *section_name = "editor";
1823 gchar *macros_fname;
1824 GArray *macros; /* current macro */
1825 int tmp_act;
1826 gboolean have_macro = FALSE;
1827 char *skeyname = NULL;
1829 hotkey =
1830 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE);
1831 if (hotkey == ESC_CHAR)
1832 return FALSE;
1834 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1836 /* return FALSE if try assign macro into restricted hotkeys */
1837 if (tmp_act == CK_MacroStartRecord
1838 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1839 return FALSE;
1841 edit_delete_macro (edit, hotkey);
1843 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1844 macros_config = mc_config_init (macros_fname, FALSE);
1845 g_free (macros_fname);
1847 if (macros_config == NULL)
1848 return FALSE;
1850 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1852 marcros_string = g_string_sized_new (250);
1853 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1855 skeyname = lookup_key_by_code (hotkey);
1857 for (i = 0; i < macro_index; i++)
1859 macro_action_t m_act;
1860 const char *action_name;
1862 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1864 if (action_name == NULL)
1865 break;
1867 m_act.action = record_macro_buf[i].action;
1868 m_act.ch = record_macro_buf[i].ch;
1869 g_array_append_val (macros, m_act);
1870 have_macro = TRUE;
1871 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1872 (int) record_macro_buf[i].ch);
1874 if (have_macro)
1876 macros_t macro;
1877 macro.hotkey = hotkey;
1878 macro.macro = macros;
1879 g_array_append_val (macros_list, macro);
1880 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1882 else
1883 mc_config_del_key (macros_config, section_name, skeyname);
1885 g_free (skeyname);
1886 edit_macro_sort_by_hotkey ();
1888 g_string_free (marcros_string, TRUE);
1889 mc_config_save_file (macros_config, NULL);
1890 mc_config_deinit (macros_config);
1891 return TRUE;
1894 /* --------------------------------------------------------------------------------------------- */
1896 gboolean
1897 edit_repeat_macro_cmd (WEdit * edit)
1899 int i, j;
1900 char *f;
1901 long count_repeat;
1902 char *error = NULL;
1904 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL);
1905 if (f == NULL || *f == '\0')
1907 g_free (f);
1908 return FALSE;
1911 count_repeat = strtol (f, &error, 0);
1913 if (*error != '\0')
1915 g_free (f);
1916 return FALSE;
1919 g_free (f);
1921 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1922 edit->force |= REDRAW_PAGE;
1924 for (j = 0; j < count_repeat; j++)
1925 for (i = 0; i < macro_index; i++)
1926 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1927 edit_update_screen (edit);
1928 return TRUE;
1931 /* --------------------------------------------------------------------------------------------- */
1932 /** return FALSE on error */
1934 gboolean
1935 edit_load_macro_cmd (WEdit * edit)
1937 mc_config_t *macros_config = NULL;
1938 gchar **profile_keys, **keys;
1939 gchar **values, **curr_values;
1940 gsize len, values_len;
1941 const char *section_name = "editor";
1942 gchar *macros_fname;
1943 int hotkey;
1945 (void) edit;
1947 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1948 macros_config = mc_config_init (macros_fname, TRUE);
1949 g_free (macros_fname);
1951 if (macros_config == NULL)
1952 return FALSE;
1954 profile_keys = keys = mc_config_get_keys (macros_config, section_name, &len);
1955 while (*profile_keys != NULL)
1957 gboolean have_macro;
1958 GArray *macros;
1959 macros_t macro;
1961 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1963 curr_values = values = mc_config_get_string_list (macros_config, section_name,
1964 *profile_keys, &values_len);
1965 hotkey = lookup_key (*profile_keys, NULL);
1966 have_macro = FALSE;
1968 while (*curr_values != NULL && *curr_values[0] != '\0')
1970 char **macro_pair = NULL;
1972 macro_pair = g_strsplit (*curr_values, ":", 2);
1974 if (macro_pair != NULL)
1976 macro_action_t m_act;
1977 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
1978 m_act.action = 0;
1979 else
1981 m_act.action = keybind_lookup_action (macro_pair[0]);
1982 g_free (macro_pair[0]);
1983 macro_pair[0] = NULL;
1985 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
1986 m_act.ch = -1;
1987 else
1989 m_act.ch = strtol (macro_pair[1], NULL, 0);
1990 g_free (macro_pair[1]);
1991 macro_pair[1] = NULL;
1993 if (m_act.action != 0)
1995 /* a shell command */
1996 if ((m_act.action / CK_PipeBlock (0)) == 1)
1998 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
1999 m_act.ch = -1;
2001 g_array_append_val (macros, m_act);
2002 have_macro = TRUE;
2004 g_strfreev (macro_pair);
2005 macro_pair = NULL;
2007 curr_values++;
2009 if (have_macro)
2011 macro.hotkey = hotkey;
2012 macro.macro = macros;
2013 g_array_append_val (macros_list, macro);
2015 profile_keys++;
2016 g_strfreev (values);
2018 g_strfreev (keys);
2019 mc_config_deinit (macros_config);
2020 edit_macro_sort_by_hotkey ();
2021 return TRUE;
2024 /* }}} Macro stuff end here */
2026 /* --------------------------------------------------------------------------------------------- */
2027 /** returns TRUE on success */
2029 gboolean
2030 edit_save_confirm_cmd (WEdit * edit)
2032 char *f = NULL;
2034 if (edit->filename_vpath == NULL)
2035 return edit_save_as_cmd (edit);
2037 if (!edit_check_newline (edit))
2038 return FALSE;
2040 if (edit_confirm_save)
2042 char *filename;
2043 gboolean ok;
2045 filename = vfs_path_to_str (edit->filename_vpath);
2046 f = g_strdup_printf (_("Confirm save file: \"%s\""), filename);
2047 g_free (filename);
2048 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2049 g_free (f);
2050 if (!ok)
2051 return FALSE;
2053 return edit_save_cmd (edit);
2056 /* --------------------------------------------------------------------------------------------- */
2058 * Ask file to edit and load it.
2060 * @return TRUE on success or cancel of ask.
2063 gboolean
2064 edit_load_cmd (Dlg_head * h)
2066 char *exp;
2067 gboolean ret = TRUE; /* possible cancel */
2069 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2070 MC_HISTORY_EDIT_LOAD, INPUT_LAST_TEXT);
2072 if (exp != NULL && *exp != '\0')
2074 vfs_path_t *exp_vpath;
2076 exp_vpath = vfs_path_from_str (exp);
2077 ret = edit_load_file_from_filename (h, exp_vpath);
2078 vfs_path_free (exp_vpath);
2081 g_free (exp);
2083 return ret;
2086 /* --------------------------------------------------------------------------------------------- */
2088 * Load syntax file to edit.
2090 * @return TRUE on success
2093 gboolean
2094 edit_load_syntax_file (Dlg_head * h)
2096 vfs_path_t *extdir_vpath;
2097 int dir = 0;
2098 gboolean ret = FALSE;
2100 if (geteuid () == 0)
2101 dir = query_dialog (_("Syntax file edit"),
2102 _("Which syntax file you want to edit?"), D_NORMAL, 2,
2103 _("&User"), _("&System wide"));
2105 extdir_vpath =
2106 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
2107 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
2109 vfs_path_free (extdir_vpath);
2110 extdir_vpath =
2111 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
2114 if (dir == 0)
2116 vfs_path_t *user_syntax_file_vpath;
2118 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
2119 check_for_default (extdir_vpath, user_syntax_file_vpath);
2120 ret = edit_load_file_from_filename (h, user_syntax_file_vpath);
2121 vfs_path_free (user_syntax_file_vpath);
2123 else if (dir == 1)
2124 ret = edit_load_file_from_filename (h, extdir_vpath);
2126 vfs_path_free (extdir_vpath);
2128 return ret;
2131 /* --------------------------------------------------------------------------------------------- */
2133 * Load menu file to edit.
2135 * @return TRUE on success
2138 gboolean
2139 edit_load_menu_file (Dlg_head * h)
2141 vfs_path_t *buffer_vpath;
2142 vfs_path_t *menufile_vpath;
2143 int dir;
2144 gboolean ret;
2146 dir = query_dialog (_("Menu edit"),
2147 _("Which menu file do you want to edit?"), D_NORMAL,
2148 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2150 menufile_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2151 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
2153 vfs_path_free (menufile_vpath);
2154 menufile_vpath = vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2157 switch (dir)
2159 case 0:
2160 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
2161 check_for_default (menufile_vpath, buffer_vpath);
2162 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
2163 break;
2165 case 1:
2166 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
2167 check_for_default (menufile_vpath, buffer_vpath);
2168 break;
2170 case 2:
2171 buffer_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2172 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
2174 vfs_path_free (buffer_vpath);
2175 buffer_vpath =
2176 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2178 break;
2180 default:
2181 vfs_path_free (menufile_vpath);
2182 return FALSE;
2185 ret = edit_load_file_from_filename (h, buffer_vpath);
2187 vfs_path_free (buffer_vpath);
2188 vfs_path_free (menufile_vpath);
2190 return ret;
2193 /* --------------------------------------------------------------------------------------------- */
2195 * Close window with opened file.
2197 * @return TRUE if file was closed.
2200 gboolean
2201 edit_close_cmd (WEdit * edit)
2203 gboolean ret;
2205 ret = (edit != NULL) && edit_ok_to_exit (edit);
2207 if (ret)
2209 Dlg_head *h = WIDGET (edit)->owner;
2211 if (edit->locked != 0)
2212 unlock_file (edit->filename_vpath);
2214 del_widget (edit);
2216 if (edit_widget_is_editor (WIDGET (h->current->data)))
2217 edit = (WEdit *) h->current->data;
2218 else
2220 edit = find_editor (h);
2221 if (edit != NULL)
2222 dlg_set_top_widget (edit);
2226 if (edit != NULL)
2227 edit->force |= REDRAW_COMPLETELY;
2229 return ret;
2232 /* --------------------------------------------------------------------------------------------- */
2234 if mark2 is -1 then marking is from mark1 to the cursor.
2235 Otherwise its between the markers. This handles this.
2236 Returns 1 if no text is marked.
2240 eval_marks (WEdit * edit, off_t * start_mark, off_t * end_mark)
2242 if (edit->mark1 != edit->mark2)
2244 off_t start_bol, start_eol;
2245 off_t end_bol, end_eol;
2246 off_t diff1, diff2;
2247 long col1, col2;
2248 long end_mark_curs;
2250 if (edit->end_mark_curs < 0)
2251 end_mark_curs = edit->curs1;
2252 else
2253 end_mark_curs = edit->end_mark_curs;
2255 if (edit->mark2 >= 0)
2257 *start_mark = min (edit->mark1, edit->mark2);
2258 *end_mark = max (edit->mark1, edit->mark2);
2260 else
2262 *start_mark = min (edit->mark1, end_mark_curs);
2263 *end_mark = max (edit->mark1, end_mark_curs);
2264 edit->column2 = edit->curs_col + edit->over_col;
2267 if (edit->column_highlight
2268 && (((edit->mark1 > end_mark_curs) && (edit->column1 < edit->column2))
2269 || ((edit->mark1 < end_mark_curs) && (edit->column1 > edit->column2))))
2271 start_bol = edit_bol (edit, *start_mark);
2272 start_eol = edit_eol (edit, start_bol - 1) + 1;
2273 end_bol = edit_bol (edit, *end_mark);
2274 end_eol = edit_eol (edit, *end_mark);
2275 col1 = min (edit->column1, edit->column2);
2276 col2 = max (edit->column1, edit->column2);
2278 diff1 = edit_move_forward3 (edit, start_bol, col2, 0) -
2279 edit_move_forward3 (edit, start_bol, col1, 0);
2280 diff2 = edit_move_forward3 (edit, end_bol, col2, 0) -
2281 edit_move_forward3 (edit, end_bol, col1, 0);
2283 *start_mark -= diff1;
2284 *end_mark += diff2;
2285 *start_mark = max (*start_mark, start_eol);
2286 *end_mark = min (*end_mark, end_eol);
2288 return 0;
2290 else
2292 *start_mark = *end_mark = 0;
2293 edit->column2 = edit->column1 = 0;
2294 return 1;
2298 /* --------------------------------------------------------------------------------------------- */
2300 void
2301 edit_insert_over (WEdit * edit)
2303 int i;
2305 for (i = 0; i < edit->over_col; i++)
2307 edit_insert (edit, ' ');
2309 edit->over_col = 0;
2312 /* --------------------------------------------------------------------------------------------- */
2314 off_t
2315 edit_insert_column_of_text_from_file (WEdit * edit, int file,
2316 off_t * start_pos, off_t * end_pos, long *col1, long *col2)
2318 off_t cursor;
2319 int col;
2320 off_t blocklen = -1, width = 0;
2321 unsigned char *data;
2323 cursor = edit->curs1;
2324 col = edit_get_col (edit);
2325 data = g_malloc0 (TEMP_BUF_LEN);
2327 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
2329 off_t i;
2330 for (width = 0; width < blocklen; width++)
2332 if (data[width] == '\n')
2333 break;
2335 for (i = 0; i < blocklen; i++)
2337 if (data[i] == '\n')
2338 { /* fill in and move to next line */
2339 long l;
2340 off_t p;
2341 if (edit_get_byte (edit, edit->curs1) != '\n')
2343 l = width - (edit_get_col (edit) - col);
2344 while (l > 0)
2346 edit_insert (edit, ' ');
2347 l -= space_width;
2350 for (p = edit->curs1;; p++)
2352 if (p == edit->last_byte)
2354 edit_cursor_move (edit, edit->last_byte - edit->curs1);
2355 edit_insert_ahead (edit, '\n');
2356 p++;
2357 break;
2359 if (edit_get_byte (edit, p) == '\n')
2361 p++;
2362 break;
2365 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
2366 l = col - edit_get_col (edit);
2367 while (l >= space_width)
2369 edit_insert (edit, ' ');
2370 l -= space_width;
2372 continue;
2374 edit_insert (edit, data[i]);
2377 *col1 = col;
2378 *col2 = col + width;
2379 *start_pos = cursor;
2380 *end_pos = edit->curs1;
2381 edit_cursor_move (edit, cursor - edit->curs1);
2382 g_free (data);
2384 return blocklen;
2387 /* --------------------------------------------------------------------------------------------- */
2389 void
2390 edit_block_copy_cmd (WEdit * edit)
2392 off_t start_mark, end_mark, current = edit->curs1;
2393 long col_delta = 0;
2394 off_t mark1, mark2;
2395 long c1, c2;
2396 off_t size;
2397 unsigned char *copy_buf;
2399 edit_update_curs_col (edit);
2400 if (eval_marks (edit, &start_mark, &end_mark))
2401 return;
2403 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2405 /* all that gets pushed are deletes hence little space is used on the stack */
2407 edit_push_markers (edit);
2409 if (edit->column_highlight)
2411 col_delta = abs (edit->column2 - edit->column1);
2412 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2414 else
2416 while (size--)
2417 edit_insert_ahead (edit, copy_buf[size]);
2420 g_free (copy_buf);
2421 edit_scroll_screen_over_cursor (edit);
2423 if (edit->column_highlight)
2424 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2425 else if (start_mark < current && end_mark > current)
2426 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2428 edit->force |= REDRAW_PAGE;
2432 /* --------------------------------------------------------------------------------------------- */
2434 void
2435 edit_block_move_cmd (WEdit * edit)
2437 off_t current;
2438 unsigned char *copy_buf = NULL;
2439 off_t start_mark, end_mark;
2440 long line;
2442 if (eval_marks (edit, &start_mark, &end_mark))
2443 return;
2445 if (!edit->column_highlight && edit->curs1 > start_mark && edit->curs1 < end_mark)
2446 return;
2448 line = edit->curs_line;
2449 if (edit->mark2 < 0)
2450 edit_mark_cmd (edit, FALSE);
2451 edit_push_markers (edit);
2453 if (edit->column_highlight)
2455 off_t mark1, mark2;
2456 off_t size;
2457 long c1, c2, b_width;
2458 long x, x2;
2460 c1 = min (edit->column1, edit->column2);
2461 c2 = max (edit->column1, edit->column2);
2462 b_width = c2 - c1;
2464 edit_update_curs_col (edit);
2466 x = edit->curs_col;
2467 x2 = x + edit->over_col;
2469 /* do nothing when cursor inside first line of selected area */
2470 if ((edit_eol (edit, edit->curs1) == edit_eol (edit, start_mark)) && x2 > c1 && x2 <= c2)
2471 return;
2473 if (edit->curs1 > start_mark && edit->curs1 < edit_eol (edit, end_mark))
2475 if (x > c2)
2476 x -= b_width;
2477 else if (x > c1 && x <= c2)
2478 x = c1;
2480 /* save current selection into buffer */
2481 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2483 /* remove current selection */
2484 edit_block_delete_cmd (edit);
2486 edit->over_col = max (0, edit->over_col - b_width);
2487 /* calculate the cursor pos after delete block */
2488 current = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), x, 0);
2489 edit_cursor_move (edit, current - edit->curs1);
2490 edit_scroll_screen_over_cursor (edit);
2492 /* add TWS if need before block insertion */
2493 if (option_cursor_beyond_eol && edit->over_col > 0)
2494 edit_insert_over (edit);
2496 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2497 edit_set_markers (edit, mark1, mark2, c1, c2);
2499 else
2501 off_t count;
2503 current = edit->curs1;
2504 copy_buf = g_malloc0 (end_mark - start_mark);
2505 edit_cursor_move (edit, start_mark - edit->curs1);
2506 edit_scroll_screen_over_cursor (edit);
2508 for (count = start_mark; count < end_mark; count++)
2509 copy_buf[end_mark - count - 1] = edit_delete (edit, TRUE);
2511 edit_scroll_screen_over_cursor (edit);
2512 edit_cursor_move (edit,
2513 current - edit->curs1 -
2514 (((current - edit->curs1) > 0) ? end_mark - start_mark : 0));
2515 edit_scroll_screen_over_cursor (edit);
2516 while (count-- > start_mark)
2517 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2518 edit_set_markers (edit, edit->curs1, edit->curs1 + end_mark - start_mark, 0, 0);
2521 edit_scroll_screen_over_cursor (edit);
2522 g_free (copy_buf);
2523 edit->force |= REDRAW_PAGE;
2526 /* --------------------------------------------------------------------------------------------- */
2527 /** returns 1 if canceelled by user */
2530 edit_block_delete_cmd (WEdit * edit)
2532 off_t start_mark, end_mark;
2533 if (eval_marks (edit, &start_mark, &end_mark))
2535 edit_delete_line (edit);
2536 return 0;
2538 return edit_block_delete (edit);
2541 /* --------------------------------------------------------------------------------------------- */
2542 /** call with edit = 0 before shutdown to close memory leaks */
2544 void
2545 edit_replace_cmd (WEdit * edit, int again)
2547 /* 1 = search string, 2 = replace with */
2548 static char *saved1 = NULL; /* saved default[123] */
2549 static char *saved2 = NULL;
2550 char *input1 = NULL; /* user input from the dialog */
2551 char *input2 = NULL;
2552 GString *input2_str = NULL;
2553 char *disp1 = NULL;
2554 char *disp2 = NULL;
2555 long times_replaced = 0;
2556 gboolean once_found = FALSE;
2558 if (!edit)
2560 g_free (saved1), saved1 = NULL;
2561 g_free (saved2), saved2 = NULL;
2562 return;
2565 edit->force |= REDRAW_COMPLETELY;
2567 if (again && !saved1 && !saved2)
2568 again = 0;
2570 if (again)
2572 input1 = g_strdup (saved1 ? saved1 : "");
2573 input2 = g_strdup (saved2 ? saved2 : "");
2575 else
2577 char *tmp_inp1, *tmp_inp2;
2579 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
2580 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
2582 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2584 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2586 g_free (disp1);
2587 g_free (disp2);
2589 if (input1 == NULL || *input1 == '\0')
2591 edit->force = REDRAW_COMPLETELY;
2592 goto cleanup;
2595 tmp_inp1 = input1;
2596 tmp_inp2 = input2;
2597 input1 = edit_replace_cmd__conv_to_input (input1);
2598 input2 = edit_replace_cmd__conv_to_input (input2);
2599 g_free (tmp_inp1);
2600 g_free (tmp_inp2);
2602 g_free (saved1), saved1 = g_strdup (input1);
2603 g_free (saved2), saved2 = g_strdup (input2);
2605 mc_search_free (edit->search);
2606 edit->search = NULL;
2609 input2_str = g_string_new (input2);
2611 if (!edit->search)
2613 edit->search = mc_search_new (input1, -1);
2614 if (edit->search == NULL)
2616 edit->search_start = edit->curs1;
2617 goto cleanup;
2619 edit->search->search_type = edit_search_options.type;
2620 edit->search->is_all_charsets = edit_search_options.all_codepages;
2621 edit->search->is_case_sensitive = edit_search_options.case_sens;
2622 edit->search->whole_words = edit_search_options.whole_words;
2623 edit->search->search_fn = edit_search_cmd_callback;
2624 edit->search_line_type = edit_get_search_line_type (edit->search);
2625 edit_search_fix_search_start_if_selection (edit);
2628 if (edit->found_len && edit->search_start == edit->found_start + 1
2629 && edit_search_options.backwards)
2630 edit->search_start--;
2632 if (edit->found_len && edit->search_start == edit->found_start - 1
2633 && !edit_search_options.backwards)
2634 edit->search_start++;
2638 gsize len = 0;
2640 if (!editcmd_find (edit, &len))
2642 if (!(edit->search->error == MC_SEARCH_E_OK ||
2643 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2645 edit_error_dialog (_("Search"), edit->search->error_str);
2647 break;
2649 once_found = TRUE;
2651 edit->search_start = edit->search->normal_offset;
2652 /*returns negative on not found or error in pattern */
2654 if ((edit->search_start >= 0) && (edit->search_start < edit->last_byte))
2656 gsize i;
2657 GString *repl_str;
2659 edit->found_start = edit->search_start;
2660 i = edit->found_len = len;
2662 edit_cursor_move (edit, edit->search_start - edit->curs1);
2663 edit_scroll_screen_over_cursor (edit);
2665 if (edit->replace_mode == 0)
2667 long l;
2668 int prompt;
2670 l = edit->curs_row - WIDGET (edit)->lines / 3;
2671 if (l > 0)
2672 edit_scroll_downward (edit, l);
2673 if (l < 0)
2674 edit_scroll_upward (edit, -l);
2676 edit_scroll_screen_over_cursor (edit);
2677 edit->force |= REDRAW_PAGE;
2678 edit_render_keypress (edit);
2680 /*so that undo stops at each query */
2681 edit_push_key_press (edit);
2682 /* and prompt 2/3 down */
2683 disp1 = edit_replace_cmd__conv_to_display (saved1);
2684 disp2 = edit_replace_cmd__conv_to_display (saved2);
2685 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2686 g_free (disp1);
2687 g_free (disp2);
2689 if (prompt == B_REPLACE_ALL)
2690 edit->replace_mode = 1;
2691 else if (prompt == B_SKIP_REPLACE)
2693 if (edit_search_options.backwards)
2694 edit->search_start--;
2695 else
2696 edit->search_start++;
2697 continue; /* loop */
2699 else if (prompt == B_CANCEL)
2701 edit->replace_mode = -1;
2702 break; /* loop */
2706 repl_str = mc_search_prepare_replace_str (edit->search, input2_str);
2708 if (edit->search->error != MC_SEARCH_E_OK)
2710 edit_error_dialog (_("Replace"), edit->search->error_str);
2711 g_string_free (repl_str, TRUE);
2712 break;
2715 /* delete then insert new */
2716 for (i = 0; i < len; i++)
2717 edit_delete (edit, TRUE);
2719 for (i = 0; i < repl_str->len; i++)
2720 edit_insert (edit, repl_str->str[i]);
2722 edit->found_len = repl_str->len;
2723 g_string_free (repl_str, TRUE);
2724 times_replaced++;
2726 /* so that we don't find the same string again */
2727 if (edit_search_options.backwards)
2729 edit->search_start--;
2731 else
2733 edit->search_start += edit->found_len + (len == 0 ? 1 : 0);
2735 if (edit->search_start >= edit->last_byte)
2736 break;
2739 edit_scroll_screen_over_cursor (edit);
2741 else
2743 /* try and find from right here for next search */
2744 edit->search_start = edit->curs1;
2745 edit_update_curs_col (edit);
2747 edit->force |= REDRAW_PAGE;
2748 edit_render_keypress (edit);
2750 if (times_replaced == 0)
2751 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL, 1, _("&OK"));
2752 break;
2755 while (edit->replace_mode >= 0);
2757 edit_scroll_screen_over_cursor (edit);
2758 edit->force |= REDRAW_COMPLETELY;
2759 edit_render_keypress (edit);
2761 if ((edit->replace_mode == 1) && (times_replaced != 0))
2762 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2764 cleanup:
2765 g_free (input1);
2766 g_free (input2);
2767 if (input2_str != NULL)
2768 g_string_free (input2_str, TRUE);
2771 /* --------------------------------------------------------------------------------------------- */
2773 mc_search_cbret_t
2774 edit_search_cmd_callback (const void *user_data, gsize char_offset, int *current_char)
2776 *current_char = edit_get_byte ((WEdit *) user_data, (off_t) char_offset);
2777 return MC_SEARCH_CB_OK;
2780 /* --------------------------------------------------------------------------------------------- */
2782 void
2783 edit_search_cmd (WEdit * edit, gboolean again)
2786 if (edit == NULL)
2787 return;
2789 if (!again)
2790 edit_search (edit);
2791 else if (edit->last_search_string != NULL)
2792 edit_do_search (edit);
2793 else
2795 /* find last search string in history */
2796 GList *history;
2798 history = history_get (MC_HISTORY_SHARED_SEARCH);
2799 if (history != NULL && history->data != NULL)
2801 edit->last_search_string = (char *) history->data;
2802 history->data = NULL;
2803 history = g_list_first (history);
2804 g_list_foreach (history, (GFunc) g_free, NULL);
2805 g_list_free (history);
2807 edit->search = mc_search_new (edit->last_search_string, -1);
2808 if (edit->search == NULL)
2810 /* if not... then ask for an expression */
2811 g_free (edit->last_search_string);
2812 edit->last_search_string = NULL;
2813 edit_search (edit);
2815 else
2817 edit->search->search_type = edit_search_options.type;
2818 edit->search->is_all_charsets = edit_search_options.all_codepages;
2819 edit->search->is_case_sensitive = edit_search_options.case_sens;
2820 edit->search->whole_words = edit_search_options.whole_words;
2821 edit->search->search_fn = edit_search_cmd_callback;
2822 edit->search_line_type = edit_get_search_line_type (edit->search);
2823 edit_do_search (edit);
2826 else
2828 /* if not... then ask for an expression */
2829 g_free (edit->last_search_string);
2830 edit->last_search_string = NULL;
2831 edit_search (edit);
2837 /* --------------------------------------------------------------------------------------------- */
2839 * Check if it's OK to close the file. If there are unsaved changes, ask user.
2841 * @return TRUE if it's OK to exit, FALSE to continue editing.
2844 gboolean
2845 edit_ok_to_exit (WEdit * edit)
2847 char *fname = (char *) N_("[NoName]");
2848 char *msg;
2849 int act;
2851 if (!edit->modified)
2852 return TRUE;
2854 if (edit->filename_vpath != NULL)
2855 fname = vfs_path_to_str (edit->filename_vpath);
2856 #ifdef ENABLE_NLS
2857 else
2858 fname = g_strdup (_(fname));
2859 #else
2860 else
2861 fname = g_strdup (fname);
2862 #endif
2864 if (!mc_global.midnight_shutdown)
2866 if (!edit_check_newline (edit))
2868 g_free (fname);
2869 return FALSE;
2872 query_set_sel (2);
2874 msg = g_strdup_printf (_("File %s was modified.\nSave before close?"), fname);
2875 act = edit_query_dialog3 (_("Close file"), msg, _("&Yes"), _("&No"), _("&Cancel"));
2877 else
2879 msg = g_strdup_printf (_("Midnight Commander is being shut down.\nSave modified file %s?"),
2880 fname);
2881 act = edit_query_dialog2 (_("Quit"), msg, _("&Yes"), _("&No"));
2883 /* Esc is No */
2884 if (act == -1)
2885 act = 1;
2888 g_free (msg);
2889 g_free (fname);
2891 switch (act)
2893 case 0: /* Yes */
2894 edit_push_markers (edit);
2895 edit_set_markers (edit, 0, 0, 0, 0);
2896 if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2897 return mc_global.midnight_shutdown;
2898 break;
2899 case 1: /* No */
2900 break;
2901 case 2: /* Cancel quit */
2902 case -1: /* Esc */
2903 return FALSE;
2906 return TRUE;
2909 /* --------------------------------------------------------------------------------------------- */
2910 /** save block, returns TRUE on success */
2912 gboolean
2913 edit_save_block (WEdit * edit, const char *filename, off_t start, off_t finish)
2915 int file;
2916 off_t len = 1;
2917 vfs_path_t *vpath;
2919 vpath = vfs_path_from_str (filename);
2920 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2921 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2922 vfs_path_free (vpath);
2923 if (file == -1)
2924 return FALSE;
2926 if (edit->column_highlight)
2928 int r;
2930 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2931 if (r > 0)
2933 unsigned char *block, *p;
2935 p = block = edit_get_block (edit, start, finish, &len);
2936 while (len)
2938 r = mc_write (file, p, len);
2939 if (r < 0)
2940 break;
2941 p += r;
2942 len -= r;
2944 g_free (block);
2947 else
2949 unsigned char *buf;
2950 off_t i = start;
2951 off_t end;
2953 len = finish - start;
2954 buf = g_malloc0 (TEMP_BUF_LEN);
2955 while (start != finish)
2957 end = min (finish, start + TEMP_BUF_LEN);
2958 for (; i < end; i++)
2959 buf[i - start] = edit_get_byte (edit, i);
2960 len -= mc_write (file, (char *) buf, end - start);
2961 start = end;
2963 g_free (buf);
2965 mc_close (file);
2967 return (len == 0);
2970 /* --------------------------------------------------------------------------------------------- */
2972 void
2973 edit_paste_from_history (WEdit * edit)
2975 (void) edit;
2976 edit_error_dialog (_("Error"), _("This function is not implemented"));
2979 /* --------------------------------------------------------------------------------------------- */
2981 gboolean
2982 edit_copy_to_X_buf_cmd (WEdit * edit)
2984 off_t start_mark, end_mark;
2986 if (eval_marks (edit, &start_mark, &end_mark))
2987 return TRUE;
2988 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2990 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2991 return FALSE;
2993 /* try use external clipboard utility */
2994 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2996 return TRUE;
2999 /* --------------------------------------------------------------------------------------------- */
3001 gboolean
3002 edit_cut_to_X_buf_cmd (WEdit * edit)
3004 off_t start_mark, end_mark;
3006 if (eval_marks (edit, &start_mark, &end_mark))
3007 return TRUE;
3008 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
3010 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
3011 return FALSE;
3013 /* try use external clipboard utility */
3014 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
3016 edit_block_delete_cmd (edit);
3017 edit_mark_cmd (edit, TRUE);
3019 return TRUE;
3022 /* --------------------------------------------------------------------------------------------- */
3024 gboolean
3025 edit_paste_from_X_buf_cmd (WEdit * edit)
3027 vfs_path_t *tmp;
3028 gboolean ret;
3030 /* try use external clipboard utility */
3031 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
3032 tmp = mc_config_get_full_vpath (EDIT_CLIP_FILE);
3033 ret = (edit_insert_file (edit, tmp) >= 0);
3034 vfs_path_free (tmp);
3036 return ret;
3039 /* --------------------------------------------------------------------------------------------- */
3041 * Ask user for the line and go to that line.
3042 * Negative numbers mean line from the end (i.e. -1 is the last line).
3045 void
3046 edit_goto_cmd (WEdit * edit)
3048 char *f;
3049 static long line = 0; /* line as typed, saved as default */
3050 long l;
3051 char *error;
3052 char s[32];
3054 g_snprintf (s, sizeof (s), "%ld", line);
3055 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE, line ? s : "");
3056 if (!f)
3057 return;
3059 if (!*f)
3061 g_free (f);
3062 return;
3065 l = strtol (f, &error, 0);
3066 if (*error)
3068 g_free (f);
3069 return;
3072 line = l;
3073 if (l < 0)
3074 l = edit->total_lines + l + 2;
3075 edit_move_display (edit, l - WIDGET (edit)->lines / 2 - 1);
3076 edit_move_to_line (edit, l - 1);
3077 edit->force |= REDRAW_COMPLETELY;
3078 g_free (f);
3082 /* --------------------------------------------------------------------------------------------- */
3083 /** Return TRUE on success */
3085 gboolean
3086 edit_save_block_cmd (WEdit * edit)
3088 off_t start_mark, end_mark;
3089 char *exp, *tmp;
3090 gboolean ret = FALSE;
3092 if (eval_marks (edit, &start_mark, &end_mark))
3093 return TRUE;
3095 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3096 exp =
3097 input_expand_dialog (_("Save block"), _("Enter file name:"),
3098 MC_HISTORY_EDIT_SAVE_BLOCK, tmp);
3099 g_free (tmp);
3100 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3102 if (exp != NULL && *exp != '\0')
3104 if (edit_save_block (edit, exp, start_mark, end_mark))
3105 ret = TRUE;
3106 else
3107 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3109 edit->force |= REDRAW_COMPLETELY;
3112 g_free (exp);
3114 return ret;
3118 /* --------------------------------------------------------------------------------------------- */
3120 /** returns TRUE on success */
3121 gboolean
3122 edit_insert_file_cmd (WEdit * edit)
3124 char *tmp;
3125 char *exp;
3126 gboolean ret = FALSE;
3128 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3129 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3130 MC_HISTORY_EDIT_INSERT_FILE, tmp);
3131 g_free (tmp);
3133 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3135 if (exp != NULL && *exp != '\0')
3137 vfs_path_t *exp_vpath;
3139 exp_vpath = vfs_path_from_str (exp);
3140 ret = (edit_insert_file (edit, exp_vpath) >= 0);
3141 vfs_path_free (exp_vpath);
3143 if (!ret)
3144 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3147 g_free (exp);
3149 edit->force |= REDRAW_COMPLETELY;
3150 return ret;
3153 /* --------------------------------------------------------------------------------------------- */
3154 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3157 edit_sort_cmd (WEdit * edit)
3159 static char *old = 0;
3160 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3161 off_t start_mark, end_mark;
3162 int e;
3164 if (eval_marks (edit, &start_mark, &end_mark))
3166 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3167 return 0;
3170 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
3171 edit_save_block (edit, tmp, start_mark, end_mark);
3172 g_free (tmp);
3174 exp = input_dialog (_("Run sort"),
3175 _("Enter sort options (see manpage) separated by whitespace:"),
3176 MC_HISTORY_EDIT_SORT, (old != NULL) ? old : "");
3178 if (!exp)
3179 return 1;
3180 g_free (old);
3181 old = exp;
3182 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
3183 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
3184 tmp =
3185 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3186 " > ", tmp_edit_temp_name, (char *) NULL);
3187 g_free (tmp_edit_temp_name);
3188 g_free (tmp_edit_block_name);
3190 e = system (tmp);
3191 g_free (tmp);
3192 if (e)
3194 if (e == -1 || e == 127)
3196 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3198 else
3200 char q[8];
3201 sprintf (q, "%d ", e);
3202 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3203 edit_error_dialog (_("Sort"), tmp);
3204 g_free (tmp);
3206 return -1;
3209 edit->force |= REDRAW_COMPLETELY;
3211 if (edit_block_delete_cmd (edit))
3212 return 1;
3215 vfs_path_t *tmp_vpath;
3217 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3218 edit_insert_file (edit, tmp_vpath);
3219 vfs_path_free (tmp_vpath);
3221 return 0;
3224 /* --------------------------------------------------------------------------------------------- */
3226 * Ask user for a command, execute it and paste its output back to the
3227 * editor.
3231 edit_ext_cmd (WEdit * edit)
3233 char *exp, *tmp, *tmp_edit_temp_file;
3234 int e;
3236 exp =
3237 input_dialog (_("Paste output of external command"),
3238 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, NULL);
3240 if (!exp)
3241 return 1;
3243 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3244 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3245 g_free (tmp_edit_temp_file);
3246 e = system (tmp);
3247 g_free (tmp);
3248 g_free (exp);
3250 if (e)
3252 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3253 return -1;
3256 edit->force |= REDRAW_COMPLETELY;
3259 vfs_path_t *tmp_vpath;
3261 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3262 edit_insert_file (edit, tmp_vpath);
3263 vfs_path_free (tmp_vpath);
3265 return 0;
3268 /* --------------------------------------------------------------------------------------------- */
3269 /** if block is 1, a block must be highlighted and the shell command
3270 processes it. If block is 0 the shell command is a straight system
3271 command, that just produces some output which is to be inserted */
3273 void
3274 edit_block_process_cmd (WEdit * edit, int macro_number)
3276 char *fname;
3277 char *macros_fname = NULL;
3279 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3280 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3281 user_menu (edit, macros_fname, 0);
3282 g_free (fname);
3283 g_free (macros_fname);
3284 edit->force |= REDRAW_COMPLETELY;
3287 /* --------------------------------------------------------------------------------------------- */
3289 void
3290 edit_mail_dialog (WEdit * edit)
3292 char *tmail_to;
3293 char *tmail_subject;
3294 char *tmail_cc;
3296 static char *mail_cc_last = 0;
3297 static char *mail_subject_last = 0;
3298 static char *mail_to_last = 0;
3300 QuickWidget quick_widgets[] = {
3301 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
3302 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
3303 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input", &tmail_cc),
3304 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT, N_("Copies to")),
3305 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-2",
3306 &tmail_subject),
3307 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT, N_("Subject")),
3308 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-3", &tmail_to),
3309 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT, N_("To")),
3310 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT, N_("mail -s <subject> -c <cc> <to>")),
3311 QUICK_END
3314 QuickDialog Quick_input = {
3315 50, MAIL_DLG_HEIGHT, -1, -1, N_("Mail"),
3316 "[Input Line Keys]", quick_widgets, NULL, NULL, FALSE
3319 quick_widgets[2].u.input.text = mail_cc_last ? mail_cc_last : "";
3320 quick_widgets[4].u.input.text = mail_subject_last ? mail_subject_last : "";
3321 quick_widgets[6].u.input.text = mail_to_last ? mail_to_last : "";
3323 if (quick_dialog (&Quick_input) != B_CANCEL)
3325 g_free (mail_cc_last);
3326 g_free (mail_subject_last);
3327 g_free (mail_to_last);
3328 mail_cc_last = tmail_cc;
3329 mail_subject_last = tmail_subject;
3330 mail_to_last = tmail_to;
3331 pipe_mail (edit, mail_to_last, mail_subject_last, mail_cc_last);
3336 /*******************/
3337 /* Word Completion */
3338 /*******************/
3340 /* --------------------------------------------------------------------------------------------- */
3342 * Complete current word using regular expression search
3343 * backwards beginning at the current cursor position.
3346 void
3347 edit_complete_word_cmd (WEdit * edit)
3349 gsize i, max_len, word_len = 0, num_compl = 0;
3350 off_t word_start = 0;
3351 unsigned char *bufpos;
3352 char *match_expr;
3353 struct selection compl[MAX_WORD_COMPLETIONS]; /* completions */
3355 /* search start of word to be completed */
3356 if (!edit_find_word_start (edit, &word_start, &word_len))
3357 return;
3359 /* prepare match expression */
3360 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3362 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3363 match_expr =
3364 g_strdup_printf
3365 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3366 (int) word_len, bufpos);
3368 /* collect the possible completions */
3369 /* start search from begin to end of file */
3370 max_len =
3371 edit_collect_completions (edit, word_start, word_len, match_expr,
3372 (struct selection *) &compl, &num_compl);
3374 if (num_compl > 0)
3376 /* insert completed word if there is only one match */
3377 if (num_compl == 1)
3379 for (i = word_len; i < compl[0].len; i++)
3380 edit_insert (edit, *(compl[0].text + i));
3382 /* more than one possible completion => ask the user */
3383 else
3385 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3386 /* !!! pressed again the selection dialog pops up, but that !!! */
3387 /* !!! seems to require a further internal state !!! */
3388 /*tty_beep (); */
3390 /* let the user select the preferred completion */
3391 editcmd_dialog_completion_show (edit, max_len, word_len,
3392 (struct selection *) &compl, num_compl);
3396 g_free (match_expr);
3397 /* release memory before return */
3398 for (i = 0; i < num_compl; i++)
3399 g_free (compl[i].text);
3402 /* --------------------------------------------------------------------------------------------- */
3404 #ifdef HAVE_CHARSET
3405 void
3406 edit_select_codepage_cmd (WEdit * edit)
3408 if (do_select_codepage ())
3409 edit_set_codeset (edit);
3411 edit->force = REDRAW_PAGE;
3412 send_message (WIDGET (edit), NULL, WIDGET_DRAW, 0, NULL);
3414 #endif
3416 /* --------------------------------------------------------------------------------------------- */
3418 void
3419 edit_insert_literal_cmd (WEdit * edit)
3421 int char_for_insertion;
3423 char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3424 _("Press any key:"), FALSE);
3425 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3428 /* --------------------------------------------------------------------------------------------- */
3430 void
3431 edit_begin_end_macro_cmd (WEdit * edit)
3433 /* edit is a pointer to the widget */
3434 if (edit != NULL)
3436 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3437 edit_execute_key_command (edit, command, -1);
3441 /* --------------------------------------------------------------------------------------------- */
3443 void
3444 edit_begin_end_repeat_cmd (WEdit * edit)
3446 /* edit is a pointer to the widget */
3447 if (edit != NULL)
3449 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3450 edit_execute_key_command (edit, command, -1);
3454 /* --------------------------------------------------------------------------------------------- */
3456 gboolean
3457 edit_load_forward_cmd (WEdit * edit)
3459 if (edit->modified
3460 && edit_query_dialog2 (_("Warning"),
3461 _("Current text was modified without a file save.\n"
3462 "Continue discards these changes"), _("C&ontinue"),
3463 _("&Cancel")) == 1)
3465 edit->force |= REDRAW_COMPLETELY;
3466 return TRUE;
3469 if (edit_stack_iterator + 1 >= MAX_HISTORY_MOVETO)
3470 return FALSE;
3472 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3473 return FALSE;
3475 edit_stack_iterator++;
3476 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3477 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3478 edit_history_moveto[edit_stack_iterator].line);
3480 return FALSE;
3483 /* --------------------------------------------------------------------------------------------- */
3485 gboolean
3486 edit_load_back_cmd (WEdit * edit)
3488 if (edit->modified
3489 && edit_query_dialog2 (_("Warning"),
3490 _("Current text was modified without a file save.\n"
3491 "Continue discards these changes"), _("C&ontinue"),
3492 _("&Cancel")) == 1)
3494 edit->force |= REDRAW_COMPLETELY;
3495 return TRUE;
3498 /* we are in the bottom of the stack, NO WAY! */
3499 if (edit_stack_iterator == 0)
3500 return FALSE;
3502 edit_stack_iterator--;
3503 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3504 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3505 edit_history_moveto[edit_stack_iterator].line);
3507 return FALSE;
3510 /* --------------------------------------------------------------------------------------------- */
3512 void
3513 edit_get_match_keyword_cmd (WEdit * edit)
3515 gsize word_len = 0, max_len = 0;
3516 int num_def = 0;
3517 int i;
3518 off_t word_start = 0;
3519 unsigned char *bufpos;
3520 char *match_expr;
3521 char *path = NULL;
3522 char *ptr = NULL;
3523 char *tagfile = NULL;
3525 etags_hash_t def_hash[MAX_DEFINITIONS];
3527 for (i = 0; i < MAX_DEFINITIONS; i++)
3529 def_hash[i].filename = NULL;
3532 /* search start of word to be completed */
3533 if (!edit_find_word_start (edit, &word_start, &word_len))
3534 return;
3536 /* prepare match expression */
3537 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3538 match_expr = g_strdup_printf ("%.*s", (int) word_len, bufpos);
3540 ptr = g_get_current_dir ();
3541 path = g_strconcat (ptr, G_DIR_SEPARATOR_S, (char *) NULL);
3542 g_free (ptr);
3544 /* Recursive search file 'TAGS' in parent dirs */
3547 ptr = g_path_get_dirname (path);
3548 g_free (path);
3549 path = ptr;
3550 g_free (tagfile);
3551 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3552 if (exist_file (tagfile))
3553 break;
3555 while (strcmp (path, G_DIR_SEPARATOR_S) != 0);
3557 if (tagfile)
3559 num_def =
3560 etags_set_definition_hash (tagfile, path, match_expr, (etags_hash_t *) & def_hash);
3561 g_free (tagfile);
3563 g_free (path);
3565 max_len = MAX_WIDTH_DEF_DIALOG;
3566 word_len = 0;
3567 if (num_def > 0)
3569 editcmd_dialog_select_definition_show (edit, match_expr, max_len, word_len,
3570 (etags_hash_t *) & def_hash, num_def);
3572 g_free (match_expr);
3575 /* --------------------------------------------------------------------------------------------- */
3577 #ifdef HAVE_ASPELL
3579 edit_suggest_current_word (WEdit * edit)
3581 gsize cut_len = 0;
3582 gsize word_len = 0;
3583 off_t word_start = 0;
3584 int retval = B_SKIP_WORD;
3585 char *match_word;
3587 /* search start of word to spell check */
3588 match_word = edit_get_word_from_pos (edit, edit->curs1, &word_start, &word_len, &cut_len);
3590 #ifdef HAVE_CHARSET
3591 if (mc_global.source_codepage >= 0 && (mc_global.source_codepage != mc_global.display_codepage))
3593 GString *tmp_word;
3595 tmp_word = str_convert_to_display (match_word);
3596 g_free (match_word);
3597 match_word = g_string_free (tmp_word, FALSE);
3599 #endif
3600 if (!aspell_check (match_word, (int) word_len))
3602 GArray *suggest;
3603 unsigned int res;
3605 suggest = g_array_new (TRUE, FALSE, sizeof (char *));
3607 res = aspell_suggest (suggest, match_word, (int) word_len);
3608 if (res != 0)
3610 char *new_word = NULL;
3612 edit->found_start = word_start;
3613 edit->found_len = word_len;
3614 edit->force |= REDRAW_PAGE;
3615 edit_scroll_screen_over_cursor (edit);
3616 edit_render_keypress (edit);
3618 retval = spell_dialog_spell_suggest_show (edit, match_word, &new_word, suggest);
3619 edit_cursor_move (edit, word_len - cut_len);
3621 if (retval == B_ENTER && new_word != NULL)
3623 guint i;
3624 char *cp_word;
3626 #ifdef HAVE_CHARSET
3627 if (mc_global.source_codepage >= 0 &&
3628 (mc_global.source_codepage != mc_global.display_codepage))
3630 GString *tmp_word;
3632 tmp_word = str_convert_to_input (new_word);
3633 g_free (new_word);
3634 new_word = g_string_free (tmp_word, FALSE);
3636 #endif
3637 cp_word = new_word;
3638 for (i = 0; i < word_len; i++)
3639 edit_backspace (edit, TRUE);
3640 for (; *new_word; new_word++)
3641 edit_insert (edit, *new_word);
3642 g_free (cp_word);
3644 else if (retval == B_ADD_WORD && match_word != NULL)
3645 aspell_add_to_dict (match_word, (int) word_len);
3648 g_array_free (suggest, TRUE);
3649 edit->found_start = 0;
3650 edit->found_len = 0;
3652 g_free (match_word);
3653 return retval;
3656 /* --------------------------------------------------------------------------------------------- */
3658 void
3659 edit_spellcheck_file (WEdit * edit)
3661 if (edit->curs_line > 0)
3663 edit_cursor_move (edit, -edit->curs1);
3664 edit_move_to_prev_col (edit, 0);
3665 edit_update_curs_row (edit);
3670 int c1, c2;
3672 c2 = edit_get_byte (edit, edit->curs1);
3676 if (edit->curs1 >= edit->last_byte)
3677 return;
3679 c1 = c2;
3680 edit_cursor_move (edit, 1);
3681 c2 = edit_get_byte (edit, edit->curs1);
3683 while (is_break_char (c1) || is_break_char (c2));
3685 while (edit_suggest_current_word (edit) != B_CANCEL);
3688 /* --------------------------------------------------------------------------------------------- */
3690 void
3691 edit_set_spell_lang (void)
3693 GArray *lang_list;
3695 lang_list = g_array_new (TRUE, FALSE, sizeof (char *));
3696 if (aspell_get_lang_list (lang_list) != 0)
3698 char *lang;
3700 lang = spell_dialog_lang_list_show (lang_list);
3701 if (lang != NULL)
3702 (void) aspell_set_lang (lang);
3704 aspell_array_clean (lang_list);
3706 #endif /* HAVE_ASPELL */
3708 /* --------------------------------------------------------------------------------------------- */