First bunch of mhl_mem_free removal patches
[midnight-commander.git] / edit / editoptions.c
blob777431f0d5c49b37474356763b4cbc88712b69ef
1 /* editor options dialog box
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2007
4 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.
24 #include <config.h>
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <sys/types.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <string.h>
33 #include <ctype.h>
34 #include <errno.h>
35 #include <sys/stat.h>
37 #include <stdlib.h>
39 #include "../src/global.h"
41 #include "edit.h"
42 #include "usermap.h"
43 #include "../src/dialog.h" /* B_CANCEL */
44 #include "../src/wtools.h" /* QuickDialog */
46 #define OPT_DLG_H 17
47 #define OPT_DLG_W 72
49 #ifndef USE_INTERNAL_EDIT
50 #define USE_INTERNAL_EDIT 1
51 #endif
53 static const char *key_emu_str[] =
54 {N_("Intuitive"), N_("Emacs"), N_("User-defined"), NULL};
56 static const char *wrap_str[] =
57 {N_("None"), N_("Dynamic paragraphing"), N_("Type writer wrap"), NULL};
59 static void
60 i18n_translate_array (const char *array[])
62 while (*array!=NULL) {
63 *array = _(*array);
64 array++;
68 void
69 edit_options_dialog (void)
71 char wrap_length[32], tab_spacing[32], *p, *q;
72 int wrap_mode = 0;
73 int old_syntax_hl;
74 int tedit_key_emulation = edit_key_emulation;
75 int toption_fill_tabs_with_spaces = option_fill_tabs_with_spaces;
76 int toption_save_position = option_save_position;
77 int tedit_confirm_save = edit_confirm_save;
78 int tedit_syntax_highlighting = option_syntax_highlighting;
79 int toption_return_does_auto_indent = option_return_does_auto_indent;
80 int toption_backspace_through_tabs = option_backspace_through_tabs;
81 int toption_fake_half_tabs = option_fake_half_tabs;
82 static int i18n_flag = 0;
84 QuickWidget quick_widgets[] = {
85 /* 0 */
86 {quick_button, 6, 10, OPT_DLG_H - 3, OPT_DLG_H, N_("&Cancel"), 0,
87 B_CANCEL, 0, 0, NULL},
88 /* 1 */
89 {quick_button, 2, 10, OPT_DLG_H - 3, OPT_DLG_H, N_("&OK"), 0,
90 B_ENTER, 0, 0, NULL},
91 /* 2 */
92 {quick_label, OPT_DLG_W / 2, OPT_DLG_W, OPT_DLG_H - 5, OPT_DLG_H,
93 N_("Word wrap line length: "), 0, 0, 0, 0, NULL},
94 /* 3 */
95 {quick_input, OPT_DLG_W / 2 + 24, OPT_DLG_W, OPT_DLG_H - 5,
96 OPT_DLG_H, "", OPT_DLG_W / 2 - 4 - 24, 0, 0, 0, "edit-word-wrap"},
97 /* 4 */
98 {quick_label, OPT_DLG_W / 2, OPT_DLG_W, OPT_DLG_H - 6, OPT_DLG_H,
99 N_("Tab spacing: "), 0, 0, 0, 0, NULL},
100 /* 5 */
101 {quick_input, OPT_DLG_W / 2 + 24, OPT_DLG_W, OPT_DLG_H - 6,
102 OPT_DLG_H, "", OPT_DLG_W / 2 - 4 - 24, 0, 0, 0,
103 "edit-tab-spacing"},
104 /* 6 */
105 {quick_checkbox, OPT_DLG_W / 2 + 1, OPT_DLG_W, OPT_DLG_H - 8,
106 OPT_DLG_H, N_("Synta&x highlighting"), 8, 0, 0, 0, NULL},
107 /* 7 */
108 {quick_checkbox, OPT_DLG_W / 2 + 1, OPT_DLG_W, OPT_DLG_H - 9,
109 OPT_DLG_H, N_("Save file &position"), 0, 0, 0, 0, NULL},
110 /* 8 */
111 {quick_checkbox, OPT_DLG_W / 2 + 1, OPT_DLG_W, OPT_DLG_H - 10,
112 OPT_DLG_H, N_("Confir&m before saving"), 6, 0, 0, 0, NULL},
113 /* 9 */
114 {quick_checkbox, OPT_DLG_W / 2 + 1, OPT_DLG_W, OPT_DLG_H - 11,
115 OPT_DLG_H, N_("Fill tabs with &spaces"), 0, 0, 0, 0, NULL},
116 /* 10 */
117 {quick_checkbox, OPT_DLG_W / 2 + 1, OPT_DLG_W, OPT_DLG_H - 12,
118 OPT_DLG_H, N_("&Return does autoindent"), 0, 0, 0, 0, NULL},
119 /* 11 */
120 {quick_checkbox, OPT_DLG_W / 2 + 1, OPT_DLG_W, OPT_DLG_H - 13,
121 OPT_DLG_H, N_("&Backspace through tabs"), 0, 0, 0, 0, NULL},
122 /* 12 */
123 {quick_checkbox, OPT_DLG_W / 2 + 1, OPT_DLG_W, OPT_DLG_H - 14,
124 OPT_DLG_H, N_("&Fake half tabs"), 0, 0, 0, 0, NULL},
125 /* 13 */
126 {quick_radio, 5, OPT_DLG_W, OPT_DLG_H - 7, OPT_DLG_H, "", 3, 0, 0,
127 const_cast(char **, wrap_str), "wrapm"},
128 /* 14 */
129 {quick_label, 4, OPT_DLG_W, OPT_DLG_H - 8, OPT_DLG_H,
130 N_("Wrap mode"), 0, 0,
131 0, 0, NULL},
132 /* 15 */
133 {quick_radio, 5, OPT_DLG_W, OPT_DLG_H - 13, OPT_DLG_H, "", 3, 0, 0,
134 const_cast(char **, key_emu_str), "keyemu"},
135 /* 16 */
136 {quick_label, 4, OPT_DLG_W, OPT_DLG_H - 14, OPT_DLG_H,
137 N_("Key emulation"), 0, 0, 0, 0, NULL},
138 NULL_QuickWidget
141 QuickDialog Quick_options =
142 { OPT_DLG_W, OPT_DLG_H, -1, 0, N_(" Editor options "), "", 0, 0 };
144 if (!i18n_flag) {
145 i18n_translate_array (key_emu_str);
146 i18n_translate_array (wrap_str);
147 i18n_flag = 1;
150 snprintf (wrap_length, sizeof (wrap_length), "%d",
151 option_word_wrap_line_length);
152 snprintf (tab_spacing, sizeof (tab_spacing), "%d",
153 option_tab_spacing);
155 quick_widgets[3].text = wrap_length;
156 quick_widgets[3].str_result = &p;
157 quick_widgets[5].text = tab_spacing;
158 quick_widgets[5].str_result = &q;
159 quick_widgets[6].result = &tedit_syntax_highlighting;
160 quick_widgets[7].result = &toption_save_position;
161 quick_widgets[8].result = &tedit_confirm_save;
162 quick_widgets[9].result = &toption_fill_tabs_with_spaces;
163 quick_widgets[10].result = &toption_return_does_auto_indent;
164 quick_widgets[11].result = &toption_backspace_through_tabs;
165 quick_widgets[12].result = &toption_fake_half_tabs;
167 if (option_auto_para_formatting)
168 wrap_mode = 1;
169 else if (option_typewriter_wrap)
170 wrap_mode = 2;
171 else
172 wrap_mode = 0;
174 quick_widgets[13].result = &wrap_mode;
175 quick_widgets[13].value = wrap_mode;
177 quick_widgets[15].result = &tedit_key_emulation;
178 quick_widgets[15].value = tedit_key_emulation;
180 Quick_options.widgets = quick_widgets;
182 if (quick_dialog (&Quick_options) == B_CANCEL)
183 return;
185 old_syntax_hl = option_syntax_highlighting;
187 if (p) {
188 option_word_wrap_line_length = atoi (p);
189 g_free (p);
191 if (q) {
192 option_tab_spacing = atoi (q);
193 if (option_tab_spacing <= 0)
194 option_tab_spacing = 8;
195 g_free (q);
198 option_syntax_highlighting = tedit_syntax_highlighting;
199 edit_confirm_save = tedit_confirm_save;
200 option_save_position = toption_save_position;
201 option_fill_tabs_with_spaces = toption_fill_tabs_with_spaces;
202 option_return_does_auto_indent = toption_return_does_auto_indent;
203 option_backspace_through_tabs = toption_backspace_through_tabs;
204 option_fake_half_tabs = toption_fake_half_tabs;
206 if (wrap_mode == 1) {
207 option_auto_para_formatting = 1;
208 option_typewriter_wrap = 0;
209 } else if (wrap_mode == 2) {
210 option_auto_para_formatting = 0;
211 option_typewriter_wrap = 1;
212 } else {
213 option_auto_para_formatting = 0;
214 option_typewriter_wrap = 0;
217 /* Reload menu if key emulation has changed */
218 if (edit_key_emulation != tedit_key_emulation) {
219 edit_key_emulation = tedit_key_emulation;
220 edit_reload_menu ();
223 /* Load or unload syntax rules if the option has changed */
224 if (option_syntax_highlighting != old_syntax_hl)
225 edit_load_syntax (wedit, NULL, option_syntax_type);
226 /* Load usermap if it's needed */
227 edit_load_user_map (wedit);