robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
33.0 KiB1053 linesraw
1// Island of misfit toys.
2
3#include <stdbool.h>
4#include <stdint.h>
5#include <string.h>
6
7#include "nvim/api/buffer.h"
8#include "nvim/api/deprecated.h"
9#include "nvim/api/extmark.h"
10#include "nvim/api/keysets_defs.h"
11#include "nvim/api/private/defs.h"
12#include "nvim/api/private/dispatch.h"
13#include "nvim/api/private/helpers.h"
14#include "nvim/api/private/validate.h"
15#include "nvim/api/vimscript.h"
16#include "nvim/buffer_defs.h"
17#include "nvim/decoration.h"
18#include "nvim/decoration_defs.h"
19#include "nvim/eval/vars.h"
20#include "nvim/extmark.h"
21#include "nvim/globals.h"
22#include "nvim/highlight.h"
23#include "nvim/highlight_group.h"
24#include "nvim/lua/executor.h"
25#include "nvim/marktree.h"
26#include "nvim/memory.h"
27#include "nvim/memory_defs.h"
28#include "nvim/message.h"
29#include "nvim/option.h"
30#include "nvim/option_defs.h"
31#include "nvim/pos_defs.h"
32#include "nvim/strings.h"
33#include "nvim/types_defs.h"
34#include "nvim/window.h"
35
36#include "api/deprecated.c.generated.h"
37
38/// @deprecated Use nvim_exec2() instead.
39/// @see nvim_exec2
40String nvim_exec(uint64_t channel_id, String src, Boolean output, Error *err)
41  FUNC_API_SINCE(7) FUNC_API_DEPRECATED_SINCE(11)
42  FUNC_API_RET_ALLOC
43{
44  Dict(exec_opts) opts = { .output = output };
45  return exec_impl(channel_id, src, &opts, err);
46}
47
48/// @deprecated
49/// @see nvim_exec2
50String nvim_command_output(uint64_t channel_id, String command, Error *err)
51  FUNC_API_SINCE(1) FUNC_API_DEPRECATED_SINCE(7)
52  FUNC_API_RET_ALLOC
53{
54  Dict(exec_opts) opts = { .output = true };
55  return exec_impl(channel_id, command, &opts, err);
56}
57
58/// @deprecated Use nvim_exec_lua() instead.
59/// @see nvim_exec_lua
60Object nvim_execute_lua(String code, Array args, Arena *arena, Error *err)
61  FUNC_API_SINCE(3)
62  FUNC_API_DEPRECATED_SINCE(7)
63  FUNC_API_REMOTE_ONLY
64{
65  return nlua_exec(code, NULL, args, kRetObject, arena, err);
66}
67
68/// Gets the buffer number
69///
70/// @deprecated The buffer number now is equal to the object id
71///
72/// @param buffer     Buffer id, or 0 for current buffer
73/// @param[out] err   Error details, if any
74/// @return Buffer number
75Integer nvim_buf_get_number(Buffer buffer, Error *err)
76  FUNC_API_SINCE(1)
77  FUNC_API_DEPRECATED_SINCE(2)
78{
79  buf_T *buf = find_buffer_by_handle(buffer, err);
80
81  if (!buf) {
82    return 0;
83  }
84
85  return buf->b_fnum;
86}
87
88static uint32_t src2ns(Integer *src_id)
89{
90  if (*src_id == 0) {
91    *src_id = nvim_create_namespace((String)STRING_INIT);
92  }
93  if (*src_id < 0) {
94    return (((uint32_t)1) << 31) - 1;
95  }
96  return (uint32_t)(*src_id);
97}
98
99/// Clears highlights and virtual text from namespace and range of lines
100///
101/// @deprecated use |nvim_buf_clear_namespace()|.
102///
103/// @param buffer     Buffer id, or 0 for current buffer
104/// @param ns_id      Namespace to clear, or -1 to clear all.
105/// @param line_start Start of range of lines to clear
106/// @param line_end   End of range of lines to clear (exclusive) or -1 to clear
107///                   to end of file.
108/// @param[out] err   Error details, if any
109void nvim_buf_clear_highlight(Buffer buffer, Integer ns_id, Integer line_start, Integer line_end,
110                              Error *err)
111  FUNC_API_SINCE(1)
112  FUNC_API_DEPRECATED_SINCE(7)
113{
114  nvim_buf_clear_namespace(buffer, ns_id, line_start, line_end, err);
115}
116
117/// Adds a highlight to buffer.
118///
119/// @deprecated use |nvim_buf_set_extmark()| or |vim.hl.range()|
120///
121/// Namespaces are used for batch deletion/updating of a set of highlights. To
122/// create a namespace, use |nvim_create_namespace()| which returns a namespace
123/// id. Pass it in to this function as `ns_id` to add highlights to the
124/// namespace. All highlights in the same namespace can then be cleared with
125/// single call to |nvim_buf_clear_namespace()|. If the highlight never will be
126/// deleted by an API call, pass `ns_id = -1`.
127///
128/// As a shorthand, `ns_id = 0` can be used to create a new namespace for the
129/// highlight, the allocated id is then returned. If `hl_group` is the empty
130/// string no highlight is added, but a new `ns_id` is still returned. This is
131/// supported for backwards compatibility, new code should use
132/// |nvim_create_namespace()| to create a new empty namespace.
133///
134/// @param buffer     Buffer id, or 0 for current buffer
135/// @param ns_id      namespace to use or -1 for ungrouped highlight
136/// @param hl_group   Name of the highlight group to use
137/// @param line       Line to highlight (zero-indexed)
138/// @param col_start  Start of (byte-indexed) column range to highlight
139/// @param col_end    End of (byte-indexed) column range to highlight,
140///                   or -1 to highlight to end of line
141/// @param[out] err   Error details, if any
142/// @return The ns_id that was used
143Integer nvim_buf_add_highlight(Buffer buffer, Integer ns_id, String hl_group, Integer line,
144                               Integer col_start, Integer col_end, Error *err)
145  FUNC_API_SINCE(1)
146  FUNC_API_DEPRECATED_SINCE(13)
147{
148  buf_T *buf = find_buffer_by_handle(buffer, err);
149  if (!buf) {
150    return 0;
151  }
152
153  VALIDATE_RANGE((line >= 0 && line < MAXLNUM), "line number", {
154    return 0;
155  });
156  VALIDATE_RANGE((col_start >= 0 && col_start <= MAXCOL), "column", {
157    return 0;
158  });
159
160  if (col_end < 0 || col_end > MAXCOL) {
161    col_end = MAXCOL;
162  }
163
164  uint32_t ns = src2ns(&ns_id);
165
166  if (!(line < buf->b_ml.ml_line_count)) {
167    // safety check, we can't add marks outside the range
168    return ns_id;
169  }
170
171  int hl_id = 0;
172  if (hl_group.size > 0) {
173    hl_id = syn_check_group(hl_group.data, hl_group.size);
174  } else {
175    return ns_id;
176  }
177
178  int end_line = (int)line;
179  if (col_end == MAXCOL) {
180    col_end = 0;
181    end_line++;
182  }
183
184  DecorInline decor = DECOR_INLINE_INIT;
185  decor.data.hl.hl_id = hl_id;
186
187  extmark_set(buf, ns, NULL, (int)line, (colnr_T)col_start, end_line, (colnr_T)col_end,
188              decor, MT_FLAG_DECOR_HL, true, false, false, false, NULL);
189  return ns_id;
190}
191/// Set the virtual text (annotation) for a buffer line.
192///
193/// @deprecated use nvim_buf_set_extmark to use full virtual text functionality.
194///
195/// The text will be placed after the buffer text. Virtual text will never
196/// cause reflow, rather virtual text will be truncated at the end of the screen
197/// line. The virtual text will begin one cell (|lcs-eol| or space) after the
198/// ordinary text.
199///
200/// Namespaces are used to support batch deletion/updating of virtual text.
201/// To create a namespace, use |nvim_create_namespace()|. Virtual text is
202/// cleared using |nvim_buf_clear_namespace()|. The same `ns_id` can be used for
203/// both virtual text and highlights added by |nvim_buf_add_highlight()|, both
204/// can then be cleared with a single call to |nvim_buf_clear_namespace()|. If
205/// the virtual text never will be cleared by an API call, pass `ns_id = -1`.
206///
207/// As a shorthand, `ns_id = 0` can be used to create a new namespace for the
208/// virtual text, the allocated id is then returned.
209///
210/// @param buffer     Buffer id, or 0 for current buffer
211/// @param src_id     Namespace to use or 0 to create a namespace,
212///                   or -1 for a ungrouped annotation
213/// @param line       Line to annotate with virtual text (zero-indexed)
214/// @param chunks     A list of [text, hl_group] arrays, each representing a
215///                   text chunk with specified highlight. `hl_group` element
216///                   can be omitted for no highlight.
217/// @param opts       Optional parameters. Currently not used.
218/// @param[out] err   Error details, if any
219/// @return The ns_id that was used
220Integer nvim_buf_set_virtual_text(Buffer buffer, Integer src_id, Integer line, Array chunks,
221                                  Dict(empty) *opts, Error *err)
222  FUNC_API_SINCE(5)
223  FUNC_API_DEPRECATED_SINCE(8)
224{
225  buf_T *buf = find_buffer_by_handle(buffer, err);
226  if (!buf) {
227    return 0;
228  }
229
230  if (line < 0 || line >= MAXLNUM) {
231    api_set_error(err, kErrorTypeValidation, "Line number outside range");
232    return 0;
233  }
234
235  uint32_t ns_id = src2ns(&src_id);
236  int width;
237
238  VirtText virt_text = parse_virt_text(chunks, err, &width, false);
239  if (ERROR_SET(err)) {
240    return 0;
241  }
242
243  DecorVirtText *existing = decor_find_virttext(buf, (int)line, ns_id);
244
245  if (existing) {
246    clear_virttext(&existing->data.virt_text);
247    existing->data.virt_text = virt_text;
248    existing->width = width;
249    return src_id;
250  }
251
252  DecorVirtText *vt = xmalloc(sizeof *vt);
253  *vt = (DecorVirtText)DECOR_VIRT_TEXT_INIT;
254  vt->data.virt_text = virt_text;
255  vt->width = width;
256  vt->priority = 0;
257
258  DecorInline decor = { .ext = true, .data.ext.vt = vt, .data.ext.sh_idx = DECOR_ID_INVALID };
259
260  extmark_set(buf, ns_id, NULL, (int)line, 0, -1, -1, decor, 0, true,
261              false, false, false, NULL);
262  return src_id;
263}
264
265/// Gets a highlight definition by id. |hlID()|
266///
267/// @deprecated use |nvim_get_hl()| instead
268///
269/// @param hl_id Highlight id as returned by |hlID()|
270/// @param rgb Export RGB colors
271/// @param[out] err Error details, if any
272/// @return Highlight definition map
273/// @see nvim_get_hl_by_name
274Dict nvim_get_hl_by_id(Integer hl_id, Boolean rgb, Arena *arena, Error *err)
275  FUNC_API_SINCE(3)
276  FUNC_API_DEPRECATED_SINCE(9)
277{
278  Dict dic = ARRAY_DICT_INIT;
279  VALIDATE_INT((syn_get_final_id((int)hl_id) != 0), "highlight id", hl_id, {
280    return dic;
281  });
282  int attrcode = syn_id2attr((int)hl_id);
283  return hl_get_attr_by_id(attrcode, rgb, arena, err);
284}
285
286/// Gets a highlight definition by name.
287///
288/// @deprecated use |nvim_get_hl()| instead
289///
290/// @param name Highlight group name
291/// @param rgb Export RGB colors
292/// @param[out] err Error details, if any
293/// @return Highlight definition map
294/// @see nvim_get_hl_by_id
295Dict nvim_get_hl_by_name(String name, Boolean rgb, Arena *arena, Error *err)
296  FUNC_API_SINCE(3)
297  FUNC_API_DEPRECATED_SINCE(9)
298{
299  Dict result = ARRAY_DICT_INIT;
300  int id = syn_name2id(name.data);
301
302  VALIDATE_S((id != 0), "highlight name", name.data, {
303    return result;
304  });
305  return nvim_get_hl_by_id(id, rgb, arena, err);
306}
307
308/// Inserts a sequence of lines to a buffer at a certain index
309///
310/// @deprecated use nvim_buf_set_lines(buffer, lnum, lnum, true, lines)
311///
312/// @param buffer     Buffer id
313/// @param lnum       Insert the lines after `lnum`. If negative, appends to
314///                   the end of the buffer.
315/// @param lines      Array of lines
316/// @param[out] err   Error details, if any
317void buffer_insert(Buffer buffer, Integer lnum, ArrayOf(String) lines, Arena *arena, Error *err)
318  FUNC_API_DEPRECATED_SINCE(1)
319{
320  // "lnum" will be the index of the line after inserting,
321  // no matter if it is negative or not
322  nvim_buf_set_lines(0, buffer, lnum, lnum, true, lines, arena, err);
323}
324
325/// Gets a buffer line
326///
327/// @deprecated use nvim_buf_get_lines instead.
328///             for positive indices (including 0) use
329///                 "nvim_buf_get_lines(buffer, index, index+1, true)"
330///             for negative indices use
331///                 "nvim_buf_get_lines(buffer, index-1, index, true)"
332///
333/// @param buffer   Buffer id
334/// @param index    Line index
335/// @param[out] err Error details, if any
336/// @return Line string
337String buffer_get_line(Buffer buffer, Integer index, Arena *arena, Error *err)
338  FUNC_API_DEPRECATED_SINCE(1)
339{
340  String rv = { .size = 0 };
341
342  index = convert_index(index);
343  Array slice = nvim_buf_get_lines(0, buffer, index, index + 1, true, arena, NULL, err);
344
345  if (!ERROR_SET(err) && slice.size) {
346    rv = slice.items[0].data.string;
347  }
348
349  return rv;
350}
351
352/// Sets a buffer line
353///
354/// @deprecated use nvim_buf_set_lines instead.
355///             for positive indices use
356///                 "nvim_buf_set_lines(buffer, index, index+1, true, [line])"
357///             for negative indices use
358///                 "nvim_buf_set_lines(buffer, index-1, index, true, [line])"
359///
360/// @param buffer   Buffer id
361/// @param index    Line index
362/// @param line     Contents of the new line
363/// @param[out] err Error details, if any
364void buffer_set_line(Buffer buffer, Integer index, String line, Arena *arena, Error *err)
365  FUNC_API_DEPRECATED_SINCE(1)
366{
367  Object l = STRING_OBJ(line);
368  Array array = { .items = &l, .size = 1 };
369  index = convert_index(index);
370  nvim_buf_set_lines(0, buffer, index, index + 1, true,  array, arena, err);
371}
372
373/// Deletes a buffer line
374///
375/// @deprecated use nvim_buf_set_lines instead.
376///             for positive indices use
377///                 "nvim_buf_set_lines(buffer, index, index+1, true, [])"
378///             for negative indices use
379///                 "nvim_buf_set_lines(buffer, index-1, index, true, [])"
380/// @param buffer   buffer id
381/// @param index    line index
382/// @param[out] err Error details, if any
383void buffer_del_line(Buffer buffer, Integer index, Arena *arena, Error *err)
384  FUNC_API_DEPRECATED_SINCE(1)
385{
386  Array array = ARRAY_DICT_INIT;
387  index = convert_index(index);
388  nvim_buf_set_lines(0, buffer, index, index + 1, true, array, arena, err);
389}
390
391/// Retrieves a line range from the buffer
392///
393/// @deprecated use nvim_buf_get_lines(buffer, newstart, newend, false)
394///             where newstart = start + int(not include_start) - int(start < 0)
395///                   newend = end + int(include_end) - int(end < 0)
396///                   int(bool) = 1 if bool is true else 0
397/// @param buffer         Buffer id
398/// @param start          First line index
399/// @param end            Last line index
400/// @param include_start  True if the slice includes the `start` parameter
401/// @param include_end    True if the slice includes the `end` parameter
402/// @param[out] err       Error details, if any
403/// @return Array of lines
404ArrayOf(String) buffer_get_line_slice(Buffer buffer,
405                                      Integer start,
406                                      Integer end,
407                                      Boolean include_start,
408                                      Boolean include_end,
409                                      Arena *arena,
410                                      Error *err)
411  FUNC_API_DEPRECATED_SINCE(1)
412{
413  start = convert_index(start) + !include_start;
414  end = convert_index(end) + include_end;
415  return nvim_buf_get_lines(0, buffer, start, end, false, arena, NULL, err);
416}
417
418/// Replaces a line range on the buffer
419///
420/// @deprecated use nvim_buf_set_lines(buffer, newstart, newend, false, lines)
421///             where newstart = start + int(not include_start) + int(start < 0)
422///                   newend = end + int(include_end) + int(end < 0)
423///                   int(bool) = 1 if bool is true else 0
424///
425/// @param buffer         Buffer id, or 0 for current buffer
426/// @param start          First line index
427/// @param end            Last line index
428/// @param include_start  True if the slice includes the `start` parameter
429/// @param include_end    True if the slice includes the `end` parameter
430/// @param replacement    Array of lines to use as replacement (0-length
431//                        array will delete the line range)
432/// @param[out] err       Error details, if any
433void buffer_set_line_slice(Buffer buffer, Integer start, Integer end, Boolean include_start,
434                           Boolean include_end, ArrayOf(String) replacement, Arena *arena,
435                           Error *err)
436  FUNC_API_DEPRECATED_SINCE(1)
437{
438  start = convert_index(start) + !include_start;
439  end = convert_index(end) + include_end;
440  nvim_buf_set_lines(0, buffer, start, end, false, replacement, arena, err);
441}
442
443/// Sets a buffer-scoped (b:) variable
444///
445/// @deprecated
446///
447/// @param buffer     Buffer id, or 0 for current buffer
448/// @param name       Variable name
449/// @param value      Variable value
450/// @param[out] err   Error details, if any
451/// @return Old value or nil if there was no previous value.
452///
453///         @warning It may return nil if there was no previous value
454///                  or if previous value was `v:null`.
455Object buffer_set_var(Buffer buffer, String name, Object value, Arena *arena, Error *err)
456  FUNC_API_DEPRECATED_SINCE(1)
457{
458  buf_T *buf = find_buffer_by_handle(buffer, err);
459
460  if (!buf) {
461    return NIL;
462  }
463
464  return dict_set_var(buf->b_vars, name, value, false, true, arena, err);
465}
466
467/// Removes a buffer-scoped (b:) variable
468///
469/// @deprecated
470///
471/// @param buffer     Buffer id, or 0 for current buffer
472/// @param name       Variable name
473/// @param[out] err   Error details, if any
474/// @return Old value
475Object buffer_del_var(Buffer buffer, String name, Arena *arena, Error *err)
476  FUNC_API_DEPRECATED_SINCE(1)
477{
478  buf_T *buf = find_buffer_by_handle(buffer, err);
479
480  if (!buf) {
481    return NIL;
482  }
483
484  return dict_set_var(buf->b_vars, name, NIL, true, true, arena, err);
485}
486
487/// Sets a window-scoped (w:) variable
488///
489/// @deprecated
490///
491/// @param window   |window-ID|, or 0 for current window
492/// @param name     Variable name
493/// @param value    Variable value
494/// @param[out] err Error details, if any
495/// @return Old value or nil if there was no previous value.
496///
497///         @warning It may return nil if there was no previous value
498///                  or if previous value was `v:null`.
499Object window_set_var(Window window, String name, Object value, Arena *arena, Error *err)
500  FUNC_API_DEPRECATED_SINCE(1)
501{
502  win_T *win = find_window_by_handle(window, err);
503
504  if (!win) {
505    return NIL;
506  }
507
508  return dict_set_var(win->w_vars, name, value, false, true, arena, err);
509}
510
511/// Removes a window-scoped (w:) variable
512///
513/// @deprecated
514///
515/// @param window   |window-ID|, or 0 for current window
516/// @param name     variable name
517/// @param[out] err Error details, if any
518/// @return Old value
519Object window_del_var(Window window, String name, Arena *arena, Error *err)
520  FUNC_API_DEPRECATED_SINCE(1)
521{
522  win_T *win = find_window_by_handle(window, err);
523
524  if (!win) {
525    return NIL;
526  }
527
528  return dict_set_var(win->w_vars, name, NIL, true, true, arena, err);
529}
530
531/// Sets a tab-scoped (t:) variable
532///
533/// @deprecated
534///
535/// @param tabpage  |tab-ID|, or 0 for current tabpage
536/// @param name     Variable name
537/// @param value    Variable value
538/// @param[out] err Error details, if any
539/// @return Old value or nil if there was no previous value.
540///
541///         @warning It may return nil if there was no previous value
542///                  or if previous value was `v:null`.
543Object tabpage_set_var(Tabpage tabpage, String name, Object value, Arena *arena, Error *err)
544  FUNC_API_DEPRECATED_SINCE(1)
545{
546  tabpage_T *tab = find_tab_by_handle(tabpage, err);
547
548  if (!tab) {
549    return NIL;
550  }
551
552  return dict_set_var(tab->tp_vars, name, value, false, true, arena, err);
553}
554
555/// Removes a tab-scoped (t:) variable
556///
557/// @deprecated
558///
559/// @param tabpage  |tab-ID|, or 0 for current tabpage
560/// @param name     Variable name
561/// @param[out] err Error details, if any
562/// @return Old value
563Object tabpage_del_var(Tabpage tabpage, String name, Arena *arena, Error *err)
564  FUNC_API_DEPRECATED_SINCE(1)
565{
566  tabpage_T *tab = find_tab_by_handle(tabpage, err);
567
568  if (!tab) {
569    return NIL;
570  }
571
572  return dict_set_var(tab->tp_vars, name, NIL, true, true, arena, err);
573}
574
575/// @deprecated
576/// @see nvim_set_var
577/// @warning May return nil if there was no previous value
578///          OR if previous value was `v:null`.
579/// @return Old value or nil if there was no previous value.
580Object vim_set_var(String name, Object value, Arena *arena, Error *err)
581  FUNC_API_DEPRECATED_SINCE(1)
582{
583  return dict_set_var(get_globvar_dict(), name, value, false, true, arena, err);
584}
585
586/// @deprecated
587/// @see nvim_del_var
588Object vim_del_var(String name, Arena *arena, Error *err)
589  FUNC_API_DEPRECATED_SINCE(1)
590{
591  return dict_set_var(get_globvar_dict(), name, NIL, true, true, arena, err);
592}
593
594static int64_t convert_index(int64_t index)
595{
596  return index < 0 ? index - 1 : index;
597}
598
599/// Gets the option information for one option
600///
601/// @deprecated Use @ref nvim_get_option_info2 instead.
602///
603/// @param          name Option name
604/// @param[out] err Error details, if any
605/// @return         Option Information
606DictAs(get_option_info) nvim_get_option_info(String name, Arena *arena, Error *err)
607  FUNC_API_SINCE(7)
608  FUNC_API_DEPRECATED_SINCE(11)
609{
610  return get_option_info(name, OPT_GLOBAL, curbuf, curwin, arena, err);
611}
612
613/// Sets the global value of an option.
614///
615/// @deprecated
616/// @param channel_id
617/// @param name     Option name
618/// @param value    New option value
619/// @param[out] err Error details, if any
620void nvim_set_option(uint64_t channel_id, String name, Object value, Error *err)
621  FUNC_API_SINCE(1)
622  FUNC_API_DEPRECATED_SINCE(11)
623{
624  set_option_to(channel_id, NULL, kOptScopeGlobal, name, value, err);
625}
626
627/// Gets the global value of an option.
628///
629/// @deprecated
630/// @param name     Option name
631/// @param[out] err Error details, if any
632/// @return         Option value (global)
633Object nvim_get_option(String name, Error *err)
634  FUNC_API_SINCE(1)
635  FUNC_API_DEPRECATED_SINCE(11)
636  FUNC_API_RET_ALLOC
637{
638  return get_option_from(NULL, kOptScopeGlobal, name, err);
639}
640
641/// Gets a buffer option value
642///
643/// @deprecated
644/// @param buffer     Buffer id, or 0 for current buffer
645/// @param name       Option name
646/// @param[out] err   Error details, if any
647/// @return Option value
648Object nvim_buf_get_option(Buffer buffer, String name, Error *err)
649  FUNC_API_SINCE(1)
650  FUNC_API_DEPRECATED_SINCE(11)
651  FUNC_API_RET_ALLOC
652{
653  buf_T *buf = find_buffer_by_handle(buffer, err);
654
655  if (!buf) {
656    return (Object)OBJECT_INIT;
657  }
658
659  return get_option_from(buf, kOptScopeBuf, name, err);
660}
661
662/// Sets a buffer option value. Passing `nil` as value deletes the option (only
663/// works if there's a global fallback)
664///
665/// @deprecated
666/// @param channel_id
667/// @param buffer     Buffer id, or 0 for current buffer
668/// @param name       Option name
669/// @param value      Option value
670/// @param[out] err   Error details, if any
671void nvim_buf_set_option(uint64_t channel_id, Buffer buffer, String name, Object value, Error *err)
672  FUNC_API_SINCE(1)
673  FUNC_API_DEPRECATED_SINCE(11)
674{
675  buf_T *buf = find_buffer_by_handle(buffer, err);
676
677  if (!buf) {
678    return;
679  }
680
681  set_option_to(channel_id, buf, kOptScopeBuf, name, value, err);
682}
683
684/// Gets a window option value
685///
686/// @deprecated
687/// @param window   |window-ID|, or 0 for current window
688/// @param name     Option name
689/// @param[out] err Error details, if any
690/// @return Option value
691Object nvim_win_get_option(Window window, String name, Error *err)
692  FUNC_API_SINCE(1)
693  FUNC_API_DEPRECATED_SINCE(11)
694  FUNC_API_RET_ALLOC
695{
696  win_T *win = find_window_by_handle(window, err);
697
698  if (!win) {
699    return (Object)OBJECT_INIT;
700  }
701
702  return get_option_from(win, kOptScopeWin, name, err);
703}
704
705/// Sets a window option value. Passing `nil` as value deletes the option (only
706/// works if there's a global fallback)
707///
708/// @deprecated
709/// @param channel_id
710/// @param window   |window-ID|, or 0 for current window
711/// @param name     Option name
712/// @param value    Option value
713/// @param[out] err Error details, if any
714void nvim_win_set_option(uint64_t channel_id, Window window, String name, Object value, Error *err)
715  FUNC_API_SINCE(1)
716  FUNC_API_DEPRECATED_SINCE(11)
717{
718  win_T *win = find_window_by_handle(window, err);
719
720  if (!win) {
721    return;
722  }
723
724  set_option_to(channel_id, win, kOptScopeWin, name, value, err);
725}
726
727/// Gets the value of a global or local (buffer, window) option.
728///
729/// @param[in]   from       Pointer to buffer or window for local option value.
730/// @param       scope      Option scope. See OptScope in option.h.
731/// @param       name       The option name.
732/// @param[out]  err        Details of an error that may have occurred.
733///
734/// @return  the option value. Must be freed by caller.
735static Object get_option_from(void *from, OptScope scope, String name, Error *err)
736{
737  VALIDATE_S(name.size > 0, "option name", "<empty>", {
738    return (Object)OBJECT_INIT;
739  });
740
741  OptIndex opt_idx = find_option(name.data);
742  VALIDATE_S(opt_idx != kOptInvalid, "option name", name.data, {
743    return (Object)OBJECT_INIT;
744  });
745
746  // Reject a scope the option doesn't support. This must be an explicit check: an unset value is
747  // itself Nil, so a Nil value can't distinguish "unsupported scope" from "unset value".
748  VALIDATE_S(option_has_scope(opt_idx, scope), "option name", name.data, {
749    return (Object)OBJECT_INIT;
750  });
751
752  Object value = get_option_value_for(opt_idx, scope == kOptScopeGlobal ? OPT_GLOBAL : OPT_LOCAL,
753                                      scope, from, err);
754  if (ERROR_SET(err)) {
755    return (Object)OBJECT_INIT;
756  }
757
758  return value;
759}
760
761/// Sets the value of a global or local (buffer, window) option.
762///
763/// @param[in]   to         Pointer to buffer or window for local option value.
764/// @param       scope      Option scope. See OptScope in option.h.
765/// @param       name       The option name.
766/// @param       value      New option value.
767/// @param[out]  err        Details of an error that may have occurred.
768static void set_option_to(uint64_t channel_id, void *to, OptScope scope, String name, Object value,
769                          Error *err)
770{
771  VALIDATE_S(name.size > 0, "option name", "<empty>", {
772    return;
773  });
774
775  OptIndex opt_idx = find_option(name.data);
776  VALIDATE_S(opt_idx != kOptInvalid, "option name", name.data, {
777    return;
778  });
779
780  // Only scalar Objects (nil/boolean/number/string) are valid option values.
781  // Don't use `name` in the error message here, because `name` can be any String.
782  // No need to check the option's value type here; the option setter handles that.
783  const bool valid = value.type == kObjectTypeNil || value.type == kObjectTypeBoolean
784                     || value.type == kObjectTypeInteger || value.type == kObjectTypeString;
785  VALIDATE_EXP(valid, "value", "valid option type", api_typename(value.type), {
786    return;
787  });
788  Object optval = value;
789
790  // For global-win-local options -> setlocal
791  // For        win-local options -> setglobal and setlocal (opt_flags == 0)
792  const int opt_flags
793    = (scope == kOptScopeWin && !option_has_scope(opt_idx, kOptScopeGlobal))
794      ? 0
795      : ((scope == kOptScopeGlobal) ? OPT_GLOBAL : OPT_LOCAL);
796
797  WITH_SCRIPT_CONTEXT(channel_id, {
798    set_option_value_for(opt_idx, optval, opt_flags, scope, to, true, err);
799  });
800}
801
802/// @deprecated Use nvim_exec_lua() instead.
803///
804/// Calls many API methods atomically.
805///
806/// This has two main usages:
807/// 1. To perform several requests from an async context atomically, i.e.
808///    without interleaving redraws, RPC requests from other clients, or user
809///    interactions (however API methods may trigger autocommands or event
810///    processing which have such side effects, e.g. |:sleep| may wake timers).
811/// 2. To minimize RPC overhead (roundtrips) of a sequence of many requests.
812///
813/// @param channel_id
814/// @param calls an array of calls, where each call is described by an array
815///              with two elements: the request name, and an array of arguments.
816/// @param[out] err Validation error details (malformed `calls` parameter),
817///             if any. Errors from batched calls are given in the return value.
818///
819/// @return Array of two elements. The first is an array of return
820/// values. The second is NIL if all calls succeeded. If a call resulted in
821/// an error, it is a three-element array with the zero-based index of the call
822/// which resulted in an error, the error type and the error message. If an
823/// error occurred, the values from all preceding calls will still be returned.
824Array nvim_call_atomic(uint64_t channel_id, Array calls, Arena *arena, Error *err)
825  FUNC_API_SINCE(1) FUNC_API_DEPRECATED_SINCE(12) FUNC_API_REMOTE_ONLY
826{
827  Array rv = arena_array(arena, 2);
828  Array results = arena_array(arena, calls.size);
829  Error nested_error = ERROR_INIT;
830
831  size_t i;  // also used for freeing the variables
832  for (i = 0; i < calls.size; i++) {
833    VALIDATE_T("'calls' item", kObjectTypeArray, calls.items[i].type, {
834      goto theend;
835    });
836    Array call = calls.items[i].data.array;
837    VALIDATE_EXP((call.size == 2), "'calls' item", "2-item Array", NULL, {
838      goto theend;
839    });
840    VALIDATE_T("name", kObjectTypeString, call.items[0].type, {
841      goto theend;
842    });
843    String name = call.items[0].data.string;
844    VALIDATE_T("call args", kObjectTypeArray, call.items[1].type, {
845      goto theend;
846    });
847    Array args = call.items[1].data.array;
848
849    MsgpackRpcRequestHandler handler =
850      msgpack_rpc_get_handler_for(name.data,
851                                  name.size,
852                                  &nested_error);
853
854    if (ERROR_SET(&nested_error)) {
855      break;
856    }
857
858    Object result = handler.fn(channel_id, args, arena, &nested_error);
859    if (ERROR_SET(&nested_error)) {
860      // error handled after loop
861      break;
862    }
863    // TODO(bfredl): wasteful copy. It could be avoided to encoding to msgpack
864    // directly here. But `result` might become invalid when next api function
865    // is called in the loop.
866    ADD_C(results, copy_object(result, arena));
867    if (handler.ret_alloc) {
868      api_free_object(result);
869    }
870  }
871
872  ADD_C(rv, ARRAY_OBJ(results));
873  if (ERROR_SET(&nested_error)) {
874    Array errval = arena_array(arena, 3);
875    ADD_C(errval, INTEGER_OBJ((Integer)i));
876    ADD_C(errval, INTEGER_OBJ(nested_error.type));
877    ADD_C(errval, STRING_OBJ(copy_string(cstr_as_string(nested_error.msg), arena)));
878    ADD_C(rv, ARRAY_OBJ(errval));
879  } else {
880    ADD_C(rv, NIL);
881  }
882
883theend:
884  api_clear_error(&nested_error);
885  return rv;
886}
887
888/// @deprecated
889///
890/// @param channel_id Channel id (passed automatically by the dispatcher)
891/// @param event      Event type string
892void nvim_subscribe(uint64_t channel_id, String event)
893// XXX: c_grammar.lua is order-sensitive.
894  FUNC_API_SINCE(1) FUNC_API_DEPRECATED_SINCE(13) FUNC_API_REMOTE_ONLY
895{
896  // Does nothing. `rpcnotify(0,…)` broadcasts to all channels, there are no "subscriptions".
897}
898
899/// @deprecated
900///
901/// @param channel_id Channel id (passed automatically by the dispatcher)
902/// @param event      Event type string
903void nvim_unsubscribe(uint64_t channel_id, String event)
904// XXX: c_grammar.lua is order-sensitive.
905  FUNC_API_SINCE(1) FUNC_API_DEPRECATED_SINCE(13) FUNC_API_REMOTE_ONLY
906{
907  // Does nothing. `rpcnotify(0,…)` broadcasts to all channels, there are no "subscriptions".
908}
909
910enum { LINE_BUFFER_MIN_SIZE = 4096, };
911
912/// Writes a message to vim output or error buffer. The string is split
913/// and flushed after each newline. Incomplete lines are kept for writing
914/// later.
915///
916/// @param message  Message to write
917/// @param to_err   true: message is an error (uses `emsg` instead of `msg`)
918/// @param writeln  Append a trailing newline
919static void write_msg(String message, bool to_err, bool writeln)
920{
921  static StringBuilder out_line_buf = KV_INITIAL_VALUE;
922  static StringBuilder err_line_buf = KV_INITIAL_VALUE;
923  StringBuilder *line_buf = to_err ? &err_line_buf : &out_line_buf;
924
925  msg_ext_no_fast();
926#define PUSH_CHAR(c) \
927  if (kv_max(*line_buf) == 0) { \
928    kv_resize(*line_buf, LINE_BUFFER_MIN_SIZE); \
929  } \
930  if (c == NL) { \
931    kv_push(*line_buf, NUL); \
932    if (to_err) { \
933      emsg(line_buf->items); \
934    } else { \
935      msg(line_buf->items, 0); \
936    } \
937    if (msg_silent == 0) { \
938      msg_didout = true; \
939    } \
940    kv_drop(*line_buf, kv_size(*line_buf)); \
941    kv_resize(*line_buf, LINE_BUFFER_MIN_SIZE); \
942  } else if (c == NUL) { \
943    kv_push(*line_buf, NL); \
944  } else { \
945    kv_push(*line_buf, c); \
946  }
947
948  no_wait_return++;
949  for (uint32_t i = 0; i < message.size; i++) {
950    if (got_int) {
951      break;
952    }
953    PUSH_CHAR(message.data[i]);
954  }
955  if (writeln) {
956    PUSH_CHAR(NL);
957  }
958  no_wait_return--;
959  msg_end();
960}
961
962/// @deprecated
963///
964/// @param str Message
965void nvim_out_write(String str)
966  FUNC_API_SINCE(1) FUNC_API_DEPRECATED_SINCE(13)
967{
968  write_msg(str, false, false);
969}
970
971/// @deprecated
972///
973/// @param str Message
974void nvim_err_write(String str)
975  FUNC_API_SINCE(1) FUNC_API_DEPRECATED_SINCE(13)
976{
977  write_msg(str, true, false);
978}
979
980/// @deprecated
981///
982/// @param str Message
983void nvim_err_writeln(String str)
984  FUNC_API_SINCE(1) FUNC_API_DEPRECATED_SINCE(13)
985{
986  write_msg(str, true, true);
987}
988
989/// @deprecated
990///
991/// Use `nvim_echo` or `nvim_exec_lua("vim.notify(...)", ...)` instead.
992///
993/// @param msg        Message to display to the user
994/// @param log_level  The log level
995/// @param opts       Reserved for future use.
996/// @param[out] err   Error details, if any
997Object nvim_notify(String msg, Integer log_level, Dict opts, Arena *arena, Error *err)
998  FUNC_API_SINCE(7) FUNC_API_DEPRECATED_SINCE(13)
999{
1000  MAXSIZE_TEMP_ARRAY(args, 3);
1001  ADD_C(args, STRING_OBJ(msg));
1002  ADD_C(args, INTEGER_OBJ(log_level));
1003  ADD_C(args, DICT_OBJ(opts));
1004
1005  return NLUA_EXEC_STATIC("return vim.notify(...)", args, kRetObject, arena, err);
1006}
1007
1008/// @deprecated
1009///
1010/// Use |nvim_win_resize()| instead, e.g., nvim_win_resize(win, -1, height)
1011///
1012/// Sets the window height.
1013///
1014/// @param win   |window-ID|, or 0 for current window
1015/// @param height   Height as a count of rows
1016/// @param[out] err Error details, if any
1017void nvim_win_set_height(Window win, Integer height, Error *err)
1018  FUNC_API_SINCE(1) FUNC_API_DEPRECATED_SINCE(15)
1019{
1020  win_T *w = find_window_by_handle(win, err);
1021
1022  if (!w) {
1023    return;
1024  }
1025
1026  TRY_WRAP(err, {
1027    win_setheight_win((int)height, w, true);
1028  });
1029}
1030
1031/// @deprecated
1032///
1033/// Use |nvim_win_resize()| instead, e.g., nvim_win_resize(win, width, -1)
1034///
1035/// Sets the window width. This will only succeed if the screen is split
1036/// vertically.
1037///
1038/// @param win   |window-ID|, or 0 for current window
1039/// @param width    Width as a count of columns
1040/// @param[out] err Error details, if any
1041void nvim_win_set_width(Window win, Integer width, Error *err)
1042  FUNC_API_SINCE(1) FUNC_API_DEPRECATED_SINCE(15)
1043{
1044  win_T *w = find_window_by_handle(win, err);
1045
1046  if (!w) {
1047    return;
1048  }
1049
1050  TRY_WRAP(err, {
1051    win_setwidth_win((int)width, w, true);
1052  });
1053}