;;; 06-custom-exwm --- Provide EXWM specific config

;;; Commentary:

;;; Code:

(defun leo/which-session ()
  "Return the current Xsession name.  i.e. i3, exwm, etc..."
  (shell-command-to-string "cat ~/.dmrc | tail -n1 | awk -F'=' '{print $2}' | tr -d \"\n\""))

(when (string-equal "exwm" (eval (leo/which-session)))
  ;; https://github.com/ch11ng/exwm
  (use-package exwm
    :ensure t
    :demand t
    :bind
    ("<XF86AudioLowerVolume>" . (lambda() (interactive) (shell-command-to-string "amixer sset Master 2%-")))
    ("<XF86AudioRaiseVolume>" . (lambda() (interactive) (shell-command-to-string "amixer sset Master 2%+")))
    ("<XF86AudioMute>"        . (lambda() (interactive) (shell-command-to-string "amixer sset Master 0")))
    ("<pause>"                . (lambda() (interactive) (shell-command-to-string "playerctl play-pause")))
    ("<XF86AudioPrev>"        . (lambda() (interactive) (shell-command-to-string "playerctl next")))
    ("<XF86AudioNext>"        . (lambda() (interactive) (shell-command-to-string "playerctl prev")))
    :custom
    (exwm-workspace-show-all-buffers t "Show all buffers regardless of current workspace.")
    (exwm-workspace-warp-cursor t "Move cursor when window focus changes.")
    :config
    (require 'exwm-config)
    (exwm-config-default)

    (display-time-mode 1)
    (display-battery-mode 1)

    (require 'exwm-systemtray)
    (exwm-systemtray-enable)

    (require 'exwm-randr)
    (defun leo/exwm-change-screen-hook ()
      (let ((xrandr-output-regexp "\n\\([^ ]+\\) connected ")
            default-output)
        (with-temp-buffer
          (call-process "xrandr" nil t nil)
          (goto-char (point-min))
          (re-search-forward xrandr-output-regexp nil 'noerror)
          (setq default-output (match-string 1))
          (forward-line)
          (if (not (re-search-forward xrandr-output-regexp nil 'noerror))
              (call-process "xrandr" nil nil nil "--output" default-output "--auto")
            (call-process
             "xrandr" nil nil nil
             "--output" default-output "--primary" "--auto"
             "--output" (match-string 1) "--off")
            (setq exwm-randr-workspace-output-plist (list 0 default-output))))))
    (add-hook 'exwm-randr-screen-change-hook 'leo/exwm-change-screen-hook)
    (exwm-randr-enable)

    ;; (start-process "polybar" "*polybar*" "~/scripts/polybar/polybar_launch")

    (setq exwm-workspace-index-map (lambda (i) (number-to-string (1+ i))))

    (dolist (k '(XF86AudioLowerVolume
                 XF86AudioRaiseVolume
                 XF86AudioMude
                 pause
                 XF86AudioPrev
                 XF86AudioNext))
      (push k exwm-input-prefix-keys))

    (setq exwm-input-simulation-keys
          '(
            ;; movement
            ([?\C-b] . [left])
            ([?\M-b] . [C-left])
            ([?\C-f] . [right])
            ([?\M-f] . [C-right])
            ([?\C-p] . [up])
            ([?\C-n] . [down])
            ([?\C-a] . [home])
            ([?\C-e] . [end])
            ([?\M-v] . [prior])
            ([?\C-v] . [next])
            ([?\C-d] . [delete])
            ([?\C-k] . [S-end delete])
            ;; cut/paste.
            ([?\C-w] . [?\C-x])
            ([?\M-w] . [?\C-c])
            ([?\C-y] . [?\C-v])
            ;; search
            ([?\C-s] . [?\C-f])))

    (setq exwm-input-global-keys
          `(
            ;; Bind "s-r" to exit char-mode and fullscreen mode.
            ([?\s-r] . exwm-reset)
	    ;; Super + R to restart exwm
	    ([8388690] . exwm-restart)

            ;; Bind "s-w" to switch workspace interactively.
            ([?\s-w] . exwm-workspace-switch)

            ;; Bind "s-!" to "s-^" to switch to a workspace by its index.
            ([8388641] . (lambda ()(interactive) (exwm-workspace-switch-create 1)))
            ([8388672] . (lambda ()(interactive) (exwm-workspace-switch-create 2)))
            ([8388643] . (lambda ()(interactive) (exwm-workspace-switch-create 3)))
            ([8388644] . (lambda ()(interactive) (exwm-workspace-switch-create 4)))
            ([8388645] . (lambda ()(interactive) (exwm-workspace-switch-create 5)))
            ([8388702] . (lambda ()(interactive) (exwm-workspace-switch-create 6)))
            ([8388649] . (lambda ()(interactive) (exwm-workspace-switch-create 0)))

	    ;; Open ST in other window
	    ([s-return] . (lambda ()(interactive) (split-window-right) (other-window) (start-process "" nil "st")))

            ;; Bind "s-<f2>" to "slock", a simple X display locker.
            ;; ([s-f2] . (lambda ()
            ;;      (interactive)
            ;;      (start-process "" nil "/usr/bin/slock")))))
            )
          )
    )

  (use-package exwm-edit
    :ensure t
    :config
    (defun ag-exwm/on-exwm-edit-compose ()
      (spacemacs/toggle-visual-line-navigation-on)
      (funcall 'markdown-mode))

    ;; (add-hook 'exwm-edit-compose-hook 'ag-exwm/on-exwm-edit-compose))
    )

  ;; https://github.com/emacsmirror/dmenu
  (use-package dmenu
    :ensure t
    :bind ("s-SPC" . dmenu)
    )
  )

(provide '06-custom-exwm)
;;; 06-custom-exwm.el ends here
