editcmd.c 73 KB

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