editcmd.c 66 KB

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