emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Bastien <bzg@gnu.org>
To: Alan Schmitt <alan.schmitt@polytechnique.org>
Cc: Org Mode <emacs-orgmode@gnu.org>
Subject: Re: A small hack to document programs externally
Date: Mon, 03 Feb 2014 09:59:08 +0100	[thread overview]
Message-ID: <87ha8gy3kz.fsf@bzg.ath.cx> (raw)
In-Reply-To: <m2y51vp1yf.fsf@polytechnique.org> (Alan Schmitt's message of "Sat, 01 Feb 2014 11:22:16 +0100")

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

  reply	other threads:[~2014-02-03  8:59 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2014-02-03 12:11       ` Alan Schmitt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ha8gy3kz.fsf@bzg.ath.cx \
    --to=bzg@gnu.org \
    --cc=alan.schmitt@polytechnique.org \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).