i3wm.el 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. ;;; i3wm.el --- i3wm integration library
  2. ;; Copyright (C) 2016 Samuel Flint
  3. ;; Author: Samuel W. Flint <swflint@flintfam.org>
  4. ;; Version: 0.5
  5. ;; Package-requires: ((json))
  6. ;; Keywords: convenience, extensions
  7. ;; URL: https://git.flintfam.org/swflint/emacs-i3
  8. ;;; Commentary:
  9. ;;
  10. ;; This provides an interface to the i3 window manager, with a few
  11. ;; pre-built accessors to the various i3 commands.
  12. ;;; Code:
  13. (require 'json)
  14. ;;; Primitive functions:
  15. (defun i3wm-command (command &rest arguments)
  16. "I3wm-command COMMAND &rest ARGUMENTS.
  17. Execute the givenn COMMAND with the given ARGUMENTS."
  18. (json-read-from-string
  19. (shell-command-to-string
  20. (format "i3-msg \"%s\"" (apply #'format command arguments)))))
  21. (defun i3wm-get-workspaces ()
  22. "I3wm-get-workspaces.
  23. List all workspaces."
  24. (json-read-from-string
  25. (shell-command-to-string "i3-msg -t get_workspaces")))
  26. (defun i3wm-get-outputs ()
  27. "I3wm-get-outputs.
  28. List all outputs."
  29. (json-read-from-string
  30. (shell-command-to-string "i3-msg -t get_outputs")))
  31. (defun i3wm-get-version ()
  32. "I3wm-get-version.
  33. Retrieve i3 version."
  34. (json-read-from-string
  35. (shell-command-to-string "i3-msg -t get_version")))
  36. ;;; i3 commands
  37. (defun i3wm-exec (program)
  38. "I3wm-exec PROGRAM.
  39. Executes the given PROGRAM."
  40. (i3wm-command "exec %s" program))
  41. (defun i3wm-exec-no-startup-id (program)
  42. "I3wm-exec-no-startup-id PROGRAM.
  43. Execute the given PROGRAM with --no-startup-id."
  44. (i3wm-command "exec --no-startup-id %s" program))
  45. (defun i3wm-workspace (workspace)
  46. "I3wm-command WORKSPACE.
  47. Switch to the given i3 workspace"
  48. (i3wm-command "workspace %s" workspace))
  49. (defun i3wm-workspace-numbered (number)
  50. "I3wm-workspace-numbered NUMBER.
  51. Switch to the workspace with the given number."
  52. (i3wm-command "workspace number %d" number))
  53. (defun i3wm-workspace-next ()
  54. "I3wm-workspace-next.
  55. Switch to the next workspace."
  56. (i3wm-command "workspace next"))
  57. (defun i3wm-workspace-prev ()
  58. "I3wm-workspace-prev.
  59. Switch to the previous workspace."
  60. (i3wm-command "workspace prev"))
  61. (defun i3wm-split-horizontally ()
  62. "I3wm-split-horizontally.
  63. Split the current container horizontally."
  64. (i3wm-command "split h"))
  65. (defun i3wm-split-vertically ()
  66. "I3wm-split-vertically.
  67. Split the current container vertically."
  68. (i3wm-command "split v"))
  69. (defun i3wm-stacking ()
  70. "I3wm-stacking.
  71. Switch to a stacking layout."
  72. (i3wm-command "layout stacking"))
  73. (defun i3wm-tabbed ()
  74. "I3wm-tabbed.
  75. Switch to a tabbed layout."
  76. (i3wm-command "layout tabbed"))
  77. (defun i3wm-fullscreen ()
  78. "I3wm-fullscreen.
  79. Switch to a fullscreen layout."
  80. (i3wm-command "fullscreen toggle"))
  81. (defun i3wm-floating ()
  82. "I3wm-floating.
  83. Toggle container floating."
  84. (i3wm-command "floating toggle"))
  85. ;; Interactive commands
  86. ;;;###autoload
  87. (defun i3wm-switch-to-workspace (workspace)
  88. "I3wm-switch-to-workspace WORKSPACE.
  89. If called interactively, prompt for and provide completion for
  90. workspace name and switch to it."
  91. (interactive (list (completing-read "Workspace Name: "
  92. (mapcar (lambda (workspace)
  93. (cdr (assoc 'name workspace)))
  94. (i3wm-get-workspaces)))))
  95. (i3wm-workspace workspace))
  96. ;;;###autoload
  97. (defun i3wm-switch-to-workspace-number (number)
  98. "I3wm-switch-to-workspace-number NUMBER.
  99. If called interactively, prompt for a workspace number and
  100. switch."
  101. (interactive (list (read-number "Workspace Number: ")))
  102. (i3wm-workspace-numbered number))
  103. (provide 'i3wm)
  104. ;;; i3wm.el ends here