editcmd.c 72 KB

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