]> git.lizzy.rs Git - micro.git/blob - runtime/help/plugins.md
051c33c354411dde43f910876a643045612fae1c
[micro.git] / runtime / help / plugins.md
1 # Plugins
2
3 Micro supports creating plugins with a simple Lua system. Every plugin has a
4 main script which is run at startup which should be placed in 
5 `~/.config/micro/plugins/pluginName/pluginName.lua`.
6
7 There are a number of callback functions which you can create in your
8 plugin to run code at times other than startup. The naming scheme is
9 `onAction(view)`. For example a function which is run every time the user saves
10 the buffer would be:
11
12 ```lua
13 function onSave(view)
14     ...
15     return false
16 end
17 ```
18
19 The `view` variable is a reference to the view the action is being executed on.
20 This is almost always the current view, which you can get with `CurView()` as well.
21
22 All available actions are listed in the keybindings section of the help.
23
24 These functions should also return a boolean specifying whether the view
25 should be relocated to the cursor or not after the action is complete.
26
27 Note that these callbacks occur after the action has been completed. If you
28 want a callback before the action is executed, use `preAction()`. In this case
29 the boolean returned specifies whether or not the action should be executed
30 after the lua code completes.
31
32 ---
33
34 There are a number of functions and variables that are available to you in
35 oder to access the inner workings of micro. Here is a list (the type signatures
36 for functions are given using Go's type system):
37
38 * `OS`: variable which gives the OS micro is currently running on (this is the same
39 as Go's GOOS variable, so `darwin`, `windows`, `linux`, `freebsd`...)
40
41 * `tabs`: a list of all the tabs currently in use
42
43 * `curTab`: the index of the current tabs in the tabs list
44
45 * `messenger`: lets you send messages to the user or create prompts
46
47 * `GetOption(name string)`: returns the value of the requested option
48
49 * `AddOption(name string, value interface{})`: sets the given option with the given
50    value (`interface{}` means any type in Go).
51
52 * `SetOption(option, value string)`: sets the given option to the value. This will
53    set the option globally, unless it is a local only option.
54
55 * `SetLocalOption(option, value string, buffer *Buffer)`: sets the given option to
56    the value locally in the given buffer.
57
58 * `BindKey(key, action string)`: binds `key` to `action`.
59
60 * `MakeCommand(name, function string, completions ...Completion)`: 
61    creates a command with `name` which will call `function` when executed.
62    Use 0 for completions to get NoCompletion.
63
64 * `CurView()`: returns the current view
65
66 * `HandleCommand(cmd string)`: runs the given command
67
68 * `HandleShellCommand(shellCmd string, interactive bool)`: runs the given shell
69    command
70
71 * `JobStart(cmd string, onStdout, onStderr, onExit string, userargs ...string)`:
72    Starts running the given shell command in the background. `onStdout` `onStderr` and `onExit`
73    are callbacks to lua functions which will be called when the given actions happen
74    to the background process.
75    `userargs` are the arguments which will get passed to the callback functions
76
77 * `JobSend(cmd *exec.Cmd, data string)`: send a string into the stdin of the job process
78
79 * `JobStop(cmd *exec.Cmd)`: kill a job
80
81 This may seem like a small list of available functions but some of the objects
82 returned by the functions have many methods. `CurView()` returns a view object
83 which has all the actions which you can call. For example `CurView():Save(false)`.
84 You can see the full list of possible actions in the keybindings help topic.
85 The boolean on all the actions indicates whether or not the lua callbacks should
86 be run. I would recommend generally sticking to false when making a plugin to
87 avoid recursive problems, for example if you call `CurView():Save(true)` in `onSave()`.
88 Just use `CurView():Save(false)` so that it won't call `onSave()` again.
89
90 Using the view object, you can also access the buffer associated with that view
91 by using `CurView().Buf`, which lets you access the `FileType`, `Path`, `Name`...
92
93 The possible methods which you can call using the `messenger` variable are:
94
95 * `messenger.Message(msg ...interface{})`
96 * `messenger.Error(msg ...interface{})`
97 * `messenger.YesNoPrompt(prompt string) (bool, bool)`
98 * `messenger.Prompt(prompt, historyType string, completionType Completion) (string, bool)`
99
100 If you want a standard prompt, just use `messenger.Prompt(prompt, "", 0)`
101
102 # Default plugins
103
104 For examples of plugins, see the default plugins `linter`, `go`, and `autoclose`.
105 They are stored in Micro's github repository [here](https://github.com/zyedidia/micro/tree/master/runtime/plugins).