#+TITLE: Org Mode Configuration
#+PROPERTY: header-args :tangle ~/.emacs.d/config.el

* Org Mode

** Basic setup

#+begin_src elisp
  (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)
    )
#+end_src

#+begin_src elisp
  (defvar org-directory (concat (getenv "HOME") "/memex/"))
#+end_src

** Looks

Basic 
#+begin_src elisp
  (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)
#+end_src

Indentation of headers
#+begin_src elisp
  (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)
#+end_src

Indentation of lists
#+begin_src elisp
  (setq org-list-demote-modify-bullet t)
#+end_src

Org-modern
#+begin_src elisp
  (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)
    )
#+end_src

Highlight Sourcecode Syntax
#+begin_src elisp
  (setq org-src-fontify-natively t)
  (setq org-src-tab-acts-natively t)
#+end_src

Images
#+begin_src elisp
  (setq org-startup-with-inline-images t)
  (setq org-image-actual-width '(300))
#+end_src

** Capture

#+begin_src elisp
  (defvar org-default-notes-file (concat org-directory "inbox.org"))
#+end_src

*** Org-protocol

Linux configuration
#+begin_src bash :tangle no
[Desktop Entry]
Name=org-protocol
Comment=Intercept calls from emacsclient to trigger custom actions
Categories=Other;
Keywords=org-protocol;
Icon=emacs
Type=Application
Exec=emacsclient -- %u
Terminal=false
StartupWMClass=Emacs
MimeType=x-scheme-handler/org-protocol;
#+end_src

#+begin_src bash :tangle no
update-desktop-database ~/.local/share/applications/
#+end_src

Basic configuration
#+begin_src elisp
  (require 'org-protocol)
  (setq org-protocol-default-buffer-for-file-links "*scratch*") ; fixes 'no buffers remain to edit error for org-protocol capturer
#+end_src

Org-protocol templates
#+begin_src elisp :tangle ~/.emacs.d/custom.el
  (defvar org-capture-templates '(
                                ("p" "Protocol"
                                 entry
                                 (file "inbox.org")
                                 "* %^{Title}\nSource: %u, %c\n #+BEGIN_QUOTE\n%i\n#+END_QUOTE\n\n\n%?"
                                 )
                                ("L" "Protocol Link"
                                 entry
                                 (file "inbox.org")
                                 "* %? [[%:link][%:description]]\n:PROPERTIES:\n:TITLE: %:description\n:URI: %:link\n:CREATED: %U\n:END:"
                                 :prepend nil
                                 :empty-lines 1
                                 :created t
                                 :kill-buffer t
                                 )
                                )
        )
#+end_src

#+begin_src elisp
  (setq org-protocol-default-template-key "L")
#+end_src

Convert Orgzly captures to org-protocol captures standard
#+begin_src elisp
  (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")
          )
        )
      )
    )
#+end_src

** Exporting

#+begin_src elisp :tangle no
  (setq org-export-with-smart-quotes t)
  (setq org-export-backends '(beamer html latex md))
#+end_src

Export to EPUB
#+begin_src elisp :tangle no
  (use-package ox-epub)
#+end_src

** org-attach

#+begin_src elisp
  (defvar org-attach-id-dir (concat org-directory "/library"))
#+end_src

** Enable shell scripting support in org-babel

#+begin_src elisp
  (defvar org-babel-do-load-languages  'org-babel-load-languages  '((shell . t)))
#+end_src

** Insert org-mode links from clipboard
#+begin_src elisp :tangle no
  (use-package org-cliplink
    :bind
    (("C-x p i" . org-cliplink))
    )
#+end_src

** Deft
#+begin_src elisp :tangle no
  (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)
    )
#+end_src