edit.c 70 KB

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