kaka.farm

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 985c6ce034bdc3f8b364fb84134e6e20a0e88856
parent 8e1ec54d3c8b3d5e6b407c8c092ff6cd65a2f95d
Author: Yuval Langer <yuval.langer@gmail.com>
Date:   Fri,  7 Jul 2023 14:02:57 +0300

Add the ChatGPT explaining and improving my elisp function thing.

Diffstat:
Akaka.farm/call-process-chatgpt.org | 120+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 120 insertions(+), 0 deletions(-)

diff --git a/kaka.farm/call-process-chatgpt.org b/kaka.farm/call-process-chatgpt.org @@ -0,0 +1,120 @@ +#+author: + +* ChatGPT explaining Emacs Lisp + +A perplexed friend asked ChatGPT what my code does. I shared it with +a bunch of people and a person on #systemcrafters-general:matrix.org +then suggested I ask ChatGPT for improvements. + +** Original + +#+begin_src elisp + (progn + (defun kakafarm/call-process-with-string-as-input (program &optional input-string &rest args) + (with-temp-buffer + (let ((our-output-buffer (current-buffer))) + (if input-string + (with-temp-buffer + (let ((our-input-buffer (current-buffer))) + (progn + (erase-buffer) + (insert input-string) + (apply 'call-process-region + (buffer-end -1) + (buffer-end 1) + program + nil + our-output-buffer + nil + args)))) + (apply 'call-process + program + nil + our-output-buffer + nil + args))) + (buffer-string))) + + (kakafarm/call-process-with-string-as-input "cat" + "cat says moo") + (kakafarm/call-process-with-string-as-input "echo" + nil + "echo echo echo")) +#+end_src + +** Explanation by ChatGPT + +#+begin_quote + The code defines a function named kakafarm/call-process-with-string-as-input. This function takes three arguments: program, input-string, and args. It executes a command-line program (program) and returns the output as a string. The input-string argument is optional and allows passing input to the program. + + The function starts by creating a temporary buffer using the with-temp-buffer macro. This buffer will be used to store the output of the command. The variable our-output-buffer is bound to the current buffer. + + Next, there is a conditional statement that checks if an input-string is provided. If it is, another temporary buffer (our-input-buffer) is created to hold the input string. The erase-buffer function clears the our-input-buffer, and the insert function inserts the input-string into the buffer. + + Then, the function call-process-region is called with arguments (buffer-end -1) and (buffer-end 1), which represent the beginning and end of the our-input-buffer. This function executes the specified program with the given input and redirects the output to our-output-buffer. The args are also passed to the call-process-region function. + + If no input-string is provided, the function call-process is used instead. It directly executes the program with the given args, without any input. + + After executing the command, the buffer-string function is called to retrieve the contents of the our-output-buffer. This value is then returned as the result of the function. + + Finally, the function is invoked twice to demonstrate its usage. In the first invocation, the program "cat" is executed with the input string "cat says moo". In the second invocation, the program "echo" is executed without any input, but with the arguments "echo echo echo". + + Overall, this code snippet defines a utility function that simplifies calling command-line programs and capturing their output in Lisp. It demonstrates the flexibility of Lisp in handling strings, buffers, and external processes. +#+end_quote + +** Improvements by ChatGPT +*** Code + +#+begin_src elisp + (defun kakafarm/call-process-with-string-as-input (program &optional input-string &rest args) + (if input-string + (with-temp-buffer + (insert input-string) + (apply 'call-process-region (point-min) (point-max) program nil t nil args)) + (apply 'call-process program nil t nil args))) + + (list (kakafarm/call-process-with-string-as-input "cat" "cat says moo") + (kakafarm/call-process-with-string-as-input "echo" nil "-n" "echo echo echo")) +#+end_src + +*** Explanation + +#+begin_quote + Improvements: + + Removed unnecessary progn wrapper around the defun. + Simplified the logic for handling input strings. No need for nested with-temp-buffer calls and separate buffer variables. + Replaced (buffer-end -1) and (buffer-end 1) with (point-min) and (point-max) respectively for clarity. + Replaced (buffer-string) with t in call-process-region to discard the output buffer and avoid unnecessary memory allocation. + Removed unnecessary nil argument in call-process. + These improvements make the code cleaner and more efficient. +#+end_quote + +** Doom? + +My friend asked if it make sense. Does it? + +*** Running the code + +#+begin_src elisp + (progn ; Adding a progn so I can run both expressions in one go. + (defun kakafarm/call-process-with-string-as-input (program &optional input-string &rest args) + (if input-string + (with-temp-buffer + (insert input-string) + (apply 'call-process-region (point-min) (point-max) program nil t nil args)) + (apply 'call-process program nil t nil args))) + + (list (kakafarm/call-process-with-string-as-input "cat" "cat says moo") + (kakafarm/call-process-with-string-as-input "echo" nil "-n" "echo echo echo"))) + echo echo echo ; The result after running eval-last-sexp (C-x C-e). +#+end_src + +I've expected a string return value of the whole progn expression, yet +what I've got was an insertion to this here buffer. + +We are safe... + +... + +for now.