editcmd.c 76 KB

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