[org] cp-org-insert-block - fix region behaviour, clean up code

This commit is contained in:
contrapunctus 2021-03-10 20:25:12 +05:30
parent 631d7e0fa6
commit b76049d21c
1 changed files with 21 additions and 13 deletions

View File

@ -1468,20 +1468,28 @@ _u_: next word ^_l_: edit lines^ _s_: next whole symbol
(format "[%s](%s)" desc link)))))
(org-link-set-parameters "gemini" :export #'org-link-gemini-export-link))
;; TODO - if `region-active-p', insert syntax at region boundaries
(defun cp-org-insert-block (type &optional lang)
(let* ((col (- (point) (point-at-bol)))
(indent (make-string col ?\ )))
(when (region-active-p)
(goto-char (region-beginning)))
(insert (format "#+BEGIN_%s %s\n" type
(if (stringp lang) lang "")))
(let ((point-inside-block (point)))
(when (region-active-p)
(goto-char (region-end)))
(insert (format "\n%s#+END_%s" indent type))
(goto-char point-inside-block)
(insert indent))))
"Insert block of TYPE at point, or at beginning and end of region."
(let* ((point (point))
(column (- (point) (point-at-bol)))
(indent (make-string column ?\ ))
(region-start (region-beginning))
(region-end (region-end))
(start-string (format "#+BEGIN_%s %s\n" type
(if (stringp lang) lang "")))
(end-string (format "%s#+END_%s" indent type)))
(cond ((region-active-p)
(save-excursion
(goto-char region-end)
(insert end-string "\n")
(goto-char region-start)
(insert start-string)))
;; new empty block - insert the block syntax and place point in the middle
(t (save-excursion (insert "\n" end-string))
(insert start-string)
(let ((point-inside-block (point)))
(goto-char point-inside-block)
(insert indent))))))
;; (use-package org-src-mode
;; :hook (org-src-mode . (lambda () (when (derived-mode-p 'emacs-lisp-mode)))))