editcmd.c 73 KB

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