editcmd.c 69 KB

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