![]() ![]() See tr for an example mixing strings and characters. See CharacterProcessing and StringModification. Reversing a string: ( defun string/reverse (str) (and (string-match (rx-to-string `(: bos ,prefix) t) ( defun string/starts-with (string prefix) Thanks to on #emacs-beginners irc channel for the explanation. ) syntax and require a, if you want them to be evaluated. Symbols are not evaluated by default in the `(. Because there is no, before : or eos, both : and eos are not evaluated. The only symbol that is evaluated is suffix. `(: ,suffix eos) is convenience syntax for (list ': suffix 'eos). ` is special syntax (note the backtick, not just tick) that allows some things in the list to be preceded by a comma, which are then evaluated. It wants the value of the symbol named “suffix”, not the symbol itself. ,suffix is SEXP1 and eos is SEXP2 '(: suffix eos) would create a list of three elements: The symbol named “:”, the symbol named “suffix”, and the symbol named “eos”. ![]() Matches what SEXP1 matches, followed by what SEXP2 matches, etc. The help page for “rx” elucidates the syntax: (: SEXP1 SEXP2. The first argument to rx-to-string `(: ,suffix eos) is a regular expression in sexp form. (and (string-match (rx-to-string `(: ,suffix eos) t) ![]() (string-equal (substring s 0 (length begins)) begins))Īlternatively, match using a RegularExpression: ( defun string/ends-with (string suffix) "Return non-nil if string S starts with BEGINS." (t nil))) ( defun string/starts-with (s begins) (string= (substring s (- (length ending))) ending)) "Return non-nil if string S ends with ENDING." (string-suffix-p "dozer" "bulldozer") optional ignore case argument tooĬustom functions: ( defun string/ends-with (s ending) (string-prefix-p "bull" "bulldozer") optional ignore case argument too => "bc" starts-with and ends-with predicates The TO argument is optional: (substring "abcdefg" -3)īuffers: ( with-temp-buffer (insert "abcdefg") (buffer-substring 2 4)) While it is quite common in other programming languages to work on strings contained in variables, in Emacs it is also common to work on text in buffers. => t nil prior to Emacs 23 Strings vs buffer text ![]() => t nil prior to Emacs 23 (eq "" (propertize "" 'face 'italic)) => t nil prior to Emacs 23 (eq "" (purecopy "")) => t ` string-empty-p' was added in 24.4.Īs a (very minor) space and performance optimization, starting with Emacs 23, Emacs keeps an interned copy of the empty string as a single object (eq "" "") The empty string (zero-length string, null string, …): (string-equal "" "") Should this page grow too large, we’ll split it up later. Just link to more elaborate pages when appropriate. There’s going to be some overlap with Categor圜ode, obviously. These recipes should be pastable into the *scratch* buffer so that users can hit ‘C-j’ and evaluate them step by step. It would be helpful to have here in one spot. For every task addressed, a worked-out solution is presented as a short, focused, directly usable piece of code.Īll this stuff can be found elsewhere, but it is scattered about in libraries, manuals, etc. This page contains snippets of code that demonstrate basic EmacsLisp programming operations in the spirit of the O’Reilly cookbook series of books. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |