Code Monkey home page Code Monkey logo

Comments (6)

saf-dmitry avatar saf-dmitry commented on August 16, 2024 3

Maybe this code can help:

(defun my-deft-cd ()
  "Run Deft in selected directory.
Prompt the user for a directory and run Deft in it."
  (interactive)
  (let ((deft-directory (expand-file-name (read-directory-name "Deft directory: "))))
    (if (get-buffer deft-buffer)
        (and (switch-to-buffer deft-buffer) (deft-refresh))
      (deft))))

from deft.

bulak avatar bulak commented on August 16, 2024 1

I was also hoping for something similar. I would like to set the deft-directory per project using .dir-locals.el. However, it seems somehow the value of this variable can not be changed by .dir-locals.el and always remains as the value set during init. Is there any way to achieve this?

from deft.

EFLS avatar EFLS commented on August 16, 2024

As far as I recall, it is possible to change it during runtime with a simple setq. Make sure to hit deft-refresh after changing.

As for setting it as a local variable, I'm not sure how that would work.

from deft.

Zyrohex avatar Zyrohex commented on August 16, 2024

@hubgitname - Sorry for commenting on an old issue, but I stumbled upon this issue while I was looking for a solution. I threw together something real fast to make this work. It'll require projectile to work. Note it'll only work on a repo project. It also relies on a doom-emacs function to get your project-root directory which I provided below.

What this does is change your deft-directory to the projectile-project root directory and refreshes the deft directory when you switch projects.

(defun doom-project-root (&optional dir)
  "Return the project root of DIR (defaults to `default-directory').
Returns nil if not in a project."
  (let ((projectile-project-root (unless dir projectile-project-root))
        projectile-require-project-root)
    (projectile-project-root dir)))

Then add this function that'll set your deft-directory to your current project-root

(defun zyro/projectile-update-deft-directory ()
  "Checks if directory is projectile-project and updates DEFT directory to the projects root folder."
  (interactive)
  (if (projectile-project-p)
      (setq deft-directory (expand-file-name (doom-project-root)))))

Finally add these 2 hooks to projectile-after-switch-project-hook so it updates the directory and refreshes deft.

(add-hook 'projectile-after-switch-project-hook 'zyro/projectile-update-deft-directory)
(add-hook 'projectile-after-switch-project-hook 'deft-refresh)

from deft.

jacksonbenete avatar jacksonbenete commented on August 16, 2024

Maybe this code can help:

(defun my-deft-cd ()
  "Run Deft in selected directory.
Prompt the user for a directory and run Deft in it."
  (interactive)
  (let ((deft-directory (expand-file-name (read-directory-name "Deft directory: "))))
    (if (get-buffer deft-buffer)
        (and (switch-to-buffer deft-buffer) (deft-refresh))
      (deft))))

This partially works. It opens the directory just right, but if you create a new fie either RET or C-RET, the file is created in the defined "root" directory, in my case .notes/, and not in the current directory.

E.g.
Create a new directory under .notes/ (mkdir foo)
Open Deft, it shows .notes files (default directory).
C-x C-g to run my-deft-cd and change directory to .notes/foo/.
The new directory is empty.
Create new deft file.
Write something. ~ autosave, etc
cd inside .notes/foo and ls, the directory will be empty, the new file was created inside .notes/.

In the next days I will try to solve this, as I also think that this could be a very nice improvement for organization.
It's probably the case to setq the current directory in the correct variable as using let might not be enough (I'm new to Lisp so maybe I'm wrong).
Maybe it's the case to create a temp variable to setq the default directory into it, setq the find directory and when Deft or Emacs closes, setq back the default directory before exit.

from deft.

jacksonbenete avatar jacksonbenete commented on August 16, 2024

This will offer a solution on top of @saf-dmitry code.

(with-eval-after-load "deft"
  (and
   ;; Save temp variable to recover configurations later.
   (setq deft-default-directory deft-directory)
   (defun deft-change-directory ()
     "Run Deft in selected directory from prompt.
      If directory doesn't exists, create."
     (interactive)
     (setq deft-directory (expand-file-name (read-directory-name "Deft directory: ")))
     (unless (file-directory-p deft-directory)
       (make-directory deft-directory t))
     (if (get-buffer deft-buffer)
	 (and (switch-to-buffer deft-buffer) (deft-refresh))
       (deft)))
   (defun restore-deft ()
     "When Deft buffer is killed, recover default deft-directory."
     (interactive)
     (if (string= (buffer-name) deft-buffer)
	 (setq deft-directory deft-default-directory)
       nil)))
  (add-hook 'kill-buffer-hook 'restore-deft)
  (global-set-key (kbd "C-x C-g") 'deft-change-directory))

If you put (setq deft-default-directory deft-directory) inside the deft-change-directory function it will not have a desirable behavior (IMO), I.e. everytime you would close Deft, and open it again, Deft would be in the last visited directory instead on the configured default one.

deft-default-directory will act as a temp variable for the original deft-directory.
deft-change-directory will prompt you to enter a directory by pressing C-x C-g. If the desired directory doesn't exists it will create one just like on find-file. (maybe it would be best to prompt if you want it to be created)
restore-deft will reset the deft-directory for the default path when you kill the Deft buffer.

So you can have something like:

.deft/       ;; for general notes
.deft/book1  ;; specific notes
.deft/book2  ...

Of course Deft is pretty much organized already with it's killer search engine. But sometimes you might want a better organization for different projects and it will solve it just right. Now I'm one step closer to completly migrate from Scrivener to Emacs.

Although it's a good solution for our use case, this might not be a functionality for Deft as the author @jrblevin conceived it, so I will not offer a Pull Request without permission.
But if it solves your problem @hubgitname , you can mark as Solved or let me know if I can improve it.

Edit: Everything is inside eval-after-load because I noticed that some deft variables would not be already bound and would rise an error on a new Emacs instance.

from deft.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.