29

You want the command M-x diff-buffer-with-file. See the manual: diff-buffer-with-file is an interactive autoloaded compiled Lisp function in `diff.el'. (diff-buffer-with-file &optional BUFFER) View the differences between BUFFER and its associated file. This requires the external program diff to be in your exec-path. You may also be ...


16

Command ediff-current-file: ediff-current-file is an interactive autoloaded Lisp function in `ediff.el'. (ediff-current-file) Start ediff between current buffer and its file on disk. This command can be used instead of `revert-buffer'. If there is nothing to revert then this command fails.


10

There is a built-in macro for this, with-silent-modifications. Its intended usecase is for making changes to text properties of a buffer as text properties are part of the text and changes to them would otherwise be reflected in the buffer modification status and trigger the respective hooks. Despite there being rudimentary protection against more invasive ...


6

@stsquad's answer got me on the right path. Basically, the steps are: save buffer-undo-list disable undo do your thing re-enable undo and restore the buffer-undo-list So here's a sketch of such a function: (defun disable-undo-one-off () (let ((undo buffer-undo-list)) ; save the undo list (buffer-disable-undo) ; disable undo ...


5

Building off of the comments, here are two ways to achieve what you're trying to do. (Not extensively tested, so YMMV.) Option 1: post-command-hook Create a function that tests whether or not the buffer is modified, and then hook it into post-command-hook: (defun hl-line-mode-toggle-maybe () "Turn on `hl-line-mode' when buffer is unmodified, turn it ...


5

This isn't specific to focus in/out but rather to just updating buffers when the underlying file changes. Emacs calls this "reverting" the buffer and has the functions revert-buffer to do it manually and auto-revert-mode to revert automatically when the file changes on disk. auto-revert-mode can be added to the hook for whatever mode your using or you turn ...


4

Yes, just put text-property read-only on those bits of text. For example, to make a zone of text from position START to position END read-only, do this: (put-text-property start end 'read-only t)


3

An undo operation combines several elements from the undo list. A nil entry in the list marks the boundary between two change groups. By removing the nil at the start of the list which is automatically inserted by the toplevel loop┬╣, you can group the timestamp update with the last buffer change, which technically doesn't match your request but practically ...


2

The problem with what you suggest is that the undo-tree is a list of deltas to get from where you are to where you want to be. While it is perfectly possible to disable undo tracking on a buffer I'm not sure what the effect of not actively recording changes would be. I currently have a toggle to turn undo on/off on a particular buffer as it doesn't make ...


2

When running undo, there are two possibilities. Either you're undoing something, or redoing something. You can tell the difference by what gets echoed to the minibuffer. On my Emacs (24.3.1), after explicitly setting the modified status of the buffer to nil, any "undo" results in setting the buffer status to edited, but "redo" does not change the buffer ...


2

As you're familiar with ediff you can also: M-x ediff-current-file Start ediff between current buffer and its file on disk. This command can be used instead of `revert-buffer'. If there is nothing to revert then this command fails. I have both this and a custom diff-buffer-with-file variant bound to keys, as I use them quite frequently.


1

Yes, it should be safe (assuming you're careful enough to make sure that the buffer is really unchanged in the end). But be careful: if the code that is run while the buffer is modified is complex enough you might get into trouble (e.g. it might fill the syntax-ppss cache with data which won't be flushed when you undo the changes). Even worse if the code ...


1

I use scratch buffers for that purpose. (defun python-scratch () (interactive) (let ((python-scratch-buffer (get-buffer-create "*python-scratch*"))) (switch-to-buffer python-scratch-buffer) (python-mode))) You can convert the buffer with the file to a scratch buffer. (defun my-detach-buffer-from-file () (interactive) (setq buffer-file-...


1

Issue with csharp-mode. Fixed in melpa version 20160909.801. See the bug report here.


1

As politza mentioned let binding of inhibit-modification-hooks to nil worked well. (let ((inhibit-modification-hooks nil)) (my-custom-function))


1

My crystal ball tells me that when you call (set-buffer-modified-p nil) you've only made "one" modification to the buffer since it was unmodified. So if you hadn't called (set-buffer-modified-p nil), the "undo" would have marked the buffer as unmodified. And as it turns out, calling set-buffer-unmodified has no effect on that part of the behavior: when ...


Only top voted, non community-wiki answers of a minimum length are eligible