Use long for line numbers and columns.
[midnight-commander.git] / src / editor / editcmd.c
blobf3db1b72cfbff04a6c85ab6501b89d7dedd2333b
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 #include "src/main.h" /* mactos_t */
72 #ifdef HAVE_CHARSET
73 #include "src/selcodepage.h"
74 #endif
75 #include "src/keybind-defaults.h"
76 #include "src/util.h" /* check_for_default() */
77 #include "src/filemanager/layout.h" /* mc_refresh() */
79 #include "edit-impl.h"
80 #include "editwidget.h"
81 #include "editcmd_dialogs.h"
82 #ifdef HAVE_ASPELL
83 #include "spell.h"
84 #include "spell_dialogs.h"
85 #endif
86 #include "etags.h"
88 /*** global variables ****************************************************************************/
90 /* search and replace: */
91 int search_create_bookmark = FALSE;
93 /* queries on a save */
94 int edit_confirm_save = 1;
96 /*** file scope macro definitions ****************************************************************/
98 #define space_width 1
100 #define TEMP_BUF_LEN 1024
102 #define INPUT_INDEX 9
104 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
105 (and the above) routines to work properly - paul */
107 #define is_digit(x) ((x) >= '0' && (x) <= '9')
109 #define MAIL_DLG_HEIGHT 12
111 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
113 /*** file scope type declarations ****************************************************************/
115 /*** file scope variables ************************************************************************/
117 /*** file scope functions ************************************************************************/
118 /* --------------------------------------------------------------------------------------------- */
120 /* If 0 (quick save) then a) create/truncate <filename> file,
121 b) save to <filename>;
122 if 1 (safe save) then a) save to <tempnam>,
123 b) rename <tempnam> to <filename>;
124 if 2 (do backups) then a) save to <tempnam>,
125 b) rename <filename> to <filename.backup_ext>,
126 c) rename <tempnam> to <filename>. */
128 /* returns 0 on error, -1 on abort */
130 static int
131 edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath)
133 char *p;
134 gchar *tmp;
135 long filelen = 0;
136 int this_save_mode, fd = -1;
137 vfs_path_t *real_filename_vpath;
138 vfs_path_t *savename_vpath = NULL;
139 const char *start_filename;
140 const vfs_path_element_t *vpath_element;
142 vpath_element = vfs_path_get_by_index (filename_vpath, 0);
143 if (vpath_element == NULL)
144 return 0;
146 start_filename = vpath_element->path;
147 if (*start_filename == '\0')
148 return 0;
150 if (*start_filename != PATH_SEP && edit->dir_vpath != NULL)
152 real_filename_vpath = vfs_path_append_vpath_new (edit->dir_vpath, filename_vpath, NULL);
154 else
156 real_filename_vpath = vfs_path_clone (filename_vpath);
159 this_save_mode = option_save_mode;
160 if (this_save_mode != EDIT_QUICK_SAVE)
162 if (!vfs_file_is_local (real_filename_vpath)
163 || (fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY)) == -1)
166 * The file does not exists yet, so no safe save or
167 * backup are necessary.
169 this_save_mode = EDIT_QUICK_SAVE;
171 if (fd != -1)
172 mc_close (fd);
175 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt)
177 int rv;
178 struct stat sb;
180 rv = mc_stat (real_filename_vpath, &sb);
181 if (rv == 0 && sb.st_nlink > 1)
183 rv = edit_query_dialog3 (_("Warning"),
184 _("File has hard-links. Detach before saving?"),
185 _("&Yes"), _("&No"), _("&Cancel"));
186 switch (rv)
188 case 0:
189 this_save_mode = EDIT_SAFE_SAVE;
190 /* fallthrough */
191 case 1:
192 edit->skip_detach_prompt = 1;
193 break;
194 default:
195 vfs_path_free (real_filename_vpath);
196 return -1;
200 /* Prevent overwriting changes from other editor sessions. */
201 if (rv == 0 && edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
203 /* The default action is "Cancel". */
204 query_set_sel (1);
206 rv = edit_query_dialog2 (_("Warning"),
207 _("The file has been modified in the meantime. Save anyway?"),
208 _("&Yes"), _("&Cancel"));
209 if (rv != 0)
211 vfs_path_free (real_filename_vpath);
212 return -1;
217 if (this_save_mode != EDIT_QUICK_SAVE)
219 char *savedir, *saveprefix;
221 savedir = vfs_path_tokens_get (real_filename_vpath, 0, -1);
222 if (savedir == NULL)
223 savedir = g_strdup (".");
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 long 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 vfs_path_t *tmp_vpath;
369 gboolean ok;
371 #ifdef HAVE_ASSERT_H
372 assert (option_backup_ext != NULL);
373 #endif
374 tmp_vpath = vfs_path_append_new (real_filename_vpath, option_backup_ext, (char *) NULL);
375 ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
376 vfs_path_free (tmp_vpath);
377 if (!ok)
378 goto error_save;
381 if (this_save_mode != EDIT_QUICK_SAVE)
382 if (mc_rename (savename_vpath, real_filename_vpath) == -1)
383 goto error_save;
385 vfs_path_free (real_filename_vpath);
386 vfs_path_free (savename_vpath);
387 return 1;
388 error_save:
389 /* FIXME: Is this safe ?
390 * if (this_save_mode != EDIT_QUICK_SAVE)
391 * mc_unlink (savename);
393 vfs_path_free (real_filename_vpath);
394 vfs_path_free (savename_vpath);
395 return 0;
398 /* --------------------------------------------------------------------------------------------- */
400 static gboolean
401 edit_check_newline (WEdit * edit)
403 return !(option_check_nl_at_eof && edit->last_byte > 0
404 && edit_get_byte (edit, edit->last_byte - 1) != '\n'
405 && edit_query_dialog2 (_("Warning"),
406 _("The file you are saving is not finished with a newline"),
407 _("C&ontinue"), _("&Cancel")));
410 /* --------------------------------------------------------------------------------------------- */
412 static vfs_path_t *
413 edit_get_save_file_as (WEdit * edit)
415 #define DLG_WIDTH 64
416 #define DLG_HEIGHT 14
418 static LineBreaks cur_lb = LB_ASIS;
420 char *filename = vfs_path_to_str (edit->filename_vpath);
421 char *filename_res;
423 const char *lb_names[LB_NAMES] = {
424 N_("&Do not change"),
425 N_("&Unix format (LF)"),
426 N_("&Windows/DOS format (CR LF)"),
427 N_("&Macintosh format (CR)")
430 QuickWidget quick_widgets[] = {
431 QUICK_BUTTON (6, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
432 QUICK_BUTTON (2, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
433 QUICK_RADIO (5, DLG_WIDTH, DLG_HEIGHT - 8, DLG_HEIGHT, LB_NAMES, lb_names, (int *) &cur_lb),
434 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 9, DLG_HEIGHT, N_("Change line breaks to:")),
435 QUICK_INPUT (3, DLG_WIDTH, DLG_HEIGHT - 11, DLG_HEIGHT, filename, DLG_WIDTH - 6, 0,
436 "save-as", &filename_res),
437 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 12, DLG_HEIGHT, N_("Enter file name:")),
438 QUICK_END
441 QuickDialog Quick_options = {
442 DLG_WIDTH, DLG_HEIGHT, -1, -1,
443 N_("Save As"), "[Save File As]",
444 quick_widgets, NULL, NULL, FALSE
447 if (quick_dialog (&Quick_options) != B_CANCEL)
449 char *fname;
450 vfs_path_t *ret_vpath;
452 edit->lb = cur_lb;
453 fname = tilde_expand (filename_res);
454 g_free (filename_res);
455 ret_vpath = vfs_path_from_str (fname);
456 g_free (fname);
457 return ret_vpath;
459 g_free (filename);
461 return NULL;
463 #undef DLG_WIDTH
464 #undef DLG_HEIGHT
467 /* --------------------------------------------------------------------------------------------- */
469 /** returns TRUE on success */
471 static gboolean
472 edit_save_cmd (WEdit * edit)
474 int res, save_lock = 0;
476 if (!edit->locked && !edit->delete_file)
477 save_lock = lock_file (edit->filename_vpath);
478 res = edit_save_file (edit, edit->filename_vpath);
480 /* Maintain modify (not save) lock on failure */
481 if ((res > 0 && edit->locked) || save_lock)
482 edit->locked = unlock_file (edit->filename_vpath);
484 /* On failure try 'save as', it does locking on its own */
485 if (res == 0)
486 return edit_save_as_cmd (edit);
487 edit->force |= REDRAW_COMPLETELY;
488 if (res > 0)
490 edit->delete_file = 0;
491 edit->modified = 0;
494 return TRUE;
497 /* --------------------------------------------------------------------------------------------- */
499 * Load file content
501 * @param h screen the owner of editor window
502 * @param vpath vfs file path
503 * @return TRUE if file content was successfully loaded, FALSE otherwise
506 static inline gboolean
507 edit_load_file_from_filename (Dlg_head * h, const vfs_path_t * vpath)
509 return edit_add_window (h, h->y + 1, h->x, h->lines - 2, h->cols, vpath, 0);
512 /* --------------------------------------------------------------------------------------------- */
514 static void
515 edit_delete_column_of_text (WEdit * edit)
517 long p, q, r, m1, m2;
518 long b, c, d, n;
520 eval_marks (edit, &m1, &m2);
521 n = edit_move_forward (edit, m1, 0, m2) + 1;
522 c = edit_move_forward3 (edit, edit_bol (edit, m1), 0, m1);
523 d = edit_move_forward3 (edit, edit_bol (edit, m2), 0, m2);
524 b = max (min (c, d), min (edit->column1, edit->column2));
525 c = max (c, max (edit->column1, edit->column2));
527 while (n--)
529 r = edit_bol (edit, edit->curs1);
530 p = edit_move_forward3 (edit, r, b, 0);
531 q = edit_move_forward3 (edit, r, c, 0);
532 if (p < m1)
533 p = m1;
534 if (q > m2)
535 q = m2;
536 edit_cursor_move (edit, p - edit->curs1);
537 while (q > p)
539 /* delete line between margins */
540 if (edit_get_byte (edit, edit->curs1) != '\n')
541 edit_delete (edit, 1);
542 q--;
544 if (n)
545 /* move to next line except on the last delete */
546 edit_cursor_move (edit, edit_move_forward (edit, edit->curs1, 1, 0) - edit->curs1);
550 /* --------------------------------------------------------------------------------------------- */
551 /** if success return 0 */
553 static int
554 edit_block_delete (WEdit * edit)
556 long count;
557 long start_mark, end_mark;
558 int curs_pos;
559 long curs_line, c1, c2;
561 if (eval_marks (edit, &start_mark, &end_mark))
562 return 0;
563 if (edit->column_highlight && edit->mark2 < 0)
564 edit_mark_cmd (edit, FALSE);
565 if ((end_mark - start_mark) > option_max_undo / 2)
567 /* Warning message with a query to continue or cancel the operation */
568 if (edit_query_dialog2
569 (_("Warning"),
571 ("Block is large, you may not be able to undo this action"),
572 _("C&ontinue"), _("&Cancel")))
574 return 1;
577 c1 = min (edit->column1, edit->column2);
578 c2 = max (edit->column1, edit->column2);
579 edit->column1 = c1;
580 edit->column2 = c2;
582 edit_push_markers (edit);
584 curs_line = edit->curs_line;
586 curs_pos = edit->curs_col + edit->over_col;
588 /* move cursor to start of selection */
589 edit_cursor_move (edit, start_mark - edit->curs1);
590 edit_scroll_screen_over_cursor (edit);
591 count = start_mark;
592 if (start_mark < end_mark)
594 if (edit->column_highlight)
596 int line_width;
598 if (edit->mark2 < 0)
599 edit_mark_cmd (edit, FALSE);
600 edit_delete_column_of_text (edit);
601 /* move cursor to the saved position */
602 edit_move_to_line (edit, curs_line);
603 /* calculate line width and cursor position before cut */
604 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
605 edit_eol (edit, edit->curs1));
606 if (option_cursor_beyond_eol && curs_pos > line_width)
607 edit->over_col = curs_pos - line_width;
609 else
611 while (count < end_mark)
613 edit_delete (edit, 1);
614 count++;
618 edit_set_markers (edit, 0, 0, 0, 0);
619 edit->force |= REDRAW_PAGE;
620 return 0;
623 /* --------------------------------------------------------------------------------------------- */
625 * Get EOL symbol for searching.
627 * @param edit editor object
628 * @return EOL symbol
631 static inline char
632 edit_search_get_current_end_line_char (const WEdit * edit)
634 switch (edit->lb)
636 case LB_MAC:
637 return '\r';
638 default:
639 return '\n';
643 /* --------------------------------------------------------------------------------------------- */
645 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
647 * @param search search object
648 * @return result of checks.
651 static edit_search_line_t
652 edit_get_search_line_type (mc_search_t * search)
654 edit_search_line_t search_line_type = 0;
656 if (search->search_type != MC_SEARCH_T_REGEX)
657 return search_line_type;
659 if (*search->original == '^')
660 search_line_type |= AT_START_LINE;
662 if (search->original[search->original_len - 1] == '$')
663 search_line_type |= AT_END_LINE;
664 return search_line_type;
667 /* --------------------------------------------------------------------------------------------- */
669 * Calculating the start position of next line.
671 * @param edit editor object
672 * @param current_pos current position
673 * @param max_pos max position
674 * @param end_string_symbol end of line symbol
675 * @return start position of next line
678 static off_t
679 edit_calculate_start_of_next_line (WEdit * edit, off_t current_pos, off_t max_pos,
680 char end_string_symbol)
682 off_t i;
684 for (i = current_pos; i < max_pos; i++)
686 current_pos++;
687 if (edit_get_byte (edit, i) == end_string_symbol)
688 break;
691 return current_pos;
694 /* --------------------------------------------------------------------------------------------- */
696 * Calculating the end position of previous line.
698 * @param edit editor object
699 * @param current_pos current position
700 * @param end_string_symbol end of line symbol
701 * @return end position of previous line
704 static off_t
705 edit_calculate_end_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
707 off_t i;
709 for (i = current_pos - 1; i >= 0; i--)
710 if (edit_get_byte (edit, i) == end_string_symbol)
711 break;
713 return i;
716 /* --------------------------------------------------------------------------------------------- */
718 * Calculating the start position of previous line.
720 * @param edit editor object
721 * @param current_pos current position
722 * @param end_string_symbol end of line symbol
723 * @return start position of previous line
726 static inline off_t
727 edit_calculate_start_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
729 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
730 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
732 return (current_pos + 1);
735 /* --------------------------------------------------------------------------------------------- */
737 * Calculating the start position of current line.
739 * @param edit editor object
740 * @param current_pos current position
741 * @param end_string_symbol end of line symbol
742 * @return start position of current line
745 static inline off_t
746 edit_calculate_start_of_current_line (WEdit * edit, off_t current_pos, char end_string_symbol)
748 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
750 return (current_pos + 1);
753 /* --------------------------------------------------------------------------------------------- */
755 * Fixing (if needed) search start position if 'only in selection' option present.
757 * @param edit editor object
760 static void
761 edit_search_fix_search_start_if_selection (WEdit * edit)
763 long start_mark = 0;
764 long end_mark = 0;
766 if (!edit_search_options.only_in_selection)
767 return;
769 if (eval_marks (edit, &start_mark, &end_mark) != 0)
770 return;
772 if (edit_search_options.backwards)
774 if (edit->search_start > end_mark || edit->search_start <= start_mark)
775 edit->search_start = end_mark;
777 else
779 if (edit->search_start < start_mark || edit->search_start >= end_mark)
780 edit->search_start = start_mark;
784 /* --------------------------------------------------------------------------------------------- */
786 static gboolean
787 editcmd_find (WEdit * edit, gsize * len)
789 off_t search_start = edit->search_start;
790 off_t search_end;
791 long start_mark = 0;
792 long end_mark = edit->last_byte;
793 int mark_res = 0;
794 char end_string_symbol;
796 end_string_symbol = edit_search_get_current_end_line_char (edit);
798 /* prepare for search */
799 if (edit_search_options.only_in_selection)
801 mark_res = eval_marks (edit, &start_mark, &end_mark);
802 if (mark_res != 0)
804 edit->search->error = MC_SEARCH_E_NOTFOUND;
805 edit->search->error_str = g_strdup (_("Search string not found"));
806 return FALSE;
809 /* fix the start and the end of search block positions */
810 if ((edit->search_line_type & AT_START_LINE) != 0
811 && (start_mark != 0 || edit_get_byte (edit, start_mark - 1) != end_string_symbol))
813 start_mark =
814 edit_calculate_start_of_next_line (edit, start_mark, edit->last_byte,
815 end_string_symbol);
817 if ((edit->search_line_type & AT_END_LINE) != 0
818 && (end_mark - 1 != edit->last_byte
819 || edit_get_byte (edit, end_mark) != end_string_symbol))
821 end_mark = edit_calculate_end_of_previous_line (edit, end_mark, end_string_symbol);
823 if (start_mark >= end_mark)
825 edit->search->error = MC_SEARCH_E_NOTFOUND;
826 edit->search->error_str = g_strdup (_("Search string not found"));
827 return FALSE;
830 else
832 if (edit_search_options.backwards)
833 end_mark = max (1, edit->curs1) - 1;
836 /* search */
837 if (edit_search_options.backwards)
839 /* backward search */
840 search_end = end_mark;
842 if ((edit->search_line_type & AT_START_LINE) != 0)
843 search_start =
844 edit_calculate_start_of_current_line (edit, search_start, end_string_symbol);
846 while ((int) search_start >= start_mark)
848 if (search_end > (off_t) (search_start + edit->search->original_len)
849 && mc_search_is_fixed_search_str (edit->search))
851 search_end = search_start + edit->search->original_len;
853 if (mc_search_run (edit->search, (void *) edit, search_start, search_end, len)
854 && edit->search->normal_offset == search_start)
856 return TRUE;
860 if ((edit->search_line_type & AT_START_LINE) != 0)
861 search_start =
862 edit_calculate_start_of_previous_line (edit, search_start, end_string_symbol);
863 else
864 search_start--;
866 edit->search->error_str = g_strdup (_("Search string not found"));
868 else
870 /* forward search */
871 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
872 search_start =
873 edit_calculate_start_of_next_line (edit, search_start, end_mark, end_string_symbol);
874 return mc_search_run (edit->search, (void *) edit, search_start, end_mark, len);
876 return FALSE;
879 /* --------------------------------------------------------------------------------------------- */
881 static char *
882 edit_replace_cmd__conv_to_display (char *str)
884 #ifdef HAVE_CHARSET
885 GString *tmp;
887 tmp = str_convert_to_display (str);
888 if (tmp != NULL)
890 if (tmp->len != 0)
891 return g_string_free (tmp, FALSE);
892 g_string_free (tmp, TRUE);
894 #endif
895 return g_strdup (str);
898 /* --------------------------------------------------------------------------------------------- */
900 static char *
901 edit_replace_cmd__conv_to_input (char *str)
903 #ifdef HAVE_CHARSET
904 GString *tmp;
906 tmp = str_convert_to_input (str);
907 if (tmp != NULL)
909 if (tmp->len != 0)
910 return g_string_free (tmp, FALSE);
911 g_string_free (tmp, TRUE);
913 #endif
914 return g_strdup (str);
917 /* --------------------------------------------------------------------------------------------- */
919 static void
920 edit_do_search (WEdit * edit)
922 gsize len = 0;
924 if (edit->search == NULL)
925 edit->search_start = edit->curs1;
927 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
929 if (search_create_bookmark)
931 int found = 0, books = 0;
932 long l = 0, l_last = -1;
933 long q = 0;
935 search_create_bookmark = FALSE;
936 book_mark_flush (edit, -1);
938 while (TRUE)
940 if (!mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len))
941 break;
942 if (found == 0)
943 edit->search_start = edit->search->normal_offset;
944 found++;
945 l += edit_count_lines (edit, q, edit->search->normal_offset);
946 if (l != l_last)
948 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
949 books++;
951 l_last = l;
952 q = edit->search->normal_offset + 1;
955 if (found == 0)
956 edit_error_dialog (_("Search"), _("Search string not found"));
957 else
958 edit_cursor_move (edit, edit->search_start - edit->curs1);
960 else
962 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
963 && edit_search_options.backwards)
964 edit->search_start--;
966 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
967 && !edit_search_options.backwards)
968 edit->search_start++;
970 if (editcmd_find (edit, &len))
972 edit->found_start = edit->search_start = edit->search->normal_offset;
973 edit->found_len = len;
974 edit->over_col = 0;
975 edit_cursor_move (edit, edit->search_start - edit->curs1);
976 edit_scroll_screen_over_cursor (edit);
977 if (edit_search_options.backwards)
978 edit->search_start--;
979 else
980 edit->search_start++;
982 else
984 edit->search_start = edit->curs1;
985 if (edit->search->error_str != NULL)
986 edit_error_dialog (_("Search"), edit->search->error_str);
990 edit->force |= REDRAW_COMPLETELY;
991 edit_scroll_screen_over_cursor (edit);
994 /* --------------------------------------------------------------------------------------------- */
996 static void
997 edit_search (WEdit * edit)
999 if (editcmd_dialog_search_show (edit))
1001 edit->search_line_type = edit_get_search_line_type (edit->search);
1002 edit_search_fix_search_start_if_selection (edit);
1003 edit_do_search (edit);
1007 /* --------------------------------------------------------------------------------------------- */
1008 /** Return a null terminated length of text. Result must be g_free'd */
1010 static unsigned char *
1011 edit_get_block (WEdit * edit, long start, long finish, int *l)
1013 unsigned char *s, *r;
1014 r = s = g_malloc0 (finish - start + 1);
1015 if (edit->column_highlight)
1017 *l = 0;
1018 /* copy from buffer, excluding chars that are out of the column 'margins' */
1019 while (start < finish)
1021 int c;
1022 long x;
1023 x = edit_move_forward3 (edit, edit_bol (edit, start), 0, start);
1024 c = edit_get_byte (edit, start);
1025 if ((x >= edit->column1 && x < edit->column2)
1026 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1028 *s++ = c;
1029 (*l)++;
1031 start++;
1034 else
1036 *l = finish - start;
1037 while (start < finish)
1038 *s++ = edit_get_byte (edit, start++);
1040 *s = 0;
1041 return r;
1044 /* --------------------------------------------------------------------------------------------- */
1045 /** copies a block to clipboard file */
1047 static gboolean
1048 edit_save_block_to_clip_file (WEdit * edit, long start, long finish)
1050 gboolean ret;
1051 gchar *tmp;
1053 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1054 ret = edit_save_block (edit, tmp, start, finish);
1055 g_free (tmp);
1056 return ret;
1059 /* --------------------------------------------------------------------------------------------- */
1061 static void
1062 pipe_mail (WEdit * edit, char *to, char *subject, char *cc)
1064 FILE *p = 0;
1065 char *s;
1067 to = name_quote (to, 0);
1068 subject = name_quote (subject, 0);
1069 cc = name_quote (cc, 0);
1070 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1071 g_free (to);
1072 g_free (subject);
1073 g_free (cc);
1075 if (s)
1077 p = popen (s, "w");
1078 g_free (s);
1081 if (p)
1083 long i;
1084 for (i = 0; i < edit->last_byte; i++)
1085 fputc (edit_get_byte (edit, i), p);
1086 pclose (p);
1090 /* --------------------------------------------------------------------------------------------- */
1091 /** find first character of current word */
1093 static gboolean
1094 edit_find_word_start (WEdit * edit, long *word_start, gsize * word_len)
1096 int c, last;
1097 gsize i;
1099 /* return if at begin of file */
1100 if (edit->curs1 <= 0)
1101 return FALSE;
1103 c = (unsigned char) edit_get_byte (edit, edit->curs1 - 1);
1104 /* return if not at end or in word */
1105 if (is_break_char (c))
1106 return FALSE;
1108 /* search start of word to be completed */
1109 for (i = 2;; i++)
1111 /* return if at begin of file */
1112 if ((gsize) edit->curs1 < i)
1113 return FALSE;
1115 last = c;
1116 c = (unsigned char) edit_get_byte (edit, edit->curs1 - i);
1118 if (is_break_char (c))
1120 /* return if word starts with digit */
1121 if (isdigit (last))
1122 return FALSE;
1124 *word_start = edit->curs1 - (i - 1); /* start found */
1125 *word_len = i - 1;
1126 break;
1129 /* success */
1130 return TRUE;
1133 /* --------------------------------------------------------------------------------------------- */
1135 * Get current word under cursor
1137 * @param edit editor object
1138 * @param srch mc_search object
1139 * @param word_start start word position
1141 * @return newly allocated string or NULL if no any words under cursor
1144 static char *
1145 edit_collect_completions_get_current_word (WEdit * edit, mc_search_t * srch, long word_start)
1147 gsize len = 0, i;
1148 GString *temp;
1150 if (!mc_search_run (srch, (void *) edit, word_start, edit->last_byte, &len))
1151 return NULL;
1153 temp = g_string_sized_new (len);
1155 for (i = 0; i < len; i++)
1157 int chr;
1159 chr = edit_get_byte (edit, word_start + i);
1160 if (!isspace (chr))
1161 g_string_append_c (temp, chr);
1164 return g_string_free (temp, temp->len == 0);
1167 /* --------------------------------------------------------------------------------------------- */
1168 /** collect the possible completions */
1170 static gsize
1171 edit_collect_completions (WEdit * edit, long word_start, gsize word_len,
1172 char *match_expr, struct selection *compl, gsize * num)
1174 gsize len = 0;
1175 gsize max_len = 0;
1176 gsize i;
1177 int skip;
1178 GString *temp;
1179 mc_search_t *srch;
1180 long last_byte, start = -1;
1181 char *current_word;
1183 srch = mc_search_new (match_expr, -1);
1184 if (srch == NULL)
1185 return 0;
1187 if (mc_config_get_bool
1188 (mc_main_config, CONFIG_APP_SECTION, "editor_wordcompletion_collect_entire_file", 0))
1190 last_byte = edit->last_byte;
1192 else
1194 last_byte = word_start;
1197 srch->search_type = MC_SEARCH_T_REGEX;
1198 srch->is_case_sensitive = TRUE;
1199 srch->search_fn = edit_search_cmd_callback;
1201 current_word = edit_collect_completions_get_current_word (edit, srch, word_start);
1203 temp = g_string_new ("");
1205 /* collect max MAX_WORD_COMPLETIONS completions */
1206 while (mc_search_run (srch, (void *) edit, start + 1, last_byte, &len))
1208 g_string_set_size (temp, 0);
1209 start = srch->normal_offset;
1211 /* add matched completion if not yet added */
1212 for (i = 0; i < len; i++)
1214 skip = edit_get_byte (edit, start + i);
1215 if (isspace (skip))
1216 continue;
1218 /* skip current word */
1219 if (start + (long) i == word_start)
1220 break;
1222 g_string_append_c (temp, skip);
1225 if (temp->len == 0)
1226 continue;
1228 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1229 continue;
1231 skip = 0;
1233 for (i = 0; i < *num; i++)
1235 if (strncmp
1236 ((char *) &compl[i].text[word_len],
1237 (char *) &temp->str[word_len], max (len, compl[i].len) - word_len) == 0)
1239 struct selection this = compl[i];
1240 for (++i; i < *num; i++)
1242 compl[i - 1] = compl[i];
1244 compl[*num - 1] = this;
1245 skip = 1;
1246 break; /* skip it, already added */
1249 if (skip != 0)
1250 continue;
1252 if (*num == MAX_WORD_COMPLETIONS)
1254 g_free (compl[0].text);
1255 for (i = 1; i < *num; i++)
1257 compl[i - 1] = compl[i];
1259 (*num)--;
1261 #ifdef HAVE_CHARSET
1263 GString *recoded;
1264 recoded = str_convert_to_display (temp->str);
1266 if (recoded && recoded->len)
1267 g_string_assign (temp, recoded->str);
1269 g_string_free (recoded, TRUE);
1271 #endif
1272 compl[*num].text = g_strdup (temp->str);
1273 compl[*num].len = temp->len;
1274 (*num)++;
1275 start += len;
1277 /* note the maximal length needed for the completion dialog */
1278 if (len > max_len)
1279 max_len = len;
1282 mc_search_free (srch);
1283 g_string_free (temp, TRUE);
1284 g_free (current_word);
1286 return max_len;
1289 /* --------------------------------------------------------------------------------------------- */
1291 static void
1292 edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width,
1293 long *start_pos, long *end_pos, int *col1, int *col2)
1295 long cursor;
1296 int i, col;
1298 cursor = edit->curs1;
1299 col = edit_get_col (edit);
1301 for (i = 0; i < size; i++)
1303 if (data[i] != '\n')
1304 edit_insert (edit, data[i]);
1305 else
1306 { /* fill in and move to next line */
1307 int l;
1308 long p;
1310 if (edit_get_byte (edit, edit->curs1) != '\n')
1312 l = width - (edit_get_col (edit) - col);
1313 while (l > 0)
1315 edit_insert (edit, ' ');
1316 l -= space_width;
1319 for (p = edit->curs1;; p++)
1321 if (p == edit->last_byte)
1323 edit_cursor_move (edit, edit->last_byte - edit->curs1);
1324 edit_insert_ahead (edit, '\n');
1325 p++;
1326 break;
1328 if (edit_get_byte (edit, p) == '\n')
1330 p++;
1331 break;
1334 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
1335 l = col - edit_get_col (edit);
1336 while (l >= space_width)
1338 edit_insert (edit, ' ');
1339 l -= space_width;
1344 *col1 = col;
1345 *col2 = col + width;
1346 *start_pos = cursor;
1347 *end_pos = edit->curs1;
1348 edit_cursor_move (edit, cursor - edit->curs1);
1351 /* --------------------------------------------------------------------------------------------- */
1353 static int
1354 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1356 const macros_t *m1 = (const macros_t *) macro1;
1357 const macros_t *m2 = (const macros_t *) macro2;
1359 return m1->hotkey - m2->hotkey;
1362 /* --------------------------------------------------------------------------------------------- */
1364 static void
1365 edit_macro_sort_by_hotkey (void)
1367 if (macros_list != NULL && macros_list->len != 0)
1368 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1371 /* --------------------------------------------------------------------------------------------- */
1373 static gboolean
1374 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1376 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1377 macros_t *result;
1378 macros_t search_macro;
1380 (void) edit;
1382 search_macro.hotkey = hotkey;
1383 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1384 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1386 if (result != NULL && result->macro != NULL)
1388 *indx = (result - array_start);
1389 *macros = result;
1390 return TRUE;
1392 *indx = 0;
1393 return FALSE;
1396 /* --------------------------------------------------------------------------------------------- */
1397 /** returns FALSE on error */
1399 static gboolean
1400 edit_delete_macro (WEdit * edit, int hotkey)
1402 mc_config_t *macros_config = NULL;
1403 const char *section_name = "editor";
1404 gchar *macros_fname;
1405 guint indx;
1406 char *skeyname;
1407 const macros_t *macros = NULL;
1409 /* clear array of actions for current hotkey */
1410 while (edit_get_macro (edit, hotkey, &macros, &indx))
1412 if (macros->macro != NULL)
1413 g_array_free (macros->macro, TRUE);
1414 macros = NULL;
1415 g_array_remove_index (macros_list, indx);
1416 edit_macro_sort_by_hotkey ();
1419 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1420 macros_config = mc_config_init (macros_fname, FALSE);
1421 g_free (macros_fname);
1423 if (macros_config == NULL)
1424 return FALSE;
1426 skeyname = lookup_key_by_code (hotkey);
1427 while (mc_config_del_key (macros_config, section_name, skeyname))
1429 g_free (skeyname);
1430 mc_config_save_file (macros_config, NULL);
1431 mc_config_deinit (macros_config);
1432 return TRUE;
1435 /* --------------------------------------------------------------------------------------------- */
1437 * Callback for the iteration of objects in the 'editors' array.
1438 * Toggle syntax highlighting in editor object.
1440 * @param data probably WEdit object
1441 * @param user_data unused
1444 static void
1445 edit_syntax_onoff_cb (void *data, void *user_data)
1447 (void) user_data;
1449 if (edit_widget_is_editor ((const Widget *) data))
1451 WEdit *edit = (WEdit *) data;
1453 if (option_syntax_highlighting)
1454 edit_load_syntax (edit, NULL, edit->syntax_type);
1455 edit->force |= REDRAW_PAGE;
1459 /* --------------------------------------------------------------------------------------------- */
1461 * Callback for the iteration of objects in the 'editors' array.
1462 * Redraw editor object.
1464 * @param data probably WEdit object
1465 * @param user_data unused
1468 static void
1469 edit_redraw_page_cb (void *data, void *user_data)
1471 (void) user_data;
1473 if (edit_widget_is_editor ((const Widget *) data))
1474 ((WEdit *) data)->force |= REDRAW_PAGE;
1477 /* --------------------------------------------------------------------------------------------- */
1478 /*** public functions ****************************************************************************/
1479 /* --------------------------------------------------------------------------------------------- */
1481 void
1482 edit_refresh_cmd (void)
1484 clr_scr ();
1485 repaint_screen ();
1486 tty_keypad (TRUE);
1489 /* --------------------------------------------------------------------------------------------- */
1491 * Toggle syntax highlighting in all editor windows.
1493 * @param h root widget for all windows
1496 void
1497 edit_syntax_onoff_cmd (Dlg_head * h)
1499 option_syntax_highlighting = !option_syntax_highlighting;
1500 g_list_foreach (h->widgets, edit_syntax_onoff_cb, NULL);
1501 dlg_redraw (h);
1504 /* --------------------------------------------------------------------------------------------- */
1506 * Toggle tabs showing in all editor windows.
1508 * @param h root widget for all windows
1511 void
1512 edit_show_tabs_tws_cmd (Dlg_head * h)
1514 enable_show_tabs_tws = !enable_show_tabs_tws;
1515 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1516 dlg_redraw (h);
1519 /* --------------------------------------------------------------------------------------------- */
1521 * Toggle right margin showing in all editor windows.
1523 * @param h root widget for all windows
1526 void
1527 edit_show_margin_cmd (Dlg_head * h)
1529 show_right_margin = !show_right_margin;
1530 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1531 dlg_redraw (h);
1534 /* --------------------------------------------------------------------------------------------- */
1536 * Toggle line numbers showing in all editor windows.
1538 * @param h root widget for all windows
1541 void
1542 edit_show_numbers_cmd (Dlg_head * h)
1544 option_line_state = !option_line_state;
1545 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
1546 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1547 dlg_redraw (h);
1550 /* --------------------------------------------------------------------------------------------- */
1552 void
1553 edit_save_mode_cmd (void)
1555 /* diaog sizes */
1556 const int DLG_X = 38;
1557 const int DLG_Y = 13;
1559 char *str_result;
1561 const char *str[] = {
1562 N_("&Quick save"),
1563 N_("&Safe save"),
1564 N_("&Do backups with following extension:")
1567 QuickWidget widgets[] = {
1568 /* 0 */
1569 QUICK_BUTTON (18, DLG_X, DLG_Y - 3, DLG_Y, N_("&Cancel"), B_CANCEL, NULL),
1570 /* 1 */
1571 QUICK_BUTTON (6, DLG_X, DLG_Y - 3, DLG_Y, N_("&OK"), B_ENTER, NULL),
1572 /* 2 */
1573 QUICK_CHECKBOX (4, DLG_X, 8, DLG_Y, N_("Check &POSIX new line"), &option_check_nl_at_eof),
1574 /* 3 */
1575 QUICK_INPUT (8, DLG_X, 6, DLG_Y, option_backup_ext, 9, 0, "edit-backup-ext", &str_result),
1576 /* 4 */
1577 QUICK_RADIO (4, DLG_X, 3, DLG_Y, 3, str, &option_save_mode),
1578 QUICK_END
1581 QuickDialog dialog = {
1582 DLG_X, DLG_Y, -1, -1, N_("Edit Save Mode"),
1583 "[Edit Save Mode]", widgets, NULL, NULL, FALSE
1586 size_t i;
1587 size_t maxlen = 0;
1588 size_t w0, w1, b_len, w3;
1590 #ifdef HAVE_ASSERT_H
1591 assert (option_backup_ext != NULL);
1592 #endif
1594 /* OK/Cancel buttons */
1595 w0 = str_term_width1 (_(widgets[0].u.button.text)) + 3;
1596 w1 = str_term_width1 (_(widgets[1].u.button.text)) + 5; /* default button */
1597 b_len = w0 + w1 + 3;
1599 maxlen = max (b_len, (size_t) str_term_width1 (_(dialog.title)) + 2);
1601 w3 = 0;
1602 for (i = 0; i < 3; i++)
1604 #ifdef ENABLE_NLS
1605 str[i] = _(str[i]);
1606 #endif
1607 w3 = max (w3, (size_t) str_term_width1 (str[i]));
1610 maxlen = max (maxlen, w3 + 4);
1612 dialog.xlen = min ((size_t) COLS, maxlen + 8);
1614 widgets[3].u.input.len = w3;
1615 widgets[1].relative_x = (dialog.xlen - b_len) / 2;
1616 widgets[0].relative_x = widgets[1].relative_x + w0 + 2;
1618 for (i = 0; i < sizeof (widgets) / sizeof (widgets[0]); i++)
1619 widgets[i].x_divisions = dialog.xlen;
1621 if (quick_dialog (&dialog) != B_CANCEL)
1623 g_free (option_backup_ext);
1624 option_backup_ext = str_result;
1628 /* --------------------------------------------------------------------------------------------- */
1630 void
1631 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1633 vfs_path_free (edit->filename_vpath);
1634 edit->filename_vpath = vfs_path_clone (name_vpath);
1636 if (edit->dir_vpath == NULL)
1637 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1640 /* --------------------------------------------------------------------------------------------- */
1641 /* Here we want to warn the users of overwriting an existing file,
1642 but only if they have made a change to the filename */
1643 /* returns TRUE on success */
1644 gboolean
1645 edit_save_as_cmd (WEdit * edit)
1647 /* This heads the 'Save As' dialog box */
1648 vfs_path_t *exp_vpath;
1649 int save_lock = 0;
1650 int different_filename = 0;
1652 if (!edit_check_newline (edit))
1653 return FALSE;
1655 exp_vpath = edit_get_save_file_as (edit);
1656 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1658 if (exp_vpath != NULL)
1660 if (vfs_path_len (exp_vpath) == 0)
1661 goto ret;
1662 else
1664 int rv;
1666 if (vfs_path_cmp (edit->filename_vpath, exp_vpath) != 0)
1668 int file;
1669 struct stat sb;
1671 if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1673 edit_error_dialog (_("Save as"),
1674 get_sys_error (_
1675 ("Cannot save: destination is not a regular file")));
1676 goto ret;
1679 different_filename = 1;
1680 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1682 if (file != -1)
1684 /* the file exists */
1685 mc_close (file);
1686 /* Overwrite the current file or cancel the operation */
1687 if (edit_query_dialog2
1688 (_("Warning"),
1689 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1690 goto ret;
1692 else
1694 edit->stat1.st_mode |= S_IWUSR;
1696 save_lock = lock_file (exp_vpath);
1698 else
1700 /* filenames equal, check if already locked */
1701 if (!edit->locked && !edit->delete_file)
1702 save_lock = lock_file (exp_vpath);
1705 if (different_filename)
1708 * Allow user to write into saved (under another name) file
1709 * even if original file had r/o user permissions.
1711 edit->stat1.st_mode |= S_IWRITE;
1714 rv = edit_save_file (edit, exp_vpath);
1715 switch (rv)
1717 case 1:
1718 /* Succesful, so unlock both files */
1719 if (different_filename)
1721 if (save_lock)
1722 unlock_file (exp_vpath);
1723 if (edit->locked)
1724 edit->locked = unlock_file (edit->filename_vpath);
1726 else
1728 if (edit->locked || save_lock)
1729 edit->locked = unlock_file (edit->filename_vpath);
1732 edit_set_filename (edit, exp_vpath);
1733 if (edit->lb != LB_ASIS)
1734 edit_reload (edit, exp_vpath);
1735 edit->modified = 0;
1736 edit->delete_file = 0;
1737 if (different_filename)
1738 edit_load_syntax (edit, NULL, edit->syntax_type);
1739 vfs_path_free (exp_vpath);
1740 edit->force |= REDRAW_COMPLETELY;
1741 return TRUE;
1742 default:
1743 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1744 /* fallthrough */
1745 case -1:
1746 /* Failed, so maintain modify (not save) lock */
1747 if (save_lock)
1748 unlock_file (exp_vpath);
1749 break;
1754 ret:
1755 vfs_path_free (exp_vpath);
1756 edit->force |= REDRAW_COMPLETELY;
1757 return FALSE;
1760 /* {{{ Macro stuff starts here */
1761 /* --------------------------------------------------------------------------------------------- */
1763 void
1764 edit_delete_macro_cmd (WEdit * edit)
1766 int hotkey;
1768 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE);
1770 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1771 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1774 /* --------------------------------------------------------------------------------------------- */
1776 /** returns FALSE on error */
1777 gboolean
1778 edit_execute_macro (WEdit * edit, int hotkey)
1780 gboolean res = FALSE;
1782 if (hotkey != 0)
1784 const macros_t *macros;
1785 guint indx;
1787 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1788 macros->macro != NULL && macros->macro->len != 0)
1790 guint i;
1792 edit->force |= REDRAW_PAGE;
1794 for (i = 0; i < macros->macro->len; i++)
1796 const macro_action_t *m_act;
1798 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1799 edit_execute_cmd (edit, m_act->action, m_act->ch);
1800 res = TRUE;
1805 return res;
1808 /* --------------------------------------------------------------------------------------------- */
1810 /** returns FALSE on error */
1811 gboolean
1812 edit_store_macro_cmd (WEdit * edit)
1814 int i;
1815 int hotkey;
1816 GString *marcros_string;
1817 mc_config_t *macros_config = NULL;
1818 const char *section_name = "editor";
1819 gchar *macros_fname;
1820 GArray *macros; /* current macro */
1821 int tmp_act;
1822 gboolean have_macro = FALSE;
1823 char *skeyname = NULL;
1825 hotkey =
1826 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE);
1827 if (hotkey == ESC_CHAR)
1828 return FALSE;
1830 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1832 /* return FALSE if try assign macro into restricted hotkeys */
1833 if (tmp_act == CK_MacroStartRecord
1834 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1835 return FALSE;
1837 edit_delete_macro (edit, hotkey);
1839 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1840 macros_config = mc_config_init (macros_fname, FALSE);
1841 g_free (macros_fname);
1843 if (macros_config == NULL)
1844 return FALSE;
1846 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1848 marcros_string = g_string_sized_new (250);
1849 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1851 skeyname = lookup_key_by_code (hotkey);
1853 for (i = 0; i < macro_index; i++)
1855 macro_action_t m_act;
1856 const char *action_name;
1858 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1860 if (action_name == NULL)
1861 break;
1863 m_act.action = record_macro_buf[i].action;
1864 m_act.ch = record_macro_buf[i].ch;
1865 g_array_append_val (macros, m_act);
1866 have_macro = TRUE;
1867 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1868 (int) record_macro_buf[i].ch);
1870 if (have_macro)
1872 macros_t macro;
1873 macro.hotkey = hotkey;
1874 macro.macro = macros;
1875 g_array_append_val (macros_list, macro);
1876 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1878 else
1879 mc_config_del_key (macros_config, section_name, skeyname);
1881 g_free (skeyname);
1882 edit_macro_sort_by_hotkey ();
1884 g_string_free (marcros_string, TRUE);
1885 mc_config_save_file (macros_config, NULL);
1886 mc_config_deinit (macros_config);
1887 return TRUE;
1890 /* --------------------------------------------------------------------------------------------- */
1892 gboolean
1893 edit_repeat_macro_cmd (WEdit * edit)
1895 int i, j;
1896 char *f;
1897 long count_repeat;
1898 char *error = NULL;
1900 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL);
1901 if (f == NULL || *f == '\0')
1903 g_free (f);
1904 return FALSE;
1907 count_repeat = strtol (f, &error, 0);
1909 if (*error != '\0')
1911 g_free (f);
1912 return FALSE;
1915 g_free (f);
1917 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1918 edit->force |= REDRAW_PAGE;
1920 for (j = 0; j < count_repeat; j++)
1921 for (i = 0; i < macro_index; i++)
1922 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1923 edit_update_screen (edit);
1924 return TRUE;
1927 /* --------------------------------------------------------------------------------------------- */
1928 /** return FALSE on error */
1930 gboolean
1931 edit_load_macro_cmd (WEdit * edit)
1933 mc_config_t *macros_config = NULL;
1934 gchar **profile_keys, **keys;
1935 gchar **values, **curr_values;
1936 gsize len, values_len;
1937 const char *section_name = "editor";
1938 gchar *macros_fname;
1939 int hotkey;
1941 (void) edit;
1943 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1944 macros_config = mc_config_init (macros_fname, TRUE);
1945 g_free (macros_fname);
1947 if (macros_config == NULL)
1948 return FALSE;
1950 profile_keys = keys = mc_config_get_keys (macros_config, section_name, &len);
1951 while (*profile_keys != NULL)
1953 gboolean have_macro;
1954 GArray *macros;
1955 macros_t macro;
1957 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1959 curr_values = values = mc_config_get_string_list (macros_config, section_name,
1960 *profile_keys, &values_len);
1961 hotkey = lookup_key (*profile_keys, NULL);
1962 have_macro = FALSE;
1964 while (*curr_values != NULL && *curr_values[0] != '\0')
1966 char **macro_pair = NULL;
1968 macro_pair = g_strsplit (*curr_values, ":", 2);
1970 if (macro_pair != NULL)
1972 macro_action_t m_act;
1973 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
1974 m_act.action = 0;
1975 else
1977 m_act.action = keybind_lookup_action (macro_pair[0]);
1978 g_free (macro_pair[0]);
1979 macro_pair[0] = NULL;
1981 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
1982 m_act.ch = -1;
1983 else
1985 m_act.ch = strtol (macro_pair[1], NULL, 0);
1986 g_free (macro_pair[1]);
1987 macro_pair[1] = NULL;
1989 if (m_act.action != 0)
1991 /* a shell command */
1992 if ((m_act.action / CK_PipeBlock (0)) == 1)
1994 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
1995 m_act.ch = -1;
1997 g_array_append_val (macros, m_act);
1998 have_macro = TRUE;
2000 g_strfreev (macro_pair);
2001 macro_pair = NULL;
2003 curr_values++;
2005 if (have_macro)
2007 macro.hotkey = hotkey;
2008 macro.macro = macros;
2009 g_array_append_val (macros_list, macro);
2011 profile_keys++;
2012 g_strfreev (values);
2014 g_strfreev (keys);
2015 mc_config_deinit (macros_config);
2016 edit_macro_sort_by_hotkey ();
2017 return TRUE;
2020 /* }}} Macro stuff end here */
2022 /* --------------------------------------------------------------------------------------------- */
2023 /** returns TRUE on success */
2025 gboolean
2026 edit_save_confirm_cmd (WEdit * edit)
2028 char *f = NULL;
2030 if (edit->filename_vpath == NULL)
2031 return edit_save_as_cmd (edit);
2033 if (!edit_check_newline (edit))
2034 return FALSE;
2036 if (edit_confirm_save)
2038 char *filename;
2039 gboolean ok;
2041 filename = vfs_path_to_str (edit->filename_vpath);
2042 f = g_strdup_printf (_("Confirm save file: \"%s\""), filename);
2043 g_free (filename);
2044 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2045 g_free (f);
2046 if (!ok)
2047 return FALSE;
2049 return edit_save_cmd (edit);
2052 /* --------------------------------------------------------------------------------------------- */
2054 * Ask file to edit and load it.
2056 * @returns TRUE on success or cancel of ask.
2059 gboolean
2060 edit_load_cmd (Dlg_head * h)
2062 char *exp;
2063 gboolean ret = TRUE; /* possible cancel */
2065 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2066 MC_HISTORY_EDIT_LOAD, INPUT_LAST_TEXT);
2068 if (exp != NULL && *exp != '\0')
2070 vfs_path_t *exp_vpath;
2072 exp_vpath = vfs_path_from_str (exp);
2073 ret = edit_load_file_from_filename (h, exp_vpath);
2074 vfs_path_free (exp_vpath);
2077 g_free (exp);
2079 return ret;
2082 /* --------------------------------------------------------------------------------------------- */
2084 * Load syntax file to edit.
2086 * @returns TRUE on success
2089 gboolean
2090 edit_load_syntax_file (Dlg_head * h)
2092 vfs_path_t *extdir_vpath;
2093 int dir = 0;
2094 gboolean ret = FALSE;
2096 if (geteuid () == 0)
2097 dir = query_dialog (_("Syntax file edit"),
2098 _("Which syntax file you want to edit?"), D_NORMAL, 2,
2099 _("&User"), _("&System wide"));
2101 extdir_vpath =
2102 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
2103 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
2105 vfs_path_free (extdir_vpath);
2106 extdir_vpath =
2107 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
2110 if (dir == 0)
2112 vfs_path_t *user_syntax_file_vpath;
2114 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
2115 check_for_default (extdir_vpath, user_syntax_file_vpath);
2116 ret = edit_load_file_from_filename (h, user_syntax_file_vpath);
2117 vfs_path_free (user_syntax_file_vpath);
2119 else if (dir == 1)
2120 ret = edit_load_file_from_filename (h, extdir_vpath);
2122 vfs_path_free (extdir_vpath);
2124 return ret;
2127 /* --------------------------------------------------------------------------------------------- */
2129 * Load menu file to edit.
2131 * @returns TRUE on success
2134 gboolean
2135 edit_load_menu_file (Dlg_head * h)
2137 vfs_path_t *buffer_vpath;
2138 vfs_path_t *menufile_vpath;
2139 int dir;
2140 gboolean ret;
2142 dir = query_dialog (_("Menu edit"),
2143 _("Which menu file do you want to edit?"), D_NORMAL,
2144 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2146 menufile_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2147 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
2149 vfs_path_free (menufile_vpath);
2150 menufile_vpath = vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2153 switch (dir)
2155 case 0:
2156 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
2157 check_for_default (menufile_vpath, buffer_vpath);
2158 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
2159 break;
2161 case 1:
2162 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
2163 check_for_default (menufile_vpath, buffer_vpath);
2164 break;
2166 case 2:
2167 buffer_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2168 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
2170 vfs_path_free (buffer_vpath);
2171 buffer_vpath =
2172 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2174 break;
2176 default:
2177 vfs_path_free (menufile_vpath);
2178 return FALSE;
2181 ret = edit_load_file_from_filename (h, buffer_vpath);
2183 vfs_path_free (buffer_vpath);
2184 vfs_path_free (menufile_vpath);
2186 return ret;
2189 /* --------------------------------------------------------------------------------------------- */
2191 * Close window with opened file.
2193 * @returns TRUE if file was closed.
2196 gboolean
2197 edit_close_cmd (WEdit * edit)
2199 gboolean ret;
2201 ret = (edit != NULL) && edit_ok_to_exit (edit);
2203 if (ret)
2205 Dlg_head *h = ((Widget *) edit)->owner;
2207 if (edit->locked != 0)
2208 unlock_file (edit->filename_vpath);
2210 del_widget (edit);
2212 if (edit_widget_is_editor ((Widget *) h->current->data))
2213 edit = (WEdit *) h->current->data;
2214 else
2216 edit = find_editor (h);
2217 if (edit != NULL)
2218 dlg_set_top_widget (edit);
2222 if (edit != NULL)
2223 edit->force |= REDRAW_COMPLETELY;
2225 return ret;
2228 /* --------------------------------------------------------------------------------------------- */
2230 if mark2 is -1 then marking is from mark1 to the cursor.
2231 Otherwise its between the markers. This handles this.
2232 Returns 1 if no text is marked.
2236 eval_marks (WEdit * edit, long *start_mark, long *end_mark)
2238 if (edit->mark1 != edit->mark2)
2240 long start_bol, start_eol;
2241 long end_bol, end_eol;
2242 long col1, col2;
2243 long diff1, diff2;
2244 long end_mark_curs;
2246 if (edit->end_mark_curs < 0)
2247 end_mark_curs = edit->curs1;
2248 else
2249 end_mark_curs = edit->end_mark_curs;
2251 if (edit->mark2 >= 0)
2253 *start_mark = min (edit->mark1, edit->mark2);
2254 *end_mark = max (edit->mark1, edit->mark2);
2256 else
2258 *start_mark = min (edit->mark1, end_mark_curs);
2259 *end_mark = max (edit->mark1, end_mark_curs);
2260 edit->column2 = edit->curs_col + edit->over_col;
2263 if (edit->column_highlight
2264 && (((edit->mark1 > end_mark_curs) && (edit->column1 < edit->column2))
2265 || ((edit->mark1 < end_mark_curs) && (edit->column1 > edit->column2))))
2267 start_bol = edit_bol (edit, *start_mark);
2268 start_eol = edit_eol (edit, start_bol - 1) + 1;
2269 end_bol = edit_bol (edit, *end_mark);
2270 end_eol = edit_eol (edit, *end_mark);
2271 col1 = min (edit->column1, edit->column2);
2272 col2 = max (edit->column1, edit->column2);
2274 diff1 =
2275 edit_move_forward3 (edit, start_bol, col2, 0) - edit_move_forward3 (edit, start_bol,
2276 col1, 0);
2277 diff2 =
2278 edit_move_forward3 (edit, end_bol, col2, 0) - edit_move_forward3 (edit, end_bol,
2279 col1, 0);
2281 *start_mark -= diff1;
2282 *end_mark += diff2;
2283 *start_mark = max (*start_mark, start_eol);
2284 *end_mark = min (*end_mark, end_eol);
2286 return 0;
2288 else
2290 *start_mark = *end_mark = 0;
2291 edit->column2 = edit->column1 = 0;
2292 return 1;
2296 /* --------------------------------------------------------------------------------------------- */
2298 void
2299 edit_insert_over (WEdit * edit)
2301 int i;
2303 for (i = 0; i < edit->over_col; i++)
2305 edit_insert (edit, ' ');
2307 edit->over_col = 0;
2310 /* --------------------------------------------------------------------------------------------- */
2313 edit_insert_column_of_text_from_file (WEdit * edit, int file,
2314 long *start_pos, long *end_pos, long *col1, long *col2)
2316 long cursor;
2317 int col;
2318 int blocklen = -1, width = 0;
2319 unsigned char *data;
2321 cursor = edit->curs1;
2322 col = edit_get_col (edit);
2323 data = g_malloc0 (TEMP_BUF_LEN);
2325 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
2327 int i;
2328 for (width = 0; width < blocklen; width++)
2330 if (data[width] == '\n')
2331 break;
2333 for (i = 0; i < blocklen; i++)
2335 if (data[i] == '\n')
2336 { /* fill in and move to next line */
2337 int l;
2338 long p;
2339 if (edit_get_byte (edit, edit->curs1) != '\n')
2341 l = width - (edit_get_col (edit) - col);
2342 while (l > 0)
2344 edit_insert (edit, ' ');
2345 l -= space_width;
2348 for (p = edit->curs1;; p++)
2350 if (p == edit->last_byte)
2352 edit_cursor_move (edit, edit->last_byte - edit->curs1);
2353 edit_insert_ahead (edit, '\n');
2354 p++;
2355 break;
2357 if (edit_get_byte (edit, p) == '\n')
2359 p++;
2360 break;
2363 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
2364 l = col - edit_get_col (edit);
2365 while (l >= space_width)
2367 edit_insert (edit, ' ');
2368 l -= space_width;
2370 continue;
2372 edit_insert (edit, data[i]);
2375 *col1 = col;
2376 *col2 = col + width;
2377 *start_pos = cursor;
2378 *end_pos = edit->curs1;
2379 edit_cursor_move (edit, cursor - edit->curs1);
2380 g_free (data);
2382 return blocklen;
2385 /* --------------------------------------------------------------------------------------------- */
2387 void
2388 edit_block_copy_cmd (WEdit * edit)
2390 long start_mark, end_mark, current = edit->curs1;
2391 long col_delta = 0;
2392 long mark1, mark2;
2393 int c1, c2;
2394 int size;
2395 unsigned char *copy_buf;
2397 edit_update_curs_col (edit);
2398 if (eval_marks (edit, &start_mark, &end_mark))
2399 return;
2401 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2403 /* all that gets pushed are deletes hence little space is used on the stack */
2405 edit_push_markers (edit);
2407 if (edit->column_highlight)
2409 col_delta = abs (edit->column2 - edit->column1);
2410 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2412 else
2414 while (size--)
2415 edit_insert_ahead (edit, copy_buf[size]);
2418 g_free (copy_buf);
2419 edit_scroll_screen_over_cursor (edit);
2421 if (edit->column_highlight)
2422 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2423 else if (start_mark < current && end_mark > current)
2424 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2426 edit->force |= REDRAW_PAGE;
2430 /* --------------------------------------------------------------------------------------------- */
2432 void
2433 edit_block_move_cmd (WEdit * edit)
2435 long current;
2436 unsigned char *copy_buf = NULL;
2437 long start_mark, end_mark;
2438 long line;
2440 if (eval_marks (edit, &start_mark, &end_mark))
2441 return;
2443 line = edit->curs_line;
2444 if (edit->mark2 < 0)
2445 edit_mark_cmd (edit, FALSE);
2446 edit_push_markers (edit);
2448 if (edit->column_highlight)
2450 long mark1, mark2;
2451 int size;
2452 int b_width = 0;
2453 int c1, c2;
2454 int x, x2;
2456 c1 = min (edit->column1, edit->column2);
2457 c2 = max (edit->column1, edit->column2);
2458 b_width = (c2 - c1);
2460 edit_update_curs_col (edit);
2462 x = edit->curs_col;
2463 x2 = x + edit->over_col;
2465 /* do nothing when cursor inside first line of selected area */
2466 if ((edit_eol (edit, edit->curs1) == edit_eol (edit, start_mark)) && (x2 > c1 && x2 <= c2))
2467 return;
2469 if (edit->curs1 > start_mark && edit->curs1 < edit_eol (edit, end_mark))
2471 if (x > c2)
2472 x -= b_width;
2473 else if (x > c1 && x <= c2)
2474 x = c1;
2476 /* save current selection into buffer */
2477 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2479 /* remove current selection */
2480 edit_block_delete_cmd (edit);
2482 edit->over_col = max (0, edit->over_col - b_width);
2483 /* calculate the cursor pos after delete block */
2484 current = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), x, 0);
2485 edit_cursor_move (edit, current - edit->curs1);
2486 edit_scroll_screen_over_cursor (edit);
2488 /* add TWS if need before block insertion */
2489 if (option_cursor_beyond_eol && edit->over_col > 0)
2490 edit_insert_over (edit);
2492 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2493 edit_set_markers (edit, mark1, mark2, c1, c2);
2495 else
2497 long count;
2499 current = edit->curs1;
2500 copy_buf = g_malloc0 (end_mark - start_mark);
2501 edit_cursor_move (edit, start_mark - edit->curs1);
2502 edit_scroll_screen_over_cursor (edit);
2503 count = start_mark;
2504 while (count < end_mark)
2506 copy_buf[end_mark - count - 1] = edit_delete (edit, 1);
2507 count++;
2509 edit_scroll_screen_over_cursor (edit);
2510 edit_cursor_move (edit,
2511 current - edit->curs1 -
2512 (((current - edit->curs1) > 0) ? end_mark - start_mark : 0));
2513 edit_scroll_screen_over_cursor (edit);
2514 while (count-- > start_mark)
2515 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2516 edit_set_markers (edit, edit->curs1, edit->curs1 + end_mark - start_mark, 0, 0);
2519 edit_scroll_screen_over_cursor (edit);
2520 g_free (copy_buf);
2521 edit->force |= REDRAW_PAGE;
2524 /* --------------------------------------------------------------------------------------------- */
2525 /** returns 1 if canceelled by user */
2528 edit_block_delete_cmd (WEdit * edit)
2530 long start_mark, end_mark;
2531 if (eval_marks (edit, &start_mark, &end_mark))
2533 edit_delete_line (edit);
2534 return 0;
2536 return edit_block_delete (edit);
2539 /* --------------------------------------------------------------------------------------------- */
2540 /** call with edit = 0 before shutdown to close memory leaks */
2542 void
2543 edit_replace_cmd (WEdit * edit, int again)
2545 /* 1 = search string, 2 = replace with */
2546 static char *saved1 = NULL; /* saved default[123] */
2547 static char *saved2 = NULL;
2548 char *input1 = NULL; /* user input from the dialog */
2549 char *input2 = NULL;
2550 GString *input2_str = NULL;
2551 char *disp1 = NULL;
2552 char *disp2 = NULL;
2553 long times_replaced = 0;
2554 gboolean once_found = FALSE;
2556 if (!edit)
2558 g_free (saved1), saved1 = NULL;
2559 g_free (saved2), saved2 = NULL;
2560 return;
2563 edit->force |= REDRAW_COMPLETELY;
2565 if (again && !saved1 && !saved2)
2566 again = 0;
2568 if (again)
2570 input1 = g_strdup (saved1 ? saved1 : "");
2571 input2 = g_strdup (saved2 ? saved2 : "");
2573 else
2575 char *tmp_inp1, *tmp_inp2;
2577 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
2578 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
2580 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2582 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2584 g_free (disp1);
2585 g_free (disp2);
2587 if (input1 == NULL || *input1 == '\0')
2589 edit->force = REDRAW_COMPLETELY;
2590 goto cleanup;
2593 tmp_inp1 = input1;
2594 tmp_inp2 = input2;
2595 input1 = edit_replace_cmd__conv_to_input (input1);
2596 input2 = edit_replace_cmd__conv_to_input (input2);
2597 g_free (tmp_inp1);
2598 g_free (tmp_inp2);
2600 g_free (saved1), saved1 = g_strdup (input1);
2601 g_free (saved2), saved2 = g_strdup (input2);
2603 mc_search_free (edit->search);
2604 edit->search = NULL;
2607 input2_str = g_string_new (input2);
2609 if (!edit->search)
2611 edit->search = mc_search_new (input1, -1);
2612 if (edit->search == NULL)
2614 edit->search_start = edit->curs1;
2615 goto cleanup;
2617 edit->search->search_type = edit_search_options.type;
2618 edit->search->is_all_charsets = edit_search_options.all_codepages;
2619 edit->search->is_case_sensitive = edit_search_options.case_sens;
2620 edit->search->whole_words = edit_search_options.whole_words;
2621 edit->search->search_fn = edit_search_cmd_callback;
2622 edit->search_line_type = edit_get_search_line_type (edit->search);
2623 edit_search_fix_search_start_if_selection (edit);
2626 if (edit->found_len && edit->search_start == edit->found_start + 1
2627 && edit_search_options.backwards)
2628 edit->search_start--;
2630 if (edit->found_len && edit->search_start == edit->found_start - 1
2631 && !edit_search_options.backwards)
2632 edit->search_start++;
2636 gsize len = 0;
2638 if (!editcmd_find (edit, &len))
2640 if (!(edit->search->error == MC_SEARCH_E_OK ||
2641 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2643 edit_error_dialog (_("Search"), edit->search->error_str);
2645 break;
2647 once_found = TRUE;
2649 edit->search_start = edit->search->normal_offset;
2650 /*returns negative on not found or error in pattern */
2652 if ((edit->search_start >= 0) && (edit->search_start < edit->last_byte))
2654 gsize i;
2655 GString *repl_str;
2657 edit->found_start = edit->search_start;
2658 i = edit->found_len = len;
2660 edit_cursor_move (edit, edit->search_start - edit->curs1);
2661 edit_scroll_screen_over_cursor (edit);
2663 if (edit->replace_mode == 0)
2665 long l;
2666 int prompt;
2668 l = (long) (edit->curs_row - edit->widget.lines / 3);
2669 if (l > 0)
2670 edit_scroll_downward (edit, l);
2671 if (l < 0)
2672 edit_scroll_upward (edit, -l);
2674 edit_scroll_screen_over_cursor (edit);
2675 edit->force |= REDRAW_PAGE;
2676 edit_render_keypress (edit);
2678 /*so that undo stops at each query */
2679 edit_push_key_press (edit);
2680 /* and prompt 2/3 down */
2681 disp1 = edit_replace_cmd__conv_to_display (saved1);
2682 disp2 = edit_replace_cmd__conv_to_display (saved2);
2683 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2684 g_free (disp1);
2685 g_free (disp2);
2687 if (prompt == B_REPLACE_ALL)
2688 edit->replace_mode = 1;
2689 else if (prompt == B_SKIP_REPLACE)
2691 if (edit_search_options.backwards)
2692 edit->search_start--;
2693 else
2694 edit->search_start++;
2695 continue; /* loop */
2697 else if (prompt == B_CANCEL)
2699 edit->replace_mode = -1;
2700 break; /* loop */
2704 repl_str = mc_search_prepare_replace_str (edit->search, input2_str);
2706 if (edit->search->error != MC_SEARCH_E_OK)
2708 edit_error_dialog (_("Replace"), edit->search->error_str);
2709 g_string_free (repl_str, TRUE);
2710 break;
2713 /* delete then insert new */
2714 for (i = 0; i < len; i++)
2715 edit_delete (edit, 1);
2717 for (i = 0; i < repl_str->len; i++)
2718 edit_insert (edit, repl_str->str[i]);
2720 edit->found_len = repl_str->len;
2721 g_string_free (repl_str, TRUE);
2722 times_replaced++;
2724 /* so that we don't find the same string again */
2725 if (edit_search_options.backwards)
2727 edit->search_start--;
2729 else
2731 edit->search_start += edit->found_len + (len == 0 ? 1 : 0);
2733 if (edit->search_start >= edit->last_byte)
2734 break;
2737 edit_scroll_screen_over_cursor (edit);
2739 else
2741 /* try and find from right here for next search */
2742 edit->search_start = edit->curs1;
2743 edit_update_curs_col (edit);
2745 edit->force |= REDRAW_PAGE;
2746 edit_render_keypress (edit);
2748 if (times_replaced == 0)
2749 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL, 1, _("&OK"));
2750 break;
2753 while (edit->replace_mode >= 0);
2755 edit_scroll_screen_over_cursor (edit);
2756 edit->force |= REDRAW_COMPLETELY;
2757 edit_render_keypress (edit);
2759 if ((edit->replace_mode == 1) && (times_replaced != 0))
2760 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2762 cleanup:
2763 g_free (input1);
2764 g_free (input2);
2765 if (input2_str != NULL)
2766 g_string_free (input2_str, TRUE);
2769 /* --------------------------------------------------------------------------------------------- */
2771 mc_search_cbret_t
2772 edit_search_cmd_callback (const void *user_data, gsize char_offset, int *current_char)
2774 *current_char = edit_get_byte ((WEdit *) user_data, (long) char_offset);
2775 return MC_SEARCH_CB_OK;
2778 /* --------------------------------------------------------------------------------------------- */
2780 void
2781 edit_search_cmd (WEdit * edit, gboolean again)
2784 if (edit == NULL)
2785 return;
2787 if (!again)
2788 edit_search (edit);
2789 else if (edit->last_search_string != NULL)
2790 edit_do_search (edit);
2791 else
2793 /* find last search string in history */
2794 GList *history;
2796 history = history_get (MC_HISTORY_SHARED_SEARCH);
2797 if (history != NULL && history->data != NULL)
2799 edit->last_search_string = (char *) history->data;
2800 history->data = NULL;
2801 history = g_list_first (history);
2802 g_list_foreach (history, (GFunc) g_free, NULL);
2803 g_list_free (history);
2805 edit->search = mc_search_new (edit->last_search_string, -1);
2806 if (edit->search == NULL)
2808 /* if not... then ask for an expression */
2809 g_free (edit->last_search_string);
2810 edit->last_search_string = NULL;
2811 edit_search (edit);
2813 else
2815 edit->search->search_type = edit_search_options.type;
2816 edit->search->is_all_charsets = edit_search_options.all_codepages;
2817 edit->search->is_case_sensitive = edit_search_options.case_sens;
2818 edit->search->whole_words = edit_search_options.whole_words;
2819 edit->search->search_fn = edit_search_cmd_callback;
2820 edit->search_line_type = edit_get_search_line_type (edit->search);
2821 edit_do_search (edit);
2824 else
2826 /* if not... then ask for an expression */
2827 g_free (edit->last_search_string);
2828 edit->last_search_string = NULL;
2829 edit_search (edit);
2835 /* --------------------------------------------------------------------------------------------- */
2837 * Check if it's OK to close the file. If there are unsaved changes, ask user.
2839 * @returns TRUE if it's OK to exit, FALSE to continue editing.
2842 gboolean
2843 edit_ok_to_exit (WEdit * edit)
2845 char *fname = (char *) N_("[NoName]");
2846 char *msg;
2847 int act;
2849 if (!edit->modified)
2850 return TRUE;
2852 if (edit->filename_vpath != NULL)
2853 fname = vfs_path_to_str (edit->filename_vpath);
2854 #ifdef ENABLE_NLS
2855 else
2856 fname = g_strdup (_(fname));
2857 #else
2858 else
2859 fname = g_strdup (fname);
2860 #endif
2862 if (!mc_global.midnight_shutdown)
2864 if (!edit_check_newline (edit))
2866 g_free (fname);
2867 return FALSE;
2870 query_set_sel (2);
2872 msg = g_strdup_printf (_("File %s was modified.\nSave before close?"), fname);
2873 act = edit_query_dialog3 (_("Close file"), msg, _("&Yes"), _("&No"), _("&Cancel"));
2875 else
2877 msg = g_strdup_printf (_("Midnight Commander is being shut down.\nSave modified file %s?"),
2878 fname);
2879 act = edit_query_dialog2 (_("Quit"), msg, _("&Yes"), _("&No"));
2881 /* Esc is No */
2882 if (act == -1)
2883 act = 1;
2886 g_free (msg);
2887 g_free (fname);
2889 switch (act)
2891 case 0: /* Yes */
2892 edit_push_markers (edit);
2893 edit_set_markers (edit, 0, 0, 0, 0);
2894 if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2895 return mc_global.midnight_shutdown;
2896 break;
2897 case 1: /* No */
2898 break;
2899 case 2: /* Cancel quit */
2900 case -1: /* Esc */
2901 return FALSE;
2904 return TRUE;
2907 /* --------------------------------------------------------------------------------------------- */
2908 /** save block, returns TRUE on success */
2910 gboolean
2911 edit_save_block (WEdit * edit, const char *filename, long start, long finish)
2913 int len, file;
2914 vfs_path_t *vpath;
2916 vpath = vfs_path_from_str (filename);
2917 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2918 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2919 vfs_path_free (vpath);
2920 if (file == -1)
2921 return FALSE;
2923 if (edit->column_highlight)
2925 int r;
2927 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2928 if (r > 0)
2930 unsigned char *block, *p;
2932 p = block = edit_get_block (edit, start, finish, &len);
2933 while (len)
2935 r = mc_write (file, p, len);
2936 if (r < 0)
2937 break;
2938 p += r;
2939 len -= r;
2941 g_free (block);
2944 else
2946 unsigned char *buf;
2947 int i = start, end;
2949 len = finish - start;
2950 buf = g_malloc0 (TEMP_BUF_LEN);
2951 while (start != finish)
2953 end = min (finish, start + TEMP_BUF_LEN);
2954 for (; i < end; i++)
2955 buf[i - start] = edit_get_byte (edit, i);
2956 len -= mc_write (file, (char *) buf, end - start);
2957 start = end;
2959 g_free (buf);
2961 mc_close (file);
2963 return (len == 0);
2966 /* --------------------------------------------------------------------------------------------- */
2968 void
2969 edit_paste_from_history (WEdit * edit)
2971 (void) edit;
2972 edit_error_dialog (_("Error"), _("This function is not implemented"));
2975 /* --------------------------------------------------------------------------------------------- */
2977 gboolean
2978 edit_copy_to_X_buf_cmd (WEdit * edit)
2980 long start_mark, end_mark;
2982 if (eval_marks (edit, &start_mark, &end_mark))
2983 return TRUE;
2984 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2986 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2987 return FALSE;
2989 /* try use external clipboard utility */
2990 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2992 return TRUE;
2995 /* --------------------------------------------------------------------------------------------- */
2997 gboolean
2998 edit_cut_to_X_buf_cmd (WEdit * edit)
3000 long start_mark, end_mark;
3002 if (eval_marks (edit, &start_mark, &end_mark))
3003 return TRUE;
3004 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
3006 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
3007 return FALSE;
3009 /* try use external clipboard utility */
3010 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
3012 edit_block_delete_cmd (edit);
3013 edit_mark_cmd (edit, TRUE);
3015 return TRUE;
3018 /* --------------------------------------------------------------------------------------------- */
3020 gboolean
3021 edit_paste_from_X_buf_cmd (WEdit * edit)
3023 vfs_path_t *tmp;
3024 gboolean ret;
3026 /* try use external clipboard utility */
3027 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
3028 tmp = mc_config_get_full_vpath (EDIT_CLIP_FILE);
3029 ret = (edit_insert_file (edit, tmp) >= 0);
3030 vfs_path_free (tmp);
3032 return ret;
3035 /* --------------------------------------------------------------------------------------------- */
3037 * Ask user for the line and go to that line.
3038 * Negative numbers mean line from the end (i.e. -1 is the last line).
3041 void
3042 edit_goto_cmd (WEdit * edit)
3044 char *f;
3045 static long line = 0; /* line as typed, saved as default */
3046 long l;
3047 char *error;
3048 char s[32];
3050 g_snprintf (s, sizeof (s), "%ld", line);
3051 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE, line ? s : "");
3052 if (!f)
3053 return;
3055 if (!*f)
3057 g_free (f);
3058 return;
3061 l = strtol (f, &error, 0);
3062 if (*error)
3064 g_free (f);
3065 return;
3068 line = l;
3069 if (l < 0)
3070 l = edit->total_lines + l + 2;
3071 edit_move_display (edit, l - edit->widget.lines / 2 - 1);
3072 edit_move_to_line (edit, l - 1);
3073 edit->force |= REDRAW_COMPLETELY;
3074 g_free (f);
3078 /* --------------------------------------------------------------------------------------------- */
3079 /** Return TRUE on success */
3081 gboolean
3082 edit_save_block_cmd (WEdit * edit)
3084 long start_mark, end_mark;
3085 char *exp, *tmp;
3086 gboolean ret = FALSE;
3088 if (eval_marks (edit, &start_mark, &end_mark))
3089 return TRUE;
3091 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3092 exp =
3093 input_expand_dialog (_("Save block"), _("Enter file name:"),
3094 MC_HISTORY_EDIT_SAVE_BLOCK, tmp);
3095 g_free (tmp);
3096 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3098 if (exp != NULL && *exp != '\0')
3100 if (edit_save_block (edit, exp, start_mark, end_mark))
3101 ret = TRUE;
3102 else
3103 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3105 edit->force |= REDRAW_COMPLETELY;
3108 g_free (exp);
3110 return ret;
3114 /* --------------------------------------------------------------------------------------------- */
3116 /** returns TRUE on success */
3117 gboolean
3118 edit_insert_file_cmd (WEdit * edit)
3120 char *tmp;
3121 char *exp;
3122 gboolean ret = FALSE;
3124 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3125 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3126 MC_HISTORY_EDIT_INSERT_FILE, tmp);
3127 g_free (tmp);
3129 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3131 if (exp != NULL && *exp != '\0')
3133 vfs_path_t *exp_vpath;
3135 exp_vpath = vfs_path_from_str (exp);
3136 ret = (edit_insert_file (edit, exp_vpath) >= 0);
3137 vfs_path_free (exp_vpath);
3139 if (!ret)
3140 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3143 g_free (exp);
3145 edit->force |= REDRAW_COMPLETELY;
3146 return ret;
3149 /* --------------------------------------------------------------------------------------------- */
3150 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3153 edit_sort_cmd (WEdit * edit)
3155 static char *old = 0;
3156 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3157 long start_mark, end_mark;
3158 int e;
3160 if (eval_marks (edit, &start_mark, &end_mark))
3162 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3163 return 0;
3166 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
3167 edit_save_block (edit, tmp, start_mark, end_mark);
3168 g_free (tmp);
3170 exp = input_dialog (_("Run sort"),
3171 _("Enter sort options (see manpage) separated by whitespace:"),
3172 MC_HISTORY_EDIT_SORT, (old != NULL) ? old : "");
3174 if (!exp)
3175 return 1;
3176 g_free (old);
3177 old = exp;
3178 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
3179 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
3180 tmp =
3181 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3182 " > ", tmp_edit_temp_name, (char *) NULL);
3183 g_free (tmp_edit_temp_name);
3184 g_free (tmp_edit_block_name);
3186 e = system (tmp);
3187 g_free (tmp);
3188 if (e)
3190 if (e == -1 || e == 127)
3192 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3194 else
3196 char q[8];
3197 sprintf (q, "%d ", e);
3198 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3199 edit_error_dialog (_("Sort"), tmp);
3200 g_free (tmp);
3202 return -1;
3205 edit->force |= REDRAW_COMPLETELY;
3207 if (edit_block_delete_cmd (edit))
3208 return 1;
3211 vfs_path_t *tmp_vpath;
3213 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3214 edit_insert_file (edit, tmp_vpath);
3215 vfs_path_free (tmp_vpath);
3217 return 0;
3220 /* --------------------------------------------------------------------------------------------- */
3222 * Ask user for a command, execute it and paste its output back to the
3223 * editor.
3227 edit_ext_cmd (WEdit * edit)
3229 char *exp, *tmp, *tmp_edit_temp_file;
3230 int e;
3232 exp =
3233 input_dialog (_("Paste output of external command"),
3234 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, NULL);
3236 if (!exp)
3237 return 1;
3239 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3240 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3241 g_free (tmp_edit_temp_file);
3242 e = system (tmp);
3243 g_free (tmp);
3244 g_free (exp);
3246 if (e)
3248 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3249 return -1;
3252 edit->force |= REDRAW_COMPLETELY;
3255 vfs_path_t *tmp_vpath;
3257 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3258 edit_insert_file (edit, tmp_vpath);
3259 vfs_path_free (tmp_vpath);
3261 return 0;
3264 /* --------------------------------------------------------------------------------------------- */
3265 /** if block is 1, a block must be highlighted and the shell command
3266 processes it. If block is 0 the shell command is a straight system
3267 command, that just produces some output which is to be inserted */
3269 void
3270 edit_block_process_cmd (WEdit * edit, int macro_number)
3272 char *fname;
3273 char *macros_fname = NULL;
3275 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3276 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3277 user_menu (edit, macros_fname, 0);
3278 g_free (fname);
3279 g_free (macros_fname);
3280 edit->force |= REDRAW_COMPLETELY;
3283 /* --------------------------------------------------------------------------------------------- */
3285 void
3286 edit_mail_dialog (WEdit * edit)
3288 char *tmail_to;
3289 char *tmail_subject;
3290 char *tmail_cc;
3292 static char *mail_cc_last = 0;
3293 static char *mail_subject_last = 0;
3294 static char *mail_to_last = 0;
3296 QuickWidget quick_widgets[] = {
3297 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
3298 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
3299 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input", &tmail_cc),
3300 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT, N_("Copies to")),
3301 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-2",
3302 &tmail_subject),
3303 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT, N_("Subject")),
3304 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-3", &tmail_to),
3305 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT, N_("To")),
3306 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT, N_("mail -s <subject> -c <cc> <to>")),
3307 QUICK_END
3310 QuickDialog Quick_input = {
3311 50, MAIL_DLG_HEIGHT, -1, -1, N_("Mail"),
3312 "[Input Line Keys]", quick_widgets, NULL, NULL, FALSE
3315 quick_widgets[2].u.input.text = mail_cc_last ? mail_cc_last : "";
3316 quick_widgets[4].u.input.text = mail_subject_last ? mail_subject_last : "";
3317 quick_widgets[6].u.input.text = mail_to_last ? mail_to_last : "";
3319 if (quick_dialog (&Quick_input) != B_CANCEL)
3321 g_free (mail_cc_last);
3322 g_free (mail_subject_last);
3323 g_free (mail_to_last);
3324 mail_cc_last = tmail_cc;
3325 mail_subject_last = tmail_subject;
3326 mail_to_last = tmail_to;
3327 pipe_mail (edit, mail_to_last, mail_subject_last, mail_cc_last);
3332 /*******************/
3333 /* Word Completion */
3334 /*******************/
3336 /* --------------------------------------------------------------------------------------------- */
3338 * Complete current word using regular expression search
3339 * backwards beginning at the current cursor position.
3342 void
3343 edit_complete_word_cmd (WEdit * edit)
3345 gsize i, max_len, word_len = 0, num_compl = 0;
3346 long word_start = 0;
3347 unsigned char *bufpos;
3348 char *match_expr;
3349 struct selection compl[MAX_WORD_COMPLETIONS]; /* completions */
3351 /* search start of word to be completed */
3352 if (!edit_find_word_start (edit, &word_start, &word_len))
3353 return;
3355 /* prepare match expression */
3356 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3358 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3359 match_expr =
3360 g_strdup_printf
3361 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3362 (int) word_len, bufpos);
3364 /* collect the possible completions */
3365 /* start search from begin to end of file */
3366 max_len =
3367 edit_collect_completions (edit, word_start, word_len, match_expr,
3368 (struct selection *) &compl, &num_compl);
3370 if (num_compl > 0)
3372 /* insert completed word if there is only one match */
3373 if (num_compl == 1)
3375 for (i = word_len; i < compl[0].len; i++)
3376 edit_insert (edit, *(compl[0].text + i));
3378 /* more than one possible completion => ask the user */
3379 else
3381 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3382 /* !!! pressed again the selection dialog pops up, but that !!! */
3383 /* !!! seems to require a further internal state !!! */
3384 /*tty_beep (); */
3386 /* let the user select the preferred completion */
3387 editcmd_dialog_completion_show (edit, max_len, word_len,
3388 (struct selection *) &compl, num_compl);
3392 g_free (match_expr);
3393 /* release memory before return */
3394 for (i = 0; i < num_compl; i++)
3395 g_free (compl[i].text);
3398 /* --------------------------------------------------------------------------------------------- */
3400 #ifdef HAVE_CHARSET
3401 void
3402 edit_select_codepage_cmd (WEdit * edit)
3404 if (do_select_codepage ())
3405 edit_set_codeset (edit);
3407 edit->force = REDRAW_PAGE;
3408 send_message ((Widget *) edit, WIDGET_DRAW, 0);
3410 #endif
3412 /* --------------------------------------------------------------------------------------------- */
3414 void
3415 edit_insert_literal_cmd (WEdit * edit)
3417 int char_for_insertion;
3419 char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3420 _("Press any key:"), FALSE);
3421 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3424 /* --------------------------------------------------------------------------------------------- */
3426 void
3427 edit_begin_end_macro_cmd (WEdit * edit)
3429 /* edit is a pointer to the widget */
3430 if (edit != NULL)
3432 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3433 edit_execute_key_command (edit, command, -1);
3437 /* --------------------------------------------------------------------------------------------- */
3439 void
3440 edit_begin_end_repeat_cmd (WEdit * edit)
3442 /* edit is a pointer to the widget */
3443 if (edit != NULL)
3445 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3446 edit_execute_key_command (edit, command, -1);
3450 /* --------------------------------------------------------------------------------------------- */
3452 gboolean
3453 edit_load_forward_cmd (WEdit * edit)
3455 if (edit->modified
3456 && edit_query_dialog2 (_("Warning"),
3457 _("Current text was modified without a file save.\n"
3458 "Continue discards these changes"), _("C&ontinue"),
3459 _("&Cancel")) == 1)
3461 edit->force |= REDRAW_COMPLETELY;
3462 return TRUE;
3465 if (edit_stack_iterator + 1 >= MAX_HISTORY_MOVETO)
3466 return FALSE;
3468 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3469 return FALSE;
3471 edit_stack_iterator++;
3472 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3473 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3474 edit_history_moveto[edit_stack_iterator].line);
3476 return FALSE;
3479 /* --------------------------------------------------------------------------------------------- */
3481 gboolean
3482 edit_load_back_cmd (WEdit * edit)
3484 if (edit->modified
3485 && edit_query_dialog2 (_("Warning"),
3486 _("Current text was modified without a file save.\n"
3487 "Continue discards these changes"), _("C&ontinue"),
3488 _("&Cancel")) == 1)
3490 edit->force |= REDRAW_COMPLETELY;
3491 return TRUE;
3494 /* we are in the bottom of the stack, NO WAY! */
3495 if (edit_stack_iterator == 0)
3496 return FALSE;
3498 edit_stack_iterator--;
3499 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3500 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3501 edit_history_moveto[edit_stack_iterator].line);
3503 return FALSE;
3506 /* --------------------------------------------------------------------------------------------- */
3508 void
3509 edit_get_match_keyword_cmd (WEdit * edit)
3511 gsize word_len = 0, max_len = 0;
3512 int num_def = 0;
3513 int i;
3514 long word_start = 0;
3515 unsigned char *bufpos;
3516 char *match_expr;
3517 char *path = NULL;
3518 char *ptr = NULL;
3519 char *tagfile = NULL;
3521 etags_hash_t def_hash[MAX_DEFINITIONS];
3523 for (i = 0; i < MAX_DEFINITIONS; i++)
3525 def_hash[i].filename = NULL;
3528 /* search start of word to be completed */
3529 if (!edit_find_word_start (edit, &word_start, &word_len))
3530 return;
3532 /* prepare match expression */
3533 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3534 match_expr = g_strdup_printf ("%.*s", (int) word_len, bufpos);
3536 ptr = g_get_current_dir ();
3537 path = g_strconcat (ptr, G_DIR_SEPARATOR_S, (char *) NULL);
3538 g_free (ptr);
3540 /* Recursive search file 'TAGS' in parent dirs */
3543 ptr = g_path_get_dirname (path);
3544 g_free (path);
3545 path = ptr;
3546 g_free (tagfile);
3547 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3548 if (exist_file (tagfile))
3549 break;
3551 while (strcmp (path, G_DIR_SEPARATOR_S) != 0);
3553 if (tagfile)
3555 num_def =
3556 etags_set_definition_hash (tagfile, path, match_expr, (etags_hash_t *) & def_hash);
3557 g_free (tagfile);
3559 g_free (path);
3561 max_len = MAX_WIDTH_DEF_DIALOG;
3562 word_len = 0;
3563 if (num_def > 0)
3565 editcmd_dialog_select_definition_show (edit, match_expr, max_len, word_len,
3566 (etags_hash_t *) & def_hash, num_def);
3568 g_free (match_expr);
3571 /* --------------------------------------------------------------------------------------------- */
3573 #ifdef HAVE_ASPELL
3575 edit_suggest_current_word (WEdit * edit)
3577 gsize cut_len = 0;
3578 gsize word_len = 0;
3579 long word_start = 0;
3580 int retval = B_SKIP_WORD;
3581 char *match_word;
3583 /* search start of word to spell check */
3584 match_word = edit_get_word_from_pos (edit, edit->curs1, &word_start, &word_len, &cut_len);
3586 #ifdef HAVE_CHARSET
3587 if (mc_global.source_codepage >= 0 && (mc_global.source_codepage != mc_global.display_codepage))
3589 GString *tmp_word;
3591 tmp_word = str_convert_to_display (match_word);
3592 g_free (match_word);
3593 match_word = g_string_free (tmp_word, FALSE);
3595 #endif
3596 if (!aspell_check (match_word, (int) word_len))
3598 GArray *suggest;
3599 unsigned int res;
3601 suggest = g_array_new (TRUE, FALSE, sizeof (char *));
3603 res = aspell_suggest (suggest, match_word, (int) word_len);
3604 if (res != 0)
3606 char *new_word = NULL;
3608 edit->found_start = word_start;
3609 edit->found_len = word_len;
3610 edit->force |= REDRAW_PAGE;
3611 edit_scroll_screen_over_cursor (edit);
3612 edit_render_keypress (edit);
3614 retval = spell_dialog_spell_suggest_show (edit, match_word, &new_word, suggest);
3615 edit_cursor_move (edit, word_len - cut_len);
3617 if (retval == B_ENTER && new_word != NULL)
3619 guint i;
3620 char *cp_word;
3622 #ifdef HAVE_CHARSET
3623 if (mc_global.source_codepage >= 0 &&
3624 (mc_global.source_codepage != mc_global.display_codepage))
3626 GString *tmp_word;
3628 tmp_word = str_convert_to_input (new_word);
3629 g_free (new_word);
3630 new_word = g_string_free (tmp_word, FALSE);
3632 #endif
3633 cp_word = new_word;
3634 for (i = 0; i < word_len; i++)
3635 edit_backspace (edit, 1);
3636 for (; *new_word; new_word++)
3637 edit_insert (edit, *new_word);
3638 g_free (cp_word);
3640 else if (retval == B_ADD_WORD && match_word != NULL)
3641 aspell_add_to_dict (match_word, (int) word_len);
3644 g_array_free (suggest, TRUE);
3645 edit->found_start = 0;
3646 edit->found_len = 0;
3648 g_free (match_word);
3649 return retval;
3652 /* --------------------------------------------------------------------------------------------- */
3654 void
3655 edit_spellcheck_file (WEdit * edit)
3657 if (edit->curs_line > 0)
3659 edit_cursor_move (edit, -edit->curs1);
3660 edit_move_to_prev_col (edit, 0);
3661 edit_update_curs_row (edit);
3666 int c1, c2;
3668 c2 = edit_get_byte (edit, edit->curs1);
3672 if (edit->curs1 >= edit->last_byte)
3673 return;
3675 c1 = c2;
3676 edit_cursor_move (edit, 1);
3677 c2 = edit_get_byte (edit, edit->curs1);
3679 while (is_break_char (c1) || is_break_char (c2));
3681 while (edit_suggest_current_word (edit) != B_CANCEL);
3684 /* --------------------------------------------------------------------------------------------- */
3686 void
3687 edit_set_spell_lang (void)
3689 GArray *lang_list;
3691 lang_list = g_array_new (TRUE, FALSE, sizeof (char *));
3692 if (aspell_get_lang_list (lang_list) != 0)
3694 char *lang;
3696 lang = spell_dialog_lang_list_show (lang_list);
3697 if (lang != NULL)
3698 (void) aspell_set_lang (lang);
3700 aspell_array_clean (lang_list);
3702 #endif /* HAVE_ASPELL */
3704 /* --------------------------------------------------------------------------------------------- */