[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Best way to get buffer changes in a program?
From: |
Thorsten Jolitz |
Subject: |
Re: Best way to get buffer changes in a program? |
Date: |
Tue, 18 Mar 2014 17:44:49 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) |
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