-
Notifications
You must be signed in to change notification settings - Fork 296
Expand file tree
/
Copy pathmini-misc.txt
More file actions
431 lines (339 loc) · 17.3 KB
/
mini-misc.txt
File metadata and controls
431 lines (339 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
*mini.misc* Miscellaneous functions
MIT License Copyright (c) 2021 Evgeni Chasnovski
------------------------------------------------------------------------------
*MiniMisc*
Features the following functions:
- |MiniMisc.bench_time()| to benchmark function execution time.
Useful in combination with `stat_summary()`.
- |MiniMisc.log_add()|, |MiniMisc.log_show()| and other helper functions to work
with a special in-memory log array. Useful when debugging Lua code.
- |MiniMisc.put()| and |MiniMisc.put_text()| to pretty print its arguments
into command line and current buffer respectively.
- |MiniMisc.resize_window()| to resize current window to its editable width.
- |MiniMisc.safely()| to execute a function on a condition and warn on error.
Useful to organize |init.lua| in fail-safe sections with simple lazy loading.
- |MiniMisc.setup_auto_root()| to set up automated change of current directory.
- |MiniMisc.setup_termbg_sync()| to set up terminal background synchronization
(removes possible "frame" around current Neovim instance).
- |MiniMisc.setup_restore_cursor()| to set up automated restoration of
cursor position on file reopen.
- |MiniMisc.stat_summary()| to compute summary statistics of numerical array.
Useful in combination with `bench_time()`.
- |MiniMisc.tbl_head()| and |MiniMisc.tbl_tail()| to return "first" and "last"
elements of table.
- |MiniMisc.zoom()| to zoom in and out of a buffer, making it full screen
in a floating window.
- And more.
# Setup ~
This module doesn't need setup, but it can be done to improve usability.
Setup with `require('mini.misc').setup({})` (replace `{}` with your
`config` table). It will create global Lua table `MiniMisc` which you can
use for scripting or manually (with `:lua MiniMisc.*`).
See |MiniMisc.config| for `config` structure and default values.
This module doesn't have runtime options, so using `vim.b.minimisc_config`
will have no effect here.
------------------------------------------------------------------------------
*MiniMisc.setup()*
`MiniMisc.setup`({config})
Module setup
Parameters ~
{config} `(table|nil)` Module config table. See |MiniMisc.config|.
Usage ~
>lua
require('mini.misc').setup() -- use default config
-- OR
require('mini.misc').setup({}) -- replace {} with your config table
<
------------------------------------------------------------------------------
*MiniMisc.config*
`MiniMisc.config`
Defaults ~
>lua
MiniMisc.config = {
-- Array of fields to make global (to be used as independent variables)
make_global = { 'put', 'put_text' },
}
<
------------------------------------------------------------------------------
*MiniMisc.bench_time()*
`MiniMisc.bench_time`({f}, {n}, {...})
Execute `f` several times and time how long it took
Parameters ~
{f} `(function)` Function which execution to benchmark.
{n} `(number|nil)` Number of times to execute `f(...)`. Default: 1.
{...} `(any)` Arguments when calling `f`.
Return ~
`(...)` Table with durations (in seconds; up to nanoseconds) and
output of (last) function execution.
------------------------------------------------------------------------------
*MiniMisc.get_gutter_width()*
`MiniMisc.get_gutter_width`({win_id})
Compute width of gutter (info column on the left of the window)
Parameters ~
{win_id} `(number|nil)` Window identifier (see |win_getid()|) for which gutter
width is computed. Default: 0 for current.
------------------------------------------------------------------------------
*MiniMisc.log_add()*
`MiniMisc.log_add`({desc}, {state}, {opts})
Add an entry to the in-memory log array
Useful when trying to debug a Lua code (like Neovim config or plugin).
Use this instead of ad-hoc `print()` statements.
Each entry is a table with the following fields:
- <desc> `(any)` - entry description. Usually a string describing a place
in the code.
- <state> `(any)` - data about current state. Usually a table.
- <timestamp> `(number)` - a timestamp of when the entry was added. A number of
milliseconds since the in-memory log was initiated (after |MiniMisc.setup()|
or |MiniMisc.log_clear()|). Useful during profiling.
Parameters ~
{desc} `(any)` Entry description.
{state} `(any)` Data about current state.
{opts} `(table|nil)` Options. Possible fields:
- <deepcopy> - (boolean) Whether to apply |vim.deepcopy| to the {state}.
Usually helpful to record the exact state during code execution and avoid
side effects of tables being changed in-place. Default `true`.
Usage ~
>lua
local t = { a = 1 }
MiniMisc.log_add('before', { t = t }) -- Will show `t = { a = 1 }` state
t.a = t.a + 1
MiniMisc.log_add('after', { t = t }) -- Will show `t = { a = 2 }` state
-- Use `:lua MiniMisc.log_show()` or `:=MiniMisc.log_get()` to see the log
<
See also ~
- |MiniMisc.log_get()| to get log array
- |MiniMisc.log_show()| to show log array in the dedicated buffer
- |MiniMisc.log_clear()| to clear the log array
------------------------------------------------------------------------------
*MiniMisc.log_get()*
`MiniMisc.log_get`()
Get log array
Return ~
`(table[])` Log array. Returned as is, without |vim.deepcopy()|.
See also ~
- |MiniMisc.log_add()| to add to the log array
------------------------------------------------------------------------------
*MiniMisc.log_show()*
`MiniMisc.log_show`()
Show log array in a scratch buffer
See also ~
- |MiniMisc.log_add()| to add to the log array
------------------------------------------------------------------------------
*MiniMisc.log_clear()*
`MiniMisc.log_clear`()
Clear log array
This also sets a new starting point for entry timestamps.
See also ~
- |MiniMisc.log_add()| to add to the log array
------------------------------------------------------------------------------
*MiniMisc.put()*
`MiniMisc.put`({...})
Print Lua objects in command line
Parameters ~
{...} `(any)` Any number of objects to be printed each on separate line.
------------------------------------------------------------------------------
*MiniMisc.put_text()*
`MiniMisc.put_text`({...})
Print Lua objects in current buffer
Parameters ~
{...} `(any)` Any number of objects to be printed each on separate line.
------------------------------------------------------------------------------
*MiniMisc.resize_window()*
`MiniMisc.resize_window`({win_id}, {text_width})
Resize window to have exact number of editable columns
Parameters ~
{win_id} `(number|nil)` Window identifier (see |win_getid()|) to be resized.
Default: 0 for current.
{text_width} `(number|nil)` Number of editable columns resized window will
display. Default: first element of 'colorcolumn' or otherwise 'textwidth'
(using screen width as its default but not more than 79).
------------------------------------------------------------------------------
*MiniMisc.safely()*
`MiniMisc.safely`({when}, {f})
Execute a function on a condition and warn on error
Input function is executed exactly once. Its possible error is captured and is
shown as a |vim.notify()| warning.
Useful to organize |init.lua| in fail-safe sections with simple lazy loading.
Parameters ~
{when} `(string)` When to execute a function. One of:
- `'now'` - immediately.
- `'later'` - queue to be executed soon without blocking the execution of next
code in file. Queued functions are executed in order they are added.
- `'delay:<number>'` - after a specified delay with |vim.defer_fn()|.
- `'event:<events>'` - on whichever specified event is triggered first.
- `'event:<events>~<patterns>` - same as above, but events must match
specified |autocmd-pattern|.
- `'filetype:<filetypes>'` - same as `'event:FileType~<filetypes>'`, but follow
successful function execution with |filetype-detect| for all normal buffers
(if new |ftdetect| scripts were added) and sourcing |ftplugin| (for buffers
matching `<filetypes>`). Intended to be used for loading "language plugins".
{f} `(function)` Function to execute (without arguments).
Usage ~
>lua
MiniMisc.safely('later', function()
vim.notify('This will be executed after the next "now" call')
end)
MiniMisc.safely('now', function() error('This will be a warning') end)
MiniMisc.safely('event:InsertEnter', function()
require('mini.completion').setup()
end)
MiniMisc.safely('event:CmdlineEnter~/', function()
vim.notify('Start searching for the first time')
end)
MiniMisc.safely('filetype:tex,plaintex', function()
-- Load plugin to improve writing LaTeX
end)
<
------------------------------------------------------------------------------
*MiniMisc.setup_auto_root()*
`MiniMisc.setup_auto_root`({names}, {fallback})
Set up automated change of current directory
What it does:
- Creates autocommand which on every |BufEnter| event with |MiniMisc.find_root()|
finds root directory for current buffer file and sets |current-directory|
to it (using |chdir()|).
- Resets |'autochdir'| to `false`.
Parameters ~
{names} `(table|function|nil)` Forwarded to |MiniMisc.find_root()|.
{fallback} `(function|nil)` Forwarded to |MiniMisc.find_root()|.
Usage ~
>lua
require('mini.misc').setup()
MiniMisc.setup_auto_root()
<
------------------------------------------------------------------------------
*MiniMisc.find_root()*
`MiniMisc.find_root`({buf_id}, {names}, {fallback})
Find root directory
Based on a buffer name (full path to file opened in a buffer) find a root
directory. If buffer is not associated with file, returns `nil`.
Root directory is a directory containing at least one of pre-defined files.
It is searched using |vim.fs.find()| with `upward = true` starting from
directory of current buffer file until first occurrence of root file(s).
Notes:
- Uses directory path caching to speed up computations. This means that no
changes in root directory will be detected after directory path was already
used in this function. Reload Neovim to account for that.
Parameters ~
{buf_id} `(number|nil)` Buffer identifier (see |bufnr()|) to use.
Default: 0 for current.
{names} `(table|function|nil)` Array of file names or a callable used to
identify a root directory. Forwarded to |vim.fs.find()|.
Default: `{ '.git', 'Makefile' }`.
{fallback} `(function|nil)` Callable fallback to use if no root is found
with |vim.fs.find()|. Will be called with a buffer path and should return
a valid directory path.
------------------------------------------------------------------------------
*MiniMisc.setup_termbg_sync()*
`MiniMisc.setup_termbg_sync`({opts})
Set up terminal background synchronization
What it does:
- Checks if terminal emulator supports OSC 11 control sequence through
appropriate `stdout`. Stops if not.
- Creates autocommands for |ColorScheme| and |VimResume| events, which
change terminal background to have same color as |guibg| of |hl-Normal|.
- Creates autocommands for |VimLeavePre| and |VimSuspend| events which set
terminal background back to its original color.
- Synchronizes background immediately to allow not depend on loading order.
Primary use case is to remove possible "frame" around current Neovim instance
which appears if Neovim's |hl-Normal| background color differs from what is
used by terminal emulator itself.
Works only on Neovim>=0.10.
Parameters ~
{opts} `(table|nil)` Options. Possible fields:
- <explicit_reset> `(boolean)` - whether to reset terminal background by
explicitly setting it to the color it had when this function was called.
Set to `true` if terminal emulator doesn't support OSC 111 control sequence.
Default: `false`.
------------------------------------------------------------------------------
*MiniMisc.setup_restore_cursor()*
`MiniMisc.setup_restore_cursor`({opts})
Restore cursor position on file open
When reopening a file this will make sure the cursor is placed back to the
position where you left before. This implements |restore-cursor| in a nicer way.
File should have a recognized file type (see 'filetype') and be opened in
a normal buffer (see 'buftype').
Note: it relies on file mark data stored in 'shadafile' (see |shada-f|).
Be sure to enable it.
Parameters ~
{opts} `(table|nil)` Options. Possible fields:
- <center> - (boolean) Center the window after we restored the cursor.
Default: `true`.
- <ignore_filetype> - Array with file types to be ignored (see 'filetype').
Default: `{ "gitcommit", "gitrebase" }`.
Usage ~
>lua
require('mini.misc').setup_restore_cursor()
<
------------------------------------------------------------------------------
*MiniMisc.stat_summary()*
`MiniMisc.stat_summary`({t})
Compute summary statistics of numerical array
This might be useful to compute summary of time benchmarking with
|MiniMisc.bench_time()|.
Parameters ~
{t} `(table)` Array (table suitable for `ipairs`) of numbers.
Return ~
`(table)` Table with summary values under following keys (may be
extended in the future): <maximum>, <mean>, <median>, <minimum>, <n>
(number of elements), <sd> (sample standard deviation).
------------------------------------------------------------------------------
*MiniMisc.tbl_head()*
`MiniMisc.tbl_head`({t}, {n})
Return "first" elements of table as decided by `pairs`
Note: order of elements might vary.
Parameters ~
{t} `(table)` Input table.
{n} `(number|nil)` Maximum number of first elements. Default: 5.
Return ~
`(table)` Table with at most `n` first elements of `t` (with same keys).
------------------------------------------------------------------------------
*MiniMisc.tbl_tail()*
`MiniMisc.tbl_tail`({t}, {n})
Return "last" elements of table as decided by `pairs`
This function makes two passes through elements of `t`:
- First to count number of elements.
- Second to construct result.
Note: order of elements might vary.
Parameters ~
{t} `(table)` Input table.
{n} `(number|nil)` Maximum number of last elements. Default: 5.
Return ~
`(table)` Table with at most `n` last elements of `t` (with same keys).
------------------------------------------------------------------------------
*MiniMisc.use_nested_comments()*
`MiniMisc.use_nested_comments`({buf_id})
Add possibility of nested comment leader
This works by parsing 'commentstring' buffer option, extracting
non-whitespace comment leader (symbols on the left of commented line), and
locally modifying 'comments' option (by prepending `n:<leader>`). Does
nothing if 'commentstring' is empty or has comment symbols both in front
and back (like "/*%s*/").
Nested comment leader added with this function is useful for formatting
nested comments. For example, have in Lua "first-level" comments with '--'
and "second-level" comments with '----'. With nested comment leader second
type can be formatted with `gq` in the same way as first one.
Recommended usage is with |autocmd|: >lua
local use_nested_comments = function() MiniMisc.use_nested_comments() end
vim.api.nvim_create_autocmd('BufEnter', { callback = use_nested_comments })
<
Note: for most filetypes 'commentstring' option is added only when buffer
with this filetype is entered, so using non-current `buf_id` can not lead
to desired effect.
Parameters ~
{buf_id} `(number|nil)` Buffer identifier (see |bufnr()|) in which function
will operate. Default: 0 for current.
------------------------------------------------------------------------------
*MiniMisc.zoom()*
`MiniMisc.zoom`({buf_id}, {config})
Zoom in and out of a buffer, making it full screen in a floating window
This function is useful when working with multiple windows but temporarily
needing to zoom into one to see more of the code from that buffer. Call it
again (without arguments) to zoom out.
Parameters ~
{buf_id} `(number|nil)` Buffer identifier (see |bufnr()|) to be zoomed.
Default: 0 for current.
{config} `(table|nil)` Optional config for window (as for |nvim_open_win()|).
Return ~
`(boolean)` Whether current buffer is zoomed in.
vim:tw=78:ts=8:noet:ft=help:norl: