editcmd.c 76 KB

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