jrblevin / deft

Deft for Emacs
http://jblevins.org/projects/deft/
708 stars 86 forks source link

[question / feature request] using deft in a custom link? #52

Open mediapathic opened 6 years ago

mediapathic commented 6 years ago

I am trying to implement a zettlekasten-like system in emacs using deft and org-mode. I'm currently making a custom link type that allows for seeing all items that match that id rather than linking to a file directly. Put another way: I want a custom link type that has the effect of executing deft with the contents of the link being the search string. I feel like I essentially understand how to create a custom link type, but calling deft with an argument doesn't work. My code currently looks like this:

(org-add-link-type
               "id" 'zettel/deft-search-link)

(defun zettel/deft-search-link (uid)
; "run deft, hopefully with uid as a search term"
; "FIXME: 'deft' does not take an argument. Figure out how to do this, ask the author?"
(deft uid))

My question then, is, is there a function within deft that I could be calling that does take an argument and opens a deft buffer (preferably in other window)? If not, is it easy to modify deft to do so? Alternately, am I just going about this in a ridiculous way and there's already an easier solution?

Thanks!

EFLS commented 6 years ago

I have also been thinking about using deft to manage a Zettelkasten-like system. No clue as to how something like this could be implemented though.

Currently, I am creating an org-id for each header to link files together, but a true implementation does indeed require searching for unique IDs of files, rather than headers.

mediapathic commented 6 years ago

@EFLS I'm doing something very similar. I'm creating my ids using the 201804021450 timestamp convention, which I have bound to an abbrev. Currently I'm copy-pasting ids into a deft field. I'd like a more efficient way of doing a bunch of things here. 😄

(Is there a place we could compare notes where we're not off-topic, if you've gotten farther on this problem than I have?)

EFLS commented 6 years ago

A function that would allow easier searching with deft would indeed be useful.

You also might want to check out the org-brain package, which looks promising (and which works nicely parallel to deft): https://github.com/Kungsgeten/org-brain

saf-dmitry commented 6 years ago

Apparently, we can use deft-filter function for this (see also #54).

The first function provides an API for global search in Deft notes (can be added to Deft mode):

(defun deft-search-global (str)
  "Call Deft search on filter string STR."
  ;; Sanitize the filter string
  (setq str (replace-regexp-in-string "[[:space:]]+" " " str))
  ;; Call Deft search on the filter string
  (let ((deft-incremental-search t)) (deft) (deft-filter str t))
  ;; If there is a single match, open the file
  (when (= (length deft-current-files) 1)
    (deft-open-file (car deft-current-files))))

The second function calls deft-global-searchon wiki link, hashtag, or word at point and can be modified to understand your specific syntax:

(defun deft-search ()
  "Call Deft search on thing at point.
Thing can be a double-bracketed link, a hashtag, or a word."
  (interactive)
  (require 'thingatpt)
  (let* ((link-re "\\[\\[\\([^]]+\\)\\]\\]")
         (htag-re "\\([#@]\\(?:[[:alnum:]_-]+:\\)?[[:alnum:]_-]+\\)")
         (string (cond
                  ((thing-at-point-looking-at link-re)
                   (match-string-no-properties 1))
                  ((thing-at-point-looking-at htag-re)
                   (match-string-no-properties 1))
                  (t (thing-at-point 'word t)))))
    (if string
        (deft-search-global string)
      (user-error "No search term at point"))))
EFLS commented 6 years ago

Many thanks @saf-dmitry.

I'm fairly new to elisp, but I've taken your suggestion and made my own set of functions, attempting to expand deft into a Zettelkasten system.

Check it out here if you're interested: https://github.com/EFLS/zetteldeft

@mediapathic, check it out if you're still looking for a Zettelkasten setup based on deft.