editcmd.c 69 KB

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