diff options
| author | Gerd Moellmann | 2000-09-19 15:36:55 +0000 |
|---|---|---|
| committer | Gerd Moellmann | 2000-09-19 15:36:55 +0000 |
| commit | f7e7d5a26bac0f0117a76aadba89df4d20393202 (patch) | |
| tree | 8dc96c32ac6f960470f03bc512a89381c964a8ae | |
| parent | 001f93f3eb48bbf9c8ee75445fdc623ede8ca441 (diff) | |
| download | emacs-f7e7d5a26bac0f0117a76aadba89df4d20393202.tar.gz emacs-f7e7d5a26bac0f0117a76aadba89df4d20393202.zip | |
(sh-search-word): Rewritten for
speed.
| -rw-r--r-- | lisp/progmodes/sh-script.el | 106 |
1 files changed, 77 insertions, 29 deletions
diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el index 11922a117ea..5f111fa3b9a 100644 --- a/lisp/progmodes/sh-script.el +++ b/lisp/progmodes/sh-script.el | |||
| @@ -3305,44 +3305,92 @@ Argument ARG if non-nil disables this test." | |||
| 3305 | (goto-char next-change))) | 3305 | (goto-char next-change))) |
| 3306 | )) | 3306 | )) |
| 3307 | 3307 | ||
| 3308 | ;; (defun sh-search-word (word &optional limit) | ||
| 3309 | ;; "Search forward for regexp WORD occurring as a word not in string nor comment. | ||
| 3310 | ;; If found, returns non nil with the match available in \(match-string 2\). | ||
| 3311 | ;; Yes 2, not 1, since we build a regexp to guard against false matches | ||
| 3312 | ;; such as matching \"a-case\" when we are searching for \"case\". | ||
| 3313 | ;; If not found, it returns nil. | ||
| 3314 | ;; The search maybe limited by optional argument LIMIT." | ||
| 3315 | ;; (interactive "sSearch for: ") | ||
| 3316 | ;; (let ((found nil) | ||
| 3317 | ;; ;; Cannot use \\b here since it matches "-" and "_" | ||
| 3318 | ;; (regexp (sh-mkword-regexp word)) | ||
| 3319 | ;; start state where) | ||
| 3320 | ;; (setq start (point)) | ||
| 3321 | ;; (while (and (setq start (point)) | ||
| 3322 | ;; (not found) | ||
| 3323 | ;; (re-search-forward regexp limit t)) | ||
| 3324 | ;; ;; Found str; check it is not in a comment or string. | ||
| 3325 | ;; (setq state | ||
| 3326 | ;; ;; Stop on comment: | ||
| 3327 | ;; (parse-partial-sexp start (point) nil nil nil 'syntax_table)) | ||
| 3328 | ;; (if (setq where (nth 8 state)) | ||
| 3329 | ;; ;; in comment or string | ||
| 3330 | ;; (if (= where -1) | ||
| 3331 | ;; (setq found (point)) | ||
| 3332 | ;; (if (eq (char-after where) ?#) | ||
| 3333 | ;; (end-of-line) | ||
| 3334 | ;; (goto-char where) | ||
| 3335 | ;; (unless (sh-safe-forward-sexp) | ||
| 3336 | ;; ;; If the above fails we must either give up or | ||
| 3337 | ;; ;; move forward and try again. | ||
| 3338 | ;; (forward-line 1)) | ||
| 3339 | ;; )) | ||
| 3340 | ;; ;; not in comment or string, so accept it | ||
| 3341 | ;; (setq found (point)) | ||
| 3342 | ;; )) | ||
| 3343 | ;; found | ||
| 3344 | ;; )) | ||
| 3345 | |||
| 3308 | (defun sh-search-word (word &optional limit) | 3346 | (defun sh-search-word (word &optional limit) |
| 3309 | "Search forward for regexp WORD occurring as a word not in string nor comment. | 3347 | "Search forward for regexp WORD occurring as a word not in string nor comment. |
| 3310 | If found, returns non nil with the match available in \(match-string 2\). | 3348 | If found, returns non-nil, with the match available in \(match-string 2\). |
| 3311 | Yes 2, not 1, since we build a regexp to guard against false matches | 3349 | Yes, that is 2, not 1. |
| 3312 | such as matching \"a-case\" when we are searching for \"case\". | ||
| 3313 | If not found, it returns nil. | 3350 | If not found, it returns nil. |
| 3314 | The search maybe limited by optional argument LIMIT." | 3351 | The search may be limited by optional argument LIMIT." |
| 3315 | (interactive "sSearch for: ") | 3352 | (interactive "sSearch for: ") |
| 3316 | (let ((found nil) | 3353 | (let ((found nil) |
| 3317 | ;; Cannot use \\b here since it matches "-" and "_" | 3354 | start state where match) |
| 3318 | (regexp (sh-mkword-regexp word)) | ||
| 3319 | start state where) | ||
| 3320 | (setq start (point)) | 3355 | (setq start (point)) |
| 3321 | (while (and (setq start (point)) | 3356 | (debug) |
| 3322 | (not found) | 3357 | (while (and (not found) |
| 3323 | (re-search-forward regexp limit t)) | 3358 | (re-search-forward word limit t)) |
| 3324 | ;; Found str; check it is not in a comment or string. | 3359 | (setq match (match-data)) |
| 3325 | (setq state | 3360 | ;; Found the word as a string; check it occurs as a word. |
| 3326 | ;; Stop on comment: | 3361 | (when (and (or (= (match-beginning 0) (point-min)) |
| 3327 | (parse-partial-sexp start (point) nil nil nil 'syntax_table)) | 3362 | (save-excursion |
| 3328 | (if (setq where (nth 8 state)) | 3363 | (goto-char (1- (match-beginning 0))) |
| 3329 | ;; in comment or string | 3364 | (looking-at "[^-a-z0-9_]"))) |
| 3330 | (if (= where -1) | 3365 | (or (= (point) (point-max)) |
| 3331 | (setq found (point)) | 3366 | (looking-at "[^-a-z0-9_]"))) |
| 3332 | (if (eq (char-after where) ?#) | 3367 | ;; Check it is not in a comment or string. |
| 3333 | (end-of-line) | 3368 | (setq state |
| 3334 | (goto-char where) | 3369 | ;; Stop on comment: |
| 3335 | (unless (sh-safe-forward-sexp) | 3370 | (parse-partial-sexp start (point) nil nil nil 'syntax_table)) |
| 3336 | ;; If the above fails we must either give up or | 3371 | (if (setq where (nth 8 state)) |
| 3337 | ;; move forward and try again. | 3372 | ;; in comment or string |
| 3338 | (forward-line 1)) | 3373 | (if (= where -1) |
| 3339 | )) | 3374 | (setq found (point)) |
| 3340 | ;; not in comment or string, so accept it | 3375 | (if (eq (char-after where) ?#) |
| 3341 | (setq found (point)) | 3376 | (end-of-line) |
| 3342 | )) | 3377 | (goto-char where) |
| 3378 | (unless (sh-safe-forward-sexp) | ||
| 3379 | ;; If the above fails we must either give up or | ||
| 3380 | ;; move forward and try again. | ||
| 3381 | (forward-line 1)))) | ||
| 3382 | ;; not in comment or string, so accept it | ||
| 3383 | (setq found (point))) | ||
| 3384 | (setq start (point)))) | ||
| 3385 | (when found | ||
| 3386 | (set-match-data match) | ||
| 3387 | (goto-char (1- (match-beginning 0))) | ||
| 3388 | (looking-at (sh-mkword-regexp word)) | ||
| 3389 | (goto-char found)) | ||
| 3343 | found | 3390 | found |
| 3344 | )) | 3391 | )) |
| 3345 | 3392 | ||
| 3393 | |||
| 3346 | (defun sh-scan-case () | 3394 | (defun sh-scan-case () |
| 3347 | "Scan a case statement for right parens belonging to case alternatives. | 3395 | "Scan a case statement for right parens belonging to case alternatives. |
| 3348 | Mark each as having syntax `sh-special-syntax'. | 3396 | Mark each as having syntax `sh-special-syntax'. |