(message "* --[ Loading my Emacs init file ]--")
(message "* --[ * Created by TSheffield *  ]--")

(defun c-mode-common-hook ()
  (c-toggle-electric-state -1)
  (c-set-style "k&r")
  )
;;(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)


;; disable C-z kill and set to undo
(global-set-key "\C-Z" nil)
(global-set-key "\C-Z" 'undo)

;; replace selected text on paste
(delete-selection-mode)

;; stop creating ~ backup files
(setq make-backup-files nil)

;; enable clipboard share
(setq x-select-enable-clipboard t)

;; set colors
(set-background-color "black")
(set-foreground-color "white")

;; set indent
(setq-default standard-indent 4)
(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)
;; (setq tab-width 3)
(defvaralias 'c-basic-offset 'tab-width)
;; this generates tab stops from 4 to 120 by 4, defines alt+i (tab-to-tab-stop) operation
(setq tab-stop-list (number-sequence 4 120 4))

;; set indent style by default
(setq c-default-style "linux"
      c-basic-offset 0)

;; line highlighting
(defvar running-xemacs (string-match "XEmacs" emacs-version))
(if (not running-xemacs)
    (delete-selection-mode 1))

;; no line wrap
(set-default 'truncate-lines t)

;; show cursor column position within line
(column-number-mode 1)

;; option to scroll by one line, no jumps
(global-set-key (quote [M-down]) (quote scroll-up-line))
(global-set-key (quote [M-up]) (quote scroll-down-line))

;; add block comment capability
(global-set-key (kbd "C-;") 'comment-region)
(global-set-key (kbd "C-'") 'uncomment-region)

;; show file path
(setq frame-title-format
      (list (format "%s %%S: %%j " (system-name))
        '(buffer-file-name "%f" (dired-directory dired-directory "%b"))))

;; make Completions buffer disappear for good
(add-hook 'minibuffer-exit-hook 
      '(lambda ()
         (let ((buffer "*Completions*"))
           (and (get-buffer buffer)
            (kill-buffer buffer)))))

;; view scons files in Python mode
(setq auto-mode-alist
      (cons '("SConstruct" . python-mode) auto-mode-alist))
(setq auto-mode-alist
      (cons '("SConscript" . python-mode) auto-mode-alist))

;; use the ibuffer add-on
(require 'ibuffer)
;(global-set-key (kbd "C-x C-b") 'ibuffer-other-window) - opens in split
;(global-set-key (kbd "C-x C-b") 'buffer-menu) - opens emacs default buffer list
(global-set-key (kbd "C-x C-b") 'ibuffer)
(setq ibuffer-default-sorting-mode 'alphabetic)

;; try buffer stack add-on (MUST first download buffer-stack.el and place into .emacs.d)
(require 'cl)
(add-to-list 'load-path "~/.emacs.d/")
(load "buffer-stack.el")
(require 'buffer-stack)
(global-set-key [(f5)] 'buffer-stack-rebuild )
(global-set-key [(f9)] 'buffer-stack-bury)
(global-set-key [(control f9)] 'buffer-stack-bury-and-kill)
(global-set-key [(f10)] 'buffer-stack-up)
(global-set-key [(f11)] 'buffer-stack-down)
(global-set-key [(f12)] 'buffer-stack-track)
(global-set-key [(control f12)] 'buffer-stack-untrack)

;; fix next and prev buffer commands
(global-set-key (kbd "M-<right>") 'next-buffer)
(global-set-key (kbd "M-<left>") 'previous-buffer)

;; be rid of useless buffers
(custom-set-variables '(inhibit-startup-screen t) )
;; (kill-buffer "*scratch*")

;; work on sorting buffer list goes here
(defun reorder-buffer-list (new-list)
  (while new-list
    (bury-buffer (car new-list))
    (setq new-list (cdr new-list))))

;; set up backtab
(global-set-key (kbd "<backtab>") 'un-indent-by-removing-4-spaces)
(defun un-indent-by-removing-4-spaces ()
  "remove 4 spaces from beginning of of line"
  (interactive)
  (save-excursion
    (save-match-data
      (beginning-of-line)
      ;; get rid of tabs at beginning of line
      (when (looking-at "^\\s-+")
        (untabify (match-beginning 0) (match-end 0)))
      (when (looking-at "^    ")
        (replace-match ""))
      (when (looking-at "^  ")
        (replace-match ""))
      (when (looking-at "^ ")
	(replace-match "")))))

;; set up block tab
(global-set-key [C-tab] 'indent-for-tab-command)

;; copy file path to clipboard
(global-set-key (kbd "C-c C-f") 'copy-file-name-to-clipboard)
(defun copy-file-name-to-clipboard ()
  "Put the current file name on the clipboard"
  (setq select-enable-clipboard nil)
  (setq select-enable-primary t)
  (interactive)
  (let ((filename (if (equal major-mode 'dired-mode)
                      default-directory
                    (buffer-file-name))))
    (when filename
      (with-temp-buffer
        (insert filename)
        (kill-region (point-min) (point-max)))
      (message filename))))

