edit.c 78 KB

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