i3wm.el 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. ;; Keywords: convenience, extensions
  6. ;; URL: https://git.flintfam.org/swf-projects/emacs-i3
  7. ;; License: GNU General Public License version 3, or (at your option) any later version
  8. ;; This file is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;; This file is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;
  20. ;; This provides an interface to the i3 window manager, with a few
  21. ;; pre-built accessors to the various i3 commands.
  22. ;;; Code:
  23. (require 'json)
  24. ;;; Primitive functions:
  25. (defun i3wm-command (command &rest arguments)
  26. "Execute the given COMMAND with the given ARGUMENTS."
  27. (json-read-from-string
  28. (shell-command-to-string
  29. (format "i3-msg %s" (shell-quote-argument (apply #'format command arguments))))))
  30. (defun i3wm-get-workspaces ()
  31. "List all workspaces."
  32. (json-read-from-string
  33. (shell-command-to-string "i3-msg -t get_workspaces")))
  34. (defun i3wm-get-outputs ()
  35. "List all outputs."
  36. (json-read-from-string
  37. (shell-command-to-string "i3-msg -t get_outputs")))
  38. (defun i3wm-get-version ()
  39. "Retrieve i3 version."
  40. (json-read-from-string
  41. (shell-command-to-string "i3-msg -t get_version")))
  42. ;;; i3 commands
  43. (defun i3wm-exec (program)
  44. "Execute the given PROGRAM."
  45. (i3wm-command "exec %s" program))
  46. (defun i3wm-exec-no-startup-id (program)
  47. "Execute the given PROGRAM with --no-startup-id."
  48. (i3wm-command "exec --no-startup-id %s" program))
  49. (defun i3wm-workspace (workspace)
  50. "Switch to the given i3 WORKSPACE."
  51. (i3wm-command "workspace %s" workspace))
  52. (defun i3wm-workspace-numbered (number)
  53. "Switch to the workspace with the given NUMBER."
  54. (i3wm-command "workspace number %d" number))
  55. (defun i3wm-workspace-next ()
  56. "Switch to the next workspace."
  57. (i3wm-command "workspace next"))
  58. (defun i3wm-workspace-prev ()
  59. "Switch to the previous workspace."
  60. (i3wm-command "workspace prev"))
  61. (defun i3wm-split-horizontally ()
  62. "Split the current container horizontally."
  63. (i3wm-command "split h"))
  64. (defun i3wm-split-vertically ()
  65. "Split the current container vertically."
  66. (i3wm-command "split v"))
  67. (defun i3wm-stacking ()
  68. "Switch to a stacking layout."
  69. (i3wm-command "layout stacking"))
  70. (defun i3wm-tabbed ()
  71. "Switch to a tabbed layout."
  72. (i3wm-command "layout tabbed"))
  73. (defun i3wm-fullscreen ()
  74. "Switch to a fullscreen layout."
  75. (i3wm-command "fullscreen toggle"))
  76. (defun i3wm-floating ()
  77. "Toggle container floating."
  78. (i3wm-command "floating toggle"))
  79. ;; Interactive commands
  80. ;;;###autoload
  81. (defun i3wm-switch-to-workspace (workspace)
  82. "Prompt for and switch to a WORKSPACE."
  83. (interactive (list (completing-read "Workspace Name: "
  84. (mapcar (lambda (workspace)
  85. (cdr (assoc 'name workspace)))
  86. (i3wm-get-workspaces)))))
  87. (i3wm-workspace workspace))
  88. ;;;###autoload
  89. (defun i3wm-switch-to-workspace-number (number)
  90. "Prompt for and switch to the workspace numbered NUMBER."
  91. (interactive (list (read-number "Workspace Number: ")))
  92. (i3wm-workspace-numbered number))
  93. (provide 'i3wm)
  94. ;;; i3wm.el ends here