builtin.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. ---@meta
  2. -- luacheck: no unused args
  3. error('Cannot require a meta file')
  4. --- @brief <pre>help
  5. --- vim.api.{func}({...}) *vim.api*
  6. --- Invokes Nvim |API| function {func} with arguments {...}.
  7. --- Example: call the "nvim_get_current_line()" API function: >lua
  8. --- print(tostring(vim.api.nvim_get_current_line()))
  9. ---
  10. --- vim.NIL *vim.NIL*
  11. --- Special value representing NIL in |RPC| and |v:null| in Vimscript
  12. --- conversion, and similar cases. Lua `nil` cannot be used as part of a Lua
  13. --- table representing a Dictionary or Array, because it is treated as
  14. --- missing: `{"foo", nil}` is the same as `{"foo"}`.
  15. ---
  16. --- vim.type_idx *vim.type_idx*
  17. --- Type index for use in |lua-special-tbl|. Specifying one of the values from
  18. --- |vim.types| allows typing the empty table (it is unclear whether empty Lua
  19. --- table represents empty list or empty array) and forcing integral numbers
  20. --- to be |Float|. See |lua-special-tbl| for more details.
  21. ---
  22. --- vim.val_idx *vim.val_idx*
  23. --- Value index for tables representing |Float|s. A table representing
  24. --- floating-point value 1.0 looks like this: >lua
  25. --- {
  26. --- [vim.type_idx] = vim.types.float,
  27. --- [vim.val_idx] = 1.0,
  28. --- }
  29. --- < See also |vim.type_idx| and |lua-special-tbl|.
  30. ---
  31. --- vim.types *vim.types*
  32. --- Table with possible values for |vim.type_idx|. Contains two sets of
  33. --- key-value pairs: first maps possible values for |vim.type_idx| to
  34. --- human-readable strings, second maps human-readable type names to values
  35. --- for |vim.type_idx|. Currently contains pairs for `float`, `array` and
  36. --- `dictionary` types.
  37. ---
  38. --- Note: One must expect that values corresponding to `vim.types.float`,
  39. --- `vim.types.array` and `vim.types.dictionary` fall under only two following
  40. --- assumptions:
  41. --- 1. Value may serve both as a key and as a value in a table. Given the
  42. --- properties of Lua tables this basically means “value is not `nil`”.
  43. --- 2. For each value in `vim.types` table `vim.types[vim.types[value]]` is the
  44. --- same as `value`.
  45. --- No other restrictions are put on types, and it is not guaranteed that
  46. --- values corresponding to `vim.types.float`, `vim.types.array` and
  47. --- `vim.types.dictionary` will not change or that `vim.types` table will only
  48. --- contain values for these three types.
  49. ---
  50. --- *log_levels* *vim.log.levels*
  51. --- Log levels are one of the values defined in `vim.log.levels`:
  52. ---
  53. --- vim.log.levels.DEBUG
  54. --- vim.log.levels.ERROR
  55. --- vim.log.levels.INFO
  56. --- vim.log.levels.TRACE
  57. --- vim.log.levels.WARN
  58. --- vim.log.levels.OFF
  59. ---
  60. --- </pre>
  61. ---@nodoc
  62. ---@class vim.NIL
  63. ---@type vim.NIL
  64. ---@nodoc
  65. vim.NIL = ...
  66. --- Returns true if the code is executing as part of a "fast" event handler,
  67. --- where most of the API is disabled. These are low-level events (e.g.
  68. --- |lua-loop-callbacks|) which can be invoked whenever Nvim polls for input.
  69. --- When this is `false` most API functions are callable (but may be subject
  70. --- to other restrictions such as |textlock|).
  71. function vim.in_fast_event() end
  72. --- Creates a special empty table (marked with a metatable), which Nvim
  73. --- converts to an empty dictionary when translating Lua values to Vimscript
  74. --- or API types. Nvim by default converts an empty table `{}` without this
  75. --- metatable to an list/array.
  76. ---
  77. --- Note: If numeric keys are present in the table, Nvim ignores the metatable
  78. --- marker and converts the dict to a list/array anyway.
  79. --- @return table
  80. function vim.empty_dict() end
  81. --- Sends {event} to {channel} via |RPC| and returns immediately. If {channel}
  82. --- is 0, the event is broadcast to all channels.
  83. ---
  84. --- This function also works in a fast callback |lua-loop-callbacks|.
  85. --- @param channel integer
  86. --- @param method string
  87. --- @param ...? any
  88. function vim.rpcnotify(channel, method, ...) end
  89. --- Sends a request to {channel} to invoke {method} via |RPC| and blocks until
  90. --- a response is received.
  91. ---
  92. --- Note: NIL values as part of the return value is represented as |vim.NIL|
  93. --- special value
  94. --- @param channel integer
  95. --- @param method string
  96. --- @param ...? any
  97. function vim.rpcrequest(channel, method, ...) end
  98. --- Compares strings case-insensitively.
  99. --- @param a string
  100. --- @param b string
  101. --- @return 0|1|-1
  102. --- if strings are
  103. --- equal, {a} is greater than {b} or {a} is lesser than {b}, respectively.
  104. function vim.stricmp(a, b) end
  105. --- Gets a list of the starting byte positions of each UTF-8 codepoint in the given string.
  106. ---
  107. --- Embedded NUL bytes are treated as terminating the string.
  108. --- @param str string
  109. --- @return integer[]
  110. function vim.str_utf_pos(str) end
  111. --- Gets the distance (in bytes) from the starting byte of the codepoint (character) that {index}
  112. --- points to.
  113. ---
  114. --- The result can be added to {index} to get the starting byte of a character.
  115. ---
  116. --- Examples:
  117. ---
  118. --- ```lua
  119. --- -- The character 'æ' is stored as the bytes '\xc3\xa6' (using UTF-8)
  120. ---
  121. --- -- Returns 0 because the index is pointing at the first byte of a character
  122. --- vim.str_utf_start('æ', 1)
  123. ---
  124. --- -- Returns -1 because the index is pointing at the second byte of a character
  125. --- vim.str_utf_start('æ', 2)
  126. --- ```
  127. ---
  128. --- @param str string
  129. --- @param index integer
  130. --- @return integer
  131. function vim.str_utf_start(str, index) end
  132. --- Gets the distance (in bytes) from the last byte of the codepoint (character) that {index} points
  133. --- to.
  134. ---
  135. --- Examples:
  136. ---
  137. --- ```lua
  138. --- -- The character 'æ' is stored as the bytes '\xc3\xa6' (using UTF-8)
  139. ---
  140. --- -- Returns 0 because the index is pointing at the last byte of a character
  141. --- vim.str_utf_end('æ', 2)
  142. ---
  143. --- -- Returns 1 because the index is pointing at the penultimate byte of a character
  144. --- vim.str_utf_end('æ', 1)
  145. --- ```
  146. ---
  147. --- @param str string
  148. --- @param index integer
  149. --- @return integer
  150. function vim.str_utf_end(str, index) end
  151. --- The result is a String, which is the text {str} converted from
  152. --- encoding {from} to encoding {to}. When the conversion fails `nil` is
  153. --- returned. When some characters could not be converted they
  154. --- are replaced with "?".
  155. --- The encoding names are whatever the iconv() library function
  156. --- can accept, see ":Man 3 iconv".
  157. ---
  158. --- @param str string Text to convert
  159. --- @param from string Encoding of {str}
  160. --- @param to string Target encoding
  161. --- @return string? : Converted string if conversion succeeds, `nil` otherwise.
  162. function vim.iconv(str, from, to, opts) end
  163. --- Schedules {fn} to be invoked soon by the main event-loop. Useful
  164. --- to avoid |textlock| or other temporary restrictions.
  165. --- @param fn fun()
  166. function vim.schedule(fn) end
  167. --- Wait for {time} in milliseconds until {callback} returns `true`.
  168. ---
  169. --- Executes {callback} immediately and at approximately {interval}
  170. --- milliseconds (default 200). Nvim still processes other events during
  171. --- this time.
  172. ---
  173. --- Cannot be called while in an |api-fast| event.
  174. ---
  175. --- Examples:
  176. ---
  177. --- ```lua
  178. --- ---
  179. --- -- Wait for 100 ms, allowing other events to process
  180. --- vim.wait(100, function() end)
  181. ---
  182. --- ---
  183. --- -- Wait for 100 ms or until global variable set.
  184. --- vim.wait(100, function() return vim.g.waiting_for_var end)
  185. ---
  186. --- ---
  187. --- -- Wait for 1 second or until global variable set, checking every ~500 ms
  188. --- vim.wait(1000, function() return vim.g.waiting_for_var end, 500)
  189. ---
  190. --- ---
  191. --- -- Schedule a function to set a value in 100ms
  192. --- vim.defer_fn(function() vim.g.timer_result = true end, 100)
  193. ---
  194. --- -- Would wait ten seconds if results blocked. Actually only waits 100 ms
  195. --- if vim.wait(10000, function() return vim.g.timer_result end) then
  196. --- print('Only waiting a little bit of time!')
  197. --- end
  198. --- ```
  199. ---
  200. --- @param time integer Number of milliseconds to wait
  201. --- @param callback? fun(): boolean Optional callback. Waits until {callback} returns true
  202. --- @param interval? integer (Approximate) number of milliseconds to wait between polls
  203. --- @param fast_only? boolean If true, only |api-fast| events will be processed.
  204. --- @return boolean, nil|-1|-2
  205. --- - If {callback} returns `true` during the {time}: `true, nil`
  206. --- - If {callback} never returns `true` during the {time}: `false, -1`
  207. --- - If {callback} is interrupted during the {time}: `false, -2`
  208. --- - If {callback} errors, the error is raised.
  209. function vim.wait(time, callback, interval, fast_only) end
  210. --- Attach to |ui-events|, similar to |nvim_ui_attach()| but receive events
  211. --- as Lua callback. Can be used to implement screen elements like
  212. --- popupmenu or message handling in Lua.
  213. ---
  214. --- {options} should be a dictionary-like table, where `ext_...` options should
  215. --- be set to true to receive events for the respective external element.
  216. ---
  217. --- {callback} receives event name plus additional parameters. See |ui-popupmenu|
  218. --- and the sections below for event format for respective events.
  219. ---
  220. --- Callbacks for `msg_show` events are executed in |api-fast| context; showing
  221. --- the message should be scheduled.
  222. ---
  223. --- Excessive errors inside the callback will result in forced detachment.
  224. ---
  225. --- WARNING: This api is considered experimental. Usability will vary for
  226. --- different screen elements. In particular `ext_messages` behavior is subject
  227. --- to further changes and usability improvements. This is expected to be
  228. --- used to handle messages when setting 'cmdheight' to zero (which is
  229. --- likewise experimental).
  230. ---
  231. --- Example (stub for a |ui-popupmenu| implementation):
  232. ---
  233. --- ```lua
  234. --- ns = vim.api.nvim_create_namespace('my_fancy_pum')
  235. ---
  236. --- vim.ui_attach(ns, {ext_popupmenu=true}, function(event, ...)
  237. --- if event == "popupmenu_show" then
  238. --- local items, selected, row, col, grid = ...
  239. --- print("display pum ", #items)
  240. --- elseif event == "popupmenu_select" then
  241. --- local selected = ...
  242. --- print("selected", selected)
  243. --- elseif event == "popupmenu_hide" then
  244. --- print("FIN")
  245. --- end
  246. --- end)
  247. --- ```
  248. ---
  249. --- @since 0
  250. ---
  251. --- @param ns integer
  252. --- @param options table<string, any>
  253. --- @param callback fun()
  254. function vim.ui_attach(ns, options, callback) end
  255. --- Detach a callback previously attached with |vim.ui_attach()| for the
  256. --- given namespace {ns}.
  257. --- @param ns integer
  258. function vim.ui_detach(ns) end