I'm trying to write a macro that will allow me to surround currently highlighted lines of text with an #ifdef. Ideally with the cursor placed after the #ifdef to be ready to enter the macro name. I'm able to record to create a macro, but I'm only able to do it for one line of code.
Before:
bool first_selected_line = false;
int second_selected_line = 0;
After:
#ifdef // if possible, cursor placed here in insert mode
bool first_selected_line = false;
int second_selected_line = 0;
#else
bool first_selected_line = false;
int second_selected_line = 0;
#endif
Any ideas?
You could do something along the lines of:
qjc#ifdef<esc>magpO#else<esc>gpO#endif<esc>`aq
Basically:
Start recording qj
Delete what you selected and go into insertmode c
Type your construct, pasting your code back as necessary
You put a mark (ma) just after typing #ifdef and jump back to it at the end
Repeat the macro with #j
Hope this example helps!
I would probably use snipmate or some other plugin to accomplish this task. There are couple ways to go about it manually though. Here's my solution for a macro:
Visually select the text then...
qqc#ifdef
<C-r><C-o>"
#else
<C-r><C-o>"
#endif<esc>'[A<C-o>q
You also don't have to visually select the text at all if you don't want to. Use the same macro but start with qqcj instead.
Related
I am trying to create a calculator program similar to what Windows has, and I am currently having trouble with making a backspace key. It should, when clicked, remove the last character from the textBox Text field.
Here what I was thinking would work:
private: System::Void button14_Click(System::Object^ sender, System::EventArgs^ e) {
textBox1->Text[textBox1->TextLength] = ""; }
The error list looks like this:
I have also seen suggestions to use pop_back(), remove() and erase() on the Internet, but I don't have an idea how to use them.
Strings are immutable... you can't technically change the string.
IDK how to write this in the weird CLI C++ syntax, but something close to:
String^ s = textBox1->Text;
s = s^.Substring(0, s.Length - 1); // original s preserved, but no longer used
textBox1->Text = s;
IDK if it would allow something like, but if it does, I'd prefer it:
auto s = textBox1->Text;
s = s^.Substring(0, s.Length - 1); // original s preserved, but no longer used
textBox1->Text = s;
I want to extract text from pdf and relayout it.
My code is the following:
BOOL CTextEditorDoc::loadTxt()
{
if(m_strPDFPath.IsEmpty())
return FALSE;
#ifdef _DEBUG
DWORD dwTick = GetTickCount();
CString strLog;
#endif
CString strFile;
fz_context *ctx;
fz_document* doc;
fz_matrix ctm;
fz_page *page;
fz_device *dev;
fz_text_page *text;
fz_text_sheet *sheet;
int i,line,rotation,pagecount;
if(!gb2312toutf8(m_strPDFPath,strFile))
return FALSE;
ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
fz_try(ctx){
doc = fz_open_document(ctx, strFile.GetBuffer(0));
}fz_catch(ctx){
fz_free_context(ctx);
return FALSE;
}
line = 0;
rotation = 0;
pagecount = 0;
pagecount = fz_count_pages(doc);
fz_rotate(&ctm, rotation);
fz_pre_scale(&ctm,1.0f,1.0f);
sheet = fz_new_text_sheet(ctx);
for(i=0;i<pagecount;i++){
page = fz_load_page(doc,i);
text = fz_new_text_page(ctx);
dev = fz_new_text_device(ctx, sheet, text);
#ifdef _DEBUG
dwTick = GetTickCount();
#endif
fz_run_page(doc, page, dev, &ctm, NULL);
#ifdef _DEBUG
strLog.Format("run page:%d ms\n",GetTickCount() - dwTick);
OutputDebugString(strLog);
dwTick = GetTickCount();
#endif
//m_linesInfoVector.push_back(line);
print_text_page(ctx,m_strContent,text,line);
#ifdef _DEBUG
strLog.Format("print text:%d ms\n",GetTickCount() - dwTick);
OutputDebugString(strLog);
dwTick = GetTickCount();
#endif
fz_free_device(dev);
fz_free_text_page(ctx,text);
fz_free_page(doc, page);
}
fz_free_text_sheet(ctx,sheet);
fz_close_document(doc);
fz_free_context(ctx);
return TRUE;
}
This code can extract all the text of pdf but it may be too slow. How to improve it?
Most of time is spent in function fz_run_page. Maybe just to extract text from pdf, I don't need to execute fz_run_page?
At a quick glance your code looks fine.
To extract text from a PDF you need to interpret the PDF operator streams. fz_run_page does this. It results in calls to whatever device you specify - in this case the structured text extraction device. This collates the randomly positioned glyphs from all over the page into a more structure form of words/lines/paragraphs/columns etc.
So, in short you're doing the right thing.
There are no current user servicable ways to improve the speed of this. It is possible that we could maybe use a device hint to avoid reading images etc in future versions. I will ponder on this and discuss it with the other devs. But for now you're doing the right thing.
HTH.
No, the fz_run_page call is needed. You need to interpret the pages of the document to pull out the text, and that is what fz_run_page does.
Possibly you could create a simpler text device that avoided keeping track of the character positions, but I doubt that that would make an real difference to performance.
For example --
when i do gg=G on
int main()
{
return 0;
}
it will change it to
int main()
{
return 0;
}
What I want is --
int main(){
return 0;
}
The '{' should be on the funciton prototype line
AFAIK:
= re-adjusts indent, it doesn't reformat your codes' style. e.g, the code block style (your question); or add/removing empty lines; add/remove spaces e.g. a=2 -> a = 2 ...
you could do this to change the { before/after you gg=G:
:%s/)\n\s*{\s*$/) {/g
you could also write them into one line, and make a mapping to do it in one short.
e.g, this line:
:%s/)\n\s*{\s*$/) {/g|norm! gg=G
will turn:
int main()
{
if(foo)
{
return 1;
}
if(a>0)
return a;
for(int i=1;i<20;i++)
{
int foo=0;
foo=i;
}
return 0;
}
into
int main() {
if(foo) {
return 1;
}
if(a>0)
return a;
for(int i=1;i<20;i++) {
int foo=0;
foo=i;
}
return 0;
}
EDIT
My original answer suggested :g/)$/j to "join" the two lines, but I found it is not safe, for example:
if (a>0)
return a;
will be turned into
if (a>0) return a;
which is not expected by OP.
To go along with Cubic's Answer
To use astyle without modifying file you can use the command gq and the option `formatprg'
formatprg specifies an external program that will be used to format the buffer. After the command has been run the buffer will be replaced by the output of the program.
For exmample: To set this to work with c files you can put the following in your vimdc
autocmd FileType *.c set formatprg=astyle\ --style=kr
Note: the \ allows you to pass the different command line options to style.
Now to use this in your file you can type gggqG to apply the formatting to the whole file.
You could use astyle, with something like
nnoremap <A-S-f> :w<CR>:!astyle % --style=java<CR>:edit<CR>
Which binds it to Alt-Shift-f (note that this saves/reloads the file which may not always be what you want, there are ways around that but I didn't want to go too much into this right now).
Of course, you'll have to figure out what options to pass to astyle for your preferred formatting yourself.
I am using a custom keymap using langmap option in vimrc.
I am trying to use snipmate but I am running into trouble. When I type a word and hit tab it allows me to edit the parameter. The problem is that the first character is the remapped one, while I want it to be the actual key.
For instance, I'll type this:
for
and hit tab to expand the snippet:
for (i = 0; i < COUNT; ++i)
The i is highlighted which means I can edit it. I type "aaa":
for (baa = 0; i < COUNT; ++i)
It comes out baa even though I typed aaa. This is because I remapped a and b.
How can I fix this?
Here is my keymapping:
set langmap=nj,N},ek,E{,il,IL,{^,}$,lb,LB,uw,UW,ye,YE,jg,JG,\\;z,f\\.,F\\,,zu,ZU,.?,\\,/,/v,? V,ta,TA,si,SI,ro,RO,ac,AC,wr,WR,xx,XX,dd,DD,bs,BS,gf,GF,pt,PT,kn,KN,cy,CY,vp,VP,o\\;
It won't make much sense to others, and I haven't finalized how I want it to look.
From your :set langmap I understand that you mapped a to c so, by typing aaa, did you expect to obtain ccc?
From what I understand (:help langmap), your custom substitutions are not available in INSERT mode for actually inserting stuff and I don't see a mention of the SELECT mode you are in when overwriting SnipMate's placeholders.
If I do this
:set langmap+=ac,bs
and I type aaa in SELECT mode, I obtain caa.
That's because langmap applies to the first a (:help Select-mode) and, therefore inserts c. But, after this first character I am in INSERT mode for all subsequent characters. Since langmap doesn't apply in INSERT mode, aa is inserted as is.
What is not clear to me is why you obtain baa instead of caa. Your langmap seems to be pretty clear about your intention: you want a to insert c and b to insert s. Typing a shouldn't insert b.
I smell a risk of mistyping in your .vimrc. Try this: reset your set langmap and start adding your mappings one by one.
May I ask you what is the purpose of such a massive remapping?
C program which outputs mappings similar behavior to langmap but not for select:
/* input:
lhs rhs optional-descripton
lhs rhs ...
*/
#include <stdlib.h>
#include <stdio.h>
int main() {
FILE *fi = fopen("in.txt", "r");
FILE *fo = fopen("out.txt", "w");
char lc[8], rc[8];
while (fscanf(fi, "\n%s %s", lc, rc) != EOF) {
fprintf(fo, "nnoremap %s %s\n", lc, rc);
fprintf(fo, "xnoremap %s %s\n", lc, rc);
fprintf(fo, "onoremap %s %s\n", lc, rc);
while (fgetc(fi) != '\n');
}
fclose(fo);
fclose(fi);
}
It doesn't work identically to langmap and so it might break other bindings.
This has now been fixed in vim 7.4.1150. See
https://github.com/vim/vim/issues/572
for details.
Say I have the current text in the buffer, where _ marks the cursor
int main(int argc, char **argv) {
printf("Hello, world!\n");
_
}
I have indentexpr on (though a solution with cindent or autoindent will probably work, too).
How do I begin inserting so my cursor is placed at the appropriate column to follow the indention rules, i.e.:
int main(int argc, char **argv) {
printf("Hello, world!\n");
_
}
Currently I find myself using ddO often (or ddo at the end of the buffer), but it seems there should be a better way. Using == or even >> or v> do not seem to work because the line is blank.
Try going back into normal mode and typing S
If I'm on a blank line, but at the wrong insertion point, I tend to use CTRL-f (while in insert mode) to indent to the correct place.
This is useful when I've hit ESC to get out of insert mode, and I've then lost the proper indentation. Hitting i followed by CTRL-f does the trick.