 (require 'map)
 (require 'org)
 (require 'seq)
+(require 'company)
 
 (defcustom company-org-block-complete-at-bol t
-  "If t, detect completion when at begining of line, otherwise detect
- completion anywhere."
+  "If t, detect completion only at the beginning of lines."
   :type 'boolean)
 
 (defcustom company-org-block-explicit-lang-defaults t

  :type 'boolean)

(defcustom company-org-block-edit-mode 'auto
  "Customize whether edit mode, post completion was inserted."
  :type '(choice
	  (const :tag "nil: no edit after insertion" nil)
	  (const :tag "prompt: ask before edit" prompt)
	  (const :tag "auto edit, no prompt" auto)))

;; (defvar company-org-block-auto-major-edit t "If t, enter
;; major mode after completion.")

(defvar company-org--regexp "<\\([^ ]*\\)")

(defun company-org-block (command &optional arg &rest ignored)
  "Complete org babel languages into source blocks."
  (interactive (list 'interactive))
  (cl-case command
    (interactive (company-begin-backend 'company-org-block))

                (map-values org-babel-tangle-lang-exts)))))

(defun company-org-block--template-p (template)
  "Check if there is a TEMPLATE available for completion."
  (seq-contains (map-values org-structure-template-alist)
                template))

(defun company-org-block--expand (insertion)
  "Replace INSERTION with generated source block."
  (delete-region (point) (- (point) (1+ ;; Include "<" in length.
                                     (length insertion))))
  (if (company-org-block--template-p insertion)
