Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. (defun query-replace--split-string (string)
  2. "Split string STRING at a character with property `separator'"
  3. (let* ((length (length string))
  4. (split-pos (text-property-any 0 length 'separator t string)))
  5. (cond (split-pos
  6. (cl-assert (not (text-property-any
  7. (1+ split-pos) length 'separator t string)))
  8. (cons (substring-no-properties string 0 split-pos)
  9. (substring-no-properties string (1+ split-pos) length)))
  10. ((string-match query-replace-from-to-separator string)
  11. (cons (substring-no-properties string 0 (match-beginning 0))
  12. (substring-no-properties string (match-end 0) length)))
  13. (t (substring-no-properties string)))))
  14.  
  15. (defun query-replace-read-from (prompt regexp-flag)
  16. "Query and return the `from' argument of a query-replace operation.
  17. The return value can also be a pair (FROM . TO) indicating that the user
  18. wants to replace FROM with TO."
  19. (if query-replace-interactive
  20. (car (if regexp-flag regexp-search-ring search-ring))
  21. (let ((sep-char (replace-regexp-in-string
  22. " " "" query-replace-from-to-separator)))
  23. (when (stringp query-replace-from-to-separator)
  24. (setq query-replace-from-to-separator
  25. (propertize (if (char-displayable-p (string-to-char sep-char))
  26. query-replace-from-to-separator
  27. " -> ")
  28. 'face 'minibuffer-prompt)))
  29. (let* ((history-add-new-input nil)
  30. (separator
  31. (if (and query-replace-from-to-separator
  32. (> (string-bytes sep-char) (length sep-char)))
  33. (propertize "\0"
  34. 'display query-replace-from-to-separator
  35. 'separator t)
  36. (propertize query-replace-from-to-separator 'separator t)))
  37. (minibuffer-history
  38. (append
  39. (when separator
  40. (mapcar (lambda (from-to)
  41. (concat (query-replace-descr (car from-to))
  42. separator
  43. (query-replace-descr (cdr from-to))))
  44. query-replace-defaults))
  45. (symbol-value query-replace-from-history-variable)))
  46. (minibuffer-allow-text-properties t) ; separator uses text-properties
  47. (prompt
  48. (cond ((and query-replace-defaults separator)
  49. (format "%s (default %s): " prompt (car minibuffer-history)))
  50. (query-replace-defaults
  51. (format "%s (default %s -> %s): " prompt
  52. (query-replace-descr (caar query-replace-defaults))
  53. (query-replace-descr (cdar query-replace-defaults))))
  54. (t (format "%s: " prompt))))
  55. (from
  56. ;; The save-excursion here is in case the user marks and copies
  57. ;; a region in order to specify the minibuffer input.
  58. ;; That should not clobber the region for the query-replace itself.
  59. (save-excursion
  60. (minibuffer-with-setup-hook
  61. (lambda ()
  62. (setq-local text-property-default-nonsticky
  63. (cons '(separator . t) text-property-default-nonsticky)))
  64. (if regexp-flag
  65. (read-regexp prompt nil 'minibuffer-history)
  66. (read-from-minibuffer
  67. prompt nil nil nil nil (car search-ring) t)))))
  68. (to))
  69. (if (and (zerop (length from)) query-replace-defaults)
  70. (cons (caar query-replace-defaults)
  71. (query-replace-compile-replacement
  72. (cdar query-replace-defaults) regexp-flag))
  73. (setq from (query-replace--split-string from))
  74. (when (consp from) (setq to (cdr from) from (car from)))
  75. (add-to-history query-replace-from-history-variable from nil t)
  76. ;; Warn if user types \n or \t, but don't reject the input.
  77. (and regexp-flag
  78. (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from)
  79. (let ((match (match-string 3 from)))
  80. (cond
  81. ((string= match "\\n")
  82. (message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead"))
  83. ((string= match "\\t")
  84. (message "Note: `\\t' here doesn't match a tab; to do that, just type TAB")))
  85. (sit-for 2)))
  86. (if (not to)
  87. from
  88. (add-to-history query-replace-to-history-variable to nil t)
  89. (add-to-history 'query-replace-defaults (cons from to) nil t)
  90. (cons from (query-replace-compile-replacement to regexp-flag))))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement