edit.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955
  1. /* editor low level data handling and cursor fundamentals.
  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. #include <config.h>
  19. #include <stdio.h>
  20. #include <stdarg.h>
  21. #include <sys/types.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include <errno.h>
  26. #include <sys/stat.h>
  27. #include <stdlib.h>
  28. #include "../src/global.h"
  29. #include "edit.h"
  30. #include "editlock.h"
  31. #include "edit-widget.h"
  32. #include "editcmddef.h"
  33. #include "usermap.h"
  34. #include "../src/cmd.h" /* view_other_cmd() */
  35. #include "../src/user.h" /* user_menu_cmd() */
  36. #include "../src/wtools.h" /* query_dialog() */
  37. #include "../src/timefmt.h" /* time formatting */
  38. #include "../src/strutil.h" /* utf string functions */
  39. #include "../src/charsets.h" /* get_codepage_id */
  40. #include "../src/main.h" /* source_codepage */
  41. /*
  42. what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
  43. or EDIT_KEY_EMULATION_EMACS
  44. */
  45. int edit_key_emulation = EDIT_KEY_EMULATION_NORMAL;
  46. int option_word_wrap_line_length = 72;
  47. int option_typewriter_wrap = 0;
  48. int option_auto_para_formatting = 0;
  49. int option_tab_spacing = 8;
  50. int option_fill_tabs_with_spaces = 0;
  51. int option_return_does_auto_indent = 1;
  52. int option_backspace_through_tabs = 0;
  53. int option_fake_half_tabs = 1;
  54. int option_save_mode = EDIT_QUICK_SAVE;
  55. int option_save_position = 1;
  56. int option_max_undo = 32768;
  57. int option_persistent_selections = 1;
  58. int option_edit_right_extreme = 0;
  59. int option_edit_left_extreme = 0;
  60. int option_edit_top_extreme = 0;
  61. int option_edit_bottom_extreme = 0;
  62. const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
  63. char *option_backup_ext = NULL;
  64. /*-
  65. *
  66. * here's a quick sketch of the layout: (don't run this through indent.)
  67. *
  68. * (b1 is buffers1 and b2 is buffers2)
  69. *
  70. * |
  71. * \0\0\0\0\0m e _ f i l e . \nf i n . \n|T h i s _ i s _ s o\0\0\0\0\0\0\0\0\0
  72. * ______________________________________|______________________________________
  73. * |
  74. * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
  75. * |-> |-> |-> |-> |-> |-> |
  76. * |
  77. * _<------------------------->|<----------------->_
  78. * WEdit->curs2 | WEdit->curs1
  79. * ^ | ^
  80. * | ^|^ |
  81. * cursor ||| cursor
  82. * |||
  83. * file end|||file beginning
  84. * |
  85. * |
  86. *
  87. * _
  88. * This_is_some_file
  89. * fin.
  90. */
  91. static void user_menu (WEdit *edit);
  92. int edit_get_byte (WEdit * edit, long byte_index)
  93. {
  94. unsigned long p;
  95. if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
  96. return '\n';
  97. if (byte_index >= edit->curs1) {
  98. p = edit->curs1 + edit->curs2 - byte_index - 1;
  99. return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
  100. } else {
  101. return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
  102. }
  103. }
  104. char *edit_get_byte_ptr (WEdit * edit, long byte_index)
  105. {
  106. unsigned long p;
  107. if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
  108. return NULL;
  109. if (byte_index >= edit->curs1) {
  110. p = edit->curs1 + edit->curs2 - byte_index - 1;
  111. return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE]+(EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
  112. } else {
  113. return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE]+(byte_index & M_EDIT_BUF_SIZE));
  114. }
  115. }
  116. char *edit_get_buf_ptr (WEdit * edit, long byte_index)
  117. {
  118. unsigned long p;
  119. if (byte_index >= (edit->curs1 + edit->curs2) ) {
  120. byte_index -= 1;
  121. };
  122. if ( byte_index < 0 ) {
  123. return NULL;
  124. }
  125. if (byte_index >= edit->curs1) {
  126. p = edit->curs1 + edit->curs2 - 1;
  127. return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] + (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
  128. } else {
  129. return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
  130. }
  131. }
  132. int edit_get_utf (WEdit * edit, long byte_index, int *char_width)
  133. {
  134. gchar *str = NULL;
  135. int res = -1;
  136. gunichar ch;
  137. gchar *next_ch = NULL;
  138. int width = 0;
  139. if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) {
  140. *char_width = 0;
  141. return '\n';
  142. }
  143. str = edit_get_byte_ptr (edit, byte_index);
  144. res = g_utf8_get_char_validated (str, -1);
  145. if ( res < 0 ) {
  146. ch = *str;
  147. width = 0;
  148. } else {
  149. ch = res;
  150. /* Calculate UTF-8 char width */
  151. next_ch = g_utf8_next_char(str);
  152. if ( next_ch ) {
  153. if ( next_ch != str ) {
  154. width = next_ch - str;
  155. } else {
  156. width = 0;
  157. }
  158. } else {
  159. ch = 0;
  160. width = 0;
  161. }
  162. }
  163. *char_width = width;
  164. return ch;
  165. }
  166. int edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
  167. {
  168. gchar *str, *buf = NULL;
  169. int res = -1;
  170. gunichar ch;
  171. gchar *next_ch = NULL;
  172. int width = 0;
  173. gchar *prn_buf=NULL;
  174. if ( byte_index > 0 ) {
  175. byte_index--;
  176. }
  177. ch = edit_get_utf (edit, byte_index, &width);
  178. if ( width == 1 ) {
  179. *char_width = width;
  180. return ch;
  181. }
  182. if ( byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0 ) {
  183. *char_width = 1;
  184. return 0;
  185. }
  186. str = edit_get_byte_ptr (edit, byte_index);
  187. buf = edit_get_buf_ptr (edit, byte_index);
  188. /* get prev utf8 char */
  189. str = g_utf8_find_prev_char (buf, str);
  190. res = g_utf8_get_char_validated (str, -1);
  191. if ( res < 0 ) {
  192. ch = *str;
  193. width = 0;
  194. } else {
  195. ch = res;
  196. /* Calculate UTF-8 char width */
  197. next_ch = g_utf8_next_char(str);
  198. if ( next_ch ) {
  199. if ( next_ch != str ) {
  200. width = next_ch - str;
  201. } else {
  202. width = 0;
  203. }
  204. } else {
  205. ch = 0;
  206. width = 0;
  207. }
  208. }
  209. *char_width = width;
  210. return ch;
  211. }
  212. /*
  213. * Initialize the buffers for an empty files.
  214. */
  215. static void
  216. edit_init_buffers (WEdit *edit)
  217. {
  218. int j;
  219. for (j = 0; j <= MAXBUFF; j++) {
  220. edit->buffers1[j] = NULL;
  221. edit->buffers2[j] = NULL;
  222. }
  223. edit->curs1 = 0;
  224. edit->curs2 = 0;
  225. edit->buffers2[0] = g_malloc (EDIT_BUF_SIZE);
  226. }
  227. /*
  228. * Load file OR text into buffers. Set cursor to the beginning of file.
  229. * Return 1 on error.
  230. */
  231. static int
  232. edit_load_file_fast (WEdit *edit, const char *filename)
  233. {
  234. long buf, buf2;
  235. int file = -1;
  236. edit->curs2 = edit->last_byte;
  237. buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
  238. edit->utf8 = str_isutf8 (get_codepage_id( source_codepage ));
  239. if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
  240. GString *errmsg = g_string_new(NULL);
  241. g_string_sprintf(errmsg, _(" Cannot open %s for reading "), filename);
  242. edit_error_dialog (_("Error"), get_sys_error (errmsg->str));
  243. g_string_free (errmsg, TRUE);
  244. return 1;
  245. }
  246. if (!edit->buffers2[buf2])
  247. edit->buffers2[buf2] = g_malloc (EDIT_BUF_SIZE);
  248. mc_read (file,
  249. (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
  250. (edit->curs2 & M_EDIT_BUF_SIZE),
  251. edit->curs2 & M_EDIT_BUF_SIZE);
  252. for (buf = buf2 - 1; buf >= 0; buf--) {
  253. /* edit->buffers2[0] is already allocated */
  254. if (!edit->buffers2[buf])
  255. edit->buffers2[buf] = g_malloc (EDIT_BUF_SIZE);
  256. mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
  257. }
  258. mc_close (file);
  259. return 0;
  260. }
  261. /* detecting an error on save is easy: just check if every byte has been written. */
  262. /* detecting an error on read, is not so easy 'cos there is not way to tell
  263. whether you read everything or not. */
  264. /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
  265. static const struct edit_filters {
  266. const char *read, *write, *extension;
  267. } all_filters[] = {
  268. { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
  269. { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
  270. { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
  271. { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
  272. };
  273. /* Return index of the filter or -1 is there is no appropriate filter */
  274. static int edit_find_filter (const char *filename)
  275. {
  276. size_t i, l, e;
  277. if (!filename)
  278. return -1;
  279. l = strlen (filename);
  280. for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++) {
  281. e = strlen (all_filters[i].extension);
  282. if (l > e)
  283. if (!strcmp (all_filters[i].extension, filename + l - e))
  284. return i;
  285. }
  286. return -1;
  287. }
  288. static char *
  289. edit_get_filter (const char *filename)
  290. {
  291. int i, l;
  292. char *p, *quoted_name;
  293. i = edit_find_filter (filename);
  294. if (i < 0)
  295. return 0;
  296. quoted_name = name_quote (filename, 0);
  297. l = str_term_width1 (quoted_name);
  298. p = g_malloc (str_term_width1 (all_filters[i].read) + l + 2);
  299. sprintf (p, all_filters[i].read, quoted_name);
  300. g_free (quoted_name);
  301. return p;
  302. }
  303. char *
  304. edit_get_write_filter (const char *write_name, const char *filename)
  305. {
  306. int i, l;
  307. char *p, *writename;
  308. i = edit_find_filter (filename);
  309. if (i < 0)
  310. return 0;
  311. writename = name_quote (write_name, 0);
  312. l = str_term_width1 (writename);
  313. p = g_malloc (str_term_width1 (all_filters[i].write) + l + 2);
  314. sprintf (p, all_filters[i].write, writename);
  315. g_free (writename);
  316. return p;
  317. }
  318. static long
  319. edit_insert_stream (WEdit * edit, FILE * f)
  320. {
  321. int c;
  322. long i = 0;
  323. while ((c = fgetc (f)) >= 0) {
  324. edit_insert (edit, c);
  325. i++;
  326. }
  327. return i;
  328. }
  329. long edit_write_stream (WEdit * edit, FILE * f)
  330. {
  331. long i;
  332. for (i = 0; i < edit->last_byte; i++)
  333. if (fputc (edit_get_byte (edit, i), f) < 0)
  334. break;
  335. return i;
  336. }
  337. #define TEMP_BUF_LEN 1024
  338. /* inserts a file at the cursor, returns 1 on success */
  339. int
  340. edit_insert_file (WEdit *edit, const char *filename)
  341. {
  342. char *p;
  343. if ((p = edit_get_filter (filename))) {
  344. FILE *f;
  345. long current = edit->curs1;
  346. f = (FILE *) popen (p, "r");
  347. if (f) {
  348. edit_insert_stream (edit, f);
  349. edit_cursor_move (edit, current - edit->curs1);
  350. if (pclose (f) > 0) {
  351. GString *errmsg = g_string_new (NULL);
  352. g_string_sprintf (errmsg, _(" Error reading from pipe: %s "), p);
  353. edit_error_dialog (_("Error"), errmsg->str);
  354. g_string_free (errmsg, TRUE);
  355. g_free (p);
  356. return 0;
  357. }
  358. } else {
  359. GString *errmsg = g_string_new (NULL);
  360. g_string_sprintf (errmsg, _(" Cannot open pipe for reading: %s "), p);
  361. edit_error_dialog (_("Error"), errmsg->str);
  362. g_string_free (errmsg, TRUE);
  363. g_free (p);
  364. return 0;
  365. }
  366. g_free (p);
  367. } else {
  368. int i, file, blocklen;
  369. long current = edit->curs1;
  370. unsigned char *buf;
  371. if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
  372. return 0;
  373. buf = g_malloc (TEMP_BUF_LEN);
  374. while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
  375. for (i = 0; i < blocklen; i++)
  376. edit_insert (edit, buf[i]);
  377. }
  378. edit_cursor_move (edit, current - edit->curs1);
  379. g_free (buf);
  380. mc_close (file);
  381. if (blocklen)
  382. return 0;
  383. }
  384. return 1;
  385. }
  386. /* Open file and create it if necessary. Return 0 for success, 1 for error. */
  387. static int
  388. check_file_access (WEdit *edit, const char *filename, struct stat *st)
  389. {
  390. int file;
  391. GString *errmsg = (GString *) 0;
  392. /* Try opening an existing file */
  393. file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
  394. if (file < 0) {
  395. /*
  396. * Try creating the file. O_EXCL prevents following broken links
  397. * and opening existing files.
  398. */
  399. file =
  400. mc_open (filename,
  401. O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL,
  402. 0666);
  403. if (file < 0) {
  404. g_string_sprintf (errmsg = g_string_new (NULL),
  405. _(" Cannot open %s for reading "), filename);
  406. goto cleanup;
  407. } else {
  408. /* New file, delete it if it's not modified or saved */
  409. edit->delete_file = 1;
  410. }
  411. }
  412. /* Check what we have opened */
  413. if (mc_fstat (file, st) < 0) {
  414. g_string_sprintf (errmsg = g_string_new (NULL),
  415. _(" Cannot get size/permissions for %s "), filename);
  416. goto cleanup;
  417. }
  418. /* We want to open regular files only */
  419. if (!S_ISREG (st->st_mode)) {
  420. g_string_sprintf (errmsg = g_string_new (NULL),
  421. _(" %s is not a regular file "), filename);
  422. goto cleanup;
  423. }
  424. /*
  425. * Don't delete non-empty files.
  426. * O_EXCL should prevent it, but let's be on the safe side.
  427. */
  428. if (st->st_size > 0) {
  429. edit->delete_file = 0;
  430. }
  431. if (st->st_size >= SIZE_LIMIT) {
  432. g_string_sprintf (errmsg = g_string_new (NULL),
  433. _(" File %s is too large "), filename);
  434. goto cleanup;
  435. }
  436. cleanup:
  437. (void) mc_close (file);
  438. if (errmsg) {
  439. edit_error_dialog (_("Error"), errmsg->str);
  440. g_string_free (errmsg, TRUE);
  441. return 1;
  442. }
  443. return 0;
  444. }
  445. /*
  446. * Open the file and load it into the buffers, either directly or using
  447. * a filter. Return 0 on success, 1 on error.
  448. *
  449. * Fast loading (edit_load_file_fast) is used when the file size is
  450. * known. In this case the data is read into the buffers by blocks.
  451. * If the file size is not known, the data is loaded byte by byte in
  452. * edit_insert_file.
  453. */
  454. static int
  455. edit_load_file (WEdit *edit)
  456. {
  457. int fast_load = 1;
  458. /* Cannot do fast load if a filter is used */
  459. if (edit_find_filter (edit->filename) >= 0)
  460. fast_load = 0;
  461. /*
  462. * VFS may report file size incorrectly, and slow load is not a big
  463. * deal considering overhead in VFS.
  464. */
  465. if (!vfs_file_is_local (edit->filename))
  466. fast_load = 0;
  467. /*
  468. * FIXME: line end translation should disable fast loading as well
  469. * Consider doing fseek() to the end and ftell() for the real size.
  470. */
  471. if (*edit->filename) {
  472. /* If we are dealing with a real file, check that it exists */
  473. if (check_file_access (edit, edit->filename, &edit->stat1))
  474. return 1;
  475. } else {
  476. /* nothing to load */
  477. fast_load = 0;
  478. }
  479. edit_init_buffers (edit);
  480. if (fast_load) {
  481. edit->last_byte = edit->stat1.st_size;
  482. edit_load_file_fast (edit, edit->filename);
  483. /* If fast load was used, the number of lines wasn't calculated */
  484. edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
  485. } else {
  486. edit->last_byte = 0;
  487. if (*edit->filename) {
  488. edit->stack_disable = 1;
  489. if (!edit_insert_file (edit, edit->filename)) {
  490. edit_clean (edit);
  491. return 1;
  492. }
  493. edit->stack_disable = 0;
  494. }
  495. }
  496. return 0;
  497. }
  498. /* Restore saved cursor position in the file */
  499. static void
  500. edit_load_position (WEdit *edit)
  501. {
  502. char *filename;
  503. long line, column;
  504. if (!edit->filename || !*edit->filename)
  505. return;
  506. filename = vfs_canon (edit->filename);
  507. load_file_position (filename, &line, &column);
  508. g_free (filename);
  509. edit_move_to_line (edit, line - 1);
  510. edit->prev_col = column;
  511. edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
  512. edit_move_display (edit, line - (edit->num_widget_lines / 2));
  513. }
  514. /* Save cursor position in the file */
  515. static void
  516. edit_save_position (WEdit *edit)
  517. {
  518. char *filename;
  519. if (!edit->filename || !*edit->filename)
  520. return;
  521. filename = vfs_canon (edit->filename);
  522. save_file_position (filename, edit->curs_line + 1, edit->curs_col);
  523. g_free (filename);
  524. }
  525. /* Clean the WEdit stricture except the widget part */
  526. static inline void
  527. edit_purge_widget (WEdit *edit)
  528. {
  529. int len = sizeof (WEdit) - sizeof (Widget);
  530. char *start = (char *) edit + sizeof (Widget);
  531. memset (start, 0, len);
  532. edit->macro_i = -1; /* not recording a macro */
  533. }
  534. #define space_width 1
  535. /*
  536. * Fill in the edit structure. Return NULL on failure. Pass edit as
  537. * NULL to allocate a new structure.
  538. *
  539. * If line is 0, try to restore saved position. Otherwise put the
  540. * cursor on that line and show it in the middle of the screen.
  541. */
  542. WEdit *
  543. edit_init (WEdit *edit, int lines, int columns, const char *filename,
  544. long line)
  545. {
  546. int to_free = 0;
  547. option_auto_syntax = 1; /* Resetting to auto on every invokation */
  548. if (!edit) {
  549. #ifdef ENABLE_NLS
  550. /*
  551. * Expand option_whole_chars_search by national letters using
  552. * current locale
  553. */
  554. static char option_whole_chars_search_buf[256];
  555. if (option_whole_chars_search_buf != option_whole_chars_search) {
  556. size_t i;
  557. size_t len = str_term_width1 (option_whole_chars_search);
  558. strcpy (option_whole_chars_search_buf,
  559. option_whole_chars_search);
  560. for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
  561. if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i)) {
  562. option_whole_chars_search_buf[len++] = i;
  563. }
  564. }
  565. option_whole_chars_search_buf[len] = 0;
  566. option_whole_chars_search = option_whole_chars_search_buf;
  567. }
  568. #endif /* ENABLE_NLS */
  569. edit = g_malloc0 (sizeof (WEdit));
  570. to_free = 1;
  571. }
  572. edit_purge_widget (edit);
  573. edit->num_widget_lines = lines;
  574. edit->num_widget_columns = columns;
  575. edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  576. edit->stat1.st_uid = getuid ();
  577. edit->stat1.st_gid = getgid ();
  578. edit->stat1.st_mtime = 0;
  579. edit->bracket = -1;
  580. edit->force |= REDRAW_PAGE;
  581. edit_set_filename (edit, filename);
  582. edit->stack_size = START_STACK_SIZE;
  583. edit->stack_size_mask = START_STACK_SIZE - 1;
  584. edit->undo_stack = g_malloc ((edit->stack_size + 10) * sizeof (long));
  585. if (edit_load_file (edit)) {
  586. /* edit_load_file already gives an error message */
  587. if (to_free)
  588. g_free (edit);
  589. return 0;
  590. }
  591. edit->loading_done = 1;
  592. edit->modified = 0;
  593. edit->locked = 0;
  594. edit_load_syntax (edit, 0, 0);
  595. {
  596. int color;
  597. edit_get_syntax_color (edit, -1, &color);
  598. }
  599. /* load saved cursor position */
  600. if ((line == 0) && option_save_position) {
  601. edit_load_position (edit);
  602. } else {
  603. if (line <= 0)
  604. line = 1;
  605. edit_move_display (edit, line - 1);
  606. edit_move_to_line (edit, line - 1);
  607. }
  608. edit_load_user_map(edit);
  609. return edit;
  610. }
  611. /* Clear the edit struct, freeing everything in it. Return 1 on success */
  612. int
  613. edit_clean (WEdit *edit)
  614. {
  615. int j = 0;
  616. if (!edit)
  617. return 0;
  618. /* a stale lock, remove it */
  619. if (edit->locked)
  620. edit->locked = edit_unlock_file (edit->filename);
  621. /* save cursor position */
  622. if (option_save_position)
  623. edit_save_position (edit);
  624. /* File specified on the mcedit command line and never saved */
  625. if (edit->delete_file)
  626. unlink (edit->filename);
  627. edit_free_syntax_rules (edit);
  628. book_mark_flush (edit, -1);
  629. for (; j <= MAXBUFF; j++) {
  630. g_free (edit->buffers1[j]);
  631. g_free (edit->buffers2[j]);
  632. }
  633. g_free (edit->undo_stack);
  634. g_free (edit->filename);
  635. g_free (edit->dir);
  636. edit_purge_widget (edit);
  637. return 1;
  638. }
  639. /* returns 1 on success */
  640. int edit_renew (WEdit * edit)
  641. {
  642. int lines = edit->num_widget_lines;
  643. int columns = edit->num_widget_columns;
  644. int retval = 1;
  645. edit_clean (edit);
  646. if (!edit_init (edit, lines, columns, "", 0))
  647. retval = 0;
  648. return retval;
  649. }
  650. /*
  651. * Load a new file into the editor. If it fails, preserve the old file.
  652. * To do it, allocate a new widget, initialize it and, if the new file
  653. * was loaded, copy the data to the old widget.
  654. * Return 1 on success, 0 on failure.
  655. */
  656. int
  657. edit_reload (WEdit *edit, const char *filename)
  658. {
  659. WEdit *e;
  660. int lines = edit->num_widget_lines;
  661. int columns = edit->num_widget_columns;
  662. e = g_malloc0 (sizeof (WEdit));
  663. e->widget = edit->widget;
  664. if (!edit_init (e, lines, columns, filename, 0)) {
  665. g_free (e);
  666. return 0;
  667. }
  668. edit_clean (edit);
  669. memcpy (edit, e, sizeof (WEdit));
  670. g_free (e);
  671. return 1;
  672. }
  673. /*
  674. * Load a new file into the editor and set line. If it fails, preserve the old file.
  675. * To do it, allocate a new widget, initialize it and, if the new file
  676. * was loaded, copy the data to the old widget.
  677. * Return 1 on success, 0 on failure.
  678. */
  679. int
  680. edit_reload_line (WEdit *edit, const char *filename, long line)
  681. {
  682. WEdit *e;
  683. int lines = edit->num_widget_lines;
  684. int columns = edit->num_widget_columns;
  685. e = g_malloc0 (sizeof (WEdit));
  686. e->widget = edit->widget;
  687. if (!edit_init (e, lines, columns, filename, line)) {
  688. g_free (e);
  689. return 0;
  690. }
  691. edit_clean (edit);
  692. memcpy (edit, e, sizeof (WEdit));
  693. g_free (e);
  694. return 1;
  695. }
  696. /*
  697. Recording stack for undo:
  698. The following is an implementation of a compressed stack. Identical
  699. pushes are recorded by a negative prefix indicating the number of times the
  700. same char was pushed. This saves space for repeated curs-left or curs-right
  701. delete etc.
  702. eg:
  703. pushed: stored:
  704. a
  705. b a
  706. b -3
  707. b b
  708. c --> -4
  709. c c
  710. c d
  711. c
  712. d
  713. If the stack long int is 0-255 it represents a normal insert (from a backspace),
  714. 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
  715. of the cursor functions #define'd in edit.h. 1000 through 700'000'000 is to
  716. set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
  717. position.
  718. The only way the cursor moves or the buffer is changed is through the routines:
  719. insert, backspace, insert_ahead, delete, and cursor_move.
  720. These record the reverse undo movements onto the stack each time they are
  721. called.
  722. Each key press results in a set of actions (insert; delete ...). So each time
  723. a key is pressed the current position of start_display is pushed as
  724. KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
  725. over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
  726. tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
  727. */
  728. void edit_push_action (WEdit * edit, long c,...)
  729. {
  730. unsigned long sp = edit->stack_pointer;
  731. unsigned long spm1;
  732. long *t;
  733. /* first enlarge the stack if necessary */
  734. if (sp > edit->stack_size - 10) { /* say */
  735. if (option_max_undo < 256)
  736. option_max_undo = 256;
  737. if (edit->stack_size < (unsigned long) option_max_undo) {
  738. t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
  739. if (t) {
  740. edit->undo_stack = t;
  741. edit->stack_size <<= 1;
  742. edit->stack_size_mask = edit->stack_size - 1;
  743. }
  744. }
  745. }
  746. spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
  747. if (edit->stack_disable)
  748. return;
  749. #ifdef FAST_MOVE_CURSOR
  750. if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
  751. va_list ap;
  752. edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
  753. edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
  754. va_start (ap, c);
  755. c = -(va_arg (ap, int));
  756. va_end (ap);
  757. } else
  758. #endif /* ! FAST_MOVE_CURSOR */
  759. if (edit->stack_bottom != sp
  760. && spm1 != edit->stack_bottom
  761. && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
  762. int d;
  763. if (edit->undo_stack[spm1] < 0) {
  764. d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
  765. if (d == c) {
  766. if (edit->undo_stack[spm1] > -1000000000) {
  767. if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
  768. edit->undo_stack[spm1]--;
  769. return;
  770. }
  771. }
  772. /* #define NO_STACK_CURSMOVE_ANIHILATION */
  773. #ifndef NO_STACK_CURSMOVE_ANIHILATION
  774. else if ((c == CURS_LEFT && d == CURS_RIGHT)
  775. || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
  776. if (edit->undo_stack[spm1] == -2)
  777. edit->stack_pointer = spm1;
  778. else
  779. edit->undo_stack[spm1]++;
  780. return;
  781. }
  782. #endif
  783. } else {
  784. d = edit->undo_stack[spm1];
  785. if (d == c) {
  786. if (c >= KEY_PRESS)
  787. return; /* --> no need to push multiple do-nothings */
  788. edit->undo_stack[sp] = -2;
  789. goto check_bottom;
  790. }
  791. #ifndef NO_STACK_CURSMOVE_ANIHILATION
  792. else if ((c == CURS_LEFT && d == CURS_RIGHT)
  793. || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
  794. edit->stack_pointer = spm1;
  795. return;
  796. }
  797. #endif
  798. }
  799. }
  800. edit->undo_stack[sp] = c;
  801. check_bottom:
  802. edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
  803. /* if the sp wraps round and catches the stack_bottom then erase
  804. * the first set of actions on the stack to make space - by moving
  805. * stack_bottom forward one "key press" */
  806. c = (edit->stack_pointer + 2) & edit->stack_size_mask;
  807. if ((unsigned long) c == edit->stack_bottom ||
  808. (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
  809. do {
  810. edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
  811. } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
  812. /*If a single key produced enough pushes to wrap all the way round then we would notice that the [stack_bottom] does not contain KEY_PRESS. The stack is then initialised: */
  813. if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
  814. edit->stack_bottom = edit->stack_pointer = 0;
  815. }
  816. /*
  817. TODO: if the user undos until the stack bottom, and the stack has not wrapped,
  818. then the file should be as it was when he loaded up. Then set edit->modified to 0.
  819. */
  820. static long
  821. pop_action (WEdit * edit)
  822. {
  823. long c;
  824. unsigned long sp = edit->stack_pointer;
  825. if (sp == edit->stack_bottom) {
  826. return STACK_BOTTOM;
  827. }
  828. sp = (sp - 1) & edit->stack_size_mask;
  829. if ((c = edit->undo_stack[sp]) >= 0) {
  830. /* edit->undo_stack[sp] = '@'; */
  831. edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
  832. return c;
  833. }
  834. if (sp == edit->stack_bottom) {
  835. return STACK_BOTTOM;
  836. }
  837. c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
  838. if (edit->undo_stack[sp] == -2) {
  839. /* edit->undo_stack[sp] = '@'; */
  840. edit->stack_pointer = sp;
  841. } else
  842. edit->undo_stack[sp]++;
  843. return c;
  844. }
  845. /* is called whenever a modification is made by one of the four routines below */
  846. static inline void edit_modification (WEdit * edit)
  847. {
  848. edit->caches_valid = 0;
  849. edit->screen_modified = 1;
  850. /* raise lock when file modified */
  851. if (!edit->modified && !edit->delete_file)
  852. edit->locked = edit_lock_file (edit->filename);
  853. edit->modified = 1;
  854. }
  855. /*
  856. Basic low level single character buffer alterations and movements at the cursor.
  857. Returns char passed over, inserted or removed.
  858. */
  859. void
  860. edit_insert (WEdit *edit, int c)
  861. {
  862. /* check if file has grown to large */
  863. if (edit->last_byte >= SIZE_LIMIT)
  864. return;
  865. /* first we must update the position of the display window */
  866. if (edit->curs1 < edit->start_display) {
  867. edit->start_display++;
  868. if (c == '\n')
  869. edit->start_line++;
  870. }
  871. /* Mark file as modified, unless the file hasn't been fully loaded */
  872. if (edit->loading_done) {
  873. edit_modification (edit);
  874. }
  875. /* now we must update some info on the file and check if a redraw is required */
  876. if (c == '\n') {
  877. if (edit->book_mark)
  878. book_mark_inc (edit, edit->curs_line);
  879. edit->curs_line++;
  880. edit->total_lines++;
  881. edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
  882. }
  883. /* save the reverse command onto the undo stack */
  884. edit_push_action (edit, BACKSPACE);
  885. /* update markers */
  886. edit->mark1 += (edit->mark1 > edit->curs1);
  887. edit->mark2 += (edit->mark2 > edit->curs1);
  888. edit->last_get_rule += (edit->last_get_rule > edit->curs1);
  889. /* add a new buffer if we've reached the end of the last one */
  890. if (!(edit->curs1 & M_EDIT_BUF_SIZE))
  891. edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] =
  892. g_malloc (EDIT_BUF_SIZE);
  893. /* perform the insertion */
  894. edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->
  895. curs1 & M_EDIT_BUF_SIZE]
  896. = (unsigned char) c;
  897. /* update file length */
  898. edit->last_byte++;
  899. /* update cursor position */
  900. edit->curs1++;
  901. }
  902. /* same as edit_insert and move left */
  903. void edit_insert_ahead (WEdit * edit, int c)
  904. {
  905. if (edit->last_byte >= SIZE_LIMIT)
  906. return;
  907. if (edit->curs1 < edit->start_display) {
  908. edit->start_display++;
  909. if (c == '\n')
  910. edit->start_line++;
  911. }
  912. edit_modification (edit);
  913. if (c == '\n') {
  914. if (edit->book_mark)
  915. book_mark_inc (edit, edit->curs_line);
  916. edit->total_lines++;
  917. edit->force |= REDRAW_AFTER_CURSOR;
  918. }
  919. edit_push_action (edit, DELCHAR);
  920. edit->mark1 += (edit->mark1 >= edit->curs1);
  921. edit->mark2 += (edit->mark2 >= edit->curs1);
  922. edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
  923. if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
  924. edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
  925. edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
  926. edit->last_byte++;
  927. edit->curs2++;
  928. }
  929. int edit_delete (WEdit * edit)
  930. {
  931. int p;
  932. int cw = 1;
  933. if (!edit->curs2)
  934. return 0;
  935. edit->mark1 -= (edit->mark1 > edit->curs1);
  936. edit->mark2 -= (edit->mark2 > edit->curs1);
  937. edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
  938. cw = 1;
  939. if ( edit->utf8 ) {
  940. edit_get_utf (edit, edit->curs1, &cw);
  941. if ( cw < 1 )
  942. cw = 1;
  943. }
  944. for ( int i = 1; i<= cw; i++ ) {
  945. p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
  946. if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
  947. g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
  948. edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
  949. }
  950. edit->last_byte--;
  951. edit->curs2--;
  952. }
  953. edit_modification (edit);
  954. if (p == '\n') {
  955. if (edit->book_mark)
  956. book_mark_dec (edit, edit->curs_line);
  957. edit->total_lines--;
  958. edit->force |= REDRAW_AFTER_CURSOR;
  959. }
  960. edit_push_action (edit, p + 256);
  961. if (edit->curs1 < edit->start_display) {
  962. edit->start_display--;
  963. if (p == '\n')
  964. edit->start_line--;
  965. }
  966. return p;
  967. }
  968. static int
  969. edit_backspace (WEdit * edit)
  970. {
  971. int p;
  972. int cw = 1;
  973. if (!edit->curs1)
  974. return 0;
  975. edit->mark1 -= (edit->mark1 >= edit->curs1);
  976. edit->mark2 -= (edit->mark2 >= edit->curs1);
  977. edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
  978. cw = 1;
  979. if ( edit->utf8 ) {
  980. edit_get_prev_utf (edit, edit->curs1, &cw);
  981. if ( cw < 1 )
  982. cw = 1;
  983. }
  984. for ( int i = 1; i<= cw; i++ ) {
  985. p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
  986. if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
  987. g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
  988. edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
  989. }
  990. edit->last_byte--;
  991. edit->curs1--;
  992. }
  993. edit_modification (edit);
  994. if (p == '\n') {
  995. if (edit->book_mark)
  996. book_mark_dec (edit, edit->curs_line);
  997. edit->curs_line--;
  998. edit->total_lines--;
  999. edit->force |= REDRAW_AFTER_CURSOR;
  1000. }
  1001. edit_push_action (edit, p);
  1002. if (edit->curs1 < edit->start_display) {
  1003. edit->start_display--;
  1004. if (p == '\n')
  1005. edit->start_line--;
  1006. }
  1007. return p;
  1008. }
  1009. #ifdef FAST_MOVE_CURSOR
  1010. static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
  1011. {
  1012. unsigned long next;
  1013. while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
  1014. edit->curs_line--;
  1015. next -= (unsigned long) dest;
  1016. n -= next;
  1017. src += next;
  1018. dest += next;
  1019. }
  1020. }
  1021. int
  1022. edit_move_backward_lots (WEdit *edit, long increment)
  1023. {
  1024. int r, s, t;
  1025. unsigned char *p;
  1026. if (increment > edit->curs1)
  1027. increment = edit->curs1;
  1028. if (increment <= 0)
  1029. return -1;
  1030. edit_push_action (edit, CURS_RIGHT_LOTS, increment);
  1031. t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
  1032. if (r > increment)
  1033. r = increment;
  1034. s = edit->curs1 & M_EDIT_BUF_SIZE;
  1035. p = 0;
  1036. if (s > r) {
  1037. memqcpy (edit,
  1038. edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
  1039. edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r,
  1040. r);
  1041. } else {
  1042. if (s) {
  1043. memqcpy (edit,
  1044. edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
  1045. s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
  1046. p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
  1047. edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
  1048. }
  1049. memqcpy (edit,
  1050. edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
  1051. edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
  1052. EDIT_BUF_SIZE - (r - s), r - s);
  1053. }
  1054. increment -= r;
  1055. edit->curs1 -= r;
  1056. edit->curs2 += r;
  1057. if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
  1058. if (p)
  1059. edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
  1060. else
  1061. edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
  1062. g_malloc (EDIT_BUF_SIZE);
  1063. } else {
  1064. g_free (p);
  1065. }
  1066. s = edit->curs1 & M_EDIT_BUF_SIZE;
  1067. while (increment) {
  1068. p = 0;
  1069. r = EDIT_BUF_SIZE;
  1070. if (r > increment)
  1071. r = increment;
  1072. t = s;
  1073. if (r < t)
  1074. t = r;
  1075. memqcpy (edit,
  1076. edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
  1077. EDIT_BUF_SIZE - t,
  1078. edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
  1079. t);
  1080. if (r >= s) {
  1081. if (t) {
  1082. p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
  1083. edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
  1084. }
  1085. memqcpy (edit,
  1086. edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
  1087. EDIT_BUF_SIZE - r,
  1088. edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
  1089. EDIT_BUF_SIZE - (r - s), r - s);
  1090. }
  1091. increment -= r;
  1092. edit->curs1 -= r;
  1093. edit->curs2 += r;
  1094. if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
  1095. if (p)
  1096. edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
  1097. else
  1098. edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
  1099. g_malloc (EDIT_BUF_SIZE);
  1100. } else {
  1101. g_free (p);
  1102. }
  1103. }
  1104. return edit_get_byte (edit, edit->curs1);
  1105. }
  1106. #endif /* ! FAST_MOVE_CURSOR */
  1107. /* moves the cursor right or left: increment positive or negative respectively */
  1108. void edit_cursor_move (WEdit * edit, long increment)
  1109. {
  1110. /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
  1111. int c;
  1112. long curs1 = edit->curs1;
  1113. long curs2 = edit->curs2;
  1114. int cw = 1;
  1115. int char_step = 0;
  1116. /* one char move*/
  1117. if ( increment == -1 || increment == 1)
  1118. char_step = 1;
  1119. #ifdef FAST_MOVE_CURSOR
  1120. if (increment < -256) {
  1121. edit->force |= REDRAW_PAGE;
  1122. edit_move_backward_lots (edit, -increment);
  1123. return;
  1124. }
  1125. #endif /* ! FAST_MOVE_CURSOR */
  1126. if (increment < 0) {
  1127. for (; increment < 0; increment++) {
  1128. if (!edit->curs1)
  1129. return;
  1130. edit_push_action (edit, CURS_RIGHT);
  1131. cw = 1;
  1132. if ( edit->utf8 && char_step ) {
  1133. edit_get_prev_utf (edit, curs1, &cw);
  1134. if ( cw < 1 )
  1135. cw = 1;
  1136. }
  1137. for ( int i = 1; i<= cw; i++ ) {
  1138. c = edit_get_byte (edit, curs1 - 1);
  1139. if (!((curs2 + 1) & M_EDIT_BUF_SIZE))
  1140. edit->buffers2[(curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
  1141. edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (curs2 & M_EDIT_BUF_SIZE) - 1] = c;
  1142. curs2++;
  1143. c = edit->buffers1[(curs1 - 1) >> S_EDIT_BUF_SIZE][(curs1 - 1) & M_EDIT_BUF_SIZE];
  1144. if (!((curs1 - 1) & M_EDIT_BUF_SIZE)) {
  1145. g_free (edit->buffers1[curs1 >> S_EDIT_BUF_SIZE]);
  1146. edit->buffers1[curs1 >> S_EDIT_BUF_SIZE] = NULL;
  1147. }
  1148. curs1--;
  1149. }
  1150. edit->curs1 = curs1;
  1151. edit->curs2 = curs2;
  1152. if (c == '\n') {
  1153. edit->curs_line--;
  1154. edit->force |= REDRAW_LINE_BELOW;
  1155. }
  1156. }
  1157. } else if (increment > 0) {
  1158. for (; increment > 0; increment--) {
  1159. if (!edit->curs2)
  1160. return;
  1161. edit_push_action (edit, CURS_LEFT);
  1162. cw = 1;
  1163. if ( edit->utf8 && char_step ) {
  1164. edit_get_utf (edit, curs1, &cw);
  1165. if ( cw < 1 )
  1166. cw = 1;
  1167. }
  1168. for ( int i = 1; i<= cw; i++ ) {
  1169. c = edit_get_byte (edit, curs1);
  1170. if (!(curs1 & M_EDIT_BUF_SIZE))
  1171. edit->buffers1[curs1 >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
  1172. edit->buffers1[curs1 >> S_EDIT_BUF_SIZE][curs1 & M_EDIT_BUF_SIZE] = c;
  1173. curs1++;
  1174. c = edit->buffers2[(curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
  1175. if (!(curs2 & M_EDIT_BUF_SIZE)) {
  1176. g_free (edit->buffers2[curs2 >> S_EDIT_BUF_SIZE]);
  1177. edit->buffers2[curs2 >> S_EDIT_BUF_SIZE] = 0;
  1178. }
  1179. curs2--;
  1180. }
  1181. edit->curs1 = curs1;
  1182. edit->curs2 = curs2;
  1183. if (c == '\n') {
  1184. edit->curs_line++;
  1185. edit->force |= REDRAW_LINE_ABOVE;
  1186. }
  1187. }
  1188. }
  1189. }
  1190. /* These functions return positions relative to lines */
  1191. /* returns index of last char on line + 1 */
  1192. long edit_eol (WEdit * edit, long current)
  1193. {
  1194. if (current < edit->last_byte) {
  1195. for (;; current++)
  1196. if (edit_get_byte (edit, current) == '\n')
  1197. break;
  1198. } else
  1199. return edit->last_byte;
  1200. return current;
  1201. }
  1202. /* returns index of first char on line */
  1203. long edit_bol (WEdit * edit, long current)
  1204. {
  1205. if (current > 0) {
  1206. for (;; current--)
  1207. if (edit_get_byte (edit, current - 1) == '\n')
  1208. break;
  1209. } else
  1210. return 0;
  1211. return current;
  1212. }
  1213. int edit_count_lines (WEdit * edit, long current, int upto)
  1214. {
  1215. int lines = 0;
  1216. if (upto > edit->last_byte)
  1217. upto = edit->last_byte;
  1218. if (current < 0)
  1219. current = 0;
  1220. while (current < upto)
  1221. if (edit_get_byte (edit, current++) == '\n')
  1222. lines++;
  1223. return lines;
  1224. }
  1225. /* If lines is zero this returns the count of lines from current to upto. */
  1226. /* If upto is zero returns index of lines forward current. */
  1227. long edit_move_forward (WEdit * edit, long current, int lines, long upto)
  1228. {
  1229. if (upto) {
  1230. return edit_count_lines (edit, current, upto);
  1231. } else {
  1232. int next;
  1233. if (lines < 0)
  1234. lines = 0;
  1235. while (lines--) {
  1236. next = edit_eol (edit, current) + 1;
  1237. if (next > edit->last_byte)
  1238. break;
  1239. else
  1240. current = next;
  1241. }
  1242. return current;
  1243. }
  1244. }
  1245. /* Returns offset of 'lines' lines up from current */
  1246. long edit_move_backward (WEdit * edit, long current, int lines)
  1247. {
  1248. if (lines < 0)
  1249. lines = 0;
  1250. current = edit_bol (edit, current);
  1251. while((lines--) && current != 0)
  1252. current = edit_bol (edit, current - 1);
  1253. return current;
  1254. }
  1255. /* If cols is zero this returns the count of columns from current to upto. */
  1256. /* If upto is zero returns index of cols across from current. */
  1257. long edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
  1258. {
  1259. long p, q;
  1260. int col = 0;
  1261. int cw = 1;
  1262. if (upto) {
  1263. q = upto;
  1264. cols = -10;
  1265. } else
  1266. q = edit->last_byte + 2;
  1267. for (col = 0, p = current; p < q; p++) {
  1268. int c;
  1269. if (cols != -10) {
  1270. if (col == cols)
  1271. return p;
  1272. if (col > cols)
  1273. return p - 1;
  1274. }
  1275. if ( !edit->utf8 ) {
  1276. c = edit_get_byte (edit, p);
  1277. } else {
  1278. cw = 1;
  1279. c = edit_get_byte (edit, p);
  1280. edit_get_utf (edit, p, &cw);
  1281. }
  1282. if (c == '\t')
  1283. col += TAB_SIZE - col % TAB_SIZE;
  1284. else if (c == '\n') {
  1285. if (upto)
  1286. return col;
  1287. else
  1288. return p;
  1289. } else if (c < 32 || c == 127)
  1290. col += 2; /* Caret notation for control characters */
  1291. else
  1292. col++;
  1293. if ( cw > 1 )
  1294. col -= cw-1;
  1295. }
  1296. return col;
  1297. }
  1298. /* returns the current column position of the cursor */
  1299. int edit_get_col (WEdit * edit)
  1300. {
  1301. return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
  1302. }
  1303. /* Scrolling functions */
  1304. void edit_update_curs_row (WEdit * edit)
  1305. {
  1306. edit->curs_row = edit->curs_line - edit->start_line;
  1307. }
  1308. void edit_update_curs_col (WEdit * edit)
  1309. {
  1310. edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
  1311. }
  1312. /*moves the display start position up by i lines */
  1313. void edit_scroll_upward (WEdit * edit, unsigned long i)
  1314. {
  1315. unsigned long lines_above = edit->start_line;
  1316. if (i > lines_above)
  1317. i = lines_above;
  1318. if (i) {
  1319. edit->start_line -= i;
  1320. edit->start_display = edit_move_backward (edit, edit->start_display, i);
  1321. edit->force |= REDRAW_PAGE;
  1322. edit->force &= (0xfff - REDRAW_CHAR_ONLY);
  1323. }
  1324. edit_update_curs_row (edit);
  1325. }
  1326. /* returns 1 if could scroll, 0 otherwise */
  1327. void edit_scroll_downward (WEdit * edit, int i)
  1328. {
  1329. int lines_below;
  1330. lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
  1331. if (lines_below > 0) {
  1332. if (i > lines_below)
  1333. i = lines_below;
  1334. edit->start_line += i;
  1335. edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
  1336. edit->force |= REDRAW_PAGE;
  1337. edit->force &= (0xfff - REDRAW_CHAR_ONLY);
  1338. }
  1339. edit_update_curs_row (edit);
  1340. }
  1341. void edit_scroll_right (WEdit * edit, int i)
  1342. {
  1343. edit->force |= REDRAW_PAGE;
  1344. edit->force &= (0xfff - REDRAW_CHAR_ONLY);
  1345. edit->start_col -= i;
  1346. }
  1347. void edit_scroll_left (WEdit * edit, int i)
  1348. {
  1349. if (edit->start_col) {
  1350. edit->start_col += i;
  1351. if (edit->start_col > 0)
  1352. edit->start_col = 0;
  1353. edit->force |= REDRAW_PAGE;
  1354. edit->force &= (0xfff - REDRAW_CHAR_ONLY);
  1355. }
  1356. }
  1357. /* high level cursor movement commands */
  1358. static int is_in_indent (WEdit *edit)
  1359. {
  1360. long p = edit_bol (edit, edit->curs1);
  1361. while (p < edit->curs1)
  1362. if (!strchr (" \t", edit_get_byte (edit, p++)))
  1363. return 0;
  1364. return 1;
  1365. }
  1366. static int left_of_four_spaces (WEdit *edit);
  1367. void
  1368. edit_move_to_prev_col (WEdit * edit, long p)
  1369. {
  1370. edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->prev_col, 0) - edit->curs1);
  1371. if (is_in_indent (edit) && option_fake_half_tabs) {
  1372. edit_update_curs_col (edit);
  1373. if (space_width)
  1374. if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
  1375. int q = edit->curs_col;
  1376. edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
  1377. p = edit_bol (edit, edit->curs1);
  1378. edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
  1379. if (!left_of_four_spaces (edit))
  1380. edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
  1381. }
  1382. }
  1383. }
  1384. /* move i lines */
  1385. void edit_move_up (WEdit * edit, unsigned long i, int scroll)
  1386. {
  1387. unsigned long p, l = edit->curs_line;
  1388. if (i > l)
  1389. i = l;
  1390. if (i) {
  1391. if (i > 1)
  1392. edit->force |= REDRAW_PAGE;
  1393. if (scroll)
  1394. edit_scroll_upward (edit, i);
  1395. p = edit_bol (edit, edit->curs1);
  1396. edit_cursor_move (edit, (p = edit_move_backward (edit, p, i)) - edit->curs1);
  1397. edit_move_to_prev_col (edit, p);
  1398. edit->search_start = edit->curs1;
  1399. edit->found_len = 0;
  1400. }
  1401. }
  1402. static int
  1403. is_blank (WEdit *edit, long offset)
  1404. {
  1405. long s, f;
  1406. int c;
  1407. s = edit_bol (edit, offset);
  1408. f = edit_eol (edit, offset) - 1;
  1409. while (s <= f) {
  1410. c = edit_get_byte (edit, s++);
  1411. if (!isspace (c))
  1412. return 0;
  1413. }
  1414. return 1;
  1415. }
  1416. /* returns the offset of line i */
  1417. static long
  1418. edit_find_line (WEdit *edit, int line)
  1419. {
  1420. int i, j = 0;
  1421. int m = 2000000000;
  1422. if (!edit->caches_valid) {
  1423. for (i = 0; i < N_LINE_CACHES; i++)
  1424. edit->line_numbers[i] = edit->line_offsets[i] = 0;
  1425. /* three offsets that we *know* are line 0 at 0 and these two: */
  1426. edit->line_numbers[1] = edit->curs_line;
  1427. edit->line_offsets[1] = edit_bol (edit, edit->curs1);
  1428. edit->line_numbers[2] = edit->total_lines;
  1429. edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
  1430. edit->caches_valid = 1;
  1431. }
  1432. if (line >= edit->total_lines)
  1433. return edit->line_offsets[2];
  1434. if (line <= 0)
  1435. return 0;
  1436. /* find the closest known point */
  1437. for (i = 0; i < N_LINE_CACHES; i++) {
  1438. int n;
  1439. n = abs (edit->line_numbers[i] - line);
  1440. if (n < m) {
  1441. m = n;
  1442. j = i;
  1443. }
  1444. }
  1445. if (m == 0)
  1446. return edit->line_offsets[j]; /* know the offset exactly */
  1447. if (m == 1 && j >= 3)
  1448. i = j; /* one line different - caller might be looping, so stay in this cache */
  1449. else
  1450. i = 3 + (rand () % (N_LINE_CACHES - 3));
  1451. if (line > edit->line_numbers[j])
  1452. edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
  1453. else
  1454. edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
  1455. edit->line_numbers[i] = line;
  1456. return edit->line_offsets[i];
  1457. }
  1458. int line_is_blank (WEdit * edit, long line)
  1459. {
  1460. return is_blank (edit, edit_find_line (edit, line));
  1461. }
  1462. /* moves up until a blank line is reached, or until just
  1463. before a non-blank line is reached */
  1464. static void edit_move_up_paragraph (WEdit * edit, int scroll)
  1465. {
  1466. int i;
  1467. if (edit->curs_line <= 1) {
  1468. i = 0;
  1469. } else {
  1470. if (line_is_blank (edit, edit->curs_line)) {
  1471. if (line_is_blank (edit, edit->curs_line - 1)) {
  1472. for (i = edit->curs_line - 1; i; i--)
  1473. if (!line_is_blank (edit, i)) {
  1474. i++;
  1475. break;
  1476. }
  1477. } else {
  1478. for (i = edit->curs_line - 1; i; i--)
  1479. if (line_is_blank (edit, i))
  1480. break;
  1481. }
  1482. } else {
  1483. for (i = edit->curs_line - 1; i; i--)
  1484. if (line_is_blank (edit, i))
  1485. break;
  1486. }
  1487. }
  1488. edit_move_up (edit, edit->curs_line - i, scroll);
  1489. }
  1490. /* move i lines */
  1491. void edit_move_down (WEdit * edit, int i, int scroll)
  1492. {
  1493. long p, l = edit->total_lines - edit->curs_line;
  1494. if (i > l)
  1495. i = l;
  1496. if (i) {
  1497. if (i > 1)
  1498. edit->force |= REDRAW_PAGE;
  1499. if (scroll)
  1500. edit_scroll_downward (edit, i);
  1501. p = edit_bol (edit, edit->curs1);
  1502. edit_cursor_move (edit, (p = edit_move_forward (edit, p, i, 0)) - edit->curs1);
  1503. edit_move_to_prev_col (edit, p);
  1504. edit->search_start = edit->curs1;
  1505. edit->found_len = 0;
  1506. }
  1507. }
  1508. /* moves down until a blank line is reached, or until just
  1509. before a non-blank line is reached */
  1510. static void edit_move_down_paragraph (WEdit * edit, int scroll)
  1511. {
  1512. int i;
  1513. if (edit->curs_line >= edit->total_lines - 1) {
  1514. i = edit->total_lines;
  1515. } else {
  1516. if (line_is_blank (edit, edit->curs_line)) {
  1517. if (line_is_blank (edit, edit->curs_line + 1)) {
  1518. for (i = edit->curs_line + 1; i; i++)
  1519. if (!line_is_blank (edit, i) || i > edit->total_lines) {
  1520. i--;
  1521. break;
  1522. }
  1523. } else {
  1524. for (i = edit->curs_line + 1; i; i++)
  1525. if (line_is_blank (edit, i) || i >= edit->total_lines)
  1526. break;
  1527. }
  1528. } else {
  1529. for (i = edit->curs_line + 1; i; i++)
  1530. if (line_is_blank (edit, i) || i >= edit->total_lines)
  1531. break;
  1532. }
  1533. }
  1534. edit_move_down (edit, i - edit->curs_line, scroll);
  1535. }
  1536. static void edit_begin_page (WEdit *edit)
  1537. {
  1538. edit_update_curs_row (edit);
  1539. edit_move_up (edit, edit->curs_row, 0);
  1540. }
  1541. static void edit_end_page (WEdit *edit)
  1542. {
  1543. edit_update_curs_row (edit);
  1544. edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
  1545. }
  1546. /* goto beginning of text */
  1547. static void edit_move_to_top (WEdit * edit)
  1548. {
  1549. if (edit->curs_line) {
  1550. edit_cursor_move (edit, -edit->curs1);
  1551. edit_move_to_prev_col (edit, 0);
  1552. edit->force |= REDRAW_PAGE;
  1553. edit->search_start = 0;
  1554. edit_update_curs_row(edit);
  1555. }
  1556. }
  1557. /* goto end of text */
  1558. static void edit_move_to_bottom (WEdit * edit)
  1559. {
  1560. if (edit->curs_line < edit->total_lines) {
  1561. edit_cursor_move (edit, edit->curs2);
  1562. edit->start_display = edit->last_byte;
  1563. edit->start_line = edit->total_lines;
  1564. edit_update_curs_row(edit);
  1565. edit_scroll_upward (edit, edit->num_widget_lines - 1);
  1566. edit->force |= REDRAW_PAGE;
  1567. }
  1568. }
  1569. /* goto beginning of line */
  1570. static void edit_cursor_to_bol (WEdit * edit)
  1571. {
  1572. edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
  1573. edit->search_start = edit->curs1;
  1574. edit->prev_col = edit_get_col (edit);
  1575. }
  1576. /* goto end of line */
  1577. static void edit_cursor_to_eol (WEdit * edit)
  1578. {
  1579. edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
  1580. edit->search_start = edit->curs1;
  1581. edit->prev_col = edit_get_col (edit);
  1582. }
  1583. /* move cursor to line 'line' */
  1584. void edit_move_to_line (WEdit * e, long line)
  1585. {
  1586. if(line < e->curs_line)
  1587. edit_move_up (e, e->curs_line - line, 0);
  1588. else
  1589. edit_move_down (e, line - e->curs_line, 0);
  1590. edit_scroll_screen_over_cursor (e);
  1591. }
  1592. /* scroll window so that first visible line is 'line' */
  1593. void edit_move_display (WEdit * e, long line)
  1594. {
  1595. if(line < e->start_line)
  1596. edit_scroll_upward (e, e->start_line - line);
  1597. else
  1598. edit_scroll_downward (e, line - e->start_line);
  1599. }
  1600. /* save markers onto undo stack */
  1601. void edit_push_markers (WEdit * edit)
  1602. {
  1603. edit_push_action (edit, MARK_1 + edit->mark1);
  1604. edit_push_action (edit, MARK_2 + edit->mark2);
  1605. }
  1606. void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
  1607. {
  1608. edit->mark1 = m1;
  1609. edit->mark2 = m2;
  1610. edit->column1 = c1;
  1611. edit->column2 = c2;
  1612. }
  1613. /* highlight marker toggle */
  1614. void edit_mark_cmd (WEdit * edit, int unmark)
  1615. {
  1616. edit_push_markers (edit);
  1617. if (unmark) {
  1618. edit_set_markers (edit, 0, 0, 0, 0);
  1619. edit->force |= REDRAW_PAGE;
  1620. } else {
  1621. if (edit->mark2 >= 0) {
  1622. edit_set_markers (edit, edit->curs1, -1, edit->curs_col, edit->curs_col);
  1623. edit->force |= REDRAW_PAGE;
  1624. } else
  1625. edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col);
  1626. }
  1627. }
  1628. static unsigned long
  1629. my_type_of (int c)
  1630. {
  1631. int x, r = 0;
  1632. const char *p, *q;
  1633. const char option_chars_move_whole_word[] =
  1634. "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
  1635. if (!c)
  1636. return 0;
  1637. if (c == '!') {
  1638. if (*option_chars_move_whole_word == '!')
  1639. return 2;
  1640. return 0x80000000UL;
  1641. }
  1642. if (g_ascii_isupper ((gchar) c))
  1643. c = 'A';
  1644. else if (g_ascii_islower ((gchar) c))
  1645. c = 'a';
  1646. else if (g_ascii_isalpha (c))
  1647. c = 'a';
  1648. else if (isdigit (c))
  1649. c = '0';
  1650. else if (isspace (c))
  1651. c = ' ';
  1652. q = strchr (option_chars_move_whole_word, c);
  1653. if (!q)
  1654. return 0xFFFFFFFFUL;
  1655. do {
  1656. for (x = 1, p = option_chars_move_whole_word; p < q; p++)
  1657. if (*p == '!')
  1658. x <<= 1;
  1659. r |= x;
  1660. } while ((q = strchr (q + 1, c)));
  1661. return r;
  1662. }
  1663. static void
  1664. edit_left_word_move (WEdit *edit, int s)
  1665. {
  1666. for (;;) {
  1667. int c1, c2;
  1668. edit_cursor_move (edit, -1);
  1669. if (!edit->curs1)
  1670. break;
  1671. c1 = edit_get_byte (edit, edit->curs1 - 1);
  1672. c2 = edit_get_byte (edit, edit->curs1);
  1673. if (!(my_type_of (c1) & my_type_of (c2)))
  1674. break;
  1675. if (isspace (c1) && !isspace (c2))
  1676. break;
  1677. if (s)
  1678. if (!isspace (c1) && isspace (c2))
  1679. break;
  1680. }
  1681. }
  1682. static void edit_left_word_move_cmd (WEdit * edit)
  1683. {
  1684. edit_left_word_move (edit, 0);
  1685. edit->force |= REDRAW_PAGE;
  1686. }
  1687. static void
  1688. edit_right_word_move (WEdit *edit, int s)
  1689. {
  1690. for (;;) {
  1691. int c1, c2;
  1692. edit_cursor_move (edit, 1);
  1693. if (edit->curs1 >= edit->last_byte)
  1694. break;
  1695. c1 = edit_get_byte (edit, edit->curs1 - 1);
  1696. c2 = edit_get_byte (edit, edit->curs1);
  1697. if (!(my_type_of (c1) & my_type_of (c2)))
  1698. break;
  1699. if (isspace (c1) && !isspace (c2))
  1700. break;
  1701. if (s)
  1702. if (!isspace (c1) && isspace (c2))
  1703. break;
  1704. }
  1705. }
  1706. static void edit_right_word_move_cmd (WEdit * edit)
  1707. {
  1708. edit_right_word_move (edit, 0);
  1709. edit->force |= REDRAW_PAGE;
  1710. }
  1711. static void edit_right_delete_word (WEdit * edit)
  1712. {
  1713. int c1, c2;
  1714. for (;;) {
  1715. if (edit->curs1 >= edit->last_byte)
  1716. break;
  1717. c1 = edit_delete (edit);
  1718. c2 = edit_get_byte (edit, edit->curs1);
  1719. if ((isspace (c1) == 0) != (isspace (c2) == 0))
  1720. break;
  1721. if (!(my_type_of (c1) & my_type_of (c2)))
  1722. break;
  1723. }
  1724. }
  1725. static void edit_left_delete_word (WEdit * edit)
  1726. {
  1727. int c1, c2;
  1728. for (;;) {
  1729. if (edit->curs1 <= 0)
  1730. break;
  1731. c1 = edit_backspace (edit);
  1732. c2 = edit_get_byte (edit, edit->curs1 - 1);
  1733. if ((isspace (c1) == 0) != (isspace (c2) == 0))
  1734. break;
  1735. if (!(my_type_of (c1) & my_type_of (c2)))
  1736. break;
  1737. }
  1738. }
  1739. /*
  1740. the start column position is not recorded, and hence does not
  1741. undo as it happed. But who would notice.
  1742. */
  1743. static void
  1744. edit_do_undo (WEdit * edit)
  1745. {
  1746. long ac;
  1747. long count = 0;
  1748. edit->stack_disable = 1; /* don't record undo's onto undo stack! */
  1749. while ((ac = pop_action (edit)) < KEY_PRESS) {
  1750. switch ((int) ac) {
  1751. case STACK_BOTTOM:
  1752. goto done_undo;
  1753. case CURS_RIGHT:
  1754. edit_cursor_move (edit, 1);
  1755. break;
  1756. case CURS_LEFT:
  1757. edit_cursor_move (edit, -1);
  1758. break;
  1759. case BACKSPACE:
  1760. edit_backspace (edit);
  1761. break;
  1762. case DELCHAR:
  1763. edit_delete (edit);
  1764. break;
  1765. case COLUMN_ON:
  1766. column_highlighting = 1;
  1767. break;
  1768. case COLUMN_OFF:
  1769. column_highlighting = 0;
  1770. break;
  1771. }
  1772. if (ac >= 256 && ac < 512)
  1773. edit_insert_ahead (edit, ac - 256);
  1774. if (ac >= 0 && ac < 256)
  1775. edit_insert (edit, ac);
  1776. if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
  1777. edit->mark1 = ac - MARK_1;
  1778. edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
  1779. } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
  1780. edit->mark2 = ac - MARK_2;
  1781. edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
  1782. }
  1783. if (count++)
  1784. edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
  1785. }
  1786. if (edit->start_display > ac - KEY_PRESS) {
  1787. edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
  1788. edit->force |= REDRAW_PAGE;
  1789. } else if (edit->start_display < ac - KEY_PRESS) {
  1790. edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
  1791. edit->force |= REDRAW_PAGE;
  1792. }
  1793. edit->start_display = ac - KEY_PRESS; /* see push and pop above */
  1794. edit_update_curs_row (edit);
  1795. done_undo:;
  1796. edit->stack_disable = 0;
  1797. }
  1798. static void edit_delete_to_line_end (WEdit * edit)
  1799. {
  1800. while (edit_get_byte (edit, edit->curs1) != '\n') {
  1801. if (!edit->curs2)
  1802. break;
  1803. edit_delete (edit);
  1804. }
  1805. }
  1806. static void edit_delete_to_line_begin (WEdit * edit)
  1807. {
  1808. while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
  1809. if (!edit->curs1)
  1810. break;
  1811. edit_backspace (edit);
  1812. }
  1813. }
  1814. void
  1815. edit_delete_line (WEdit *edit)
  1816. {
  1817. /*
  1818. * Delete right part of the line.
  1819. * Note that edit_get_byte() returns '\n' when byte position is
  1820. * beyond EOF.
  1821. */
  1822. while (edit_get_byte (edit, edit->curs1) != '\n') {
  1823. (void) edit_delete (edit);
  1824. }
  1825. /*
  1826. * Delete '\n' char.
  1827. * Note that edit_delete() will not corrupt anything if called while
  1828. * cursor position is EOF.
  1829. */
  1830. (void) edit_delete (edit);
  1831. /*
  1832. * Delete left part of the line.
  1833. * Note, that edit_get_byte() returns '\n' when byte position is < 0.
  1834. */
  1835. while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
  1836. (void) edit_backspace (edit);
  1837. };
  1838. }
  1839. static void insert_spaces_tab (WEdit * edit, int half)
  1840. {
  1841. int i;
  1842. edit_update_curs_col (edit);
  1843. i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
  1844. while (i > 0) {
  1845. edit_insert (edit, ' ');
  1846. i -= space_width;
  1847. }
  1848. }
  1849. static int is_aligned_on_a_tab (WEdit * edit)
  1850. {
  1851. edit_update_curs_col (edit);
  1852. if ((edit->curs_col % (TAB_SIZE * space_width)) && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width))
  1853. return 0; /* not alligned on a tab */
  1854. return 1;
  1855. }
  1856. static int right_of_four_spaces (WEdit *edit)
  1857. {
  1858. int i, ch = 0;
  1859. for (i = 1; i <= HALF_TAB_SIZE; i++)
  1860. ch |= edit_get_byte (edit, edit->curs1 - i);
  1861. if (ch == ' ')
  1862. return is_aligned_on_a_tab (edit);
  1863. return 0;
  1864. }
  1865. static int left_of_four_spaces (WEdit *edit)
  1866. {
  1867. int i, ch = 0;
  1868. for (i = 0; i < HALF_TAB_SIZE; i++)
  1869. ch |= edit_get_byte (edit, edit->curs1 + i);
  1870. if (ch == ' ')
  1871. return is_aligned_on_a_tab (edit);
  1872. return 0;
  1873. }
  1874. int edit_indent_width (WEdit * edit, long p)
  1875. {
  1876. long q = p;
  1877. while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
  1878. q++;
  1879. return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
  1880. }
  1881. void edit_insert_indent (WEdit * edit, int indent)
  1882. {
  1883. if (!option_fill_tabs_with_spaces) {
  1884. while (indent >= TAB_SIZE) {
  1885. edit_insert (edit, '\t');
  1886. indent -= TAB_SIZE;
  1887. }
  1888. }
  1889. while (indent-- > 0)
  1890. edit_insert (edit, ' ');
  1891. }
  1892. static void
  1893. edit_auto_indent (WEdit * edit)
  1894. {
  1895. long p;
  1896. char c;
  1897. p = edit->curs1;
  1898. /* use the previous line as a template */
  1899. p = edit_move_backward (edit, p, 1);
  1900. /* copy the leading whitespace of the line */
  1901. for (;;) { /* no range check - the line _is_ \n-terminated */
  1902. c = edit_get_byte (edit, p++);
  1903. if (c != ' ' && c != '\t')
  1904. break;
  1905. edit_insert (edit, c);
  1906. }
  1907. }
  1908. static void edit_double_newline (WEdit * edit)
  1909. {
  1910. edit_insert (edit, '\n');
  1911. if (edit_get_byte (edit, edit->curs1) == '\n')
  1912. return;
  1913. if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
  1914. return;
  1915. edit->force |= REDRAW_PAGE;
  1916. edit_insert (edit, '\n');
  1917. }
  1918. static void edit_tab_cmd (WEdit * edit)
  1919. {
  1920. int i;
  1921. if (option_fake_half_tabs) {
  1922. if (is_in_indent (edit)) {
  1923. /*insert a half tab (usually four spaces) unless there is a
  1924. half tab already behind, then delete it and insert a
  1925. full tab. */
  1926. if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
  1927. for (i = 1; i <= HALF_TAB_SIZE; i++)
  1928. edit_backspace (edit);
  1929. edit_insert (edit, '\t');
  1930. } else {
  1931. insert_spaces_tab (edit, 1);
  1932. }
  1933. return;
  1934. }
  1935. }
  1936. if (option_fill_tabs_with_spaces) {
  1937. insert_spaces_tab (edit, 0);
  1938. } else {
  1939. edit_insert (edit, '\t');
  1940. }
  1941. return;
  1942. }
  1943. static void check_and_wrap_line (WEdit * edit)
  1944. {
  1945. int curs, c;
  1946. if (!option_typewriter_wrap)
  1947. return;
  1948. edit_update_curs_col (edit);
  1949. if (edit->curs_col < option_word_wrap_line_length)
  1950. return;
  1951. curs = edit->curs1;
  1952. for (;;) {
  1953. curs--;
  1954. c = edit_get_byte (edit, curs);
  1955. if (c == '\n' || curs <= 0) {
  1956. edit_insert (edit, '\n');
  1957. return;
  1958. }
  1959. if (c == ' ' || c == '\t') {
  1960. int current = edit->curs1;
  1961. edit_cursor_move (edit, curs - edit->curs1 + 1);
  1962. edit_insert (edit, '\n');
  1963. edit_cursor_move (edit, current - edit->curs1 + 1);
  1964. return;
  1965. }
  1966. }
  1967. }
  1968. static void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
  1969. void edit_push_key_press (WEdit * edit)
  1970. {
  1971. edit_push_action (edit, KEY_PRESS + edit->start_display);
  1972. if (edit->mark2 == -1)
  1973. edit_push_action (edit, MARK_1 + edit->mark1);
  1974. }
  1975. /* this find the matching bracket in either direction, and sets edit->bracket */
  1976. static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
  1977. {
  1978. const char * const b = "{}{[][()(", *p;
  1979. int i = 1, a, inc = -1, c, d, n = 0;
  1980. unsigned long j = 0;
  1981. long q;
  1982. edit_update_curs_row (edit);
  1983. c = edit_get_byte (edit, edit->curs1);
  1984. p = strchr (b, c);
  1985. /* no limit */
  1986. if (!furthest_bracket_search)
  1987. furthest_bracket_search--;
  1988. /* not on a bracket at all */
  1989. if (!p)
  1990. return -1;
  1991. /* the matching bracket */
  1992. d = p[1];
  1993. /* going left or right? */
  1994. if (strchr ("{[(", c))
  1995. inc = 1;
  1996. for (q = edit->curs1 + inc;; q += inc) {
  1997. /* out of buffer? */
  1998. if (q >= edit->last_byte || q < 0)
  1999. break;
  2000. a = edit_get_byte (edit, q);
  2001. /* don't want to eat CPU */
  2002. if (j++ > furthest_bracket_search)
  2003. break;
  2004. /* out of screen? */
  2005. if (in_screen) {
  2006. if (q < edit->start_display)
  2007. break;
  2008. /* count lines if searching downward */
  2009. if (inc > 0 && a == '\n')
  2010. if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
  2011. break;
  2012. }
  2013. /* count bracket depth */
  2014. i += (a == c) - (a == d);
  2015. /* return if bracket depth is zero */
  2016. if (!i)
  2017. return q;
  2018. }
  2019. /* no match */
  2020. return -1;
  2021. }
  2022. static long last_bracket = -1;
  2023. void edit_find_bracket (WEdit * edit)
  2024. {
  2025. edit->bracket = edit_get_bracket (edit, 1, 10000);
  2026. if (last_bracket != edit->bracket)
  2027. edit->force |= REDRAW_PAGE;
  2028. last_bracket = edit->bracket;
  2029. }
  2030. static void edit_goto_matching_bracket (WEdit *edit)
  2031. {
  2032. long q;
  2033. q = edit_get_bracket (edit, 0, 0);
  2034. if (q < 0)
  2035. return;
  2036. edit->bracket = edit->curs1;
  2037. edit->force |= REDRAW_PAGE;
  2038. edit_cursor_move (edit, q - edit->curs1);
  2039. }
  2040. /*
  2041. * This executes a command as though the user initiated it through a key
  2042. * press. Callback with WIDGET_KEY as a message calls this after
  2043. * translating the key press. This function can be used to pass any
  2044. * command to the editor. Note that the screen wouldn't update
  2045. * automatically. Either of command or char_for_insertion must be
  2046. * passed as -1. Commands are executed, and char_for_insertion is
  2047. * inserted at the cursor.
  2048. */
  2049. void edit_execute_key_command (WEdit *edit, int command, int char_for_insertion)
  2050. {
  2051. if (command == CK_Begin_Record_Macro) {
  2052. edit->macro_i = 0;
  2053. edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
  2054. return;
  2055. }
  2056. if (command == CK_End_Record_Macro && edit->macro_i != -1) {
  2057. edit->force |= REDRAW_COMPLETELY;
  2058. edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
  2059. edit->macro_i = -1;
  2060. return;
  2061. }
  2062. if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
  2063. edit->macro[edit->macro_i].command = command;
  2064. edit->macro[edit->macro_i++].ch = char_for_insertion;
  2065. }
  2066. /* record the beginning of a set of editing actions initiated by a key press */
  2067. if (command != CK_Undo && command != CK_Ext_Mode)
  2068. edit_push_key_press (edit);
  2069. edit_execute_cmd (edit, command, char_for_insertion);
  2070. if (column_highlighting)
  2071. edit->force |= REDRAW_PAGE;
  2072. }
  2073. static const char * const shell_cmd[] = SHELL_COMMANDS_i;
  2074. /*
  2075. This executes a command at a lower level than macro recording.
  2076. It also does not push a key_press onto the undo stack. This means
  2077. that if it is called many times, a single undo command will undo
  2078. all of them. It also does not check for the Undo command.
  2079. */
  2080. void
  2081. edit_execute_cmd (WEdit *edit, int command, int char_for_insertion)
  2082. {
  2083. edit->force |= REDRAW_LINE;
  2084. /* The next key press will unhighlight the found string, so update
  2085. * the whole page */
  2086. if (edit->found_len || column_highlighting)
  2087. edit->force |= REDRAW_PAGE;
  2088. if (command / 100 == 6) { /* a highlight command like shift-arrow */
  2089. column_highlighting = 0;
  2090. if (!edit->highlight
  2091. || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
  2092. edit_mark_cmd (edit, 1); /* clear */
  2093. edit_mark_cmd (edit, 0); /* marking on */
  2094. }
  2095. edit->highlight = 1;
  2096. } else { /* any other command */
  2097. if (edit->highlight)
  2098. edit_mark_cmd (edit, 0); /* clear */
  2099. edit->highlight = 0;
  2100. }
  2101. /* first check for undo */
  2102. if (command == CK_Undo) {
  2103. edit_do_undo (edit);
  2104. edit->found_len = 0;
  2105. edit->prev_col = edit_get_col (edit);
  2106. edit->search_start = edit->curs1;
  2107. return;
  2108. }
  2109. /* An ordinary key press */
  2110. if (char_for_insertion >= 0) {
  2111. if (edit->overwrite) {
  2112. if (edit_get_byte (edit, edit->curs1) != '\n')
  2113. edit_delete (edit);
  2114. }
  2115. edit_insert (edit, char_for_insertion);
  2116. if (option_auto_para_formatting) {
  2117. format_paragraph (edit, 0);
  2118. edit->force |= REDRAW_PAGE;
  2119. } else
  2120. check_and_wrap_line (edit);
  2121. edit->found_len = 0;
  2122. edit->prev_col = edit_get_col (edit);
  2123. edit->search_start = edit->curs1;
  2124. edit_find_bracket (edit);
  2125. return;
  2126. }
  2127. switch (command) {
  2128. case CK_Begin_Page:
  2129. case CK_End_Page:
  2130. case CK_Begin_Page_Highlight:
  2131. case CK_End_Page_Highlight:
  2132. case CK_Word_Left:
  2133. case CK_Word_Right:
  2134. case CK_Up:
  2135. case CK_Down:
  2136. case CK_Left:
  2137. case CK_Right:
  2138. if ( edit->mark2 >= 0 ) {
  2139. if ( !option_persistent_selections ) {
  2140. if (column_highlighting)
  2141. edit_push_action (edit, COLUMN_ON);
  2142. column_highlighting = 0;
  2143. edit_mark_cmd (edit, 1);
  2144. }
  2145. }
  2146. }
  2147. switch (command) {
  2148. case CK_Begin_Page:
  2149. case CK_End_Page:
  2150. case CK_Begin_Page_Highlight:
  2151. case CK_End_Page_Highlight:
  2152. case CK_Word_Left:
  2153. case CK_Word_Right:
  2154. case CK_Up:
  2155. case CK_Down:
  2156. case CK_Word_Left_Highlight:
  2157. case CK_Word_Right_Highlight:
  2158. case CK_Up_Highlight:
  2159. case CK_Down_Highlight:
  2160. if (edit->mark2 == -1)
  2161. break; /*marking is following the cursor: may need to highlight a whole line */
  2162. case CK_Left:
  2163. case CK_Right:
  2164. case CK_Left_Highlight:
  2165. case CK_Right_Highlight:
  2166. edit->force |= REDRAW_CHAR_ONLY;
  2167. }
  2168. /* basic cursor key commands */
  2169. switch (command) {
  2170. case CK_BackSpace:
  2171. /* if non persistent selection and text selected */
  2172. if ( !option_persistent_selections ) {
  2173. if ( edit->mark1 != edit->mark2 ) {
  2174. edit_block_delete_cmd (edit);
  2175. break;
  2176. }
  2177. }
  2178. if (option_backspace_through_tabs && is_in_indent (edit)) {
  2179. while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
  2180. && edit->curs1 > 0)
  2181. edit_backspace (edit);
  2182. break;
  2183. } else {
  2184. if (option_fake_half_tabs) {
  2185. int i;
  2186. if (is_in_indent (edit) && right_of_four_spaces (edit)) {
  2187. for (i = 0; i < HALF_TAB_SIZE; i++)
  2188. edit_backspace (edit);
  2189. break;
  2190. }
  2191. }
  2192. }
  2193. edit_backspace (edit);
  2194. break;
  2195. case CK_Delete:
  2196. /* if non persistent selection and text selected */
  2197. if ( !option_persistent_selections ) {
  2198. if ( edit->mark1 != edit->mark2 ) {
  2199. edit_block_delete_cmd (edit);
  2200. break;
  2201. }
  2202. }
  2203. if (option_fake_half_tabs) {
  2204. int i;
  2205. if (is_in_indent (edit) && left_of_four_spaces (edit)) {
  2206. for (i = 1; i <= HALF_TAB_SIZE; i++)
  2207. edit_delete (edit);
  2208. break;
  2209. }
  2210. }
  2211. edit_delete (edit);
  2212. break;
  2213. case CK_Delete_Word_Left:
  2214. edit_left_delete_word (edit);
  2215. break;
  2216. case CK_Delete_Word_Right:
  2217. edit_right_delete_word (edit);
  2218. break;
  2219. case CK_Delete_Line:
  2220. edit_delete_line (edit);
  2221. break;
  2222. case CK_Delete_To_Line_End:
  2223. edit_delete_to_line_end (edit);
  2224. break;
  2225. case CK_Delete_To_Line_Begin:
  2226. edit_delete_to_line_begin (edit);
  2227. break;
  2228. case CK_Enter:
  2229. if (option_auto_para_formatting) {
  2230. edit_double_newline (edit);
  2231. if (option_return_does_auto_indent)
  2232. edit_auto_indent (edit);
  2233. format_paragraph (edit, 0);
  2234. } else {
  2235. edit_insert (edit, '\n');
  2236. if (option_return_does_auto_indent) {
  2237. edit_auto_indent (edit);
  2238. }
  2239. }
  2240. break;
  2241. case CK_Return:
  2242. edit_insert (edit, '\n');
  2243. break;
  2244. case CK_Page_Up:
  2245. case CK_Page_Up_Highlight:
  2246. edit_move_up (edit, edit->num_widget_lines - 1, 1);
  2247. break;
  2248. case CK_Page_Down:
  2249. case CK_Page_Down_Highlight:
  2250. edit_move_down (edit, edit->num_widget_lines - 1, 1);
  2251. break;
  2252. case CK_Left:
  2253. case CK_Left_Highlight:
  2254. if (option_fake_half_tabs) {
  2255. if (is_in_indent (edit) && right_of_four_spaces (edit)) {
  2256. edit_cursor_move (edit, -HALF_TAB_SIZE);
  2257. edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
  2258. break;
  2259. }
  2260. }
  2261. edit_cursor_move (edit, -1);
  2262. break;
  2263. case CK_Right:
  2264. case CK_Right_Highlight:
  2265. if (option_fake_half_tabs) {
  2266. if (is_in_indent (edit) && left_of_four_spaces (edit)) {
  2267. edit_cursor_move (edit, HALF_TAB_SIZE);
  2268. edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
  2269. break;
  2270. }
  2271. }
  2272. edit_cursor_move (edit, 1);
  2273. break;
  2274. case CK_Begin_Page:
  2275. case CK_Begin_Page_Highlight:
  2276. edit_begin_page (edit);
  2277. break;
  2278. case CK_End_Page:
  2279. case CK_End_Page_Highlight:
  2280. edit_end_page (edit);
  2281. break;
  2282. case CK_Word_Left:
  2283. case CK_Word_Left_Highlight:
  2284. edit_left_word_move_cmd (edit);
  2285. break;
  2286. case CK_Word_Right:
  2287. case CK_Word_Right_Highlight:
  2288. edit_right_word_move_cmd (edit);
  2289. break;
  2290. case CK_Up:
  2291. case CK_Up_Highlight:
  2292. edit_move_up (edit, 1, 0);
  2293. break;
  2294. case CK_Down:
  2295. case CK_Down_Highlight:
  2296. edit_move_down (edit, 1, 0);
  2297. break;
  2298. case CK_Paragraph_Up:
  2299. case CK_Paragraph_Up_Highlight:
  2300. edit_move_up_paragraph (edit, 0);
  2301. break;
  2302. case CK_Paragraph_Down:
  2303. case CK_Paragraph_Down_Highlight:
  2304. edit_move_down_paragraph (edit, 0);
  2305. break;
  2306. case CK_Scroll_Up:
  2307. case CK_Scroll_Up_Highlight:
  2308. edit_move_up (edit, 1, 1);
  2309. break;
  2310. case CK_Scroll_Down:
  2311. case CK_Scroll_Down_Highlight:
  2312. edit_move_down (edit, 1, 1);
  2313. break;
  2314. case CK_Home:
  2315. case CK_Home_Highlight:
  2316. edit_cursor_to_bol (edit);
  2317. break;
  2318. case CK_End:
  2319. case CK_End_Highlight:
  2320. edit_cursor_to_eol (edit);
  2321. break;
  2322. case CK_Tab:
  2323. edit_tab_cmd (edit);
  2324. if (option_auto_para_formatting) {
  2325. format_paragraph (edit, 0);
  2326. edit->force |= REDRAW_PAGE;
  2327. } else
  2328. check_and_wrap_line (edit);
  2329. break;
  2330. case CK_Toggle_Insert:
  2331. edit->overwrite = (edit->overwrite == 0);
  2332. break;
  2333. case CK_Mark:
  2334. if (edit->mark2 >= 0) {
  2335. if (column_highlighting)
  2336. edit_push_action (edit, COLUMN_ON);
  2337. column_highlighting = 0;
  2338. }
  2339. edit_mark_cmd (edit, 0);
  2340. break;
  2341. case CK_Column_Mark:
  2342. if (!column_highlighting)
  2343. edit_push_action (edit, COLUMN_OFF);
  2344. column_highlighting = 1;
  2345. edit_mark_cmd (edit, 0);
  2346. break;
  2347. case CK_Unmark:
  2348. if (column_highlighting)
  2349. edit_push_action (edit, COLUMN_ON);
  2350. column_highlighting = 0;
  2351. edit_mark_cmd (edit, 1);
  2352. break;
  2353. case CK_Toggle_Bookmark:
  2354. book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
  2355. if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
  2356. book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
  2357. else
  2358. book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
  2359. break;
  2360. case CK_Flush_Bookmarks:
  2361. book_mark_flush (edit, BOOK_MARK_COLOR);
  2362. book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
  2363. edit->force |= REDRAW_PAGE;
  2364. break;
  2365. case CK_Next_Bookmark:
  2366. if (edit->book_mark) {
  2367. struct _book_mark *p;
  2368. p = (struct _book_mark *) book_mark_find (edit,
  2369. edit->curs_line);
  2370. if (p->next) {
  2371. p = p->next;
  2372. if (p->line >= edit->start_line + edit->num_widget_lines
  2373. || p->line < edit->start_line)
  2374. edit_move_display (edit,
  2375. p->line -
  2376. edit->num_widget_lines / 2);
  2377. edit_move_to_line (edit, p->line);
  2378. }
  2379. }
  2380. break;
  2381. case CK_Prev_Bookmark:
  2382. if (edit->book_mark) {
  2383. struct _book_mark *p;
  2384. p = (struct _book_mark *) book_mark_find (edit,
  2385. edit->curs_line);
  2386. while (p->line == edit->curs_line)
  2387. if (p->prev)
  2388. p = p->prev;
  2389. if (p->line >= 0) {
  2390. if (p->line >= edit->start_line + edit->num_widget_lines
  2391. || p->line < edit->start_line)
  2392. edit_move_display (edit,
  2393. p->line -
  2394. edit->num_widget_lines / 2);
  2395. edit_move_to_line (edit, p->line);
  2396. }
  2397. }
  2398. break;
  2399. case CK_Beginning_Of_Text:
  2400. case CK_Beginning_Of_Text_Highlight:
  2401. edit_move_to_top (edit);
  2402. break;
  2403. case CK_End_Of_Text:
  2404. case CK_End_Of_Text_Highlight:
  2405. edit_move_to_bottom (edit);
  2406. break;
  2407. case CK_Copy:
  2408. edit_block_copy_cmd (edit);
  2409. break;
  2410. case CK_Remove:
  2411. edit_block_delete_cmd (edit);
  2412. break;
  2413. case CK_Move:
  2414. edit_block_move_cmd (edit);
  2415. break;
  2416. case CK_XStore:
  2417. edit_copy_to_X_buf_cmd (edit);
  2418. break;
  2419. case CK_XCut:
  2420. edit_cut_to_X_buf_cmd (edit);
  2421. break;
  2422. case CK_XPaste:
  2423. edit_paste_from_X_buf_cmd (edit);
  2424. break;
  2425. case CK_Selection_History:
  2426. edit_paste_from_history (edit);
  2427. break;
  2428. case CK_Save_As:
  2429. edit_save_as_cmd (edit);
  2430. break;
  2431. case CK_Save:
  2432. edit_save_confirm_cmd (edit);
  2433. break;
  2434. case CK_Load:
  2435. edit_load_cmd (edit);
  2436. break;
  2437. case CK_Save_Block:
  2438. edit_save_block_cmd (edit);
  2439. break;
  2440. case CK_Insert_File:
  2441. edit_insert_file_cmd (edit);
  2442. break;
  2443. case CK_Load_Prev_File:
  2444. edit_load_back_cmd (edit);
  2445. break;
  2446. case CK_Load_Next_File:
  2447. edit_load_forward_cmd (edit);
  2448. break;
  2449. case CK_Toggle_Syntax:
  2450. if ((option_syntax_highlighting ^= 1) == 1)
  2451. edit_load_syntax (edit, NULL, option_syntax_type);
  2452. edit->force |= REDRAW_PAGE;
  2453. break;
  2454. case CK_Find:
  2455. edit_search_cmd (edit, 0);
  2456. break;
  2457. case CK_Find_Again:
  2458. edit_search_cmd (edit, 1);
  2459. break;
  2460. case CK_Replace:
  2461. edit_replace_cmd (edit, 0);
  2462. break;
  2463. case CK_Replace_Again:
  2464. edit_replace_cmd (edit, 1);
  2465. break;
  2466. case CK_Complete_Word:
  2467. edit_complete_word_cmd (edit);
  2468. break;
  2469. case CK_Find_Definition:
  2470. edit_get_match_keyword_cmd (edit);
  2471. break;
  2472. case CK_Exit:
  2473. dlg_stop (edit->widget.parent);
  2474. break;
  2475. case CK_New:
  2476. edit_new_cmd (edit);
  2477. break;
  2478. case CK_Help:
  2479. edit_help_cmd (edit);
  2480. break;
  2481. case CK_Refresh:
  2482. edit_refresh_cmd (edit);
  2483. break;
  2484. case CK_Date:{
  2485. char s[1024];
  2486. /* fool gcc to prevent a Y2K warning */
  2487. char time_format[] = "_c";
  2488. time_format[0] = '%';
  2489. FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format);
  2490. edit_print_string (edit, s);
  2491. edit->force |= REDRAW_PAGE;
  2492. break;
  2493. }
  2494. case CK_Goto:
  2495. edit_goto_cmd (edit);
  2496. break;
  2497. case CK_Paragraph_Format:
  2498. format_paragraph (edit, 1);
  2499. edit->force |= REDRAW_PAGE;
  2500. break;
  2501. case CK_Delete_Macro:
  2502. edit_delete_macro_cmd (edit);
  2503. break;
  2504. case CK_Match_Bracket:
  2505. edit_goto_matching_bracket (edit);
  2506. break;
  2507. case CK_User_Menu:
  2508. user_menu (edit);
  2509. break;
  2510. case CK_Sort:
  2511. edit_sort_cmd (edit);
  2512. break;
  2513. case CK_ExtCmd:
  2514. edit_ext_cmd (edit);
  2515. break;
  2516. case CK_Mail:
  2517. edit_mail_dialog (edit);
  2518. break;
  2519. case CK_Shell:
  2520. view_other_cmd ();
  2521. break;
  2522. case CK_Select_Codepage:
  2523. edit_select_codepage_cmd (edit);
  2524. break;
  2525. case CK_Insert_Literal:
  2526. edit_insert_literal_cmd (edit);
  2527. break;
  2528. case CK_Execute_Macro:
  2529. edit_execute_macro_cmd (edit);
  2530. break;
  2531. case CK_Begin_End_Macro:
  2532. edit_begin_end_macro_cmd (edit);
  2533. break;
  2534. case CK_Ext_Mode:
  2535. edit->extmod = 1;
  2536. break;
  2537. default:
  2538. break;
  2539. }
  2540. /* CK_Pipe_Block */
  2541. if ((command / 1000) == 1) /* a shell command */
  2542. edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
  2543. if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
  2544. struct macro m[MAX_MACRO_LENGTH];
  2545. int nm;
  2546. if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
  2547. edit_execute_macro (edit, m, nm);
  2548. }
  2549. /* keys which must set the col position, and the search vars */
  2550. switch (command) {
  2551. case CK_Find:
  2552. case CK_Find_Again:
  2553. case CK_Replace:
  2554. case CK_Replace_Again:
  2555. case CK_Complete_Word:
  2556. edit->prev_col = edit_get_col (edit);
  2557. break;
  2558. case CK_Up:
  2559. case CK_Up_Highlight:
  2560. case CK_Down:
  2561. case CK_Down_Highlight:
  2562. case CK_Page_Up:
  2563. case CK_Page_Up_Highlight:
  2564. case CK_Page_Down:
  2565. case CK_Page_Down_Highlight:
  2566. case CK_Beginning_Of_Text:
  2567. case CK_Beginning_Of_Text_Highlight:
  2568. case CK_End_Of_Text:
  2569. case CK_End_Of_Text_Highlight:
  2570. case CK_Paragraph_Up:
  2571. case CK_Paragraph_Up_Highlight:
  2572. case CK_Paragraph_Down:
  2573. case CK_Paragraph_Down_Highlight:
  2574. case CK_Scroll_Up:
  2575. case CK_Scroll_Up_Highlight:
  2576. case CK_Scroll_Down:
  2577. case CK_Scroll_Down_Highlight:
  2578. edit->search_start = edit->curs1;
  2579. edit->found_len = 0;
  2580. break;
  2581. default:
  2582. edit->found_len = 0;
  2583. edit->prev_col = edit_get_col (edit);
  2584. edit->search_start = edit->curs1;
  2585. }
  2586. edit_find_bracket (edit);
  2587. if (option_auto_para_formatting) {
  2588. switch (command) {
  2589. case CK_BackSpace:
  2590. case CK_Delete:
  2591. case CK_Delete_Word_Left:
  2592. case CK_Delete_Word_Right:
  2593. case CK_Delete_To_Line_End:
  2594. case CK_Delete_To_Line_Begin:
  2595. format_paragraph (edit, 0);
  2596. edit->force |= REDRAW_PAGE;
  2597. }
  2598. }
  2599. }
  2600. static void
  2601. edit_execute_macro (WEdit *edit, struct macro macro[], int n)
  2602. {
  2603. int i = 0;
  2604. if (edit->macro_depth++ > 256) {
  2605. edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
  2606. edit->macro_depth--;
  2607. return;
  2608. }
  2609. edit->force |= REDRAW_PAGE;
  2610. for (; i < n; i++) {
  2611. edit_execute_cmd (edit, macro[i].command, macro[i].ch);
  2612. }
  2613. edit_update_screen (edit);
  2614. edit->macro_depth--;
  2615. }
  2616. /* User edit menu, like user menu (F2) but only in editor. */
  2617. static void
  2618. user_menu (WEdit * edit)
  2619. {
  2620. FILE *fd;
  2621. int nomark;
  2622. struct stat status;
  2623. long start_mark, end_mark;
  2624. char *block_file = concat_dir_and_file (home_dir, BLOCK_FILE);
  2625. int rc = 0;
  2626. nomark = eval_marks (edit, &start_mark, &end_mark);
  2627. if (!nomark) /* remember marked or not */
  2628. edit_save_block (edit, block_file, start_mark, end_mark);
  2629. /* run shell scripts from menu */
  2630. user_menu_cmd (edit);
  2631. if (mc_stat (block_file, &status) != 0 || !status.st_size) {
  2632. /* no block messages */
  2633. goto cleanup;
  2634. }
  2635. if (!nomark) {
  2636. /* i.e. we have marked block */
  2637. rc = edit_block_delete_cmd (edit);
  2638. }
  2639. if (!rc) {
  2640. edit_insert_file (edit, block_file);
  2641. }
  2642. /* truncate block file */
  2643. if ((fd = fopen (block_file, "w"))) {
  2644. fclose (fd);
  2645. }
  2646. edit_refresh_cmd (edit);
  2647. edit->force |= REDRAW_COMPLETELY;
  2648. cleanup:
  2649. g_free (block_file);
  2650. }