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 new syntax node of src-block type containing :language,
:switches, :parameters, :begin, :end, :number-lines,
:retain-labels, :use-labels, :label-fmt, :preserve-indent,
:value, :post-blank and :post-affiliated properties.

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 new syntax node of `src-block' type containing `:language',
`:switches', `:parameters', `:begin', `:end', `:number-lines',
`:retain-labels', `:use-labels', `:label-fmt', `:preserve-indent',
`:value', `:post-blank' and `:post-affiliated' properties.

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]*$")
		    (org-element--get-cached-string
                     (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-p "-i\\>" switches)))
		 (label-fmt
		  (and switches
		       (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
		       (match-string-no-properties 1 switches)))
		 ;; Should labels be retained in (or stripped from)
		 ;; source blocks?
		 (retain-labels
		  (or (not switches)
		      (not (string-match-p "-r\\>" switches))
		      (and number-lines (string-match-p "-k\\>" switches))))
		 ;; What should code-references use - labels or
		 ;; line-numbers?
		 (use-labels
		  (or (not switches)
		      (and retain-labels
			   (not (string-match-p "-k\\>" switches)))))
		 ;; Retrieve code.
		 (value
                  (org-element-deferred-create
                   t #'org-element--unescape-substring
                   (- (line-beginning-position 2) begin)
                   (- contents-end begin)))
		 (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)))))
	    (org-element-create
             '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)))))))))