Add "Unicode (UTF-8) with BOM" to encoding selection (#509)

This commit is contained in:
1024jp 2016-04-09 16:44:45 +09:00
parent d68e19aaa5
commit 60ac88645c
7 changed files with 47 additions and 26 deletions

View File

@ -7,7 +7,10 @@ develop
### New Features
- Respect the UTF-8 BOM's existance in the opened file.
- Add independent “Unicode (UTF-8) with BOM” encoding to encoding list.
- Respect the existance of the UTF-8 BOM in opened files.
- Enable switching the document encoding between with and without BOM from the toolbar popup button and the “Format” menu.
- The “Unicode (UTF-8) with BOM” item will be automatically added to just after the normal “Unicode (UTF-8)”.
- Now, the execute permission can be given to the file to save from the save panel.
- Add a new theme “Lakritz”.

View File

@ -285,7 +285,7 @@
NSString *actionName = [NSString stringWithFormat:NSLocalizedString(@"Encoding to “%@”", nil),
[NSString localizedNameOfStringEncoding:encoding]];
BOOL success = [self doSetEncoding:encoding updateDocument:YES askLossy:NO lossy:lossy asActionName:actionName];
BOOL success = [self doSetEncoding:encoding withUTF8BOM:NO updateDocument:YES askLossy:NO lossy:lossy asActionName:actionName];
return @(success);
}

View File

@ -72,7 +72,7 @@ extern NSString *_Nonnull const CEIncompatibleConvertedCharKey;
- (nullable NSString *)IANACharSetName;
- (nullable NSArray<NSDictionary<NSString *, id> *> *)findCharsIncompatibleWithEncoding:(NSStringEncoding)encoding;
- (BOOL)reinterpretWithEncoding:(NSStringEncoding)encoding error:(NSError * _Nullable __autoreleasing * _Nullable)outError;
- (BOOL)doSetEncoding:(NSStringEncoding)encoding updateDocument:(BOOL)updateDocument askLossy:(BOOL)askLossy lossy:(BOOL)lossy asActionName:(nullable NSString *)actionName;
- (BOOL)doSetEncoding:(NSStringEncoding)encoding withUTF8BOM:(BOOL)withUTF8BOM updateDocument:(BOOL)updateDocument askLossy:(BOOL)askLossy lossy:(BOOL)lossy asActionName:(nullable NSString *)actionName;
// line ending
- (void)doSetLineEnding:(CENewLineType)lineEnding;

View File

@ -622,7 +622,7 @@ NSString *_Nonnull const CEIncompatibleConvertedCharKey = @"convertedChar";
[document setSyntaxStyleWithName:[[self syntaxStyle] styleName]];
[document doSetLineEnding:[self lineEnding]];
[document doSetEncoding:[self encoding] updateDocument:NO askLossy:NO lossy:NO asActionName:nil];
[document doSetEncoding:[self encoding] withUTF8BOM:[self hasUTF8BOM] updateDocument:NO askLossy:NO lossy:NO asActionName:nil];
// apply text orientation
CEEditorWrapper *editor = [self editor];
@ -720,10 +720,8 @@ NSString *_Nonnull const CEIncompatibleConvertedCharKey = @"convertedChar";
return ([self isWritable] || [self didAlertNotWritable]);
} else if ([menuItem action] == @selector(changeEncoding:)) {
state = ([menuItem tag] == [self encoding]) ? NSOnState : NSOffState;
if ([menuItem tag] == NSUTF8StringEncoding && [self encoding] == NSUTF8StringEncoding) {
[menuItem setTitle:[NSString localizedNameOfStringEncoding:[self encoding] withUTF8BOM:[self hasUTF8BOM]]];
}
NSInteger encodingTag = [self hasUTF8BOM] ? -[self encoding] : [self encoding];
state = ([menuItem tag] == encodingTag) ? NSOnState : NSOffState;
} else if (([menuItem action] == @selector(changeLineEndingToLF:)) ||
([menuItem action] == @selector(changeLineEndingToCR:)) ||
@ -999,10 +997,10 @@ NSString *_Nonnull const CEIncompatibleConvertedCharKey = @"convertedChar";
// ------------------------------------------------------
///
- (BOOL)doSetEncoding:(NSStringEncoding)encoding updateDocument:(BOOL)updateDocument askLossy:(BOOL)askLossy lossy:(BOOL)lossy asActionName:(nullable NSString *)actionName
- (BOOL)doSetEncoding:(NSStringEncoding)encoding withUTF8BOM:(BOOL)withUTF8BOM updateDocument:(BOOL)updateDocument askLossy:(BOOL)askLossy lossy:(BOOL)lossy asActionName:(nullable NSString *)actionName
// ------------------------------------------------------
{
if (encoding == [self encoding]) {
if (encoding == [self encoding] && withUTF8BOM == [self hasUTF8BOM]) {
return YES;
}
@ -1034,7 +1032,8 @@ NSString *_Nonnull const CEIncompatibleConvertedCharKey = @"convertedChar";
// register undo
NSUndoManager *undoManager = [self undoManager];
[[undoManager prepareWithInvocationTarget:self] redoSetEncoding:encoding updateDocument:updateDocument
[[undoManager prepareWithInvocationTarget:self] redoSetEncoding:encoding withUTF8BOM:withUTF8BOM
updateDocument:updateDocument
askLossy:NO lossy:allowsLossy
asActionName:actionName]; // redo in undo
if (shouldShowList) {
@ -1042,12 +1041,15 @@ NSString *_Nonnull const CEIncompatibleConvertedCharKey = @"convertedChar";
}
[[undoManager prepareWithInvocationTarget:self] updateEncodingInToolbarAndInfo];
[[undoManager prepareWithInvocationTarget:self] setEncoding:[self encoding]]; //
[[undoManager prepareWithInvocationTarget:self] setHasUTF8BOM:[self hasUTF8BOM]]; //
if (actionName) {
[undoManager setActionName:actionName];
}
}
[self setEncoding:encoding];
[self setHasUTF8BOM:withUTF8BOM];
[self updateEncodingInToolbarAndInfo]; //
if (shouldShowList) {
@ -1216,15 +1218,19 @@ NSString *_Nonnull const CEIncompatibleConvertedCharKey = @"convertedChar";
- (IBAction)changeEncoding:(nullable id)sender
// ------------------------------------------------------
{
NSStringEncoding encoding = [sender tag];
NSStringEncoding encoding = labs([sender tag]);
BOOL withUTF8BOM = ([sender tag] == -NSUTF8StringEncoding);
if ((encoding < 1) || (encoding == [self encoding])) { return; }
if ((encoding == [self encoding]) && (withUTF8BOM == [self hasUTF8BOM])) { return; }
NSInteger result;
NSString *encodingName = [sender title];
//
if (([[[self editor] string] length] < 1) || ![self fileURL]) {
if (([[[self editor] string] length] < 1) || // no content yet
![self fileURL] || // not yet saved
(encoding == NSUTF8StringEncoding && encoding == [self encoding])) // toggle only BOM existance
{
result = NSAlertFirstButtonReturn;
} else {
@ -1241,9 +1247,9 @@ NSString *_Nonnull const CEIncompatibleConvertedCharKey = @"convertedChar";
if (result == NSAlertFirstButtonReturn) { // = Convert
NSString *actionName = [NSString stringWithFormat:NSLocalizedString(@"Encoding to “%@”", nil),
[NSString localizedNameOfStringEncoding:encoding]];
[NSString localizedNameOfStringEncoding:encoding withUTF8BOM:withUTF8BOM]];
[self doSetEncoding:encoding updateDocument:YES askLossy:YES lossy:NO asActionName:actionName];
[self doSetEncoding:encoding withUTF8BOM:withUTF8BOM updateDocument:YES askLossy:YES lossy:NO asActionName:actionName];
} else if (result == NSAlertSecondButtonReturn) { // = Reinterpret
if (![self fileURL]) { return; } //
@ -1257,7 +1263,7 @@ NSString *_Nonnull const CEIncompatibleConvertedCharKey = @"convertedChar";
NSInteger secondResult = [alert runModal];
if (secondResult != NSAlertSecondButtonReturn) { // = Cancel
//
[[[self windowController] toolbarController] setSelectedEncoding:[self encoding]];
[[[self windowController] toolbarController] setSelectedEncoding:[self encoding] withUTF8BOM:[self hasUTF8BOM]];
return;
}
}
@ -1275,7 +1281,7 @@ NSString *_Nonnull const CEIncompatibleConvertedCharKey = @"convertedChar";
}
//
[[[self windowController] toolbarController] setSelectedEncoding:[self encoding]];
[[[self windowController] toolbarController] setSelectedEncoding:[self encoding] withUTF8BOM:[self hasUTF8BOM]];
}
@ -1354,7 +1360,7 @@ NSString *_Nonnull const CEIncompatibleConvertedCharKey = @"convertedChar";
// ------------------------------------------------------
{
//
[[[self windowController] toolbarController] setSelectedEncoding:[self encoding]];
[[[self windowController] toolbarController] setSelectedEncoding:[self encoding] withUTF8BOM:[self hasUTF8BOM]];
//
[[self windowController] updateModeInfoIfNeeded];
@ -1561,10 +1567,10 @@ NSString *_Nonnull const CEIncompatibleConvertedCharKey = @"convertedChar";
// ------------------------------------------------------
/// Redo
- (void)redoSetEncoding:(NSStringEncoding)encoding updateDocument:(BOOL)updateDocument askLossy:(BOOL)askLossy lossy:(BOOL)lossy asActionName:(nullable NSString *)actionName
- (void)redoSetEncoding:(NSStringEncoding)encoding withUTF8BOM:(BOOL)withUTF8BOM updateDocument:(BOOL)updateDocument askLossy:(BOOL)askLossy lossy:(BOOL)lossy asActionName:(nullable NSString *)actionName
// ------------------------------------------------------
{
[[[self undoManager] prepareWithInvocationTarget:self] doSetEncoding:encoding updateDocument:updateDocument
[[[self undoManager] prepareWithInvocationTarget:self] doSetEncoding:encoding withUTF8BOM:withUTF8BOM updateDocument:updateDocument
askLossy:askLossy lossy:lossy asActionName:actionName];
}

View File

@ -30,6 +30,8 @@
#import "CEDefaults.h"
#import "CEEncodings.h"
#import "NSString+CEEncoding.h"
NSString *_Nonnull const CEEncodingListDidUpdateNotification = @"CESyntaxListDidUpdateNotification";
@ -172,6 +174,15 @@ NSString *_Nonnull const CEEncodingListDidUpdateNotification = @"CESyntaxListDid
[item setAction:@selector(changeEncoding:)];
[item setTarget:nil];
[menu addItem:item];
// add "UTF-8 with BOM" item just after the normal UTF-8
if ([item tag] == NSUTF8StringEncoding) {
NSMenuItem *bomItem = [[NSMenuItem alloc] initWithTitle:[NSString localizedNameOfUTF8EncodingWithBOM]
action:@selector(changeEncoding:)
keyEquivalent:@""];
[bomItem setTag:-NSUTF8StringEncoding]; // negative value is sign for "with BOM"
[menu addItem:bomItem];
}
}
}

View File

@ -63,7 +63,7 @@ typedef NS_ENUM(NSInteger, CEToolbarItemTag) {
// Public method
- (void)toggleItemWithTag:(CEToolbarItemTag)tag setOn:(BOOL)setOn;
- (void)setSelectedEncoding:(NSStringEncoding)encoding;
- (void)setSelectedEncoding:(NSStringEncoding)encoding withUTF8BOM:(BOOL)withUTF8BOM;
- (void)setSelectedLineEnding:(CENewLineType)lineEnding;
- (void)setSelectedSyntaxWithName:(nonnull NSString *)name;

View File

@ -111,10 +111,11 @@
// ------------------------------------------------------
/// select item in the encoding popup menu
- (void)setSelectedEncoding:(NSStringEncoding)encoding
- (void)setSelectedEncoding:(NSStringEncoding)encoding withUTF8BOM:(BOOL)withUTF8BOM
// ------------------------------------------------------
{
[[self encodingPopupButton] selectItemWithTag:encoding];
NSInteger tag = withUTF8BOM ? -encoding : encoding;
[[self encodingPopupButton] selectItemWithTag:tag];
}
@ -242,12 +243,12 @@
// ------------------------------------------------------
{
// store current selection
NSStringEncoding encoding = [[self encodingPopupButton] selectedTag];
NSInteger tag = [[self encodingPopupButton] selectedTag];
[[CEEncodingManager sharedManager] updateChangeEncodingMenu:[[self encodingPopupButton] menu]];
// reapply to the menu
[[self encodingPopupButton] selectItemWithTag:encoding];
[[self encodingPopupButton] selectItemWithTag:tag];
}