edit.c 86 KB

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