From mboxrd@z Thu Jan 1 00:00:00 1970 From: Brian Carlson Subject: Starting source code export at non-zero (-n value) Date: Mon, 16 May 2016 23:33:20 -0400 Message-ID: <573A9100.3020503@abutilize.com> Reply-To: hacker@abutilize.com Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------030801040709030502060409" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:37421) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b2VlG-0001lC-IB for emacs-orgmode@gnu.org; Mon, 16 May 2016 23:33:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b2VlC-0001pN-7X for emacs-orgmode@gnu.org; Mon, 16 May 2016 23:33:33 -0400 Received: from gateway21.websitewelcome.com ([192.185.45.147]:57401) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b2VlB-0001nH-Un for emacs-orgmode@gnu.org; Mon, 16 May 2016 23:33:30 -0400 Received: from cm7.websitewelcome.com (cm7.websitewelcome.com [108.167.139.20]) by gateway21.websitewelcome.com (Postfix) with ESMTP id 490B49452A6C2 for ; Mon, 16 May 2016 22:33:13 -0500 (CDT) Received: from pool-173-76-187-225.bstnma.fios.verizon.net ([173.76.187.225]:48819 helo=[192.168.1.19]) by gator3208.hostgator.com with esmtpsa (TLSv1.2:DHE-RSA-AES128-SHA:128) (Exim 4.86_1) (envelope-from ) id 1b2Vkt-000UNo-Cx for emacs-orgmode@gnu.org; Mon, 16 May 2016 22:33:11 -0500 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode@gnu.org This is a multi-part message in MIME format. --------------030801040709030502060409 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Hello other org mode aficionados! (I apologize for the errant email I send a minute ago. ) I have created a (possible) patch to the export (ox.el and other ox-XXX.el) files that allow for "source code" and "example" blocks to have line numbers starting at an arbitrary number. Before the (wonderful) ox redesign I used to have advice around some functions. I hadn't needed this functionality for a while, but now that I did I thought I would share. The code is written with the following design: -n is the same as -n 1 : The functionality is unchanged +n is the same as +n 1 : The functionality is unchanged -n X will "reset" and start new code block starting at line X +n X will "add" X to the last line of the block before. ======== example: * Heading 1 * ~-n 10~ #+BEGIN_SRC emacs-lisp -n 10 (save-excursion ;; This is line 10 (goto-char (point-min))) ;; 11 #+END_SRC * ~-n~ #+BEGIN_SRC emacs-lisp -n (save-excursion ;; line 1 (goto-char (point-min))) ;; line 2 #+END_SRC * ~+n 155~ #+BEGIN_SRC emacs-lisp +n 155 (save-excursion ;; line 157 (goto-char (point-min))) ;; line 158 #+END_SRC Gives the following "Text - ascii output" 1 Heading 1 =========== * `-n 10' ,---- | 10 (save-excursion ;; This is line 10 | 11 (goto-char (point-min))) ;; 11 `---- * `-n' ,---- | 1 (save-excursion ;; line 1 | 2 (goto-char (point-min))) ;; line 2 `---- * `+n 155' ,---- | 157 (save-excursion ;; line 157 | 158 (goto-char (point-min))) ;; line 158 `---- I have tested the code (using make test) and generated (hand verified) the LaTeX and HTML output. ODT has an issue already with +n in that each code block starts at line 1. (I did not address that). I have included the git format patch output. This is a fairly small set of changes, but I do assign the Copyright to the Free Software Foundation. Thanks, ;-b --------------030801040709030502060409 Content-Type: text/x-patch; name="0001-ox-provide-n-offset-functionality.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0001-ox-provide-n-offset-functionality.patch" >From 6b4db0a978cc3492f0d0ac7e29008de6846fbe4a Mon Sep 17 00:00:00 2001 From: Brian Carlson Date: Mon, 16 May 2016 10:58:01 -0400 Subject: [PATCH] ox: provide [+-]n functionality --- contrib/lisp/ox-groff.el | 4 ++-- lisp/org-element.el | 16 +++++++++++----- lisp/ox-html.el | 4 ++-- lisp/ox-latex.el | 4 ++-- lisp/ox-odt.el | 4 ++-- lisp/ox.el | 16 ++++++++++------ 6 files changed, 29 insertions(+), 19 deletions(-) diff --git a/contrib/lisp/ox-groff.el b/contrib/lisp/ox-groff.el index b49edce..517da9a 100644 --- a/contrib/lisp/ox-groff.el +++ b/contrib/lisp/ox-groff.el @@ -1488,9 +1488,9 @@ contextual information." (custom-env (and lang (cadr (assq (intern lang) org-groff-custom-lang-environments)))) - (num-start (case (org-element-property :number-lines src-block) + (num-start (case (car (org-element-property :number-lines src-block)) (continued (org-export-get-loc src-block info)) - (new 0))) + (new (or (cdr (org-element-property :number-lines src-block)) 0)))) (retain-labels (org-element-property :retain-labels src-block)) (caption (and (not (org-export-read-attribute :attr_groff src-block :disable-caption)) diff --git a/lisp/org-element.el b/lisp/org-element.el index 368da60..65f9833 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -1896,8 +1896,11 @@ containing `:begin', `:end', `:number-lines', `:preserve-indent', ;; Switches analysis (number-lines (cond ((not switches) nil) - ((string-match "-n\\>" switches) 'new) - ((string-match "+n\\>" switches) 'continued))) + ((string-match "-n *\\([0-9]+\\)" switches) (cons 'new (- (string-to-number (match-string 1 switches)) 1 ))) + ((string-match "+n *\\([0-9]+\\)" switches) (cons 'continued (- (string-to-number (match-string 1 switches)) 1 ))) + ((string-match "-n" switches) (cons 'new 0)) + ((string-match "+n" switches) (cons 'continued 0)) + )) (preserve-indent (and switches (string-match "-i\\>" switches))) ;; Should labels be retained in (or stripped from) example @@ -2393,7 +2396,7 @@ Assume point is at the beginning of the block." (looking-at (concat "^[ \t]*#\\+BEGIN_SRC" "\\(?: +\\(\\S-+\\)\\)?" - "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?" + "\\(\\(?: +\\(?:-l \".+\"\\|[+-]n *[[:digit:]]+\\|[+-][[:alpha:]]\\)\\)+\\)?" "\\(.*\\)[ \t]*$")) (org-match-string-no-properties 1))) ;; Get switches. @@ -2403,8 +2406,11 @@ Assume point is at the beginning of the block." ;; Switches analysis (number-lines (cond ((not switches) nil) - ((string-match "-n\\>" switches) 'new) - ((string-match "+n\\>" switches) 'continued))) + ((string-match "-n *\\([0-9]+\\)" switches) (cons 'new (- (string-to-number (match-string 1 switches)) 1 ))) + ((string-match "+n *\\([0-9]+\\)" switches) (cons 'continued (- (string-to-number (match-string 1 switches)) 1 ))) + ((string-match "-n" switches) (cons 'new 0)) + ((string-match "+n" switches) (cons 'continued 0)) + )) (preserve-indent (and switches (string-match "-i\\>" switches))) (label-fmt diff --git a/lisp/ox-html.el b/lisp/ox-html.el index b188c38..5503d9e 100644 --- a/lisp/ox-html.el +++ b/lisp/ox-html.el @@ -2209,9 +2209,9 @@ a plist used as a communication channel." ;; Does the src block contain labels? (retain-labels (org-element-property :retain-labels element)) ;; Does it have line numbers? - (num-start (case (org-element-property :number-lines element) + (num-start (case (car (org-element-property :number-lines element)) (continued (org-export-get-loc element info)) - (new 0)))) + (new (or (cdr (org-element-property :number-lines element)) 0))))) (org-html-do-format-code code lang refs retain-labels num-start))) diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el index 7fa68c5..0159d48 100644 --- a/lisp/ox-latex.el +++ b/lisp/ox-latex.el @@ -2762,9 +2762,9 @@ contextual information." (custom-env (and lang (cadr (assq (intern lang) org-latex-custom-lang-environments)))) - (num-start (case (org-element-property :number-lines src-block) + (num-start (case (car (org-element-property :number-lines src-block)) (continued (org-export-get-loc src-block info)) - (new 0))) + (new (or (cdr (org-element-property :number-lines src-block)) 0)))) (retain-labels (org-element-property :retain-labels src-block)) (attributes (org-export-read-attribute :attr_latex src-block)) (float (plist-get attributes :float)) diff --git a/lisp/ox-odt.el b/lisp/ox-odt.el index d996689..9acf3c6 100644 --- a/lisp/ox-odt.el +++ b/lisp/ox-odt.el @@ -3168,9 +3168,9 @@ and prefix with \"OrgSrc\". For example, ;; Does the src block contain labels? (retain-labels (org-element-property :retain-labels element)) ;; Does it have line numbers? - (num-start (case (org-element-property :number-lines element) + (num-start (case (car (org-element-property :number-lines element)) (continued (org-export-get-loc element info)) - (new 0)))) + (new (or (cdr (org-element-property :number-lines element)) 0))))) (org-odt-do-format-code code info lang refs retain-labels num-start))) (defun org-odt-src-block (src-block _contents info) diff --git a/lisp/ox.el b/lisp/ox.el index 5ad17ec..c05f55e 100644 --- a/lisp/ox.el +++ b/lisp/ox.el @@ -4148,9 +4148,9 @@ error if no block contains REF." (when (re-search-backward ref-re nil t) (cond ((org-element-property :use-labels el) ref) - ((eq (org-element-property :number-lines el) 'continued) + ((eq (car (org-element-property :number-lines el)) 'continued) (+ (org-export-get-loc el info) (line-number-at-pos))) - (t (line-number-at-pos))))))) + (t (+ (or (cdr (org-element-property :number-lines el)) 0) (line-number-at-pos)))))))) info 'first-match) (signal 'org-link-broken (list ref)))) @@ -4467,15 +4467,19 @@ ELEMENT is excluded from count." ;; Only count lines from src-block and example-block elements ;; with a "+n" or "-n" switch. A "-n" switch resets counter. ((not (memq (org-element-type el) '(src-block example-block))) nil) - ((let ((linums (org-element-property :number-lines el))) + ((let* ((linums (org-element-property :number-lines el))) (when linums ;; Accumulate locs or reset them. (let ((lines (org-count-lines (org-trim (org-element-property :value el))))) - (setq loc (if (eq linums 'new) lines (+ loc lines)))))) + (setq loc (if (eq (car linums) 'new) + (+ lines (cdr linums)) + (+ loc lines (cdr linums))))))) ;; Return nil to stay in the loop. nil))) info 'first-match) + (if (eq (car (org-element-property :number-lines element)) 'continued) + (setq loc (+ (or (cdr (org-element-property :number-lines element)) 0) loc))) ;; Return value. loc)) @@ -4573,9 +4577,9 @@ code." (let* ((refs (and (org-element-property :retain-labels element) (cdr code-info))) ;; Handle line numbering. - (num-start (cl-case (org-element-property :number-lines element) + (num-start (cl-case (car (org-element-property :number-lines element)) (continued (org-export-get-loc element info)) - (new 0))) + (new (or (cdr (org-element-property :number-lines element)) 0)))) (num-fmt (and num-start (format "%%%ds " -- 2.8.2 --------------030801040709030502060409--