edit.c 68 KB

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