Kungsgeten / yankpad

Paste snippets from an org-mode file
MIT License
216 stars 18 forks source link

Company mode autocompletion and expansion #15

Closed sid-kurias closed 7 years ago

sid-kurias commented 7 years ago

I have written a company-mode backend for yankpad. This will show yankpad snippets that use key expansion in the drop down. When a candidate is selected it will autocomplete. The repository is here https://github.com/sid-kurias/company-yankpad . You can use it as you see best

Kungsgeten commented 7 years ago

Hi! Good idea about using company, thanks for sharing! It seems like it completes the snippet names though, and not the keywords?

Kungsgeten commented 7 years ago

Perhaps something like this would work better? Now you do not get the full snippet name as completion candidates though.

(defun company-yankpad (command &optional arg &rest ignored)
  (interactive (list 'interactive))
  (case command
    (interactive (company-begin-backend 'company-yankpad))
    (prefix (company-grab-symbol))
    (candidates (delq nil (mapcar
               (lambda (c)
                             (string-match (concat "^\\([[:word:]]+\\)"
                                                   yankpad-expand-separator)
                                           (car c))
                             (let ((snippet (match-string 1 (car c))))
                               (when snippet
                                 (if (string-prefix-p arg snippet t) snippet ))))
               (yankpad-active-snippets))))
    (post-completion (yankpad-expand))
    (duplicates t)))
sid-kurias commented 7 years ago

I see what you mean. I now show the snippet name alongside the keyword in the company drop down. Please let me know if that is what you were referring to.

sid-kurias commented 7 years ago

Sorry did not mean to close it. Must have hit the wrong button.

Kungsgeten commented 7 years ago

Nice work! I'm not quite sure if you want to complete candidates that doesn't have a keyword though? Lets say my yankpad look like this:

* My category
** snippet1
Insert some text (no keyword)
** snippet2: Another snippet, with a keyword!
A different snippet.

Now if I'm in the My category category, and use company-yankpad it will complete both snippet1 and snippet2: Another snippet, with a keyword!. When completing snippet1, however, it will only insert the snippet name, not the content of the snippet.

Perhaps something could be done by using yankpad-insert-snippet-name instead of yankpad-expand, upon post completion?

sid-kurias commented 7 years ago

yankpad-insert-snippet-name? I did not find that anywhere.

I think both names and keywords complete now. I am using yankpad-expand for keywords and yankpad-insert-from-current-category for names. If that is not optimal do let me know.

Kungsgeten commented 7 years ago

Yes, I got the name wrong :) yankpad-insert-from-current-category is what I meant :) It seems to work great now! Would it be okay if I added it to yankpad.el? Ofcourse I'll give you credit in the code and on the README page.

sid-kurias commented 7 years ago

oh yes, please go ahead and use it as you please. Don't worry about credits. I really like yankpad and orgmode and emacs :-)

Kungsgeten commented 7 years ago

Your code has now been added, thanks again!