       (let ((regionp (use-region-p)))
         (if (or regionp
                 (and (not regionp)
-                     (looking-back "^")))
+                     (looking-back "^[[:blank:]]*")))
             (hydra-org-template/body)
           (self-insert-command 1))))
 


    (defun modi/org-template-expand (str &optional lang)
      "Expand Org template."
      (let (beg old-beg end content)
        ;; Save restriction to automatically undo the upcoming `narrow-to-region'
        (save-restriction
          (when (use-region-p)
            (setq beg (region-beginning))
            (setq end (region-end))
            ;; Note that regardless of the direction of selection, we will always
            ;; have (region-beginning) < (region-end).
            (save-excursion
              ;; If `point' is at `end', exchange point and mark so that now the
              ;; `point' is now at `beg'
              (when (> (point) (mark))
                (exchange-point-and-mark))
              ;; Insert a newline if `beg' is *not* at beginning of the line.
              ;; Example: You have ^abc$ where ^ is bol and $ is eol.
              ;;          "bc" is selected and <e is pressed to result in:
              ;;            a
              ;;            #+BEGIN_EXAMPLE
              ;;            bc
              ;;            #+END_EXAMPLE
              (when (/= beg (line-beginning-position))
                (electric-indent-just-newline 1)
                (setq old-beg beg)
                (setq beg (point))
                ;; Adjust the `end' due to newline
                (setq end (+ end (- beg old-beg)))))
            (save-excursion
              ;; If `point' is at `beg', exchange point and mark so that now the
              ;; `point' is now at `end'
              (when (< (point) (mark))
                (exchange-point-and-mark))
              ;; If the `end' position is at the beginning of a line decrement
              ;; the position by 1, so that the resultant position is eol on
              ;; the previous line.
              (when (= end (line-beginning-position))
                (setq end (1- end)))
              ;; Insert a newline if `point'/`end' is *not* at end of the line.
              ;; Example: You have ^abc$ where ^ is bol and $ is eol.
              ;;          "a" is selected and <e is pressed to result in:
              ;;            #+BEGIN_EXAMPLE
              ;;            a
              ;;            #+END_EXAMPLE
              ;;            bc
              (when (not (looking-at "[[:blank:]]*$"))
                (electric-indent-just-newline 1)))
            ;; Narrow to region so that the text surround the region does
            ;; not mess up the upcoming `org-try-structure-completion' eval
            (narrow-to-region beg end)
            (setq content (delete-and-extract-region beg end)))
          (insert str)
          (org-try-structure-completion)
          (when (string= "<s" str)
            (cond
             (lang
              (insert lang)
              (forward-line))
             ((and content (not lang))
              (insert "???")
              (forward-line))
             (t
              )))
          ;; A special case for org source blocks.. need to escape *'s and #'s
          ;; with commas.
          ;; * -> ,*    ,* -> ,,*    #+ -> ,#+    ,#+ -> ,,#+ -- (org) Literal examples
          (when (string= "org" lang)
            (setq content (replace-regexp-in-string "^,?\\(\\*\\|#\\+\\)" ",\\&" content)))
          ;; At this point the cursor will be between the #+BEGIN and #+END lines
          (when content
            (insert content)
            (deactivate-mark)))))

    (defhydra hydra-org-template (:color blue
                                  :hint nil)

;; Time-stamp: <2017-10-20 16:47:58 kmodi>
;; Hi-lock: (("\\(^;\\{3,\\}\\)\\( *.*\\)" (1 'org-hide prepend) (2 '(:inherit org-level-1 :height 1.3 :weight bold :overline t :underline t) prepend)))
;; Hi-Lock: end

