edit.c 68 KB

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