Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial try on functions for sway/wayland #93

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions emacs-everywhere.el
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@
(list "powershell" "-NoProfile" "-Command"
"& {(New-Object -ComObject wscript.shell).SendKeys(\"^v\")}"))
('x11 (list "xdotool" "key" "--clearmodifiers" "Shift+Insert"))
('wayland (list "ydotool" "key" "42:1" "110:1" "42:0" "110:0"))
('wayland (cond ((executable-find "dotool") (list "dotool"))
;; Note: for ydotool to work you need to have the ydotoold daemon running:
((executable-find "ydotool") (list "ydotool" "key" "42:1" "110:1" "42:0" "110:0"))
((executable-find "wtype") (list "wtype" "-M" "Shift" "-P" "Insert" "-m" "Shift" "-p" "Insert"))))
('unknown
(list "notify-send"
"No paste command defined for emacs-everywhere"
Expand Down Expand Up @@ -107,6 +110,7 @@ it worked can be a good idea."
(`(windows . ,_) (list "powershell" "-NoProfile" "-command"
"& {Add-Type 'using System; using System.Runtime.InteropServices; public class Tricks { [DllImport(\"user32.dll\")] public static extern bool SetForegroundWindow(IntPtr hWnd); }'; [tricks]::SetForegroundWindow(%w) }"))
(`(x11 . ,_) (list "xdotool" "windowactivate" "--sync" "%w"))
(`(wayland . sway) (list "swaymsg" "[con_id=%w]" "focus"))
(`(wayland . KDE) (list "kdotool" "windowactivate" "%w"))) ; No --sync
"Command to refocus the active window when emacs-everywhere was triggered.
This is given as a list in the form (CMD ARGS...).
Expand Down Expand Up @@ -232,6 +236,7 @@ Make sure that it will be matched by `emacs-everywhere-file-patterns'."
(`(quartz . ,_) #'emacs-everywhere--app-info-osx)
(`(windows . ,_) #'emacs-everywhere--app-info-windows)
(`(x11 . ,_) #'emacs-everywhere--app-info-linux-x11)
(`(wayland . sway) #'emacs-everywhere--app-info-linux-sway)
(`(wayland . KDE) #'emacs-everywhere--app-info-linux-kde))
"Function that asks the system for information on the current foreground app.
On most systems, this should be set to a sensible default, but it
Expand Down Expand Up @@ -425,7 +430,9 @@ Never paste content when ABORT is non-nil."
emacs-everywhere-paste-command
(not abort))
(apply #'call-process (car emacs-everywhere-paste-command)
nil nil nil (cdr emacs-everywhere-paste-command)))))
(if (cdr emacs-everywhere-paste-command) nil
(make-temp-file nil nil nil "key shift+insert")) nil nil
(cdr emacs-everywhere-paste-command)))))
;; Clean up after ourselves in case the buffer survives `server-buffer-done'
;; (b/c `server-existing-buffer' is non-nil).
(emacs-everywhere-mode -1)
Expand Down Expand Up @@ -475,8 +482,30 @@ Please go to 'System Preferences > Security & Privacy > Privacy > Accessibility'
(pcase emacs-everywhere--display-server
(`(x11 . ,_) (emacs-everywhere--app-info-linux-x11))
(`(wayland . KDE) (emacs-everywhere--app-info-linux-kde))
(`(wayland . sway) (emacs-everywhere--app-info-linux-sway))
(_ (user-error "Unable to fetch app info with display server %S" emacs-everywhere--display-server))))


(defun emacs-everywhere--app-info-linux-sway ()
"Return information on the current active window, on a Linux Sway session."
(let* ((json-string (emacs-everywhere--call "sh" "-c" "swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true)'"))
(json-object (json-read-from-string json-string))
(window-id (number-to-string (cdr (assoc 'id json-object))))
(app-name (cdr (assoc 'app_id json-object)))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While using this branch I noticed that not all applications have an app_id on my system, leading to a Wrong type argument: stringp, nil in *Messages*.

I think the ones running with Xwayland do not have one (Slack in my case). They have window_properties['class'] set though, so I could get them to work with this change:

Suggested change
(app-name (cdr (assoc 'app_id json-object)))
(app-name (cdr (assoc 'app_id json-object)))
(app-name (if app-name app-name (cdr (assoc 'class (cdr (assoc 'window_properties json-object))))))

(window-title (cdr (assoc 'name json-object)))
(tmp-window-geometry (cdr (assoc 'geometry json-object)))
(window-geometry (list
(cdr (assoc 'x tmp-window-geometry))
(cdr (assoc 'y tmp-window-geometry))
(cdr (assoc 'width tmp-window-geometry))
(cdr (assoc 'height tmp-window-geometry)))))
(make-emacs-everywhere-app
:id window-id
:class app-name
:title window-title
:geometry window-geometry)))


(defun emacs-everywhere--app-info-linux-x11 ()
"Return information on the current active window, on a Linux X11 sessions."
(let ((window-id (emacs-everywhere--call "xdotool" "getactivewindow")))
Expand Down