[org] cp-org-insert-block - clean up code, add comments

This commit is contained in:
contrapunctus 2021-03-11 06:17:14 +05:30
parent 2b2bf26a2f
commit 85eb74a0a2
1 changed files with 12 additions and 6 deletions

View File

@ -1668,25 +1668,31 @@ _u_: next word ^_l_: edit lines^ _s_: next whole symbol
(org-link-set-parameters "gemini" :export #'org-link-gemini-export-link))
(defun cp-org-insert-block (type &optional lang)
"Insert block of TYPE at point, or at beginning and end of region."
(let* ((point (point))
(column (- (point) (point-at-bol)))
"Insert block of TYPE at point, or at beginning and end of region.
TYPE should be an Org block type, e.g. SRC, QUOTE, etc.
If TYPE is SRC, LANG should be the name of the language as a string, e.g. \"emacs-lisp\"."
(let* ((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)))
;; create a block around a region - preserve position of point
(cond ((region-active-p)
(save-excursion
;; inserting at region-start would make region-end
;; invalid, so we insert at the end first
(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)
;; new empty block - insert the block syntax and place point
;; inside the block
(t (insert start-string)
(let ((point-inside-block (point)))
(insert "\n" end-string)
(goto-char point-inside-block)
(insert indent))))))