unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: "Christopher M. Miles" <numbchild@gmail.com>
Cc: help-gnu-emacs@gnu.org
Subject: Re: [QUESTION] I have problem on my org-contacts capf function source code
Date: Mon, 15 Nov 2021 16:30:21 -0500	[thread overview]
Message-ID: <jwvwnl915ep.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <PAXPR08MB6640C2EE9E74A1990EE25971A3989@PAXPR08MB6640.eurprd08.prod.outlook.com> (Christopher M. Miles's message of "Mon, 15 Nov 2021 23:43:59 +0800")

> I followed your steps. Got following trace output:
>
> #+begin_example
> ======================================================================
> 1 -> (org-contacts-org-complete-function)
> 1 <- org-contacts-org-complete-function: nil
> ======================================================================
> 1 -> (org-contacts-org-complete-function)
> 1 <- org-contacts-org-complete-function: nil
> ======================================================================
> 1 -> (org-contacts-org-complete-function)
> 1 <- org-contacts-org-complete-function: nil
> #+end_example

So when that function was called (what called it, BTW?  Did you hit
`M-TAB` or are you using `company-mode`?) apparently
`org-contacts-prefix-p` ended up being nil.

And what do you get when you try

    M-: (org-contacts-org-complete-function) RET

?

> I found ~org-contacts-org-complete-function~ returned a special value:
>
> #+begin_example
> #f(compiled-function (string pred action) #<bytecode -0x9e1a398d61d3acb>)
> #+end_example

I can't see any way M-: (org-contacts-org-complete-function) RET
can return the above value.  So I suspect a "pilot error".
This looks like the 3rd value in the returned list (i.e. the value
returned by `completion-table-dynamic`).

> #+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* ((bounds (bounds-of-thing-at-point 'symbol))
>               (begin (1- (car bounds)))
>               (end (cdr bounds))
>               (symbol (buffer-substring-no-properties begin end))
>               (org-contacts-prefix-p (string-prefix-p "@" symbol))
>               ;; (prefix (substring-no-properties symbol 1 nil))
>               )
>     (when org-contacts-prefix-p
>       (list begin
>             end
>             (completion-table-dynamic
>              (lambda (_)
>                (mapcar
>                 (lambda (contact) (plist-get contact :name))
>                 (org-contacts--all-contacts))))))))
> #+end_src

This gives a `begin..end` region which presumably includes `@`.
Does (plist-get contact :name) return names that start with `@`?
If not, the completion will never match.

> And test with execute following ~add-hook~ in org-mode buffer or
>  emacs-lisp-mode buffer:

In emacs-lisp-mode, `@` has symbol syntax, so
(bounds-of-thing-at-point 'symbol)
will include `@` in the returned region, whereas in Org mode
`@` seems to have punctuation syntax so the `@` will not be included in
the returned region.

Maybe instead of `bounds-of-thing-at-point` you want to use something
less "magic", like (skip-chars-backward "[:alnum:]@").

> #+begin_src emacs-lisp
> (add-hook 'completion-at-point-functions 'org-contacts-org-complete-function nil 'local)
> #+end_src




  reply	other threads:[~2021-11-15 21:30 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
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 [this message]
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=jwvwnl915ep.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=help-gnu-emacs@gnu.org \
    --cc=numbchild@gmail.com \
    /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).