emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [patch, ox] #+INCLUDE resolves links
@ 2014-09-21  0:51 Rasmus
  2014-09-21 11:46 ` Rasmus
  0 siblings, 1 reply; 21+ messages in thread
From: Rasmus @ 2014-09-21  0:51 UTC (permalink / raw)
  To: emacs-orgmode

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

Hi,

This patch allows INCLUDE to have intuitive links as resolved by
`org-link'-search'.  A couple of examples:

#+INCLUDE: file.org::#custom_id :noheadline :lines "3-"
#+INCLUDE: file.org::*headline :lines "-10"

:noheading tries to get rid of the first headline, and immediately
subsequent drawer and property-drawer, if present.  :noheading is only
interpret when a headline argument is present.  :lines is interpreted
relatively, if coupled with a headline link.

I should work for other types of links as well though it could be
limited to headlines only.

Perhaps it would even make sense to let it take a no-file argument to
locate things within the same buffer.  This would be useful for
including, say, tables in babel/code-appendices.  

What do you think?

—Rasmus

--
Hooray!

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ox-Allow-links-with-INCLUDE-keyword.patch --]
[-- Type: text/x-diff, Size: 5253 bytes --]

From 727b20f454cb4f1582874f1b35d5e5f53ec44f79 Mon Sep 17 00:00:00 2001
From: Rasmus <rasmus@gmx.us>
Date: Sat, 20 Sep 2014 22:22:15 +0200
Subject: [PATCH] ox: Allow links with #+INCLUDE-keyword

* ox.el (org-export--prepare-file-contents,
org-export-expand-include-keyword): Handle links and add option
no-heading.
---
 lisp/ox.el | 64 ++++++++++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 44 insertions(+), 20 deletions(-)

diff --git a/lisp/ox.el b/lisp/ox.el
index f01f951..e78743a 100644
--- a/lisp/ox.el
+++ b/lisp/ox.el
@@ -3321,13 +3321,23 @@ paths."
 	  ;; Extract arguments from keyword's value.
 	  (let* ((value (org-element-property :value element))
 		 (ind (org-get-indentation))
+		 headline
 		 (file (and (string-match
 			     "^\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)" value)
-			    (prog1 (expand-file-name
-				    (org-remove-double-quotes
-				     (match-string 1 value))
-				    dir)
-			      (setq value (replace-match "" nil nil value)))))
+			    (let ((matched (save-match-data
+					     (org-split-string (match-string 1 value) "::"))))
+			      (setq headline (car-safe (cdr-safe matched)))
+			      (prog1 (expand-file-name
+				      (org-remove-double-quotes
+				       (car matched))
+				      dir)
+				(setq value (replace-match "" nil nil value))))))
+
+		 (no-headline
+		  (and (string-match
+			":\\(no-?headline[[:space:]]*\\(?:'t\\|true\\|yes\\)?\\)" value)
+		       (prog1 t
+		 	 (setq value (replace-match "" nil nil value)))))
 		 (lines
 		  (and (string-match
 			":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\""
@@ -3370,18 +3380,18 @@ paths."
 		(insert
 		 (let ((ind-str (make-string ind ? ))
 		       (arg-str (if (stringp src-args)
-				  (format " %s" src-args)
-				""))
+				    (format " %s" src-args)
+				  ""))
 		       (contents
 			(org-escape-code-in-string
-			 (org-export--prepare-file-contents file lines))))
+			 (org-export--prepare-file-contents file headline no-headline lines))))
 		   (format "%s#+BEGIN_%s%s\n%s%s#+END_%s\n"
 			   ind-str block arg-str contents ind-str block))))
 	       ((stringp block)
 		(insert
 		 (let ((ind-str (make-string ind ? ))
 		       (contents
-			 (org-export--prepare-file-contents file lines)))
+			(org-export--prepare-file-contents file headline no-headline lines)))
 		   (format "%s#+BEGIN_%s\n%s%s#+END_%s\n"
 			   ind-str block contents ind-str block))))
 	       (t
@@ -3390,7 +3400,7 @@ paths."
 		   (let ((org-inhibit-startup t)) (org-mode))
 		   (insert
 		    (org-export--prepare-file-contents
-		     file lines ind minlevel
+		     file headline no-headline lines ind minlevel
 		     (or (gethash file file-prefix)
 			 (puthash file (incf current-prefix) file-prefix))))
 		   (org-export-expand-include-keyword
@@ -3398,7 +3408,7 @@ paths."
 		    (file-name-directory file))
 		   (buffer-string)))))))))))))
 
-(defun org-export--prepare-file-contents (file &optional lines ind minlevel id)
+(defun org-export--prepare-file-contents (file &optional headline no-headline lines ind minlevel id)
   "Prepare the contents of FILE for inclusion and return them as a string.
 
 When optional argument LINES is a string specifying a range of
@@ -3420,6 +3430,20 @@ This is useful to avoid conflicts when more than one Org file
 with footnotes is included in a document."
   (with-temp-buffer
     (insert-file-contents file)
+    (org-mode)
+    (when headline
+      (org-link-search headline)
+      (narrow-to-region
+       (org-element-property
+	(if no-headline :contents-begin :begin) (org-element-at-point))
+       (org-element-property :end (org-element-at-point)))
+      ;; get rid of drawers and properties
+      (when no-headline
+	(let ((element (org-element-at-point)))
+	  (while (member (org-element-type element) '(drawer property-drawer))
+	    (delete-region (org-element-property :begin element)
+			   (org-element-property :end   element))
+	    (setq element (org-element-at-point))))))
     (when lines
       (let* ((lines (split-string lines "-"))
 	     (lbeg (string-to-number (car lines)))
@@ -3492,15 +3516,15 @@ with footnotes is included in a document."
 		    ((org-string-match-p "\\`[0-9]+\\'" label)
 		     (insert (format "fn:%d-" id)))
 		    (t (forward-char 3) (insert (format "%d-" id)))))))))
-    (org-element-normalize-string (buffer-string))))
-
-(defun org-export-execute-babel-code ()
-  "Execute every Babel code in the visible part of current buffer."
-  ;; Get a pristine copy of current buffer so Babel references can be
-  ;; properly resolved.
-  (let ((reference (org-export-copy-buffer)))
-    (unwind-protect (org-babel-exp-process-buffer reference)
-      (kill-buffer reference))))
+    (org-element-normalize-string (buffer-string)))
+
+  (defun org-export-execute-babel-code ()
+    "Execute every Babel code in the visible part of current buffer."
+    ;; Get a pristine copy of current buffer so Babel references can be
+    ;; properly resolved.
+    (let ((reference (org-export-copy-buffer)))
+      (unwind-protect (org-babel-exp-process-buffer reference)
+	(kill-buffer reference)))))
 
 (defun org-export--copy-to-kill-ring-p ()
   "Return a non-nil value when output should be added to the kill ring.
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2014-10-02 20:58 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-09-21  0:51 [patch, ox] #+INCLUDE resolves links Rasmus
2014-09-21 11:46 ` Rasmus
2014-09-21 13:53   ` Nicolas Goaziou
2014-09-21 14:46     ` Rasmus
2014-09-21 19:51       ` Nicolas Goaziou
2014-09-23 23:25     ` Rasmus
2014-09-24 21:22       ` Nicolas Goaziou
2014-09-28 19:32         ` Rasmus
2014-09-30  8:07           ` Nicolas Goaziou
2014-09-30 10:18             ` Rasmus
2014-09-30 14:29               ` Nicolas Goaziou
2014-09-30 21:48                 ` Rasmus
2014-10-01 20:03                   ` Nicolas Goaziou
2014-10-01 21:27                     ` Rasmus
2014-10-02  7:29                       ` Xavier Garrido
2014-10-02  8:55                         ` Rasmus
2014-10-02 16:30                           ` Aaron Ecay
2014-10-02 16:53                           ` Nicolas Goaziou
2014-10-02 17:47                             ` Rasmus
2014-10-02 19:11                               ` Achim Gratz
2014-10-02 20:58                                 ` Rasmus

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).