约定
操作要定义 transform(text)。它返回的内容会转成文本,插入到你正在输入的位置。
def transform(text):
return text.upper()返回 None 就什么都不插入。想快速试一下,也可以不写函数,直接在顶层给 result 变量赋值。
文本从哪里来
| 输入 | 操作收到的内容 |
|---|---|
| 无 | 从头生成文本。transform("")。 |
| 当前单词 | 将正在输入的单词传给 transform(text)。 |
| 选中内容 | 传入你选中的文本;没有选中内容时不传入任何内容。 |
| 当前行 | 传入光标所在的那一行。 |
| 当前句子 | 传入光标所在的那个句子。 |
| 光标前的文本 | 传入光标前的所有内容。 |
| 光标之后的文本 | 传入光标之后的全部内容。 |
| 整个输入框 | 传入整个输入框的内容,包括光标前后。 |
| 剪贴板 | 传入剪贴板文本。需要完全访问。 |
“替换输入”决定输出会不会顶掉输入原来的位置。凡是读取输入框的来源都适用。“无”和“剪贴板”在文档里没有可替换的东西,它们的输出总是插入。
def transform(text):
words = len(text.split())
return f"{words} words"请键盘代劳
动作除了返回一个字符串,也可以用和面板一样的命令。要做不止一处修改、或者把光标停在特定位置,就靠它。
def transform(text):
replace(len(text), "(" + text + ")")
move_cursor(-1)toast() 和 close() 是面板的东西:动作没有自己的界面来显示消息。不过在编写时,两者都会出现在运行控制台里。
这门语言
脚本跑在 Clink 自己的解释器上,而不是 CPython。它读得懂 Python 里好用的那一部分:推导式、lambda、元组、集合、f-string 格式、try 和 except。其余的会连同行号一起拒掉。
类型
intfloatstrboolNonelisttupledictset
关键字
ifelifelsewhileforindefreturnbreakcontinuepassTrueFalseNoneandornotlambdatryexceptfinallyraiseimportfromasisdelglobal
内置函数
lenstrreprprintintfloatboolabsroundminmaxsumrangesortedreversedlistdictsettupleenumeratezipordchranyalltypemapfilterdivmodpowhexbinoctformatisinstancecallable
字符串方法
upperlowercasefoldtitlecapitalizeswapcasestriplstriprstripreplacesplitrsplitsplitlinesjoinstartswithendswithfindrfindindexrindexcountpartitionrpartitionzfillljustrjustcenterisdigitisnumericisalphaisalnumisspaceisupperisloweristitleremoveprefixremovesuffixformat
列表方法
appendextendpopinsertremoveindexcountsortreverseclearcopy
字典方法
keysvaluesitemsgetpoppopitemsetdefaultupdateclearcopy
集合方法
adddiscardremovepopclearcopyupdateunionintersectiondifferencesymmetric_differenceissubsetissupersetisdisjoint
元组方法
countindex
正则匹配对象的方法
groupgroupsgroupdictstartendspan
异常
ExceptionValueErrorTypeErrorKeyErrorIndexErrorNameErrorZeroDivisionErrorAttributeErrorRuntimeErrorStopIteration
拒绝
classwithnonlocalassertyieldasyncawaitmatch
命名空间
可以导入的命名空间只有下面这些,别无其他。它们都只做计算:脚本够不到文件系统、网络,也够不到任何进程。
import json
dumpsloads
import math
acosasinatanatan2ceilcombcopysigncosdegreeseexpfabsfactorialfloorfmodgcdhypotinfisfiniteisinfisnanlcmloglog10log2nanpipowprodradianssinsqrttantautrunc
import random
choicechoicesrandintrandomrandrangesampleseedshuffleuniform
import re
DOTALLIIGNORECASEMMULTILINESescapefindallfinditerfullmatchmatchsearchsplitsubsubn
import sys
version
import time
formatmonotonicpartstime
import re
def transform(text):
return re.sub(r"\s+", " ", text).strip()正则引擎是 Clink 自己写的,它和脚本其余部分共用同一份步数预算,所以那种本来会算到天荒地老的模式,结果是报错,而不是键盘不再响应。不支持反向引用和前后查找。
为什么不用 CPython
键盘扩展有一条很硬的内存上限,越过去 iOS 会毫无预告地结束它。完整的 Python 运行时挤不进键盘旁边,所以 Clink 带的是一个为此写的小解释器:没有 import,没有文件,没有网络,还有一份步数额度,用来终止失控的脚本,而不是键盘。
print 输出到编辑器里的控制台,绝不会进入你正在写的内容。