;; GETTING STARTED

;; INITIALIZE MELPA PACKAGES
;; Keep this on top
;; https://melpa.org/#/getting-started
(require 'package)
(let* ((no-ssl (and (memq system-type '(windows-nt ms-dos))
                    (not (gnutls-available-p))))
       (url (concat (if no-ssl "http" "https") "://melpa.org/packages/")))
  (add-to-list 'package-archives (cons "melpa" url) t))
(when (< emacs-major-version 24)
  ;; For important compatibility libraries like cl-lib
  (add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/")))
(package-initialize)

;; setting the default directory for extra packages
(add-to-list 'load-path "~/.emacs.d/lisp/")

;; SOME BASIC PREFERENCES
;; turns off the menu at top
(menu-bar-mode -1) 
;; enable longlines-mode for text files
(add-hook 'text-mode-hook 'visual-line-mode)
;; load word-count tools, use "wc" command to get instant count
(load "wc-mode")
;; but normally don't initialize because it's too CPU-intensive on large files
;; (add-hook 'text-mode-hook 'wc-mode)
;; enable apsell as default spell checker
(setq-default ispell-program-name "aspell")
;; set window margins
;; (a bit buggy on multiple-window startup, but resolves)
(add-hook 'window-configuration-change-hook
          (lambda ()
            (set-window-margins (car 
				 (get-buffer-window-list 
				  (current-buffer) nil t)) 2 2)))
;; Set markdown-mode as default for markdown files
;; (can always opt out into text-mode)
(autoload 'markdown-mode "markdown-mode" "Major mode for editing Markdown files" t)
(add-to-list 'auto-mode-alist '("\\.markdown\\'" . markdown-mode))
(add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-mode))
;; Turn on Flyspell (no need for lazy-mode, it seems; perhaps delete that)
;;(require 'flyspell-lazy)
;;(flyspell-lazy-mode 1)
(dolist (hook '(text-mode-hook))
  (add-hook hook (lambda ()
                   (flyspell-mode 1)
                   (set-variable 'flyspell-issue-message-flag nil)
                   (flyspell-buffer))))
;; 4-space tabs as default
(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)
(setq indent-line-function 'insert-tab)

;; BACKUP CONTROL
;; source: http://is.gd/ag5KOC
(setq backup-directory-alist '(("." . "~/.emacs.d/backup"))
  backup-by-copying t    ; Don't delink hardlinks
  version-control t      ; Use version numbers on backups
  delete-old-versions t  ; Automatically delete excess backups
  kept-new-versions 20   ; how many of the newest versions to keep
  kept-old-versions 5    ; and how many of the old
  )

;; HORIZONTAL WINDOWS
;; open horizontal windows as default for multiple files
(defun 2-windows-vertical-to-horizontal ()
  (let ((buffers (mapcar 'window-buffer (window-list))))
    (when (= 2 (length buffers))
      (delete-other-windows)
      (set-window-buffer (split-window-horizontally) (cadr buffers)))))
(add-hook 'emacs-startup-hook '2-windows-vertical-to-horizontal)

;; DICT
;; For getting definitions of words
(defun dict ()
  (interactive)
  (let (word)
    (setq word (read-string "Define: "))
    (shell-command (concat "dict " word))))

;; ASCIIFY
;; replace complex curly quotes with ASCII straight ones
;; replace em-dash with ---
(defun asciify ()
  (interactive)
  (setq start (point))
  (beginning-of-buffer)
  (while (search-forward "“" nil t) (replace-match "\"" nil t))
  (beginning-of-buffer)
  (while (search-forward "”" nil t) (replace-match "\"" nil t))
  (beginning-of-buffer)
  (while (search-forward "‘" nil t) (replace-match "\'" nil t))
  (beginning-of-buffer)
  (while (search-forward "’" nil t) (replace-match "\'" nil t))
  (beginning-of-buffer)
  (while (search-forward "—" nil t) (replace-match "---" nil t))
  (goto-char start))

;; PANDOC
;; for converting markdown text in buffer to another format and opening it
;; file-type argument must be a pandoc-recognized file format
;; to specify .csl file for bibliography, use YAML metadata "csl:" command
(defun pandoc ()
  (interactive)
  (let (file-type file-ext tmp-out-file tmp-out-file out-file current-dir)
    (setq file-type (read-string "File type (epub, [html], odt, pdf, slides, etc.): "))
	(setq file-ext file-type)
	;; set default as html
    (when (string-equal file-type "")
	  (setq file-type "html")
	  (setq file-ext "html"))
    ;; set up pdf routine
    (when (string-equal file-type "pdf")
      (setq file-type "latex --variable fontsize='11pt'"))
	;; set up slides revealjs routine
	(when (string-equal file-type "slides")
	  (setq file-ext "html")
	  ;; Light approach, which requires web access and readable code (but speaker notes and relative paths don't work):
	  ;; (setq file-type "revealjs -V revealjs-url='http://lab.hakim.se/reveal-js' --standalone"))
	  ;; Heavy embedded binary approach (relies on reveal.js/ being in ~/.pandoc/):
	  (setq file-type "revealjs --self-contained -V revealjs-url='/home/ntnsndr/.revealjs/reveal.js' -V theme=ntn -V controls=false --slide-level=2"))
	;; proceed
    (setq tmp-in-file "/tmp/pandoc-in.txt")
    (setq tmp-out-file (concat "/tmp/pandoc-out." file-ext))
    (write-region nil nil tmp-in-file)
    (shell-command (concat
                    "iconv -t utf-8 " tmp-in-file " | " ;; pipe in utf-8 for pandoc
                    "pandoc -s" ;; embed full header in HTML
                    " -f markdown+smart" ;; add curly quotes
                    " --bibliography $HOME/Zotero/lib.bib" ;; connect to BibTeX export from Zotero
                    " -t " file-type
                    " -o " tmp-out-file))
;; End convert routine
    (shell-command (concat "xdg-open " tmp-out-file))
   )
)
