guitou Foujanet schrieb:
> Hello ,
> 
> I have started to use VIM 1 month ago and I think I start to
> understand the philosophy of this editor.
> But I have a problem, I am sure that there is a solution. My problem
> is the following: I would like to increment a binary number. For
> example, I would like to increment 00000001 -> 00000010 -> 00000011
> and so on...
> 
> But the key Ctrl-A increment a decimal number, and I don't find the
> method to increment a binary number.
> 
> And after that, I would like to use a macro to increment a number from
> 2^0 to 2^8 for example.
> 
> Thank you,
> 
> Guitou

See attached plugin, it is a continuation of the
"bin2hex, hex2bin availble in VIM?" thread a few days ago.

"Help" is in the comments to the code.

If you're not used to folding, type  zi  to toggle folding on and off.

Source the script:
    :h :so

To enable it permanently, put it in the plugin folder ...

Default keys are <Leader>ba  and  <Leader>bx  to increment/decrement a
binary number, both accept a count.
    :h <Leader>


HTH,
Andy
-- 

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

" Vim plugin -- deal with binary numbers in the text
" File:         binary.vim
" Created:      2009 Jun 16
" Last Change:  2009 Jun 16
" Rev Days:     0
" Author:       Andy Wokula <[email protected]>


" Script Init Folklore: {{{
if exists("loaded_binary")
    finish
endif
let loaded_binary = 1

if v:version < 700
    echomsg "Binary: you need at least Vim 7.0"
    finish
endif
"}}}


" Config Variables:

" minimum length of the result: 1:byte, 2:word, 4:dword, ...
" g:binary_octets_min {{{
if !exists("g:binary_octets_min")
    let g:binary_octets_min = 1
endif
"}}}
" add "b" to the binary number in the text?
" g:binary_suffix {{{
if !exists("g:binary_suffix")
    let g:binary_suffix = "b"
    " if longer than one char, the test pattern in GetBinvac() needs a
    " change
endif
"}}}


" Mappings:

" Default Keys {{{
if !hasmapto("<Plug>IncBinary")
    nmap <unique> <Leader>ba <Plug>IncBinary
endif
if !hasmapto("<Plug>DecBinary")
    nmap <unique> <Leader>bx <Plug>DecBinary
endif
"}}}
" Interface Mappings {{{

" increase a binary number at the cursor
nnoremap <silent> <Plug>IncBinary
    \ :<C-U>call SetBinvac(GetBinvac()+v:count1)<CR>

" decrease a binary number at the cursor
nnoremap <silent> <Plug>DecBinary
    \ :<C-U>call SetBinvac(GetBinvac()-v:count1)<CR>
"}}}


" Functions:

" GetBinaryValueAtCursor(), returns a number
func! GetBinvac() "{{{
    let wordac = expand("<cword>")
    if wordac !~ '^[01]\+\D\=$'
        throw "Not a binary number at cursor"
    endif
    return eval("0x".Bin2Hex(wordac))
endfunc "}}}

" SetBinaryValueAtCursor(), replaced string in the unnamed register
func! SetBinvac(number) "{{{
    let fmtstr = "%0".(2*g:binary_octets_min)."X"
    let binarystr = Hex2Bin(printf(fmtstr, a:number))
    call StartOfWord()
    exec "normal! ciw\<C-R>=binarystr\r". g:binary_suffix
endfunc "}}}

" return the first run of binary digits found in string {bin} converted
" to (lower case) hex digits
func! Bin2Hex(bin) "{{{
    let bin = matchstr(a:bin, '[01]\+')
    if bin == ""
        return "0"
    endif
    let bin = repeat("0", PadSize(strlen(bin), 4)). bin
    let runs = split(bin, '....\zs')
    call map(runs, 's:fourbd2hd(v:val)')
    return join(runs, '')
endfunc "}}}

" return the first run of hex digits in string {hex} converted into
" binary digits (a match at "0x" is skipped)
func! Hex2Bin(hex) "{{{
    let hex = matchstr(a:hex, '\%(0x\)\...@!\x\+')
    if hex == ""
        return "0"
    endif
    return join(map(split(hex, '\m'), 's:hd2fourbd(v:val)'), '')
endfunc "}}}


" Lib Functions: functions that could be extracted

" calculate number of pad bytes to make {len} a multiple of {chunksize}
func! PadSize(len, chunksize) "{{{
    return -1 + (a:chunksize - (a:len + a:chunksize - 1) % a:chunksize)
endfunc
" padsize := PadSize(len,chunksize) <=>
" 0 <= padsize < chunksize AND (len + padsize) mod chunksize = 0
"}}}

" move cursor to Start of word (the word returned by expand("<cword>"))
func! StartOfWord() "{{{
    let xw = expand("<cword>")
    if xw != ""
        if xw =~ '\k'
            call search('\k\>','cW')
        else
            call search('\s...@!','cW')
        endif
        if strlen(xw) > 1
            normal! b
        endif
    endif
endfunc "}}}


" Local Functions:

" convert 4 bin digits (string) into 1 (lower case) hex digit
func! s:fourbd2hd(bd) "{{{
    let bd = a:bd
    return printf("%x", 8*bd[0] + 4*bd[1] + 2*bd[2] + bd[3])
endfunc "}}}

" convert 1 hex digit (string) into 4 bin digits
func! s:hd2fourbd(hd) "{{{
    let hd = eval("0x". a:hd)
    return (hd/8) . (hd/4%2) . (hd/2%2) . (hd%2)
endfunc "}}}



" Modeline: {{{1
" NOTE: must not use substitute() in the functions to avoid recursive
" submatch() when called from :s/.../\=.../
"   substitute(bin,'[01]\{4}', '\=s:fourbd2hd(submatch(0))', 'g')
"   substitute(a:hex, '\x', '\=s:hd2fourbd(submatch(0))', 'g')

" vim:tw=72 et fdm=marker:

Reply via email to