Next: , Up: zap-to-char   [Contents][Index]

The Complete zap-to-char Implementation

The zap-to-char function removes the text in the region between the location of the cursor (i.e., of point) up to and including the next occurrence of a specified character. The text that zap-to-char removes is put in the kill ring; and it can be retrieved from the kill ring by typing C-y (yank). If the command is given an argument, it removes text through that number of occurrences. Thus, if the cursor were at the beginning of this sentence and the character were ‘s’, ‘Thus’ would be removed. If the argument were two, ‘Thus, if the curs’ would be removed, up to and including the ‘s’ in ‘cursor’.

If the specified character is not found, zap-to-char will say “Search failed”, tell you the character you typed, and not remove any text.

In order to determine how much text to remove, zap-to-char uses a search function. Searches are used extensively in code that manipulates text, and we will focus attention on them as well as on the deletion command.

Here is the complete text of the version 22 implementation of the function:

(defun zap-to-char (arg char)
  "Kill up to and including ARG'th occurrence of CHAR.
  (interactive "p\ncZap to char: ")
  (kill-region (point) (progn
                         (search-forward (char-to-string char)
                                         nil nil arg)

The documentation is thorough. You do need to know the jargon meaning of the word ‘kill’.
