+(defadvice term-exec (before program-args-list compile activate)
+ "If the PROGRAM argument is a list, interpret it as (PROGRAM . SWITCHES).
+This allows you to pass a list of arguments through `ansi-term'."
+ (let ((program (ad-get-arg 2)))
+ (if (listp program)
+ (progn
+ (ad-set-arg 2 (car program))
+ (ad-set-arg 4 (cdr program))))))
+
+(defadvice term-exec-1 (around hack-environment compile activate)
+ "Hack the environment inherited by inferiors in the terminal."
+ (let ((process-environment process-environment))
+ (setenv "LD_PRELOAD" nil)
+ ad-do-it))
+
+(defadvice shell (around hack-environment compile activate)
+ "Hack the environment inherited by inferiors in the shell."
+ (let ((process-environment process-environment))
+ (setenv "LD_PRELOAD" nil)
+ ad-do-it))
+
+(defun ssh (host)
+ "Open a terminal containing an ssh session to the HOST."
+ (interactive "sHost: ")
+ (ansi-term (list "ssh" host) (format "ssh@%s" host)))
+
+(defvar git-grep-command
+ "env PAGER=cat git grep --no-color -nH -e "
+ "*The default command for \\[git-grep].")
+
+(defvar git-grep-history nil)
+
+(defun git-grep (command-args)
+ "Run `git grep' with user-specified args and collect output in a buffer."
+ (interactive
+ (list (read-shell-command "Run git grep (like this): "
+ git-grep-command 'git-grep-history)))
+ (grep command-args))
+
+;;;--------------------------------------------------------------------------
+;;; Magit configuration.
+
+(setq magit-diff-refine-hunk 'all
+ magit-view-git-manual-method 'man
+ magit-log-margin '(nil age magit-log-margin-width t 18)
+ magit-wip-after-save-local-mode-lighter ""
+ magit-wip-after-apply-mode-lighter ""
+ magit-wip-before-change-mode-lighter "")
+(eval-after-load "magit"
+ '(progn (global-magit-file-mode 1)
+ (magit-wip-after-save-mode 1)
+ (magit-wip-after-apply-mode 1)
+ (magit-wip-before-change-mode 1)
+ (add-to-list 'magit-no-confirm 'safe-with-wip)
+ (push '(:eval (if (or magit-wip-after-save-local-mode
+ magit-wip-after-apply-mode
+ magit-wip-before-change-mode)
+ (format " wip:%s%s%s"
+ (if magit-wip-after-apply-mode "A" "")
+ (if magit-wip-before-change-mode "C" "")
+ (if magit-wip-after-save-local-mode "S" ""))))
+ minor-mode-alist)
+ (dolist (popup '(magit-diff-popup
+ magit-diff-refresh-popup
+ magit-diff-mode-refresh-popup
+ magit-revision-mode-refresh-popup))
+ (magit-define-popup-switch popup ?R "Reverse diff" "-R"))))
+
+(defadvice magit-wip-commit-buffer-file
+ (around mdw-just-this-buffer activate compile)
+ (let ((magit-save-repository-buffers nil)) ad-do-it))
+
+(setq magit-repolist-columns
+ '(("Name" 16 magit-repolist-column-ident nil)
+ ("Version" 18 magit-repolist-column-version nil)
+ ("St" 2 magit-repolist-column-dirty nil)
+ ("L<U" 3 mdw-repolist-column-unpulled-from-upstream nil)
+ ("L>U" 3 mdw-repolist-column-unpushed-to-upstream nil)
+ ("Path" 32 magit-repolist-column-path nil)))
+
+(setq magit-repository-directories '(("~/etc/profile" . 0)
+ ("~/src/" . 1)))
+
+(defadvice magit-list-repos (around mdw-dirname () activate compile)
+ "Make sure the returned names are directory names.
+Otherwise child processes get started in the wrong directory and
+there is sadness."
+ (setq ad-return-value (mapcar #'file-name-as-directory ad-do-it)))
+
+(defun mdw-repolist-column-unpulled-from-upstream (_id)
+ "Insert number of upstream commits not in the current branch."
+ (let ((upstream (magit-get-upstream-branch (magit-get-current-branch) t)))
+ (and upstream
+ (let ((n (cadr (magit-rev-diff-count "HEAD" upstream))))
+ (propertize (number-to-string n) 'face
+ (if (> n 0) 'bold 'shadow))))))
+
+(defun mdw-repolist-column-unpushed-to-upstream (_id)
+ "Insert number of commits in the current branch but not its upstream."
+ (let ((upstream (magit-get-upstream-branch (magit-get-current-branch) t)))
+ (and upstream
+ (let ((n (car (magit-rev-diff-count "HEAD" upstream))))
+ (propertize (number-to-string n) 'face
+ (if (> n 0) 'bold 'shadow))))))
+
+(defun mdw-try-smerge ()
+ (save-excursion
+ (goto-char (point-min))
+ (when (re-search-forward "^<<<<<<< " nil t)
+ (smerge-mode 1))))
+(add-hook 'find-file-hook 'mdw-try-smerge t)
+
+;;;--------------------------------------------------------------------------
+;;; MPC configuration.
+
+(eval-when-compile (trap (require 'mpc)))
+
+(setq mpc-browser-tags '(Artist|Composer|Performer Album|Playlist))
+
+(defun mdw-mpc-now-playing ()
+ (interactive)
+ (require 'mpc)
+ (save-excursion
+ (set-buffer (mpc-proc-cmd (mpc-proc-cmd-list '("status" "currentsong"))))
+ (mpc--status-callback))
+ (let ((state (cdr (assq 'state mpc-status))))
+ (cond ((member state '("stop"))
+ (message "mpd stopped."))
+ ((member state '("play" "pause"))
+ (let* ((artist (cdr (assq 'Artist mpc-status)))
+ (album (cdr (assq 'Album mpc-status)))
+ (title (cdr (assq 'Title mpc-status)))
+ (file (cdr (assq 'file mpc-status)))
+ (duration-string (cdr (assq 'Time mpc-status)))
+ (time-string (cdr (assq 'time mpc-status)))
+ (time (and time-string
+ (string-to-number
+ (if (string-match ":" time-string)
+ (substring time-string
+ 0 (match-beginning 0))
+ (time-string)))))
+ (duration (and duration-string
+ (string-to-number duration-string)))
+ (pos (and time duration
+ (format " [%d:%02d/%d:%02d]"
+ (/ time 60) (mod time 60)
+ (/ duration 60) (mod duration 60))))
+ (fmt (cond ((and artist title)
+ (format "`%s' by %s%s" title artist
+ (if album (format ", from `%s'" album)
+ "")))
+ (file
+ (format "`%s' (no tags)" file))
+ (t
+ "(no idea what's playing!)"))))
+ (if (string= state "play")
+ (message "mpd playing %s%s" fmt (or pos ""))
+ (message "mpd paused in %s%s" fmt (or pos "")))))
+ (t
+ (message "mpd in unknown state `%s'" state)))))
+
+(defmacro mdw-define-mpc-wrapper (func bvl interactive &rest body)
+ `(defun ,func ,bvl
+ (interactive ,@interactive)
+ (require 'mpc)
+ ,@body
+ (mdw-mpc-now-playing)))
+
+(mdw-define-mpc-wrapper mdw-mpc-play-or-pause () nil
+ (if (member (cdr (assq 'state (mpc-cmd-status))) '("play"))
+ (mpc-pause)
+ (mpc-play)))
+
+(mdw-define-mpc-wrapper mdw-mpc-next () nil (mpc-next))
+(mdw-define-mpc-wrapper mdw-mpc-prev () nil (mpc-prev))
+(mdw-define-mpc-wrapper mdw-mpc-stop () nil (mpc-stop))
+
+(defun mdw-mpc-louder (step)
+ (interactive (list (if current-prefix-arg
+ (prefix-numeric-value current-prefix-arg)
+ +10)))
+ (mpc-proc-cmd (format "volume %+d" step)))
+
+(defun mdw-mpc-quieter (step)
+ (interactive (list (if current-prefix-arg
+ (prefix-numeric-value current-prefix-arg)
+ +10)))
+ (mpc-proc-cmd (format "volume %+d" (- step))))
+
+(defun mdw-mpc-hack-lines (arg interactivep func)
+ (if (and interactivep (use-region-p))
+ (let ((from (region-beginning)) (to (region-end)))
+ (goto-char from)
+ (beginning-of-line)
+ (funcall func)
+ (forward-line)
+ (while (< (point) to)
+ (funcall func)
+ (forward-line)))
+ (let ((n (prefix-numeric-value arg)))
+ (cond ((minusp n)
+ (unless (bolp)
+ (beginning-of-line)
+ (funcall func)
+ (incf n))
+ (while (minusp n)
+ (forward-line -1)
+ (funcall func)
+ (incf n)))
+ (t
+ (beginning-of-line)
+ (while (plusp n)
+ (funcall func)
+ (forward-line)
+ (decf n)))))))
+
+(defun mdw-mpc-select-one ()
+ (when (and (get-char-property (point) 'mpc-file)
+ (not (get-char-property (point) 'mpc-select)))
+ (mpc-select-toggle)))
+
+(defun mdw-mpc-unselect-one ()
+ (when (get-char-property (point) 'mpc-select)
+ (mpc-select-toggle)))
+
+(defun mdw-mpc-select (&optional arg interactivep)
+ (interactive (list current-prefix-arg t))
+ (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
+
+(defun mdw-mpc-unselect (&optional arg interactivep)
+ (interactive (list current-prefix-arg t))
+ (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-unselect-one))
+
+(defun mdw-mpc-unselect-backwards (arg)
+ (interactive "p")
+ (mdw-mpc-hack-lines (- arg) t 'mdw-mpc-unselect-one))
+
+(defun mdw-mpc-unselect-all ()
+ (interactive)
+ (setq mpc-select nil)
+ (mpc-selection-refresh))
+
+(defun mdw-mpc-next-line (arg)
+ (interactive "p")
+ (beginning-of-line)
+ (forward-line arg))
+
+(defun mdw-mpc-previous-line (arg)
+ (interactive "p")
+ (beginning-of-line)
+ (forward-line (- arg)))
+
+(defun mdw-mpc-playlist-add (&optional arg interactivep)
+ (interactive (list current-prefix-arg t))
+ (let ((mpc-select mpc-select))
+ (when (or arg (and interactivep (use-region-p)))
+ (setq mpc-select nil)
+ (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
+ (setq mpc-select (reverse mpc-select))
+ (mpc-playlist-add)))
+
+(defun mdw-mpc-playlist-delete (&optional arg interactivep)
+ (interactive (list current-prefix-arg t))
+ (setq mpc-select (nreverse mpc-select))
+ (mpc-select-save
+ (when (or arg (and interactivep (use-region-p)))
+ (setq mpc-select nil)
+ (mpc-selection-refresh)
+ (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
+ (mpc-playlist-delete)))
+
+(defun mdw-mpc-hack-tagbrowsers ()
+ (setq-local mode-line-format
+ '("%e"
+ mode-line-frame-identification
+ mode-line-buffer-identification)))
+(add-hook 'mpc-tagbrowser-mode-hook 'mdw-mpc-hack-tagbrowsers)
+
+(defun mdw-mpc-hack-songs ()
+ (setq-local header-line-format
+ ;; '("MPC " mpc-volume " " mpc-current-song)
+ (list (propertize " " 'display '(space :align-to 0))
+ ;; 'mpc-songs-format-description
+ '(:eval
+ (let ((deactivate-mark) (hscroll (window-hscroll)))
+ (with-temp-buffer
+ (mpc-format mpc-songs-format 'self hscroll)
+ ;; That would be simpler than the hscroll handling in
+ ;; mpc-format, but currently move-to-column does not
+ ;; recognize :space display properties.
+ ;; (move-to-column hscroll)
+ ;; (delete-region (point-min) (point))
+ (buffer-string)))))))
+(add-hook 'mpc-songs-mode-hook 'mdw-mpc-hack-songs)
+
+(eval-after-load "mpc"
+ '(progn
+ (define-key mpc-mode-map "m" 'mdw-mpc-select)
+ (define-key mpc-mode-map "u" 'mdw-mpc-unselect)
+ (define-key mpc-mode-map "\177" 'mdw-mpc-unselect-backwards)
+ (define-key mpc-mode-map "\e\177" 'mdw-mpc-unselect-all)
+ (define-key mpc-mode-map "n" 'mdw-mpc-next-line)
+ (define-key mpc-mode-map "p" 'mdw-mpc-previous-line)
+ (define-key mpc-mode-map "/" 'mpc-songs-search)
+ (setq mpc-songs-mode-map (make-sparse-keymap))
+ (set-keymap-parent mpc-songs-mode-map mpc-mode-map)
+ (define-key mpc-songs-mode-map "l" 'mpc-playlist)
+ (define-key mpc-songs-mode-map "+" 'mdw-mpc-playlist-add)
+ (define-key mpc-songs-mode-map "-" 'mdw-mpc-playlist-delete)
+ (define-key mpc-songs-mode-map "\r" 'mpc-songs-jump-to)))
+
+;;;--------------------------------------------------------------------------
+;;; Inferior Emacs Lisp.
+
+(setq comint-prompt-read-only t)
+
+(eval-after-load "comint"
+ '(progn
+ (define-key comint-mode-map "\C-w" 'comint-kill-region)
+ (define-key comint-mode-map [C-S-backspace] 'comint-kill-whole-line)))
+
+(eval-after-load "ielm"
+ '(progn
+ (define-key ielm-map "\C-w" 'comint-kill-region)
+ (define-key ielm-map [C-S-backspace] 'comint-kill-whole-line)))
+