I edit/write/manipulate a lot of XML files and I'd like to create a simple way to use XPath for searching/selecting in the current document. Ideally, I'd open a textfield akin to the "Incremental Search" (CTRL+S) field and just write the expression, hit enter and the results would open in a "Web Preview" window.
I can get the durrent document's contents and do the XPath stuff, but I don't know how to hi-jack (if at all possible) the Incremental Search bar, or create something that worked similar.
Any clues for how to approach that?
An alternative way would be to just open a $DIALOG, asking for the XPath expression and then do the processing - but I can't see a $DIALOG command that lets me do that (read input)...
-- Chriztian
On Mar 13, 2014, at 1:47 PM, Chriztian Steinmeier wrote:
I edit/write/manipulate a lot of XML files and I'd like to create a simple way to use XPath for searching/selecting in the current document. Ideally, I'd open a textfield akin to the "Incremental Search" (CTRL+S) field and just write the expression, hit enter and the results would open in a "Web Preview" window.
I can get the durrent document's contents and do the XPath stuff, but I don't know how to hi-jack (if at all possible) the Incremental Search bar, or create something that worked similar.
Any clues for how to approach that?
Hi Chriztian,
well, if you want to come up with the result in an HTML window then I'd go with BASH/Perl/Ruby/Python/... + HTML+JavaScript, i.e. create a tmcommand which creates an HTML page containing an INPUT field, the JS onChange event of that INPUT field triggers a JS function which take the XPATH, validates it and applies it to the document (use TextMate.system() JS-to-TM/BASH-bridge) and show the result, maybe via AXAJ injection, within the HTML window.
Cheers, --Hans
On 13 Mar 2014, at 19:47, Chriztian Steinmeier wrote:
[…] An alternative way would be to just open a $DIALOG, asking for the XPath expression and then do the processing - but I can't see a $DIALOG command that lets me do that (read input)...
Using the ruby UI wrapper (for DIALOG), such command could take the following form:
#!/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -wKU require "#{ENV['TM_SUPPORT_PATH']}/lib/exit_codes" require "#{ENV['TM_SUPPORT_PATH']}/lib/ui"
query = TextMate::UI.request_string(:prompt => "XPath Query:") TextMate.exit_discard if query.to_s.empty?
# output HTML based on stdin and query…
You can mix this with the suggestion by Hans-Jörg, i.e. let the generated HTML include a text field prepopulated with the query string, allowing the user to change the query.
If so, you may want to allow an initial empty query string, for that, change ‘query.to_s.empty?’ → ‘query.nil?’ in the above. This will still allow the user to abort the command (and never see the HTML output window) via the Cancel button (or ⌘,) but if they just press return, the window will appear.