edit.c 69 KB

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