reddit / emacs

emacs-lisp, how to stay in the same buffer using with-current-buffer

dm_g · 2022-09-14 05:00
I am trying to write a small function that extracts the currently selected text, calls a command line command, and then shows in a tool-tip the output of this command. So far everything works, but sometimes, depending on the size of the output of the shell command, the current buffer is replaced with the buffer that captures the output of the shell command. Is there a way to make sure that the buffer stays the same and it is not replaced with the shell output? This is my function. Thank you in advance: (defun jp-define () (interactive) (let ( (word (buffer-substring-no-properties (region-beginning) (region-end)))) (shell-command (format "tango '%s'" word) "*tango*") (let ( (content (with-current-buffer "*tango*" (buffer-substring-no-properties (point-min) (point-max)) ))) (text-translator-pos-tip-show content) )))
cretan_bull · 2022-09-14 06:08
> depending on the size of the output of the shell command I'm not sure what's going on with that. But, unless you for some reason want the output buffer of the command hanging around, I'm pretty sure using `with-temp-buffer` will fix your problem: (defun jp-define () (interactive) (let* ((word (buffer-substring-no-properties (region-beginning) (region-end))) (content (with-temp-buffer (shell-command (format "tango '%s'" word) (current-buffer)) (buffer-substring-no-properties (point-min) (point-max))))) (text-translator-pos-tip-show content)))
howardthegeek · 2022-09-14 14:35
That logic is part of ahell-command. Have you seen if shell-command-to-string works better for you?
dm_g · 2022-09-14 20:49
Thank you. I followed your advice and improved the code. it now works as I expected: \- Select region \- Run command, save output \- Show output in tooltip and minibuffer using message \- Create a log of words defined with their definitions in another buffer This might be useful to people looking up words in another language (myougiden is a command line Japanese dictionary; I would like to improve this code to implement yomichan parsing, thus mimicking yomichan behaviour) ​ ​ (defun dmg-jp-tip-show (msg) (pos-tip-show msg ; string nil nil ; pos nil ; window 10 ; timeout )) (defun dmg-jp-define () "show definition of the currently selected word in a tooltip and a message. Keeps a log of searched words in a buffer too. Uses myougiden." (interactive) (save-excursion (let* ( (term (buffer-substring-no-properties (region-beginning) (region-end))) (definition (shell-command-to-string (format "myougiden --human --color=no '%s'" term))) ) (if (> (length definition) 0) (progn (dmg-jp-tip-show definition) (message definition) (with-current-buffer "*tango*" (goto-char (point-max)) (insert (format "\n----Term: %s\n%s" term definition)) ) ) (message (format "Term [%s] not found" term)) ))))
dm_g · 2022-09-14 21:26
Thank you. I wanted to keep the buffer so I could review the output after the fleeting tooltip disappears (if needed). I think shell-command is the one that is responsible for switching current buffer. It seems that if the output is small, it is displayed with message, otherwise it shows the buffer in a window. I'll have to read the code of shell-command to understand what is going on.
7890yuiop · 2022-09-15 12:41
Note that `with-current-buffer` has *nothing at all* to do with which buffers are displayed. You use it in order to make a particular buffer current for processing -- it makes no difference whether or not that buffer is displayed in a window.