Go to the first, previous, next, last section, table of contents.


zap-to-char

The zap-to-char function is written differently in GNU Emacs version 18 and version 19. The version 19 implementation is simpler, and works slightly differently. We will first show the function as it is written for version 19 and then for version 18.

The Emacs version 19 implementation of the interactive 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'.

The Emacs version 18 implementation removes the text from point up to but not including the specified character. Thus, in the example shown in the previous paragraph, the `s' would not be removed.

In addition, the version 18 implementation will go to the end of the buffer if the specified character is not found; but the version 19 implementation will simply generate an error (and not remove any text).

In order to determine how much text to remove, both versions of zap-to-char use a search function. Searches are used extensively in code that manipulates text, and it is worth focusing attention on the search function as well as on the deletion command.

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

(defun zap-to-char (arg char)  ; version 19 implementation
  "Kill up to and including ARG'th occurrence of CHAR.
Goes backward if ARG is negative; error if CHAR not found."
  (interactive "*p\ncZap to char: ")
  (kill-region (point)
               (progn
                 (search-forward
                  (char-to-string char) nil nil arg)
                 (point))))


Go to the first, previous, next, last section, table of contents.