From b1066b42c57af93e9de66cd6729702d58ecd6a45 Mon Sep 17 00:00:00 2001 From: Yannick Marion Date: Fri, 10 Nov 2017 14:35:20 +0100 Subject: [PATCH 1/4] Added an option for custom cite command completion updated readme.md accordingly --- README.md | 3 ++- lib/latexer-hook.coffee | 14 +++++++++++++- lib/latexer.coffee | 6 ++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4e1765f..bc70acd 100644 --- a/README.md +++ b/README.md @@ -29,8 +29,9 @@ Latexer will automatically scan your document to find your bibfiles. For example ##### LaTeX Triggers - * Typing in `\cite{`, `\textcite{`, `\citet{`, `\citet*{`, `\citep{` or `\citep*{`. You can also write something in square brackets before, e.g. `\cite[Theorem 1]{`. + * Typing in `\cite{`, `\citet{`, `\citet*{`, `\citep{`, `\citep*{`, or any control sequences that end in `cite{`. You can also write something in square brackets before, e.g. `\cite[Theorem 1]{` * Deleting anything so that the left of the cursor reads `\cite{`, `\textcite{`, `\citet{`, `\citet*{`, `\citep{` or `\citep*{`, e.g. deleting the word 'something' from `\cite{something}` + * Custom Triggers can be added via Settings ##### [Pandoc-style Citation][1] Triggers diff --git a/lib/latexer-hook.coffee b/lib/latexer-hook.coffee index 1f52917..b2f39dc 100644 --- a/lib/latexer-hook.coffee +++ b/lib/latexer-hook.coffee @@ -8,13 +8,15 @@ module.exports = beginRex: /\\begin{([^}]+)}/ mathRex: /(\\+)\[/ refRex: /\\(\w*ref({|{[^}]+,)|[cC](page)?refrange({[^,}]*})?{)$/ - citeRex: /\\\w*(cite|citet|citep|citet\*|citep\*)(\[[^\]]+\])?({|{[^}]+,)$/ constructor: (@editor) -> @disposables = new CompositeDisposable @disposables.add @editor.onDidChangeTitle => @subscribeBuffer() @disposables.add @editor.onDidChangePath => @subscribeBuffer() @disposables.add @editor.onDidSave => @subscribeBuffer() + @oldKeys = [] + @buildCiteRex() + @disposables.add @editor.onDidDestroy(@destroy.bind(this)) @subscribeBuffer() @lv = new LabelView @@ -55,11 +57,21 @@ module.exports = ) if refOpt and (match = line.match(@refRex)) @lv.show(editor) + @buildCiteRex() if citeOpt and (match = line.match(@citeRex)) @cv.show(editor) if pandocCiteOpt and pandoc.isPandocStyleCitation(line) @cv.show(editor) + buildCiteRex: -> + curKeys = atom.config.get("latexer.autocomplete_citations_by") + if @oldKeys != curKeys + citeFilter = "\\\\\\w*(" + citeFilter += curKeys.join("|").replace(/\*/g, "\\*") + citeFilter += ")(\\[[^\\]]+\\])?({|{[^}]+,)$" + @citeRex = RegExp(citeFilter) + @oldKeys = curKeys + environmentCheck: (editor)-> pos = editor.getCursorBufferPosition().toArray() return if pos[0] <= 0 diff --git a/lib/latexer.coffee b/lib/latexer.coffee index 043511d..05afad2 100644 --- a/lib/latexer.coffee +++ b/lib/latexer.coffee @@ -18,6 +18,12 @@ module.exports = Latexer = items: type: "string" + autocomplete_citations_by: + type: "array" + default: ["cite", "citet", "citep", "citet*", "citep*"] + items: + type: "string" + autocomplete_environments: type: "boolean" default: true From 4482ba0ed81ec98fa134e1d6ec147a9eb59c1708 Mon Sep 17 00:00:00 2001 From: Yannick Marion Date: Mon, 13 Nov 2017 12:39:17 +0100 Subject: [PATCH 2/4] Added an option for custom ref command completion --- lib/latexer-hook.coffee | 19 +++++++++++++++---- lib/latexer.coffee | 6 ++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/latexer-hook.coffee b/lib/latexer-hook.coffee index b2f39dc..f75f1ba 100644 --- a/lib/latexer-hook.coffee +++ b/lib/latexer-hook.coffee @@ -7,15 +7,16 @@ module.exports = class LatexerHook beginRex: /\\begin{([^}]+)}/ mathRex: /(\\+)\[/ - refRex: /\\(\w*ref({|{[^}]+,)|[cC](page)?refrange({[^,}]*})?{)$/ constructor: (@editor) -> @disposables = new CompositeDisposable @disposables.add @editor.onDidChangeTitle => @subscribeBuffer() @disposables.add @editor.onDidChangePath => @subscribeBuffer() @disposables.add @editor.onDidSave => @subscribeBuffer() - @oldKeys = [] + @oldCiteKeys = [] @buildCiteRex() + @oldRefKeys = [] + @buildRefRex() @disposables.add @editor.onDidDestroy(@destroy.bind(this)) @subscribeBuffer() @@ -55,6 +56,7 @@ module.exports = [cursor.row, cursor.column] ] ) + @buildRefRex() if refOpt and (match = line.match(@refRex)) @lv.show(editor) @buildCiteRex() @@ -65,12 +67,21 @@ module.exports = buildCiteRex: -> curKeys = atom.config.get("latexer.autocomplete_citations_by") - if @oldKeys != curKeys + if @oldCiteKeys != curKeys citeFilter = "\\\\\\w*(" citeFilter += curKeys.join("|").replace(/\*/g, "\\*") citeFilter += ")(\\[[^\\]]+\\])?({|{[^}]+,)$" @citeRex = RegExp(citeFilter) - @oldKeys = curKeys + @oldCiteKeys = curKeys + + buildRefRex: -> + curRefs = atom.config.get("latexer.autocomplete_references_by") + if @oldRefKeys != curRefs + refFilter = "\\\\(\\w*(" + refFilter += curRefs.join("|").replace(/\*/g, "\\*") + refFilter += ")({|{[^}]+,)|[cC](page)?refrange({[^,}]*})?{)$" + @refRex = RegExp(refFilter) + @oldRefKeys = curRefs environmentCheck: (editor)-> pos = editor.getCursorBufferPosition().toArray() diff --git a/lib/latexer.coffee b/lib/latexer.coffee index 05afad2..3d04e73 100644 --- a/lib/latexer.coffee +++ b/lib/latexer.coffee @@ -32,6 +32,12 @@ module.exports = Latexer = type: "boolean" default: true + autocomplete_references_by: + type: "array" + default: ["ref"] + items: + type: "string" + autocomplete_citations: type: "boolean" default: true From ae1ca14dc34854d71520105080130b02b8c0d8a4 Mon Sep 17 00:00:00 2001 From: Yannick Marion Date: Tue, 14 Nov 2017 14:26:41 +0100 Subject: [PATCH 3/4] better regex for cite recognition Standard cite commands can have up to two square brackets before the bibliography key. Additionally, there doesn't have to be content inside those square brackets. --- lib/latexer-hook.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/latexer-hook.coffee b/lib/latexer-hook.coffee index f75f1ba..3b1c158 100644 --- a/lib/latexer-hook.coffee +++ b/lib/latexer-hook.coffee @@ -70,7 +70,7 @@ module.exports = if @oldCiteKeys != curKeys citeFilter = "\\\\\\w*(" citeFilter += curKeys.join("|").replace(/\*/g, "\\*") - citeFilter += ")(\\[[^\\]]+\\])?({|{[^}]+,)$" + citeFilter += ")(\\[[^\\]]*\\]){0,2}({|{[^}]+,)$" @citeRex = RegExp(citeFilter) @oldCiteKeys = curKeys From 49a563f80b9382e346f40c9c8c6d8133960db382 Mon Sep 17 00:00:00 2001 From: Yannick Marion Date: Tue, 28 Nov 2017 14:27:26 +0100 Subject: [PATCH 4/4] Updated README.md to reflect changes --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bc70acd..6b0bdb5 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ Features Triggers: * Typing in `\ref{`, `\eqref{` or any control sequences that ends in `ref{` * Deleting anything so that the left of the cursor reads `\ref{`, `\eqref{`, and the like. E.g. deleting the word 'something' from `\pageref{something}` + * Custom Triggers can be added via settings ### Bibliography and citation autocompletion @@ -29,9 +30,9 @@ Latexer will automatically scan your document to find your bibfiles. For example ##### LaTeX Triggers - * Typing in `\cite{`, `\citet{`, `\citet*{`, `\citep{`, `\citep*{`, or any control sequences that end in `cite{`. You can also write something in square brackets before, e.g. `\cite[Theorem 1]{` + * Typing in `\cite{`, `\citet{`, `\citet*{`, `\citep{`, `\citep*{`, or any control sequences that end in `cite{`. You can also write something in square brackets before, e.g. `\cite[Theorem 1]{` or `\cite[See:][Page 51-59]{` * Deleting anything so that the left of the cursor reads `\cite{`, `\textcite{`, `\citet{`, `\citet*{`, `\citep{` or `\citep*{`, e.g. deleting the word 'something' from `\cite{something}` - * Custom Triggers can be added via Settings + * Custom Triggers can be added via settings ##### [Pandoc-style Citation][1] Triggers