armandino
armandino

Reputation: 18538

Emacs - set mark on edit location

I want emacs to add last edit location to the mark ring, so I can jump back to previous edit locations.

Ideally this would only mark one edit location per line. When I edit another line, the last edit location on that line would be added to the ring, and so forth.

I'm not familiar with Lisp to implement this myself. If anyone knows of a plugin or can kindly provide a solution that would be great! :)

Upvotes: 3

Views: 521

Answers (3)

shenedu
shenedu

Reputation: 1161

I implement a similar function, by recording 2 file's last edit locations(not per buffer), and cycle them when requested. Somewhat like how eclipse does(but less powerful, only 2 file's are recorded)

emacs-last-edit-location

the code:

;;; record two different file's last change. cycle them
(defvar feng-last-change-pos1 nil)
(defvar feng-last-change-pos2 nil)

(defun feng-swap-last-changes ()
  (when feng-last-change-pos2
    (let ((tmp feng-last-change-pos2))
      (setf feng-last-change-pos2 feng-last-change-pos1
            feng-last-change-pos1 tmp))))

(defun feng-goto-last-change ()
  (interactive)
  (when feng-last-change-pos1
    (let* ((buffer (find-file-noselect (car feng-last-change-pos1)))
           (win (get-buffer-window buffer)))
      (if win
          (select-window win)
        (switch-to-buffer-other-window buffer))
      (goto-char (cdr feng-last-change-pos1))
      (feng-swap-last-changes))))

(defun feng-buffer-change-hook (beg end len)
  (let ((bfn (buffer-file-name))
        (file (car feng-last-change-pos1)))
    (when bfn
      (if (or (not file) (equal bfn file)) ;; change the same file
          (setq feng-last-change-pos1 (cons bfn end))
        (progn (setq feng-last-change-pos2 (cons bfn end))
               (feng-swap-last-changes))))))

(add-hook 'after-change-functions 'feng-buffer-change-hook)
;;; just quick to reach
(global-set-key (kbd "M-`") 'feng-goto-last-change)

Upvotes: 0

event_jr
event_jr

Reputation: 17707

Session.el provides this functionality bound to "C-x C-/" or session-jump-to-last-change.

Session dos it per buffer. I'm unaware of anything that does it globally.

Upvotes: 1

Oleg Pavliv
Oleg Pavliv

Reputation: 21172

You can install a package goto-last-change which allows you to jump sequentially to the buffer undo positions (last edit locations).

Upvotes: 3

Related Questions