Use uninterned symbols in macros to avoid surprising local vars

This commit is contained in:
Magnar Sveen 2013-03-21 10:17:50 +01:00
parent 9dac934bce
commit 6e159238b2

View File

@ -41,13 +41,14 @@
(defmacro mc/add-fake-cursor-to-undo-list (&rest forms) (defmacro mc/add-fake-cursor-to-undo-list (&rest forms)
"Make sure point is in the right place when undoing" "Make sure point is in the right place when undoing"
`(let ((undo-cleaner (cons 'apply (cons 'deactivate-cursor-after-undo (list id))))) (let ((uc (make-symbol "undo-cleaner")))
(setq buffer-undo-list (cons undo-cleaner buffer-undo-list)) `(let ((,uc (cons 'apply (cons 'deactivate-cursor-after-undo (list id)))))
,@forms (setq buffer-undo-list (cons ,uc buffer-undo-list))
(if (eq undo-cleaner (car buffer-undo-list)) ;; if nothing has been added to the undo-list ,@forms
(setq buffer-undo-list (cdr buffer-undo-list)) ;; then pop the cleaner right off again (if (eq ,uc (car buffer-undo-list)) ;; if nothing has been added to the undo-list
(setq buffer-undo-list ;; otherwise add a function to activate this cursor (setq buffer-undo-list (cdr buffer-undo-list)) ;; then pop the cleaner right off again
(cons (cons 'apply (cons 'activate-cursor-for-undo (list id))) buffer-undo-list))))) (setq buffer-undo-list ;; otherwise add a function to activate this cursor
(cons (cons 'apply (cons 'activate-cursor-for-undo (list id))) buffer-undo-list))))))
(defun mc/all-fake-cursors (&optional start end) (defun mc/all-fake-cursors (&optional start end)
(remove-if-not 'mc/fake-cursor-p (remove-if-not 'mc/fake-cursor-p
@ -61,35 +62,40 @@
(defmacro mc/save-excursion (&rest forms) (defmacro mc/save-excursion (&rest forms)
"Saves and restores all the state that multiple-cursors cares about." "Saves and restores all the state that multiple-cursors cares about."
`(let ((current-state (mc/store-current-state-in-overlay (let ((cs (make-symbol "current-state")))
(make-overlay (point) (point) nil nil t)))) `(let ((,cs (mc/store-current-state-in-overlay
(overlay-put current-state 'type 'original-cursor) (make-overlay (point) (point) nil nil t))))
(save-excursion ,@forms) (overlay-put ,cs 'type 'original-cursor)
(mc/pop-state-from-overlay current-state))) (save-excursion ,@forms)
(mc/pop-state-from-overlay ,cs))))
(defun mc--compare-by-overlay-start (o1 o2) (defun mc--compare-by-overlay-start (o1 o2)
(< (overlay-start o1) (overlay-start o2))) (< (overlay-start o1) (overlay-start o2)))
(defmacro mc/for-each-cursor-ordered (&rest forms) (defmacro mc/for-each-cursor-ordered (&rest forms)
"Runs the body for each cursor, fake and real, bound to the name cursor" "Runs the body for each cursor, fake and real, bound to the name cursor"
`(let ((real-cursor-id (overlay-get (mc/create-fake-cursor-at-point) 'mc-id))) (let ((rci (make-symbol "real-cursor-id")))
(mapc #'(lambda (cursor) `(let ((,rci (overlay-get (mc/create-fake-cursor-at-point) 'mc-id)))
(when (mc/fake-cursor-p cursor) (mapc #'(lambda (cursor)
,@forms)) (when (mc/fake-cursor-p cursor)
(sort (overlays-in (point-min) (point-max)) 'mc--compare-by-overlay-start)) ,@forms))
(mc/pop-state-from-overlay (mc/cursor-with-id real-cursor-id)))) (sort (overlays-in (point-min) (point-max)) 'mc--compare-by-overlay-start))
(mc/pop-state-from-overlay (mc/cursor-with-id ,rci)))))
(defmacro mc/save-window-scroll (&rest forms) (defmacro mc/save-window-scroll (&rest forms)
"Saves and restores the window scroll position" "Saves and restores the window scroll position"
`(let ((p (set-marker (make-marker) (point))) (let ((p (make-symbol "p"))
(start (set-marker (make-marker) (window-start))) (s (make-symbol "start"))
(hscroll (window-hscroll))) (h (make-symbol "hscroll")))
,@forms `(let ((,p (set-marker (make-marker) (point)))
(goto-char p) (,s (set-marker (make-marker) (window-start)))
(set-window-start nil start t) (,h (window-hscroll)))
(set-window-hscroll nil hscroll) ,@forms
(set-marker p nil) (goto-char ,p)
(set-marker start nil))) (set-window-start nil ,s t)
(set-window-hscroll nil ,h)
(set-marker ,p nil)
(set-marker ,s nil))))
(defun mc/make-cursor-overlay-at-eol (pos) (defun mc/make-cursor-overlay-at-eol (pos)
"Create overlay to look like cursor at end of line." "Create overlay to look like cursor at end of line."