约定

操作要定义 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。其余的会连同行号一起拒掉。

类型

  • int
  • float
  • str
  • bool
  • None
  • list
  • tuple
  • dict
  • set

关键字

  • if
  • elif
  • else
  • while
  • for
  • in
  • def
  • return
  • break
  • continue
  • pass
  • True
  • False
  • None
  • and
  • or
  • not
  • lambda
  • try
  • except
  • finally
  • raise
  • import
  • from
  • as
  • is
  • del
  • global

内置函数

  • len
  • str
  • repr
  • print
  • int
  • float
  • bool
  • abs
  • round
  • min
  • max
  • sum
  • range
  • sorted
  • reversed
  • list
  • dict
  • set
  • tuple
  • enumerate
  • zip
  • ord
  • chr
  • any
  • all
  • type
  • map
  • filter
  • divmod
  • pow
  • hex
  • bin
  • oct
  • format
  • isinstance
  • callable

字符串方法

  • upper
  • lower
  • casefold
  • title
  • capitalize
  • swapcase
  • strip
  • lstrip
  • rstrip
  • replace
  • split
  • rsplit
  • splitlines
  • join
  • startswith
  • endswith
  • find
  • rfind
  • index
  • rindex
  • count
  • partition
  • rpartition
  • zfill
  • ljust
  • rjust
  • center
  • isdigit
  • isnumeric
  • isalpha
  • isalnum
  • isspace
  • isupper
  • islower
  • istitle
  • removeprefix
  • removesuffix
  • format

列表方法

  • append
  • extend
  • pop
  • insert
  • remove
  • index
  • count
  • sort
  • reverse
  • clear
  • copy

字典方法

  • keys
  • values
  • items
  • get
  • pop
  • popitem
  • setdefault
  • update
  • clear
  • copy

集合方法

  • add
  • discard
  • remove
  • pop
  • clear
  • copy
  • update
  • union
  • intersection
  • difference
  • symmetric_difference
  • issubset
  • issuperset
  • isdisjoint

元组方法

  • count
  • index

正则匹配对象的方法

  • group
  • groups
  • groupdict
  • start
  • end
  • span

异常

  • Exception
  • ValueError
  • TypeError
  • KeyError
  • IndexError
  • NameError
  • ZeroDivisionError
  • AttributeError
  • RuntimeError
  • StopIteration

拒绝

  • class
  • with
  • nonlocal
  • assert
  • yield
  • async
  • await
  • match

命名空间

可以导入的命名空间只有下面这些,别无其他。它们都只做计算:脚本够不到文件系统、网络,也够不到任何进程。

import json

  • dumps
  • loads

import math

  • acos
  • asin
  • atan
  • atan2
  • ceil
  • comb
  • copysign
  • cos
  • degrees
  • e
  • exp
  • fabs
  • factorial
  • floor
  • fmod
  • gcd
  • hypot
  • inf
  • isfinite
  • isinf
  • isnan
  • lcm
  • log
  • log10
  • log2
  • nan
  • pi
  • pow
  • prod
  • radians
  • sin
  • sqrt
  • tan
  • tau
  • trunc

import random

  • choice
  • choices
  • randint
  • random
  • randrange
  • sample
  • seed
  • shuffle
  • uniform

import re

  • DOTALL
  • I
  • IGNORECASE
  • M
  • MULTILINE
  • S
  • escape
  • findall
  • finditer
  • fullmatch
  • match
  • search
  • split
  • sub
  • subn

import sys

  • version

import time

  • format
  • monotonic
  • parts
  • time
import re

def transform(text):
    return re.sub(r"\s+", " ", text).strip()
用正则把连成串的空白压成一个。

正则引擎是 Clink 自己写的,它和脚本其余部分共用同一份步数预算,所以那种本来会算到天荒地老的模式,结果是报错,而不是键盘不再响应。不支持反向引用和前后查找。

为什么不用 CPython

键盘扩展有一条很硬的内存上限,越过去 iOS 会毫无预告地结束它。完整的 Python 运行时挤不进键盘旁边,所以 Clink 带的是一个为此写的小解释器:没有 import,没有文件,没有网络,还有一份步数额度,用来终止失控的脚本,而不是键盘。

print 输出到编辑器里的控制台,绝不会进入你正在写的内容。

分享

“共享操作”会写出 .clinkext 文件,“导入操作”再把它以新的 id 取回。一个操作应小于 48,000 字节,并且必须定义 def transform(。

创作器 ›
在 App Store 下载