File: cream-numberlines.vim

package info (click to toggle)
cream 0.43-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,260 kB
  • sloc: sh: 323; php: 261; makefile: 5
file content (97 lines) | stat: -rw-r--r-- 3,292 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"
" cream-numberlines.vim -- Number lines of a selection with optional
"                          (dialog prompt) start number
"
" Cream -- An easy-to-use configuration of the famous Vim  text editor
" [ http://cream.sourceforge.net ] Copyright (C) 2001-2011 Steve Hall
"
" License:
" This program is free software; you can redistribute it and/or modify
" it under the terms of the GNU General Public License as published by
" the Free Software Foundation; either version 2 of  the  License,  or
" (at your option) any later version.
" [ http://www.gnu.org/licenses/gpl.html ]
"
" This program is distributed in the hope that it will be useful,  but
" WITHOUT  ANY  WARRANTY;  without  even  the  implied   warranty   of
" MERCHANTABILITY or FITNESS FOR A PARTICULAR  PURPOSE.  See  the  GNU
" General Public License for more details.
"
" You should have received a copy of the GNU  General  Public  License
" along with  this  program;  if  not,  write  to  the  Free  Software
" Foundation,  Inc.,  59  Temple  Place  -  Suite  330,   Boston,   MA
" 02111-1307, USA.
"
" Updated: 2003-05-09T00:00:45-0400
" Version: 0.2.1
" Source:  http://vim.sourceforge.net/scripts/script.php?script_id=605
" Author:  Steve Hall  [ digitect@mindspring.com ]
"
" Installation:
" o Drop this file into your plugins directory and (re)start Vim.
"
" Usage:
" o Must be called from visual mode (with a selection).
" o Required argument indicates mode, only "v" (visual) allowed.
" o Example mapping:
"	  vmap <silent> <F12> :<C-u>call Cream_number_lines("v")
" o Example menu:
"     vmenu <silent> 40.151 I&nsert.Line\ Numbers\ (selection) :<C-u>call Cream_number_lines("v")<CR>
"

function! Cream_number_lines(mode)

	" only possible with selection (visual mode)
	if a:mode !=? "v"
		return
	endif

	" prompt for line number (default current)
	let startno = inputdialog("Enter the beginning number...", line("'<"))
	" verify '0' means 0 and not quit
	if startno == 0
		let n = confirm("Ok to start with line number '0' or Cancel function...\n", "&Ok\n&Cancel", 1, "Info")
		if n != 1
			return
		endif
	endif
	" force to digit (theoretical)
	let startno = startno + 0

	" first line ok an any position
	let mypos1 = line("'<")
	" don't use last line if end mark is in first column
	if col("'>") == 1	
		let mypos2 = line("'>") - 1
	else
		let mypos2 = line("'>")
	endif

	" calculate difference in line and starting number
	let startdiff = startno - line("'<")

	" file's current number of lines
	let maxlinelen = strlen(line('$')) + 0

	" how large line numbers can be
	let maxsize = maxlinelen
	" require minimum of 3 columns (reduces amount of re-justification
	" if user adds enough lines to bump up the width of the line
	" number column; we're assuming any file will have at least 100
	" lines)
	if maxlinelen < 3
		let maxlinelen = 3 + 0
	endif
	" create spacer the number of spaces of maxlines
	let myspacer = "   "
	while strlen(myspacer) <= maxlinelen
		let myspacer = myspacer . " "
	endwhile

	" add line numbers (regexp help by Stefan Roemer <roemer@in.tum.de>, 2003-03-27)
	execute mypos1 . "," . mypos2 . "s/^/\\=submatch(1) . " .
		\ "strpart('" . myspacer . "', 1, " . maxlinelen . " - strlen((line('.') + " . startdiff . "))) . " .
		\ "(line('.') + " . startdiff . ") . ':'"

endfunction