;; GETTING STARTED

;; INITIALIZE MELPA
;; Keep this on top
;; https://melpa.org/#/getting-started
(require 'package)
(add-to-list 'package-archives
             '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)

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

;; SOME BASIC PREFERENCES
;; Revert buffers when the underlying file has changed
(global-auto-revert-mode 1)
;; turns off the menus at top
(menu-bar-mode -1)
(tool-bar-mode -1)
;; enable word wrap
(global-visual-line-mode t)
;; 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
(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)

;; 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 (docx, epub, [html], odt, pdf, pptx, reveal, 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 "reveal")
	  (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 ~/.revealjs/):
	  (setq file-type "revealjs --self-contained -V revealjs-url='/home/ntnsndr/.revealjs/reveal.js' -V theme=ntn -V controls=false --slide-level=2"))
    ;; set up slides pptx routine
    (when (string-equal file-type "pptx")
      (setq file-ext "pptx")
      (setq file-type "pptx --reference-doc='/home/ntnsndr/.templates/Slideshow.pptx'"))
	;; proceed
    (setq tmp-in-file "/tmp/pandoc-in.tmp")
    (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))
   )
)
(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.
 '(package-selected-packages (quote (lua-mode wc-mode markdown-mode))))
(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.
 )
