i3wm.el 3.4 KB

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