[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master 2413f9d 2/4: Merge company-math
From: |
Vitalie Spinu |
Subject: |
[elpa] master 2413f9d 2/4: Merge company-math |
Date: |
Tue, 7 May 2019 04:58:23 -0400 (EDT) |
branch: master
commit 2413f9df98eaf124234bca575d5e6bf191027a13
Merge: 975fe35 dd16eda
Author: Vitalie Spinu <address@hidden>
Commit: Vitalie Spinu <address@hidden>
Merge company-math
---
packages/company-math/company-math.el | 153 +++++++++++++++++++++++++---------
packages/company-math/readme.md | 35 ++++----
2 files changed, 132 insertions(+), 56 deletions(-)
diff --git a/packages/company-math/company-math.el
b/packages/company-math/company-math.el
index 49d3028..c55a717 100644
--- a/packages/company-math/company-math.el
+++ b/packages/company-math/company-math.el
@@ -4,8 +4,8 @@
;; Author: Vitalie Spinu <address@hidden>
;; URL: https://github.com/vspinu/company-math
;; Keywords: Unicode, symbols, completion
-;; Version: 1.1
-;; Package-Requires: ((company "0.8.0") (math-symbol-lists "1.0"))
+;; Version: 1.3
+;; Package-Requires: ((company "0.8.0") (math-symbol-lists "1.2"))
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
@@ -29,7 +29,7 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
-
+;;
;;; Code:
(require 'math-symbol-lists)
@@ -41,13 +41,47 @@
: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."
+(defcustom company-math-symbol-prefix "\\"
+ "Prefix to use for latex and unicode symbols."
:group 'company-math
:type 'string)
+(defcustom company-math-subscript-prefix "__"
+ "Prefix for unicode subscripts.
+When nil, no custom prefix is active. Irrespective of the value
+of this variable, prefix composed of `company-math-symbol-prefix'
+and \"_\" is always active (\"\\_\"). This variable takes effect
+in a new Emacs session."
+ :group 'company-math
+ :type '(choice (const :tag "No Custom Prefix" nil)
+ string))
+
+(defcustom company-math-superscript-prefix "^^"
+ "Prefix for unicode superscripts.
+When nil, no custom prefix is active. Irrespective of the value
+of this variable, prefix composed of `company-math-symbol-prefix'
+and \"^\" is always active (\"\\^\"). This variable takes effect
+in a new Emacs session."
+ :group 'company-math
+ :type '(choice (const :tag "No Custom Prefix" nil)
+ string))
+
+;; no more custom since since v.1.2
+(when (boundp 'company-math-prefix-regexp)
+ (warn "`company-math-prefix-regexp' is deprecated, please remove from your
custom settings."))
+
+(defvar company-math--latex-prefix-regexp
+ (concat (regexp-quote company-math-symbol-prefix)
+ "[^ \t\n]+"))
+
+(let ((psym (regexp-quote company-math-symbol-prefix))
+ (psub (when company-math-symbol-prefix
+ (concat "\\|" (regexp-quote company-math-subscript-prefix))))
+ (psup (when company-math-superscript-prefix
+ (concat "\\|" (regexp-quote company-math-superscript-prefix)))))
+ (setq company-math--unicode-prefix-regexp
+ (concat "\\(" psym psub psup "\\)[^ \t\n]*")))
+
(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
@@ -56,7 +90,7 @@ When set to special value t, allow on all faces except those
in
:type '(choice (const t)
(repeat :tag "Faces" symbol)))
-(defcustom company-math-allow-latex-symbols-in-faces '(tex-math
font-latex-math-face)
+(defcustom company-math-allow-latex-symbols-in-faces '(tex-math
font-latex-math-face org-latex-and-related)
"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'."
@@ -77,27 +111,46 @@ When set to special value t, allow on all faces except
those in
;;; 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
+(defun company-math--make-candidates (alist prefix)
+ "Build a list of math symbols ready to be used in a company backend.
+ALIST is one of the defined alist in package `math-symbol-lists'.
+PREFIX is a string to be prefixed to each symbol. 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)))
+ (lambda (el)
+ (let* ((tex (concat prefix (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--latex-commands
+ (mapcar (lambda (c) (concat company-math-symbol-prefix c))
math-symbol-list-latex-commands)
+ "List of LaTeX math completion candidates.")
+
(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)
+ (append (company-math--make-candidates math-symbol-list-basic
company-math-symbol-prefix)
+ (company-math--make-candidates math-symbol-list-extended
company-math-symbol-prefix)))
+ "List of LaTeX math completion candidates.")
+
+(defconst company-math--unicode
+ (append
+ (append (when company-math-subscript-prefix
+ (company-math--make-candidates math-symbol-list-subscripts
company-math-subscript-prefix))
+ (company-math--make-candidates math-symbol-list-subscripts (concat
company-math-symbol-prefix "_"))
+ (when company-math-superscript-prefix
+ (company-math--make-candidates math-symbol-list-superscripts
company-math-superscript-prefix))
+ (company-math--make-candidates math-symbol-list-superscripts
(concat company-math-symbol-prefix "^")))
+ company-math--symbols)
+ "List of math completion candidates for unicode backend.")
+
+(defun company-math--prefix (regexp allow-faces disallow-faces)
+ "Response to company prefix command.
+REGEXP is the regexp, ALLOW-FACES and DISALLOW-FACES are list of
+various faces to allow or disallow completion on."
(let* ((face (get-text-property (point) 'face))
(face (or (car-safe face) face))
(insertp (and (not (memq face disallow-faces))
@@ -105,65 +158,83 @@ corresponding unicode symbol."
(memq face allow-faces)))))
(when insertp
(save-excursion
- (when (looking-back company-math-prefix-regexp (point-at-bol))
- (match-string 1))))))
+ (let* ((ppss (syntax-ppss))
+ (min-point (if (nth 3 ppss)
+ (max (nth 8 ppss) (point-at-bol))
+ (point-at-bol))))
+ (when (looking-back regexp min-point 'greedy)
+ (match-string 0)))))))
(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)
+ (when (re-search-backward company-math--unicode-prefix-regexp) ; should
always match
+ (goto-char (match-beginning 0))
+ ;; allow subsups to start with \
+ (let ((start (max (point-min) (- (point) (length
company-math-symbol-prefix)))))
+ (when (string= (buffer-substring-no-properties start (point))
+ company-math-symbol-prefix)
+ (goto-char start)))
+ (delete-region (point) pos)
(insert symbol))))
;;; BACKENDS
;;;###autoload
-(defun company-latex-commands (command &optional arg &rest ignored)
- "Company backend for latex commands."
+(defun company-latex-commands (command &optional arg &rest _ignored)
+ "Company backend for latex commands.
+COMMAND and ARG is as required by company backends."
(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))
+ (company-math--prefix company-math--latex-prefix-regexp t '())))
+ (candidates (all-completions arg company-math--latex-commands))
(sorted t)))
;;;###autoload
-(defun company-math-symbols-latex (command &optional arg &rest ignored)
- "Company backend for LaTeX mathematical symbols."
+(defun company-math-symbols-latex (command &optional arg &rest _ignored)
+ "Company backend for LaTeX mathematical symbols.
+COMMAND and ARG is as required by company backends."
(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--prefix company-math--latex-prefix-regexp
+ 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)
+(defun company-math-symbols-unicode (command &optional arg &rest _ignored)
"Company backend for insertion of Unicode mathematical symbols.
+COMMAND and ARG is as required by company backends.
See the unicode-math page [1] for a list of fonts that have a
-good support for mathematical symbols.
+good support for mathematical symbols. Unicode provides only a
+limited range of sub(super)scripts; see the wikipedia page [2]
+for details.
[1]
http://ftp.snt.utwente.nl/pub/software/tex/help/Catalogue/entries/unicode-math.html
-"
+ [2] https://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts"
(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
+ (prefix (company-math--prefix company-math--unicode-prefix-regexp
+ company-math-allow-unicode-symbols-in-faces
company-math-disallow-unicode-symbols-in-faces))
(annotation (concat " " (get-text-property 0 :symbol arg)))
;; Space added to ensure that completions are never typed in full.
;; See https://github.com/company-mode/company-mode/issues/476
- (candidates (mapcar (lambda (candidate)
- (concat candidate " "))
- (all-completions arg company-math--symbols)))
+ (candidates (delq nil
+ (mapcar (lambda (candidate)
+ (when (get-text-property 0 :symbol candidate)
+ (concat candidate " ")))
+ (all-completions arg company-math--unicode))))
(post-completion (company-math--substitute-unicode
(get-text-property 0 :symbol arg)))))
-
(provide 'company-math)
;;; company-math.el ends here
diff --git a/packages/company-math/readme.md b/packages/company-math/readme.md
index fa80258..bd780f6 100644
--- a/packages/company-math/readme.md
+++ b/packages/company-math/readme.md
@@ -2,11 +2,11 @@ This add-on defines three
*[company-mode](http://company-mode.github.io/)* backe
* `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)
+
![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_)
+* `company-math-symbols-unicode` - math unicode symbols and
sub(super)scripts (_by default, active everywhere except math faces_)
-
![math](https://raw.github.com/vspinu/company-math/master/img/unicode-symbols.png)
+
![math](https://raw.github.com/vspinu/company-math/master/img/unicode-symbols.png)
* `company-latex-commands` - latex commands
@@ -14,27 +14,32 @@ This add-on defines three
*[company-mode](http://company-mode.github.io/)* backe
Start math completion by typing the prefix <kbd>`\`</kbd> key. To select the
completion type <kbd>RET</kbd>. Depending on the context and your configuration
-unicode symbol or latex tag will be inserted.
+unicode symbol or latex tag will be inserted.
+
+Since version 1.2 sub(super)script completion is available for the
+`company-math-symbols-unicode` backend. Subscripts are inserted with either
`__`
+or `\_` prefixes. Superscripts with `^^` or `\^`. Customize
+`company-math-subscript-prefix` and `company-math-superscript-prefix` if you
+don't like this default.
## Activation ##
-Install from [MELPA](http://melpa.milkbox.net/) repository.
+Install from ELPA or MELPA repositories.
You can either register each backend globally:
-```lisp
+```elisp
;; global activation of the unicode symbol completion
(add-to-list 'company-backends 'company-math-symbols-unicode)
-
```
or locally per emacs mode:
-```lisp
+```elisp
;; local configuration for TeX modes
(defun my-latex-mode-setup ()
@@ -48,20 +53,20 @@ or locally per emacs mode:
If you are using `AUCTeX` you might need to use `TeX-mode-hook` instead:
-```
-(add-hook TeX-mode-hook 'my-latex-mode-setup)
+```elisp
+(add-hook 'TeX-mode-hook 'my-latex-mode-setup)
```
-## Customization ##
+## Further Customization ##
-Set `company-tooltip-align-annotations` to t in order to allin symbols to the
-right as in the above previews.
+Set `company-tooltip-align-annotations` to t in order to align symbols to the
+right as in the snapshots from above.
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`,
+environments. You can use the following custom lists of faces to change this
+behavior: `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`.