editcmd.c 66 KB

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