Changes

Jump to: navigation, search

Emacs Lisp Cookbook

659 bytes added, 09:24, 21 July 2012
m
removed odd Russian text about C++ behavior
work on strings in buffers. That's why the following contains examples
of both.
 
=== Substrings ===
 
<syntaxhighlight lang="lisp">
(substring "abcdefg" 0 3)
==> "abc"
(substring "abcdefg" -3 -1)
==> "ef"
</syntaxhighlight>
 
The TO argument is optional:
 
<syntaxhighlight lang="lisp">
(substring "abcdefg" -3)
==> "efg"
</syntaxhighlight>
 
Buffers:
 
<syntaxhighlight lang="lisp">
(with-temp-buffer
(insert "abcdefg")
(buffer-substring 2 4))
==> "bc"
</syntaxhighlight>
=== Processing characters ===
</syntaxhighlight>
See CharacterProcessing and StringModification. See [[tr]] for an example of if you sometimes need to mix strings and characters.
Looking at characters in buffers:
(point)))
</syntaxhighlight>
Забавно!недавно был очень похожий случай== Search and Replace ==Searching and replacing text is a fundamental editing need. Emacs has separatefacilities for both interactive and scripted search and replace.есть функция:string addsign === Interactive Use ===The '''replace-regexp''' function provides a way to replace text interactively.This function supports embedded emacs lisp statements in the second arguement(char sign, const srnitg & sthe replacement expression). By default '''replace-regexp''' replacesevery match after the cursor location until it reaches the end of the buffer.A more useful method is to mark a region for replacement. For example: calling {{1) return sign+s; // так нельзя2) return srnitgCommand|replace-regexp RET \(s1[A-Z]\)+s; //тоже нельзя3) return srnitgRET \,(s1, downcase \1)+s;// только так можно}} on the marked region '''AABBCC''' will convert it to '''aabbcc'''.The firstarguement is a regular expression matching any capital letter and saving itas the first match, while the '''\,''' indicates embedded emacs lisp code(which calls the `downcase` function on the matched pattern)=== Scripted Use ===A cleaner solution while scripting is to combine '''search-forward-regexp'''with '''replace-match'''. но блин не сразу допетриваешь }Решил это сам проверитьEvery time the search is successful, на компиляторе gcc 3the resultsare implicitly saved in to '''match-string'''.4.2.вот такая программулинкаThis short function replacesevery pattern in a marked region with a new string drawn from its components:#include <stringsyntaxhighlight lang="lisp">#include <stdio.h>using namespace std;string addsign(defun camelCase-to_underscores (char s1, const srnitg & sstart end){//1)return s1+s; // так нельзя//2)return srnitg "Convert any string matching something like aBc to a_bc" (s1interactive "r")+s; //тоже нельзя//3)return srnitg (s1, 1)+s;// только так можно... но блин не сразу допетриваешь//4)return srnitgsave-restriction (&s1, 1narrow-to-region start end)+s;}int main (int n, goto-char **args1){ srnitg text = "1.9876"; srnitg res = addsign ('-', text); putslet (res.c_str(case-fold-search nil)); return 0;}выдала мне срвсем другое:первый вариант (while (как "нельзяsearch-forward-regexp "\\([a-z]\\) выдал правильный результат: \\([A-1.9876, второй вариант Z]\\)\\([a- ошибку компиляции, а третий(который "только так и можноz]\\)"nil t) дал нечто невразумительное (replace- кучу смеющихся рожиц и в концеmatch (concat (match-число string 1.9876. Мне даже непонятно, как у автора сообщения получился нормальный результат.Последний ) "_" (downcase (4match-ый, "мой" вариантstring 2)) дал тоже верный результат: (match-1.9876string 3)) t nil)))))</syntaxhighlight>
== Dates and times ==
</syntaxhighlight>
=== Formatting dates === Use I discovered Emacs in '97 when I was doing C++ depnmovleet for BellSouth (the function `format-time-stringSun depnmovleet tools had great integration with either vi, Emacs or XEmacs editors). I' which is d been a build in function in both Emacsen and works like `strftime': <syntaxhighlight lang="lisp"> ;; Year-Month-Day: hardcore vi user since 1990 (insert still am), but I could adapt Emacs (format-actually, for some reason I opted for XEmacs at the time-string "%Y-%m-%d" can't remember why maybe it had better fonts)better for the job at hand. Syntax highlighting check (vi never had this it only arrived with vim) ;; Hour:Minutes:Seconds , split buffers (insert top for the .h, bottom for the .cpp) check, tab completion for finding files check, nice macro record/playback features check, nice search/replace check,control behavior of tab key check, auto replace tabs with 4 spaces check. So, after a while you can imagine I crafted a pretty customized/personal .emacs file. I even continued to use emacs for a while after switching to Java in 2000 (format-it wasn't until Eclipse 2.0 before I switched to an IDE and left Emacs behind).Then I moved on, and for some reason the .emacs file got left behind. Shame really I haven't really used Emacs since it's not the same without my .emacs file and I don't have the timeor inclination to re-string "%H-%M-%S"))</syntaxhighlight> create it.
=== Conversions ===

Navigation menu