 ;;; Code:
 (require 'face-remap)
 (require 'org)
+(require 'org-compat)
 (require 'centered-buffer-mode)
 
 (defgroup space-doc nil "Minor mode for viewing Spacemacs documentation files."

    (org-indent-mode              . spacemacs//space-doc-org-indent-mode)
    (view-mode                    . spacemacs//space-doc-view-mode)
    (hide-line-numbers            . spacemacs//space-doc-hide-line-numbers)
    (alternative-emphasis            . spacemacs//space-doc-alternative-emphasis)
    (alternative-tags-look           . spacemacs//space-doc-alternative-tags-look)
    (link-protocol                . spacemacs//space-doc-link-protocol)
    (org-block-line-face-remap    . spacemacs//space-doc-org-block-line-face-remap)
    (org-kbd-face-remap           . spacemacs//space-doc-org-kbd-face-remap)

              600)))
    (org-display-inline-images)))

(defun spacemacs//space-doc-tags-fontify (startish endish &optional verbose)
  "Fontify `org-mode' tags in the fuzzy region that might start
 before STARTISH and end after ENDISH. VERBOSE ignored."
  ;; TODO add more types of tags or meta-line if needed.
  (let ((invisible-org-meta-tags-list
         `(;; Hide TITLE tag.
           "\\([ \t]*\\#\\+TITLE\\:\[ \t]*\\)"
           ;; Hide CAPTION logo meta line.
           "\\(\n.*\\#\\+CAPTION\\:.*\\)"
           ;; Hide TOC-ORG tag and spaces before it.
           ;; Use modified `toc-org-toc-org-regexp' because
           ;; the original one matches whole string.
           ,(concat "\\([ \t]*:toc\\([@_][0-9]\\|\\([@_][0-9]"
                    "[@_][a-zA-Z]+\\)\\)?:\\($\\|[^ ]*:$\\)\\)")
           ;; Hide empty line before #+BEGIN_SRC tag if
           ;; background color of the `org-block-begin-line'
           ;; face is unspecified.
           ,(unless (face-background 'org-block-begin-line)
              "\n\\(\n\\)[ \t]*\\#\\+begin_src.*$")
           ;; Hide empty line after #+END_SRC tag if
           ;; background color of the `org-block-end-line'
           ;; face is unspecified and the next line isn't
           ;;an org headline.
           ,(unless (face-background 'org-block-end-line)
              "^[ \t]*\\#\\+end_src.*\n\\(\n\\)[^\\*]")))
        (start (save-excursion (goto-char startish)
                               (point-at-bol -15)))
        (end   (save-excursion (goto-char endish)
                               (point-at-eol 15))))
    ;; Remove nils.
    (setq invisible-org-meta-tags-list
          (remove nil invisible-org-meta-tags-list))
    ;; Make `org-mode' meta tags invisible.
    (dolist (tag invisible-org-meta-tags-list)
      (save-excursion
        (goto-char start)
        (while (re-search-forward tag end t)
          (add-text-properties (match-beginning 1)
                               (match-end 1)
                               (list 'invisible 'spacemacs--space-doc-invisible-marker)))))))

(defun spacemacs//space-doc-font-lock-fontify-region-function (start end &optional verbose)
  "Wrapper around `font-lock-default-fontify-region' function for the buffer local value of
`font-lock-fontify-region-function'. Makes sure that `font-lock-default-fontify-region'
text property persist after `org-mode' shenanigans.
NOTE: Not using `advice-add' because it is global modification.
FIXME: Find cleaner solution."
  (font-lock-default-fontify-region  start end verbose)
  (spacemacs//space-doc-tags-fontify start end verbose))

(defun spacemacs//space-doc-alternative-tags-look (&optional enable)
  "Modify meta tag appearance.
If ENABLE is non-nil, modify `org-mode' meta tags appearance in the current
buffer.
This functions is aimed to be used with `spacemacs-space-doc-modificators'."
  (if enable
      (setq-local font-lock-fontify-region-function
                  'spacemacs//space-doc-font-lock-fontify-region-function)
    (kill-local-variable 'font-lock-fontify-region-function)))

(defun spacemacs//space-doc-org-block-line-face-remap (&optional enable)
  "Hide drawers.
