edit.c 84 KB

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