editcmd.c 69 KB

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