niri: app maps and edit-clipboard shortcuts

This commit is contained in:
js0ny 2025-11-25 17:35:23 +00:00
parent bd2a7fcc26
commit 40e3834b7f
5 changed files with 149 additions and 12 deletions

View file

@ -8,10 +8,21 @@
-- Entry point of neovim configuration -- Entry point of neovim configuration
require("config.options") require("config.options")
local minimal = os.getenv("NVIM_MINIMAL") or os.getenv("EDITOR_POPUP")
if minimal == "1" then
-- require("profiles.minimal")
vim.keymap.set("n", "<C-s>", ":wq!<CR>", { noremap = true, silent = true })
return
end
local term = os.getenv("TERM") or "" local term = os.getenv("TERM") or ""
if vim.g.vscode then -- TODO: VSCode Neovim Integration if vim.g.vscode then -- TODO: VSCode Neovim Integration
require("profiles.vscode") require("profiles.vscode")
elseif term == "linux" then -- Under tty -- Under tty
-- if under gui, the term will be set to linux as well, additional check required.
elseif term == "linux" and not vim.fn.has("gui_running") then
require("profiles.tty") require("profiles.tty")
vim.cmd("colorscheme vim") -- Use minimal colorscheme vim.cmd("colorscheme vim") -- Use minimal colorscheme
else else
@ -38,3 +49,43 @@ if vim.g.neovide then
vim.g.neovide_light_radius = 5 vim.g.neovide_light_radius = 5
vim.g.neovide_input_macos_option_key_is_meta = "only_left" vim.g.neovide_input_macos_option_key_is_meta = "only_left"
end end
-- 存储输入法状态的变量
vim.g.input_layout = nil
-- 退出插入模式时:记录当前输入法并切换到英文
local function fcitx2en()
-- 使用 fcitx5-remote -n 获取当前输入法名称
local current_layout = vim.fn.system("fcitx5-remote -n")
-- 去除换行符
vim.g.input_layout = vim.trim(current_layout)
-- 切换到英文输入法
vim.fn.system("fcitx5-remote -s keyboard-us")
end
-- 进入插入模式时:恢复之前的输入法状态
local function fcitx2zh()
-- 如果之前记录了输入法状态,则恢复
if vim.g.input_layout ~= nil and vim.g.input_layout ~= "" then
vim.fn.system("fcitx5-remote -s " .. vim.g.input_layout)
end
end
-- 设置 ttimeoutlen
vim.opt.ttimeoutlen = 150
-- 创建自动命令组
local fcitx_group = vim.api.nvim_create_augroup("FcitxToggle", { clear = true })
-- 退出插入模式时切换到英文并记录状态
vim.api.nvim_create_autocmd("InsertLeave", {
group = fcitx_group,
callback = fcitx2en,
})
-- 进入插入模式时恢复之前的输入法
vim.api.nvim_create_autocmd("InsertEnter", {
group = fcitx_group,
callback = fcitx2zh,
})

View file

@ -1,4 +1,9 @@
{config, ...}: let {
config,
pkgs,
lib,
...
}: let
term = config.currentUser.defaultTerminal; term = config.currentUser.defaultTerminal;
termRunner = config.currentUser.defaultTerminalRunner; termRunner = config.currentUser.defaultTerminalRunner;
iconTheme = config.currentUser.iconTheme; iconTheme = config.currentUser.iconTheme;
@ -7,8 +12,29 @@
launcher = "rofi"; launcher = "rofi";
kbdBacklightDev = config.currentHost.keyboardBacklightDevice; kbdBacklightDev = config.currentHost.keyboardBacklightDevice;
kbdBacklightStep = config.currentHost.keyboardBacklightStep; kbdBacklightStep = config.currentHost.keyboardBacklightStep;
nirictl = import ./scripts.nix {inherit pkgs;};
in { in {
home.packages = [
nirictl.focusOrLaunch
];
programs.niri.settings.binds = with config.lib.niri.actions; { programs.niri.settings.binds = with config.lib.niri.actions; {
# === Application Runner ===
"Mod+B".hotkey-overlay.title = "Focus or launch web browser";
"Mod+B".action = spawn "${lib.getExe nirictl.focusOrLaunch}" "firefox" "firefox";
"Mod+Shift+B".hotkey-overlay.title = "Launch web browser in private mode";
"Mod+Shift+B".action = spawn "firefox" "--private-window";
"Mod+Shift+A".hotkey-overlay.title = "Focus or launch CherryStudio (AI assistant)";
"Mod+Shift+A".action = spawn "${lib.getExe nirictl.focusOrLaunch}" "CherryStudio" "cherry-studio";
"Mod+O".hotkey-overlay.title = "Focus or launch Obsidian";
"Mod+O".action = spawn "${lib.getExe nirictl.focusOrLaunch}" "obsidian" "obsidian";
# TODO: Change "org.kde.dolphin" to a more generic explorer app id via config.currentUser
"Mod+E".hotkey-overlay.title = "Focus or launch file explorer";
"Mod+E".action = spawn "${lib.getExe nirictl.focusOrLaunch}" "org.kde.dolphin" "dolphin";
"Mod+Semicolon".action = spawn "neovide" "${config.home.homeDirectory}/Atelier";
"Mod+Apostrophe".action =
spawn-sh "EDITOR_MINIMAL=1 ${termRunner} -o close_on_child_death=yes --class=edit-clipboard-popup -e edit-clipboard --minimal";
"Mod+Shift+Slash".action = show-hotkey-overlay; "Mod+Shift+Slash".action = show-hotkey-overlay;
"Mod+Return".hotkey-overlay.title = "Open a Terminal: ${term}"; "Mod+Return".hotkey-overlay.title = "Open a Terminal: ${term}";
@ -22,17 +48,12 @@ in {
"Alt+Space".action = "Alt+Space".action =
spawn "${launcher}" "-show" "drun" "-icon-theme" "${iconTheme}" "-show-icons"; spawn "${launcher}" "-show" "drun" "-icon-theme" "${iconTheme}" "-show-icons";
"Mod+Shift+W".hotkey-overlay.title = "Search open Window: rofi"; "Mod+W".hotkey-overlay.title = "Search open Window: rofi";
"Mod+Shift+W".action = "Mod+W".action =
spawn "${launcher}" "-show" "window" "-icon-theme" "${iconTheme}" "-show-icons"; spawn "${launcher}" "-show" "window" "-icon-theme" "${iconTheme}" "-show-icons";
"Mod+V".action = spawn-sh "cliphist list | ${launcher} -dmenu | cliphist decode | wl-copy"; "Mod+V".action = spawn-sh "cliphist list | ${launcher} -dmenu | cliphist decode | wl-copy";
"Mod+E".hotkey-overlay.title = "Run file explorer";
"Mod+E".action = spawn "${explorer}";
"Mod+Shift+E".hotkey-overlay.title = "Run terminal explorer";
"Mod+Shift+E".action = spawn "${termRunner}" "-e" "${explorerTerm}";
"XF86AudioRaiseVolume".allow-when-locked = true; "XF86AudioRaiseVolume".allow-when-locked = true;
"XF86AudioRaiseVolume".action = "XF86AudioRaiseVolume".action =
spawn "wpctl" "set-volume" "@DEFAULT_AUDIO_SINK@" "0.1+"; spawn "wpctl" "set-volume" "@DEFAULT_AUDIO_SINK@" "0.1+";
@ -174,8 +195,6 @@ in {
"Mod+BracketLeft".action = consume-or-expel-window-left; "Mod+BracketLeft".action = consume-or-expel-window-left;
"Mod+BracketRight".action = consume-or-expel-window-right; "Mod+BracketRight".action = consume-or-expel-window-right;
"Mod+Comma".action = consume-window-into-column;
"Mod+Period".action = expel-window-from-column;
"Mod+R".action = switch-preset-column-width; "Mod+R".action = switch-preset-column-width;
"Mod+Shift+R".action = switch-preset-window-height; "Mod+Shift+R".action = switch-preset-window-height;
@ -191,7 +210,8 @@ in {
"Mod+Shift+Equal".action = set-window-height "+10%"; "Mod+Shift+Equal".action = set-window-height "+10%";
"Mod+F".action = toggle-window-floating; "Mod+F".action = toggle-window-floating;
"Mod+Shift+F".action = switch-focus-between-floating-and-tiling; "Mod+Shift+F".action = switch-focus-between-floating-and-tiling;
"Mod+W".action = toggle-column-tabbed-display; "Mod+G".hotkey-overlay.title = "Toggle Grouped Display";
"Mod+G".action = toggle-column-tabbed-display;
"Mod+Shift+S".action.screenshot = {show-pointer = true;}; "Mod+Shift+S".action.screenshot = {show-pointer = true;};
"Print".action.screenshot = {show-pointer = true;}; "Print".action.screenshot = {show-pointer = true;};

View file

@ -0,0 +1,29 @@
# Stolen from basecamp/omarchy
{pkgs, ...}: {
focusOrLaunch = pkgs.writeShellApplication {
name = "nirictl-focus-or-launch";
runtimeInputs = [pkgs.jq pkgs.niri];
text = ''
if (($# == 0)); then
echo "Usage: $0 <app_id> <command-to-launch>" >&2
exit 1
fi
APP_ID="$1"
CMD="''${2:-$1}"
WINDOWS=$(niri msg --json windows)
TARGET_ID=$(echo "$WINDOWS" | jq -r ".[] | select(.app_id == \"$APP_ID\") | .id" | head -n 1)
if [ -n "$TARGET_ID" ]; then
niri msg action focus-window --id "$TARGET_ID"
else
eval "$CMD" &
fi
'';
};
}

View file

@ -2,6 +2,16 @@
{...}: { {...}: {
programs.niri = { programs.niri = {
settings.window-rules = [ settings.window-rules = [
{
matches = [
{
app-id = "^edit-clipboard-popup$";
}
];
open-floating = true;
opacity = 0.8;
}
# {{{ float, opacity 0.8, top right: Picture-in-Picture // waybar childs # {{{ float, opacity 0.8, top right: Picture-in-Picture // waybar childs
{ {
matches = [ matches = [

View file

@ -5,6 +5,31 @@
# Reads clipboard content, opens it in $EDITOR, and writes back to clipboard # Reads clipboard content, opens it in $EDITOR, and writes back to clipboard
edit-clipboard() { edit-clipboard() {
# Parse command line arguments
while [ $# -gt 0 ]; do
case "$1" in
--minimal)
NVIM_MINIMAL=1
shift
;;
--editor)
if [ -n "$2" ] && [ "''${2#-}" = "$2" ]; then
EDITOR="$2"
shift 2
else
echo "Error: --editor requires an argument" >&2
return 1
fi
;;
*)
echo "Error: Unknown option: $1" >&2
echo "Usage: edit-clipboard [--popup] [--editor <editor>]" >&2
return 1
;;
esac
done
# Detect clipboard command based on platform # Detect clipboard command based on platform
if command -v pbpaste >/dev/null 2>&1; then if command -v pbpaste >/dev/null 2>&1; then
# macOS # macOS
@ -57,6 +82,8 @@
# Cleanup # Cleanup
rm -f "$TMPFILE" rm -f "$TMPFILE"
exit 0
} }
if [ "''${0##*/}" = "edit-clipboard" ] || [ "''${0##*/}" = "edit-clipboard.sh" ]; then if [ "''${0##*/}" = "edit-clipboard" ] || [ "''${0##*/}" = "edit-clipboard.sh" ]; then