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

Fix Copy cite command should respect preferences #10707

Merged
merged 14 commits into from
Dec 22, 2023
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/actions/StandardActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public enum StandardActions implements Action {
COPY_MORE(Localization.lang("Copy") + "..."),
COPY_TITLE(Localization.lang("Copy title"), KeyBinding.COPY_TITLE),
COPY_KEY(Localization.lang("Copy citation key"), KeyBinding.COPY_CITATION_KEY),
COPY_CITE_KEY(Localization.lang("Copy \\cite{citation key}"), KeyBinding.COPY_CITE_CITATION_KEY),
COPY_CITE_KEY(Localization.lang("Copy key with configured cite command"), KeyBinding.COPY_CITE_CITATION_KEY),
COPY_KEY_AND_TITLE(Localization.lang("Copy citation key and title"), KeyBinding.COPY_CITATION_KEY_AND_TITLE),
COPY_KEY_AND_LINK(Localization.lang("Copy citation key and link"), KeyBinding.COPY_CITATION_KEY_AND_LINK),
COPY_CITATION_HTML(Localization.lang("Copy citation (html)"), KeyBinding.COPY_PREVIEW),
Expand Down
50 changes: 29 additions & 21 deletions src/main/java/org/jabref/gui/edit/CopyMoreAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.jabref.gui.ClipBoardManager;
Expand All @@ -17,7 +16,9 @@
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.layout.Layout;
import org.jabref.logic.layout.LayoutHelper;
import org.jabref.logic.push.CitationCommandString;
import org.jabref.logic.util.OS;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.preferences.PreferencesService;
Expand Down Expand Up @@ -58,13 +59,20 @@ public void execute() {
}

switch (action) {
case COPY_TITLE -> copyTitle();
case COPY_KEY -> copyKey();
case COPY_CITE_KEY -> copyCiteKey();
case COPY_KEY_AND_TITLE -> copyKeyAndTitle();
case COPY_KEY_AND_LINK -> copyKeyAndLink();
case COPY_DOI, COPY_DOI_URL -> copyDoi();
default -> LOGGER.info("Unknown copy command.");
case COPY_TITLE ->
copyTitle();
case COPY_KEY ->
copyKey();
case COPY_CITE_KEY ->
copyCiteKey();
case COPY_KEY_AND_TITLE ->
copyKeyAndTitle();
case COPY_KEY_AND_LINK ->
copyKeyAndLink();
case COPY_DOI, COPY_DOI_URL ->
copyDoi();
default ->
LOGGER.info("Unknown copy command.");
}
}

Expand Down Expand Up @@ -127,14 +135,14 @@ private void copyDoi() {
// Collect all non-null DOI or DOI urls
if (action == StandardActions.COPY_DOI_URL) {
copyDoiList(entries.stream()
.filter(entry -> entry.getDOI().isPresent())
.map(entry -> entry.getDOI().get().getURIAsASCIIString())
.collect(Collectors.toList()), entries.size());
.filter(entry -> entry.getDOI().isPresent())
.map(entry -> entry.getDOI().get().getURIAsASCIIString())
.collect(Collectors.toList()), entries.size());
} else {
copyDoiList(entries.stream()
.filter(entry -> entry.getDOI().isPresent())
.map(entry -> entry.getDOI().get().getDOI())
.collect(Collectors.toList()), entries.size());
.filter(entry -> entry.getDOI().isPresent())
.map(entry -> entry.getDOI().get().getDOI())
.collect(Collectors.toList()), entries.size());
}
}

Expand Down Expand Up @@ -171,11 +179,9 @@ private void copyCiteKey() {
return;
}

String citeCommand = Optional.ofNullable(preferencesService.getExternalApplicationsPreferences().getCiteCommand())
.filter(cite -> cite.contains("\\")) // must contain \
.orElse("\\cite");
CitationCommandString citeCommand = preferencesService.getExternalApplicationsPreferences().getCiteCommand();

final String copiedCiteCommand = citeCommand + "{" + String.join(",", keys) + '}';
final String copiedCiteCommand = citeCommand.prefix() + String.join(citeCommand.delimiter(), keys) + citeCommand.suffix();
clipBoardManager.setContent(copiedCiteCommand);

if (keys.size() == entries.size()) {
Expand Down Expand Up @@ -208,7 +214,9 @@ private void copyKeyAndTitle() {
for (BibEntry entry : entries) {
if (entry.hasCitationKey()) {
entriesWithKeys++;
keyAndTitle.append(layout.doLayout(entry, stateManager.getActiveDatabase().get().getDatabase()));
stateManager.getActiveDatabase()
.map(BibDatabaseContext::getDatabase)
.ifPresent(bibDatabase -> keyAndTitle.append(layout.doLayout(entry, bibDatabase)));
}
}

Expand Down Expand Up @@ -242,15 +250,15 @@ private void copyKeyAndLink() {

List<BibEntry> entriesWithKey = entries.stream()
.filter(BibEntry::hasCitationKey)
.collect(Collectors.toList());
.toList();

if (entriesWithKey.isEmpty()) {
dialogService.notify(Localization.lang("None of the selected entries have citation keys."));
return;
}

for (BibEntry entry : entriesWithKey) {
String key = entry.getCitationKey().get();
String key = entry.getCitationKey().orElse("");
String url = entry.getField(StandardField.URL).orElse("");
keyAndLink.append(url.isEmpty() ? key : String.format("<a href=\"%s\">%s</a>", url, key));
keyAndLink.append(OS.NEWLINE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.jabref.gui.push.PushToEmacs;
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.push.CitationCommandString;
import org.jabref.model.strings.StringUtil;
import org.jabref.preferences.ExternalApplicationsPreferences;
import org.jabref.preferences.PreferencesService;
Expand All @@ -39,7 +40,7 @@ public class ExternalTabViewModel implements PreferenceTabViewModel {
private final BooleanProperty autoOpenAttachedFoldersProperty = new SimpleBooleanProperty();
private final ListProperty<PushToApplication> pushToApplicationsListProperty = new SimpleListProperty<>();
private final ObjectProperty<PushToApplication> selectedPushToApplicationProperty = new SimpleObjectProperty<>();
private final StringProperty citeCommandProperty = new SimpleStringProperty("");
private final StringProperty citeCommandProperty = new SimpleStringProperty();
private final BooleanProperty useCustomTerminalProperty = new SimpleBooleanProperty();
private final StringProperty customTerminalCommandProperty = new SimpleStringProperty("");
private final BooleanProperty useCustomFileBrowserProperty = new SimpleBooleanProperty();
Expand Down Expand Up @@ -97,7 +98,8 @@ public void setValues() {
PushToApplications.getApplicationByName(initialPushToApplicationPreferences.getActiveApplicationName(), dialogService, preferences)
.orElse(new PushToEmacs(dialogService, preferences)));

citeCommandProperty.setValue(initialExternalApplicationPreferences.getCiteCommand());
citeCommandProperty.setValue(initialExternalApplicationPreferences.getCiteCommand().toString());

useCustomTerminalProperty.setValue(initialExternalApplicationPreferences.useCustomTerminal());
customTerminalCommandProperty.setValue(initialExternalApplicationPreferences.getCustomTerminalCommand());
useCustomFileBrowserProperty.setValue(initialExternalApplicationPreferences.useCustomFileBrowser());
Expand All @@ -110,7 +112,10 @@ public void storeSettings() {
ExternalApplicationsPreferences externalPreferences = preferences.getExternalApplicationsPreferences();
externalPreferences.setEMailSubject(eMailReferenceSubjectProperty.getValue());
externalPreferences.setAutoOpenEmailAttachmentsFolder(autoOpenAttachedFoldersProperty.getValue());
externalPreferences.setCiteCommand(citeCommandProperty.getValue());


externalPreferences.setCiteCommand(CitationCommandString.from(citeCommandProperty.getValue()));

externalPreferences.setUseCustomTerminal(useCustomTerminalProperty.getValue());
externalPreferences.setCustomTerminalCommand(customTerminalCommandProperty.getValue());
externalPreferences.setUseCustomFileBrowser(useCustomFileBrowserProperty.getValue());
Expand Down Expand Up @@ -229,6 +234,6 @@ public StringProperty customFileBrowserCommandProperty() {
}

public void resetCiteCommandToDefault() {
this.citeCommandProperty.setValue(preferences.getExternalApplicationsPreferences().getDefaultCiteCommand());
this.citeCommandProperty.setValue(preferences.getExternalApplicationsPreferences().getDefaultCiteCommand().toString());
}
}
25 changes: 5 additions & 20 deletions src/main/java/org/jabref/gui/push/AbstractPushToApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.jabref.gui.icon.JabRefIcon;
import org.jabref.gui.keyboard.KeyBinding;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.push.CitationCommandString;
import org.jabref.logic.util.OS;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
Expand All @@ -26,9 +27,6 @@
public abstract class AbstractPushToApplication implements PushToApplication {

private static final Logger LOGGER = LoggerFactory.getLogger(AbstractPushToApplication.class);
private static final String CITE_KEY1 = "key1";
private static final String CITE_KEY2 = "key2";

protected boolean couldNotCall; // Set to true in case the command could not be executed, e.g., if the file is not found
protected boolean couldNotPush; // Set to true in case the tunnel to the program (if one is used) does not operate
protected boolean notDefined; // Set to true if the corresponding path is not defined in the preferences
Expand All @@ -38,7 +36,6 @@ public abstract class AbstractPushToApplication implements PushToApplication {
protected final DialogService dialogService;
protected final PreferencesService preferencesService;

private String cachedCiteCommand;
private String cachedCitePrefix;
private String cachedCiteSuffix;
private String cachedCiteDelimiter;
Expand All @@ -49,23 +46,11 @@ public AbstractPushToApplication(DialogService dialogService, PreferencesService
}

private void dissectCiteCommand() {
String preferencesCiteCommand = preferencesService.getExternalApplicationsPreferences().getCiteCommand();

if (preferencesCiteCommand != null && preferencesCiteCommand.equals(cachedCiteCommand)) {
return;
}

cachedCiteCommand = preferencesCiteCommand;

int indexKey1 = cachedCiteCommand.indexOf(CITE_KEY1);
int indexKey2 = cachedCiteCommand.indexOf(CITE_KEY2);
if (indexKey1 < 0 || indexKey2 < 0 || indexKey2 < (indexKey1 + CITE_KEY1.length())) {
return;
}
CitationCommandString preferencesCiteCommand = preferencesService.getExternalApplicationsPreferences().getCiteCommand();

cachedCitePrefix = preferencesCiteCommand.substring(0, indexKey1);
cachedCiteDelimiter = preferencesCiteCommand.substring(preferencesCiteCommand.lastIndexOf(CITE_KEY1) + CITE_KEY1.length(), indexKey2);
cachedCiteSuffix = preferencesCiteCommand.substring(preferencesCiteCommand.lastIndexOf(CITE_KEY2) + CITE_KEY2.length());
cachedCitePrefix = preferencesCiteCommand.prefix();
cachedCiteDelimiter = preferencesCiteCommand.delimiter();
cachedCiteSuffix = preferencesCiteCommand.suffix();
}

@Override
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/org/jabref/logic/push/CitationCommandString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.jabref.logic.push;

public record CitationCommandString(
String prefix,
String delimiter,
String suffix) {
private static final String CITE_KEY1 = "key1";
private static final String CITE_KEY2 = "key2";

@Override
public String toString() {
return prefix + CITE_KEY1 + delimiter + CITE_KEY2 + suffix;
}

public static CitationCommandString from(String completeCiteCommand) {

int indexKey1 = completeCiteCommand.indexOf(CITE_KEY1);
int indexKey2 = completeCiteCommand.indexOf(CITE_KEY2);
if (indexKey1 < 0 || indexKey2 < 0 || indexKey2 < (indexKey1 + CITE_KEY1.length())) {
return new CitationCommandString("", "", "");
}

String prefix = completeCiteCommand.substring(0, indexKey1);
String delim = completeCiteCommand.substring(completeCiteCommand.lastIndexOf(CITE_KEY1) + CITE_KEY1.length(), indexKey2);
String suffix = completeCiteCommand.substring(completeCiteCommand.lastIndexOf(CITE_KEY2) + CITE_KEY2.length());
return new CitationCommandString(prefix, delim, suffix);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package org.jabref.preferences;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

import org.jabref.logic.push.CitationCommandString;

public class ExternalApplicationsPreferences {

private final StringProperty eMailSubject;
private final BooleanProperty shouldAutoOpenEmailAttachmentsFolder;
private final StringProperty citeCommand;
private final ObjectProperty<CitationCommandString> citeCommand;

private final StringProperty defaultCiteCommand;
private final ObjectProperty<CitationCommandString> defaultCiteCommand;

private final BooleanProperty useCustomTerminal;
private final StringProperty customTerminalCommand;
Expand All @@ -21,8 +25,8 @@ public class ExternalApplicationsPreferences {

public ExternalApplicationsPreferences(String eMailSubject,
boolean shouldAutoOpenEmailAttachmentsFolder,
String citeCommand,
String defaultCiteCommand,
CitationCommandString citeCommand,
CitationCommandString defaultCiteCommand,
boolean useCustomTerminal,
String customTerminalCommand,
boolean useCustomFileBrowser,
Expand All @@ -31,8 +35,8 @@ public ExternalApplicationsPreferences(String eMailSubject,

this.eMailSubject = new SimpleStringProperty(eMailSubject);
this.shouldAutoOpenEmailAttachmentsFolder = new SimpleBooleanProperty(shouldAutoOpenEmailAttachmentsFolder);
this.citeCommand = new SimpleStringProperty(citeCommand);
this.defaultCiteCommand = new SimpleStringProperty(defaultCiteCommand);
this.citeCommand = new SimpleObjectProperty<>(citeCommand);
this.defaultCiteCommand = new SimpleObjectProperty<>(defaultCiteCommand);
this.useCustomTerminal = new SimpleBooleanProperty(useCustomTerminal);
this.customTerminalCommand = new SimpleStringProperty(customTerminalCommand);
this.useCustomFileBrowser = new SimpleBooleanProperty(useCustomFileBrowser);
Expand Down Expand Up @@ -64,15 +68,15 @@ public void setAutoOpenEmailAttachmentsFolder(boolean shouldAutoOpenEmailAttachm
this.shouldAutoOpenEmailAttachmentsFolder.set(shouldAutoOpenEmailAttachmentsFolder);
}

public String getCiteCommand() {
public CitationCommandString getCiteCommand() {
return citeCommand.get();
}

public StringProperty citeCommandProperty() {
public ObjectProperty<CitationCommandString> citeCommandProperty() {
return citeCommand;
}

public void setCiteCommand(String citeCommand) {
public void setCiteCommand(CitationCommandString citeCommand) {
this.citeCommand.set(citeCommand);
}

Expand Down Expand Up @@ -136,7 +140,7 @@ public void setKindleEmail(String kindleEmail) {
this.kindleEmail.set(kindleEmail);
}

public String getDefaultCiteCommand() {
public CitationCommandString getDefaultCiteCommand() {
return defaultCiteCommand.getValue();
}
}
Loading