line-number last-line))
(goto-char (point-min))
(forward-line (1- line-number)))
-)
-
+ )
-(defun loc-changes-add (line-number &optional opt-buffer)
+(defun loc-changes-add-and-goto (line-number &optional opt-buffer)
"Add a marker at LINE-NUMBER and record LINE-NUMBER and its
marker association in `loc-changes-alist'."
(let ((buffer (or opt-buffer (current-buffer))))
(with-current-buffer buffer
- (loc-changes-goto-line buffer)
+ (loc-changes-goto-line line-number)
(setq loc-changes-alist
- (cons line-number (point-marker))
- loc-changes-alist)
+ (cons (cons line-number (point-marker))
+ loc-changes-alist))
))
-)
+ )
-(defun loc-changes-clear (&optional opt-buffer)
+(defun loc-changes-clear-buffer (&optional opt-buffer)
"Remove all location-tracking associations in BUFFER."
+ (interactive "bbuffer: ")
(let ((buffer (or opt-buffer (current-buffer)))
)
(with-current-buffer buffer
(setq loc-changes-alist '())
))
-)
+ )
(defun loc-changes-resync (&optional opt-buffer)
"Take existing marks and use the current (updated) positions for each of those.
previous location marks. Normally if the position hasn't been
seen before, we will add a new mark for this position. However if
NO-UPDATE is set, no mark is added."
- (error "To be continued....")
+ (unless (wholenump position)
+ (error "Expecting line-number parameter `%s' to be a whole number"
+ position))
+ (let ((elt (assq position loc-changes-alist)))
+ (if elt
+ (let ((marker (cdr elt)))
+ (unless (markerp marker)
+ (error "Internal error: loc-changes-alist is not a marker"))
+ (goto-char (marker-position marker)))
+ (if no-update
+ (loc-changes-goto-line position)
+ (loc-changes-add-and-goto position))
+ )
+ )
)
(provide 'loc-changes)
(test-unit-clear-contexts)
-;; Define a test named `foo'.
+(setq sample-buffer (find-file-noselect "./sample.txt"))
(context "basic-tests"
(tag basic-tests)
- (specify "loc-changes-goto-line error conditions"
- (assert-raises error (loc-changes-goto-line "foo"))
- (assert-raises error (loc-changes-goto-line 0))
- (assert-raises error (loc-changes-goto-line 10000)))
- (specify "loc-changes-goto-line"
- (save-excursion
- (loc-changes-goto-line 5)
- (assert-equal 5 (line-number-at-pos (point)))))
+ (specify "loc-changes-goto-line error conditions"
+ (assert-raises error (loc-changes-goto-line "foo"))
+ (message "buffer %s" (current-buffer))
+ (assert-raises error (loc-changes-goto-line 0))
+ (assert-raises error (loc-changes-goto-line 10000)))
+ (specify "loc-changes-goto-line"
+ (save-excursion
+ (set-buffer sample-buffer)
+ (loc-changes-goto-line 5)
+ (assert-equal 5 (line-number-at-pos (point)))))
+ (specify "loc-changes-clear-buffer null"
+ (loc-changes-clear-buffer)
+ (assert-equal '() loc-changes-alist))
-)
+ (specify "loc-changes-add-and-goto - update"
+ (save-excursion
+ (set-buffer sample-buffer)
+ (loc-changes-add-and-goto 10)
+ (assert-equal 10 (line-number-at-pos)
+ "point should be at line 10")
+ (assert-t (assq 10 loc-changes-alist)
+ "Should find 10 in loc-changes-alist")
+ (assert-t (markerp (cdr (assq 10 loc-changes-alist)))
+ "10 in loc-changes-alist should be a marker"))
+ )
+
+ (specify "loc-changes-goto - update"
+ (save-excursion
+ (set-buffer sample-buffer)
+ (loc-changes-goto 11)
+ (assert-equal 11 (line-number-at-pos)
+ "point should be at line 11")
+ (assert-t (assq 11 loc-changes-alist)
+ "Should find 11 in loc-changes-alist")
+ (assert-t (markerp (cdr (assq 11 loc-changes-alist)))
+ "11 in loc-changes-alist should be a marker"))
+ )
+
+ (specify "loc-changes-goto - no update"
+ (save-excursion
+ (set-buffer sample-buffer)
+ (loc-changes-goto 12 nil 't)
+ (assert-equal 12 (line-number-at-pos)
+ "point should be at line 12")
+ (assert-nil (assq 12 loc-changes-alist)
+ "Should not find 12 in loc-changes-alist")
+ )
+ )
+ )
(test-unit "basic-tests")