Scite Open To Line

lua-users home
wiki

SciteOpenToLine

SciteOpenToLine looks at the file SciTE has just opened. If there are colons:in:the:name, we will tell SciTE to also try to open another document, interpreting the portion after the colon as a line number. The benefit is the ability to open files with line numbers appended to the end, such as the output produced by grep, most compilers, profilers and debuggers. These files can now be opened with scite directly, without having to pass special commands or edit out the colons. This has the advantage that you can double-click on the debugger line and then middle-click-paste in the terminal, a line like this without editing:

scite ../../foo/docs/html/gdiobj_8c-source.html:420:3

would now cause SciTE and go to line 420 of gdiobj_8c-source.html

I leave column highlighting as a trivial exercise.

function marker_define(idx,typ,fore,back)
	editor:MarkerDefine(idx,typ)
	if fore then editor:MarkerSetFore(idx,color_parse(fore)) end
	if back then editor:MarkerSetBack(idx,color_parse(back)) end
end

-- Compatibility: Lua-5.1
function split(str, pat)
   local t = {}  -- NOTE: use {n = 0} in Lua-5.0
   local fpat = "(.-)" .. pat
   local last_end = 1
   local s, e, cap = str:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
	 table.insert(t,cap)
      end
      last_end = e+1
      s, e, cap = str:find(fpat, last_end)
   end
   if last_end <= #str then
      cap = str:sub(last_end)
      table.insert(t, cap)
   end
   return t
end

--~ Naming this function OnOpen causes it to run every time SciTE opens a file
--~ and determine if the opened file has :somenumber: in the extension.
--~ If it does, we open it in another window and go to that line.
function OnOpen()
    local ext = props['FileExt']   -- e.g 'cpp'
	if string.match(ext,':') then
		marker_define(0,0,'black','red')
		local extSplit = split(ext,':')
		local f = props['FileName']    -- e.g 'test'
		local path = props['FileDir']  -- e.g. '/home/steve/progs'
--~ 		print (f,ext,extSplit[1],extSplit[2],path)
		newpat=path..'/'..f..'.'..extSplit[1]
		scite.Open(newpat)
		line=tonumber(extSplit[2]-1)
		editor:GotoLine(line)
		editor:MarkerAdd(line,0)
		editor.SelectionStart   = editor.CurrentPos
		editor.SelectionEnd   = editor.LineEndPosition[line]
	end
end

Optional

Users may desire to bind SciTE to a hotkey so that they only need press a button and any highlighted file anywhere on the desktop, terminal session or web is opened in SciTE.

On Linux: a bash script, such as "scite `xsel`" can be bound to a hotkey or mouse gesture with your window manager to open the highlighted file, colons and all, from any terminal or web page without even having to type "scite " into the terminal or clicking "paste"

    Problem: SciTE, when launched from a hotkey, has no idea where the highlighted file is.
    We must somehow tell SciTE how to get there. For this we need to download xsel.

    1. Add to ~/.bashrc (this stores your current working directory in a readable file)

	pwd > ~/.PWD
	c(){ command cd "$@"; pwd > ~/.PWD; }

	Note 1: don't put anything in .bashrc that would output text to the terminal or
	you will have strange problems, such as being unable to scp to your machine.

	Note 2: be sure you don't already have the command, 'c' on your system.
		remember to type 'c' instead of 'cd' in terminal from now on, unless...

	Note 3: The following changes the default behavior of your shell and will not be
	supported. It is not well tested and is probably guaranteed to break something.
	You can go ahead and try it. It works for me. Just don't bother me about it.
		"to hook into user's default cd command use a line like this:"
		cd(){ command cd "$@"; pwd > ~/.PWD; }

     2. Create a scite launcher script that trims selection and searches a few likely
	places for the file you have highlighted. This script is called from a hotkey
	so it does not affect SciTE's default behavior.

	cat << EOF > ~/bin/xselscite.sh
	#!/bin/bash
	# remove whitespace from both ends of selected text
	 x=$(xsel|sed -e 's/^[ ]*//' -e 's/[ ]*$//')
	 
	# search a few likely places for a file to open
	# todo: expand this list
	if [[ -f ${x/\~\//} ]]; then
		f="${x/\~\//}"
	elif [[ -f $(<~/.PWD)/$x ]]; then
		f="$(<~/.PWD)/$x"
	else
		f="${x::25}" # New file? Name < 25 chars
	fi
	#try to prevent scite from opening more than 4
	#files in case somebody selects the whole page
	[[ $(echo $f|wc -l) < 5 ]]&&scite "$f"
	EOF

	chmod +x ~/bin/xselscite.sh

    3. Go to your window manager's keyboard shortcut or mouse gesture configuration and add a keyboard
	shortcut that points to your new script.

    4. Enjoy having a one-button SciTE launcher.

RecentChanges · preferences
edit · history
Last edited November 1, 2010 8:38 am GMT (diff)