edit.c 68 KB

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