edit.c 70 KB

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