kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

niri.kak (8973B)


      1 # https://yalter.github.io/niri/
      2 # ------------------------------
      3 
      4 # Kakoune Niri windowing module
      5 
      6 # Niri is a scrolling window manager;
      7 # vertical/horizontal splits aren't really a native concept.
      8 # A workspace consists of columns of arbitrary width, where each
      9 # column can have one or more window in it. Windows in a column
     10 # are stacked on top of each other. To "consume" a window into a
     11 # column is to pull it BELOW the other windows in the column.
     12 # For example, consider:
     13 # +-----+ +-----+                         +-----+
     14 # |     | |     |                         |  A  |
     15 # |  A  | |  B  |  --consume B into A-->  +-----+
     16 # |     | |     |                         |  B  |
     17 # +-----+ +-----+                         +-----+
     18 
     19 # We provide three ways to open a new window.
     20 
     21 # normal: simply spawn a new window to the right.
     22 #   This is the default for new windows in Niri.
     23 # Alias: horizontal
     24 # +-----+ +-----+
     25 # |     | |     |
     26 # | KAK | | NEW |
     27 # |     | |     |
     28 # +-----+ +-----+
     29 
     30 # consume: the new window is consumed into the current column.
     31 # Alias: vertical
     32 # +-----+
     33 # | KAK |
     34 # +-----+
     35 # | NEW |
     36 # +-----+
     37 
     38 # consume-right: spawn new window and consume into column to the right
     39 #   Note: FOO is an already open window to the right.
     40 # Alias: none
     41 # +-----+ +-----+
     42 # |     | | FOO |
     43 # | KAK | +-----+
     44 # |     | | NEW |
     45 # +-----+ +-----+
     46 
     47 # In terms of closest analogues to the vertical & horizontal split paradigm,
     48 # we alias "vertical" to "consume" and we alias "horizontal" to "normal".
     49 # There isn't a clean alias for "consume-right", but it's a convenient command
     50 # that users can invoke directly.
     51 
     52 provide-module niri %~
     53 
     54 # ensure we're actually in niri
     55 # and that another module isn't already loaded
     56 evaluate-commands %sh{
     57     [ -z "${kak_opt_windowing_modules}" ] || 
     58     [ -n "$NIRI_SOCKET" ] ||
     59     echo 'fail NIRI_SOCKET is not set'
     60 }
     61 
     62 # load wayland module
     63 require-module wayland
     64 
     65 alias global niri-terminal-window wayland-terminal-window
     66 
     67 # first arg MUST be left|right, rest are passed to program in new terminal
     68 # Direction is relative to the new window, which is always opened to the right.
     69 # Therefore, left means consume into current column.
     70 define-command -hidden niri-terminal-consume-impl -params 1.. %{
     71     # When Niri creates a new window, it's always in a new column
     72     # to the right of the focused window, and the new window is focused.
     73     # Niri doesn't allow us to specify where the new window should go,
     74     # we can only operate on it AFTER it's been spawned.
     75     # The idea to spawn a terminal, and then consume it
     76     # either into the current col, or the col to the right.
     77     # However, after spawning the terminal, we can't safely
     78     # operate on it immediately because it takes some time (aprx. 0.1s)
     79     # to actually initialize.
     80 
     81     # 1.  Open niri's event stream, wait for it to initialize.
     82     #       Note: event stream is newline-delimited JSON.
     83     # 2.  Execute command to open new terminal window.
     84     # 3.  Watch the event stream and wait for window to be spawned.
     85     # 3.a Only operate on window if not floating.
     86     # 3.b Switch focus to new window if not already focused.
     87     #     ^ (Not certain this is necessary, but better to be safe).
     88     # 4.  Once the window exists, consume in specified direction.
     89     nop %sh{
     90         # First arg always left|right. No need for guards b/c this is private function.
     91         direction=$1
     92         shift
     93         # create fifo for collecting event stream
     94         temp=$(mktemp -d kak-niri-XXXXXX)
     95         fifo="$temp/fifo"
     96         mkfifo "$fifo"
     97         # register func to cleanup fifo
     98         cleanup() {
     99         	rm -rf "$temp"
    100         }
    101         trap cleanup EXIT INT TERM
    102         # read event stream to fifo
    103         niri msg -j event-stream >"$fifo" &
    104         stream_pid=$!
    105         # register timeout fallback
    106         (
    107         	sleep 3
    108         	kill "$!" 2>/dev/null
    109         	kill "$stream_pid" 2>/dev/null
    110         ) >/dev/null 2>&1 </dev/null &
    111         watchdog_pid=$!
    112 
    113         # safely quote arguments for passing to new terminal
    114         kakquote() {
    115         	set -- "$*" ""
    116         	while [ "${1#*\'}" != "$1" ]; do
    117         		set -- "${1#*\'}" "$2${1%%\'*}''"
    118         	done
    119         	printf "'%s' " "$2$1"
    120         }
    121         # loop over args and quote all
    122         quoted_args=''
    123         for arg in "$@"; do
    124         	quoted_args="$quoted_args$(kakquote "$arg")"
    125         done
    126         # run this block async to avoid user-facing lag
    127         {
    128             # track when new window is ready
    129         	ready=false
    130         	# read lines of event stream from fifo
    131         	#   Note: each line is one event.
    132         	#   We wait for the event that indicates stream is initialized,
    133         	#   then we spawn the new terminal window. The next WindowOpenedOrChanged
    134         	#   event immediately after running niri-terminal-window SHOULD be
    135         	#   the event for this window, but it's not guaranteed. There is a very,
    136         	#   very tiny window for a race condition here. Unfortunately, I don't
    137         	#   believe we have a good way to handle it because there's no way to
    138         	#   know the window's ID before it's spawned. This may be a good job for
    139         	#   the Ostrich algorithm (https://en.wikipedia.org/wiki/Ostrich_algorithm)
    140         	while IFS= read -r line; do
    141         	    # each line is a JSON object
    142         		case "$line" in
    143         		# this line means stream is initialized
    144         		*"OverviewOpenedOrClosed"*)
    145         		    # guard to avoid spawning terminal twice
    146         			if [ "$ready" = false ]; then
    147         				ready=true
    148         				# spawn terminal with command
    149         				printf 'niri-terminal-window %s' "$quoted_args" >"$kak_command_fifo"
    150         			fi
    151         			;;
    152         		# newly opened window is now available to act on
    153         		*"WindowOpenedOrChanged"*)
    154         		    # only operate if window isn't floating
    155         		    # This accursed incantation is to access the JSON field
    156         		    # without depending on jq.
    157         			if expr "${line}" : '.*"is_floating":[[:space:]]*false.*' \
    158         				>/dev/null; then
    159         				# handle new window being unfocused
    160         				if expr "${line}" : '.*"is_focused":[[:space:]]*false.*' \
    161         					>/dev/null; then
    162         					# extract the window ID
    163         					# (POSIX compliant JSON parsing is ugly, sorry)
    164         					id="$(echo "$line" | sed -n 's/.*"id":[[:space:]]*\([0-9][0-9]*\).*/\1/p')"
    165         					# focus the new window
    166         					niri msg action focus-window --id "$id"
    167         				fi
    168         				# consume the new window in specified direction
    169         				niri msg action consume-or-expel-window-${direction}
    170         			fi
    171         			# close the stream reader and timeout watchdog
    172         			kill "$stream_pid"
    173         			wait "$stream_pid" 2>/dev/null
    174         			kill "$watchdog_pid" 2>/dev/null
    175         			exit 0
    176         			;;
    177         		esac
    178         	done <"$fifo"
    179         } >/dev/null 2>&1 </dev/null &
    180     }
    181 }
    182 
    183 define-command niri-terminal-consume -params 1.. -docstring '
    184     niri-terminal-consume <program> [<arguments>]: create a new terminal as a Niri window and consume it into the current column
    185     The program passed as argument will be executed in the new terminal' \
    186 %{
    187     niri-terminal-consume-impl left %arg{@}
    188 }
    189 complete-command niri-terminal-consume shell
    190 
    191 define-command niri-terminal-consume-right -params 1.. -docstring '
    192     niri-terminal-consume-right <program> [<arguments>]: create a new terminal as a Niri window and consume it into the column to the right
    193     The program passed as argument will be executed in the new terminal' \
    194 %{
    195     niri-terminal-consume-impl right %arg{@}
    196 }
    197 complete-command niri-terminal-consume-right shell
    198 
    199 define-command niri-new-consume -params .. -docstring '
    200 niri-new-consume [<commands>]: create a new Kakoune client and consume it into the current column
    201 The optional arguments are passed as commands to the new client' \
    202 %{
    203     niri-terminal-consume kak -c %val{session} -e "%arg{@}"
    204 }
    205 complete-command -menu niri-new-consume command
    206 
    207 define-command niri-new-consume-right -params .. -docstring '
    208 niri-new-consume-right [<commands>]: create a new Kakoune client and consume it into the column on the right
    209 The optional arguments are passed as commands to the new client' \
    210 %{
    211     niri-terminal-consume-right kak -c %val{session} -e "%arg{@}"
    212 }
    213 complete-command -menu niri-new-consume-right command
    214 
    215 define-command niri-new-window -params .. -docstring '
    216 niri-new-window [<commands>]: create a new Kakoune client
    217 The ''niri-terminal-window'' command is used to determine the user''s preferred terminal emulator
    218 The optional arguments are passed as commands to the new client' \
    219 %{
    220     niri-terminal-window kak -c %val{session} -e "%arg{@}"
    221 }
    222 complete-command -menu niri-new-window command
    223 
    224 alias global niri-terminal-vertical niri-terminal-consume
    225 # can't use niri-terminal-window below because it's an alias, not a command
    226 alias global niri-terminal-horizontal wayland-terminal-window
    227 
    228 ~