branch: externals/m-buffer
commit 3702262fbc7b4eaa625f99162eba8c46fb612c74
Author: Phillip Lord <[email protected]>
Commit: Phillip Lord <[email protected]>
m-buffer was not stateless at all!
A bug in m-buffer-match meant that the m-buffer-match-* functions was
not at all stateless. The bug was caused because m-buffer-match saved
all the various states before setting the current buffer.
---
m-buffer.el | 12 ++++++------
test/m-buffer-test.el | 26 ++++++++++++++++++++++++++
2 files changed, 32 insertions(+), 6 deletions(-)
diff --git a/m-buffer.el b/m-buffer.el
index dadd538c23..769e4e30b3 100644
--- a/m-buffer.el
+++ b/m-buffer.el
@@ -11,7 +11,7 @@
;; The contents of this file are subject to the GPL License, Version 3.0.
-;; Copyright (C) 2014, Phillip Lord, Newcastle University
+;; Copyright (C) 2014, 2015, Phillip Lord, Newcastle University
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
@@ -139,11 +139,11 @@ NUMERIC -- Non-nil if we should return integers not
markers."
;; this is all the global state that exists and that we are changing.
;; #+begin_src emacs-lisp
- (save-match-data
- (save-excursion
- (save-restriction
- (with-current-buffer
- buffer
+ (with-current-buffer
+ buffer
+ (save-match-data
+ (save-excursion
+ (save-restriction
(when widen (widen))
;; #+end_src
diff --git a/test/m-buffer-test.el b/test/m-buffer-test.el
index afe828374b..029f5a1c51 100644
--- a/test/m-buffer-test.el
+++ b/test/m-buffer-test.el
@@ -421,4 +421,30 @@ should not have moved."
))))
+(ert-deftest point-stationionary-with-current ()
+ "This test addresses a bug where m-buffer did not correctly
+protect global state when the buffer being operated on was not
+current -- in this case, a match could move point.
+
+The two clauses are identical, one changing the current buffer
+and one changing a buffer which is not current."
+ (should
+ (let ((out) (out-point))
+ (with-temp-buffer
+ (insert "one\ntwo\nthree\n")
+ (setq out (current-buffer))
+ (setq out-point (point))
+ (m-buffer-match-first-line out)
+ (= (point) out-point))))
+
+ (should
+ (let ((out) (out-point))
+ (with-temp-buffer
+ (insert "one\ntwo\nthree\n")
+ (setq out (current-buffer))
+ (setq out-point (point))
+ (with-temp-buffer
+ (m-buffer-match-first-line out))
+ (= (point) out-point)))))
+
;;; m-buffer-test.el ends here