edit.c 70 KB

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