Re: [code] [textadept] Macros/add-ons for quick C/C++ function traversal?

From: Gabriel Dubatti <gdubatti.att.gmail.com>
Date: Thu, 11 Oct 2018 09:49:04 -0300

Hi Ardi,

El 11/10/2018 a las 08:14, ardi escribió:
> Hi,
>
> I've been a long time NEdit user, and one of the features I used the
> most was to jump to the beginning of either the next or the previous
> C/C++ function in the current buffer (I used to assign the F4 key for
> jumping to the next function, and the F3 key for the previous one...
> as well as the F2 key for jumping to the function whose name is the
> current selection). Note that I'm not talking about ctags files, NEdit
> was able to locate the beginning of a function just from regular
> expressions rules, without the need of ctags files.
>
> I tend to believe that this functionality will already exist in
> Textadept, or that otherwise other users will have written the proper
> macros/commands for getting that functionality. Does it already exist?
> Where?
>
> Thanks!!
>
> ardi

I use the following code for C like and lua lexers.

For C/C++ the code looks for "{" and "}" at the beginning of each row 
(you might have to adapt it to your coding style).

--------------

local function get_lexer()
   local GETLEXERLANGUAGE= _SCINTILLA.properties.lexer_language[1]
   return buffer:private_lexer_call(GETLEXERLANGUAGE):match('^[^/]+')
end

local function find_line(fmatch,dirf,roff)
   local r
   local curr= buffer:line_from_position(buffer.current_pos)
   if dirf then --forward
     for i = curr +1 -roff, buffer.line_count, 1 do
       if buffer:get_line(i):match(fmatch) then
         r=i+roff
         break
       end
     end
   else  --backward
     for i = curr -1 -roff, 0, -1 do
       if buffer:get_line(i):match(fmatch) then
         r=i+roff
         break
       end
     end
   end
   if r then
     if r < 0 then r= 0 end
     buffer:ensure_visible_enforce_policy(r)
     buffer:goto_line(r)
     return true
   end
   return false
end

-- Ctrl+, = GOTO previous FUNCTION/C-BLOCK BEG
-- Ctrl+. = GOTO next     FUNCTION/C-BLOCK BEG
--$nnnnnnnnn  ($=cursor position)
--{
--....
--}
--LUA version: $[....]function[ mm.www](
local function find_begin(dirf)
   local sbeg='^{'
   local roff=-1
   local lexer= get_lexer()
   if lexer == 'lua' then
     sbeg='^.*function%s*[%w_.]*%('
     roff=0
   end
   if not find_line(sbeg,dirf,roff) then
     ui.statusbar_text= 'main block begin: not found'
   end
end

-- Ctrl+; = GOTO previous FUNCTION/C-BLOCK END
-- Ctrl.: = GOTO next     FUNCTION/C-BLOCK END
--nnnnnnnnn
--{
--....
--$}          ($=cursor position)
--LUA version: $end
local function find_end(dirf)
   local send='^}'
   local lexer= get_lexer()
   if lexer == 'lua' then send='^end' end
   if not find_line(send,dirf,0) then
     ui.statusbar_text= 'main block end: not found'
   end
end

   keys["c,"] = function() find_begin(false) end
   keys["c."] = function() find_begin(true) end
   keys["c;"] = function() find_end(false) end
   keys["c:"] = function() find_end(true) end
-------

-- 
You are subscribed to code.att.foicica.com.
To change subscription settings, send an e-mail to code+help.att.foicica.com.
To unsubscribe, send an e-mail to code+unsubscribe.att.foicica.com.
Received on Thu 11 Oct 2018 - 08:49:04 EDT

This archive was generated by hypermail 2.2.0 : Fri 12 Oct 2018 - 06:40:35 EDT