eww-fcitx5-toggle 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env -S emacs -x
  2. ;; -*- mode: emacs-lisp; lexical-binding: t -*-
  3. (require 'cl-lib)
  4. (require 'server)
  5. (require 'dbus)
  6. (defun cmdline-for-pid (pid)
  7. "Return the command line arguments passed to PID.
  8. PID can be a string or a number."
  9. (butlast (string-split
  10. (with-temp-buffer
  11. (insert-file-contents-literally
  12. (format "/proc/%s/cmdline" pid))
  13. (buffer-substring-no-properties (point-min)
  14. (point-max)))
  15. "\0")))
  16. (defun current-eww-config-dir ()
  17. "Return the configuration directory for a currently running eww process."
  18. ;; This probably only works on Linux
  19. (catch 'found
  20. (dolist (subdir (directory-files "/proc"))
  21. (when (string-match-p (rx bos (+ num) eos) subdir)
  22. (ignore-error permission-denied
  23. (let* ((attrs (file-attributes (format "/proc/%s/exe" subdir)))
  24. (type (file-attribute-type attrs)))
  25. (when (and (stringp type)
  26. (string-match-p (rx (or bos "/") "eww") type))
  27. (cl-maplist (lambda (tail)
  28. (when (equal (car tail) "-c")
  29. (throw 'found (cl-second tail))))
  30. (cmdline-for-pid subdir)))))))))
  31. (defun set-eww-fcitx-state (state)
  32. "Set the Fcitx state for Eww to STATE."
  33. (let ((args (list "update" (format "fcitx5-state=%s" state)))
  34. (cfg-dir (current-eww-config-dir)))
  35. (when cfg-dir
  36. (setq args (nconc (list "-c" cfg-dir) args)))
  37. (apply 'call-process "eww" nil 0 nil args)))
  38. (cl-defun has-focused-window-p (&optional (server "server"))
  39. "Return non-nil if SERVER has at least one focused window.
  40. SERVER defaults to \"server\"."
  41. (server-eval-at
  42. server '(cl-some 'frame-focus-state (frame-list))))
  43. (if (has-focused-window-p)
  44. (server-eval-at "server" '(my/global-toggle-mozc))
  45. (dbus-call-method :session "org.fcitx.Fcitx5" "/controller"
  46. "org.fcitx.Fcitx.Controller1" "Toggle")
  47. (let ((state (dbus-call-method :session "org.fcitx.Fcitx5" "/controller"
  48. "org.fcitx.Fcitx.Controller1" "State")))
  49. (set-eww-fcitx-state state)))
  50. ;; Local Variables:
  51. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  52. ;; End: