From: Thorsten Jolitz <tjolitz@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Best way to get buffer changes in a program?
Date: Tue, 18 Mar 2014 17:44:49 +0100 [thread overview]
Message-ID: <87ppljmp6m.fsf@gmail.com> (raw)
In-Reply-To: 874n2voflj.fsf@gmail.com
Thorsten Jolitz <tjolitz@gmail.com> writes:
> Andreas Röhler <andreas.roehler@easy-emacs.de> writes:
>
>> Am 18.03.2014 01:02, schrieb Thorsten Jolitz:
>>>
>>> Hi List,
>>>
>>> there are many ways to track and visualize changes in Emacs (see
>>> e.g. http://www.emacswiki.org/emacs/TrackChanges).
>>>
>>> However, I would like to get non-interactively a list of lines (or
>>> positions) in the buffer that changed after the last command - how to I
>>> do that? Is is somehow possible to determine in a program that calls
>>> other functions that insert text which new lines have been inserted
>>> (when
>>> a buffer increased in size after a command was executed)?
>>>
>>> Thanks in advance for any hints.
>>>
>>
>> You could start from ediff-regions-... running at alist of functions -
>> all that against a list of original buffers.
>
> Thanks for bringing me on the right track, after quite a lot of research in
> the ediff libraries I found this low-level non-interactive function that
> gives me the line numbers I need:
>
> ,-------------------------------------------------------------------------
> | ;; Run the diff program on FILE1 and FILE2 and put the output in
> | ;; DIFF-BUFFER Return the size of DIFF-BUFFER The return code isn't used
> | ;; in the program at present.
> |
> | (defun ediff-make-diff2-buffer (diff-buffer file1 file2) ...)
> `-------------------------------------------------------------------------
Just for the record, although I doubt that this is a frequently
requested functionality, this is what I finally came up with to get the
line numbers of the lines that are in file A but not in file B.
#+begin_src emacs-lisp
(defun omm-get-diff-lines (buf-or-file1 buf-or-file2 &optional exec-diff-program)
"Get line-number of added lines in diff of file args.
BUF-OR-FILE1 should be the file (or its visiting buffer) where
new lines have been added, BUF-OR-FILE2 the smaller original
file (or its visiting buffer).
Optional string argument EXEC-DIFF-PROGRAM defaults to \"diff\"
and should be an executable shell-command."
(let* ((file1 (cond
((file-readable-p buf-or-file1)
buffer-or-file1)
((file-readable-p
(buffer-file-name (get-buffer buf-or-file1)))
(buffer-file-name (get-buffer buf-or-file1)))
(t (error
"%s not a file (visiting buffer)"
buf-or-file1))))
(file2 (cond
((file-readable-p buf-or-file2)
buffer-or-file2)
((file-readable-p
(buffer-file-name (get-buffer buf-or-file2)))
(buffer-file-name (get-buffer buf-or-file2)))
(t (error
"%s not a file (visiting buffer)"
buf-or-file2))))
(diff-prg (or (org-string-nw-p exec-diff-program)
"diff"))
(diff-lst (split-string
(shell-command-to-string
(format "%s %s %s" diff-prg file1 file2))
"\\(---\\|^<.*$\\|^>.*\\|\\\n\\)")))
(mapcar (lambda (--pair)
(split-string (cadr --pair) ","))
(mapcar (lambda (--diff)
(split-string --diff "a"))
(setq diff-lst
(remove-if
(lambda (--strg)
(or (not (org-string-nw-p --strg))
(not (member
"a"
(split-string --strg "")))))
diff-lst))))))
#+end_src
--
cheers,
Thorsten
prev parent reply other threads:[~2014-03-18 16:44 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-03-18 0:02 Best way to get buffer changes in a program? Thorsten Jolitz
2014-03-18 6:40 ` Andreas Röhler
2014-03-18 12:28 ` Thorsten Jolitz
2014-03-18 16:44 ` Thorsten Jolitz [this message]
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87ppljmp6m.fsf@gmail.com \
--to=tjolitz@gmail.com \
--cc=help-gnu-emacs@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 external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.