(scope-1 local arg)))))
((consp arg) (scope-1 local arg))))
-(defun scope-declare-function (local fn _file arglist _fileonly)
- (scope-defun local fn (when (listp arglist) arglist) nil))
-
(defun scope-loop-for-and (local rest)
(if (eq (scope-sym-bare (car rest)) 'and)
(scope-loop-for local local (cadr rest) (cddr rest))
(scope-1 l expr)
(scope-cl-lambda l args body))
-(scope-define-macro-analyzer declare-function (l fn file &optional arglist fileonly)
- (scope-declare-function l fn file arglist fileonly))
+(scope-define-macro-analyzer declare-function (l &optional fn _file arglist _fileonly)
+ (scope-report-s fn 'function)
+ (scope-lambda l (and (listp arglist) arglist) nil))
(scope-define-macro-analyzer cl-block (l name &rest body)
(scope-block l name body))
"Refactor code."
:group 'programming)
-(defcustom refactor-apply-edits-function #'refactor-apply-edits-at-once
+(defcustom refactor-apply-edits-function #'refactor-apply-edits-dtrt
"Function to use for applying edits during refactoring.
`refactor-apply-edits' calls this function with one argument, a list of
run hook `refactor-replacement-functions' with the corresponding TOKEN
as an argument while BUFFER is current."
:type '(choice (const :tag "Apply edits at once" refactor-apply-edits-at-once)
+ (const :tag "Do the right thing" refactor-apply-edits-dtrt)
(const :tag "Query about each edit" refactor-query-apply-edits)
(const :tag "Display edits as diff" refactor-display-edits-as-diff)
(function :tag "Custom function")))
(dolist (token tokens)
(run-hook-with-args 'refactor-replacement-functions token)))))
+(defun refactor-diff-buffer-file-name (buf)
+ (or (and (not (buffer-modified-p buf))
+ (buffer-file-name buf))
+ (with-current-buffer buf
+ (let ((tempfile (make-temp-file "buffer-content-")))
+ (write-region nil nil tempfile nil 'nomessage)
+ tempfile))))
+
+(defun refactor-diff (old old-label new new-label out)
+ "Insert diff betweeen OLD and NEW buffers into OUT buffer.
+
+OLD-LABEL and NEW-LABEL are the labels to use for OLD and NEW,
+respectively."
+ (call-process
+ diff-command nil out nil "-U" "2" "--label" old-label "--label" new-label
+ (refactor-diff-buffer-file-name old) (refactor-diff-buffer-file-name new)))
+
(defun refactor-display-edits-as-diff (edits)
"Display EDITS as a diff."
(with-current-buffer (get-buffer-create "*Refactor Diff*")
(let ((refactor-replacement-functions
(list (lambda (token) (push token tokens)))))
(refactor--apply-replacements reps))
- (diff-no-select buf (current-buffer) nil t diff))
+ (refactor-diff buf (buffer-file-name buf) (current-buffer) "*edits*" diff))
(with-current-buffer target
(let ((start (point)))
(insert-buffer-substring diff)
(setq success t))
(funcall (if success #'accept-change-group #'cancel-change-group) change-group))))
+(defun refactor-apply-edits-dtrt (edits)
+ "If EDITS only affect current buffer, apply them at once; else show diff."
+ (if (and (eq (car (car edits)) (current-buffer)) (null (cdr edits)))
+ (refactor-apply-edits-at-once edits)
+ (refactor-display-edits-as-diff edits)))
+
;;;###autoload
(defun refactor-apply-edits (edits)
"Apply EDITS.