(defun elmord-scrot ()
  (interactive)
  (let* ((filename (expand-file-name (format-time-string "~/%Y%m%d_%H%M%S_scrot.png")))
         (process (start-process "exwm-scrot" "*exwm-scrot*"
                                 "bash" "-c" (concat "sleep 0.5; scrot -s " filename))))
    (message "Select rectangle for screenshot...")
    (set-process-sentinel process
      (lambda (proc status)
        (cond ((equal status "finished\n")
               (gui-backend-set-selection 'CLIPBOARD filename)
               (message "Screenshot saved in %s" filename))
              (t
               (message "scrot: %s" (string-trim status))))))))

(exwm-input-set-key (kbd "<print>") 'elmord-scrot)


;;;; Urgency handling.


;; Telegram notifications hack.
;; (defun elmord-exwm-telegram-title-hook ()
;;     ;;    (when (equal exwm-class-name "TelegramDesktop")
;;     (run-hooks 'elmord-exwm-update-hints-hook)))


(defun elmord-exwm-telegram-urgent-p (buffer)
  (and (equal (buffer-local-value 'exwm-class-name buffer) "TelegramDesktop")
       (string-match "([0-9]+)" (or (buffer-local-value 'exwm-title buffer) ""))))

(defun elmord-exwm-evolution-urgent-p (buffer)
  (and (equal (buffer-local-value 'exwm-class-name buffer) "Evolution")
       (string-match "([0-9]+ unread)" (or (buffer-local-value 'exwm-title buffer) ""))))

(defun elmord-exwm-chromium-skype-urgent-p (buffer)
  (and (equal (buffer-local-value 'exwm-class-name buffer) "Chromium-browser")
       (string-match "([0-9]+) Skype" (or (buffer-local-value 'exwm-title buffer) ""))))






;; Much better (but with possible unknown side-effects?): allow the
;; buffer to be modified, and transfer any modification to the
;; application via clipboard.

;; (defun elmord-exwm-after-change-function (begin end length)
;;   (elmord-exwm-send-string (buffer-substring begin end))
;;   (delete-region begin end)
;;   (set-buffer-modified-p nil))




(defun elmord-exwm-terminal-p (&optional class-name)
  (member (or class-name exwm-class-name)
          '("Xfce4-terminal" "X-terminal-emulator")))




(defvar elmord-exwm-play-skype-alerts nil)

(defun elmord-exwm-window-chromium-p ()
  (member exwm-class-name '("Chromium" "Chromium-browser")))

(defun elmord-exwm-set-urgency-from-title ()
  (when (and (elmord-exwm-window-chromium-p)
             (string-match "([0-9]+) Skype" exwm-title)
             (not (eq (current-buffer) (window-buffer))))
    (setq exwm--hints-urgency t)
    (when elmord-exwm-play-skype-alerts
      (start-process "sound-alert" nil "play" "/usr/share/sounds/purple/receive.wav"))
    (elmord-update-mode-line-exwm-urgency)))

(add-hook 'exwm-manage-finish-hook
          (defun elmord-exwm-chromium-skype-urgent-hook ()
            (when (elmord-exwm-window-chromium-p)
              (add-hook 'exwm-update-title-hook
                        'elmord-exwm-set-urgency-from-title
                        t ;; local
                        ))))





;; (exwm-input--update-global-prefix-keys)

(exwm-enable)
