use less magic symbol extract code

> #+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:]@").
This commit is contained in:
stardiviner 2021-11-18 15:06:23 +08:00
parent 1b028de47c
commit f3fa9fc0b8

View file

@ -677,9 +677,8 @@ description."
(defun org-contacts-org-complete-function () (defun org-contacts-org-complete-function ()
"Function used in `completion-at-point-functions' in `org-mode' to complete @name." "Function used in `completion-at-point-functions' in `org-mode' to complete @name."
(when-let* ((bounds (bounds-of-thing-at-point 'symbol)) (when-let* ((end (point))
(begin (1- (car bounds))) (begin (save-excursion (skip-chars-backward "[:alnum:]@") (point)))
(end (cdr bounds))
(symbol (buffer-substring-no-properties begin end)) (symbol (buffer-substring-no-properties begin end))
(org-contacts-prefix-p (string-prefix-p "@" symbol)) (org-contacts-prefix-p (string-prefix-p "@" symbol))
;; (prefix (substring-no-properties symbol 1 nil)) ;; (prefix (substring-no-properties symbol 1 nil))