From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rasmus Subject: [patch, ox] #+INCLUDE resolves links Date: Sun, 21 Sep 2014 02:51:34 +0200 Message-ID: <87k34x6bjd.fsf@gmx.us> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:42831) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XVVNh-00030z-PN for emacs-orgmode@gnu.org; Sat, 20 Sep 2014 20:52:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XVVNb-0004M3-FU for emacs-orgmode@gnu.org; Sat, 20 Sep 2014 20:52:01 -0400 Received: from plane.gmane.org ([80.91.229.3]:59749) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XVVNb-0004I9-58 for emacs-orgmode@gnu.org; Sat, 20 Sep 2014 20:51:55 -0400 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1XVVNV-00081h-5b for emacs-orgmode@gnu.org; Sun, 21 Sep 2014 02:51:49 +0200 Received: from 46.166.186.235 ([46.166.186.235]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 21 Sep 2014 02:51:49 +0200 Received: from rasmus by 46.166.186.235 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 21 Sep 2014 02:51:49 +0200 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-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit 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! --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-ox-Allow-links-with-INCLUDE-keyword.patch >From 727b20f454cb4f1582874f1b35d5e5f53ec44f79 Mon Sep 17 00:00:00 2001 From: Rasmus 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 --=-=-=--