From mboxrd@z Thu Jan 1 00:00:00 1970 From: Bastien Subject: Re: A small hack to document programs externally Date: Mon, 03 Feb 2014 09:59:08 +0100 Message-ID: <87ha8gy3kz.fsf@bzg.ath.cx> References: <87wqifrdub.fsf@bzg.ath.cx> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:60233) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WAFN7-0005ww-ND for emacs-orgmode@gnu.org; Mon, 03 Feb 2014 03:59:22 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WAFN2-0001ih-Vf for emacs-orgmode@gnu.org; Mon, 03 Feb 2014 03:59:17 -0500 Received: from mail-we0-x22f.google.com ([2a00:1450:400c:c03::22f]:34835) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WAFN2-0001iZ-Ov for emacs-orgmode@gnu.org; Mon, 03 Feb 2014 03:59:12 -0500 Received: by mail-we0-f175.google.com with SMTP id q59so1823712wes.20 for ; Mon, 03 Feb 2014 00:59:12 -0800 (PST) In-Reply-To: (Alan Schmitt's message of "Sat, 01 Feb 2014 11:22:16 +0100") 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: Alan Schmitt Cc: Org Mode Hi Alan, Alan Schmitt 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