editcmd.c 70 KB

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