Skip to content

Commit d7e9405

Browse files
authored
Merge pull request #387 from macvim-dev/feature/qlstephen-latest
Update QLStephen
2 parents 3b3d235 + e9a431e commit d7e9405

7 files changed

Lines changed: 75 additions & 2980 deletions

File tree

src/MacVim/qlstephen/GeneratePreviewForURL.m

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#import "QLSFileAttributes.h"
88

9+
#define DEFAULT_MAX_FILE_SIZE 1024 * 100
910

1011
// Generate a preview for the document with the given url
1112
OSStatus GeneratePreviewForURL(void *thisInterface,
@@ -40,6 +41,38 @@ OSStatus GeneratePreviewForURL(void *thisInterface,
4041
(NSString *)kQLPreviewPropertyHeightKey : @800
4142
};
4243

44+
// Get size of current File
45+
NSFileManager *man = [NSFileManager defaultManager];
46+
NSURL *file_url = (__bridge NSURL *)(url);
47+
NSDictionary *attrs = [man attributesOfItemAtPath: [file_url path] error: NULL];
48+
49+
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
50+
51+
// the plugin is running as com.apple.quicklook.satellite therefore we need to load our own settings
52+
NSDictionary *defaults = [userDefaults persistentDomainForName:@"com.whomwah.quicklookstephen"];
53+
54+
long long maxFileSizeSetting = [[defaults valueForKey:@"maxFileSize"] longLongValue];
55+
unsigned long long maxFileSize = DEFAULT_MAX_FILE_SIZE;
56+
if(maxFileSizeSetting > 0) {
57+
maxFileSize = maxFileSizeSetting;
58+
}
59+
60+
// Display less data, if file is too big
61+
if(attrs.fileSize > maxFileSize) {
62+
NSFileHandle *myFile= [NSFileHandle fileHandleForReadingAtPath:[file_url path]];
63+
if(!myFile) {
64+
return noErr;
65+
}
66+
NSData *displayData = [myFile readDataOfLength:maxFileSize];
67+
[myFile closeFile];
68+
69+
QLPreviewRequestSetDataRepresentation(
70+
request,
71+
(__bridge CFDataRef)displayData,
72+
kUTTypePlainText,
73+
(__bridge CFDictionaryRef)previewProperties);
74+
return noErr;
75+
}
4376
QLPreviewRequestSetURLRepresentation(
4477
request,
4578
url,

src/MacVim/qlstephen/GenerateThumbnailForURL.m

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
#include <QuickLook/QuickLook.h>
44

55
#import <Foundation/Foundation.h>
6-
#import "RegexKitLite.h"
76

87
#import "QLSFileAttributes.h"
98

10-
119
/**
1210
* This dictionary is used for a file with no extension. It maps the MIME type
1311
* (as returned by file(1)) onto an appropriate thumbnail badge.
@@ -72,10 +70,11 @@
7270
// Does the filename match a known pattern? If so, use the appropriate badge.
7371
if (!badge && [fileExtension isEqualToString:@""]) {
7472
NSDictionary *map = filenameRegexToBadgeMap();
75-
7673
[map enumerateKeysAndObjectsUsingBlock:
77-
^(NSString *regex, NSString *candidateBadge, BOOL *stop) {
78-
if ([fileName rkl_isMatchedByRegex:regex]) {
74+
^(NSString *pattern, NSString *candidateBadge, BOOL *stop) {
75+
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
76+
NSRange range = [regex rangeOfFirstMatchInString:fileName options:0 range:NSMakeRange(0, fileName.length)];
77+
if (range.location != NSNotFound) {
7978
badge = candidateBadge;
8079
*stop = true;
8180
}

src/MacVim/qlstephen/Info.plist

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
<key>CFBundleName</key>
3333
<string>QLStephen</string>
3434
<key>CFBundleShortVersionString</key>
35-
<string>1.4</string>
35+
<string>1.44</string>
3636
<key>CFBundleVersion</key>
37-
<string>1.4.2</string>
37+
<string>1.4.4</string>
3838
<key>CFPlugInDynamicRegisterFunction</key>
3939
<string></string>
4040
<key>CFPlugInDynamicRegistration</key>
@@ -54,7 +54,7 @@
5454
<key>CFPlugInUnloadFunction</key>
5555
<string></string>
5656
<key>NSHumanReadableCopyright</key>
57-
<string>Copyright © 2013 Duncan Robertson. All rights reserved.</string>
57+
<string>Copyright © 2016 Duncan Robertson. All rights reserved.</string>
5858
<key>QLNeedsToBeRunInMainThread</key>
5959
<true/>
6060
<key>QLPreviewHeight</key>

src/MacVim/qlstephen/QLSFileAttributes.m

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//
77

88
#import "QLSFileAttributes.h"
9-
#import "RegexKitLite.h"
109

1110
@interface QLSFileAttributes ()
1211

@@ -19,18 +18,22 @@ @interface QLSFileAttributes ()
1918

2019
@implementation QLSFileAttributes
2120

22-
+ (instancetype)attributesForItemAtURL:(NSURL *)aURL
23-
{
24-
NSString *magicString = [self magicStringForItemAtURL:aURL];
25-
if (!magicString) return nil;
21+
+ (instancetype)attributesForItemAtURL:(NSURL *)aURL {
22+
if ([aURL.lastPathComponent isEqualToString:@".DS_Store"]) {
23+
NSLog(@"Ignore the .DS_Store file.");
24+
return nil;
25+
}
2626

27-
NSArray *matches = [magicString rkl_captureComponentsMatchedByRegex:
28-
@"(\\S+/\\S+); charset=(\\S+)"];
27+
NSString *magicString = [self magicStringForItemAtURL:aURL usingLcALL:@"en_US.UTF-8"];
28+
if (!magicString) magicString = [self magicStringForItemAtURL:aURL usingLcALL:@"C"];
29+
if (!magicString) return nil;
30+
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\S+/\\S+); charset=(\\S+)" options:0 error:nil];
31+
NSTextCheckingResult *match = [regex firstMatchInString:magicString options:0 range:NSMakeRange(0, magicString.length)];
2932

30-
if (![matches count]) return nil;
33+
if (!match) return nil;
3134

32-
NSString *mimeType = matches[1];
33-
NSString *charset = matches[2];
35+
NSString *mimeType = [magicString substringWithRange:[match rangeAtIndex:1]];
36+
NSString *charset = [magicString substringWithRange:[match rangeAtIndex:2]];
3437

3538
BOOL mimeTypeIsTextual = [self mimeTypeIsTextual:mimeType];
3639

@@ -50,14 +53,13 @@ + (instancetype)attributesForItemAtURL:(NSURL *)aURL
5053
// Private Methods
5154
////////////////////////////////////////////////////////////////////////////////
5255

53-
+ (NSString *)magicStringForItemAtURL:(NSURL *)aURL
54-
{
56+
+ (NSString *)magicStringForItemAtURL:(NSURL *)aURL usingLcALL:(NSString *)lcALL {
5557
NSString *path = [aURL path];
5658
NSParameterAssert(path);
5759

5860
NSMutableDictionary *environment =
5961
[NSProcessInfo.processInfo.environment mutableCopy];
60-
environment[@"LC_ALL"] = @"en_US.UTF-8";
62+
environment[@"LC_ALL"] = lcALL;
6163

6264
NSTask *task = [NSTask new];
6365
task.launchPath = @"/usr/bin/file";
@@ -94,8 +96,7 @@ + (NSString *)magicStringForItemAtURL:(NSURL *)aURL
9496
* @return YES if mimeType contains "text", or if the mime type conforms to the
9597
* public.text UTI.
9698
*/
97-
+ (BOOL)mimeTypeIsTextual:(NSString *)mimeType
98-
{
99+
+ (BOOL)mimeTypeIsTextual:(NSString *)mimeType {
99100
NSArray *components = [mimeType componentsSeparatedByString:@"/"];
100101
if (components.count != 2)
101102
return NO;

src/MacVim/qlstephen/QuickLookStephen.xcodeproj/project.pbxproj

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
/* Begin PBXBuildFile section */
1010
0107ABFF15C76F2900C65F1A /* QLSFileAttributes.m in Sources */ = {isa = PBXBuildFile; fileRef = 0107ABFD15C76F2900C65F1A /* QLSFileAttributes.m */; };
11-
0149EB3B15F2E8E4003AB298 /* RegexKitLite.m in Sources */ = {isa = PBXBuildFile; fileRef = 0149EB3915F2E8E4003AB298 /* RegexKitLite.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
1211
0149EB3E15F2E9A3003AB298 /* libicucore.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 0149EB3D15F2E9A3003AB298 /* libicucore.dylib */; };
1312
2C05A19C06CAA52B00D84F6F /* GeneratePreviewForURL.m in Sources */ = {isa = PBXBuildFile; fileRef = 2C05A19B06CAA52B00D84F6F /* GeneratePreviewForURL.m */; };
1413
61E3BCFB0870B4F2002186A0 /* GenerateThumbnailForURL.m in Sources */ = {isa = PBXBuildFile; fileRef = 61E3BCFA0870B4F2002186A0 /* GenerateThumbnailForURL.m */; };
@@ -23,9 +22,7 @@
2322

2423
/* Begin PBXFileReference section */
2524
0107ABFC15C76F2900C65F1A /* QLSFileAttributes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QLSFileAttributes.h; sourceTree = "<group>"; };
26-
0107ABFD15C76F2900C65F1A /* QLSFileAttributes.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QLSFileAttributes.m; sourceTree = "<group>"; };
27-
0149EB3815F2E8E4003AB298 /* RegexKitLite.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RegexKitLite.h; sourceTree = "<group>"; };
28-
0149EB3915F2E8E4003AB298 /* RegexKitLite.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RegexKitLite.m; sourceTree = "<group>"; };
25+
0107ABFD15C76F2900C65F1A /* QLSFileAttributes.m */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.c.objc; path = QLSFileAttributes.m; sourceTree = "<group>"; tabWidth = 2; usesTabs = 0; };
2926
0149EB3D15F2E9A3003AB298 /* libicucore.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libicucore.dylib; path = usr/lib/libicucore.dylib; sourceTree = SDKROOT; };
3027
089C167EFE841241C02AAC07 /* English */ = {isa = PBXFileReference; fileEncoding = 10; lastKnownFileType = text.plist.strings; name = English; path = English.lproj/InfoPlist.strings; sourceTree = "<group>"; };
3128
08FB77B6FE84183AC02AAC07 /* main.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = "<group>"; };
@@ -57,19 +54,9 @@
5754
/* End PBXFrameworksBuildPhase section */
5855

5956
/* Begin PBXGroup section */
60-
0149EB3F15F2EC37003AB298 /* Third-Party */ = {
61-
isa = PBXGroup;
62-
children = (
63-
0149EB3815F2E8E4003AB298 /* RegexKitLite.h */,
64-
0149EB3915F2E8E4003AB298 /* RegexKitLite.m */,
65-
);
66-
name = "Third-Party";
67-
sourceTree = "<group>";
68-
};
6957
089C166AFE841209C02AAC07 /* QuickLookStephen */ = {
7058
isa = PBXGroup;
7159
children = (
72-
0149EB3F15F2EC37003AB298 /* Third-Party */,
7360
08FB77AFFE84173DC02AAC07 /* Source */,
7461
089C167CFE841241C02AAC07 /* Resources */,
7562
089C1671FE841209C02AAC07 /* External Frameworks and Libraries */,
@@ -130,6 +117,7 @@
130117
8D57630F048677EA00EA77CD /* Resources */,
131118
8D576311048677EA00EA77CD /* Sources */,
132119
8D576313048677EA00EA77CD /* Frameworks */,
120+
698210201CC62DD000504BBF /* ShellScript */,
133121
);
134122
buildRules = (
135123
);
@@ -146,7 +134,7 @@
146134
089C1669FE841209C02AAC07 /* Project object */ = {
147135
isa = PBXProject;
148136
attributes = {
149-
LastUpgradeCheck = 0720;
137+
LastUpgradeCheck = 0730;
150138
};
151139
buildConfigurationList = 2CA326220896AD4900168862 /* Build configuration list for PBXProject "QuickLookStephen" */;
152140
compatibilityVersion = "Xcode 3.2";
@@ -175,6 +163,22 @@
175163
};
176164
/* End PBXResourcesBuildPhase section */
177165

166+
/* Begin PBXShellScriptBuildPhase section */
167+
698210201CC62DD000504BBF /* ShellScript */ = {
168+
isa = PBXShellScriptBuildPhase;
169+
buildActionMask = 2147483647;
170+
files = (
171+
);
172+
inputPaths = (
173+
);
174+
outputPaths = (
175+
);
176+
runOnlyForDeploymentPostprocessing = 0;
177+
shellPath = /bin/sh;
178+
shellScript = "qlmanage -r";
179+
};
180+
/* End PBXShellScriptBuildPhase section */
181+
178182
/* Begin PBXSourcesBuildPhase section */
179183
8D576311048677EA00EA77CD /* Sources */ = {
180184
isa = PBXSourcesBuildPhase;
@@ -184,7 +188,6 @@
184188
2C05A19C06CAA52B00D84F6F /* GeneratePreviewForURL.m in Sources */,
185189
61E3BCFB0870B4F2002186A0 /* GenerateThumbnailForURL.m in Sources */,
186190
0107ABFF15C76F2900C65F1A /* QLSFileAttributes.m in Sources */,
187-
0149EB3B15F2E8E4003AB298 /* RegexKitLite.m in Sources */,
188191
);
189192
runOnlyForDeploymentPostprocessing = 0;
190193
};

0 commit comments

Comments
 (0)