(message "* --[ Loading my Emacs init file ]--")
(message "* --[ * Created by TSheffield *  ]--")
(message "* --[  Note: REQUIRES PLUG-INS   ]--")

;; set word wrap style as default or no line wrap
;;(global-visual-line-mode t)
(set-default 'truncate-lines t)

;; make vertical buffer split the default if applied
(setq split-width-threshold 1)

;;add disaster.el disassembly viewer, uses ctrl-shift-d
(add-to-list 'load-path "/home/tyler/.emacs.d/addons")
(load "disaster.el")
(require 'disaster)
(global-unset-key (kbd "C-S-d"))
(global-set-key (kbd "C-S-d") 'disaster)

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

;; shows cursor column num after line (line#,col#)
(setq column-number-mode t)

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

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

;; set colors
(set-background-color "#2f2f2f")
(set-foreground-color "white")

;; set indent
;;(setq-default standard-indent 3)
;; turns off tabs for indentation
(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)
;;(setq tab-width 3)
(defvaralias 'c-basic-offset 'tab-width)
(setq tab-stop-list (number-sequence 4 120 4))
;;(setq tab-stop-list (number-sequence 8 120 8))
;; If you wish to change the amount of spaces that the Tab key inserts, check out the TabStopList page. Do not confuse this with tab-width, which is only good for displaying existing tabs in files!

;; set shell script mode indentation default (2 is for HR coding style)
(setq-default sh-indentation 2)
;; none of this crap works, emacs ignores it
(setq-default sh-indent-after-continuation nil)
(setq-default sh-indent-for-continuation 0)

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

;; 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/addons)
(require 'cl)
(add-to-list 'load-path "~/.emacs.d/addons")
(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 and stop split screen on init
(setq inhibit-startup-screen t)
(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(inhibit-startup-screen t)
 '(truncate-lines t))
(add-hook 'emacs-startup-hook
          (lambda () (delete-other-windows)) 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))
    )
  )

;; buffer name sort idea from https://www.emacswiki.org/emacs/BufferMenuSorting
(defun sort-buffers ()
    "Put the buffer list in alphabetical order."
    (interactive)
    (dolist (buff (buffer-list-sorted)) (bury-buffer buff))
    (when (interactive-p) (list-buffers))
    )
  
  (defun buffer-list-sorted ()
    (sort (buffer-list) 
  	(function
  	 (lambda
  	   (a b) (string<
  		  (downcase (buffer-name a))
  		  (downcase (buffer-name b))
  		  )))))

(global-set-key "\M-b"    'sort-buffers)

;; 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)

;; set word wrap style as default
(global-visual-line-mode t)

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

;; set default font and size
(set-default-font "Ubuntu Mono 11" nil t)
;;(print (font-family-list)) 

;; highlight web links in text
(defun do-web-highlight ()
  (highlight-phrase "http:.*" 'hi-blue)
  (highlight-phrase "https:.*" 'hi-blue)
  )
(add-hook 'find-file-hook 'do-web-highlight)

;;add keys to scroll window up/down by one line
(global-set-key (kbd "M-<down>") (kbd "C-u 1 C-v"))
(global-set-key (kbd "M-<up>") (kbd "C-u 1 M-v"))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )


