editcmd.c 80 KB

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