Skip to content

Commit 17c9086

Browse files
committed
Add possibility to reformat all entries on save (under Preferences, File)
Implements [JabRef#756](JabRef#756)
1 parent c80ff7f commit 17c9086

23 files changed

+149
-12
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ to [sourceforge feature requests](https://sourceforge.net/p/jabref/features/) by
1313
## [Unreleased]
1414

1515
### Changed
16+
- Implemented [#756](https://github.com/JabRef/jabref/issues/756): Add possibility to reformat all entries on save (under Preferences, File)
1617
- Comments and preamble are serialized with capitalized first letter, i.e. `@Comment` instead of `@comment` and `@Preamble` instead of `@PREAMBLE`.
1718
- Global sorting options and preferences are removed. Databases can still be sorted on save, but this is configured locally and stored in the file
1819
- OvidImporter now also imports fields: doi, issn, language and keywords

src/main/java/net/sf/jabref/JabRefPreferences.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
public class JabRefPreferences {
6161
private static final Log LOGGER = LogFactory.getLog(JabRefPreferences.class);
6262
public static final String EXTERNAL_FILE_TYPES = "externalFileTypes";
63-
6463
/**
6564
* HashMap that contains all preferences which are set by default
6665
*/
@@ -110,6 +109,7 @@ public class JabRefPreferences {
110109
public static final String TABLE_SECONDARY_SORT_DESCENDING = "secDescending";
111110
public static final String TABLE_TERTIARY_SORT_FIELD = "terSort";
112111
public static final String TABLE_TERTIARY_SORT_DESCENDING = "terDescending";
112+
public static final String REFORMAT_FILE_ON_SAVE_AND_EXPORT = "reformatFileOnSaveAndExport";
113113
public static final String EXPORT_IN_ORIGINAL_ORDER = "exportInOriginalOrder";
114114
public static final String EXPORT_IN_SPECIFIED_ORDER = "exportInSpecifiedOrder";
115115
public static final String EXPORT_PRIMARY_SORT_FIELD = "exportPriSort";
@@ -505,6 +505,8 @@ private JabRefPreferences() {
505505
defaults.put(TABLE_TERTIARY_SORT_FIELD, "title");
506506
defaults.put(TABLE_TERTIARY_SORT_DESCENDING, Boolean.FALSE);
507507

508+
defaults.put(REFORMAT_FILE_ON_SAVE_AND_EXPORT, Boolean.FALSE);
509+
508510
// export order
509511
defaults.put(EXPORT_IN_ORIGINAL_ORDER, Boolean.FALSE);
510512
defaults.put(EXPORT_IN_SPECIFIED_ORDER, Boolean.FALSE);

src/main/java/net/sf/jabref/bibtex/BibEntryWriter.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ public BibEntryWriter(LatexFieldFormatter fieldFormatter, boolean write) {
2727
}
2828

2929
public void write(BibEntry entry, Writer out, BibDatabaseMode bibDatabaseMode) throws IOException {
30+
write(entry, out, bibDatabaseMode, false);
31+
}
32+
public void write(BibEntry entry, Writer out, BibDatabaseMode bibDatabaseMode, Boolean reformat) throws IOException {
3033
// if the entry has not been modified, write it as it was
31-
if (!entry.hasChanged()) {
34+
if (!reformat && !entry.hasChanged()) {
3235
out.write(entry.getParsedSerialization());
3336
return;
3437
}

src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public void writePartOfDatabase(Writer writer, BibDatabaseContext bibDatabaseCon
178178
writePreamble(writer, bibDatabaseContext.getDatabase().getPreamble());
179179

180180
// Write strings if there are any.
181-
writeStrings(writer, bibDatabaseContext.getDatabase());
181+
writeStrings(writer, bibDatabaseContext.getDatabase(), preferences.isReformatFile());
182182

183183
// Write database entries.
184184
List<BibEntry> sortedEntries = BibDatabaseWriter.getSortedEntries(bibDatabaseContext,
@@ -198,7 +198,7 @@ public void writePartOfDatabase(Writer writer, BibDatabaseContext bibDatabaseCon
198198
entryType -> typesToWrite.put(entryType.getName(), entryType));
199199
}
200200

201-
bibtexEntryWriter.write(entry, writer, bibDatabaseContext.getMode());
201+
bibtexEntryWriter.write(entry, writer, bibDatabaseContext.getMode(), preferences.isReformatFile());
202202

203203
}
204204

@@ -324,13 +324,14 @@ private void writePreamble(Writer fw, String preamble) throws IOException {
324324
}
325325
}
326326

327-
private void writeString(Writer fw, BibtexString bs, Map<String, BibtexString> remaining, int maxKeyLength)
327+
private void writeString(Writer fw, BibtexString bs, Map<String, BibtexString> remaining, int maxKeyLength,
328+
Boolean reformatFile)
328329
throws IOException {
329330
// First remove this from the "remaining" list so it can't cause problem with circular refs:
330331
remaining.remove(bs.getName());
331332

332333
//if the string has not been modified, write it back as it was
333-
if (!bs.hasChanged()) {
334+
if (!reformatFile && !bs.hasChanged()) {
334335
fw.write(bs.getParsedSerialization());
335336
return;
336337
}
@@ -351,7 +352,7 @@ private void writeString(Writer fw, BibtexString bs, Map<String, BibtexString> r
351352
Object referred = remaining.get(foundLabel.substring(1, foundLabel.length() - 1));
352353
// If the label we found exists as a key in the "remaining" Map, we go on and write it now:
353354
if (referred != null) {
354-
writeString(fw, (BibtexString) referred, remaining, maxKeyLength);
355+
writeString(fw, (BibtexString) referred, remaining, maxKeyLength, reformatFile);
355356
}
356357
}
357358

@@ -384,9 +385,10 @@ private void writeString(Writer fw, BibtexString bs, Map<String, BibtexString> r
384385
*
385386
* @param fw The Writer to send the output to.
386387
* @param database The database whose strings we should write.
388+
* @param reformatFile
387389
* @throws IOException If anything goes wrong in writing.
388390
*/
389-
private void writeStrings(Writer fw, BibDatabase database) throws IOException {
391+
private void writeStrings(Writer fw, BibDatabase database, Boolean reformatFile) throws IOException {
390392
List<BibtexString> strings = database.getStringKeySet().stream().map(database::getString).collect(
391393
Collectors.toList());
392394
strings.sort(new BibtexStringComparator(true));
@@ -402,7 +404,7 @@ private void writeStrings(Writer fw, BibDatabase database) throws IOException {
402404
isFirstStringInType = true;
403405
for (BibtexString bs : strings) {
404406
if (remaining.containsKey(bs.getName()) && (bs.getType() == t)) {
405-
writeString(fw, bs, remaining, maxKeyLength);
407+
writeString(fw, bs, remaining, maxKeyLength, reformatFile);
406408
isFirstStringInType = false;
407409
}
408410
}

src/main/java/net/sf/jabref/exporter/SavePreferences.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
public class SavePreferences {
1010

11+
private boolean reformatFile;
1112
private boolean saveInOriginalOrder;
1213
private SaveOrderConfig saveOrder;
1314
private Charset encoding;
@@ -20,13 +21,14 @@ public SavePreferences() {
2021
}
2122

2223
public SavePreferences(Boolean saveInOriginalOrder, SaveOrderConfig saveOrder, Charset encoding, Boolean makeBackup,
23-
DatabaseSaveType saveType, Boolean takeMetadataSaveOrderInAccount) {
24+
DatabaseSaveType saveType, Boolean takeMetadataSaveOrderInAccount, Boolean reformatFile) {
2425
this.saveInOriginalOrder = saveInOriginalOrder;
2526
this.saveOrder = saveOrder;
2627
this.encoding = encoding;
2728
this.makeBackup = makeBackup;
2829
this.saveType = saveType;
2930
this.takeMetadataSaveOrderInAccount = takeMetadataSaveOrderInAccount;
31+
this.reformatFile = reformatFile;
3032
}
3133

3234
public static SavePreferences loadForExportFromPreferences(JabRefPreferences preferences) {
@@ -43,8 +45,9 @@ public static SavePreferences loadForExportFromPreferences(JabRefPreferences pre
4345
Boolean makeBackup = preferences.getBoolean(JabRefPreferences.BACKUP);
4446
DatabaseSaveType saveType = DatabaseSaveType.ALL;
4547
Boolean takeMetadataSaveOrderInAccount = false;
48+
Boolean reformatFile = preferences.getBoolean(JabRefPreferences.REFORMAT_FILE_ON_SAVE_AND_EXPORT);
4649
return new SavePreferences(saveInOriginalOrder, saveOrder, encoding, makeBackup, saveType,
47-
takeMetadataSaveOrderInAccount);
50+
takeMetadataSaveOrderInAccount, reformatFile);
4851
}
4952

5053
public static SavePreferences loadForSaveFromPreferences(JabRefPreferences preferences) {
@@ -54,8 +57,9 @@ public static SavePreferences loadForSaveFromPreferences(JabRefPreferences prefe
5457
Boolean makeBackup = preferences.getBoolean(JabRefPreferences.BACKUP);
5558
DatabaseSaveType saveType = DatabaseSaveType.ALL;
5659
Boolean takeMetadataSaveOrderInAccount = true;
60+
Boolean reformatFile = preferences.getBoolean(JabRefPreferences.REFORMAT_FILE_ON_SAVE_AND_EXPORT);
5761
return new SavePreferences(saveInOriginalOrder, saveOrder, encoding, makeBackup, saveType,
58-
takeMetadataSaveOrderInAccount);
62+
takeMetadataSaveOrderInAccount, reformatFile);
5963
}
6064

6165
public Boolean getTakeMetadataSaveOrderInAccount() {
@@ -98,6 +102,14 @@ public void setSaveType(DatabaseSaveType saveType) {
98102
this.saveType = saveType;
99103
}
100104

105+
public Boolean isReformatFile() {
106+
return reformatFile;
107+
}
108+
109+
public void setReformatFile(boolean reformatFile) {
110+
this.reformatFile = reformatFile;
111+
}
112+
101113
public enum DatabaseSaveType {
102114
ALL,
103115
PLAIN_BIBTEX

src/main/java/net/sf/jabref/gui/preftabs/FileTab.java

+9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import javax.swing.event.ChangeListener;
3333

3434
import net.sf.jabref.Globals;
35+
import net.sf.jabref.JabRef;
3536
import net.sf.jabref.gui.JabRefFrame;
3637
import net.sf.jabref.JabRefPreferences;
3738

@@ -56,6 +57,7 @@ class FileTab extends JPanel implements PrefsTab {
5657
private final JCheckBox promptBeforeUsingAutoSave;
5758
private final JComboBox<String> valueDelimiter;
5859
private final JComboBox<String> newlineSeparator;
60+
private final JCheckBox reformatFileOnSaveAndExport;
5961
private final JRadioButton resolveStringsStandard;
6062
private final JRadioButton resolveStringsAll;
6163
private final JTextField bracesAroundCapitalsFields;
@@ -86,6 +88,8 @@ public FileTab(JabRefFrame frame, JabRefPreferences prefs) {
8688
// This is sort of a quick hack
8789
newlineSeparator = new JComboBox<>(new String[] {"CR", "CR/LF", "LF"});
8890

91+
reformatFileOnSaveAndExport = new JCheckBox(Localization.lang("Always reformat .bib file on save and export"));
92+
8993
bracesAroundCapitalsFields = new JTextField(25);
9094
nonWrappableFields = new JTextField(25);
9195
doNotResolveStringsFor = new JTextField(30);
@@ -128,6 +132,9 @@ public void stateChanged(ChangeEvent changeEvent) {
128132
builder.append(newlineSeparator);
129133
builder.nextLine();
130134

135+
builder.append(reformatFileOnSaveAndExport, 3);
136+
builder.nextLine();
137+
131138
builder.appendSeparator(Localization.lang("Autosave"));
132139
builder.append(autoSave, 1);
133140
JButton help = new HelpAction(HelpFiles.autosaveHelp).getHelpButton();
@@ -164,6 +171,7 @@ public void setValues() {
164171
// fallback: windows standard
165172
newlineSeparator.setSelectedIndex(1);
166173
}
174+
reformatFileOnSaveAndExport.setSelected(prefs.getBoolean(JabRefPreferences.REFORMAT_FILE_ON_SAVE_AND_EXPORT));
167175

168176
resolveStringsAll.setSelected(prefs.getBoolean(JabRefPreferences.RESOLVE_STRINGS_ALL_FIELDS));
169177
resolveStringsStandard.setSelected(!resolveStringsAll.isSelected());
@@ -195,6 +203,7 @@ public void storeSettings() {
195203
// we also have to change Globals variable as globals is not a getter, but a constant
196204
Globals.NEWLINE = newline;
197205

206+
prefs.putBoolean(JabRefPreferences.REFORMAT_FILE_ON_SAVE_AND_EXPORT, reformatFileOnSaveAndExport.isSelected());
198207
prefs.putBoolean(JabRefPreferences.BACKUP, backup.isSelected());
199208
prefs.putBoolean(JabRefPreferences.OPEN_LAST_EDITED, openLast.isSelected());
200209
prefs.putBoolean(JabRefPreferences.RESOLVE_STRINGS_ALL_FIELDS, resolveStringsAll.isSelected());

src/main/resources/l10n/JabRef_da.properties

+2
Original file line numberDiff line numberDiff line change
@@ -1748,3 +1748,5 @@ New_%0_database=
17481748
New_%0_database_created.=
17491749

17501750
No_entry_found_for_IBSN_%0_at_www.ebook.de=
1751+
1752+
Always_reformat_.bib_file_on_save_and_export=

src/main/resources/l10n/JabRef_de.properties

+2
Original file line numberDiff line numberDiff line change
@@ -2457,3 +2457,5 @@ New_%0_database=
24572457
New_%0_database_created.=
24582458

24592459
No_entry_found_for_IBSN_%0_at_www.ebook.de=
2460+
2461+
Always_reformat_.bib_file_on_save_and_export=

src/main/resources/l10n/JabRef_en.properties

+3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ All_fields=All_fields
9191
All_subgroups_(recursively)=All_subgroups_(recursively)
9292

9393
Allow_editing_in_table_cells=Allow_editing_in_table_cells
94+
95+
Always_reformat_.bib_file_on_save_and_export=Always_reformat_.bib_file_on_save_and_export
96+
9497
An_Error_occurred_while_fetching_from_OAI2_source_(%0)\:=An_Error_occurred_while_fetching_from_OAI2_source_(%0):
9598
An_Exception_occurred_while_accessing_'%0'=An_Exception_occurred_while_accessing_'%0'
9699
An_SAXException_occurred_while_parsing_'%0'\:=An_SAXException_occurred_while_parsing_'%0':

src/main/resources/l10n/JabRef_es.properties

+2
Original file line numberDiff line numberDiff line change
@@ -1646,3 +1646,5 @@ New_%0_database=
16461646
New_%0_database_created.=
16471647

16481648
No_entry_found_for_IBSN_%0_at_www.ebook.de=
1649+
1650+
Always_reformat_.bib_file_on_save_and_export=

src/main/resources/l10n/JabRef_fa.properties

+2
Original file line numberDiff line numberDiff line change
@@ -2433,3 +2433,5 @@ New_%0_database=
24332433
New_%0_database_created.=
24342434

24352435
No_entry_found_for_IBSN_%0_at_www.ebook.de=
2436+
2437+
Always_reformat_.bib_file_on_save_and_export=

src/main/resources/l10n/JabRef_fr.properties

+2
Original file line numberDiff line numberDiff line change
@@ -1686,3 +1686,5 @@ New_%0_database=
16861686
New_%0_database_created.=
16871687

16881688
No_entry_found_for_IBSN_%0_at_www.ebook.de=
1689+
1690+
Always_reformat_.bib_file_on_save_and_export=

src/main/resources/l10n/JabRef_in.properties

+2
Original file line numberDiff line numberDiff line change
@@ -1667,3 +1667,5 @@ New_%0_database=
16671667
New_%0_database_created.=
16681668

16691669
No_entry_found_for_IBSN_%0_at_www.ebook.de=
1670+
1671+
Always_reformat_.bib_file_on_save_and_export=

src/main/resources/l10n/JabRef_it.properties

+2
Original file line numberDiff line numberDiff line change
@@ -1765,3 +1765,5 @@ New_%0_database=
17651765
New_%0_database_created.=
17661766

17671767
No_entry_found_for_IBSN_%0_at_www.ebook.de=
1768+
1769+
Always_reformat_.bib_file_on_save_and_export=

src/main/resources/l10n/JabRef_ja.properties

+2
Original file line numberDiff line numberDiff line change
@@ -2438,3 +2438,5 @@ New_%0_database=
24382438
New_%0_database_created.=
24392439

24402440
No_entry_found_for_IBSN_%0_at_www.ebook.de=
2441+
2442+
Always_reformat_.bib_file_on_save_and_export=

src/main/resources/l10n/JabRef_nl.properties

+2
Original file line numberDiff line numberDiff line change
@@ -2440,3 +2440,5 @@ New_%0_database=
24402440
New_%0_database_created.=
24412441

24422442
No_entry_found_for_IBSN_%0_at_www.ebook.de=
2443+
2444+
Always_reformat_.bib_file_on_save_and_export=

src/main/resources/l10n/JabRef_no.properties

+2
Original file line numberDiff line numberDiff line change
@@ -2839,3 +2839,5 @@ New_%0_database=
28392839
New_%0_database_created.=
28402840

28412841
No_entry_found_for_IBSN_%0_at_www.ebook.de=
2842+
2843+
Always_reformat_.bib_file_on_save_and_export=

src/main/resources/l10n/JabRef_pt_BR.properties

+2
Original file line numberDiff line numberDiff line change
@@ -1659,3 +1659,5 @@ New_%0_database=
16591659
New_%0_database_created.=
16601660

16611661
No_entry_found_for_IBSN_%0_at_www.ebook.de=
1662+
1663+
Always_reformat_.bib_file_on_save_and_export=

src/main/resources/l10n/JabRef_ru.properties

+2
Original file line numberDiff line numberDiff line change
@@ -2439,3 +2439,5 @@ New_%0_database=
24392439
New_%0_database_created.=
24402440

24412441
No_entry_found_for_IBSN_%0_at_www.ebook.de=
2442+
2443+
Always_reformat_.bib_file_on_save_and_export=

src/main/resources/l10n/JabRef_tr.properties

+2
Original file line numberDiff line numberDiff line change
@@ -1680,3 +1680,5 @@ New_%0_database=
16801680
New_%0_database_created.=
16811681

16821682
No_entry_found_for_IBSN_%0_at_www.ebook.de=
1683+
1684+
Always_reformat_.bib_file_on_save_and_export=

src/main/resources/l10n/JabRef_vi.properties

+2
Original file line numberDiff line numberDiff line change
@@ -2437,3 +2437,5 @@ New_%0_database=
24372437
New_%0_database_created.=
24382438

24392439
No_entry_found_for_IBSN_%0_at_www.ebook.de=
2440+
2441+
Always_reformat_.bib_file_on_save_and_export=

src/main/resources/l10n/JabRef_zh.properties

+2
Original file line numberDiff line numberDiff line change
@@ -2434,3 +2434,5 @@ New_%0_database=
24342434
New_%0_database_created.=
24352435

24362436
No_entry_found_for_IBSN_%0_at_www.ebook.de=
2437+
2438+
Always_reformat_.bib_file_on_save_and_export=

0 commit comments

Comments
 (0)