forked from halirutan/SE-Editor-Buttons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathm_toolbar.user.js
More file actions
811 lines (717 loc) · 34.1 KB
/
m_toolbar.user.js
File metadata and controls
811 lines (717 loc) · 34.1 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
// ==UserScript==
// @name Mathematica Toolbar Buttons
// @author Nathan Osman, Patrick Scheibe
// @version 1.4.5
// @downloadURL https://github.com/halirutan/SE-Editor-Buttons/raw/master/src/m_toolbar.user.js
// @namespace http://halirutan.de
// @description Adds some buttons to the editing toolbar to make it easy to insert links, unicode glyphs and shortcut keys.
// @include https://mathematica.stackexchange.com/*
// @include https://mathematica.meta.stackexchange.com/*
// @include https://math.stackexchange.com/*
// ==/UserScript==
// This method injects code into the current page and executes it after the dependant scripts are loaded
function EmbedCodeOnPage(code) {
var code_element = document.createElement('script');
code_element.type = 'text/javascript';
code_element.textContent = '(' + code + ')()';
document.getElementsByTagName('head')[0].appendChild(code_element);
}
// This method call wraps the entire contents of the UserScript
EmbedCodeOnPage(function () {
// Adds an array of buttons to the toolbar, being careful not to
// stomp on any toes (clobber any existing buttons that are there)
// The 'buttons' array contains one or more items that look like this:
// { 'icon': url, 'tooltip': 'text to display' }
// Each item must also contain either an array named 'menu' (to display a menu)
// that contains items like: { 'text': 'item text', 'callback': function() { ... } }
// or include 'callback' (a function to be called when the button is clicked)
function AddToolbarButtons(toolbar, buttons) {
// First, get the x-offset of the rightmost button in the toolbar
var left = toolbar.find('li:not(.wmd-help-button):last').css('left');
// Either we received the offset of the last button or the buttons haven't
// been added to the toolbar yet, in which case we stick the spacer at offset 375
if (left === null)
left = 400;
else
left = parseInt(left.replace(/\D/g, '')) + 25;
// Begin by adding a spacer at the calculated position
//toolbar.append('<li class="wmd-spacer wmd-spacer-max" style="left: ' + left + 'px;"></li>');
toolbar.append('<li class="wmd-spacer wmd-spacer-max"></li>');
// Now add each of the buttons
for (var i = 0; i < buttons.length; ++i) {
var button = $('<li class="wmd-button" style="background-image: url(' + buttons[i]['icon'] +
'); background-position: center; background-repeat: no-repeat; left: ' + (left += 25) +
'px;" title="' + buttons[i]['tooltip'].replace('"', '"') + '"></li>');
if (typeof buttons[i]['callback'] == 'undefined') {
// Create the list and populate it
var ul = $('<ul style="background-color: #eee; border: 1px solid #aaa; display: none; list-style-type: none; margin: 0; position: absolute; top: 20px; width: 140px; z-index: 1;"></ul>');
$.each(buttons[i]['menu'], function (key, item) {
var li = $('<li style="margin: 0; padding: 4px;">' + item['text'] + '</li>');
li.hover(function () {
$(this).css('background-color', '#ccc');
},
function () {
$(this).css('background-color', 'inherit');
});
li.click(item['callback']);
ul.append(li);
});
button.css('position', 'relative').append(ul);
button.hover(function () {
ul.stop(true, true).slideDown('fast');
},
function () {
ul.stop(true, true).slideUp('fast');
});
} else
button.click(buttons[i]['callback']);
toolbar.append(button);
}
}
// Returns the start and end point of the selection in the editor
function SelectionStartEnd(editor) {
return [editor[0].selectionStart, editor[0].selectionEnd].sort(function (a, b) { return a - b; });
}
// Inserts the specified text at the current cursor position in the editor, replacing
// the current selection (if any)
function InsertIntoEditor(editor, text) {
// Grab the current contents of the editor as well as the current selection
var old_val = editor.val();
var [s_start, s_end] = SelectionStartEnd(editor);
// Splice the contents and insert the new text
var new_val = old_val.substr(0, s_start) + text + old_val.substr(s_end);
editor.val(new_val);
// Set the focus to the editor and select the newly added text
editor[0].focus();
editor[0].selectionStart = s_start;
editor[0].selectionEnd = s_start + text.length;
// Refresh the preview
StackExchange.MarkdownEditor.refreshAllPreviews();
}
//=====================
// UserScript Source
//=====================
// We only want to run the following code on certain pages, so
// match the current page against this RegEx pattern
// halirutan: The buttons will now appear when you edit a post in the review-queue
if (location.pathname.match(/^\/(?:questions\/(?:ask|\d+)|review\/(?:close|first-posts|late-answers|low-quality-posts|reopen|suggested-edits)\/\d+|posts\/\d+\/edit|users\/edit|edit-tag-wiki)/) === null)
return;
// For each button row, inject our buttons into the toolbar
// https://stackoverflow.com/a/16726669
let observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (!mutation.addedNodes) return
for (let i = 0; i < mutation.addedNodes.length; i++) {
let node = mutation.addedNodes[i];
if (node.matches && node.matches('.wmd-button-row')) {
InjectButtons(node);
}
}
})
})
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: false,
characterData: false
});
function InjectButtons(elm){
var button_row = $(elm);
var editor = button_row.parent().parent().find('.wmd-input');
// Asks the user the specified question and inserts the answer into the editor with the specified markdown
function InsertMarkdown(question, markdown) {
var answer = prompt(question);
if (answer !== null)
InsertIntoEditor(editor, markdown.replace(/#INPUT#/g, answer));
}
// I simply copied the logic from InsertIntoEditor() to create a function which either asks for input
// or, if something is selected, uses the current selection as input. This is in my eyes a bit more usable
// then always using a input dialog.
function ReplaceMarkdown(question, markdown, preprocess) {
// Grab the current contents of the editor as well as the current selection
var old_val = editor.val();
var [s_start, s_end] = SelectionStartEnd(editor);
// shrink the selection if there are leading/trailing whitespaces
s_start += old_val.substr(s_start, s_end - s_start).match(/^\s*/)[0].length;
s_end -= old_val.substr(s_start, s_end - s_start).match(/\s*$/)[0].length;
// Check whether text was selected and prompt the user for input if not.
var answer;
if (s_start == s_end) {
answer = prompt(question);
} else {
answer = old_val.substr(s_start, s_end - s_start);
}
if (answer !== null && answer !== "") {
if (preprocess !== undefined) {
answer = preprocess(answer)
}
var text = markdown.replace(/#INPUT#/g, answer);
// Splice the contents and insert the new text
var new_val = old_val.substr(0, s_start) + text + old_val.substr(s_end);
editor.val(new_val);
// Set the focus to the editor and select the newly added text
editor[0].focus();
editor[0].selectionStart = s_start;
editor[0].selectionEnd = s_start + text.length;
// Refresh the preview
StackExchange.MarkdownEditor.refreshAllPreviews();
}
}
// These are the glyphs we want to replace inside the markdown editor
var rules = [
['\\\\\\[Aleph\\]', 'ℵ'],
['\\\\\\[Alpha\\]', 'α'],
['\\\\\\[And\\]', '∧'],
['\\\\\\[Angle\\]', '∠'],
['\\\\\\[Angstrom\\]', 'Å'],
['\\\\\\[AscendingEllipsis\\]', '⋰'],
['\\\\\\[Backslash\\]', '∖'],
['\\\\\\[BeamedEighthNote\\]', '♫'],
['\\\\\\[BeamedSixteenthNote\\]', '♬'],
['\\\\\\[Because\\]', '∵'],
['\\\\\\[Bet\\]', 'ℶ'],
['\\\\\\[Beta\\]', 'β'],
['\\\\\\[Bullet\\]', '•'],
['\\\\\\[CapitalAlpha\\]', 'Α'],
['\\\\\\[CapitalBeta\\]', 'Β'],
['\\\\\\[CapitalChi\\]', 'Χ'],
['\\\\\\[CapitalDelta\\]', 'Δ'],
['\\\\\\[CapitalDigamma\\]', 'Ϝ'],
['\\\\\\[CapitalEpsilon\\]', 'Ε'],
['\\\\\\[CapitalEta\\]', 'Η'],
['\\\\\\[CapitalGamma\\]', 'Γ'],
['\\\\\\[CapitalIota\\]', 'Ι'],
['\\\\\\[CapitalKappa\\]', 'Κ'],
['\\\\\\[CapitalKoppa\\]', 'Ϟ'],
['\\\\\\[CapitalLambda\\]', 'Λ'],
['\\\\\\[CapitalMu\\]', 'Μ'],
['\\\\\\[CapitalNu\\]', 'Ν'],
['\\\\\\[CapitalOmega\\]', 'Ω'],
['\\\\\\[CapitalOmicron\\]', 'Ο'],
['\\\\\\[CapitalPhi\\]', 'Φ'],
['\\\\\\[CapitalPi\\]', 'Π'],
['\\\\\\[CapitalPsi\\]', 'Ψ'],
['\\\\\\[CapitalRho\\]', 'Ρ'],
['\\\\\\[CapitalSampi\\]', 'Ϡ'],
['\\\\\\[CapitalSigma\\]', 'Σ'],
['\\\\\\[CapitalStigma\\]', 'Ϛ'],
['\\\\\\[CapitalTau\\]', 'Τ'],
['\\\\\\[CapitalTheta\\]', 'Θ'],
['\\\\\\[CapitalUpsilon\\]', 'Υ'],
['\\\\\\[CapitalXi\\]', 'Ξ'],
['\\\\\\[CapitalZeta\\]', 'Ζ'],
['\\\\\\[Cap\\]', '⌢'],
['\\\\\\[CenterDot\\]', '·'],
['\\\\\\[CenterEllipsis\\]', '⋯'],
['\\\\\\[Checkmark\\]', '��'],
['\\\\\\[Chi\\]', 'χ'],
['\\\\\\[CircleDot\\]', '⊙'],
['\\\\\\[CircleMinus\\]', '⊖'],
['\\\\\\[CirclePlus\\]', '⊕'],
['\\\\\\[CircleTimes\\]', '⊗'],
['\\\\\\[ClockwiseContourIntegral\\]', '∲'],
['\\\\\\[CloseCurlyDoubleQuote\\]', '”'],
['\\\\\\[CloseCurlyQuote\\]', '’'],
['\\\\\\[CloverLeaf\\]', '⌘'],
['\\\\\\[ClubSuit\\]', '♣'],
['\\\\\\[Colon\\]', '∶'],
['\\\\\\[Congruent\\]', '≡'],
['\\\\\\[ContourIntegral\\]', '∮'],
['\\\\\\[Coproduct\\]', '∐'],
['\\\\\\[CounterClockwiseContourIntegral\\]', '∳'],
['\\\\\\[CupCap\\]', '≍'],
['\\\\\\[Cup\\]', '⌣'],
['\\\\\\[CurlyCapitalUpsilon\\]', 'ϒ'],
['\\\\\\[CurlyEpsilon\\]', 'ε'],
['\\\\\\[CurlyKappa\\]', 'ϰ'],
['\\\\\\[CurlyPhi\\]', 'φ'],
['\\\\\\[CurlyPi\\]', 'ϖ'],
['\\\\\\[CurlyRho\\]', 'ϱ'],
['\\\\\\[CurlyTheta\\]', 'ϑ'],
['\\\\\\[Dagger\\]', '†'],
['\\\\\\[Dalet\\]', 'ℸ'],
['\\\\\\[Dash\\]', '–'],
['\\\\\\[Degree\\]', '°'],
['\\\\\\[Del\\]', '∇'],
['\\\\\\[Delta\\]', 'δ'],
['\\\\\\[DescendingEllipsis\\]', '⋱'],
['\\\\\\[Diameter\\]', '⌀'],
['\\\\\\[Diamond\\]', '⋄'],
['\\\\\\[DiamondSuit\\]', '♢'],
['\\\\\\[Digamma\\]', 'ϝ'],
['\\\\\\[Divide\\]', '÷'],
['\\\\\\[DotEqual\\]', '≐'],
['\\\\\\[DoubleContourIntegral\\]', '∯'],
['\\\\\\[DoubleDagger\\]', '‡'],
['\\\\\\[DoubleDownArrow\\]', '⇓'],
['\\\\\\[DoubleLeftArrow\\]', '⇐'],
['\\\\\\[DoubleLeftRightArrow\\]', '⇔'],
['\\\\\\[DoubleLeftTee\\]', '⫤'],
['\\\\\\[DoubleLongLeftArrow\\]', '⟸'],
['\\\\\\[DoubleLongLeftRightArrow\\]', '⟺'],
['\\\\\\[DoubleLongRightArrow\\]', '⟹'],
['\\\\\\[DoublePrime\\]', '″'],
['\\\\\\[DoubleRightArrow\\]', '⇒'],
['\\\\\\[DoubleRightTee\\]', '⊨'],
['\\\\\\[DoubleUpArrow\\]', '⇑'],
['\\\\\\[DoubleUpDownArrow\\]', '⇕'],
['\\\\\\[DoubleVerticalBar\\]', '∥'],
['\\\\\\[DownArrowBar\\]', '⤓'],
['\\\\\\[DownArrow\\]', '↓'],
['\\\\\\[DownArrowUpArrow\\]', '⇵'],
['\\\\\\[DownLeftRightVector\\]', '⥐'],
['\\\\\\[DownLeftTeeVector\\]', '⥞'],
['\\\\\\[DownLeftVector\\]', '↽'],
['\\\\\\[DownLeftVectorBar\\]', '⥖'],
['\\\\\\[DownPointer\\]', '▾'],
['\\\\\\[DownRightTeeVector\\]', '⥟'],
['\\\\\\[DownRightVector\\]', '⇁'],
['\\\\\\[DownRightVectorBar\\]', '⥗'],
['\\\\\\[DownTeeArrow\\]', '↧'],
['\\\\\\[DownTee\\]', '⊤'],
['\\\\\\[Earth\\]', '♁'],
['\\\\\\[EighthNote\\]', '♪'],
['\\\\\\[Element\\]', '∈'],
['\\\\\\[Ellipsis\\]', '…'],
['\\\\\\[EmptyCircle\\]', '○'],
['\\\\\\[EmptyDiamond\\]', '◇'],
['\\\\\\[EmptyDownTriangle\\]', '▽'],
['\\\\\\[EmptyRectangle\\]', '▯'],
['\\\\\\[EmptySet\\]', '∅'],
['\\\\\\[EmptySmallCircle\\]', '◦'],
['\\\\\\[EmptySmallSquare\\]', '◻'],
['\\\\\\[EmptySquare\\]', '��'],
['\\\\\\[EmptyUpTriangle\\]', '△'],
['\\\\\\[EmptyVerySmallSquare\\]', '▫'],
['\\\\\\[Epsilon\\]', 'ϵ'],
['\\\\\\[EqualTilde\\]', '≂'],
['\\\\\\[Equilibrium\\]', '⇌'],
['\\\\\\[Equivalent\\]', '⧦'],
['\\\\\\[Eta\\]', 'η'],
['\\\\\\[Euro\\]', '€'],
['\\\\\\[Exists\\]', '∃'],
['\\\\\\[FilledCircle\\]', '●'],
['\\\\\\[FilledDiamond\\]', '◆'],
['\\\\\\[FilledDownTriangle\\]', '▼'],
['\\\\\\[FilledLeftTriangle\\]', '◀'],
['\\\\\\[FilledRectangle\\]', '▮'],
['\\\\\\[FilledRightTriangle\\]', '▶'],
['\\\\\\[FilledSmallSquare\\]', '◼'],
['\\\\\\[FilledSquare\\]', '■'],
['\\\\\\[FilledUpTriangle\\]', '▲'],
['\\\\\\[FilledVerySmallSquare\\]', '▪'],
['\\\\\\[FinalSigma\\]', 'ς'],
['\\\\\\[FivePointedStar\\]', '★'],
['\\\\\\[Flat\\]', '♭'],
['\\\\\\[ForAll\\]', '∀'],
['\\\\\\[Gamma\\]', 'γ'],
['\\\\\\[Gimel\\]', 'ℷ'],
['\\\\\\[GothicCapitalC\\]', 'ℭ'],
['\\\\\\[GothicCapitalH\\]', 'ℌ'],
['\\\\\\[GothicCapitalI\\]', 'ℑ'],
['\\\\\\[GothicCapitalR\\]', 'ℜ'],
['\\\\\\[GothicCapitalZ\\]', 'ℨ'],
['\\\\\\[GreaterEqualLess\\]', '⋛'],
['\\\\\\[GreaterEqual\\]', '≥'],
['\\\\\\[GreaterFullEqual\\]', '≧'],
['\\\\\\[GreaterGreater\\]', '≫'],
['\\\\\\[GreaterLess\\]', '≷'],
['\\\\\\[GreaterSlantEqual\\]', '⩾'],
['\\\\\\[GreaterTilde\\]', '≳'],
['\\\\\\[HappySmiley\\]', '☺'],
['\\\\\\[HBar\\]', 'ℏ'],
['\\\\\\[HeartSuit\\]', '♡'],
['\\\\\\[HorizontalLine\\]', '─'],
['\\\\\\[HumpDownHump\\]', '≎'],
['\\\\\\[HumpEqual\\]', '≏'],
['\\\\\\[Hyphen\\]', '‐'],
['\\\\\\[Infinity\\]', '∞'],
['\\\\\\[Integral\\]', '∫'],
['\\\\\\[Intersection\\]', '⋂'],
['\\\\\\[Iota\\]', 'ι'],
['\\\\\\[Jupiter\\]', '♃'],
['\\\\\\[Kappa\\]', 'κ'],
['\\\\\\[Koppa\\]', 'ϟ'],
['\\\\\\[Lambda\\]', 'λ'],
['\\\\\\[LeftAngleBracket\\]', '〈'],
['\\\\\\[LeftArrowBar\\]', '⇤'],
['\\\\\\[LeftArrow\\]', '←'],
['\\\\\\[LeftArrowRightArrow\\]', '⇆'],
['\\\\\\[LeftCeiling\\]', '⌈'],
['\\\\\\[LeftDoubleBracket\\]', '〚'],
['\\\\\\[LeftDownTeeVector\\]', '⥡'],
['\\\\\\[LeftDownVectorBar\\]', '⥙'],
['\\\\\\[LeftDownVector\\]', '⇃'],
['\\\\\\[LeftFloor\\]', '⌊'],
['\\\\\\[LeftPointer\\]', '◂'],
['\\\\\\[LeftRightArrow\\]', '↔'],
['\\\\\\[LeftRightVector\\]', '⥎'],
['\\\\\\[LeftTee\\]', '⊣'],
['\\\\\\[LeftTeeArrow\\]', '↤'],
['\\\\\\[LeftTeeVector\\]', '⥚'],
['\\\\\\[LeftTriangle\\]', '⊲'],
['\\\\\\[LeftTriangleBar\\]', '⧏'],
['\\\\\\[LeftTriangleEqual\\]', '⊴'],
['\\\\\\[LeftUpDownVector\\]', '⥑'],
['\\\\\\[LeftUpTeeVector\\]', '⥠'],
['\\\\\\[LeftUpVector\\]', '↿'],
['\\\\\\[LeftUpVectorBar\\]', '⥘'],
['\\\\\\[LeftVector\\]', '↼'],
['\\\\\\[LeftVectorBar\\]', '⥒'],
['\\\\\\[LessEqual\\]', '≤'],
['\\\\\\[LessEqualGreater\\]', '⋚'],
['\\\\\\[LessFullEqual\\]', '≦'],
['\\\\\\[LessGreater\\]', '≶'],
['\\\\\\[LessLess\\]', '≪'],
['\\\\\\[LessSlantEqual\\]', '⩽'],
['\\\\\\[LessTilde\\]', '≲'],
['\\\\\\[LongDash\\]', '—'],
['\\\\\\[LongLeftArrow\\]', '⟵'],
['\\\\\\[LongLeftRightArrow\\]', '⟷'],
['\\\\\\[LongRightArrow\\]', '⟶'],
['\\\\\\[LowerLeftArrow\\]', '↙'],
['\\\\\\[LowerRightArrow\\]', '↘'],
['\\\\\\[Mars\\]', '♂'],
['\\\\\\[MeasuredAngle\\]', '∡'],
['\\\\\\[Mercury\\]', '☿'],
['\\\\\\[Mho\\]', '℧'],
['\\\\\\[Micro\\]', 'µ'],
['\\\\\\[MinusPlus\\]', '∓'],
['\\\\\\[Mu\\]', 'μ'],
['\\\\\\[Nand\\]', '⊼'],
['\\\\\\[Natural\\]', '♮'],
['\\\\\\[Neptune\\]', '♆'],
['\\\\\\[NestedGreaterGreater\\]', '⪢'],
['\\\\\\[NestedLessLess\\]', '⪡'],
['\\\\\\[Nor\\]', '⊽'],
['\\\\\\[NotCongruent\\]', '≢'],
['\\\\\\[NotCupCap\\]', '≭'],
['\\\\\\[NotDoubleVerticalBar\\]', '∦'],
['\\\\\\[NotElement\\]', '∉'],
['\\\\\\[NotEqual\\]', '≠'],
['\\\\\\[NotExists\\]', '∄'],
['\\\\\\[NotGreater\\]', '≯'],
['\\\\\\[NotGreaterEqual\\]', '≱'],
['\\\\\\[NotGreaterFullEqual\\]', '≩'],
['\\\\\\[NotGreaterLess\\]', '≹'],
['\\\\\\[NotGreaterTilde\\]', '≵'],
['\\\\\\[NotLeftTriangle\\]', '⋪'],
['\\\\\\[NotLeftTriangleEqual\\]', '⋬'],
['\\\\\\[NotLessEqual\\]', '≰'],
['\\\\\\[NotLessFullEqual\\]', '≨'],
['\\\\\\[NotLessGreater\\]', '≸'],
['\\\\\\[NotLess\\]', '≮'],
['\\\\\\[NotLessTilde\\]', '≴'],
['\\\\\\[Not\\]', '¬'],
['\\\\\\[NotPrecedes\\]', '⊀'],
['\\\\\\[NotPrecedesSlantEqual\\]', '⋠'],
['\\\\\\[NotPrecedesTilde\\]', '⋨'],
['\\\\\\[NotReverseElement\\]', '∌'],
['\\\\\\[NotRightTriangle\\]', '⋫'],
['\\\\\\[NotRightTriangleEqual\\]', '⋭'],
['\\\\\\[NotSquareSubsetEqual\\]', '⋢'],
['\\\\\\[NotSquareSupersetEqual\\]', '⋣'],
['\\\\\\[NotSubset\\]', '⊄'],
['\\\\\\[NotSubsetEqual\\]', '⊈'],
['\\\\\\[NotSucceeds\\]', '⊁'],
['\\\\\\[NotSucceedsSlantEqual\\]', '⋡'],
['\\\\\\[NotSucceedsTilde\\]', '⋩'],
['\\\\\\[NotSuperset\\]', '⊅'],
['\\\\\\[NotSupersetEqual\\]', '⊉'],
['\\\\\\[NotTilde\\]', '≁'],
['\\\\\\[NotTildeEqual\\]', '≄'],
['\\\\\\[NotTildeFullEqual\\]', '≇'],
['\\\\\\[NotTildeTilde\\]', '≉'],
['\\\\\\[Nu\\]', 'ν'],
['\\\\\\[Omega\\]', 'ω'],
['\\\\\\[Omicron\\]', 'ο'],
['\\\\\\[OpenCurlyDoubleQuote\\]', '“'],
['\\\\\\[OpenCurlyQuote\\]', '‘'],
['\\\\\\[Or\\]', '∨'],
['\\\\\\[OverBracket\\]', '⎴'],
['\\\\\\[Paragraph\\]', '¶'],
['\\\\\\[PartialD\\]', '∂'],
['\\\\\\[Phi\\]', 'ϕ'],
['\\\\\\[Pi\\]', 'π'],
['\\\\\\[PlusMinus\\]', '±'],
['\\\\\\[Pluto\\]', '♇'],
['\\\\\\[Precedes\\]', '≺'],
['\\\\\\[PrecedesEqual\\]', '⪯'],
['\\\\\\[PrecedesSlantEqual\\]', '≼'],
['\\\\\\[PrecedesTilde\\]', '≾'],
['\\\\\\[Prime\\]', '′'],
['\\\\\\[Product\\]', '∏'],
['\\\\\\[Proportion\\]', '∷'],
['\\\\\\[Proportional\\]', '∝'],
['\\\\\\[Psi\\]', 'ψ'],
['\\\\\\[QuarterNote\\]', '♩'],
['\\\\\\[ReturnIndicator\\]', '↵'],
['\\\\\\[ReverseDoublePrime\\]', '‶'],
['\\\\\\[ReverseElement\\]', '∋'],
['\\\\\\[ReverseEquilibrium\\]', '⇋'],
['\\\\\\[ReversePrime\\]', '‵'],
['\\\\\\[ReverseUpEquilibrium\\]', '⥯'],
['\\\\\\[Rho\\]', 'ρ'],
['\\\\\\[RightAngle\\]', '∟'],
['\\\\\\[RightAngleBracket\\]', '〉'],
['\\\\\\[RightArrow\\]', '→'],
['\\\\\\[RightArrowBar\\]', '⇥'],
['\\\\\\[RightArrowLeftArrow\\]', '⇄'],
['\\\\\\[RightCeiling\\]', '⌉'],
['\\\\\\[RightDoubleBracket\\]', '〛'],
['\\\\\\[RightDownTeeVector\\]', '⥝'],
['\\\\\\[RightDownVector\\]', '⇂'],
['\\\\\\[RightDownVectorBar\\]', '⥕'],
['\\\\\\[RightFloor\\]', '⌋'],
['\\\\\\[RightPointer\\]', '▸'],
['\\\\\\[RightTee\\]', '⊢'],
['\\\\\\[RightTeeArrow\\]', '↦'],
['\\\\\\[RightTeeVector\\]', '⥛'],
['\\\\\\[RightTriangle\\]', '⊳'],
['\\\\\\[RightTriangleBar\\]', '⧐'],
['\\\\\\[RightTriangleEqual\\]', '⊵'],
['\\\\\\[RightUpDownVector\\]', '⥏'],
['\\\\\\[RightUpTeeVector\\]', '⥜'],
['\\\\\\[RightUpVector\\]', '↾'],
['\\\\\\[RightUpVectorBar\\]', '⥔'],
['\\\\\\[RightVector\\]', '⇀'],
['\\\\\\[RightVectorBar\\]', '⥓'],
['\\\\\\[RoundImplies\\]', '⥰'],
['\\\\\\[SadSmiley\\]', '☹'],
['\\\\\\[Sampi\\]', 'ϡ'],
['\\\\\\[Saturn\\]', '♄'],
['\\\\\\[ScriptCapitalB\\]', 'ℬ'],
['\\\\\\[ScriptCapitalE\\]', 'ℰ'],
['\\\\\\[ScriptCapitalF\\]', 'ℱ'],
['\\\\\\[ScriptCapitalH\\]', 'ℋ'],
['\\\\\\[ScriptCapitalI\\]', 'ℐ'],
['\\\\\\[ScriptCapitalL\\]', 'ℒ'],
['\\\\\\[ScriptCapitalM\\]', 'ℳ'],
['\\\\\\[ScriptCapitalR\\]', 'ℛ'],
['\\\\\\[ScriptE\\]', 'ℯ'],
['\\\\\\[ScriptG\\]', 'ℊ'],
['\\\\\\[ScriptL\\]', 'ℓ'],
['\\\\\\[ScriptO\\]', 'ℴ'],
['\\\\\\[Sharp\\]', '♯'],
['\\\\\\[Sigma\\]', 'σ'],
['\\\\\\[SixPointedStar\\]', '✶'],
['\\\\\\[SkeletonIndicator\\]', '⁃'],
['\\\\\\[SmallCircle\\]', '∘'],
['\\\\\\[SpaceIndicator\\]', '␣'],
['\\\\\\[SpadeSuit\\]', '♠'],
['\\\\\\[SphericalAngle\\]', '∢'],
['\\\\\\[Sqrt\\]', '√'],
['\\\\\\[SquareIntersection\\]', '⊓'],
['\\\\\\[SquareSubset\\]', '⊏'],
['\\\\\\[SquareSubsetEqual\\]', '⊑'],
['\\\\\\[SquareSuperset\\]', '⊐'],
['\\\\\\[SquareSupersetEqual\\]', '⊒'],
['\\\\\\[SquareUnion\\]', '⊔'],
['\\\\\\[Star\\]', '⋆'],
['\\\\\\[Stigma\\]', 'ϛ'],
['\\\\\\[Subset\\]', '⊂'],
['\\\\\\[SubsetEqual\\]', '⊆'],
['\\\\\\[Succeeds\\]', '≻'],
['\\\\\\[SucceedsEqual\\]', '⪰'],
['\\\\\\[SucceedsSlantEqual\\]', '≽'],
['\\\\\\[SucceedsTilde\\]', '≿'],
['\\\\\\[SuchThat\\]', '∍'],
['\\\\\\[Sum\\]', '∑'],
['\\\\\\[Superset\\]', '⊃'],
['\\\\\\[SupersetEqual\\]', '⊇'],
['\\\\\\[Tau\\]', 'τ'],
['\\\\\\[Therefore\\]', '∴'],
['\\\\\\[Theta\\]', 'θ'],
['\\\\\\[Tilde\\]', '∼'],
['\\\\\\[TildeEqual\\]', '≃'],
['\\\\\\[TildeFullEqual\\]', '≅'],
['\\\\\\[TildeTilde\\]', '≈'],
['\\\\\\[Times\\]', '×'],
['\\\\\\[Trademark\\]', '™'],
['\\\\\\[UnderBracket\\]', '⎵'],
['\\\\\\[Union\\]', '⋃'],
['\\\\\\[UnionPlus\\]', '⊎'],
['\\\\\\[UpArrow\\]', '↑'],
['\\\\\\[UpArrowBar\\]', '⤒'],
['\\\\\\[UpArrowDownArrow\\]', '⇅'],
['\\\\\\[UpDownArrow\\]', '↕'],
['\\\\\\[UpEquilibrium\\]', '⥮'],
['\\\\\\[UpperLeftArrow\\]', '↖'],
['\\\\\\[UpperRightArrow\\]', '↗'],
['\\\\\\[UpPointer\\]', '▴'],
['\\\\\\[Upsilon\\]', 'υ'],
['\\\\\\[UpTee\\]', '⊥'],
['\\\\\\[UpTeeArrow\\]', '↥'],
['\\\\\\[Uranus\\]', '♅'],
['\\\\\\[Vee\\]', '⋁'],
['\\\\\\[Venus\\]', '♀'],
['\\\\\\[VerticalEllipsis\\]', '⋮'],
['\\\\\\[VerticalLine\\]', '│'],
['\\\\\\[VerticalTilde\\]', '≀'],
['\\\\\\[WatchIcon\\]', '⌚'],
['\\\\\\[Wedge\\]', '⋀'],
['\\\\\\[WeierstrassP\\]', '℘'],
['\\\\\\[Xi\\]', 'ξ'],
['\\\\\\[Xor\\]', '⊻'],
['\\\\\\[Zeta\\]', 'ζ']
];
// This function works exclusively on selected text regions
function ReplaceGlyphs() {
// Grab the current contents of the editor as well as the current selection
var old_val = editor.val();
var [s_start, s_end] = SelectionStartEnd(editor);
if (s_start !== s_end) {
var text = old_val.substr(s_start, s_end - s_start);
// iterate through the
for (var iter = 0; iter < rules.length; iter++) {
text = text.replace(new RegExp(rules[iter][0], 'gi'), rules[iter][1]);
}
// Splice the contents and insert the new text
var new_val = old_val.substr(0, s_start) + text + old_val.substr(s_end);
editor.val(new_val);
// Set the focus to the editor and select the newly added text
editor[0].focus();
editor[0].selectionStart = s_start;
editor[0].selectionEnd = s_start + text.length;
// Refresh the preview
StackExchange.MarkdownEditor.refreshAllPreviews();
}
}
function ReplaceInOut() {
var old_val = editor.val();
var [s_start, s_end] = SelectionStartEnd(editor);
if (s_start !== s_end) {
var orig = old_val.substr(s_start, s_end - s_start);
// A simple-minded In/Out parser that indents unindented input
// Iterates through matches of In/Out prompt and tries to
// keep track of current state.
// Unindent
if (/^(?:[ ]{4}[^\n]*[\n]?)+$/.test(orig)) {
orig = orig.replace(/^[ ]{4}([^\n]*)$/gm, "\$1");
}
var text = "";
var inoutregex = /(In\[\d+\]:=[ \n]|Out\[\d+\](?:\/\/(?:(?:Standard|Full|Input|Output|Traditional|TeX|MathMLTree|Scientific|Engineering|Accounting)Form|Short|Shallow))?=[ \n]|During evaluation of In\[\d+\]:=[ \n])/g;
var state = "I", type, last = 0, hasMore = true;
while (hasMore) {
if ((match = inoutregex.exec(orig))) {
text += orig.slice(last, match.index);
last = inoutregex.lastIndex;
type = match[0].substr(0, 1);
if ((type == "D")) {
type = "O";
}
// End Output cell
if ((state == "O")) {
// Avoid having *) on its own line
// Note that the newlines get stripped if next state is output
text = text.replace(/[\s]*$/, " *)\n\n");
}
// Start Output cell
if ((type == "O")) {
// Avoid empty line preceeding "(*"
// for instance when selectig cell group
// and copying with "edit -> copy -> copy as input text"
text = text.replace(/[\n]*$/, "\n(* ");
}
state = type;
} else {
hasMore = false;
}
}
text += orig.substr(last);
if ((state == "O")) {
text = text.replace(/[\s]*$/, " *)");
}
// Indent
text = text.replace(/^(.*)$/gm, " \$1");
// iterate through the glyphs too
for (var iter = 0; iter < rules.length; iter++) {
text = text.replace(new RegExp(rules[iter][0], 'gi'), rules[iter][1]);
}
var new_val = old_val.substr(0, s_start) + text + old_val.substr(s_end);
editor.val(new_val);
// Set the focus to the editor and select the newly added text
editor[0].focus();
editor[0].selectionStart = s_start;
editor[0].selectionEnd = s_start + text.length;
// Refresh the preview
StackExchange.MarkdownEditor.refreshAllPreviews();
}
}
// Again, the same logic as in InsertIntoEditor() to create a function which either asks for input
// or, if something is selected, uses the current selection as input. This is in my eyes a bit more usable
// then always using a input dialog.
function InsertShortcut() {
// Grab the current contents of the editor as well as the current selection
var old_val = editor.val();
var [s_start, s_end] = SelectionStartEnd(editor);
// Check whether text was selected and prompt the user for input if not.
var answer;
if (s_start == s_end) {
answer = prompt("Insert keys space separated like \"Ctrl Alt C\"");
} else {
answer = old_val.substr(s_start, s_end-s_start);
}
if (answer !== null && answer !== "") {
var keys = answer.split(" ");
var text = "";
for (var i = 0; i < keys.length; i++) {
if (i !== 0) {
text += "+";
}
text += "<kbd>" + keys[i] + "</kbd>";
}
// Splice the contents and insert the new text
var new_val = old_val.substr(0, s_start) + text + old_val.substr(s_end);
editor.val(new_val);
// Set the focus to the editor and select the newly added text
editor[0].focus();
editor[0].selectionStart = s_start;
editor[0].selectionEnd = s_start + text.length;
// Refresh the preview
StackExchange.MarkdownEditor.refreshAllPreviews();
}
}
var buttons = [
// This is the button to create automatically inline code which is linked to the Wolfram doc.
// This does not work for all functions!
{
'icon': 'https://i.stack.imgur.com/Jyeyd.png',
'tooltip': 'Create a link to Wolfram online documentation.',
'callback': function () {
ReplaceMarkdown(
'Mathematica Function (like Integrate, Plot, ..)',
'[`#INPUT#`](https://reference.wolfram.com/language/ref/#INPUT#.html)',
function (answer) {
return answer.trim().replace(/^`(.*)`$/g, "\$1");
})
}
},
// Button to replace Mathematica greek characters in selected text.
{
'icon': 'https://i.stack.imgur.com/UAgo2.png',
'tooltip': 'Replace symbol forms like \\[Alpha] with unicode glyphs in selected text.',
'callback': function () {
ReplaceGlyphs();
}
},
// Button to format In[1]:= .. code
{
'icon': 'https://i.stack.imgur.com/1drB7.png',
'tooltip': 'Removes In[1]:= .. marks and replace glyphs in selected text.',
'callback': function () {
ReplaceInOut();
}
},
// Button to insert nicely rendered shortcut keys
{
'icon': 'https://i.stack.imgur.com/W9yah.png',
'tooltip': 'Create nicely rendered keyboard shortcuts.',
'callback': function () {
InsertShortcut();
}
}
];
AddToolbarButtons(button_row, buttons);
}
});