editcmd.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883
  1. /* editor high level editing commands.
  2. Copyright (C) 1996, 1997 the Free Software Foundation
  3. Authors: 1996, 1997 Paul Sheer
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307, USA.
  16. */
  17. /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
  18. #include <config.h>
  19. #include <ctype.h>
  20. #include "edit.h"
  21. #include "editlock.h"
  22. #include "editcmddef.h"
  23. #include "edit-widget.h"
  24. #include "../src/color.h" /* dialog_colors */
  25. #include "../src/tty.h" /* LINES */
  26. #include "../src/widget.h" /* listbox_new() */
  27. #include "../src/layout.h" /* clr_scr() */
  28. #include "../src/main.h" /* mc_home */
  29. #include "../src/help.h" /* interactive_display() */
  30. #include "../src/key.h" /* XCTRL */
  31. #include "../src/dialog.h" /* do_refresh() */
  32. #include "../src/wtools.h" /* message() */
  33. #include "../src/charsets.h"
  34. #define edit_get_load_file(f,h) input_expand_dialog (h, _(" Enter file name: "), f)
  35. #define edit_get_save_file(f,h) input_expand_dialog (h, _(" Enter file name: "), f)
  36. struct selection {
  37. unsigned char * text;
  38. int len;
  39. };
  40. /* globals: */
  41. /* search and replace: */
  42. static int replace_scanf = 0;
  43. static int replace_regexp = 0;
  44. static int replace_all = 0;
  45. static int replace_prompt = 1;
  46. static int replace_whole = 0;
  47. static int replace_case = 0;
  48. static int replace_backwards = 0;
  49. static int search_create_bookmark = 0;
  50. /* queries on a save */
  51. int edit_confirm_save = 1;
  52. #define NUM_REPL_ARGS 64
  53. #define MAX_REPL_LEN 1024
  54. static int edit_save_cmd (WEdit *edit);
  55. static unsigned char *edit_get_block (WEdit *edit, long start,
  56. long finish, int *l);
  57. static inline int my_lower_case (int c)
  58. {
  59. return tolower(c & 0xFF);
  60. }
  61. static const char *strcasechr (const unsigned char *s, int c)
  62. {
  63. for (c = my_lower_case (c); my_lower_case ((int) *s) != c; ++s)
  64. if (*s == '\0')
  65. return 0;
  66. return (const char *) s;
  67. }
  68. #ifndef HAVE_MEMMOVE
  69. /* for Christophe */
  70. static void *memmove (void *dest, const void *src, size_t n)
  71. {
  72. char *t;
  73. const char *s;
  74. if (dest <= src) {
  75. t = (char *) dest;
  76. s = (const char *) src;
  77. while (n--)
  78. *t++ = *s++;
  79. } else {
  80. t = (char *) dest + n;
  81. s = (const char *) src + n;
  82. while (n--)
  83. *--t = *--s;
  84. }
  85. return dest;
  86. }
  87. #endif /* !HAVE_MEMMOVE */
  88. /* #define itoa MY_itoa <---- this line is now in edit.h */
  89. static char *
  90. MY_itoa (int i)
  91. {
  92. static char t[14];
  93. char *s = t + 13;
  94. int j = i;
  95. *s-- = 0;
  96. do {
  97. *s-- = i % 10 + '0';
  98. } while ((i = i / 10));
  99. if (j < 0)
  100. *s-- = '-';
  101. return ++s;
  102. }
  103. /* Temporary strings */
  104. static char *stacked[16];
  105. /*
  106. This joins strings end on end and allocates memory for the result.
  107. The result is later automatically free'd and must not be free'd
  108. by the caller.
  109. */
  110. static const char *
  111. catstrs (const char *first,...)
  112. {
  113. static int i = 0;
  114. va_list ap;
  115. int len;
  116. char *data;
  117. if (!first)
  118. return 0;
  119. len = strlen (first);
  120. va_start (ap, first);
  121. while ((data = va_arg (ap, char *)) != 0)
  122. len += strlen (data);
  123. len++;
  124. i = (i + 1) % 16;
  125. g_free (stacked[i]);
  126. stacked[i] = g_malloc (len);
  127. va_end (ap);
  128. va_start (ap, first);
  129. strcpy (stacked[i], first);
  130. while ((data = va_arg (ap, char *)) != 0)
  131. strcat (stacked[i], data);
  132. va_end (ap);
  133. return stacked[i];
  134. }
  135. /* Free temporary strings */
  136. void freestrs(void)
  137. {
  138. size_t i;
  139. for (i = 0; i < sizeof(stacked) / sizeof(stacked[0]); i++) {
  140. g_free (stacked[i]);
  141. stacked[i] = NULL;
  142. }
  143. }
  144. void edit_help_cmd (WEdit * edit)
  145. {
  146. interactive_display (NULL, "[Internal File Editor]");
  147. edit->force |= REDRAW_COMPLETELY;
  148. }
  149. void edit_refresh_cmd (WEdit * edit)
  150. {
  151. #ifndef HAVE_SLANG
  152. clr_scr();
  153. do_refresh();
  154. #else
  155. {
  156. int color;
  157. edit_get_syntax_color (edit, -1, &color);
  158. }
  159. touchwin(stdscr);
  160. #endif /* !HAVE_SLANG */
  161. mc_refresh();
  162. doupdate();
  163. }
  164. /* If 0 (quick save) then a) create/truncate <filename> file,
  165. b) save to <filename>;
  166. if 1 (safe save) then a) save to <tempnam>,
  167. b) rename <tempnam> to <filename>;
  168. if 2 (do backups) then a) save to <tempnam>,
  169. b) rename <filename> to <filename.backup_ext>,
  170. c) rename <tempnam> to <filename>. */
  171. /* returns 0 on error */
  172. static int
  173. edit_save_file (WEdit *edit, const char *filename)
  174. {
  175. char *p;
  176. long filelen = 0;
  177. char *savename = 0;
  178. int this_save_mode, fd;
  179. if (!filename)
  180. return 0;
  181. if (!*filename)
  182. return 0;
  183. if (*filename != PATH_SEP && edit->dir) {
  184. savename = concat_dir_and_file (edit->dir, filename);
  185. filename = catstrs (savename, (char *) NULL);
  186. g_free (savename);
  187. }
  188. if (!vfs_file_is_local (filename) ||
  189. (fd = mc_open (filename, O_WRONLY | O_BINARY)) == -1) {
  190. /*
  191. * The file does not exists yet, so no safe save or
  192. * backup are necessary.
  193. */
  194. this_save_mode = EDIT_QUICK_SAVE;
  195. } else {
  196. mc_close (fd);
  197. this_save_mode = option_save_mode;
  198. }
  199. if (this_save_mode != EDIT_QUICK_SAVE) {
  200. char *savedir, *saveprefix;
  201. const char *slashpos;
  202. slashpos = strrchr (filename, PATH_SEP);
  203. if (slashpos) {
  204. savedir = g_strdup (filename);
  205. savedir[slashpos - filename + 1] = '\0';
  206. } else
  207. savedir = g_strdup (".");
  208. saveprefix = concat_dir_and_file (savedir, "cooledit");
  209. g_free (savedir);
  210. fd = mc_mkstemps (&savename, saveprefix, NULL);
  211. g_free (saveprefix);
  212. if (!savename)
  213. return 0;
  214. /* FIXME:
  215. * Close for now because mc_mkstemps use pure open system call
  216. * to create temporary file and it needs to be reopened by
  217. * VFS-aware mc_open().
  218. */
  219. close (fd);
  220. } else
  221. savename = g_strdup (filename);
  222. mc_chown (savename, edit->stat1.st_uid, edit->stat1.st_gid);
  223. mc_chmod (savename, edit->stat1.st_mode);
  224. if ((fd =
  225. mc_open (savename, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY,
  226. edit->stat1.st_mode)) == -1)
  227. goto error_save;
  228. /* pipe save */
  229. if ((p = edit_get_write_filter (savename, filename))) {
  230. FILE *file;
  231. mc_close (fd);
  232. file = (FILE *) popen (p, "w");
  233. if (file) {
  234. filelen = edit_write_stream (edit, file);
  235. #if 1
  236. pclose (file);
  237. #else
  238. if (pclose (file) != 0) {
  239. edit_error_dialog (_("Error"),
  240. catstrs (_(" Error writing to pipe: "),
  241. p, " ", (char *) NULL));
  242. g_free (p);
  243. goto error_save;
  244. }
  245. #endif
  246. } else {
  247. edit_error_dialog (_("Error"),
  248. get_sys_error (catstrs
  249. (_
  250. (" Cannot open pipe for writing: "),
  251. p, " ", (char *) NULL)));
  252. g_free (p);
  253. goto error_save;
  254. }
  255. g_free (p);
  256. } else {
  257. long buf;
  258. buf = 0;
  259. filelen = edit->last_byte;
  260. while (buf <= (edit->curs1 >> S_EDIT_BUF_SIZE) - 1) {
  261. if (mc_write (fd, (char *) edit->buffers1[buf], EDIT_BUF_SIZE)
  262. != EDIT_BUF_SIZE) {
  263. mc_close (fd);
  264. goto error_save;
  265. }
  266. buf++;
  267. }
  268. if (mc_write
  269. (fd, (char *) edit->buffers1[buf],
  270. edit->curs1 & M_EDIT_BUF_SIZE) !=
  271. (edit->curs1 & M_EDIT_BUF_SIZE)) {
  272. filelen = -1;
  273. } else if (edit->curs2) {
  274. edit->curs2--;
  275. buf = (edit->curs2 >> S_EDIT_BUF_SIZE);
  276. if (mc_write
  277. (fd,
  278. (char *) edit->buffers2[buf] + EDIT_BUF_SIZE -
  279. (edit->curs2 & M_EDIT_BUF_SIZE) - 1,
  280. 1 + (edit->curs2 & M_EDIT_BUF_SIZE)) !=
  281. 1 + (edit->curs2 & M_EDIT_BUF_SIZE)) {
  282. filelen = -1;
  283. } else {
  284. while (--buf >= 0) {
  285. if (mc_write
  286. (fd, (char *) edit->buffers2[buf],
  287. EDIT_BUF_SIZE) != EDIT_BUF_SIZE) {
  288. filelen = -1;
  289. break;
  290. }
  291. }
  292. }
  293. edit->curs2++;
  294. }
  295. if (mc_close (fd))
  296. goto error_save;
  297. }
  298. if (filelen != edit->last_byte)
  299. goto error_save;
  300. if (this_save_mode == EDIT_DO_BACKUP)
  301. if (mc_rename (filename, catstrs (filename, option_backup_ext, (char *) NULL))
  302. == -1)
  303. goto error_save;
  304. if (this_save_mode != EDIT_QUICK_SAVE)
  305. if (mc_rename (savename, filename) == -1)
  306. goto error_save;
  307. g_free (savename);
  308. return 1;
  309. error_save:
  310. /* FIXME: Is this safe ?
  311. * if (this_save_mode != EDIT_QUICK_SAVE)
  312. * mc_unlink (savename);
  313. */
  314. g_free (savename);
  315. return 0;
  316. }
  317. /*
  318. I changed this from Oleg's original routine so
  319. that option_backup_ext works with coolwidgets as well. This
  320. does mean there is a memory leak - paul.
  321. */
  322. void menu_save_mode_cmd (void)
  323. {
  324. #define DLG_X 38
  325. #define DLG_Y 10
  326. static char *str_result;
  327. static int save_mode_new;
  328. static const char *str[] =
  329. {
  330. N_("Quick save "),
  331. N_("Safe save "),
  332. N_("Do backups -->")};
  333. static QuickWidget widgets[] =
  334. {
  335. {quick_button, 18, DLG_X, 7, DLG_Y, N_("&Cancel"), 0,
  336. B_CANCEL, 0, 0, "c"},
  337. {quick_button, 6, DLG_X, 7, DLG_Y, N_("&OK"), 0,
  338. B_ENTER, 0, 0, "o"},
  339. {quick_input, 23, DLG_X, 5, DLG_Y, 0, 9,
  340. 0, 0, &str_result, "edit-backup-ext"},
  341. {quick_label, 22, DLG_X, 4, DLG_Y, N_("Extension:"), 0,
  342. 0, 0, 0, "savemext"},
  343. {quick_radio, 4, DLG_X, 3, DLG_Y, "", 3,
  344. 0, &save_mode_new, (char **) str, "t"},
  345. NULL_QuickWidget};
  346. static QuickDialog dialog =
  347. {DLG_X, DLG_Y, -1, -1, N_(" Edit Save Mode "), "[Edit Save Mode]",
  348. widgets, 0};
  349. static int i18n_flag = 0;
  350. if (!i18n_flag) {
  351. size_t i;
  352. size_t maxlen = 0;
  353. int dlg_x;
  354. size_t l1;
  355. /* OK/Cancel buttons */
  356. l1 = strlen (_(widgets[0].text)) + strlen (_(widgets[1].text)) + 5;
  357. maxlen = max (maxlen, l1);
  358. for (i = 0; i < 3; i++ ) {
  359. str[i] = _(str[i]);
  360. maxlen = max (maxlen, strlen (str[i]) + 7);
  361. }
  362. i18n_flag = 1;
  363. dlg_x = maxlen + strlen (_(widgets[3].text)) + 5 + 1;
  364. widgets[2].hotkey_pos = strlen (_(widgets[3].text)); /* input field length */
  365. dlg_x = min (COLS, dlg_x);
  366. dialog.xlen = dlg_x;
  367. i = (dlg_x - l1)/3;
  368. widgets[1].relative_x = i;
  369. widgets[0].relative_x = i + strlen (_(widgets[1].text)) + i + 4;
  370. widgets[2].relative_x = widgets[3].relative_x = maxlen + 2;
  371. for (i = 0; i < sizeof (widgets)/sizeof (widgets[0]); i++)
  372. widgets[i].x_divisions = dlg_x;
  373. }
  374. widgets[2].text = option_backup_ext;
  375. widgets[4].value = option_save_mode;
  376. if (quick_dialog (&dialog) != B_ENTER)
  377. return;
  378. option_save_mode = save_mode_new;
  379. option_backup_ext = str_result; /* this is a memory leak */
  380. option_backup_ext_int = 0;
  381. str_result[min (strlen (str_result), sizeof (int))] = '\0';
  382. memcpy (&option_backup_ext_int, str_result, strlen (option_backup_ext));
  383. }
  384. void
  385. edit_set_filename (WEdit *edit, const char *f)
  386. {
  387. g_free (edit->filename);
  388. if (!f)
  389. f = "";
  390. edit->filename = g_strdup (f);
  391. if (edit->dir == NULL && *f != PATH_SEP)
  392. #ifdef USE_VFS
  393. edit->dir = g_strdup (vfs_get_current_dir ());
  394. #else
  395. edit->dir = g_get_current_dir ();
  396. #endif
  397. }
  398. /* Here we want to warn the users of overwriting an existing file,
  399. but only if they have made a change to the filename */
  400. /* returns 1 on success */
  401. int
  402. edit_save_as_cmd (WEdit *edit)
  403. {
  404. /* This heads the 'Save As' dialog box */
  405. char *exp;
  406. int save_lock = 0;
  407. int different_filename = 0;
  408. exp = edit_get_save_file (edit->filename, _(" Save As "));
  409. edit_push_action (edit, KEY_PRESS + edit->start_display);
  410. if (exp) {
  411. if (!*exp) {
  412. g_free (exp);
  413. edit->force |= REDRAW_COMPLETELY;
  414. return 0;
  415. } else {
  416. if (strcmp (edit->filename, exp)) {
  417. int file;
  418. different_filename = 1;
  419. if ((file = mc_open (exp, O_RDONLY | O_BINARY)) != -1) {
  420. /* the file exists */
  421. mc_close (file);
  422. /* Overwrite the current file or cancel the operation */
  423. if (edit_query_dialog2
  424. (_("Warning"),
  425. _(" A file already exists with this name. "),
  426. _("&Overwrite"), _("&Cancel"))) {
  427. edit->force |= REDRAW_COMPLETELY;
  428. g_free (exp);
  429. return 0;
  430. }
  431. }
  432. save_lock = edit_lock_file (exp);
  433. } else {
  434. /* filenames equal, check if already locked */
  435. if (!edit->locked && !edit->delete_file)
  436. save_lock = edit_lock_file (exp);
  437. }
  438. if (edit_save_file (edit, exp)) {
  439. /* Succesful, so unlock both files */
  440. if (strcmp (edit->filename, exp)) {
  441. if (save_lock)
  442. edit_unlock_file (exp);
  443. if (edit->locked)
  444. edit->locked = edit_unlock_file (edit->filename);
  445. } else {
  446. if (edit->locked || save_lock)
  447. edit->locked = edit_unlock_file (edit->filename);
  448. }
  449. edit_set_filename (edit, exp);
  450. g_free (exp);
  451. edit->modified = 0;
  452. edit->delete_file = 0;
  453. if (different_filename)
  454. edit_load_syntax (edit, 0, 0);
  455. edit->force |= REDRAW_COMPLETELY;
  456. return 1;
  457. } else {
  458. /* Failed, so maintain modify (not save) lock */
  459. if (strcmp (edit->filename, exp) && save_lock)
  460. edit_unlock_file (exp);
  461. if (save_lock)
  462. edit->locked = edit_unlock_file (edit->filename);
  463. g_free (exp);
  464. edit_error_dialog (_(" Save As "),
  465. get_sys_error (_
  466. (" Cannot save file. ")));
  467. edit->force |= REDRAW_COMPLETELY;
  468. return 0;
  469. }
  470. }
  471. }
  472. edit->force |= REDRAW_COMPLETELY;
  473. return 0;
  474. }
  475. /* {{{ Macro stuff starts here */
  476. static cb_ret_t
  477. raw_callback (struct Dlg_head *h, dlg_msg_t msg, int parm)
  478. {
  479. switch (msg) {
  480. case DLG_KEY:
  481. h->running = 0;
  482. h->ret_value = parm;
  483. return MSG_HANDLED;
  484. default:
  485. return default_dlg_callback (h, msg, parm);
  486. }
  487. }
  488. /* gets a raw key from the keyboard. Passing cancel = 1 draws
  489. a cancel button thus allowing c-c etc. Alternatively, cancel = 0
  490. will return the next key pressed. ctrl-a (=B_CANCEL), ctrl-g, ctrl-c,
  491. and Esc are cannot returned */
  492. int
  493. edit_raw_key_query (const char *heading, const char *query, int cancel)
  494. {
  495. int w = strlen (query) + 7;
  496. struct Dlg_head *raw_dlg =
  497. create_dlg (0, 0, 7, w, dialog_colors, raw_callback,
  498. NULL, heading,
  499. DLG_CENTER | DLG_TRYUP | DLG_WANT_TAB);
  500. add_widget (raw_dlg,
  501. input_new (3 - cancel, w - 5, INPUT_COLOR, 2, "", 0));
  502. add_widget (raw_dlg, label_new (3 - cancel, 2, query));
  503. if (cancel)
  504. add_widget (raw_dlg,
  505. button_new (4, w / 2 - 5, B_CANCEL, NORMAL_BUTTON,
  506. _("Cancel"), 0));
  507. run_dlg (raw_dlg);
  508. w = raw_dlg->ret_value;
  509. destroy_dlg (raw_dlg);
  510. if (cancel) {
  511. if (w == XCTRL ('g') || w == XCTRL ('c') || w == ESC_CHAR
  512. || w == B_CANCEL)
  513. return 0;
  514. }
  515. return w;
  516. }
  517. /* creates a macro file if it doesn't exist */
  518. static FILE *edit_open_macro_file (const char *r)
  519. {
  520. const char *filename;
  521. int file;
  522. filename = catstrs (home_dir, MACRO_FILE, (char *) NULL);
  523. if ((file = open (filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
  524. return 0;
  525. close (file);
  526. return fopen (filename, r);
  527. }
  528. #define MAX_MACROS 1024
  529. static int saved_macro[MAX_MACROS + 1];
  530. static int saved_macros_loaded = 0;
  531. /*
  532. This is just to stop the macro file be loaded over and over for keys
  533. that aren't defined to anything. On slow systems this could be annoying.
  534. */
  535. static int
  536. macro_exists (int k)
  537. {
  538. int i;
  539. for (i = 0; i < MAX_MACROS && saved_macro[i]; i++)
  540. if (saved_macro[i] == k)
  541. return i;
  542. return -1;
  543. }
  544. /* returns 1 on error */
  545. static int
  546. edit_delete_macro (WEdit * edit, int k)
  547. {
  548. struct macro macro[MAX_MACRO_LENGTH];
  549. FILE *f, *g;
  550. int s, i, n, j = 0;
  551. (void) edit;
  552. if (saved_macros_loaded)
  553. if ((j = macro_exists (k)) < 0)
  554. return 0;
  555. g = fopen (catstrs (home_dir, TEMP_FILE, (char *) NULL), "w");
  556. if (!g) {
  557. edit_error_dialog (_(" Delete macro "),
  558. get_sys_error (_(" Cannot open temp file ")));
  559. return 1;
  560. }
  561. f = edit_open_macro_file ("r");
  562. if (!f) {
  563. edit_error_dialog (_(" Delete macro "),
  564. get_sys_error (_(" Cannot open macro file ")));
  565. fclose (g);
  566. return 1;
  567. }
  568. for (;;) {
  569. n = fscanf (f, ("key '%d 0': "), &s);
  570. if (!n || n == EOF)
  571. break;
  572. n = 0;
  573. while (fscanf (f, "%hd %hd, ", &macro[n].command, &macro[n].ch))
  574. n++;
  575. fscanf (f, ";\n");
  576. if (s != k) {
  577. fprintf (g, ("key '%d 0': "), s);
  578. for (i = 0; i < n; i++)
  579. fprintf (g, "%hd %hd, ", macro[i].command, macro[i].ch);
  580. fprintf (g, ";\n");
  581. }
  582. }
  583. fclose (f);
  584. fclose (g);
  585. if (rename (catstrs (home_dir, TEMP_FILE, (char *) NULL), catstrs (home_dir, MACRO_FILE, (char *) NULL)) == -1) {
  586. edit_error_dialog (_(" Delete macro "),
  587. get_sys_error (_(" Cannot overwrite macro file ")));
  588. return 1;
  589. }
  590. if (saved_macros_loaded)
  591. memmove (saved_macro + j, saved_macro + j + 1, sizeof (int) * (MAX_MACROS - j - 1));
  592. return 0;
  593. }
  594. /* returns 0 on error */
  595. int edit_save_macro_cmd (WEdit * edit, struct macro macro[], int n)
  596. {
  597. FILE *f;
  598. int s, i;
  599. edit_push_action (edit, KEY_PRESS + edit->start_display);
  600. s = edit_raw_key_query (_(" Save macro "),
  601. _(" Press the macro's new hotkey: "), 1);
  602. edit->force |= REDRAW_COMPLETELY;
  603. if (s) {
  604. if (edit_delete_macro (edit, s))
  605. return 0;
  606. f = edit_open_macro_file ("a+");
  607. if (f) {
  608. fprintf (f, ("key '%d 0': "), s);
  609. for (i = 0; i < n; i++)
  610. fprintf (f, "%hd %hd, ", macro[i].command, macro[i].ch);
  611. fprintf (f, ";\n");
  612. fclose (f);
  613. if (saved_macros_loaded) {
  614. for (i = 0; i < MAX_MACROS && saved_macro[i]; i++);
  615. saved_macro[i] = s;
  616. }
  617. return 1;
  618. } else
  619. edit_error_dialog (_(" Save macro "), get_sys_error (_(" Cannot open macro file ")));
  620. }
  621. return 0;
  622. }
  623. void edit_delete_macro_cmd (WEdit * edit)
  624. {
  625. int command;
  626. command = edit_raw_key_query (_ (" Delete macro "),
  627. _ (" Press macro hotkey: "), 1);
  628. if (!command)
  629. return;
  630. edit_delete_macro (edit, command);
  631. }
  632. /* return 0 on error */
  633. int edit_load_macro_cmd (WEdit * edit, struct macro macro[], int *n, int k)
  634. {
  635. FILE *f;
  636. int s, i = 0, found = 0;
  637. (void) edit;
  638. if (saved_macros_loaded)
  639. if (macro_exists (k) < 0)
  640. return 0;
  641. if ((f = edit_open_macro_file ("r"))) {
  642. struct macro dummy;
  643. do {
  644. int u;
  645. u = fscanf (f, ("key '%d 0': "), &s);
  646. if (!u || u == EOF)
  647. break;
  648. if (!saved_macros_loaded)
  649. saved_macro[i++] = s;
  650. if (!found) {
  651. *n = 0;
  652. while (*n < MAX_MACRO_LENGTH && 2 == fscanf (f, "%hd %hd, ", &macro[*n].command, &macro[*n].ch))
  653. (*n)++;
  654. } else {
  655. while (2 == fscanf (f, "%hd %hd, ", &dummy.command, &dummy.ch));
  656. }
  657. fscanf (f, ";\n");
  658. if (s == k)
  659. found = 1;
  660. } while (!found || !saved_macros_loaded);
  661. if (!saved_macros_loaded) {
  662. saved_macro[i] = 0;
  663. saved_macros_loaded = 1;
  664. }
  665. fclose (f);
  666. return found;
  667. } else
  668. edit_error_dialog (_(" Load macro "),
  669. get_sys_error (_(" Cannot open macro file ")));
  670. return 0;
  671. }
  672. /* }}} Macro stuff starts here */
  673. /* returns 1 on success */
  674. int edit_save_confirm_cmd (WEdit * edit)
  675. {
  676. const char *f;
  677. if (edit_confirm_save) {
  678. f = catstrs (_(" Confirm save file? : "), edit->filename, " ", (char *) NULL);
  679. if (edit_query_dialog2 (_(" Save file "), f, _("&Save"), _("&Cancel")))
  680. return 0;
  681. }
  682. return edit_save_cmd (edit);
  683. }
  684. /* returns 1 on success */
  685. static int
  686. edit_save_cmd (WEdit *edit)
  687. {
  688. int res, save_lock = 0;
  689. if (!edit->locked && !edit->delete_file)
  690. save_lock = edit_lock_file (edit->filename);
  691. res = edit_save_file (edit, edit->filename);
  692. /* Maintain modify (not save) lock on failure */
  693. if ((res && edit->locked) || save_lock)
  694. edit->locked = edit_unlock_file (edit->filename);
  695. /* On failure try 'save as', it does locking on its own */
  696. if (!res)
  697. return edit_save_as_cmd (edit);
  698. edit->force |= REDRAW_COMPLETELY;
  699. edit->delete_file = 0;
  700. edit->modified = 0;
  701. return 1;
  702. }
  703. /* returns 1 on success */
  704. int edit_new_cmd (WEdit * edit)
  705. {
  706. if (edit->modified) {
  707. if (edit_query_dialog2 (_ ("Warning"), _ (" Current text was modified without a file save. \n Continue discards these changes. "), _ ("C&ontinue"), _ ("&Cancel"))) {
  708. edit->force |= REDRAW_COMPLETELY;
  709. return 0;
  710. }
  711. }
  712. edit->force |= REDRAW_COMPLETELY;
  713. if (edit->locked)
  714. edit->locked = edit_unlock_file (edit->filename);
  715. return edit_renew (edit); /* if this gives an error, something has really screwed up */
  716. }
  717. /* returns 1 on error */
  718. static int
  719. edit_load_file_from_filename (WEdit * edit, char *exp)
  720. {
  721. int prev_locked = edit->locked;
  722. char *prev_filename = g_strdup (edit->filename);
  723. if (!edit_reload (edit, exp)) {
  724. g_free (prev_filename);
  725. return 1;
  726. }
  727. if (prev_locked)
  728. edit_unlock_file (prev_filename);
  729. g_free (prev_filename);
  730. return 0;
  731. }
  732. int
  733. edit_load_cmd (WEdit *edit)
  734. {
  735. char *exp;
  736. if (edit->modified) {
  737. if (edit_query_dialog2
  738. (_("Warning"),
  739. _(" Current text was modified without a file save. \n"
  740. " Continue discards these changes. "), _("C&ontinue"),
  741. _("&Cancel"))) {
  742. edit->force |= REDRAW_COMPLETELY;
  743. return 0;
  744. }
  745. }
  746. exp = edit_get_load_file (edit->filename, _(" Load "));
  747. if (exp) {
  748. if (*exp)
  749. edit_load_file_from_filename (edit, exp);
  750. g_free (exp);
  751. }
  752. edit->force |= REDRAW_COMPLETELY;
  753. return 0;
  754. }
  755. /*
  756. if mark2 is -1 then marking is from mark1 to the cursor.
  757. Otherwise its between the markers. This handles this.
  758. Returns 1 if no text is marked.
  759. */
  760. int eval_marks (WEdit * edit, long *start_mark, long *end_mark)
  761. {
  762. if (edit->mark1 != edit->mark2) {
  763. if (edit->mark2 >= 0) {
  764. *start_mark = min (edit->mark1, edit->mark2);
  765. *end_mark = max (edit->mark1, edit->mark2);
  766. } else {
  767. *start_mark = min (edit->mark1, edit->curs1);
  768. *end_mark = max (edit->mark1, edit->curs1);
  769. edit->column2 = edit->curs_col;
  770. }
  771. return 0;
  772. } else {
  773. *start_mark = *end_mark = 0;
  774. edit->column2 = edit->column1 = 0;
  775. return 1;
  776. }
  777. }
  778. #define space_width 1
  779. static void
  780. edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width)
  781. {
  782. long cursor;
  783. int i, col;
  784. cursor = edit->curs1;
  785. col = edit_get_col (edit);
  786. for (i = 0; i < size; i++) {
  787. if (data[i] == '\n') { /* fill in and move to next line */
  788. int l;
  789. long p;
  790. if (edit_get_byte (edit, edit->curs1) != '\n') {
  791. l = width - (edit_get_col (edit) - col);
  792. while (l > 0) {
  793. edit_insert (edit, ' ');
  794. l -= space_width;
  795. }
  796. }
  797. for (p = edit->curs1;; p++) {
  798. if (p == edit->last_byte) {
  799. edit_cursor_move (edit, edit->last_byte - edit->curs1);
  800. edit_insert_ahead (edit, '\n');
  801. p++;
  802. break;
  803. }
  804. if (edit_get_byte (edit, p) == '\n') {
  805. p++;
  806. break;
  807. }
  808. }
  809. edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
  810. l = col - edit_get_col (edit);
  811. while (l >= space_width) {
  812. edit_insert (edit, ' ');
  813. l -= space_width;
  814. }
  815. continue;
  816. }
  817. edit_insert (edit, data[i]);
  818. }
  819. edit_cursor_move (edit, cursor - edit->curs1);
  820. }
  821. void
  822. edit_block_copy_cmd (WEdit *edit)
  823. {
  824. long start_mark, end_mark, current = edit->curs1;
  825. int size, x;
  826. unsigned char *copy_buf;
  827. edit_update_curs_col (edit);
  828. x = edit->curs_col;
  829. if (eval_marks (edit, &start_mark, &end_mark))
  830. return;
  831. if (column_highlighting)
  832. if ((x >= edit->column1 && x < edit->column2)
  833. || (x > edit->column2 && x <= edit->column1))
  834. return;
  835. copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
  836. /* all that gets pushed are deletes hence little space is used on the stack */
  837. edit_push_markers (edit);
  838. if (column_highlighting) {
  839. edit_insert_column_of_text (edit, copy_buf, size,
  840. abs (edit->column2 - edit->column1));
  841. } else {
  842. while (size--)
  843. edit_insert_ahead (edit, copy_buf[size]);
  844. }
  845. g_free (copy_buf);
  846. edit_scroll_screen_over_cursor (edit);
  847. if (column_highlighting) {
  848. edit_set_markers (edit, 0, 0, 0, 0);
  849. edit_push_action (edit, COLUMN_ON);
  850. column_highlighting = 0;
  851. } else if (start_mark < current && end_mark > current)
  852. edit_set_markers (edit, start_mark,
  853. end_mark + end_mark - start_mark, 0, 0);
  854. edit->force |= REDRAW_PAGE;
  855. }
  856. void
  857. edit_block_move_cmd (WEdit *edit)
  858. {
  859. long count;
  860. long current;
  861. unsigned char *copy_buf;
  862. long start_mark, end_mark;
  863. int deleted = 0;
  864. int x = 0;
  865. if (eval_marks (edit, &start_mark, &end_mark))
  866. return;
  867. if (column_highlighting) {
  868. edit_update_curs_col (edit);
  869. x = edit->curs_col;
  870. if (start_mark <= edit->curs1 && end_mark >= edit->curs1)
  871. if ((x > edit->column1 && x < edit->column2)
  872. || (x > edit->column2 && x < edit->column1))
  873. return;
  874. } else if (start_mark <= edit->curs1 && end_mark >= edit->curs1)
  875. return;
  876. if ((end_mark - start_mark) > option_max_undo / 2)
  877. if (edit_query_dialog2
  878. (_("Warning"),
  879. _
  880. (" Block is large, you may not be able to undo this action. "),
  881. _("C&ontinue"), _("&Cancel")))
  882. return;
  883. edit_push_markers (edit);
  884. current = edit->curs1;
  885. if (column_highlighting) {
  886. int size, c1, c2, line;
  887. line = edit->curs_line;
  888. if (edit->mark2 < 0)
  889. edit_mark_cmd (edit, 0);
  890. c1 = min (edit->column1, edit->column2);
  891. c2 = max (edit->column1, edit->column2);
  892. copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
  893. if (x < c2) {
  894. edit_block_delete_cmd (edit);
  895. deleted = 1;
  896. }
  897. edit_move_to_line (edit, line);
  898. edit_cursor_move (edit,
  899. edit_move_forward3 (edit,
  900. edit_bol (edit, edit->curs1),
  901. x, 0) - edit->curs1);
  902. edit_insert_column_of_text (edit, copy_buf, size, c2 - c1);
  903. if (!deleted) {
  904. line = edit->curs_line;
  905. edit_update_curs_col (edit);
  906. x = edit->curs_col;
  907. edit_block_delete_cmd (edit);
  908. edit_move_to_line (edit, line);
  909. edit_cursor_move (edit,
  910. edit_move_forward3 (edit,
  911. edit_bol (edit,
  912. edit->curs1),
  913. x, 0) - edit->curs1);
  914. }
  915. edit_set_markers (edit, 0, 0, 0, 0);
  916. edit_push_action (edit, COLUMN_ON);
  917. column_highlighting = 0;
  918. } else {
  919. copy_buf = g_malloc (end_mark - start_mark);
  920. edit_cursor_move (edit, start_mark - edit->curs1);
  921. edit_scroll_screen_over_cursor (edit);
  922. count = start_mark;
  923. while (count < end_mark) {
  924. copy_buf[end_mark - count - 1] = edit_delete (edit);
  925. count++;
  926. }
  927. edit_scroll_screen_over_cursor (edit);
  928. edit_cursor_move (edit,
  929. current - edit->curs1 -
  930. (((current - edit->curs1) >
  931. 0) ? end_mark - start_mark : 0));
  932. edit_scroll_screen_over_cursor (edit);
  933. while (count-- > start_mark)
  934. edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
  935. edit_set_markers (edit, edit->curs1,
  936. edit->curs1 + end_mark - start_mark, 0, 0);
  937. }
  938. edit_scroll_screen_over_cursor (edit);
  939. g_free (copy_buf);
  940. edit->force |= REDRAW_PAGE;
  941. }
  942. static void
  943. edit_delete_column_of_text (WEdit * edit)
  944. {
  945. long p, q, r, m1, m2;
  946. int b, c, d;
  947. int n;
  948. eval_marks (edit, &m1, &m2);
  949. n = edit_move_forward (edit, m1, 0, m2) + 1;
  950. c = edit_move_forward3 (edit, edit_bol (edit, m1), 0, m1);
  951. d = edit_move_forward3 (edit, edit_bol (edit, m2), 0, m2);
  952. b = min (c, d);
  953. c = max (c, d);
  954. while (n--) {
  955. r = edit_bol (edit, edit->curs1);
  956. p = edit_move_forward3 (edit, r, b, 0);
  957. q = edit_move_forward3 (edit, r, c, 0);
  958. if (p < m1)
  959. p = m1;
  960. if (q > m2)
  961. q = m2;
  962. edit_cursor_move (edit, p - edit->curs1);
  963. while (q > p) { /* delete line between margins */
  964. if (edit_get_byte (edit, edit->curs1) != '\n')
  965. edit_delete (edit);
  966. q--;
  967. }
  968. if (n) /* move to next line except on the last delete */
  969. edit_cursor_move (edit, edit_move_forward (edit, edit->curs1, 1, 0) - edit->curs1);
  970. }
  971. }
  972. /* if success return 0 */
  973. static int
  974. edit_block_delete (WEdit *edit)
  975. {
  976. long count;
  977. long start_mark, end_mark;
  978. if (eval_marks (edit, &start_mark, &end_mark))
  979. return 0;
  980. if (column_highlighting && edit->mark2 < 0)
  981. edit_mark_cmd (edit, 0);
  982. if ((end_mark - start_mark) > option_max_undo / 2) {
  983. /* Warning message with a query to continue or cancel the operation */
  984. if (edit_query_dialog2
  985. (_("Warning"),
  986. _
  987. (" Block is large, you may not be able to undo this action. "),
  988. _("C&ontinue"), _("&Cancel"))) {
  989. return 1;
  990. }
  991. }
  992. edit_push_markers (edit);
  993. edit_cursor_move (edit, start_mark - edit->curs1);
  994. edit_scroll_screen_over_cursor (edit);
  995. count = start_mark;
  996. if (start_mark < end_mark) {
  997. if (column_highlighting) {
  998. if (edit->mark2 < 0)
  999. edit_mark_cmd (edit, 0);
  1000. edit_delete_column_of_text (edit);
  1001. } else {
  1002. while (count < end_mark) {
  1003. edit_delete (edit);
  1004. count++;
  1005. }
  1006. }
  1007. }
  1008. edit_set_markers (edit, 0, 0, 0, 0);
  1009. edit->force |= REDRAW_PAGE;
  1010. return 0;
  1011. }
  1012. /* returns 1 if canceelled by user */
  1013. int edit_block_delete_cmd (WEdit * edit)
  1014. {
  1015. long start_mark, end_mark;
  1016. if (eval_marks (edit, &start_mark, &end_mark)) {
  1017. edit_delete_line (edit);
  1018. return 0;
  1019. }
  1020. return edit_block_delete (edit);
  1021. }
  1022. #define INPUT_INDEX 9
  1023. #define SEARCH_DLG_WIDTH 58
  1024. #define SEARCH_DLG_HEIGHT 10
  1025. #define REPLACE_DLG_WIDTH 58
  1026. #define REPLACE_DLG_HEIGHT 15
  1027. #define CONFIRM_DLG_WIDTH 79
  1028. #define CONFIRM_DLG_HEIGTH 6
  1029. #define B_REPLACE_ALL (B_USER+1)
  1030. #define B_REPLACE_ONE (B_USER+2)
  1031. #define B_SKIP_REPLACE (B_USER+3)
  1032. static int
  1033. edit_replace_prompt (WEdit * edit, char *replace_text, int xpos, int ypos)
  1034. {
  1035. QuickWidget quick_widgets[] =
  1036. {
  1037. {quick_button, 63, CONFIRM_DLG_WIDTH, 3, CONFIRM_DLG_HEIGTH, N_ ("&Cancel"),
  1038. 0, B_CANCEL, 0, 0, NULL},
  1039. {quick_button, 50, CONFIRM_DLG_WIDTH, 3, CONFIRM_DLG_HEIGTH, N_ ("O&ne"),
  1040. 0, B_REPLACE_ONE, 0, 0, NULL},
  1041. {quick_button, 37, CONFIRM_DLG_WIDTH, 3, CONFIRM_DLG_HEIGTH, N_ ("A&ll"),
  1042. 0, B_REPLACE_ALL, 0, 0, NULL},
  1043. {quick_button, 21, CONFIRM_DLG_WIDTH, 3, CONFIRM_DLG_HEIGTH, N_ ("&Skip"),
  1044. 0, B_SKIP_REPLACE, 0, 0, NULL},
  1045. {quick_button, 4, CONFIRM_DLG_WIDTH, 3, CONFIRM_DLG_HEIGTH, N_ ("&Replace"),
  1046. 0, B_ENTER, 0, 0, NULL},
  1047. {quick_label, 2, CONFIRM_DLG_WIDTH, 2, CONFIRM_DLG_HEIGTH, 0,
  1048. 0, 0, 0, 0, 0},
  1049. NULL_QuickWidget};
  1050. GString *label_text = g_string_new (_(" Replace with: "));
  1051. if (*replace_text) {
  1052. size_t label_len;
  1053. label_len = label_text->len;
  1054. g_string_append (label_text, replace_text);
  1055. convert_to_display (label_text->str + label_len);
  1056. }
  1057. quick_widgets[5].text = label_text->str;
  1058. {
  1059. int retval;
  1060. QuickDialog Quick_input =
  1061. {CONFIRM_DLG_WIDTH, CONFIRM_DLG_HEIGTH, 0, 0, N_ (" Confirm replace "),
  1062. "[Input Line Keys]", 0 /*quick_widgets */, 0 };
  1063. Quick_input.widgets = quick_widgets;
  1064. Quick_input.xpos = xpos;
  1065. /* Sometimes menu can hide replaced text. I don't like it */
  1066. if ((edit->curs_row >= ypos - 1) && (edit->curs_row <= ypos + CONFIRM_DLG_HEIGTH - 1))
  1067. ypos -= CONFIRM_DLG_HEIGTH;
  1068. Quick_input.ypos = ypos;
  1069. retval = quick_dialog (&Quick_input);
  1070. g_string_free (label_text, TRUE);
  1071. return retval;
  1072. }
  1073. }
  1074. static void
  1075. edit_replace_dialog (WEdit * edit, const char *search_default,
  1076. const char *replace_default, const char *argorder_default,
  1077. /*@out@*/ char **search_text, /*@out@*/ char **replace_text,
  1078. /*@out@*/ char **arg_order)
  1079. {
  1080. int treplace_scanf = replace_scanf;
  1081. int treplace_regexp = replace_regexp;
  1082. int treplace_all = replace_all;
  1083. int treplace_prompt = replace_prompt;
  1084. int treplace_backwards = replace_backwards;
  1085. int treplace_whole = replace_whole;
  1086. int treplace_case = replace_case;
  1087. QuickWidget quick_widgets[] =
  1088. {
  1089. {quick_button, 6, 10, 12, REPLACE_DLG_HEIGHT, N_("&Cancel"), 0, B_CANCEL, 0,
  1090. 0, NULL},
  1091. {quick_button, 2, 10, 12, REPLACE_DLG_HEIGHT, N_("&OK"), 0, B_ENTER, 0,
  1092. 0, NULL},
  1093. {quick_checkbox, 33, REPLACE_DLG_WIDTH, 11, REPLACE_DLG_HEIGHT, N_("scanf &Expression"), 0, 0,
  1094. 0, 0, NULL},
  1095. {quick_checkbox, 33, REPLACE_DLG_WIDTH, 10, REPLACE_DLG_HEIGHT, N_("replace &All"), 0, 0,
  1096. 0, 0, NULL},
  1097. {quick_checkbox, 33, REPLACE_DLG_WIDTH, 9, REPLACE_DLG_HEIGHT, N_("pr&Ompt on replace"), 0, 0,
  1098. 0, 0, NULL},
  1099. {quick_checkbox, 4, REPLACE_DLG_WIDTH, 11, REPLACE_DLG_HEIGHT, N_("&Backwards"), 0, 0,
  1100. 0, 0, NULL},
  1101. {quick_checkbox, 4, REPLACE_DLG_WIDTH, 10, REPLACE_DLG_HEIGHT, N_("&Regular expression"), 0, 0,
  1102. 0, 0, NULL},
  1103. {quick_checkbox, 4, REPLACE_DLG_WIDTH, 9, REPLACE_DLG_HEIGHT, N_("&Whole words only"), 0, 0,
  1104. 0, 0, NULL},
  1105. {quick_checkbox, 4, REPLACE_DLG_WIDTH, 8, REPLACE_DLG_HEIGHT, N_("case &Sensitive"), 0, 0,
  1106. 0, 0, NULL},
  1107. {quick_input, 3, REPLACE_DLG_WIDTH, 7, REPLACE_DLG_HEIGHT, "", 52, 0, 0,
  1108. 0, "edit-argord"},
  1109. {quick_label, 2, REPLACE_DLG_WIDTH, 6, REPLACE_DLG_HEIGHT, N_(" Enter replacement argument order eg. 3,2,1,4 "), 0, 0,
  1110. 0, 0, 0},
  1111. {quick_input, 3, REPLACE_DLG_WIDTH, 5, REPLACE_DLG_HEIGHT, "", 52, 0, 0,
  1112. 0, "edit-replace"},
  1113. {quick_label, 2, REPLACE_DLG_WIDTH, 4, REPLACE_DLG_HEIGHT, N_(" Enter replacement string:"), 0, 0, 0,
  1114. 0, 0},
  1115. {quick_input, 3, REPLACE_DLG_WIDTH, 3, REPLACE_DLG_HEIGHT, "", 52, 0, 0,
  1116. 0, "edit-search"},
  1117. {quick_label, 2, REPLACE_DLG_WIDTH, 2, REPLACE_DLG_HEIGHT, N_(" Enter search string:"), 0, 0, 0,
  1118. 0, 0},
  1119. NULL_QuickWidget};
  1120. (void) edit;
  1121. quick_widgets[2].result = &treplace_scanf;
  1122. quick_widgets[3].result = &treplace_all;
  1123. quick_widgets[4].result = &treplace_prompt;
  1124. quick_widgets[5].result = &treplace_backwards;
  1125. quick_widgets[6].result = &treplace_regexp;
  1126. quick_widgets[7].result = &treplace_whole;
  1127. quick_widgets[8].result = &treplace_case;
  1128. quick_widgets[9].str_result = arg_order;
  1129. quick_widgets[9].text = argorder_default;
  1130. quick_widgets[11].str_result = replace_text;
  1131. quick_widgets[11].text = replace_default;
  1132. quick_widgets[13].str_result = search_text;
  1133. quick_widgets[13].text = search_default;
  1134. {
  1135. QuickDialog Quick_input =
  1136. {REPLACE_DLG_WIDTH, REPLACE_DLG_HEIGHT, -1, 0, N_(" Replace "),
  1137. "[Input Line Keys]", 0 /*quick_widgets */, 0};
  1138. Quick_input.widgets = quick_widgets;
  1139. if (quick_dialog (&Quick_input) != B_CANCEL) {
  1140. replace_scanf = treplace_scanf;
  1141. replace_backwards = treplace_backwards;
  1142. replace_regexp = treplace_regexp;
  1143. replace_all = treplace_all;
  1144. replace_prompt = treplace_prompt;
  1145. replace_whole = treplace_whole;
  1146. replace_case = treplace_case;
  1147. return;
  1148. } else {
  1149. *arg_order = NULL;
  1150. *replace_text = NULL;
  1151. *search_text = NULL;
  1152. return;
  1153. }
  1154. }
  1155. }
  1156. static void
  1157. edit_search_dialog (WEdit * edit, char **search_text)
  1158. {
  1159. int treplace_scanf = replace_scanf;
  1160. int treplace_regexp = replace_regexp;
  1161. int treplace_whole = replace_whole;
  1162. int treplace_case = replace_case;
  1163. int treplace_backwards = replace_backwards;
  1164. QuickWidget quick_widgets[] =
  1165. {
  1166. {quick_button, 6, 10, 7, SEARCH_DLG_HEIGHT, N_("&Cancel"), 0, B_CANCEL, 0,
  1167. 0, NULL},
  1168. {quick_button, 2, 10, 7, SEARCH_DLG_HEIGHT, N_("&OK"), 0, B_ENTER, 0,
  1169. 0, NULL},
  1170. {quick_checkbox, 33, SEARCH_DLG_WIDTH, 6, SEARCH_DLG_HEIGHT, N_("scanf &Expression"), 0, 0,
  1171. 0, 0, NULL },
  1172. {quick_checkbox, 33, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT, N_("&Backwards"), 0, 0,
  1173. 0, 0, NULL},
  1174. {quick_checkbox, 4, SEARCH_DLG_WIDTH, 6, SEARCH_DLG_HEIGHT, N_("&Regular expression"), 0, 0,
  1175. 0, 0, NULL},
  1176. {quick_checkbox, 4, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT, N_("&Whole words only"), 0, 0,
  1177. 0, 0, NULL},
  1178. {quick_checkbox, 4, SEARCH_DLG_WIDTH, 4, SEARCH_DLG_HEIGHT, N_("case &Sensitive"), 0, 0,
  1179. 0, 0, NULL},
  1180. {quick_input, 3, SEARCH_DLG_WIDTH, 3, SEARCH_DLG_HEIGHT, "", 52, 0, 0,
  1181. 0, "edit-search"},
  1182. {quick_label, 2, SEARCH_DLG_WIDTH, 2, SEARCH_DLG_HEIGHT, N_(" Enter search string:"), 0, 0, 0,
  1183. 0, 0},
  1184. NULL_QuickWidget};
  1185. (void) edit;
  1186. quick_widgets[2].result = &treplace_scanf;
  1187. quick_widgets[3].result = &treplace_backwards;
  1188. quick_widgets[4].result = &treplace_regexp;
  1189. quick_widgets[5].result = &treplace_whole;
  1190. quick_widgets[6].result = &treplace_case;
  1191. quick_widgets[7].str_result = search_text;
  1192. quick_widgets[7].text = *search_text;
  1193. {
  1194. QuickDialog Quick_input =
  1195. {SEARCH_DLG_WIDTH, SEARCH_DLG_HEIGHT, -1, 0, N_("Search"),
  1196. "[Input Line Keys]", 0 /*quick_widgets */, 0};
  1197. Quick_input.widgets = quick_widgets;
  1198. if (quick_dialog (&Quick_input) != B_CANCEL) {
  1199. replace_scanf = treplace_scanf;
  1200. replace_backwards = treplace_backwards;
  1201. replace_regexp = treplace_regexp;
  1202. replace_whole = treplace_whole;
  1203. replace_case = treplace_case;
  1204. } else {
  1205. *search_text = NULL;
  1206. }
  1207. }
  1208. }
  1209. static long sargs[NUM_REPL_ARGS][256 / sizeof (long)];
  1210. #define SCANF_ARGS sargs[0], sargs[1], sargs[2], sargs[3], \
  1211. sargs[4], sargs[5], sargs[6], sargs[7], \
  1212. sargs[8], sargs[9], sargs[10], sargs[11], \
  1213. sargs[12], sargs[13], sargs[14], sargs[15]
  1214. #define PRINTF_ARGS sargs[argord[0]], sargs[argord[1]], sargs[argord[2]], sargs[argord[3]], \
  1215. sargs[argord[4]], sargs[argord[5]], sargs[argord[6]], sargs[argord[7]], \
  1216. sargs[argord[8]], sargs[argord[9]], sargs[argord[10]], sargs[argord[11]], \
  1217. sargs[argord[12]], sargs[argord[13]], sargs[argord[14]], sargs[argord[15]]
  1218. /* This function is a modification of mc-3.2.10/src/view.c:regexp_view_search() */
  1219. /* returns -3 on error in pattern, -1 on not found, found_len = 0 if either */
  1220. static int
  1221. string_regexp_search (char *pattern, char *string, int match_type,
  1222. int match_bol, int icase, int *found_len, void *d)
  1223. {
  1224. static regex_t r;
  1225. static char *old_pattern = NULL;
  1226. static int old_type, old_icase;
  1227. regmatch_t *pmatch;
  1228. static regmatch_t s[1];
  1229. pmatch = (regmatch_t *) d;
  1230. if (!pmatch)
  1231. pmatch = s;
  1232. if (!old_pattern || strcmp (old_pattern, pattern)
  1233. || old_type != match_type || old_icase != icase) {
  1234. if (old_pattern) {
  1235. regfree (&r);
  1236. g_free (old_pattern);
  1237. old_pattern = 0;
  1238. }
  1239. if (regcomp (&r, pattern, REG_EXTENDED | (icase ? REG_ICASE : 0))) {
  1240. *found_len = 0;
  1241. return -3;
  1242. }
  1243. old_pattern = g_strdup (pattern);
  1244. old_type = match_type;
  1245. old_icase = icase;
  1246. }
  1247. if (regexec
  1248. (&r, string, d ? NUM_REPL_ARGS : 1, pmatch,
  1249. ((match_bol
  1250. || match_type != match_normal) ? 0 : REG_NOTBOL)) != 0) {
  1251. *found_len = 0;
  1252. return -1;
  1253. }
  1254. *found_len = pmatch[0].rm_eo - pmatch[0].rm_so;
  1255. return (pmatch[0].rm_so);
  1256. }
  1257. /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
  1258. (and the above) routines to work properly - paul */
  1259. typedef int (*edit_getbyte_fn) (WEdit *, long);
  1260. static long
  1261. edit_find_string (long start, unsigned char *exp, int *len, long last_byte, edit_getbyte_fn get_byte, void *data, int once_only, void *d)
  1262. {
  1263. long p, q = 0;
  1264. long l = strlen ((char *) exp), f = 0;
  1265. int n = 0;
  1266. for (p = 0; p < l; p++) /* count conversions... */
  1267. if (exp[p] == '%')
  1268. if (exp[++p] != '%') /* ...except for "%%" */
  1269. n++;
  1270. if (replace_scanf || replace_regexp) {
  1271. int c;
  1272. unsigned char *buf;
  1273. unsigned char mbuf[MAX_REPL_LEN * 2 + 3];
  1274. replace_scanf = (!replace_regexp); /* can't have both */
  1275. buf = mbuf;
  1276. if (replace_scanf) {
  1277. unsigned char e[MAX_REPL_LEN];
  1278. if (n >= NUM_REPL_ARGS)
  1279. return -3;
  1280. if (replace_case) {
  1281. for (p = start; p < last_byte && p < start + MAX_REPL_LEN; p++)
  1282. buf[p - start] = (*get_byte) (data, p);
  1283. } else {
  1284. for (p = 0; exp[p] != 0; p++)
  1285. exp[p] = my_lower_case (exp[p]);
  1286. for (p = start; p < last_byte && p < start + MAX_REPL_LEN; p++) {
  1287. c = (*get_byte) (data, p);
  1288. buf[p - start] = my_lower_case (c);
  1289. }
  1290. }
  1291. buf[(q = p - start)] = 0;
  1292. strcpy ((char *) e, (char *) exp);
  1293. strcat ((char *) e, "%n");
  1294. exp = e;
  1295. while (q) {
  1296. *((int *) sargs[n]) = 0; /* --> here was the problem - now fixed: good */
  1297. if (n == sscanf ((char *) buf, (char *) exp, SCANF_ARGS)) {
  1298. if (*((int *) sargs[n])) {
  1299. *len = *((int *) sargs[n]);
  1300. return start;
  1301. }
  1302. }
  1303. if (once_only)
  1304. return -2;
  1305. if (q + start < last_byte) {
  1306. if (replace_case) {
  1307. buf[q] = (*get_byte) (data, q + start);
  1308. } else {
  1309. c = (*get_byte) (data, q + start);
  1310. buf[q] = my_lower_case (c);
  1311. }
  1312. q++;
  1313. }
  1314. buf[q] = 0;
  1315. start++;
  1316. buf++; /* move the window along */
  1317. if (buf == mbuf + MAX_REPL_LEN) { /* the window is about to go past the end of array, so... */
  1318. memmove (mbuf, buf, strlen ((char *) buf) + 1); /* reset it */
  1319. buf = mbuf;
  1320. }
  1321. q--;
  1322. }
  1323. } else { /* regexp matching */
  1324. long offset = 0;
  1325. int found_start, match_bol, move_win = 0;
  1326. while (start + offset < last_byte) {
  1327. match_bol = (offset == 0 || (*get_byte) (data, start + offset - 1) == '\n');
  1328. if (!move_win) {
  1329. p = start + offset;
  1330. q = 0;
  1331. }
  1332. for (; p < last_byte && q < MAX_REPL_LEN; p++, q++) {
  1333. mbuf[q] = (*get_byte) (data, p);
  1334. if (mbuf[q] == '\n')
  1335. break;
  1336. }
  1337. q++;
  1338. offset += q;
  1339. mbuf[q] = 0;
  1340. buf = mbuf;
  1341. while (q) {
  1342. found_start = string_regexp_search ((char *) exp, (char *) buf, match_normal, match_bol, !replace_case, len, d);
  1343. if (found_start <= -2) { /* regcomp/regexec error */
  1344. *len = 0;
  1345. return -3;
  1346. }
  1347. else if (found_start == -1) /* not found: try next line */
  1348. break;
  1349. else if (*len == 0) { /* null pattern: try again at next character */
  1350. q--;
  1351. buf++;
  1352. match_bol = 0;
  1353. continue;
  1354. }
  1355. else /* found */
  1356. return (start + offset - q + found_start);
  1357. }
  1358. if (once_only)
  1359. return -2;
  1360. if (buf[q - 1] != '\n') { /* incomplete line: try to recover */
  1361. buf = mbuf + MAX_REPL_LEN / 2;
  1362. q = strlen ((const char *) buf);
  1363. memmove (mbuf, buf, q);
  1364. p = start + q;
  1365. move_win = 1;
  1366. }
  1367. else
  1368. move_win = 0;
  1369. }
  1370. }
  1371. } else {
  1372. *len = strlen ((const char *) exp);
  1373. if (replace_case) {
  1374. for (p = start; p <= last_byte - l; p++) {
  1375. if ((*get_byte) (data, p) == (unsigned char)exp[0]) { /* check if first char matches */
  1376. for (f = 0, q = 0; q < l && f < 1; q++)
  1377. if ((*get_byte) (data, q + p) != (unsigned char)exp[q])
  1378. f = 1;
  1379. if (f == 0)
  1380. return p;
  1381. }
  1382. if (once_only)
  1383. return -2;
  1384. }
  1385. } else {
  1386. for (p = 0; exp[p] != 0; p++)
  1387. exp[p] = my_lower_case (exp[p]);
  1388. for (p = start; p <= last_byte - l; p++) {
  1389. if (my_lower_case ((*get_byte) (data, p)) == (unsigned char)exp[0]) {
  1390. for (f = 0, q = 0; q < l && f < 1; q++)
  1391. if (my_lower_case ((*get_byte) (data, q + p)) != (unsigned char)exp[q])
  1392. f = 1;
  1393. if (f == 0)
  1394. return p;
  1395. }
  1396. if (once_only)
  1397. return -2;
  1398. }
  1399. }
  1400. }
  1401. return -2;
  1402. }
  1403. static long
  1404. edit_find_forwards (long search_start, unsigned char *exp, int *len, long last_byte, edit_getbyte_fn get_byte, void *data, int once_only, void *d)
  1405. { /*front end to find_string to check for
  1406. whole words */
  1407. long p;
  1408. p = search_start;
  1409. while ((p = edit_find_string (p, exp, len, last_byte, get_byte, data, once_only, d)) >= 0) {
  1410. if (replace_whole) {
  1411. /*If the bordering chars are not in option_whole_chars_search then word is whole */
  1412. if (!strcasechr (option_whole_chars_search, (*get_byte) (data, p - 1))
  1413. && !strcasechr (option_whole_chars_search, (*get_byte) (data, p + *len)))
  1414. return p;
  1415. if (once_only)
  1416. return -2;
  1417. } else
  1418. return p;
  1419. if (once_only)
  1420. break;
  1421. p++; /*not a whole word so continue search. */
  1422. }
  1423. return p;
  1424. }
  1425. static long
  1426. edit_find (long search_start, unsigned char *exp, int *len, long last_byte, edit_getbyte_fn get_byte, void *data, void *d)
  1427. {
  1428. long p;
  1429. if (replace_backwards) {
  1430. while (search_start >= 0) {
  1431. p = edit_find_forwards (search_start, exp, len, last_byte, get_byte, data, 1, d);
  1432. if (p == search_start)
  1433. return p;
  1434. search_start--;
  1435. }
  1436. } else {
  1437. return edit_find_forwards (search_start, exp, len, last_byte, get_byte, data, 0, d);
  1438. }
  1439. return -2;
  1440. }
  1441. #define is_digit(x) ((x) >= '0' && (x) <= '9')
  1442. #define snprint(v) { \
  1443. *p1++ = *p++; \
  1444. *p1 = '\0'; \
  1445. n = snprintf(s,e-s,q1,v); \
  1446. if (n >= (size_t) (e - s)) goto nospc; \
  1447. s += n; \
  1448. }
  1449. /* this function uses the sprintf command to do a vprintf */
  1450. /* it takes pointers to arguments instead of the arguments themselves */
  1451. /* The return value is the number of bytes written excluding '\0'
  1452. if successfull, -1 if the resulting string would be too long and
  1453. -2 if the format string is errorneous. */
  1454. static int snprintf_p (char *str, size_t size, const char *fmt,...)
  1455. __attribute__ ((format (printf, 3, 4)));
  1456. static int snprintf_p (char *str, size_t size, const char *fmt,...)
  1457. {
  1458. va_list ap;
  1459. size_t n;
  1460. const char *q, *p;
  1461. char *s = str, *e = str + size;
  1462. char q1[40];
  1463. char *p1;
  1464. int nargs = 0;
  1465. va_start (ap, fmt);
  1466. p = q = fmt;
  1467. while ((p = strchr (p, '%'))) {
  1468. n = p - q;
  1469. if (n >= (size_t) (e - s))
  1470. goto nospc;
  1471. memcpy (s, q, n); /* copy stuff between format specifiers */
  1472. s += n;
  1473. q = p;
  1474. p1 = q1;
  1475. *p1++ = *p++;
  1476. if (*p == '%') {
  1477. p++;
  1478. *s++ = '%';
  1479. if (s == e)
  1480. goto nospc;
  1481. q = p;
  1482. continue;
  1483. }
  1484. if (*p == 'n')
  1485. goto err;
  1486. /* We were passed only 16 arguments. */
  1487. if (++nargs == 16)
  1488. goto err;
  1489. if (*p == '#')
  1490. *p1++ = *p++;
  1491. if (*p == '0')
  1492. *p1++ = *p++;
  1493. if (*p == '-')
  1494. *p1++ = *p++;
  1495. if (*p == '+')
  1496. *p1++ = *p++;
  1497. if (*p == '*') {
  1498. p++;
  1499. strcpy (p1, MY_itoa (*va_arg (ap, int *))); /* replace field width with a number */
  1500. p1 += strlen (p1);
  1501. } else {
  1502. while (is_digit (*p) && p1 < q1 + 20)
  1503. *p1++ = *p++;
  1504. if (is_digit (*p))
  1505. goto err;
  1506. }
  1507. if (*p == '.')
  1508. *p1++ = *p++;
  1509. if (*p == '*') {
  1510. p++;
  1511. strcpy (p1, MY_itoa (*va_arg (ap, int *))); /* replace precision with a number */
  1512. p1 += strlen (p1);
  1513. } else {
  1514. while (is_digit (*p) && p1 < q1 + 32)
  1515. *p1++ = *p++;
  1516. if (is_digit (*p))
  1517. goto err;
  1518. }
  1519. /* flags done, now get argument */
  1520. if (*p == 's') {
  1521. snprint (va_arg (ap, char *));
  1522. } else if (*p == 'h') {
  1523. if (strchr ("diouxX", *p))
  1524. snprint (*va_arg (ap, short *));
  1525. } else if (*p == 'l') {
  1526. *p1++ = *p++;
  1527. if (strchr ("diouxX", *p))
  1528. snprint (*va_arg (ap, long *));
  1529. } else if (strchr ("cdiouxX", *p)) {
  1530. snprint (*va_arg (ap, int *));
  1531. } else if (*p == 'L') {
  1532. *p1++ = *p++;
  1533. if (strchr ("EefgG", *p))
  1534. snprint (*va_arg (ap, double *)); /* should be long double */
  1535. } else if (strchr ("EefgG", *p)) {
  1536. snprint (*va_arg (ap, double *));
  1537. } else if (strchr ("DOU", *p)) {
  1538. snprint (*va_arg (ap, long *));
  1539. } else if (*p == 'p') {
  1540. snprint (*va_arg (ap, void **));
  1541. } else
  1542. goto err;
  1543. q = p;
  1544. }
  1545. va_end (ap);
  1546. n = strlen (q);
  1547. if (n >= (size_t) (e - s))
  1548. return -1;
  1549. memcpy (s, q, n + 1);
  1550. return s + n - str;
  1551. nospc:
  1552. va_end (ap);
  1553. return -1;
  1554. err:
  1555. va_end (ap);
  1556. return -2;
  1557. }
  1558. static void regexp_error (WEdit *edit)
  1559. {
  1560. (void) edit;
  1561. edit_error_dialog (_("Error"), _(" Invalid regular expression, or scanf expression with too many conversions "));
  1562. }
  1563. /* call with edit = 0 before shutdown to close memory leaks */
  1564. void
  1565. edit_replace_cmd (WEdit *edit, int again)
  1566. {
  1567. static regmatch_t pmatch[NUM_REPL_ARGS];
  1568. /* 1 = search string, 2 = replace with, 3 = argument order */
  1569. static char *saved1 = NULL; /* saved default[123] */
  1570. static char *saved2 = NULL;
  1571. static char *saved3 = NULL;
  1572. char *default1 = NULL; /* default values */
  1573. char *default2 = NULL;
  1574. char *default3 = NULL;
  1575. char *input1 = NULL; /* user input from the dialog */
  1576. char *input2 = NULL;
  1577. char *input3 = NULL;
  1578. int replace_yes;
  1579. int replace_continue;
  1580. int treplace_prompt = 0;
  1581. long times_replaced = 0, last_search;
  1582. int argord[NUM_REPL_ARGS];
  1583. if (!edit) {
  1584. g_free (saved1), saved1 = NULL;
  1585. g_free (saved2), saved2 = NULL;
  1586. g_free (saved3), saved3 = NULL;
  1587. return;
  1588. }
  1589. last_search = edit->last_byte;
  1590. edit->force |= REDRAW_COMPLETELY;
  1591. default1 = g_strdup (saved1 ? saved1 : "");
  1592. default2 = g_strdup (saved2 ? saved2 : "");
  1593. default3 = g_strdup (saved3 ? saved3 : "");
  1594. if (again) {
  1595. if (saved1 == NULL || saved2 == NULL)
  1596. goto cleanup;
  1597. } else {
  1598. char *disp1 = g_strdup (default1);
  1599. char *disp2 = g_strdup (default2);
  1600. char *disp3 = g_strdup (default3);
  1601. convert_to_display (disp1);
  1602. convert_to_display (disp2);
  1603. convert_to_display (disp3);
  1604. edit_push_action (edit, KEY_PRESS + edit->start_display);
  1605. edit_replace_dialog (edit, disp1, disp2, disp3, &input1, &input2, &input3);
  1606. g_free (disp1);
  1607. g_free (disp2);
  1608. g_free (disp3);
  1609. convert_from_input (input1);
  1610. convert_from_input (input2);
  1611. convert_from_input (input3);
  1612. treplace_prompt = replace_prompt;
  1613. }
  1614. if (input1 == NULL || *input1 == '\0') {
  1615. edit->force = REDRAW_COMPLETELY;
  1616. goto cleanup;
  1617. }
  1618. g_free (saved1), saved1 = g_strdup (input1);
  1619. g_free (saved2), saved2 = g_strdup (input2);
  1620. g_free (saved3), saved3 = g_strdup (input3);
  1621. {
  1622. const char *s;
  1623. int ord;
  1624. size_t i;
  1625. s = input3;
  1626. for (i = 0; i < NUM_REPL_ARGS; i++) {
  1627. if (s != NULL && *s != '\0') {
  1628. ord = atoi (s);
  1629. if ((ord > 0) && (ord <= NUM_REPL_ARGS))
  1630. argord[i] = ord - 1;
  1631. else
  1632. argord[i] = i;
  1633. s = strchr (s, ',');
  1634. if (s != NULL)
  1635. s++;
  1636. } else
  1637. argord[i] = i;
  1638. }
  1639. }
  1640. replace_continue = replace_all;
  1641. if (edit->found_len && edit->search_start == edit->found_start + 1
  1642. && replace_backwards)
  1643. edit->search_start--;
  1644. if (edit->found_len && edit->search_start == edit->found_start - 1
  1645. && !replace_backwards)
  1646. edit->search_start++;
  1647. do {
  1648. int len = 0;
  1649. long new_start;
  1650. new_start =
  1651. edit_find (edit->search_start, (unsigned char *) input1, &len,
  1652. last_search, edit_get_byte, (void *) edit, pmatch);
  1653. if (new_start == -3) {
  1654. regexp_error (edit);
  1655. break;
  1656. }
  1657. edit->search_start = new_start;
  1658. /*returns negative on not found or error in pattern */
  1659. if (edit->search_start >= 0) {
  1660. int i;
  1661. edit->found_start = edit->search_start;
  1662. i = edit->found_len = len;
  1663. edit_cursor_move (edit, edit->search_start - edit->curs1);
  1664. edit_scroll_screen_over_cursor (edit);
  1665. replace_yes = 1;
  1666. if (treplace_prompt) {
  1667. int l;
  1668. l = edit->curs_row - edit->num_widget_lines / 3;
  1669. if (l > 0)
  1670. edit_scroll_downward (edit, l);
  1671. if (l < 0)
  1672. edit_scroll_upward (edit, -l);
  1673. edit_scroll_screen_over_cursor (edit);
  1674. edit->force |= REDRAW_PAGE;
  1675. edit_render_keypress (edit);
  1676. /*so that undo stops at each query */
  1677. edit_push_key_press (edit);
  1678. switch (edit_replace_prompt (edit, input2, /* and prompt 2/3 down */
  1679. (edit->num_widget_columns -
  1680. CONFIRM_DLG_WIDTH) / 2,
  1681. edit->num_widget_lines * 2 /
  1682. 3)) {
  1683. case B_ENTER:
  1684. break;
  1685. case B_SKIP_REPLACE:
  1686. replace_yes = 0;
  1687. break;
  1688. case B_REPLACE_ALL:
  1689. treplace_prompt = 0;
  1690. replace_continue = 1;
  1691. break;
  1692. case B_REPLACE_ONE:
  1693. replace_continue = 0;
  1694. break;
  1695. case B_CANCEL:
  1696. replace_yes = 0;
  1697. replace_continue = 0;
  1698. break;
  1699. }
  1700. }
  1701. if (replace_yes) { /* delete then insert new */
  1702. if (replace_scanf || replace_regexp) {
  1703. char repl_str[MAX_REPL_LEN + 2];
  1704. int ret = 0;
  1705. /* we need to fill in sargs just like with scanf */
  1706. if (replace_regexp) {
  1707. int k, j;
  1708. for (k = 1;
  1709. k < NUM_REPL_ARGS && pmatch[k].rm_eo >= 0;
  1710. k++) {
  1711. unsigned char *t;
  1712. if (pmatch[k].rm_eo - pmatch[k].rm_so > 255) {
  1713. ret = -1;
  1714. break;
  1715. }
  1716. t = (unsigned char *) &sargs[k - 1][0];
  1717. for (j = 0;
  1718. j < pmatch[k].rm_eo - pmatch[k].rm_so
  1719. && j < 255; j++, t++)
  1720. *t = (unsigned char) edit_get_byte (edit,
  1721. edit->
  1722. search_start
  1723. -
  1724. pmatch
  1725. [0].
  1726. rm_so +
  1727. pmatch
  1728. [k].
  1729. rm_so +
  1730. j);
  1731. *t = '\0';
  1732. }
  1733. for (; k <= NUM_REPL_ARGS; k++)
  1734. sargs[k - 1][0] = 0;
  1735. }
  1736. if (!ret)
  1737. ret = snprintf_p (repl_str, MAX_REPL_LEN + 2, input2, PRINTF_ARGS);
  1738. if (ret >= 0) {
  1739. times_replaced++;
  1740. while (i--)
  1741. edit_delete (edit);
  1742. while (repl_str[++i])
  1743. edit_insert (edit, repl_str[i]);
  1744. } else {
  1745. edit_error_dialog (_(" Replace "),
  1746. ret == -2
  1747. ? _(" Error in replacement format string. ")
  1748. : _(" Replacement too long. "));
  1749. replace_continue = 0;
  1750. }
  1751. } else {
  1752. times_replaced++;
  1753. while (i--)
  1754. edit_delete (edit);
  1755. while (input2[++i])
  1756. edit_insert (edit, input2[i]);
  1757. }
  1758. edit->found_len = i;
  1759. }
  1760. /* so that we don't find the same string again */
  1761. if (replace_backwards) {
  1762. last_search = edit->search_start;
  1763. edit->search_start--;
  1764. } else {
  1765. edit->search_start += i;
  1766. last_search = edit->last_byte;
  1767. }
  1768. edit_scroll_screen_over_cursor (edit);
  1769. } else {
  1770. const char *msg = _(" Replace ");
  1771. /* try and find from right here for next search */
  1772. edit->search_start = edit->curs1;
  1773. edit_update_curs_col (edit);
  1774. edit->force |= REDRAW_PAGE;
  1775. edit_render_keypress (edit);
  1776. if (times_replaced) {
  1777. message (0, msg, _(" %ld replacements made. "),
  1778. times_replaced);
  1779. } else
  1780. edit_message_dialog (msg, _(" Search string not found "));
  1781. replace_continue = 0;
  1782. }
  1783. } while (replace_continue);
  1784. edit->force = REDRAW_COMPLETELY;
  1785. edit_scroll_screen_over_cursor (edit);
  1786. cleanup:
  1787. g_free (default1);
  1788. g_free (default2);
  1789. g_free (default3);
  1790. g_free (input1);
  1791. g_free (input2);
  1792. g_free (input3);
  1793. }
  1794. void edit_search_cmd (WEdit * edit, int again)
  1795. {
  1796. static char *old = NULL;
  1797. char *exp = "";
  1798. if (!edit) {
  1799. g_free (old);
  1800. old = 0;
  1801. return;
  1802. }
  1803. exp = old ? old : exp;
  1804. if (again) { /*ctrl-hotkey for search again. */
  1805. if (!old)
  1806. return;
  1807. exp = g_strdup (old);
  1808. } else {
  1809. #ifdef HAVE_CHARSET
  1810. if (exp && *exp)
  1811. convert_to_display (exp);
  1812. #endif /* HAVE_CHARSET */
  1813. edit_search_dialog (edit, &exp);
  1814. #ifdef HAVE_CHARSET
  1815. if (exp && *exp)
  1816. convert_from_input (exp);
  1817. #endif /* HAVE_CHARSET */
  1818. edit_push_action (edit, KEY_PRESS + edit->start_display);
  1819. }
  1820. if (exp) {
  1821. if (*exp) {
  1822. int len = 0;
  1823. g_free (old);
  1824. old = g_strdup (exp);
  1825. if (search_create_bookmark) {
  1826. int found = 0, books = 0;
  1827. int l = 0, l_last = -1;
  1828. long p, q = 0;
  1829. for (;;) {
  1830. p = edit_find (q, (unsigned char *) exp, &len, edit->last_byte,
  1831. edit_get_byte, (void *) edit, 0);
  1832. if (p < 0)
  1833. break;
  1834. found++;
  1835. l += edit_count_lines (edit, q, p);
  1836. if (l != l_last) {
  1837. book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
  1838. books++;
  1839. }
  1840. l_last = l;
  1841. q = p + 1;
  1842. }
  1843. if (found) {
  1844. /* in response to number of bookmarks added because of string being found %d times */
  1845. message (0, _("Search"), _(" %d items found, %d bookmarks added "), found, books);
  1846. } else {
  1847. edit_error_dialog (_ ("Search"), _ (" Search string not found "));
  1848. }
  1849. } else {
  1850. if (edit->found_len && edit->search_start == edit->found_start + 1 && replace_backwards)
  1851. edit->search_start--;
  1852. if (edit->found_len && edit->search_start == edit->found_start - 1 && !replace_backwards)
  1853. edit->search_start++;
  1854. edit->search_start = edit_find (edit->search_start, (unsigned char *) exp, &len, edit->last_byte,
  1855. edit_get_byte, (void *) edit, 0);
  1856. if (edit->search_start >= 0) {
  1857. edit->found_start = edit->search_start;
  1858. edit->found_len = len;
  1859. edit_cursor_move (edit, edit->search_start - edit->curs1);
  1860. edit_scroll_screen_over_cursor (edit);
  1861. if (replace_backwards)
  1862. edit->search_start--;
  1863. else
  1864. edit->search_start++;
  1865. } else if (edit->search_start == -3) {
  1866. edit->search_start = edit->curs1;
  1867. regexp_error (edit);
  1868. } else {
  1869. edit->search_start = edit->curs1;
  1870. edit_error_dialog (_ ("Search"), _ (" Search string not found "));
  1871. }
  1872. }
  1873. }
  1874. g_free (exp);
  1875. }
  1876. edit->force |= REDRAW_COMPLETELY;
  1877. edit_scroll_screen_over_cursor (edit);
  1878. }
  1879. /*
  1880. * Check if it's OK to close the editor. If there are unsaved changes,
  1881. * ask user. Return 1 if it's OK to exit, 0 to continue editing.
  1882. */
  1883. int
  1884. edit_ok_to_exit (WEdit *edit)
  1885. {
  1886. if (!edit->modified)
  1887. return 1;
  1888. switch (edit_query_dialog3
  1889. (_("Quit"), _(" File was modified, Save with exit? "),
  1890. _("&Cancel quit"), _("&Yes"), _("&No"))) {
  1891. case 1:
  1892. edit_push_markers (edit);
  1893. edit_set_markers (edit, 0, 0, 0, 0);
  1894. if (!edit_save_cmd (edit))
  1895. return 0;
  1896. break;
  1897. case 2:
  1898. if (edit->locked)
  1899. edit->locked = edit_unlock_file (edit->filename);
  1900. break;
  1901. case 0:
  1902. case -1:
  1903. return 0;
  1904. }
  1905. return 1;
  1906. }
  1907. #define TEMP_BUF_LEN 1024
  1908. /* Return a null terminated length of text. Result must be g_free'd */
  1909. static unsigned char *
  1910. edit_get_block (WEdit *edit, long start, long finish, int *l)
  1911. {
  1912. unsigned char *s, *r;
  1913. r = s = g_malloc (finish - start + 1);
  1914. if (column_highlighting) {
  1915. *l = 0;
  1916. /* copy from buffer, excluding chars that are out of the column 'margins' */
  1917. while (start < finish) {
  1918. int c, x;
  1919. x = edit_move_forward3 (edit, edit_bol (edit, start), 0,
  1920. start);
  1921. c = edit_get_byte (edit, start);
  1922. if ((x >= edit->column1 && x < edit->column2)
  1923. || (x >= edit->column2 && x < edit->column1) || c == '\n') {
  1924. *s++ = c;
  1925. (*l)++;
  1926. }
  1927. start++;
  1928. }
  1929. } else {
  1930. *l = finish - start;
  1931. while (start < finish)
  1932. *s++ = edit_get_byte (edit, start++);
  1933. }
  1934. *s = 0;
  1935. return r;
  1936. }
  1937. /* save block, returns 1 on success */
  1938. int
  1939. edit_save_block (WEdit * edit, const char *filename, long start,
  1940. long finish)
  1941. {
  1942. int len, file;
  1943. if ((file =
  1944. mc_open (filename, O_CREAT | O_WRONLY | O_TRUNC,
  1945. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY)) == -1)
  1946. return 0;
  1947. if (column_highlighting) {
  1948. unsigned char *block, *p;
  1949. int r;
  1950. p = block = edit_get_block (edit, start, finish, &len);
  1951. while (len) {
  1952. r = mc_write (file, p, len);
  1953. if (r < 0)
  1954. break;
  1955. p += r;
  1956. len -= r;
  1957. }
  1958. g_free (block);
  1959. } else {
  1960. unsigned char *buf;
  1961. int i = start, end;
  1962. len = finish - start;
  1963. buf = g_malloc (TEMP_BUF_LEN);
  1964. while (start != finish) {
  1965. end = min (finish, start + TEMP_BUF_LEN);
  1966. for (; i < end; i++)
  1967. buf[i - start] = edit_get_byte (edit, i);
  1968. len -= mc_write (file, (char *) buf, end - start);
  1969. start = end;
  1970. }
  1971. g_free (buf);
  1972. }
  1973. mc_close (file);
  1974. if (len)
  1975. return 0;
  1976. return 1;
  1977. }
  1978. /* copies a block to clipboard file */
  1979. static int edit_save_block_to_clip_file (WEdit * edit, long start, long finish)
  1980. {
  1981. return edit_save_block (edit, catstrs (home_dir, CLIP_FILE, (char *) NULL), start, finish);
  1982. }
  1983. void edit_paste_from_history (WEdit *edit)
  1984. {
  1985. (void) edit;
  1986. edit_error_dialog (_(" Error "), _(" This function is not implemented. "));
  1987. }
  1988. int edit_copy_to_X_buf_cmd (WEdit * edit)
  1989. {
  1990. long start_mark, end_mark;
  1991. if (eval_marks (edit, &start_mark, &end_mark))
  1992. return 0;
  1993. if (!edit_save_block_to_clip_file (edit, start_mark, end_mark)) {
  1994. edit_error_dialog (_(" Copy to clipboard "), get_sys_error (_(" Unable to save to file. ")));
  1995. return 1;
  1996. }
  1997. edit_mark_cmd (edit, 1);
  1998. return 0;
  1999. }
  2000. int edit_cut_to_X_buf_cmd (WEdit * edit)
  2001. {
  2002. long start_mark, end_mark;
  2003. if (eval_marks (edit, &start_mark, &end_mark))
  2004. return 0;
  2005. if (!edit_save_block_to_clip_file (edit, start_mark, end_mark)) {
  2006. edit_error_dialog (_(" Cut to clipboard "), _(" Unable to save to file. "));
  2007. return 1;
  2008. }
  2009. edit_block_delete_cmd (edit);
  2010. edit_mark_cmd (edit, 1);
  2011. return 0;
  2012. }
  2013. void edit_paste_from_X_buf_cmd (WEdit * edit)
  2014. {
  2015. edit_insert_file (edit, catstrs (home_dir, CLIP_FILE, (char *) NULL));
  2016. }
  2017. /*
  2018. * Ask user for the line and go to that line.
  2019. * Negative numbers mean line from the end (i.e. -1 is the last line).
  2020. */
  2021. void
  2022. edit_goto_cmd (WEdit *edit)
  2023. {
  2024. char *f;
  2025. static long line = 0; /* line as typed, saved as default */
  2026. long l;
  2027. char *error;
  2028. char s[32];
  2029. g_snprintf (s, sizeof (s), "%ld", line);
  2030. f = input_dialog (_(" Goto line "), _(" Enter line: "), line ? s : "");
  2031. if (!f)
  2032. return;
  2033. if (!*f) {
  2034. g_free (f);
  2035. return;
  2036. }
  2037. l = strtol (f, &error, 0);
  2038. if (*error) {
  2039. g_free (f);
  2040. return;
  2041. }
  2042. line = l;
  2043. if (l < 0)
  2044. l = edit->total_lines + l + 2;
  2045. edit_move_display (edit, l - edit->num_widget_lines / 2 - 1);
  2046. edit_move_to_line (edit, l - 1);
  2047. edit->force |= REDRAW_COMPLETELY;
  2048. g_free (f);
  2049. }
  2050. /* Return 1 on success */
  2051. int
  2052. edit_save_block_cmd (WEdit *edit)
  2053. {
  2054. long start_mark, end_mark;
  2055. char *exp;
  2056. if (eval_marks (edit, &start_mark, &end_mark))
  2057. return 1;
  2058. exp =
  2059. edit_get_save_file (catstrs (home_dir, CLIP_FILE, (char *) NULL),
  2060. _(" Save Block "));
  2061. edit_push_action (edit, KEY_PRESS + edit->start_display);
  2062. if (exp) {
  2063. if (!*exp) {
  2064. g_free (exp);
  2065. return 0;
  2066. } else {
  2067. if (edit_save_block (edit, exp, start_mark, end_mark)) {
  2068. g_free (exp);
  2069. edit->force |= REDRAW_COMPLETELY;
  2070. return 1;
  2071. } else {
  2072. g_free (exp);
  2073. edit_error_dialog (_(" Save Block "),
  2074. get_sys_error (_
  2075. (" Cannot save file. ")));
  2076. }
  2077. }
  2078. }
  2079. edit->force |= REDRAW_COMPLETELY;
  2080. return 0;
  2081. }
  2082. /* returns 1 on success */
  2083. int
  2084. edit_insert_file_cmd (WEdit *edit)
  2085. {
  2086. char *exp = edit_get_load_file (catstrs (home_dir, CLIP_FILE, (char *) NULL),
  2087. _(" Insert File "));
  2088. edit_push_action (edit, KEY_PRESS + edit->start_display);
  2089. if (exp) {
  2090. if (!*exp) {
  2091. g_free (exp);
  2092. return 0;
  2093. } else {
  2094. if (edit_insert_file (edit, exp)) {
  2095. g_free (exp);
  2096. edit->force |= REDRAW_COMPLETELY;
  2097. return 1;
  2098. } else {
  2099. g_free (exp);
  2100. edit_error_dialog (_(" Insert File "),
  2101. get_sys_error (_
  2102. (" Cannot insert file. ")));
  2103. }
  2104. }
  2105. }
  2106. edit->force |= REDRAW_COMPLETELY;
  2107. return 0;
  2108. }
  2109. /* sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
  2110. int edit_sort_cmd (WEdit * edit)
  2111. {
  2112. static char *old = 0;
  2113. char *exp;
  2114. long start_mark, end_mark;
  2115. int e;
  2116. if (eval_marks (edit, &start_mark, &end_mark)) {
  2117. edit_error_dialog (_(" Sort block "), _(" You must first highlight a block of text. "));
  2118. return 0;
  2119. }
  2120. edit_save_block (edit, catstrs (home_dir, BLOCK_FILE, (char *) NULL), start_mark, end_mark);
  2121. exp = old ? old : "";
  2122. exp = input_dialog (_(" Run Sort "),
  2123. _(" Enter sort options (see manpage) separated by whitespace: "), exp);
  2124. if (!exp)
  2125. return 1;
  2126. g_free (old);
  2127. old = exp;
  2128. e = system (catstrs (" sort ", exp, " ", home_dir, BLOCK_FILE, " > ", home_dir, TEMP_FILE, (char *) NULL));
  2129. if (e) {
  2130. if (e == -1 || e == 127) {
  2131. edit_error_dialog (_(" Sort "),
  2132. get_sys_error (_(" Cannot execute sort command ")));
  2133. } else {
  2134. char q[8];
  2135. sprintf (q, "%d ", e);
  2136. edit_error_dialog (_(" Sort "),
  2137. catstrs (_(" Sort returned non-zero: "), q, (char *) NULL));
  2138. }
  2139. return -1;
  2140. }
  2141. edit->force |= REDRAW_COMPLETELY;
  2142. if (edit_block_delete_cmd (edit))
  2143. return 1;
  2144. edit_insert_file (edit, catstrs (home_dir, TEMP_FILE, (char *) NULL));
  2145. return 0;
  2146. }
  2147. /*
  2148. * Ask user for a command, execute it and paste its output back to the
  2149. * editor.
  2150. */
  2151. int
  2152. edit_ext_cmd (WEdit *edit)
  2153. {
  2154. char *exp;
  2155. int e;
  2156. exp =
  2157. input_dialog (_("Paste output of external command"),
  2158. _("Enter shell command(s):"), NULL);
  2159. if (!exp)
  2160. return 1;
  2161. e = system (catstrs (exp, " > ", home_dir, TEMP_FILE, (char *) NULL));
  2162. g_free (exp);
  2163. if (e) {
  2164. edit_error_dialog (_("External command"),
  2165. get_sys_error (_("Cannot execute command")));
  2166. return -1;
  2167. }
  2168. edit->force |= REDRAW_COMPLETELY;
  2169. edit_insert_file (edit, catstrs (home_dir, TEMP_FILE, (char *) NULL));
  2170. return 0;
  2171. }
  2172. /* if block is 1, a block must be highlighted and the shell command
  2173. processes it. If block is 0 the shell command is a straight system
  2174. command, that just produces some output which is to be inserted */
  2175. void
  2176. edit_block_process_cmd (WEdit *edit, const char *shell_cmd, int block)
  2177. {
  2178. long start_mark, end_mark;
  2179. char buf[BUFSIZ];
  2180. FILE *script_home = NULL;
  2181. FILE *script_src = NULL;
  2182. FILE *block_file = NULL;
  2183. const char *o = NULL;
  2184. const char *h = NULL;
  2185. const char *b = NULL;
  2186. char *quoted_name = NULL;
  2187. o = catstrs (mc_home, shell_cmd, (char *) NULL); /* original source script */
  2188. h = catstrs (home_dir, EDIT_DIR, shell_cmd, (char *) NULL); /* home script */
  2189. b = catstrs (home_dir, BLOCK_FILE, (char *) NULL); /* block file */
  2190. if (!(script_home = fopen (h, "r"))) {
  2191. if (!(script_home = fopen (h, "w"))) {
  2192. edit_error_dialog ("", get_sys_error (catstrs
  2193. (_
  2194. ("Error creating script:"),
  2195. h, (char *) NULL)));
  2196. return;
  2197. }
  2198. if (!(script_src = fopen (o, "r"))) {
  2199. fclose (script_home);
  2200. unlink (h);
  2201. edit_error_dialog ("", get_sys_error (catstrs
  2202. (_("Error reading script:"),
  2203. o, (char *) NULL)));
  2204. return;
  2205. }
  2206. while (fgets (buf, sizeof (buf), script_src))
  2207. fputs (buf, script_home);
  2208. if (fclose (script_home)) {
  2209. edit_error_dialog ("", get_sys_error (catstrs
  2210. (_
  2211. ("Error closing script:"),
  2212. h, (char *) NULL)));
  2213. return;
  2214. }
  2215. chmod (h, 0700);
  2216. edit_error_dialog ("", get_sys_error (catstrs
  2217. (_("Script created:"), h, (char *) NULL)));
  2218. }
  2219. open_error_pipe ();
  2220. if (block) { /* for marked block run indent formatter */
  2221. if (eval_marks (edit, &start_mark, &end_mark)) {
  2222. edit_error_dialog (_("Process block"),
  2223. _
  2224. (" You must first highlight a block of text. "));
  2225. return;
  2226. }
  2227. edit_save_block (edit, b, start_mark, end_mark);
  2228. quoted_name = name_quote (edit->filename, 0);
  2229. /*
  2230. * Run script.
  2231. * Initial space is to avoid polluting bash history.
  2232. * Arguments:
  2233. * $1 - name of the edited file (to check its extension etc).
  2234. * $2 - file containing the current block.
  2235. * $3 - file where error messages should be put
  2236. * (for compatibility with old scripts).
  2237. */
  2238. system (catstrs (" ", home_dir, EDIT_DIR, shell_cmd, " ", quoted_name,
  2239. " ", home_dir, BLOCK_FILE " /dev/null", (char *) NULL));
  2240. } else {
  2241. /*
  2242. * No block selected, just execute the command for the file.
  2243. * Arguments:
  2244. * $1 - name of the edited file.
  2245. */
  2246. system (catstrs (" ", home_dir, EDIT_DIR, shell_cmd, " ",
  2247. quoted_name, (char *) NULL));
  2248. }
  2249. g_free (quoted_name);
  2250. close_error_pipe (0, 0);
  2251. edit_refresh_cmd (edit);
  2252. edit->force |= REDRAW_COMPLETELY;
  2253. /* insert result block */
  2254. if (block) {
  2255. if (edit_block_delete_cmd (edit))
  2256. return;
  2257. edit_insert_file (edit, b);
  2258. if ((block_file = fopen (b, "w")))
  2259. fclose (block_file);
  2260. return;
  2261. }
  2262. return;
  2263. }
  2264. /* prints at the cursor */
  2265. /* returns the number of chars printed */
  2266. int edit_print_string (WEdit * e, const char *s)
  2267. {
  2268. int i = 0;
  2269. while (s[i])
  2270. edit_execute_cmd (e, -1, (unsigned char) s[i++]);
  2271. e->force |= REDRAW_COMPLETELY;
  2272. edit_update_screen (e);
  2273. return i;
  2274. }
  2275. static void pipe_mail (WEdit *edit, char *to, char *subject, char *cc)
  2276. {
  2277. FILE *p = 0;
  2278. char *s;
  2279. to = name_quote (to, 0);
  2280. subject = name_quote (subject, 0);
  2281. cc = name_quote (cc, 0);
  2282. s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "" , cc, " ", to, (char *) NULL);
  2283. g_free (to);
  2284. g_free (subject);
  2285. g_free (cc);
  2286. if (s) {
  2287. p = popen (s, "w");
  2288. g_free (s);
  2289. }
  2290. if (p) {
  2291. long i;
  2292. for (i = 0; i < edit->last_byte; i++)
  2293. fputc (edit_get_byte (edit, i), p);
  2294. pclose (p);
  2295. }
  2296. }
  2297. #define MAIL_DLG_HEIGHT 12
  2298. void edit_mail_dialog (WEdit * edit)
  2299. {
  2300. char *tmail_to;
  2301. char *tmail_subject;
  2302. char *tmail_cc;
  2303. static char *mail_cc_last = 0;
  2304. static char *mail_subject_last = 0;
  2305. static char *mail_to_last = 0;
  2306. QuickDialog Quick_input =
  2307. {50, MAIL_DLG_HEIGHT, -1, 0, N_(" Mail "),
  2308. "[Input Line Keys]", 0, 0};
  2309. QuickWidget quick_widgets[] =
  2310. {
  2311. {quick_button, 6, 10, 9, MAIL_DLG_HEIGHT, N_("&Cancel"), 0, B_CANCEL, 0,
  2312. 0, NULL},
  2313. {quick_button, 2, 10, 9, MAIL_DLG_HEIGHT, N_("&OK"), 0, B_ENTER, 0,
  2314. 0, NULL},
  2315. {quick_input, 3, 50, 8, MAIL_DLG_HEIGHT, "", 44, 0, 0,
  2316. 0, "mail-dlg-input"},
  2317. {quick_label, 2, 50, 7, MAIL_DLG_HEIGHT, N_(" Copies to"), 0, 0, 0,
  2318. 0, 0},
  2319. {quick_input, 3, 50, 6, MAIL_DLG_HEIGHT, "", 44, 0, 0,
  2320. 0, "mail-dlg-input-2"},
  2321. {quick_label, 2, 50, 5, MAIL_DLG_HEIGHT, N_(" Subject"), 0, 0, 0,
  2322. 0, 0},
  2323. {quick_input, 3, 50, 4, MAIL_DLG_HEIGHT, "", 44, 0, 0,
  2324. 0, "mail-dlg-input-3"},
  2325. {quick_label, 2, 50, 3, MAIL_DLG_HEIGHT, N_(" To"), 0, 0, 0,
  2326. 0, 0},
  2327. {quick_label, 2, 50, 2, MAIL_DLG_HEIGHT, N_(" mail -s <subject> -c <cc> <to>"), 0, 0, 0,
  2328. 0, 0},
  2329. NULL_QuickWidget};
  2330. quick_widgets[2].str_result = &tmail_cc;
  2331. quick_widgets[2].text = mail_cc_last ? mail_cc_last : "";
  2332. quick_widgets[4].str_result = &tmail_subject;
  2333. quick_widgets[4].text = mail_subject_last ? mail_subject_last : "";
  2334. quick_widgets[6].str_result = &tmail_to;
  2335. quick_widgets[6].text = mail_to_last ? mail_to_last : "";
  2336. Quick_input.widgets = quick_widgets;
  2337. if (quick_dialog (&Quick_input) != B_CANCEL) {
  2338. g_free (mail_cc_last);
  2339. g_free (mail_subject_last);
  2340. g_free (mail_to_last);
  2341. mail_cc_last = tmail_cc;
  2342. mail_subject_last = tmail_subject;
  2343. mail_to_last = tmail_to;
  2344. pipe_mail (edit, mail_to_last, mail_subject_last, mail_cc_last);
  2345. }
  2346. }
  2347. /*******************/
  2348. /* Word Completion */
  2349. /*******************/
  2350. /* find first character of current word */
  2351. static int edit_find_word_start (WEdit *edit, long *word_start, int *word_len)
  2352. {
  2353. int i, c, last;
  2354. /* return if at begin of file */
  2355. if (edit->curs1 <= 0)
  2356. return 0;
  2357. c = (unsigned char) edit_get_byte (edit, edit->curs1 - 1);
  2358. /* return if not at end or in word */
  2359. if (isspace (c) || !(isalnum (c) || c == '_'))
  2360. return 0;
  2361. /* search start of word to be completed */
  2362. for (i = 2;; i++) {
  2363. /* return if at begin of file */
  2364. if (edit->curs1 - i < 0)
  2365. return 0;
  2366. last = c;
  2367. c = (unsigned char) edit_get_byte (edit, edit->curs1 - i);
  2368. if (!(isalnum (c) || c == '_')) {
  2369. /* return if word starts with digit */
  2370. if (isdigit (last))
  2371. return 0;
  2372. *word_start = edit->curs1 - (i - 1); /* start found */
  2373. *word_len = i - 1;
  2374. break;
  2375. }
  2376. }
  2377. /* success */
  2378. return 1;
  2379. }
  2380. /* (re)set search parameters to the given values */
  2381. static void edit_set_search_parameters (int rs, int rb, int rr, int rw, int rc)
  2382. {
  2383. replace_scanf = rs;
  2384. replace_backwards = rb;
  2385. replace_regexp = rr;
  2386. replace_whole = rw;
  2387. replace_case = rc;
  2388. }
  2389. #define MAX_WORD_COMPLETIONS 100 /* in listbox */
  2390. /* collect the possible completions */
  2391. static int
  2392. edit_collect_completions (WEdit *edit, long start, int word_len,
  2393. char *match_expr, struct selection *compl,
  2394. int *num)
  2395. {
  2396. int len, max_len = 0, i, skip;
  2397. char *bufpos;
  2398. /* collect max MAX_WORD_COMPLETIONS completions */
  2399. while (*num < MAX_WORD_COMPLETIONS) {
  2400. /* get next match */
  2401. start =
  2402. edit_find (start - 1, (unsigned char *) match_expr, &len,
  2403. edit->last_byte,
  2404. edit_get_byte,
  2405. (void *) edit, 0);
  2406. /* not matched */
  2407. if (start < 0)
  2408. break;
  2409. /* add matched completion if not yet added */
  2410. bufpos =
  2411. &edit->
  2412. buffers1[start >> S_EDIT_BUF_SIZE][start & M_EDIT_BUF_SIZE];
  2413. skip = 0;
  2414. for (i = 0; i < *num; i++) {
  2415. if (strncmp
  2416. (&compl[i].text[word_len], &bufpos[word_len],
  2417. max (len, compl[i].len) - word_len) == 0) {
  2418. skip = 1;
  2419. break; /* skip it, already added */
  2420. }
  2421. }
  2422. if (skip)
  2423. continue;
  2424. compl[*num].text = g_malloc (len + 1);
  2425. compl[*num].len = len;
  2426. for (i = 0; i < len; i++)
  2427. compl[*num].text[i] = *(bufpos + i);
  2428. compl[*num].text[i] = '\0';
  2429. (*num)++;
  2430. /* note the maximal length needed for the completion dialog */
  2431. if (len > max_len)
  2432. max_len = len;
  2433. }
  2434. return max_len;
  2435. }
  2436. /* let the user select its preferred completion */
  2437. static void
  2438. edit_completion_dialog (WEdit *edit, int max_len, int word_len,
  2439. struct selection *compl, int num_compl)
  2440. {
  2441. int start_x, start_y, offset, i;
  2442. char *curr = NULL;
  2443. Dlg_head *compl_dlg;
  2444. WListbox *compl_list;
  2445. int compl_dlg_h; /* completion dialog height */
  2446. int compl_dlg_w; /* completion dialog width */
  2447. /* calculate the dialog metrics */
  2448. compl_dlg_h = num_compl + 2;
  2449. compl_dlg_w = max_len + 4;
  2450. start_x = edit->curs_col + edit->start_col - (compl_dlg_w / 2);
  2451. start_y = edit->curs_row + EDIT_TEXT_VERTICAL_OFFSET + 1;
  2452. if (start_x < 0)
  2453. start_x = 0;
  2454. if (compl_dlg_w > COLS)
  2455. compl_dlg_w = COLS;
  2456. if (compl_dlg_h > LINES - 2)
  2457. compl_dlg_h = LINES - 2;
  2458. offset = start_x + compl_dlg_w - COLS;
  2459. if (offset > 0)
  2460. start_x -= offset;
  2461. offset = start_y + compl_dlg_h - LINES;
  2462. if (offset > 0)
  2463. start_y -= (offset + 1);
  2464. /* create the dialog */
  2465. compl_dlg =
  2466. create_dlg (start_y, start_x, compl_dlg_h, compl_dlg_w,
  2467. dialog_colors, NULL, "[Completion]", NULL,
  2468. DLG_COMPACT);
  2469. /* create the listbox */
  2470. compl_list =
  2471. listbox_new (1, 1, compl_dlg_w - 2, compl_dlg_h - 2, NULL);
  2472. /* add the dialog */
  2473. add_widget (compl_dlg, compl_list);
  2474. /* fill the listbox with the completions */
  2475. for (i = 0; i < num_compl; i++)
  2476. listbox_add_item (compl_list, 0, 0, compl[i].text, NULL);
  2477. /* pop up the dialog */
  2478. run_dlg (compl_dlg);
  2479. /* apply the choosen completion */
  2480. if (compl_dlg->ret_value == B_ENTER) {
  2481. listbox_get_current (compl_list, &curr, NULL);
  2482. if (curr)
  2483. for (curr += word_len; *curr; curr++)
  2484. edit_insert (edit, *curr);
  2485. }
  2486. /* destroy dialog before return */
  2487. destroy_dlg (compl_dlg);
  2488. }
  2489. /*
  2490. * Complete current word using regular expression search
  2491. * backwards beginning at the current cursor position.
  2492. */
  2493. void
  2494. edit_complete_word_cmd (WEdit *edit)
  2495. {
  2496. int word_len = 0, i, num_compl = 0, max_len;
  2497. long word_start = 0;
  2498. char *bufpos;
  2499. char *match_expr;
  2500. struct selection compl[MAX_WORD_COMPLETIONS]; /* completions */
  2501. /* don't want to disturb another search */
  2502. int old_rs = replace_scanf;
  2503. int old_rb = replace_backwards;
  2504. int old_rr = replace_regexp;
  2505. int old_rw = replace_whole;
  2506. int old_rc = replace_case;
  2507. /* search start of word to be completed */
  2508. if (!edit_find_word_start (edit, &word_start, &word_len))
  2509. return;
  2510. /* prepare match expression */
  2511. bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE]
  2512. [word_start & M_EDIT_BUF_SIZE];
  2513. match_expr = g_strdup_printf ("%.*s[a-zA-Z_0-9]+", word_len, bufpos);
  2514. /* init search: backward, regexp, whole word, case sensitive */
  2515. edit_set_search_parameters (0, 1, 1, 1, 1);
  2516. /* collect the possible completions */
  2517. /* start search from curs1 down to begin of file */
  2518. max_len =
  2519. edit_collect_completions (edit, word_start, word_len, match_expr,
  2520. (struct selection *) &compl, &num_compl);
  2521. if (num_compl > 0) {
  2522. /* insert completed word if there is only one match */
  2523. if (num_compl == 1) {
  2524. for (i = word_len; i < compl[0].len; i++)
  2525. edit_insert (edit, *(compl[0].text + i));
  2526. }
  2527. /* more than one possible completion => ask the user */
  2528. else {
  2529. /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
  2530. /* !!! pressed again the selection dialog pops up, but that !!! */
  2531. /* !!! seems to require a further internal state !!! */
  2532. /*beep (); */
  2533. /* let the user select the preferred completion */
  2534. edit_completion_dialog (edit, max_len, word_len,
  2535. (struct selection *) &compl,
  2536. num_compl);
  2537. }
  2538. }
  2539. g_free (match_expr);
  2540. /* release memory before return */
  2541. for (i = 0; i < num_compl; i++)
  2542. g_free (compl[i].text);
  2543. /* restore search parameters */
  2544. edit_set_search_parameters (old_rs, old_rb, old_rr, old_rw, old_rc);
  2545. }