query-replace.sl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %
  3. % QUERY-REPLACE.SL - Query/Replace command
  4. %
  5. % Author: Alan Snyder
  6. % Hewlett-Packard/CRC
  7. % Date: 6 July 1982
  8. % Revised: 17 February 1983
  9. %
  10. % 17-Feb-83 Alan Snyder
  11. % Define backspace to be a synonym for rubout. Terminate when a non-command
  12. % character is read and push back the character (like EMACS).
  13. % 9-Feb-83 Alan Snyder
  14. % Must now refresh since write-message no longer does.
  15. %
  16. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  17. (CompileTime (load objects extended-char fast-int fast-strings))
  18. % Externals used here:
  19. (fluid '(last-search-string nmode-current-buffer))
  20. % Internal static variables:
  21. (fluid '(query-replace-message
  22. query-replace-help
  23. query-replace-pause-help))
  24. (setf query-replace-message "Query-Replace")
  25. (setf query-replace-help
  26. (string-concat
  27. query-replace-message
  28. " SPACE:yes RUBOUT:no ESC:exit .:yes&exit ,:yes&show !:do all ^:back"
  29. ))
  30. (setf query-replace-pause-help
  31. (string-concat
  32. query-replace-message
  33. " SPACE:go on ESC:exit !:do all ^:back"
  34. ))
  35. (de replace-string-command ()
  36. (let* ((pattern
  37. (setf last-search-string
  38. (prompt-for-string "Replace string: " last-search-string)))
  39. (replacement (prompt-for-string "Replace string with: " NIL))
  40. (count 0)
  41. (old-pos (buffer-get-position))
  42. )
  43. (while (buffer-search pattern 1)
  44. (do-string-replacement pattern replacement)
  45. (setf count (+ count 1))
  46. )
  47. (buffer-set-position old-pos)
  48. (write-prompt (BldMsg "Number of replacements: %d" count))
  49. ))
  50. (de query-replace-command ()
  51. (let* ((ask t)
  52. ch pattern replacement
  53. (pausing nil)
  54. (ring-buffer (ring-buffer-create 16))
  55. )
  56. (setf pattern
  57. (setf last-search-string
  58. (prompt-for-string
  59. "Query Replace (string to replace): "
  60. last-search-string
  61. )))
  62. (setf replacement
  63. (prompt-for-string "Replace string with: " NIL))
  64. (set-message query-replace-message)
  65. (while (or pausing (buffer-search pattern 1))
  66. (if ask
  67. (progn
  68. (cond (pausing
  69. (nmode-set-immediate-prompt "Command? ")
  70. )
  71. (t
  72. (ring-buffer-push ring-buffer (buffer-get-position))
  73. (nmode-set-immediate-prompt "Replace? ")
  74. ))
  75. (nmode-refresh)
  76. (setf ch (input-terminal-character))
  77. (write-prompt "")
  78. )
  79. (setf ch (x-char space)) % if not asking
  80. )
  81. (if pausing
  82. (selectq ch
  83. ((#.(x-char space) #.(x-char rubout)
  84. #.(x-char backspace) #.(x-char !,))
  85. (write-message query-replace-message)
  86. (setf pausing nil))
  87. (#.(x-char !!)
  88. (setf ask nil) (setf pausing nil))
  89. ((#.(x-char escape) #.(x-char !.))
  90. (exit))
  91. (#.(x-char C-L)
  92. (nmode-full-refresh))
  93. (#.(x-char ^)
  94. (ring-buffer-pop ring-buffer)
  95. (buffer-set-position (ring-buffer-top ring-buffer)))
  96. (#.(x-char ?)
  97. (write-message query-replace-pause-help) (next))
  98. (t (push-back-input-character ch) (exit))
  99. )
  100. (selectq ch
  101. (#.(x-char space)
  102. (do-string-replacement pattern replacement))
  103. (#.(x-char !,)
  104. (do-string-replacement pattern replacement)
  105. (write-message query-replace-message)
  106. (setf pausing t))
  107. ((#.(x-char rubout) #.(x-char backspace))
  108. (advance-over-string pattern))
  109. (#.(x-char !!)
  110. (do-string-replacement pattern replacement)
  111. (setf ask nil))
  112. (#.(x-char !.)
  113. (do-string-replacement pattern replacement)
  114. (exit))
  115. (#.(x-char ?)
  116. (write-message query-replace-help) (next))
  117. (#.(x-char escape)
  118. (exit))
  119. (#.(x-char C-L)
  120. (nmode-full-refresh))
  121. (#.(x-char ^)
  122. (ring-buffer-pop ring-buffer)
  123. (buffer-set-position (ring-buffer-top ring-buffer))
  124. (setf pausing t))
  125. (t (push-back-input-character ch) (exit))
  126. )
  127. )
  128. )
  129. (reset-message)
  130. (write-prompt "Query Replace Done.")
  131. ))
  132. (de do-string-replacement (pattern replacement)
  133. % Both PATTERN and REPLACEMENT must be single line strings. PATTERN is
  134. % assumed to be in the current buffer beginning at POINT. It is deleted and
  135. % replaced with REPLACEMENT. POINT is left pointing just past the inserted
  136. % text.
  137. (let ((old-pos (buffer-get-position)))
  138. (advance-over-string pattern)
  139. (extract-region T old-pos (buffer-get-position))
  140. (insert-string replacement)
  141. ))
  142. (de advance-over-string (pattern)
  143. % PATTERN must be a single line string. PATTERN is assumed to be in the
  144. % current buffer beginning at POINT. POINT is advanced past PATTERN.
  145. (let ((pattern-length (string-length pattern)))
  146. (set-char-pos (+ (current-char-pos) pattern-length))
  147. ))