 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Mode line hacking
 
-(defun my-mode-line-kill-file-full-path (event)
-  (interactive "e")
-  (with-selected-window (posn-window (event-start event))
-    (let ((full (buffer-file-name)))
-      (when full
-        (kill-new full)
-        (message "Copied: `%s'" full)))))
+(defmacro def-with-selected-window (name &rest body)
+  `(defun ,name (event)
+     (interactive "e")
+     (with-selected-window (posn-window (event-start event))
+       ,@body)))
+
+(def-with-selected-window my-mode-line-kill-file-full-path
+  (let ((full (buffer-file-name)))
+    (when full
+      (kill-new full)
+      (message "Copied: `%s'" full))))
 
 (defvar my-mode-line-buffer-identification-keymap
   (let ((map (make-sparse-keymap)))

(global-set-key (kbd "<S-f6>") 'my-find-tag-next)
(global-set-key (kbd "<f7>") 'pop-tag-mark)

(global-set-key (kbd "s-J")
                'my-sublime-expand-selection-to-indentation)
(global-set-key (kbd "M-J")
                'my-sublime-expand-selection-to-indentation)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


                            mouse-face mode-line-highlight
                            local-map ,my-mode-line-buffer-modified-p-keymap))

(def-with-selected-window my-mode-line-beginning-or-end-of-buffer
  (if (bobp)
      (goto-char (point-max))
    (goto-char (point-min))))

(def-with-selected-window my-mode-line-scroll-up
  (scroll-up))

(def-with-selected-window my-mode-line-scroll-down
  (scroll-down))

(setq my-mode-line-buffer-percentage-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map [mode-line mouse-1] 'my-mode-line-scroll-up)
    (define-key map [mode-line S-mouse-1] 'my-mode-line-scroll-down)
    (define-key map [mode-line C-mouse-1] 'my-mode-line-beginning-or-end-of-buffer)
    (define-key map [mode-line down-mouse-1] 'ignore)
    map))

(def-with-selected-window my-mode-line-goto-line
  (let ((n (read-number "Goto line: ")))
    (goto-line n)))

(setq my-mode-line-column-line-number-mode-map
      (let ((map (make-sparse-keymap)))
