editcmd.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382
  1. /* editor high level editing commands
  2. Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
  3. 2007 Free Software Foundation, Inc.
  4. Authors: 1996, 1997 Paul Sheer
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301, USA.
  17. */
  18. /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
  19. #include <config.h>
  20. #include <assert.h>
  21. #include <ctype.h>
  22. #include <stdio.h>
  23. #include <stdarg.h>
  24. #include <sys/types.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <sys/stat.h>
  29. #include <stdlib.h>
  30. #include "../src/global.h"
  31. #include "../src/history.h"
  32. #include "edit.h"
  33. #include "editlock.h"
  34. #include "editcmddef.h"
  35. #include "edit-widget.h"
  36. #include "editcmd_dialogs.h"
  37. #include "../edit/etags.h"
  38. #include "../src/panel.h"
  39. #include "../src/tty.h" /* LINES */
  40. #include "../src/widget.h" /* listbox_new() */
  41. #include "../src/layout.h" /* clr_scr() */
  42. #include "../src/main.h" /* mc_home source_codepage */
  43. #include "../src/help.h" /* interactive_display() */
  44. #include "../src/key.h" /* XCTRL */
  45. #include "../src/wtools.h" /* message() */
  46. #include "../src/charsets.h"
  47. #include "../src/selcodepage.h"
  48. #include "../src/strutil.h" /* utf string functions */
  49. /* globals: */
  50. /* search and replace: */
  51. static int search_create_bookmark = 0;
  52. /* static int search_in_all_charsets = 0; */
  53. /* queries on a save */
  54. int edit_confirm_save = 1;
  55. static int edit_save_cmd (WEdit *edit);
  56. static unsigned char *edit_get_block (WEdit *edit, long start,
  57. long finish, int *l);
  58. static void
  59. edit_search_cmd_search_create_bookmark(WEdit * edit)
  60. {
  61. int found = 0, books = 0;
  62. int l = 0, l_last = -1;
  63. long q = 0;
  64. gsize len = 0;
  65. for (;;) {
  66. if (!mc_search_run(edit->search, (void *) edit, q, edit->last_byte, &len))
  67. break;
  68. found++;
  69. l += edit_count_lines (edit, q, edit->search->normal_offset);
  70. if (l != l_last) {
  71. book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
  72. books++;
  73. }
  74. l_last = l;
  75. q = edit->search->normal_offset + 1;
  76. }
  77. if (found) {
  78. /* in response to number of bookmarks added because of string being found %d times */
  79. message (D_NORMAL, _("Search"), _(" %d items found, %d bookmarks added "), found, books);
  80. } else {
  81. edit_error_dialog (_ ("Search"), _ (" Search string not found "));
  82. }
  83. }
  84. static int
  85. edit_search_cmd_callback(const void *user_data, gsize char_offset)
  86. {
  87. return edit_get_byte ((WEdit * )user_data, (long) char_offset);
  88. }
  89. void edit_help_cmd (WEdit * edit)
  90. {
  91. interactive_display (NULL, "[Internal File Editor]");
  92. edit->force |= REDRAW_COMPLETELY;
  93. }
  94. void edit_refresh_cmd (WEdit * edit)
  95. {
  96. #ifndef HAVE_SLANG
  97. clr_scr();
  98. do_refresh();
  99. #else
  100. {
  101. int color;
  102. edit_get_syntax_color (edit, -1, &color);
  103. }
  104. touchwin(stdscr);
  105. #endif /* !HAVE_SLANG */
  106. mc_refresh();
  107. doupdate();
  108. }
  109. /* If 0 (quick save) then a) create/truncate <filename> file,
  110. b) save to <filename>;
  111. if 1 (safe save) then a) save to <tempnam>,
  112. b) rename <tempnam> to <filename>;
  113. if 2 (do backups) then a) save to <tempnam>,
  114. b) rename <filename> to <filename.backup_ext>,
  115. c) rename <tempnam> to <filename>. */
  116. /* returns 0 on error, -1 on abort */
  117. static int
  118. edit_save_file (WEdit *edit, const char *filename)
  119. {
  120. char *p;
  121. gchar *tmp;
  122. long filelen = 0;
  123. char *savename = 0;
  124. gchar *real_filename;
  125. int this_save_mode, fd = -1;
  126. if (!filename)
  127. return 0;
  128. if (!*filename)
  129. return 0;
  130. if (*filename != PATH_SEP && edit->dir) {
  131. real_filename = concat_dir_and_file (edit->dir, filename);
  132. } else {
  133. real_filename = g_strdup(filename);
  134. }
  135. this_save_mode = option_save_mode;
  136. if (this_save_mode != EDIT_QUICK_SAVE) {
  137. if (!vfs_file_is_local (real_filename) ||
  138. (fd = mc_open (real_filename, O_RDONLY | O_BINARY)) == -1) {
  139. /*
  140. * The file does not exists yet, so no safe save or
  141. * backup are necessary.
  142. */
  143. this_save_mode = EDIT_QUICK_SAVE;
  144. }
  145. if (fd != -1)
  146. mc_close (fd);
  147. }
  148. if (this_save_mode == EDIT_QUICK_SAVE &&
  149. !edit->skip_detach_prompt) {
  150. int rv;
  151. struct stat sb;
  152. rv = mc_stat (real_filename, &sb);
  153. if (rv == 0 && sb.st_nlink > 1) {
  154. rv = edit_query_dialog3 (_("Warning"),
  155. _(" File has hard-links. Detach before saving? "),
  156. _("&Yes"), _("&No"), _("&Cancel"));
  157. switch (rv) {
  158. case 0:
  159. this_save_mode = EDIT_SAFE_SAVE;
  160. /* fallthrough */
  161. case 1:
  162. edit->skip_detach_prompt = 1;
  163. break;
  164. default:
  165. g_free(real_filename);
  166. return -1;
  167. }
  168. }
  169. /* Prevent overwriting changes from other editor sessions. */
  170. if (rv == 0 && edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime) {
  171. /* The default action is "Cancel". */
  172. query_set_sel(1);
  173. rv = edit_query_dialog2 (
  174. _("Warning"),
  175. _("The file has been modified in the meantime. Save anyway?"),
  176. _("&Yes"),
  177. _("&Cancel"));
  178. if (rv != 0){
  179. g_free(real_filename);
  180. return -1;
  181. }
  182. }
  183. }
  184. if (this_save_mode != EDIT_QUICK_SAVE) {
  185. char *savedir, *saveprefix;
  186. const char *slashpos;
  187. slashpos = strrchr (real_filename, PATH_SEP);
  188. if (slashpos) {
  189. savedir = g_strdup (real_filename);
  190. savedir[slashpos - real_filename + 1] = '\0';
  191. } else
  192. savedir = g_strdup (".");
  193. saveprefix = concat_dir_and_file (savedir, "cooledit");
  194. g_free (savedir);
  195. fd = mc_mkstemps (&savename, saveprefix, NULL);
  196. g_free (saveprefix);
  197. if (!savename){
  198. g_free(real_filename);
  199. return 0;
  200. }
  201. /* FIXME:
  202. * Close for now because mc_mkstemps use pure open system call
  203. * to create temporary file and it needs to be reopened by
  204. * VFS-aware mc_open().
  205. */
  206. close (fd);
  207. } else
  208. savename = g_strdup (real_filename);
  209. mc_chown (savename, edit->stat1.st_uid, edit->stat1.st_gid);
  210. mc_chmod (savename, edit->stat1.st_mode);
  211. if ((fd =
  212. mc_open (savename, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY,
  213. edit->stat1.st_mode)) == -1)
  214. goto error_save;
  215. /* pipe save */
  216. if ((p = edit_get_write_filter (savename, real_filename))) {
  217. FILE *file;
  218. mc_close (fd);
  219. file = (FILE *) popen (p, "w");
  220. if (file) {
  221. filelen = edit_write_stream (edit, file);
  222. #if 1
  223. pclose (file);
  224. #else
  225. if (pclose (file) != 0) {
  226. tmp = g_strconcat (_(" Error writing to pipe: "),
  227. p, " ", (char *) NULL);
  228. edit_error_dialog (_("Error"), tmp);
  229. g_free(tmp);
  230. g_free (p);
  231. goto error_save;
  232. }
  233. #endif
  234. } else {
  235. tmp = g_strconcat (_(" Cannot open pipe for writing: "),
  236. p, " ", (char *) NULL);
  237. edit_error_dialog (_("Error"),
  238. get_sys_error (tmp));
  239. g_free (p);
  240. g_free(tmp);
  241. goto error_save;
  242. }
  243. g_free (p);
  244. } else {
  245. long buf;
  246. buf = 0;
  247. filelen = edit->last_byte;
  248. while (buf <= (edit->curs1 >> S_EDIT_BUF_SIZE) - 1) {
  249. if (mc_write (fd, (char *) edit->buffers1[buf], EDIT_BUF_SIZE)
  250. != EDIT_BUF_SIZE) {
  251. mc_close (fd);
  252. goto error_save;
  253. }
  254. buf++;
  255. }
  256. if (mc_write
  257. (fd, (char *) edit->buffers1[buf],
  258. edit->curs1 & M_EDIT_BUF_SIZE) !=
  259. (edit->curs1 & M_EDIT_BUF_SIZE)) {
  260. filelen = -1;
  261. } else if (edit->curs2) {
  262. edit->curs2--;
  263. buf = (edit->curs2 >> S_EDIT_BUF_SIZE);
  264. if (mc_write
  265. (fd,
  266. (char *) edit->buffers2[buf] + EDIT_BUF_SIZE -
  267. (edit->curs2 & M_EDIT_BUF_SIZE) - 1,
  268. 1 + (edit->curs2 & M_EDIT_BUF_SIZE)) !=
  269. 1 + (edit->curs2 & M_EDIT_BUF_SIZE)) {
  270. filelen = -1;
  271. } else {
  272. while (--buf >= 0) {
  273. if (mc_write
  274. (fd, (char *) edit->buffers2[buf],
  275. EDIT_BUF_SIZE) != EDIT_BUF_SIZE) {
  276. filelen = -1;
  277. break;
  278. }
  279. }
  280. }
  281. edit->curs2++;
  282. }
  283. if (mc_close (fd))
  284. goto error_save;
  285. /* Update the file information, especially the mtime. */
  286. if (mc_stat (savename, &edit->stat1) == -1)
  287. goto error_save;
  288. }
  289. if (filelen != edit->last_byte)
  290. goto error_save;
  291. if (this_save_mode == EDIT_DO_BACKUP) {
  292. assert (option_backup_ext != NULL);
  293. tmp = g_strconcat (real_filename, option_backup_ext,(char *) NULL);
  294. if (mc_rename (real_filename, tmp) == -1){
  295. g_free(tmp);
  296. goto error_save;
  297. }
  298. }
  299. if (this_save_mode != EDIT_QUICK_SAVE)
  300. if (mc_rename (savename, real_filename) == -1)
  301. goto error_save;
  302. g_free (savename);
  303. g_free(real_filename);
  304. return 1;
  305. error_save:
  306. /* FIXME: Is this safe ?
  307. * if (this_save_mode != EDIT_QUICK_SAVE)
  308. * mc_unlink (savename);
  309. */
  310. g_free(real_filename);
  311. g_free (savename);
  312. return 0;
  313. }
  314. void menu_save_mode_cmd (void)
  315. {
  316. #define DLG_X 38
  317. #define DLG_Y 10
  318. static char *str_result;
  319. static int save_mode_new;
  320. static const char *str[] =
  321. {
  322. N_("Quick save "),
  323. N_("Safe save "),
  324. N_("Do backups -->")};
  325. static QuickWidget widgets[] =
  326. {
  327. {quick_button, 18, DLG_X, 7, DLG_Y, N_("&Cancel"), 0,
  328. B_CANCEL, 0, 0, NULL, NULL, NULL},
  329. {quick_button, 6, DLG_X, 7, DLG_Y, N_("&OK"), 0,
  330. B_ENTER, 0, 0, NULL, NULL, NULL},
  331. {quick_input, 23, DLG_X, 5, DLG_Y, 0, 9,
  332. 0, 0, &str_result, "edit-backup-ext", NULL, NULL},
  333. {quick_label, 22, DLG_X, 4, DLG_Y, N_("Extension:"), 0,
  334. 0, 0, 0, NULL, NULL, NULL},
  335. {quick_radio, 4, DLG_X, 3, DLG_Y, "", 3,
  336. 0, &save_mode_new, (char **) str, NULL, NULL, NULL},
  337. NULL_QuickWidget};
  338. static QuickDialog dialog =
  339. {DLG_X, DLG_Y, -1, -1, N_(" Edit Save Mode "), "[Edit Save Mode]",
  340. widgets, 0};
  341. static int i18n_flag = 0;
  342. if (!i18n_flag) {
  343. size_t i;
  344. size_t maxlen = 0;
  345. int dlg_x;
  346. size_t l1;
  347. /* OK/Cancel buttons */
  348. l1 = str_term_width1 (_(widgets[0].text)) + str_term_width1 (_(widgets[1].text)) + 5;
  349. maxlen = max (maxlen, l1);
  350. for (i = 0; i < 3; i++ ) {
  351. str[i] = _(str[i]);
  352. maxlen = max (maxlen, (size_t) str_term_width1 (str[i]) + 7);
  353. }
  354. i18n_flag = 1;
  355. dlg_x = maxlen + str_term_width1 (_(widgets[3].text)) + 5 + 1;
  356. widgets[2].hotkey_pos = str_term_width1 (_(widgets[3].text)); /* input field length */
  357. dlg_x = min (COLS, dlg_x);
  358. dialog.xlen = dlg_x;
  359. i = (dlg_x - l1)/3;
  360. widgets[1].relative_x = i;
  361. widgets[0].relative_x = i + str_term_width1 (_(widgets[1].text)) + i + 4;
  362. widgets[2].relative_x = widgets[3].relative_x = maxlen + 2;
  363. for (i = 0; i < sizeof (widgets)/sizeof (widgets[0]); i++)
  364. widgets[i].x_divisions = dlg_x;
  365. }
  366. assert (option_backup_ext != NULL);
  367. widgets[2].text = option_backup_ext;
  368. widgets[4].value = option_save_mode;
  369. if (quick_dialog (&dialog) != B_ENTER)
  370. return;
  371. option_save_mode = save_mode_new;
  372. g_free (option_backup_ext);
  373. option_backup_ext = str_result;
  374. str_result = NULL;
  375. }
  376. void
  377. edit_set_filename (WEdit *edit, const char *f)
  378. {
  379. g_free (edit->filename);
  380. if (!f)
  381. f = "";
  382. edit->filename = g_strdup (f);
  383. if (edit->dir == NULL && *f != PATH_SEP)
  384. #ifdef USE_VFS
  385. edit->dir = g_strdup (vfs_get_current_dir ());
  386. #else
  387. edit->dir = g_get_current_dir ();
  388. #endif
  389. }
  390. /* Here we want to warn the users of overwriting an existing file,
  391. but only if they have made a change to the filename */
  392. /* returns 1 on success */
  393. int
  394. edit_save_as_cmd (WEdit *edit)
  395. {
  396. /* This heads the 'Save As' dialog box */
  397. char *exp;
  398. int save_lock = 0;
  399. int different_filename = 0;
  400. exp = input_expand_dialog (
  401. _(" Save As "), _(" Enter file name: "),MC_HISTORY_EDIT_SAVE_AS, edit->filename);
  402. edit_push_action (edit, KEY_PRESS + edit->start_display);
  403. if (exp) {
  404. if (!*exp) {
  405. g_free (exp);
  406. edit->force |= REDRAW_COMPLETELY;
  407. return 0;
  408. } else {
  409. int rv;
  410. if (strcmp (edit->filename, exp)) {
  411. int file;
  412. different_filename = 1;
  413. if ((file = mc_open (exp, O_RDONLY | O_BINARY)) != -1) {
  414. /* the file exists */
  415. mc_close (file);
  416. /* Overwrite the current file or cancel the operation */
  417. if (edit_query_dialog2
  418. (_("Warning"),
  419. _(" A file already exists with this name. "),
  420. _("&Overwrite"), _("&Cancel"))) {
  421. edit->force |= REDRAW_COMPLETELY;
  422. g_free (exp);
  423. return 0;
  424. }
  425. }
  426. save_lock = edit_lock_file (exp);
  427. } else {
  428. /* filenames equal, check if already locked */
  429. if (!edit->locked && !edit->delete_file)
  430. save_lock = edit_lock_file (exp);
  431. }
  432. rv = edit_save_file (edit, exp);
  433. switch (rv) {
  434. case 1:
  435. /* Succesful, so unlock both files */
  436. if (different_filename) {
  437. if (save_lock)
  438. edit_unlock_file (exp);
  439. if (edit->locked)
  440. edit->locked = edit_unlock_file (edit->filename);
  441. } else {
  442. if (edit->locked || save_lock)
  443. edit->locked = edit_unlock_file (edit->filename);
  444. }
  445. edit_set_filename (edit, exp);
  446. g_free (exp);
  447. edit->modified = 0;
  448. edit->delete_file = 0;
  449. if (different_filename)
  450. edit_load_syntax (edit, NULL, option_syntax_type);
  451. edit->force |= REDRAW_COMPLETELY;
  452. return 1;
  453. default:
  454. edit_error_dialog (_(" Save As "),
  455. get_sys_error (_
  456. (" Cannot save file. ")));
  457. /* fallthrough */
  458. case -1:
  459. /* Failed, so maintain modify (not save) lock */
  460. if (save_lock)
  461. edit_unlock_file (exp);
  462. g_free (exp);
  463. edit->force |= REDRAW_COMPLETELY;
  464. return 0;
  465. }
  466. }
  467. }
  468. edit->force |= REDRAW_COMPLETELY;
  469. return 0;
  470. }
  471. /* {{{ Macro stuff starts here */
  472. /* creates a macro file if it doesn't exist */
  473. static FILE *edit_open_macro_file (const char *r)
  474. {
  475. gchar *filename;
  476. FILE *fd;
  477. int file;
  478. filename = g_strconcat ( home_dir, PATH_SEP_STR MACRO_FILE, (char *) NULL);
  479. if ((file = open (filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1){
  480. g_free(filename);
  481. return 0;
  482. }
  483. close (file);
  484. fd = fopen (filename, r);
  485. g_free(filename);
  486. return fd;
  487. }
  488. #define MAX_MACROS 1024
  489. static int saved_macro[MAX_MACROS + 1];
  490. static int saved_macros_loaded = 0;
  491. /*
  492. This is just to stop the macro file be loaded over and over for keys
  493. that aren't defined to anything. On slow systems this could be annoying.
  494. */
  495. static int
  496. macro_exists (int k)
  497. {
  498. int i;
  499. for (i = 0; i < MAX_MACROS && saved_macro[i]; i++)
  500. if (saved_macro[i] == k)
  501. return i;
  502. return -1;
  503. }
  504. /* returns 1 on error */
  505. static int
  506. edit_delete_macro (WEdit * edit, int k)
  507. {
  508. gchar *tmp, *tmp2;
  509. struct macro macro[MAX_MACRO_LENGTH];
  510. FILE *f, *g;
  511. int s, i, n, j = 0;
  512. (void) edit;
  513. if (saved_macros_loaded)
  514. if ((j = macro_exists (k)) < 0)
  515. return 0;
  516. tmp = g_strconcat (home_dir, PATH_SEP_STR TEMP_FILE, (char *) NULL);
  517. g = fopen (tmp , "w");
  518. g_free(tmp);
  519. if (!g) {
  520. edit_error_dialog (_(" Delete macro "),
  521. get_sys_error (_(" Cannot open temp file ")));
  522. return 1;
  523. }
  524. f = edit_open_macro_file ("r");
  525. if (!f) {
  526. edit_error_dialog (_(" Delete macro "),
  527. get_sys_error (_(" Cannot open macro file ")));
  528. fclose (g);
  529. return 1;
  530. }
  531. for (;;) {
  532. n = fscanf (f, ("key '%d 0': "), &s);
  533. if (!n || n == EOF)
  534. break;
  535. n = 0;
  536. while (fscanf (f, "%hd %hd, ", &macro[n].command, &macro[n].ch))
  537. n++;
  538. fscanf (f, ";\n");
  539. if (s != k) {
  540. fprintf (g, ("key '%d 0': "), s);
  541. for (i = 0; i < n; i++)
  542. fprintf (g, "%hd %hd, ", macro[i].command, macro[i].ch);
  543. fprintf (g, ";\n");
  544. }
  545. }
  546. fclose (f);
  547. fclose (g);
  548. tmp = g_strconcat (home_dir, PATH_SEP_STR TEMP_FILE, (char *) NULL);
  549. tmp2 = g_strconcat (home_dir, PATH_SEP_STR MACRO_FILE, (char *) NULL);
  550. if (rename ( tmp, tmp2) == -1) {
  551. edit_error_dialog (_(" Delete macro "),
  552. get_sys_error (_(" Cannot overwrite macro file ")));
  553. g_free(tmp);
  554. g_free(tmp2);
  555. return 1;
  556. }
  557. g_free(tmp);
  558. g_free(tmp2);
  559. if (saved_macros_loaded)
  560. memmove (saved_macro + j, saved_macro + j + 1, sizeof (int) * (MAX_MACROS - j - 1));
  561. return 0;
  562. }
  563. /* returns 0 on error */
  564. int edit_save_macro_cmd (WEdit * edit, struct macro macro[], int n)
  565. {
  566. FILE *f;
  567. int s, i;
  568. edit_push_action (edit, KEY_PRESS + edit->start_display);
  569. s = editcmd_dialog_raw_key_query (_(" Save macro "),
  570. _(" Press the macro's new hotkey: "), 1);
  571. edit->force |= REDRAW_COMPLETELY;
  572. if (s) {
  573. if (edit_delete_macro (edit, s))
  574. return 0;
  575. f = edit_open_macro_file ("a+");
  576. if (f) {
  577. fprintf (f, ("key '%d 0': "), s);
  578. for (i = 0; i < n; i++)
  579. fprintf (f, "%hd %hd, ", macro[i].command, macro[i].ch);
  580. fprintf (f, ";\n");
  581. fclose (f);
  582. if (saved_macros_loaded) {
  583. for (i = 0; i < MAX_MACROS && saved_macro[i]; i++);
  584. saved_macro[i] = s;
  585. }
  586. return 1;
  587. } else
  588. edit_error_dialog (_(" Save macro "), get_sys_error (_(" Cannot open macro file ")));
  589. }
  590. return 0;
  591. }
  592. void edit_delete_macro_cmd (WEdit * edit)
  593. {
  594. int command;
  595. command = editcmd_dialog_raw_key_query (_ (" Delete macro "),
  596. _ (" Press macro hotkey: "), 1);
  597. if (!command)
  598. return;
  599. edit_delete_macro (edit, command);
  600. }
  601. /* return 0 on error */
  602. int edit_load_macro_cmd (WEdit * edit, struct macro macro[], int *n, int k)
  603. {
  604. FILE *f;
  605. int s, i = 0, found = 0;
  606. (void) edit;
  607. if (saved_macros_loaded)
  608. if (macro_exists (k) < 0)
  609. return 0;
  610. if ((f = edit_open_macro_file ("r"))) {
  611. struct macro dummy;
  612. do {
  613. int u;
  614. u = fscanf (f, ("key '%d 0': "), &s);
  615. if (!u || u == EOF)
  616. break;
  617. if (!saved_macros_loaded)
  618. saved_macro[i++] = s;
  619. if (!found) {
  620. *n = 0;
  621. while (*n < MAX_MACRO_LENGTH && 2 == fscanf (f, "%hd %hd, ", &macro[*n].command, &macro[*n].ch))
  622. (*n)++;
  623. } else {
  624. while (2 == fscanf (f, "%hd %hd, ", &dummy.command, &dummy.ch));
  625. }
  626. fscanf (f, ";\n");
  627. if (s == k)
  628. found = 1;
  629. } while (!found || !saved_macros_loaded);
  630. if (!saved_macros_loaded) {
  631. saved_macro[i] = 0;
  632. saved_macros_loaded = 1;
  633. }
  634. fclose (f);
  635. return found;
  636. } else
  637. edit_error_dialog (_(" Load macro "),
  638. get_sys_error (_(" Cannot open macro file ")));
  639. return 0;
  640. }
  641. /* }}} Macro stuff starts here */
  642. /* returns 1 on success */
  643. int edit_save_confirm_cmd (WEdit * edit)
  644. {
  645. gchar *f = NULL;
  646. if (edit_confirm_save) {
  647. f = g_strconcat (_(" Confirm save file? : "), edit->filename, " ", NULL);
  648. if (edit_query_dialog2 (_(" Save file "), f, _("&Save"), _("&Cancel"))){
  649. g_free(f);
  650. return 0;
  651. }
  652. g_free(f);
  653. }
  654. return edit_save_cmd (edit);
  655. }
  656. /* returns 1 on success */
  657. static int
  658. edit_save_cmd (WEdit *edit)
  659. {
  660. int res, save_lock = 0;
  661. if (!edit->locked && !edit->delete_file)
  662. save_lock = edit_lock_file (edit->filename);
  663. res = edit_save_file (edit, edit->filename);
  664. /* Maintain modify (not save) lock on failure */
  665. if ((res > 0 && edit->locked) || save_lock)
  666. edit->locked = edit_unlock_file (edit->filename);
  667. /* On failure try 'save as', it does locking on its own */
  668. if (!res)
  669. return edit_save_as_cmd (edit);
  670. edit->force |= REDRAW_COMPLETELY;
  671. if (res > 0) {
  672. edit->delete_file = 0;
  673. edit->modified = 0;
  674. }
  675. return 1;
  676. }
  677. /* returns 1 on success */
  678. int edit_new_cmd (WEdit * edit)
  679. {
  680. if (edit->modified) {
  681. if (edit_query_dialog2 (_ ("Warning"), _ (" Current text was modified without a file save. \n Continue discards these changes. "), _ ("C&ontinue"), _ ("&Cancel"))) {
  682. edit->force |= REDRAW_COMPLETELY;
  683. return 0;
  684. }
  685. }
  686. edit->force |= REDRAW_COMPLETELY;
  687. return edit_renew (edit); /* if this gives an error, something has really screwed up */
  688. }
  689. /* returns 1 on error */
  690. static int
  691. edit_load_file_from_filename (WEdit * edit, char *exp)
  692. {
  693. int prev_locked = edit->locked;
  694. char *prev_filename = g_strdup (edit->filename);
  695. if (!edit_reload (edit, exp)) {
  696. g_free (prev_filename);
  697. return 1;
  698. }
  699. if (prev_locked)
  700. edit_unlock_file (prev_filename);
  701. g_free (prev_filename);
  702. return 0;
  703. }
  704. int
  705. edit_load_cmd (WEdit *edit)
  706. {
  707. char *exp;
  708. if (edit->modified) {
  709. if (edit_query_dialog2
  710. (_("Warning"),
  711. _(" Current text was modified without a file save. \n"
  712. " Continue discards these changes. "), _("C&ontinue"),
  713. _("&Cancel"))) {
  714. edit->force |= REDRAW_COMPLETELY;
  715. return 0;
  716. }
  717. }
  718. exp = input_expand_dialog (_(" Load "), _(" Enter file name: "),
  719. MC_HISTORY_EDIT_LOAD, edit->filename);
  720. if (exp) {
  721. if (*exp)
  722. edit_load_file_from_filename (edit, exp);
  723. g_free (exp);
  724. }
  725. edit->force |= REDRAW_COMPLETELY;
  726. return 0;
  727. }
  728. /*
  729. if mark2 is -1 then marking is from mark1 to the cursor.
  730. Otherwise its between the markers. This handles this.
  731. Returns 1 if no text is marked.
  732. */
  733. int eval_marks (WEdit * edit, long *start_mark, long *end_mark)
  734. {
  735. if (edit->mark1 != edit->mark2) {
  736. if (edit->mark2 >= 0) {
  737. *start_mark = min (edit->mark1, edit->mark2);
  738. *end_mark = max (edit->mark1, edit->mark2);
  739. } else {
  740. *start_mark = min (edit->mark1, edit->curs1);
  741. *end_mark = max (edit->mark1, edit->curs1);
  742. edit->column2 = edit->curs_col;
  743. }
  744. return 0;
  745. } else {
  746. *start_mark = *end_mark = 0;
  747. edit->column2 = edit->column1 = 0;
  748. return 1;
  749. }
  750. }
  751. #define space_width 1
  752. static void
  753. edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width)
  754. {
  755. long cursor;
  756. int i, col;
  757. cursor = edit->curs1;
  758. col = edit_get_col (edit);
  759. for (i = 0; i < size; i++) {
  760. if (data[i] == '\n') { /* fill in and move to next line */
  761. int l;
  762. long p;
  763. if (edit_get_byte (edit, edit->curs1) != '\n') {
  764. l = width - (edit_get_col (edit) - col);
  765. while (l > 0) {
  766. edit_insert (edit, ' ');
  767. l -= space_width;
  768. }
  769. }
  770. for (p = edit->curs1;; p++) {
  771. if (p == edit->last_byte) {
  772. edit_cursor_move (edit, edit->last_byte - edit->curs1);
  773. edit_insert_ahead (edit, '\n');
  774. p++;
  775. break;
  776. }
  777. if (edit_get_byte (edit, p) == '\n') {
  778. p++;
  779. break;
  780. }
  781. }
  782. edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
  783. l = col - edit_get_col (edit);
  784. while (l >= space_width) {
  785. edit_insert (edit, ' ');
  786. l -= space_width;
  787. }
  788. continue;
  789. }
  790. edit_insert (edit, data[i]);
  791. }
  792. edit_cursor_move (edit, cursor - edit->curs1);
  793. }
  794. void
  795. edit_block_copy_cmd (WEdit *edit)
  796. {
  797. long start_mark, end_mark, current = edit->curs1;
  798. int size;
  799. unsigned char *copy_buf;
  800. edit_update_curs_col (edit);
  801. if (eval_marks (edit, &start_mark, &end_mark))
  802. return;
  803. copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
  804. /* all that gets pushed are deletes hence little space is used on the stack */
  805. edit_push_markers (edit);
  806. if (column_highlighting) {
  807. edit_insert_column_of_text (edit, copy_buf, size,
  808. abs (edit->column2 - edit->column1));
  809. } else {
  810. while (size--)
  811. edit_insert_ahead (edit, copy_buf[size]);
  812. }
  813. g_free (copy_buf);
  814. edit_scroll_screen_over_cursor (edit);
  815. if (column_highlighting) {
  816. edit_set_markers (edit, 0, 0, 0, 0);
  817. edit_push_action (edit, COLUMN_ON);
  818. column_highlighting = 0;
  819. } else if (start_mark < current && end_mark > current)
  820. edit_set_markers (edit, start_mark,
  821. end_mark + end_mark - start_mark, 0, 0);
  822. edit->force |= REDRAW_PAGE;
  823. }
  824. void
  825. edit_block_move_cmd (WEdit *edit)
  826. {
  827. long count;
  828. long current;
  829. unsigned char *copy_buf;
  830. long start_mark, end_mark;
  831. int deleted = 0;
  832. int x = 0;
  833. if (eval_marks (edit, &start_mark, &end_mark))
  834. return;
  835. if (column_highlighting) {
  836. edit_update_curs_col (edit);
  837. x = edit->curs_col;
  838. if (start_mark <= edit->curs1 && end_mark >= edit->curs1)
  839. if ((x > edit->column1 && x < edit->column2)
  840. || (x > edit->column2 && x < edit->column1))
  841. return;
  842. } else if (start_mark <= edit->curs1 && end_mark >= edit->curs1)
  843. return;
  844. if ((end_mark - start_mark) > option_max_undo / 2)
  845. if (edit_query_dialog2
  846. (_("Warning"),
  847. _
  848. (" Block is large, you may not be able to undo this action. "),
  849. _("C&ontinue"), _("&Cancel")))
  850. return;
  851. edit_push_markers (edit);
  852. current = edit->curs1;
  853. if (column_highlighting) {
  854. int size, c1, c2, line;
  855. line = edit->curs_line;
  856. if (edit->mark2 < 0)
  857. edit_mark_cmd (edit, 0);
  858. c1 = min (edit->column1, edit->column2);
  859. c2 = max (edit->column1, edit->column2);
  860. copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
  861. if (x < c2) {
  862. edit_block_delete_cmd (edit);
  863. deleted = 1;
  864. }
  865. edit_move_to_line (edit, line);
  866. edit_cursor_move (edit,
  867. edit_move_forward3 (edit,
  868. edit_bol (edit, edit->curs1),
  869. x, 0) - edit->curs1);
  870. edit_insert_column_of_text (edit, copy_buf, size, c2 - c1);
  871. if (!deleted) {
  872. line = edit->curs_line;
  873. edit_update_curs_col (edit);
  874. x = edit->curs_col;
  875. edit_block_delete_cmd (edit);
  876. edit_move_to_line (edit, line);
  877. edit_cursor_move (edit,
  878. edit_move_forward3 (edit,
  879. edit_bol (edit,
  880. edit->curs1),
  881. x, 0) - edit->curs1);
  882. }
  883. edit_set_markers (edit, 0, 0, 0, 0);
  884. edit_push_action (edit, COLUMN_ON);
  885. column_highlighting = 0;
  886. } else {
  887. copy_buf = g_malloc (end_mark - start_mark);
  888. edit_cursor_move (edit, start_mark - edit->curs1);
  889. edit_scroll_screen_over_cursor (edit);
  890. count = start_mark;
  891. while (count < end_mark) {
  892. copy_buf[end_mark - count - 1] = edit_delete (edit, 1);
  893. count++;
  894. }
  895. edit_scroll_screen_over_cursor (edit);
  896. edit_cursor_move (edit,
  897. current - edit->curs1 -
  898. (((current - edit->curs1) >
  899. 0) ? end_mark - start_mark : 0));
  900. edit_scroll_screen_over_cursor (edit);
  901. while (count-- > start_mark)
  902. edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
  903. edit_set_markers (edit, edit->curs1,
  904. edit->curs1 + end_mark - start_mark, 0, 0);
  905. }
  906. edit_scroll_screen_over_cursor (edit);
  907. g_free (copy_buf);
  908. edit->force |= REDRAW_PAGE;
  909. }
  910. static void
  911. edit_delete_column_of_text (WEdit * edit)
  912. {
  913. long p, q, r, m1, m2;
  914. int b, c, d;
  915. int n;
  916. eval_marks (edit, &m1, &m2);
  917. n = edit_move_forward (edit, m1, 0, m2) + 1;
  918. c = edit_move_forward3 (edit, edit_bol (edit, m1), 0, m1);
  919. d = edit_move_forward3 (edit, edit_bol (edit, m2), 0, m2);
  920. b = min (c, d);
  921. c = max (c, d);
  922. while (n--) {
  923. r = edit_bol (edit, edit->curs1);
  924. p = edit_move_forward3 (edit, r, b, 0);
  925. q = edit_move_forward3 (edit, r, c, 0);
  926. if (p < m1)
  927. p = m1;
  928. if (q > m2)
  929. q = m2;
  930. edit_cursor_move (edit, p - edit->curs1);
  931. while (q > p) { /* delete line between margins */
  932. if (edit_get_byte (edit, edit->curs1) != '\n')
  933. edit_delete (edit, 1);
  934. q--;
  935. }
  936. if (n) /* move to next line except on the last delete */
  937. edit_cursor_move (edit, edit_move_forward (edit, edit->curs1, 1, 0) - edit->curs1);
  938. }
  939. }
  940. /* if success return 0 */
  941. static int
  942. edit_block_delete (WEdit *edit)
  943. {
  944. long count;
  945. long start_mark, end_mark;
  946. if (eval_marks (edit, &start_mark, &end_mark))
  947. return 0;
  948. if (column_highlighting && edit->mark2 < 0)
  949. edit_mark_cmd (edit, 0);
  950. if ((end_mark - start_mark) > option_max_undo / 2) {
  951. /* Warning message with a query to continue or cancel the operation */
  952. if (edit_query_dialog2
  953. (_("Warning"),
  954. _
  955. (" Block is large, you may not be able to undo this action. "),
  956. _("C&ontinue"), _("&Cancel"))) {
  957. return 1;
  958. }
  959. }
  960. edit_push_markers (edit);
  961. edit_cursor_move (edit, start_mark - edit->curs1);
  962. edit_scroll_screen_over_cursor (edit);
  963. count = start_mark;
  964. if (start_mark < end_mark) {
  965. if (column_highlighting) {
  966. if (edit->mark2 < 0)
  967. edit_mark_cmd (edit, 0);
  968. edit_delete_column_of_text (edit);
  969. } else {
  970. while (count < end_mark) {
  971. edit_delete (edit, 1);
  972. count++;
  973. }
  974. }
  975. }
  976. edit_set_markers (edit, 0, 0, 0, 0);
  977. edit->force |= REDRAW_PAGE;
  978. return 0;
  979. }
  980. /* returns 1 if canceelled by user */
  981. int edit_block_delete_cmd (WEdit * edit)
  982. {
  983. long start_mark, end_mark;
  984. if (eval_marks (edit, &start_mark, &end_mark)) {
  985. edit_delete_line (edit);
  986. return 0;
  987. }
  988. return edit_block_delete (edit);
  989. }
  990. #define INPUT_INDEX 9
  991. static gboolean
  992. editcmd_find (WEdit *edit, gsize *len)
  993. {
  994. gsize search_start = edit->search_start;
  995. gsize search_end;
  996. if (edit->replace_backwards) {
  997. search_end = edit->curs1;
  998. while ((int) search_start >= 0) {
  999. if (search_end - search_start > edit->search->original_len && mc_search_is_fixed_search_str(edit->search))
  1000. search_end = search_start + edit->search->original_len +1;
  1001. if ( mc_search_run(edit->search, (void *) edit, search_start, search_end, len))
  1002. {
  1003. return TRUE;
  1004. }
  1005. search_start--;
  1006. }
  1007. } else {
  1008. return mc_search_run(edit->search, (void *) edit, edit->search_start, edit->last_byte, len);
  1009. }
  1010. return FALSE;
  1011. }
  1012. /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
  1013. (and the above) routines to work properly - paul */
  1014. #define is_digit(x) ((x) >= '0' && (x) <= '9')
  1015. static char *
  1016. edit_replace_cmd__conv_to_display(char *str)
  1017. {
  1018. GString *tmp;
  1019. tmp = str_convert_to_display (str);
  1020. if (tmp && tmp->len){
  1021. g_free(str);
  1022. str = tmp->str;
  1023. }
  1024. g_string_free (tmp, FALSE);
  1025. return str;
  1026. }
  1027. static char *
  1028. edit_replace_cmd__conv_to_input(char *str)
  1029. {
  1030. GString *tmp;
  1031. tmp = str_convert_to_input (str);
  1032. if (tmp && tmp->len){
  1033. g_free(str);
  1034. str = tmp->str;
  1035. }
  1036. g_string_free (tmp, FALSE);
  1037. return str;
  1038. }
  1039. /* call with edit = 0 before shutdown to close memory leaks */
  1040. void
  1041. edit_replace_cmd (WEdit *edit, int again)
  1042. {
  1043. /* 1 = search string, 2 = replace with */
  1044. static char *saved1 = NULL; /* saved default[123] */
  1045. static char *saved2 = NULL;
  1046. char *input1 = NULL; /* user input from the dialog */
  1047. char *input2 = NULL;
  1048. char *str_for_prompt_dialog = NULL;
  1049. int replace_yes;
  1050. long times_replaced = 0, last_search;
  1051. gboolean once_found = FALSE;
  1052. if (!edit) {
  1053. g_free (saved1), saved1 = NULL;
  1054. g_free (saved2), saved2 = NULL;
  1055. return;
  1056. }
  1057. last_search = edit->last_byte;
  1058. edit->force |= REDRAW_COMPLETELY;
  1059. if (again && !saved1 && !saved2)
  1060. again = 0;
  1061. if (again) {
  1062. input1 = g_strdup (saved1 ? saved1 : "");
  1063. input2 = g_strdup (saved2 ? saved2 : "");
  1064. } else {
  1065. char *disp1 = edit_replace_cmd__conv_to_display(g_strdup (saved1 ? saved1 : ""));
  1066. char *disp2 = edit_replace_cmd__conv_to_display(g_strdup (saved2 ? saved2 : ""));
  1067. edit_push_action (edit, KEY_PRESS + edit->start_display);
  1068. editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2 );
  1069. g_free (disp1);
  1070. g_free (disp2);
  1071. str_for_prompt_dialog = g_strdup(input2);
  1072. if (input1 == NULL || *input1 == '\0') {
  1073. edit->force = REDRAW_COMPLETELY;
  1074. goto cleanup;
  1075. }
  1076. input1 = edit_replace_cmd__conv_to_input(input1);
  1077. input2 = edit_replace_cmd__conv_to_input(input2);
  1078. g_free (saved1), saved1 = g_strdup (input1);
  1079. g_free (saved2), saved2 = g_strdup (input2);
  1080. if (edit->search)
  1081. {
  1082. mc_search_free(edit->search);
  1083. edit->search = NULL;
  1084. }
  1085. }
  1086. if (!edit->search)
  1087. {
  1088. edit->search = mc_search_new(input1, -1);
  1089. if (edit->search == NULL)
  1090. {
  1091. edit->search_start = edit->curs1;
  1092. return;
  1093. }
  1094. edit->search->search_type = edit->search_type;
  1095. edit->search->is_all_charsets = edit->all_codepages;
  1096. edit->search->is_case_sentitive = edit->replace_case;
  1097. edit->search->search_fn = edit_search_cmd_callback;
  1098. }
  1099. if (edit->found_len && edit->search_start == edit->found_start + 1
  1100. && edit->replace_backwards)
  1101. edit->search_start--;
  1102. if (edit->found_len && edit->search_start == edit->found_start - 1
  1103. && !edit->replace_backwards)
  1104. edit->search_start++;
  1105. do {
  1106. gsize len = 0;
  1107. long new_start;
  1108. if (! editcmd_find(edit, &len))
  1109. {
  1110. if (!(edit->search->error == MC_SEARCH_E_OK || (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
  1111. {
  1112. edit_error_dialog (_ ("Search"), edit->search->error_str);
  1113. }
  1114. break;
  1115. }
  1116. once_found = TRUE;
  1117. new_start = edit->search->normal_offset;
  1118. edit->search_start = new_start = edit->search->normal_offset;
  1119. /*returns negative on not found or error in pattern */
  1120. if (edit->search_start >= 0) {
  1121. int i;
  1122. edit->found_start = edit->search_start;
  1123. i = edit->found_len = len;
  1124. edit_cursor_move (edit, edit->search_start - edit->curs1);
  1125. edit_scroll_screen_over_cursor (edit);
  1126. replace_yes = 1;
  1127. if (edit->replace_mode==0) {
  1128. int l;
  1129. l = edit->curs_row - edit->num_widget_lines / 3;
  1130. if (l > 0)
  1131. edit_scroll_downward (edit, l);
  1132. if (l < 0)
  1133. edit_scroll_upward (edit, -l);
  1134. edit_scroll_screen_over_cursor (edit);
  1135. edit->force |= REDRAW_PAGE;
  1136. edit_render_keypress (edit);
  1137. /*so that undo stops at each query */
  1138. edit_push_key_press (edit);
  1139. switch (editcmd_dialog_replace_prompt_show (edit, str_for_prompt_dialog, /* and prompt 2/3 down */
  1140. -1,
  1141. -1)) {
  1142. case B_ENTER:
  1143. break;
  1144. case B_SKIP_REPLACE:
  1145. replace_yes = 0;
  1146. break;
  1147. case B_REPLACE_ALL:
  1148. edit->replace_mode=1;
  1149. break;
  1150. case B_REPLACE_ONE:
  1151. break;
  1152. case B_CANCEL:
  1153. replace_yes = 0;
  1154. break;
  1155. }
  1156. }
  1157. if (replace_yes) { /* delete then insert new */
  1158. GString *repl_str, *tmp_str;
  1159. tmp_str = g_string_new(input2);
  1160. repl_str = mc_search_prepare_replace_str (edit->search, tmp_str);
  1161. g_string_free(tmp_str, TRUE);
  1162. if (edit->search->error != MC_SEARCH_E_OK)
  1163. {
  1164. edit_error_dialog (_ ("Replace"), edit->search->error_str);
  1165. break;
  1166. }
  1167. while (i--)
  1168. edit_delete (edit, 1);
  1169. while (++i < repl_str->len)
  1170. edit_insert (edit, repl_str->str[i]);
  1171. g_string_free(repl_str, TRUE);
  1172. edit->found_len = i;
  1173. }
  1174. /* so that we don't find the same string again */
  1175. if (edit->replace_backwards) {
  1176. last_search = edit->search_start;
  1177. edit->search_start--;
  1178. } else {
  1179. edit->search_start += i;
  1180. last_search = edit->last_byte;
  1181. }
  1182. edit_scroll_screen_over_cursor (edit);
  1183. } else {
  1184. const char *msg = _(" Replace ");
  1185. /* try and find from right here for next search */
  1186. edit->search_start = edit->curs1;
  1187. edit_update_curs_col (edit);
  1188. edit->force |= REDRAW_PAGE;
  1189. edit_render_keypress (edit);
  1190. if (times_replaced) {
  1191. message (D_NORMAL, msg, _(" %ld replacements made. "),
  1192. times_replaced);
  1193. } else
  1194. query_dialog (msg, _(" Search string not found "),
  1195. D_NORMAL, 1, _("&OK"));
  1196. edit->replace_mode = -1;
  1197. }
  1198. } while (edit->replace_mode >0);
  1199. edit->force = REDRAW_COMPLETELY;
  1200. edit_scroll_screen_over_cursor (edit);
  1201. cleanup:
  1202. g_free (str_for_prompt_dialog);
  1203. g_free (input1);
  1204. g_free (input2);
  1205. }
  1206. void edit_search_cmd (WEdit * edit, int again)
  1207. {
  1208. char *search_string = NULL, *search_string_dup = NULL;
  1209. gsize len = 0;
  1210. if (!edit)
  1211. return;
  1212. if (edit->search != NULL)
  1213. search_string_dup = search_string = g_strndup(edit->search->original, edit->search->original_len);
  1214. if (!again)
  1215. {
  1216. #ifdef HAVE_CHARSET
  1217. GString *tmp;
  1218. if (search_string && *search_string)
  1219. {
  1220. tmp = str_convert_to_display (search_string);
  1221. g_free(search_string_dup);
  1222. search_string_dup = NULL;
  1223. if (tmp && tmp->len)
  1224. search_string = search_string_dup = tmp->str;
  1225. g_string_free (tmp, FALSE);
  1226. }
  1227. #endif /* HAVE_CHARSET */
  1228. editcmd_dialog_search_show (edit, &search_string);
  1229. #ifdef HAVE_CHARSET
  1230. if (search_string && *search_string)
  1231. {
  1232. tmp = str_convert_to_input (search_string);
  1233. if (tmp && tmp->len)
  1234. search_string = tmp->str;
  1235. g_string_free (tmp, FALSE);
  1236. if (search_string_dup)
  1237. g_free(search_string_dup);
  1238. }
  1239. #endif /* HAVE_CHARSET */
  1240. edit_push_action (edit, KEY_PRESS + edit->start_display);
  1241. if (!search_string)
  1242. {
  1243. edit->force |= REDRAW_COMPLETELY;
  1244. edit_scroll_screen_over_cursor (edit);
  1245. return;
  1246. }
  1247. if (edit->search)
  1248. {
  1249. mc_search_free(edit->search);
  1250. edit->search = NULL;
  1251. }
  1252. }
  1253. if (!edit->search)
  1254. {
  1255. edit->search = mc_search_new(search_string, -1);
  1256. if (edit->search == NULL)
  1257. {
  1258. edit->search_start = edit->curs1;
  1259. return;
  1260. }
  1261. edit->search->search_type = edit->search_type;
  1262. edit->search->is_all_charsets = edit->all_codepages;
  1263. edit->search->is_case_sentitive = edit->replace_case;
  1264. edit->search->search_fn = edit_search_cmd_callback;
  1265. }
  1266. if (search_create_bookmark)
  1267. {
  1268. edit_search_cmd_search_create_bookmark(edit);
  1269. }
  1270. else
  1271. {
  1272. if (edit->found_len && edit->search_start == edit->found_start + 1 && edit->replace_backwards)
  1273. edit->search_start--;
  1274. if (edit->found_len && edit->search_start == edit->found_start - 1 && !edit->replace_backwards)
  1275. edit->search_start++;
  1276. if (editcmd_find(edit, &len))
  1277. {
  1278. edit->found_start = edit->search_start = edit->search->normal_offset;
  1279. edit->found_len = len;
  1280. edit_cursor_move (edit, edit->search_start - edit->curs1);
  1281. edit_scroll_screen_over_cursor (edit);
  1282. if (edit->replace_backwards)
  1283. edit->search_start--;
  1284. else
  1285. edit->search_start++;
  1286. }
  1287. else
  1288. {
  1289. edit->search_start = edit->curs1;
  1290. edit_error_dialog (_ ("Search"), edit->search->error_str);
  1291. }
  1292. }
  1293. edit->force |= REDRAW_COMPLETELY;
  1294. edit_scroll_screen_over_cursor (edit);
  1295. }
  1296. /*
  1297. * Check if it's OK to close the editor. If there are unsaved changes,
  1298. * ask user. Return 1 if it's OK to exit, 0 to continue editing.
  1299. */
  1300. int
  1301. edit_ok_to_exit (WEdit *edit)
  1302. {
  1303. if (!edit->modified)
  1304. return 1;
  1305. switch (edit_query_dialog3
  1306. (_("Quit"), _(" File was modified, Save with exit? "),
  1307. _("&Cancel quit"), _("&Yes"), _("&No"))) {
  1308. case 1:
  1309. edit_push_markers (edit);
  1310. edit_set_markers (edit, 0, 0, 0, 0);
  1311. if (!edit_save_cmd (edit))
  1312. return 0;
  1313. break;
  1314. case 2:
  1315. break;
  1316. case 0:
  1317. case -1:
  1318. return 0;
  1319. }
  1320. return 1;
  1321. }
  1322. #define TEMP_BUF_LEN 1024
  1323. /* Return a null terminated length of text. Result must be g_free'd */
  1324. static unsigned char *
  1325. edit_get_block (WEdit *edit, long start, long finish, int *l)
  1326. {
  1327. unsigned char *s, *r;
  1328. r = s = g_malloc (finish - start + 1);
  1329. if (column_highlighting) {
  1330. *l = 0;
  1331. /* copy from buffer, excluding chars that are out of the column 'margins' */
  1332. while (start < finish) {
  1333. int c, x;
  1334. x = edit_move_forward3 (edit, edit_bol (edit, start), 0,
  1335. start);
  1336. c = edit_get_byte (edit, start);
  1337. if ((x >= edit->column1 && x < edit->column2)
  1338. || (x >= edit->column2 && x < edit->column1) || c == '\n') {
  1339. *s++ = c;
  1340. (*l)++;
  1341. }
  1342. start++;
  1343. }
  1344. } else {
  1345. *l = finish - start;
  1346. while (start < finish)
  1347. *s++ = edit_get_byte (edit, start++);
  1348. }
  1349. *s = 0;
  1350. return r;
  1351. }
  1352. /* save block, returns 1 on success */
  1353. int
  1354. edit_save_block (WEdit * edit, const char *filename, long start,
  1355. long finish)
  1356. {
  1357. int len, file;
  1358. if ((file =
  1359. mc_open (filename, O_CREAT | O_WRONLY | O_TRUNC,
  1360. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY)) == -1)
  1361. return 0;
  1362. if (column_highlighting) {
  1363. unsigned char *block, *p;
  1364. int r;
  1365. p = block = edit_get_block (edit, start, finish, &len);
  1366. while (len) {
  1367. r = mc_write (file, p, len);
  1368. if (r < 0)
  1369. break;
  1370. p += r;
  1371. len -= r;
  1372. }
  1373. g_free (block);
  1374. } else {
  1375. unsigned char *buf;
  1376. int i = start, end;
  1377. len = finish - start;
  1378. buf = g_malloc (TEMP_BUF_LEN);
  1379. while (start != finish) {
  1380. end = min (finish, start + TEMP_BUF_LEN);
  1381. for (; i < end; i++)
  1382. buf[i - start] = edit_get_byte (edit, i);
  1383. len -= mc_write (file, (char *) buf, end - start);
  1384. start = end;
  1385. }
  1386. g_free (buf);
  1387. }
  1388. mc_close (file);
  1389. if (len)
  1390. return 0;
  1391. return 1;
  1392. }
  1393. /* copies a block to clipboard file */
  1394. static int edit_save_block_to_clip_file (WEdit * edit, long start, long finish)
  1395. {
  1396. int ret;
  1397. gchar *tmp;
  1398. tmp = g_strconcat (home_dir, PATH_SEP_STR CLIP_FILE, (char *) NULL);
  1399. ret = edit_save_block (edit, tmp, start, finish);
  1400. g_free(tmp);
  1401. return ret;
  1402. }
  1403. void edit_paste_from_history (WEdit *edit)
  1404. {
  1405. (void) edit;
  1406. edit_error_dialog (_(" Error "), _(" This function is not implemented. "));
  1407. }
  1408. int edit_copy_to_X_buf_cmd (WEdit * edit)
  1409. {
  1410. long start_mark, end_mark;
  1411. if (eval_marks (edit, &start_mark, &end_mark))
  1412. return 0;
  1413. if (!edit_save_block_to_clip_file (edit, start_mark, end_mark)) {
  1414. edit_error_dialog (_(" Copy to clipboard "), get_sys_error (_(" Unable to save to file. ")));
  1415. return 1;
  1416. }
  1417. edit_mark_cmd (edit, 1);
  1418. return 0;
  1419. }
  1420. int edit_cut_to_X_buf_cmd (WEdit * edit)
  1421. {
  1422. long start_mark, end_mark;
  1423. if (eval_marks (edit, &start_mark, &end_mark))
  1424. return 0;
  1425. if (!edit_save_block_to_clip_file (edit, start_mark, end_mark)) {
  1426. edit_error_dialog (_(" Cut to clipboard "), _(" Unable to save to file. "));
  1427. return 1;
  1428. }
  1429. edit_block_delete_cmd (edit);
  1430. edit_mark_cmd (edit, 1);
  1431. return 0;
  1432. }
  1433. void edit_paste_from_X_buf_cmd (WEdit * edit)
  1434. {
  1435. gchar *tmp;
  1436. tmp = g_strconcat (home_dir, PATH_SEP_STR CLIP_FILE, (char *) NULL);
  1437. edit_insert_file (edit, tmp);
  1438. g_free(tmp);
  1439. }
  1440. /*
  1441. * Ask user for the line and go to that line.
  1442. * Negative numbers mean line from the end (i.e. -1 is the last line).
  1443. */
  1444. void
  1445. edit_goto_cmd (WEdit *edit)
  1446. {
  1447. char *f;
  1448. static long line = 0; /* line as typed, saved as default */
  1449. long l;
  1450. char *error;
  1451. char s[32];
  1452. g_snprintf (s, sizeof (s), "%ld", line);
  1453. f = input_dialog (_(" Goto line "), _(" Enter line: "), MC_HISTORY_EDIT_GOTO_LINE,
  1454. line ? s : "");
  1455. if (!f)
  1456. return;
  1457. if (!*f) {
  1458. g_free (f);
  1459. return;
  1460. }
  1461. l = strtol (f, &error, 0);
  1462. if (*error) {
  1463. g_free (f);
  1464. return;
  1465. }
  1466. line = l;
  1467. if (l < 0)
  1468. l = edit->total_lines + l + 2;
  1469. edit_move_display (edit, l - edit->num_widget_lines / 2 - 1);
  1470. edit_move_to_line (edit, l - 1);
  1471. edit->force |= REDRAW_COMPLETELY;
  1472. g_free (f);
  1473. }
  1474. /* Return 1 on success */
  1475. int
  1476. edit_save_block_cmd (WEdit *edit)
  1477. {
  1478. long start_mark, end_mark;
  1479. char *exp, *tmp;
  1480. if (eval_marks (edit, &start_mark, &end_mark))
  1481. return 1;
  1482. tmp = g_strconcat (home_dir, PATH_SEP_STR CLIP_FILE, (char *) NULL);
  1483. exp =
  1484. input_expand_dialog (_(" Save Block "), _(" Enter file name: "),
  1485. MC_HISTORY_EDIT_SAVE_BLOCK,
  1486. tmp);
  1487. g_free(tmp);
  1488. edit_push_action (edit, KEY_PRESS + edit->start_display);
  1489. if (exp) {
  1490. if (!*exp) {
  1491. g_free (exp);
  1492. return 0;
  1493. } else {
  1494. if (edit_save_block (edit, exp, start_mark, end_mark)) {
  1495. g_free (exp);
  1496. edit->force |= REDRAW_COMPLETELY;
  1497. return 1;
  1498. } else {
  1499. g_free (exp);
  1500. edit_error_dialog (_(" Save Block "),
  1501. get_sys_error (_
  1502. (" Cannot save file. ")));
  1503. }
  1504. }
  1505. }
  1506. edit->force |= REDRAW_COMPLETELY;
  1507. return 0;
  1508. }
  1509. /* returns 1 on success */
  1510. int
  1511. edit_insert_file_cmd (WEdit *edit)
  1512. {
  1513. gchar *tmp;
  1514. char *exp;
  1515. tmp = g_strconcat (home_dir, PATH_SEP_STR CLIP_FILE, (char *) NULL);
  1516. exp = input_expand_dialog (_(" Insert File "), _(" Enter file name: "),
  1517. MC_HISTORY_EDIT_INSERT_FILE,
  1518. tmp);
  1519. g_free(tmp);
  1520. edit_push_action (edit, KEY_PRESS + edit->start_display);
  1521. if (exp) {
  1522. if (!*exp) {
  1523. g_free (exp);
  1524. return 0;
  1525. } else {
  1526. if (edit_insert_file (edit, exp)) {
  1527. g_free (exp);
  1528. edit->force |= REDRAW_COMPLETELY;
  1529. return 1;
  1530. } else {
  1531. g_free (exp);
  1532. edit_error_dialog (_(" Insert File "),
  1533. get_sys_error (_
  1534. (" Cannot insert file. ")));
  1535. }
  1536. }
  1537. }
  1538. edit->force |= REDRAW_COMPLETELY;
  1539. return 0;
  1540. }
  1541. /* sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
  1542. int edit_sort_cmd (WEdit * edit)
  1543. {
  1544. static char *old = 0;
  1545. char *exp, *tmp;
  1546. long start_mark, end_mark;
  1547. int e;
  1548. if (eval_marks (edit, &start_mark, &end_mark)) {
  1549. edit_error_dialog (_(" Sort block "), _(" You must first highlight a block of text. "));
  1550. return 0;
  1551. }
  1552. tmp = g_strconcat (home_dir, PATH_SEP_STR BLOCK_FILE, (char *) NULL);
  1553. edit_save_block (edit, tmp, start_mark, end_mark);
  1554. g_free(tmp);
  1555. exp = input_dialog (_(" Run Sort "),
  1556. _(" Enter sort options (see manpage) separated by whitespace: "),
  1557. MC_HISTORY_EDIT_SORT, (old != NULL) ? old : "");
  1558. if (!exp)
  1559. return 1;
  1560. g_free (old);
  1561. old = exp;
  1562. tmp = g_strconcat (" sort ", exp, " ", home_dir, PATH_SEP_STR BLOCK_FILE, " > ", home_dir, PATH_SEP_STR TEMP_FILE, (char *) NULL);
  1563. e = system (tmp);
  1564. g_free(tmp);
  1565. if (e) {
  1566. if (e == -1 || e == 127) {
  1567. edit_error_dialog (_(" Sort "),
  1568. get_sys_error (_(" Cannot execute sort command ")));
  1569. } else {
  1570. char q[8];
  1571. sprintf (q, "%d ", e);
  1572. tmp = g_strconcat (_(" Sort returned non-zero: "), q, (char *) NULL);
  1573. edit_error_dialog (_(" Sort "), tmp);
  1574. g_free(tmp);
  1575. }
  1576. return -1;
  1577. }
  1578. edit->force |= REDRAW_COMPLETELY;
  1579. if (edit_block_delete_cmd (edit))
  1580. return 1;
  1581. tmp = g_strconcat (home_dir, PATH_SEP_STR TEMP_FILE, (char *) NULL);
  1582. edit_insert_file (edit, tmp);
  1583. g_free(tmp);
  1584. return 0;
  1585. }
  1586. /*
  1587. * Ask user for a command, execute it and paste its output back to the
  1588. * editor.
  1589. */
  1590. int
  1591. edit_ext_cmd (WEdit *edit)
  1592. {
  1593. char *exp, *tmp;
  1594. int e;
  1595. exp =
  1596. input_dialog (_("Paste output of external command"),
  1597. _("Enter shell command(s):"),
  1598. MC_HISTORY_EDIT_PASTE_EXTCMD, NULL);
  1599. if (!exp)
  1600. return 1;
  1601. tmp = g_strconcat (exp, " > ", home_dir, PATH_SEP_STR TEMP_FILE, (char *) NULL);
  1602. e = system (tmp);
  1603. g_free(tmp);
  1604. g_free (exp);
  1605. if (e) {
  1606. edit_error_dialog (_("External command"),
  1607. get_sys_error (_("Cannot execute command")));
  1608. return -1;
  1609. }
  1610. edit->force |= REDRAW_COMPLETELY;
  1611. tmp = g_strconcat (home_dir, PATH_SEP_STR TEMP_FILE, (char *) NULL);
  1612. edit_insert_file (edit, tmp);
  1613. g_free(tmp);
  1614. return 0;
  1615. }
  1616. /* if block is 1, a block must be highlighted and the shell command
  1617. processes it. If block is 0 the shell command is a straight system
  1618. command, that just produces some output which is to be inserted */
  1619. void
  1620. edit_block_process_cmd (WEdit *edit, const char *shell_cmd, int block)
  1621. {
  1622. long start_mark, end_mark;
  1623. char buf[BUFSIZ];
  1624. FILE *script_home = NULL;
  1625. FILE *script_src = NULL;
  1626. FILE *block_file = NULL;
  1627. gchar *o, *h, *b, *tmp;
  1628. char *quoted_name = NULL;
  1629. o = g_strconcat (mc_home, shell_cmd, (char *) NULL); /* original source script */
  1630. h = g_strconcat (home_dir, PATH_SEP_STR EDIT_DIR, shell_cmd, (char *) NULL); /* home script */
  1631. b = g_strconcat (home_dir, PATH_SEP_STR BLOCK_FILE, (char *) NULL); /* block file */
  1632. if (!(script_home = fopen (h, "r"))) {
  1633. if (!(script_home = fopen (h, "w"))) {
  1634. tmp = g_strconcat (_("Error creating script:"), h, (char *) NULL);
  1635. edit_error_dialog ("", get_sys_error (tmp));
  1636. g_free(tmp);
  1637. goto edit_block_process_cmd__EXIT;
  1638. }
  1639. if (!(script_src = fopen (o, "r"))) {
  1640. fclose (script_home);
  1641. unlink (h);
  1642. tmp = g_strconcat (_("Error reading script:"), o, (char *) NULL);
  1643. edit_error_dialog ("", get_sys_error (tmp));
  1644. g_free(tmp);
  1645. goto edit_block_process_cmd__EXIT;
  1646. }
  1647. while (fgets (buf, sizeof (buf), script_src))
  1648. fputs (buf, script_home);
  1649. if (fclose (script_home)) {
  1650. tmp = g_strconcat (_("Error closing script:"), h, (char *) NULL);
  1651. edit_error_dialog ("", get_sys_error (tmp));
  1652. g_free(tmp);
  1653. goto edit_block_process_cmd__EXIT;
  1654. }
  1655. chmod (h, 0700);
  1656. tmp = g_strconcat (_("Script created:"), h, (char *) NULL);
  1657. edit_error_dialog ("", get_sys_error (tmp));
  1658. g_free(tmp);
  1659. }
  1660. open_error_pipe ();
  1661. if (block) { /* for marked block run indent formatter */
  1662. if (eval_marks (edit, &start_mark, &end_mark)) {
  1663. edit_error_dialog (_("Process block"),
  1664. _
  1665. (" You must first highlight a block of text. "));
  1666. goto edit_block_process_cmd__EXIT;
  1667. }
  1668. edit_save_block (edit, b, start_mark, end_mark);
  1669. quoted_name = name_quote (edit->filename, 0);
  1670. /*
  1671. * Run script.
  1672. * Initial space is to avoid polluting bash history.
  1673. * Arguments:
  1674. * $1 - name of the edited file (to check its extension etc).
  1675. * $2 - file containing the current block.
  1676. * $3 - file where error messages should be put
  1677. * (for compatibility with old scripts).
  1678. */
  1679. tmp = g_strconcat (" ", home_dir, PATH_SEP_STR EDIT_DIR, shell_cmd, " ", quoted_name,
  1680. " ", home_dir, PATH_SEP_STR BLOCK_FILE " /dev/null", (char *) NULL);
  1681. system (tmp);
  1682. g_free(tmp);
  1683. } else {
  1684. /*
  1685. * No block selected, just execute the command for the file.
  1686. * Arguments:
  1687. * $1 - name of the edited file.
  1688. */
  1689. tmp = g_strconcat (" ", home_dir, PATH_SEP_STR EDIT_DIR, shell_cmd, " ",
  1690. quoted_name, (char *) NULL);
  1691. system (tmp);
  1692. g_free(tmp);
  1693. }
  1694. g_free (quoted_name);
  1695. close_error_pipe (D_NORMAL, NULL);
  1696. edit_refresh_cmd (edit);
  1697. edit->force |= REDRAW_COMPLETELY;
  1698. /* insert result block */
  1699. if (block) {
  1700. if (edit_block_delete_cmd (edit))
  1701. goto edit_block_process_cmd__EXIT;
  1702. edit_insert_file (edit, b);
  1703. if ((block_file = fopen (b, "w")))
  1704. fclose (block_file);
  1705. goto edit_block_process_cmd__EXIT;
  1706. }
  1707. edit_block_process_cmd__EXIT:
  1708. g_free(b);
  1709. g_free(h);
  1710. g_free(o);
  1711. return;
  1712. }
  1713. /* prints at the cursor */
  1714. /* returns the number of chars printed */
  1715. int edit_print_string (WEdit * e, const char *s)
  1716. {
  1717. int i = 0;
  1718. while (s[i])
  1719. edit_execute_cmd (e, -1, (unsigned char) s[i++]);
  1720. e->force |= REDRAW_COMPLETELY;
  1721. edit_update_screen (e);
  1722. return i;
  1723. }
  1724. static void pipe_mail (WEdit *edit, char *to, char *subject, char *cc)
  1725. {
  1726. FILE *p = 0;
  1727. char *s;
  1728. to = name_quote (to, 0);
  1729. subject = name_quote (subject, 0);
  1730. cc = name_quote (cc, 0);
  1731. s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "" , cc, " ", to, (char *) NULL);
  1732. g_free (to);
  1733. g_free (subject);
  1734. g_free (cc);
  1735. if (s) {
  1736. p = popen (s, "w");
  1737. g_free (s);
  1738. }
  1739. if (p) {
  1740. long i;
  1741. for (i = 0; i < edit->last_byte; i++)
  1742. fputc (edit_get_byte (edit, i), p);
  1743. pclose (p);
  1744. }
  1745. }
  1746. #define MAIL_DLG_HEIGHT 12
  1747. void edit_mail_dialog (WEdit * edit)
  1748. {
  1749. char *tmail_to;
  1750. char *tmail_subject;
  1751. char *tmail_cc;
  1752. static char *mail_cc_last = 0;
  1753. static char *mail_subject_last = 0;
  1754. static char *mail_to_last = 0;
  1755. QuickDialog Quick_input =
  1756. {50, MAIL_DLG_HEIGHT, -1, 0, N_(" Mail "),
  1757. "[Input Line Keys]", 0, 0};
  1758. QuickWidget quick_widgets[] =
  1759. {
  1760. {quick_button, 6, 10, 9, MAIL_DLG_HEIGHT, N_("&Cancel"), 0, B_CANCEL, 0,
  1761. 0, NULL, NULL, NULL},
  1762. {quick_button, 2, 10, 9, MAIL_DLG_HEIGHT, N_("&OK"), 0, B_ENTER, 0,
  1763. 0, NULL, NULL, NULL},
  1764. {quick_input, 3, 50, 8, MAIL_DLG_HEIGHT, "", 44, 0, 0,
  1765. 0, "mail-dlg-input", NULL, NULL},
  1766. {quick_label, 2, 50, 7, MAIL_DLG_HEIGHT, N_(" Copies to"), 0, 0, 0,
  1767. 0, 0, NULL, NULL},
  1768. {quick_input, 3, 50, 6, MAIL_DLG_HEIGHT, "", 44, 0, 0,
  1769. 0, "mail-dlg-input-2", NULL, NULL},
  1770. {quick_label, 2, 50, 5, MAIL_DLG_HEIGHT, N_(" Subject"), 0, 0, 0,
  1771. 0, 0, NULL, NULL},
  1772. {quick_input, 3, 50, 4, MAIL_DLG_HEIGHT, "", 44, 0, 0,
  1773. 0, "mail-dlg-input-3", NULL, NULL},
  1774. {quick_label, 2, 50, 3, MAIL_DLG_HEIGHT, N_(" To"), 0, 0, 0,
  1775. 0, 0, NULL, NULL},
  1776. {quick_label, 2, 50, 2, MAIL_DLG_HEIGHT, N_(" mail -s <subject> -c <cc> <to>"), 0, 0, 0,
  1777. 0, 0, NULL, NULL},
  1778. NULL_QuickWidget};
  1779. quick_widgets[2].str_result = &tmail_cc;
  1780. quick_widgets[2].text = mail_cc_last ? mail_cc_last : "";
  1781. quick_widgets[4].str_result = &tmail_subject;
  1782. quick_widgets[4].text = mail_subject_last ? mail_subject_last : "";
  1783. quick_widgets[6].str_result = &tmail_to;
  1784. quick_widgets[6].text = mail_to_last ? mail_to_last : "";
  1785. Quick_input.widgets = quick_widgets;
  1786. if (quick_dialog (&Quick_input) != B_CANCEL) {
  1787. g_free (mail_cc_last);
  1788. g_free (mail_subject_last);
  1789. g_free (mail_to_last);
  1790. mail_cc_last = tmail_cc;
  1791. mail_subject_last = tmail_subject;
  1792. mail_to_last = tmail_to;
  1793. pipe_mail (edit, mail_to_last, mail_subject_last, mail_cc_last);
  1794. }
  1795. }
  1796. /*******************/
  1797. /* Word Completion */
  1798. /*******************/
  1799. /* find first character of current word */
  1800. static int edit_find_word_start (WEdit *edit, long *word_start, int *word_len)
  1801. {
  1802. int i, c, last;
  1803. /* return if at begin of file */
  1804. if (edit->curs1 <= 0)
  1805. return 0;
  1806. c = (unsigned char) edit_get_byte (edit, edit->curs1 - 1);
  1807. /* return if not at end or in word */
  1808. if (isspace (c) || !(isalnum (c) || c == '_'))
  1809. return 0;
  1810. /* search start of word to be completed */
  1811. for (i = 2;; i++) {
  1812. /* return if at begin of file */
  1813. if (edit->curs1 - i < 0)
  1814. return 0;
  1815. last = c;
  1816. c = (unsigned char) edit_get_byte (edit, edit->curs1 - i);
  1817. if (!(isalnum (c) || c == '_')) {
  1818. /* return if word starts with digit */
  1819. if (isdigit (last))
  1820. return 0;
  1821. *word_start = edit->curs1 - (i - 1); /* start found */
  1822. *word_len = i - 1;
  1823. break;
  1824. }
  1825. }
  1826. /* success */
  1827. return 1;
  1828. }
  1829. /* (re)set search parameters to the given values */
  1830. static void edit_set_search_parameters (WEdit *edit, int replace_mode, int rb, int rc)
  1831. {
  1832. edit->replace_mode = replace_mode;
  1833. edit->replace_backwards = rb;
  1834. edit->replace_case = rc;
  1835. }
  1836. #define MAX_WORD_COMPLETIONS 100 /* in listbox */
  1837. /* collect the possible completions */
  1838. static int
  1839. edit_collect_completions (WEdit *edit, long start, int word_len,
  1840. char *match_expr, struct selection *compl,
  1841. int *num)
  1842. {
  1843. int len = 0, max_len = 0, i, skip;
  1844. unsigned char *bufpos;
  1845. (void) match_expr;
  1846. /* collect max MAX_WORD_COMPLETIONS completions */
  1847. while (*num < MAX_WORD_COMPLETIONS) {
  1848. /* get next match */
  1849. /*
  1850. start =
  1851. edit_find (start - 1, (unsigned char *) match_expr, &len,
  1852. edit->last_byte, edit_get_byte_ptr, (void *) edit, 0);
  1853. */
  1854. start = -1;
  1855. /* not matched */
  1856. if (start < 0)
  1857. break;
  1858. /* add matched completion if not yet added */
  1859. bufpos =
  1860. &edit->
  1861. buffers1[start >> S_EDIT_BUF_SIZE][start & M_EDIT_BUF_SIZE];
  1862. skip = 0;
  1863. for (i = 0; i < *num; i++) {
  1864. if (strncmp
  1865. ((char *) &compl[i].text[word_len],
  1866. (char *) &bufpos[word_len], max (len,
  1867. compl[i].len) -
  1868. word_len) == 0) {
  1869. skip = 1;
  1870. break; /* skip it, already added */
  1871. }
  1872. }
  1873. if (skip)
  1874. continue;
  1875. compl[*num].text = g_malloc (len + 1);
  1876. compl[*num].len = len;
  1877. for (i = 0; i < len; i++)
  1878. compl[*num].text[i] = *(bufpos + i);
  1879. compl[*num].text[i] = '\0';
  1880. (*num)++;
  1881. /* note the maximal length needed for the completion dialog */
  1882. if (len > max_len)
  1883. max_len = len;
  1884. }
  1885. return max_len;
  1886. }
  1887. /*
  1888. * Complete current word using regular expression search
  1889. * backwards beginning at the current cursor position.
  1890. */
  1891. void
  1892. edit_complete_word_cmd (WEdit *edit)
  1893. {
  1894. int word_len = 0, i, num_compl = 0, max_len;
  1895. long word_start = 0;
  1896. unsigned char *bufpos;
  1897. char *match_expr;
  1898. struct selection compl[MAX_WORD_COMPLETIONS]; /* completions */
  1899. /* don't want to disturb another search */
  1900. int old_replace_mode = edit->replace_mode;
  1901. int old_rb = edit->replace_backwards;
  1902. int old_rc = edit->replace_case;
  1903. /* search start of word to be completed */
  1904. if (!edit_find_word_start (edit, &word_start, &word_len))
  1905. return;
  1906. /* prepare match expression */
  1907. bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE]
  1908. [word_start & M_EDIT_BUF_SIZE];
  1909. match_expr = g_strdup_printf ("%.*s[a-zA-Z_0-9]+", word_len, bufpos);
  1910. /* init search: backward, regexp, whole word, case sensitive */
  1911. edit_set_search_parameters (edit, 0, 1, 1);
  1912. /* collect the possible completions */
  1913. /* start search from curs1 down to begin of file */
  1914. max_len =
  1915. edit_collect_completions (edit, word_start, word_len, match_expr,
  1916. (struct selection *) &compl, &num_compl);
  1917. if (num_compl > 0) {
  1918. /* insert completed word if there is only one match */
  1919. if (num_compl == 1) {
  1920. for (i = word_len; i < compl[0].len; i++)
  1921. edit_insert (edit, *(compl[0].text + i));
  1922. }
  1923. /* more than one possible completion => ask the user */
  1924. else {
  1925. /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
  1926. /* !!! pressed again the selection dialog pops up, but that !!! */
  1927. /* !!! seems to require a further internal state !!! */
  1928. /*beep (); */
  1929. /* let the user select the preferred completion */
  1930. editcmd_dialog_completion_show (edit, max_len, word_len,
  1931. (struct selection *) &compl,
  1932. num_compl);
  1933. }
  1934. }
  1935. g_free (match_expr);
  1936. /* release memory before return */
  1937. for (i = 0; i < num_compl; i++)
  1938. g_free (compl[i].text);
  1939. /* restore search parameters */
  1940. edit_set_search_parameters (edit, old_replace_mode, old_rb, old_rc);
  1941. }
  1942. void
  1943. edit_select_codepage_cmd (WEdit *edit)
  1944. {
  1945. #ifdef HAVE_CHARSET
  1946. do_select_codepage ();
  1947. if ( get_codepage_id (source_codepage) )
  1948. edit->utf8 = str_isutf8 (get_codepage_id (source_codepage));
  1949. edit->force = REDRAW_COMPLETELY;
  1950. edit_refresh_cmd (edit);
  1951. #endif
  1952. }
  1953. void
  1954. edit_insert_literal_cmd (WEdit *edit)
  1955. {
  1956. int char_for_insertion =
  1957. editcmd_dialog_raw_key_query (_(" Insert Literal "),
  1958. _(" Press any key: "), 0);
  1959. edit_execute_key_command (edit, -1,
  1960. ascii_alpha_to_cntrl (char_for_insertion));
  1961. }
  1962. void
  1963. edit_execute_macro_cmd (WEdit *edit)
  1964. {
  1965. int command =
  1966. CK_Macro (editcmd_dialog_raw_key_query
  1967. (_(" Execute Macro "), _(" Press macro hotkey: "),
  1968. 1));
  1969. if (command == CK_Macro (0))
  1970. command = CK_Insert_Char;
  1971. edit_execute_key_command (edit, command, -1);
  1972. }
  1973. void
  1974. edit_begin_end_macro_cmd(WEdit *edit)
  1975. {
  1976. int command;
  1977. /* edit is a pointer to the widget */
  1978. if (edit) {
  1979. command =
  1980. edit->macro_i <
  1981. 0 ? CK_Begin_Record_Macro : CK_End_Record_Macro;
  1982. edit_execute_key_command (edit, command, -1);
  1983. }
  1984. }
  1985. int
  1986. edit_load_forward_cmd (WEdit *edit)
  1987. {
  1988. if (edit->modified) {
  1989. if (edit_query_dialog2
  1990. (_("Warning"),
  1991. _(" Current text was modified without a file save. \n"
  1992. " Continue discards these changes. "), _("C&ontinue"),
  1993. _("&Cancel"))) {
  1994. edit->force |= REDRAW_COMPLETELY;
  1995. return 0;
  1996. }
  1997. }
  1998. if ( edit_stack_iterator + 1 < MAX_HISTORY_MOVETO ) {
  1999. if ( edit_history_moveto[edit_stack_iterator + 1].line < 1 ) {
  2000. return 1;
  2001. }
  2002. edit_stack_iterator++;
  2003. if ( edit_history_moveto[edit_stack_iterator].filename ) {
  2004. edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename,
  2005. edit_history_moveto[edit_stack_iterator].line);
  2006. return 0;
  2007. } else {
  2008. return 1;
  2009. }
  2010. } else {
  2011. return 1;
  2012. }
  2013. }
  2014. int
  2015. edit_load_back_cmd (WEdit *edit)
  2016. {
  2017. if (edit->modified) {
  2018. if (edit_query_dialog2
  2019. (_("Warning"),
  2020. _(" Current text was modified without a file save. \n"
  2021. " Continue discards these changes. "), _("C&ontinue"),
  2022. _("&Cancel"))) {
  2023. edit->force |= REDRAW_COMPLETELY;
  2024. return 0;
  2025. }
  2026. }
  2027. if ( edit_stack_iterator > 0 ) {
  2028. edit_stack_iterator--;
  2029. if ( edit_history_moveto[edit_stack_iterator].filename ) {
  2030. edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename,
  2031. edit_history_moveto[edit_stack_iterator].line);
  2032. return 0;
  2033. } else {
  2034. return 1;
  2035. }
  2036. } else {
  2037. return 1;
  2038. }
  2039. }
  2040. void
  2041. edit_get_match_keyword_cmd (WEdit *edit)
  2042. {
  2043. int word_len = 0;
  2044. int num_def = 0;
  2045. int max_len = 0;
  2046. int i;
  2047. long word_start = 0;
  2048. unsigned char *bufpos;
  2049. char *match_expr;
  2050. char *path = NULL;
  2051. char *ptr = NULL;
  2052. char *tagfile = NULL;
  2053. etags_hash_t def_hash[MAX_DEFINITIONS];
  2054. for ( i = 0; i < MAX_DEFINITIONS; i++) {
  2055. def_hash[i].filename = NULL;
  2056. }
  2057. /* search start of word to be completed */
  2058. if (!edit_find_word_start (edit, &word_start, &word_len))
  2059. return;
  2060. /* prepare match expression */
  2061. bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE]
  2062. [word_start & M_EDIT_BUF_SIZE];
  2063. match_expr = g_strdup_printf ("%.*s", word_len, bufpos);
  2064. ptr = g_get_current_dir ();
  2065. path = g_strconcat (ptr, G_DIR_SEPARATOR_S, (char *) NULL);
  2066. g_free (ptr);
  2067. /* Recursive search file 'TAGS' in parent dirs */
  2068. do {
  2069. ptr = g_path_get_dirname (path);
  2070. g_free(path); path = ptr;
  2071. g_free (tagfile);
  2072. tagfile = g_build_filename (path, TAGS_NAME, (char *) NULL);
  2073. if ( exist_file (tagfile) )
  2074. break;
  2075. } while (strcmp( path, G_DIR_SEPARATOR_S) != 0);
  2076. if (tagfile){
  2077. num_def = etags_set_definition_hash(tagfile, path, match_expr, (etags_hash_t *) &def_hash);
  2078. g_free (tagfile);
  2079. }
  2080. g_free (path);
  2081. max_len = MAX_WIDTH_DEF_DIALOG;
  2082. word_len = 0;
  2083. if ( num_def > 0 ) {
  2084. editcmd_dialog_select_definition_show (edit, match_expr, max_len, word_len,
  2085. (etags_hash_t *) &def_hash,
  2086. num_def);
  2087. }
  2088. g_free (match_expr);
  2089. }