edit.c 86 KB

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