edit.c 82 KB

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