Function: org-export-unravel-code

org-export-unravel-code is a byte-compiled function defined in ox.el.gz.

Signature

(org-export-unravel-code ELEMENT)

Documentation

Clean source code and extract references out of it.

ELEMENT has either a src-block an example-block type.

Return a cons cell whose CAR is the source code, cleaned from any reference, protective commas and spurious indentation, and CDR is an alist between relative line number (integer) and name of code reference on that line (string).

Source Code

;; Defined in /usr/src/emacs/lisp/org/ox.el.gz
(defun org-export-unravel-code (element)
  "Clean source code and extract references out of it.

ELEMENT has either a `src-block' an `example-block' type.

Return a cons cell whose CAR is the source code, cleaned from any
reference, protective commas and spurious indentation, and CDR is
an alist between relative line number (integer) and name of code
reference on that line (string)."
  (let* ((line 0) refs
	 (value (org-element-property :value element))
	 ;; Remove global indentation from code, if necessary.  Also
	 ;; remove final newline character, since it doesn't belongs
	 ;; to the code proper.
	 (code (replace-regexp-in-string
		"\n\\'" ""
		(if (org-src-preserve-indentation-p element) value
		  (org-remove-indentation value))))
	 ;; Build a regexp matching a loc with a reference.
	 (ref-re (org-src-coderef-regexp (org-src-coderef-format element))))
    ;; Return value.
    (cons
     ;; Code with references removed.
     (mapconcat
      (lambda (loc)
	(cl-incf line)
	(if (not (string-match ref-re loc)) loc
	  ;; Ref line: remove ref, and add its position in REFS.
	  (push (cons line (match-string 3 loc)) refs)
	  (replace-match "" nil nil loc 1)))
      (split-string code "\n") "\n")
     ;; Reference alist.
     refs)))