edit.c 84 KB

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