Browse Source

Allow prefixes in include file statements.

Carsten Dominik 17 years ago
parent
commit
3d59326fcf
5 changed files with 61 additions and 9 deletions
  1. 5 0
      contrib/ChangeLog
  2. 9 4
      contrib/lisp/org-mtags.el
  3. 8 1
      doc/org.texi
  4. 6 0
      lisp/ChangeLog
  5. 33 4
      lisp/org-exp.el

+ 5 - 0
contrib/ChangeLog

@@ -1,3 +1,8 @@
+2008-07-02  Carsten Dominik  <dominik@science.uva.nl>
+
+	* lisp/org-mtags.el (org-mtags-replace): Allow prefix and prefix1
+	as options in the include directive.
+
 2008-06-18  Christian Egli  <christian.egli@alumni.ethz.ch>
 2008-06-18  Christian Egli  <christian.egli@alumni.ethz.ch>
 
 
 	* scripts/org2hpda (DIARY): Make the location of the diary file
 	* scripts/org2hpda (DIARY): Make the location of the diary file

+ 9 - 4
contrib/lisp/org-mtags.el

@@ -72,7 +72,7 @@
 ;;        Needs to be on a line by itself, similarly the </src> tag.
 ;;        Needs to be on a line by itself, similarly the </src> tag.
 ;;        Will be translated into Org's BEGIN_SRC construct.
 ;;        Will be translated into Org's BEGIN_SRC construct.
 ;;
 ;;
-;;   <include file="FILE" markup="MARKUP" lang="LANG">
+;;   <include file="FILE" markup="MARKUP" lang="LANG" prefix="str" prefix1="str">
 ;;        Needs to be on a line by itself.
 ;;        Needs to be on a line by itself.
 ;;        Will be translated into Org's #+INCLUDE construct.
 ;;        Will be translated into Org's #+INCLUDE construct.
 ;;
 ;;
@@ -128,7 +128,7 @@ The is done in the entire buffer."
   (let ((re (concat "^[ \t]*\\(</?\\("
   (let ((re (concat "^[ \t]*\\(</?\\("
 		    (mapconcat 'identity org-mtags-supported-tags "\\|")
 		    (mapconcat 'identity org-mtags-supported-tags "\\|")
 		    "\\)\\>\\)"))
 		    "\\)\\>\\)"))
-	info tag rpl style markup lang file)
+	info tag rpl style markup lang file prefix prefix1)
     ;; First, do the <br> tag
     ;; First, do the <br> tag
     (goto-char (point-min))
     (goto-char (point-min))
     (while (re-search-forward "<br>[ \t]*$" nil t)
     (while (re-search-forward "<br>[ \t]*$" nil t)
@@ -177,12 +177,17 @@ The is done in the entire buffer."
 	 ((equal tag "include")
 	 ((equal tag "include")
 	  (setq file (plist-get info :file)
 	  (setq file (plist-get info :file)
 		markup (downcase (plist-get info :markup))
 		markup (downcase (plist-get info :markup))
-		lang (plist-get info :lang))
+		lang (plist-get info :lang)
+		prefix (plist-get info :prefix)
+		prefix1 (plist-get info :prefix1))
 	  (setq rpl "#+INCLUDE")
 	  (setq rpl "#+INCLUDE")
 	  (when markup
 	  (when markup
 	    (setq rpl (concat rpl " " markup))
 	    (setq rpl (concat rpl " " markup))
 	    (when (and (equal markup "src") lang)
 	    (when (and (equal markup "src") lang)
-	      (setq rpl (concat rpl " " lang))))))
+	      (setq rpl (concat rpl " " lang))))
+	  (setq rpl (concat rpl
+			    " :prefix " prin1-to-string prefix
+			    " :prefix1 " prin1-to-string prefix1))))
 	(when rpl
 	(when rpl
 	  (goto-char (plist-get info :match-beginning))
 	  (goto-char (plist-get info :match-beginning))
 	  (delete-region (point-at-bol) (plist-get info :match-end))
 	  (delete-region (point-at-bol) (plist-get info :match-end))

+ 8 - 1
doc/org.texi

@@ -6941,7 +6941,14 @@ The optional second and third parameter are the markup (@samp{quote},
 @samp{example}, or @samp{src}), and, if the markup is @samp{src}, the
 @samp{example}, or @samp{src}), and, if the markup is @samp{src}, the
 language for formatting the contents.  The markup is optional, if it is not
 language for formatting the contents.  The markup is optional, if it is not
 given, the text will be assumed to be in Org mode format and will be
 given, the text will be assumed to be in Org mode format and will be
-processed normally.
+processed normally.  The include line will also allow additional keyword
+parameters @code{:prefix1} and @code{:prefix} to specify prefixes for the
+first line and for each following line.  For example, to include a file as an
+item, use
+
+@example
+#+INCLUDE: "~/snippets/xx" :prefix1 "   + " :prefix "     "
+@end example
 
 
 @table @kbd
 @table @kbd
 @kindex C-c '
 @kindex C-c '

+ 6 - 0
lisp/ChangeLog

@@ -1,3 +1,9 @@
+2008-07-02  Carsten Dominik  <dominik@science.uva.nl>
+
+	* org-exp.el (org-get-file-contents)
+	(org-get-and-remove-property): New functions.
+	(org-export-handle-include-files): Handle the new prefix options.
+
 2008-07-01  Carsten Dominik  <dominik@science.uva.nl>
 2008-07-01  Carsten Dominik  <dominik@science.uva.nl>
 
 
 	* org.el (org-time=, org-time<, org-time<=, org-time>)
 	* org.el (org-time=, org-time<, org-time<=, org-time>)

+ 33 - 4
lisp/org-exp.el

@@ -1895,10 +1895,12 @@ TYPE must be a string, any of:
 (defun org-export-handle-include-files ()
 (defun org-export-handle-include-files ()
   "Include the contents of include files, with proper formatting."
   "Include the contents of include files, with proper formatting."
   (let ((case-fold-search t)
   (let ((case-fold-search t)
-	params file markup lang start end)
+	params file markup lang start end prefix prefix1)
     (goto-char (point-min))
     (goto-char (point-min))
     (while (re-search-forward "^#\\+INCLUDE:?[ \t]+\\(.*\\)" nil t)
     (while (re-search-forward "^#\\+INCLUDE:?[ \t]+\\(.*\\)" nil t)
       (setq params (read (concat "(" (match-string 1) ")"))
       (setq params (read (concat "(" (match-string 1) ")"))
+	    prefix (org-get-and-remove-property 'params :prefix)
+	    prefix1 (org-get-and-remove-property 'params :prefix1)
 	    file (org-symname-or-string (pop params))
 	    file (org-symname-or-string (pop params))
 	    markup (org-symname-or-string (pop params))
 	    markup (org-symname-or-string (pop params))
 	    lang (org-symname-or-string (pop params)))
 	    lang (org-symname-or-string (pop params)))
@@ -1914,17 +1916,45 @@ TYPE must be a string, any of:
 	    (setq start (format "#+begin_%s\n" markup)
 	    (setq start (format "#+begin_%s\n" markup)
 		  end  (format "#+end_%s" markup))))
 		  end  (format "#+end_%s" markup))))
 	(insert (or start ""))
 	(insert (or start ""))
-	(forward-char (nth 1 (insert-file-contents (expand-file-name file))))
+	(insert (org-get-file-contents (expand-file-name file) prefix prefix1))
 	(or (bolp) (newline))
 	(or (bolp) (newline))
 	(insert (or end ""))))))
 	(insert (or end ""))))))
 
 
+(defun org-get-file-contents (file &optional prefix prefix1)
+  "Get the contents of FILE and return them as a string.
+If PREFIX is a string, prepend it to each line.  If PREFIX1
+is a string, prepend it to the first line instead of PREFIX."
+  (with-temp-buffer
+    (insert-file-contents file)
+    (when (or prefix prefix1)
+      (goto-char (point-min))
+      (while (not (eobp))
+	(insert (or prefix1 prefix))
+	(setq prefix1 nil)
+	(beginning-of-line 2)))
+    (buffer-string)))
+
+(defun org-get-and-remove-property (listvar prop)
+  "Check if the value of LISTVAR contains PROP as a property.
+If yes, return the value of that property (i.e. the element following
+in the list) and remove property and value from the list in LISTVAR."
+  (let ((list (symbol-value listvar)) m v)
+    (when (setq m (member prop list))
+      (setq v (nth 1 m))
+      (if (equal (car list) prop)
+	  (set listvar (cddr list))
+	(setcdr (nthcdr (- (length list) (length m) 1) list)
+		(cddr m))
+	(set listvar list)))
+    v))
+
 (defun org-symname-or-string (s)
 (defun org-symname-or-string (s)
   (if (symbolp s)
   (if (symbolp s)
       (if s (symbol-name s) s)
       (if s (symbol-name s) s)
     s))
     s))
 
 
 ;;; Fontification of code
 ;;; Fontification of code
-;; Currently only for th HTML backend, but who knows....
+;; Currently only for the HTML backend, but who knows....
 (defun org-export-replace-src-segments ()
 (defun org-export-replace-src-segments ()
   "Replace source code segments with special code for export."
   "Replace source code segments with special code for export."
   (let ((case-fold-search t)
   (let ((case-fold-search t)
@@ -4371,4 +4401,3 @@ The XOXO buffer is named *xoxo-<source buffer name>*"
 ;; arch-tag: 65985fe9-095c-49c7-a7b6-cb4ee15c0a95
 ;; arch-tag: 65985fe9-095c-49c7-a7b6-cb4ee15c0a95
 
 
 ;;; org-exp.el ends here
 ;;; org-exp.el ends here
-