Clean Paste

@rebind / clean-pastev0.1.3Public

Paste without formatting in every app, with smart quotes straightened and extra spaces removed.

Files

Older release. Installs get 0.1.3.

main.luau1.8 KB

-- Clean Paste: paste the clipboard as plain text, with smart quotes
-- straightened and runs of spaces collapsed. Plain paste is untouched.

local IS_MAC = _REBIND.platform == "macos"

local MODS = {
  ["Ctrl+Shift"] = { ctrl = true, shift = true },
  ["Cmd/Win+Shift"] = { win = true, shift = true },
  ["Ctrl+Alt"] = { ctrl = true, alt = true },
  ["Alt+Shift"] = { alt = true, shift = true },
}

local cfg = UI.Schema({
  key = UI.Keybind("V", { label = "Key" }),
  modifier = UI.Select(
    if IS_MAC then "Cmd/Win+Shift" else "Ctrl+Shift",
    { "Ctrl+Shift", "Cmd/Win+Shift", "Ctrl+Alt", "Alt+Shift" },
    { label = "Held with the key" }
  ),
  quotes = UI.Toggle(true, { label = "Straighten smart quotes" }),
  spaces = UI.Toggle(true, { label = "Collapse extra spaces" }),
})

local function chordHeld()
  local want = MODS[cfg.modifier] or {}
  local have = Input.GetModifiers()
  for _, m in { "ctrl", "alt", "shift", "win" } do
    if (want[m] == true) ~= have[m] then
      return false
    end
  end
  return true
end

local function clean(text)
  if cfg.quotes then
    text =
      text:gsub("“", '"'):gsub("”", '"'):gsub("‘", "'"):gsub("’", "'")
  end
  if cfg.spaces then
    text = text:gsub("[ \t]+", " "):gsub(" +\n", "\n")
  end
  return text
end

Bind(cfg.key, {
  when = chordHeld,
  action = Async(function()
    local ok, text = pcall(Clipboard.Get)
    if not ok or not text then
      UI.Notify("The clipboard has no text", "warning")
      return
    end
    Clipboard.Set(clean(text))
    -- let go of the chord first, or the paste becomes another chord
    while Input.GetModifiers().shift or Input.GetModifiers().alt do
      Sleep(10)
    end
    HID.Press(if IS_MAC then "LWin+V" else "LCtrl+V")
  end),
})

3 files, 3.5 KB.