Книга: Learning GNU Emacs, 3rd Edition

11.3.1 Buffers, Text, and Regions

11.3.1 Buffers, Text, and Regions

Table 11-4 shows some basic Emacs functions relating to buffers, text, and strings that are only useful to Lisp programmers and thus aren't bound to keystrokes. We already saw a couple of them in the count-words-buffer example. Notice that some of these are predicates, and their names reflect this.

Table 11-4. Buffer and text functions

Function Value or action
point Character position of point.
mark Character position of mark.
point-min Minimum character position (usually 1).
point-max Maximum character position (usually size of buffer).
bolp Whether point is at the beginning of the line (t or nil).
eolp Whether point is at the end of the line.
bobp Whether point is at the beginning of the buffer.
eobp Whether point is at the end of the buffer.
insert Insert any number of arguments (strings or characters) into the buffer after point.
number-to-string Convert a numerical argument to a string.
string-to-number Convert a string argument to a number (integer or floating point).
char-to-string Convert a character argument to a string.
substring Given a string and two integer indices start and end, return the substring starting after start and ending before end. Indices start at 0. For example, (substring "appropriate" 2 5) returns "pro".
aref Array indexing function that can be used to return individual characters from strings; takes an integer argument and returns the character as an integer, using the ASCII code (on most machines). For example, (aref "appropriate" 3) returns 114, the ASCII code for r.

Many functions not included in the previous table deal with buffers and text, including some that you should be familiar with as user commands. Several commonly used Emacs functions use regions, which are areas of text within a buffer. When you are using Emacs, you delineate regions by setting the mark and moving the cursor. However, region-oriented functions (such as kill-region, indent-region, and shell-command-on-region—really, any function with region in its name) are actually more flexible when used within Emacs Lisp code. They typically take two integer arguments that are used as the character positions of the boundaries for the region on which they operate. These arguments default to the values of point and mark when the functions are called interactively.

Obviously, allowing point and mark as interactive defaults is a more general (and thus more desirable) approach than one in which only point and mark can be used to delineate regions. The r option to the interactive function makes it possible. For example, if we wanted to write the function translate-region-into-German, here is how we would start:

(defun translate-region-into-German (start end)
  (interactive "r")
  ...

The r option to interactive fills in the two arguments start and end when the function is called interactively, but if it is called from other Lisp code, both arguments must be supplied. The usual way to do this is like this:

(translate-region-into-German (point) (mark))

But you need not call it in this way. If you wanted to use this function to write another function called translate-buffer-into-German, you would only need to write the following as a "wrapper":

(defun translate-buffer-into-German ( )
  (translate-region-into-German (point-min) (point-max)))

In fact, it is best to avoid using point and mark within Lisp code unless doing so is really necessary; use local variables instead. Try not to write Lisp functions as lists of commands a user would invoke; that sort of behavior is better suited to macros (see Chapter 6).

Оглавление книги


Генерация: 3.071. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз