Fix [[* completion when there is text after point

* lisp/org-pcomplete.el (org-parse-arguments): Parse text between [[*
and point as a single "argument" for pcomplete.  This avoid weird
breakage when treating every space-separated text in the line as an
argument.
(pcomplete/org-mode/searchhead): Do not alter `pcomplete-stub' - it is
no longer necessary as `org-parse-arguments' now takes care about not
including * and ] into the stub.

Warning! pcomplete code is rather tricky and it is not always clear if
Org's approach is not overusing internals of pcomplete.

Reported-by: Ignacio Casso <ignaciocasso@hotmail.com>
Link: https://orgmode.org/list/87ils0lut8.fsf@localhost
This commit is contained in:
Ihor Radchenko 2024-01-07 16:43:04 +01:00
parent aab2c94531
commit 97951352bb
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
1 changed files with 23 additions and 22 deletions

View File

@ -176,21 +176,29 @@ When completing for #+STARTUP, for example, this function returns
(defun org-parse-arguments () (defun org-parse-arguments ()
"Parse whitespace separated arguments in the current region." "Parse whitespace separated arguments in the current region."
(let ((begin (line-beginning-position)) (if (equal (cons "searchhead" nil) (org-thing-at-point))
(end (line-end-position)) ;; [[* foo<point> bar link::search option.
begins args) ;; Arguments are not simply space-separated.
(save-restriction
(narrow-to-region begin end)
(save-excursion (save-excursion
(goto-char (point-min)) (let ((origin (point)))
(while (not (eobp)) (skip-chars-backward "^*" (line-beginning-position))
(skip-chars-forward " \t\n[") (cons (list (buffer-substring-no-properties (point) origin))
(setq begins (cons (point) begins)) (list (point)))))
(skip-chars-forward "^ \t\n[") (let ((begin (line-beginning-position))
(setq args (cons (buffer-substring-no-properties (end (line-end-position))
(car begins) (point)) begins args)
args))) (save-restriction
(cons (reverse args) (reverse begins)))))) (narrow-to-region begin end)
(save-excursion
(goto-char (point-min))
(while (not (eobp))
(skip-chars-forward " \t\n[")
(setq begins (cons (point) begins))
(skip-chars-forward "^ \t\n[")
(setq args (cons (buffer-substring-no-properties
(car begins) (point))
args)))
(cons (reverse args) (reverse begins)))))))
(defun org-pcomplete-initial () (defun org-pcomplete-initial ()
"Call the right completion function for first argument completions." "Call the right completion function for first argument completions."
@ -366,14 +374,7 @@ This needs more work, to handle headings with lots of spaces in them."
;; Remove the leading asterisk from ;; Remove the leading asterisk from
;; `org-link-heading-search-string' result. ;; `org-link-heading-search-string' result.
(push (substring (org-link-heading-search-string) 1) tbl)) (push (substring (org-link-heading-search-string) 1) tbl))
(pcomplete-uniquify-list tbl))) (pcomplete-uniquify-list tbl))))))
;; When completing a bracketed link, i.e., "[[*", argument
;; starts at the star, so remove this character.
;; Also, if the completion is done inside [[*head<point>]],
;; drop the closing parentheses.
(replace-regexp-in-string
"\\]+$" ""
(substring pcomplete-stub 1)))))
(defun pcomplete/org-mode/tag () (defun pcomplete/org-mode/tag ()
"Complete a tag name. Omit tags already set." "Complete a tag name. Omit tags already set."