edit.c 68 KB

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