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

Jump to entry key cli (WIP) #659

Closed
wants to merge 12 commits into from
Closed
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We added a fetcher for [LOBID](https://lobid.org/resources/api) resources. [koppor#386](https://github.com/koppor/jabref/issues/386)
- When in `biblatex` mode, the [integrity check](https://docs.jabref.org/finding-sorting-and-cleaning-entries/checkintegrity) for journal titles now also checks the field `journal`.
- We added support for pushing citations to [TeXShop](https://pages.uoregon.edu/koch/texshop/) on macOS [forum#2699](https://discourse.jabref.org/t/push-to-texshop-mac/2699).
- We added ability to jump to an entry in the command line using -j CITATIONKEY [BIBTEXFILES]. [koppor#540](https://github.com/koppor/jabref/issues/540)
u7282852 marked this conversation as resolved.
Show resolved Hide resolved

### Changed

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ public void processArguments() {
doAuxImport(loaded);
}

if (!cli.isBlank() && cli.isJumpToKey()) {
jumpToKey(loaded, cli.getJumpToKey());
}

this.parserResults = loaded;
}

Expand Down Expand Up @@ -773,6 +777,17 @@ private Optional<ParserResult> fetch(String fetchCommand) {
}
}

private void jumpToKey(List<ParserResult> loaded, String citationKey) {
for (ParserResult parserResult : loaded) {
Optional<BibEntry> entry = parserResult.getDatabase().getEntryByCitationKey(citationKey);
if (entry.isPresent()) {
parserResult.setEntryToFocus(entry.get());
return;
}
}
System.out.printf("Could not find citation key %s in any library%n", citationKey);
}

public boolean isBlank() {
return cli.isBlank();
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/jabref/cli/JabRefCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ public String getWriteMetadatatoPdf() {
cl.hasOption("embeddBibfileInPdf") ? cl.getOptionValue("embeddBibfileInPdf") : null;
}

public String getJumpToKey() {
return cl.getOptionValue("jumpToKey");
}

public boolean isJumpToKey() {
return cl.hasOption("jumpToKey");
}

private static Options getOptions() {
Options options = new Options();

Expand Down Expand Up @@ -288,6 +296,14 @@ private static Options getOptions() {
.argName("CITEKEY1[,CITEKEY2][,CITEKEYn] | PDF1[,PDF2][,PDFn] | all")
.build());

options.addOption(Option
.builder("j")
.longOpt("jumpToKey")
.desc(String.format("%s: '%s'", Localization.lang("Jump to the entry of the given citation key."), "-j key"))
.hasArg()
.argName("CITEKEY")
u7282852 marked this conversation as resolved.
Show resolved Hide resolved
.build());

return options;
}

Expand Down
23 changes: 22 additions & 1 deletion src/main/java/org/jabref/gui/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.jabref.logic.shared.exception.InvalidDBMSConnectionPropertiesException;
import org.jabref.logic.shared.exception.NotASharedDatabaseException;
import org.jabref.logic.util.WebViewStore;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.strings.StringUtil;
import org.jabref.model.util.FileUpdateMonitor;
import org.jabref.preferences.GuiPreferences;
Expand Down Expand Up @@ -201,14 +202,29 @@ private void openDatabases() {
.orElse(preferencesService.getGuiPreferences()
.getLastFocusedFile())
.toAbsolutePath();
// check whether there is a jump to entry and need to focus different file
boolean focusDifferent = false;
Optional<BibEntry> focusedEntry = Optional.empty();
for (ParserResult parserResult : parserResults) {
// Make sure this parser result is its own library instead of imported BibTeX entries
if (parserResult.getEntryToFocus().isPresent() && !parserResult.toOpenTab()) {
focusDifferent = true;
focusedEntry = parserResult.getEntryToFocus();
break;
}
}

// Add all bibDatabases databases to the frame:
boolean first = false;
for (ParserResult parserResult : parserResults) {
// Define focused tab
if (parserResult.getPath().filter(path -> path.toAbsolutePath().equals(focusedFile)).isPresent()) {
if (parserResult.getPath().filter(path -> path.toAbsolutePath().equals(focusedFile)).isPresent() && !focusDifferent) {
first = true;
}
if (parserResult.getEntryToFocus().isPresent() && focusDifferent) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should that be } else if (...?

first = true;
focusDifferent = false;
}

if (parserResult.getDatabase().isShared()) {
try {
Expand Down Expand Up @@ -279,6 +295,11 @@ private void openDatabases() {

OpenDatabaseAction.performPostOpenActions(libraryTab, pr);
}
// focus a particular entry if CLI has received a jump to entry key command
if (focusedEntry.isPresent()) {
mainFrame.getCurrentLibraryTab().clearAndSelect(focusedEntry.get());
mainFrame.showLibraryTab(mainFrame.getCurrentLibraryTab());
}

LOGGER.debug("Finished adding panels");
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/LibraryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public Node createLoadingAnimationLayout() {
public void onDatabaseLoadingStarted() {
Node loadingLayout = createLoadingAnimationLayout();
getMainTable().placeholderProperty().setValue(loadingLayout);
frame.addTab(this, true);
frame.addTab(this, false);
u7282852 marked this conversation as resolved.
Show resolved Hide resolved
}

public void onDatabaseLoadingSucceed(ParserResult result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,10 @@ public void openFiles(List<Path> filesToOpen) {
openTheFile(theFile);
fileHistory.newFile(theFile);
});
} else if (toRaise != null) {
} else if (toRaise != null && frame.getCurrentLibraryTab() == null) {
// If no files are remaining to open, this could mean that a file was
// already open. If so, we may have to raise the correct tab:
// If there is already a library focused, do not show this library
frame.showLibraryTab(toRaise);
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/jabref/logic/importer/ParserResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class ParserResult {
private boolean invalid;
private boolean toOpenTab;
private boolean changedOnMigration = false;
private BibEntry entryToFocus;

public ParserResult() {
this(Collections.emptyList());
Expand Down Expand Up @@ -99,6 +100,14 @@ public void setPath(Path path) {
file = path;
}

public Optional<BibEntry> getEntryToFocus() {
return Optional.ofNullable(entryToFocus);
}

public void setEntryToFocus(BibEntry bibEntry) {
entryToFocus = bibEntry;
}

/**
* Add a parser warning.
*
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,8 @@ Write\ BibTeXEntry\ as\ metadata\ to\ PDF.=Write BibTeXEntry as metadata to PDF.
Write\ metadata\ for\ all\ PDFs\ in\ current\ library?=Write metadata for all PDFs in current library?
Writing\ metadata...=Writing metadata...

Jump\ to\ the\ entry\ of\ the\ given\ citation\ key.=Jump to the entry of the given citation key.

Embed\ BibTeXEntry\ in\ PDF.=Embed BibTeXEntry in PDF.
File\ '%0'\ is\ write\ protected.=File '%0' is write protected.
Write\ BibTeXEntry\ as\ XMP\ metadata\ to\ PDF.=Write BibTeXEntry as XMP metadata to PDF.
Expand Down