 ;; tree, which can be visualized in complex situations.
 ;;;;
 
-;;; Turn on Undo Tree everywhere.
-(global-undo-tree-mode 1)
-
-;;; Override the default binding of M-/ to dabbrev-expand.
-(global-set-key (kbd "M-/") 'undo-tree-redo)
-
-;;; Make undo history persistent between Emacs sessions.
-(setq undo-tree-auto-save-history t)
-
-;;; Put all the undo information in a single directory.
-(setq undo-tree-history-directory-alist '(("." . "~/.emacs-undos")))
+(when (member 'undo-tree radon-packages)
+
+  ;;; Turn on Undo Tree everywhere.
+  (global-undo-tree-mode 1)
+
+  ;;; Override the default binding of M-/ to dabbrev-expand.
+  (global-set-key (kbd "M-/") 'undo-tree-redo)
+
+  ;;; Make undo history persistent between Emacs sessions.
+  (setq undo-tree-auto-save-history t)
+
+  ;;; Put all the undo information in a single directory.
+  (setq undo-tree-history-directory-alist '(("." . "~/.emacs-undos"))))
 
 ;;;; Package: Magit
 ;; A git porcelain inside Emacs.
 ;;;;
 
-;;; Create a keybinding for magit-status, as suggested in:
-;;; https://magit.vc/manual/magit/Getting-started.html#Getting-started
-(global-set-key (kbd "C-x g") 'magit-status)
+(when (member 'magit radon-packages)
+
+  ;;; Create a keybinding for magit-status, as suggested in:
+  ;;; https://magit.vc/manual/magit/Getting-started.html#Getting-started
+  (global-set-key (kbd "C-x g") 'magit-status))
 
 ;;;; Package: IDO
 ;; Makes completion more intelligent by using fuzzy matching and better

;;; Solarized Light theme in your terminal emulator).
(setq radon-tweak-colors t)

;;;; User-specific configuration (1 of 4).
(radon-load-user-config "init.local.el")

;;;; Appearance

;;; Disable the "For information about GNU Emacs..." message at startup,
;;; for *all* users.
(defun display-startup-echo-area-message ())

;;; Disable the *About GNU Emacs* buffer at startup, and go straight for
;;; the scratch buffer. This is especially useful because Projectile won't
;;; work in the startup buffer, which is annoying.
(setq inhibit-startup-screen t)

;;; Disable the menu bar, as it doesn't seem very useful...
(menu-bar-mode -1)

;;; When point is on a paren, highlight the matching paren instantly.
(setq show-paren-delay 0)
(show-paren-mode 1)

;;;; Elisp customization

;;; This is required to have access to some basic data manipulation
;;; functions, like cl-every. Why aren't these available by default?
(require 'cl-lib)

;;; Define keybindings for opening and reloading init.el.

(defun open-initfile ()
  (interactive)
  (find-file "~/.emacs.d/init.el")
  "Opens init.el in the current buffer.")

(defun reload-initfile ()
  (interactive)
  (load-file "~/.emacs.d/init.el")
  "Reloads init.el.")

(global-set-key (kbd "<f9>") 'open-initfile)
(global-set-key (kbd "<f12>") 'reload-initfile)

;;;; OSX interop

;;; Add mouse support
;;; Based on http://stackoverflow.com/a/8859057/3538165
(unless (display-graphic-p)
  (xterm-mouse-mode t)
  ;; Enable scrolling.
  (global-set-key [mouse-4]
                  (lambda ()
                    (interactive)
                    (scroll-down 1)))
  (global-set-key [mouse-5]
                  (lambda ()
                    (interactive)
                    (scroll-up 1))))

;;; Add clipboard support
;;; Based on https://gist.github.com/the-kenny/267162

(defun copy-from-osx ()
  (shell-command-to-string "pbpaste"))

(defun paste-to-osx (text &optional push)
  (let ((process-connection-type nil))
    (let ((proc (start-process "pbcopy" "*Messages*" "pbcopy")))
      (process-send-string proc text)
      (process-send-eof proc))))

(setq interprogram-cut-function 'paste-to-osx)
(setq interprogram-paste-function 'copy-from-osx)

;;;; File saving and loading

;;; Put backup files in $HOME/.emacs-backups, rather than in the current
;;; directory.
(setq backup-directory-alist '(("." . "~/.emacs-backups")))

;;; Always use copying to make backup files. This prevents links from
;;; being made to point at the backup file rather than the original.
(setq backup-by-copying t)

;;; Keep multiple numbered backup files, rather than a single unnumbered
;;; backup file.
(setq version-control t)

;;; Delete old backups silently, instead of asking for confirmation.
(setq delete-old-versions t)

;;; Don't make autosave files.
(setq auto-save-default nil)

;;; Trim trailing whitespace on save. This will get rid of end-of-line
;;; whitespace, and reduce the number of blank lines at the end of the
;;; file to one.
(add-hook 'before-save-hook 'delete-trailing-whitespace)

;;; Add a trailing newline if there is not one already, when
;;; saving. This is enabled for default for certain modes, but we want
;;; it everywhere (e.g. when editing .gitignore files).
(setq require-final-newline t)

;;; Automatically reload files that were changed on disk, if they have
;;; not been modified in Emacs since the last time they were saved.
(global-auto-revert-mode 1)

;;; Turn the delay on auto-reloading from 5 seconds down to 1 second.
(setq auto-revert-interval 1)

;;; Follow symlinks without prompting. If this isn't done, then you will
;;; get a prompt every time you edit init.el with F9. (This is assuming
;;; that ~/.emacs.d/init.el is a symlink, which is how the setup script
;;; sets it up.)
(setq vc-follow-symlinks t)

;;; Suppress the 'ls does not support --dired' warning. Doing this instead
;;; of installing a dired-compatibile ls is much easier, although it may
;;; cause problems with e.g. filenames that have leading spaces. If you
;;; have a lot of filenames with leading spaces, though, you probably have
;;; bigger problems ;)
(setq dired-use-ls-dired nil)

;;;; Text format

;;; Don't use tabs for indentation, even in deeply indented lines. (Why
;;; would anyone want their editor to *sometimes* use tabs?)
(setq-default indent-tabs-mode nil)

;;; Sentences end with one space, not two. If you end sentences with
;;; one space, this has two concrete effects on filling with M-q.
;;; Firstly, it allows Emacs to break directly after a sentence.
;;; Secondly, consider the case when the end of a line has an
;;; end-of-sentence period followed by trailing whitespace and when at
;;; least the last word of the sentence is too long for the line. In
;;; that case, this option prevents filling from inserting two spaces
;;; after the period when it is wrapped to the next line.
(setq sentence-end-double-space nil)

;;;; Packages
;; Downloads any packages that are not included with Emacs 24 by default.
;; This allows the setup to run on other systems without any additional
;; setup (other than Emacs 24 being installed).
;;;;

;;; Based on http://batsov.com/articles/2012/02/19/package-management-in-emacs-the-good-the-bad-and-the-ugly/

;;; Initialize the package management system, before we start trying
;;; to add packages.
(require 'package)
(package-initialize)

;;; The default package repository in Emacs doesn't have a lot of the
;;; packages we need, such as Projectile. Therefore, add the melpa-stable
;;; repository. We need melpa (non-stable) for helm-smex.
(add-to-list 'package-archives
             '("melpa-stable" . "https://stable.melpa.org/packages/")
             t) ; this appends to the end of the list
(add-to-list 'package-archives
             '("melpa" . "https://melpa.org/packages/")
             t) ; this appends to the end of the list

(defvar radon-packages
  '(
    ace-jump-mode ; quickly jump to words, characters, or lines onscreen

    )
  "The packages required by this Emacs setup.")

;;;; User-specific configuration (1 of 4).
(radon-load-user-config "init.local.el")

;;;; Appearance

;;; Disable the "For information about GNU Emacs..." message at startup,
;;; for *all* users.
(defun display-startup-echo-area-message ())

;;; Disable the *About GNU Emacs* buffer at startup, and go straight for
;;; the scratch buffer. This is especially useful because Projectile won't
;;; work in the startup buffer, which is annoying.
(setq inhibit-startup-screen t)

;;; Disable the menu bar, as it doesn't seem very useful...
(menu-bar-mode -1)

;;; When point is on a paren, highlight the matching paren instantly.
(setq show-paren-delay 0)
(show-paren-mode 1)

;;;; Elisp customization

;;; This is required to have access to some basic data manipulation
;;; functions, like cl-every. Why aren't these available by default?
(require 'cl-lib)

;;; Define keybindings for opening and reloading init.el.

(defun open-initfile ()
  (interactive)
  (find-file "~/.emacs.d/init.el")
  "Opens init.el in the current buffer.")

(defun reload-initfile ()
  (interactive)
  (load-file "~/.emacs.d/init.el")
  "Reloads init.el.")

(global-set-key (kbd "<f9>") 'open-initfile)
(global-set-key (kbd "<f12>") 'reload-initfile)

;;;; OSX interop

;;; Add mouse support
;;; Based on http://stackoverflow.com/a/8859057/3538165
(unless (display-graphic-p)
  (xterm-mouse-mode t)
  ;; Enable scrolling.
  (global-set-key [mouse-4]
                  (lambda ()
                    (interactive)
                    (scroll-down 1)))
  (global-set-key [mouse-5]
                  (lambda ()
                    (interactive)
                    (scroll-up 1))))

;;; Add clipboard support
;;; Based on https://gist.github.com/the-kenny/267162

(defun copy-from-osx ()
  (shell-command-to-string "pbpaste"))

(defun paste-to-osx (text &optional push)
  (let ((process-connection-type nil))
    (let ((proc (start-process "pbcopy" "*Messages*" "pbcopy")))
      (process-send-string proc text)
      (process-send-eof proc))))

(setq interprogram-cut-function 'paste-to-osx)
(setq interprogram-paste-function 'copy-from-osx)

;;;; File saving and loading

;;; Put backup files in $HOME/.emacs-backups, rather than in the current
;;; directory.
(setq backup-directory-alist '(("." . "~/.emacs-backups")))

;;; Always use copying to make backup files. This prevents links from
;;; being made to point at the backup file rather than the original.
(setq backup-by-copying t)

;;; Keep multiple numbered backup files, rather than a single unnumbered
;;; backup file.
(setq version-control t)

;;; Delete old backups silently, instead of asking for confirmation.
(setq delete-old-versions t)

;;; Don't make autosave files.
(setq auto-save-default nil)

;;; Trim trailing whitespace on save. This will get rid of end-of-line
;;; whitespace, and reduce the number of blank lines at the end of the
;;; file to one.
(add-hook 'before-save-hook 'delete-trailing-whitespace)

;;; Add a trailing newline if there is not one already, when
;;; saving. This is enabled for default for certain modes, but we want
;;; it everywhere (e.g. when editing .gitignore files).
(setq require-final-newline t)

;;; Automatically reload files that were changed on disk, if they have
;;; not been modified in Emacs since the last time they were saved.
(global-auto-revert-mode 1)

;;; Turn the delay on auto-reloading from 5 seconds down to 1 second.
(setq auto-revert-interval 1)

;;; Follow symlinks without prompting. If this isn't done, then you will
;;; get a prompt every time you edit init.el with F9. (This is assuming
;;; that ~/.emacs.d/init.el is a symlink, which is how the setup script
;;; sets it up.)
(setq vc-follow-symlinks t)

;;; Suppress the 'ls does not support --dired' warning. Doing this instead
;;; of installing a dired-compatibile ls is much easier, although it may
;;; cause problems with e.g. filenames that have leading spaces. If you
;;; have a lot of filenames with leading spaces, though, you probably have
;;; bigger problems ;)
(setq dired-use-ls-dired nil)

;;;; Text format

;;; Don't use tabs for indentation, even in deeply indented lines. (Why
;;; would anyone want their editor to *sometimes* use tabs?)
(setq-default indent-tabs-mode nil)

;;; Sentences end with one space, not two. If you end sentences with
;;; one space, this has two concrete effects on filling with M-q.
;;; Firstly, it allows Emacs to break directly after a sentence.
;;; Secondly, consider the case when the end of a line has an
;;; end-of-sentence period followed by trailing whitespace and when at
;;; least the last word of the sentence is too long for the line. In
;;; that case, this option prevents filling from inserting two spaces
;;; after the period when it is wrapped to the next line.
(setq sentence-end-double-space nil)

;;;; Packages
;; Downloads any packages that are not included with Emacs 24 by default.
;; This allows the setup to run on other systems without any additional
;; setup (other than Emacs 24 being installed).
;;;;

;;; Based on http://batsov.com/articles/2012/02/19/package-management-in-emacs-the-good-the-bad-and-the-ugly/

;;; Initialize the package management system, before we start trying
;;; to add packages.
(require 'package)
(package-initialize)

;;; The default package repository in Emacs doesn't have a lot of the
;;; packages we need, such as Projectile. Therefore, add the melpa-stable
;;; repository. We need melpa (non-stable) for helm-smex.
(add-to-list 'package-archives
             '("melpa-stable" . "https://stable.melpa.org/packages/")
             t) ; this appends to the end of the list
(add-to-list 'package-archives
             '("melpa" . "https://melpa.org/packages/")
             t) ; this appends to the end of the list

;;; Ensure that we get a stable version of CIDER, and not a snapshot version.
;;; This resolved an issue with the 0.13.0 update to CIDER trying to inject
;;; a nonexistent snapshot version of cider-nrepl, if you used the snapshot
