From fe9ec0c54c5c0224bdce6b557fe1f4bf2acbc972 Mon Sep 17 00:00:00 2001 From: Vitalie Spinu Date: Mon, 31 Aug 2015 01:38:14 +0200 Subject: [PATCH] Squashed 'packages/company-math/' content from commit b585117 git-subtree-dir: packages/company-math git-subtree-split: b585117b814d9c7474a50de5753526ebab7fa27f --- company-math.el | 159 ++++++++++++++++++++++++++++++++++++++++ img/latex-symbols.png | Bin 0 -> 3355 bytes img/unicode-symbols.png | Bin 0 -> 3333 bytes readme.md | 62 ++++++++++++++++ 4 files changed, 221 insertions(+) create mode 100644 company-math.el create mode 100644 img/latex-symbols.png create mode 100644 img/unicode-symbols.png create mode 100644 readme.md diff --git a/company-math.el b/company-math.el new file mode 100644 index 000000000..7d8efabf1 --- /dev/null +++ b/company-math.el @@ -0,0 +1,159 @@ +;;; company-math.el --- Completion backends for unicode math symbols and latex tags +;; +;; Copyright (C) 2015 Free Software Foundation, Inc. +;; Author: Vitalie Spinu +;; URL: https://github.com/vspinu/company-math +;; Keywords: Unicode, symbols, completion +;; Version: 1.0.1 +;; Package-Requires: ((company "0.8.0") (math-symbol-lists "1.0")) +;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;; This file is part of GNU Emacs. +;; +;; This program is free software; you can redistribute it and/or +;; modify it under the terms of the GNU General Public License as +;; published by the Free Software Foundation; either version 3, or +;; (at your option) any later version. +;; +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;; General Public License for more details. +;; +;; You should have received a copy of the GNU General Public License +;; along with this program; see the file COPYING. If not, write to +;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth +;; Floor, Boston, MA 02110-1301, USA. +;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;;; Code: + +(require 'math-symbol-lists) +(require 'company) +(require 'cl-lib) + +(defgroup company-math nil + "Completion back-ends for math symbols Unicode symbols and LaTeX tags." + :group 'company + :prefix "company-math-") + +(defcustom company-math-prefix-regexp "\\\\\\([^ \t]+\\)" + "Regexp matching the prefix of the company-math symbol. +First subgroup must match the actual symbol to be used in the +completion." + :group 'company-math + :type 'string) + +(defcustom company-math-allow-unicode-symbols-in-faces t + "List of faces to allow the insertion of Unicode symbols. +When set to special value t, allow on all faces except those in +`company-math-disallow-unicode-symbols-in-faces'." + :group 'company-math + :type '(choice (const t) + (repeat :tag "Faces" symbol))) + +(defcustom company-math-allow-latex-symbols-in-faces '(font-latex-math-face) + "List of faces to disallow the insertion of latex mathematical symbols. +When set to special value t, allow on all faces except those in +`company-math-disallow-latex-symbols-in-faces'." + :group 'company-math + :type '(choice (const t) + (repeat :tag "Faces" symbol))) + +(defcustom company-math-disallow-unicode-symbols-in-faces '(font-latex-math-face) + "List of faces to disallow the insertion of Unicode symbols." + :group 'company-math + :type '(repeat symbol)) + +(defcustom company-math-disallow-latex-symbols-in-faces '() + "List of faces to disallow the insertion of latex mathematical symbols." + :group 'company-math + :type '(repeat symbol)) + + +;;; INTERNALS + +(defun company-math--make-candidates (alist) + "Build a list of math symbols ready to be used in ac source. +ALIST is one of the defined alist in package `symbols'. Return a +list of LaTeX symbols with text property :symbol being the +corresponding unicode symbol." + (delq nil + (mapcar + #'(lambda (el) + (let* ((tex (substring (nth 1 el) 1)) + (ch (and (nth 2 el) (decode-char 'ucs (nth 2 el)))) + (symb (and ch (char-to-string ch)))) + (propertize tex :symbol symb))) + alist))) + +(defconst company-math--symbols + (delete-dups + (append (company-math--make-candidates math-symbol-list-basic) + (company-math--make-candidates math-symbol-list-extended))) + "List of math completion candidates.") + +(defun company-math--prefix (allow-faces disallow-faces) + (let* ((face (get-text-property (point) 'face)) + (face (or (car-safe face) face)) + (insertp (and (not (memq face disallow-faces)) + (or (eq t allow-faces) + (memq face allow-faces))))) + (when insertp + (save-excursion + (when (looking-back company-math-prefix-regexp (point-at-bol)) + (match-string 1)))))) + +(defun company-math--substitute-unicode (symbol) + "Substitute preceding latex command with with SYMBOL." + (let ((pos (point)) + (inhibit-point-motion-hooks t)) + (when (re-search-backward company-math-prefix-regexp) + (delete-region (match-beginning 0) pos) + (insert symbol)))) + + +;;; BACKENDS + +;;;###autoload +(defun company-latex-commands (command &optional arg &rest ignored) + "Company backend for latex commands." + (interactive (list 'interactive)) + (cl-case command + (interactive (company-begin-backend 'company-latex-commands)) + (prefix (unless (company-in-string-or-comment) + (company-math--prefix t '()))) + (candidates (all-completions arg math-symbol-list-latex-commands)) + (sorted t))) + +;;;###autoload +(defun company-math-symbols-latex (command &optional arg &rest ignored) + "Company backend for LaTeX mathematical symbols." + (interactive (list 'interactive)) + (cl-case command + (interactive (company-begin-backend 'company-math-symbols-latex)) + (prefix (unless (company-in-string-or-comment) + (company-math--prefix company-math-allow-latex-symbols-in-faces + company-math-disallow-latex-symbols-in-faces))) + (annotation (concat " " (get-text-property 0 :symbol arg))) + (candidates (all-completions arg company-math--symbols)))) + +;;;###autoload +(defun company-math-symbols-unicode (command &optional arg &rest ignored) + "Company backend for LaTeX mathematical symbols." + (interactive (list 'interactive)) + (cl-case command + (interactive (company-begin-backend 'company-math-symbols-unicode)) + (prefix (company-math--prefix company-math-allow-unicode-symbols-in-faces + company-math-disallow-unicode-symbols-in-faces)) + (annotation (concat " " (get-text-property 0 :symbol arg))) + (candidates (all-completions arg company-math--symbols)) + (post-completion (company-math--substitute-unicode + (get-text-property 0 :symbol arg))))) + + +(provide 'company-math) + +;;; company-math.el ends here diff --git a/img/latex-symbols.png b/img/latex-symbols.png new file mode 100644 index 0000000000000000000000000000000000000000..781a46af0571050c0aeaf9d80e06a840fd3e6a5a GIT binary patch literal 3355 zcmcInc~BEs9)2NmC?lw-jG&+biYJH%2!W_v0aj4CFJVMQt^^1Wjv#SF8DUWIK)D?e z91RJgKqP?#K}9)4VIVL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+ zshf@JVl(Bz1&{xTNN4{WzW)N}QmaPW+PKbI+SO7s1R6cV`x4;Z{+UQgnN|RZdq?ME zMk0Org4M?(AmzL{$yt@Lp+n6KAEPKwf2e;5r@ea!PIxo?9#1k0C-MWb>4}@--0q8Q zmM2YIiLiY{k=?}(S=DZN+Qp2&IQI$wXZt_ETjOfp^1BJv2f4!}3QgI{fz=x6O_4K~ z+*Q;4svWsZ^b3(93tMG-PZpoD8lz!SN7f_EC!tZ*_Eqj^o|q+YKeq<9(0UzcldPvHLfB#O literal 0 HcmV?d00001 diff --git a/img/unicode-symbols.png b/img/unicode-symbols.png new file mode 100644 index 0000000000000000000000000000000000000000..050e6fe249f8906189458dd509776263f7f45716 GIT binary patch literal 3333 zcmcInSy)ro8r>mJf?%K#5F>*FAP^vJ#EOWB3L%UR z1;ij>XqW;a%nAf72nmD`28l(&7zQDR+@rm>_q#8B>%;lau=m;DK5MOit?wkbI2}_~ z(o_NfC_5ZK>~TUtCz)%99NZKY6}h9%Bhc-J(BmH008nZEe85r- zRI~ut_}t;Jo!gD9X`V?J_o$}4&n4fj47>Yo@HUsLfQ|ZzBFkB@wL76Vj$D!5t#SNC z*48kUO|W|%vegGi+@$I^6Xx%0-`n*-)z${#<+9CEyIua%Smu_6P^)a+xQ`f7VI@Ku zm?5}fZ4Q8tUmi?hMFP+)y1aHjA9`GG`)N+nWM6ao+KEbD_Jh~~ex^6Bq;O4Mfjb$l zW))i8aG*|aN%}n?;0oLE-uYWLp2fBmloln*O_GRqPb7?b3&{;M5nCpo2_N&cBse`F z0|HG#S*S98w8xpekl_7$Y{GicwAU%&#f=Xu5gaI%0>ny4tG=Nz zF)CsWR7WB%?3UIpu`^G{lJedp_c zjUHdSrrvGDGG6G|xTD(tQc0CKARLV-_r@OU?>+?xH}c!U15ODjl?`!C#Q8j(&LEMU z?_5Okleq;sbqQ?#YJ8E@Ud=zW3$5FrH+24)7AX1Q3~s;tPEnq8@}BZ*Wqm4>=Zc2i zN!ftDBalA|^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(X;A68ORQm04jN2arayetC?`Y3D_ghj>tQ0`C9G%lj7RHl&ViE^$Ur4f zwfUFc$-Nb`H>`sPQZ3h4C4)9q_sm6(3XE>X;8P30WWiAtTB!@VUyz|TLsaB^&Rl*M rJ2agt4_xg2(%3&|?0>&9Ln@<&YcZbuc(n`qkpT`zoDLTs^7;AShNU8; literal 0 HcmV?d00001 diff --git a/readme.md b/readme.md new file mode 100644 index 000000000..cc471942e --- /dev/null +++ b/readme.md @@ -0,0 +1,62 @@ +This add-on defines three *[company-mode](http://company-mode.github.io/)* backends: + +* `company-math-symbols-latex` - math latex tags (_by default, active only on latex math faces_) + + ![symbols](https://raw.github.com/vspinu/company-math/master/img/latex-symbols.png) + +* `company-math-symbols-unicode` - unicode symbols (_by default, active everywhere except math faces_) + + ![math](https://raw.github.com/vspinu/company-math/master/img/unicode-symbols.png) + +* `company-latex-commands` - latex commands + +## Usage ## + +Start math completion by typing the prefix `\` key. To select the +completion type RET. Depending on the context and your configuration +unicode symbol or latex tag will be inserted. + +## Activation ## + +Install from [MELPA](http://melpa.milkbox.net/) repository. + +You can either register each backend globally: + + +```lisp + +;; global activation of the unicode symbol completion +(add-to-list 'company-backends 'company-math-symbols-unicode) + + +``` + +or locally per emacs mode: + + +```lisp + +;; local configuration for TeX modes +(defun my-latex-mode-setup () + (setq-local company-backends + (append '(company-math-symbols-latex company-latex-commands) + company-backends))) + +(add-hook 'TeX-mode-hook 'my-latex-mode-setup) + +``` + +## Customization ## + +Set `company-tooltip-align-annotations` to t in order to allin symbols to the +right as in the above previews. + +By default unicode symbols backend (`company-math-symbols-unicode`) is not +active in latex math environments and latex math symbols +(`company-math-symbols-latex`) is not available outside of math latex +environmnts. You can use the following variables to adjust this behavior to your +liking: `company-math-disallow-unicode-symbols-in-faces`, +`company-math-allow-unicode-symbols-in-faces`, +`company-math-disallow-latex-symbols-in-faces`, +`company-math-allow-latex-symbols-in-faces`. + -- 2.39.2