5

When input some code in orgmode, I can type:

<s Tab

and it expands to this:

#+BEGIN_SRC cursor

#+END_SRC

This is fine for writing something from scratch. But most of the time, I'd copy some paragraph which contains some code. (Code interposed in paragraphs).

In this scenario, after Tab, I have to select the code snippet, cut and paste them between BEGIN and END. Or cut the END_SRC, paste it at the end of the code.

Is there a way to do this automatically?

The ideal process would be:

  1. Select some code
  2. run a command, the code is well formatted by the BEGIN, END markdowns.
3

Here's a function that should do what you want. To use it, select a region, and run the command. It will handle putting in the #+BEGIN_SRC and #+END_SRC parts on new lines. It happens to leave you at the beginning of the source code block, but that could be changed.

(defun org-wrap-source ()
  (interactive)
  (let ((start (min (point) (mark)))
        (end (max (point) (mark))))
    (goto-char end)
    (unless (bolp)
      (newline))
    (insert "#+END_SRC\n")
    (goto-char start)
    (unless (bolp)
      (newline))
    (insert "#+BEGIN_SRC\n")))

You can bind this function to an appropriate key if you like.

|improve this answer|||||
  • Thank you. I modified it slightly to move the cursor after BEGIN_SRC (and a space) to let me specify what language the code is in. So that the syntax highlighting and indentation would be right. Added ` (backward-char) (insert " ")` after the last expression. – Nick Dec 26 '14 at 7:12
2

This function can do it for you: from chrisdone/chrisdone-emacs

(define-key org-mode-map (kbd "C-#") 'org-begin-template)
(defun org-begin-template ()
  "Make a template at point."
  (interactive)
  (if (org-at-table-p)
      (call-interactively 'org-table-rotate-recalc-marks)
    (let* ((choices '(("s" . "SRC")
                      ("e" . "EXAMPLE")
                      ("q" . "QUOTE")
                      ("v" . "VERSE")
                      ("c" . "CENTER")
                      ("l" . "LaTeX")
                      ("h" . "HTML")
                      ("a" . "ASCII")))
           (key
            (key-description
             (vector
              (read-key
               (concat (propertize "Template type: " 'face 'minibuffer-prompt)
                       (mapconcat (lambda (choice)
                                    (concat (propertize (car choice) 'face 'font-lock-type-face)
                                            ": "
                                            (cdr choice)))
                                  choices
                                  ", ")))))))
      (let ((result (assoc key choices)))
        (when result
          (let ((choice (cdr result)))
            (cond
             ((region-active-p)
              (let ((start (region-beginning))
                    (end (region-end)))
                (goto-char end)
                (insert "\n#+END_" choice)
                (goto-char start)
                (insert "#+BEGIN_" choice "\n")))
             (t
              (insert "#+BEGIN_" choice "\n")
              (save-excursion (insert "\n#+END_" choice))))))))))
|improve this answer|||||
0

My preference:

Reasons:

  • I like the structure templates; they are easy to remember and I use them all of the time
  • You can't automate something until you know what you want to do and the macro recorder makes that really easy
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.