this post was submitted on 21 Nov 2023
1 points (100.0% liked)

Emacs

311 readers
1 users here now

A community for the timeless and infinitely powerful editor. Want to see what Emacs is capable of?!

Get Emacs

Rules

  1. Posts should be emacs related
  2. Be kind please
  3. Yes, we already know: Google results for "emacs" and "vi" link to each other. We good.

Emacs Resources

Emacs Tutorials

Useful Emacs configuration files and distributions

Quick pain-saver tip

founded 1 year ago
MODERATORS
 

I added an embark config from: https://karthinks.com/software/fifteen-ways-to-use-embark/#open-any-buffer-by-splitting-any-window, which works really good, the only problem is that if a file is opened with project-find-file-in, it doesn't respect its own directory.

Steps:

  1. When the default-directory set to ~/, M-x browse-emacs-config to open a file list of ~/.config/emacs in the minibuffer
  2. embark-act on a file lisp/init-lib.el
  3. o and b to open the selected file horizontally
  4. A buffer opened on the right side, but the directory is still using the default-directory in step 1 instead of the buffer's directory, causing the buffer to visit ~/lisp/init-lib.el instead of ~/.config/emacs/lisp/init-lib.el.

Config:

(defun find-file-in-project (&optional dir)
  (interactive)
  (let ((project (project-current nil dir)))
    (project-find-file-in nil nil project)))

(defun browse-emacs-config ()
  (interactive)
  (find-file-in-project "~/.config/emacs"))

(eval-when-compile
  (defmacro my/embark-aw-action (fn)
    `(defun ,(intern (concat "my/embark-aw-" (symbol-name fn))) ()
       ,(format "Open %s buffer selected with ace-window" (symbol-name fn))
       (interactive)
       (with-demoted-errors "%s"
         (require 'ace-window)
         (let ((aw-dispatch-always t))
           (aw-switch-to-window (aw-select nil))
           (call-interactively (symbol-function ',fn)))))))

(define-key embark-file-map (kbd "o") (my/embark-aw-action find-file))
top 5 comments
sorted by: hot top controversial new old
[–] nullmove@alien.top 1 points 11 months ago (2 children)

I guess given that I see file metadata in marginalia correctly, project-find-file-in does call find-file with appropriate dynamic binding for default-directory, so I suspect the way embark works (or due to other stuffs, perhaps /u/oantolin can explain), it cannot continue from that scope, so by the time embark starts the old dynamic scope has already been unwound?

Anyway, you can just wrap that function call with your own binding that covers embark:

(defun find-file-in-project (&optional dir)
  (interactive)
  (when-let* ((project (project-current nil dir))
              (default-directory (project-root project)))
    (project-find-file-in nil nil project)))
[–] goofansu@alien.top 1 points 11 months ago

Thank you! You're right, the scope is not transformed, set default-directory in the middle function works.

[–] goofansu@alien.top 1 points 11 months ago

Thank you! You're right, the scope is not transformed, set default-directory in the middle function works.

[–] vm7_69@alien.top 1 points 11 months ago (1 children)

Hey there! It looks like you might need to adjust the default-directory when using embark-act. Have you considered using with-current-buffer to set the directory to the one of the selected file before invoking embark-act? This might help ensure that the buffer's directory is respected. Cheers!

[–] goofansu@alien.top 1 points 11 months ago

Hi, do you mean with-current-buffer in my/embark-aw-action? I don't see how to pass the selected buffer to the function.