Skip to content

Commit

Permalink
Replace all uses of looking-back
Browse files Browse the repository at this point in the history
 - it scans from the start of the buffer unless you give it a start
   position. In other words, it is hella slow in large files and in some
   suboptimal modes.

#100
  • Loading branch information
magnars committed Jun 10, 2013
1 parent 06797f2 commit d987000
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cc-mode-expansions.el
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ This function captures identifiers composed of multiple
(when (use-region-p)
(when (> (point) (mark))
(exchange-point-and-mark))
(while (looking-back "::")
(while (er/looking-back-exact "::")
(backward-char 2)
(skip-syntax-backward "_w"))
(exchange-point-and-mark)
Expand Down
10 changes: 5 additions & 5 deletions clojure-mode-expansions.el
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@
(interactive)
(let ((word-regexp "\\(\\sw\\|-\\)"))
(when (or (looking-at word-regexp)
(looking-back word-regexp))
(er/looking-back-on-line word-regexp))
(while (looking-at word-regexp)
(forward-char))
(set-mark (point))
(while (looking-back word-regexp)
(while (er/looking-back-on-line word-regexp)
(backward-char)))))

(defun er/mark-clj-set-literal ()
"Mark clj-set-literal presumes that point is outside the brackets.
If point is inside the brackets, those will be marked first anyway."
(interactive)
(when (or (looking-at "#{")
(looking-back "#"))
(er/looking-back-exact "#"))
(forward-char 1)
(search-backward "#")
(set-mark (point))
Expand All @@ -65,7 +65,7 @@ If point is inside the brackets, those will be marked first anyway."
If point is inside the string, the quotes will be marked first anyway."
(interactive)
(when (or (looking-at "#\"")
(looking-back "#"))
(er/looking-back-exact "#"))
(forward-char 1)
(search-backward "#")
(set-mark (point))
Expand All @@ -79,7 +79,7 @@ If point is inside the string, the quotes will be marked first anyway."
If point is inside the parens, they will be marked first anyway."
(interactive)
(when (or (looking-at "#(")
(looking-back "#"))
(er/looking-back-exact "#"))
(forward-char)
(search-backward "#")
(set-mark (point))
Expand Down
20 changes: 10 additions & 10 deletions er-basic-expansions.el
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@
(interactive)
(let ((word-regexp "\\sw"))
(when (or (looking-at word-regexp)
(looking-back word-regexp))
(er/looking-back-on-line word-regexp))
(skip-syntax-forward "w")
(set-mark (point))
(while (looking-back word-regexp)
(while (er/looking-back-on-line word-regexp)
(backward-char)))))

(defun er/mark-symbol ()
"Mark the entire symbol around or in front of point."
(interactive)
(let ((symbol-regexp "\\s_\\|\\sw"))
(when (or (looking-at symbol-regexp)
(looking-back symbol-regexp))
(er/looking-back-on-line symbol-regexp))
(skip-syntax-forward "_w")
(set-mark (point))
(while (looking-back symbol-regexp)
(while (er/looking-back-on-line symbol-regexp)
(backward-char)))))

(defun er/mark-symbol-with-prefix ()
Expand All @@ -55,12 +55,12 @@
(prefix-regexp "\\s'"))
(when (or (looking-at prefix-regexp)
(looking-at symbol-regexp)
(looking-back symbol-regexp))
(er/looking-back-on-line symbol-regexp))
(skip-syntax-forward "'")
(skip-syntax-forward "_w")
(set-mark (point))
(while (or (looking-back symbol-regexp)
(looking-back prefix-regexp))
(while (or (er/looking-back-on-line symbol-regexp)
(er/looking-back-on-line prefix-regexp))
(backward-char)))))

;; Mark method call
Expand All @@ -83,7 +83,7 @@ period and marks next symbol."
(interactive)
(let ((symbol-regexp "\\s_\\|\\sw\\|\\."))
(when (or (looking-at symbol-regexp)
(looking-back symbol-regexp))
(er/looking-back-on-line symbol-regexp))
(skip-syntax-backward "_w.")
(set-mark (point))
(while (looking-at symbol-regexp)
Expand Down Expand Up @@ -146,7 +146,7 @@ period and marks next symbol."
(if (er--point-inside-string-p)
(er--move-point-backward-out-of-string)
(when (and (not (use-region-p))
(looking-back "\\s\""))
(er/looking-back-on-line "\\s\""))
(backward-char)
(er--move-point-backward-out-of-string)))
(when (looking-at "\\s\"")
Expand Down Expand Up @@ -191,7 +191,7 @@ period and marks next symbol."
(defun er/mark-outside-pairs ()
"Mark pairs (as defined by the mode), including the pair chars."
(interactive)
(if (looking-back "\\s)+\\=")
(if (er/looking-back-on-line "\\s)+\\=")
(ignore-errors (backward-list 1))
(skip-chars-forward er--space-str))
(when (and (er--point-inside-pairs-p)
Expand Down
15 changes: 15 additions & 0 deletions expand-region-core.el
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,21 @@ remove the keymap depends on user input and KEEP-PRED:
(when (derived-mode-p mode)
(funcall add-fn))))))

;; Some more performant version of `looking-back'

(defun er/looking-back-on-line (regexp)
"Version of `looking-back' that only checks current line."
(looking-back regexp (line-beginning-position)))

(defun er/looking-back-exact (s)
"Version of `looking-back' that only looks for exact matches, no regexp."
(string= s (buffer-substring (- (point) (length s))
(point))))

(defun er/looking-back-max (regexp count)
"Version of `looking-back' that only check COUNT chars back."
(looking-back regexp (max 1 (- (point) count))))

(provide 'expand-region-core)

;;; expand-region-core.el ends here
2 changes: 1 addition & 1 deletion html-mode-expansions.el
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ first anyway. Does not support html-attributes with spaces
around the equal sign or unquotes attributes atm."
(interactive)
(when (or (looking-at "\\(\\s_\\|\\sw\\)*=")
(looking-back "="))
(er/looking-back-exact "="))
(search-backward " ")
(forward-char 1)
(set-mark (point))
Expand Down
6 changes: 3 additions & 3 deletions js-mode-expansions.el
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
(if (looking-at "\\s(")
(forward-list)
(forward-char)))
(when (looking-back "[\s\n]")
(when (er/looking-back-max "[\s\n]" 400)
(search-backward-regexp "[^\s\n]")
(forward-char))
(exchange-point-and-mark))
Expand All @@ -133,7 +133,7 @@ If point is inside the value, that will be marked first anyway."
(interactive)
(when (or (looking-at "\"?\\(\\s_\\|\\sw\\| \\)*\":")
(looking-at "\\(\\s_\\|\\sw\\)*:")
(looking-back ": ?"))
(er/looking-back-max ": ?" 2))
(search-backward-regexp "[{,]")
(forward-char)
(search-forward-regexp "[^\s\n]")
Expand All @@ -144,7 +144,7 @@ If point is inside the value, that will be marked first anyway."
(if (looking-at "\\s(")
(forward-list)
(forward-char)))
(when (looking-back "[\s\n]")
(when (er/looking-back-max "[\s\n]" 400)
(search-backward-regexp "[^\s\n]")
(forward-char))
(exchange-point-and-mark)))
Expand Down
2 changes: 1 addition & 1 deletion js2-mode-expansions.el
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

(defun js2-mark-parent-statement ()
(interactive)
(let* ((parent-statement (if (not (looking-back ";"))
(let* ((parent-statement (if (not (er/looking-back-exact ";"))
(js2-node-parent-stmt (js2-node-at-point))
(forward-char -1)
(js2-node-at-point)))
Expand Down
2 changes: 1 addition & 1 deletion jsp-expansions.el
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
If point is inside the brackets, they will be marked first anyway."
(interactive)
(when (or (looking-at "\\${")
(looking-back "\\$"))
(er/looking-back-exact "$"))
(forward-char 1)
(search-backward "\$")
(set-mark (point))
Expand Down
7 changes: 4 additions & 3 deletions nxml-mode-expansions.el
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
(interactive)
(cond ((looking-at "<")
(nxml-mark-token-after))
((looking-back ">")
((er/looking-back-exact ">")
(backward-char 1)
(nxml-mark-token-after))
((looking-back "<[^<>]*")
((er/looking-back-max "<[^<>]*" 1000)
(nxml-mark-token-after))))

(defun er/mark-nxml-element ()
Expand Down Expand Up @@ -70,7 +70,8 @@
(defun er/mark-nxml-attribute-string ()
"Marks an attribute string."
(interactive)
(when (looking-back "[\"']")
(when (or (er/looking-back-exact "\"")
(er/looking-back-exact "'"))
(backward-char 1))
;; Using syntax highlighting is a hack, but I can't figure out how
;; to use nxml-mode functions to do it.
Expand Down
2 changes: 1 addition & 1 deletion ruby-mode-expansions.el
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Assumes that point is at the @ - if it is inside the word, that will
be marked first anyway."
(when (looking-at "@")
(forward-char 1))
(when (looking-back "@")
(when (er/looking-back-exact "@")
(er/mark-symbol)
(forward-char -1)))

Expand Down

0 comments on commit d987000

Please sign in to comment.