emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* A small hack to document programs externally
@ 2013-12-06 16:26 Alan Schmitt
  2013-12-06 21:46 ` Suvayu Ali
  2014-01-04 15:01 ` Bastien
  0 siblings, 2 replies; 7+ messages in thread
From: Alan Schmitt @ 2013-12-06 16:26 UTC (permalink / raw)
  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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: A small hack to document programs externally
  2013-12-06 16:26 A small hack to document programs externally Alan Schmitt
@ 2013-12-06 21:46 ` Suvayu Ali
  2014-01-04 15:01 ` Bastien
  1 sibling, 0 replies; 7+ messages in thread
From: Suvayu Ali @ 2013-12-06 21:46 UTC (permalink / raw)
  To: emacs-orgmode

On Fri, Dec 06, 2013 at 05:26:05PM +0100, Alan Schmitt wrote:
> 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.)

This sounds interesting.  I have been wanting to do this for a while.
I'll have a look at your solution when I find some time, maybe this
weekend.

-- 
Suvayu

Open source is the future. It sets us free.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: A small hack to document programs externally
  2013-12-06 16:26 A small hack to document programs externally Alan Schmitt
  2013-12-06 21:46 ` Suvayu Ali
@ 2014-01-04 15:01 ` Bastien
  2014-01-04 16:36   ` Alan Schmitt
  2014-02-01 10:22   ` Alan Schmitt
  1 sibling, 2 replies; 7+ messages in thread
From: Bastien @ 2014-01-04 15:01 UTC (permalink / raw)
  To: Alan Schmitt; +Cc: Org Mode

Hi Alan,

Alan Schmitt <alan.schmitt@polytechnique.org> writes:

> 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.)

It would be nice to have this on worg/org-hacks.org !

-- 
 Bastien

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: A small hack to document programs externally
  2014-01-04 15:01 ` Bastien
@ 2014-01-04 16:36   ` Alan Schmitt
  2014-02-01 10:22   ` Alan Schmitt
  1 sibling, 0 replies; 7+ messages in thread
From: Alan Schmitt @ 2014-01-04 16:36 UTC (permalink / raw)
  To: Bastien; +Cc: Org Mode

Bastien <bzg@gnu.org> writes:

> Hi Alan,
>
> Alan Schmitt <alan.schmitt@polytechnique.org> writes:
>
>> 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.)
>
> It would be nice to have this on worg/org-hacks.org !

I'm not promising anything, but I've added such a task on my (long)
org-related hacking todo list.

Best,

Alan

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: A small hack to document programs externally
  2014-01-04 15:01 ` Bastien
  2014-01-04 16:36   ` Alan Schmitt
@ 2014-02-01 10:22   ` Alan Schmitt
  2014-02-03  8:59     ` Bastien
  1 sibling, 1 reply; 7+ messages in thread
From: Alan Schmitt @ 2014-02-01 10:22 UTC (permalink / raw)
  To: Bastien; +Cc: Org Mode

Bastien <bzg@gnu.org> writes:

> Hi Alan,
>
> Alan Schmitt <alan.schmitt@polytechnique.org> writes:
>
>> 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.)
>
> It would be nice to have this on worg/org-hacks.org !

I finally found the time to do it:
http://orgmode.org/worg/org-hacks.html#sec-1-10-4

Any criticism is highly welcome!

Alan

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: A small hack to document programs externally
  2014-02-01 10:22   ` Alan Schmitt
@ 2014-02-03  8:59     ` Bastien
  2014-02-03 12:11       ` Alan Schmitt
  0 siblings, 1 reply; 7+ messages in thread
From: Bastien @ 2014-02-03  8:59 UTC (permalink / raw)
  To: Alan Schmitt; +Cc: Org Mode

Hi Alan,

Alan Schmitt <alan.schmitt@polytechnique.org> writes:

> I finally found the time to do it:
> http://orgmode.org/worg/org-hacks.html#sec-1-10-4
>
> Any criticism is highly welcome!

Here is slightly rewritten version :

(defun fetchlines (file-path search-string &optional end before)
  "Searches for the SEARCH-STRING in FILE-PATH and returns the matching line.
If the optional argument END is provided as a number, then this
number of lines is printed.  If END is a string, then it is a
regular expression indicating the end of the expression to print.
If END is omitted, then 10 lines are printed.  If BEFORE is set,
then one fewer line is printed (this is useful when END is a
string matching the first line that should not be printed)."
  (with-temp-buffer
    (insert-file-contents file-path nil nil nil t)
    (goto-char (point-min))
    (let ((result
	   (if (search-forward search-string nil t)
	       (buffer-substring
		(line-beginning-position)
		(if end
		    (cond
		     ((integerp end)
		      (line-end-position (if before (- end 1) end)))
		     ((stringp end)
		      (let ((point (re-search-forward end nil t)))
			(if before (line-end-position 0) point)))
		     (t (line-end-position 10)))
		  (line-end-position 10))))))
      (or result ""))))

  (fetchlines (concat coqfiles f ".v") s e b)

The most common error it catches is (goto-char 1) which should be
(goto-char (point-min)) -- This way narrowing and other commands that
change (point-min) will not interfere.  Otherwise this is just using
`with-temp-buffer', which fits best here IMO.

Thanks for taking the time to add this,

-- 
 Bastien

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: A small hack to document programs externally
  2014-02-03  8:59     ` Bastien
@ 2014-02-03 12:11       ` Alan Schmitt
  0 siblings, 0 replies; 7+ messages in thread
From: Alan Schmitt @ 2014-02-03 12:11 UTC (permalink / raw)
  To: Bastien; +Cc: Org Mode

Hi Bastion,

Bastien <bzg@gnu.org> writes:

> The most common error it catches is (goto-char 1) which should be
> (goto-char (point-min)) -- This way narrowing and other commands that
> change (point-min) will not interfere.  Otherwise this is just using
> `with-temp-buffer', which fits best here IMO.

Thanks a lot! I'm still reading the giraffe book so there is much yet to
learn. I pushed this updated version on worg.

Alan

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-02-03 12:12 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-12-06 16:26 A small hack to document programs externally Alan Schmitt
2013-12-06 21:46 ` Suvayu Ali
2014-01-04 15:01 ` Bastien
2014-01-04 16:36   ` Alan Schmitt
2014-02-01 10:22   ` Alan Schmitt
2014-02-03  8:59     ` Bastien
2014-02-03 12:11       ` Alan Schmitt

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).