diff options
| author | Stefan Monnier | 2013-01-08 15:15:15 -0500 |
|---|---|---|
| committer | Stefan Monnier | 2013-01-08 15:15:15 -0500 |
| commit | a464813702e6c0af49d148ef3bc77e3727e148a1 (patch) | |
| tree | 21971701c3c621f4d9f1787c1655a8506b9fbddc | |
| parent | 3bace969f386056cedeaba7ac3661167d6d60190 (diff) | |
| download | emacs-a464813702e6c0af49d148ef3bc77e3727e148a1.tar.gz emacs-a464813702e6c0af49d148ef3bc77e3727e148a1.zip | |
* lisp/simple.el: Use lexical-binding.
(primitive-undo): Use pcase.
(minibuffer-history-isearch-push-state): Use a closure.
| -rw-r--r-- | lisp/ChangeLog | 6 | ||||
| -rw-r--r-- | lisp/simple.el | 189 |
2 files changed, 89 insertions, 106 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 72390d1ff67..58dec6e41ec 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,3 +1,9 @@ | |||
| 1 | 2013-01-08 Stefan Monnier <monnier@iro.umontreal.ca> | ||
| 2 | |||
| 3 | * simple.el: Use lexical-binding. | ||
| 4 | (primitive-undo): Use pcase. | ||
| 5 | (minibuffer-history-isearch-push-state): Use a closure. | ||
| 6 | |||
| 1 | 2013-01-08 Aaron S. Hawley <aaron.s.hawley@gmail.com> | 7 | 2013-01-08 Aaron S. Hawley <aaron.s.hawley@gmail.com> |
| 2 | 8 | ||
| 3 | * simple.el (primitive-undo): Move from undo.c. | 9 | * simple.el (primitive-undo): Move from undo.c. |
diff --git a/lisp/simple.el b/lisp/simple.el index 86c71cd2130..d06a04aa5dc 100644 --- a/lisp/simple.el +++ b/lisp/simple.el | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | ;;; simple.el --- basic editing commands for Emacs | 1 | ;;; simple.el --- basic editing commands for Emacs -*- lexical-binding: t -*- |
| 2 | 2 | ||
| 3 | ;; Copyright (C) 1985-1987, 1993-2013 Free Software Foundation, Inc. | 3 | ;; Copyright (C) 1985-1987, 1993-2013 Free Software Foundation, Inc. |
| 4 | 4 | ||
| @@ -752,7 +752,7 @@ If N is negative, delete newlines as well, leaving -N spaces." | |||
| 752 | (n (abs n))) | 752 | (n (abs n))) |
| 753 | (skip-chars-backward skip-characters) | 753 | (skip-chars-backward skip-characters) |
| 754 | (constrain-to-field nil orig-pos) | 754 | (constrain-to-field nil orig-pos) |
| 755 | (dotimes (i n) | 755 | (dotimes (_ n) |
| 756 | (if (= (following-char) ?\s) | 756 | (if (= (following-char) ?\s) |
| 757 | (forward-char 1) | 757 | (forward-char 1) |
| 758 | (insert ?\s))) | 758 | (insert ?\s))) |
| @@ -1813,8 +1813,9 @@ or to the last history element for a backward search." | |||
| 1813 | "Save a function restoring the state of minibuffer history search. | 1813 | "Save a function restoring the state of minibuffer history search. |
| 1814 | Save `minibuffer-history-position' to the additional state parameter | 1814 | Save `minibuffer-history-position' to the additional state parameter |
| 1815 | in the search status stack." | 1815 | in the search status stack." |
| 1816 | `(lambda (cmd) | 1816 | (let ((pos minibuffer-history-position)) |
| 1817 | (minibuffer-history-isearch-pop-state cmd ,minibuffer-history-position))) | 1817 | (lambda (cmd) |
| 1818 | (minibuffer-history-isearch-pop-state cmd pos)))) | ||
| 1818 | 1819 | ||
| 1819 | (defun minibuffer-history-isearch-pop-state (_cmd hist-pos) | 1820 | (defun minibuffer-history-isearch-pop-state (_cmd hist-pos) |
| 1820 | "Restore the minibuffer history search state. | 1821 | "Restore the minibuffer history search state. |
| @@ -2001,109 +2002,85 @@ Return what remains of the list." | |||
| 2001 | (did-apply nil) | 2002 | (did-apply nil) |
| 2002 | (next nil)) | 2003 | (next nil)) |
| 2003 | (while (> arg 0) | 2004 | (while (> arg 0) |
| 2004 | (while (and (consp list) | 2005 | (while (setq next (pop list)) ;Exit inner loop at undo boundary. |
| 2005 | (progn | ||
| 2006 | (setq next (car list)) | ||
| 2007 | (setq list (cdr list)) | ||
| 2008 | ;; Exit inner loop at undo boundary. | ||
| 2009 | (not (null next)))) | ||
| 2010 | ;; Handle an integer by setting point to that value. | 2006 | ;; Handle an integer by setting point to that value. |
| 2011 | (cond | 2007 | (pcase next |
| 2012 | ((integerp next) (goto-char next)) | 2008 | ((pred integerp) (goto-char next)) |
| 2013 | ((consp next) | 2009 | ;; Element (t . TIME) records previous modtime. |
| 2014 | (let ((car (car next)) | 2010 | ;; Preserve any flag of NONEXISTENT_MODTIME_NSECS or |
| 2015 | (cdr (cdr next))) | 2011 | ;; UNKNOWN_MODTIME_NSECS. |
| 2016 | (cond | 2012 | (`(t . ,time) |
| 2017 | ;; Element (t . TIME) records previous modtime. | 2013 | ;; If this records an obsolete save |
| 2018 | ;; Preserve any flag of NONEXISTENT_MODTIME_NSECS or | 2014 | ;; (not matching the actual disk file) |
| 2019 | ;; UNKNOWN_MODTIME_NSECS. | 2015 | ;; then don't mark unmodified. |
| 2020 | ((eq t car) | 2016 | (when (or (equal time (visited-file-modtime)) |
| 2021 | ;; If this records an obsolete save | 2017 | (and (consp time) |
| 2022 | ;; (not matching the actual disk file) | 2018 | (equal (list (car time) (cdr time)) |
| 2023 | ;; then don't mark unmodified. | 2019 | (visited-file-modtime)))) |
| 2024 | (when (or (equal cdr (visited-file-modtime)) | 2020 | (when (fboundp 'unlock-buffer) |
| 2025 | (and (consp cdr) | 2021 | (unlock-buffer)) |
| 2026 | (equal (list (car cdr) (cdr cdr)) | 2022 | (set-buffer-modified-p nil))) |
| 2027 | (visited-file-modtime)))) | 2023 | ;; Element (nil PROP VAL BEG . END) is property change. |
| 2028 | (when (fboundp 'unlock-buffer) | 2024 | (`(nil . ,(or `(,prop ,val ,beg . ,end) pcase--dontcare)) |
| 2029 | (unlock-buffer)) | 2025 | (when (or (> (point-min) beg) (< (point-max) end)) |
| 2030 | (set-buffer-modified-p nil))) | 2026 | (error "Changes to be undone are outside visible portion of buffer")) |
| 2031 | ;; Element (nil PROP VAL BEG . END) is property change. | 2027 | (put-text-property beg end prop val)) |
| 2032 | ((eq nil car) | 2028 | ;; Element (BEG . END) means range was inserted. |
| 2033 | (let ((beg (nth 2 cdr)) | 2029 | (`(,(and beg (pred integerp)) . ,(and end (pred integerp))) |
| 2034 | (end (nthcdr 3 cdr)) | 2030 | ;; (and `(,beg . ,end) `(,(pred integerp) . ,(pred integerp))) |
| 2035 | (prop (car cdr)) | 2031 | ;; Ideally: `(,(pred integerp beg) . ,(pred integerp end)) |
| 2036 | (val (cadr cdr))) | 2032 | (when (or (> (point-min) beg) (< (point-max) end)) |
| 2037 | (when (or (> (point-min) beg) | 2033 | (error "Changes to be undone are outside visible portion of buffer")) |
| 2038 | (< (point-max) end)) | 2034 | ;; Set point first thing, so that undoing this undo |
| 2039 | (error "Changes to be undone are outside visible portion of buffer")) | 2035 | ;; does not send point back to where it is now. |
| 2040 | (put-text-property beg end prop val))) | 2036 | (goto-char beg) |
| 2041 | ((and (integerp car) (integerp cdr)) | 2037 | (delete-region beg end)) |
| 2042 | ;; Element (BEG . END) means range was inserted. | 2038 | ;; Element (apply FUN . ARGS) means call FUN to undo. |
| 2043 | (when (or (< car (point-min)) | 2039 | (`(apply . ,fun-args) |
| 2044 | (> cdr (point-max))) | 2040 | (let ((currbuff (current-buffer))) |
| 2045 | (error "Changes to be undone are outside visible portion of buffer")) | 2041 | (if (integerp (car fun-args)) |
| 2046 | ;; Set point first thing, so that undoing this undo | 2042 | ;; Long format: (apply DELTA START END FUN . ARGS). |
| 2047 | ;; does not send point back to where it is now. | 2043 | (pcase-let* ((`(,delta ,start ,end ,fun . ,args) fun-args) |
| 2048 | (goto-char car) | 2044 | (start-mark (copy-marker start nil)) |
| 2049 | (delete-region car cdr)) | 2045 | (end-mark (copy-marker end t))) |
| 2050 | ((eq car 'apply) | 2046 | (when (or (> (point-min) start) (< (point-max) end)) |
| 2051 | ;; Element (apply FUN . ARGS) means call FUN to undo. | 2047 | (error "Changes to be undone are outside visible portion of buffer")) |
| 2052 | (let ((currbuff (current-buffer)) | 2048 | (apply fun args) ;; Use `save-current-buffer'? |
| 2053 | (car (car cdr)) | 2049 | ;; Check that the function did what the entry |
| 2054 | (cdr (cdr cdr))) | 2050 | ;; said it would do. |
| 2055 | (if (integerp car) | 2051 | (unless (and (= start start-mark) |
| 2056 | ;; Long format: (apply DELTA START END FUN . ARGS). | 2052 | (= (+ delta end) end-mark)) |
| 2057 | (let* ((delta car) | 2053 | (error "Changes to be undone by function different than announced")) |
| 2058 | (start (car cdr)) | 2054 | (set-marker start-mark nil) |
| 2059 | (end (cadr cdr)) | 2055 | (set-marker end-mark nil)) |
| 2060 | (start-mark (copy-marker start nil)) | 2056 | (apply fun-args)) |
| 2061 | (end-mark (copy-marker end t)) | 2057 | (unless (eq currbuff (current-buffer)) |
| 2062 | (cdr (cddr cdr)) | 2058 | (error "Undo function switched buffer")) |
| 2063 | (fun (car cdr)) | 2059 | (setq did-apply t))) |
| 2064 | (args (cdr cdr))) | 2060 | ;; Element (STRING . POS) means STRING was deleted. |
| 2065 | (apply fun args) ;; Use `save-current-buffer'? | 2061 | (`(,(and string (pred stringp)) . ,(and pos (pred integerp))) |
| 2066 | ;; Check that the function did what the entry | 2062 | (when (let ((apos (abs pos))) |
| 2067 | ;; said it would do. | 2063 | (or (< apos (point-min)) (> apos (point-max)))) |
| 2068 | (unless (and (eq start | 2064 | (error "Changes to be undone are outside visible portion of buffer")) |
| 2069 | (marker-position start-mark)) | 2065 | (if (< pos 0) |
| 2070 | (eq (+ delta end) | 2066 | (progn |
| 2071 | (marker-position end-mark))) | 2067 | (goto-char (- pos)) |
| 2072 | (error "Changes to be undone by function different than announced")) | 2068 | (insert string)) |
| 2073 | (set-marker start-mark nil) | 2069 | (goto-char pos) |
| 2074 | (set-marker end-mark nil)) | 2070 | ;; Now that we record marker adjustments |
| 2075 | (apply car cdr)) | 2071 | ;; (caused by deletion) for undo, |
| 2076 | (unless (eq currbuff (current-buffer)) | 2072 | ;; we should always insert after markers, |
| 2077 | (error "Undo function switched buffer")) | 2073 | ;; so that undoing the marker adjustments |
| 2078 | (setq did-apply t))) | 2074 | ;; put the markers back in the right place. |
| 2079 | ((and (stringp car) (integerp cdr)) | 2075 | (insert string) |
| 2080 | ;; Element (STRING . POS) means STRING was deleted. | 2076 | (goto-char pos))) |
| 2081 | (let ((membuf car) | 2077 | ;; (MARKER . OFFSET) means a marker MARKER was adjusted by OFFSET. |
| 2082 | (pos cdr)) | 2078 | (`(,(and marker (pred markerp)) . ,(and offset (pred integerp))) |
| 2083 | (when (or (< (abs pos) (point-min)) | 2079 | (when (marker-buffer marker) |
| 2084 | (> (abs pos) (point-max))) | 2080 | (set-marker marker |
| 2085 | (error "Changes to be undone are outside visible portion of buffer")) | 2081 | (- marker offset) |
| 2086 | (if (< pos 0) | 2082 | (marker-buffer marker)))) |
| 2087 | (progn | 2083 | (_ (error "Unrecognized entry in undo list %S" next)))) |
| 2088 | (goto-char (- pos)) | ||
| 2089 | (insert membuf)) | ||
| 2090 | (goto-char pos) | ||
| 2091 | ;; Now that we record marker adjustments | ||
| 2092 | ;; (caused by deletion) for undo, | ||
| 2093 | ;; we should always insert after markers, | ||
| 2094 | ;; so that undoing the marker adjustments | ||
| 2095 | ;; put the markers back in the right place. | ||
| 2096 | (insert membuf) | ||
| 2097 | (goto-char pos)))) | ||
| 2098 | ((and (markerp car) (integerp cdr)) | ||
| 2099 | ;; (MARKER . INTEGER) means a marker MARKER | ||
| 2100 | ;; was adjusted by INTEGER. | ||
| 2101 | (when (marker-buffer car) | ||
| 2102 | (set-marker car | ||
| 2103 | (- (marker-position car) cdr) | ||
| 2104 | (marker-buffer car)))) | ||
| 2105 | (t (error "Unrecognized entry in undo list %S" next))))) | ||
| 2106 | (t (error "Unrecognized entry in undo list %S" next)))) | ||
| 2107 | (setq arg (1- arg))) | 2084 | (setq arg (1- arg))) |
| 2108 | ;; Make sure an apply entry produces at least one undo entry, | 2085 | ;; Make sure an apply entry produces at least one undo entry, |
| 2109 | ;; so the test in `undo' for continuing an undo series | 2086 | ;; so the test in `undo' for continuing an undo series |