Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions app/liii/pi/app/interactive/clipboard-files.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
;;
;; Copyright (C) 2026 The Goldfish Scheme Authors
;;
;; Licensed under the MIT License
;;

(define-library (liii pi app interactive clipboard-files)
(export pi-clipboard-files)
(import (scheme base)
(liii either)
(liii list)
(liii os)
(liii path)
(liii string)
(liii subprocess)
(liii uri record)
) ;import
(begin
;; Match Pi's AppKit file-URL reader. Emit URLs, not raw paths, so a
;; filename containing a newline cannot become two clipboard entries.
(define %macos-files-script
(string-append "ObjC.import('AppKit');"
" const files = $.NSPasteboard.generalPasteboard.readObjectsForClassesOptions("
"$.NSArray.arrayWithObject($.NSURL.class), $({NSPasteboardURLReadingFileURLsOnlyKey: true}));"
" const urls = []; for (let i = 0; i < files.count; i++)"
" urls.push(ObjC.unwrap(files.objectAtIndex(i).absoluteString)); urls.join('\\n');"
) ;string-append
) ;define

(define %windows-files-script
(string-append "[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false);"
" Add-Type -AssemblyName System.Windows.Forms;"
" [System.Windows.Forms.Clipboard]::GetFileDropList() | ForEach-Object { $_ }"
) ;string-append
) ;define

(define (%read-command argv)
(let ((result (run-either argv :timeout 2 :stderr 'discard)))
(and (either-right? result) (to-right result))
) ;let
) ;define

(define (%clipboard-backend)
(cond ((os-macos?) 'macos)
((os-windows?) 'windows)
((and (os-linux?) (getenv "WAYLAND_DISPLAY")) 'wayland)
((and (os-linux?) (getenv "DISPLAY")) 'x11)
(else #f)
) ;cond
) ;define

(define (%clipboard-command backend)
(case backend
((macos) (list "osascript" "-l" "JavaScript" "-e" %macos-files-script))
((windows)
(list "powershell" "-NoProfile" "-NonInteractive" "-STA" "-Command"
%windows-files-script
) ;list
) ;
((wayland) '("wl-paste" "--no-newline" "--type" "text/uri-list"))
((x11) '("xclip" "-selection" "clipboard" "-t" "text/uri-list" "-o"))
(else #f)
) ;case
) ;define

(define (%local-url-path url)
(let ((encoded (cond ((string-starts? url "file:///") (substring url 7))
((string-starts? url "file://localhost/") (substring url 16))
(else #f)
) ;cond
) ;encoded
) ;
(and encoded
(not
(string-any (lambda (ch) (or (char=? ch #\?) (char=? ch #\#))) encoded)
) ;not
;; uri-decode is also used for form data, where + means space.
(uri-decode (string-replace encoded "+" "%2B"))
) ;and
) ;let
) ;define

(define (%control-char? ch)
(let ((n (char->integer ch)))
(or (< n 32) (<= 127 n 159))
) ;let
) ;define

(define (%windows-absolute? p)
(and (> (string-length p) 2)
(let ((drive (string-ref p 0)))
(or (char<=? #\A drive #\Z) (char<=? #\a drive #\z))
) ;let
(char=? (string-ref p 1) #\:)
(memv (string-ref p 2) '(#\\ #\/))
) ;and
) ;define

(define (%existing-path entry windows? regular?)
(catch #t
(lambda ()
(let ((p (if windows? entry (%local-url-path entry))))
(and p
(not (string-any %control-char? p))
(if windows? (%windows-absolute? p) (not (string-starts? p "//")))
(regular? p)
p
) ;and
) ;let
) ;lambda
(lambda _ #f)
) ;catch
) ;define

(define* (pi-clipboard-files (backend (%clipboard-backend))
(runner %read-command)
(regular? path-file?)
) ;pi-clipboard-files
(let* ((command (%clipboard-command backend))
(out
(and command (catch #t (lambda () (runner command)) (lambda _ #f)))
) ;out
) ;
(and (string? out)
(let* ((lines (string-split (string-replace out "\r\n" "\n") #\newline))
(paths
(filter string?
(map (lambda (line) (%existing-path line (eq? backend 'windows) regular?))
lines
) ;map
) ;filter
) ;paths
) ;
(and (pair? paths) (string-join paths "\n"))
) ;let*
) ;and
) ;let*
) ;define*
) ;begin
) ;define-library
54 changes: 48 additions & 6 deletions app/liii/pi/app/interactive/run.scm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
pi-app-copy-selection!
) ;export
(import (scheme base)
(scheme char)
(liii unicode)
(liii go)
(liii time)
(liii date-time)
Expand All @@ -18,6 +20,7 @@
(liii tui clipboard)
(liii pi agent queue)
(liii pi app interactive state)
(liii pi app interactive clipboard-files)
(liii pi app interactive questionnaire)
(liii pi app interactive selection)
(liii pi app interactive app)
Expand Down Expand Up @@ -131,12 +134,49 @@
(emergency-exit 0)
) ;define

(define (%pi-app-handle-paste! app clipboard-reader clipboard-image-reader)
(let ((content (or (clipboard-image-reader) (clipboard-reader))))
(define (%shell-file-arg path)
(string-append "'" (string-replace path "'" "'\\''") "'")
) ;define

(define (%separate-file-paths app paths)
(let* ((ed (pi-app-editor app))
(cursor (editor-cursor ed))
(col (cadr cursor))
(line (list-ref (editor-lines ed) (car cursor)))
(before
(and (> col 0) (not (char-whitespace? (utf8-string-ref line (- col 1)))))
) ;before
(after
(and (< col (utf8-string-length line))
(not (char-whitespace? (utf8-string-ref line col)))
) ;and
) ;after
(text
(if (pi-app-bash-mode? app)
(string-join (map %shell-file-arg (string-split paths #\newline)) " ")
paths
) ;if
) ;text
) ;
(string-append (if before " " "") text (if after " " ""))
) ;let*
) ;define

(define (%pi-app-handle-paste! app clipboard-reader clipboard-image-reader
clipboard-files-reader
) ;%pi-app-handle-paste!
(let* ((files (clipboard-files-reader))
(image (and (not files) (clipboard-image-reader)))
(content (cond (files (%separate-file-paths app files))
(image (%separate-file-paths app image))
(else (clipboard-reader))
) ;cond
) ;content
) ;
(when (and (string? content) (not (string-null? content)))
(pi-app-handle-input! app (string-append "\x1b;[200~" content "\x1b;[201~"))
) ;when
) ;let
) ;let*
) ;define

(define* (pi-app-run app
Expand All @@ -145,6 +185,7 @@
(force-exit pi-app-force-exit)
(clipboard-reader native-clipboard-text)
(clipboard-image-reader native-clipboard-image-path)
(clipboard-files-reader pi-clipboard-files)
) ;pi-app-run
(let* ((term (pi-app-term app))
(ed (pi-app-editor app))
Expand Down Expand Up @@ -199,10 +240,11 @@
) ;else
) ;cond
) ;
;; Ctrl+V:优先尝试读取外部剪贴板图片(图片转临时文件路径并粘贴);
;; 无图片时读取文本,复用编辑器的 bracketed paste 处理
;; Ctrl+V:依次尝试文件列表、原始图片、纯文本。
((and (not (pi-app-active-selector app)) (eq? k 'ctrl-v))
(%pi-app-handle-paste! app clipboard-reader clipboard-image-reader)
(%pi-app-handle-paste! app clipboard-reader
clipboard-image-reader clipboard-files-reader
) ;%pi-app-handle-paste!
(pi-app-render! app)
(loop)
) ;
Expand Down
17 changes: 17 additions & 0 deletions app/tests/liii/pi/app/interactive/clipboard-files/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# (liii pi app interactive clipboard-files) 模块函数索引

`(liii pi app interactive clipboard-files)` 读取系统剪贴板中的本地文件引用,不读取文件内容。

```scheme
(import (liii pi app interactive clipboard-files))
```

## 查看函数文档

```bash
gf doc liii/pi/app/interactive/clipboard-files "pi-clipboard-files"
```

## 函数分类索引

- `pi-clipboard-files`:将有效的本地路径按换行连接;未读到有效引用时返回 `#f`。
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
;; pi-clipboard-files
;; 从平台文件列表生成编辑器路径文本,不读取文件内容。
;;
;; 语法
;; ----
;; (pi-clipboard-files [backend runner regular?])
;;
;; 参数
;; ----
;; backend : 平台符号;runner : 接受 argv 并返回剪贴板表示;regular? : 本地文件谓词。
;;
;; 返回值
;; ----
;; 路径之间用换行分隔;无有效引用或读取失败时返回 #f。

(import (scheme base) (liii check) (liii pi app interactive clipboard-files))
(check-set-mode! 'report-failed)

(define (sample backend command payload paths)
(pi-clipboard-files backend
(lambda (argv) (and (string=? command (car argv)) payload))
(lambda (p) (member p paths))
) ;pi-clipboard-files
) ;define

(define urls
"# comment\r\nfile:///tmp/A%20B.png\r\nfile://localhost/tmp/%E4%B8%AD+.txt\r\nfile:///missing\r\nfile://remote/tmp/A%20B.png\r\nfile:///bad%0Aname\r\nfile:///bad%zz"
) ;define

(define paths '("/tmp/A B.png" "/tmp/中+.txt" "/bad\nname"))
(for-each
(lambda (entry)
(check (sample (car entry) (cadr entry) urls paths)
=>
"/tmp/A B.png\n/tmp/中+.txt"
) ;check
) ;lambda
'((macos "osascript") (wayland "wl-paste") (x11 "xclip"))
) ;for-each
(check (sample 'windows
"powershell"
"C:\\A B.png\r\nD:\\中.txt\r\n"
'("C:\\A B.png" "D:\\中.txt")
) ;sample
=>
"C:\\A B.png\nD:\\中.txt"
) ;check
(check (sample 'wayland "wl-paste" "file:///missing\n" '()) => #f)
(check (sample 'macos "osascript" #f '()) => #f)

(check-report)
Loading
Loading