Fix of DOXYGEN docs: @return instead of @returns
[midnight-commander.git] / src / editor / editcmd.c
blob7671186a8e36928b86f49d7d96cc1ef01d9c4dcc
1 /*
2 Editor high level editing commands
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2011, 2012
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer, 1996, 1997
10 Andrew Borodin <aborodin@vmail.ru> 2012
11 Ilia Maslakov <il.smind@gmail.com> 2012
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /** \file
30 * \brief Source: editor high level editing commands
31 * \author Paul Sheer
32 * \date 1996, 1997
35 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
37 #include <config.h>
39 #ifdef HAVE_ASSERT_H
40 #include <assert.h>
41 #endif
42 #include <ctype.h>
44 #include <stdio.h>
45 #include <stdarg.h>
46 #include <sys/types.h>
47 #include <unistd.h>
48 #include <string.h>
49 #include <errno.h>
50 #include <sys/stat.h>
51 #include <stdlib.h>
52 #include <fcntl.h>
54 #include "lib/global.h"
55 #include "lib/tty/tty.h"
56 #include "lib/tty/key.h" /* XCTRL */
57 #include "lib/mcconfig.h"
58 #include "lib/skin.h"
59 #include "lib/strutil.h" /* utf string functions */
60 #include "lib/lock.h"
61 #include "lib/util.h" /* tilde_expand() */
62 #include "lib/vfs/vfs.h"
63 #include "lib/widget.h"
64 #include "lib/event.h" /* mc_event_raise() */
65 #ifdef HAVE_CHARSET
66 #include "lib/charsets.h"
67 #endif
69 #include "src/history.h"
70 #include "src/setup.h" /* option_tab_spacing */
71 #ifdef HAVE_CHARSET
72 #include "src/selcodepage.h"
73 #endif
74 #include "src/keybind-defaults.h"
75 #include "src/util.h" /* check_for_default() */
76 #include "src/filemanager/layout.h" /* mc_refresh() */
78 #include "edit-impl.h"
79 #include "editwidget.h"
80 #include "editcmd_dialogs.h"
81 #ifdef HAVE_ASPELL
82 #include "spell.h"
83 #include "spell_dialogs.h"
84 #endif
85 #include "etags.h"
87 /*** global variables ****************************************************************************/
89 /* search and replace: */
90 int search_create_bookmark = FALSE;
92 /* queries on a save */
93 int edit_confirm_save = 1;
95 /*** file scope macro definitions ****************************************************************/
97 #define space_width 1
99 #define TEMP_BUF_LEN 1024
101 #define INPUT_INDEX 9
103 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
104 (and the above) routines to work properly - paul */
106 #define is_digit(x) ((x) >= '0' && (x) <= '9')
108 #define MAIL_DLG_HEIGHT 12
110 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
112 /*** file scope type declarations ****************************************************************/
114 /*** file scope variables ************************************************************************/
116 /*** file scope functions ************************************************************************/
117 /* --------------------------------------------------------------------------------------------- */
119 /* If 0 (quick save) then a) create/truncate <filename> file,
120 b) save to <filename>;
121 if 1 (safe save) then a) save to <tempnam>,
122 b) rename <tempnam> to <filename>;
123 if 2 (do backups) then a) save to <tempnam>,
124 b) rename <filename> to <filename.backup_ext>,
125 c) rename <tempnam> to <filename>. */
127 /* returns 0 on error, -1 on abort */
129 static int
130 edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath)
132 char *p;
133 gchar *tmp;
134 off_t filelen = 0;
135 int this_save_mode, fd = -1;
136 vfs_path_t *real_filename_vpath;
137 vfs_path_t *savename_vpath = NULL;
138 const char *start_filename;
139 const vfs_path_element_t *vpath_element;
141 vpath_element = vfs_path_get_by_index (filename_vpath, 0);
142 if (vpath_element == NULL)
143 return 0;
145 start_filename = vpath_element->path;
146 if (*start_filename == '\0')
147 return 0;
149 if (*start_filename != PATH_SEP && edit->dir_vpath != NULL)
151 real_filename_vpath = vfs_path_append_vpath_new (edit->dir_vpath, filename_vpath, NULL);
153 else
155 real_filename_vpath = vfs_path_clone (filename_vpath);
158 this_save_mode = option_save_mode;
159 if (this_save_mode != EDIT_QUICK_SAVE)
161 if (!vfs_file_is_local (real_filename_vpath)
162 || (fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY)) == -1)
165 * The file does not exists yet, so no safe save or
166 * backup are necessary.
168 this_save_mode = EDIT_QUICK_SAVE;
170 if (fd != -1)
171 mc_close (fd);
174 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt)
176 int rv;
177 struct stat sb;
179 rv = mc_stat (real_filename_vpath, &sb);
180 if (rv == 0 && sb.st_nlink > 1)
182 rv = edit_query_dialog3 (_("Warning"),
183 _("File has hard-links. Detach before saving?"),
184 _("&Yes"), _("&No"), _("&Cancel"));
185 switch (rv)
187 case 0:
188 this_save_mode = EDIT_SAFE_SAVE;
189 /* fallthrough */
190 case 1:
191 edit->skip_detach_prompt = 1;
192 break;
193 default:
194 vfs_path_free (real_filename_vpath);
195 return -1;
199 /* Prevent overwriting changes from other editor sessions. */
200 if (rv == 0 && edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
202 /* The default action is "Cancel". */
203 query_set_sel (1);
205 rv = edit_query_dialog2 (_("Warning"),
206 _("The file has been modified in the meantime. Save anyway?"),
207 _("&Yes"), _("&Cancel"));
208 if (rv != 0)
210 vfs_path_free (real_filename_vpath);
211 return -1;
216 if (this_save_mode != EDIT_QUICK_SAVE)
218 char *savedir, *saveprefix;
220 savedir = vfs_path_tokens_get (real_filename_vpath, 0, -1);
221 if (savedir == NULL)
222 savedir = g_strdup (".");
224 /* Token-related function never return leading slash, so we need add it manually */
225 saveprefix = mc_build_filename ("/", savedir, "cooledit", NULL);
226 g_free (savedir);
227 fd = mc_mkstemps (&savename_vpath, saveprefix, NULL);
228 g_free (saveprefix);
229 if (savename_vpath == NULL)
231 vfs_path_free (real_filename_vpath);
232 return 0;
234 /* FIXME:
235 * Close for now because mc_mkstemps use pure open system call
236 * to create temporary file and it needs to be reopened by
237 * VFS-aware mc_open().
239 close (fd);
241 else
242 savename_vpath = vfs_path_clone (real_filename_vpath);
244 (void) mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
245 (void) mc_chmod (savename_vpath, edit->stat1.st_mode);
247 fd = mc_open (savename_vpath, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
248 if (fd == -1)
249 goto error_save;
251 /* pipe save */
252 p = edit_get_write_filter (savename_vpath, real_filename_vpath);
253 if (p != NULL)
255 FILE *file;
257 mc_close (fd);
258 file = (FILE *) popen (p, "w");
260 if (file)
262 filelen = edit_write_stream (edit, file);
263 #if 1
264 pclose (file);
265 #else
266 if (pclose (file) != 0)
268 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
269 edit_error_dialog (_("Error"), tmp);
270 g_free (tmp);
271 g_free (p);
272 goto error_save;
274 #endif
276 else
278 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
279 edit_error_dialog (_("Error"), get_sys_error (tmp));
280 g_free (p);
281 g_free (tmp);
282 goto error_save;
284 g_free (p);
286 else if (edit->lb == LB_ASIS)
287 { /* do not change line breaks */
288 off_t buf;
289 buf = 0;
290 filelen = edit->last_byte;
291 while (buf <= (edit->curs1 >> S_EDIT_BUF_SIZE) - 1)
293 if (mc_write (fd, (char *) edit->buffers1[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
295 mc_close (fd);
296 goto error_save;
298 buf++;
300 if (mc_write
301 (fd, (char *) edit->buffers1[buf],
302 edit->curs1 & M_EDIT_BUF_SIZE) != (edit->curs1 & M_EDIT_BUF_SIZE))
304 filelen = -1;
306 else if (edit->curs2)
308 edit->curs2--;
309 buf = (edit->curs2 >> S_EDIT_BUF_SIZE);
310 if (mc_write
311 (fd,
312 (char *) edit->buffers2[buf] + EDIT_BUF_SIZE -
313 (edit->curs2 & M_EDIT_BUF_SIZE) - 1,
314 1 + (edit->curs2 & M_EDIT_BUF_SIZE)) != 1 + (edit->curs2 & M_EDIT_BUF_SIZE))
316 filelen = -1;
318 else
320 while (--buf >= 0)
322 if (mc_write (fd, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
324 filelen = -1;
325 break;
329 edit->curs2++;
331 if (mc_close (fd))
332 goto error_save;
334 /* Update the file information, especially the mtime. */
335 if (mc_stat (savename_vpath, &edit->stat1) == -1)
336 goto error_save;
338 else
339 { /* change line breaks */
340 FILE *file;
341 const vfs_path_element_t *path_element;
343 mc_close (fd);
345 path_element = vfs_path_get_by_index (savename_vpath, -1);
346 file = (FILE *) fopen (path_element->path, "w");
347 if (file != NULL)
349 filelen = edit_write_stream (edit, file);
350 fclose (file);
352 else
354 char *msg;
356 msg = g_strdup_printf (_("Cannot open file for writing: %s"), path_element->path);
357 edit_error_dialog (_("Error"), msg);
358 g_free (msg);
359 goto error_save;
363 if (filelen != edit->last_byte)
364 goto error_save;
366 if (this_save_mode == EDIT_DO_BACKUP)
368 char *tmp_store_filename;
369 vfs_path_element_t *last_vpath_element;
370 vfs_path_t *tmp_vpath;
371 gboolean ok;
373 #ifdef HAVE_ASSERT_H
374 assert (option_backup_ext != NULL);
375 #endif
376 /* add backup extention to the path */
377 tmp_vpath = vfs_path_clone (real_filename_vpath);
378 last_vpath_element = (vfs_path_element_t *) vfs_path_get_by_index (tmp_vpath, -1);
379 tmp_store_filename = last_vpath_element->path;
380 last_vpath_element->path = g_strdup_printf ("%s%s", tmp_store_filename, option_backup_ext);
381 g_free (tmp_store_filename);
383 ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
384 vfs_path_free (tmp_vpath);
385 if (!ok)
386 goto error_save;
389 if (this_save_mode != EDIT_QUICK_SAVE)
390 if (mc_rename (savename_vpath, real_filename_vpath) == -1)
391 goto error_save;
393 vfs_path_free (real_filename_vpath);
394 vfs_path_free (savename_vpath);
395 return 1;
396 error_save:
397 /* FIXME: Is this safe ?
398 * if (this_save_mode != EDIT_QUICK_SAVE)
399 * mc_unlink (savename);
401 vfs_path_free (real_filename_vpath);
402 vfs_path_free (savename_vpath);
403 return 0;
406 /* --------------------------------------------------------------------------------------------- */
408 static gboolean
409 edit_check_newline (WEdit * edit)
411 return !(option_check_nl_at_eof && edit->last_byte > 0
412 && edit_get_byte (edit, edit->last_byte - 1) != '\n'
413 && edit_query_dialog2 (_("Warning"),
414 _("The file you are saving is not finished with a newline"),
415 _("C&ontinue"), _("&Cancel")));
418 /* --------------------------------------------------------------------------------------------- */
420 static vfs_path_t *
421 edit_get_save_file_as (WEdit * edit)
423 #define DLG_WIDTH 64
424 #define DLG_HEIGHT 14
426 static LineBreaks cur_lb = LB_ASIS;
428 char *filename = vfs_path_to_str (edit->filename_vpath);
429 char *filename_res;
431 const char *lb_names[LB_NAMES] = {
432 N_("&Do not change"),
433 N_("&Unix format (LF)"),
434 N_("&Windows/DOS format (CR LF)"),
435 N_("&Macintosh format (CR)")
438 QuickWidget quick_widgets[] = {
439 QUICK_BUTTON (6, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
440 QUICK_BUTTON (2, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
441 QUICK_RADIO (5, DLG_WIDTH, DLG_HEIGHT - 8, DLG_HEIGHT, LB_NAMES, lb_names, (int *) &cur_lb),
442 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 9, DLG_HEIGHT, N_("Change line breaks to:")),
443 QUICK_INPUT (3, DLG_WIDTH, DLG_HEIGHT - 11, DLG_HEIGHT, filename, DLG_WIDTH - 6, 0,
444 "save-as", &filename_res),
445 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 12, DLG_HEIGHT, N_("Enter file name:")),
446 QUICK_END
449 QuickDialog Quick_options = {
450 DLG_WIDTH, DLG_HEIGHT, -1, -1,
451 N_("Save As"), "[Save File As]",
452 quick_widgets, NULL, NULL, FALSE
455 if (quick_dialog (&Quick_options) != B_CANCEL)
457 char *fname;
458 vfs_path_t *ret_vpath;
460 edit->lb = cur_lb;
461 fname = tilde_expand (filename_res);
462 g_free (filename_res);
463 ret_vpath = vfs_path_from_str (fname);
464 g_free (fname);
465 return ret_vpath;
467 g_free (filename);
469 return NULL;
471 #undef DLG_WIDTH
472 #undef DLG_HEIGHT
475 /* --------------------------------------------------------------------------------------------- */
477 /** returns TRUE on success */
479 static gboolean
480 edit_save_cmd (WEdit * edit)
482 int res, save_lock = 0;
484 if (!edit->locked && !edit->delete_file)
485 save_lock = lock_file (edit->filename_vpath);
486 res = edit_save_file (edit, edit->filename_vpath);
488 /* Maintain modify (not save) lock on failure */
489 if ((res > 0 && edit->locked) || save_lock)
490 edit->locked = unlock_file (edit->filename_vpath);
492 /* On failure try 'save as', it does locking on its own */
493 if (res == 0)
494 return edit_save_as_cmd (edit);
495 edit->force |= REDRAW_COMPLETELY;
496 if (res > 0)
498 edit->delete_file = 0;
499 edit->modified = 0;
502 return TRUE;
505 /* --------------------------------------------------------------------------------------------- */
507 * Load file content
509 * @param h screen the owner of editor window
510 * @param vpath vfs file path
511 * @return TRUE if file content was successfully loaded, FALSE otherwise
514 static inline gboolean
515 edit_load_file_from_filename (Dlg_head * h, const vfs_path_t * vpath)
517 return edit_add_window (h, h->y + 1, h->x, h->lines - 2, h->cols, vpath, 0);
520 /* --------------------------------------------------------------------------------------------- */
522 static void
523 edit_delete_column_of_text (WEdit * edit)
525 off_t p, q, r;
526 off_t m1, m2;
527 off_t n;
528 long b, c, d;
530 eval_marks (edit, &m1, &m2);
531 n = edit_move_forward (edit, m1, 0, m2) + 1;
532 c = (long) edit_move_forward3 (edit, edit_bol (edit, m1), 0, m1);
533 d = (long) edit_move_forward3 (edit, edit_bol (edit, m2), 0, m2);
534 b = max (min (c, d), min (edit->column1, edit->column2));
535 c = max (c, max (edit->column1, edit->column2));
537 while (n--)
539 r = edit_bol (edit, edit->curs1);
540 p = edit_move_forward3 (edit, r, b, 0);
541 q = edit_move_forward3 (edit, r, c, 0);
542 if (p < m1)
543 p = m1;
544 if (q > m2)
545 q = m2;
546 edit_cursor_move (edit, p - edit->curs1);
547 while (q > p)
549 /* delete line between margins */
550 if (edit_get_byte (edit, edit->curs1) != '\n')
551 edit_delete (edit, TRUE);
552 q--;
554 if (n)
555 /* move to next line except on the last delete */
556 edit_cursor_move (edit, edit_move_forward (edit, edit->curs1, 1, 0) - edit->curs1);
560 /* --------------------------------------------------------------------------------------------- */
561 /** if success return 0 */
563 static int
564 edit_block_delete (WEdit * edit)
566 off_t start_mark, end_mark;
567 off_t curs_pos;
568 long curs_line, c1, c2;
570 if (eval_marks (edit, &start_mark, &end_mark))
571 return 0;
572 if (edit->column_highlight && edit->mark2 < 0)
573 edit_mark_cmd (edit, FALSE);
574 if ((end_mark - start_mark) > option_max_undo / 2)
576 /* Warning message with a query to continue or cancel the operation */
577 if (edit_query_dialog2
578 (_("Warning"),
580 ("Block is large, you may not be able to undo this action"),
581 _("C&ontinue"), _("&Cancel")))
583 return 1;
586 c1 = min (edit->column1, edit->column2);
587 c2 = max (edit->column1, edit->column2);
588 edit->column1 = c1;
589 edit->column2 = c2;
591 edit_push_markers (edit);
593 curs_line = edit->curs_line;
595 curs_pos = edit->curs_col + edit->over_col;
597 /* move cursor to start of selection */
598 edit_cursor_move (edit, start_mark - edit->curs1);
599 edit_scroll_screen_over_cursor (edit);
601 if (start_mark < end_mark)
603 if (edit->column_highlight)
605 off_t line_width;
607 if (edit->mark2 < 0)
608 edit_mark_cmd (edit, FALSE);
609 edit_delete_column_of_text (edit);
610 /* move cursor to the saved position */
611 edit_move_to_line (edit, curs_line);
612 /* calculate line width and cursor position before cut */
613 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
614 edit_eol (edit, edit->curs1));
615 if (option_cursor_beyond_eol && curs_pos > line_width)
616 edit->over_col = curs_pos - line_width;
618 else
620 off_t count;
622 for (count = start_mark; count < end_mark; count++)
623 edit_delete (edit, TRUE);
626 edit_set_markers (edit, 0, 0, 0, 0);
627 edit->force |= REDRAW_PAGE;
628 return 0;
631 /* --------------------------------------------------------------------------------------------- */
633 * Get EOL symbol for searching.
635 * @param edit editor object
636 * @return EOL symbol
639 static inline char
640 edit_search_get_current_end_line_char (const WEdit * edit)
642 switch (edit->lb)
644 case LB_MAC:
645 return '\r';
646 default:
647 return '\n';
651 /* --------------------------------------------------------------------------------------------- */
653 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
655 * @param search search object
656 * @return result of checks.
659 static edit_search_line_t
660 edit_get_search_line_type (mc_search_t * search)
662 edit_search_line_t search_line_type = 0;
664 if (search->search_type != MC_SEARCH_T_REGEX)
665 return search_line_type;
667 if (*search->original == '^')
668 search_line_type |= AT_START_LINE;
670 if (search->original[search->original_len - 1] == '$')
671 search_line_type |= AT_END_LINE;
672 return search_line_type;
675 /* --------------------------------------------------------------------------------------------- */
677 * Calculating the start position of next line.
679 * @param edit editor object
680 * @param current_pos current position
681 * @param max_pos max position
682 * @param end_string_symbol end of line symbol
683 * @return start position of next line
686 static off_t
687 edit_calculate_start_of_next_line (WEdit * edit, off_t current_pos, off_t max_pos,
688 char end_string_symbol)
690 off_t i;
692 for (i = current_pos; i < max_pos; i++)
694 current_pos++;
695 if (edit_get_byte (edit, i) == end_string_symbol)
696 break;
699 return current_pos;
702 /* --------------------------------------------------------------------------------------------- */
704 * Calculating the end position of previous line.
706 * @param edit editor object
707 * @param current_pos current position
708 * @param end_string_symbol end of line symbol
709 * @return end position of previous line
712 static off_t
713 edit_calculate_end_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
715 off_t i;
717 for (i = current_pos - 1; i >= 0; i--)
718 if (edit_get_byte (edit, i) == end_string_symbol)
719 break;
721 return i;
724 /* --------------------------------------------------------------------------------------------- */
726 * Calculating the start position of previous line.
728 * @param edit editor object
729 * @param current_pos current position
730 * @param end_string_symbol end of line symbol
731 * @return start position of previous line
734 static inline off_t
735 edit_calculate_start_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
737 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
738 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
740 return (current_pos + 1);
743 /* --------------------------------------------------------------------------------------------- */
745 * Calculating the start position of current line.
747 * @param edit editor object
748 * @param current_pos current position
749 * @param end_string_symbol end of line symbol
750 * @return start position of current line
753 static inline off_t
754 edit_calculate_start_of_current_line (WEdit * edit, off_t current_pos, char end_string_symbol)
756 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
758 return (current_pos + 1);
761 /* --------------------------------------------------------------------------------------------- */
763 * Fixing (if needed) search start position if 'only in selection' option present.
765 * @param edit editor object
768 static void
769 edit_search_fix_search_start_if_selection (WEdit * edit)
771 off_t start_mark = 0;
772 off_t end_mark = 0;
774 if (!edit_search_options.only_in_selection)
775 return;
777 if (eval_marks (edit, &start_mark, &end_mark) != 0)
778 return;
780 if (edit_search_options.backwards)
782 if (edit->search_start > end_mark || edit->search_start <= start_mark)
783 edit->search_start = end_mark;
785 else
787 if (edit->search_start < start_mark || edit->search_start >= end_mark)
788 edit->search_start = start_mark;
792 /* --------------------------------------------------------------------------------------------- */
794 static gboolean
795 editcmd_find (WEdit * edit, gsize * len)
797 off_t search_start = edit->search_start;
798 off_t search_end;
799 off_t start_mark = 0;
800 off_t end_mark = edit->last_byte;
801 int mark_res = 0;
802 char end_string_symbol;
804 end_string_symbol = edit_search_get_current_end_line_char (edit);
806 /* prepare for search */
807 if (edit_search_options.only_in_selection)
809 mark_res = eval_marks (edit, &start_mark, &end_mark);
810 if (mark_res != 0)
812 edit->search->error = MC_SEARCH_E_NOTFOUND;
813 edit->search->error_str = g_strdup (_("Search string not found"));
814 return FALSE;
817 /* fix the start and the end of search block positions */
818 if ((edit->search_line_type & AT_START_LINE) != 0
819 && (start_mark != 0 || edit_get_byte (edit, start_mark - 1) != end_string_symbol))
821 start_mark =
822 edit_calculate_start_of_next_line (edit, start_mark, edit->last_byte,
823 end_string_symbol);
825 if ((edit->search_line_type & AT_END_LINE) != 0
826 && (end_mark - 1 != edit->last_byte
827 || edit_get_byte (edit, end_mark) != end_string_symbol))
829 end_mark = edit_calculate_end_of_previous_line (edit, end_mark, end_string_symbol);
831 if (start_mark >= end_mark)
833 edit->search->error = MC_SEARCH_E_NOTFOUND;
834 edit->search->error_str = g_strdup (_("Search string not found"));
835 return FALSE;
838 else
840 if (edit_search_options.backwards)
841 end_mark = max (1, edit->curs1) - 1;
844 /* search */
845 if (edit_search_options.backwards)
847 /* backward search */
848 search_end = end_mark;
850 if ((edit->search_line_type & AT_START_LINE) != 0)
851 search_start =
852 edit_calculate_start_of_current_line (edit, search_start, end_string_symbol);
854 while ((int) search_start >= start_mark)
856 if (search_end > (off_t) (search_start + edit->search->original_len)
857 && mc_search_is_fixed_search_str (edit->search))
859 search_end = search_start + edit->search->original_len;
861 if (mc_search_run (edit->search, (void *) edit, search_start, search_end, len)
862 && edit->search->normal_offset == search_start)
864 return TRUE;
868 if ((edit->search_line_type & AT_START_LINE) != 0)
869 search_start =
870 edit_calculate_start_of_previous_line (edit, search_start, end_string_symbol);
871 else
872 search_start--;
874 edit->search->error_str = g_strdup (_("Search string not found"));
876 else
878 /* forward search */
879 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
880 search_start =
881 edit_calculate_start_of_next_line (edit, search_start, end_mark, end_string_symbol);
882 return mc_search_run (edit->search, (void *) edit, search_start, end_mark, len);
884 return FALSE;
887 /* --------------------------------------------------------------------------------------------- */
889 static char *
890 edit_replace_cmd__conv_to_display (char *str)
892 #ifdef HAVE_CHARSET
893 GString *tmp;
895 tmp = str_convert_to_display (str);
896 if (tmp != NULL)
898 if (tmp->len != 0)
899 return g_string_free (tmp, FALSE);
900 g_string_free (tmp, TRUE);
902 #endif
903 return g_strdup (str);
906 /* --------------------------------------------------------------------------------------------- */
908 static char *
909 edit_replace_cmd__conv_to_input (char *str)
911 #ifdef HAVE_CHARSET
912 GString *tmp;
914 tmp = str_convert_to_input (str);
915 if (tmp != NULL)
917 if (tmp->len != 0)
918 return g_string_free (tmp, FALSE);
919 g_string_free (tmp, TRUE);
921 #endif
922 return g_strdup (str);
925 /* --------------------------------------------------------------------------------------------- */
927 static void
928 edit_do_search (WEdit * edit)
930 gsize len = 0;
932 if (edit->search == NULL)
933 edit->search_start = edit->curs1;
935 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
937 if (search_create_bookmark)
939 int found = 0, books = 0;
940 long l = 0, l_last = -1;
941 long q = 0;
943 search_create_bookmark = FALSE;
944 book_mark_flush (edit, -1);
946 while (mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len))
948 if (found == 0)
949 edit->search_start = edit->search->normal_offset;
950 found++;
951 l += edit_count_lines (edit, q, edit->search->normal_offset);
952 if (l != l_last)
954 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
955 books++;
957 l_last = l;
958 q = edit->search->normal_offset + 1;
961 if (found == 0)
962 edit_error_dialog (_("Search"), _("Search string not found"));
963 else
964 edit_cursor_move (edit, edit->search_start - edit->curs1);
966 else
968 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
969 && edit_search_options.backwards)
970 edit->search_start--;
972 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
973 && !edit_search_options.backwards)
974 edit->search_start++;
976 if (editcmd_find (edit, &len))
978 edit->found_start = edit->search_start = edit->search->normal_offset;
979 edit->found_len = len;
980 edit->over_col = 0;
981 edit_cursor_move (edit, edit->search_start - edit->curs1);
982 edit_scroll_screen_over_cursor (edit);
983 if (edit_search_options.backwards)
984 edit->search_start--;
985 else
986 edit->search_start++;
988 else
990 edit->search_start = edit->curs1;
991 if (edit->search->error_str != NULL)
992 edit_error_dialog (_("Search"), edit->search->error_str);
996 edit->force |= REDRAW_COMPLETELY;
997 edit_scroll_screen_over_cursor (edit);
1000 /* --------------------------------------------------------------------------------------------- */
1002 static void
1003 edit_search (WEdit * edit)
1005 if (editcmd_dialog_search_show (edit))
1007 edit->search_line_type = edit_get_search_line_type (edit->search);
1008 edit_search_fix_search_start_if_selection (edit);
1009 edit_do_search (edit);
1013 /* --------------------------------------------------------------------------------------------- */
1014 /** Return a null terminated length of text. Result must be g_free'd */
1016 static unsigned char *
1017 edit_get_block (WEdit * edit, off_t start, off_t finish, off_t * l)
1019 unsigned char *s, *r;
1021 r = s = g_malloc0 (finish - start + 1);
1022 if (edit->column_highlight)
1024 *l = 0;
1025 /* copy from buffer, excluding chars that are out of the column 'margins' */
1026 while (start < finish)
1028 int c;
1029 off_t x;
1031 x = edit_move_forward3 (edit, edit_bol (edit, start), 0, start);
1032 c = edit_get_byte (edit, start);
1033 if ((x >= edit->column1 && x < edit->column2)
1034 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1036 *s++ = c;
1037 (*l)++;
1039 start++;
1042 else
1044 *l = finish - start;
1045 while (start < finish)
1046 *s++ = edit_get_byte (edit, start++);
1048 *s = '\0';
1049 return r;
1052 /* --------------------------------------------------------------------------------------------- */
1053 /** copies a block to clipboard file */
1055 static gboolean
1056 edit_save_block_to_clip_file (WEdit * edit, off_t start, off_t finish)
1058 gboolean ret;
1059 gchar *tmp;
1061 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1062 ret = edit_save_block (edit, tmp, start, finish);
1063 g_free (tmp);
1064 return ret;
1067 /* --------------------------------------------------------------------------------------------- */
1069 static void
1070 pipe_mail (WEdit * edit, char *to, char *subject, char *cc)
1072 FILE *p = 0;
1073 char *s;
1075 to = name_quote (to, 0);
1076 subject = name_quote (subject, 0);
1077 cc = name_quote (cc, 0);
1078 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1079 g_free (to);
1080 g_free (subject);
1081 g_free (cc);
1083 if (s)
1085 p = popen (s, "w");
1086 g_free (s);
1089 if (p)
1091 off_t i;
1092 for (i = 0; i < edit->last_byte; i++)
1093 fputc (edit_get_byte (edit, i), p);
1094 pclose (p);
1098 /* --------------------------------------------------------------------------------------------- */
1099 /** find first character of current word */
1101 static gboolean
1102 edit_find_word_start (WEdit * edit, off_t * word_start, gsize * word_len)
1104 int c, last;
1105 gsize i;
1107 /* return if at begin of file */
1108 if (edit->curs1 <= 0)
1109 return FALSE;
1111 c = edit_get_byte (edit, edit->curs1 - 1);
1112 /* return if not at end or in word */
1113 if (is_break_char (c))
1114 return FALSE;
1116 /* search start of word to be completed */
1117 for (i = 2;; i++)
1119 /* return if at begin of file */
1120 if ((gsize) edit->curs1 < i)
1121 return FALSE;
1123 last = c;
1124 c = edit_get_byte (edit, edit->curs1 - i);
1126 if (is_break_char (c))
1128 /* return if word starts with digit */
1129 if (isdigit (last))
1130 return FALSE;
1132 *word_start = edit->curs1 - (i - 1); /* start found */
1133 *word_len = i - 1;
1134 break;
1137 /* success */
1138 return TRUE;
1141 /* --------------------------------------------------------------------------------------------- */
1143 * Get current word under cursor
1145 * @param edit editor object
1146 * @param srch mc_search object
1147 * @param word_start start word position
1149 * @return newly allocated string or NULL if no any words under cursor
1152 static char *
1153 edit_collect_completions_get_current_word (WEdit * edit, mc_search_t * srch, off_t word_start)
1155 gsize len = 0;
1156 off_t i;
1157 GString *temp;
1159 if (!mc_search_run (srch, (void *) edit, word_start, edit->last_byte, &len))
1160 return NULL;
1162 temp = g_string_sized_new (len);
1164 for (i = 0; i < (off_t) len; i++)
1166 int chr;
1168 chr = edit_get_byte (edit, word_start + i);
1169 if (!isspace (chr))
1170 g_string_append_c (temp, chr);
1173 return g_string_free (temp, temp->len == 0);
1176 /* --------------------------------------------------------------------------------------------- */
1177 /** collect the possible completions */
1179 static gsize
1180 edit_collect_completions (WEdit * edit, off_t word_start, gsize word_len,
1181 char *match_expr, struct selection *compl, gsize * num)
1183 gsize len = 0;
1184 gsize max_len = 0;
1185 gsize i;
1186 int skip;
1187 GString *temp;
1188 mc_search_t *srch;
1189 off_t last_byte, start = -1;
1190 char *current_word;
1192 srch = mc_search_new (match_expr, -1);
1193 if (srch == NULL)
1194 return 0;
1196 if (mc_config_get_bool
1197 (mc_main_config, CONFIG_APP_SECTION, "editor_wordcompletion_collect_entire_file", 0))
1199 last_byte = edit->last_byte;
1201 else
1203 last_byte = word_start;
1206 srch->search_type = MC_SEARCH_T_REGEX;
1207 srch->is_case_sensitive = TRUE;
1208 srch->search_fn = edit_search_cmd_callback;
1210 current_word = edit_collect_completions_get_current_word (edit, srch, word_start);
1212 temp = g_string_new ("");
1214 /* collect max MAX_WORD_COMPLETIONS completions */
1215 while (mc_search_run (srch, (void *) edit, start + 1, last_byte, &len))
1217 g_string_set_size (temp, 0);
1218 start = srch->normal_offset;
1220 /* add matched completion if not yet added */
1221 for (i = 0; i < len; i++)
1223 skip = edit_get_byte (edit, start + i);
1224 if (isspace (skip))
1225 continue;
1227 /* skip current word */
1228 if (start + (off_t) i == word_start)
1229 break;
1231 g_string_append_c (temp, skip);
1234 if (temp->len == 0)
1235 continue;
1237 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1238 continue;
1240 skip = 0;
1242 for (i = 0; i < *num; i++)
1244 if (strncmp
1245 ((char *) &compl[i].text[word_len],
1246 (char *) &temp->str[word_len], max (len, compl[i].len) - word_len) == 0)
1248 struct selection this = compl[i];
1249 for (++i; i < *num; i++)
1251 compl[i - 1] = compl[i];
1253 compl[*num - 1] = this;
1254 skip = 1;
1255 break; /* skip it, already added */
1258 if (skip != 0)
1259 continue;
1261 if (*num == MAX_WORD_COMPLETIONS)
1263 g_free (compl[0].text);
1264 for (i = 1; i < *num; i++)
1266 compl[i - 1] = compl[i];
1268 (*num)--;
1270 #ifdef HAVE_CHARSET
1272 GString *recoded;
1273 recoded = str_convert_to_display (temp->str);
1275 if (recoded && recoded->len)
1276 g_string_assign (temp, recoded->str);
1278 g_string_free (recoded, TRUE);
1280 #endif
1281 compl[*num].text = g_strndup (temp->str, temp->len);
1282 compl[*num].len = temp->len;
1283 (*num)++;
1284 start += len;
1286 /* note the maximal length needed for the completion dialog */
1287 if (len > max_len)
1288 max_len = len;
1291 mc_search_free (srch);
1292 g_string_free (temp, TRUE);
1293 g_free (current_word);
1295 return max_len;
1298 /* --------------------------------------------------------------------------------------------- */
1300 static void
1301 edit_insert_column_of_text (WEdit * edit, unsigned char *data, off_t size, long width,
1302 off_t * start_pos, off_t * end_pos, long *col1, long *col2)
1304 off_t i, cursor;
1305 long col;
1307 cursor = edit->curs1;
1308 col = edit_get_col (edit);
1310 for (i = 0; i < size; i++)
1312 if (data[i] != '\n')
1313 edit_insert (edit, data[i]);
1314 else
1315 { /* fill in and move to next line */
1316 long l;
1317 off_t p;
1319 if (edit_get_byte (edit, edit->curs1) != '\n')
1321 for (l = width - (edit_get_col (edit) - col); l > 0; l -= space_width)
1322 edit_insert (edit, ' ');
1324 for (p = edit->curs1;; p++)
1326 if (p == edit->last_byte)
1328 edit_cursor_move (edit, edit->last_byte - edit->curs1);
1329 edit_insert_ahead (edit, '\n');
1330 p++;
1331 break;
1333 if (edit_get_byte (edit, p) == '\n')
1335 p++;
1336 break;
1339 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
1341 for (l = col - edit_get_col (edit); l >= space_width; l -= space_width)
1342 edit_insert (edit, ' ');
1346 *col1 = col;
1347 *col2 = col + width;
1348 *start_pos = cursor;
1349 *end_pos = edit->curs1;
1350 edit_cursor_move (edit, cursor - edit->curs1);
1353 /* --------------------------------------------------------------------------------------------- */
1355 static int
1356 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1358 const macros_t *m1 = (const macros_t *) macro1;
1359 const macros_t *m2 = (const macros_t *) macro2;
1361 return m1->hotkey - m2->hotkey;
1364 /* --------------------------------------------------------------------------------------------- */
1366 static void
1367 edit_macro_sort_by_hotkey (void)
1369 if (macros_list != NULL && macros_list->len != 0)
1370 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1373 /* --------------------------------------------------------------------------------------------- */
1375 static gboolean
1376 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1378 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1379 macros_t *result;
1380 macros_t search_macro;
1382 (void) edit;
1384 search_macro.hotkey = hotkey;
1385 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1386 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1388 if (result != NULL && result->macro != NULL)
1390 *indx = (result - array_start);
1391 *macros = result;
1392 return TRUE;
1394 *indx = 0;
1395 return FALSE;
1398 /* --------------------------------------------------------------------------------------------- */
1399 /** returns FALSE on error */
1401 static gboolean
1402 edit_delete_macro (WEdit * edit, int hotkey)
1404 mc_config_t *macros_config = NULL;
1405 const char *section_name = "editor";
1406 gchar *macros_fname;
1407 guint indx;
1408 char *skeyname;
1409 const macros_t *macros = NULL;
1411 /* clear array of actions for current hotkey */
1412 while (edit_get_macro (edit, hotkey, &macros, &indx))
1414 if (macros->macro != NULL)
1415 g_array_free (macros->macro, TRUE);
1416 macros = NULL;
1417 g_array_remove_index (macros_list, indx);
1418 edit_macro_sort_by_hotkey ();
1421 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1422 macros_config = mc_config_init (macros_fname, FALSE);
1423 g_free (macros_fname);
1425 if (macros_config == NULL)
1426 return FALSE;
1428 skeyname = lookup_key_by_code (hotkey);
1429 while (mc_config_del_key (macros_config, section_name, skeyname))
1431 g_free (skeyname);
1432 mc_config_save_file (macros_config, NULL);
1433 mc_config_deinit (macros_config);
1434 return TRUE;
1437 /* --------------------------------------------------------------------------------------------- */
1439 * Callback for the iteration of objects in the 'editors' array.
1440 * Toggle syntax highlighting in editor object.
1442 * @param data probably WEdit object
1443 * @param user_data unused
1446 static void
1447 edit_syntax_onoff_cb (void *data, void *user_data)
1449 (void) user_data;
1451 if (edit_widget_is_editor ((const Widget *) data))
1453 WEdit *edit = (WEdit *) data;
1455 if (option_syntax_highlighting)
1456 edit_load_syntax (edit, NULL, edit->syntax_type);
1457 edit->force |= REDRAW_PAGE;
1461 /* --------------------------------------------------------------------------------------------- */
1463 * Callback for the iteration of objects in the 'editors' array.
1464 * Redraw editor object.
1466 * @param data probably WEdit object
1467 * @param user_data unused
1470 static void
1471 edit_redraw_page_cb (void *data, void *user_data)
1473 (void) user_data;
1475 if (edit_widget_is_editor ((const Widget *) data))
1476 ((WEdit *) data)->force |= REDRAW_PAGE;
1479 /* --------------------------------------------------------------------------------------------- */
1480 /*** public functions ****************************************************************************/
1481 /* --------------------------------------------------------------------------------------------- */
1483 void
1484 edit_refresh_cmd (void)
1486 clr_scr ();
1487 repaint_screen ();
1488 tty_keypad (TRUE);
1491 /* --------------------------------------------------------------------------------------------- */
1493 * Toggle syntax highlighting in all editor windows.
1495 * @param h root widget for all windows
1498 void
1499 edit_syntax_onoff_cmd (Dlg_head * h)
1501 option_syntax_highlighting = !option_syntax_highlighting;
1502 g_list_foreach (h->widgets, edit_syntax_onoff_cb, NULL);
1503 dlg_redraw (h);
1506 /* --------------------------------------------------------------------------------------------- */
1508 * Toggle tabs showing in all editor windows.
1510 * @param h root widget for all windows
1513 void
1514 edit_show_tabs_tws_cmd (Dlg_head * h)
1516 enable_show_tabs_tws = !enable_show_tabs_tws;
1517 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1518 dlg_redraw (h);
1521 /* --------------------------------------------------------------------------------------------- */
1523 * Toggle right margin showing in all editor windows.
1525 * @param h root widget for all windows
1528 void
1529 edit_show_margin_cmd (Dlg_head * h)
1531 show_right_margin = !show_right_margin;
1532 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1533 dlg_redraw (h);
1536 /* --------------------------------------------------------------------------------------------- */
1538 * Toggle line numbers showing in all editor windows.
1540 * @param h root widget for all windows
1543 void
1544 edit_show_numbers_cmd (Dlg_head * h)
1546 option_line_state = !option_line_state;
1547 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
1548 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1549 dlg_redraw (h);
1552 /* --------------------------------------------------------------------------------------------- */
1554 void
1555 edit_save_mode_cmd (void)
1557 /* diaog sizes */
1558 const int DLG_X = 38;
1559 const int DLG_Y = 13;
1561 char *str_result;
1563 const char *str[] = {
1564 N_("&Quick save"),
1565 N_("&Safe save"),
1566 N_("&Do backups with following extension:")
1569 QuickWidget widgets[] = {
1570 /* 0 */
1571 QUICK_BUTTON (18, DLG_X, DLG_Y - 3, DLG_Y, N_("&Cancel"), B_CANCEL, NULL),
1572 /* 1 */
1573 QUICK_BUTTON (6, DLG_X, DLG_Y - 3, DLG_Y, N_("&OK"), B_ENTER, NULL),
1574 /* 2 */
1575 QUICK_CHECKBOX (4, DLG_X, 8, DLG_Y, N_("Check &POSIX new line"), &option_check_nl_at_eof),
1576 /* 3 */
1577 QUICK_INPUT (8, DLG_X, 6, DLG_Y, option_backup_ext, 9, 0, "edit-backup-ext", &str_result),
1578 /* 4 */
1579 QUICK_RADIO (4, DLG_X, 3, DLG_Y, 3, str, &option_save_mode),
1580 QUICK_END
1583 QuickDialog dialog = {
1584 DLG_X, DLG_Y, -1, -1, N_("Edit Save Mode"),
1585 "[Edit Save Mode]", widgets, NULL, NULL, FALSE
1588 size_t i;
1589 size_t maxlen = 0;
1590 size_t w0, w1, b_len, w3;
1592 #ifdef HAVE_ASSERT_H
1593 assert (option_backup_ext != NULL);
1594 #endif
1596 /* OK/Cancel buttons */
1597 w0 = str_term_width1 (_(widgets[0].u.button.text)) + 3;
1598 w1 = str_term_width1 (_(widgets[1].u.button.text)) + 5; /* default button */
1599 b_len = w0 + w1 + 3;
1601 maxlen = max (b_len, (size_t) str_term_width1 (_(dialog.title)) + 2);
1603 w3 = 0;
1604 for (i = 0; i < 3; i++)
1606 #ifdef ENABLE_NLS
1607 str[i] = _(str[i]);
1608 #endif
1609 w3 = max (w3, (size_t) str_term_width1 (str[i]));
1612 maxlen = max (maxlen, w3 + 4);
1614 dialog.xlen = min ((size_t) COLS, maxlen + 8);
1616 widgets[3].u.input.len = w3;
1617 widgets[1].relative_x = (dialog.xlen - b_len) / 2;
1618 widgets[0].relative_x = widgets[1].relative_x + w0 + 2;
1620 for (i = 0; i < sizeof (widgets) / sizeof (widgets[0]); i++)
1621 widgets[i].x_divisions = dialog.xlen;
1623 if (quick_dialog (&dialog) != B_CANCEL)
1625 g_free (option_backup_ext);
1626 option_backup_ext = str_result;
1630 /* --------------------------------------------------------------------------------------------- */
1632 void
1633 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1635 vfs_path_free (edit->filename_vpath);
1636 edit->filename_vpath = vfs_path_clone (name_vpath);
1638 if (edit->dir_vpath == NULL)
1639 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1642 /* --------------------------------------------------------------------------------------------- */
1643 /* Here we want to warn the users of overwriting an existing file,
1644 but only if they have made a change to the filename */
1645 /* returns TRUE on success */
1646 gboolean
1647 edit_save_as_cmd (WEdit * edit)
1649 /* This heads the 'Save As' dialog box */
1650 vfs_path_t *exp_vpath;
1651 int save_lock = 0;
1652 int different_filename = 0;
1654 if (!edit_check_newline (edit))
1655 return FALSE;
1657 exp_vpath = edit_get_save_file_as (edit);
1658 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1660 if (exp_vpath != NULL)
1662 if (vfs_path_len (exp_vpath) == 0)
1663 goto ret;
1664 else
1666 int rv;
1668 if (vfs_path_cmp (edit->filename_vpath, exp_vpath) != 0)
1670 int file;
1671 struct stat sb;
1673 if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1675 edit_error_dialog (_("Save as"),
1676 get_sys_error (_
1677 ("Cannot save: destination is not a regular file")));
1678 goto ret;
1681 different_filename = 1;
1682 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1684 if (file != -1)
1686 /* the file exists */
1687 mc_close (file);
1688 /* Overwrite the current file or cancel the operation */
1689 if (edit_query_dialog2
1690 (_("Warning"),
1691 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1692 goto ret;
1694 else
1696 edit->stat1.st_mode |= S_IWUSR;
1698 save_lock = lock_file (exp_vpath);
1700 else
1702 /* filenames equal, check if already locked */
1703 if (!edit->locked && !edit->delete_file)
1704 save_lock = lock_file (exp_vpath);
1707 if (different_filename)
1710 * Allow user to write into saved (under another name) file
1711 * even if original file had r/o user permissions.
1713 edit->stat1.st_mode |= S_IWRITE;
1716 rv = edit_save_file (edit, exp_vpath);
1717 switch (rv)
1719 case 1:
1720 /* Succesful, so unlock both files */
1721 if (different_filename)
1723 if (save_lock)
1724 unlock_file (exp_vpath);
1725 if (edit->locked)
1726 edit->locked = unlock_file (edit->filename_vpath);
1728 else
1730 if (edit->locked || save_lock)
1731 edit->locked = unlock_file (edit->filename_vpath);
1734 edit_set_filename (edit, exp_vpath);
1735 if (edit->lb != LB_ASIS)
1736 edit_reload (edit, exp_vpath);
1737 edit->modified = 0;
1738 edit->delete_file = 0;
1739 if (different_filename)
1740 edit_load_syntax (edit, NULL, edit->syntax_type);
1741 vfs_path_free (exp_vpath);
1742 edit->force |= REDRAW_COMPLETELY;
1743 return TRUE;
1744 default:
1745 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1746 /* fallthrough */
1747 case -1:
1748 /* Failed, so maintain modify (not save) lock */
1749 if (save_lock)
1750 unlock_file (exp_vpath);
1751 break;
1756 ret:
1757 vfs_path_free (exp_vpath);
1758 edit->force |= REDRAW_COMPLETELY;
1759 return FALSE;
1762 /* {{{ Macro stuff starts here */
1763 /* --------------------------------------------------------------------------------------------- */
1765 void
1766 edit_delete_macro_cmd (WEdit * edit)
1768 int hotkey;
1770 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE);
1772 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1773 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1776 /* --------------------------------------------------------------------------------------------- */
1778 /** returns FALSE on error */
1779 gboolean
1780 edit_execute_macro (WEdit * edit, int hotkey)
1782 gboolean res = FALSE;
1784 if (hotkey != 0)
1786 const macros_t *macros;
1787 guint indx;
1789 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1790 macros->macro != NULL && macros->macro->len != 0)
1792 guint i;
1794 edit->force |= REDRAW_PAGE;
1796 for (i = 0; i < macros->macro->len; i++)
1798 const macro_action_t *m_act;
1800 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1801 edit_execute_cmd (edit, m_act->action, m_act->ch);
1802 res = TRUE;
1807 return res;
1810 /* --------------------------------------------------------------------------------------------- */
1812 /** returns FALSE on error */
1813 gboolean
1814 edit_store_macro_cmd (WEdit * edit)
1816 int i;
1817 int hotkey;
1818 GString *marcros_string;
1819 mc_config_t *macros_config = NULL;
1820 const char *section_name = "editor";
1821 gchar *macros_fname;
1822 GArray *macros; /* current macro */
1823 int tmp_act;
1824 gboolean have_macro = FALSE;
1825 char *skeyname = NULL;
1827 hotkey =
1828 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE);
1829 if (hotkey == ESC_CHAR)
1830 return FALSE;
1832 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1834 /* return FALSE if try assign macro into restricted hotkeys */
1835 if (tmp_act == CK_MacroStartRecord
1836 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1837 return FALSE;
1839 edit_delete_macro (edit, hotkey);
1841 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1842 macros_config = mc_config_init (macros_fname, FALSE);
1843 g_free (macros_fname);
1845 if (macros_config == NULL)
1846 return FALSE;
1848 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1850 marcros_string = g_string_sized_new (250);
1851 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1853 skeyname = lookup_key_by_code (hotkey);
1855 for (i = 0; i < macro_index; i++)
1857 macro_action_t m_act;
1858 const char *action_name;
1860 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1862 if (action_name == NULL)
1863 break;
1865 m_act.action = record_macro_buf[i].action;
1866 m_act.ch = record_macro_buf[i].ch;
1867 g_array_append_val (macros, m_act);
1868 have_macro = TRUE;
1869 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1870 (int) record_macro_buf[i].ch);
1872 if (have_macro)
1874 macros_t macro;
1875 macro.hotkey = hotkey;
1876 macro.macro = macros;
1877 g_array_append_val (macros_list, macro);
1878 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1880 else
1881 mc_config_del_key (macros_config, section_name, skeyname);
1883 g_free (skeyname);
1884 edit_macro_sort_by_hotkey ();
1886 g_string_free (marcros_string, TRUE);
1887 mc_config_save_file (macros_config, NULL);
1888 mc_config_deinit (macros_config);
1889 return TRUE;
1892 /* --------------------------------------------------------------------------------------------- */
1894 gboolean
1895 edit_repeat_macro_cmd (WEdit * edit)
1897 int i, j;
1898 char *f;
1899 long count_repeat;
1900 char *error = NULL;
1902 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL);
1903 if (f == NULL || *f == '\0')
1905 g_free (f);
1906 return FALSE;
1909 count_repeat = strtol (f, &error, 0);
1911 if (*error != '\0')
1913 g_free (f);
1914 return FALSE;
1917 g_free (f);
1919 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1920 edit->force |= REDRAW_PAGE;
1922 for (j = 0; j < count_repeat; j++)
1923 for (i = 0; i < macro_index; i++)
1924 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1925 edit_update_screen (edit);
1926 return TRUE;
1929 /* --------------------------------------------------------------------------------------------- */
1930 /** return FALSE on error */
1932 gboolean
1933 edit_load_macro_cmd (WEdit * edit)
1935 mc_config_t *macros_config = NULL;
1936 gchar **profile_keys, **keys;
1937 gchar **values, **curr_values;
1938 gsize len, values_len;
1939 const char *section_name = "editor";
1940 gchar *macros_fname;
1941 int hotkey;
1943 (void) edit;
1945 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1946 macros_config = mc_config_init (macros_fname, TRUE);
1947 g_free (macros_fname);
1949 if (macros_config == NULL)
1950 return FALSE;
1952 profile_keys = keys = mc_config_get_keys (macros_config, section_name, &len);
1953 while (*profile_keys != NULL)
1955 gboolean have_macro;
1956 GArray *macros;
1957 macros_t macro;
1959 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1961 curr_values = values = mc_config_get_string_list (macros_config, section_name,
1962 *profile_keys, &values_len);
1963 hotkey = lookup_key (*profile_keys, NULL);
1964 have_macro = FALSE;
1966 while (*curr_values != NULL && *curr_values[0] != '\0')
1968 char **macro_pair = NULL;
1970 macro_pair = g_strsplit (*curr_values, ":", 2);
1972 if (macro_pair != NULL)
1974 macro_action_t m_act;
1975 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
1976 m_act.action = 0;
1977 else
1979 m_act.action = keybind_lookup_action (macro_pair[0]);
1980 g_free (macro_pair[0]);
1981 macro_pair[0] = NULL;
1983 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
1984 m_act.ch = -1;
1985 else
1987 m_act.ch = strtol (macro_pair[1], NULL, 0);
1988 g_free (macro_pair[1]);
1989 macro_pair[1] = NULL;
1991 if (m_act.action != 0)
1993 /* a shell command */
1994 if ((m_act.action / CK_PipeBlock (0)) == 1)
1996 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
1997 m_act.ch = -1;
1999 g_array_append_val (macros, m_act);
2000 have_macro = TRUE;
2002 g_strfreev (macro_pair);
2003 macro_pair = NULL;
2005 curr_values++;
2007 if (have_macro)
2009 macro.hotkey = hotkey;
2010 macro.macro = macros;
2011 g_array_append_val (macros_list, macro);
2013 profile_keys++;
2014 g_strfreev (values);
2016 g_strfreev (keys);
2017 mc_config_deinit (macros_config);
2018 edit_macro_sort_by_hotkey ();
2019 return TRUE;
2022 /* }}} Macro stuff end here */
2024 /* --------------------------------------------------------------------------------------------- */
2025 /** returns TRUE on success */
2027 gboolean
2028 edit_save_confirm_cmd (WEdit * edit)
2030 char *f = NULL;
2032 if (edit->filename_vpath == NULL)
2033 return edit_save_as_cmd (edit);
2035 if (!edit_check_newline (edit))
2036 return FALSE;
2038 if (edit_confirm_save)
2040 char *filename;
2041 gboolean ok;
2043 filename = vfs_path_to_str (edit->filename_vpath);
2044 f = g_strdup_printf (_("Confirm save file: \"%s\""), filename);
2045 g_free (filename);
2046 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2047 g_free (f);
2048 if (!ok)
2049 return FALSE;
2051 return edit_save_cmd (edit);
2054 /* --------------------------------------------------------------------------------------------- */
2056 * Ask file to edit and load it.
2058 * @return TRUE on success or cancel of ask.
2061 gboolean
2062 edit_load_cmd (Dlg_head * h)
2064 char *exp;
2065 gboolean ret = TRUE; /* possible cancel */
2067 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2068 MC_HISTORY_EDIT_LOAD, INPUT_LAST_TEXT);
2070 if (exp != NULL && *exp != '\0')
2072 vfs_path_t *exp_vpath;
2074 exp_vpath = vfs_path_from_str (exp);
2075 ret = edit_load_file_from_filename (h, exp_vpath);
2076 vfs_path_free (exp_vpath);
2079 g_free (exp);
2081 return ret;
2084 /* --------------------------------------------------------------------------------------------- */
2086 * Load syntax file to edit.
2088 * @return TRUE on success
2091 gboolean
2092 edit_load_syntax_file (Dlg_head * h)
2094 vfs_path_t *extdir_vpath;
2095 int dir = 0;
2096 gboolean ret = FALSE;
2098 if (geteuid () == 0)
2099 dir = query_dialog (_("Syntax file edit"),
2100 _("Which syntax file you want to edit?"), D_NORMAL, 2,
2101 _("&User"), _("&System wide"));
2103 extdir_vpath =
2104 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
2105 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
2107 vfs_path_free (extdir_vpath);
2108 extdir_vpath =
2109 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
2112 if (dir == 0)
2114 vfs_path_t *user_syntax_file_vpath;
2116 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
2117 check_for_default (extdir_vpath, user_syntax_file_vpath);
2118 ret = edit_load_file_from_filename (h, user_syntax_file_vpath);
2119 vfs_path_free (user_syntax_file_vpath);
2121 else if (dir == 1)
2122 ret = edit_load_file_from_filename (h, extdir_vpath);
2124 vfs_path_free (extdir_vpath);
2126 return ret;
2129 /* --------------------------------------------------------------------------------------------- */
2131 * Load menu file to edit.
2133 * @return TRUE on success
2136 gboolean
2137 edit_load_menu_file (Dlg_head * h)
2139 vfs_path_t *buffer_vpath;
2140 vfs_path_t *menufile_vpath;
2141 int dir;
2142 gboolean ret;
2144 dir = query_dialog (_("Menu edit"),
2145 _("Which menu file do you want to edit?"), D_NORMAL,
2146 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2148 menufile_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2149 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
2151 vfs_path_free (menufile_vpath);
2152 menufile_vpath = vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2155 switch (dir)
2157 case 0:
2158 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
2159 check_for_default (menufile_vpath, buffer_vpath);
2160 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
2161 break;
2163 case 1:
2164 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
2165 check_for_default (menufile_vpath, buffer_vpath);
2166 break;
2168 case 2:
2169 buffer_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2170 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
2172 vfs_path_free (buffer_vpath);
2173 buffer_vpath =
2174 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2176 break;
2178 default:
2179 vfs_path_free (menufile_vpath);
2180 return FALSE;
2183 ret = edit_load_file_from_filename (h, buffer_vpath);
2185 vfs_path_free (buffer_vpath);
2186 vfs_path_free (menufile_vpath);
2188 return ret;
2191 /* --------------------------------------------------------------------------------------------- */
2193 * Close window with opened file.
2195 * @return TRUE if file was closed.
2198 gboolean
2199 edit_close_cmd (WEdit * edit)
2201 gboolean ret;
2203 ret = (edit != NULL) && edit_ok_to_exit (edit);
2205 if (ret)
2207 Dlg_head *h = ((Widget *) edit)->owner;
2209 if (edit->locked != 0)
2210 unlock_file (edit->filename_vpath);
2212 del_widget (edit);
2214 if (edit_widget_is_editor ((Widget *) h->current->data))
2215 edit = (WEdit *) h->current->data;
2216 else
2218 edit = find_editor (h);
2219 if (edit != NULL)
2220 dlg_set_top_widget (edit);
2224 if (edit != NULL)
2225 edit->force |= REDRAW_COMPLETELY;
2227 return ret;
2230 /* --------------------------------------------------------------------------------------------- */
2232 if mark2 is -1 then marking is from mark1 to the cursor.
2233 Otherwise its between the markers. This handles this.
2234 Returns 1 if no text is marked.
2238 eval_marks (WEdit * edit, off_t * start_mark, off_t * end_mark)
2240 if (edit->mark1 != edit->mark2)
2242 off_t start_bol, start_eol;
2243 off_t end_bol, end_eol;
2244 off_t diff1, diff2;
2245 long col1, col2;
2246 long end_mark_curs;
2248 if (edit->end_mark_curs < 0)
2249 end_mark_curs = edit->curs1;
2250 else
2251 end_mark_curs = edit->end_mark_curs;
2253 if (edit->mark2 >= 0)
2255 *start_mark = min (edit->mark1, edit->mark2);
2256 *end_mark = max (edit->mark1, edit->mark2);
2258 else
2260 *start_mark = min (edit->mark1, end_mark_curs);
2261 *end_mark = max (edit->mark1, end_mark_curs);
2262 edit->column2 = edit->curs_col + edit->over_col;
2265 if (edit->column_highlight
2266 && (((edit->mark1 > end_mark_curs) && (edit->column1 < edit->column2))
2267 || ((edit->mark1 < end_mark_curs) && (edit->column1 > edit->column2))))
2269 start_bol = edit_bol (edit, *start_mark);
2270 start_eol = edit_eol (edit, start_bol - 1) + 1;
2271 end_bol = edit_bol (edit, *end_mark);
2272 end_eol = edit_eol (edit, *end_mark);
2273 col1 = min (edit->column1, edit->column2);
2274 col2 = max (edit->column1, edit->column2);
2276 diff1 = edit_move_forward3 (edit, start_bol, col2, 0) -
2277 edit_move_forward3 (edit, start_bol, col1, 0);
2278 diff2 = edit_move_forward3 (edit, end_bol, col2, 0) -
2279 edit_move_forward3 (edit, end_bol, 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 /* --------------------------------------------------------------------------------------------- */
2312 off_t
2313 edit_insert_column_of_text_from_file (WEdit * edit, int file,
2314 off_t * start_pos, off_t * end_pos, long *col1, long *col2)
2316 off_t cursor;
2317 int col;
2318 off_t 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 off_t 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 long l;
2338 off_t 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 off_t start_mark, end_mark, current = edit->curs1;
2391 long col_delta = 0;
2392 off_t mark1, mark2;
2393 long c1, c2;
2394 off_t 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 off_t current;
2436 unsigned char *copy_buf = NULL;
2437 off_t start_mark, end_mark;
2438 long line;
2440 if (eval_marks (edit, &start_mark, &end_mark))
2441 return;
2443 if (!edit->column_highlight && edit->curs1 > start_mark && edit->curs1 < end_mark)
2444 return;
2446 line = edit->curs_line;
2447 if (edit->mark2 < 0)
2448 edit_mark_cmd (edit, FALSE);
2449 edit_push_markers (edit);
2451 if (edit->column_highlight)
2453 off_t mark1, mark2;
2454 off_t size;
2455 long c1, c2, b_width;
2456 long x, x2;
2458 c1 = min (edit->column1, edit->column2);
2459 c2 = max (edit->column1, edit->column2);
2460 b_width = c2 - c1;
2462 edit_update_curs_col (edit);
2464 x = edit->curs_col;
2465 x2 = x + edit->over_col;
2467 /* do nothing when cursor inside first line of selected area */
2468 if ((edit_eol (edit, edit->curs1) == edit_eol (edit, start_mark)) && x2 > c1 && x2 <= c2)
2469 return;
2471 if (edit->curs1 > start_mark && edit->curs1 < edit_eol (edit, end_mark))
2473 if (x > c2)
2474 x -= b_width;
2475 else if (x > c1 && x <= c2)
2476 x = c1;
2478 /* save current selection into buffer */
2479 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2481 /* remove current selection */
2482 edit_block_delete_cmd (edit);
2484 edit->over_col = max (0, edit->over_col - b_width);
2485 /* calculate the cursor pos after delete block */
2486 current = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), x, 0);
2487 edit_cursor_move (edit, current - edit->curs1);
2488 edit_scroll_screen_over_cursor (edit);
2490 /* add TWS if need before block insertion */
2491 if (option_cursor_beyond_eol && edit->over_col > 0)
2492 edit_insert_over (edit);
2494 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2495 edit_set_markers (edit, mark1, mark2, c1, c2);
2497 else
2499 off_t count;
2501 current = edit->curs1;
2502 copy_buf = g_malloc0 (end_mark - start_mark);
2503 edit_cursor_move (edit, start_mark - edit->curs1);
2504 edit_scroll_screen_over_cursor (edit);
2506 for (count = start_mark; count < end_mark; count++)
2507 copy_buf[end_mark - count - 1] = edit_delete (edit, TRUE);
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 off_t 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, TRUE);
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, (off_t) 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 * @return 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, off_t start, off_t finish)
2913 int file;
2914 off_t len = 1;
2915 vfs_path_t *vpath;
2917 vpath = vfs_path_from_str (filename);
2918 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2919 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2920 vfs_path_free (vpath);
2921 if (file == -1)
2922 return FALSE;
2924 if (edit->column_highlight)
2926 int r;
2928 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2929 if (r > 0)
2931 unsigned char *block, *p;
2933 p = block = edit_get_block (edit, start, finish, &len);
2934 while (len)
2936 r = mc_write (file, p, len);
2937 if (r < 0)
2938 break;
2939 p += r;
2940 len -= r;
2942 g_free (block);
2945 else
2947 unsigned char *buf;
2948 off_t i = start;
2949 off_t end;
2951 len = finish - start;
2952 buf = g_malloc0 (TEMP_BUF_LEN);
2953 while (start != finish)
2955 end = min (finish, start + TEMP_BUF_LEN);
2956 for (; i < end; i++)
2957 buf[i - start] = edit_get_byte (edit, i);
2958 len -= mc_write (file, (char *) buf, end - start);
2959 start = end;
2961 g_free (buf);
2963 mc_close (file);
2965 return (len == 0);
2968 /* --------------------------------------------------------------------------------------------- */
2970 void
2971 edit_paste_from_history (WEdit * edit)
2973 (void) edit;
2974 edit_error_dialog (_("Error"), _("This function is not implemented"));
2977 /* --------------------------------------------------------------------------------------------- */
2979 gboolean
2980 edit_copy_to_X_buf_cmd (WEdit * edit)
2982 off_t start_mark, end_mark;
2984 if (eval_marks (edit, &start_mark, &end_mark))
2985 return TRUE;
2986 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2988 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2989 return FALSE;
2991 /* try use external clipboard utility */
2992 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2994 return TRUE;
2997 /* --------------------------------------------------------------------------------------------- */
2999 gboolean
3000 edit_cut_to_X_buf_cmd (WEdit * edit)
3002 off_t start_mark, end_mark;
3004 if (eval_marks (edit, &start_mark, &end_mark))
3005 return TRUE;
3006 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
3008 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
3009 return FALSE;
3011 /* try use external clipboard utility */
3012 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
3014 edit_block_delete_cmd (edit);
3015 edit_mark_cmd (edit, TRUE);
3017 return TRUE;
3020 /* --------------------------------------------------------------------------------------------- */
3022 gboolean
3023 edit_paste_from_X_buf_cmd (WEdit * edit)
3025 vfs_path_t *tmp;
3026 gboolean ret;
3028 /* try use external clipboard utility */
3029 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
3030 tmp = mc_config_get_full_vpath (EDIT_CLIP_FILE);
3031 ret = (edit_insert_file (edit, tmp) >= 0);
3032 vfs_path_free (tmp);
3034 return ret;
3037 /* --------------------------------------------------------------------------------------------- */
3039 * Ask user for the line and go to that line.
3040 * Negative numbers mean line from the end (i.e. -1 is the last line).
3043 void
3044 edit_goto_cmd (WEdit * edit)
3046 char *f;
3047 static long line = 0; /* line as typed, saved as default */
3048 long l;
3049 char *error;
3050 char s[32];
3052 g_snprintf (s, sizeof (s), "%ld", line);
3053 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE, line ? s : "");
3054 if (!f)
3055 return;
3057 if (!*f)
3059 g_free (f);
3060 return;
3063 l = strtol (f, &error, 0);
3064 if (*error)
3066 g_free (f);
3067 return;
3070 line = l;
3071 if (l < 0)
3072 l = edit->total_lines + l + 2;
3073 edit_move_display (edit, l - edit->widget.lines / 2 - 1);
3074 edit_move_to_line (edit, l - 1);
3075 edit->force |= REDRAW_COMPLETELY;
3076 g_free (f);
3080 /* --------------------------------------------------------------------------------------------- */
3081 /** Return TRUE on success */
3083 gboolean
3084 edit_save_block_cmd (WEdit * edit)
3086 off_t start_mark, end_mark;
3087 char *exp, *tmp;
3088 gboolean ret = FALSE;
3090 if (eval_marks (edit, &start_mark, &end_mark))
3091 return TRUE;
3093 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3094 exp =
3095 input_expand_dialog (_("Save block"), _("Enter file name:"),
3096 MC_HISTORY_EDIT_SAVE_BLOCK, tmp);
3097 g_free (tmp);
3098 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3100 if (exp != NULL && *exp != '\0')
3102 if (edit_save_block (edit, exp, start_mark, end_mark))
3103 ret = TRUE;
3104 else
3105 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3107 edit->force |= REDRAW_COMPLETELY;
3110 g_free (exp);
3112 return ret;
3116 /* --------------------------------------------------------------------------------------------- */
3118 /** returns TRUE on success */
3119 gboolean
3120 edit_insert_file_cmd (WEdit * edit)
3122 char *tmp;
3123 char *exp;
3124 gboolean ret = FALSE;
3126 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3127 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3128 MC_HISTORY_EDIT_INSERT_FILE, tmp);
3129 g_free (tmp);
3131 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3133 if (exp != NULL && *exp != '\0')
3135 vfs_path_t *exp_vpath;
3137 exp_vpath = vfs_path_from_str (exp);
3138 ret = (edit_insert_file (edit, exp_vpath) >= 0);
3139 vfs_path_free (exp_vpath);
3141 if (!ret)
3142 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3145 g_free (exp);
3147 edit->force |= REDRAW_COMPLETELY;
3148 return ret;
3151 /* --------------------------------------------------------------------------------------------- */
3152 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3155 edit_sort_cmd (WEdit * edit)
3157 static char *old = 0;
3158 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3159 off_t start_mark, end_mark;
3160 int e;
3162 if (eval_marks (edit, &start_mark, &end_mark))
3164 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3165 return 0;
3168 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
3169 edit_save_block (edit, tmp, start_mark, end_mark);
3170 g_free (tmp);
3172 exp = input_dialog (_("Run sort"),
3173 _("Enter sort options (see manpage) separated by whitespace:"),
3174 MC_HISTORY_EDIT_SORT, (old != NULL) ? old : "");
3176 if (!exp)
3177 return 1;
3178 g_free (old);
3179 old = exp;
3180 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
3181 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
3182 tmp =
3183 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3184 " > ", tmp_edit_temp_name, (char *) NULL);
3185 g_free (tmp_edit_temp_name);
3186 g_free (tmp_edit_block_name);
3188 e = system (tmp);
3189 g_free (tmp);
3190 if (e)
3192 if (e == -1 || e == 127)
3194 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3196 else
3198 char q[8];
3199 sprintf (q, "%d ", e);
3200 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3201 edit_error_dialog (_("Sort"), tmp);
3202 g_free (tmp);
3204 return -1;
3207 edit->force |= REDRAW_COMPLETELY;
3209 if (edit_block_delete_cmd (edit))
3210 return 1;
3213 vfs_path_t *tmp_vpath;
3215 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3216 edit_insert_file (edit, tmp_vpath);
3217 vfs_path_free (tmp_vpath);
3219 return 0;
3222 /* --------------------------------------------------------------------------------------------- */
3224 * Ask user for a command, execute it and paste its output back to the
3225 * editor.
3229 edit_ext_cmd (WEdit * edit)
3231 char *exp, *tmp, *tmp_edit_temp_file;
3232 int e;
3234 exp =
3235 input_dialog (_("Paste output of external command"),
3236 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, NULL);
3238 if (!exp)
3239 return 1;
3241 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3242 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3243 g_free (tmp_edit_temp_file);
3244 e = system (tmp);
3245 g_free (tmp);
3246 g_free (exp);
3248 if (e)
3250 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3251 return -1;
3254 edit->force |= REDRAW_COMPLETELY;
3257 vfs_path_t *tmp_vpath;
3259 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3260 edit_insert_file (edit, tmp_vpath);
3261 vfs_path_free (tmp_vpath);
3263 return 0;
3266 /* --------------------------------------------------------------------------------------------- */
3267 /** if block is 1, a block must be highlighted and the shell command
3268 processes it. If block is 0 the shell command is a straight system
3269 command, that just produces some output which is to be inserted */
3271 void
3272 edit_block_process_cmd (WEdit * edit, int macro_number)
3274 char *fname;
3275 char *macros_fname = NULL;
3277 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3278 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3279 user_menu (edit, macros_fname, 0);
3280 g_free (fname);
3281 g_free (macros_fname);
3282 edit->force |= REDRAW_COMPLETELY;
3285 /* --------------------------------------------------------------------------------------------- */
3287 void
3288 edit_mail_dialog (WEdit * edit)
3290 char *tmail_to;
3291 char *tmail_subject;
3292 char *tmail_cc;
3294 static char *mail_cc_last = 0;
3295 static char *mail_subject_last = 0;
3296 static char *mail_to_last = 0;
3298 QuickWidget quick_widgets[] = {
3299 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
3300 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
3301 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input", &tmail_cc),
3302 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT, N_("Copies to")),
3303 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-2",
3304 &tmail_subject),
3305 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT, N_("Subject")),
3306 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-3", &tmail_to),
3307 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT, N_("To")),
3308 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT, N_("mail -s <subject> -c <cc> <to>")),
3309 QUICK_END
3312 QuickDialog Quick_input = {
3313 50, MAIL_DLG_HEIGHT, -1, -1, N_("Mail"),
3314 "[Input Line Keys]", quick_widgets, NULL, NULL, FALSE
3317 quick_widgets[2].u.input.text = mail_cc_last ? mail_cc_last : "";
3318 quick_widgets[4].u.input.text = mail_subject_last ? mail_subject_last : "";
3319 quick_widgets[6].u.input.text = mail_to_last ? mail_to_last : "";
3321 if (quick_dialog (&Quick_input) != B_CANCEL)
3323 g_free (mail_cc_last);
3324 g_free (mail_subject_last);
3325 g_free (mail_to_last);
3326 mail_cc_last = tmail_cc;
3327 mail_subject_last = tmail_subject;
3328 mail_to_last = tmail_to;
3329 pipe_mail (edit, mail_to_last, mail_subject_last, mail_cc_last);
3334 /*******************/
3335 /* Word Completion */
3336 /*******************/
3338 /* --------------------------------------------------------------------------------------------- */
3340 * Complete current word using regular expression search
3341 * backwards beginning at the current cursor position.
3344 void
3345 edit_complete_word_cmd (WEdit * edit)
3347 gsize i, max_len, word_len = 0, num_compl = 0;
3348 off_t word_start = 0;
3349 unsigned char *bufpos;
3350 char *match_expr;
3351 struct selection compl[MAX_WORD_COMPLETIONS]; /* completions */
3353 /* search start of word to be completed */
3354 if (!edit_find_word_start (edit, &word_start, &word_len))
3355 return;
3357 /* prepare match expression */
3358 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3360 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3361 match_expr =
3362 g_strdup_printf
3363 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3364 (int) word_len, bufpos);
3366 /* collect the possible completions */
3367 /* start search from begin to end of file */
3368 max_len =
3369 edit_collect_completions (edit, word_start, word_len, match_expr,
3370 (struct selection *) &compl, &num_compl);
3372 if (num_compl > 0)
3374 /* insert completed word if there is only one match */
3375 if (num_compl == 1)
3377 for (i = word_len; i < compl[0].len; i++)
3378 edit_insert (edit, *(compl[0].text + i));
3380 /* more than one possible completion => ask the user */
3381 else
3383 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3384 /* !!! pressed again the selection dialog pops up, but that !!! */
3385 /* !!! seems to require a further internal state !!! */
3386 /*tty_beep (); */
3388 /* let the user select the preferred completion */
3389 editcmd_dialog_completion_show (edit, max_len, word_len,
3390 (struct selection *) &compl, num_compl);
3394 g_free (match_expr);
3395 /* release memory before return */
3396 for (i = 0; i < num_compl; i++)
3397 g_free (compl[i].text);
3400 /* --------------------------------------------------------------------------------------------- */
3402 #ifdef HAVE_CHARSET
3403 void
3404 edit_select_codepage_cmd (WEdit * edit)
3406 if (do_select_codepage ())
3407 edit_set_codeset (edit);
3409 edit->force = REDRAW_PAGE;
3410 send_message ((Widget *) edit, WIDGET_DRAW, 0);
3412 #endif
3414 /* --------------------------------------------------------------------------------------------- */
3416 void
3417 edit_insert_literal_cmd (WEdit * edit)
3419 int char_for_insertion;
3421 char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3422 _("Press any key:"), FALSE);
3423 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3426 /* --------------------------------------------------------------------------------------------- */
3428 void
3429 edit_begin_end_macro_cmd (WEdit * edit)
3431 /* edit is a pointer to the widget */
3432 if (edit != NULL)
3434 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3435 edit_execute_key_command (edit, command, -1);
3439 /* --------------------------------------------------------------------------------------------- */
3441 void
3442 edit_begin_end_repeat_cmd (WEdit * edit)
3444 /* edit is a pointer to the widget */
3445 if (edit != NULL)
3447 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3448 edit_execute_key_command (edit, command, -1);
3452 /* --------------------------------------------------------------------------------------------- */
3454 gboolean
3455 edit_load_forward_cmd (WEdit * edit)
3457 if (edit->modified
3458 && edit_query_dialog2 (_("Warning"),
3459 _("Current text was modified without a file save.\n"
3460 "Continue discards these changes"), _("C&ontinue"),
3461 _("&Cancel")) == 1)
3463 edit->force |= REDRAW_COMPLETELY;
3464 return TRUE;
3467 if (edit_stack_iterator + 1 >= MAX_HISTORY_MOVETO)
3468 return FALSE;
3470 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3471 return FALSE;
3473 edit_stack_iterator++;
3474 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3475 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3476 edit_history_moveto[edit_stack_iterator].line);
3478 return FALSE;
3481 /* --------------------------------------------------------------------------------------------- */
3483 gboolean
3484 edit_load_back_cmd (WEdit * edit)
3486 if (edit->modified
3487 && edit_query_dialog2 (_("Warning"),
3488 _("Current text was modified without a file save.\n"
3489 "Continue discards these changes"), _("C&ontinue"),
3490 _("&Cancel")) == 1)
3492 edit->force |= REDRAW_COMPLETELY;
3493 return TRUE;
3496 /* we are in the bottom of the stack, NO WAY! */
3497 if (edit_stack_iterator == 0)
3498 return FALSE;
3500 edit_stack_iterator--;
3501 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3502 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3503 edit_history_moveto[edit_stack_iterator].line);
3505 return FALSE;
3508 /* --------------------------------------------------------------------------------------------- */
3510 void
3511 edit_get_match_keyword_cmd (WEdit * edit)
3513 gsize word_len = 0, max_len = 0;
3514 int num_def = 0;
3515 int i;
3516 off_t word_start = 0;
3517 unsigned char *bufpos;
3518 char *match_expr;
3519 char *path = NULL;
3520 char *ptr = NULL;
3521 char *tagfile = NULL;
3523 etags_hash_t def_hash[MAX_DEFINITIONS];
3525 for (i = 0; i < MAX_DEFINITIONS; i++)
3527 def_hash[i].filename = NULL;
3530 /* search start of word to be completed */
3531 if (!edit_find_word_start (edit, &word_start, &word_len))
3532 return;
3534 /* prepare match expression */
3535 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3536 match_expr = g_strdup_printf ("%.*s", (int) word_len, bufpos);
3538 ptr = g_get_current_dir ();
3539 path = g_strconcat (ptr, G_DIR_SEPARATOR_S, (char *) NULL);
3540 g_free (ptr);
3542 /* Recursive search file 'TAGS' in parent dirs */
3545 ptr = g_path_get_dirname (path);
3546 g_free (path);
3547 path = ptr;
3548 g_free (tagfile);
3549 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3550 if (exist_file (tagfile))
3551 break;
3553 while (strcmp (path, G_DIR_SEPARATOR_S) != 0);
3555 if (tagfile)
3557 num_def =
3558 etags_set_definition_hash (tagfile, path, match_expr, (etags_hash_t *) & def_hash);
3559 g_free (tagfile);
3561 g_free (path);
3563 max_len = MAX_WIDTH_DEF_DIALOG;
3564 word_len = 0;
3565 if (num_def > 0)
3567 editcmd_dialog_select_definition_show (edit, match_expr, max_len, word_len,
3568 (etags_hash_t *) & def_hash, num_def);
3570 g_free (match_expr);
3573 /* --------------------------------------------------------------------------------------------- */
3575 #ifdef HAVE_ASPELL
3577 edit_suggest_current_word (WEdit * edit)
3579 gsize cut_len = 0;
3580 gsize word_len = 0;
3581 off_t word_start = 0;
3582 int retval = B_SKIP_WORD;
3583 char *match_word;
3585 /* search start of word to spell check */
3586 match_word = edit_get_word_from_pos (edit, edit->curs1, &word_start, &word_len, &cut_len);
3588 #ifdef HAVE_CHARSET
3589 if (mc_global.source_codepage >= 0 && (mc_global.source_codepage != mc_global.display_codepage))
3591 GString *tmp_word;
3593 tmp_word = str_convert_to_display (match_word);
3594 g_free (match_word);
3595 match_word = g_string_free (tmp_word, FALSE);
3597 #endif
3598 if (!aspell_check (match_word, (int) word_len))
3600 GArray *suggest;
3601 unsigned int res;
3603 suggest = g_array_new (TRUE, FALSE, sizeof (char *));
3605 res = aspell_suggest (suggest, match_word, (int) word_len);
3606 if (res != 0)
3608 char *new_word = NULL;
3610 edit->found_start = word_start;
3611 edit->found_len = word_len;
3612 edit->force |= REDRAW_PAGE;
3613 edit_scroll_screen_over_cursor (edit);
3614 edit_render_keypress (edit);
3616 retval = spell_dialog_spell_suggest_show (edit, match_word, &new_word, suggest);
3617 edit_cursor_move (edit, word_len - cut_len);
3619 if (retval == B_ENTER && new_word != NULL)
3621 guint i;
3622 char *cp_word;
3624 #ifdef HAVE_CHARSET
3625 if (mc_global.source_codepage >= 0 &&
3626 (mc_global.source_codepage != mc_global.display_codepage))
3628 GString *tmp_word;
3630 tmp_word = str_convert_to_input (new_word);
3631 g_free (new_word);
3632 new_word = g_string_free (tmp_word, FALSE);
3634 #endif
3635 cp_word = new_word;
3636 for (i = 0; i < word_len; i++)
3637 edit_backspace (edit, TRUE);
3638 for (; *new_word; new_word++)
3639 edit_insert (edit, *new_word);
3640 g_free (cp_word);
3642 else if (retval == B_ADD_WORD && match_word != NULL)
3643 aspell_add_to_dict (match_word, (int) word_len);
3646 g_array_free (suggest, TRUE);
3647 edit->found_start = 0;
3648 edit->found_len = 0;
3650 g_free (match_word);
3651 return retval;
3654 /* --------------------------------------------------------------------------------------------- */
3656 void
3657 edit_spellcheck_file (WEdit * edit)
3659 if (edit->curs_line > 0)
3661 edit_cursor_move (edit, -edit->curs1);
3662 edit_move_to_prev_col (edit, 0);
3663 edit_update_curs_row (edit);
3668 int c1, c2;
3670 c2 = edit_get_byte (edit, edit->curs1);
3674 if (edit->curs1 >= edit->last_byte)
3675 return;
3677 c1 = c2;
3678 edit_cursor_move (edit, 1);
3679 c2 = edit_get_byte (edit, edit->curs1);
3681 while (is_break_char (c1) || is_break_char (c2));
3683 while (edit_suggest_current_word (edit) != B_CANCEL);
3686 /* --------------------------------------------------------------------------------------------- */
3688 void
3689 edit_set_spell_lang (void)
3691 GArray *lang_list;
3693 lang_list = g_array_new (TRUE, FALSE, sizeof (char *));
3694 if (aspell_get_lang_list (lang_list) != 0)
3696 char *lang;
3698 lang = spell_dialog_lang_list_show (lang_list);
3699 if (lang != NULL)
3700 (void) aspell_set_lang (lang);
3702 aspell_array_clean (lang_list);
3704 #endif /* HAVE_ASPELL */
3706 /* --------------------------------------------------------------------------------------------- */