Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manually edit the triggers of cite and ref #65

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -29,8 +30,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]{` 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

##### [Pandoc-style Citation][1] Triggers

Expand Down
27 changes: 25 additions & 2 deletions lib/latexer-hook.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ module.exports =
class LatexerHook
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()

@oldCiteKeys = []
@buildCiteRex()
@oldRefKeys = []
@buildRefRex()

@disposables.add @editor.onDidDestroy(@destroy.bind(this))
@subscribeBuffer()
@lv = new LabelView
Expand Down Expand Up @@ -53,13 +56,33 @@ module.exports =
[cursor.row, cursor.column]
]
)
@buildRefRex()
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 @oldCiteKeys != curKeys
citeFilter = "\\\\\\w*("
citeFilter += curKeys.join("|").replace(/\*/g, "\\*")
citeFilter += ")(\\[[^\\]]*\\]){0,2}({|{[^}]+,)$"
@citeRex = RegExp(citeFilter)
@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()
return if pos[0] <= 0
Expand Down
12 changes: 12 additions & 0 deletions lib/latexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,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
Expand Down