editcmd.c 69 KB

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