editcmd.c 84 KB

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