edit.c 70 KB

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