work on strings in buffers. That's why the following contains examples

work on strings in buffers. That's why the following contains examples

of both.

of both.

−

−

Just found in my email's sent item. Nicolas is the head of IT of UK office:=================================Re : Qubee Service in BangladeshDear Nicolas Ruelle:Greeting!I would be glad if you take some of your time and go thugroh my email. This is regards to the internet service you are providing in Bangladesh. Since I failed to get the contact email address of head of IT or some upper management of Qubee Bangladesh I had no choice but take this opportunity to write you few words of mine.In order for me to work to my remote office in Canada I needed a true reliable consumer/business high speed internet which I did not find until I learned about Qubee's WiMax as I believe wireless is the reliable link among others when it comes to Bangladesh.Incident 1:Since I started the Qubee service on 2nd week of January with 512kbs it was satisfactory with the speed accept for the VPN issue that started to occur from the day one. I was able to dial in the VPN and connect but was not able to communicate with any of the servers within the LAN, I had done some checks with other ISPs and found no issues which satisfied me that it was either the WiMax Router/Modem or the ISP's router. I called the customer care number wanted to speak to someone from the technical support. Customer care asked me provide her with the problem and so did I. Unfortunately the individual had no clear picture about VPN but asked me to check download speed, radio's parameter, and all the basic things that not related to the issue I had specified. At the end I had to introduce myself as an advance user and a technical person and asked her if I could speak to someone from the technical support. She created ticket for it and informed me that someone will get back to me soon. 36 hours past no one contacted me and I had to call the customer care number to follow up with my ticket. Few hours later I had a phone call from some individual and I had to describe the issue and I was asked to check the speed and other non-related VPN checks. It was frustrating. I beged to speak to someone who is familiar with the VPN and its usage. Next day I had a phone call from the customer care and told me that there was no issues from Qubee end but the VPN provider or my PC so I had to fight against the customer care's word. Few days later I received a call from Qubee and asking for my VPN credential for them to test from their office which surprised me big time. I refused and offered myself with my laptop at Qubee's office. I went there to prove that it is the Qubee not the VPN provider by connecting via other ISP's Edge Modem. They started tracing finally and at a point I had to ask them to test my laptop with a public IP assigned and it went successful. The issue was resolved by assigning a public IP address as all the Qubee modems are assigned with private 10.11. network.Incident 2:A week ago around 7 PM local time my internet link went down all of sudden. After investing I found it was about 70-90% packet loss to some hosts including Google and Qubee's local DNS server. I called the customer care number after my further investigation to make sure my end was good. The customer care individual wanted to me access my PC to troubleshoot and take some reports on the modem. Unfortunately I was unable to explain to her that it was not possible for her get into PC due to network issue. I was surprised that she claimed to be technical without knowing about ping utility and packet loss terms. She also did not have idea about pinging between their DNS name and my end or how it works. I requested to talk to someone from technical team, we have to follow our procedure and some will visit your premises tomorrow , she replied. I knew that the issue may recover next day or later by itself but I needed support at that particular time as I was in the middle of my regular remote work. At the end I had to forget about hoping and use my backup internet (real slow) to report the office about my unavailability for that evening (imagine if I had no backup internet).Next morning everything was resolved by itself, I suspect there were some issues within the base station during the incident period. I had a visitor from Qubee's third party support to check my modem and some of the numbers of it after 3 days. He found nothing or could not do anything but took some snapshots with him. Note: issue resolved by it self. Imagine you were down for three days and no followups were made. Next day I had another phone call from Qubee's third party contractor to schedule a time to visit my place regarding the ticket Download/upload Issue . Note the ticket description as I specifically asked her to put packet loss somewhere in the ticket. I had to tell him everything was fine, no issues.Summary:I would like Qubee to put extra attention to be a modern and standard internet service provider in Bangladesh. As a subscriber I would like to see the following in Qubee. * Valuing customers by listening and understanding their specific problems and the needs (should be one them on the top) * A 24/7 true technical support team with basic troubleshooting skills set. * An escalation procedure should be implemented as well so customers are in touch in time (no 3 days waiting periods). * Understanding the terms Customer Care and Technical Support and implementation of it. * Response time and proper follow up manner. * Downtime notifications in advance * Responding emails with their inquiries. (I never received one) * Plug-n-play account activation system, where customer will be able to activate their unit on their own after purchasing the box from the outlet so installation engineer is required to visit the customer premises.Thank you!================Reply received after 30 minutes:================Dear Sayeed,Thank you very much for this valuable input. I am forwarding it immediately to the appropriate managers at Qubee Bangladesh: the head of customer care and the head of IT.Best regards,Nicolas Ruelle============I believe it never helped.

=== Processing characters ===

=== Processing characters ===

Line 65:

Line 63:

</syntaxhighlight>

</syntaxhighlight>

−

See CharacterProcessing and StringModification. See [[tr]] for an example of you sometimes need to mix strings and characters.

+

See CharacterProcessing and StringModification. See [[tr]] for an example if you sometimes need to mix strings and characters.

I discovered Emacs in '97 when I was doing C++ depnmovleet for BellSouth (the Sun depnmovleet tools had great integration with either vi, Emacs or XEmacs editors). I'd been a hardcore vi user since 1990 (still am), but I could adapt Emacs (actually, for some reason I opted for XEmacs at the time 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), split buffers (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 (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 time or inclination to re-create it.

=== Conversions ===

=== Conversions ===

Line 422:

Line 457:

TODO

TODO

−

−

Its funny that your example uses std::string beaucse it has the solution you're looking for.What you're trying to do is initialize a class member before initializing the base class, and you found that you can't. The typical way to get around this is to abandon hope of extending Foo and implement the wrapper in terms of Foo. (has-a vs. is-a) This solution has the annoyance of you needing to redeclare each and every method you want to use and then forwarding those calls to Foo, and you won't be able to use a FooWrapper in place of a Foo as well, meaning you may need to change a ton of references in your code. However, all you have to do is have FooWrapper expose its underlying Foo and you're good. (kinda like what string::c_str() does)Check out your copy of Effective C++. From the TOC I think item 40 discusses this.

== Sequences ==

== Sequences ==

Line 615:

Line 648:

In the latter, a more efficient algorithm would use a loop (a non-local exit).

In the latter, a more efficient algorithm would use a loop (a non-local exit).

−

−

I don't think so. Normally you will open it up with latest veorisn, then save it in an earlier veorisn, that wat you can retain the 2008 veorisn with all the new options, and then convert it to an earlier. The improvements which are not handled by an earlier veorisn will be prompted when you try to save. You can lose layers or part of layers.

=== Vectors ===

=== Vectors ===

Line 728:

Line 759:

(point-max)

(point-max)

file))))

file))))

−

</syntaxhighlight>

+

</syntaxhighlight>

−

+

−

Maybe you are interested in word-count.el. It's cnirately to much foryour use-case but it is a great tool if you have some target lengthfor a document (e.g. diary entries).What I'm really missing in Emacs is some usage statistics. I canalready get an overview over my keybind habits with keyfreq but Iwould like that to be more comprehensive. Average buffer-length, timespent in different modes, time spent in files that are part of acertain directory, lines added etc. Maybe I'm just addicted to lifestatistics and this is not really useful. Who knows.

+

=== Searching ===

=== Searching ===

Line 876:

Line 905:

[[Category:Customization]]

[[Category:Customization]]

[[Category:Intermediate]]

[[Category:Intermediate]]

+

[[Category:Emacs Lisp]]

+

[[Category:Lisp]]

+

[[Category:Programming]]

+

[[Category:Tutorial]]

Revision as of 08:46, 26 March 2014

This page contains snippets of code that demonstrate basic Emacs Lisp
programming operations in the spirit of O'Reilly's Cookbook
series of books. For every task addressed, a worked-out solution
is presented as a short, focused, directly usable piece of code.

All this stuff can be found elsewhere, but it is scattered about in
libraries, manuals, etc. It would be helpful to have it here in one
spot.

These recipes should be pastable into the *scratch* buffer so that
users can hit C-j and evaluate them step by step.

As a space and performance optimization, Emacs keeps an intern-ed copy
of the empty string as a single object

(eq""(purecopy ""))==> t
(eq""(propertize "" 'face 'italic))==> t

Strings vs buffer content

While it is quite common in other programming languages to work on
strings contained in variables in Emacs it is even more idiomatic to
work on strings in buffers. That's why the following contains examples
of both.

Splitting strings

The 'split-string' function is defined in 'subr.el' as

(defun split-string (string &optional separators omit-nulls)...)

where 'separators' is a regular expression describing where to split the string. 'separators' defaults to white-space characters (spaces, form feeds, tabs, newlines, carriage returns, and vertical tabs). If 'omit-nulls' is set as 't' then zero-length strings are deleted from output.

Formatting

Killing text

As the Emacs Lisp Manual says, "Most of the kill commands are primarily for
interactive use [...] When you need to delete text for internal
purposes within a Lisp function, you should normally use deletion
functions, so as not to disturb the kill ring contents."

The following mimic the `kill-' commands but without disturbing the kill ring.

Delete region

The Lisp equivalent of `kill-region' (`C-w') but without kill ring side effects::

(delete-region (region-beginning)(region-end))

According to the ElispManual, "Few programs need to use the
`region-beginning' and `region-end' functions." This is because
Lisp code should not rely on nor "alter the mark unless altering
the mark is part of the user-level functionality of the
command. (And, in that case, this effect should be documented.)
To remember a location for internal use in the Lisp program,
store it in a Lisp variable. For example: [...]"

(let((beg (point)))(forward-line 1)(delete-region beg (point)))

Delete line

The equivalent of `kill-line' (`C-k') but without kill ring side effects:

Delete word

The equivalent of `kill-word' (`M-d') but without kill ring side effects:

(let((beg (point)))(forward-word 1)(delete-region beg (point)))

Alternatively, replacing the `let' with `save-excursion'.

(delete-region (point)(save-excursion
(forward-word 1)(point)))

Delete sentence

The equivalent of `kill-sentence' (`M-k') but without kill ring side effects:

(let((beg (point)))(forward-sentence 1)(delete-region beg (point)))

Alternatively, replacing the `let' with `save-excursion'.

(delete-region (point)(save-excursion
(forward-sentence 1)(point)))

Search and Replace

Searching and replacing text is a fundamental editing need. Emacs has separate
facilities for both interactive and scripted search and replace.

Interactive Use

The replace-regexp function provides a way to replace text interactively.
This function supports embedded emacs lisp statements in the second arguement
(the replacement expression). By default replace-regexp replaces
every 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

M-x replace-regexp RET \([A-Z]\) RET \,(downcase \1)

on the marked region AABBCC will convert it to aabbcc. The first
arguement is a regular expression matching any capital letter and saving it
as 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, the results
are implicitly saved in to match-string. This short function replaces
every pattern in a marked region with a new string drawn from its components:

Note how `cons' adds an element to the front of the list,
so that usually the list has to be reversed after the loop.
`nreverse' is particularly efficient because it does this
destructively by swiveling pointers around. See
DestructiveOperations for more about this.

Copying:

Use `copy-sequence' to make a copy of a list that won't change the
elements of the original.

Emacs Lisp doesn't come with a `filter' function to keep elements that satisfy a conditional and excise the elements that do not satisfy it. One can use `mapcar' to iterate over a list with a conditional, and then use `delq' to remove the `nil' values.

Files

Read

Processing a file is usually done with a temporary buffer:

(defun process-file (file)"Read the contents of a file into a temp buffer and then do
something there."(when(file-readable-p file)(with-temp-buffer
(insert-file-contents file)(goto-char (point-min))(while (not(eobp));; do something here with buffer content(forward-line)))))

On the chance that a buffer may already be actively visiting the file,
consider using `find-file-noselect'

(defun file-string (file)"Read the contents of a file and return as a string."(with-current-buffer (find-file-noselect file)(buffer-string)))

Write

To write something to a file you can create a temporary buffer, insert the things to write there and write the buffer contents to a file. The following example read a string and a filename (with completion, but doesn't need to exist, see InteractiveCodeChar F) and write the string to that file.

Path splitting

Splitting the path can be done with `split-string' and with the slash. Previously, Emacs would determine the character separating directory names with `directory-sep-char'. However, the variable is obselete with Emacs 21.1.