dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

company-graphviz-dot.el (4143B)


      1 ;;; company-graphviz-dot.el --- Company completion function for
      2 ;;; graphviz-dot-mode
      3 
      4 ;; Copyright (C) Bjarte Johansen <bjarte.johansen@gmail.com>
      5 ;; Copyright (C) 2019 - 2020 Pieter Pareit <pieter.pareit@gmail.com>
      6 
      7 ;; This program is free software; you can redistribute it and/or
      8 ;; modify it under the terms of the GNU General Public License as
      9 ;; published by the Free Software Foundation; either version 2 of
     10 ;; the License, or (at your option) any later version.
     11 
     12 ;; This program is distributed in the hope that it will be
     13 ;; useful, but WITHOUT ANY WARRANTY; without even the implied
     14 ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     15 ;; PURPOSE.  See the GNU General Public License for more details.
     16 
     17 ;; You should have received a copy of the GNU General Public
     18 ;; License along with this program; if not, write to the Free
     19 ;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
     20 ;; MA 02111-1307 USA
     21 
     22 ;; Authors: Bjarte Johansen <bjarte.johansen@gmail.com>
     23 ;;          Pieter Pareit <pieter.pareit@gmail.com>
     24 ;; Homepage: http://ppareit.github.com/graphviz-dot-mode/
     25 ;; Created:
     26 ;; Last modified:
     27 ;; Version: 0.0.1
     28 ;; Keywords: mode dot dot-language dotlanguage graphviz graphs att company
     29 
     30 ;;; Commentary:
     31 
     32 ;;; Code:
     33 
     34 (require 'company)
     35 (require 'graphviz-dot-mode)
     36 
     37 (eval-when-compile
     38   (require 'cl-lib))
     39 
     40 (defun company-gz-dot--candidates (arg)
     41   "Return good candidates for the argument ARG for company."
     42   (all-completions arg
     43 		   (cl-case (company-graphviz-dot--syntax-at-point)
     44 		     (compasspoint graphviz-values-type-portpos)
     45 		     (color graphviz-dot-color-keywords)
     46 		     (arrow graphviz-values-type-arrow)
     47 		     (shape graphviz-values-type-shape)
     48 		     (style graphviz-values-type-style)
     49 		     (dir graphviz-values-type-dir)
     50 		     (outputmode graphviz-values-type-outputmode)
     51 		     (packmode graphviz-values-type-packmode)
     52 		     (pagedir graphviz-values-type-pagedir)
     53 		     (portpos graphviz-values-type-portpos)
     54 		     (bool graphviz-values-type-bool)
     55 		     (value graphviz-dot-value-keywords)
     56 		     ((comment string) nil)
     57 		     (t graphviz-dot-attr-keywords))))
     58 
     59 (defun company-graphviz-dot--syntax-at-point ()
     60   "Return the syntax at point.
     61 This can be one of comment, string, out, value, attribute, color,
     62 arrow, shape, style, dir, outputmode or other."
     63   (let ((state (syntax-ppss)))
     64     (cond
     65      ((nth 4 state) 'comment)
     66      ((nth 3 state) 'string)
     67      ((not (nth 1 state)) 'out)
     68      (t (save-excursion
     69           (skip-chars-backward "^[\\[,;=:\n]")
     70           (backward-char)
     71           (cond
     72            ((looking-at "[\\[,;\n]") 'attribute)
     73 	   ((looking-at ":") 'compasspoint)
     74            ((looking-at "=")
     75 	    (progn
     76 	      (backward-word 1)
     77 	      (cond
     78 	       ((looking-at "[a-zA-Z]*color")  'color)
     79 	       ((member (word-at-point) graphviz-attributes-type-arrow) 'arrow)
     80 	       ((member (word-at-point) graphviz-attributes-type-shape) 'shape)
     81 	       ((member (word-at-point) graphviz-attributes-type-style) 'style)
     82 	       ((member (word-at-point) graphviz-attributes-type-dir) 'dir)
     83 	       ((member (word-at-point) graphviz-attributes-type-outputmode) 'outputmode)
     84 	       ((member (word-at-point) graphviz-attributes-type-packmode) 'packmode)
     85 	       ((member (word-at-point) graphviz-attributes-type-pagedir) 'pagedir)
     86 	       ((member (word-at-point) graphviz-attributes-type-portpos) 'portpos)
     87 	       ((member (word-at-point) graphviz-attributes-type-bool) 'bool)
     88 	       (t 'value))))
     89            (t 'other)))))))
     90 
     91 
     92 (defun company-graphviz-dot-backend (command &optional arg &rest ignored)
     93   "Company back-end for `graphviz-dot-mode'.
     94 In the signature, COMMAND, ARG and IGNORED are mandated by `company-mode'."
     95   (interactive (list 'interactive))
     96   (cl-case command
     97     (interactive (company-begin-backend 'company-graphviz-dot-backend))
     98     (prefix (and (eq major-mode 'graphviz-dot-mode)
     99                  (company-grab-symbol)))
    100     (candidates (company-gz-dot--candidates arg))
    101     (no-cache t)
    102     (require-match 'never)))
    103 
    104 (add-to-list 'company-backends 'company-graphviz-dot-backend)
    105 
    106 (provide 'company-graphviz-dot)
    107 ;;; company-graphviz-dot.el ends here