edit.c 85 KB

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