From b549bd6cc9dd82ce1aeab6925d8128cf86a79932 Mon Sep 17 00:00:00 2001 From: "Jose A. Ortega Ruiz" Date: Sat, 4 Apr 2009 22:34:21 +0200 Subject: [PATCH] FUEL: More robust extract word refactoring (catching empty region case). --- misc/fuel/fuel-refactor.el | 55 +++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/misc/fuel/fuel-refactor.el b/misc/fuel/fuel-refactor.el index 942d439466..a410bb5047 100644 --- a/misc/fuel/fuel-refactor.el +++ b/misc/fuel/fuel-refactor.el @@ -36,7 +36,7 @@ (let ((name (match-string-no-properties 2)) (body (match-string-no-properties 4)) (end (match-end 0))) - (list (split-string body nil t) name pos end))))) + (list (split-string (or body "") nil t) name pos end))))) (defun fuel-refactor--find (code to) (let ((candidate) (result)) @@ -88,7 +88,7 @@ (defun fuel-refactor--insert-word (word stack-effect code) (let ((start (goto-char (fuel-refactor--insertion-point)))) (open-line 1) - (insert ": " word " " stack-effect "\n" code " ;\n") + (insert ": " word " " stack-effect "\n" (or code " ") " ;\n") (indent-region start (point)) (move-overlay fuel-stack--overlay start (point)))) @@ -103,39 +103,46 @@ (delete-overlay fuel-stack--overlay))) (defun fuel-refactor--extract (begin end) - (unless (< begin end) (error "No proper region to extract")) - (let* ((code (buffer-substring begin end)) - (existing (fuel-refactor--reuse-existing code)) - (code-str (or existing (fuel--region-to-string begin end))) + (let* ((rp (< begin end)) + (code (and rp (buffer-substring begin end))) + (existing (and code (fuel-refactor--reuse-existing code))) + (code-str (and code (or existing (fuel--region-to-string begin end)))) (word (or (car existing) (read-string "New word name: "))) (stack-effect (or existing - (fuel-stack--infer-effect code-str) + (and code-str (fuel-stack--infer-effect code-str)) (read-string "Stack effect: ")))) - (goto-char begin) - (delete-region begin end) - (insert word) - (indent-region begin (point)) + (when rp + (goto-char begin) + (delete-region begin end) + (insert word) + (indent-region begin (point))) (save-excursion (let ((start (or (cadr existing) (point)))) (unless existing (fuel-refactor--insert-word word stack-effect code)) - (fuel-refactor--extract-other start - (or (car (cddr existing)) (point)) - code))))) + (if rp + (fuel-refactor--extract-other start + (or (car (cddr existing)) (point)) + code) + (unwind-protect + (sit-for fuel-stack-highlight-period) + (delete-overlay fuel-stack--overlay))))))) (defun fuel-refactor-extract-region (begin end) "Extracts current region as a separate word." (interactive "r") - (let ((begin (save-excursion - (goto-char begin) - (when (zerop (skip-syntax-backward "w")) - (skip-syntax-forward "-")) - (point))) - (end (save-excursion - (goto-char end) - (skip-syntax-forward "w") - (point)))) - (fuel-refactor--extract begin end))) + (if (= begin end) + (fuel-refactor--extract begin end) + (let ((begin (save-excursion + (goto-char begin) + (when (zerop (skip-syntax-backward "w")) + (skip-syntax-forward "-")) + (point))) + (end (save-excursion + (goto-char end) + (skip-syntax-forward "w") + (point)))) + (fuel-refactor--extract begin end)))) (defun fuel-refactor-extract-sexp () "Extracts current innermost sexp (up to point) as a separate