├── .editorconfig ├── LICENSE ├── README.md ├── demo.gif └── lua └── zincoxide.lua /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | end_of_line = lf 5 | insert_final_newline = true 6 | indent_style = space 7 | indent_size = 2 8 | max_line_length = 80 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2024 Yaksh Bariya and contributors 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ZincOxide (ZnO) 2 | 3 | Supercharge your neovim cd command with new your existing shell's `zoxide`. 4 | 5 | ![Demo](demo.gif) 6 | 7 | ## Installation (lazy.nvim) 8 | 9 | ```lua 10 | { 11 | -- You can also use the codeberg mirror if you want to use the plugin without relying on GitHub 12 | -- "https://codeberg.org/CodingThunder/zincoxide.git" -- for HTTPS 13 | -- "git@codeberg.org:CodingThunder/zincoxide.git" -- for SSH 14 | -- NOTE: the username on both github and codeberg are different 15 | "thunder-coding/zincoxide", 16 | opts = { 17 | -- name of zoxide binary in your "$PATH" or path to the binary 18 | -- the command is executed using vim.fn.system() 19 | -- eg. "zoxide" or "/usr/bin/zoxide" 20 | zincoxide_cmd = "zoxide", 21 | -- Kinda experimental as of now 22 | complete = true, 23 | -- Available options { "tabs", "window", "global" } 24 | behaviour = "tabs", 25 | }, 26 | cmd = { "Z", "Zg", "Zt", "Zw" }, 27 | } 28 | ``` 29 | -------------------------------------------------------------------------------- /demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thunder-coding/zincoxide/fd0b6f890baa31d302b6c1726e00c06efdd50a13/demo.gif -------------------------------------------------------------------------------- /lua/zincoxide.lua: -------------------------------------------------------------------------------- 1 | local M = {} 2 | 3 | local default_opts = { 4 | zoxide_cmd = "zoxide", 5 | complete = true, 6 | behaviour = "tabs", 7 | } 8 | 9 | function M.setup(opts) 10 | M.opts = {} 11 | 12 | -- If user has not provided any configuration for the option, use defaults 13 | for _, opt in ipairs({ "zoxide_cmd", "complete", "behaviour" }) do 14 | if opts[opt] == nil then 15 | M.opts[opt] = default_opts[opt] 16 | else 17 | M.opts[opt] = opts[opt] 18 | end 19 | end 20 | 21 | local copts = { nargs = "*", bang = true } 22 | 23 | -- Only provide completions if opt.complete = true 24 | if M.opts.complete then 25 | copts.complete = M.complete 26 | end 27 | 28 | -- Register the command ':Z' 29 | vim.api.nvim_create_user_command("Z", function(opts_) 30 | M.cd(opts_, M.opts.behaviour) 31 | end, copts) 32 | vim.api.nvim_create_user_command("Zg", function(opts_) 33 | M.cd(opts_, "global") 34 | end, copts) 35 | vim.api.nvim_create_user_command("Zw", function(opts_) 36 | M.cd(opts_, "window") 37 | end, copts) 38 | vim.api.nvim_create_user_command("Zt", function(opts_) 39 | M.cd(opts_, "tabs") 40 | end, copts) 41 | end 42 | 43 | -- Provide completions for the cmdline. Only return completions if number of 44 | -- arguments is 1 45 | -- We can't complete multiple arguments as generally all the arguments are 46 | -- parts of strings of entries in zoxide's database 47 | function M.complete(arglead, cmdline, _) 48 | local current_path = vim.fn.getcwd() 49 | local cmd = 50 | { M.opts.zoxide_cmd, "query", "-l", "--exclude=" .. current_path, "--" } 51 | local args = vim.api.nvim_parse_cmd(cmdline, {}).args 52 | local home = os.getenv("HOME") .. "/" 53 | 54 | -- If number of arguments passed to ':Z' is greater than 1, we cannot provide 55 | -- completions as you can pass almost anything to 'z', and anyways z doesn't 56 | -- even provide completions so we are doing more than what's needed :) 57 | if #args > 1 or (#args == 1 and #arglead == 0) then 58 | return {} 59 | end 60 | 61 | local dir_completes = {} 62 | 63 | if #args == 0 then 64 | local dir = vim.loop.fs_opendir(current_path, nil) 65 | if dir == nil then 66 | goto end_dirsearch 67 | end 68 | while true do 69 | local entry = vim.loop.fs_readdir(dir) 70 | -- We've read everything 71 | if entry == nil then 72 | vim.loop.fs_closedir(dir) 73 | break 74 | end 75 | 76 | -- Only add to completions if is a directory, also if it is a symlink, try to resolve it and add it too if it is a directory 77 | local name 78 | if entry[1].type == "directory" then 79 | name = entry[1].name 80 | elseif entry[1].type == "link" then 81 | local link_dir = 82 | vim.loop.fs_opendir(current_path .. "/" .. entry[1].name) 83 | if link_dir ~= nil then 84 | name = entry[1].name 85 | vim.loop.fs_closedir(link_dir) 86 | else 87 | goto continue 88 | end 89 | else 90 | goto continue 91 | end 92 | dir_completes[#dir_completes + 1] = name 93 | ::continue:: 94 | end 95 | ::end_dirsearch:: 96 | end 97 | 98 | -- If exactly one argument is passed, we can do our best to complete its 99 | -- Completion item can be a relative directory, absolute directory or any of 100 | -- the entries in zoxide's database 101 | if #args == 1 then 102 | -- Get the last directory we can make out of from the string 103 | local dir_string = args[1]:match(".*/") or "./" 104 | -- Expand the tilde expression to home 105 | if home ~= nil and vim.startswith(dir_string, "~/") then 106 | dir_string = home .. string.sub(dir_string, 3) 107 | end 108 | local dir = vim.loop.fs_opendir(dir_string, nil) 109 | -- If this is a directory, read it and provide the completions for directories in the directory which the user has typed out 110 | if dir ~= nil then 111 | while true do 112 | local entry = vim.loop.fs_readdir(dir) 113 | -- We've read everything 114 | if entry == nil then 115 | vim.loop.fs_closedir(dir) 116 | break 117 | end 118 | -- Only add to completions if is a directory, also if it is a symlink, try to resolve it and add it too if it is a directory 119 | local name 120 | if entry[1].type == "directory" then 121 | name = entry[1].name 122 | elseif entry[1].type == "link" then 123 | local link_dir = vim.loop.fs_opendir(dir_string .. entry[1].name) 124 | if link_dir ~= nil then 125 | name = entry[1].name 126 | vim.loop.fs_closedir(link_dir) 127 | else 128 | goto continue 129 | end 130 | else 131 | goto continue 132 | end 133 | -- fuzzy search on steroids 134 | local x = ".*" .. table.concat(vim.split(args[1], ""), ".*") .. ".*" 135 | -- Filter completion results to options that match the user string 136 | if string.match(dir_string .. name, x) then 137 | dir_completes[#dir_completes + 1] = dir_string .. name 138 | end 139 | ::continue:: 140 | end 141 | end 142 | end 143 | -- Do not pass any arguments to zoxide since we are using a hacky fuzzy search whereas zoxide doesn't do fuzzy search 144 | local zoxide_output = vim.fn.system(cmd) 145 | -- Although this should not happen, but let's just handle it just in case 146 | if vim.v.shell_error ~= 0 then 147 | error(zoxide_output) 148 | end 149 | -- Read the directories in zoxide database and separate each entry 150 | local zoxide_entries = 151 | vim.split(zoxide_output, "\n", { plain = true, trimempty = false }) 152 | local completions = {} 153 | -- we just converted the tilde earlier to HOME, as vim.loop doesn't understand it. So let's convert back 154 | for _, entry in pairs(dir_completes) do 155 | if home ~= nil and vim.startswith(entry, home) then 156 | entry = "~/" .. string.sub(entry, #home + 1) 157 | end 158 | completions[#completions + 1] = entry 159 | end 160 | -- zoxide will return full path, so let's just replace the user directory with shorthand tilde notation to make the suggestions size smaller on screen. 161 | for _, entry in pairs(zoxide_entries) do 162 | if home ~= nil and vim.startswith(entry, home) then 163 | entry = "~/" .. string.sub(entry, #home + 1) 164 | end 165 | -- Fuzzy search on steroids part2 166 | if 167 | #args == 1 168 | and string.match( 169 | entry, 170 | ".*" .. table.concat(vim.split(args[1], ""), ".*") .. ".*" 171 | ) 172 | then 173 | completions[#completions + 1] = entry 174 | end 175 | end 176 | -- Finally return the completions back 177 | return completions 178 | end 179 | 180 | -- Resolve the argument(s) provided to paths which can be cded into 181 | function M.resolve(args) 182 | local home = os.getenv("HOME") 183 | 184 | -- If no argument is supplied, just send the user to home. 185 | -- Same for tilde, expand ourselves instead of relying on NeoVim to do that for us 186 | if #args == 0 or (#args == 1 and args[1] == "~") then 187 | return home 188 | end 189 | 190 | if #args == 1 then 191 | -- Return as is if it is relative path, do not try to verify if it exists or not 192 | if vim.startswith(args[1], "./") then 193 | return args[1] 194 | end 195 | 196 | -- Most shells expand the tilde expression themselves, so the behaviour which we get is the same as the below case, but zoxide doesn't understand the '~' character as "$HOME" as '~' is a valid character that can be used to name files and directories on a lot of filesystems, so have to expand it ourselves 197 | if home ~= nil and vim.startswith(args[1], "~/") then 198 | args[1] = home .. "/" .. string.sub(args[1], 3) 199 | end 200 | 201 | -- If an absolute path, first check if destination is a directory, if directory then return it 202 | -- If not a directory, then ask zoxide to resolve it 203 | -- If the path exists relative to the current directory, simply cd there instead of looking up in the database. 204 | local dir = vim.loop.fs_opendir(args[1]) 205 | if dir ~= nil then 206 | vim.loop.fs_closedir(dir) 207 | -- This is needed as relative directories need to be resolved for 208 | -- `zoxide add` to work properly, or else the matching one will not be the 209 | -- relative directory where we just cd into 210 | return vim.loop.fs_realpath(args[1]) 211 | end 212 | end 213 | 214 | -- Explicitly ask zoxide not to return the current directory itself 215 | -- Although this should not be needed 216 | local current_path = vim.fn.getcwd() 217 | local cmd = { M.opts.zoxide_cmd, "query", "--exclude=" .. current_path } 218 | cmd[#cmd + 1] = "--" 219 | 220 | -- Append the arguments provided to ':Z' to zoxide commannd 221 | for _, part in pairs(args) do 222 | cmd[#cmd + 1] = part 223 | end 224 | 225 | -- zoxide by default should only return one path on success. So just take the program output 226 | local path = vim.fn.system(cmd) 227 | 228 | -- If something went wrong, return a table with the path returned by the zoxide command and the exit code 229 | if vim.v.shell_error ~= 0 then 230 | return { path, vim.v.shell_error } 231 | end 232 | 233 | -- If last character is a newline character, just strip it off 234 | if string.sub(path, -1, -1) == "\n" then 235 | path = string.sub(path, 0, -2) 236 | end 237 | 238 | -- Return the resolved path 239 | return path 240 | end 241 | 242 | -- The actual function that is executed when ':Z' is called 243 | function M.cd(opts, behaviour) 244 | local target = M.resolve(opts.fargs) 245 | if type(target) ~= "string" then 246 | if opts.bang then 247 | return 248 | end 249 | ---@cast target -nil 250 | if target[2] == -1 then 251 | error("zoxide binary (" .. M.opts.zoxide_cmd .. ") could not be executed") 252 | elseif target[2] ~= 0 then 253 | error("zoxide error: '" .. target[1] .. "'") 254 | end 255 | end 256 | -- Increment score 257 | vim.fn.system({ M.opts.zoxide_cmd, "add", "--", target }) 258 | if behaviour == "tabs" then 259 | vim.cmd("tcd " .. target) 260 | elseif behaviour == "window" then 261 | vim.cmd("lcd " .. target) 262 | elseif behaviour == "global" then 263 | vim.cmd("cd " .. target) 264 | end 265 | end 266 | 267 | return M 268 | --------------------------------------------------------------------------------