I am a freshman with respect to learning emacs lisp and I'm therefore reading the emacs lisp manual. When i read 6.6 Char-Tables, I was confused about how to use the char-table. In that chapter, I can't find a good example to use char-table.
Of course, elisp has the syntax-table which consists of the char-table and syntax-table can do a lot of things.
I want to know how to make a good use of char-table and in which cases I would use it.
Thanks
I'm not very familiar with Emacs Lisp programming, but alone from the description of the data structure I was immediately thinking of keymaps. And, as it turns out, I was right.
Especially the use of "parent" char-tables makes sense in this context: For example, when activating a mode, that mode probably has a keymap with the keybindings it provides. Before setting the current keymap to the mode's keymap, the current keymap is probably made the parent of the mode's keymap. That way, when the mode's keymap does not provide a mapping, the previous keymap(s) can be queried for an appropriate mapping.
Here's how I'd implement char-tables in C, as a rough sketch:
struct char_table {
void * data[256 /* or how many char codes there are */];
struct char_table * parent;
};
void * lookup(
struct char_table * table,
char /* or a more suitable type for char codes */ key) {
void * result = table.data[key /* plus possibly some calculation */];
if (!result && table.parent) {
result = lookup(table.parent, key);
}
return result;
}
Emacs's char-tables are simply a special kind of array that's indexed by characters rather than by integers. There are many characters in Unicode, so it's worth having a special implementation of such an array where many ranges of characters share the same value.
Related
I am using C++ mode to write some code.
I found that for some function without a return type, the indentation is wrongly done (indented to start of line). One frequent case is the constructor for class type, e.g.
class Person {
public:
Person(const std::string &s): name(s) { }
Person(const std::string &s0, const std::string &s1): name(s0), address(s1) { }
private:
std::string name = "Default_Name";
std::string address = "Default_Addr";
}
The colon before initializer list may also be a cause.
Question:
Is there away to fix this?
The formatting for comments is not very good so I'm answering here:
Yeah, I've been bitten by that before, too. Best solution depends on whether you think you will be writing a lot of C code,too. If you will be, then you can name your C++ headers with ".hh" at the end, so Emacs knows.
Or, you can set a file local variable that sets the mode: file local variables
Or, if you will not be writing a lot of C code, you can modify auto-mode-alist in your .emacs so that .h defaults to c++ mode:
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))
Where I work we use a lot of our own custom data types. The c++ that emacs recognizes, notices many of the custom datatypes from the STL such as string, vector, etc. This means that in my editor if i declare a function like so:
string getString() const {
return str;
}
The return value will be highlighted green and due to this the function name will in blue. Now if I decide to use a custom string this messes everything up. So now my cpp files are mostly in white because we don't use the normal stl classes here. How can I program my emacs editor to recognize that when I mean 'String', color it the same way you would as 'string'?
You need font-lock-add-keywords. Here's an example:
(font-lock-add-keywords 'cc-mode
'(("String" . font-lock-type-face)
("str" . font-lock-type-face)))
It adds a list of regular expression/font lock pairs.
There's much more to be read on the topic of adding-keywords.
In particular, ctypes.el might be of interest to you.
By default Emacs will not highlight constants, struct members, function calls etc (unless inside the definition). I am talking about C major mode here.
I want some basic highlighting, just based on text matching. For example, A word containing only upper case and underscore, [A-Z_]+, for example SOME_CONST, is a constant (unless otherwise highlighted). Similarly, I can match for [a-zA-Z_][a-zA-Z0-9_]\s( as function call; ->[a-zA-Z_][a-zA-Z0-9_]* as a struct member etc.
How can I do this emacs ?
I think the elisp function that you want is font-lock-add-keywords. I've added the following to my .emacs and gotten what I think you want for upper case words:
(font-lock-add-keywords 'c-mode '("\\<\[A-Z_\]\+\\>"))
You'd have to add a bit more to handle integer constants. Some of the documentation around this warns that if you're not intelligent about your regular expressions it can slow things down dramatically, and that you should use regexp-opt for matching multiple keywords.
The part that was a bit confusing for me is that the argument to font-lock-add-keywords can be a regular expression.
Is there any diff/merge tool for programming languages, that works in a syntax-aware way (like XML Diff Tool), doing more than compare line-by-line (and optionally ignoring whitespace).
I'm interested in a program actually following the language syntax and delimeters, suggesting changes without breaking syntactic correctness, or bundling statements separated over multiple lines. Example behavior would be:
*upon finding an if(){ which introduces an extra nesting level automatically bundle the closing brace } several lines below with it.)
*keep matching syntax elements together, avoid silliness like removing a block tends to create:
int function_A()
{
int ret;
ret = something;
ret += something_else;
return ret;
}
int function_B()
{
if(valid)
{
int ret;
ret = something;
ret += something_else;
return ret;
}
else return -1;
}
Personally, I'd love to find software capable of handling C++ syntax, but knowing about solutions for other languages would be interesting too.
Semantic Merge.
Languages supported, from the website:
We started with C# and Vb.net, then added Java. Now C is already supported and then we’ll focus on C++, Objective-C and JavaScript, depending on your
feedback
While KDiff3 does not compare syntax elements in a grammar context, it does have a higher granularity than "the whole line changed", and it will highlighting exactly what parts within a line that is changed.
And in my experience it has a very good algorithm for detecting changes. Given your example above, it correctly compares function_A and function_B out of the box:
And even so, should the algorithm fail to match what you want, for instance like the following:
you can always override manually by placing sync marks where you want to have it perform the comparision.
Alternative 1:
Alternative 2:
Sounds like you'd be interested in Bram Cohen's (BitTorrent creator) Patience Diff algorithm (which is used in the bazaar version control system).
See The diff problem has been solved and especially Patience Diff Advantages:
Excerpt from second link:
Another advantage of patience diff is that it frequently doesn't match lines which just plain shouldn't match. For example, if you've completely rewritten a section of code it shouldn't match up the blank lines in each version, as this example shows. Finally, there's this example:
void func1() {
x += 1
}
+void functhreehalves() {
+ x += 1.5
+}
+
void func2() {
x += 2
}
Which is straightforward and obvious, but frequently diff algorithms will interpret it like this:
void func1() {
x += 1
+}
+
+void functhreehalves() {
+ x += 1.5
}
void func2() {
x += 2
}
Beyond Compare does some of what you're asking. It doesn't maintain syntactical correctness or compare language blocks at a time, but it can do the following:
Some understanding of language syntax, so it can do syntax highlighting of compared files, and it can also recognize and optionally ignore unimportant differences (like comments, including multiline comments).
Support for using external conversion programs for loading and saving data. Out of the box, it supports using this to prettify XML and HTML before comparing it. You could set up GNU Indent to standardize syntax before comparing two C files.
Optional line weights to let you give a higher weight to matching, e.g., closing braces. I've not tried this feature.
Replacements, to ignore for a single session every place where old_variable_name on the left was replaced with new_variable_name on the right.
It's by far the best diff-and-merge tool that I've used. It's also cross platform, cheap ($30 for standard, $50 for pro), and has a very generous evaluation period, so it's worth a try.
See our SmartDifferencer tools.
SmartDifferencers are language specific, driven by production quality language parsers, build ASTs, and compare the trees. This makes them completely indepedent of text layout and intervening comments; remarkably, they are immune to changes in the text of literals (radix, move decimal point+change exponent, different escape sequences) if the actual value represented by the literal isn't different. The result is reported in language syntax terms, and plausible editing actions (move, copy, insert, delete, rename-identifier-within-block).
There are versions for C#, Java, C++, Python, and a variety of other languages. There are examples of each of these at the website.
A SmartDifferencer exists for C, but parsing C files without the full compiler command line is sometimes problematic, so sometimes it fails and you have to fall back to more primitive compare tools, like diff. We are working to improve this situation.
Please look at Compare++.
It can do language-aware structured comparison for C/C++, Java, C#, Javascript, CSS, ...
and Optionally ignore comment, pure formatted, white-space and case changes and have unique ability to align moved sections such as C++ function, Java namespace, C# method, CSS selector, ...
If you are using eclipse, the integrated compare editor provides syntax aware diff/merge, at least for Java. Check "Open Structure Compare automatically" under the "General/Compare/Patch" preferences, then choose "Java Structure Compare" in the compare editor.
Look at https://en.wikipedia.org/wiki/Comparison_of_file_comparison_tools especially column Structured comparison.
Currently there are only two tools who understand language structure.
Compare++ (Works great for C++)
Pretty Diff (Language aware code comparison tool for several web based languages. It also beautifies, minifies, and a few other things..)
Unfortunately many tools have this column still empty.
I have been using Emacs for more than three years now but it still takes me days to write even small functions in Lisp. I've looked through GNU Emacs Lisp Reference Manual but it's huge and structured completely opposite from JavaDoc, not from functions to descriptions but the other way around.
What will make my life much easier is some sort of small JavaDoc like document with most commonly used Emacs internal functions and they quick description.
(point) - returns current position in buffer
(save-excursion (p)) - saves current position in buffer before
executing (p) and restores it afterward.
Does anyone know where I can find something like that?
Have you tried the build-in manual in emacs? Open any lisp buffer (or any buffer in lisp mode), move your point to any function or variable, and hit C-h f (for function) or C-h v (for variable). Emacs will give you a fairly concise description of the function/variable.
For example, the manual content for (save-excursion) is
save-excursion is a special form in `C source code'.
(save-excursion &rest BODY)
Save point, mark, and current buffer; execute BODY; restore those things.
Executes BODY just like `progn'.
The values of point, mark and the current buffer are restored
even in case of abnormal exit (throw or error).
The state of activation of the mark is also restored.
This construct does not save `deactivate-mark', and therefore
functions that change the buffer will still cause deactivation
of the mark at the end of the command. To prevent that, bind
`deactivate-mark' with `let'.
The good thing also is the build-in manual give you "link" to to the source code of the function and to other functions that might be related, which make it nice to browse around.
Of course you can't learn lisp this way, but for looking up documentation of function this is a nice starter. When you find the build-in manual not understandable (which sometimes does happen), then it's time for you to google the function ;)
This site has some emacs lisp summary information that may be useful: http://xahlee.org/emacs/elisp.html.
In particularly, check out these links on that page: Basic Text-editing Functions, Emacs Lisp Idioms and Batch Text Processing
The GNU Introduction to emacs lisp is certainly more approachable than the reference manual.
I would add a couple of things:
M-x apropos - searches functions and variables for whatever string you specify (e.g. directory). Note that this is slightly different than C-h a, which only finds interactive functions
find a similar piece of code and copy it - you can learn an awful lot about how to do things by looking at what's already done. If you have a particular function you want to see examples of, one good way is to visit the main lisp source directory in dired (e.g. d:/product/emacs/lisp or /usr/share/lib/emacs/lisp) and do % g which will grep through all files looking for whatever string you type. Open up that file and see what other people have done with it.
C-h f and C-h v - as someone else mentioned, you can open up source, position point over a function or variable and then get documentation on it.
Check out the Emacs wiki, which has a crap-load of Emacs lisp modules for you to peruse.
I think you are taking the wrong approach. When learning a
programming language and set of libraries (collectively, "Emacs
Lisp"), you need to approach it on both the micro and macro scale.
Before you can start writing software, you need to know what tools you
have available. That is what the Emacs Lisp manual aims to educate
you on. You really need to sit down and read the whole thing. That
way you know what features Emacs provides.
After you do that, you need "micro-level" information. There are
a number of sources that provide this. If you have a general idea of
what you need to do ("working with buffers"), then the Lisp reference
is a good place to figure out what you need to know. If you know that
there's a function that does what you want, but don't quite remember
the name, then M-x apropos (C-u C-h a) will help you search the
documentation. If you know what function you want to use, but don't
remember quite how it works, then M-x describe-function (C-h f)
will sort that out for you.
So anyway, the key is to learn Emacs Lisp, and then let Emacs help you
with the details. A list of functions isn't going to teach you much.
(Oh, one more thing -- you should familiarize yourself with Common
Lisp. Most Emacs libraries use cl, which are the useful CL
functions implemented in Emacs Lisp. loop, destructuring-bind,
defun*, and so on are all there, and they are very helpful.)
Good suggestions from others -- Emacs help system is your friend.
In addition:
http://www.emacswiki.org/emacs/EmacsNewbieWithIcicles
http://www.emacswiki.org/emacs/Icicles_-_Progressive_Completion
http://www.emacswiki.org/emacs/Icicles_-_Nutshell_View#ChippingAway
In order to understand what's on, quite often it's useful having a look at the source code.
http://repo.or.cz/w/elbb.git/blob/HEAD:/code/Go-to-Emacs-Lisp-Definition.el
Have you tryed <f1> f ? It is bound to describe-function. Example with point:
point is a built-in function in C source code.
(point)
Return value of point, as an integer.
Beginning of buffer is position (point-min).
[back]
Like most Lisp systeme, Emacs has an integrated documentation tool!
Any Lisp function or variable can declare an optional doc string.
Almost all standard command or function do declare a usefull doc string.
Emacs (like most Lisp systems) allows you to display the doc string of any function or variable (<f1> f and <f1> v) at any time.
When displayed, the doc string is browsable, meaning that you can click on symbols to see their doc string, or go to the source of the corresponding function or variable.
As soon as you evaluate any defun or defvar, its doc string is available through describe-function or describe-variable: this doc is alive!
M-x find-library RET <library name> is all you really need
If you're willing to fork over money for a dead tree, I'd recommend
(source: oreilly.com)
In XEmacs, and I believe in Emacs as well,
pressing C-h f, then the tab key for tab completion, which at that point is all functions, will give you a list of functions the editor knows about.
You just use cursor keys and scroll to one you want to know about and press enter to see details.
If a list of functions, with further info available, is what you want, that will give it to you.
This list is all currently available functions, so if you have packages of lisp installed, it shows the functions those packages supply as well as native functions. In my copy of XEmacs, today, I have 6,586 functions in the list. Emacs will be similar.
The problem is that not all functions have names that make them context-meaningful (ie not all menu variables/functions have the word menu in them, so you will miss some things if you go by names only.
You can use the INFO pages (on the menu) to view them more topically arranged, and get to the same usage information.
Download source code for Emacs. Go to src/ folder and type:
grep -r DEFUN *
You will get list of all primitive Lisp functions of Emacs.