Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. (defun replace-regexp-entire-buffer (pattern replacement)
  2. "Perform regular-expression replacement throughout buffer."
  3. (interactive
  4. (let ((args (query-replace-read-args "Replace" t)))
  5. (setcdr (cdr args) nil) ; remove third value returned from query---args
  6. args))
  7. (save-excursion
  8. (goto-char (point-min))
  9. (while (re-search-forward pattern nil t)
  10. (replace-match replacement))))
  11.  
  12. (defun my/query-replace (from-string to-string &optional delimited start end)
  13. "Replace some occurrences of FROM-STRING with TO-STRING. As each match is
  14. found, the user must type a character saying what to do with it. This is a
  15. modified version of the standard `query-replace' function in `replace.el',
  16. This modified version defaults to operating on the entire buffer instead of
  17. working only from POINT to the end of the buffer. For more information, see
  18. the documentation of `query-replace'"
  19. (interactive
  20. (let ((common
  21. (query-replace-read-args
  22. (concat "Query replace"
  23. (if current-prefix-arg " word" "")
  24. (if (and transient-mark-mode mark-active) " in region" ""))
  25. nil)))
  26. (list (nth 0 common) (nth 1 common) (nth 2 common)
  27. (if (and transient-mark-mode mark-active)
  28. (region-beginning)
  29. (buffer-end -1))
  30. (if (and transient-mark-mode mark-active)
  31. (region-end)
  32. (buffer-end 1)))))
  33. (perform-replace from-string to-string t nil delimited nil nil start end))
  34. ;; Replace the default key mapping
  35. (define-key esc-map "%" 'my/query-replace)
  36.  
  37. (defun my/query-replace-regexp (regexp to-string &optional delimited start end)
  38. "Replace some things after point matching REGEXP with TO-STRING. As each
  39. match is found, the user must type a character saying what to do with
  40. it. This is a modified version of the standard `query-replace-regexp'
  41. function in `replace.el', This modified version defaults to operating on the
  42. entire buffer instead of working only from POINT to the end of the
  43. buffer. For more information, see the documentation of `query-replace-regexp'"
  44. (interactive
  45. (let ((common
  46. (query-replace-read-args
  47. (concat "Query replace"
  48. (if current-prefix-arg " word" "")
  49. " regexp"
  50. (if (and transient-mark-mode mark-active) " in region" ""))
  51. t)))
  52. (list (nth 0 common) (nth 1 common) (nth 2 common)
  53. (if (and transient-mark-mode mark-active)
  54. (region-beginning)
  55. (buffer-end -1))
  56. (if (and transient-mark-mode mark-active)
  57. (region-end)
  58. (buffer-end 1)))))
  59. (perform-replace regexp to-string t t delimited nil nil start end))
  60. ;; Replace the default key mapping
  61. (define-key esc-map [?C-%] 'my/query-replace-regexp)
  62.  
  63. (defun query-replace-from-top ()
  64. (interactive)
  65. (let ((orig-point (point)))
  66. (save-excursion
  67. (goto-char (point-min))
  68. (call-interactively 'query-replace))
  69. (message "Back to old point.")
  70. (goto-char orig-point)))
  71. (bind-key* "M-%" 'query-replace-from-top)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement