Function: org-export-get-loc

org-export-get-loc is a byte-compiled function defined in ox.el.gz.

Signature

(org-export-get-loc ELEMENT INFO)

Documentation

Return count of lines of code before ELEMENT.

ELEMENT is an example-block or src-block element. INFO is the plist used as a communication channel.

Count includes every line of code in example-block or src-block with a "+n" or "-n" switch before block. Return nil if ELEMENT doesn't allow line numbering.

Source Code

;; Defined in /usr/src/emacs/lisp/org/ox.el.gz
;;;; For Src-Blocks
;;
;; `org-export-get-loc' counts number of code lines accumulated in
;; src-block or example-block elements with a "+n" switch until
;; a given element, excluded.  Note: "-n" switches reset that count.
;;
;; `org-export-unravel-code' extracts source code (along with a code
;; references alist) from an `example-block' or `src-block' type
;; element.
;;
;; `org-export-format-code' applies a formatting function to each line
;; of code, providing relative line number and code reference when
;; appropriate.  Since it doesn't access the original element from
;; which the source code is coming, it expects from the code calling
;; it to know if lines should be numbered and if code references
;; should appear.
;;
;; Eventually, `org-export-format-code-default' is a higher-level
;; function (it makes use of the two previous functions) which handles
;; line numbering and code references inclusion, and returns source
;; code in a format suitable for plain text or verbatim output.

(defun org-export-get-loc (element info)
  "Return count of lines of code before ELEMENT.

ELEMENT is an example-block or src-block element.  INFO is the
plist used as a communication channel.

Count includes every line of code in example-block or src-block
with a \"+n\" or \"-n\" switch before block.  Return nil if
ELEMENT doesn't allow line numbering."
  (pcase (org-element-property :number-lines element)
    (`(new . ,n) n)
    (`(continued . ,n)
     (let ((loc 0))
       (org-element-map (plist-get info :parse-tree) '(src-block example-block)
	 (lambda (el)
	   ;; ELEMENT is reached: Quit loop and return locs.
	   (if (eq el element) (+ loc n)
	     ;; Only count lines from src-block and example-block
	     ;; elements with a "+n" or "-n" switch.
	     (let ((linum (org-element-property :number-lines el)))
	       (when linum
		 (let ((lines (org-count-lines
			       (org-element-property :value el))))
		   ;; Accumulate locs or reset them.
		   (pcase linum
		     (`(new . ,n) (setq loc (+ n lines)))
		     (`(continued . ,n) (cl-incf loc (+ n lines)))))))
	     nil))		      ;Return nil to stay in the loop.
	 info 'first-match)))))