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

inserting links in a comment before source-code blocks

This commit is contained in:
Eric Schulte 2009-06-27 11:58:13 -07:00
parent e05ddae2f7
commit 0d3952ecf6

View File

@ -42,66 +42,71 @@ file.")
"Extract the bodies of all source code blocks form the current
file into their own source-specific files."
(interactive)
(let ((base-name (file-name-sans-extension (buffer-file-name)))
blocks)
;; blocks will be two nested association lists, first grouped by
;; language, then by session, the contents of the second a-list
;; will be source-code blocks
(org-babel-map-source-blocks (buffer-file-name)
(let* ((link (progn (org-store-link nil) (pop org-stored-links)))
(source-name (intern (org-babel-get-src-block-name)))
(info (org-babel-get-src-block-info))
(lang (first info))
(body (second info))
(params (third info))
(spec (list link source-name params body))
(session (cdr (assoc :session params)))
by-lang by-session)
;; add the spec for this block to blocks under it's lang and session
(setq by-lang (org-babel-alist-pop lang blocks))
(setq by-session (org-babel-alist-pop session by-lang))
(setq blocks (cons ;; by-language
(cons lang (cons ;; by-session
(cons session (cons spec by-session)) by-lang))
blocks))))
;; blocks should contain all source-blocks organized by language
;; and session
;; (message "block = %S" blocks) ;; debugging
(mapc ;; for every language create a file
(lambda (by-lang)
(let* ((lang (car by-lang))
(lang-f (intern (concat lang "-mode")))
(lang-specs (cdr (assoc lang org-babel-tangle-langs)))
(ext (first lang-specs))
(she-bang (second lang-specs))
(by-session (cdr by-lang)))
(flet ((to-file (filename specs)
(with-temp-file filename
(funcall lang-f)
(when she-bang (insert she-bang))
(comment-region (point) (progn (insert "generated by org-babel-tangle") (point)))
(message "specs=%s" specs)
(mapc #'org-babel-spec-to-string specs))))
;; (message "lang=%s lang-specs=%s ext=%s she-bang=%s by-session=%s" lang lang-specs ext she-bang by-session) ;; debugging
;; if there are multiple sessions then break out by session
(if (> (length by-session) 1)
(mapc (lambda (session-pair)
(to-file (format "%s-%s.%s" base-name (car session-pair) ext) (cdr session-pair)))
by-session)
(message "by-session=%s" by-session)
(to-file (format "%s.%s" base-name ext) (cdr (car by-session)))))))
blocks)))
(save-excursion
(let ((base-name (file-name-sans-extension (buffer-file-name)))
blocks)
;; blocks will be two nested association lists, first grouped by
;; language, then by session, the contents of the second a-list
;; will be source-code blocks
(org-babel-map-source-blocks (buffer-file-name)
(let* ((link (progn (call-interactively 'org-store-link)
(org-babel-clean-text-properties (car (pop org-stored-links)))))
(source-name (intern (org-babel-get-src-block-name)))
(info (org-babel-get-src-block-info))
(lang (first info))
(body (second info))
(params (third info))
(spec (list link source-name params body))
(session (cdr (assoc :session params)))
by-lang by-session)
;; add the spec for this block to blocks under it's lang and session
(setq by-lang (org-babel-alist-pop lang blocks))
(setq by-session (org-babel-alist-pop session by-lang))
(setq blocks (cons ;; by-language
(cons lang (cons ;; by-session
(cons session (cons spec by-session)) by-lang))
blocks))))
;; blocks should contain all source-blocks organized by language
;; and session
(mapc ;; for every language create a file
(lambda (by-lang)
(let* ((lang (car by-lang))
(lang-f (intern (concat lang "-mode")))
(lang-specs (cdr (assoc lang org-babel-tangle-langs)))
(ext (first lang-specs))
(she-bang (second lang-specs))
(by-session (cdr by-lang)))
(flet ((to-file (filename specs)
(with-temp-file filename
(funcall lang-f)
(when she-bang (insert she-bang))
(comment-region (point) (progn (insert "generated by org-babel-tangle") (point)))
(mapc #'org-babel-spec-to-string specs))))
;; if there are multiple sessions then break out by session
(if (> (length by-session) 1)
(mapc (lambda (session-pair)
(to-file (format "%s-%s.%s" base-name (car session-pair) ext) (cdr session-pair)))
by-session)
(to-file (format "%s.%s" base-name ext) (cdr (car by-session)))))))
blocks))))
(defun org-babel-spec-to-string (spec)
"Return the string version of spec suitable for inclusion in a
source code file. SPEC has the form
"Insert the source-code specified by SPEC into the current
source code file. This function uses `comment-region' which
assumes that the appropriate major-mode is set. SPEC has the
form
(link source-name params body)"
(flet ((insert-comment (text)
(comment-region (point) (progn (insert text) (point)))))
(insert-comment (format "\n\nlink: %s" (first spec)))
(insert-comment (format "\nsource-name: %s" (second spec)))
(insert (format "\n%s\n" (fourth spec)))))
(let ((link (first spec))
(source-name (second spec))
(body (fourth spec)))
(insert "\n\n")
(insert-comment (format "[[%s][%s]]" (org-link-escape link) source-name))
(insert (format "\n%s\n" (org-babel-chomp body)))
(insert-comment (format "%s ends here" source-name))
(insert "\n"))))
(provide 'org-babel-tangle)
;;; org-babel-tangle.el ends here