(setq tab-width 4)
(set-frame-position (selected-frame) 0 0)
(set-frame-height (selected-frame) 59)

(global-font-lock-mode 1)

(global-set-key [backspace] 'delete-backward-char)
(define-key isearch-mode-map [backspace] 'isearch-delete-char)

(setq line-move-visual nil)

(defun my-shell-mode-hook ()
;(add-hook 'comint-output-filter-functions
;	    'shell-strip-ctrl-m nil t)
  (add-hook 'comint-output-filter-functions
	    'comint-watch-for-password-prompt nil t)
  (setq explicit-shell-file-name "bash")
  ;; For subprocesses invoked via the shell
  ;; (e.g., "shell -c command")
  (setq shell-file-name explicit-shell-file-name)
  (ansi-color-for-comint-mode-on)
  ;(shell-strip-ctrl-m)
  (setq shell-prompt-pattern "^\[[A-z0-9]+:[A-z0-9/]+\] ")
  (local-set-key "\C-xs" 'cycle-go-shell)
  (local-set-key "\C-xS" 'cycle-find-shell-or-shells-buffer-list)
  (local-set-key "\C-j" 'lfd-send-input)
  (local-set-key [f5] 'comint-previous-matching-input-from-input)
  (local-set-key [f12] 'compilation-shell-minor-mode)
  )

(put 'upcase-region 'disabled nil)
(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.
 '(delete-selection-mode t)
 '(ediff-diff-program "/bin/diff")
 '(kill-whole-line t)
 '(mouse-drag-copy-region nil)
 '(package-selected-packages
   '(tramp go-mode typescript-mode magit bash-completion json-mode))
 '(select-enable-clipboard t)
 '(shell-mode-hook '(my-shell-mode-hook))
 '(tab-width 4)
 '(tramp-verbose 1 t))
(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.
 '(default ((t (:family "Courier New" :foundry "outline" :slant normal :weight normal :height 98 :width normal)))))

(require 'ehelp)
(define-key global-map "\C-h" 'ehelp-command)
(define-key global-map [help] 'ehelp-command)
(define-key global-map [f1] 'ehelp-command)

;(iswitchb-default-keybindings)

;(require 'ido)
;(ido-mode t)
;(setq ido-use-filename-at-point 'guess)

;;(iswitchb-mode t)
;;(global-set-key "\C-xb" 'iswitchb-buffer)
(setq load-path (cons (concat (getenv "HOME") "/elisp") load-path))

(eval-after-load "icomplete" '(progn (require 'icomplete+)))
(require 'icomplete)
(icomplete-mode)

(load-library "buffer-cycle.el")
(load-library "cycle-shell.el")

(global-set-key "\C-xF" 'find-file-at-point)

(global-set-key "\C-x," 'backward-word)
(global-set-key "\C-x." 'forward-word)

(global-set-key "\C-x\C-d" 'kill-word)

(global-set-key "\C-x\C-e" 'cycle-make-shell)

(global-set-key "\C-x/" 'isearch-forward-regexp)
(global-set-key "\C-x?" 'isearch-backward-regexp)
(global-set-key "\C-x\C-r" 'query-replace-regexp)

(global-set-key "\C-x\C-l" 'goto-line)

(global-set-key "\C-x\C-f" 'find-file-other-window)
(global-set-key "\C-x4\C-f" 'find-file)

(global-set-key "\C-x\C-p" 'push-window-configuration)
(global-set-key "\C-x\C-k" 'exit-recursive-edit)
(global-set-key [f11] 'next-error)

(defun my-sgml-mode-hook ()
  (setq tab-width 4)
  (setq indent-tabs-mode nil)
  )

(add-hook 'sgml-mode-hook 'my-sgml-mode-hook)

(defun line-to-top-of-window ()
  "Scroll the current buffer so the line that the cursor is on becomes the top
line of the window"
  (interactive)
  (recenter 0))

(global-set-key "\C-x\C-O" 'line-to-top-of-window)

(defun lfd-send-input ()
  (interactive)
  (end-of-line)
  (comint-send-input)
  (if (not comint-process-echoes)
      (previous-line 1))
  (beginning-of-line)
  (line-to-top-of-window)
  )

(defun add-mode-line-dirtrack ()
  (add-to-list 'mode-line-buffer-identification 
	       '(:propertize (" " default-directory " ") face dired-directory)))
(add-hook 'shell-mode-hook 'add-mode-line-dirtrack)
(add-hook 'shell-mode-hook 'my-shell-mode-hook)

(defun copy-line (&optional arg)
  "Save the current line in the kill ring.  With a prefix argument, paste the
line on the following line."
  (interactive "P")
  (setq savepoint (point))
  (beginning-of-line)
  (setq spoint (point))
  (end-of-line)
  (forward-char 1)
  (setq epoint (point))
  (kill-ring-save spoint epoint)
  (if arg
      (yank)
    (goto-char savepoint)
      ))

;%% uncomment this if you want "copy-line" instead of "search-backward-regexp".
(global-set-key "\C-r" 'copy-line)

;(require 'compile-)
(load-library "compile")
;(require 'compile+)

(defun my-delete-indentation (&optional arg)
  "Interface to delete-indentation, but with reverse meaning for the prefix
argument."
  (interactive "P")
  (if arg
      (delete-indentation)
    (delete-indentation 1))
  )

(global-set-key "\C-x\C-j" 'my-delete-indentation)

;; Electric-help-mode
(require 'ehelp)
(load-library "ehelp")
(define-key global-map "\C-h" 'ehelp-command)
(define-key global-map [help] 'ehelp-command)
(define-key global-map [f1] 'ehelp-command)

;(shell)

(put 'downcase-region 'disabled nil)

(put 'set-goal-column 'disabled nil)

(defun my-java-mode-hook ()
  (setq c-tab-always-indent nil)
  )

(add-hook 'java-mode-hook 'my-java-mode-hook)

(put 'narrow-to-region 'disabled nil)

(global-set-key [backspace] 'delete-backward-char)

;; Add to your .emacs file. (from Steven Collins [spc.for.nbc@gmail.com])
;; Handle locating files containing errors for compilers that report using DOS style paths.
(when (eq system-type 'cygwin)
  (require 'compile)
  (setq compilation-parse-errors-filename-function
    '(lambda (path)
       (replace-regexp-in-string "\n" ""
          (shell-command-to-string (concat "cygpath --unix '" path "'"))))))

;; This is just very weird.  The first shell that executes doesn't execute
;; $HOME/.bashrc, but the successive ones do.  So, run the shell, then kill
;; the buffer.
;(shell)
;(kill-buffer "*shell*")
;(shell)

(and (= emacs-major-version 23) (defun server-ensure-safe-dir (dir) "Noop" t))

(add-hook 'after-init-hook 'server-start)
;(server-start 't)

(defun bf-pretty-print-xml-region (begin end)
  "Pretty format XML markup in region. You need to have nxml-mode
http://www.emacswiki.org/cgi-bin/wiki/NxmlMode installed to do
this.  The function inserts linebreaks to separate tags that have
nothing but whitespace between them.  It then indents the markup
by using nxml's indentation rules."
  (interactive "r")
  (save-excursion
      (nxml-mode)
      (goto-char begin)
      (while (search-forward-regexp "\>[ \\t]*\<" nil t) 
        (backward-char) (insert "\n"))
      (indent-region begin end))
    (message "Ah, much better!"))

(defun region-ms-to-datetime ()
  (interactive)
  (save-excursion
    (setq time-string (buffer-substring (region-beginning) (region-end)))
    (message (concat "time-string[" time-string "]"))
    (setq long-number (string-to-number time-string))
    (message (concat "long-number[" (number-to-string long-number) "]"))
;    (message (format-time-string "%Y/%m/%d %H:%M:%S" long-number))
    )
  )

(fset 'fixlastcommafirst
   [?\C-  ?\C-f ?\M-x ?u ?p ?c ?a ?s ?e ?- ?r ?e ?g ?i ?o ?n return ?\C-x ?. ?\C-  ?\C-x ?/ ?< ?\C-b ?\C-b ?\C-w ?\C-a ?\C-y ?  ?\C-a ?\C-d ?\C-d ?\C-  ?\C-f ?\M-x ?u ?p ?c ?a ?s ?e ?- ?r ?e ?g ?i ?o ?n return ?\C-e ?\C-f])

;;; Convert a line like this:
;;;    public void setStuff(int stuff) { this.stuff = stuff; }
;;;
;;; to:
;;;    public void setuff(int aStuff) { setStuff(aStuff); return this; }

(fset 'convert-setter-to-fluent-setter
   [?\C-x ?/ ?s ?e ?t ?\C-x ?, ?\C-d ?\C-d ?\C-d ?\C-  ?\C-f ?\M-x ?d ?o ?w ?n ?c ?a ?s ?e ?- ?r ?e ?g ?i ?o ?n return ?\C-x ?/ ?  ?\C-f ?\C-b ?a ?\C-  ?\C-f ?\M-x ?u ?p ?c ?a ?s ?e ?- ?r ?e ?g ?i ?o ?n return ?\C-x ?/ ?\{ ?\C-f ?\C-x ?\C-d ?\C-d ?s ?e ?t ?\C-  ?\C-f ?\M-x ?u ?p ?c ?a ?s ?e ?- ?r ?e ?g ?i ?o ?n return ?\C-x ?/ ?  ?\C-b ?\C-d ?\C-d ?\C-d ?\( ?\C-f ?\C-b ?a ?\C-  ?\C-f ?\M-x ?u ?p ?c ?a ?s ?e ?- ?r ?e ?g ?i ?o ?n return ?\C-x ?/ ?\; ?\C-b ?\) ?\C-f ?\C-f ?r ?e ?t ?u ?r ?n ?  ?t ?h ?i ?s ?\; ?  ?\C-e ?\C-f])

;;; use groovy-mode when file ends in .groovy or has #!/bin/groovy at start
(autoload 'groovy-mode "groovy-mode" "Major mode for editing Groovy code." t)
(add-to-list 'auto-mode-alist '("\.groovy$" . groovy-mode))
(add-to-list 'interpreter-mode-alist '("groovy" . groovy-mode))
(add-to-list 'auto-mode-alist '("\.gradle$" . groovy-mode))
(add-to-list 'interpreter-mode-alist '("gradle" . groovy-mode))

;;; make Groovy mode electric by default.
(add-hook 'groovy-mode-hook
          '(lambda ()
             (require 'groovy-electric)
	     (define-key groovy-mode-map "\C-x\C-e" nil)
             (groovy-electric-mode)))

;; Add package repo that has icicles.
(when (> emacs-major-version 23)
  (require 'package)
  (package-initialize)
  (add-to-list 'package-archives 
	       '("melpa" . "http://melpa.milkbox.net/packages/")
	       'APPEND))

;(load-library "icicles")
;(icicle-mode)

(require 'cl)

(setq remote-file-name-inhibit-cache nil)
(setq vc-ignore-dir-regexp
      (format "%s\\|%s"
                    vc-ignore-dir-regexp
                    tramp-file-name-regexp))
(setq tramp-verbose 1)

(require 'bash-completion)
(bash-completion-setup)

(require 'typescript-mode)
(add-to-list 'auto-mode-alist '(".tsx$" . typescript-mode))

(require 'yaml-mode)
