]> git.eshelyaron.com Git - emacs.git/commitdiff
(refactor-apply-edits-dtrt): New function.
authorEshel Yaron <me@eshelyaron.com>
Tue, 24 Jun 2025 06:27:46 +0000 (08:27 +0200)
committerEshel Yaron <me@eshelyaron.com>
Tue, 24 Jun 2025 06:27:46 +0000 (08:27 +0200)
lisp/emacs-lisp/scope.el
lisp/progmodes/cc-langs.el
lisp/progmodes/cc-mode.el
lisp/progmodes/refactor.el

index 504eddecc48ffb200f05b78ab8a53573819f04a0..980b45d0b9fc9d6b68202c1f943d44689513b618 100644 (file)
@@ -696,9 +696,6 @@ Optional argument LOCAL is a local context to extend."
         (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))
@@ -1924,8 +1921,9 @@ a (possibly empty) list of safe macros.")
   (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))
index dc9b8b7aed9d7bc0abd513d9da0e25b5c11b2c7d..668891ab6fcd71cf700999a66ca4d237df29a2d4 100644 (file)
 ;;; Code:
 
 ;; For Emacs < 22.2.
-(eval-and-compile
-  (unless (fboundp 'declare-function) (defmacro declare-function (&rest _))))
 
 (eval-when-compile
   (let ((load-path
index 494a750666052b260478f0c36efc7db6e528d3bd..ef7e99a04cb3487ef82ec521eca71d53f2e6e00d 100644 (file)
 
 ;;; Code:
 
-;; For Emacs < 22.2.
-(eval-and-compile
-  (unless (fboundp 'declare-function) (defmacro declare-function (&rest _r))))
-
 (eval-when-compile
   (let ((load-path
         (if (and (boundp 'byte-compile-dest-file)
index 6a0fe315851d9db44b0f5682a7a576bcaaf7e0e8..93c0a1c9d989077a5e5c846a214003d6b024fe7a 100644 (file)
@@ -32,7 +32,7 @@
   "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
@@ -44,6 +44,7 @@ at BEG afterwards.  If this function applies a replacement, it should
 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")))
@@ -283,6 +284,23 @@ argument is the token corresponding to that text replacement.")
       (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*")
@@ -302,7 +320,7 @@ argument is the token corresponding to that text replacement.")
                 (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)
@@ -386,6 +404,12 @@ argument is the token corresponding to that text replacement.")
           (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.