edit.c 85 KB

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