-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSCollection_utils.m
193 lines (150 loc) · 6.53 KB
/
NSCollection_utils.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//
// NSCollection_utils.m
// Notation
//
// Created by Zachary Schneirov on 1/13/06.
// Copyright 2006 Zachary Schneirov. All rights reserved.
//
#import "NSCollection_utils.h"
#import "AttributedPlainText.h"
#import "NSString_NV.h"
#import "NoteObject.h"
#import "BufferUtils.h"
@implementation NSDictionary (FontTraits)
- (BOOL)attributesHaveFontTrait:(NSFontTraitMask)desiredTrait orAttribute:(NSString*)attrName {
if ([self objectForKey:attrName])
return YES;
NSFont *font = [self objectForKey:NSFontAttributeName];
if (font) {
NSFontTraitMask traits = [[NSFontManager sharedFontManager] traitsOfFont:font];
return traits & desiredTrait;
}
return NO;
}
@end
@implementation NSMutableDictionary (FontTraits)
- (void)addDesiredAttributesFromDictionary:(NSDictionary*)dict {
id underlineStyle = [dict objectForKey:NSUnderlineStyleAttributeName];
id strokeWidthStyle = [dict objectForKey:NSStrokeWidthAttributeName];
id obliquenessStyle = [dict objectForKey:NSObliquenessAttributeName];
id linkStyle = [dict objectForKey:NSLinkAttributeName];
if (linkStyle)
[self setObject:linkStyle forKey:NSLinkAttributeName];
if (underlineStyle)
[self setObject:underlineStyle forKey:NSUnderlineStyleAttributeName];
if (strokeWidthStyle)
[self setObject:strokeWidthStyle forKey:NSStrokeWidthAttributeName];
if (obliquenessStyle)
[self setObject:obliquenessStyle forKey:NSObliquenessAttributeName];
}
- (void)applyStyleInverted:(BOOL)opposite trait:(NSFontTraitMask)trait forFont:(NSFont*)font
alternateAttributeName:(NSString*)attrName alternateAttributeValue:(id)value {
NSFontManager *fontMan = [NSFontManager sharedFontManager];
if (opposite) {
font = [fontMan convertFont:font toNotHaveTrait:trait];
[self removeObjectForKey:attrName];
} else {
font = [fontMan convertFont:font toHaveTrait:trait];
NSFontTraitMask newTraits = [fontMan traitsOfFont:font];
if (!(newTraits & trait)) {
[self setObject:value forKey:attrName];
} else {
[self removeObjectForKey:attrName];
}
}
[self setObject:font forKey:NSFontAttributeName];
}
@end
@implementation NSSet (Utilities)
- (NSMutableSet*)setIntersectedWithSet:(NSSet*)set {
NSMutableSet *existingItems = [NSMutableSet setWithSet:self];
[existingItems intersectSet:set];
return existingItems;
}
@end
@implementation NSArray (NoteUtilities)
- (unsigned int)indexOfNoteWithUUIDBytes:(CFUUIDBytes*)bytes {
unsigned int i;
for (i=0; i<[self count]; i++) {
NoteObject *note = [self objectAtIndex:i];
CFUUIDBytes *noteBytes = [note uniqueNoteIDBytes];
if (!memcmp(noteBytes, bytes, sizeof(CFUUIDBytes)))
return i;
}
return NSNotFound;
}
#if 0
- (NSRange)nextRangeForString:(NSString*)string activeNote:(NoteObject*)startNote options:(unsigned)opts range:(NSRange)inRange {
unsigned noteCount = [self count];
NSRange range = NSMakeRange(NSNotFound, 0);
if (count > 0) {
unsigned noteIndex, startIndex = [self indexOfObjectIdenticalTo:startNote];
BOOL reversed = opts | NSBackwardsSearch;
if (startIndex == NSNotFound) startIndex = reversed ? count - 1 : 0;
noteIndex = startIndex;
unsigned quoteIndex = [string rangeOfString:@"\"" options:NSLiteralSearch].location;
NSArray *words = [string componentsSeparatedByString:quoteIndex == NSNotFound ? @" " : @"\""];
do {
NSRange range = [[self objectAtIndex:noteIndex] nextRangeForWords:words options:opts range:inRange];
noteIndex = noteIndex + reversed ? -1 : 1;
} while (range.location == NSNotFound && (reversed ? noteIndex > 0 : noteIndex < count - 1));
}
return range;
}
#endif
- (void)addMenuItemsForURLsInNotes:(NSMenu*)urlsMenu {
//iterate over notes in array
//accumulate links as NSMenuItems, with separators between them and disabled items being names of notes
unsigned int i;
//while ([urlsMenu numberOfItems]) {
// [urlsMenu removeItemAtIndex:0];
//}
// NSMenu *urlsMenu = [[NSMenu alloc] initWithTitle:@"URLs Menu"];
NSDictionary *blackAttrs = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont menuFontOfSize:13.0f], NSFontAttributeName, nil];
NSDictionary *grayAttrs = [NSDictionary dictionaryWithObjectsAndKeys:[NSColor grayColor], NSForegroundColorAttributeName,
[NSFont menuFontOfSize:13.0f], NSFontAttributeName, nil];
BOOL didAddInitialSeparator = NO;
for (i = 0; i<[self count]; i++) {
NoteObject *aNote = [self objectAtIndex:i];
NSArray *urls = [[aNote contentString] allLinks];
if ([urls count] > 0) {
if (!didAddInitialSeparator) {
[urlsMenu addItem:[NSMenuItem separatorItem]];
didAddInitialSeparator = YES;
}
unsigned int j;
for (j=0; j<[urls count]; j++) {
NSURL *url = [urls objectAtIndex:j];
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Copy URL",@"contextual menu item title to copy urls")
action:@selector(copyItemToPasteboard:) keyEquivalent:@""];
NSString *urlString = [url absoluteString];
if ([urlString length] > 60) urlString = [[urlString substringToIndex: 60] stringByAppendingString:NSLocalizedString(@"...", @"ellipsis character")];
NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:[NSLocalizedString(@"Copy ",@"menu item prefix to copy a URL") stringByAppendingString:urlString] attributes:blackAttrs];
NSAttributedString *titleDesc = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@" (%@)", titleOfNote(aNote)] attributes:grayAttrs];
[titleString appendAttributedString:titleDesc];
[item setAttributedTitle:titleString];
[titleDesc release];
[titleString release];
[item setRepresentedObject:[url absoluteString]];
[item setTarget:[item representedObject]];
[urlsMenu addItem:item];
}
}
}
// if (![urlsMenu numberOfItems])
// [urlsMenu addItemWithTitle:@"No URLs Found" action:NULL keyEquivalent:@""];
// return [urlsMenu autorelease];
}
@end
@implementation NSMutableArray (Sorting)
- (void)sortUnstableUsingFunction:(int (*)(id *, id *))compare {
[self sortUsingFunction:(int (*)(id, id, void *))genericSortContextLast context:compare];
}
- (void)sortStableUsingFunction:(int (*)(id *, id *))compare usingBuffer:(id **)buffer ofSize:(unsigned int*)bufSize {
unsigned int count = CFArrayGetCount((CFArrayRef)self);
ResizeBuffer((void***)buffer, count, bufSize);
CFArrayGetValues((CFArrayRef)self, CFRangeMake(0, [self count]), (const void **)*buffer);
mergesort((void *)*buffer, (size_t)count, sizeof(id), (int (*)(const void *, const void *))compare);
CFArrayReplaceValues((CFMutableArrayRef)self, CFRangeMake(0, count), (const void **)*buffer, count);
}
@end