Skip to content

Clipboard expansion fails when URL encoding is contained #701

@swnakamura

Description

@swnakamura

Describe the bug

In org-capture functionality, you can use %x template to expand clipboard item. However, it fails when the clipboard contains %-encoded string such as %20, which frequently appears in URL strings.

Steps to reproduce

  1. Make an item in org_capture_templates that contains %x in template. For example,
t = {
  description = 'Test',
  template = '* TODO [[%x][%?]]\n',
}
  1. Copy a text to the clipboard that contains URL encoding, e.g., nvim-orgmode%20is%20great!
  2. Capture the clipboard by <leader>oct (t is the template key).
  3. You'll encounter the error below:
[orgmode] .../nvim-orgmode/lua/orgmode/capture/template/init.lua:230: invalid capture index

Expected behavior

Properly open capture window according to the template, e.g., * TODO [[nvim-orgmode%20is%20great!][]]

This might be because the %-encoding in Lua regex is treated as capture. I could have the intended behavior by doubly encoding the string:

diff --git a/lua/orgmode/capture/template/init.lua b/lua/orgmode/capture/template/init.lua
index 0a01c03..941b4cb 100644
--- a/lua/orgmode/capture/template/init.lua
+++ b/lua/orgmode/capture/template/init.lua
@@ -15,7 +15,7 @@ local expansions = {
     return os.getenv('USER')
   end,
   ['%x'] = function()
-    return vim.fn.getreg('+')
+    return vim.fn.getreg('+'):gsub('%%', '%%%%')
   end,
   ['%t'] = function()
     return string.format('<%s>', Date.today():to_string())

But I'm not sure if this is the proper solution.

Emacs functionality

No response

Minimal init.lua

local tmp_dir = vim.env.TMPDIR or vim.env.TMP or vim.env.TEMP or '/tmp'
local nvim_root = tmp_dir .. '/nvim_orgmode'
local lazy_root = nvim_root .. '/lazy'
local lazypath = lazy_root .. '/lazy.nvim'

-- Install lazy.nvim if not already installed
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({
        'git',
        'clone',
        '--filter=blob:none',
        'https://github.com/folke/lazy.nvim.git',
        '--branch=stable', -- latest stable release
        lazypath,
    })
end
vim.opt.rtp:prepend(lazypath)

require('lazy').setup({
    {
        'nvim-orgmode/orgmode',
        dependencies = {
            { 'nvim-treesitter/nvim-treesitter', lazy = true },
        },
        event = 'VeryLazy',
        config = function()
            -- Load treesitter grammar for org
            require('orgmode').setup_ts_grammar()

            -- Setup treesitter
            require('nvim-treesitter.configs').setup({
                highlight = {
                    enable = true,
                },
                ensure_installed = { 'org' },
            })

            -- Setup orgmode
            require('orgmode').setup({
                org_capture_templates = {
                    t = {
                        description = 'Test',
                        template = '* TODO [[%x][%?]]\n',
                    }

                }
            })
        end,
    },
}, {
    root = lazy_root,
    lockfile = nvim_root .. '/lazy.json',
    install = {
        missing = false,
    },
})

require('lazy').sync({
    wait = true,
    show = false,
})

Screenshots and recordings

No response

OS / Distro

macOS 14.3.1 (23D60)

Neovim version/commit

NVIM v0.10.0-dev-24d26b4 Build type: Release LuaJIT 2.1.1707061634

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions