query-replace.sl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. %
  2. % QUERY-REPLACE.SL - Query/Replace command for EMODE
  3. %
  4. % Author: Alan Snyder
  5. % Hewlett-Packard/CRC
  6. % Date: 6 July 1982
  7. %
  8. % This file implements a query-replace command.
  9. % Modifications by William Galway:
  10. % "defun" -> "de" so TAGS can find things.
  11. % "setq" -> "setf"
  12. % This file requires COMMON, RING-BUFFER, BUFFER-POSITION.
  13. (fluid '(CurrentLineIndex point CurrentWindowDescriptor Prompt_Window
  14. last_search_string))
  15. (de query-replace-command ()
  16. (let* ((ask t)
  17. ch pattern replacement
  18. (pausing nil)
  19. (pause-message "Command?")
  20. (normal-message "Replace?")
  21. (help-message
  22. "Replace? SPACE:yes RUBOUT:no ESC:exit .:yes&exit ,:yes&show !:do all ^:back")
  23. (pause-help-message
  24. "Command? SPACE:go on ESC:exit !:do all ^:back")
  25. (message normal-message)
  26. (ring-buffer (ring-buffer-create 16))
  27. )
  28. % Get string to replace. Default is last search string (but don't
  29. % bother to update the default search string. (??))
  30. (setf pattern
  31. (prompt_for_string
  32. "Query Replace (string to replace): "
  33. last_search_string
  34. ))
  35. % Clear out the "default search string" message.
  36. (show_message "")
  37. (setf replacement
  38. (prompt_for_string "Replace string with: " NIL))
  39. (write-prompt "")
  40. (while (or pausing (buffer_search pattern 1))
  41. (if ask
  42. (progn (if (not pausing)
  43. (ring-buffer-push ring-buffer (buffer-get-position)))
  44. (show_message message)
  45. (setf ch (GetNextCommandCharacter))
  46. (show_message ""))
  47. (setf ch (char space)))
  48. (if pausing
  49. (selectq ch
  50. ((#.(char space) #.(char rubout) #/,) (setf pausing nil))
  51. (#/! (setf ask nil) (setf pausing nil))
  52. ((#.(char escape) #/.) (exit))
  53. (#.(char ff) (FullRefresh))
  54. (#/^ (ring-buffer-pop ring-buffer)
  55. (buffer-set-position (ring-buffer-top ring-buffer)))
  56. (#/? (setf message pause-help-message) (next))
  57. (t (ding))
  58. )
  59. (selectq ch
  60. (#.(char space) (do-string-replacement pattern replacement))
  61. (#/, (do-string-replacement pattern replacement)
  62. (setf pausing t))
  63. (#.(char rubout) (advance-over-string pattern))
  64. (#/! (do-string-replacement pattern replacement)
  65. (setf ask nil))
  66. (#/. (do-string-replacement pattern replacement)
  67. (exit))
  68. (#/? (setf message help-message) (next))
  69. (#.(char escape) (exit))
  70. (#.(char ff) (FullRefresh))
  71. (#/^ (ring-buffer-pop ring-buffer)
  72. (buffer-set-position (ring-buffer-top ring-buffer))
  73. (setf pausing t))
  74. (t (ding))
  75. )
  76. )
  77. (setf message (if pausing pause-message normal-message))
  78. )
  79. % Show we're done in the prompt window (to avoid "harming" message in
  80. % the message window).
  81. (write-prompt "Query Replace Done.")
  82. ))
  83. (de do-string-replacement (pattern replacement)
  84. % Both PATTERN and REPLACEMENT must be single line strings.
  85. % PATTERN is assumed to be in the current buffer beginning at POINT.
  86. % It is deleted and replaced with REPLACEMENT.
  87. % POINT is left pointing just past the inserted text.
  88. (let ((pattern-length (add1 (size pattern))))
  89. (delete_or_copy T CurrentLineIndex point
  90. CurrentLineIndex (+ point pattern-length))
  91. (insert_string replacement)
  92. ))
  93. (de advance-over-string (pattern)
  94. % PATTERN must be a single line string.
  95. % PATTERN is assumed to be in the current buffer beginning at POINT.
  96. % POINT is advanced past PATTERN.
  97. (let ((pattern-length (add1 (size pattern))))
  98. (setf point (+ point pattern-length))
  99. ))
  100. % "Write a string" into the prompt window (but don't select the prompt
  101. % window).
  102. (de write-prompt (string)
  103. (let ((old-window CurrentWindowDescriptor))
  104. % Show the string and select the window.
  105. (show_prompt string)
  106. % Back to original window.
  107. (SelectWindow old-window)))