diff options
| author | Gerd Moellmann | 2000-09-30 16:00:54 +0000 |
|---|---|---|
| committer | Gerd Moellmann | 2000-09-30 16:00:54 +0000 |
| commit | e32eb3e64165e8de72accf08b5c4e34b48fb4603 (patch) | |
| tree | 5cfbf27471589c9ee1b8e5c620244f55df653116 /lisp/replace.el | |
| parent | 9c99d2061c56f13145980f4bdb8ca9dc4752f516 (diff) | |
| download | emacs-e32eb3e64165e8de72accf08b5c4e34b48fb4603.tar.gz emacs-e32eb3e64165e8de72accf08b5c4e34b48fb4603.zip | |
(keep-lines-read-args): New function.
(keep-lines, flush-lines, how-many): Use keep-lines-read-args to
read arguments interactively. Add parameters RSTART and REND.
Operate on the active region in Transient Mark mode.
Diffstat (limited to 'lisp/replace.el')
| -rw-r--r-- | lisp/replace.el | 110 |
1 files changed, 80 insertions, 30 deletions
diff --git a/lisp/replace.el b/lisp/replace.el index 66636452fee..8645e81420b 100644 --- a/lisp/replace.el +++ b/lisp/replace.el | |||
| @@ -279,83 +279,133 @@ What you probably want is a loop like this: | |||
| 279 | which will run faster and will not set the mark or print anything." | 279 | which will run faster and will not set the mark or print anything." |
| 280 | (interactive (query-replace-read-args "Replace regexp" t)) | 280 | (interactive (query-replace-read-args "Replace regexp" t)) |
| 281 | (perform-replace regexp to-string start end nil t delimited)) | 281 | (perform-replace regexp to-string start end nil t delimited)) |
| 282 | |||
| 282 | 283 | ||
| 283 | (defvar regexp-history nil | 284 | (defvar regexp-history nil |
| 284 | "History list for some commands that read regular expressions.") | 285 | "History list for some commands that read regular expressions.") |
| 285 | 286 | ||
| 287 | |||
| 286 | (defalias 'delete-non-matching-lines 'keep-lines) | 288 | (defalias 'delete-non-matching-lines 'keep-lines) |
| 287 | (defun keep-lines (regexp) | 289 | (defalias 'delete-matching-lines 'flush-lines) |
| 290 | (defalias 'count-matches 'how-many) | ||
| 291 | |||
| 292 | |||
| 293 | (defun keep-lines-read-args (prompt) | ||
| 294 | "Read arguments for `keep-lines' and friends. | ||
| 295 | Prompt for a regexp with PROMPT. | ||
| 296 | |||
| 297 | Value is a list (REGEXP START END). | ||
| 298 | |||
| 299 | If in Transient Mark node, and the mark is active, START is the | ||
| 300 | start of the region, and end is a marker for the end of the region. | ||
| 301 | Otherwise, START is the current point, and END is `point-max-marker'." | ||
| 302 | (let ((regexp (read-from-minibuffer prompt nil nil nil | ||
| 303 | 'regexp-history nil t)) | ||
| 304 | start end) | ||
| 305 | (if (and transient-mark-mode mark-active) | ||
| 306 | (setq start (region-beginning) | ||
| 307 | end (save-excursion (goto-char (region-end)) (point-marker))) | ||
| 308 | (setq start (point) | ||
| 309 | end (point-max-marker))) | ||
| 310 | (list regexp start end))) | ||
| 311 | |||
| 312 | |||
| 313 | (defun keep-lines (regexp &optional rstart rend) | ||
| 288 | "Delete all lines except those containing matches for REGEXP. | 314 | "Delete all lines except those containing matches for REGEXP. |
| 289 | A match split across lines preserves all the lines it lies in. | 315 | A match split across lines preserves all the lines it lies in. |
| 290 | Applies to all lines after point. | 316 | Applies to all lines after point. |
| 291 | 317 | ||
| 292 | If REGEXP contains upper case characters (excluding those preceded by `\\'), | 318 | If REGEXP contains upper case characters (excluding those preceded by `\\'), |
| 293 | the matching is case-sensitive." | 319 | the matching is case-sensitive. |
| 294 | (interactive (list (read-from-minibuffer | 320 | |
| 295 | "Keep lines (containing match for regexp): " | 321 | Second and third arg RSTART and REND specify the region to operate on. |
| 296 | nil nil nil 'regexp-history nil t))) | 322 | |
| 323 | In Transient Mark mode, if the mark is active, operate on the contents | ||
| 324 | of the region. Otherwise, operate from point to the end of the buffer." | ||
| 325 | (interactive | ||
| 326 | (keep-lines-read-args "Keep lines (containing match for regexp): ")) | ||
| 327 | (if rstart | ||
| 328 | (goto-char (min rstart rend)) | ||
| 329 | (setq rstart (point) rend (point-max-marker))) | ||
| 297 | (save-excursion | 330 | (save-excursion |
| 298 | (or (bolp) (forward-line 1)) | 331 | (or (bolp) (forward-line 1)) |
| 299 | (let ((start (point)) | 332 | (let ((start (point)) |
| 300 | (case-fold-search (and case-fold-search | 333 | (case-fold-search (and case-fold-search |
| 301 | (isearch-no-upper-case-p regexp t)))) | 334 | (isearch-no-upper-case-p regexp t)))) |
| 302 | (while (not (eobp)) | 335 | (while (< (point) rend) |
| 303 | ;; Start is first char not preserved by previous match. | 336 | ;; Start is first char not preserved by previous match. |
| 304 | (if (not (re-search-forward regexp nil 'move)) | 337 | (if (not (re-search-forward regexp rend 'move)) |
| 305 | (delete-region start (point-max)) | 338 | (delete-region start rend) |
| 306 | (let ((end (save-excursion (goto-char (match-beginning 0)) | 339 | (let ((end (save-excursion (goto-char (match-beginning 0)) |
| 307 | (beginning-of-line) | 340 | (beginning-of-line) |
| 308 | (point)))) | 341 | (point)))) |
| 309 | ;; Now end is first char preserved by the new match. | 342 | ;; Now end is first char preserved by the new match. |
| 310 | (if (< start end) | 343 | (if (< start end) |
| 311 | (delete-region start end)))) | 344 | (delete-region start end)))) |
| 312 | (setq start (save-excursion (forward-line 1) | 345 | |
| 313 | (point))) | 346 | (setq start (save-excursion (forward-line 1) (point))) |
| 314 | ;; If the match was empty, avoid matching again at same place. | 347 | ;; If the match was empty, avoid matching again at same place. |
| 315 | (and (not (eobp)) (= (match-beginning 0) (match-end 0)) | 348 | (and (< (point) rend) |
| 349 | (= (match-beginning 0) (match-end 0)) | ||
| 316 | (forward-char 1)))))) | 350 | (forward-char 1)))))) |
| 317 | 351 | ||
| 318 | (defalias 'delete-matching-lines 'flush-lines) | 352 | |
| 319 | (defun flush-lines (regexp) | 353 | (defun flush-lines (regexp &optional rstart rend) |
| 320 | "Delete lines containing matches for REGEXP. | 354 | "Delete lines containing matches for REGEXP. |
| 321 | If a match is split across lines, all the lines it lies in are deleted. | 355 | If a match is split across lines, all the lines it lies in are deleted. |
| 322 | Applies to lines after point. | 356 | Applies to lines after point. |
| 323 | 357 | ||
| 324 | If REGEXP contains upper case characters (excluding those preceded by `\\'), | 358 | If REGEXP contains upper case characters (excluding those preceded by `\\'), |
| 325 | the matching is case-sensitive." | 359 | the matching is case-sensitive. |
| 326 | (interactive (list (read-from-minibuffer | 360 | |
| 327 | "Flush lines (containing match for regexp): " | 361 | Second and third arg RSTART and REND specify the region to operate on. |
| 328 | nil nil nil 'regexp-history nil t))) | 362 | |
| 363 | In Transient Mark mode, if the mark is active, operate on the contents | ||
| 364 | of the region. Otherwise, operate from point to the end of the buffer." | ||
| 365 | (interactive | ||
| 366 | (keep-lines-read-args "Flush lines (containing match for regexp): ")) | ||
| 367 | (if rstart | ||
| 368 | (goto-char (min rstart rend)) | ||
| 369 | (setq rstart (point) rend (point-max-marker))) | ||
| 329 | (let ((case-fold-search (and case-fold-search | 370 | (let ((case-fold-search (and case-fold-search |
| 330 | (isearch-no-upper-case-p regexp t)))) | 371 | (isearch-no-upper-case-p regexp t)))) |
| 331 | (save-excursion | 372 | (save-excursion |
| 332 | (while (and (not (eobp)) | 373 | (while (and (< (point) rend) |
| 333 | (re-search-forward regexp nil t)) | 374 | (re-search-forward regexp rend t)) |
| 334 | (delete-region (save-excursion (goto-char (match-beginning 0)) | 375 | (delete-region (save-excursion (goto-char (match-beginning 0)) |
| 335 | (beginning-of-line) | 376 | (beginning-of-line) |
| 336 | (point)) | 377 | (point)) |
| 337 | (progn (forward-line 1) (point))))))) | 378 | (progn (forward-line 1) (point))))))) |
| 338 | 379 | ||
| 339 | (defalias 'count-matches 'how-many) | 380 | |
| 340 | (defun how-many (regexp) | 381 | (defun how-many (regexp &optional rstart rend) |
| 341 | "Print number of matches for REGEXP following point. | 382 | "Print number of matches for REGEXP following point. |
| 342 | 383 | ||
| 343 | If REGEXP contains upper case characters (excluding those preceded by `\\'), | 384 | If REGEXP contains upper case characters (excluding those preceded by `\\'), |
| 344 | the matching is case-sensitive." | 385 | the matching is case-sensitive. |
| 345 | (interactive (list (read-from-minibuffer | 386 | |
| 346 | "How many matches for (regexp): " | 387 | Second and third arg RSTART and REND specify the region to operate on. |
| 347 | nil nil nil 'regexp-history nil t))) | 388 | |
| 348 | (let ((count 0) opoint | 389 | In Transient Mark mode, if the mark is active, operate on the contents |
| 349 | (case-fold-search (and case-fold-search | 390 | of the region. Otherwise, operate from point to the end of the buffer." |
| 350 | (isearch-no-upper-case-p regexp t)))) | 391 | (interactive |
| 392 | (keep-lines-read-args "How many matches for (regexp): ")) | ||
| 393 | (if rstart | ||
| 394 | (goto-char (min rstart rend)) | ||
| 395 | (setq rstart (point) rend (point-max-marker))) | ||
| 396 | (let ((count 0) | ||
| 397 | opoint | ||
| 398 | (case-fold-search (and case-fold-search | ||
| 399 | (isearch-no-upper-case-p regexp t)))) | ||
| 351 | (save-excursion | 400 | (save-excursion |
| 352 | (while (and (not (eobp)) | 401 | (while (and (< (point) rend) |
| 353 | (progn (setq opoint (point)) | 402 | (progn (setq opoint (point)) |
| 354 | (re-search-forward regexp nil t))) | 403 | (re-search-forward regexp rend t))) |
| 355 | (if (= opoint (point)) | 404 | (if (= opoint (point)) |
| 356 | (forward-char 1) | 405 | (forward-char 1) |
| 357 | (setq count (1+ count)))) | 406 | (setq count (1+ count)))) |
| 358 | (message "%d occurrences" count)))) | 407 | (message "%d occurrences" count)))) |
| 408 | |||
| 359 | 409 | ||
| 360 | (defvar occur-mode-map ()) | 410 | (defvar occur-mode-map ()) |
| 361 | (if occur-mode-map | 411 | (if occur-mode-map |