我正在构建一个与Emacs通信的程序,我面临的挑战之一就是编写Emacs的进程过滤器功能。它的输入字符串是一系列要评估的s表达式。这是一个示例:
(gimme-append-to-buffer "25 - William Christie dir, Les Arts Florissants - Scene 2. Prelude - Les Arts Florissants\n")
(gimme-append-to-buffer "26 - William Christie dir, Les Arts Florissants - Cybele: 'Je Veux Joindre' - Les Arts Florissants\n")
(gimme-append-to-buffer "27 - William Christie dir, Les Arts Florissants - Scene 3. Cybele: 'Tu T'Etonnes, Melisse' - Les Arts Florissants\n")
(gimme-append-to-buffer "28 - William Christie dir, Les Arts Florissants - Cybele: 'Que Les Plus Doux Zephyrs'. Scene 4. - Les Arts Florissants\n")
(gimme-append-to-buffer "29 - William Christie dir, Les Arts Florissants - Entree Des Nations - Les Arts Florissants\n")
(gimme-append-to-buffer "30 - William Christie dir, Les Arts Florissants - Entree Des Zephyrs - Les Arts Florissants\n")
(gimme-append-to-buffer "31 - William Christie dir, Les Arts Florissants - Choeur Des Nations' 'Que Devant Vous' - Les Arts Florissants\n")
(gimme-append-to-buffer "32 - William Christie dir, Les Arts Florissants - Atys: 'Indigne Que Je Suis' - Les Arts Florissants\n")
(gimme-append-to-buffer "33 - William Christie dir, Les Arts Florissants - Reprise Du Choeur Des Nations : 'Que Devant Nous' - Les Arts Florissants\n")
(gimme-append-to-buffer "34 - William Christie dir, Les Arts Flor*emphasized text*issants - Reprise De L'Air Des Zephyrs - Les Arts Florissants\n")
我遇到的第一个问题是,当函数被调用时,字符串在某种程度上没有完全形成,所以写(mapcar 'eval (format "(%s)" input-string))
之类的东西是行不通的。
为了解决第一个问题,我正在使用一个循环。我写的全部功能是:
(defun eval-all-sexps (s)
(loop for x = (ignore-errors (read-from-string s))
then (ignore-errors (read-from-string (substring s position)))
while x
summing (or (cdr x) 0) into position
doing (eval (car x))))
现在出现的第二个问题是函数被调用了两次,输入有点大,首先是有效但部分内容,然后看起来像剩下的数据。
为了解决这个问题,我正在考虑使用垃圾变量来保留循环中剩下的内容,然后将它连接到下一个调用的输入,但我想知道你们是否有任何其他建议如何更优雅地处理这个问题。
谢谢!
答案 0 :(得分:3)
这就是我在shell模式下所做的事情 我在流程输出中查找特殊的记录结束字符串,以了解输出何时完成。在那之前,我把这些块连接起来。
(defun my-shell-exec-filter (process result)
(let ((end-of-result (string-match my-shell-end-of-record-string result)))
(if (and end-of-result
(numberp end-of-result)
(> end-of-result 0))
(setq my-shell-reply
(concat my-shell-reply
(substring (substring result 0 end-of-result) 0 -1)))
(progn
(setq my-shell-reply (concat my-shell-reply result))
(accept-process-output process my-shell-exec-timeout 5)))))
这不太正确,因为记录结束字符串可以在该过滤器fn的多个调用中分割。所以要正确,它应该首先连接,然后检查匹配。但无论如何你都明白了。
它也会递归,因此,对于非常长的输入,它可能会导致堆栈溢出异常。所以它肯定不像我想的那么干净。
事情已经开始了
(setq my-shell-reply nil)
(set-process-filter proc my-shell-exec-filter)
(process-send-string proc "whatever...")
(if (not (accept-process-output proc my-shell-exec-timeout 100))
(error "unexpected response."))
;; examine my-shell-reply here ...
答案 1 :(得分:2)
我的最终解决方案是:
(defun eval-all-sexps (s)
(let ((s (concat gimme-filter-remainder s)))
(setq gimme-filter-remainder
(loop for x = (ignore-errors (read-from-string s))
then (ignore-errors (read-from-string (substring s position)))
while x
summing (or (cdr x) 0) into position
doing (eval (car x))
finally (return (substring s position))))))