Skip to content

Commit

Permalink
Make format script command object-first, make file NSObject category
Browse files Browse the repository at this point in the history
  • Loading branch information
hofman committed Jan 14, 2022
1 parent 3b6192e commit 3beb184
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 32 deletions.
7 changes: 0 additions & 7 deletions NSNumber_SKExtensions.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,3 @@
- (NSString *)greekCounterValue;

@end

@interface NSObject (SKExtensions)

- (NSUInteger)countOfTexLines;
- (id)objectInTexLinesAtIndex:(NSUInteger)idx;

@end
12 changes: 0 additions & 12 deletions NSNumber_SKExtensions.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,3 @@ - (NSAppleEventDescriptor *)scriptingTexLineDescriptor {
}

@end

@implementation NSObject (SKExtensions)

- (NSUInteger)countOfTexLines {
return INT_MAX;
}

- (id)objectInTexLinesAtIndex:(NSUInteger)idx {
return [NSNumber numberWithUnsignedInteger:idx];
}

@end
11 changes: 9 additions & 2 deletions SKFormatCommand.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// SKFormatCommand.h
// NSObject_SKExtensions.h
// Skim
//
// Created by Christiaan Hofman on 8/19/09.
Expand Down Expand Up @@ -39,5 +39,12 @@
#import <Cocoa/Cocoa.h>


@interface SKFormatCommand : NSScriptCommand

@interface NSObject (SKExtensions)

- (NSUInteger)countOfTexLines;
- (id)objectInTexLinesAtIndex:(NSUInteger)idx;

- (id)handleFormatScriptCommand:(NSScriptCommand *)command;

@end
25 changes: 16 additions & 9 deletions SKFormatCommand.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// SKFormatCommand.m
// NSObject_SKExtensions.m
// Skim
//
// Created by Christiaan Hofman on 8/19/09.
Expand Down Expand Up @@ -42,11 +42,18 @@ This software is Copyright (c) 2009-2022
#import "NSAttributedString_SKExtensions.h"


@implementation SKFormatCommand
@implementation NSObject (SKExtensions)

- (id)performDefaultImplementation {
id receiver = [self evaluatedReceivers];
NSDictionary *args = [self evaluatedArguments];
- (NSUInteger)countOfTexLines {
return INT_MAX;
}

- (id)objectInTexLinesAtIndex:(NSUInteger)idx {
return [NSNumber numberWithUnsignedInteger:idx];
}

- (id)handleFormatScriptCommand:(NSScriptCommand *)comand {
NSDictionary *args = [comand evaluatedArguments];
id template = [args objectForKey:@"Template"];
id file = [args objectForKey:@"File"];
NSAttributedString *attrString = nil;
Expand All @@ -55,24 +62,24 @@ - (id)performDefaultImplementation {
id text = nil;

if (template == nil)
[self setScriptErrorNumber:NSRequiredArgumentsMissingScriptError];
[comand setScriptErrorNumber:NSRequiredArgumentsMissingScriptError];
else if ([template isKindOfClass:[NSString class]])
string = template;
else if ([template isKindOfClass:[NSAttributedString class]])
attrString = template;
else if ([template isKindOfClass:[NSURL class]] == NO)
[self setScriptErrorNumber:NSArgumentsWrongScriptError];
[comand setScriptErrorNumber:NSArgumentsWrongScriptError];
else if ([[SKTemplateManager sharedManager] isRichTextTemplateType:[template path]])
attrString = [[[NSAttributedString alloc] initWithURL:template options:[NSDictionary dictionary] documentAttributes:&docAttrs error:NULL] autorelease];
else
string = [NSString stringWithContentsOfURL:template encoding:NSUTF8StringEncoding error:NULL];

if (string) {
text = [SKTemplateParser stringByParsingTemplateString:string usingObject:receiver];
text = [SKTemplateParser stringByParsingTemplateString:string usingObject:self];
if (file)
[text writeToURL:file atomically:YES encoding:NSUTF8StringEncoding error:NULL];
} else if (attrString) {
NSAttributedString *attrText = [SKTemplateParser attributedStringByParsingTemplateAttributedString:attrString usingObject:receiver];
NSAttributedString *attrText = [SKTemplateParser attributedStringByParsingTemplateAttributedString:attrString usingObject:self];
if (attrText) {
text = [attrText richTextSpecifier];
if (file) {
Expand Down
3 changes: 1 addition & 2 deletions Skim.sdef
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,6 @@

<command name="format" code="SKIMFrmt"
description="Format an object using a template.">
<cocoa class="SKFormatCommand"/>
<direct-parameter
description="The object to format using a template.">
<type type="specifier"/>
Expand Down Expand Up @@ -602,7 +601,7 @@
<cocoa key="texLines"/>
</element>
<responds-to name="format">
<cocoa method=""/>
<cocoa method="handleFormatScriptCommand:"/>
</responds-to>
</class-extension>

Expand Down

0 comments on commit 3beb184

Please sign in to comment.