Popup menu for section navigation in Notepad++

Post your working scripts, libraries and tools for AHK v1.1 and older
jakejake
Posts: 18
Joined: 12 Feb 2017, 12:26
Contact:

Popup menu for section navigation in Notepad++

21 Jun 2017, 09:57

This code brings up any text in <<double angled brackets>> and displays it in a popup menu as buttons. Clicking takes you to the line which that text is on in the document.

It uses FileRead with the path in the window title of notepad++ and populates the content of the brackets in the active file you are on in notepad. It is supposed to create a new column after every 30 section titles and a blank <<>> bracket adds a gap in the menu to separate groups.

I use it for long documents, I can comment the tags out in a big ahk scripts, and It is really helpful for organizing my list of favorite bandcamp urls by genre.

I don't know much about writing code, but I found this script useful and some nice people in this forum helped me so I thought I would share :) The coding is far from perfect though.

It is probably way more convoluted and complicated than it needs to be, so if you try it and think it can be better, let me know!
here is the script:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

Global Brkt_f
Global Brkt_b

Brkt_f := "<" . "<"
Brkt_b := ">" . ">"

a::
	NavigationWindow()
	Gui, Show
	return
		

GetNotepadDirect(){
		NPDirect :=
		WinGetTitle, NPDirect, A
		StringTrimRight, NPDirect, NPDirect, 12
		StringReplace, NPDirect, NPDirect, *
	return NPDirect
	}		

GuiClose:
	Gui Destroy
	return
	
NavWndwBut:
	WinActivate, %NMDirect%
	Send, ^{End} ;go to bottom of document so section titles end up consistently at top of page when jumped to
	Send, ^g ;notepad's go to line num
	Send, % $Test := nLines(ArchiveContents, A_GuiControl) ;enter the appropriate line number
	Send, {Enter}
	Gui, Destroy
	return

NavigationWindow(){
	Global ArchiveContents
	Global NMDirect
	
	NMDirect :=
	NMDirect := GetNotepadDirect() ;store path of active notepad file
	FileRead, ArchiveContents, %NMDirect% ;get notepad file contents
	StringReplace, ArchiveContents, ArchiveContents, `r`n, `n, All ;reformat for regexreplace to work

	ArchiveContentSections := RegExReplace(ArchiveContents, "(^|" . Brkt_b . ").*?(" . Brkt_f . "|$)", "*")
	StringTrimLeft, ArchiveContentSections, ArchiveContentSections, 1
	StringTrimRight, ArchiveContentSections, ArchiveContentSections, 1
	StringSplit, SxnName, ArchiveContentSections, *
	NavPos :=
	
	Loop, %SxnName0%
		{
		ColNum = 0
		RowNum = 30
		ColLimit := ColNum * RowNum
		ColTop := ColLimit + RowNum
		ntEffct := "p+60"
		
		if (A_Index = Coltop)
			{ ;top of additional columns
			NavPos := "ym"
			ColNum++
			}
		else if (A_Index > RowNum)
			{  ;rest of additional columns
			NavPos := "xp y+m"
			}
		if (SxnName%A_Index% = "") ;add separation in menu for blank brackets
		Gui, Add, Text, %NavPos%,  
		else
		Gui, Add, Button, gNavWndwBut %NavPos%, % SxnName%A_Index%
		}
	}	
	
nLines(ArchiveContents, SectionName)
	{
	TheSectionName := "<" . "<" . SectionName . ">" . ">"
	StringReplace, NewArchiveContents, ArchiveContents, %TheSectionName%, @, All 
	StringSplit, NewArchive, NewArchiveContents, @
	StringReplace NewArchive1, NewArchive1, `n, `n, All UseErrorLevel
	LineVar = %ErrorLevel%
	LineNum := LineVar + 1
	return LineNum
	}
Here is what the bracketed sections would look like in a document:

Code: Select all

<<Section1>>
Lorem ipsum dolor sit amet, vivamus mauris sed amet porttitor.
Libero nulla dolor facilisi scelerisque gravida et, sed non,
nec lorem sed inceptos egestas quidem, bibendum libero metus
non pellentesque. At eget. Magna metus magna, neque ut et
tincidunt dictum qui, sed natoque. Eleifend aenean augue imperdiet
sapien, facilisi enim dui in. Interdum morbi quia donec quis,
massa adipiscing volutpat. Ultrices tortor vel class eu. Felis
in pede elementum et tincidunt, lacus donec cras posuere, augue eu
viverra, lacinia diam leo nibh libero sollicitudin.

<<Section2>>
Facilisis vehicula, porta nam magna, ut lectus suspendisse risus
ac eget nec, quam massa porttitor, sociis vel. Faucibus mauris
amet, beatae vivamus lacus pretium, malesuada dapibus fringilla
id duis et ut. Aliquid a lobortis duis luctus pulvinar, amet
inventore pellentesque tincidunt non, duis ipsum suspendisse
sapien fusce sed nonummy, ante ut, dui augue quam euismod nulla
tristique eros. Metus placerat metus aliquet. Omnis eu.
Non magnis faucibus, integer amet vestibulum suspendisse, mattis
dolor vehicula lobortis, fringilla pellentesque.

<<>>
<<Section3>>
Tellus turpis natoque tincidunt pretium venenatis, fusce ante in
egestas a. Eleifend sollicitudin ut orci. Diam velit vulputate
nec, at a quisque varius sed ante. Arcu commodo varius condimentum.
Etiam mattis molestie, vitae sodales ac a, erat facilisis et elit
magna, tempus id felis aliquam consectetuer dictum morbi, nullam
vel nec. Et mi maecenas. Non ut at lorem. Consectetuer lacus
pede nullam tellus integer adipiscing, mollis aliquam egestas nisl
eu posuere, sit taciti. Ante faucibus sollicitudin nam sed, massa
consequat lectus nunc lectus, wisi erat blandit ac placerat
vehicula, lorem tempus id ullamcorper. Venenatis elit occaecati,
suscipit vitae metus in a.
jakejake
Posts: 18
Joined: 12 Feb 2017, 12:26
Contact:

Re: Popup menu for section navigation in Notepad++

21 Jun 2017, 12:32

gif of the script in action:
Image
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Popup menu for section navigation in Notepad++

21 Jun 2017, 15:55

:bravo: Very nice. I will put this in my npp script, time will tell if I remember to use it :lol:
For convenience,

Code: Select all

#ifwinactive ahk_exe notepad++.exe
:*?:<<<::
	SendInput,;<<>>{Left 2}^v^s
return
The I just need to copy a function name (or anything) and hit <<< to tag it.

Thanks for sharing.
Guest

Re: Popup menu for section navigation in Notepad++

23 Jun 2017, 08:48

Thanks! here's the code I was using the code below to add brackets around a selection, but I think I like your method better.

Code: Select all

^Capslock::
IfWinActive ahk_class Notepad++
	{
	Saved=%ClipboardAll%
	Send ^x
	Sleep, 100
	ClipWait, 2
	Clipboard:=Brkt_f . Clipboard . Brkt_b
	Sleep, 100
	ClipWait
	Send ^v
	Sleep, 100
	Clipboard=%Saved%
	return
	}

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Google [Bot] and 269 guests