The variable case-fold-searchdetermines whether searches are case-sensitive. It applies to all searches: incremental searches, word searches, searches within search-and-replace operations, and so on. By default, case-fold-searchis set to t, which means "ignore case unless the user types in mixed or uppercase." This sensible default is usually just what you want. But if you need case-sensitive searches, the Case-Insensitive Search option on the Options menu provides an easy way to experiment with this variable.
Likewise, if you don't want Emacs to adjust the case of your replacement strings, you can set the variable case-replace. Again, its value is t(for "true") by default, which means "adjust the case of a replacement string to match the original text"—that is, capitalize the replacement if the original word was capitalized and so on. Setting this variable to nilmeans "never adjust the case of the replacement string; always put it in exactly as I typed it." To change the value of case-replace, type M-x set-variable Enter case-replace Enter nil Enter(there's no menu option for this variable).
Both the menu option and the set-variablecommand change the behavior of Emacs only temporarily. If you start a new editing session, you'll be back to the default behavior. This is probably what you want, because searching separately for capitalized and lowercase words is inconvenient.
You can set the value for the Case-Insensitive Search option permanently by selecting Save Options from the Options menu or by adding this line to your .emacs file:
(setq-default case-fold-search nil) ; require exact matches
To set case-replacepermanently, add the following line to your .emacs file. You'll need to restart Emacs to have the change take effect.
(setq-default case-replace nil) ; never change case when replacing
You could change these variables through Emacs's interactive customization facility, Custom, instead (see Chapter 10).
3.2.6 Regular Expressions for Search and Replacement Operations
Sometimes none of the simpler searches described in this chapter are adequate. Regular expressions allow you to build searches with strings that contain various wildcards.
Table 3-4shows some of the characters you can use in creating a regular expression.
Table 3-4. Characters for creating regular expressions
Character(s) |
Match |
^ |
Matches the beginning of a line. |
$ |
Matches the end of a line. |
. |
Matches any single character (like ? in filenames). |
.* |
Matches any group of zero or more characters (. matches any character and * matches zero or more of the previous character). |
\< |
Matches the beginning of a word. |
\> |
Matches the end of a word. |
[ ] |
Matches any character specified within the brackets; for example, [a-z] matches any alphabetic character. |
\s, \S |
Matches any whitespace character: space, a newline, a tab, a carriage return, a formfeed, or a backspace; \S matches any character except whitespace. |
\d, \D |
Matches any single digit, 0-9; \D matches any character but a digit. |
\w, \W |
Matches any "word" character (upper- and lowercase letters, digits, and the underscore character); \W matches any character but these. |
If you do a regular expression search for ^word$, you would find instances of word on a line by itself. The ^says that the wmust be the first character on the line, the $says that the dmust be the last character.
If you wanted to find all words starting with beg and ending with the letter s , you could use beg[a-z]*sas your regular expression. This would find the words begins , begets , and begonias , in addition to really odd words like shibegrees and altbegaslia . If you don't want these mutants—that is, if you really want words that begin with beg and end with s , use \. The \<is a special sequence that matches the beginning of a word; \>matches the end of a word. If you wanted to find the words beg , big , and bag ; but not begonias , and certainly not any strange words with beg on the inside, you would use \as the regular expression.
To search for a ^, $, ., *, [, ], or any number of other special characters, you obviously can't use the character itself. Put a backslash ( \) first—i.e., to search for a period, search for \. For example, to search for the electronic mail address`:
howie@mcds.com
the regular expression would be:
howie@mcds\.com
This is a barebones introduction to regular expressions; see Chapter 11for more details and Mastering Regular Expressions by Jeffrey Friedl (O'Reilly) for a book-length treatment of this topic.
You can use regular expressions in incremental searches and in query-replace. Table 3-5lists the commands you use for regular expression searches. Although they are initiated with slightly different commands, the searches are the same as those described earlier in this chapter.
Table 3-5. Regular expression search commands
Keystrokes |
Command name |
Action |
C-M-s Enter Edit → Search → Regexp Forward |
re-search-forward |
Search for a regular expression forward. |
C-M-r Enter Edit → Search → Regexp Backwards |
re-search-backward |
Search for a regular expression backward. |
C-M-s Edit → Search → Incremental Search → Forward Regexp |
isearch-forward-regexp |
Search incrementally forward for a regular expression. |
C-M-r Edit → Search → Incremental Search → Backward Regexp |
isearch-backward-regexp |
Search incrementally backward for a regular expression. |
C-M-% Edit → Replace → Replace Regexp |
query-replace-regexp |
Query-replace a regular expression. |
( none ) |
replace-regexp |
Globally replace a regular expression unconditionally (use with caution). |
3.3 Checking Spelling Using Ispell
Emacs includes two spell-checking interfaces: to the Unix spell checker, spell
, and to Ispell, which many people, including us, prefer. We say "interfaces" because Emacs does not include the executables for either of these spell-checkers. Because Ispell is superior and runs on a variety of platforms, we'll cover only Ispell here. If you attempt to run Ispell and it is not available, you'll have to install it. Chapter 13provides details on installing Ispell on Windows and on Mac OS X.
Читать дальше