emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Brian Carlson <hacker@abutilize.com>
To: emacs-orgmode@gnu.org
Subject: Starting source code export at non-zero (-n value)
Date: Mon, 16 May 2016 23:33:20 -0400	[thread overview]
Message-ID: <573A9100.3020503@abutilize.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2078 bytes --]

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

[-- Attachment #2: 0001-ox-provide-n-offset-functionality.patch --]
[-- Type: text/x-patch, Size: 7664 bytes --]

From 6b4db0a978cc3492f0d0ac7e29008de6846fbe4a Mon Sep 17 00:00:00 2001
From: Brian Carlson <hacker@abutilize.com>
Date: Mon, 16 May 2016 10:58:01 -0400
Subject: [PATCH] ox: provide [+-]n <offset> 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)))
 
 \f
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


             reply	other threads:[~2016-05-17  3:33 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-17  3:33 Brian Carlson [this message]
2016-05-20 20:48 ` Starting source code export at non-zero (-n value) Nicolas Goaziou
2016-05-22 15:57   ` Brian Carlson
2016-05-23  4:00   ` PATCH: ox: " Brian Carlson
2016-05-24 19:44     ` Nicolas Goaziou
2016-05-24 20:33       ` Nicolas Goaziou
2016-05-26  3:06         ` Brian Carlson
2016-05-26  6:52           ` Nicolas Goaziou
2016-05-30  2:42             ` Brian Carlson
2016-05-30  2:45             ` Brian Carlson
2016-05-31 16:47             ` Brian Carlson
2016-05-31 20:37               ` Nicolas Goaziou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=573A9100.3020503@abutilize.com \
    --to=hacker@abutilize.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).