(require 'sawfish.wm.defaults)
(require 'sawfish.wm.ext.match-window)

;;; Programs to run on startup
(system "setxkbmap dvorak")
(system "setxkbmap -option compose:caps")
(system "lxpanel &")
; (system "xscreensaver &")

;; For VMs
; (system "xset s off")
; (system "xset -dpms")


;;; Custom functions

(defun launch-app (app)
  (case app
    ('chrome (system "google-chrome --disable-gpu-compositing &"))
    ('term-code (system "gnome-terminal --role code --geometry=120x34 &"))
    ('term-quick (system "gnome-terminal --role quick --geometry=80x24 &"))
    ('term-std (system "gnome-terminal --role terminal --geometry=100x28 &"))
    ('quassel (system "quasselclient &")) ))

; Note that the documentation for 'insert-workspace' is wrong, and we insert
; *after* the provided value, not before
(defun launch-app-in-new-workspace (app)
  (progn
    (select-workspace (insert-workspace (cdr (workspace-limits))))
    (launch-app app) ))

(defun send-to-new-workspace ()
  (progn
    (set 'space current-workspace)
    (set 'window (input-focus))
    (move-window-to-workspace
      window
      space
      (insert-workspace (cdr (workspace-limits)))
      t )
    (select-workspace (cdr (workspace-limits))) ))

(defun send-workspace-to-top ()
  (move-workspace current-workspace (- (cdr (workspace-limits)) current-workspace)) )

(defun build-new-workspace ()
  (progn
    (launch-app-in-new-workspace 'chrome)
    (launch-app 'term-code)))

(defun destroy-workspace ()
  (progn
    (mapcar
      (lambda (win)
        (if (not (equal "panel" win)) (destroy-window win)) )
      (workspace-windows) )
    (remove-workspace)
    (previous-workspace 1) ))


;;; Keybinds
(define apps-keymap (make-keymap))

(bind-keys apps-keymap
  "t" '(launch-app 'term-std)
  "q" '(launch-app 'term-quick)
  "o" '(launch-app 'term-code)
  "c" '(launch-app 'chrome)
  "i" '(launch-app 'quassel) )

(unbind-keys global-keymap
  "W-Left"
  "W-Right"
  "M-Up"
  "M-Down"
  "C-Tab")

; Workspace bindings
(bind-keys global-keymap
  "M-C-Delete"       'restart
  "Super-Left"       '(previous-workspace 1)
  "Super-Right"      '(next-workspace 1)
  "Super-Up"         '(select-workspace (cdr (workspace-limits))) ; top of stack
  "Super-Down"       '(select-workspace (car (workspace-limits))) ; bottom of stack
  "Super-Next"       'move-workspace-forwards
  "Super-Prior"      'move-workspace-backwards
  "Super-Shift-Next"  send-workspace-to-top
  "Super-End"         destroy-workspace
  "M-F1"             '(select-workspace 0)
  "M-F2"             '(select-workspace 1)
  "M-F3"             '(select-workspace 2)
  "M-F4"             '(select-workspace 3)
  "M-F5"             '(select-workspace 4)
  "M-F6"             '(select-workspace 5)
  "M-F7"             '(select-workspace 6)
  "M-F8"             '(select-workspace 7)
)

; Window-specific workspace bindings
(bind-keys window-keymap
  "Super-Shift-Left"  'send-to-previous-workspace
  "Super-Shift-Right" 'send-to-next-workspace
  "Super-Shift-Up"     send-to-new-workspace )

; General bindings
(bind-keys global-keymap
  "M-TAB"       'cycle-windows
  "M-Shift-TAB" 'cycle-windows-backward
  "Super-l"     '(system "xscreensaver-command -lock")
  "Super-x"     apps-keymap
  "Super-n"     build-new-workspace
)

; Given a list of paired letter-command items, returns a new list of paired
; letter-function items, where each anonymous function calls the passed
; function with the original string as its argument
; 'function' should be a function, 'map' should be a list of strings
;
; This is currently unused, because my needs changed, but it's an excellent idea.
(defun funcmap (function map)
  (mapcar
    (lambda (item)
      (if (= (length item) 1)
          item
          (lambda () (function item)) ))
    map) )


;;; Custom variables
(set 'lock-first-workspace nil)

;;; Hooks
(add-hook 'idle-hook delete-empty-workspaces)

;;; Window Matching
(add-window-matcher '((WM_WINDOW_ROLE . "^code|terminal$"))
                    '((position . south-east)) )

(add-window-matcher '((WM_WINDOW_ROLE . "^quick$"))
                    '((position . north-east)) )