;;; rul-org.el --- Org configuration
(require 'org)
(require 'org-capture)
(require 'org-protocol)
(require 'org-habit)

(require 'rul-org-agenda)

(setq org-attach-use-inheritance t)
(setq org-cycle-separator-lines 0)
(setq org-hide-leading-stars nil)
(setq org-startup-indented t)
(setq org-edit-src-content-indentation 0)

(use-package org-modern :ensure t)
(use-package org-pomodoro
  :ensure t
  :config
  (defun rul/disable-notifications ()
    "Disable GNOME notifications."
    (shell-command "gsettings set org.gnome.desktop.notifications show-banners false"))

  (defun rul/enable-notifications ()
    "Enable GNOME notifications."
    (shell-command "gsettings set org.gnome.desktop.notifications show-banners true"))

  ;; Add hooks for Pomodoro start and finish
  (add-hook 'org-pomodoro-started-hook #'rul/disable-notifications)
  (add-hook 'org-pomodoro-finished-hook #'rul/enable-notifications)
  (add-hook 'org-pomodoro-killed-hook #'rul/enable-notifications))

;; (add-hook 'org-mode-hook 'turn-off-auto-fill)
;; (add-hook 'auto-save-hook 'org-save-all-org-buffers)
(add-hook 'org-mode-hook 'visual-line-mode)

(use-package org-download
  :ensure t
  :config
  (add-hook 'dired-mode-hook 'org-download-enable))

(setq org-startup-indented t
      org-pretty-entities nil
      org-hide-emphasis-markers t
      ;; show actually italicized text instead of /italicized text/
      org-fontify-whole-heading-line t
      org-fontify-done-headline t
      org-fontify-quote-and-verse-blocks t)

;; ORG BINDINGS ;;
(global-set-key (kbd "C-c l") #'org-store-link)
(global-set-key (kbd "C-c c") #'org-capture)
(global-set-key (kbd "C-c s") #'org-schedule)


(global-set-key (kbd "<f9>") 'bh/punch-in)
(global-set-key (kbd "<f10>") 'bh/punch-out)
(global-set-key (kbd "<f11>") 'org-clock-goto)
(global-set-key (kbd "<f12>") 'org-agenda)

;; ORG STATES ;;
(setq org-todo-keywords
      (quote ((sequence "TODO(t)" "MAYBE(m)" "NEXT(n)" "|" "DONE(d)")
              (sequence "WAITING(w@/!)" "HOLD(h@/!)" "|" "CANCELLED(c@/!)" "MEETING"))))

(setq org-use-fast-todo-selection t)

(setq org-todo-state-tags-triggers
      (quote (("CANCELLED" ("CANCELLED" . t))
              ("WAITING" ("WAITING" . t))
              ("HOLD" ("WAITING") ("HOLD" . t))
              (done ("WAITING") ("HOLD"))
              ("TODO" ("WAITING") ("CANCELLED") ("HOLD"))
              ("NEXT" ("WAITING") ("CANCELLED") ("HOLD"))
              ("DONE" ("WAITING") ("CANCELLED") ("HOLD")))))

(setq org-enforce-todo-dependencies t)
(setq org-log-done (quote time))
(setq org-log-redeadline (quote time))
(setq org-log-reschedule (quote time))
(setq org-log-into-drawer t)

;; CAPTURE ;;
(setq org-capture-templates
      (quote
       (

        ("w" "Todo" entry
         (file+headline org-refile-path "Tasks")
         "* TODO %?"
         :empty-lines 1)

        ("m"
         "Capture incoming email"
         entry
         (file+headline org-refile-path "Incoming")
         "* TODO Re: %:description\n\n  Source: %u, %a\n"
         :empty-lines 1)

        ("e" "Elfeed entry" entry
         (file+headline org-refile-path "Read later")
         "* %? [[%:link][%:description]]\n  %U\n  %:description\n")

        ("L" "Web Link" entry
         (file+headline org-refile-path "Read later")
         "* %?[[%:link][%:description]]\n %:initial\n \nCaptured On: %U"
         )

        ("l" "Web Link with Selection" entry
         (file+headline org-refile-path "Read later")
         "* [[%:link][%:description]]\n %:initial\n \nCaptured On: %U")

        )))

;; REFILE ;;

; Targets include this file and any file contributing to the agenda - up to 3 levels deep
(setq org-refile-targets
      '((nil :maxlevel . 3)
        (org-agenda-files :maxlevel . 3)))

; Targets complete directly with IDO
(setq org-outline-path-complete-in-steps nil)

; Allow refile to create parent tasks with confirmation
(setq org-refile-allow-creating-parent-nodes (quote confirm))



;; ORG REPORTS ;;
; Set default column view headings: Task Effort Clock_Summary
(setq org-columns-default-format "%80ITEM(Task) %10Effort(Effort){:} %10CLOCKSUM")

(defun my-org-clocktable-indent-string (level)
  (if (= level 1)
      ""
    (let ((str "^"))
      (while (> level 2)
        (setq level (1- level)
              str (concat str "--")))
      (concat str "-> "))))

(advice-add 'org-clocktable-indent-string :override #'my-org-clocktable-indent-string)

(setq org-clock-clocktable-default-properties '(:maxlevel 4 :scope file :formula %))

; global Effort estimate values
; global STYLE property values for completion
(setq org-global-properties (quote (("Effort_ALL" . "0:15 0:30 0:45 1:00 2:00 3:00 4:00 5:00 6:00 0:00")
                                    ("STYLE_ALL" . "habit"))))

;; TAGS ;;
; Tags with fast selection keys
(setq org-tag-alist (quote ((:startgroup)
                            ("@errand" . ?e)
                            ("@office" . ?o)
                            ("@home" . ?H)
                            (:endgroup)
                            ("WAITING" . ?w)
                            ("HOLD" . ?h)
                            ("CANCELLED" . ?c)
                            ("FLAGGED" . ??))))

(setq org-stuck-projects
      '("+LEVEL=2+PROJECT/-MAYBE-DONE" ("NEXT") ("@shop")
        "\\<IGNORE\\>"))

; Allow setting single tags without the menu
(setq org-fast-tag-selection-single-key (quote expert))

;; org-modern
(add-hook 'org-mode-hook 'org-modern-mode)
(add-hook 'org-agenda-finalize-hook #'org-modern-agenda)

;; Honor ATTR_ORG attribute. Defaults to image's width if not set.
(setq org-image-actual-width nil)

(provide 'rul-org)