Sunday, June 28, 2009

AhkWikiPad



This is my version of a plain-text desktop wiki in AHK. I used the "Scripting Pad" at the AHK Forum as a basis for this application.

AhkWikiPad is just a simple editor that allows wiki-like links to other files (typically in the same directory as the application). Each wiki-page is a separate text file.

It should be placed into its own directory. Two files must be present at all times, namely "Home" (no extension) and "garbage.txt", which is used for creating new pages.

Links should be enclosed in double brackets, like so [[Plain-text wiki]]. If the file to which a link refers does not exist, it will be created. Just select the name, including the square brackets, and press CTRL-O to create the page or to jump to the page, if it already exists. The wiki-names are case-sensitive.

CTRL-D will delete both the link and the file, but it does not remove the reference to the file in other wiki-pages. I will need to look into that. AhkWikiPad cannot automatically rename the files corresponding to the links either, but that should be easy to implement. (The bigger problem will be to figure out how to rename the link in all other wiki files.)[1]

So, AhkWikiPad is very rudimentary at this point, but perhaps not bad for the "work" of a rainy Sunday afternoon of just experimenting in order to see whether such a thing a thing is possible.

#SingleInstance off
;#NoTrayIcon

gui, font, s10, Tahoma
gui, color, FFFFCC

Menu, FileMenu, Add, &Open `tCtrl-Shift-O, FileOpen
Menu, FileMenu, Add, &Save `tCtrl-S, FileSave
Menu, FileMenu, Add, Save &As `tCtrl-Shift-S, FileSaveAs
Menu, FileMenu, Add, E&xit `tCtrl-X, SaveAndExit
Menu, FileMenu, Add, &Discard, Exit
Menu, EditMenu, Add, &Time `tCtrl-T, Time
Menu, EditMenu, add, &Date `tCtrl-D, Date
Menu, MyMenuBar, Add, &File, :FileMenu
Menu, MyMenuBar, Add, &Edit, :EditMenu
Gui, Menu, MyMenuBar

Gui, +Resize
Gui, font, s10
Gui, Add, Edit, vMainEdit WantTab r30 x5 y5,
Gui, Show, w600 x0 y0, AhkWikiPad - untitled
CurrentFileName =
Gosub Initial
return

Initial:
FileRead, MainEdit, Home
GuiControl,, MainEdit, %MainEdit%
CurrentFileName = Home
Gui, Show,, AhKWikiPad - %CurrentFileName%
return

^+o::
Gosub FileOpen
return

^s::
Gosub FileSave
return

^+s::
Gosub FileSaveAs
return

^x::
Gosub SaveAndExit
return

^d::
Gosub Date
return

^t::
Gosub Time
return

Time:
Send %a_Hour%:%A_Min%
return

Date:
Send %a_dddd%, %A_MMMM% %a_DD%, %A_YYYY%
return

^o::
clipboard =
Send ^c
ClipWait
clipboard = %clipboard%
StringReplace, clipboard, clipboard,`],, All
Stringreplace, clipboard, clipboard,`[,, All
Stringreplace, clipboard, clipboard,`.,, All
Stringreplace, clipboard, clipboard,`,,, All
Stringreplace, clipboard, clipboard,`;,, All
Gosub FileSave
Gosub WikiRead
return

^+d::
clipboard =
Send ^x
ClipWait
clipboard = %clipboard%
StringReplace, clipboard, clipboard,`],, All
Stringreplace, clipboard, clipboard,`[,, All
Stringreplace, clipboard, clipboard,`.,, All
Stringreplace, clipboard, clipboard,`,,, All
Stringreplace, clipboard, clipboard,`;,, All
Gosub FileDelete
return

WikiRead:
FileRead, MainEdit, %clipboard%
if ErrorLevel <> 0
{
FileCopy garbage.txt, %clipboard%
Gosub WikiRead
return
}
FileRead, MainEdit, %clipboard%
GuiControl,, MainEdit, %MainEdit%
CurrentFileName = %clipboard%
Gui, Show,, AhkWikiPad - %CurrentFileName%
return

FileDelete:
FileDelete, %Clipboard%
return

FileOpen:
FileSelectFile, SelectedFileName, 3,, AhkWikiPad - Open File,
if SelectedFileName =
return
Gosub FileRead
return

FileRead:
FileRead, MainEdit, %SelectedFileName%
if ErrorLevel <> 0
{
MsgBox Could not open "%SelectedFileName%".
return
}
GuiControl,, MainEdit, %MainEdit%
CurrentFileName = %SelectedFileName%
Gui, Show,, AhKWikiPad - %CurrentFileName%
return

FileSave:
if CurrentFileName =
Goto FileSaveAs
Gosub SaveCurrentFile
return

FileSaveAs:
FileSelectFile, SelectedFileName, S16, , AhkWikiPad - save file,
if SelectedFileName =
return
CurrentFileName = %SelectedFileName%
Gosub SaveCurrentFile
Gui, Show,, AhKWikiPad - %CurrentFileName%
return

SaveCurrentFile:
IfExist %CurrentFileName%
{
FileDelete %CurrentFileName%
if ErrorLevel <> 0
{
MsgBox The attempt to overwrite "%CurrentFileName%" failed.
return
}
}
GuiControlGet, MainEdit
FileAppend, %MainEdit%, %CurrentFileName%
Gui, Show,, AhkWikipad - %CurrentFileName%
return

GuiDropFiles:
Loop, parse, A_GuiControlEvent, `n
{
SelectedFileName = %A_LoopField%
break
}
Gosub FileRead
return

GuiSize:
if ErrorLevel = 1
return
NewWidth := A_GuiWidth - 10
NewHeight := A_GuiHeight - 10
GuiControl, Move, MainEdit, W%NewWidth% H%NewHeight%
return

SaveAndExit:
IfExist %CurrentFileName%
Gosub SaveCurrentFile
IfNotExist %CurrentFileName%
{
gosub FileSaveAs
return
}
ExitApp
return

GuiClose:
IfNotExist %CurrentFileName%
{
msgbox, 3, AhkWikiPad - exit, Save this new file or not?,
IfMsgBox, Yes
{
Gosub FileSaveAs
if SelectedFileName =
return
else
ExitApp
}
IfMsgBox, No
ExitApp
IfMsgBox, Cancel
return
}
IfExist %CurrentFileName%
{
msgbox, 3, AhkWikiPad - exit, Save %CurrentFileName% before exit?,
IfMsgBox, Yes
{
FileDelete %CurrentFileName%
if ErrorLevel <> 0
{
MsgBox The attempt to overwrite "%CurrentFileName%" failed.
return
}
GuiControlGet, MainEdit
FileAppend, %MainEdit%, %CurrentFileName%
ExitApp
}
IfMsgBox, No
ExitApp
IfMsgBox, Cancel
return
}
return

Exit:
ExitApp


Perhaps I should use one large text file with labels for wiki pages, rather than separate text files.

On the other hand, I should perhaps not waste any more time on it, as it will never become a serious contender.

1. Notetab cannot do these things either. See Notetab as a Plain-Text Wiki.

4 comments:

L. S. Russell Blogger Beta said...

Great work. But, why not use gVim? gVim will allow you to use jump tags as a wiki of sorts. Of course it is a little more work than an actual wiki but much more adaptive.

MK said...

Thanks for the comments. I have played with GVim (and Emacs org mode).

I am actually using ConnectedText for my personal needs.

I just wanted to see whether it could be done at all (in AHK).

alx said...

You may also find this plain text wiki built within TextMate (Mac) interesting:
http://interconnected.org/home/2007/05/20/plain_text_wiki

MK said...

Yes, I know of it, but I don't own a Mac.