mirror of https://github.com/Chizi123/.emacs.d.git

Chizi123
2018-11-21 7074318d7ab58aca124f590c42fd820e8eb258a5
commit | author | age
5cb5f7 1 ;;; company-keywords.el --- A company backend for programming language keywords
C 2
3 ;; Copyright (C) 2009-2011, 2016  Free Software Foundation, Inc.
4
5 ;; Author: Nikolaj Schumacher
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
21
22
23 ;;; Commentary:
24 ;;
25
26 ;;; Code:
27
28 (require 'company)
29 (require 'cl-lib)
30
31 (defun company-keywords-upper-lower (&rest lst)
32   ;; Upcase order is different for _.
33   (nconc (sort (mapcar 'upcase lst) 'string<) lst))
34
35 (defvar company-keywords-alist
36   ;; Please contribute corrections or additions.
37   `((c++-mode
38      "alignas" "alignof" "asm" "auto" "bool" "break" "case" "catch" "char"
39      "char16_t" "char32_t" "class" "const" "const_cast" "constexpr" "continue"
40      "decltype" "default" "delete" "do" "double" "dynamic_cast" "else" "enum"
41      "explicit" "export" "extern" "false" "final" "float" "for" "friend"
42      "goto" "if" "inline" "int" "long" "mutable" "namespace" "new" "noexcept"
43      "nullptr" "operator" "override"
44      "private" "protected" "public" "register" "reinterpret_cast"
45      "return" "short" "signed" "sizeof" "static" "static_assert"
46      "static_cast" "struct" "switch" "template" "this" "thread_local"
47      "throw" "true" "try" "typedef" "typeid" "typename"
48      "union" "unsigned" "using" "virtual" "void" "volatile" "wchar_t" "while")
49     (c-mode
50      "auto" "break" "case" "char" "const" "continue" "default" "do"
51      "double" "else" "enum" "extern" "float" "for" "goto" "if" "int" "long"
52      "register" "return" "short" "signed" "sizeof" "static" "struct"
53      "switch" "typedef" "union" "unsigned" "void" "volatile" "while")
54     (csharp-mode
55      "abstract" "add" "alias" "as" "base" "bool" "break" "byte" "case"
56      "catch" "char" "checked" "class" "const" "continue" "decimal" "default"
57      "delegate" "do" "double" "else" "enum" "event" "explicit" "extern"
58      "false" "finally" "fixed" "float" "for" "foreach" "get" "global" "goto"
59      "if" "implicit" "in" "int" "interface" "internal" "is" "lock" "long"
60      "namespace" "new" "null" "object" "operator" "out" "override" "params"
61      "partial" "private" "protected" "public" "readonly" "ref" "remove"
62      "return" "sbyte" "sealed" "set" "short" "sizeof" "stackalloc" "static"
63      "string" "struct" "switch" "this" "throw" "true" "try" "typeof" "uint"
64      "ulong" "unchecked" "unsafe" "ushort" "using" "value" "var" "virtual"
65      "void" "volatile" "where" "while" "yield")
66     (d-mode
67      ;; from http://www.digitalmars.com/d/2.0/lex.html
68      "abstract" "alias" "align" "asm"
69      "assert" "auto" "body" "bool" "break" "byte" "case" "cast" "catch"
70      "cdouble" "cent" "cfloat" "char" "class" "const" "continue" "creal"
71      "dchar" "debug" "default" "delegate" "delete" "deprecated" "do"
72      "double" "else" "enum" "export" "extern" "false" "final" "finally"
73      "float" "for" "foreach" "foreach_reverse" "function" "goto" "idouble"
74      "if" "ifloat" "import" "in" "inout" "int" "interface" "invariant"
75      "ireal" "is" "lazy" "long" "macro" "mixin" "module" "new" "nothrow"
76      "null" "out" "override" "package" "pragma" "private" "protected"
77      "public" "pure" "real" "ref" "return" "scope" "short" "static" "struct"
78      "super" "switch" "synchronized" "template" "this" "throw" "true" "try"
79      "typedef" "typeid" "typeof" "ubyte" "ucent" "uint" "ulong" "union"
80      "unittest" "ushort" "version" "void" "volatile" "wchar" "while" "with")
81     (f90-mode .
82      ;; from f90.el
83      ;; ".AND." ".GE." ".GT." ".LT." ".LE." ".NE." ".OR." ".TRUE." ".FALSE."
84      ,(company-keywords-upper-lower
85       "abs" "abstract" "achar" "acos" "adjustl" "adjustr" "aimag" "aint"
86       "align" "all" "all_prefix" "all_scatter" "all_suffix" "allocatable"
87       "allocate" "allocated" "and" "anint" "any" "any_prefix" "any_scatter"
88       "any_suffix" "asin" "assign" "assignment" "associate" "associated"
89       "asynchronous" "atan" "atan2" "backspace" "bind" "bit_size" "block"
90       "btest" "c_alert" "c_associated" "c_backspace" "c_bool"
91       "c_carriage_return" "c_char" "c_double" "c_double_complex" "c_f_pointer"
92       "c_f_procpointer" "c_float" "c_float_complex" "c_form_feed" "c_funloc"
93       "c_funptr" "c_horizontal_tab" "c_int" "c_int16_t" "c_int32_t" "c_int64_t"
94       "c_int8_t" "c_int_fast16_t" "c_int_fast32_t" "c_int_fast64_t"
95       "c_int_fast8_t" "c_int_least16_t" "c_int_least32_t" "c_int_least64_t"
96       "c_int_least8_t" "c_intmax_t" "c_intptr_t" "c_loc" "c_long"
97       "c_long_double" "c_long_double_complex" "c_long_long" "c_new_line"
98       "c_null_char" "c_null_funptr" "c_null_ptr" "c_ptr" "c_short"
99       "c_signed_char" "c_size_t" "c_vertical_tab" "call" "case" "ceiling"
100       "char" "character" "character_storage_size" "class" "close" "cmplx"
101       "command_argument_count" "common" "complex" "conjg" "contains" "continue"
102       "copy_prefix" "copy_scatter" "copy_suffix" "cos" "cosh" "count"
103       "count_prefix" "count_scatter" "count_suffix" "cpu_time" "cshift"
104       "cycle" "cyclic" "data" "date_and_time" "dble" "deallocate" "deferred"
105       "digits" "dim" "dimension" "distribute" "do" "dot_product" "double"
106       "dprod" "dynamic" "elemental" "else" "elseif" "elsewhere" "end" "enddo"
107       "endfile" "endif" "entry" "enum" "enumerator" "eoshift" "epsilon" "eq"
108       "equivalence" "eqv" "error_unit" "exit" "exp" "exponent" "extends"
109       "extends_type_of" "external" "extrinsic" "false" "file_storage_size"
110       "final" "floor" "flush" "forall" "format" "fraction" "function" "ge"
111       "generic" "get_command" "get_command_argument" "get_environment_variable"
112       "goto" "grade_down" "grade_up" "gt" "hpf_alignment" "hpf_distribution"
113       "hpf_template" "huge" "iachar" "iall" "iall_prefix" "iall_scatter"
114       "iall_suffix" "iand" "iany" "iany_prefix" "iany_scatter" "iany_suffix"
115       "ibclr" "ibits" "ibset" "ichar" "ieee_arithmetic" "ieee_exceptions"
116       "ieee_features" "ieee_get_underflow_mode" "ieee_set_underflow_mode"
117       "ieee_support_underflow_control" "ieor" "if" "ilen" "implicit"
118       "import" "include" "independent" "index" "inherit" "input_unit"
119       "inquire" "int" "integer" "intent" "interface" "intrinsic" "ior"
120       "iostat_end" "iostat_eor" "iparity" "iparity_prefix" "iparity_scatter"
121       "iparity_suffix" "ishft" "ishftc" "iso_c_binding" "iso_fortran_env"
122       "kind" "lbound" "le" "leadz" "len" "len_trim" "lge" "lgt" "lle" "llt"
123       "log" "log10" "logical" "lt" "matmul" "max" "maxexponent" "maxloc"
124       "maxval" "maxval_prefix" "maxval_scatter" "maxval_suffix" "merge"
125       "min" "minexponent" "minloc" "minval" "minval_prefix" "minval_scatter"
126       "minval_suffix" "mod" "module" "modulo" "move_alloc" "mvbits" "namelist"
127       "ne" "nearest" "neqv" "new" "new_line" "nint" "non_intrinsic"
128       "non_overridable" "none" "nopass" "not" "null" "nullify"
129       "number_of_processors" "numeric_storage_size" "only" "onto" "open"
130       "operator" "optional" "or" "output_unit" "pack" "parameter" "parity"
131       "parity_prefix" "parity_scatter" "parity_suffix" "pass" "pause"
132       "pointer" "popcnt" "poppar" "precision" "present" "print" "private"
133       "procedure" "processors" "processors_shape" "product" "product_prefix"
134       "product_scatter" "product_suffix" "program" "protected" "public"
135       "pure" "radix" "random_number" "random_seed" "range" "read" "real"
136       "realign" "recursive" "redistribute" "repeat" "reshape" "result"
137       "return" "rewind" "rrspacing" "same_type_as" "save" "scale" "scan"
138       "select" "selected_char_kind" "selected_int_kind" "selected_real_kind"
139       "sequence" "set_exponent" "shape" "sign" "sin" "sinh" "size" "spacing"
140       "spread" "sqrt" "stop" "subroutine" "sum" "sum_prefix" "sum_scatter"
141       "sum_suffix" "system_clock" "tan" "tanh" "target" "template" "then"
142       "tiny" "transfer" "transpose" "trim" "true" "type" "ubound" "unpack"
143       "use" "value" "verify" "volatile" "wait" "where" "while" "with" "write"))
144     (go-mode
145      ;; 1. Keywords ref: https://golang.org/ref/spec#Keywords
146      ;; 2. Builtin functions and types ref: https://golang.org/pkg/builtin/
147      "append" "bool" "break" "byte" "cap" "case" "chan" "close" "complex" "complex128"
148      "complex64" "const" "continue" "copy" "default" "defer" "delete" "else" "error"
149      "fallthrough" "false" "float32" "float64" "for" "func" "go" "goto" "if" "imag"
150      "import" "int" "int16" "int32" "int64" "int8" "interface" "len" "make"
151      "map" "new" "nil" "package" "panic" "print" "println" "range" "real" "recover"
152      "return" "rune" "select" "string" "struct" "switch" "true" "type" "uint" "uint16"
153      "uint32" "uint64" "uint8" "uintptr" "var")
154     (java-mode
155      "abstract" "assert" "boolean" "break" "byte" "case" "catch" "char" "class"
156      "continue" "default" "do" "double" "else" "enum" "extends" "final"
157      "finally" "float" "for" "if" "implements" "import" "instanceof" "int"
158      "interface" "long" "native" "new" "package" "private" "protected" "public"
159      "return" "short" "static" "strictfp" "super" "switch" "synchronized"
160      "this" "throw" "throws" "transient" "try" "void" "volatile" "while")
161     (javascript-mode
162      ;; https://tc39.github.io/ecma262/ + async, static and undefined
163      "async" "await" "break" "case" "catch" "class" "const" "continue"
164      "debugger" "default" "delete" "do" "else" "enum" "export" "extends" "false"
165      "finally" "for" "function" "if" "import" "in" "instanceof" "let" "new"
166      "null" "return" "static" "super" "switch" "this" "throw" "true" "try"
167      "typeof" "undefined" "var" "void" "while" "with" "yield")
168     (kotlin-mode
169      "abstract" "annotation" "as" "break" "by" "catch" "class" "companion"
170      "const" "constructor" "continue" "data" "do" "else" "enum" "false" "final"
171      "finally" "for" "fun" "if" "import" "in" "init" "inner" "interface"
172      "internal" "is" "lateinit" "nested" "null" "object" "open" "out" "override"
173      "package" "private" "protected" "public" "return" "super" "this" "throw"
174      "trait" "true" "try" "typealias" "val" "var" "when" "while")
175     (objc-mode
176      "@catch" "@class" "@encode" "@end" "@finally" "@implementation"
177      "@interface" "@private" "@protected" "@protocol" "@public"
178      "@selector" "@synchronized" "@throw" "@try" "alloc" "autorelease"
179      "bycopy" "byref" "in" "inout" "oneway" "out" "release" "retain")
180     (perl-mode
181      ;; from cperl.el
182      "AUTOLOAD" "BEGIN" "CHECK" "CORE" "DESTROY" "END" "INIT" "__END__"
183      "__FILE__" "__LINE__" "abs" "accept" "alarm" "and" "atan2" "bind"
184      "binmode" "bless" "caller" "chdir" "chmod" "chomp" "chop" "chown" "chr"
185      "chroot" "close" "closedir" "cmp" "connect" "continue" "cos"
186      "crypt" "dbmclose" "dbmopen" "defined" "delete" "die" "do" "dump" "each"
187      "else" "elsif" "endgrent" "endhostent" "endnetent" "endprotoent"
188      "endpwent" "endservent" "eof" "eq" "eval" "exec" "exists" "exit" "exp"
189      "fcntl" "fileno" "flock" "for" "foreach" "fork" "format" "formline"
190      "ge" "getc" "getgrent" "getgrgid" "getgrnam" "gethostbyaddr"
191      "gethostbyname" "gethostent" "getlogin" "getnetbyaddr" "getnetbyname"
192      "getnetent" "getpeername" "getpgrp" "getppid" "getpriority"
193      "getprotobyname" "getprotobynumber" "getprotoent" "getpwent" "getpwnam"
194      "getpwuid" "getservbyname" "getservbyport" "getservent" "getsockname"
195      "getsockopt" "glob" "gmtime" "goto" "grep" "gt" "hex" "if" "index" "int"
196      "ioctl" "join" "keys" "kill" "last" "lc" "lcfirst" "le" "length"
197      "link" "listen" "local" "localtime" "lock" "log" "lstat" "lt" "map"
198      "mkdir" "msgctl" "msgget" "msgrcv" "msgsnd" "my" "ne" "next" "no"
199      "not" "oct" "open" "opendir" "or" "ord" "our" "pack" "package" "pipe"
200      "pop" "pos" "print" "printf" "push" "q" "qq" "quotemeta" "qw" "qx"
201      "rand" "read" "readdir" "readline" "readlink" "readpipe" "recv" "redo"
202      "ref" "rename" "require" "reset" "return" "reverse" "rewinddir" "rindex"
203      "rmdir" "scalar" "seek" "seekdir" "select" "semctl" "semget" "semop"
204      "send" "setgrent" "sethostent" "setnetent" "setpgrp" "setpriority"
205      "setprotoent" "setpwent" "setservent" "setsockopt" "shift" "shmctl"
206      "shmget" "shmread" "shmwrite" "shutdown" "sin" "sleep" "socket"
207      "socketpair" "sort" "splice" "split" "sprintf" "sqrt" "srand" "stat"
208      "study" "sub" "substr" "symlink" "syscall" "sysopen" "sysread" "system"
209      "syswrite" "tell" "telldir" "tie" "time" "times" "tr" "truncate" "uc"
210      "ucfirst" "umask" "undef" "unless" "unlink" "unpack" "unshift" "untie"
211      "until" "use" "utime" "values" "vec" "wait" "waitpid"
212      "wantarray" "warn" "while" "write" "x" "xor" "y")
213     (php-mode
214      "__CLASS__" "__DIR__" "__FILE__" "__FUNCTION__" "__LINE__" "__METHOD__"
215      "__NAMESPACE__" "_once" "abstract" "and" "array" "as" "break" "case"
216      "catch" "cfunction" "class" "clone" "const" "continue" "declare"
217      "default" "die" "do" "echo" "else" "elseif" "empty" "enddeclare"
218      "endfor" "endforeach" "endif" "endswitch" "endwhile" "eval" "exception"
219      "exit" "extends" "final" "for" "foreach" "function" "global"
220      "goto" "if" "implements" "include" "instanceof" "interface"
221      "isset" "list" "namespace" "new" "old_function" "or" "php_user_filter"
222      "print" "private" "protected" "public" "require" "require_once" "return"
223      "static" "switch" "this" "throw" "try" "unset" "use" "var" "while" "xor")
224     (python-mode
225      ;; https://docs.python.org/3/reference/lexical_analysis.html#keywords
226      "False" "None" "True" "and" "as" "assert" "break" "class" "continue" "def"
227      "del" "elif" "else" "except" "exec" "finally" "for" "from" "global" "if"
228      "import" "in" "is" "lambda" "nonlocal" "not" "or" "pass" "print" "raise"
229      "return" "try" "while" "with" "yield")
230     (ruby-mode
231      "BEGIN" "END" "alias" "and"  "begin" "break" "case" "class" "def" "defined?"
232      "do" "else" "elsif"  "end" "ensure" "false" "for" "if" "in" "module"
233      "next" "nil" "not" "or" "redo" "rescue" "retry" "return" "self" "super"
234      "then" "true" "undef" "unless" "until" "when" "while" "yield")
235     ;; From https://doc.rust-lang.org/grammar.html#keywords
236     ;; but excluding unused reserved words: https://www.reddit.com/r/rust/comments/34fq0k/is_there_a_good_list_of_rusts_keywords/cqucvnj
237     (rust-mode
238      "Self"
239      "as" "box" "break" "const" "continue" "crate" "else" "enum" "extern"
240      "false" "fn" "for" "if" "impl" "in" "let" "loop" "macro" "match" "mod"
241      "move" "mut" "pub" "ref" "return" "self" "static" "struct" "super"
242      "trait" "true" "type" "unsafe" "use" "where" "while")
243     (scala-mode
244      "abstract" "case" "catch" "class" "def" "do" "else" "extends" "false"
245      "final" "finally" "for" "forSome" "if" "implicit" "import" "lazy" "match"
246      "new" "null" "object" "override" "package" "private" "protected"
247      "return" "sealed" "super" "this" "throw" "trait" "true" "try" "type" "val"
248      "var" "while" "with" "yield")
249     (swift-mode
250      "Protocol" "Self" "Type" "and" "as" "assignment" "associatedtype"
251      "associativity" "available" "break" "case" "catch" "class" "column" "continue"
252      "convenience" "default" "defer" "deinit" "didSet" "do" "dynamic" "dynamicType"
253      "else" "elseif" "endif" "enum" "extension" "fallthrough" "false" "file"
254      "fileprivate" "final" "for" "func" "function" "get" "guard" "higherThan" "if"
255      "import" "in" "indirect" "infix" "init" "inout" "internal" "is" "lazy" "left"
256      "let" "line" "lowerThan" "mutating" "nil" "none" "nonmutating" "open"
257      "operator" "optional" "override" "postfix" "precedence" "precedencegroup"
258      "prefix" "private" "protocol" "public" "repeat" "required" "rethrows" "return"
259      "right" "selector" "self" "set" "static" "struct" "subscript" "super" "switch"
260      "throw" "throws" "true" "try" "typealias" "unowned" "var" "weak" "where"
261      "while" "willSet")
262     (julia-mode
263      "abstract" "break" "case" "catch" "const" "continue" "do" "else" "elseif"
264      "end" "eval" "export" "false" "finally" "for" "function" "global" "if"
265      "ifelse" "immutable" "import" "importall" "in" "let" "macro" "module"
266      "otherwise" "quote" "return" "switch" "throw" "true" "try" "type"
267      "typealias" "using" "while"
268      )
269     ;; From https://github.com/apache/thrift/blob/master/contrib/thrift.el
270     (thrift-mode
271      "binary" "bool" "byte" "const" "double" "enum" "exception" "extends"
272      "i16" "i32" "i64" "include" "list" "map" "oneway" "optional" "required"
273      "service" "set" "string" "struct" "throws" "typedef" "void"
274      )
275     ;; aliases
276     (js2-mode . javascript-mode)
277     (js2-jsx-mode . javascript-mode)
278     (espresso-mode . javascript-mode)
279     (js-mode . javascript-mode)
280     (js-jsx-mode . javascript-mode)
281     (rjsx-mode . javascript-mode)
282     (cperl-mode . perl-mode)
283     (jde-mode . java-mode)
284     (ess-julia-mode . julia-mode)
285     (enh-ruby-mode . ruby-mode))
286   "Alist mapping major-modes to sorted keywords for `company-keywords'.")
287
288 ;;;###autoload
289 (defun company-keywords (command &optional arg &rest ignored)
290   "`company-mode' backend for programming language keywords."
291   (interactive (list 'interactive))
292   (cl-case command
293     (interactive (company-begin-backend 'company-keywords))
294     (prefix (and (assq major-mode company-keywords-alist)
295                  (not (company-in-string-or-comment))
296                  (or (company-grab-symbol) 'stop)))
297     (candidates
298      (let ((completion-ignore-case nil)
299            (symbols (cdr (assq major-mode company-keywords-alist))))
300        (all-completions arg (if (consp symbols)
301                                 symbols
302                               (cdr (assq symbols company-keywords-alist))))))
303     (sorted t)))
304
305 (provide 'company-keywords)
306 ;;; company-keywords.el ends here