m-xcmd.sl 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %
  3. % M-XCMD.SL - Miscellaneous Extended Commands
  4. %
  5. % Author: Jeffrey Soreff
  6. % Hewlett-Packard/CRC
  7. % Date: 24 January 1983
  8. % Revised: 17 February 1983
  9. %
  10. % 17-Feb-83 Alan Snyder
  11. % Revise M-X Set Visited Filename to actualize the new file name (i.e.,
  12. % convert it to the true name of the file). Revise M-X Rename Buffer to
  13. % convert buffer name to upper case and to check for use of an existing
  14. % buffer name.
  15. %
  16. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  17. (compiletime (load fast-int))
  18. (fluid '(nmode-current-buffer))
  19. (de delete-matching-lines-command () (delete-possibly-matching-lines nil))
  20. (de delete-non-matching-lines-command () (delete-possibly-matching-lines t))
  21. (de delete-possibly-matching-lines (retain-if-match)
  22. % This function prompts for a string which it searches for in all
  23. % lines including and after the current one. The search is
  24. % insensitive to case. If retain-if-match is true then all lines
  25. % with the string will be retained and all lines lacking it will be
  26. % deleted, otherwise all lines with the string will be deleted.
  27. % Point is left at the start of the line that it was originally on.
  28. % This function does not return a useful value.
  29. (move-to-start-of-line)
  30. (let ((modified-flag (=> nmode-current-buffer modified?))
  31. (starting-line (current-line-pos))
  32. (next-unfilled-line (current-line-pos))
  33. (match-string (string-upcase
  34. (prompt-for-string "Comparison String: " ""))))
  35. (for (from test-line starting-line (- (current-buffer-size) 1) 1)
  36. (do (when
  37. (if retain-if-match % This sets the sign of the selections.
  38. (forward-search-on-line test-line 0 match-string)
  39. (not (forward-search-on-line test-line 0 match-string)))
  40. (current-buffer-store next-unfilled-line
  41. (current-buffer-fetch test-line))
  42. (incr next-unfilled-line))))
  43. (if (= next-unfilled-line (current-buffer-size)) % No lines were tossed.
  44. (=> nmode-current-buffer set-modified? modified-flag)
  45. % Else
  46. (extract-region t
  47. (buffer-position-create next-unfilled-line 0)
  48. (progn (move-to-buffer-end) (buffer-get-position))))
  49. (set-line-pos starting-line)))
  50. (de count-occurrences-command ()
  51. % This function counts the number of instances of a string after the
  52. % current buffer position. The counting is insensitive to case.
  53. % The user is prompted for the string. If the user supplies an
  54. % empty string, they are told that it can't be counted. This avoids
  55. % an infinite loop. The count obtained is displayed in the prompt
  56. % line. This function does not return a useful value.
  57. (let ((count 0)
  58. (initial-place (buffer-get-position))
  59. (match-string (string-upcase
  60. (prompt-for-string "Count Occurrences: " ""))))
  61. (if (equal match-string "")
  62. (write-prompt "One can't count instances of the empty string.")
  63. (while (forward-search match-string)
  64. (incr count)
  65. (move-forward))
  66. (buffer-set-position initial-place)
  67. (write-prompt (bldmsg "%d occurrences" count)))))
  68. (de set-key-command ()
  69. % This binds a user-selected function to a command. The user is
  70. % prompted for the function name and the key sequence of the
  71. % command. This function then tests to see if the user's function
  72. % exists, then asks for confirmation just before doing the binding.
  73. % This function does not return a useful value.
  74. (let ((function (intern (string-upcase
  75. (prompt-for-string "Function Name: " "")))))
  76. (if (funboundp function)
  77. (write-prompt (bldmsg "No function %w was found." function))
  78. (let* ((junk (write-message (bldmsg "Put %p on key:" function)))
  79. (command (input-command)))
  80. (when (nmode-y-or-n? (bldmsg "Load %w with %w"
  81. (command-name command) function))
  82. (set-text-command command function))))))
  83. (de set-visited-filename-command ()
  84. % This command allows a user to alter the filename associated with the
  85. % current buffer. Prompt-for-defaulted-filename is used to set default
  86. % characteristics. This function does not return a useful value.
  87. (let* ((new-name
  88. (prompt-for-defaulted-filename "Set Visited Filename: " NIL)))
  89. (=> nmode-current-buffer set-file-name
  90. (or (actualize-file-name new-name) new-name)
  91. )))
  92. (de rename-buffer-command ()
  93. % This function allows the user to rename the current buffer if it is not a
  94. % system buffer like main or output. It prompts the user for a new buffer
  95. % name. If the user inputs an empty string, the buffer name is set to a
  96. % converted version of the filename associated with the buffer. Buffer
  97. % names are converted to upper case. An error is reported if the user
  98. % chooses the name of another existing buffer. This function does not
  99. % return a useful value.
  100. (if (not (buffer-killable? nmode-current-buffer)) % tests for main and output
  101. (nmode-error (bldmsg "Buffer %w cannot be renamed."
  102. (=> nmode-current-buffer name)))
  103. (let* ((old-name (=> nmode-current-buffer name))
  104. (new-name
  105. (string-upcase
  106. (prompt-for-string
  107. "Rename Buffer: "
  108. (let ((filename (=> nmode-current-buffer file-name))) % Default
  109. (if filename
  110. (filename-to-buffername filename)
  111. % Else, if there is no filename
  112. (=> nmode-current-buffer name)))))))
  113. (when (not (string= new-name old-name))
  114. (if (buffer-exists? new-name)
  115. (nmode-error (bldmsg "Name %w already in use." new-name))
  116. (=> nmode-current-buffer set-name new-name)
  117. )))))
  118. (de kill-some-buffers-command ()
  119. % This functions lists the killable buffers one by one, letting the
  120. % user kill, retain, or examine each one as it is named. This
  121. % function does not return a useful value.
  122. (let ((buffer-list (nmode-user-buffers)))
  123. (while buffer-list
  124. (let ((buffer-to-die (car buffer-list)))
  125. (setf buffer-list (cdr buffer-list))
  126. (when (and (buffer-killable? buffer-to-die)
  127. (let ((name (=> buffer-to-die name))
  128. (mod-warn (if (=> buffer-to-die modified?)
  129. "HAS BEEN EDITED"
  130. "is unmodified")))
  131. (recursive-edit-y-or-n
  132. buffer-to-die
  133. (bldmsg
  134. "Buffer %w %w. Kill it? Type Y or N or ^R to edit"
  135. name mod-warn)
  136. (bldmsg
  137. "Type Y to kill or N to save buffer %w" name))))
  138. (buffer-kill-and-detach buffer-to-die))))))
  139. (de insert-date-command ()
  140. % This inserts the current date into the text, after point, and
  141. % moves point past it. It does not return a useful value.
  142. (insert-string (current-date-time)))
  143. (de revert-file-command ()
  144. % This function allows the user to replace the current buffer's
  145. % contents with the contents of the file associated with the current
  146. % buffer, if there is one. It asks for confirmation before actually
  147. % performing the replacement. This function does not return a
  148. % useful value.
  149. (let ((fn (=> nmode-current-buffer file-name))
  150. (bn (=> nmode-current-buffer name)))
  151. (if (and
  152. (if fn T (write-prompt "No file to read old copy from") NIL)
  153. (nmode-y-or-n?
  154. (BldMsg "Want to replace buffer %w with %w from disk?"
  155. bn fn)))
  156. (read-file-into-buffer nmode-current-buffer fn))))