editcmd.c 75 KB

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