edit.c 80 KB

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