(use-package org
  :config
  (defvar org-outline-path-complete-in-steps nil)
  :bind (("C-c l" . org-store-link)
	 ("C-c a" . org-agenda)
	 ("C-c c" . org-capture)
	 :map org-mode-map)
  )

(setq org-directory (expand-file-name "~/memex/"))

(defvar org-pretty-entities t) ; Improve org mode looks
(defvar org-hide-emphasis-markers t) ; Hide emphasis markup
(defvar org-num-mode nil)
(defvar org-startup-folded 'shw2levels)

(defvar org-startup-indented t) ; Indent org heirarchy
(defvar org-adapt-indentation t)
(defvar org-hide-leading-stars t) ; Minimal Outline
(defvar org-odd-levels-only nil)

(setq org-list-demote-modify-bullet t)

(use-package org-modern
  :ensure t
  :config
      ;; Choose some fonts
  (set-face-attribute 'default nil :family "sans-serif")
  (set-face-attribute 'variable-pitch nil :family "sans-serif")
  (set-face-attribute 'org-modern-symbol nil :family "Iosevka")

  ;; Edit settings
  (defvar org-auto-align-tags nil)
  (defvar org-tags-column 0)
  (defvar org-catch-invisible-edits 'show-and-error)
  (defvar org-special-ctrl-a/e t)
  (defvar org-insert-heading-respect-content t)

   ;; Org styling, hide markup etc.
   (defvar org-hide-emphasis-markers t)
   (defvar org-pretty-entities t)

   ;; Agenda styling
   (defvar org-agenda-tags-column 0)
   (defvar org-agenda-block-separator ?─)
   (defvar org-agenda-time-grid
     '((daily today require-timed)
       (800 1000 1200 1400 1600 1800 2000)
       " ┄┄┄┄┄ " "┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄"))
   (defvar org-agenda-current-time-string
     "◀── now ─────────────────────────────────────────────────")

  ;; Ellipsis styling
   (defvar org-ellipsis "…")
   (set-face-attribute 'org-ellipsis nil :inherit 'default :box nil)
  
   (global-org-modern-mode)
  )

(setq org-src-fontify-natively t)
(setq org-src-tab-acts-natively t)

(setq org-startup-with-inline-images t)
(setq org-image-actual-width '(300))

(defvar org-default-notes-file (concat org-directory "inbox.org"))

(require 'org-protocol)
(setq org-protocol-default-buffer-for-file-links "*scratch*") ; fixes 'no buffers remain to edit error for org-protocol capturer

(setq org-protocol-default-template-key "L")

(defun my/org-convert-orgzly-to-org-protocol ()
  "Reformat Orgzly bookmark at point to org-protocol bookmark."
  (interactive)
  (when (org-at-heading-p)
    (let ((headline (nth 4 (org-heading-components))))
      ;; Find and store the link. Delete the link line.
      (search-forward-regexp "^https?://\\S-*" nil t)
      (let ((link (match-string 0)))
        (beginning-of-line)
        (kill-line)
        ;; Delete any trailing blank spaces
        (org-back-to-heading)
        (end-of-line)
        (when (not (org-on-heading-p))
          (delete-char 1)
          )
        ;; Set new headline
        (goto-char (org-entry-beginning-position))
        (org-edit-headline (format "[[%s][%s]]" link headline))
        ;; Set new properties
        (org-set-property "TITLE" headline)
        (org-set-property "URI" link)
        (message "Reformatted Orgzly bookmark at point to org-protocol bookmark")
        )
      )
    )
  )

(setq org-export-with-smart-quotes t)
(setq org-export-backends '(beamer html latex md))

(use-package ox-epub)

(setq org-attach-id-dir (concat org-directory "/library"))

(defvar org-babel-do-load-languages  'org-babel-load-languages  '((shell . t)))

(use-package org-cliplink
  :bind
  (("C-x p i" . org-cliplink))
  )

(use-package deft
  :commands (deft)
  :init
  (defvar deft-extensions '("org"))
  (defvar deft-recursive nil)
  (defvar deft-use-filename-as-title t)
  :config
  (defvar deft-directory org-directory)
  (defvar deft-recursive t)
  (defvar deft-strip-summary-regexp ":PROPERTIES:\n\\(.+\n\\)+:END:\n")
  (defvar deft-use-filename-as-title t)
  :bind  ("C-c n d" . deft)
  )
