replxx.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /* linenoise.h -- guerrilla line editing library against the idea that a
  2. * line editing lib needs to be 20,000 lines of C code.
  3. *
  4. * See linenoise.c for more information.
  5. *
  6. * Copyright (c) 2010, Salvatore Sanfilippo <antirez at gmail dot com>
  7. * Copyright (c) 2010, Pieter Noordhuis <pcnoordhuis at gmail dot com>
  8. *
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of Redis nor the names of its contributors may be used
  20. * to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #ifndef __REPLXX_H
  36. #define __REPLXX_H
  37. #define REPLXX_VERSION "0.0.2"
  38. #define REPLXX_VERSION_MAJOR 0
  39. #define REPLXX_VERSION_MINOR 0
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /*
  44. * For use in Windows DLLs:
  45. *
  46. * If you are building replxx into a DLL,
  47. * unless you are using supplied CMake based build,
  48. * ensure that 'REPLXX_BUILDING_DLL' is defined when
  49. * building the DLL so that proper symbols are exported.
  50. */
  51. #if defined( _WIN32 ) && ! defined( REPLXX_STATIC )
  52. # ifdef REPLXX_BUILDING_DLL
  53. # define REPLXX_IMPEXP __declspec( dllexport )
  54. # else
  55. # define REPLXX_IMPEXP __declspec( dllimport )
  56. # endif
  57. #else
  58. # define REPLXX_IMPEXP /**/
  59. #endif
  60. /*! \brief Color definitions to use in highlighter callbacks.
  61. */
  62. typedef enum {
  63. REPLXX_COLOR_BLACK = 0,
  64. REPLXX_COLOR_RED = 1,
  65. REPLXX_COLOR_GREEN = 2,
  66. REPLXX_COLOR_BROWN = 3,
  67. REPLXX_COLOR_BLUE = 4,
  68. REPLXX_COLOR_MAGENTA = 5,
  69. REPLXX_COLOR_CYAN = 6,
  70. REPLXX_COLOR_LIGHTGRAY = 7,
  71. REPLXX_COLOR_GRAY = 8,
  72. REPLXX_COLOR_BRIGHTRED = 9,
  73. REPLXX_COLOR_BRIGHTGREEN = 10,
  74. REPLXX_COLOR_YELLOW = 11,
  75. REPLXX_COLOR_BRIGHTBLUE = 12,
  76. REPLXX_COLOR_BRIGHTMAGENTA = 13,
  77. REPLXX_COLOR_BRIGHTCYAN = 14,
  78. REPLXX_COLOR_WHITE = 15,
  79. REPLXX_COLOR_DEFAULT = 1u << 16u
  80. } ReplxxColor;
  81. enum { REPLXX_KEY_BASE = 0x0010ffff + 1 };
  82. enum { REPLXX_KEY_BASE_SHIFT = 0x01000000 };
  83. enum { REPLXX_KEY_BASE_CONTROL = 0x02000000 };
  84. enum { REPLXX_KEY_BASE_META = 0x04000000 };
  85. enum { REPLXX_KEY_ESCAPE = 27 };
  86. enum { REPLXX_KEY_PAGE_UP = REPLXX_KEY_BASE + 1 };
  87. enum { REPLXX_KEY_PAGE_DOWN = REPLXX_KEY_PAGE_UP + 1 };
  88. enum { REPLXX_KEY_DOWN = REPLXX_KEY_PAGE_DOWN + 1 };
  89. enum { REPLXX_KEY_UP = REPLXX_KEY_DOWN + 1 };
  90. enum { REPLXX_KEY_LEFT = REPLXX_KEY_UP + 1 };
  91. enum { REPLXX_KEY_RIGHT = REPLXX_KEY_LEFT + 1 };
  92. enum { REPLXX_KEY_HOME = REPLXX_KEY_RIGHT + 1 };
  93. enum { REPLXX_KEY_END = REPLXX_KEY_HOME + 1 };
  94. enum { REPLXX_KEY_DELETE = REPLXX_KEY_END + 1 };
  95. enum { REPLXX_KEY_INSERT = REPLXX_KEY_DELETE + 1 };
  96. enum { REPLXX_KEY_F1 = REPLXX_KEY_INSERT + 1 };
  97. enum { REPLXX_KEY_F2 = REPLXX_KEY_F1 + 1 };
  98. enum { REPLXX_KEY_F3 = REPLXX_KEY_F2 + 1 };
  99. enum { REPLXX_KEY_F4 = REPLXX_KEY_F3 + 1 };
  100. enum { REPLXX_KEY_F5 = REPLXX_KEY_F4 + 1 };
  101. enum { REPLXX_KEY_F6 = REPLXX_KEY_F5 + 1 };
  102. enum { REPLXX_KEY_F7 = REPLXX_KEY_F6 + 1 };
  103. enum { REPLXX_KEY_F8 = REPLXX_KEY_F7 + 1 };
  104. enum { REPLXX_KEY_F9 = REPLXX_KEY_F8 + 1 };
  105. enum { REPLXX_KEY_F10 = REPLXX_KEY_F9 + 1 };
  106. enum { REPLXX_KEY_F11 = REPLXX_KEY_F10 + 1 };
  107. enum { REPLXX_KEY_F12 = REPLXX_KEY_F11 + 1 };
  108. enum { REPLXX_KEY_F13 = REPLXX_KEY_F12 + 1 };
  109. enum { REPLXX_KEY_F14 = REPLXX_KEY_F13 + 1 };
  110. enum { REPLXX_KEY_F15 = REPLXX_KEY_F14 + 1 };
  111. enum { REPLXX_KEY_F16 = REPLXX_KEY_F15 + 1 };
  112. enum { REPLXX_KEY_F17 = REPLXX_KEY_F16 + 1 };
  113. enum { REPLXX_KEY_F18 = REPLXX_KEY_F17 + 1 };
  114. enum { REPLXX_KEY_F19 = REPLXX_KEY_F18 + 1 };
  115. enum { REPLXX_KEY_F20 = REPLXX_KEY_F19 + 1 };
  116. enum { REPLXX_KEY_F21 = REPLXX_KEY_F20 + 1 };
  117. enum { REPLXX_KEY_F22 = REPLXX_KEY_F21 + 1 };
  118. enum { REPLXX_KEY_F23 = REPLXX_KEY_F22 + 1 };
  119. enum { REPLXX_KEY_F24 = REPLXX_KEY_F23 + 1 };
  120. enum { REPLXX_KEY_MOUSE = REPLXX_KEY_F24 + 1 };
  121. enum { REPLXX_KEY_PASTE_START = REPLXX_KEY_MOUSE + 1 };
  122. enum { REPLXX_KEY_PASTE_FINISH = REPLXX_KEY_PASTE_START + 1 };
  123. #define REPLXX_KEY_SHIFT( key ) ( ( key ) | REPLXX_KEY_BASE_SHIFT )
  124. #define REPLXX_KEY_CONTROL( key ) ( ( key ) | REPLXX_KEY_BASE_CONTROL )
  125. #define REPLXX_KEY_META( key ) ( ( key ) | REPLXX_KEY_BASE_META )
  126. enum { REPLXX_KEY_BACKSPACE = REPLXX_KEY_CONTROL( 'H' ) };
  127. enum { REPLXX_KEY_TAB = REPLXX_KEY_CONTROL( 'I' ) };
  128. enum { REPLXX_KEY_ENTER = REPLXX_KEY_CONTROL( 'M' ) };
  129. enum { REPLXX_KEY_ABORT = REPLXX_KEY_META( REPLXX_KEY_CONTROL( 'M' ) ) };
  130. /*! \brief List of built-in actions that act upon user input.
  131. */
  132. typedef enum {
  133. REPLXX_ACTION_INSERT_CHARACTER,
  134. REPLXX_ACTION_NEW_LINE,
  135. REPLXX_ACTION_DELETE_CHARACTER_UNDER_CURSOR,
  136. REPLXX_ACTION_DELETE_CHARACTER_LEFT_OF_CURSOR,
  137. REPLXX_ACTION_KILL_TO_END_OF_LINE,
  138. REPLXX_ACTION_KILL_TO_BEGINING_OF_LINE,
  139. REPLXX_ACTION_KILL_TO_END_OF_WORD,
  140. REPLXX_ACTION_KILL_TO_BEGINING_OF_WORD,
  141. REPLXX_ACTION_KILL_TO_END_OF_SUBWORD,
  142. REPLXX_ACTION_KILL_TO_BEGINING_OF_SUBWORD,
  143. REPLXX_ACTION_KILL_TO_WHITESPACE_ON_LEFT,
  144. REPLXX_ACTION_YANK,
  145. REPLXX_ACTION_YANK_CYCLE,
  146. REPLXX_ACTION_YANK_LAST_ARG,
  147. REPLXX_ACTION_MOVE_CURSOR_TO_BEGINING_OF_LINE,
  148. REPLXX_ACTION_MOVE_CURSOR_TO_END_OF_LINE,
  149. REPLXX_ACTION_MOVE_CURSOR_ONE_WORD_LEFT,
  150. REPLXX_ACTION_MOVE_CURSOR_ONE_WORD_RIGHT,
  151. REPLXX_ACTION_MOVE_CURSOR_ONE_SUBWORD_LEFT,
  152. REPLXX_ACTION_MOVE_CURSOR_ONE_SUBWORD_RIGHT,
  153. REPLXX_ACTION_MOVE_CURSOR_LEFT,
  154. REPLXX_ACTION_MOVE_CURSOR_RIGHT,
  155. REPLXX_ACTION_LINE_NEXT,
  156. REPLXX_ACTION_LINE_PREVIOUS,
  157. REPLXX_ACTION_HISTORY_MOVE_NEXT,
  158. REPLXX_ACTION_HISTORY_MOVE_PREVIOUS,
  159. REPLXX_ACTION_HISTORY_FIRST,
  160. REPLXX_ACTION_HISTORY_LAST,
  161. REPLXX_ACTION_HISTORY_RESTORE,
  162. REPLXX_ACTION_HISTORY_RESTORE_CURRENT,
  163. REPLXX_ACTION_HISTORY_INCREMENTAL_SEARCH,
  164. REPLXX_ACTION_HISTORY_COMMON_PREFIX_SEARCH,
  165. REPLXX_ACTION_HINT_NEXT,
  166. REPLXX_ACTION_HINT_PREVIOUS,
  167. REPLXX_ACTION_CAPITALIZE_WORD,
  168. REPLXX_ACTION_LOWERCASE_WORD,
  169. REPLXX_ACTION_UPPERCASE_WORD,
  170. REPLXX_ACTION_CAPITALIZE_SUBWORD,
  171. REPLXX_ACTION_LOWERCASE_SUBWORD,
  172. REPLXX_ACTION_UPPERCASE_SUBWORD,
  173. REPLXX_ACTION_TRANSPOSE_CHARACTERS,
  174. REPLXX_ACTION_TOGGLE_OVERWRITE_MODE,
  175. #ifndef _WIN32
  176. REPLXX_ACTION_VERBATIM_INSERT,
  177. REPLXX_ACTION_SUSPEND,
  178. #endif
  179. REPLXX_ACTION_BRACKETED_PASTE,
  180. REPLXX_ACTION_CLEAR_SCREEN,
  181. REPLXX_ACTION_CLEAR_SELF,
  182. REPLXX_ACTION_REPAINT,
  183. REPLXX_ACTION_COMPLETE_LINE,
  184. REPLXX_ACTION_COMPLETE_NEXT,
  185. REPLXX_ACTION_COMPLETE_PREVIOUS,
  186. REPLXX_ACTION_COMMIT_LINE,
  187. REPLXX_ACTION_ABORT_LINE,
  188. REPLXX_ACTION_SEND_EOF
  189. } ReplxxAction;
  190. /*! \brief Possible results of key-press handler actions.
  191. */
  192. typedef enum {
  193. REPLXX_ACTION_RESULT_CONTINUE, /*!< Continue processing user input. */
  194. REPLXX_ACTION_RESULT_RETURN, /*!< Return user input entered so far. */
  195. REPLXX_ACTION_RESULT_BAIL /*!< Stop processing user input, returns nullptr from the \e input() call. */
  196. } ReplxxActionResult;
  197. typedef struct ReplxxStateTag {
  198. char const* text;
  199. int cursorPosition;
  200. } ReplxxState;
  201. typedef struct Replxx Replxx;
  202. typedef struct ReplxxHistoryScan ReplxxHistoryScan;
  203. typedef struct ReplxxHistoryEntryTag {
  204. char const* timestamp;
  205. char const* text;
  206. } ReplxxHistoryEntry;
  207. /*! \brief Create Replxx library resource holder.
  208. *
  209. * Use replxx_end() to free resources acquired with this function.
  210. *
  211. * \return Replxx library resource holder.
  212. */
  213. REPLXX_IMPEXP Replxx* replxx_init( void );
  214. /*! \brief Cleanup resources used by Replxx library.
  215. *
  216. * \param replxx - a Replxx library resource holder.
  217. */
  218. REPLXX_IMPEXP void replxx_end( Replxx* replxx );
  219. /*! \brief Line modification callback type definition.
  220. *
  221. * User can observe and modify line contents (and cursor position)
  222. * in response to changes to both introduced by the user through
  223. * normal interactions.
  224. *
  225. * When callback returns Replxx updates current line content
  226. * and current cursor position to the ones updated by the callback.
  227. *
  228. * \param line[in,out] - a R/W reference to an UTF-8 encoded input entered by the user so far.
  229. * \param cursorPosition[in,out] - a R/W reference to current cursor position.
  230. * \param userData - pointer to opaque user data block.
  231. */
  232. typedef void (replxx_modify_callback_t)(char** input, int* contextLen, void* userData);
  233. /*! \brief Register modify callback.
  234. *
  235. * \param fn - user defined callback function.
  236. * \param userData - pointer to opaque user data block to be passed into each invocation of the callback.
  237. */
  238. REPLXX_IMPEXP void replxx_set_modify_callback( Replxx*, replxx_modify_callback_t* fn, void* userData );
  239. /*! \brief Highlighter callback type definition.
  240. *
  241. * If user want to have colorful input she must simply install highlighter callback.
  242. * The callback would be invoked by the library after each change to the input done by
  243. * the user. After callback returns library uses data from colors buffer to colorize
  244. * displayed user input.
  245. *
  246. * \e size of \e colors buffer is equal to number of code points in user \e input
  247. * which will be different from simple `strlen( input )`!
  248. *
  249. * \param input - an UTF-8 encoded input entered by the user so far.
  250. * \param colors - output buffer for color information.
  251. * \param size - size of output buffer for color information.
  252. * \param userData - pointer to opaque user data block.
  253. */
  254. typedef void (replxx_highlighter_callback_t)(char const* input, ReplxxColor* colors, int size, void* userData);
  255. /*! \brief Register highlighter callback.
  256. *
  257. * \param fn - user defined callback function.
  258. * \param userData - pointer to opaque user data block to be passed into each invocation of the callback.
  259. */
  260. REPLXX_IMPEXP void replxx_set_highlighter_callback( Replxx*, replxx_highlighter_callback_t* fn, void* userData );
  261. typedef struct replxx_completions replxx_completions;
  262. /*! \brief Completions callback type definition.
  263. *
  264. * \e contextLen is counted in Unicode code points (not in bytes!).
  265. *
  266. * For user input:
  267. * if ( obj.me
  268. *
  269. * input == "if ( obj.me"
  270. * contextLen == 2 (depending on \e replxx_set_word_break_characters())
  271. *
  272. * Client application is free to update \e contextLen to be 6 (or any other non-negative
  273. * number not greater than the number of code points in input) if it makes better sense
  274. * for given client application semantics.
  275. *
  276. * \param input - UTF-8 encoded input entered by the user until current cursor position.
  277. * \param completions - pointer to opaque list of user completions.
  278. * \param contextLen[in,out] - length of the additional context to provide while displaying completions.
  279. * \param userData - pointer to opaque user data block.
  280. */
  281. typedef void(replxx_completion_callback_t)(const char* input, replxx_completions* completions, int* contextLen, void* userData);
  282. /*! \brief Register completion callback.
  283. *
  284. * \param fn - user defined callback function.
  285. * \param userData - pointer to opaque user data block to be passed into each invocation of the callback.
  286. */
  287. REPLXX_IMPEXP void replxx_set_completion_callback( Replxx*, replxx_completion_callback_t* fn, void* userData );
  288. /*! \brief Add another possible completion for current user input.
  289. *
  290. * \param completions - pointer to opaque list of user completions.
  291. * \param str - UTF-8 encoded completion string.
  292. */
  293. REPLXX_IMPEXP void replxx_add_completion( replxx_completions* completions, const char* str );
  294. /*! \brief Add another possible completion for current user input.
  295. *
  296. * \param completions - pointer to opaque list of user completions.
  297. * \param str - UTF-8 encoded completion string.
  298. * \param color - a color for the completion.
  299. */
  300. REPLXX_IMPEXP void replxx_add_color_completion( replxx_completions* completions, const char* str, ReplxxColor color );
  301. typedef struct replxx_hints replxx_hints;
  302. /*! \brief Hints callback type definition.
  303. *
  304. * \e contextLen is counted in Unicode code points (not in bytes!).
  305. *
  306. * For user input:
  307. * if ( obj.me
  308. *
  309. * input == "if ( obj.me"
  310. * contextLen == 2 (depending on \e replxx_set_word_break_characters())
  311. *
  312. * Client application is free to update \e contextLen to be 6 (or any other non-negative
  313. * number not greater than the number of code points in input) if it makes better sense
  314. * for given client application semantics.
  315. *
  316. * \param input - UTF-8 encoded input entered by the user until current cursor position.
  317. * \param hints - pointer to opaque list of possible hints.
  318. * \param contextLen[in,out] - length of the additional context to provide while displaying hints.
  319. * \param color - a color used for displaying hints.
  320. * \param userData - pointer to opaque user data block.
  321. */
  322. typedef void(replxx_hint_callback_t)(const char* input, replxx_hints* hints, int* contextLen, ReplxxColor* color, void* userData);
  323. /*! \brief Register hints callback.
  324. *
  325. * \param fn - user defined callback function.
  326. * \param userData - pointer to opaque user data block to be passed into each invocation of the callback.
  327. */
  328. REPLXX_IMPEXP void replxx_set_hint_callback( Replxx*, replxx_hint_callback_t* fn, void* userData );
  329. /*! \brief Key press handler type definition.
  330. *
  331. * \param code - the key code replxx got from terminal.
  332. * \return Decision on how should input() behave after this key handler returns.
  333. */
  334. typedef ReplxxActionResult (key_press_handler_t)( int code, void* userData );
  335. /*! \brief Add another possible hint for current user input.
  336. *
  337. * \param hints - pointer to opaque list of hints.
  338. * \param str - UTF-8 encoded hint string.
  339. */
  340. REPLXX_IMPEXP void replxx_add_hint( replxx_hints* hints, const char* str );
  341. /*! \brief Read line of user input.
  342. *
  343. * Returned pointer is managed by the library and is not to be freed in the client.
  344. *
  345. * \param prompt - prompt to be displayed before getting user input.
  346. * \return An UTF-8 encoded input given by the user (or nullptr on EOF).
  347. */
  348. REPLXX_IMPEXP char const* replxx_input( Replxx*, const char* prompt );
  349. /*! \brief Get current state data.
  350. *
  351. * This call is intended to be used in handlers.
  352. *
  353. * \param state - buffer for current state of the model.
  354. */
  355. REPLXX_IMPEXP void replxx_get_state( Replxx*, ReplxxState* state );
  356. /*! \brief Set new state data.
  357. *
  358. * This call is intended to be used in handlers.
  359. *
  360. * \param state - new state of the model.
  361. */
  362. REPLXX_IMPEXP void replxx_set_state( Replxx*, ReplxxState* state );
  363. /*! \brief Enable/disable case insensitive history search and completion.
  364. *
  365. * \param val - if set to non-zero then history search and completion will be case insensitive.
  366. */
  367. REPLXX_IMPEXP void replxx_set_ignore_case( Replxx*, int val );
  368. /*! \brief Print formatted string to standard output.
  369. *
  370. * This function ensures proper handling of ANSI escape sequences
  371. * contained in printed data, which is especially useful on Windows
  372. * since Unixes handle them correctly out of the box.
  373. *
  374. * \param fmt - printf style format.
  375. */
  376. REPLXX_IMPEXP int replxx_print( Replxx*, char const* fmt, ... );
  377. /*! \brief Prints a char array with the given length to standard output.
  378. *
  379. * \copydetails print
  380. *
  381. * \param str - The char array to print.
  382. * \param length - The length of the array.
  383. */
  384. REPLXX_IMPEXP int replxx_write( Replxx*, char const* str, int length );
  385. /*! \brief Asynchronously change the prompt while replxx_input() call is in efect.
  386. *
  387. * Can be used to change the prompt from callbacks or other threads.
  388. *
  389. * \param prompt - The prompt string to change to.
  390. */
  391. REPLXX_IMPEXP void replxx_set_prompt( Replxx*, const char* prompt );
  392. /*! \brief Schedule an emulated key press event.
  393. *
  394. * \param code - key press code to be emulated.
  395. */
  396. REPLXX_IMPEXP void replxx_emulate_key_press( Replxx*, int unsigned code );
  397. /*! \brief Invoke built-in action handler.
  398. *
  399. * \pre This function can be called only from key-press handler.
  400. *
  401. * \param action - a built-in action to invoke.
  402. * \param code - a supplementary key-code to consume by built-in action handler.
  403. * \return The action result informing the replxx what shall happen next.
  404. */
  405. REPLXX_IMPEXP ReplxxActionResult replxx_invoke( Replxx*, ReplxxAction action, int unsigned code );
  406. /*! \brief Bind user defined action to handle given key-press event.
  407. *
  408. * \param code - handle this key-press event with following handler.
  409. * \param handler - use this handler to handle key-press event.
  410. * \param userData - supplementary user data passed to invoked handlers.
  411. */
  412. REPLXX_IMPEXP void replxx_bind_key( Replxx*, int code, key_press_handler_t handler, void* userData );
  413. /*! \brief Bind internal `replxx` action (by name) to handle given key-press event.
  414. *
  415. * Action names are the same as unique part of names of ReplxxAction enumerations
  416. * but in lower case, e.g.: an action for recalling previous history line
  417. * is \e REPLXX_ACTION_LINE_PREVIOUS so action name to be used in this
  418. * interface for the same effect is "line_previous".
  419. *
  420. * \param code - handle this key-press event with following handler.
  421. * \param actionName - name of internal action to be invoked on key press.
  422. * \return -1 if invalid action name was used, 0 otherwise.
  423. */
  424. int replxx_bind_key_internal( Replxx*, int code, char const* actionName );
  425. REPLXX_IMPEXP void replxx_set_preload_buffer( Replxx*, const char* preloadText );
  426. REPLXX_IMPEXP void replxx_history_add( Replxx*, const char* line );
  427. REPLXX_IMPEXP int replxx_history_size( Replxx* );
  428. /*! \brief Set set of word break characters.
  429. *
  430. * This setting influences word based cursor movement and line editing capabilities.
  431. *
  432. * \param wordBreakers - 7-bit ASCII set of word breaking characters.
  433. */
  434. REPLXX_IMPEXP void replxx_set_word_break_characters( Replxx*, char const* wordBreakers );
  435. /*! \brief How many completions should trigger pagination.
  436. */
  437. REPLXX_IMPEXP void replxx_set_completion_count_cutoff( Replxx*, int count );
  438. /*! \brief Set maximum number of displayed hint rows.
  439. */
  440. REPLXX_IMPEXP void replxx_set_max_hint_rows( Replxx*, int count );
  441. /*! \brief Set a delay before hint are shown after user stopped typing..
  442. *
  443. * \param milliseconds - a number of milliseconds to wait before showing hints.
  444. */
  445. REPLXX_IMPEXP void replxx_set_hint_delay( Replxx*, int milliseconds );
  446. /*! \brief Set tab completion behavior.
  447. *
  448. * \param val - use double tab to invoke completions (if != 0).
  449. */
  450. REPLXX_IMPEXP void replxx_set_double_tab_completion( Replxx*, int val );
  451. /*! \brief Set tab completion behavior.
  452. *
  453. * \param val - invoke completion even if user input is empty (if != 0).
  454. */
  455. REPLXX_IMPEXP void replxx_set_complete_on_empty( Replxx*, int val );
  456. /*! \brief Set tab completion behavior.
  457. *
  458. * \param val - beep if completion is ambiguous (if != 0).
  459. */
  460. REPLXX_IMPEXP void replxx_set_beep_on_ambiguous_completion( Replxx*, int val );
  461. /*! \brief Set complete next/complete previous behavior.
  462. *
  463. * COMPLETE_NEXT/COMPLETE_PREVIOUS actions have two modes of operations,
  464. * in case when a partial completion is possible complete only partial part (`false` setting)
  465. * or complete first proposed completion fully (`true` setting).
  466. * The default is to complete fully (a `true` setting - complete immediately).
  467. *
  468. * \param val - complete immediately.
  469. */
  470. REPLXX_IMPEXP void replxx_set_immediate_completion( Replxx*, int val );
  471. /*! \brief Set history duplicate entries behaviour.
  472. *
  473. * \param val - should history contain only unique entries?
  474. */
  475. REPLXX_IMPEXP void replxx_set_unique_history( Replxx*, int val );
  476. /*! \brief Disable output coloring.
  477. *
  478. * \param val - if set to non-zero disable output colors.
  479. */
  480. REPLXX_IMPEXP void replxx_set_no_color( Replxx*, int val );
  481. /*! \brief Enable/disable (prompt width) indent for multiline entry.
  482. *
  483. * \param val - if set to non-zero then multiline indent will be enabled.
  484. */
  485. REPLXX_IMPEXP void replxx_set_indent_multiline( Replxx*, int val );
  486. /*! \brief Set maximum number of entries in history list.
  487. */
  488. REPLXX_IMPEXP void replxx_set_max_history_size( Replxx*, int len );
  489. REPLXX_IMPEXP ReplxxHistoryScan* replxx_history_scan_start( Replxx* );
  490. REPLXX_IMPEXP void replxx_history_scan_stop( Replxx*, ReplxxHistoryScan* );
  491. REPLXX_IMPEXP int replxx_history_scan_next( Replxx*, ReplxxHistoryScan*, ReplxxHistoryEntry* );
  492. /*! \brief Synchronize REPL's history with given file.
  493. *
  494. * Synchronizing means loading existing history from given file,
  495. * merging it with current history sorted by timestamps,
  496. * saving merged version to given file,
  497. * keeping merged version as current REPL's history.
  498. *
  499. * This call is an equivalent of calling:
  500. * replxx_history_save( rx, "some-file" );
  501. * replxx_history_load( rx, "some-file" );
  502. *
  503. * \param filename - a path to the file with which REPL's current history should be synchronized.
  504. * \return 0 iff history file was successfully created, -1 otherwise.
  505. */
  506. REPLXX_IMPEXP int replxx_history_sync( Replxx*, const char* filename );
  507. /*! \brief Save REPL's history into given file.
  508. *
  509. * Saving means loading existing history from given file,
  510. * merging it with current history sorted by timestamps,
  511. * saving merged version to given file,
  512. * keeping original (NOT merged) version as current REPL's history.
  513. *
  514. * \param filename - a path to the file where REPL's history should be saved.
  515. * \return 0 iff history file was successfully created, -1 otherwise.
  516. */
  517. REPLXX_IMPEXP int replxx_history_save( Replxx*, const char* filename );
  518. /*! \brief Load REPL's history from given file.
  519. *
  520. * \param filename - a path to the file which contains REPL's history that should be loaded.
  521. * \return 0 iff history file was successfully opened, -1 otherwise.
  522. */
  523. REPLXX_IMPEXP int replxx_history_load( Replxx*, const char* filename );
  524. /*! \brief Clear REPL's in-memory history.
  525. */
  526. REPLXX_IMPEXP void replxx_history_clear( Replxx* );
  527. REPLXX_IMPEXP void replxx_clear_screen( Replxx* );
  528. #ifdef __REPLXX_DEBUG__
  529. void replxx_debug_dump_print_codes(void);
  530. #endif
  531. /* the following is extension to the original linenoise API */
  532. REPLXX_IMPEXP int replxx_install_window_change_handler( Replxx* );
  533. REPLXX_IMPEXP void replxx_enable_bracketed_paste( Replxx* );
  534. REPLXX_IMPEXP void replxx_disable_bracketed_paste( Replxx* );
  535. /*! \brief Combine two color definitions to get encompassing color definition.
  536. *
  537. * To be used only for combining foreground and background colors.
  538. *
  539. * \param color1 - first input color.
  540. * \param color2 - second input color.
  541. * \return A new color definition that represent combined input colors.
  542. */
  543. ReplxxColor replxx_color_combine( ReplxxColor color1, ReplxxColor color2 );
  544. /*! \brief Transform foreground color definition into a background color definition.
  545. *
  546. * \param color - an input foreground color definition.
  547. * \return A background color definition that is a transformed input \e color.
  548. */
  549. ReplxxColor replxx_color_bg( ReplxxColor color );
  550. /*! \brief Add `bold` attribute to color definition.
  551. *
  552. * \param color - an input color definition.
  553. * \return A new color definition with bold attribute set.
  554. */
  555. ReplxxColor replxx_color_bold( ReplxxColor color );
  556. /*! \brief Add `underline` attribute to color definition.
  557. *
  558. * \param color - an input color definition.
  559. * \return A new color definition with underline attribute set.
  560. */
  561. ReplxxColor replxx_color_underline( ReplxxColor color );
  562. /*! \brief Create a new grayscale color of given brightness level.
  563. *
  564. * \param level - a brightness level for new color, must be between 0 (darkest) and 23 (brightest).
  565. * \return A new grayscale color of a given brightest \e level.
  566. */
  567. ReplxxColor replxx_color_grayscale( int level );
  568. /*! \brief Create a new color in 6×6×6 RGB color space from base component levels.
  569. *
  570. * \param red - a red (of RGB) component level, must be 0 and 5.
  571. * \param green - a green (of RGB) component level, must be 0 and 5.
  572. * \param blue - a blue (of RGB) component level, must be 0 and 5.
  573. * \return A new color in 6×6×6 RGB color space.
  574. */
  575. ReplxxColor replxx_color_rgb666( int red, int green, int blue );
  576. #ifdef __cplusplus
  577. }
  578. #endif
  579. #endif /* __REPLXX_H */