edit.c 84 KB

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