edit.c 70 KB

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