unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: "Christopher M. Miles" <numbchild@gmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: help-gnu-emacs@gnu.org
Subject: [SOLVED] Re: [QUESTION] I have problem on my org-contacts capf function source code
Date: Thu, 18 Nov 2021 14:56:45 +0800	[thread overview]
Message-ID: <PAXPR08MB6640954BBDECF5C8A5F063F4A39B9@PAXPR08MB6640.eurprd08.prod.outlook.com> (raw)
In-Reply-To: <jwvwnl915ep.fsf-monnier+emacs@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 3782 bytes --]


I found the problem reason.

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

Here is my workable code:

#+begin_src emacs-lisp
(defun org-contacts-org-complete--annotation-function (candidate)
  "Return org-contacts tags of contact candidate."
  ;; TODO
  "Tags: ")

(defun org-contacts-org-complete--doc-function (candidate)
  "Return org-contacts content of contact 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)))))))

(defun org-contacts-org-complete--location-function (candidate)
  "Return org-contacts location of contact 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))))

(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
            
            ;; (all-completions
            ;;  prefix
            ;;  (mapcar
            ;;   (lambda (contact) (plist-get contact :name))
            ;;   (org-contacts--all-contacts))
            ;;  'stringp)

            (completion-table-dynamic
             (lambda (_)
               (mapcar
                (lambda (contact) (concat "@" (plist-get contact :name))) ; <----- prepend "@" solved the problem.
                (org-contacts--all-contacts))))

            ;; :predicate 'stringp
            ;; :exclusive 'no
            ;; properties check out `completion-extra-properties'
            ;; :annotation-function #'org-contacts-org-complete--annotation-function
            ;; :exit-function ; TODO change completion candidate inserted contact name into org-contact link??

            ;; :company-docsig #'identity                                    ; metadata
            ;; :company-doc-buffer #'org-contacts-org-complete--doc-function ; doc popup
            ;; :company-location #'org-contacts-org-complete--location-function
            ))))

;; (add-hook 'completion-at-point-functions 'org-contacts-org-complete-function nil 'local)
#+end_src

Also learned your other detailed suggestion. Learned lot from you. Thanks for your teaching. 😄

-- 
[ stardiviner ]
       I try to make every word tell the meaning that I want to express.

       Blog: https://stardiviner.github.io/
       IRC(freenode): stardiviner, Matrix: stardiviner
       GPG: F09F650D7D674819892591401B5DF1C95AE89AC3

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

      reply	other threads:[~2021-11-18  6:56 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
2021-11-18  6:56           ` Christopher M. Miles [this message]

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=PAXPR08MB6640954BBDECF5C8A5F063F4A39B9@PAXPR08MB6640.eurprd08.prod.outlook.com \
    --to=numbchild@gmail.com \
    --cc=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).