display.c 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710
  1. /* display.c -- readline redisplay facility. */
  2. /* Copyright (C) 1987-2009 Free Software Foundation, Inc.
  3. This file is part of the GNU Readline Library (Readline), a library
  4. for reading lines of text with interactive input and history editing.
  5. Readline 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 3 of the License, or
  8. (at your option) any later version.
  9. Readline 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 Readline. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #define READLINE_LIBRARY
  17. #if defined (HAVE_CONFIG_H)
  18. # include <config.h>
  19. #endif
  20. #include <sys/types.h>
  21. #if defined (HAVE_UNISTD_H)
  22. # include <unistd.h>
  23. #endif /* HAVE_UNISTD_H */
  24. #include "posixstat.h"
  25. #if defined (HAVE_STDLIB_H)
  26. # include <stdlib.h>
  27. #else
  28. # include "ansi_stdlib.h"
  29. #endif /* HAVE_STDLIB_H */
  30. #include <stdio.h>
  31. #ifdef __MSDOS__
  32. # include <pc.h>
  33. #endif
  34. /* System-specific feature definitions and include files. */
  35. #include "rldefs.h"
  36. #include "rlmbutil.h"
  37. /* Termcap library stuff. */
  38. #include "tcap.h"
  39. /* Some standard library routines. */
  40. #include "readline.h"
  41. #include "history.h"
  42. #include "rlprivate.h"
  43. #include "xmalloc.h"
  44. #if !defined (strchr) && !defined (__STDC__)
  45. extern char *strchr (), *strrchr ();
  46. #endif /* !strchr && !__STDC__ */
  47. static void update_line PARAMS((char *, char *, int, int, int, int));
  48. static void space_to_eol PARAMS((int));
  49. static void delete_chars PARAMS((int));
  50. static void insert_some_chars PARAMS((char *, int, int));
  51. static void cr PARAMS((void));
  52. /* State of visible and invisible lines. */
  53. struct line_state
  54. {
  55. char *line;
  56. int *lbreaks;
  57. int lbsize;
  58. #if defined (HANDLE_MULTIBYTE)
  59. int *wrapped_line;
  60. int wbsize;
  61. #endif
  62. };
  63. /* The line display buffers. One is the line currently displayed on
  64. the screen. The other is the line about to be displayed. */
  65. static struct line_state line_state_array[2];
  66. static struct line_state *line_state_visible = &line_state_array[0];
  67. static struct line_state *line_state_invisible = &line_state_array[1];
  68. static int line_structures_initialized = 0;
  69. /* Backwards-compatible names. */
  70. #define inv_lbreaks (line_state_invisible->lbreaks)
  71. #define inv_lbsize (line_state_invisible->lbsize)
  72. #define vis_lbreaks (line_state_visible->lbreaks)
  73. #define vis_lbsize (line_state_visible->lbsize)
  74. #define visible_line (line_state_visible->line)
  75. #define invisible_line (line_state_invisible->line)
  76. #if defined (HANDLE_MULTIBYTE)
  77. static int _rl_col_width PARAMS((const char *, int, int, int));
  78. #else
  79. # define _rl_col_width(l, s, e, f) (((e) <= (s)) ? 0 : (e) - (s))
  80. #endif
  81. /* Heuristic used to decide whether it is faster to move from CUR to NEW
  82. by backing up or outputting a carriage return and moving forward. CUR
  83. and NEW are either both buffer positions or absolute screen positions. */
  84. #define CR_FASTER(new, cur) (((new) + 1) < ((cur) - (new)))
  85. /* _rl_last_c_pos is an absolute cursor position in multibyte locales and a
  86. buffer index in others. This macro is used when deciding whether the
  87. current cursor position is in the middle of a prompt string containing
  88. invisible characters. XXX - might need to take `modmark' into account. */
  89. #define PROMPT_ENDING_INDEX \
  90. ((MB_CUR_MAX > 1 && rl_byte_oriented == 0) ? prompt_physical_chars : prompt_last_invisible+1)
  91. /* **************************************************************** */
  92. /* */
  93. /* Display stuff */
  94. /* */
  95. /* **************************************************************** */
  96. /* This is the stuff that is hard for me. I never seem to write good
  97. display routines in C. Let's see how I do this time. */
  98. /* (PWP) Well... Good for a simple line updater, but totally ignores
  99. the problems of input lines longer than the screen width.
  100. update_line and the code that calls it makes a multiple line,
  101. automatically wrapping line update. Careful attention needs
  102. to be paid to the vertical position variables. */
  103. /* Keep two buffers; one which reflects the current contents of the
  104. screen, and the other to draw what we think the new contents should
  105. be. Then compare the buffers, and make whatever changes to the
  106. screen itself that we should. Finally, make the buffer that we
  107. just drew into be the one which reflects the current contents of the
  108. screen, and place the cursor where it belongs.
  109. Commands that want to can fix the display themselves, and then let
  110. this function know that the display has been fixed by setting the
  111. RL_DISPLAY_FIXED variable. This is good for efficiency. */
  112. /* Application-specific redisplay function. */
  113. rl_voidfunc_t *rl_redisplay_function = rl_redisplay;
  114. /* Global variables declared here. */
  115. /* What YOU turn on when you have handled all redisplay yourself. */
  116. int rl_display_fixed = 0;
  117. int _rl_suppress_redisplay = 0;
  118. int _rl_want_redisplay = 0;
  119. /* The stuff that gets printed out before the actual text of the line.
  120. This is usually pointing to rl_prompt. */
  121. char *rl_display_prompt = (char *)NULL;
  122. /* Pseudo-global variables declared here. */
  123. /* The visible cursor position. If you print some text, adjust this. */
  124. /* NOTE: _rl_last_c_pos is used as a buffer index when not in a locale
  125. supporting multibyte characters, and an absolute cursor position when
  126. in such a locale. This is an artifact of the donated multibyte support.
  127. Care must be taken when modifying its value. */
  128. int _rl_last_c_pos = 0;
  129. int _rl_last_v_pos = 0;
  130. static int cpos_adjusted;
  131. static int cpos_buffer_position;
  132. static int prompt_multibyte_chars;
  133. /* Number of lines currently on screen minus 1. */
  134. int _rl_vis_botlin = 0;
  135. /* Variables used only in this file. */
  136. /* The last left edge of text that was displayed. This is used when
  137. doing horizontal scrolling. It shifts in thirds of a screenwidth. */
  138. static int last_lmargin;
  139. /* A buffer for `modeline' messages. */
  140. static char msg_buf[128];
  141. /* Non-zero forces the redisplay even if we thought it was unnecessary. */
  142. static int forced_display;
  143. /* Default and initial buffer size. Can grow. */
  144. static int line_size = 1024;
  145. /* Variables to keep track of the expanded prompt string, which may
  146. include invisible characters. */
  147. static char *local_prompt, *local_prompt_prefix;
  148. static int local_prompt_len;
  149. static int prompt_visible_length, prompt_prefix_length;
  150. /* The number of invisible characters in the line currently being
  151. displayed on the screen. */
  152. static int visible_wrap_offset;
  153. /* The number of invisible characters in the prompt string. Static so it
  154. can be shared between rl_redisplay and update_line */
  155. static int wrap_offset;
  156. /* The index of the last invisible character in the prompt string. */
  157. static int prompt_last_invisible;
  158. /* The length (buffer offset) of the first line of the last (possibly
  159. multi-line) buffer displayed on the screen. */
  160. static int visible_first_line_len;
  161. /* Number of invisible characters on the first physical line of the prompt.
  162. Only valid when the number of physical characters in the prompt exceeds
  163. (or is equal to) _rl_screenwidth. */
  164. static int prompt_invis_chars_first_line;
  165. static int prompt_last_screen_line;
  166. static int prompt_physical_chars;
  167. /* set to a non-zero value by rl_redisplay if we are marking modified history
  168. lines and the current line is so marked. */
  169. static int modmark;
  170. /* Variables to save and restore prompt and display information. */
  171. /* These are getting numerous enough that it's time to create a struct. */
  172. static char *saved_local_prompt;
  173. static char *saved_local_prefix;
  174. static int saved_last_invisible;
  175. static int saved_visible_length;
  176. static int saved_prefix_length;
  177. static int saved_local_length;
  178. static int saved_invis_chars_first_line;
  179. static int saved_physical_chars;
  180. /* Expand the prompt string S and return the number of visible
  181. characters in *LP, if LP is not null. This is currently more-or-less
  182. a placeholder for expansion. LIP, if non-null is a place to store the
  183. index of the last invisible character in the returned string. NIFLP,
  184. if non-zero, is a place to store the number of invisible characters in
  185. the first prompt line. The previous are used as byte counts -- indexes
  186. into a character buffer. */
  187. /* Current implementation:
  188. \001 (^A) start non-visible characters
  189. \002 (^B) end non-visible characters
  190. all characters except \001 and \002 (following a \001) are copied to
  191. the returned string; all characters except those between \001 and
  192. \002 are assumed to be `visible'. */
  193. static char *
  194. expand_prompt (pmt, lp, lip, niflp, vlp)
  195. char *pmt;
  196. int *lp, *lip, *niflp, *vlp;
  197. {
  198. char *r, *ret, *p, *igstart;
  199. int l, rl, last, ignoring, ninvis, invfl, invflset, ind, pind, physchars;
  200. /* Short-circuit if we can. */
  201. if ((MB_CUR_MAX <= 1 || rl_byte_oriented) && strchr (pmt, RL_PROMPT_START_IGNORE) == 0)
  202. {
  203. r = savestring (pmt);
  204. if (lp)
  205. *lp = strlen (r);
  206. if (lip)
  207. *lip = 0;
  208. if (niflp)
  209. *niflp = 0;
  210. if (vlp)
  211. *vlp = lp ? *lp : strlen (r);
  212. return r;
  213. }
  214. l = strlen (pmt);
  215. r = ret = (char *)xmalloc (l + 1);
  216. invfl = 0; /* invisible chars in first line of prompt */
  217. invflset = 0; /* we only want to set invfl once */
  218. igstart = 0;
  219. for (rl = ignoring = last = ninvis = physchars = 0, p = pmt; p && *p; p++)
  220. {
  221. /* This code strips the invisible character string markers
  222. RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
  223. if (ignoring == 0 && *p == RL_PROMPT_START_IGNORE) /* XXX - check ignoring? */
  224. {
  225. ignoring = 1;
  226. igstart = p;
  227. continue;
  228. }
  229. else if (ignoring && *p == RL_PROMPT_END_IGNORE)
  230. {
  231. ignoring = 0;
  232. if (p != (igstart + 1))
  233. last = r - ret - 1;
  234. continue;
  235. }
  236. else
  237. {
  238. #if defined (HANDLE_MULTIBYTE)
  239. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  240. {
  241. pind = p - pmt;
  242. ind = _rl_find_next_mbchar (pmt, pind, 1, MB_FIND_NONZERO);
  243. l = ind - pind;
  244. while (l--)
  245. *r++ = *p++;
  246. if (!ignoring)
  247. {
  248. /* rl ends up being assigned to prompt_visible_length,
  249. which is the number of characters in the buffer that
  250. contribute to characters on the screen, which might
  251. not be the same as the number of physical characters
  252. on the screen in the presence of multibyte characters */
  253. rl += ind - pind;
  254. physchars += _rl_col_width (pmt, pind, ind, 0);
  255. }
  256. else
  257. ninvis += ind - pind;
  258. p--; /* compensate for later increment */
  259. }
  260. else
  261. #endif
  262. {
  263. *r++ = *p;
  264. if (!ignoring)
  265. {
  266. rl++; /* visible length byte counter */
  267. physchars++;
  268. }
  269. else
  270. ninvis++; /* invisible chars byte counter */
  271. }
  272. if (invflset == 0 && rl >= _rl_screenwidth)
  273. {
  274. invfl = ninvis;
  275. invflset = 1;
  276. }
  277. }
  278. }
  279. if (rl < _rl_screenwidth)
  280. invfl = ninvis;
  281. *r = '\0';
  282. if (lp)
  283. *lp = rl;
  284. if (lip)
  285. *lip = last;
  286. if (niflp)
  287. *niflp = invfl;
  288. if (vlp)
  289. *vlp = physchars;
  290. return ret;
  291. }
  292. /* Just strip out RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE from
  293. PMT and return the rest of PMT. */
  294. char *
  295. _rl_strip_prompt (pmt)
  296. char *pmt;
  297. {
  298. char *ret;
  299. ret = expand_prompt (pmt, (int *)NULL, (int *)NULL, (int *)NULL, (int *)NULL);
  300. return ret;
  301. }
  302. /*
  303. * Expand the prompt string into the various display components, if
  304. * necessary.
  305. *
  306. * local_prompt = expanded last line of string in rl_display_prompt
  307. * (portion after the final newline)
  308. * local_prompt_prefix = portion before last newline of rl_display_prompt,
  309. * expanded via expand_prompt
  310. * prompt_visible_length = number of visible characters in local_prompt
  311. * prompt_prefix_length = number of visible characters in local_prompt_prefix
  312. *
  313. * This function is called once per call to readline(). It may also be
  314. * called arbitrarily to expand the primary prompt.
  315. *
  316. * The return value is the number of visible characters on the last line
  317. * of the (possibly multi-line) prompt.
  318. */
  319. int
  320. rl_expand_prompt (prompt)
  321. char *prompt;
  322. {
  323. char *p, *t;
  324. int c;
  325. /* Clear out any saved values. */
  326. FREE (local_prompt);
  327. FREE (local_prompt_prefix);
  328. local_prompt = local_prompt_prefix = (char *)0;
  329. local_prompt_len = 0;
  330. prompt_last_invisible = prompt_invis_chars_first_line = 0;
  331. prompt_visible_length = prompt_physical_chars = 0;
  332. if (prompt == 0 || *prompt == 0)
  333. return (0);
  334. p = strrchr (prompt, '\n');
  335. if (!p)
  336. {
  337. /* The prompt is only one logical line, though it might wrap. */
  338. local_prompt = expand_prompt (prompt, &prompt_visible_length,
  339. &prompt_last_invisible,
  340. &prompt_invis_chars_first_line,
  341. &prompt_physical_chars);
  342. local_prompt_prefix = (char *)0;
  343. local_prompt_len = local_prompt ? strlen (local_prompt) : 0;
  344. return (prompt_visible_length);
  345. }
  346. else
  347. {
  348. /* The prompt spans multiple lines. */
  349. t = ++p;
  350. local_prompt = expand_prompt (p, &prompt_visible_length,
  351. &prompt_last_invisible,
  352. &prompt_invis_chars_first_line,
  353. &prompt_physical_chars);
  354. c = *t; *t = '\0';
  355. /* The portion of the prompt string up to and including the
  356. final newline is now null-terminated. */
  357. local_prompt_prefix = expand_prompt (prompt, &prompt_prefix_length,
  358. (int *)NULL,
  359. (int *)NULL,
  360. (int *)NULL);
  361. *t = c;
  362. local_prompt_len = local_prompt ? strlen (local_prompt) : 0;
  363. return (prompt_prefix_length);
  364. }
  365. }
  366. /* Initialize the VISIBLE_LINE and INVISIBLE_LINE arrays, and their associated
  367. arrays of line break markers. MINSIZE is the minimum size of VISIBLE_LINE
  368. and INVISIBLE_LINE; if it is greater than LINE_SIZE, LINE_SIZE is
  369. increased. If the lines have already been allocated, this ensures that
  370. they can hold at least MINSIZE characters. */
  371. static void
  372. init_line_structures (minsize)
  373. int minsize;
  374. {
  375. register int n;
  376. if (invisible_line == 0) /* initialize it */
  377. {
  378. if (line_size < minsize)
  379. line_size = minsize;
  380. visible_line = (char *)xmalloc (line_size);
  381. invisible_line = (char *)xmalloc (line_size);
  382. }
  383. else if (line_size < minsize) /* ensure it can hold MINSIZE chars */
  384. {
  385. line_size *= 2;
  386. if (line_size < minsize)
  387. line_size = minsize;
  388. visible_line = (char *)xrealloc (visible_line, line_size);
  389. invisible_line = (char *)xrealloc (invisible_line, line_size);
  390. }
  391. for (n = minsize; n < line_size; n++)
  392. {
  393. visible_line[n] = 0;
  394. invisible_line[n] = 1;
  395. }
  396. if (vis_lbreaks == 0)
  397. {
  398. /* should be enough. */
  399. inv_lbsize = vis_lbsize = 256;
  400. #if defined (HANDLE_MULTIBYTE)
  401. line_state_visible->wbsize = vis_lbsize;
  402. line_state_visible->wrapped_line = (int *)xmalloc (line_state_visible->wbsize * sizeof (int));
  403. line_state_invisible->wbsize = inv_lbsize;
  404. line_state_invisible->wrapped_line = (int *)xmalloc (line_state_invisible->wbsize * sizeof (int));
  405. #endif
  406. inv_lbreaks = (int *)xmalloc (inv_lbsize * sizeof (int));
  407. vis_lbreaks = (int *)xmalloc (vis_lbsize * sizeof (int));
  408. inv_lbreaks[0] = vis_lbreaks[0] = 0;
  409. }
  410. line_structures_initialized = 1;
  411. }
  412. /* Basic redisplay algorithm. */
  413. void
  414. rl_redisplay ()
  415. {
  416. register int in, out, c, linenum, cursor_linenum;
  417. register char *line;
  418. int inv_botlin, lb_botlin, lb_linenum, o_cpos;
  419. int newlines, lpos, temp, n0, num, prompt_lines_estimate;
  420. char *prompt_this_line;
  421. #if defined (HANDLE_MULTIBYTE)
  422. wchar_t wc;
  423. size_t wc_bytes;
  424. int wc_width;
  425. mbstate_t ps;
  426. int _rl_wrapped_multicolumn = 0;
  427. #endif
  428. if (_rl_echoing_p == 0)
  429. return;
  430. /* Block keyboard interrupts because this function manipulates global
  431. data structures. */
  432. _rl_block_sigint ();
  433. RL_SETSTATE (RL_STATE_REDISPLAYING);
  434. if (!rl_display_prompt)
  435. rl_display_prompt = "";
  436. if (line_structures_initialized == 0)
  437. {
  438. init_line_structures (0);
  439. rl_on_new_line ();
  440. }
  441. /* Draw the line into the buffer. */
  442. cpos_buffer_position = -1;
  443. prompt_multibyte_chars = prompt_visible_length - prompt_physical_chars;
  444. line = invisible_line;
  445. out = inv_botlin = 0;
  446. /* Mark the line as modified or not. We only do this for history
  447. lines. */
  448. modmark = 0;
  449. if (_rl_mark_modified_lines && current_history () && rl_undo_list)
  450. {
  451. line[out++] = '*';
  452. line[out] = '\0';
  453. modmark = 1;
  454. }
  455. /* If someone thought that the redisplay was handled, but the currently
  456. visible line has a different modification state than the one about
  457. to become visible, then correct the caller's misconception. */
  458. if (visible_line[0] != invisible_line[0])
  459. rl_display_fixed = 0;
  460. /* If the prompt to be displayed is the `primary' readline prompt (the
  461. one passed to readline()), use the values we have already expanded.
  462. If not, use what's already in rl_display_prompt. WRAP_OFFSET is the
  463. number of non-visible characters in the prompt string. */
  464. if (rl_display_prompt == rl_prompt || local_prompt)
  465. {
  466. if (local_prompt_prefix && forced_display)
  467. _rl_output_some_chars (local_prompt_prefix, strlen (local_prompt_prefix));
  468. if (local_prompt_len > 0)
  469. {
  470. temp = local_prompt_len + out + 2;
  471. if (temp >= line_size)
  472. {
  473. line_size = (temp + 1024) - (temp % 1024);
  474. visible_line = (char *)xrealloc (visible_line, line_size);
  475. line = invisible_line = (char *)xrealloc (invisible_line, line_size);
  476. }
  477. strncpy (line + out, local_prompt, local_prompt_len);
  478. out += local_prompt_len;
  479. }
  480. line[out] = '\0';
  481. wrap_offset = local_prompt_len - prompt_visible_length;
  482. }
  483. else
  484. {
  485. int pmtlen;
  486. prompt_this_line = strrchr (rl_display_prompt, '\n');
  487. if (!prompt_this_line)
  488. prompt_this_line = rl_display_prompt;
  489. else
  490. {
  491. prompt_this_line++;
  492. pmtlen = prompt_this_line - rl_display_prompt; /* temp var */
  493. if (forced_display)
  494. {
  495. _rl_output_some_chars (rl_display_prompt, pmtlen);
  496. /* Make sure we are at column zero even after a newline,
  497. regardless of the state of terminal output processing. */
  498. if (pmtlen < 2 || prompt_this_line[-2] != '\r')
  499. cr ();
  500. }
  501. }
  502. prompt_physical_chars = pmtlen = strlen (prompt_this_line);
  503. temp = pmtlen + out + 2;
  504. if (temp >= line_size)
  505. {
  506. line_size = (temp + 1024) - (temp % 1024);
  507. visible_line = (char *)xrealloc (visible_line, line_size);
  508. line = invisible_line = (char *)xrealloc (invisible_line, line_size);
  509. }
  510. strncpy (line + out, prompt_this_line, pmtlen);
  511. out += pmtlen;
  512. line[out] = '\0';
  513. wrap_offset = prompt_invis_chars_first_line = 0;
  514. }
  515. #define CHECK_INV_LBREAKS() \
  516. do { \
  517. if (newlines >= (inv_lbsize - 2)) \
  518. { \
  519. inv_lbsize *= 2; \
  520. inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
  521. } \
  522. } while (0)
  523. #if defined (HANDLE_MULTIBYTE)
  524. #define CHECK_LPOS() \
  525. do { \
  526. lpos++; \
  527. if (lpos >= _rl_screenwidth) \
  528. { \
  529. if (newlines >= (inv_lbsize - 2)) \
  530. { \
  531. inv_lbsize *= 2; \
  532. inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
  533. } \
  534. inv_lbreaks[++newlines] = out; \
  535. if (newlines >= (line_state_invisible->wbsize - 1)) \
  536. { \
  537. line_state_invisible->wbsize *= 2; \
  538. line_state_invisible->wrapped_line = (int *)xrealloc (line_state_invisible->wrapped_line, line_state_invisible->wbsize * sizeof(int)); \
  539. } \
  540. line_state_invisible->wrapped_line[newlines] = _rl_wrapped_multicolumn; \
  541. lpos = 0; \
  542. } \
  543. } while (0)
  544. #else
  545. #define CHECK_LPOS() \
  546. do { \
  547. lpos++; \
  548. if (lpos >= _rl_screenwidth) \
  549. { \
  550. if (newlines >= (inv_lbsize - 2)) \
  551. { \
  552. inv_lbsize *= 2; \
  553. inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
  554. } \
  555. inv_lbreaks[++newlines] = out; \
  556. lpos = 0; \
  557. } \
  558. } while (0)
  559. #endif
  560. /* inv_lbreaks[i] is where line i starts in the buffer. */
  561. inv_lbreaks[newlines = 0] = 0;
  562. lpos = prompt_physical_chars + modmark;
  563. #if defined (HANDLE_MULTIBYTE)
  564. memset (line_state_invisible->wrapped_line, 0, line_state_invisible->wbsize * sizeof (int));
  565. num = 0;
  566. #endif
  567. /* prompt_invis_chars_first_line is the number of invisible characters in
  568. the first physical line of the prompt.
  569. wrap_offset - prompt_invis_chars_first_line is the number of invis
  570. chars on the second (or, more generally, last) line. */
  571. /* This is zero-based, used to set the newlines */
  572. prompt_lines_estimate = lpos / _rl_screenwidth;
  573. /* what if lpos is already >= _rl_screenwidth before we start drawing the
  574. contents of the command line? */
  575. while (lpos >= _rl_screenwidth)
  576. {
  577. int z;
  578. /* fix from Darin Johnson <darin@acuson.com> for prompt string with
  579. invisible characters that is longer than the screen width. The
  580. prompt_invis_chars_first_line variable could be made into an array
  581. saying how many invisible characters there are per line, but that's
  582. probably too much work for the benefit gained. How many people have
  583. prompts that exceed two physical lines?
  584. Additional logic fix from Edward Catmur <ed@catmur.co.uk> */
  585. #if defined (HANDLE_MULTIBYTE)
  586. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0 && prompt_multibyte_chars > 0)
  587. {
  588. n0 = num;
  589. temp = local_prompt_len;
  590. while (num < temp)
  591. {
  592. z = _rl_col_width (local_prompt, n0, num, 1);
  593. if (z > _rl_screenwidth)
  594. {
  595. num = _rl_find_prev_mbchar (local_prompt, num, MB_FIND_ANY);
  596. break;
  597. }
  598. else if (z == _rl_screenwidth)
  599. break;
  600. num++;
  601. }
  602. temp = num;
  603. }
  604. else
  605. #endif /* !HANDLE_MULTIBYTE */
  606. temp = ((newlines + 1) * _rl_screenwidth);
  607. /* Now account for invisible characters in the current line. */
  608. /* XXX - this assumes that the invisible characters may be split, but only
  609. between the first and the last lines. */
  610. temp += ((local_prompt_prefix == 0) ? ((newlines == 0) ? prompt_invis_chars_first_line
  611. : ((newlines == prompt_lines_estimate) ? wrap_offset : prompt_invis_chars_first_line))
  612. : ((newlines == 0) ? wrap_offset : 0));
  613. inv_lbreaks[++newlines] = temp;
  614. #if defined (HANDLE_MULTIBYTE)
  615. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0 && prompt_multibyte_chars > 0)
  616. lpos -= _rl_col_width (local_prompt, n0, num, 1);
  617. else
  618. #endif
  619. lpos -= _rl_screenwidth;
  620. }
  621. prompt_last_screen_line = newlines;
  622. /* Draw the rest of the line (after the prompt) into invisible_line, keeping
  623. track of where the cursor is (cpos_buffer_position), the number of the line containing
  624. the cursor (lb_linenum), the last line number (lb_botlin and inv_botlin).
  625. It maintains an array of line breaks for display (inv_lbreaks).
  626. This handles expanding tabs for display and displaying meta characters. */
  627. lb_linenum = 0;
  628. #if defined (HANDLE_MULTIBYTE)
  629. in = 0;
  630. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  631. {
  632. memset (&ps, 0, sizeof (mbstate_t));
  633. /* XXX - what if wc_bytes ends up <= 0? check for MB_INVALIDCH */
  634. wc_bytes = mbrtowc (&wc, rl_line_buffer, rl_end, &ps);
  635. }
  636. else
  637. wc_bytes = 1;
  638. while (in < rl_end)
  639. #else
  640. for (in = 0; in < rl_end; in++)
  641. #endif
  642. {
  643. c = (unsigned char)rl_line_buffer[in];
  644. #if defined (HANDLE_MULTIBYTE)
  645. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  646. {
  647. if (MB_INVALIDCH (wc_bytes))
  648. {
  649. /* Byte sequence is invalid or shortened. Assume that the
  650. first byte represents a character. */
  651. wc_bytes = 1;
  652. /* Assume that a character occupies a single column. */
  653. wc_width = 1;
  654. memset (&ps, 0, sizeof (mbstate_t));
  655. }
  656. else if (MB_NULLWCH (wc_bytes))
  657. break; /* Found '\0' */
  658. else
  659. {
  660. temp = wcwidth (wc);
  661. wc_width = (temp >= 0) ? temp : 1;
  662. }
  663. }
  664. #endif
  665. if (out + 8 >= line_size) /* XXX - 8 for \t */
  666. {
  667. line_size *= 2;
  668. visible_line = (char *)xrealloc (visible_line, line_size);
  669. invisible_line = (char *)xrealloc (invisible_line, line_size);
  670. line = invisible_line;
  671. }
  672. if (in == rl_point)
  673. {
  674. cpos_buffer_position = out;
  675. lb_linenum = newlines;
  676. }
  677. #if defined (HANDLE_MULTIBYTE)
  678. if (META_CHAR (c) && _rl_output_meta_chars == 0) /* XXX - clean up */
  679. #else
  680. if (META_CHAR (c))
  681. #endif
  682. {
  683. if (_rl_output_meta_chars == 0)
  684. {
  685. sprintf (line + out, "\\%o", c);
  686. if (lpos + 4 >= _rl_screenwidth)
  687. {
  688. temp = _rl_screenwidth - lpos;
  689. CHECK_INV_LBREAKS ();
  690. inv_lbreaks[++newlines] = out + temp;
  691. lpos = 4 - temp;
  692. }
  693. else
  694. lpos += 4;
  695. out += 4;
  696. }
  697. else
  698. {
  699. line[out++] = c;
  700. CHECK_LPOS();
  701. }
  702. }
  703. #if defined (DISPLAY_TABS)
  704. else if (c == '\t')
  705. {
  706. register int newout;
  707. #if 0
  708. newout = (out | (int)7) + 1;
  709. #else
  710. newout = out + 8 - lpos % 8;
  711. #endif
  712. temp = newout - out;
  713. if (lpos + temp >= _rl_screenwidth)
  714. {
  715. register int temp2;
  716. temp2 = _rl_screenwidth - lpos;
  717. CHECK_INV_LBREAKS ();
  718. inv_lbreaks[++newlines] = out + temp2;
  719. lpos = temp - temp2;
  720. while (out < newout)
  721. line[out++] = ' ';
  722. }
  723. else
  724. {
  725. while (out < newout)
  726. line[out++] = ' ';
  727. lpos += temp;
  728. }
  729. }
  730. #endif
  731. else if (c == '\n' && _rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
  732. {
  733. line[out++] = '\0'; /* XXX - sentinel */
  734. CHECK_INV_LBREAKS ();
  735. inv_lbreaks[++newlines] = out;
  736. lpos = 0;
  737. }
  738. else if (CTRL_CHAR (c) || c == RUBOUT)
  739. {
  740. line[out++] = '^';
  741. CHECK_LPOS();
  742. line[out++] = CTRL_CHAR (c) ? UNCTRL (c) : '?';
  743. CHECK_LPOS();
  744. }
  745. else
  746. {
  747. #if defined (HANDLE_MULTIBYTE)
  748. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  749. {
  750. register int i;
  751. _rl_wrapped_multicolumn = 0;
  752. if (_rl_screenwidth < lpos + wc_width)
  753. for (i = lpos; i < _rl_screenwidth; i++)
  754. {
  755. /* The space will be removed in update_line() */
  756. line[out++] = ' ';
  757. _rl_wrapped_multicolumn++;
  758. CHECK_LPOS();
  759. }
  760. if (in == rl_point)
  761. {
  762. cpos_buffer_position = out;
  763. lb_linenum = newlines;
  764. }
  765. for (i = in; i < in+wc_bytes; i++)
  766. line[out++] = rl_line_buffer[i];
  767. for (i = 0; i < wc_width; i++)
  768. CHECK_LPOS();
  769. }
  770. else
  771. {
  772. line[out++] = c;
  773. CHECK_LPOS();
  774. }
  775. #else
  776. line[out++] = c;
  777. CHECK_LPOS();
  778. #endif
  779. }
  780. #if defined (HANDLE_MULTIBYTE)
  781. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  782. {
  783. in += wc_bytes;
  784. /* XXX - what if wc_bytes ends up <= 0? check for MB_INVALIDCH */
  785. wc_bytes = mbrtowc (&wc, rl_line_buffer + in, rl_end - in, &ps);
  786. }
  787. else
  788. in++;
  789. #endif
  790. }
  791. line[out] = '\0';
  792. if (cpos_buffer_position < 0)
  793. {
  794. cpos_buffer_position = out;
  795. lb_linenum = newlines;
  796. }
  797. inv_botlin = lb_botlin = newlines;
  798. CHECK_INV_LBREAKS ();
  799. inv_lbreaks[newlines+1] = out;
  800. cursor_linenum = lb_linenum;
  801. /* CPOS_BUFFER_POSITION == position in buffer where cursor should be placed.
  802. CURSOR_LINENUM == line number where the cursor should be placed. */
  803. /* PWP: now is when things get a bit hairy. The visible and invisible
  804. line buffers are really multiple lines, which would wrap every
  805. (screenwidth - 1) characters. Go through each in turn, finding
  806. the changed region and updating it. The line order is top to bottom. */
  807. /* If we can move the cursor up and down, then use multiple lines,
  808. otherwise, let long lines display in a single terminal line, and
  809. horizontally scroll it. */
  810. if (_rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
  811. {
  812. int nleft, pos, changed_screen_line, tx;
  813. if (!rl_display_fixed || forced_display)
  814. {
  815. forced_display = 0;
  816. /* If we have more than a screenful of material to display, then
  817. only display a screenful. We should display the last screen,
  818. not the first. */
  819. if (out >= _rl_screenchars)
  820. {
  821. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  822. out = _rl_find_prev_mbchar (line, _rl_screenchars, MB_FIND_ANY);
  823. else
  824. out = _rl_screenchars - 1;
  825. }
  826. /* The first line is at character position 0 in the buffer. The
  827. second and subsequent lines start at inv_lbreaks[N], offset by
  828. OFFSET (which has already been calculated above). */
  829. #define INVIS_FIRST() (prompt_physical_chars > _rl_screenwidth ? prompt_invis_chars_first_line : wrap_offset)
  830. #define WRAP_OFFSET(line, offset) ((line == 0) \
  831. ? (offset ? INVIS_FIRST() : 0) \
  832. : ((line == prompt_last_screen_line) ? wrap_offset-prompt_invis_chars_first_line : 0))
  833. #define W_OFFSET(line, offset) ((line) == 0 ? offset : 0)
  834. #define VIS_LLEN(l) ((l) > _rl_vis_botlin ? 0 : (vis_lbreaks[l+1] - vis_lbreaks[l]))
  835. #define INV_LLEN(l) (inv_lbreaks[l+1] - inv_lbreaks[l])
  836. #define VIS_CHARS(line) (visible_line + vis_lbreaks[line])
  837. #define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
  838. #define INV_LINE(line) (invisible_line + inv_lbreaks[line])
  839. #define OLD_CPOS_IN_PROMPT() (cpos_adjusted == 0 && \
  840. _rl_last_c_pos != o_cpos && \
  841. _rl_last_c_pos > wrap_offset && \
  842. o_cpos < prompt_last_invisible)
  843. /* For each line in the buffer, do the updating display. */
  844. for (linenum = 0; linenum <= inv_botlin; linenum++)
  845. {
  846. /* This can lead us astray if we execute a program that changes
  847. the locale from a non-multibyte to a multibyte one. */
  848. o_cpos = _rl_last_c_pos;
  849. cpos_adjusted = 0;
  850. update_line (VIS_LINE(linenum), INV_LINE(linenum), linenum,
  851. VIS_LLEN(linenum), INV_LLEN(linenum), inv_botlin);
  852. /* update_line potentially changes _rl_last_c_pos, but doesn't
  853. take invisible characters into account, since _rl_last_c_pos
  854. is an absolute cursor position in a multibyte locale. See
  855. if compensating here is the right thing, or if we have to
  856. change update_line itself. There are several cases in which
  857. update_line adjusts _rl_last_c_pos itself (so it can pass
  858. _rl_move_cursor_relative accurate values); it communicates
  859. this back by setting cpos_adjusted. If we assume that
  860. _rl_last_c_pos is correct (an absolute cursor position) each
  861. time update_line is called, then we can assume in our
  862. calculations that o_cpos does not need to be adjusted by
  863. wrap_offset. */
  864. if (linenum == 0 && (MB_CUR_MAX > 1 && rl_byte_oriented == 0) && OLD_CPOS_IN_PROMPT())
  865. _rl_last_c_pos -= prompt_invis_chars_first_line; /* XXX - was wrap_offset */
  866. else if (linenum == prompt_last_screen_line && prompt_physical_chars > _rl_screenwidth &&
  867. (MB_CUR_MAX > 1 && rl_byte_oriented == 0) &&
  868. cpos_adjusted == 0 &&
  869. _rl_last_c_pos != o_cpos &&
  870. _rl_last_c_pos > (prompt_last_invisible - _rl_screenwidth - prompt_invis_chars_first_line))
  871. _rl_last_c_pos -= (wrap_offset-prompt_invis_chars_first_line);
  872. /* If this is the line with the prompt, we might need to
  873. compensate for invisible characters in the new line. Do
  874. this only if there is not more than one new line (which
  875. implies that we completely overwrite the old visible line)
  876. and the new line is shorter than the old. Make sure we are
  877. at the end of the new line before clearing. */
  878. if (linenum == 0 &&
  879. inv_botlin == 0 && _rl_last_c_pos == out &&
  880. (wrap_offset > visible_wrap_offset) &&
  881. (_rl_last_c_pos < visible_first_line_len))
  882. {
  883. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  884. nleft = _rl_screenwidth - _rl_last_c_pos;
  885. else
  886. nleft = _rl_screenwidth + wrap_offset - _rl_last_c_pos;
  887. if (nleft)
  888. _rl_clear_to_eol (nleft);
  889. }
  890. #if 0
  891. /* This segment is intended to handle the case where the prompt
  892. has invisible characters on the second line and the new line
  893. to be displayed needs to clear the rest of the old characters
  894. out (e.g., when printing the i-search prompt). In general,
  895. the case of the new line being shorter than the old.
  896. Incomplete */
  897. else if (linenum == prompt_last_screen_line &&
  898. prompt_physical_chars > _rl_screenwidth &&
  899. wrap_offset != prompt_invis_chars_first_line &&
  900. _rl_last_c_pos == out &&
  901. #endif
  902. /* Since the new first line is now visible, save its length. */
  903. if (linenum == 0)
  904. visible_first_line_len = (inv_botlin > 0) ? inv_lbreaks[1] : out - wrap_offset;
  905. }
  906. /* We may have deleted some lines. If so, clear the left over
  907. blank ones at the bottom out. */
  908. if (_rl_vis_botlin > inv_botlin)
  909. {
  910. char *tt;
  911. for (; linenum <= _rl_vis_botlin; linenum++)
  912. {
  913. tt = VIS_CHARS (linenum);
  914. _rl_move_vert (linenum);
  915. _rl_move_cursor_relative (0, tt);
  916. _rl_clear_to_eol
  917. ((linenum == _rl_vis_botlin) ? strlen (tt) : _rl_screenwidth);
  918. }
  919. }
  920. _rl_vis_botlin = inv_botlin;
  921. /* CHANGED_SCREEN_LINE is set to 1 if we have moved to a
  922. different screen line during this redisplay. */
  923. changed_screen_line = _rl_last_v_pos != cursor_linenum;
  924. if (changed_screen_line)
  925. {
  926. _rl_move_vert (cursor_linenum);
  927. /* If we moved up to the line with the prompt using _rl_term_up,
  928. the physical cursor position on the screen stays the same,
  929. but the buffer position needs to be adjusted to account
  930. for invisible characters. */
  931. if ((MB_CUR_MAX == 1 || rl_byte_oriented) && cursor_linenum == 0 && wrap_offset)
  932. _rl_last_c_pos += wrap_offset;
  933. }
  934. /* We have to reprint the prompt if it contains invisible
  935. characters, since it's not generally OK to just reprint
  936. the characters from the current cursor position. But we
  937. only need to reprint it if the cursor is before the last
  938. invisible character in the prompt string. */
  939. nleft = prompt_visible_length + wrap_offset;
  940. if (cursor_linenum == 0 && wrap_offset > 0 && _rl_last_c_pos > 0 &&
  941. #if 0
  942. _rl_last_c_pos <= PROMPT_ENDING_INDEX && local_prompt)
  943. #else
  944. _rl_last_c_pos < PROMPT_ENDING_INDEX && local_prompt)
  945. #endif
  946. {
  947. #if defined (__MSDOS__)
  948. putc ('\r', rl_outstream);
  949. #else
  950. if (_rl_term_cr)
  951. tputs (_rl_term_cr, 1, _rl_output_character_function);
  952. #endif
  953. if (modmark)
  954. _rl_output_some_chars ("*", 1);
  955. _rl_output_some_chars (local_prompt, nleft);
  956. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  957. _rl_last_c_pos = _rl_col_width (local_prompt, 0, nleft, 1) - wrap_offset + modmark;
  958. else
  959. _rl_last_c_pos = nleft + modmark;
  960. }
  961. /* Where on that line? And where does that line start
  962. in the buffer? */
  963. pos = inv_lbreaks[cursor_linenum];
  964. /* nleft == number of characters in the line buffer between the
  965. start of the line and the desired cursor position. */
  966. nleft = cpos_buffer_position - pos;
  967. /* NLEFT is now a number of characters in a buffer. When in a
  968. multibyte locale, however, _rl_last_c_pos is an absolute cursor
  969. position that doesn't take invisible characters in the prompt
  970. into account. We use a fudge factor to compensate. */
  971. /* Since _rl_backspace() doesn't know about invisible characters in the
  972. prompt, and there's no good way to tell it, we compensate for
  973. those characters here and call _rl_backspace() directly. */
  974. if (wrap_offset && cursor_linenum == 0 && nleft < _rl_last_c_pos)
  975. {
  976. /* TX == new physical cursor position in multibyte locale. */
  977. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  978. tx = _rl_col_width (&visible_line[pos], 0, nleft, 1) - visible_wrap_offset;
  979. else
  980. tx = nleft;
  981. if (tx >= 0 && _rl_last_c_pos > tx)
  982. {
  983. _rl_backspace (_rl_last_c_pos - tx); /* XXX */
  984. _rl_last_c_pos = tx;
  985. }
  986. }
  987. /* We need to note that in a multibyte locale we are dealing with
  988. _rl_last_c_pos as an absolute cursor position, but moving to a
  989. point specified by a buffer position (NLEFT) that doesn't take
  990. invisible characters into account. */
  991. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  992. _rl_move_cursor_relative (nleft, &invisible_line[pos]);
  993. else if (nleft != _rl_last_c_pos)
  994. _rl_move_cursor_relative (nleft, &invisible_line[pos]);
  995. }
  996. }
  997. else /* Do horizontal scrolling. */
  998. {
  999. #define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
  1000. int lmargin, ndisp, nleft, phys_c_pos, t;
  1001. /* Always at top line. */
  1002. _rl_last_v_pos = 0;
  1003. /* Compute where in the buffer the displayed line should start. This
  1004. will be LMARGIN. */
  1005. /* The number of characters that will be displayed before the cursor. */
  1006. ndisp = cpos_buffer_position - wrap_offset;
  1007. nleft = prompt_visible_length + wrap_offset;
  1008. /* Where the new cursor position will be on the screen. This can be
  1009. longer than SCREENWIDTH; if it is, lmargin will be adjusted. */
  1010. phys_c_pos = cpos_buffer_position - (last_lmargin ? last_lmargin : wrap_offset);
  1011. t = _rl_screenwidth / 3;
  1012. /* If the number of characters had already exceeded the screenwidth,
  1013. last_lmargin will be > 0. */
  1014. /* If the number of characters to be displayed is more than the screen
  1015. width, compute the starting offset so that the cursor is about
  1016. two-thirds of the way across the screen. */
  1017. if (phys_c_pos > _rl_screenwidth - 2)
  1018. {
  1019. lmargin = cpos_buffer_position - (2 * t);
  1020. if (lmargin < 0)
  1021. lmargin = 0;
  1022. /* If the left margin would be in the middle of a prompt with
  1023. invisible characters, don't display the prompt at all. */
  1024. if (wrap_offset && lmargin > 0 && lmargin < nleft)
  1025. lmargin = nleft;
  1026. }
  1027. else if (ndisp < _rl_screenwidth - 2) /* XXX - was -1 */
  1028. lmargin = 0;
  1029. else if (phys_c_pos < 1)
  1030. {
  1031. /* If we are moving back towards the beginning of the line and
  1032. the last margin is no longer correct, compute a new one. */
  1033. lmargin = ((cpos_buffer_position - 1) / t) * t; /* XXX */
  1034. if (wrap_offset && lmargin > 0 && lmargin < nleft)
  1035. lmargin = nleft;
  1036. }
  1037. else
  1038. lmargin = last_lmargin;
  1039. /* If the first character on the screen isn't the first character
  1040. in the display line, indicate this with a special character. */
  1041. if (lmargin > 0)
  1042. line[lmargin] = '<';
  1043. /* If SCREENWIDTH characters starting at LMARGIN do not encompass
  1044. the whole line, indicate that with a special character at the
  1045. right edge of the screen. If LMARGIN is 0, we need to take the
  1046. wrap offset into account. */
  1047. t = lmargin + M_OFFSET (lmargin, wrap_offset) + _rl_screenwidth;
  1048. if (t < out)
  1049. line[t - 1] = '>';
  1050. if (rl_display_fixed == 0 || forced_display || lmargin != last_lmargin)
  1051. {
  1052. forced_display = 0;
  1053. o_cpos = _rl_last_c_pos;
  1054. cpos_adjusted = 0;
  1055. update_line (&visible_line[last_lmargin],
  1056. &invisible_line[lmargin],
  1057. 0,
  1058. _rl_screenwidth + visible_wrap_offset,
  1059. _rl_screenwidth + (lmargin ? 0 : wrap_offset),
  1060. 0);
  1061. if ((MB_CUR_MAX > 1 && rl_byte_oriented == 0) && OLD_CPOS_IN_PROMPT())
  1062. _rl_last_c_pos -= prompt_invis_chars_first_line; /* XXX - was wrap_offset */
  1063. /* If the visible new line is shorter than the old, but the number
  1064. of invisible characters is greater, and we are at the end of
  1065. the new line, we need to clear to eol. */
  1066. t = _rl_last_c_pos - M_OFFSET (lmargin, wrap_offset);
  1067. if ((M_OFFSET (lmargin, wrap_offset) > visible_wrap_offset) &&
  1068. (_rl_last_c_pos == out) &&
  1069. t < visible_first_line_len)
  1070. {
  1071. nleft = _rl_screenwidth - t;
  1072. _rl_clear_to_eol (nleft);
  1073. }
  1074. visible_first_line_len = out - lmargin - M_OFFSET (lmargin, wrap_offset);
  1075. if (visible_first_line_len > _rl_screenwidth)
  1076. visible_first_line_len = _rl_screenwidth;
  1077. _rl_move_cursor_relative (cpos_buffer_position - lmargin, &invisible_line[lmargin]);
  1078. last_lmargin = lmargin;
  1079. }
  1080. }
  1081. fflush (rl_outstream);
  1082. /* Swap visible and non-visible lines. */
  1083. {
  1084. struct line_state *vtemp = line_state_visible;
  1085. line_state_visible = line_state_invisible;
  1086. line_state_invisible = vtemp;
  1087. rl_display_fixed = 0;
  1088. /* If we are displaying on a single line, and last_lmargin is > 0, we
  1089. are not displaying any invisible characters, so set visible_wrap_offset
  1090. to 0. */
  1091. if (_rl_horizontal_scroll_mode && last_lmargin)
  1092. visible_wrap_offset = 0;
  1093. else
  1094. visible_wrap_offset = wrap_offset;
  1095. }
  1096. RL_UNSETSTATE (RL_STATE_REDISPLAYING);
  1097. _rl_release_sigint ();
  1098. }
  1099. /* PWP: update_line() is based on finding the middle difference of each
  1100. line on the screen; vis:
  1101. /old first difference
  1102. /beginning of line | /old last same /old EOL
  1103. v v v v
  1104. old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
  1105. new: eddie> Oh, my little buggy says to me, as lurgid as
  1106. ^ ^ ^ ^
  1107. \beginning of line | \new last same \new end of line
  1108. \new first difference
  1109. All are character pointers for the sake of speed. Special cases for
  1110. no differences, as well as for end of line additions must be handled.
  1111. Could be made even smarter, but this works well enough */
  1112. static void
  1113. update_line (old, new, current_line, omax, nmax, inv_botlin)
  1114. register char *old, *new;
  1115. int current_line, omax, nmax, inv_botlin;
  1116. {
  1117. register char *ofd, *ols, *oe, *nfd, *nls, *ne;
  1118. int temp, lendiff, wsatend, od, nd, twidth, o_cpos;
  1119. int current_invis_chars;
  1120. int col_lendiff, col_temp;
  1121. #if defined (HANDLE_MULTIBYTE)
  1122. mbstate_t ps_new, ps_old;
  1123. int new_offset, old_offset;
  1124. #endif
  1125. /* If we're at the right edge of a terminal that supports xn, we're
  1126. ready to wrap around, so do so. This fixes problems with knowing
  1127. the exact cursor position and cut-and-paste with certain terminal
  1128. emulators. In this calculation, TEMP is the physical screen
  1129. position of the cursor. */
  1130. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1131. temp = _rl_last_c_pos;
  1132. else
  1133. temp = _rl_last_c_pos - WRAP_OFFSET (_rl_last_v_pos, visible_wrap_offset);
  1134. if (temp == _rl_screenwidth && _rl_term_autowrap && !_rl_horizontal_scroll_mode
  1135. && _rl_last_v_pos == current_line - 1)
  1136. {
  1137. #if defined (HANDLE_MULTIBYTE)
  1138. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1139. {
  1140. wchar_t wc;
  1141. mbstate_t ps;
  1142. int tempwidth, bytes;
  1143. size_t ret;
  1144. /* This fixes only double-column characters, but if the wrapped
  1145. character comsumes more than three columns, spaces will be
  1146. inserted in the string buffer. */
  1147. if (current_line < line_state_visible->wbsize && line_state_visible->wrapped_line[current_line] > 0)
  1148. _rl_clear_to_eol (line_state_visible->wrapped_line[current_line]);
  1149. memset (&ps, 0, sizeof (mbstate_t));
  1150. ret = mbrtowc (&wc, new, MB_CUR_MAX, &ps);
  1151. if (MB_INVALIDCH (ret))
  1152. {
  1153. tempwidth = 1;
  1154. ret = 1;
  1155. }
  1156. else if (MB_NULLWCH (ret))
  1157. tempwidth = 0;
  1158. else
  1159. tempwidth = wcwidth (wc);
  1160. if (tempwidth > 0)
  1161. {
  1162. int count, i;
  1163. bytes = ret;
  1164. for (count = 0; count < bytes; count++)
  1165. putc (new[count], rl_outstream);
  1166. _rl_last_c_pos = tempwidth;
  1167. _rl_last_v_pos++;
  1168. memset (&ps, 0, sizeof (mbstate_t));
  1169. ret = mbrtowc (&wc, old, MB_CUR_MAX, &ps);
  1170. if (ret != 0 && bytes != 0)
  1171. {
  1172. if (MB_INVALIDCH (ret))
  1173. ret = 1;
  1174. memmove (old+bytes, old+ret, strlen (old+ret));
  1175. memcpy (old, new, bytes);
  1176. /* Fix up indices if we copy data from one line to another */
  1177. omax += bytes - ret;
  1178. for (i = current_line+1; i < inv_botlin+1; i++)
  1179. vis_lbreaks[i] += bytes - ret;
  1180. }
  1181. }
  1182. else
  1183. {
  1184. putc (' ', rl_outstream);
  1185. _rl_last_c_pos = 1;
  1186. _rl_last_v_pos++;
  1187. if (old[0] && new[0])
  1188. old[0] = new[0];
  1189. }
  1190. }
  1191. else
  1192. #endif
  1193. {
  1194. if (new[0])
  1195. putc (new[0], rl_outstream);
  1196. else
  1197. putc (' ', rl_outstream);
  1198. _rl_last_c_pos = 1;
  1199. _rl_last_v_pos++;
  1200. if (old[0] && new[0])
  1201. old[0] = new[0];
  1202. }
  1203. }
  1204. /* Find first difference. */
  1205. #if defined (HANDLE_MULTIBYTE)
  1206. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1207. {
  1208. /* See if the old line is a subset of the new line, so that the
  1209. only change is adding characters. */
  1210. temp = (omax < nmax) ? omax : nmax;
  1211. if (memcmp (old, new, temp) == 0) /* adding at the end */
  1212. {
  1213. ofd = old + temp;
  1214. nfd = new + temp;
  1215. }
  1216. else
  1217. {
  1218. memset (&ps_new, 0, sizeof(mbstate_t));
  1219. memset (&ps_old, 0, sizeof(mbstate_t));
  1220. if (omax == nmax && STREQN (new, old, omax))
  1221. {
  1222. ofd = old + omax;
  1223. nfd = new + nmax;
  1224. }
  1225. else
  1226. {
  1227. new_offset = old_offset = 0;
  1228. for (ofd = old, nfd = new;
  1229. (ofd - old < omax) && *ofd &&
  1230. _rl_compare_chars(old, old_offset, &ps_old, new, new_offset, &ps_new); )
  1231. {
  1232. old_offset = _rl_find_next_mbchar (old, old_offset, 1, MB_FIND_ANY);
  1233. new_offset = _rl_find_next_mbchar (new, new_offset, 1, MB_FIND_ANY);
  1234. ofd = old + old_offset;
  1235. nfd = new + new_offset;
  1236. }
  1237. }
  1238. }
  1239. }
  1240. else
  1241. #endif
  1242. for (ofd = old, nfd = new;
  1243. (ofd - old < omax) && *ofd && (*ofd == *nfd);
  1244. ofd++, nfd++)
  1245. ;
  1246. /* Move to the end of the screen line. ND and OD are used to keep track
  1247. of the distance between ne and new and oe and old, respectively, to
  1248. move a subtraction out of each loop. */
  1249. for (od = ofd - old, oe = ofd; od < omax && *oe; oe++, od++);
  1250. for (nd = nfd - new, ne = nfd; nd < nmax && *ne; ne++, nd++);
  1251. /* If no difference, continue to next line. */
  1252. if (ofd == oe && nfd == ne)
  1253. return;
  1254. wsatend = 1; /* flag for trailing whitespace */
  1255. #if defined (HANDLE_MULTIBYTE)
  1256. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1257. {
  1258. ols = old + _rl_find_prev_mbchar (old, oe - old, MB_FIND_ANY);
  1259. nls = new + _rl_find_prev_mbchar (new, ne - new, MB_FIND_ANY);
  1260. while ((ols > ofd) && (nls > nfd))
  1261. {
  1262. memset (&ps_old, 0, sizeof (mbstate_t));
  1263. memset (&ps_new, 0, sizeof (mbstate_t));
  1264. #if 0
  1265. /* On advice from jir@yamato.ibm.com */
  1266. _rl_adjust_point (old, ols - old, &ps_old);
  1267. _rl_adjust_point (new, nls - new, &ps_new);
  1268. #endif
  1269. if (_rl_compare_chars (old, ols - old, &ps_old, new, nls - new, &ps_new) == 0)
  1270. break;
  1271. if (*ols == ' ')
  1272. wsatend = 0;
  1273. ols = old + _rl_find_prev_mbchar (old, ols - old, MB_FIND_ANY);
  1274. nls = new + _rl_find_prev_mbchar (new, nls - new, MB_FIND_ANY);
  1275. }
  1276. }
  1277. else
  1278. {
  1279. #endif /* HANDLE_MULTIBYTE */
  1280. ols = oe - 1; /* find last same */
  1281. nls = ne - 1;
  1282. while ((ols > ofd) && (nls > nfd) && (*ols == *nls))
  1283. {
  1284. if (*ols != ' ')
  1285. wsatend = 0;
  1286. ols--;
  1287. nls--;
  1288. }
  1289. #if defined (HANDLE_MULTIBYTE)
  1290. }
  1291. #endif
  1292. if (wsatend)
  1293. {
  1294. ols = oe;
  1295. nls = ne;
  1296. }
  1297. #if defined (HANDLE_MULTIBYTE)
  1298. /* This may not work for stateful encoding, but who cares? To handle
  1299. stateful encoding properly, we have to scan each string from the
  1300. beginning and compare. */
  1301. else if (_rl_compare_chars (ols, 0, NULL, nls, 0, NULL) == 0)
  1302. #else
  1303. else if (*ols != *nls)
  1304. #endif
  1305. {
  1306. if (*ols) /* don't step past the NUL */
  1307. {
  1308. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1309. ols = old + _rl_find_next_mbchar (old, ols - old, 1, MB_FIND_ANY);
  1310. else
  1311. ols++;
  1312. }
  1313. if (*nls)
  1314. {
  1315. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1316. nls = new + _rl_find_next_mbchar (new, nls - new, 1, MB_FIND_ANY);
  1317. else
  1318. nls++;
  1319. }
  1320. }
  1321. /* count of invisible characters in the current invisible line. */
  1322. current_invis_chars = W_OFFSET (current_line, wrap_offset);
  1323. if (_rl_last_v_pos != current_line)
  1324. {
  1325. _rl_move_vert (current_line);
  1326. if ((MB_CUR_MAX == 1 || rl_byte_oriented) && current_line == 0 && visible_wrap_offset)
  1327. _rl_last_c_pos += visible_wrap_offset;
  1328. }
  1329. /* If this is the first line and there are invisible characters in the
  1330. prompt string, and the prompt string has not changed, and the current
  1331. cursor position is before the last invisible character in the prompt,
  1332. and the index of the character to move to is past the end of the prompt
  1333. string, then redraw the entire prompt string. We can only do this
  1334. reliably if the terminal supports a `cr' capability.
  1335. This is not an efficiency hack -- there is a problem with redrawing
  1336. portions of the prompt string if they contain terminal escape
  1337. sequences (like drawing the `unbold' sequence without a corresponding
  1338. `bold') that manifests itself on certain terminals. */
  1339. lendiff = local_prompt_len;
  1340. od = ofd - old; /* index of first difference in visible line */
  1341. if (current_line == 0 && !_rl_horizontal_scroll_mode &&
  1342. _rl_term_cr && lendiff > prompt_visible_length && _rl_last_c_pos > 0 &&
  1343. od >= lendiff && _rl_last_c_pos < PROMPT_ENDING_INDEX)
  1344. {
  1345. #if defined (__MSDOS__)
  1346. putc ('\r', rl_outstream);
  1347. #else
  1348. tputs (_rl_term_cr, 1, _rl_output_character_function);
  1349. #endif
  1350. if (modmark)
  1351. _rl_output_some_chars ("*", 1);
  1352. _rl_output_some_chars (local_prompt, lendiff);
  1353. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1354. {
  1355. /* We take wrap_offset into account here so we can pass correct
  1356. information to _rl_move_cursor_relative. */
  1357. _rl_last_c_pos = _rl_col_width (local_prompt, 0, lendiff, 1) - wrap_offset + modmark;
  1358. cpos_adjusted = 1;
  1359. }
  1360. else
  1361. _rl_last_c_pos = lendiff + modmark;
  1362. }
  1363. o_cpos = _rl_last_c_pos;
  1364. /* When this function returns, _rl_last_c_pos is correct, and an absolute
  1365. cursor postion in multibyte mode, but a buffer index when not in a
  1366. multibyte locale. */
  1367. _rl_move_cursor_relative (od, old);
  1368. #if 1
  1369. #if defined (HANDLE_MULTIBYTE)
  1370. /* We need to indicate that the cursor position is correct in the presence of
  1371. invisible characters in the prompt string. Let's see if setting this when
  1372. we make sure we're at the end of the drawn prompt string works. */
  1373. if (current_line == 0 && MB_CUR_MAX > 1 && rl_byte_oriented == 0 &&
  1374. (_rl_last_c_pos > 0 || o_cpos > 0) &&
  1375. _rl_last_c_pos == prompt_physical_chars)
  1376. cpos_adjusted = 1;
  1377. #endif
  1378. #endif
  1379. /* if (len (new) > len (old))
  1380. lendiff == difference in buffer
  1381. col_lendiff == difference on screen
  1382. When not using multibyte characters, these are equal */
  1383. lendiff = (nls - nfd) - (ols - ofd);
  1384. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1385. col_lendiff = _rl_col_width (new, nfd - new, nls - new, 1) - _rl_col_width (old, ofd - old, ols - old, 1);
  1386. else
  1387. col_lendiff = lendiff;
  1388. /* If we are changing the number of invisible characters in a line, and
  1389. the spot of first difference is before the end of the invisible chars,
  1390. lendiff needs to be adjusted. */
  1391. if (current_line == 0 && !_rl_horizontal_scroll_mode &&
  1392. current_invis_chars != visible_wrap_offset)
  1393. {
  1394. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1395. {
  1396. lendiff += visible_wrap_offset - current_invis_chars;
  1397. col_lendiff += visible_wrap_offset - current_invis_chars;
  1398. }
  1399. else
  1400. {
  1401. lendiff += visible_wrap_offset - current_invis_chars;
  1402. col_lendiff = lendiff;
  1403. }
  1404. }
  1405. /* Insert (diff (len (old), len (new)) ch. */
  1406. temp = ne - nfd;
  1407. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1408. col_temp = _rl_col_width (new, nfd - new, ne - new, 1);
  1409. else
  1410. col_temp = temp;
  1411. if (col_lendiff > 0) /* XXX - was lendiff */
  1412. {
  1413. /* Non-zero if we're increasing the number of lines. */
  1414. int gl = current_line >= _rl_vis_botlin && inv_botlin > _rl_vis_botlin;
  1415. /* If col_lendiff is > 0, implying that the new string takes up more
  1416. screen real estate than the old, but lendiff is < 0, meaning that it
  1417. takes fewer bytes, we need to just output the characters starting
  1418. from the first difference. These will overwrite what is on the
  1419. display, so there's no reason to do a smart update. This can really
  1420. only happen in a multibyte environment. */
  1421. if (lendiff < 0)
  1422. {
  1423. _rl_output_some_chars (nfd, temp);
  1424. _rl_last_c_pos += _rl_col_width (nfd, 0, temp, 1);
  1425. /* If nfd begins before any invisible characters in the prompt,
  1426. adjust _rl_last_c_pos to account for wrap_offset and set
  1427. cpos_adjusted to let the caller know. */
  1428. if (current_line == 0 && wrap_offset && ((nfd - new) <= prompt_last_invisible))
  1429. {
  1430. _rl_last_c_pos -= wrap_offset;
  1431. cpos_adjusted = 1;
  1432. }
  1433. return;
  1434. }
  1435. /* Sometimes it is cheaper to print the characters rather than
  1436. use the terminal's capabilities. If we're growing the number
  1437. of lines, make sure we actually cause the new line to wrap
  1438. around on auto-wrapping terminals. */
  1439. else if (_rl_terminal_can_insert && ((2 * col_temp) >= col_lendiff || _rl_term_IC) && (!_rl_term_autowrap || !gl))
  1440. {
  1441. /* If lendiff > prompt_visible_length and _rl_last_c_pos == 0 and
  1442. _rl_horizontal_scroll_mode == 1, inserting the characters with
  1443. _rl_term_IC or _rl_term_ic will screw up the screen because of the
  1444. invisible characters. We need to just draw them. */
  1445. /* The same thing happens if we're trying to draw before the last
  1446. invisible character in the prompt string or we're increasing the
  1447. number of invisible characters in the line and we're not drawing
  1448. the entire prompt string. */
  1449. if (*ols && ((_rl_horizontal_scroll_mode &&
  1450. _rl_last_c_pos == 0 &&
  1451. lendiff > prompt_visible_length &&
  1452. current_invis_chars > 0) == 0) &&
  1453. (((MB_CUR_MAX > 1 && rl_byte_oriented == 0) &&
  1454. current_line == 0 && wrap_offset &&
  1455. ((nfd - new) <= prompt_last_invisible) &&
  1456. (col_lendiff < prompt_visible_length)) == 0) &&
  1457. (visible_wrap_offset >= current_invis_chars))
  1458. {
  1459. insert_some_chars (nfd, lendiff, col_lendiff);
  1460. _rl_last_c_pos += col_lendiff;
  1461. }
  1462. #if 0 /* XXX - for now */
  1463. else if ((MB_CUR_MAX > 1 && rl_byte_oriented == 0) && _rl_last_c_pos == 0 && wrap_offset && (nfd-new) <= prompt_last_invisible && col_lendiff < prompt_visible_length && visible_wrap_offset >= current_invis_chars)
  1464. {
  1465. _rl_output_some_chars (nfd, lendiff);
  1466. _rl_last_c_pos += col_lendiff;
  1467. }
  1468. #endif
  1469. else if ((MB_CUR_MAX == 1 || rl_byte_oriented != 0) && *ols == 0 && lendiff > 0)
  1470. {
  1471. /* At the end of a line the characters do not have to
  1472. be "inserted". They can just be placed on the screen. */
  1473. /* However, this screws up the rest of this block, which
  1474. assumes you've done the insert because you can. */
  1475. _rl_output_some_chars (nfd, lendiff);
  1476. _rl_last_c_pos += col_lendiff;
  1477. }
  1478. else
  1479. {
  1480. _rl_output_some_chars (nfd, temp);
  1481. _rl_last_c_pos += col_temp;
  1482. /* If nfd begins before the last invisible character in the
  1483. prompt, adjust _rl_last_c_pos to account for wrap_offset
  1484. and set cpos_adjusted to let the caller know. */
  1485. if ((MB_CUR_MAX > 1 && rl_byte_oriented == 0) && current_line == 0 && wrap_offset && ((nfd - new) <= prompt_last_invisible))
  1486. {
  1487. _rl_last_c_pos -= wrap_offset;
  1488. cpos_adjusted = 1;
  1489. }
  1490. return;
  1491. }
  1492. /* Copy (new) chars to screen from first diff to last match. */
  1493. temp = nls - nfd;
  1494. if ((temp - lendiff) > 0)
  1495. {
  1496. _rl_output_some_chars (nfd + lendiff, temp - lendiff);
  1497. /* XXX -- this bears closer inspection. Fixes a redisplay bug
  1498. reported against bash-3.0-alpha by Andreas Schwab involving
  1499. multibyte characters and prompt strings with invisible
  1500. characters, but was previously disabled. */
  1501. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1502. twidth = _rl_col_width (nfd+lendiff, 0, temp-col_lendiff, 1);
  1503. else
  1504. twidth = temp - lendiff;
  1505. _rl_last_c_pos += twidth;
  1506. /* If nfd begins before the last invisible character in the
  1507. prompt, adjust _rl_last_c_pos to account for wrap_offset
  1508. and set cpos_adjusted to let the caller know. */
  1509. if ((MB_CUR_MAX > 1 && rl_byte_oriented == 0) && current_line == 0 && wrap_offset && ((nfd - new) <= prompt_last_invisible))
  1510. {
  1511. _rl_last_c_pos -= wrap_offset;
  1512. cpos_adjusted = 1;
  1513. }
  1514. }
  1515. }
  1516. else
  1517. {
  1518. /* cannot insert chars, write to EOL */
  1519. _rl_output_some_chars (nfd, temp);
  1520. _rl_last_c_pos += col_temp;
  1521. /* If we're in a multibyte locale and were before the last invisible
  1522. char in the current line (which implies we just output some invisible
  1523. characters) we need to adjust _rl_last_c_pos, since it represents
  1524. a physical character position. */
  1525. if ((MB_CUR_MAX > 1 && rl_byte_oriented == 0) &&
  1526. current_line == prompt_last_screen_line && wrap_offset &&
  1527. wrap_offset != prompt_invis_chars_first_line &&
  1528. ((nfd-new) < (prompt_last_invisible-(current_line*_rl_screenwidth))))
  1529. {
  1530. _rl_last_c_pos -= wrap_offset - prompt_invis_chars_first_line;
  1531. cpos_adjusted = 1;
  1532. }
  1533. }
  1534. }
  1535. else /* Delete characters from line. */
  1536. {
  1537. /* If possible and inexpensive to use terminal deletion, then do so. */
  1538. if (_rl_term_dc && (2 * col_temp) >= -col_lendiff)
  1539. {
  1540. /* If all we're doing is erasing the invisible characters in the
  1541. prompt string, don't bother. It screws up the assumptions
  1542. about what's on the screen. */
  1543. if (_rl_horizontal_scroll_mode && _rl_last_c_pos == 0 &&
  1544. -lendiff == visible_wrap_offset)
  1545. col_lendiff = 0;
  1546. if (col_lendiff)
  1547. delete_chars (-col_lendiff); /* delete (diff) characters */
  1548. /* Copy (new) chars to screen from first diff to last match */
  1549. temp = nls - nfd;
  1550. if (temp > 0)
  1551. {
  1552. /* If nfd begins at the prompt, or before the invisible
  1553. characters in the prompt, we need to adjust _rl_last_c_pos
  1554. in a multibyte locale to account for the wrap offset and
  1555. set cpos_adjusted accordingly. */
  1556. _rl_output_some_chars (nfd, temp);
  1557. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1558. {
  1559. _rl_last_c_pos += _rl_col_width (nfd, 0, temp, 1);
  1560. if (current_line == 0 && wrap_offset && ((nfd - new) <= prompt_last_invisible))
  1561. {
  1562. _rl_last_c_pos -= wrap_offset;
  1563. cpos_adjusted = 1;
  1564. }
  1565. }
  1566. else
  1567. _rl_last_c_pos += temp;
  1568. }
  1569. }
  1570. /* Otherwise, print over the existing material. */
  1571. else
  1572. {
  1573. if (temp > 0)
  1574. {
  1575. /* If nfd begins at the prompt, or before the invisible
  1576. characters in the prompt, we need to adjust _rl_last_c_pos
  1577. in a multibyte locale to account for the wrap offset and
  1578. set cpos_adjusted accordingly. */
  1579. _rl_output_some_chars (nfd, temp);
  1580. _rl_last_c_pos += col_temp; /* XXX */
  1581. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1582. {
  1583. if (current_line == 0 && wrap_offset && ((nfd - new) <= prompt_last_invisible))
  1584. {
  1585. _rl_last_c_pos -= wrap_offset;
  1586. cpos_adjusted = 1;
  1587. }
  1588. }
  1589. }
  1590. lendiff = (oe - old) - (ne - new);
  1591. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1592. col_lendiff = _rl_col_width (old, 0, oe - old, 1) - _rl_col_width (new, 0, ne - new, 1);
  1593. else
  1594. col_lendiff = lendiff;
  1595. #if 0
  1596. if (col_lendiff)
  1597. #else
  1598. /* If we've already printed over the entire width of the screen,
  1599. including the old material, then col_lendiff doesn't matter and
  1600. space_to_eol will insert too many spaces. XXX - maybe we should
  1601. adjust col_lendiff based on the difference between _rl_last_c_pos
  1602. and _rl_screenwidth */
  1603. if (col_lendiff && ((MB_CUR_MAX == 1 || rl_byte_oriented) || (_rl_last_c_pos < _rl_screenwidth)))
  1604. #endif
  1605. {
  1606. if (_rl_term_autowrap && current_line < inv_botlin)
  1607. space_to_eol (col_lendiff);
  1608. else
  1609. _rl_clear_to_eol (col_lendiff);
  1610. }
  1611. }
  1612. }
  1613. }
  1614. /* Tell the update routines that we have moved onto a new (empty) line. */
  1615. int
  1616. rl_on_new_line ()
  1617. {
  1618. if (visible_line)
  1619. visible_line[0] = '\0';
  1620. _rl_last_c_pos = _rl_last_v_pos = 0;
  1621. _rl_vis_botlin = last_lmargin = 0;
  1622. if (vis_lbreaks)
  1623. vis_lbreaks[0] = vis_lbreaks[1] = 0;
  1624. visible_wrap_offset = 0;
  1625. return 0;
  1626. }
  1627. /* Tell the update routines that we have moved onto a new line with the
  1628. prompt already displayed. Code originally from the version of readline
  1629. distributed with CLISP. rl_expand_prompt must have already been called
  1630. (explicitly or implicitly). This still doesn't work exactly right. */
  1631. int
  1632. rl_on_new_line_with_prompt ()
  1633. {
  1634. int prompt_size, i, l, real_screenwidth, newlines;
  1635. char *prompt_last_line, *lprompt;
  1636. /* Initialize visible_line and invisible_line to ensure that they can hold
  1637. the already-displayed prompt. */
  1638. prompt_size = strlen (rl_prompt) + 1;
  1639. init_line_structures (prompt_size);
  1640. /* Make sure the line structures hold the already-displayed prompt for
  1641. redisplay. */
  1642. lprompt = local_prompt ? local_prompt : rl_prompt;
  1643. strcpy (visible_line, lprompt);
  1644. strcpy (invisible_line, lprompt);
  1645. /* If the prompt contains newlines, take the last tail. */
  1646. prompt_last_line = strrchr (rl_prompt, '\n');
  1647. if (!prompt_last_line)
  1648. prompt_last_line = rl_prompt;
  1649. l = strlen (prompt_last_line);
  1650. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1651. _rl_last_c_pos = _rl_col_width (prompt_last_line, 0, l, 1); /* XXX */
  1652. else
  1653. _rl_last_c_pos = l;
  1654. /* Dissect prompt_last_line into screen lines. Note that here we have
  1655. to use the real screenwidth. Readline's notion of screenwidth might be
  1656. one less, see terminal.c. */
  1657. real_screenwidth = _rl_screenwidth + (_rl_term_autowrap ? 0 : 1);
  1658. _rl_last_v_pos = l / real_screenwidth;
  1659. /* If the prompt length is a multiple of real_screenwidth, we don't know
  1660. whether the cursor is at the end of the last line, or already at the
  1661. beginning of the next line. Output a newline just to be safe. */
  1662. if (l > 0 && (l % real_screenwidth) == 0)
  1663. _rl_output_some_chars ("\n", 1);
  1664. last_lmargin = 0;
  1665. newlines = 0; i = 0;
  1666. while (i <= l)
  1667. {
  1668. _rl_vis_botlin = newlines;
  1669. vis_lbreaks[newlines++] = i;
  1670. i += real_screenwidth;
  1671. }
  1672. vis_lbreaks[newlines] = l;
  1673. visible_wrap_offset = 0;
  1674. rl_display_prompt = rl_prompt; /* XXX - make sure it's set */
  1675. return 0;
  1676. }
  1677. /* Actually update the display, period. */
  1678. int
  1679. rl_forced_update_display ()
  1680. {
  1681. register char *temp;
  1682. if (visible_line)
  1683. {
  1684. temp = visible_line;
  1685. while (*temp)
  1686. *temp++ = '\0';
  1687. }
  1688. rl_on_new_line ();
  1689. forced_display++;
  1690. (*rl_redisplay_function) ();
  1691. return 0;
  1692. }
  1693. /* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.
  1694. (Well, when we don't have multibyte characters, _rl_last_c_pos is a
  1695. buffer index.)
  1696. DATA is the contents of the screen line of interest; i.e., where
  1697. the movement is being done. */
  1698. void
  1699. _rl_move_cursor_relative (new, data)
  1700. int new;
  1701. const char *data;
  1702. {
  1703. register int i;
  1704. int woff; /* number of invisible chars on current line */
  1705. int cpos, dpos; /* current and desired cursor positions */
  1706. int adjust;
  1707. woff = WRAP_OFFSET (_rl_last_v_pos, wrap_offset);
  1708. cpos = _rl_last_c_pos;
  1709. if (cpos == 0 && cpos == new)
  1710. return;
  1711. #if defined (HANDLE_MULTIBYTE)
  1712. /* If we have multibyte characters, NEW is indexed by the buffer point in
  1713. a multibyte string, but _rl_last_c_pos is the display position. In
  1714. this case, NEW's display position is not obvious and must be
  1715. calculated. We need to account for invisible characters in this line,
  1716. as long as we are past them and they are counted by _rl_col_width. */
  1717. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1718. {
  1719. adjust = 1;
  1720. /* Try to short-circuit common cases and eliminate a bunch of multibyte
  1721. character function calls. */
  1722. /* 1. prompt string */
  1723. if (new == local_prompt_len && memcmp (data, local_prompt, new) == 0)
  1724. {
  1725. dpos = prompt_physical_chars;
  1726. cpos_adjusted = 1;
  1727. adjust = 0;
  1728. }
  1729. /* 2. prompt_string + line contents */
  1730. else if (new > local_prompt_len && local_prompt && memcmp (data, local_prompt, local_prompt_len) == 0)
  1731. {
  1732. dpos = prompt_physical_chars + _rl_col_width (data, local_prompt_len, new, 1);
  1733. cpos_adjusted = 1;
  1734. adjust = 0;
  1735. }
  1736. else
  1737. dpos = _rl_col_width (data, 0, new, 1);
  1738. /* Use NEW when comparing against the last invisible character in the
  1739. prompt string, since they're both buffer indices and DPOS is a
  1740. desired display position. */
  1741. if (adjust && ((new > prompt_last_invisible) || /* XXX - don't use woff here */
  1742. (prompt_physical_chars >= _rl_screenwidth &&
  1743. _rl_last_v_pos == prompt_last_screen_line &&
  1744. wrap_offset >= woff && dpos >= woff &&
  1745. new > (prompt_last_invisible-(_rl_screenwidth*_rl_last_v_pos)-wrap_offset))))
  1746. /* XXX last comparison might need to be >= */
  1747. {
  1748. dpos -= woff;
  1749. /* Since this will be assigned to _rl_last_c_pos at the end (more
  1750. precisely, _rl_last_c_pos == dpos when this function returns),
  1751. let the caller know. */
  1752. cpos_adjusted = 1;
  1753. }
  1754. }
  1755. else
  1756. #endif
  1757. dpos = new;
  1758. /* If we don't have to do anything, then return. */
  1759. if (cpos == dpos)
  1760. return;
  1761. /* It may be faster to output a CR, and then move forwards instead
  1762. of moving backwards. */
  1763. /* i == current physical cursor position. */
  1764. #if defined (HANDLE_MULTIBYTE)
  1765. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1766. i = _rl_last_c_pos;
  1767. else
  1768. #endif
  1769. i = _rl_last_c_pos - woff;
  1770. if (dpos == 0 || CR_FASTER (dpos, _rl_last_c_pos) ||
  1771. (_rl_term_autowrap && i == _rl_screenwidth))
  1772. {
  1773. #if defined (__MSDOS__)
  1774. putc ('\r', rl_outstream);
  1775. #else
  1776. tputs (_rl_term_cr, 1, _rl_output_character_function);
  1777. #endif /* !__MSDOS__ */
  1778. cpos = _rl_last_c_pos = 0;
  1779. }
  1780. if (cpos < dpos)
  1781. {
  1782. /* Move the cursor forward. We do it by printing the command
  1783. to move the cursor forward if there is one, else print that
  1784. portion of the output buffer again. Which is cheaper? */
  1785. /* The above comment is left here for posterity. It is faster
  1786. to print one character (non-control) than to print a control
  1787. sequence telling the terminal to move forward one character.
  1788. That kind of control is for people who don't know what the
  1789. data is underneath the cursor. */
  1790. /* However, we need a handle on where the current display position is
  1791. in the buffer for the immediately preceding comment to be true.
  1792. In multibyte locales, we don't currently have that info available.
  1793. Without it, we don't know where the data we have to display begins
  1794. in the buffer and we have to go back to the beginning of the screen
  1795. line. In this case, we can use the terminal sequence to move forward
  1796. if it's available. */
  1797. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1798. {
  1799. if (_rl_term_forward_char)
  1800. {
  1801. for (i = cpos; i < dpos; i++)
  1802. tputs (_rl_term_forward_char, 1, _rl_output_character_function);
  1803. }
  1804. else
  1805. {
  1806. tputs (_rl_term_cr, 1, _rl_output_character_function);
  1807. for (i = 0; i < new; i++)
  1808. putc (data[i], rl_outstream);
  1809. }
  1810. }
  1811. else
  1812. for (i = cpos; i < new; i++)
  1813. putc (data[i], rl_outstream);
  1814. }
  1815. #if defined (HANDLE_MULTIBYTE)
  1816. /* NEW points to the buffer point, but _rl_last_c_pos is the display point.
  1817. The byte length of the string is probably bigger than the column width
  1818. of the string, which means that if NEW == _rl_last_c_pos, then NEW's
  1819. display point is less than _rl_last_c_pos. */
  1820. #endif
  1821. else if (cpos > dpos)
  1822. _rl_backspace (cpos - dpos);
  1823. _rl_last_c_pos = dpos;
  1824. }
  1825. /* PWP: move the cursor up or down. */
  1826. void
  1827. _rl_move_vert (to)
  1828. int to;
  1829. {
  1830. register int delta, i;
  1831. if (_rl_last_v_pos == to || to > _rl_screenheight)
  1832. return;
  1833. if ((delta = to - _rl_last_v_pos) > 0)
  1834. {
  1835. for (i = 0; i < delta; i++)
  1836. putc ('\n', rl_outstream);
  1837. #if defined (__MSDOS__)
  1838. putc ('\r', rl_outstream);
  1839. #else
  1840. tputs (_rl_term_cr, 1, _rl_output_character_function);
  1841. #endif
  1842. _rl_last_c_pos = 0;
  1843. }
  1844. else
  1845. { /* delta < 0 */
  1846. #ifdef __MSDOS__
  1847. int row, col;
  1848. fflush (rl_outstream); /* make sure the cursor pos is current! */
  1849. ScreenGetCursor (&row, &col);
  1850. ScreenSetCursor (row + delta, col);
  1851. i = -delta; /* in case someone wants to use it after the loop */
  1852. #else /* !__MSDOS__ */
  1853. if (_rl_term_up && *_rl_term_up)
  1854. for (i = 0; i < -delta; i++)
  1855. tputs (_rl_term_up, 1, _rl_output_character_function);
  1856. #endif /* !__MSDOS__ */
  1857. }
  1858. _rl_last_v_pos = to; /* Now TO is here */
  1859. }
  1860. /* Physically print C on rl_outstream. This is for functions which know
  1861. how to optimize the display. Return the number of characters output. */
  1862. int
  1863. rl_show_char (c)
  1864. int c;
  1865. {
  1866. int n = 1;
  1867. if (META_CHAR (c) && (_rl_output_meta_chars == 0))
  1868. {
  1869. fprintf (rl_outstream, "M-");
  1870. n += 2;
  1871. c = UNMETA (c);
  1872. }
  1873. #if defined (DISPLAY_TABS)
  1874. if ((CTRL_CHAR (c) && c != '\t') || c == RUBOUT)
  1875. #else
  1876. if (CTRL_CHAR (c) || c == RUBOUT)
  1877. #endif /* !DISPLAY_TABS */
  1878. {
  1879. fprintf (rl_outstream, "C-");
  1880. n += 2;
  1881. c = CTRL_CHAR (c) ? UNCTRL (c) : '?';
  1882. }
  1883. putc (c, rl_outstream);
  1884. fflush (rl_outstream);
  1885. return n;
  1886. }
  1887. int
  1888. rl_character_len (c, pos)
  1889. register int c, pos;
  1890. {
  1891. unsigned char uc;
  1892. uc = (unsigned char)c;
  1893. if (META_CHAR (uc))
  1894. return ((_rl_output_meta_chars == 0) ? 4 : 1);
  1895. if (uc == '\t')
  1896. {
  1897. #if defined (DISPLAY_TABS)
  1898. return (((pos | 7) + 1) - pos);
  1899. #else
  1900. return (2);
  1901. #endif /* !DISPLAY_TABS */
  1902. }
  1903. if (CTRL_CHAR (c) || c == RUBOUT)
  1904. return (2);
  1905. return ((ISPRINT (uc)) ? 1 : 2);
  1906. }
  1907. /* How to print things in the "echo-area". The prompt is treated as a
  1908. mini-modeline. */
  1909. static int msg_saved_prompt = 0;
  1910. #if defined (USE_VARARGS)
  1911. int
  1912. #if defined (PREFER_STDARG)
  1913. rl_message (const char *format, ...)
  1914. #else
  1915. rl_message (va_alist)
  1916. va_dcl
  1917. #endif
  1918. {
  1919. va_list args;
  1920. #if defined (PREFER_VARARGS)
  1921. char *format;
  1922. #endif
  1923. #if defined (PREFER_STDARG)
  1924. va_start (args, format);
  1925. #else
  1926. va_start (args);
  1927. format = va_arg (args, char *);
  1928. #endif
  1929. #if defined (HAVE_VSNPRINTF)
  1930. vsnprintf (msg_buf, sizeof (msg_buf) - 1, format, args);
  1931. #else
  1932. vsprintf (msg_buf, format, args);
  1933. msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
  1934. #endif
  1935. va_end (args);
  1936. if (saved_local_prompt == 0)
  1937. {
  1938. rl_save_prompt ();
  1939. msg_saved_prompt = 1;
  1940. }
  1941. rl_display_prompt = msg_buf;
  1942. local_prompt = expand_prompt (msg_buf, &prompt_visible_length,
  1943. &prompt_last_invisible,
  1944. &prompt_invis_chars_first_line,
  1945. &prompt_physical_chars);
  1946. local_prompt_prefix = (char *)NULL;
  1947. local_prompt_len = local_prompt ? strlen (local_prompt) : 0;
  1948. (*rl_redisplay_function) ();
  1949. return 0;
  1950. }
  1951. #else /* !USE_VARARGS */
  1952. int
  1953. rl_message (format, arg1, arg2)
  1954. char *format;
  1955. {
  1956. sprintf (msg_buf, format, arg1, arg2);
  1957. msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
  1958. rl_display_prompt = msg_buf;
  1959. if (saved_local_prompt == 0)
  1960. {
  1961. rl_save_prompt ();
  1962. msg_saved_prompt = 1;
  1963. }
  1964. local_prompt = expand_prompt (msg_buf, &prompt_visible_length,
  1965. &prompt_last_invisible,
  1966. &prompt_invis_chars_first_line,
  1967. &prompt_physical_chars);
  1968. local_prompt_prefix = (char *)NULL;
  1969. local_prompt_len = local_prompt ? strlen (local_prompt) : 0;
  1970. (*rl_redisplay_function) ();
  1971. return 0;
  1972. }
  1973. #endif /* !USE_VARARGS */
  1974. /* How to clear things from the "echo-area". */
  1975. int
  1976. rl_clear_message ()
  1977. {
  1978. rl_display_prompt = rl_prompt;
  1979. if (msg_saved_prompt)
  1980. {
  1981. rl_restore_prompt ();
  1982. msg_saved_prompt = 0;
  1983. }
  1984. (*rl_redisplay_function) ();
  1985. return 0;
  1986. }
  1987. int
  1988. rl_reset_line_state ()
  1989. {
  1990. rl_on_new_line ();
  1991. rl_display_prompt = rl_prompt ? rl_prompt : "";
  1992. forced_display = 1;
  1993. return 0;
  1994. }
  1995. void
  1996. rl_save_prompt ()
  1997. {
  1998. saved_local_prompt = local_prompt;
  1999. saved_local_prefix = local_prompt_prefix;
  2000. saved_prefix_length = prompt_prefix_length;
  2001. saved_local_length = local_prompt_len;
  2002. saved_last_invisible = prompt_last_invisible;
  2003. saved_visible_length = prompt_visible_length;
  2004. saved_invis_chars_first_line = prompt_invis_chars_first_line;
  2005. saved_physical_chars = prompt_physical_chars;
  2006. local_prompt = local_prompt_prefix = (char *)0;
  2007. local_prompt_len = 0;
  2008. prompt_last_invisible = prompt_visible_length = prompt_prefix_length = 0;
  2009. prompt_invis_chars_first_line = prompt_physical_chars = 0;
  2010. }
  2011. void
  2012. rl_restore_prompt ()
  2013. {
  2014. FREE (local_prompt);
  2015. FREE (local_prompt_prefix);
  2016. local_prompt = saved_local_prompt;
  2017. local_prompt_prefix = saved_local_prefix;
  2018. local_prompt_len = saved_local_length;
  2019. prompt_prefix_length = saved_prefix_length;
  2020. prompt_last_invisible = saved_last_invisible;
  2021. prompt_visible_length = saved_visible_length;
  2022. prompt_invis_chars_first_line = saved_invis_chars_first_line;
  2023. prompt_physical_chars = saved_physical_chars;
  2024. /* can test saved_local_prompt to see if prompt info has been saved. */
  2025. saved_local_prompt = saved_local_prefix = (char *)0;
  2026. saved_local_length = 0;
  2027. saved_last_invisible = saved_visible_length = saved_prefix_length = 0;
  2028. saved_invis_chars_first_line = saved_physical_chars = 0;
  2029. }
  2030. char *
  2031. _rl_make_prompt_for_search (pchar)
  2032. int pchar;
  2033. {
  2034. int len;
  2035. char *pmt, *p;
  2036. rl_save_prompt ();
  2037. /* We've saved the prompt, and can do anything with the various prompt
  2038. strings we need before they're restored. We want the unexpanded
  2039. portion of the prompt string after any final newline. */
  2040. p = rl_prompt ? strrchr (rl_prompt, '\n') : 0;
  2041. if (p == 0)
  2042. {
  2043. len = (rl_prompt && *rl_prompt) ? strlen (rl_prompt) : 0;
  2044. pmt = (char *)xmalloc (len + 2);
  2045. if (len)
  2046. strcpy (pmt, rl_prompt);
  2047. pmt[len] = pchar;
  2048. pmt[len+1] = '\0';
  2049. }
  2050. else
  2051. {
  2052. p++;
  2053. len = strlen (p);
  2054. pmt = (char *)xmalloc (len + 2);
  2055. if (len)
  2056. strcpy (pmt, p);
  2057. pmt[len] = pchar;
  2058. pmt[len+1] = '\0';
  2059. }
  2060. /* will be overwritten by expand_prompt, called from rl_message */
  2061. prompt_physical_chars = saved_physical_chars + 1;
  2062. return pmt;
  2063. }
  2064. /* Quick redisplay hack when erasing characters at the end of the line. */
  2065. void
  2066. _rl_erase_at_end_of_line (l)
  2067. int l;
  2068. {
  2069. register int i;
  2070. _rl_backspace (l);
  2071. for (i = 0; i < l; i++)
  2072. putc (' ', rl_outstream);
  2073. _rl_backspace (l);
  2074. for (i = 0; i < l; i++)
  2075. visible_line[--_rl_last_c_pos] = '\0';
  2076. rl_display_fixed++;
  2077. }
  2078. /* Clear to the end of the line. COUNT is the minimum
  2079. number of character spaces to clear, */
  2080. void
  2081. _rl_clear_to_eol (count)
  2082. int count;
  2083. {
  2084. #ifndef __MSDOS__
  2085. if (_rl_term_clreol)
  2086. tputs (_rl_term_clreol, 1, _rl_output_character_function);
  2087. else
  2088. #endif
  2089. if (count)
  2090. space_to_eol (count);
  2091. }
  2092. /* Clear to the end of the line using spaces. COUNT is the minimum
  2093. number of character spaces to clear, */
  2094. static void
  2095. space_to_eol (count)
  2096. int count;
  2097. {
  2098. register int i;
  2099. for (i = 0; i < count; i++)
  2100. putc (' ', rl_outstream);
  2101. _rl_last_c_pos += count;
  2102. }
  2103. void
  2104. _rl_clear_screen ()
  2105. {
  2106. #if defined (__GO32__)
  2107. ScreenClear (); /* FIXME: only works in text modes */
  2108. ScreenSetCursor (0, 0); /* term_clrpag is "cl" which homes the cursor */
  2109. #else
  2110. if (_rl_term_clrpag)
  2111. tputs (_rl_term_clrpag, 1, _rl_output_character_function);
  2112. else
  2113. rl_crlf ();
  2114. #endif
  2115. }
  2116. /* Insert COUNT characters from STRING to the output stream at column COL. */
  2117. static void
  2118. insert_some_chars (string, count, col)
  2119. char *string;
  2120. int count, col;
  2121. {
  2122. #if defined (__MSDOS__) || defined (__MINGW32__)
  2123. _rl_output_some_chars (string, count);
  2124. #else
  2125. /* DEBUGGING */
  2126. if (MB_CUR_MAX == 1 || rl_byte_oriented)
  2127. if (count != col)
  2128. _rl_ttymsg ("debug: insert_some_chars: count (%d) != col (%d)", count, col);
  2129. /* If IC is defined, then we do not have to "enter" insert mode. */
  2130. if (_rl_term_IC)
  2131. {
  2132. char *buffer;
  2133. buffer = tgoto (_rl_term_IC, 0, col);
  2134. tputs (buffer, 1, _rl_output_character_function);
  2135. _rl_output_some_chars (string, count);
  2136. }
  2137. else
  2138. {
  2139. register int i;
  2140. /* If we have to turn on insert-mode, then do so. */
  2141. if (_rl_term_im && *_rl_term_im)
  2142. tputs (_rl_term_im, 1, _rl_output_character_function);
  2143. /* If there is a special command for inserting characters, then
  2144. use that first to open up the space. */
  2145. if (_rl_term_ic && *_rl_term_ic)
  2146. {
  2147. for (i = col; i--; )
  2148. tputs (_rl_term_ic, 1, _rl_output_character_function);
  2149. }
  2150. /* Print the text. */
  2151. _rl_output_some_chars (string, count);
  2152. /* If there is a string to turn off insert mode, we had best use
  2153. it now. */
  2154. if (_rl_term_ei && *_rl_term_ei)
  2155. tputs (_rl_term_ei, 1, _rl_output_character_function);
  2156. }
  2157. #endif /* __MSDOS__ || __MINGW32__ */
  2158. }
  2159. /* Delete COUNT characters from the display line. */
  2160. static void
  2161. delete_chars (count)
  2162. int count;
  2163. {
  2164. if (count > _rl_screenwidth) /* XXX */
  2165. return;
  2166. #if !defined (__MSDOS__) && !defined (__MINGW32__)
  2167. if (_rl_term_DC && *_rl_term_DC)
  2168. {
  2169. char *buffer;
  2170. buffer = tgoto (_rl_term_DC, count, count);
  2171. tputs (buffer, count, _rl_output_character_function);
  2172. }
  2173. else
  2174. {
  2175. if (_rl_term_dc && *_rl_term_dc)
  2176. while (count--)
  2177. tputs (_rl_term_dc, 1, _rl_output_character_function);
  2178. }
  2179. #endif /* !__MSDOS__ && !__MINGW32__ */
  2180. }
  2181. void
  2182. _rl_update_final ()
  2183. {
  2184. int full_lines;
  2185. full_lines = 0;
  2186. /* If the cursor is the only thing on an otherwise-blank last line,
  2187. compensate so we don't print an extra CRLF. */
  2188. if (_rl_vis_botlin && _rl_last_c_pos == 0 &&
  2189. visible_line[vis_lbreaks[_rl_vis_botlin]] == 0)
  2190. {
  2191. _rl_vis_botlin--;
  2192. full_lines = 1;
  2193. }
  2194. _rl_move_vert (_rl_vis_botlin);
  2195. /* If we've wrapped lines, remove the final xterm line-wrap flag. */
  2196. if (full_lines && _rl_term_autowrap && (VIS_LLEN(_rl_vis_botlin) == _rl_screenwidth))
  2197. {
  2198. char *last_line;
  2199. last_line = &visible_line[vis_lbreaks[_rl_vis_botlin]];
  2200. cpos_buffer_position = -1; /* don't know where we are in buffer */
  2201. _rl_move_cursor_relative (_rl_screenwidth - 1, last_line); /* XXX */
  2202. _rl_clear_to_eol (0);
  2203. putc (last_line[_rl_screenwidth - 1], rl_outstream);
  2204. }
  2205. _rl_vis_botlin = 0;
  2206. rl_crlf ();
  2207. fflush (rl_outstream);
  2208. rl_display_fixed++;
  2209. }
  2210. /* Move to the start of the current line. */
  2211. static void
  2212. cr ()
  2213. {
  2214. if (_rl_term_cr)
  2215. {
  2216. #if defined (__MSDOS__)
  2217. putc ('\r', rl_outstream);
  2218. #else
  2219. tputs (_rl_term_cr, 1, _rl_output_character_function);
  2220. #endif
  2221. _rl_last_c_pos = 0;
  2222. }
  2223. }
  2224. /* Redraw the last line of a multi-line prompt that may possibly contain
  2225. terminal escape sequences. Called with the cursor at column 0 of the
  2226. line to draw the prompt on. */
  2227. static void
  2228. redraw_prompt (t)
  2229. char *t;
  2230. {
  2231. char *oldp;
  2232. oldp = rl_display_prompt;
  2233. rl_save_prompt ();
  2234. rl_display_prompt = t;
  2235. local_prompt = expand_prompt (t, &prompt_visible_length,
  2236. &prompt_last_invisible,
  2237. &prompt_invis_chars_first_line,
  2238. &prompt_physical_chars);
  2239. local_prompt_prefix = (char *)NULL;
  2240. local_prompt_len = local_prompt ? strlen (local_prompt) : 0;
  2241. rl_forced_update_display ();
  2242. rl_display_prompt = oldp;
  2243. rl_restore_prompt();
  2244. }
  2245. /* Redisplay the current line after a SIGWINCH is received. */
  2246. void
  2247. _rl_redisplay_after_sigwinch ()
  2248. {
  2249. char *t;
  2250. /* Clear the last line (assuming that the screen size change will result in
  2251. either more or fewer characters on that line only) and put the cursor at
  2252. column 0. Make sure the right thing happens if we have wrapped to a new
  2253. screen line. */
  2254. if (_rl_term_cr)
  2255. {
  2256. _rl_move_vert (_rl_vis_botlin);
  2257. #if defined (__MSDOS__)
  2258. putc ('\r', rl_outstream);
  2259. #else
  2260. tputs (_rl_term_cr, 1, _rl_output_character_function);
  2261. #endif
  2262. _rl_last_c_pos = 0;
  2263. #if defined (__MSDOS__)
  2264. space_to_eol (_rl_screenwidth);
  2265. putc ('\r', rl_outstream);
  2266. #else
  2267. if (_rl_term_clreol)
  2268. tputs (_rl_term_clreol, 1, _rl_output_character_function);
  2269. else
  2270. {
  2271. space_to_eol (_rl_screenwidth);
  2272. tputs (_rl_term_cr, 1, _rl_output_character_function);
  2273. }
  2274. #endif
  2275. if (_rl_last_v_pos > 0)
  2276. _rl_move_vert (0);
  2277. }
  2278. else
  2279. rl_crlf ();
  2280. /* Redraw only the last line of a multi-line prompt. */
  2281. t = strrchr (rl_display_prompt, '\n');
  2282. if (t)
  2283. redraw_prompt (++t);
  2284. else
  2285. rl_forced_update_display ();
  2286. }
  2287. void
  2288. _rl_clean_up_for_exit ()
  2289. {
  2290. if (_rl_echoing_p)
  2291. {
  2292. _rl_move_vert (_rl_vis_botlin);
  2293. _rl_vis_botlin = 0;
  2294. fflush (rl_outstream);
  2295. rl_restart_output (1, 0);
  2296. }
  2297. }
  2298. void
  2299. _rl_erase_entire_line ()
  2300. {
  2301. cr ();
  2302. _rl_clear_to_eol (0);
  2303. cr ();
  2304. fflush (rl_outstream);
  2305. }
  2306. /* return the `current display line' of the cursor -- the number of lines to
  2307. move up to get to the first screen line of the current readline line. */
  2308. int
  2309. _rl_current_display_line ()
  2310. {
  2311. int ret, nleft;
  2312. /* Find out whether or not there might be invisible characters in the
  2313. editing buffer. */
  2314. if (rl_display_prompt == rl_prompt)
  2315. nleft = _rl_last_c_pos - _rl_screenwidth - rl_visible_prompt_length;
  2316. else
  2317. nleft = _rl_last_c_pos - _rl_screenwidth;
  2318. if (nleft > 0)
  2319. ret = 1 + nleft / _rl_screenwidth;
  2320. else
  2321. ret = 0;
  2322. return ret;
  2323. }
  2324. #if defined (HANDLE_MULTIBYTE)
  2325. /* Calculate the number of screen columns occupied by STR from START to END.
  2326. In the case of multibyte characters with stateful encoding, we have to
  2327. scan from the beginning of the string to take the state into account. */
  2328. static int
  2329. _rl_col_width (str, start, end, flags)
  2330. const char *str;
  2331. int start, end, flags;
  2332. {
  2333. wchar_t wc;
  2334. mbstate_t ps;
  2335. int tmp, point, width, max;
  2336. if (end <= start)
  2337. return 0;
  2338. if (MB_CUR_MAX == 1 || rl_byte_oriented)
  2339. {
  2340. _rl_ttymsg ("_rl_col_width: called with MB_CUR_MAX == 1");
  2341. return (end - start);
  2342. }
  2343. memset (&ps, 0, sizeof (mbstate_t));
  2344. point = 0;
  2345. max = end;
  2346. /* Try to short-circuit common cases. The adjustment to remove wrap_offset
  2347. is done by the caller. */
  2348. /* 1. prompt string */
  2349. if (flags && start == 0 && end == local_prompt_len && memcmp (str, local_prompt, local_prompt_len) == 0)
  2350. return (prompt_physical_chars + wrap_offset);
  2351. /* 2. prompt string + line contents */
  2352. else if (flags && start == 0 && local_prompt_len > 0 && end > local_prompt_len && local_prompt && memcmp (str, local_prompt, local_prompt_len) == 0)
  2353. {
  2354. tmp = prompt_physical_chars + wrap_offset;
  2355. /* XXX - try to call ourselves recursively with non-prompt portion */
  2356. tmp += _rl_col_width (str, local_prompt_len, end, flags);
  2357. return (tmp);
  2358. }
  2359. while (point < start)
  2360. {
  2361. tmp = mbrlen (str + point, max, &ps);
  2362. if (MB_INVALIDCH ((size_t)tmp))
  2363. {
  2364. /* In this case, the bytes are invalid or too short to compose a
  2365. multibyte character, so we assume that the first byte represents
  2366. a single character. */
  2367. point++;
  2368. max--;
  2369. /* Clear the state of the byte sequence, because in this case the
  2370. effect of mbstate is undefined. */
  2371. memset (&ps, 0, sizeof (mbstate_t));
  2372. }
  2373. else if (MB_NULLWCH (tmp))
  2374. break; /* Found '\0' */
  2375. else
  2376. {
  2377. point += tmp;
  2378. max -= tmp;
  2379. }
  2380. }
  2381. /* If START is not a byte that starts a character, then POINT will be
  2382. greater than START. In this case, assume that (POINT - START) gives
  2383. a byte count that is the number of columns of difference. */
  2384. width = point - start;
  2385. while (point < end)
  2386. {
  2387. tmp = mbrtowc (&wc, str + point, max, &ps);
  2388. if (MB_INVALIDCH ((size_t)tmp))
  2389. {
  2390. /* In this case, the bytes are invalid or too short to compose a
  2391. multibyte character, so we assume that the first byte represents
  2392. a single character. */
  2393. point++;
  2394. max--;
  2395. /* and assume that the byte occupies a single column. */
  2396. width++;
  2397. /* Clear the state of the byte sequence, because in this case the
  2398. effect of mbstate is undefined. */
  2399. memset (&ps, 0, sizeof (mbstate_t));
  2400. }
  2401. else if (MB_NULLWCH (tmp))
  2402. break; /* Found '\0' */
  2403. else
  2404. {
  2405. point += tmp;
  2406. max -= tmp;
  2407. tmp = wcwidth(wc);
  2408. width += (tmp >= 0) ? tmp : 1;
  2409. }
  2410. }
  2411. width += point - end;
  2412. return width;
  2413. }
  2414. #endif /* HANDLE_MULTIBYTE */