I'm having no luck getting Emacs (cc-mode) to indent multiline template arguments. Here's an example line:
typedef ::boost::zip_iterator< ::boost::tuple<
vector<int>::const_iterator, vector<float>::const_iterator > >;
I'd like the second line to be indented, as like in a function. It is indented, until I enter the second-to-last >, at which point the second line up moves to the left to align with the typedef.
When I start typing the second line, the syntactic analysis is ((statement-cont 52)), until the second-to-last >, at which point it becomes ((defun-block-intro 46)). Deleting the character doesn't return to the old syntactic analysis.
I expected to have template-args-cont as the syntactic analysis.
I'm using the emacs 22.2 (ubuntu intrepid) and cc-mode version 5.31.5 that came with it.
You should just need to set template-args-cont to some useful value. To experiment with it, put your cursor on the second line and enter C-cC-o for c-set-offset. Insert a convenient value. With 4, I get:
typedef ::boost::zip_iterator< ::boost::tuple<
vector<int>::const_iterator, vector<float>::const_iterator > >;
If that doesn't work, check your version: I have cc-mode version 5.31.6. To check, do M-x c-version. I get
Using CC Mode version 5.31.6
Related
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.
I am slightly confused by the difference between keys and key-bindings in emacs after reading the first chapter of "Writing GNU Emacs Extensions" (O'Reilly book).
The chapter starts by noting that in some emacs version the keys backspace and DEL, invoke the help menu instead of deleting. In other words, they invoke what is invoked by C-h.
The chapter then goes on to show how to change this behaviour using Lisp code. This is done by binding the keystroke C-h to the delete command.
This is a bit confusing to me. In my mind DEL, Backspace and C-h are three different keystrokes (the first two consisting of a single key).
Why does remapping C-h effect what DEL and Backspace does?
I would have thought to change what backspace does for example, you would remap backspace to another command, not the keystroke C-h.
Unless remapped by a low-level keybord driver, the effect of the Backspace key is to send the character with numeric code 8, which, in certain operating systems, is exactly the same code generated by pressing Control-h. You can verify this fact by simply writing anything on a unix (or linux) shell and then pressing Backspace and Control-h: both of them have the effect of erasing the previous character, since the character with numeric code 8 is interpreted by the operating system as the control character BS, backspace, used either to erase the last character printed or to overprint it (see wikipedia). Analogously, Control-J is equivalent to the RETURN key, etc.
The same Wikipedia page describe DEL as “originally intended to be an ignored character, but now used in some systems to erase a character”.
So, when you assign a command to a keystroke you are actually assigning a command to a character code, and if two or more keys generate the same code, by pressing them you are invoking the same command.
#Renzo answered your question about how these keys are related and how binding one can seem to affect another. Here is some more information about what's going on in this particular case.
See the Emacs manual, node DEL Does Not Delete.
There you will see this, following an explanation of the problem/confusion:
To fix the problem in every Emacs session, put one of the following
lines into your initialization file (*note Init File::). For the first
case above, where BACKSPACE deletes forwards instead of backwards, use
this line to make BACKSPACE act as DEL:
(normal-erase-is-backspace-mode 0)
For the other two cases, use this line:
(normal-erase-is-backspace-mode 1)
Another way to fix the problem for every Emacs session is to
customize the variable normal-erase-is-backspace: the value t
specifies the mode where BS or BACKSPACE is DEL, and nil
specifies the other mode. *Note Easy Customization::.
See also the GNU Emacs FAQ question about Backspace invoking help. There you will see, in addition to information similar to that above, information about how to remap DEL on UNIX - use this:
stty erase '^?'
Wrt C-j and RET (not mentioned in the question, but mentioned in #Renzo's answer): The default behavior of Emacs changed in most programming modes, in Emacs 24.4.
Starting with that release, electric--indent-mode is turned on by default, which means that RET inserts a newline character and indents, whereas C-j just inserts a newline character. Prior to that release, these keys had the opposite behaviors. If you prefer the old behavior then do this, to turn off electric-indent-mode:
(when (fboundp 'electric-indent-mode)
(electric-indent-mode -1)) ; Use classic `C-j' and `RET'.
I have been playing around with vim macros lately (in MacVim at the moment), and sometimes I like to explicitly assign a macro into a register using e.g. :let #a='(macro keystrokes)'. This is generally working fine, but I found a weird behavior in which anytime I assign a string value that ends in Carriage Return / ^M, vim automatically adds a Linefeed / ^J to the end before putting it in the register, which affects the execution of the macro!
Example: Let's say I record a simple macro that gets into insert mode, types "hey", escapes out of insert mode, and then hits Enter twice to go down 2 lines. I record this into register #a by typing qaihey<Escape><Enter><Enter>q, which stores the following in #a:
ihey^[^M^M
So far so good, and executing the macro by typing #a does just what it's supposed to. Another perfectly ok way I can get this same macro into register #a would be by typing the whole thing (ihey<Ctrl-V><Escape><Ctrl-V><Enter><Ctrl-V><Enter>) into a buffer and then yanking it with "ay -- the end result is just the same. But here's the weird thing -- suppose I just wanted to assign that string directly into #a using a let statement:
:let #a='ihey^[^M^M'
Now if I type :reg to look at the value it says there's an extra ^J on the end for some reason:
"8 ...
"9 ...
"a ihey^[^M^M^J
"b ...
Having the extra ^J causes it to go down an extra line when I execute the macro, so it's actually changing the behavior.
Anybody know why this extra character is being added? Anyone know how I can get a string value ending with ^M into a register (or any variable), without having an extra ^J get added?
Some quick checking the vim help files says this:
:let #{reg-name} = {expr1} *:let-register* *:let-#*
...
If the result of {expr1} ends in a <CR> or <NL>, the
register will be linewise, otherwise it will be set to
characterwise.
In other words, ending the string with a carriage return will make Vim interpret it as a line ending
Quick googling give this from the VimTips wiki:
Note however, that the above method using :let will not work as expected for any macros which you make ending in a < CR > or < NL > character (carriage return or newline). This is because, as documented in :help :let-#, Vim will treat the register as "linewise" under these conditions. The reason for this is to make registers set with :let act "the right way" when dealing with yanked/deleted text, but it can cause headaches when dealing with recorded macros. Possible workarounds include using the setreg() function or adding "no-op" commands to the end of the macro, such as a < ESC >. See the discussion on vim_dev about unexpected behavior of the :let command for details.
So you have a few options: use setreg() or add some kind of no-op sequence (< ESC >) to the string when you use let.
In order to insert special keys easily using let, just use double quotes:
:let #a="ihey\<Esc>\<Return>\<Return>"
If you use single quotes vim will insert:
\<Esc>\<Return>\<Return> instead of their functions
using vim + janus (https://github.com/carlhuda/janus)
is enabled by default in .vim/vimrc
filetype plugin indent on
i'm writing perl, every newline is indented by default.
how i can select a block (or the full document) and
reindent automatically?
You can select the block by going to visual mode (press v or Shift + v or Ctrl + v in normal mode). Reindent can be done pressing = after selection of the block.
To apply the same formatting to the whole document you can use:
gg=G which means "go to the beginning", "reformat" until "end of document".
You can use :help = to get more information on reformatting.
If your block is a paragraph separated by empty line you, can use vip to select it quickly.
See :help text-objects for more information.
Since you're asking about perl I'm assuming "blocks" are identified by { and }. If that's the case, I'd suggest indenting the block by using one of the following from somewhere inside the block:
>i{ to forcibly indent all lines by one shiftwidth
=i{ to reindent the block per the indent rules for perl
I personally prefer the latter, but if you have some non-standard indentation you don't want to screw up inside the block then you'd want to use the prior.
This takes advantage of vim's "Block" text objects to operate on only the desired text.
At my current job, we have coding-style standards that are different from the ones I normally follow. Fortunately, we have a canned RC file for perltidy that I can apply to reformat files before I submit them to our review process.
I have code for emacs that I use to run a command over a buffer and replace the buffer with the output, which I have adapted for this. But I sometimes alternate between emacs and vim, and would like to have the same capabilities there. I'm sure that this or something similar is simple and had been done and re-done many times over. But I've not had much luck finding any examples of vim-script that seem to do what I need. Which is, in essence, to be able to hit a key combo (like Ctrl-F6, what I use in emacs) and have the buffer be reformatted in-place by perltidy. While I'm a comfortable vim-user, I'm completely clueless at writing this sort of thing for vim.
After trying #hobbs answer I noticed that when filtering the entire buffer through perltidy the cursor returned to byte 1, and I had to make a mental note of the original line number so I could go back after :Tidy completed.
So building on #hobbs' and #Ignacio's answers, I added the following to my .vimrc:
"define :Tidy command to run perltidy on visual selection || entire buffer"
command -range=% -nargs=* Tidy <line1>,<line2>!perltidy
"run :Tidy on entire buffer and return cursor to (approximate) original position"
fun DoTidy()
let l = line(".")
let c = col(".")
:Tidy
call cursor(l, c)
endfun
"shortcut for normal mode to run on entire buffer then return to current line"
au Filetype perl nmap <F2> :call DoTidy()<CR>
"shortcut for visual mode to run on the current visual selection"
au Filetype perl vmap <F2> :Tidy<CR>
(closing " added to comments for SO syntax highlighting purposes (not required, but valid vim syntax))
DoTidy() will return the cursor to its original position plus or minus at most X bytes, where X is the number of bytes added/removed by perltidy relative to the original cursor position. But this is fairly trivial as long as you keep things tidy :).
[Vim version: 7.2]
EDIT: Updated DoTidy() to incorporate #mikew's comment for readability and for compatibility with Vim 7.0
My tidy command:
command -range=% -nargs=* Tidy <line1>,<line2>!
\perltidy (your default options go here) <args>
If you use a visual selection or provide a range then it will tidy the selected range, otherwise it will use the whole file. You can put a set of default options (if you have any) at the point where I wrote (your default options go here), but any arguments that you provide to :Tidy will be appended to the perltidy commandline, overriding your defaults. (If you use a .perltidyrc you might not have default args -- that's fine -- but then again you might want to have a default like --profile=vim that sets up defaults only for when you're working in vim. Whatever works.)
The command to filter the entire buffer through an external program is:
:%!command
Put the following in ~/.vimrc to bind it to Ctrl-F6 in normal mode:
:nmap <C-F6> :%!command<CR>
For added fun:
:au Filetype perl nmap <C-F6> :%!command<CR>
This will only map the filter if editing a Perl file.
Taking hobbs' answer a step further, you can map that command to a shortcut key:
command -range=% -nargs=* Tidy <line1>,<line2>!perltidy -q
noremap <C-F6> :Tidy<CR>
And another step further: Only map the command when you're in a Perl buffer (since you probably wouldn't want to run perltidy on any other language):
autocmd BufRead,BufNewFile *.pl,*.plx,*.pm command! -range=% -nargs=* Tidy <line1>,<line2>!perltidy -q
autocmd BufRead,BufNewFile *.pl,*.plx,*.pm noremap <C-F6> :Tidy<CR>
Now you can press Ctrl-F6 without an active selection to format the whole file, or with an active selection to format just that section.
Instead of creating a new keyboard shortcut, how about replacing the meaning of the = command which is already in people's finger memory for indenting stuff? Yes, perlcritic does more than just indent but when you use perlcritic anyways, then you probably don't want to go back to the inferior "just indent" = command. So lets overwrite it!
filetype plugin indent on
autocmd FileType perl setlocal equalprg=perltidy
And now we can use = just like before but with the added functionality of perlcritic that goes beyond just indenting lines:
== run perlcritic on the current line
5== run perlcritic on five lines
=i{ Re-indent the 'inner block', i.e. the contents of the block
=a{ Re-indent 'a block', i.e. block and containing braces
=2a{ Re-indent '2 blocks', i.e. this block and containing block
gg=G run perlcritic on the entire buffer
And the best part is, that you don't have to learn any new shortcuts but can continue using the ones you already used with more power. :)
I'm used to select text using line oriented visual Shift+V and then I press : an I have !perltidy -pbp -et4 somewhere in history so I hit once or more up arrow ⇧.