From mboxrd@z Thu Jan 1 00:00:00 1970 From: Alan Schmitt Subject: A small hack to document programs externally Date: Fri, 06 Dec 2013 17:26:05 +0100 Message-ID: Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:57457) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VoyEH-0001SA-OQ for emacs-orgmode@gnu.org; Fri, 06 Dec 2013 11:26:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VoyEA-0000BP-8g for emacs-orgmode@gnu.org; Fri, 06 Dec 2013 11:26:13 -0500 Received: from mail2-relais-roc.national.inria.fr ([192.134.164.83]:3529) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VoyEA-0000B5-24 for emacs-orgmode@gnu.org; Fri, 06 Dec 2013 11:26:06 -0500 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: Org Mode Hello, I've just written a small hack to refer to code in other files from an org-mode buffer, so that the referred code would be copied in the buffer to be exported when exporting the org buffer. (Our use case is a large Coq development which we want to document online. Our may constraint is that we need the documentation to live in files outside the code, yet still refer to the actual code.) Here is a small test case showing the approach --8<---------------cut here---------------start------------->8--- #+OPTIONS: d:RESULTS * utilities :noexport: #+name: fetch #+BEGIN_SRC emacs-lisp :results raw :var f="foo.v" :var s="Definition" :var n=0 (defun fetchlines (file-path search-string nb-lines) "Searches for the SEARCH-STRING in FILE-PATH and returns the matching line and the following NB-LINES." (let ((myBuffer (get-buffer-create "fetchTemp")) result) (set-buffer myBuffer) (insert-file-contents file-path nil nil nil t) (goto-char 1) (setq result (if (search-forward search-string nil t) (let ((pos-beg (line-beginning-position)) (pos-end (if (> nb-lines 0) (line-end-position nb-lines) (re-search-forward "\\.$" nil t)) )) (buffer-substring pos-beg pos-end)) "")) (kill-buffer myBuffer) result)) (fetchlines f s n) #+END_SRC #+name: wrap-coq #+BEGIN_SRC emacs-lisp :var text="" :results raw (concat "#+BEGIN_SRC coq\n" text "\n#+END_SRC") #+END_SRC * example :PROPERTIES: :results: drawer :post: wrap-coq(text=*this*) :END: #+call: fetch(f="/Users/schmitta/work/hocorecoq/coq/HOC01defs.v", s="Inductive process") #+RESULTS: :RESULTS: #+BEGIN_SRC coq Inductive process : Set := | Send : chan -> process -> process | Receive : chan -> lvar -> process -> process | Lvar : lvar -> process | Gvar : var -> process | Par : process -> process -> process | Nil : process. #+END_SRC :END: --8<---------------cut here---------------end--------------->8--- The ~fetch~ function takes as arguments the file where to search, the string to be searched, and the number of lines to print. If this last argument is omitted (or equal to 0), then everything until a final dot ('.' followed by a newline) is printed (this corresponds to the end of a definition or statement in Coq). Thanks to everyone who answered my recent questions on the list. And if you have suggestions to improve this (including ways it may already be doable natively), please don't hesitate to send them. Best, Alan