Subversion Repositories programming

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
363 ira 1
"*****************************************************************************
2
"** Name:      chartab.vim - print a character table                        **
3
"**                                                                         **
4
"** Type:      global VIM plugin                                            **
5
"**                                                                         **
6
"** Author:    Christian Habermann                                          **
7
"**            christian (at) habermann-net (point) de                      **
8
"**                                                                         **
9
"** Copyright: (c) 2004 by Christian Habermann                              **
10
"**                                                                         **
11
"** License:   GNU General Public License 2 (GPL 2) or later                **
12
"**                                                                         **
13
"**            This program is free software; you can redistribute it       **
14
"**            and/or modify it under the terms of the GNU General Public   **
15
"**            License as published by the Free Software Foundation; either **
16
"**            version 2 of the License, or (at your option) any later      **
17
"**            version.                                                     **
18
"**                                                                         **
19
"**            This program is distributed in the hope that it will be      **
20
"**            useful, but WITHOUT ANY WARRANTY; without even the implied   **
21
"**            warrenty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      **
22
"**            PURPOSE.                                                     **
23
"**            See the GNU General Public License for more details.         **
24
"**                                                                         **
25
"** Version:   1.0.0                                                        **
26
"**            tested under Linux and Win32, VIM and GVIM 6.2               **
27
"**                                                                         **
28
"** History:   1.0.0  31. Jan. 2004                                         **
29
"**              initial version                                            **
30
"**                                                                         **
31
"**                                                                         **
32
"*****************************************************************************
33
"** Description:                                                            **
34
"**   This script provides a character table (yes, yet another one :-) ).   **
35
"**   But it has some nice features:                                        **
36
"**     - it takes advantage of syntax-highlighting                         **
37
"**     - it allows to toggle base of codes without leaving buffer          **
38
"**     - it opens in currently active window, so no rearrangement of       **
39
"**       windows occur                                                     **
40
"**     - special codes are viewed with their real names (NUL, ETX,...)     **
41
"**     - quitting is very simple and fast - just one keystroke             **
42
"**                                                                         **
43
"**   Installation:                                                         **
44
"**     To use this script copy it into your local plugin-directory         **
45
"**     Unix:    ~./.vim/plugin                                             **
46
"**     Windows: c:\vimfiles\plugin                                         **
47
"**     After starting VIM this script is sourced automatically.            **
48
"**                                                                         **
49
"**     By default, press <Leader>ct to view character table.               **
50
"**                                                                         **
51
"**   Configuration:                                                        **
52
"**     - <Plug>CT_CharTable                                                **
53
"**       mapping to open character table                                   **
54
"**       default:                                                          **
55
"**          map <silent> <unique> <Leader>ct <Plug>CT_CharTable            **
56
"**                                                                         **
57
"**     - g:ct_base                                                         **
58
"**       Defines base of codes. Allowed values are 'hex' and 'dec'.        **
59
"**       Default is 'dec'.                                                 **
60
"**       Add  let g:ct_base="hex" to your .vimrc if you want to change it. **
61
"**                                                                         **
62
"**   Known limitations:                                                    **
63
"**     If a character is not printable by Vim it is printed as two         **
64
"**     characters, e.g. ~A. This may cause a misalignment of the table.    **
65
"**                                                                         **
66
"**   Known bugs:                                                           **
67
"**     none - well, up to now :-)                                          **
68
"**                                                                         **
69
"**                                                                         **
70
"**   Happy vimming....                                                     **
71
"*****************************************************************************
72
 
73
" allow user to avoid loading this plugin and prevent loading twice
74
if exists ("ct_chartable")
75
    finish
76
endif
77
 
78
let ct_chartable = 1
79
 
80
 
81
 
82
 
83
"*****************************************************************************
84
"************************** C O N F I G U R A T I O N ************************
85
"*****************************************************************************
86
 
87
" the mappings:
88
if !hasmapto('<Plug>CT_CharTable')
89
    map <silent> <unique> <Leader>ct <Plug>CT_CharTable
90
endif
91
 
92
map <silent> <unique> <script> <Plug>CT_CharTable  :call <SID>CT_CharTable()<CR>
93
 
94
 
95
 
96
if !exists('g:ct_base')      " base of numbers, default is hex (hex, dec)
97
    let g:ct_base = "dec"
98
endif
99
 
100
 
101
 
102
 
103
"*****************************************************************************
104
"****************** I N T E R F A C E  T O  C O R E **************************
105
"*****************************************************************************
106
 
107
"*****************************************************************************
108
"** this function separates plugin-core-function from user                  **
109
"*****************************************************************************
110
function <SID>CT_CharTable()
111
    call s:CharTable()
112
endfunction
113
 
114
 
115
 
116
"***************************  END OF USER'S WORLD  ***************************
117
 
118
 
119
 
120
 
121
"*****************************************************************************
122
"************************* I N I T I A L I S A T I O N ***********************
123
"*****************************************************************************
124
 
125
" used to store number of buffer showing character table 
126
" set it to impossible value
127
let s:viewBufNr = -1 
128
 
129
 
130
 
131
 
132
 
133
 
134
"*****************************************************************************
135
"************************ C O R E  F U N C T I O N S *************************
136
"*****************************************************************************
137
 
138
 
139
 
140
 
141
"*****************************************************************************
142
"** input:   none                                                           **
143
"** output:  none                                                           **
144
"*****************************************************************************
145
"** remarks:                                                                **
146
"**   This is the main function where all jobs are initiated.               **
147
"**                                                                         **
148
"**   The buffer loaded to view search-result will be deleted at:           **
149
"**     - loading another buffer into the same window (automatic)           **
150
"**     - user cancles view (see CloseViewBuffer())                         **
151
"**                                                                         **
152
"*****************************************************************************
153
function s:CharTable()
154
 
155
    " allow modifications (necessary if called from the scripts own buffer)
156
    setlocal modifiable
157
 
158
 
159
    "open buffer for viewing character table
160
    call s:OpenViewBuffer()
161
 
162
 
163
    " define locale mappings of user interface
164
    call s:SetLocalKeyMappings()
165
 
166
 
167
    " set syntax highlighting for view
168
    call s:SetupSyntaxHighlighting()
169
 
170
 
171
 
172
    " make string for header of view => s:txt
173
    call s:MakeHeader()
174
 
175
 
176
    " add character table => s:txt; that's what user want to see
177
    call s:MakeCharTable()
178
 
179
 
180
    " output result
181
    setlocal modifiable
182
 
183
    put! = s:txt
184
 
185
    setlocal nomodifiable
186
 
187
endfunction
188
 
189
 
190
 
191
 
192
"*****************************************************************************
193
"** input:   none                                                           **
194
"** output:  none                                                           **
195
"*****************************************************************************
196
"** remarks:                                                                **
197
"**   Make string for header of view.                                       **
198
"*****************************************************************************
199
function s:MakeHeader()
200
 
201
    let s:txt =         "\"             Character Table\n"
202
    let s:txt = s:txt . "\"            =================\n"
203
    let s:txt = s:txt . "\"\n"
204
    let s:txt = s:txt . "\"  b : toggle base\n"
205
    let s:txt = s:txt . "\"  q : quit\n\n\n"
206
 
207
endfunction
208
 
209
 
210
 
211
 
212
"*****************************************************************************
213
"** input:   none                                                           **
214
"** output:  none                                                           **
215
"*****************************************************************************
216
"** remarks:                                                                **
217
"**   Print character table into current buffer.                            **
218
"*****************************************************************************
219
function s:MakeCharTable()
220
 
221
    let NUM_OF_ROWS = 32
222
    let NUM_OF_COLS = 8
223
 
224
    let code = 0
225
    let row  = 0  
226
 
227
 
228
    while row < NUM_OF_ROWS
229
 
230
        let column = 0
231
 
232
        while column < NUM_OF_COLS
233
            let code = row + column * NUM_OF_ROWS
234
 
235
            " add code number
236
            let s:txt = s:txt ." " . s:Nr2String(code, g:ct_base). " "
237
 
238
            " add character
239
            if (s:IsCodeSpecialChar(code))
240
                let s:txt = s:txt . s:GetStringOfSpecialChar(code)
241
                let spaceToNext = " "
242
            else
243
                let s:txt = s:txt . nr2char(code)
244
                let spaceToNext = "   "
245
            endif
246
 
247
            let s:txt = s:txt . ( (column == (NUM_OF_COLS - 1)) ? "" : spaceToNext . "|" )
248
 
249
            let column = column + 1
250
        endwhile
251
 
252
        let s:txt = s:txt . "\n"
253
 
254
        let row = row + 1
255
    endwhile
256
 
257
 
258
endfunction
259
 
260
 
261
 
262
 
263
"*****************************************************************************
264
"** input:   code: number to be converted to a string                       **
265
"**          base: defines base of number ("hex" or "dec")                  **
266
"** output:  formated string                                                **
267
"*****************************************************************************
268
"** remarks:                                                                **
269
"**   This function converts a number to a string.                          **
270
"*****************************************************************************
271
function s:Nr2String(nr, base)
272
 
273
    let nr   = a:nr
274
    let base = a:base
275
 
276
 
277
    " get base as a number
278
    if (a:base == "dec")
279
        let base = 10
280
    else
281
        let base = 16
282
    endif
283
 
284
 
285
    " convert number to string
286
    let strng = (nr == 0) ? "0" : ""
287
 
288
    while nr
289
        let strng = '0123456789ABCDEF'[nr % base] . strng
290
        let nr = nr / base
291
    endwhile
292
 
293
 
294
    " format string: 3 digits, right alignment
295
    if (base == 10)
296
        if (strlen(strng) == 1)
297
            let strng = "  " . strng 
298
        elseif (strlen(strng) == 2)
299
            let strng = " " . strng      
300
        endif
301
    else       " assume hex
302
        if (strlen(strng) == 1)
303
            let strng = "0" . strng 
304
        endif
305
 
306
        let strng = strng . "h"
307
    endif
308
 
309
 
310
 
311
    return strng
312
 
313
endfunction
314
 
315
 
316
 
317
 
318
 
319
"*****************************************************************************
320
"** input:   code: number to be tested                                      **
321
"** output:  0  : no special character                                      **
322
"**          > 0: code is a special character                               **
323
"*****************************************************************************
324
"** remarks:                                                                **
325
"**   This function tests whether 'code' is a special character.            **
326
"**   Special characters are: 0...0x20 and 0x7F, space (0x20) is included   **
327
"**                           here to print it as ' '                       **
328
"*****************************************************************************
329
function s:IsCodeSpecialChar(code)
330
 
331
  if ( ((a:code >= 0) && (a:code <= 0x20)) || (a:code == 0x7F) )
332
      return 1
333
  else
334
      return 0
335
  endif
336
 
337
 
338
endfunction
339
 
340
 
341
 
342
 
343
"*****************************************************************************
344
"** input:   code: code of special character 0..0x20 or 0x7F                **
345
"** output:  name of special character                                      **
346
"*****************************************************************************
347
"** remarks:                                                                **
348
"**   returns name of special character, e.g. code = 0 will return NUL      **
349
"*****************************************************************************
350
function s:GetStringOfSpecialChar(code)
351
 
352
    let strng = "-"
353
 
354
    if (a:code == 0)
355
        let strng = "NUL"
356
    elseif (a:code ==  1)
357
        let strng = "SOH"
358
    elseif (a:code ==  2)
359
        let strng = "STX"
360
    elseif (a:code ==  3)
361
        let strng = "ETX"
362
    elseif (a:code ==  4)
363
        let strng = "EOT"
364
    elseif (a:code ==  5)
365
        let strng = "ENQ"
366
    elseif (a:code ==  6)
367
        let strng = "ACK"
368
    elseif (a:code ==  7)
369
        let strng = "BEL"
370
    elseif (a:code ==  8)
371
        let strng = "BS "
372
    elseif (a:code ==  9)
373
        let strng = "TAB"
374
    elseif (a:code == 10)
375
        let strng = "LF "
376
    elseif (a:code == 11)
377
        let strng = "VT "
378
    elseif (a:code == 12)
379
        let strng = "FF "
380
    elseif (a:code == 13)
381
        let strng = "CR "
382
    elseif (a:code == 14)
383
        let strng = "SO "
384
    elseif (a:code == 15)
385
        let strng = "SI "
386
    elseif (a:code == 16)
387
        let strng = "DLE"
388
    elseif (a:code == 17)
389
        let strng = "DC1"
390
    elseif (a:code == 18)
391
        let strng = "DC2"
392
    elseif (a:code == 19)
393
        let strng = "DC3"
394
    elseif (a:code == 20)
395
        let strng = "DC4"
396
    elseif (a:code == 21)
397
        let strng = "NAK"
398
    elseif (a:code == 22)
399
        let strng = "SYN"
400
    elseif (a:code == 23)
401
        let strng = "ETB"
402
    elseif (a:code == 24)
403
        let strng = "CAN"
404
    elseif (a:code == 25)
405
        let strng = "EM "
406
    elseif (a:code == 26)
407
        let strng = "SUB"
408
    elseif (a:code == 27)
409
        let strng = "ESC"
410
    elseif (a:code == 28)
411
        let strng = "FS "
412
    elseif (a:code == 29)
413
        let strng = "GS "
414
    elseif (a:code == 30)
415
        let strng = "RS "
416
    elseif (a:code == 31)
417
        let strng = "US "
418
    elseif (a:code == 32)
419
        let strng = "\' \'"
420
    elseif (a:code == 127)
421
        let strng = "DEL"
422
    endif
423
 
424
 
425
    return strng
426
 
427
endfunction
428
 
429
 
430
 
431
 
432
 
433
"*****************************************************************************
434
"** input:   none                                                           **
435
"** output:  none                                                           **
436
"*****************************************************************************
437
"** remarks:                                                                **
438
"**   set local/temporarily key-mappings valid while viewing result         **
439
"*****************************************************************************
440
function s:SetLocalKeyMappings()
441
                                         " use 'q' to close view-buffer
442
                                         " and switch to previously used buffer
443
    nnoremap <buffer> <silent> q :call <SID>CT_Exit()<cr>
444
 
445
                                         " use 'b' to toggle base of codes
446
    nnoremap <buffer> <silent> b :call <SID>CT_ToggleBase()<cr>
447
 
448
endfunction
449
 
450
 
451
 
452
 
453
"*****************************************************************************
454
"** input:   errNr: number which defines an error (> 0)                     **
455
"** output:  none                                                           **
456
"*****************************************************************************
457
"** remarks:                                                                **
458
"**   this function prints an error-msg                                     **
459
"*****************************************************************************
460
"function s:Error(errNr)
461
"    
462
"    if (a:errNr == 1)
463
"        echo scriptName.": can't open buffer"
464
"    else
465
"        echo scriptName.": unknown error"
466
"    endif
467
"
468
"endfunction
469
 
470
 
471
 
472
 
473
"*****************************************************************************
474
"** input:   none                                                           **
475
"** output:  none                                                           **
476
"*****************************************************************************
477
"** remarks:                                                                **
478
"**   set syntax-highlighting (if VIM has 'syntax')                         **
479
"*****************************************************************************
480
function s:SetupSyntaxHighlighting()
481
 
482
    " don't continue, if this version of VIM does not support syntax-highlighting
483
    if !has('syntax')
484
        return
485
    endif
486
 
487
    syntax match ctComment  "^\".*"
488
 
489
 
490
    " matches both separator or start of line and code
491
    syn match ctSepaCode  "\(^\||\)[ ]\+[0-9A-Fa-fh]\+" contains=ctCode,ctSeparator
492
 
493
    " matches code only
494
    syn match ctCode      "[0-9A-Fa-fh]\+" contained
495
 
496
    " matches serparator only
497
    syn match ctSeparator "|" contained
498
 
499
 
500
    if !exists('g:ct_syntaxHighInit')
501
        let g:ct_syntaxHighInit = 0
502
 
503
        hi def link ctComment   Comment
504
        hi def link ctCode      String
505
        hi def link ctSeparator Comment
506
    endif
507
 
508
endfunction
509
 
510
 
511
 
512
 
513
"*****************************************************************************
514
"** input:   none                                                           **
515
"** output:  none                                                           **
516
"*****************************************************************************
517
"** remarks:                                                                **
518
"**   Open a buffer to view character table and for user interaction.       **
519
"**                                                                         **
520
"*****************************************************************************
521
function s:OpenViewBuffer()
522
 
523
    " save current buffer number so that we can switch back to this buffer
524
    " when finishing job
525
    " but only if the current buffer isn't already one of chartab's
526
    if (s:viewBufNr != winbufnr(0))
527
        let s:startBufNr = winbufnr(0)
528
    endif
529
 
530
 
531
    " open new buffer
532
    execute "enew"
533
 
534
 
535
    " save buffer number used by this script to view result
536
    let s:viewBufNr = winbufnr(0)
537
 
538
 
539
    " buffer specific settings:
540
    "   - nomodifiable:     don't allow to edit this buffer
541
    "   - noswapfile:       we don't need a swapfile
542
    "   - buftype=nowrite:  buffer will not be written
543
    "   - bufhidden=delete: delete this buffer if it will be hidden
544
    "   - nowrap:           don't wrap around long lines
545
    "   - iabclear:         no abbreviations in insert mode
546
    setlocal nomodifiable
547
    setlocal noswapfile
548
    setlocal buftype=nowrite
549
    setlocal bufhidden=delete
550
    setlocal nowrap
551
    iabclear <buffer>
552
 
553
endfunction
554
 
555
 
556
 
557
 
558
"*****************************************************************************
559
"** input:   none                                                           **
560
"** output:  none                                                           **
561
"*****************************************************************************
562
"** remarks:                                                                **
563
"**   Switch to buffer in which the script was invoked. Then the view-      **
564
"**   buffer will be deleted automatically.                                 **
565
"**   If there was no buffer at start of script, delete view-buffer         **
566
"**   explicitely.                                                          **
567
"*****************************************************************************
568
function s:CloseViewBuffer()
569
 
570
    " if start and view-buffer are the same, there was no buffer at invoking script
571
    if (s:startBufNr != s:viewBufNr)
572
        exec("buffer! ".s:startBufNr)
573
    else
574
        exec("bdelete ".s:startBufNr)
575
    endif
576
 
577
endfunction
578
 
579
 
580
 
581
 
582
"*****************************************************************************
583
"** input:   none                                                           **
584
"** output:  none                                                           **
585
"*****************************************************************************
586
"** remarks:                                                                **
587
"**   toggle base of numbers                                                **
588
"*****************************************************************************
589
function <SID>CT_ToggleBase()
590
 
591
    " save current cursor position
592
    let lineNr = line(".")
593
    let colNr  = col(".")
594
 
595
 
596
    " toggle base of numbers
597
    let g:ct_base = (g:ct_base == "dec") ? "hex" : "dec"
598
 
599
    " redraw view with new base
600
    call <SID>CT_CharTable()
601
 
602
 
603
    " restore cursor position
604
    call cursor(lineNr, colNr)
605
 
606
endfunction
607
 
608
 
609
 
610
 
611
"*****************************************************************************
612
"** input:   none                                                           **
613
"** output:  none                                                           **
614
"*****************************************************************************
615
"** remarks:                                                                **
616
"**   Job is done. Clean up.                                                **
617
"*****************************************************************************
618
function <SID>CT_Exit()
619
 
620
    call s:CloseViewBuffer()
621
 
622
endfunction
623
 
624
 
625
 
626
"*** EOF ***