From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nik Clayton Subject: company-mode completions for noweb references Date: Tue, 6 Nov 2018 19:32:20 +0100 Message-ID: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="0000000000008ed674057a033919" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:51858) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gK69h-0001w7-3s for emacs-orgmode@gnu.org; Tue, 06 Nov 2018 13:32:50 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gK69e-0006Ca-7M for emacs-orgmode@gnu.org; Tue, 06 Nov 2018 13:32:47 -0500 Received: from mail-it1-x130.google.com ([2607:f8b0:4864:20::130]:33032) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gK69d-0005qk-TS for emacs-orgmode@gnu.org; Tue, 06 Nov 2018 13:32:46 -0500 Received: by mail-it1-x130.google.com with SMTP id p11-v6so11076781itf.0 for ; Tue, 06 Nov 2018 10:32:33 -0800 (PST) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode --0000000000008ed674057a033919 Content-Type: text/plain; charset="UTF-8" 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 &optional arg &rest 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) --0000000000008ed674057a033919 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
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.
<= div>
This function will complete "^<<" with a= list of the defined named blocks in the current file, if you're in a S= RC block.

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

Best, N

(defun my/org-src-block-name-backend (command &optional arg &= rest ignored)
=C2=A0 "Complete `<<' with the names of def= ined SRC blocks."
=C2=A0 (interactive (list 'interactive))
= =C2=A0 (cl-case command
=C2=A0=C2=A0=C2=A0 (interactive (company-begin-b= ackend 'my/org-src-block-name-backend))
=C2=A0=C2=A0=C2=A0 (init (re= quire 'org-element))
=C2=A0=C2=A0=C2=A0 (prefix (and (eq major-mode = 'org-mode)
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0(eq 'src-= block (car (org-element-at-point)))
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2= =A0 =C2=A0(cons (company-grab-line "^<<\\(\\w*\\)" 1) t)))<= br>=C2=A0=C2=A0=C2=A0 (candidates
=C2=A0=C2=A0=C2=A0=C2=A0 (org-element-= map (org-element-parse-buffer) 'src-block
=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 (lambda (src-block)
=C2=A0=C2=A0=C2=A0 =C2=A0(let ((name (o= rg-element-property :name src-block)))
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0 (= when name
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 (propertize
=C2= =A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 name
=C2=A0=C2=A0=C2=A0 = =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 :value (org-element-property :value src-bloc= k)
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 :annotation (org-el= ement-property :raw-value (org-element-lineage src-block '(headline))))= )))))
=C2=A0=C2=A0=C2=A0 (sorted t)=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 ; Show candidates in same order as doc
=C2=A0=C2=A0= =C2=A0 (ignore-case t)
=C2=A0=C2=A0=C2=A0 (duplicates nil)=C2=A0=C2=A0= =C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ; No need to= remove duplicates
=C2=A0=C2=A0=C2=A0 (post-completion=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ; Close the refere= nce with ">>"
=C2=A0=C2=A0=C2=A0=C2=A0 (insert ">= ;>"))
=C2=A0=C2=A0=C2=A0 ;; Show the contents of the block in a = doc-buffer. If you have
=C2=A0=C2=A0=C2=A0 ;; company-quickhelp-mode ena= bled it will show in a popup
=C2=A0=C2=A0=C2=A0 (doc-buffer (company-doc= -buffer (get-text-property 0 :value arg)))
=C2=A0=C2=A0=C2=A0 (annotatio= n (format " [%s]" (get-text-property 0 :annotation arg)))))
(add-to-list 'company-backends 'my/org-src-block-name-backend)
--0000000000008ed674057a033919--