unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Elisp: can't read buffer content with with-current-buffer
@ 2022-06-02 16:45 Alessandro Bertulli
  2022-06-02 20:33 ` Tassilo Horn
  0 siblings, 1 reply; 4+ messages in thread
From: Alessandro Bertulli @ 2022-06-02 16:45 UTC (permalink / raw)
  To: help-gnu-emacs

Hi all!
I'm trying to set up some custom Elisp function to help me run Java
programs. Since it's possible I have different classes containing a main
method (for instance, when developing multi process or client-server
applications) I wanted to use completing-read. Therefore, I need a
list/function for completion. To do so, I need to scan the project I'm
working on, and search for the main methods that can be run. No problem
here, I can do that using grep and regexp. But then I don't know how to
collect those results in a list!

Specifically, let's say I have the directory "my_project" containing
some Java files. I want a list of strings, each one being one of the
files returned by grep, like:
'("~/my_project/src/main/java/ClassOne.java"
  "~/my_project/src/main/java/ClassTwo.java")

In this function I wrote, however, it seems I can't do that:

(defun get-main-method-list ()
  "Scan the project for main methods to run."
  (interactive)
  (let* ((project-root (projectile-acquire-root))
	 (output-buffer (get-buffer-create "*java-grep*")))
    (async-shell-command (format "grep --include=\\*.java -rnwl \'%s\' -e \'%s\'"
				 project-root
				 "public[[:space:]]\\+static[[:space:]]\\+void[[:space:]]\\+main")
			 output-buffer
			 output-buffer)
    (with-current-buffer output-buffer
      (let ((buffer-content (buffer-string)))
	(print buffer-content) ;; debug
	(set-text-properties 0 (length buffer-content) nil buffer-content)
	(split-string buffer-content "\n")))))

See where I put a debug (print): it prints nothing or newlines, and the
function seems to return '(""). Is this a problem in using
with-current-buffer? If no, what am I getting wrong? Is this the
preferred way to tokenize a buffer into a list of strings?

Thanks for your time, have a nice day!

Alessandro



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Elisp: can't read buffer content with with-current-buffer
  2022-06-02 16:45 Elisp: can't read buffer content with with-current-buffer Alessandro Bertulli
@ 2022-06-02 20:33 ` Tassilo Horn
  2022-06-02 22:48   ` Alessandro Bertulli
  0 siblings, 1 reply; 4+ messages in thread
From: Tassilo Horn @ 2022-06-02 20:33 UTC (permalink / raw)
  To: Alessandro Bertulli; +Cc: help-gnu-emacs

Alessandro Bertulli <alessandro.bertulli96@gmail.com> writes:

Hi Alessandro,

> In this function I wrote, however, it seems I can't do that:
>
> (defun get-main-method-list ()
>   "Scan the project for main methods to run."
>   (interactive)
>   (let* ((project-root (projectile-acquire-root))
> 	 (output-buffer (get-buffer-create "*java-grep*")))
>     (async-shell-command (format "grep --include=\\*.java -rnwl \'%s\' -e \'%s\'"
> 				 project-root
> 				 "public[[:space:]]\\+static[[:space:]]\\+void[[:space:]]\\+main")
> 			 output-buffer
> 			 output-buffer)
>     (with-current-buffer output-buffer
>       (let ((buffer-content (buffer-string)))
> 	(print buffer-content) ;; debug
> 	(set-text-properties 0 (length buffer-content) nil buffer-content)
> 	(split-string buffer-content "\n")))))
>
> See where I put a debug (print): it prints nothing or newlines, and
> the function seems to return '(""). Is this a problem in using
> with-current-buffer?

No.  You call `async-shell-command' which just starts the command and
immediately returns (without waiting for it to finish) and then your
output-buffer hasn't received the grep output yet.  So use
`shell-command' instead.

And instead of using set-text-properties to remove properties, use

  (buffer-substring-no-properties (point-min) (point-max))

Bye,
Tassilo



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Elisp: can't read buffer content with with-current-buffer
  2022-06-02 20:33 ` Tassilo Horn
@ 2022-06-02 22:48   ` Alessandro Bertulli
  2022-06-03  6:25     ` Tassilo Horn
  0 siblings, 1 reply; 4+ messages in thread
From: Alessandro Bertulli @ 2022-06-02 22:48 UTC (permalink / raw)
  To: Tassilo Horn; +Cc: help-gnu-emacs

Thanks Tassilo, it now works!

However, now I have a next problem. I have this function for debug:

(defun run-java-main-method (mainClass)

  (interactive
   (completing-read "Run main method: " (get-main-method-list)))
   (message "Read %s" mainClass)
  )

So that it simply echoes the entered text. Minibuffer completion works
fine (I use Vertico), but when pressing RET I got this error (I report
the debugger output, got by setting debug-on-error to t):

Debugger entered--Lisp error: (wrong-type-argument listp
"/home/alessandro/akka/src/main/java/com/example/Ak...")
  call-interactively(run-java-main-method nil nil)
  command-execute(run-java-main-method)

I don't get it. Where is it expecting a list?


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Elisp: can't read buffer content with with-current-buffer
  2022-06-02 22:48   ` Alessandro Bertulli
@ 2022-06-03  6:25     ` Tassilo Horn
  0 siblings, 0 replies; 4+ messages in thread
From: Tassilo Horn @ 2022-06-03  6:25 UTC (permalink / raw)
  To: Alessandro Bertulli; +Cc: help-gnu-emacs

Alessandro Bertulli <alessandro.bertulli96@gmail.com> writes:

Hi Alessandro,

> Thanks Tassilo, it now works!

Great.

> However, now I have a next problem. I have this function for debug:
>
> (defun run-java-main-method (mainClass)
>
>   (interactive
>    (completing-read "Run main method: " (get-main-method-list)))
>    (message "Read %s" mainClass)
>   )
>
> So that it simply echoes the entered text. Minibuffer completion works
> fine (I use Vertico), but when pressing RET I got this error (I report
> the debugger output, got by setting debug-on-error to t):
>
> Debugger entered--Lisp error: (wrong-type-argument listp
> "/home/alessandro/akka/src/main/java/com/example/Ak...")
>   call-interactively(run-java-main-method nil nil)
>   command-execute(run-java-main-method)
>
> I don't get it. Where is it expecting a list?

A function receives a list of arguments and `interactive' is supposed to
get a list (or a string which is interpreted according its spec).  So
this should do:

(defun run-java-main-method (mainClass)
  (interactive
   (list (completing-read "Run main method: " (get-main-method-list))))
  (message "Read %s" mainClass))

Bye,
Tassilo



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-06-03  6:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-02 16:45 Elisp: can't read buffer content with with-current-buffer Alessandro Bertulli
2022-06-02 20:33 ` Tassilo Horn
2022-06-02 22:48   ` Alessandro Bertulli
2022-06-03  6:25     ` Tassilo Horn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).