lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sat, Sep 8, 2012 at 11:18 PM, Rena <hyperhacker@gmail.com> wrote:

> The problem with simple string searching is it fails easily:
>
> if label_exists('foo') then
>   error("oops, the string '::foo::' occurs in this file")
> end
>
> --::bar:: doThingsWeDontNeedRightNow()
> if label_exists('bar') then
>   error("oh no!")
> end

It's not so problematic in the particular use case I'm looking at if
the colons are included in the pattern to search for. E.g., "::foo::",
 "::bar::". This works because I'm searching the script's plain text
fetched as a variable.

The outliner that embeds Lua, NoteCase Pro, allows multiple plain text
tags (node metadata) per node. Tags are rather exuberantly useful
because the app has a mode in which arbitrary groups of nodes can be
displayed in a flat (non-hierarchical) list view, All tags used in the
current document are displayed in the Tags Pane, regardless of mode.

I am toying with the concept of using tags as script branchers, by
adding a Ctrl+Enter event trigger that fires if the focus is in the
Tags Pane and a tag is selected when the keyboard action is executed.
The second related enhancement would be a new API that returns the
text of the currently selected tag, Nc_Note_Tag_GetCurrent().

My prototype script that would be assigned to the new event trigger
follows.[1] It seems to work just fine so far. Note that the call to
the hypothesized API is in a long comment, since I don't have that API
to play with. The two lines following that comment are string
assignments that simulate the API's possible returns, one for a tag
that has a corresponding label and one for a tag that does not.

The script presented an unusual problem in that a given tag may or may
not have a corresponding label in the script. So testing whether the
corresponding label exists before branching is necessary.

Also, I wanted to work with the goto statement to make it easier for
novices to drop in and remove scripted actions but the label name in
the goto statement had to be a variable rather than a literal string.
The following two lines work around the goto statement's distaste for
string variables:

strToLoad = "goto strLabelText"
  load(strToLoad)

Best regards,

Paul
______________

[1] Hopefully, the API names' somewhat Hungarian notation will aid in
understanding what they do. :-)

function Test_If_Label_Exists(nScriptDocID, strScriptNoteID, strLabel)

  -- get this script's plain text
  local strContent = Nc_Note_Content_Get(nScriptDocID, strScriptNoteID, 0)

  -- look for label & evaluate
  local nFrom, nTo = string.find(strContent, strLabel)
  if nFrom ~= nil and nTo ~= nil then
    nFoundLabel = 1
  else
    nFoundLabel = 0
  end -- if nFrom

 return nFoundLabel
end -- function

nFoundLabel = nil

-- get script's doc and note IDs
nScriptDocID = Nc_Script_DocID_Get()
strScriptNoteID = Nc_Script_NoteID_Get()

--[[
 local strLabelText = Nc_Note_Tag_GetCurrent()
--]]

strLabelText = 'clone node'
-- strLabelText = 'no corresponding label'

strLabelText = string.gsub(strLabelText, '%s', '')
strLabel = '::' .. strLabelText .. '::'

-- goto label if it exists, else abort
nLabelExists = Test_If_Label_Exists(nScriptDocID, strScriptNoteID, strLabel)
if nLabelExists == 1 then
  strToLoad = 'goto strLabelText'
  load(strToLoad)
else
  goto exit
end

-- begin tag-specific actions

::insertdate::
Nc_GUI_InfoBox('Scripted action for \'insert date\' tag inserts the
date in current note at cursor position.', 1, 'Action')
goto exit

::clonenode::
Nc_GUI_InfoBox('Scripted action for \'clone node\' tag clones the
current note to the HTML clipboard for insertion elsewhere in the same
doc.', 1, 'Action')
goto exit

::eventdate::
Nc_GUI_InfoBox('Scripted action for \'event date\' tag launches pop-up
for adding or deleting the current node's EventDate custom property
and value.', 1, 'Action')
goto exit

-- etc.

::exit::