emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] A small hack to document programs externally


From: Bastien
Subject: Re: [O] A small hack to document programs externally
Date: Mon, 03 Feb 2014 09:59:08 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

Hi Alan,

Alan Schmitt <address@hidden> 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 via email to

[Prev in Thread] Current Thread [Next in Thread]