m4
. While we're at it, we also add some more handlers to the SubEthaEditTools to try to make a more complete set, with the goal being to write scripts for the mode by using the handlers without directly interacting with SubEthaEdit in the AppleScript.This requires relatively minor changes to the makefile I've been using, but nothing too serious. Also, I construct SubEthaEditTools itself using
m4
, with more general purpose handlers for, e.g., string manipulation placed in their own files. With the more modular structure, some additional minor rewrites of some handlers seems appropriate, which do not warrant specific comment. A concept that is made explicit in the SubEthaEditTools is that of the extended selection. That is, the selection modified either so that the beginning is extended to the start of the first line of the selection, or to the end of the last line or the selection, or both. The extended selection has appeared implicitly several times, so it seems worthwhile to make it explicit. Further, there are now handlers both for extending the selection (forward or backward) and for referring to the extended selection without modifying the actual selection.
Update: I've replaced the
quotedForm
handler with a doubleQuotedForm
handler, to help prevent confusion with the quoted form of
action for AppleScript strings. I've also worked in a few usages of quoted form of
in the various scripts for the LaTeX mode (I didn't know about quoted form of
until recently).Also, I've added a license. It's an MIT-style license, so should be suitably permissive for use by others.
Update 2 (2008/04/23): I've added a few more handlers. Specifically,
modeSetting
, selectionIsEmpty
, and documentIsAvailable
. -- SubEthaEdit Tools
(*
Copyright (c) 2008, Michael J. Barber
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*)
-- Environment management
on modeEnvironment()
join of {"export __CF_USER_TEXT_ENCODING=0x1F5:0x8000100:0x8000100;", "export SEE_MODE_RESOURCES=", doubleQuotedForm for modeResources(), "; ", readEnvironment out of environmentFilePath()} by ""
end modeEnvironment
on modeSetting for envVar
try
tell application "System Events"
tell property list file (my environmentFilePath())
get value of the property list item envVar
end tell
end tell
on error
missing value
end try
end modeEnvironmentSetting
to openEnvironmentSettings()
openEnvironment at environmentFilePath() with settingDefaultEnvironment
end openEnvironment
on environmentFilePath()
tell application "SubEthaEdit" to set modeName to name of the mode of the front document
join of {path to preferences from user domain as string, "de.codingmonkeys.SubEthaEdit.", modeName, "_environment.plist"} by ""
end environmentFileName
-- Manipulation of document text
on documentText()
tell the front document of application "SubEthaEdit" to get the contents
end documentText
on selectionIsEmpty()
tell the front document of application "SubEthaEdit" to get the length of the selection
result is equal to 0
end selectionIsEmpty
to completeSelectedLines()
extendSelection with extendingFront and extendingEnd
end completeSelectedLines
on selectionText()
tell the front document of application "SubEthaEdit" to get the contents of the selection
end selectionText
to setSelectionText to newText
tell application "SubEthaEdit" to set the contents of the selection of the front document to the newText
end setSelectionText
on selectionRange given extendingFront:shouldExtendFront, extendingEnd:shouldExtendEnd
tell the front document of application "SubEthaEdit"
if shouldExtendFront and shouldExtendEnd then
get {startCharacterIndex of the first paragraph of the selection, nextCharacterIndex of the last paragraph of the selection}
else if shouldExtendFront then
get {startCharacterIndex of the first paragraph of the selection, nextCharacterIndex of the selection}
else if shouldExtendEnd then
get {startCharacterIndex of the selection, nextCharacterIndex of the last paragraph of the selection}
else
get {startCharacterIndex of the selection, nextCharacterIndex of the selection}
end if
end tell
end selectionRange
to setSelectionRange to newRange
tell the front document of application "SubEthaEdit"
set selection to newRange
end tell
end setSelectionRange
on extendedSelectionText given extendingFront:shouldExtendFront, extendingEnd:shouldExtendEnd
set {startChar, nextChar} to selectionRange given extendingFront:shouldExtendFront, extendingEnd:shouldExtendEnd
tell the front document of application "SubEthaEdit"
get the contents of characters startChar through (nextChar - 1) as text
end tell
end extendedSelectionText
to extendSelection given extendingFront:shouldExtendFront, extendingEnd:shouldExtendEnd
set {startChar, nextChar} to (selectionRange given extendingFront:shouldExtendFront, extendingEnd:shouldExtendEnd)
setSelectionRange to {startChar, nextChar-1}
end extendSelection
-- Manipulation of document properties
to checkSaveStatus given updating:shouldSave
tell application "SubEthaEdit"
if not (exists path of front document) then
error "You have to save the document first"
end if
if shouldSave and (modified of front document) then
try
save front document
end try
end if
end tell
end checkSaveStatus
on documentIsAvailable()
tell application "SubEthaEdit" to get the count of the documents
result > 0
end documentIsAvailable
to requireNewlineAtEOF()
tell the front document of application "SubEthaEdit"
if "" is equal to the contents of the last paragraph then
-- final line terminated, do nothing
else
set the contents of the last insertion point of the last paragraph to return
end if
end tell
end requireNewlineAtEOF
on documentPath()
tell application "SubEthaEdit" to get the path of the front document
end documentPath
on documentLine()
tell application "SubEthaEdit" to get the startLineNumber of selection of front document
end documentLine
on modeResources()
tell application "SubEthaEdit" to get the resource path of the mode of the front document
end modeResources
-- String Utilities
on replacement of oldDelim by newDelim for sourceString
return join of (tokens of sourceString between oldDelim) by newDelim
end replacement
on tokens of str between delimiters
set oldTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to delimiters
set strtoks to text items of str
set text item delimiters of AppleScript to oldTIDs
return strtoks
end tokens
on join of tokenList by delimiter
set oldTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to delimiter
set joinedString to tokenList as string
set text item delimiters of AppleScript to oldTIDs
return joinedString
end join
on doubleQuotedForm for baseString
quote & baseString & quote
end doubleQuotedForm
on shellTransform of inText for envString through pipeline given alteringLineEndings:altEnds
set shellscript to join of {envString, "pbpaste", "|", pipeline} by space
set the oldClipboard to the clipboard
set the clipboard to the inText
try
set shellresponse to do shell script shellscript altering line endings altEnds
on error errMsg number errNum from badObject
set the clipboard to the oldClipboard
error errMsg number errNum from badObject
end try
set the clipboard to the oldClipboard
shellresponse
end shellTransform
-- Handling of environment settings using a plist file
to writeDefaultEnvironment at envPath
set savedClipboard to the clipboard
set the clipboard to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict/>
</plist>"
try
do shell script "pbpaste > " & (POSIX path of envPath)
on error errMsg number errNum from badObject
set the clipboard to the savedClipboard
error errMsg number errNum from badObject
end try
set the clipboard to the savedClipboard
end writeDefaultEnvironment
to openEnvironment at envFilePath given settingDefaultEnvironment:shouldSetDefault
tell application "System Events"
if not exists file envFilePath
if shouldSetDefault
my writeDefaultEnvironment at envFilePath
else
error ("Can't get environment file " & quote & envFilePath & quote) number -1728
end if
end if
open file envFilePath
end tell
end openEnvironment
to readEnvironment out of plist
readListPair out of plist
environmentString from result
end readEnvironment
to readListPair out of plist
tell application "System Events"
if exists file plist then
tell property list file plist
get {name, value} of every property list item
end tell
else
{{}, {}}
end if
end tell
end readPlist
on environmentString from keyValueListPair
set {plistKeys, plistValues} to keyValueListPair
set accumulator to {}
set oldTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to ""
repeat with i from 1 to number of items in plistKeys
set tokens to {"export ", item i of plistKeys, "=", item i of plistValues, ";"}
copy (tokens as string) to the end of the accumulator
end repeat
set AppleScript's text item delimiters to space
set envString to accumulator as string
set AppleScript's text item delimiters to oldTIDs
envString
end environmentString
(*
Copyright (c) 2008, Michael J. Barber
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*)
-- Environment management
on modeEnvironment()
join of {"export __CF_USER_TEXT_ENCODING=0x1F5:0x8000100:0x8000100;", "export SEE_MODE_RESOURCES=", doubleQuotedForm for modeResources(), "; ", readEnvironment out of environmentFilePath()} by ""
end modeEnvironment
on modeSetting for envVar
try
tell application "System Events"
tell property list file (my environmentFilePath())
get value of the property list item envVar
end tell
end tell
on error
missing value
end try
end modeEnvironmentSetting
to openEnvironmentSettings()
openEnvironment at environmentFilePath() with settingDefaultEnvironment
end openEnvironment
on environmentFilePath()
tell application "SubEthaEdit" to set modeName to name of the mode of the front document
join of {path to preferences from user domain as string, "de.codingmonkeys.SubEthaEdit.", modeName, "_environment.plist"} by ""
end environmentFileName
-- Manipulation of document text
on documentText()
tell the front document of application "SubEthaEdit" to get the contents
end documentText
on selectionIsEmpty()
tell the front document of application "SubEthaEdit" to get the length of the selection
result is equal to 0
end selectionIsEmpty
to completeSelectedLines()
extendSelection with extendingFront and extendingEnd
end completeSelectedLines
on selectionText()
tell the front document of application "SubEthaEdit" to get the contents of the selection
end selectionText
to setSelectionText to newText
tell application "SubEthaEdit" to set the contents of the selection of the front document to the newText
end setSelectionText
on selectionRange given extendingFront:shouldExtendFront, extendingEnd:shouldExtendEnd
tell the front document of application "SubEthaEdit"
if shouldExtendFront and shouldExtendEnd then
get {startCharacterIndex of the first paragraph of the selection, nextCharacterIndex of the last paragraph of the selection}
else if shouldExtendFront then
get {startCharacterIndex of the first paragraph of the selection, nextCharacterIndex of the selection}
else if shouldExtendEnd then
get {startCharacterIndex of the selection, nextCharacterIndex of the last paragraph of the selection}
else
get {startCharacterIndex of the selection, nextCharacterIndex of the selection}
end if
end tell
end selectionRange
to setSelectionRange to newRange
tell the front document of application "SubEthaEdit"
set selection to newRange
end tell
end setSelectionRange
on extendedSelectionText given extendingFront:shouldExtendFront, extendingEnd:shouldExtendEnd
set {startChar, nextChar} to selectionRange given extendingFront:shouldExtendFront, extendingEnd:shouldExtendEnd
tell the front document of application "SubEthaEdit"
get the contents of characters startChar through (nextChar - 1) as text
end tell
end extendedSelectionText
to extendSelection given extendingFront:shouldExtendFront, extendingEnd:shouldExtendEnd
set {startChar, nextChar} to (selectionRange given extendingFront:shouldExtendFront, extendingEnd:shouldExtendEnd)
setSelectionRange to {startChar, nextChar-1}
end extendSelection
-- Manipulation of document properties
to checkSaveStatus given updating:shouldSave
tell application "SubEthaEdit"
if not (exists path of front document) then
error "You have to save the document first"
end if
if shouldSave and (modified of front document) then
try
save front document
end try
end if
end tell
end checkSaveStatus
on documentIsAvailable()
tell application "SubEthaEdit" to get the count of the documents
result > 0
end documentIsAvailable
to requireNewlineAtEOF()
tell the front document of application "SubEthaEdit"
if "" is equal to the contents of the last paragraph then
-- final line terminated, do nothing
else
set the contents of the last insertion point of the last paragraph to return
end if
end tell
end requireNewlineAtEOF
on documentPath()
tell application "SubEthaEdit" to get the path of the front document
end documentPath
on documentLine()
tell application "SubEthaEdit" to get the startLineNumber of selection of front document
end documentLine
on modeResources()
tell application "SubEthaEdit" to get the resource path of the mode of the front document
end modeResources
-- String Utilities
on replacement of oldDelim by newDelim for sourceString
return join of (tokens of sourceString between oldDelim) by newDelim
end replacement
on tokens of str between delimiters
set oldTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to delimiters
set strtoks to text items of str
set text item delimiters of AppleScript to oldTIDs
return strtoks
end tokens
on join of tokenList by delimiter
set oldTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to delimiter
set joinedString to tokenList as string
set text item delimiters of AppleScript to oldTIDs
return joinedString
end join
on doubleQuotedForm for baseString
quote & baseString & quote
end doubleQuotedForm
on shellTransform of inText for envString through pipeline given alteringLineEndings:altEnds
set shellscript to join of {envString, "pbpaste", "|", pipeline} by space
set the oldClipboard to the clipboard
set the clipboard to the inText
try
set shellresponse to do shell script shellscript altering line endings altEnds
on error errMsg number errNum from badObject
set the clipboard to the oldClipboard
error errMsg number errNum from badObject
end try
set the clipboard to the oldClipboard
shellresponse
end shellTransform
-- Handling of environment settings using a plist file
to writeDefaultEnvironment at envPath
set savedClipboard to the clipboard
set the clipboard to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict/>
</plist>"
try
do shell script "pbpaste > " & (POSIX path of envPath)
on error errMsg number errNum from badObject
set the clipboard to the savedClipboard
error errMsg number errNum from badObject
end try
set the clipboard to the savedClipboard
end writeDefaultEnvironment
to openEnvironment at envFilePath given settingDefaultEnvironment:shouldSetDefault
tell application "System Events"
if not exists file envFilePath
if shouldSetDefault
my writeDefaultEnvironment at envFilePath
else
error ("Can't get environment file " & quote & envFilePath & quote) number -1728
end if
end if
open file envFilePath
end tell
end openEnvironment
to readEnvironment out of plist
readListPair out of plist
environmentString from result
end readEnvironment
to readListPair out of plist
tell application "System Events"
if exists file plist then
tell property list file plist
get {name, value} of every property list item
end tell
else
{{}, {}}
end if
end tell
end readPlist
on environmentString from keyValueListPair
set {plistKeys, plistValues} to keyValueListPair
set accumulator to {}
set oldTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to ""
repeat with i from 1 to number of items in plistKeys
set tokens to {"export ", item i of plistKeys, "=", item i of plistValues, ";"}
copy (tokens as string) to the end of the accumulator
end repeat
set AppleScript's text item delimiters to space
set envString to accumulator as string
set AppleScript's text item delimiters to oldTIDs
envString
end environmentString
No comments:
Post a Comment