Sunday, October 12, 2014

Save state of the folds: mkview

I have tried in the past to use fold, but found them to be cumbersome and unwieldy. Most problematic of all, was that the VIM doesn't save state of the fold: reopening the file with folds presents you with an useless screen showing you how many top level fold there are in the file. Real useless.

But as it turned out, VIM has a feature to preserve state of folds between sessions: views.

After some time googling, I have stumbled on this tip. The .vimrc's code is fairly trivial (when you know what to look for):


autocmd BufWinLeave *.* mkview
autocmd BufWinEnter *.* silent loadview 

Every time one closes a file,VIM would save under ~/.vim/view/ (default, see 'viewdir') the view information for the file. Every time one opens a file, VIM would try to restore the view information. And the state of the fold with it.

Friday, February 28, 2014

VIM got fork: Neovim

Heise.de reports (German) that VIM has got a fork. The name of the new fork is Neovim. The site at the moment has only two things: link to donation site and link to Github repo.

I do not approve of redundant forks. But IMHO VIM source code is in dire need of clean up.

Bram doesn't approve. Discussion on the vim_dev group.

In the end, we'll see in couple of years whether the people behind Neovim have what it takes.

Saturday, February 15, 2014

A trick for keywordprg

Scouting web for what's possible with git aliases, I have found a cool trick on how to combine/chain calls to several tools when the program allows to configure only one. The trick turned out to be not new, but more of the old, forgotten ones: define one-shot shell function.

Example. How to make perl's keyword program to look into both functions and module helps.

Old one, looking only for function's help:

au BufReadPost *.pl set keywordprg=perldoc\ -f

The trick in action:

au BufReadPost *.pl set keywordprg=f(){\ perldoc\ -f\ $*\|\|perldoc\ $*;};f

Deescaped command looks like that: f(){ perldoc -f $* || perldoc $*; }; f

VIM would append the word to search for at the end and run it. Shell would see pretty normal function definition and immediately after a call to it.

The trick obviously works only on the platforms which have Bourne shell.

Friday, November 15, 2013

Highlight the locking primitives in the C/C++ code

A quick hack to highlight the locking calls in a C/C++ program:

:syn match cTodo '\<\i*[lL]ock\i*\>'

Reuses colors of the "TODO" and "XXX" items.

Edit1 I like this one better:


function! LockSyntaxFix()
        :syn match WarningMsg '\c\i*\(un\)\@<!\(lock\)\i*'
        :syn match MoreMsg '\c\i*unlock\i*'
endfunction

Red (WarningMsg) for lock, green (MoreMsg) for unlock. Trick is in the regexp which allows to match "lock" in all cases except "unlock". Packed into a function to be called when debugging locking.

Thursday, January 24, 2013

[off-topic] split/filter C++ class declarations from source into header file

Occasionally, I end up writing a simple one-file prototype for the whatever future C++ thingy I'm doing at the moment. Occasionally, the prototype ends up being pretty big and un-prototyle-like a really working program.

For many years now, the most painful part, if prototype was successful, was to make out of it something looking more like a real source code, suitable for integration with the rest of the project.

Namely: move class declarations to a header file. This often ends up being very very mundane task, especially if the prototype was to test some elaborate data model.

Literally ages into the C++ and ages into the *NIX, it downed on me that sed has a trick for it. If source is properly formatted and the main problem is the move of class declarations, then sed's /begin_re/,/end_re/ should work fine. And it does indeed.

$ sed '/^\(struct\|class\)/,/^}/p; d' < prototype.cc > realthing.h
$ sed '/^\(struct\|class\)/,/^}/d;' < prototype.cc > realthing.cc


Basically, sed is told to take from the input the blocks surrounded by struct or class and closing curly bracket (both in first column). For the header we 'p'rint the blocks and 'd'elete the rest, while for the source file we do the opposite (print is the default action of sed, thus no 'p' in the second command). The test with diff should show no differences (I don't like losing lines in the un-checked-in source so I always double check):

$ diff -wu <(sort < prototype.cc) \
     <(cat realthing.h realthing.cc | sort) | less


P.S. That of course does nothing to typedefs and forward declarations, but those are peanuts compared to the declaration of classes. And anyway, I try to put both typedefs and forward declarations into a struct or class just to give them common namespace prefix.

Thursday, January 17, 2013

Few tricks for the GVIM diff mode

Couple of useful tricks for the GVIM diff mode (~/.gvimrc):

- Equalize the diff file window sizes when resizing the GVIM window. That is probably applicable to console vim too, but I generally diff with the GUI vim. (The "Trailing characters" error from :exec occasionally drives me up the wall. But I have finally found out how to properly trigger keyboard shortcut!)

- Toggle font size very very small/very small and small on F5 (font names below are for *nix).

The Doom1-styled toggles I wanted to try already for a long time. In VIM the need for toggles isn't that high since the boolean parameters can be already toggled (:set hls! or :set wrap! for example). I found no other (readable) way to implement them but with functions - one function per state of toggle. A function which is part part of the toggle group does two things: first it sets parameter(s) it needs to set, then it map the toggle shortcut to the next function in the toggle group. To "initialize" the toggle group, simply call one of the functions.


if &diff
        " equalize size of diffed file windows
        au VimResized * :execute "normal! \<C-W>="

        " toggle diff font
        function! DiffSmallFont1()
                set guifont=-misc-fixed-medium-r-normal--9-90-75-75-c-60-iso10646-1
                map :call DiffSmallFont2()<CR>
        endfunction
        function! DiffSmallFont2()
                set guifont=-misc-fixed-medium-r-normal--10-100-75-75-c-60-iso10646-1
                map <F5> :call DiffSmallFont3()<CR>
        endfunction
        function! DiffSmallFont3()
                set guifont=-misc-fixed-medium-r-normal--13-120-75-75-c-70-iso10646-1
                map <F5> :call DiffSmallFont1()<CR>
        endfunction
        call DiffSmallFont2()
endif




Monday, December 03, 2012

[off-topic] Perl or PCRE: sort strings with numbers

A little trick with regular expressions (if backtracking is supported) on how to compare two strings which might include number.

The trick is to join the strings with NUL character (never occurring in human readable strings anyway) and use it as an anchor to find the longest common sub-string, in both strings followed by a number. And then compare the numbers.

#!/usr/bin/env perl
use strictuse warnings;

sub cmp_str_with_numbers
{
        #my ($a, $b) = @_;
        warn $a."<=>".$b;
        my $s = $a."\x00".$b;
        if ($s =~ m/^(.*)(\d+).*?\x00\1(\d+)/) {
                if ($2 != $3) {
                        return $2 <=> $3;
                }
        }
        return $a cmp $b;
}

my @test1 = (
        'Test 2 ccc',
        'Test 1 aaa 1',
        'Test 1 aaa 10',
        'Test 1 aaa 2',
        'Test 10 bbb',
);

my @out0 = sort @test1;
my @out1 = sort cmp_str_with_numbers @test1;
print "original:\n";
print "\t$_\n" for @test1;
print "normal sort:\n";
print "\t$_\n" for @out0;
print "number-aware sort:\n";
print "\t$_\n" for @out1;

Output:

original:
        Test 2 ccc
        Test 1 aaa 1
        Test 1 aaa 10
        Test 1 aaa 2
        Test 10 bbb
normal sort:
        Test 1 aaa 1
        Test 1 aaa 10
        Test 1 aaa 2
        Test 10 bbb
        Test 2 ccc
number-aware sort:
        Test 1 aaa 1
        Test 1 aaa 2
        Test 1 aaa 10
        Test 2 ccc
        Test 10 bbb

Saturday, December 01, 2012

Regex to match the word under cursor

The VIM-specific regex below matches the word under cursor. (Pasting unmodified as it is in my vimrc to also match German letters.)

/[a-zA-Z0-9ßÄÜÖäüö]*\%#[a-zA-Z0-9ßÄÜÖäüö]*

Documentation is under ':h /\%#'

Example usage: enclose the word under cursor in 'em' tag. Best experience if that is triggered on a keyboard shortcut.

:s![a-zA-Z0-9ßÄÜÖäüö]*\%#[a-zA-Z0-9ßÄÜÖäüö]*!<em>\0</em>!

Negative side-effect: causes fancy behavior of a seemingly random word to be highlighted when 'set hls' is in effect.

Search for a misspelled word

Alternative 1:

/The\S\+les\(Themistokles\)\@<!

Alternative 2:

/\(Themistokles\)\@!\(\<The\S\+les\>\)

Both search for any word which starts with 'The' and ends with 'les', but is not 'Themistokles'.

[link] Wrap a visual selection in an HTML tag

Wrap a visual selection in an HTML tag.

Pretty useful function. I have only slightly modified it to take the tag as parameter and insert the tag on the line before/after selection. And hooked it on a keyboard shortcut.

[ The '^M' below should be converted there into real ^M (typed as ^V^M). ]

" Wrap visual selection in an HTML tag.
vmap <C-q> <Esc>:call VisualHTMLTagWrap('cite')<CR>
vmap <C-T> <Esc>:call VisualHTMLTagWrap('title')<CR>
function! VisualHTMLTagWrap(tag)
 normal `>
 if &selection == 'exclusive'
  exe "normal i^M</".a:tag.">"
 else
  exe "normal a^M</".a:tag.">"
 endif
 normal `<
 exe "normal i<".a:tag.">^M"
 normal `>
 normal j
endfunction