[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Elisp: can't read buffer content with with-current-buffer

From: Tassilo Horn
Subject: Re: Elisp: can't read buffer content with with-current-buffer
Date: Thu, 02 Jun 2022 22:33:08 +0200
User-agent: mu4e 1.7.26; emacs 29.0.50

Alessandro Bertulli <> 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))


reply via email to

[Prev in Thread] Current Thread [Next in Thread]