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