Emacs: cursor position inside parenthesis - emacs

In Emacs, I want to achieve an IDE-like behaviour with parenthesis block and cursor position. That is, when I type, for example, int main() { RET, it should expand to
int main() {
I <- cursor position
}
I've installed smartparens plugin to automatically insert pairs, but it doesn't deal with the cursor:
int main() {
I <- cursor position}

Consider using Yasnippets, a template system for Emacs which comes with a lot of templates preinstalled. In your case:
mainTAB
will expand to:
int main(int argc, char *argv[])
{
CURSOR
return 0;
}

What you request is already the case in Emacs, starting with release 24.4.
You can enable the behavior by turning on electric-indent-mode, if it is not already enabled by default (Emacs 24.4 or later). Put this in your init file to enable the mode:
(when (fboundp 'electric-indent-mode) (electric-indent-mode 1))
However, you need at least Emacs release 24.1 to use electric-indent-mode. It is not available in older releases.

As #Drew pointed out electric-indent-mode, I've looked up different electric-modes in Emacs and figured out that electric-pair-mode fits exactly my needs.

Related

Emacs semicolon presses indent to width of entire previous line, declaring private class variables. How to fix & stop? (c-electric-semi&comma?)

Everything is indented normally in my .cpp file until the moment I press the semicolon ; on the following lines --- at which point emacs indents all the way to the full length of the last line typed...
This oddly doesn't happen if I remove the access modifier and declare vars int x and int y for any class or struct..
class Blah {
private int x;
private int y;
private int z;
};
If I highlight the whole field and press < TAB >, Emacs views this as the proper indent for the region. Can't seem to find anything else related on this besides other qs on indent customization
Additional details:
C-h k ; yields this description, so it might have to do with this feature ---- though I don't understand because the indentation described appears to refer to the immediate next newline not the current the cursor is on.
; runs the command c-electric-semi&comma (found in c++-mode-map),
which is an interactive compiled Lisp function in ‘cc-cmds.el’.
It is bound to ,, ;.
(c-electric-semi&comma ARG)
Insert a comma or semicolon.
If ‘c-electric-flag’ is non-nil, point isn’t inside a literal and a
numeric ARG hasn’t been supplied, the command performs several electric
actions:
(a) When the auto-newline feature is turned on (indicated by "/la" on
the mode line) a newline might be inserted. See the variable
‘c-hanging-semi&comma-criteria’ for how newline insertion is determined.
(b) Any auto-newlines are indented. The original line is also
reindented unless ‘c-syntactic-indentation’ is nil.
(c) If auto-newline is turned on, a comma following a brace list or a
semicolon following a defun might be cleaned up, depending on the
settings of ‘c-cleanup-list’.
; is one of many keys that triggers the "correct this line's indentation" command. There's nothing special about ; here, it's just that Emacs normally keeps your indentation right according to the style defined for the file.
As 0x5453 says in a comment, your C++ file is syntactically invalid, and the indenter is trying its best to come up with a reasonable indentation for this incorrect file. If you fix your code to be legal, the indentation will also be resolved.

How to make Emacs autofill code for c files

I'm completely new to Emacs and I'm looking for a way to make Emacs automatically write these lines of code for C files
#include <stdio.h>
int main()
{
return 0;
}
I'm sorry for messing up these lines, but I think you've got the point.
Yasnippet is perfect for re-usable snippets .
Have a look at the emacs wiki
Install using: M-x package-install yasnippet, or look on project on github.
Once installed, enable yasnippet-minor-mode with M-x yas-minor-mode-on (when your C file is open, the buffer's major mode is on c-mode and yas-minor-mode adds yas functionality.)
C-c&C-n will open a buffer for a snippet.
Give it a name and copy your code into the body, result should look like:
# key: c
# name: c_include
# --
#include <stdio.h>
int main()
{
return 0;
}
C-cC-c to close and save the buffer.
It will now appear with the name you gave it in the yasnippet menu when you call
C-c&C-s
Yasnippet has a lot more functionality. Read the doc.
Find C templates in AndreaCrotti yasnippet snippets Github repository

How can I define auto-indent and auto-pairing in .emacs file for emacs 24.3.1?

Just FYI, I am new to the .emacs file.
I would lik to set up my .emacs file to auto-indent and auto-pair a certain way to make writing code a little faster. I have found some info as to how to do these things independently but I'm not sure how to put it all together for the emacs version that I have. Ultimately, I would like to set up these definitions specific to which ever language I am coding in. Just to get me started I will use java as an example.
Obviously auto-pairing for ", (, ' are pretty straigforward. I would just like it to auto insert a closing ", ), ' and place the cursor in the middle.
For {, I would like it auto insert two newlines and the closing } whith the cursor in the middle.
Example
while (true) {
<--- cursor would be here with auto-indent of 2 spaces
}
I would also like this to work for nested curly braces which the appropriate indentation.
Example
while(true) {
if (...) {
}
}
Here is what I have so far in my .emacs file:
(defun java-autoindent ()
(when (and (eq major-mode 'java-mode) (looking-back "[{;]"))
(newline-and-indent)))
(add-hook 'post-self-insert-hook 'java-autoindent)
Obviously this just inserts a line and auto indents, but I also want the closed } to be included on the line below. I also tried using electric-pair but that didn't work.
My wish list may be a little unrealistic. I'm not even sure that this is possible, but I would be happy with the closest that I could get.
Any help to get me going in the right direction would be greatly appreciated.
Emacs defines modes for each type of language you code in. Some modes are derived from others and there is a mode called prog-mode which most programming modes are derived from.
The mode for a language is where things like indentation are defined because these tend to be language specific. The rules for indentation can be quite complicated, which is why people often use a mode with similar indentation style as the parent and derive a new mode from that.
Have a look at modes and derived mode in the emacs elisp manual.
With respect to adding matching/closing delimiters, have a look at electric-pair-mode (I think it was in emacs 24.4 - I'm running 25 and forget when it was introduced).
With respect to your requirement to enter some code, some newlines and position the cursor in a specific place, you probably want to look at one of emacs' template solutions. yasnippet is a popular choice and it is easy to define new templates in it. There are also many existing packaged yasnippet templates you can download/install. If you don't like yasnippet, google emacs template and have a look there are quitre a few frameworks.

emacs JavsScript distable auto new lines

I use emacs edit js files, when I enter a ;, it will auto jump a new line.
like this:
function test() {
var x = 1;
// <----- cursor auto in here !!!
}
How to disable it ?
It sounds like you are using electric-mode and have the auto-newline enabled.
Try evaluating this in a js buffer and see if it makes a difference (type M-: to eval):
(c-toggle-auto-newline 0)
If it does, just disable the electric mode in your setup or add that line to your js2-mode hook.

Auto completion keyword in Emacs

for example when I am typing int main() it would be nice if I could just hit tab and get the list of keywords (auto completion) I want. How can I do this in Emacs?
You can autocomplete with M-/. There are also other autocomplete packages available.
You need CEDET, it contains many good tools that can be used to make EMACS have many features that you would see in a more modern IDE.
You may use yasnippet. By writing 'main' and then TAB, you get
int main (int argc, char *argv[])
{
return 0;
}
(This is not really auto completion.)
Asher's answer with the list is probably the best but if you want something that gives you a drop down of possible (non semantic) completions which you can select, you can use auto complete mode.