editcmd.c 70 KB

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