1
0
mirror of https://git.savannah.gnu.org/git/emacs/org-mode.git synced 2025-01-26 20:20:38 +00:00

now able to return the current source-code block's body with noweb references expanded

new function is `org-babel-expand-noweb-references'
This commit is contained in:
Eric Schulte 2009-07-31 16:53:02 -06:00
parent 96016821ba
commit 17b2008968
3 changed files with 56 additions and 8 deletions

View File

@ -49,7 +49,6 @@ file using `load-file'."
(sixth (file-attributes file))))))
(let* ((base-name (file-name-sans-extension file))
(exported-file (concat base-name ".el")))
;; (message "building %s" exported-file) ;; debugging
;; tangle if the org-mode file is newer than the elisp file
(unless (and (file-exists-p exported-file) (> (age file) (age exported-file)))
(org-babel-tangle-file file base-name "emacs-lisp"))
@ -74,7 +73,7 @@ exported source code blocks by language."
(save-excursion
(let ((block-counter 0)
path-collector)
(mapc ;; for every language create a file
(mapc ;; map over all languages
(lambda (by-lang)
(let* ((lang (car by-lang))
(specs (cdr by-lang))
@ -165,5 +164,48 @@ form
(insert-comment (format "%s ends here" source-name))
(insert "\n"))))
(defun org-babel-expand-noweb-references (&optional info parent-buffer)
"This function expands Noweb style references in the body of
the current source-code block. The reference must be inside of a
comment or it will be skipped. For example the following
reference would be replaced with the body of the source-code
block named 'example-block' (assuming the '#' character starts a
comment) .
# <<example-block>>
This function must be called from inside of the buffer containing
the source-code block which holds BODY."
(interactive)
(let* ((parent-buffer (or parent-buffer (current-buffer)))
(info (or info (org-babel-get-src-block-info)))
(lang (first info))
(body (second info))
(new-body "") index source-name)
(with-temp-buffer
(insert body) (goto-char (point-min))
(funcall (intern (concat lang "-mode")))
(setq index (point))
(while (and (re-search-forward "<<\\(.+\\)>>" nil t)
(save-match-data (comment-beginning)))
(save-match-data (setf source-name (match-string 1)))
;; add interval to new-body
(goto-char (match-end 0))
(setq new-body (concat new-body (buffer-substring index (point))))
(setq index (point))
;; if found, add body of referenced source-block
(setq new-body
(concat new-body
(save-excursion
(set-buffer parent-buffer)
(let ((point (org-babel-find-named-block source-name)))
(if point
(save-excursion
(goto-char point)
(concat "\n" (second (org-babel-get-src-block-info))))
""))))))
(setq new-body (concat new-body (buffer-substring index (point-max)))))
new-body))
(provide 'org-babel-tangle)
;;; org-babel-tangle.el ends here

View File

@ -208,10 +208,10 @@ would then be [[#sandbox][the sandbox]].
* Tasks [39/61]
** TODO new reference syntax *inside* source code blocks
** STARTED new reference syntax *inside* source code blocks
This is from an email discussion on the org-mode mailing list with
Sébastien. The goal here is to mimic the source-block reference style
of Nuweb. Upon export and/or tangle these references could be
of Noweb. Upon export and/or tangle these references could be
replaced with the actual body of the referenced source-code block.
See the following for an example.
@ -227,14 +227,14 @@ puts "---------------------------footer---------------------------"
#+end_src
#+srcname: ems-ruby-print-message
#+begin_src ruby :file ruby-nuweb.rb
#+begin_src ruby :file ruby-noweb.rb
# <<ems-ruby-print-header>>
puts " Ruby "
# <<ems-ruby-print-footer>>
#+end_src
Upon export the previous source-code block would result in a file
being generated at =ruby-nuweb.rb= with the following contents
being generated at =ruby-noweb.rb= with the following contents
: puts "---------------------------header---------------------------"
: puts " Ruby "
@ -1179,7 +1179,7 @@ org-mode file) we want to get nearly every source-code block.
Sometimes we want to only extract those source-code blocks which
reference a indicate that they should be extracted (e.g. traditional
literate programming along the Nuweb model)
literate programming along the Noweb model)
I'm not sure how we can devise a single simple tangling system that
naturally fits both of these use cases.

View File

@ -19,12 +19,18 @@ more text
end
#+end_src
#+srcname: first-ruby-block
#+begin_src ruby :session special :tangle trivial-symbol
:block_the_first
#+end_src
#+srcname: ruby-with-noweb-references
#+begin_src ruby
# <<ruby-no-session>>
hello()
#+end_src
** Some subsection