Re: [O] company-mode completions for noweb references

2018-11-06 Thread Eric S Fraga
On Tuesday,  6 Nov 2018 at 19:32, Nik Clayton wrote:
> Hoi,
>
> I've been writing a few things using Org where I'm making frequent use of
> noweb references in SRC blocks, and decided that completion would be handy
> for them.

Thanks for this.  Very useful.  I'll incorporate it into my config and
will come back if I see anything strange.
-- 
Eric S Fraga via Emacs 27.0.50, Org release_9.1.13-783-g97fac4



[O] company-mode completions for noweb references

2018-11-06 Thread Nik Clayton
Hoi,

I've been writing a few things using Org where I'm making frequent use of
noweb references in SRC blocks, and decided that completion would be handy
for them.

This function will complete "^<<" with a list of the defined named blocks
in the current file, if you're in a SRC block.

It could be made smarter (e.g., only offer the completion if ":noweb yes"
is set, only offer to complete from named blocks with the same language as
the current block), but I thought I'd throw it out here in case anyone else
finds it useful.

Best, N

(defun my/org-src-block-name-backend (command  arg  ignored)
  "Complete `<<' with the names of defined SRC blocks."
  (interactive (list 'interactive))
  (cl-case command
(interactive (company-begin-backend 'my/org-src-block-name-backend))
(init (require 'org-element))
(prefix (and (eq major-mode 'org-mode)
 (eq 'src-block (car (org-element-at-point)))
 (cons (company-grab-line "^<<\\(\\w*\\)" 1) t)))
(candidates
 (org-element-map (org-element-parse-buffer) 'src-block
   (lambda (src-block)
 (let ((name (org-element-property :name src-block)))
   (when name
 (propertize
  name
  :value (org-element-property :value src-block)
  :annotation (org-element-property :raw-value (org-element-lineage
src-block '(headline)
(sorted t); Show candidates in same order as doc
(ignore-case t)
(duplicates nil)   ; No need to remove duplicates
(post-completion   ; Close the reference with ">>"
 (insert ">>"))
;; Show the contents of the block in a doc-buffer. If you have
;; company-quickhelp-mode enabled it will show in a popup
(doc-buffer (company-doc-buffer (get-text-property 0 :value arg)))
(annotation (format " [%s]" (get-text-property 0 :annotation arg)

(add-to-list 'company-backends 'my/org-src-block-name-backend)