editcmd.c 70 KB

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