How to write a decent process filter?

I am creating a program that interacts with Emacs, and one of the problems I am facing is writing the Emacs process filter function. Its input string is a series of s-expressions to evaluate. Here is an example:

(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")

The first problem that I encountered is that the string is somehow not completely formed when the function is called that way, so writing something of this type (mapcar 'eval (format "(%s)" input-string))will not work.

To deal with this first problem, I used a loop. The complete function I wrote is:

 (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))))

Now a second problem has appeared: the function is called twice with several large entries, first with actual but partial content, and then with what looks like parts of the remaining data.

, , , , , , , .

!

+3
2

, , .
, , . .

(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 ... 
+3

:

(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))))))
+2

Source: https://habr.com/ru/post/1746105/


All Articles