unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: [QUESTION] I have problem on my org-contacts capf function source code
Date: Sun, 14 Nov 2021 18:10:29 -0500	[thread overview]
Message-ID: <jwvh7ce49z5.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: PAXPR08MB66400603B62A2B3587928123A3979@PAXPR08MB6640.eurprd08.prod.outlook.com

> 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.


        Stefan




  reply	other threads:[~2021-11-14 23:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-14  9:59 [QUESTION] I have problem on my org-contacts capf function source code Christopher M. Miles
2021-11-14 23:10 ` Stefan Monnier via Users list for the GNU Emacs text editor [this message]
2021-11-15  9:05   ` Christopher M. Miles
2021-11-15 13:50     ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-11-15 15:43       ` Christopher M. Miles
2021-11-15 21:30         ` Stefan Monnier
2021-11-18  6:56           ` [SOLVED] " Christopher M. Miles

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwvh7ce49z5.fsf-monnier+emacs@gnu.org \
    --to=help-gnu-emacs@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).