[Top][All Lists]

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

Re: [QUESTION] I have problem on my org-contacts capf function source co

From: Stefan Monnier
Subject: Re: [QUESTION] I have problem on my org-contacts capf function source code
Date: Sun, 14 Nov 2021 18:10:29 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

> I try to write a capf function for org-contacts to auto complete
> contact names after "@". Here is my code, but it still does not
> work.

Some description of what you mean by "doesn't work" would be helpful.

> #+begin_src emacs-lisp
> (defun org-contacts-org-complete-function ()
>   "Function used in `completion-at-point-functions' in `org-mode' to complete 
> @name."
>   (when-let* ((@-prefix-p (string-prefix-p "@" (thing-at-point 'symbol)))
>               (symbol (thing-at-point 'symbol))
>               (prefix (substring-no-properties symbol 1 nil))
>               (bounds (bounds-of-thing-at-point 'symbol))
>               (begin (car bounds))
>               (end (cdr bounds)))

You ask thingatpt to compute the same information 3 times.  Not only
it's inefficient, but if for some reason it doesn't return the same info
all three times your code will be broken.  So better start with
`bounds-of-thing-at-point` and then use `buffer-substring` to extract
`symbol` from it, and then use that in the `string-prefix-p` test.

>     (list begin
>           end
>           (all-completions
>            prefix
>            (mapcar
>             (lambda (contact) (plist-get contact :name))
>             (org-contacts--all-contacts))
>            'stringp)

Don't use `prefix` here.
Provide the a general completion table which can be used with other
prefixes as well: the CAPF function should only choose which kind of
completion to perform and which part of the buffer.

>           ;; (completion-table-dynamic
>           ;;  (lambda (input)
>           ;;    (mapcar
>           ;;     (lambda (contact) (plist-get contact :name))
>           ;;     (org-contacts--all-contacts))))

That would be better, yes.

>           :exclusive 'no

Is this *really* necessary?  This functionality is fundamentally very
hard to implement, so it comes with a lot of warts and restrictions.
Only use it if it's really really indispensable.

>           :annotation-function          ; tags
>           ;; TODO
>           (lambda (candidate)
>             "Tags: ")
>           :company-docsig #'identity         ; metadata
>           :company-doc-buffer                ; doc popup
>           (lambda (candidate)
>             (let ((name (plist-get candidate :name))
>                   (file (plist-get candidate :file))
>                   (position (plist-get candidate :position)))
>               (company-doc-buffer
>                ;; get org-contact headline and property drawer.
>                (with-current-buffer (find-file-noselect file)
>                  (goto-char position)
>                  (when (derived-mode-p 'org-mode)
>                    ;; `org-edit-src-code' is not a real narrowing command.
>                    ;; Remove this first conditional if you don't want it.
>                    (cond ((ignore-errors (org-edit-src-code))
>                           (delete-other-windows))
>                          ((org-at-block-p)
>                           (org-narrow-to-block))
>                          (t (org-narrow-to-subtree)))
>                    (buffer-substring (point-min) (point-max)))))))
>           :company-location (lambda (candidate)
>                               (let ((name (plist-get candidate :name))
>                                     (file (plist-get candidate :file))
>                                     (position (plist-get candidate 
> :position)))
>                                 (with-current-buffer (find-file-noselect file)
>                                   (goto-char position)
>                                   (cons (current-buffer) position)))))))

I recommend you move those functions outside of the CAFP function
instead, give them a name and refer to them by name here.  Will make the
code easier to read, will help indentation-depth, and can also
help debugging.


reply via email to

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