我不知道这个问题是否与主题相关,因为从某种意义上说,这实际上是一个 AppleScript 问题 - 但它也与 TeXShop 有关,它是 TeXLive 发行版的一部分,因此似乎值得一试。欢迎提出更好的提问地点的建议。
我经常使用 TeXShop 的“插入开始/结束”宏,该宏由 CMD-B 调用。如果我equation
在空白行上输入内容并按 CMD-B,它会将其变成
\begin{equation}
|
\end{equation}
其中|
代表光标。
问题是我喜欢缩进代码,但这个宏不尊重这一点。如果我输入以下内容(|
光标在哪里)
\begin{proof}
% ...
equation|
\end{proof}
然后按 CMD-B 我得到
\begin{proof}
% ...
\begin{equation}
|
\end{equation}
\end{proof}
我真正想要的是
\begin{proof}
% ...
\begin{equation}
|
\end{equation}
\end{proof}
所有内容都缩进到与单词equation
原来的相同级别,并且环境的内容使用额外的制表符缩进。
TeXShop 的宏使用 AppleScript,因此原则上应该可以通过编辑宏来实现此行为。但我不是 AppleScript 专家,所以我想知道周围是否有人是,或者谁已经这样做了。
这是插入开始/结束宏的当前代码:
--Applescript direct
-- Script to insert a begin/end block
-- Ramon Figueroa-Centeno March 10, 2009
tell application "TeXShop"
-- The linefeed character.
set linefeed to ASCII character 10
set TeX_delimiters to {linefeed, return, tab} & characters of ¬
" {}[](),:;.\\|/?!<>`'\"@#$%^~&-+=" as list
set the front_document to the front document
-- set the front_document to document #DOCUMENTNAME#
-- The whole text of the document
set whole_document to (the text of the front_document) as string
-- The offset of the selection
set selection_offset to offset of the selection of the front_document
repeat until (selection_offset = 0) or (character selection_offset of ¬
the whole_document is in TeX_delimiters)
set selection_offset to selection_offset - 1
end repeat
set the offset of the selection of the front_document to selection_offset
set selection_length to 0
try
set next_character to character (selection_offset + selection_length + 1) of ¬
the whole_document
repeat until (next_character is in TeX_delimiters)
set selection_length to selection_length + 1
try
set next_character to character (selection_offset + selection_length + 1) of ¬
the whole_document
on error
-- reached the end of the document
exit repeat
end try
end repeat
end try
set the length of the selection of the front_document to selection_length
-- The selection is empty, so stop
if selection_length = 0 then
beep
return
end if
set environment_name to the content of the selection of the front_document
set begin_environment to "\\begin{" & environment_name & "}"
set end_environment to "\\end{" & environment_name & "}"
set environment to begin_environment & linefeed & " " & linefeed & end_environment
set insertion_point to (count of begin_environment) + selection_offset + 3
-- determine if we are the beginning of a line
set at_the_beginning to ((selection_offset = 0) or (character selection_offset of ¬
the whole_document is in {linefeed, return}))
-- determine if we are at the end of a line
-- (since we do not know if we are at the end of the document
-- we use a "try" to avoid having to count the characters of the document)
set at_the_end to false
try
if character (selection_offset + selection_length + 1) of ¬
the whole_document is in {linefeed, return} then
set at_the_end to true
end if
on error
set at_the_end to true
end try
if at_the_beginning and at_the_end then
-- say "at the beginning and the end"
set the content of the selection of the front_document to ¬
environment
set the offset of the selection of the front_document to ¬
insertion_point - 1
else if at_the_beginning then
-- say "at the beginning"
set the content of the selection of the front_document to ¬
environment & linefeed
set the offset of the selection of the front_document to ¬
insertion_point
else if at_the_end then
-- say "at the end"
set the content of the selection of the front_document to ¬
linefeed & environment
set the offset of the selection of the front_document to ¬
insertion_point
else
-- say "at the middle"
set the content of the selection of the front_document to ¬
linefeed & environment & linefeed
set the offset of the selection of the front_document to ¬
insertion_point
end if
end tell
PS我之前问过类似的问题,插入环境时的 TeXShop 缩进,但那只是关于改变环境内文本的缩进,而不是环境本身。我已经解决了这个问题,并将在那里发布一个自我回答。