edit.c 69 KB

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