Skip to content
Navigation Menu
{{ message }}
forked from weechat/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpastebuf.lua
More file actions
1461 lines (1336 loc) · 43.3 KB
/
Copy pathpastebuf.lua
File metadata and controls
1461 lines (1336 loc) · 43.3 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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
-- pastebuf
--
-- A script for viewing the content of a pastebin inside Weechat buffer.
--
-- Usage:
-- /pastebuf <pastebin-url> [<optional-syntax-language>]
--
-- To use syntax highlighting, set plugins.var.lua.pastebuf.syntax_highlighter to
-- an external command that will highlight the text. If the command contains
-- $lang, it will be replaced by the name of syntax language specified with
-- /pastebuf command. For example, to use pygmentize as syntax highlighter:
--
-- /set plugins.var.lua.pastebuf.syntax_highlighter "pygmentize -l $lang"
--
-- See README in https://github.com/tomoe-mami/weechat-scripts/tree/master/pastebuf
-- for more information.
--
-- Author: tomoe-mami <rumia.youkai.of.dusk@gmail.com>
-- License: WTFPL
-- Requires: Weechat 0.4.3+
-- URL: https://github.com/tomoe-mami/weechat-scripts/tree/master/pastebuf
--
-- History:
--
-- 2014-04-21 Gussi <gussi@gussi.is>
-- v0.3: * Added support for paste.is.
-- It's using sticky notes. API support added.
--
-- 2014-04-04 tomoe-mami <rumia.youkai.of.dusk@gmail.com>
-- * added `run` command inside paste buffer
-- * added option to enable opening url from unsupported service
-- * support url of "hidden paste" from paste.debian.net
-- * added support for paste.ee
--
-- 2014-01-24 tomoe-mami <rumia.youkai.of.dusk@gmail.com>
-- v0.2: * more supported services
-- * fixed csi sgr parsing
--
-- 2014-01-15 tomoe-mami <rumia.youkai.of.dusk@gmail.com>
-- v0.1: * initial release
--
--]]
local w = weechat
local g = {
script = {
name = "pastebuf",
author = "tomoe-mami <rumia.youkai.of.dusk@gmail.com>",
version = "0.3",
license = "WTFPL",
description = "View text from various pastebin sites inside a buffer.",
url = "https://github.com/tomoe-mami/weechat-scripts/tree/master/pastebuf"
},
config = {},
defaults = {
fetch_timeout = {
value = 30000,
type = "number",
description = "Timeout for fetching URL (in milliseconds)"
},
highlighter_timeout = {
value = 3000,
type = "number",
description = "Timeout for syntax highlighter (in milliseconds)"
},
show_line_number = {
value = true,
type = "boolean",
description = "Show line number"
},
open_unsupported_url = {
value = false,
type = "boolean",
description = "Force open raw text of unsupported URL format"
},
color_line_number = {
value = "default,darkgray",
type = "string",
description = "Color for line number"
},
color_line = {
value = "default,default",
type = "string",
description = "Color for line content"
},
syntax_highlighter = {
value = "",
type = "string",
description =
"External command that will be used as syntax highlighter. " ..
"$lang will be replaced by the name of syntax language"
},
shell = {
value = "/bin/sh",
type = "string",
description =
"Location of your shell or just the shell name if it's already in $PATH"
},
sticky_notes_retardness_level = {
value = 1,
type = "number",
description =
"The retardness level of Sticky Notes API. Use level 0 if they " ..
"somehow fixed their JSON string. Use level 1 to fix their awful " ..
"JSON string first before decoding it. Use level 2 if level 1 " ..
"failed fixing their JSON string. In level 2, we'll abandon their " ..
"API and just fetch the raw paste. Default is 1."
}
},
sites = {
__generic__ = {
pattern = "^([^:/]+://[^/]+)(.*)$",
id = "%2",
raw = "%1%2"
},
["bpaste.net"] = {
pattern = "^([^:/]+://[^/]+)/show/(%w+)",
id = "%s",
raw = "%1/raw/%2"
},
["codepad.org"] = {
pattern = "^([^:/]+://[^/]+)/(%w+)",
id = "%2",
raw = "%1/%2/raw.php"
},
["dpaste.com"] = {
pattern = "^([^:/]+://[^/]+)/(%w+)",
id = "%2",
raw = "%1/%2/plain/"
},
["dpaste.de"] = {
pattern = "^([^:/]+://[^/]+)/(%w+)",
id = "%2",
raw = "%1/%2/raw"
},
["fpaste.org"] = {
pattern = "^([^:/]+://[^/]+)/(%w+/?%w*)",
id = "%2",
raw = "%1/%2/raw"
},
["gist.github.com"] = {
pattern = "^([^:/]+://[^/]+)/([^/]+/[^/]+)",
id = "%2",
raw = "https://gist.githubusercontent.com/%2/raw"
},
["ideone.com"] = {
pattern = "^([^:/]+://[^/]+)/(%w+)",
id = "%2",
raw = "%1/plain/%2"
},
["paste.debian.net"] = {
pattern = "^([^:/]+://[^/]+)/(%w+)",
id = "%2",
raw = "%1/plain/%2"
},
["paste.ee"] = {
pattern = "^([^:/]+://[^/]+)/p/(%w+)",
id = "%2",
raw = "%1/r/%2"
},
["pastebin.ca"] = {
pattern = "^([^:/]+://[^/]+)/(%w+)",
id = "%2",
raw = "%1/raw/%2"
},
["pastebin.com"] = {
pattern = "^([^:/]+://[^/]+)/(%w+)",
id = "%2",
raw = "%1/raw.php?i=%2"
},
["pastebin.osuosl.org"] = {
pattern = "^([^:/]+://[^/]+)/(%w+)",
id = "%2",
raw = "%1/%2/raw/"
},
["paste.opensuse.org"] = {
pattern = "^([^:/]+://[^/]+)/(%w+)",
id = "%2",
raw = "%1/view/raw/%2"
},
["pastie.org"] = {},
["sprunge.us"] = {
pattern = "^([^:/]+://[^/]+)/(%w+)",
id = "%2",
raw = "%1/%2"
},
["vpaste.net"] = {
pattern = "^([^:/]+://[^/]+)/(%w+)",
id = "%2",
raw = "%1/%2?raw"
},
["paste.is"] = {
pattern = "^([^:/]+://[^/]+)/(%w+)",
id = "%2",
raw = "%1/%2/raw/"
}
},
keys = {
normal = {
["meta2-A"] = "/window scroll -1", -- arrow up
["meta2-B"] = "/window scroll 1", -- arrow down
["meta2-C"] = "/window scroll_horiz 1", -- arrow right
["meta2-D"] = "/window scroll_horiz -1", -- arrow left
["meta-OA"] = "/window scroll -10", -- ctrl+arrow up
["meta-OB"] = "/window scroll 10", -- ctrl+arrow down
["meta-OC"] = "/window scroll_horiz 10", -- ctrl+arrow right
["meta-OD"] = "/window scroll_horiz -10", -- ctrl+arrow left
["meta2-1~"] = "/pastebuf **scroll start", -- home
["meta2-4~"] = "/pastebuf **scroll end", -- end
["meta-c"] = "/buffer close", -- alt+c
}
},
hide_stderr = true,
buffers = {},
actions = {},
langs = {
-- syntax lang aliases.
none = false,
plain = false,
text = false,
shell = "sh",
markdown = false
},
sgr = {
attributes = {
[1] = "*", -- bold
[3] = "/", -- italic
[4] = "_", -- underline
[7] = "!" -- inverse
},
colors = {
[ 0] = "black",
[ 1] = "red",
[ 2] = "green",
[ 3] = "yellow",
[ 4] = "blue",
[ 5] = "magenta",
[ 6] = "cyan",
[ 7] = "gray",
[ 8] = "darkgray",
[ 9] = "lightred",
[10] = "lightgreen",
[11] = "brown",
[12] = "lightblue",
[13] = "lightmagenta",
[14] = "lightcyan",
[15] = "white"
}
}
}
function prepare_modules(modules)
local module_exists = function (name)
if package.loaded[name] then
return true
else
for _, searcher in ipairs(package.searchers or package.loaders) do
local loader = searcher(name)
if type(loader) == "function" then
package.preload[name] = loader
return true
end
end
return false
end
end
for alias, name in pairs(modules) do
if module_exists(name) then
_G[alias] = require(name)
end
end
end
function convert_plugin_option_value(opt_type, opt_value)
if opt_type == "number" or opt_type == "boolean" then
opt_value = tonumber(opt_value)
if opt_type == "boolean" then
opt_value = (opt_value ~= 0)
end
end
return opt_value
end
function load_config()
local shell = os.getenv("SHELL")
if shell and shell ~= "" then
g.defaults.shell.value = shell
end
for opt_name, info in pairs(g.defaults) do
if w.config_is_set_plugin(opt_name) == 0 then
local val
if info.type == "boolean" then
val = info.value and 1 or 0
elseif info.type == "number" then
val = info.value or 0
else
val = info.value or ""
if opt_name == "syntax_highlighter" and val == "" then
val = nil
end
end
w.config_set_plugin(opt_name, val)
w.config_set_desc_plugin(opt_name, info.description or "")
g.config[opt_name] = val
else
local val = w.config_get_plugin(opt_name)
g.config[opt_name] = convert_plugin_option_value(info.type, val)
end
end
end
function bind_keys(buffer, flag)
local prefix = flag and "key_bind_" or "key_unbind_"
for key, command in pairs(g.keys) do
w.buffer_set(buffer, prefix .. key, flag and command or "")
end
end
-- crude converter from csi sgr colors to weechat color
function convert_csi_sgr(text)
local fg, bg, attr = "", "", "|"
local shift_param = function(s)
if s then
local p1, p2, chunk = s:find("^(%d+);?")
if p1 then
return chunk, s:sub(p2 + 1)
end
end
end
local convert_cb = function(code)
local chunk, code = shift_param(code)
while chunk do
chunk = tonumber(chunk)
if chunk == 0 then
attr = ""
local c2 = shift_param(code)
if not c2 or c2 == "" then
fg, bg = "", ""
end
elseif g.sgr.attributes[chunk] then
attr = g.sgr.attributes[chunk]
elseif chunk >= 30 and chunk <= 37 then
fg = g.sgr.colors[ chunk - 30 ]
elseif chunk == 38 then
local c2, c3
c2, code = shift_param(code)
fg, c2 = "default", tonumber(c2)
if c2 == 5 then
c3, code = shift_param(code)
if c3 then
fg = tonumber(c3)
end
end
elseif chunk == 39 then
fg = "default"
elseif chunk >= 40 and chunk <= 47 then
bg = g.sgr.colors[ chunk - 40 ]
elseif chunk == 48 then
local c2, c3
c2, code = shift_param(code)
bg, c2 = "default", tonumber(c2)
if c2 == 5 then
c3, code = shift_param(code)
if c3 then
bg = tonumber(c3)
end
end
elseif chunk == 49 then
bg = "default"
elseif chunk >= 90 and chunk <= 97 then
fg = g.sgr.colors[ chunk - 82 ]
elseif chunk >= 100 and chunk <= 107 then
bg = g.sgr.colors[ chunk - 92 ]
end
chunk, code = shift_param(code)
end
local result
if fg == "" and bg == "" and attr == "" then
result = "reset"
else
result = attr .. fg
if bg and bg ~= "" then
result = result .. "," .. bg
end
end
return w.color(result)
end
return text:gsub("\27%[([%d;]*)m", convert_cb)
end
function message(s)
w.print("", g.script.name .. "\t" .. s)
end
function get_lang(lang)
if not lang or lang == "" then
return false
end
lang = lang:lower()
if g.langs[lang] ~= nil then
lang = g.langs[lang]
end
return lang
end
-- false will delete a localvar. nil (or value not specified) will return
-- the current value. anything else will set the localvar to that value.
function localvar(pointer, variable, value)
if value == nil then
return w.buffer_get_string(pointer, "localvar_" .. variable)
elseif value == false then
w.buffer_set(pointer, "localvar_del_" .. variable, "")
else
if value == true then value = 1 end
w.buffer_set(pointer, "localvar_set_" .. variable, value)
end
end
function parse_response_header(response)
local p, c, m, h, r = response:match("^(%S+) (%d+) (.-)\r\n(.-)\r\n\r\n(.*)$")
if p then
c = tonumber(c)
if c == 301 or c == 302 or c == 303 then
if r ~= "" then
-- since we use followlocation=1, there will be another block of header
-- after the first empty line. parse that one instead.
return parse_response_header(r)
end
end
local result = {
protocol = p,
status_code = c,
status_message = m
}
if h then
result.headers = {}
h = h .. "\r\n"
for name, value in h:gmatch("([^:]+):%s+(.-)\r\n") do
result.headers[name] = value
end
end
return result
end
end
function exec_generic_cb(short_name, cmd, status, response, err)
local buffer = g.buffers[short_name]
if not buffer then
return
end
if status == 0 or status == w.WEECHAT_HOOK_PROCESS_RUNNING then
buffer.temp = buffer.temp .. response
if buffer.callback_partial and type(buffer.callback_partial) == "function" then
buffer.callback_partial(buffer, short_name, response)
end
if status == 0 then
local data = buffer.temp
buffer.temp = nil
if buffer.callback_ok and type(buffer.callback_ok) == "function" then
buffer.callback_ok(buffer, short_name, data)
end
end
elseif status >= 1 or status == w.WEECHAT_HOOK_PROCESS_ERROR then
if (cmd:sub(1, 4) ~= "url:" and g.hide_stderr) or not err or err == "" then
err = "Error when trying to access " .. cmd
end
message(string.format("Error %d: %s", status, err))
if buffer.callback_error and type(buffer.callback_error) == "function" then
buffer.callback_error(buffer, short_name, status, err)
end
end
end
function exec_generic(short_name, cmd, options, callbacks)
local buffer = g.buffers[short_name]
buffer.temp = ""
if callbacks then
local cb_type = type(callbacks)
local types = { ok = true, error = true, partial = true, input = true }
if cb_type == "function" then
buffer.callback_ok = callbacks
elseif cb_type == "table" then
for t, f in pairs(callbacks) do
if type(f) == "function" and types[t] then
buffer["callback_" .. t] = f
end
end
end
end
if cmd:sub(1,4) == "url:" and options then
if not options.useragent then
options.useragent = g.useragent
end
if not options.followlocation then
options.followlocation = 1
end
end
if options then
buffer.hook = w.hook_process_hashtable(
cmd,
options,
g.config.fetch_timeout,
"exec_generic_cb",
short_name)
if options.stdin and options.stdin == 1 and buffer.callback_input then
buffer.callback_input(buffer, short_name, buffer.hook)
end
else
buffer.hook = w.hook_process(
cmd,
g.config.fetch_timeout,
"exec_generic_cb",
short_name)
end
return buffer.hook
end
function request_head(short_name, url, options, callbacks)
if not options then
options = {}
end
options.nobody = 1
options.header = 1
exec_generic(short_name, "url:" .. url, options, callbacks)
end
function copy_table(t)
local r = {}
for k,v in pairs(t) do
r[k] = v
end
return r
end
function get_site_config(u)
local host = u:match("^https?://([^/]+)")
if host then
if host:match("^www%.") then
host = host:sub(5)
end
local site
if not g.sites[host] then
if not g.config.open_unsupported_url then
return
else
site = copy_table(g.sites.__generic__)
end
else
site = copy_table(g.sites[host])
end
site.host = host
site.url = u
if not site.handler then
site.id = string.gsub(u, site.pattern, site.id)
site.raw = string.gsub(u, site.pattern, site.raw)
end
return site
end
end
function init_mode(buf_ptr, mode)
local prev_mode = localvar(buf_ptr, "mode")
for key, _ in pairs(g.keys[prev_mode]) do
w.buffer_set(buf_ptr, "key_unbind_" .. key, "")
end
for key, cmd in pairs(g.keys[mode]) do
w.buffer_set(buf_ptr, "key_bind_" .. key, cmd)
end
localvar(buf_ptr, "mode", mode)
end
function action_scroll(buffer, short_name, param)
if param == "start" then
w.command(buffer.pointer, "/window scroll_top")
w.command(buffer.pointer, "/window scroll_horiz -100%")
elseif param == "end" then
w.command(buffer.pointer, "/window scroll_bottom")
w.command(buffer.pointer, "/window scroll_horiz -100%")
end
end
function action_run(buffer, short_name, param)
if not g.config.shell or g.config.shell == "" then
message(
"Can not run command because the shell is empty. " ..
"Please specify your shell in plugins.var.lua.pastebuf.shell")
return
end
local cmd, opt = param, param:sub(1, 3)
local exec_options, exec_cb = {}, { ok = display_colors }
if opt== "-n " then
cmd = param:sub(4)
else
exec_options.stdin = 1
end
if cmd == "" then
message("Please specify an external command")
return
end
localvar(buffer.pointer, "temp", buffer.temp_name)
cmd = w.buffer_string_replace_local_var(buffer.pointer, cmd)
localvar(buffer.pointer, "temp", false)
if exec_options.stdin then
exec_cb.input = function (_, _, hook)
local fp = open_file(buffer.temp_name)
for line in fp:lines() do
w.hook_set(hook, "stdin", line .. "\n")
end
w.hook_set(hook, "stdin_close", "")
fp:close()
end
end
exec_generic(
short_name,
string.format("%s -c %q", g.config.shell, cmd),
exec_options,
exec_cb)
end
function action_save(buffer, short_name, filename)
if not filename or filename == "" then
message("You need to specify destination filename after `save`")
else
filename = filename:gsub("^~/", os.getenv("HOME") .. "/")
local output = open_file(filename, "w")
if output then
local input = open_file(buffer.temp_name)
if input then
local chunk_size, written, chunk = 64 * 1024, 0
chunk = input:read(chunk_size)
while chunk do
output:write(chunk)
written = written + #chunk
chunk = input:read(chunk_size)
end
input:close()
message(string.format(
"%d %s written to %s",
written,
(written == 1 and "byte" or "bytes"),
filename))
end
output:close()
end
end
end
function action_change_language(buffer, short_name, new_lang)
if not g.config.syntax_highlighter then
return
end
new_lang = new_lang:match("^%s*(%S+)")
if not new_lang then
message("You need to specify the name of syntax language after `lang`")
else
local current_lang = localvar(buffer.pointer, "lang") or ""
new_lang = get_lang(new_lang)
if current_lang ~= new_lang then
local fp = open_file(buffer.temp_name)
if fp then
buffer.file = fp
if new_lang then
localvar(buffer.pointer, "lang", new_lang)
run_syntax_highlighter(short_name, fp)
else
localvar(buffer.pointer, "lang", false)
display_plain(short_name, fp)
end
fp:close()
buffer.file = nil
end
end
end
end
function action_open_recent_url(current_buffer, limit)
local list = {}
limit = tonumber(limit)
if not limit or limit == 0 then
limit = 1
end
local buf_lines = w.infolist_get("buffer_lines", current_buffer, "")
if buf_lines and buf_lines ~= "" then
local url_matcher = "(https?://[%w:!/#_~@&=,;%+%?%[%]%.%%%-]+)"
local process_line = function ()
if w.infolist_integer(buf_lines, "displayed") ~= 1 then
return 0
end
local line = w.infolist_string(buf_lines, "message")
line = w.string_remove_color(line, "")
local url = line:match(url_matcher)
if not url then
return 0
end
local site = get_site_config(url)
if site then
if site.handler and type(site.handler) == "function" then
site.handler(site, url)
else
handler_normal(site, url)
end
return 1
end
return 0
end
w.infolist_prev(buf_lines)
local c = process_line()
while c < limit do
if w.infolist_prev(buf_lines) ~= 1 then break end
c = c + process_line()
end
w.infolist_free(buf_lines)
if not c or c == 0 then
message("No URLs from supported paste services found")
end
end
end
function create_buffer(site)
local short_name
if site.short_name then
short_name = site.short_name
else
short_name = string.format("%s:%s", site.host, site.id)
end
local name = string.format("%s:%s", g.script.name, short_name)
local buffer = w.buffer_new(name, "buffer_input_cb", "", "buffer_close_cb", "")
if buffer and buffer ~= "" then
local default_mode = "normal"
w.buffer_set(buffer, "type", "free")
w.buffer_set(buffer, "short_name", short_name)
w.buffer_set(buffer, "display", "1")
localvar(buffer, "mode", default_mode)
init_mode(buffer, default_mode)
g.buffers[short_name] = { pointer = buffer }
return g.buffers[short_name], short_name
end
end
function display_plain(short_name, fp)
local pointer = g.buffers[short_name].pointer
local total_lines = 0
if g.config.show_line_number then
local lines, total_lines = {}, 0
for line in fp:lines() do
total_lines = total_lines + 1
lines[total_lines] = line
end
if total_lines > 0 then
w.buffer_clear(pointer)
local num_col_width = #tostring(total_lines)
local y = 0
for _, line in ipairs(lines) do
print_line(pointer, y, num_col_width, line)
y = y + 1
end
end
else
for line in fp:lines() do
print_line(pointer, total_lines, nil, line)
total_lines = total_lines + 1
end
end
end
function display_colors(buffer, short_name, data)
local y, num_col_width = 0
data = convert_csi_sgr(data)
if g.config.show_line_number then
local _, total_lines = string.gsub(data .. "\n", ".-\n[^\n]*", "")
num_col_width = #tostring(total_lines)
end
w.buffer_clear(buffer.pointer)
for line in data:gmatch("(.-)\n") do
print_line(buffer.pointer, y, num_col_width, line)
y = y + 1
end
end
function run_syntax_highlighter(short_name, fp)
local buffer = g.buffers[short_name]
local cmd = w.buffer_string_replace_local_var(
buffer.pointer,
g.config.syntax_highlighter)
local input_cb = function (_, _, hook)
for line in fp:lines() do
w.hook_set(hook, "stdin", line .. "\n")
end
w.hook_set(hook, "stdin_close", "")
end
exec_generic(
short_name,
cmd,
{ stdin = 1 },
{
ok = display_colors,
input = input_cb
});
end
function open_file(filename, mode)
local fp = io.open(filename, mode or "r")
if not fp then
message(string.format("Unable to open file %s", filename))
else
return fp
end
end
function write_temp(data)
local temp_name = os.tmpname()
local fp = open_file(temp_name, "w+")
if fp then
fp:write(data)
fp:seek("set")
return fp, temp_name
end
end
function display_paste(short_name)
local buffer = g.buffers[short_name]
local fp = open_file(buffer.temp_name)
if fp then
buffer.file = fp
local lang = get_lang(localvar(buffer.pointer, "lang"))
if g.config.syntax_highlighter and lang then
run_syntax_highlighter(short_name, fp)
else
display_plain(short_name, fp)
end
fp:close()
buffer.file = nil
localvar(buffer.pointer, "temp", buffer.temp_name)
w.buffer_set(
buffer.pointer,
"title",
string.format("%s: %s", g.script.name, localvar(buffer.pointer, "url")))
end
end
function print_line(buffer, y, num_width, content)
local line = w.color(g.config.color_line) .. " " .. content
if num_width then
line = string.format(
"%s %" .. num_width .. "d %s",
w.color(g.config.color_line_number),
y + 1,
line)
end
w.print_y(buffer, y, line)
end
function decode_json_response(s)
if not s or s == "" then
message("Error: No response received")
else
local decoded = json.decode(s)
if not decoded or type(decoded) ~= "table" then
message("Error: Unable to parse server response")
else
return decoded
end
end
end
function handler_sticky_notes(site, url, lang)
local id, hash = url:match("^https?://[^/]+/(%w+)/?(%w*)")
if id then
site.id = id
local short_name = string.format("%s:%s", site.host, id)
if not g.buffers[short_name] then
g.buffers[short_name] = { host = site.host, url = url }
local api_url = string.format("http://%s/api/json/%s", site.host, site.id)
if hash then
api_url = api_url .. "/" .. hash
local fix_json = function (json_string)
return json_string:gsub('"data":%s*"(.-)"', function (s)
s = s:gsub("\\", "\\\\")
s = s:gsub(
"([\t\n\r\b\f])",
{
["\t"] = "\\t", ["\n"] = "\\n",
["\r"] = "\\r", ["\b"] = "\\b",
["\f"] = "\\f"
})
s = s:gsub(
"&([^;]+);",
{ lt = "<", gt = ">", quot = '\\"', amp = "&" })
return string.format('"data": "%s"', s)
end)
end
local process_info = function (buffer, short_name, data)
if g.config.sticky_notes_retardness_level == 1 then
data = fix_json(data)
end
local info = decode_json_response(data)
if not info then
if g.buffers[short_name] then
g.buffers[short_name] = nil
end
return
end
if info.result.error then
message(string.format("Error: %s", info.result.error))
else
local param = {
short_name = short_name,
url = buffer.url,
host = buffer.host
}
local buffer = create_buffer(param)
if not buffer then return end
localvar(buffer.pointer, "url", param.url)
localvar(buffer.pointer, "host", param.host)
localvar(buffer.pointer, "id", param.id)
w.buffer_set(
buffer.pointer,
"title",
string.format("%s: %s", g.script.name, param.url))
local use_highlighter = false
if info.result.language and
info.result.language ~= json.null and
info.result.language ~= "" then
local lang = get_lang(info.result.language)
if lang then
localvar(buffer.pointer, "lang", lang)
if g.config.syntax_highlighter then
use_highlighter = true
end
end
end
buffer.file, buffer.temp_name = write_temp(info.result.data)
if buffer.file then
if use_highlighter then
run_syntax_highlighter(short_name, buffer.file)
else
display_plain(short_name, buffer.file)
end
buffer.file:close()
buffer.file = nil
end
end
end
local on_error = function (buffer, short_name, status, message)
g.buffers[short_name] = nil
end
exec_generic(short_name, "url:" .. api_url, {}, { ok = process_info, error = on_error })
end
end
end
end
function handler_gist(site, url)
local first, second = url:match("^https://gist%.github%.com/([^/]+)/?([^/]*)")
local host, gist_id = "gist.github.com"
if second and second ~= "" then
gist_id = second
elseif first then
gist_id = first
else
message("Unrecognized gist url")
return w.WEECHAT_RC_ERROR
end
You can’t perform that action at this time.
