Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add option to match only complete words, not inside words

  • Loading branch information...
commit da0a24701b64023f1b8096888a15c5daec648e40 1 parent f348f2d
Tassilo Horn authored October 27, 2011

Showing 1 changed file with 25 additions and 14 deletions. Show diff stats Hide diff stats

  1. 39  iedit.el
39  iedit.el
@@ -30,7 +30,7 @@
30 30
 ;; This package provides a more intuitive way of replace-string operation:
31 31
 ;;
32 32
 ;; - Select the occurrence in the buffer
33  
-;;   In Transient Mark mode, just mark a region, the content of the 
  33
+;;   In Transient Mark mode, just mark a region, the content of the
34 34
 ;;   region will be used as the occurrence. (if Transient Mark mode is disabled,
35 35
 ;;   using C-u C-x C-x or C-SPC C-SPC to activate it just for this one time).
36 36
 ;;
@@ -86,6 +86,14 @@
86 86
   :type 'boolean
87 87
   :group 'iedit)
88 88
 
  89
+(defcustom iedit-only-at-word-boundaries t
  90
+  "If no-nil, matches have to start and end at work boundaries.
  91
+  For example, when invoking iedit-mode on the \"in\" in the
  92
+  sentence \"The king in the castle...\", the \"king\" is not
  93
+  edited."
  94
+  :type 'boolean
  95
+  :group 'iedit)
  96
+
89 97
 (defcustom iedit-unmatched-lines-invisible-default nil
90 98
   "If no-nil, hide lines that do not cover any occurrences by
91 99
 default."
@@ -104,7 +112,7 @@ default."
104 112
 
105 113
 (or (assq 'iedit-mode minor-mode-alist)
106 114
     (nconc minor-mode-alist
107  
-           (list '(iedit-mode iedit-mode))))
  115
+	   (list '(iedit-mode iedit-mode))))
108 116
 
109 117
 (defvar iedit-occurrences-overlays nil
110 118
   "The occurrences slot contains a list of overlays used to
@@ -258,8 +266,8 @@ Commands:
258 266
       (deactivate-mark)
259 267
       (iedit-start occurrence))))
260 268
 
261  
-(defun iedit-start (occurrence-exp)
262  
-  "Start an iedit for the occurrence-exp in the current buffer."
  269
+(defun iedit-start (occurrence)
  270
+  "Start an iedit for the occurrence in the current buffer."
263 271
   (setq	iedit-mode " Iedit")
264 272
   (setq iedit-occurrences-overlays nil)
265 273
   (setq iedit-unmatched-lines-invisible iedit-unmatched-lines-invisible-default)
@@ -270,18 +278,21 @@ Commands:
270 278
   (add-hook 'kbd-macro-termination-hook 'iedit-done)
271 279
   ;; Find and record each occurrence's markers and add the overlay to the occurrences
272 280
   (let ((counter 0)
273  
-        (case-fold-search (not iedit-case-sensitive)))
  281
+        (case-fold-search (not iedit-case-sensitive))
  282
+	(occurrence-exp (regexp-quote occurrence)))
274 283
   (save-excursion
  284
+    (when iedit-only-at-word-boundaries
  285
+      (setq occurrence-exp (concat "\\<" occurrence-exp "\\>")))
275 286
     (goto-char (point-min))
276  
-    (while (search-forward occurrence-exp nil t)
  287
+    (while (re-search-forward occurrence-exp nil t)
277 288
       (push (iedit-make-occurrence-overlay (match-beginning 0) (match-end 0))
278 289
             iedit-occurrences-overlays)
279 290
       (setq counter (1+ counter)))      ; at less 1
280 291
       (setq iedit-occurrences-overlays (nreverse iedit-occurrences-overlays))
281 292
       (if iedit-unmatched-lines-invisible
282 293
           (iedit-hide-unmatched-lines))
283  
-      (message "%d matches for \"%s\"" 
284  
-               counter 
  294
+      (message "%d matches for \"%s\""
  295
+               counter
285 296
                (if (> (length occurrence-exp) 50)
286 297
                    (concat (substring occurrence-exp 0 50) "...")
287 298
                  occurrence-exp)))))
@@ -310,8 +321,8 @@ Commands:
310 321
 (defun iedit-done ()
311 322
   "Exit iedit mode."
312 323
   (let ((ov (car iedit-occurrences-overlays)))
313  
-    (if ov 
314  
-        (setq iedit-last-occurrence-in-history 
  324
+    (if ov
  325
+        (setq iedit-last-occurrence-in-history
315 326
               (buffer-substring (overlay-start ov) (overlay-end ov)))))
316 327
   (remove-overlays (point-min) (point-max) iedit-occurrence-overlay-name t)
317 328
   (remove-overlays (point-min) (point-max) iedit-invisible-overlay-name t)
@@ -371,13 +382,13 @@ beginning of the buffer."
371 382
     (when in-occurrence
372 383
       (setq pos  (next-single-char-property-change pos 'iedit-occurrence-overlay-name)))
373 384
     (setq pos (next-single-char-property-change pos 'iedit-occurrence-overlay-name))
374  
-    
  385
+
375 386
     (if (/= pos (point-max))
376 387
         (setq iedit-forward-success t)
377 388
       (if (and iedit-forward-success in-occurrence)
378 389
           (progn (message "This is the last occurrence.")
379 390
                  (setq iedit-forward-success nil))
380  
-        (progn 
  391
+        (progn
381 392
           (if (get-char-property (point-min) 'iedit-occurrence-overlay-name)
382 393
               (setq pos (point-min))
383 394
             (setq pos (next-single-char-property-change (point-min) 'iedit-occurrence-overlay-name)))
@@ -400,12 +411,12 @@ the buffer."
400 411
     ;; At the start of the first occurrence
401 412
     (if (or (and (eq pos (point-min))
402 413
                  (not (get-char-property (point-min) 'iedit-occurrence-overlay-name)))
403  
-            (and (eq (point) (point-min)) 
  414
+            (and (eq (point) (point-min))
404 415
                  in-occurrence))
405 416
         (if (and iedit-forward-success in-occurrence)
406 417
             (progn (message "This is the first occurrence.")
407 418
                    (setq iedit-forward-success nil))
408  
-          (progn 
  419
+          (progn
409 420
             (setq pos (previous-single-char-property-change (point-max) 'iedit-occurrence-overlay-name))
410 421
             (if (not (get-char-property (- (point-max) 1) 'iedit-occurrence-overlay-name))
411 422
                 (setq pos (previous-single-char-property-change pos 'iedit-occurrence-overlay-name)))

0 notes on commit da0a247

Please sign in to comment.
Something went wrong with that request. Please try again.