Function: org-element-src-block-parser

org-element-src-block-parser is a byte-compiled function defined in org-element.el.gz.

Signature

(org-element-src-block-parser LIMIT AFFILIATED)

Documentation

Parse a source block.

LIMIT bounds the search. AFFILIATED is a list of which CAR is the buffer position at the beginning of the first affiliated keyword and CDR is a plist of affiliated keywords along with their value.

Return a list whose CAR is src-block and CDR is a plist containing :language, :switches, :parameters, :begin,
:end, :number-lines, :retain-labels, :use-labels,
:label-fmt, :preserve-indent, :value, :post-blank and
:post-affiliated keywords.

Assume point is at the beginning of the block.

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-element.el.gz
;;;; Src Block

(defun org-element-src-block-parser (limit affiliated)
  "Parse a source block.

LIMIT bounds the search.  AFFILIATED is a list of which CAR is
the buffer position at the beginning of the first affiliated
keyword and CDR is a plist of affiliated keywords along with
their value.

Return a list whose CAR is `src-block' and CDR is a plist
containing `:language', `:switches', `:parameters', `:begin',
`:end', `:number-lines', `:retain-labels', `:use-labels',
`:label-fmt', `:preserve-indent', `:value', `:post-blank' and
`:post-affiliated' keywords.

Assume point is at the beginning of the block."
  (let ((case-fold-search t))
    (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
						limit t)))
	;; Incomplete block: parse it as a paragraph.
	(org-element-paragraph-parser limit affiliated)
      (let ((contents-end (match-beginning 0)))
	(save-excursion
	  (let* ((begin (car affiliated))
		 (post-affiliated (point))
		 ;; Get language as a string.
		 (language
		  (progn
		    (looking-at
		     "^[ \t]*#\\+BEGIN_SRC\
\\(?: +\\(\\S-+\\)\\)?\
\\(\\(?: +\\(?:-\\(?:l \".+\"\\|[ikr]\\)\\|[-+]n\\(?: *[0-9]+\\)?\\)\\)+\\)?\
\\(.*\\)[ \t]*$")
		    (match-string-no-properties 1)))
		 ;; Get switches.
		 (switches (match-string-no-properties 2))
		 ;; Get parameters.
		 (parameters (match-string-no-properties 3))
		 ;; Switches analysis.
		 (number-lines
		  (and switches
		       (string-match "\\([-+]\\)n\\(?: *\\([0-9]+\\)\\)?\\>"
				     switches)
		       (cons
			(if (equal (match-string 1 switches) "-")
			    'new
			  'continued)
			(if (not (match-end 2)) 0
			  ;; Subtract 1 to give number of lines before
			  ;; first line.
			  (1- (string-to-number (match-string 2 switches)))))))
		 (preserve-indent (and switches
				       (string-match "-i\\>" switches)))
		 (label-fmt
		  (and switches
		       (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
		       (match-string 1 switches)))
		 ;; Should labels be retained in (or stripped from)
		 ;; source blocks?
		 (retain-labels
		  (or (not switches)
		      (not (string-match "-r\\>" switches))
		      (and number-lines (string-match "-k\\>" switches))))
		 ;; What should code-references use - labels or
		 ;; line-numbers?
		 (use-labels
		  (or (not switches)
		      (and retain-labels
			   (not (string-match "-k\\>" switches)))))
		 ;; Retrieve code.
		 (value (org-unescape-code-in-string
			 (buffer-substring-no-properties
			  (line-beginning-position 2) contents-end)))
		 (pos-before-blank (progn (goto-char contents-end)
					  (forward-line)
					  (point)))
		 ;; Get position after ending blank lines.
		 (end (progn (skip-chars-forward " \r\t\n" limit)
			     (if (eobp) (point) (line-beginning-position)))))
	    (list 'src-block
		  (nconc
		   (list :language language
			 :switches (and (org-string-nw-p switches)
					(org-trim switches))
			 :parameters (and (org-string-nw-p parameters)
					  (org-trim parameters))
			 :begin begin
			 :end end
			 :number-lines number-lines
			 :preserve-indent preserve-indent
			 :retain-labels retain-labels
			 :use-labels use-labels
			 :label-fmt label-fmt
			 :value value
			 :post-blank (count-lines pos-before-blank end)
			 :post-affiliated post-affiliated)
		   (cdr affiliated)))))))))