adv_windowlist.pl 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879
  1. use strict;
  2. use warnings;
  3. our $VERSION = '1.8'; # 63feb35d8b0a8b6
  4. our %IRSSI = (
  5. authors => 'Nei',
  6. contact => 'Nei @ anti@conference.jabber.teamidiot.de',
  7. url => "http://anti.teamidiot.de/",
  8. name => 'adv_windowlist',
  9. description => 'Adds a permanent advanced window list on the right or in a status bar.',
  10. sbitems => 'awl_shared',
  11. license => 'GNU GPLv2 or later',
  12. );
  13. # UPGRADE NOTE
  14. # ============
  15. # for users of 0.7 or earlier series, please note that appearance
  16. # settings have moved to /format, i.e. inside your theme!
  17. # the fifo (screen) has been replaced by an external viewer script
  18. # Usage
  19. # =====
  20. # copy the script to ~/.irssi/scripts/
  21. #
  22. # In irssi:
  23. #
  24. # /run adv_windowlist
  25. #
  26. # In your shell (for example a tmux split):
  27. #
  28. # perl ~/.irssi/scripts/adv_windowlist.pl
  29. #
  30. # To use sbar mode instead:
  31. #
  32. # /toggle awl_viewer
  33. #
  34. # Hint: to get rid of the old [Act:] display
  35. # /statusbar window remove act
  36. #
  37. # to get it back:
  38. # /statusbar window add -after lag -priority 10 act
  39. # Options
  40. # =======
  41. # formats can be cleared with /format -delete
  42. #
  43. # /format awl_display_(no)key(_active|_visible) <string>
  44. # * string : Format String for one window. The following $'s are expanded:
  45. # $C : Name
  46. # $N : Number of the Window
  47. # $Q : meta-Keymap
  48. # $H : Start hilighting
  49. # $S : Stop hilighting
  50. # /+++++++++++++++++++++++++++++++++,
  51. # | **** I M P O R T A N T : **** |
  52. # | |
  53. # | don't forget to use $S if you |
  54. # | used $H before! |
  55. # | |
  56. # '+++++++++++++++++++++++++++++++++/
  57. # key : a key binding that goes to this window could be detected in /bind
  58. # nokey : no such key binding was detected
  59. # active : window would receive the input you are currently typing
  60. # visible : window is also visible on screen but not active (a split window)
  61. #
  62. # /format awl_name_display <string>
  63. # * string : Format String for window names
  64. # $0 : name as formatted by the settings
  65. #
  66. # /format awl_display_header <string>
  67. # * string : Format String for this header line. The following $'s are expanded:
  68. # $C : network tag
  69. #
  70. # /format awl_separator(2) <string>
  71. # * string : Character to use between the channel entries
  72. # variant 2 can be used for alternating separators (only in status bar
  73. # without block display)
  74. #
  75. # /format awl_abbrev_chars <string>
  76. # * string : Character to use when shortening long names. The second character
  77. # will be used if two blocks need to be filled.
  78. #
  79. # /format awl_title <string>
  80. # * string : Text to display in the title string or title bar
  81. #
  82. # /format awl_viewer_item_bg <string>
  83. # * string : Format String specifying the viewer's item background colour
  84. #
  85. # /set awl_prefer_name <ON|OFF>
  86. # * this setting decides whether awl will use the active_name (OFF) or the
  87. # window name as the name/caption in awl_display_*.
  88. # That way you can rename windows using /window name myownname.
  89. #
  90. # /set awl_hide_empty <num>
  91. # * if visible windows without items should be hidden from the window list
  92. # set it to 0 to show all windows
  93. # 1 to hide visible windows without items (negative exempt
  94. # active window)
  95. #
  96. # /set awl_detach <list>
  97. # * list of windows that should be hidden from the window list. you
  98. # can also use /awl detach and /awl attach to manage this
  99. # setting. an optional data_level can be specified with ",num"
  100. #
  101. # /set awl_detach_data <num>
  102. # * num : hide the detached window if its data_level is below num
  103. #
  104. # /set awl_detach_aht <ON|OFF>
  105. # * if enabled, also detach all windows listed in the
  106. # activity_hide_targets setting
  107. #
  108. # /set awl_hide_data <num>
  109. # * num : hide the window if its data_level is below num
  110. # set it to 0 to basically disable this feature,
  111. # 1 if you don't want windows without activity to be shown
  112. # 2 to show only those windows with channel text or hilight
  113. # 3 to show only windows with hilight (negative exempt active window)
  114. #
  115. # /set awl_hide_name_data <num>
  116. # * num : hide the name of the window if its data_level is below num
  117. # (only works in status bar without block display)
  118. # you will want to change your formats to add $H...$S around $Q or $N
  119. # if you plan to use this
  120. #
  121. # /set awl_maxlines <num>
  122. # * num : number of lines to use for the window list (0 to disable, negative
  123. # lock)
  124. #
  125. # /set awl_maxcolumns <num>
  126. # * num : number of columns to use for the window list when using the
  127. # tmux integration (0 to disable)
  128. #
  129. # /set awl_block <num>
  130. # * num : width of a column in viewer mode (negative values = block
  131. # display in status bar mode)
  132. # /+++++++++++++++++++++++++++++++++,
  133. # | ****** W A R N I N G ! ****** |
  134. # | |
  135. # | If your block display looks |
  136. # | DISTORTED, you need to add the |
  137. # | following line to your .theme |
  138. # | file under |
  139. # | abstracts = { : |
  140. # | |
  141. # | sb_act_none = "%K$*"; |
  142. # | |
  143. # '+++++++++++++++++++++++++++++++++/
  144. #
  145. # /set awl_sbar_maxlength <ON|OFF>
  146. # * if you enable the maxlength setting, the block width will be used as a
  147. # maximum length for the non-block status bar mode too.
  148. #
  149. # /set awl_height_adjust <num>
  150. # * num : how many lines to leave empty in viewer mode
  151. #
  152. # /set awl_sort <-data_level|-last_line|refnum>
  153. # * you can change the window sort order with this variable
  154. # -data_level : sort windows with hilight first
  155. # -last_line : sort windows in order of activity
  156. # refnum : sort windows by window number
  157. # active/server/tag : sort by server name
  158. # lru : sort windows with the last recently used last
  159. # "-" reverses the sort order
  160. # typechecks are supported via ::, e.g. active::Query or active::Irc::Query
  161. # undefinedness can be checked with ~, e.g. ~active
  162. # string comparison can be done with =, e.g. name=(status)
  163. # to make sort case insensitive, use #i, e.g. name#i
  164. # any key in the window hash can be tested, e.g. active/chat_type=XMPP
  165. # multiple criteria can be separated with , or +, e.g. -data_level+-last_line
  166. #
  167. # /set awl_placement <top|bottom>
  168. # /set awl_position <num>
  169. # * these settings correspond to /statusbar because awl will create
  170. # status bars for you
  171. # (see /help statusbar to learn more)
  172. #
  173. # /set awl_all_disable <ON|OFF>
  174. # * if you set awl_all_disable to ON, awl will also remove the
  175. # last status bar it created if it is empty.
  176. # As you might guess, this only makes sense with awl_hide_data > 0 ;)
  177. #
  178. # /set awl_viewer <ON|OFF>
  179. # * enable the external viewer script
  180. #
  181. # /set awl_viewer_launch <ON|OFF>
  182. # * try to auto-launch the viewer under tmux or with a shell command
  183. # /awl restart is required all auto-launch related settings to take
  184. # effect
  185. #
  186. # /set awl_viewer_tmux_position <left|top|right|bottom|custom>
  187. # * try to split in this direction when using tmux for the viewer
  188. # custom : use custom_command setting
  189. #
  190. # /set awl_viewer_xwin_command <shell command>
  191. # * custom command to run in order to start the viewer when irssi is
  192. # running under X
  193. # %A - gets replaced by the command to run the viewer
  194. # %qA - additionally quote the command
  195. #
  196. # /set awl_viewer_custom_command <shell command>
  197. # * custom command to run in order to start the viewer
  198. #
  199. # /set awl_viewer_launch_env <string>
  200. # * specific environment settings for use on viewer auto-launch,
  201. # without the AWL_ prefix
  202. #
  203. # /set awl_shared_sbar <left<right|OFF>
  204. # * share a status bar for the first awl item, you will need to manually
  205. # /statusbar window add -after lag -priority 10 awl_shared
  206. # left : space in cells occupied on the left of status bar
  207. # right : space occupied on the right
  208. # Note: you need to replace "left" AND "right" with the appropriate numbers!
  209. #
  210. # /set awl_path <path>
  211. # * path to the file which the viewer script reads
  212. #
  213. # /set fancy_abbrev <no|head|strict|fancy>
  214. # * how to shorten too long names
  215. # no : shorten in the middle
  216. # head : always cut off the ends
  217. # strict : shorten repeating substrings
  218. # fancy : combination of no+strict
  219. #
  220. # /set awl_custom_xform <perl code>
  221. # * specify a custom routine to transform window names
  222. # example: s/^#// remove the #-mark of IRC channels
  223. # the special flags $CHANNEL / $TAG / $QUERY / $NAME can be
  224. # tested in conditionals
  225. #
  226. # /set awl_last_line_shade <timeout>
  227. # * set timeout to shade activity base colours, to enable
  228. # you also need to add +-last_line to awl_sort
  229. # (requires 256 colour support)
  230. #
  231. # /set awl_no_mode_hint <ON|OFF>
  232. # * whether to show the hint of running the viewer script in the
  233. # status bar
  234. #
  235. # /set awl_mouse <ON|OFF>
  236. # * enable the terminal mouse in irssi
  237. # (use the awl-patched mouse.pl for gestures and commands if you need
  238. # them and disable mouse_escape)
  239. #
  240. # /set awl_mouse_offset <num>
  241. # * specifies where on the screen is the awl status bar
  242. # (0 = on top/bottom, 1 = one additional line in between,
  243. # e.g. prompt)
  244. # you MUST set this correctly otherwise the mouse coordinates will
  245. # be off
  246. #
  247. # /set mouse_scroll <num>
  248. # * how many lines the mouse wheel scrolls
  249. #
  250. # /set mouse_escape <num>
  251. # * seconds to disable the mouse, when not clicked on the windowlist
  252. #
  253. # Commands
  254. # ========
  255. # /awl detach <num>
  256. # * hide the current window from the window list. num specifies the
  257. # data_level (optional)
  258. #
  259. # /awl attach
  260. # * unhide the current window from the window list
  261. #
  262. # /awl ack
  263. # * change to the next window with activity, ignoring detached windows
  264. #
  265. # /awl redraw
  266. # * redraws the windowlist. There may be occasions where the
  267. # windowlist can get destroyed so you can use this command to
  268. # force a redraw.
  269. #
  270. # /awl restart
  271. # * restart the connection to the viewer script.
  272. # Viewer script
  273. # =============
  274. # When run from the command line, adv_windowlist acts as the viewer
  275. # script to be used together with the irssi script to display the
  276. # window list in a sidebar/terminal of its own.
  277. #
  278. # One optional parameter is accepted, the awl_path
  279. #
  280. # The viewer can be configured by three environment variables:
  281. #
  282. # AWL_HI9=1
  283. # * interpret %9 as high-intensity toggle instead of bold. This had
  284. # been the default prior to version 0.9b8
  285. #
  286. # AWL_AUTOFOCUS=0
  287. # * disable auto-focus behaviour when activating a window
  288. #
  289. # AWL_NOTITLE=1
  290. # * disable the title bar
  291. # Nei =^.^= ( anti@conference.jabber.teamidiot.de )
  292. no warnings 'redefine';
  293. use constant IN_IRSSI => __PACKAGE__ ne 'main' || $ENV{IRSSI_MOCK};
  294. use constant SCRIPT_FILE => __FILE__;
  295. no if !IN_IRSSI, strict => (qw(subs refs));
  296. use if IN_IRSSI, Irssi => ();
  297. use if IN_IRSSI, 'Irssi::TextUI' => ();
  298. use v5.10;
  299. use Encode;
  300. use Storable ();
  301. use IO::Socket::UNIX;
  302. use List::Util qw(min max reduce);
  303. use Hash::Util qw(lock_keys);
  304. use Text::ParseWords qw(shellwords);
  305. BEGIN {
  306. if ($] < 5.012) {
  307. *CORE::GLOBAL::length = *CORE::GLOBAL::length = sub (_) {
  308. defined $_[0] ? CORE::length($_[0]) : undef
  309. };
  310. }
  311. *Irssi::active_win = {}; # hide incorrect warning
  312. }
  313. unless (IN_IRSSI) {
  314. local *_ = \@ARGV;
  315. &AwlViewer::main;
  316. exit;
  317. }
  318. use constant GLOB_QUEUE_TIMER => 100;
  319. our $BLOCK_ALL; # localized blocker
  320. my @actString; # status bar texts
  321. my @win_items;
  322. my $currentLines = 0;
  323. my %awins;
  324. my $globTime; # timer to limit remake calls
  325. my %CHANGED;
  326. my $VIEWER_MODE;
  327. my $MOUSE_ON;
  328. my %mouse_coords;
  329. my %statusbars;
  330. my %S; # settings
  331. my $settings_str = '1';
  332. my $window_sort_func;
  333. my $custom_xform;
  334. my ($sb_base_width, $sb_base_width_pre, $sb_base_width_post);
  335. my $print_text_activity;
  336. my $shade_line_timer;
  337. my ($screenHeight, $screenWidth);
  338. my %viewer;
  339. my (%keymap, %nummap, %wnmap, %specialmap, %wnmap_exp, %custom_key_map);
  340. my %banned_channels;
  341. my %detach_map;
  342. my %abbrev_cache;
  343. use constant setc => 'awl';
  344. sub set ($) {
  345. setc . '_' . $_[0]
  346. }
  347. sub add_statusbar {
  348. for (@_) {
  349. # add subs
  350. my $l = set $_;
  351. {
  352. my $close = $_;
  353. no strict 'refs';
  354. *{$l} = sub { awl($close, @_) };
  355. }
  356. Irssi::command("^statusbar $l reset");
  357. Irssi::command("statusbar $l enable");
  358. if (lc $S{placement} eq 'top') {
  359. Irssi::command("statusbar $l placement top");
  360. }
  361. if (my $x = $S{position}) {
  362. Irssi::command("statusbar $l position $x");
  363. }
  364. Irssi::command("statusbar $l add -priority 100 -alignment left barstart");
  365. Irssi::command("statusbar $l add $l");
  366. Irssi::command("statusbar $l add -priority 100 -alignment right barend");
  367. Irssi::command("statusbar $l disable");
  368. Irssi::statusbar_item_register($l, '$0', $l);
  369. $statusbars{$_} = 1;
  370. Irssi::command("statusbar $l enable");
  371. }
  372. }
  373. sub remove_statusbar {
  374. for (@_) {
  375. my $l = set $_;
  376. Irssi::command("statusbar $l disable");
  377. Irssi::command("statusbar $l reset");
  378. Irssi::statusbar_item_unregister($l);
  379. {
  380. no strict 'refs';
  381. undef &{$l};
  382. }
  383. delete $statusbars{$_};
  384. }
  385. }
  386. my $awl_shared_empty = sub {
  387. return if $BLOCK_ALL;
  388. my ($item, $get_size_only) = @_;
  389. $item->default_handler($get_size_only, '', '', 0);
  390. };
  391. sub syncLines {
  392. my $maxLines = $S{maxlines};
  393. my $newLines = ($maxLines > 0 and @actString > $maxLines) ?
  394. $maxLines :
  395. ($maxLines < 0) ?
  396. -$maxLines :
  397. @actString;
  398. $currentLines = 1 if !$currentLines && $S{shared_sbar};
  399. if ($S{shared_sbar} && !$statusbars{shared}) {
  400. my $l = set 'shared';
  401. {
  402. no strict 'refs';
  403. *{$l} = sub {
  404. return if $BLOCK_ALL;
  405. my ($item, $get_size_only) = @_;
  406. my $text = $actString[0];
  407. my $title = _get_format(set 'title');
  408. if (length $title) {
  409. $title =~ s{\\(.)|(.)}{
  410. defined $2 ? quotemeta $2
  411. : $1 eq 'V' ? '\u'
  412. : $1 eq ':' ? quotemeta ':%n'
  413. : $1 =~ /^[uUFQE]$/ ? "\\$1"
  414. : quotemeta "\\$1"
  415. }sge;
  416. $title = eval qq{"$title"};
  417. $title .= ' ';
  418. }
  419. my $pat = defined $text ? "{sb $title\$*}" : '{sb }';
  420. $text //= '';
  421. $item->default_handler($get_size_only, $pat, $text, 0);
  422. };
  423. }
  424. $statusbars{shared} = 1;
  425. remove_statusbar (0) if $statusbars{0};
  426. }
  427. elsif ($statusbars{shared} && !$S{shared_sbar}) {
  428. add_statusbar (0) if $currentLines && $newLines;
  429. delete $statusbars{shared};
  430. my $l = set 'shared';
  431. {
  432. no strict 'refs';
  433. *{$l} = $awl_shared_empty;
  434. }
  435. }
  436. if ($currentLines == $newLines) { return; }
  437. elsif ($newLines > $currentLines) {
  438. add_statusbar ($currentLines .. ($newLines - 1));
  439. }
  440. else {
  441. remove_statusbar (reverse ($newLines .. ($currentLines - 1)));
  442. }
  443. $currentLines = $newLines;
  444. }
  445. sub awl {
  446. return if $BLOCK_ALL;
  447. my ($line, $item, $get_size_only) = @_;
  448. my $text = $actString[$line];
  449. my $pat = defined $text ? '{sb $*}' : '{sb }';
  450. $text //= '';
  451. $item->default_handler($get_size_only, $pat, $text, 0);
  452. }
  453. # remove old statusbars
  454. { my %killBar;
  455. sub get_old_status {
  456. my ($textDest, $cont, $cont_stripped) = @_;
  457. if ($textDest->{level} == 524288 and $textDest->{target} eq '' and !defined $textDest->{server}) {
  458. my $name = quotemeta(set '');
  459. if ($cont_stripped =~ m/^$name(\d+)\s/) { $killBar{$1} = 1; }
  460. Irssi::signal_stop;
  461. }
  462. }
  463. sub killOldStatus {
  464. %killBar = ();
  465. Irssi::signal_add_first('print text' => 'get_old_status');
  466. Irssi::command('statusbar');
  467. Irssi::signal_remove('print text' => 'get_old_status');
  468. remove_statusbar(keys %killBar);
  469. }
  470. }
  471. sub _add_map {
  472. my ($type, $target, $map) = @_;
  473. ($type->{$target}) = sort { length $a <=> length $b || $a cmp $b }
  474. $map, exists $type->{$target} ? $type->{$target} : ();
  475. }
  476. sub get_keymap {
  477. my ($textDest, undef, $cont_stripped) = @_;
  478. if ($textDest->{level} == 524288 and $textDest->{target} eq '' and !defined $textDest->{server}) {
  479. my $one_meta_or_ctrl_key = qr/((?:meta-)*?)(?:(meta-|\^)(\S)|(\w+))/;
  480. $cont_stripped = as_uni($cont_stripped);
  481. if ($cont_stripped =~ m/((?:$one_meta_or_ctrl_key-)*$one_meta_or_ctrl_key)\s+(.*)$/) {
  482. my ($combo, $command) = ($1, $10);
  483. my $map = '';
  484. while ($combo =~ s/(?:-|^)$one_meta_or_ctrl_key$//) {
  485. my ($level, $ctl, $key, $nkey) = ($1, $2, $3, $4);
  486. my $numlevel = ($level =~ y/-//);
  487. $ctl = '' if !$ctl || $ctl ne '^';
  488. $map = ('-' x ($numlevel%2)) . ('+' x ($numlevel/2)) .
  489. $ctl . (defined $key ? $key : "\01$nkey\01") . $map;
  490. }
  491. for ($command) {
  492. last unless length $map;
  493. if (/^change_window (\d+)/i) {
  494. _add_map(\%nummap, $1, $map);
  495. }
  496. elsif (/^(?:command window goto|change_window) (\S+)/i) {
  497. my $window = $1;
  498. if ($window !~ /\D/) {
  499. _add_map(\%nummap, $window, $map);
  500. }
  501. elsif (lc $window eq 'active') {
  502. _add_map(\%specialmap, '_active', $map);
  503. }
  504. else {
  505. _add_map(\%wnmap, $window, $map);
  506. }
  507. }
  508. elsif (/^(?:active_window|command ((awl )?ack))/i) {
  509. _add_map(\%specialmap, '_active', $map);
  510. $viewer{use_ack} = $1;
  511. }
  512. elsif (/^command window last/i) {
  513. _add_map(\%specialmap, '_last', $map);
  514. }
  515. elsif (/^(?:upper_window|command window up)/i) {
  516. _add_map(\%specialmap, '_up', $map);
  517. }
  518. elsif (/^(?:lower_window|command window down)/i) {
  519. _add_map(\%specialmap, '_down', $map);
  520. }
  521. elsif (/^key\s+(\w+)/i) {
  522. $custom_key_map{$1} = $map;
  523. }
  524. }
  525. }
  526. Irssi::signal_stop;
  527. }
  528. }
  529. sub update_keymap {
  530. %nummap = %wnmap = %specialmap = %custom_key_map = ();
  531. Irssi::signal_remove('command bind' => 'watch_keymap');
  532. Irssi::signal_add_first('print text' => 'get_keymap');
  533. Irssi::command('bind');
  534. Irssi::signal_remove('print text' => 'get_keymap');
  535. for (keys %custom_key_map) {
  536. if (exists $custom_key_map{$_} &&
  537. $custom_key_map{$_} =~ s/\01(\w+)\01/exists $custom_key_map{$1} ? $custom_key_map{$1} : "\02"/ge) {
  538. if ($custom_key_map{$_} =~ /\02/) {
  539. delete $custom_key_map{$_};
  540. }
  541. else {
  542. redo;
  543. }
  544. }
  545. }
  546. for my $keymap (\(%specialmap, %wnmap, %nummap)) {
  547. for (keys %$keymap) {
  548. if ($keymap->{$_} =~ s/\01(\w+)\01/exists $custom_key_map{$1} ? $custom_key_map{$1} : "\02"/ge) {
  549. if ($keymap->{$_} =~ /\02/) {
  550. delete $keymap->{$_};
  551. }
  552. }
  553. }
  554. }
  555. Irssi::signal_add('command bind' => 'watch_keymap');
  556. delete $viewer{client_keymap};
  557. &wl_changed;
  558. }
  559. # watch keymap changes
  560. sub watch_keymap {
  561. Irssi::timeout_add_once(1000, 'update_keymap', undef);
  562. }
  563. { my %strip_table = (
  564. # fe-common::core::formats.c:format_expand_styles
  565. # delete format_backs format_fores bold_fores other stuff
  566. (map { $_ => '' } (split //, '04261537' . 'kbgcrmyw' . 'KBGCRMYW' . 'U9_8I:|FnN>#[' . 'pP')),
  567. # escape
  568. (map { $_ => $_ } (split //, '{}%')),
  569. );
  570. sub ir_strip_codes { # strip %codes
  571. my $o = shift;
  572. $o =~ s/(%(%|Z.{6}|z.{6}|X..|x..|.))/exists $strip_table{$2} ? $strip_table{$2} :
  573. $2 =~ m{x(?:0[a-f]|[1-6][0-9a-z]|7[a-x])|z[0-9a-f]{6}}i ? '' : $1/gex;
  574. $o
  575. }
  576. }
  577. ## ir_parse_special -- wrapper around parse_special
  578. ## $i - input format
  579. ## $args - array ref of arguments to format
  580. ## $win - different target window (default current window)
  581. ## $flags - different kind of escape flags (default 4|8)
  582. ## returns formatted str
  583. sub ir_parse_special {
  584. my $o;
  585. my $i = shift;
  586. my $args = shift // [];
  587. y/ /\177/ for @$args; # hack to escape spaces
  588. my $win = shift || Irssi::active_win;
  589. my $flags = shift // 0x4|0x8;
  590. my @cmd_args = ($i, (join ' ', @$args), $flags);
  591. my $server = Irssi::active_server();
  592. if (ref $win and ref $win->{active}) {
  593. $o = $win->{active}->parse_special(@cmd_args);
  594. }
  595. elsif (ref $win and ref $win->{active_server}) {
  596. $o = $win->{active_server}->parse_special(@cmd_args);
  597. }
  598. elsif (ref $server) {
  599. $o = $server->parse_special(@cmd_args);
  600. }
  601. else {
  602. $o = &Irssi::parse_special(@cmd_args);
  603. }
  604. $o =~ y/\177/ /;
  605. $o
  606. }
  607. sub sb_format_expand { # Irssi::current_theme->format_expand wrapper
  608. Irssi::current_theme->format_expand(
  609. $_[0],
  610. (
  611. Irssi::EXPAND_FLAG_IGNORE_REPLACES
  612. |
  613. ($_[1] ? 0 : Irssi::EXPAND_FLAG_IGNORE_EMPTY)
  614. )
  615. )
  616. }
  617. { my $term_type = Irssi::version > 20040819 ? 'term_charset' : 'term_type';
  618. if (Irssi->can('string_width')) {
  619. *screen_length = sub { Irssi::string_width($_[0]) };
  620. }
  621. else {
  622. local $@;
  623. eval { require Text::CharWidth; };
  624. unless ($@) {
  625. *screen_length = sub { Text::CharWidth::mbswidth($_[0]) };
  626. }
  627. else {
  628. my $err = $@; chomp $err; $err =~ s/\sat .* line \d+\.$//;
  629. #Irssi::print("%_$IRSSI{name}: warning:%_ Text::CharWidth module failed to load. Length calculation may be off! Error was:");
  630. print "%_$IRSSI{name}:%_ $err";
  631. *screen_length = sub {
  632. my $temp = shift;
  633. if (lc Irssi::settings_get_str($term_type) eq 'utf-8') {
  634. Encode::_utf8_on($temp);
  635. }
  636. length($temp)
  637. };
  638. }
  639. }
  640. sub as_uni {
  641. no warnings 'utf8';
  642. Encode::decode(Irssi::settings_get_str($term_type), $_[0], 0)
  643. }
  644. sub as_tc {
  645. Encode::encode(Irssi::settings_get_str($term_type), $_[0], 0)
  646. }
  647. }
  648. sub sb_length {
  649. screen_length(ir_strip_codes($_[0]))
  650. }
  651. sub run_custom_xform {
  652. local $@;
  653. eval {
  654. $custom_xform->()
  655. };
  656. if ($@) {
  657. $@ =~ /^(.*)/;
  658. print '%_'.(set 'custom_xform').'%_ died (disabling): '.$1;
  659. $custom_xform = undef;
  660. }
  661. }
  662. sub remove_uniform {
  663. my $o = shift;
  664. $o =~ s/^xmpp:(.*?[%@]).+\.[^.]+$/$1/ or
  665. $o =~ s#^psyc://.+\.[^.]+/([@~].*)$#$1#;
  666. if ($custom_xform) {
  667. run_custom_xform() for $o;
  668. }
  669. $o
  670. }
  671. sub remove_uniform_vars {
  672. my $win = shift;
  673. my $name = __PACKAGE__ . '::custom_xform::' . $win->{active}{type}
  674. if ref $win->{active} && $win->{active}{type};
  675. no strict 'refs';
  676. local ${$name} = 1 if $name;
  677. remove_uniform(+shift);
  678. }
  679. sub lc1459 {
  680. my $x = shift;
  681. $x =~ y/][\\^/}{|~/;
  682. lc $x
  683. }
  684. sub window_list {
  685. my $i = 0;
  686. map { $_->[1] } sort $window_sort_func map { [ $i++, $_ ] } Irssi::windows;
  687. }
  688. sub _calculate_abbrev {
  689. my ($wins, $abbrevList) = @_;
  690. if ($S{fancy_abbrev} !~ /^(no|off|head)/i) {
  691. my @nameList = map { ref $_ ? remove_uniform_vars($_, as_uni($_->get_active_name) // '') : '' } @$wins;
  692. for (my $i = 0; $i < @nameList - 1; ++$i) {
  693. my ($x, $y) = ($nameList[$i], $nameList[$i + 1]);
  694. s/^[+#!=]// for $x, $y;
  695. my $res = exists $abbrev_cache{$x}{$y} ? $abbrev_cache{$x}{$y}
  696. : $abbrev_cache{$x}{$y} = string_LCSS($x, $y);
  697. if (defined $res) {
  698. for ($nameList[$i], $nameList[$i + 1]) {
  699. $abbrevList->{$_} //= int((index $_, $res) + (length $res) / 2);
  700. }
  701. }
  702. }
  703. }
  704. }
  705. my %act_last_line_shades = (
  706. r => [qw[ 50 40 30 20 ]],
  707. g => [qw[ 1O 1I 1C 16 ]],
  708. y => [qw[ 5O 4I 3C 26 ]],
  709. b => [qw[ 15 14 13 12 ]],
  710. m => [qw[ 54 43 32 21 ]],
  711. c => [qw[ 1S 1L 1E 17 ]],
  712. w => [qw[ 7W 7T 7Q 3E ]],
  713. K => [qw[ 7M 7K 27 7H ]],
  714. R => [qw[ 60 50 40 30 ]],
  715. G => [qw[ 1U 1O 1I 1C ]],
  716. Y => [qw[ 6U 5O 4I 3C ]],
  717. B => [qw[ 2B 2A 29 28 ]],
  718. M => [qw[ 65 54 43 32 ]],
  719. C => [qw[ 1Z 1S 1L 1E ]],
  720. W => [qw[ 6Z 5S 7R 7O ]],
  721. );
  722. sub _format_display {
  723. my (undef, $format, $cformat, $hilight, $name, $number, $key, $win) = @_;
  724. if ($print_text_activity && $S{line_shade}) {
  725. my @hilight_code = split /\177/, sb_format_expand("{$hilight \177}"), 2;
  726. my $max_time = max(1, log($S{line_shade}) - log(1000));
  727. my $time_delta = min(3, min($max_time, log(max(1, time - $win->{last_line}))) / $max_time * 3);
  728. if ($hilight_code[0] =~ /%(.)/ && exists $act_last_line_shades{$1}) {
  729. $hilight = 'sb_act_hilight_color %X'.$act_last_line_shades{$1}[$time_delta];
  730. }
  731. }
  732. $cformat = '$0' unless length $cformat;
  733. my %map = ('$C' => $cformat, '$N' => '$1', '$Q' => '$2');
  734. $format =~ s<(\$.)><$map{$1}//$1>ge;
  735. $format =~ s<\$H((?:\$.|[^\$])*?)\$S><{$hilight $1%n}>g;
  736. my @ret = ir_parse_special(sb_format_expand($format), [$name, $number, $key], $win);
  737. @ret
  738. }
  739. sub _get_format {
  740. Irssi::current_theme->get_format(__PACKAGE__, @_)
  741. }
  742. sub _is_detached {
  743. my ($win, $active_number) = @_;
  744. my $level = $win->{data_level} // 0;
  745. my $number = $win->{refnum};
  746. my $name = lc1459( as_uni($win->{name}) );
  747. my $active = lc1459( as_uni($win->get_active_name) // '' );
  748. my $tag = $win->{active} && $win->{active}{server} ? lc1459( as_uni($win->{active}{server}{tag}) // '' ) : '';
  749. my @cond = ($number);
  750. push @cond, "$name" if length $name;
  751. push @cond, "$tag/$active" if length $tag && length $active;
  752. push @cond, "$active" if length $active;
  753. push @cond, "$tag/*", "$tag/::all" if length $tag;
  754. push @cond, "*", "::all";
  755. for my $cond (@cond) {
  756. if (exists $detach_map{ $cond }) {
  757. my $dd = $detach_map{ $cond } // $S{detach_data};
  758. return $win->{data_level} < abs $dd
  759. && ($number != $active_number || 0 <= $dd);
  760. }
  761. }
  762. return;
  763. }
  764. sub _calculate_items {
  765. my ($wins, $abbrevList) = @_;
  766. my $display_header = _get_format(set 'display_header');
  767. my $name_format = _get_format(set 'name_display');
  768. my $abbrev_chars = as_uni(_get_format(set 'abbrev_chars'));
  769. my %displays;
  770. my $active = Irssi::active_win;
  771. @win_items = ();
  772. %keymap = (%nummap, %wnmap_exp);
  773. my ($numPad, $keyPad) = (0, 0);
  774. if ($VIEWER_MODE or $S{block} < 0) {
  775. $numPad = length((sort { length $b <=> length $a } keys %keymap)[0]) // 0;
  776. $keyPad = length((sort { length $b <=> length $a } values %keymap)[0]) // 0;
  777. }
  778. my $last_net;
  779. my ($abbrev1, $abbrev2) = $abbrev_chars =~ /(\X)(.*)/;
  780. my @abbrev_chars = ('~', "\x{301c}");
  781. unless (defined $abbrev1 && screen_length(as_tc($abbrev1)) == 1) { $abbrev1 = $abbrev_chars[0] }
  782. unless (length $abbrev2) {
  783. $abbrev2 = $abbrev1;
  784. if ($abbrev1 eq $abbrev_chars[0]) {
  785. $abbrev2 = $abbrev_chars[1];
  786. }
  787. else {
  788. $abbrev2 = $abbrev1;
  789. }
  790. }
  791. if (screen_length(as_tc($abbrev2)) == 1) {
  792. $abbrev2 x= 2;
  793. }
  794. while (screen_length(as_tc($abbrev2)) > 2) {
  795. chop $abbrev2;
  796. }
  797. unless (screen_length(as_tc($abbrev2)) == 2) {
  798. $abbrev2 = $abbrev_chars[1];
  799. }
  800. for my $win (@$wins) {
  801. my $global_tag_header_mode;
  802. next unless ref $win;
  803. my $backup_win = Storable::dclone($win);
  804. delete $backup_win->{active} unless ref $backup_win->{active};
  805. $global_tag_header_mode =
  806. $display_header && ($last_net // '') ne ($backup_win->{active}{server}{tag} // '');
  807. if ($win->{data_level} < abs $S{hide_data}
  808. && ($win->{refnum} != $active->{refnum} || 0 <= $S{hide_data})) {
  809. next; }
  810. elsif (exists $awins{$win->{refnum}} && $S{hide_empty} && !$win->items
  811. && ($win->{refnum} != $active->{refnum} || 0 <= $S{hide_empty})) {
  812. next; }
  813. elsif (_is_detached($win, $active->{refnum})) {
  814. next; }
  815. my $colour = $win->{hilight_color} // '';
  816. my $hilight = do {
  817. if ($win->{data_level} == 0) { 'sb_act_none'; }
  818. elsif ($win->{data_level} == 1) { 'sb_act_text'; }
  819. elsif ($win->{data_level} == 2) { 'sb_act_msg'; }
  820. elsif ($colour ne '') { "sb_act_hilight_color $colour"; }
  821. elsif ($win->{data_level} == 3) { 'sb_act_hilight'; }
  822. else { 'sb_act_special'; }
  823. };
  824. my $number = $win->{refnum};
  825. my ($name, $display, $cdisplay);
  826. if ($global_tag_header_mode) {
  827. $display = $display_header;
  828. $name = as_uni($backup_win->{active}{server}{tag}) // '';
  829. if ($custom_xform) {
  830. no strict 'refs';
  831. local ${ __PACKAGE__ . '::custom_xform::TAG' } = 1;
  832. run_custom_xform() for $name;
  833. }
  834. }
  835. else {
  836. my @display = ('display_nokey');
  837. if (defined $keymap{$number} and $keymap{$number} ne '') {
  838. unshift @display, map { (my $cpy = $_) =~ s/_no/_/; $cpy } @display;
  839. }
  840. if (exists $awins{$number}) {
  841. unshift @display, map { my $cpy = $_; $cpy .= '_visible'; $cpy } @display;
  842. }
  843. if ($active->{refnum} == $number) {
  844. unshift @display, map { my $cpy = $_; $cpy .= '_active'; $cpy }
  845. grep { !/_visible$/ } @display;
  846. }
  847. $display = (grep { length $_ }
  848. map { $displays{$_} //= _get_format(set $_) }
  849. @display)[0];
  850. $cdisplay = $name_format;
  851. $name = as_uni($win->get_active_name) // '';
  852. $name = '*' if $S{banned_on} and exists $banned_channels{lc1459($name)};
  853. $name = remove_uniform_vars($win, $name) if $name ne '*';
  854. if ($name ne '*' and $win->{name} ne '' and $S{prefer_name}) {
  855. $name = as_uni($win->{name});
  856. if ($custom_xform) {
  857. no strict 'refs';
  858. local ${ __PACKAGE__ . '::custom_xform::NAME' } = 1;
  859. run_custom_xform() for $name;
  860. }
  861. }
  862. if (!$VIEWER_MODE && $S{block} >= 0 && $S{hide_name}
  863. && $win->{data_level} < abs $S{hide_name}
  864. && ($win->{refnum} != $active->{refnum} || 0 <= $S{hide_name})) {
  865. $name = '';
  866. $cdisplay = '';
  867. }
  868. }
  869. $display = "$display%n";
  870. my $num_ent = (' 'x max(0,$numPad - length $number)) . $number;
  871. my $key_ent = exists $keymap{$number} ? ((' 'x max(0,$keyPad - length $keymap{$number})) . $keymap{$number}) : ' 'x$keyPad;
  872. if ($VIEWER_MODE or $S{sbar_maxlen} or $S{block} < 0) {
  873. my $baseLength = sb_length(_format_display(
  874. '', $display, $cdisplay, $hilight,
  875. 'x', # placeholder
  876. $num_ent,
  877. $key_ent,
  878. $win)) - 1;
  879. my $diff = (abs $S{block}) - (screen_length(as_tc($name)) + $baseLength);
  880. if ($diff < 0) { # too long
  881. my $screen_length = screen_length(as_tc($name));
  882. if ((abs $diff) >= $screen_length) { $name = '' } # forget it
  883. elsif ((abs $diff) + screen_length(as_tc(substr($name, 0, 1))) >= $screen_length) { $name = substr($name, 0, 1); }
  884. else {
  885. my $ulen = length $name;
  886. my $middle2 = exists $abbrevList->{$name} ?
  887. ($S{fancy_strict}) ?
  888. 2* $abbrevList->{$name} :
  889. (2*($abbrevList->{$name} + $ulen) / 3) :
  890. ($S{fancy_head}) ?
  891. 2*$ulen :
  892. $ulen;
  893. my $first = 1;
  894. while (length $name > 1) {
  895. my $cp = $middle2 >= 0 ? $middle2/2 : -1; # clearing position
  896. my $rm = 2;
  897. # if character at end is wider than 1 cell -> replace it with ~
  898. if (screen_length(as_tc(substr $name, $cp, 1)) > 1) {
  899. if ($first || $cp < 0) {
  900. $rm = 1;
  901. $first = undef;
  902. }
  903. }
  904. elsif ($cp < 0) { # elsif at end -> replace last 2 characters
  905. --$cp;
  906. }
  907. (substr $name, $cp, $rm) = $abbrev1;
  908. if ($cp > -1 && $rm > 1) {
  909. --$middle2;
  910. }
  911. my $sl = screen_length(as_tc($name));
  912. if ($sl + $baseLength < abs $S{block}) {
  913. (substr $name, ($middle2+1)/2, 1) = $abbrev2;
  914. last;
  915. }
  916. elsif ($sl + $baseLength == abs $S{block}) {
  917. last;
  918. }
  919. }
  920. }
  921. }
  922. elsif ($VIEWER_MODE or $S{block} < 0) {
  923. $name .= (' ' x $diff);
  924. }
  925. }
  926. push @win_items, _format_display(
  927. '', $display, $cdisplay, $hilight,
  928. as_tc($name),
  929. $num_ent,
  930. as_tc($key_ent),
  931. $win);
  932. if ($global_tag_header_mode) {
  933. $last_net = $backup_win->{active}{server}{tag};
  934. redo;
  935. }
  936. $mouse_coords{refnum}{$#win_items} = $number;
  937. }
  938. }
  939. sub _spread_items {
  940. my $width = $screenWidth - $sb_base_width - 1;
  941. my @separator = _get_format(set 'separator');
  942. if ($S{block} >= 0) {
  943. my $sep2 = _get_format(set 'separator2');
  944. push @separator, $sep2 if length $sep2 && $sep2 ne $separator[0];
  945. }
  946. $separator[0] .= '%n';
  947. my @sepLen = map { sb_length($_) } @separator;
  948. @actString = ();
  949. my $curLine;
  950. my $curLen = 0;
  951. if ($S{shared_sbar}) {
  952. $curLen += $S{shared_sbar}[0] + 2;
  953. $width -= $S{shared_sbar}[2];
  954. }
  955. my $mouse_header_check = 0;
  956. for my $it (@win_items) {
  957. my $itemLen = sb_length($it);
  958. if ($curLen) {
  959. if ($curLen + $itemLen + $sepLen[$mouse_header_check % @sepLen] > $width) {
  960. $width += $S{shared_sbar}[2]
  961. if !@actString && $S{shared_sbar};
  962. push @actString, $curLine;
  963. $curLine = undef;
  964. $curLen = 0;
  965. }
  966. elsif (defined $curLine) {
  967. $curLine .= $separator[$mouse_header_check % @separator];
  968. $curLen += $sepLen[$mouse_header_check % @sepLen];
  969. }
  970. }
  971. $curLine .= $it;
  972. if (exists $mouse_coords{refnum}{$mouse_header_check}) {
  973. $mouse_coords{scalar @actString}{ $_ } = $mouse_coords{refnum}{$mouse_header_check}
  974. for $curLen .. $curLen + $itemLen - 1;
  975. }
  976. $curLen += $itemLen;
  977. }
  978. continue {
  979. ++$mouse_header_check;
  980. }
  981. $curLen -= $S{shared_sbar}[0]
  982. if !@actString && $S{shared_sbar};
  983. push @actString, $curLine if $curLen;
  984. }
  985. sub remake {
  986. my %abbrevList;
  987. my @wins = window_list();
  988. if ($VIEWER_MODE or $S{sbar_maxlen} or $S{block} < 0) {
  989. _calculate_abbrev(\@wins, \%abbrevList);
  990. }
  991. %mouse_coords = ( refnum => +{} );
  992. _calculate_items(\@wins, \%abbrevList);
  993. unless ($VIEWER_MODE) {
  994. _spread_items();
  995. push @actString, undef unless @actString || $S{all_disable};
  996. }
  997. }
  998. sub update_wl {
  999. return if $BLOCK_ALL;
  1000. remake();
  1001. Irssi::statusbar_items_redraw(set $_) for keys %statusbars;
  1002. unless ($VIEWER_MODE) {
  1003. Irssi::timeout_add_once(100, 'syncLines', undef);
  1004. }
  1005. else {
  1006. syncViewer();
  1007. }
  1008. }
  1009. sub screenFullRedraw {
  1010. my ($window) = @_;
  1011. if (!ref $window or $window->{refnum} == Irssi::active_win->{refnum}) {
  1012. $viewer{fullRedraw} = 1 if $viewer{client};
  1013. $settings_str = '';
  1014. &setup_changed;
  1015. }
  1016. }
  1017. sub restartViewerServer {
  1018. if ($VIEWER_MODE) {
  1019. stop_viewer();
  1020. start_viewer();
  1021. }
  1022. }
  1023. sub _simple_quote {
  1024. my @r = map {
  1025. my $x = $_;
  1026. $x =~ s/'/'"'"'/g;
  1027. $x = "'$x'";
  1028. } @_;
  1029. wantarray ? @r : shift @r
  1030. }
  1031. sub _viewer_command_replace_format {
  1032. my ($ecmd, @args) = @_;
  1033. my $file = _simple_quote(SCRIPT_FILE());
  1034. my $path = _simple_quote($viewer{path});
  1035. my @env;
  1036. for my $env (shellwords($S{viewer_launch_env})) {
  1037. if ($env =~ /^(\w+)(?:=(.*))$/) {
  1038. push @env, "AWL_$1=$2"
  1039. }
  1040. }
  1041. my $cmd = join ' ',
  1042. (@env ? ('env', _simple_quote(@env)) : ()),
  1043. 'perl', $file, '-1', _simple_quote(@args), $path;
  1044. $ecmd =~ s{%(%|\w+)}{
  1045. my $sub = $1;
  1046. if ($sub eq '%') {
  1047. '%'
  1048. }
  1049. elsif ($sub =~ /^(q*)A(.*)/) {
  1050. my $ret = $cmd;
  1051. for (1..length $1) {
  1052. $ret = _simple_quote($ret);
  1053. }
  1054. "$ret$2"
  1055. }
  1056. else {
  1057. "%$sub"
  1058. }
  1059. }gex;
  1060. $ecmd
  1061. }
  1062. sub start_viewer {
  1063. unlink $viewer{path} if -S $viewer{path} || -p _;
  1064. $viewer{server} = IO::Socket::UNIX->new(
  1065. Type => SOCK_STREAM,
  1066. Local => $viewer{path},
  1067. Listen => 1
  1068. );
  1069. unless ($viewer{server}) {
  1070. $viewer{msg} = "Viewer: $!";
  1071. $viewer{retry} = Irssi::timeout_add_once(5000, 'retry_viewer', 1);
  1072. return;
  1073. }
  1074. $viewer{server}->blocking(0);
  1075. set_viewer_mode_hint();
  1076. $viewer{server_tag} = Irssi::input_add($viewer{server}->fileno, INPUT_READ, 'vi_connected', undef);
  1077. if ($S{viewer_launch}) {
  1078. if (length $ENV{TMUX_PANE} && length $ENV{TMUX} && lc $S{viewer_tmux_position} ne 'custom') {
  1079. my $cmd = _viewer_command_replace_format('%qA', '-p', lc $S{viewer_tmux_position});
  1080. Irssi::command("exec - tmux neww -d $cmd 2>&1 &");
  1081. }
  1082. elsif (length $ENV{WINDOWID} && length $ENV{DISPLAY} && length $S{viewer_xwin_command} && $S{viewer_xwin_command} =~ /\S/) {
  1083. my $cmd = _viewer_command_replace_format($S{viewer_xwin_command});
  1084. Irssi::command("exec - $cmd 2>&1 &");
  1085. }
  1086. elsif (length $S{viewer_custom_command} && $S{viewer_custom_command} =~ /\S/) {
  1087. my $cmd = _viewer_command_replace_format($S{viewer_custom_command});
  1088. Irssi::command("exec - $cmd 2>&1 &");
  1089. }
  1090. }
  1091. }
  1092. sub set_viewer_mode_hint {
  1093. return unless $viewer{server};
  1094. if ($S{no_mode_hint}) {
  1095. $viewer{msg} = undef;
  1096. }
  1097. else {
  1098. my ($name) = __PACKAGE__ =~ /::([^:]+)$/;
  1099. $viewer{msg} = "Run $name from the shell or switch to sbar mode";
  1100. }
  1101. }
  1102. sub retry_viewer {
  1103. start_viewer();
  1104. }
  1105. sub vi_close_client {
  1106. Irssi::input_remove(delete $viewer{client_tag}) if exists $viewer{client_tag};
  1107. $viewer{client}->close if $viewer{client};
  1108. delete $viewer{client};
  1109. delete $viewer{client_keymap};
  1110. delete $viewer{client_settings};
  1111. delete $viewer{client_env};
  1112. delete $viewer{fullRedraw};
  1113. }
  1114. sub vi_connected {
  1115. vi_close_client();
  1116. $viewer{client} = $viewer{server}->accept or return;
  1117. $viewer{client}->blocking(0);
  1118. $viewer{client_tag} = Irssi::input_add($viewer{client}->fileno, INPUT_READ, 'vi_clientinput', undef);
  1119. syncViewer();
  1120. }
  1121. use constant VIEWER_BLOCK_SIZE => 1024;
  1122. sub vi_clientinput {
  1123. if ($viewer{client}->read(my $buf, VIEWER_BLOCK_SIZE)) {
  1124. $viewer{rcvbuf} .= $buf;
  1125. if ($viewer{rcvbuf} =~ s/^(?:(active|\d+)|(last|up|down))\n//igm) {
  1126. if (defined $2) {
  1127. Irssi::command("window $2");
  1128. }
  1129. elsif (lc $1 eq 'active' && $viewer{use_ack}) {
  1130. Irssi::command($viewer{use_ack});
  1131. }
  1132. else {
  1133. Irssi::command("window goto $1");
  1134. }
  1135. }
  1136. }
  1137. else {
  1138. vi_close_client();
  1139. Irssi::timeout_add_once(100, 'syncViewer', undef);
  1140. }
  1141. }
  1142. sub stop_viewer {
  1143. Irssi::timeout_remove(delete $viewer{retry}) if exists $viewer{retry};
  1144. vi_close_client();
  1145. Irssi::input_remove(delete $viewer{server_tag}) if exists $viewer{server_tag};
  1146. return unless $viewer{server};
  1147. $viewer{server}->close;
  1148. delete $viewer{server};
  1149. }
  1150. sub _encode_var {
  1151. my $str;
  1152. while (@_) {
  1153. my ($name, $var) = splice @_, 0, 2;
  1154. my $type = ref $var ? $var =~ /HASH/ ? 'map' : $var =~ /ARRAY/ ? 'list' : '' : '';
  1155. $str .= "\n\U$name$type\_begin\n";
  1156. if ($type eq 'map') {
  1157. no warnings 'numeric';
  1158. $str .= " $_\n ${$var}{$_}\n" for sort { $a <=> $b || $a cmp $b } keys %$var;
  1159. }
  1160. elsif ($type eq 'list') {
  1161. $str .= " $_\n" for @$var;
  1162. }
  1163. else {
  1164. $str .= " $var\n";
  1165. }
  1166. $str .= "\U$name$type\_end\n";
  1167. }
  1168. $str
  1169. }
  1170. sub syncViewer {
  1171. if ($viewer{client}) {
  1172. @actString = ();
  1173. if ($currentLines) {
  1174. killOldStatus();
  1175. $currentLines = 0;
  1176. }
  1177. my $str;
  1178. unless ($viewer{client_keymap}) {
  1179. $str .= _encode_var('key', +{ %nummap, %specialmap });
  1180. $viewer{client_keymap} = 1;
  1181. }
  1182. unless ($viewer{client_settings}) {
  1183. $str .= _encode_var(
  1184. block => $S{block},
  1185. ha => $S{height_adjust},
  1186. mc => $S{maxcolumns},
  1187. ml => $S{maxlines},
  1188. );
  1189. $viewer{client_settings} = 1;
  1190. }
  1191. unless ($viewer{client_env}) {
  1192. $str .= _encode_var(irssienv => +{
  1193. length $ENV{TMUX_PANE} && length $ENV{TMUX} ?
  1194. (tmux_pane => $ENV{TMUX_PANE},
  1195. tmux_srv => $ENV{TMUX}) : (),
  1196. length $ENV{WINDOWID} ?
  1197. (xwinid => $ENV{WINDOWID}) : (),
  1198. });
  1199. $viewer{client_env} = 1;
  1200. }
  1201. my $separator = _get_format(set 'separator');
  1202. my $sepLen = sb_length($separator);
  1203. my $item_bg = _get_format(set 'viewer_item_bg');
  1204. my $title = _get_format(set 'title');
  1205. if (length $title) {
  1206. $title =~ s{\\(.)|(.)}{
  1207. defined $2 ? quotemeta $2
  1208. : $1 eq 'V' ? '\U'
  1209. : $1 eq ':' ? quotemeta '%N'
  1210. : $1 =~ /^[uUFQE]$/ ? "\\$1"
  1211. : quotemeta "\\$1"
  1212. }sge;
  1213. $title = eval qq{"$title"};
  1214. }
  1215. $str .= _encode_var(redraw => 1) if delete $viewer{fullRedraw};
  1216. $str .= _encode_var(separator => $separator,
  1217. seplen => $sepLen,
  1218. itembg => $item_bg,
  1219. title => $title,
  1220. mouse => $mouse_coords{refnum},
  1221. key2 => \%wnmap_exp,
  1222. win => \@win_items);
  1223. my $was = $viewer{client}->blocking(1);
  1224. $viewer{client}->print($str);
  1225. $viewer{client}->blocking($was);
  1226. }
  1227. elsif ($viewer{server}) {
  1228. if (defined $viewer{msg}) {
  1229. @actString = ((uc setc()).": $viewer{msg}");
  1230. }
  1231. else {
  1232. @actString = ();
  1233. }
  1234. }
  1235. elsif (defined $viewer{msg}) {
  1236. @actString = ((uc setc()).": $viewer{msg}");
  1237. }
  1238. if (@actString) {
  1239. Irssi::timeout_add_once(100, 'syncLines', undef);
  1240. }
  1241. elsif ($currentLines) {
  1242. killOldStatus();
  1243. $currentLines = 0;
  1244. }
  1245. }
  1246. sub reset_awl {
  1247. Irssi::timeout_remove($shade_line_timer) if $shade_line_timer; $shade_line_timer = undef;
  1248. my $was_sort = $S{sort} // '';
  1249. my $was_xform = $S{xform} // '';
  1250. my $was_shared = $S{shared_sbar};
  1251. my $was_no_hint = $S{no_mode_hint};
  1252. %S = (
  1253. sort => Irssi::settings_get_str( set 'sort'),
  1254. fancy_abbrev => Irssi::settings_get_str('fancy_abbrev'),
  1255. xform => Irssi::settings_get_str( set 'custom_xform'),
  1256. block => Irssi::settings_get_int( set 'block'),
  1257. banned_on => Irssi::settings_get_bool('banned_channels_on'),
  1258. prefer_name => Irssi::settings_get_bool(set 'prefer_name'),
  1259. hide_data => Irssi::settings_get_int( set 'hide_data'),
  1260. hide_name => Irssi::settings_get_int( set 'hide_name_data'),
  1261. hide_empty => Irssi::settings_get_int( set 'hide_empty'),
  1262. detach => Irssi::settings_get_str( set 'detach'),
  1263. detach_data => Irssi::settings_get_int( set 'detach_data'),
  1264. detach_aht => Irssi::settings_get_bool(set 'detach_aht'),
  1265. sbar_maxlen => Irssi::settings_get_bool(set 'sbar_maxlength'),
  1266. placement => Irssi::settings_get_str( set 'placement'),
  1267. position => Irssi::settings_get_int( set 'position'),
  1268. maxlines => Irssi::settings_get_int( set 'maxlines'),
  1269. maxcolumns => Irssi::settings_get_int( set 'maxcolumns'),
  1270. all_disable => Irssi::settings_get_bool(set 'all_disable'),
  1271. height_adjust => Irssi::settings_get_int( set 'height_adjust'),
  1272. mouse_offset => Irssi::settings_get_int( set 'mouse_offset'),
  1273. mouse_scroll => Irssi::settings_get_int( 'mouse_scroll'),
  1274. mouse_escape => Irssi::settings_get_int( 'mouse_escape'),
  1275. line_shade => Irssi::settings_get_time(set 'last_line_shade'),
  1276. no_mode_hint => Irssi::settings_get_bool(set 'no_mode_hint'),
  1277. viewer_launch => Irssi::settings_get_bool(set 'viewer_launch'),
  1278. viewer_launch_env => Irssi::settings_get_str(set 'viewer_launch_env'),
  1279. viewer_xwin_command => Irssi::settings_get_str(set 'viewer_xwin_command'),
  1280. viewer_custom_command => Irssi::settings_get_str(set 'viewer_custom_command'),
  1281. viewer_tmux_position => Irssi::settings_get_str(set 'viewer_tmux_position'),
  1282. );
  1283. $S{fancy_strict} = $S{fancy_abbrev} =~ /^strict/i;
  1284. $S{fancy_head} = $S{fancy_abbrev} =~ /^head/i;
  1285. my $shared = Irssi::settings_get_str(set 'shared_sbar');
  1286. if ($shared =~ /^(\d+)([<])(\d+)$/) {
  1287. $S{shared_sbar} = [$1, $2, $3];
  1288. }
  1289. else {
  1290. Irssi::settings_set_str(set 'shared_sbar', 'OFF');
  1291. $S{shared_sbar} = undef;
  1292. }
  1293. lock_keys(%S);
  1294. if ($was_sort ne $S{sort}) {
  1295. $print_text_activity = undef;
  1296. my @sort_order = grep { @$_ > 4 } map {
  1297. s/^\s*//;
  1298. my $reverse = s/^\W*\K[-!]//;
  1299. my $undef_check = s/^\W*\K~// ? 1 : undef;
  1300. my $equal_check = s/=(.*)\s?$// ? $1 : undef;
  1301. s/\s*$//;
  1302. my $ignore_case = s/#i$// ? 1 : undef;
  1303. $print_text_activity = 1 if $_ eq 'last_line';
  1304. my @path = split '/';
  1305. my $class_check = @path && $path[-1] =~ s/(::.*)$// ? $1 : undef;
  1306. my $lru = "@path" eq 'lru';
  1307. [ $reverse ? -1 : 1, $undef_check, $equal_check, $class_check, $ignore_case, $lru, @path ]
  1308. } "$S{sort}," =~ /([^+,]*|[^+,]*=[^,]*?\s(?=\+)|[^+,]*=[^,]*)[+,]/g;
  1309. $window_sort_func = sub {
  1310. no warnings qw(numeric uninitialized);
  1311. for my $so (@sort_order) {
  1312. my @x = map {
  1313. my $ret = 0;
  1314. $_ = lc1459($_) if defined $_ && !ref $_ && $so->[4];
  1315. $ret = $_ eq ($so->[4] ? lc1459($so->[2]) : $so->[2]) ? 1 : -1 if defined $so->[2];
  1316. $ret = defined $_ ? ($ret || -3) : 3 if $so->[1];
  1317. $ret = ref $_ && $_->isa('Irssi'.$so->[3]) ? 2 : ($ret || -2) if $so->[3];
  1318. -$ret || $_
  1319. }
  1320. map {
  1321. $so->[5] ? $_->[0] : reduce { return unless ref $a; $a->{$b} } $_->[1], @{$so}[6..$#$so]
  1322. } $a, $b;
  1323. return ((($x[0] <=> $x[1] || $x[0] cmp $x[1]) * $so->[0]) || next);
  1324. }
  1325. return ($a->[1]{refnum} <=> $b->[1]{refnum});
  1326. };
  1327. }
  1328. if ($was_xform ne $S{xform}) {
  1329. if ($S{xform} !~ /\S/) {
  1330. $custom_xform = undef;
  1331. }
  1332. else {
  1333. my $script_pkg = __PACKAGE__ . '::custom_xform';
  1334. local $@;
  1335. $custom_xform = eval qq{
  1336. package $script_pkg;
  1337. use strict;
  1338. no warnings;
  1339. our (\$QUERY, \$CHANNEL, \$TAG, \$NAME);
  1340. return sub {
  1341. # line 1 @{[ set 'custom_xform' ]}\n$S{xform}\n}};
  1342. if ($@) {
  1343. $@ =~ /^(.*)/;
  1344. print '%_'.(set 'custom_xform').'%_ did not compile: '.$1;
  1345. }
  1346. }
  1347. }
  1348. my $new_settings = join "\n", $VIEWER_MODE
  1349. ? ("\\", $S{block}, $S{height_adjust}, $S{maxlines}, $S{maxcolumns})
  1350. : ("!", $S{placement}, $S{position});
  1351. my $first_viewer = $settings_str eq '1';
  1352. if ($settings_str ne $new_settings) {
  1353. @actString = ();
  1354. %abbrev_cache = ();
  1355. $currentLines = 0;
  1356. killOldStatus();
  1357. delete $viewer{client_settings};
  1358. $settings_str = $new_settings;
  1359. }
  1360. my $was_mouse_mode = $MOUSE_ON;
  1361. if ($MOUSE_ON = Irssi::settings_get_bool(set 'mouse') and !$was_mouse_mode) {
  1362. install_mouse();
  1363. }
  1364. elsif ($was_mouse_mode and !$MOUSE_ON) {
  1365. uninstall_mouse();
  1366. }
  1367. unless ($first_viewer) {
  1368. my $path = Irssi::settings_get_str(set 'path');
  1369. my $was_viewer_mode = $VIEWER_MODE;
  1370. if ($was_viewer_mode &&
  1371. defined $viewer{path} && $viewer{path} ne $path) {
  1372. stop_viewer();
  1373. $was_viewer_mode = 0;
  1374. }
  1375. elsif ($was_viewer_mode && $S{no_mode_hint} != $was_no_hint + 0) {
  1376. set_viewer_mode_hint();
  1377. }
  1378. $viewer{path} = $path;
  1379. if ($VIEWER_MODE = Irssi::settings_get_bool(set 'viewer') and !$was_viewer_mode) {
  1380. start_viewer();
  1381. }
  1382. elsif ($was_viewer_mode and !$VIEWER_MODE) {
  1383. stop_viewer();
  1384. }
  1385. }
  1386. %banned_channels = map { lc1459(as_uni($_)) => undef }
  1387. split ' ', Irssi::settings_get_str('banned_channels');
  1388. %detach_map = ($S{detach_aht}
  1389. ? (map { ( lc1459(as_uni($_)) => undef ) }
  1390. split ' ', Irssi::settings_get_str('activity_hide_targets')) : (),
  1391. (map { my ($k, $v) = (split /(?:,(-?\d+))$/, $_)[0, 1];
  1392. ( lc1459(as_uni($k)) => $v ) }
  1393. split ' ', $S{detach}));
  1394. my @sb_base = split /\177/, sb_format_expand("{sbstart}{sb \177}{sbend}"), 2;
  1395. $sb_base_width_pre = sb_length($sb_base[0]);
  1396. $sb_base_width_post = max 0, sb_length($sb_base[1])-1;
  1397. $sb_base_width = $sb_base_width_pre + $sb_base_width_post;
  1398. if ($print_text_activity && $S{line_shade}) {
  1399. $shade_line_timer = Irssi::timeout_add(max(10 * GLOB_QUEUE_TIMER, 100*$S{line_shade}**(1/3)), 'wl_changed', undef);
  1400. }
  1401. $CHANGED{AWINS} = 1;
  1402. }
  1403. sub hide_window {
  1404. my ($data) = @_;
  1405. my $ent;
  1406. $data =~ s/\s*$//;
  1407. my $win = Irssi::active_win;
  1408. my $number = $win->{refnum};
  1409. my $name = as_uni($win->{name});
  1410. my $active = as_uni($win->get_active_name) // '';
  1411. my $tag = $win->{active} && $win->{active}{server} ? as_uni($win->{active}{server}{tag}) // '' : '';
  1412. if (length $name) {
  1413. $ent = "$name";
  1414. }
  1415. elsif (length $tag && length $active) {
  1416. $ent = "$tag/$active";
  1417. }
  1418. else {
  1419. $ent = "$number";
  1420. }
  1421. my $found = 0;
  1422. my @setting;
  1423. for my $s (split ' ', $S{detach}) {
  1424. my ($k, $v) = (split /(?:,(-?\d+))$/, $s)[0, 1];
  1425. if (lc1459(as_uni($k)) eq lc1459($ent)) {
  1426. unless ($found) {
  1427. if ($data =~ /^(-?\d+)$/) {
  1428. $ent .= ",$1";
  1429. }
  1430. if (defined $v && 0 == abs $v) {
  1431. $win->print("Hiding window $ent");
  1432. }
  1433. push @setting, as_tc($ent);
  1434. $found = 1;
  1435. }
  1436. }
  1437. else {
  1438. push @setting, defined $v ? "$k,$v" : $k;
  1439. }
  1440. }
  1441. unless ($found) {
  1442. $win->print("Hiding window $ent");
  1443. if ($data =~ /^(-?\d+)$/) {
  1444. $ent .= ",$1";
  1445. }
  1446. push @setting, as_tc($ent);
  1447. }
  1448. if (@setting) {
  1449. Irssi::command("^set ".(set 'detach')." @setting");
  1450. } else {
  1451. Irssi::command("^set -clear ".(set 'detach'));
  1452. }
  1453. }
  1454. sub unhide_window {
  1455. my ($data, $server, $witem) = @_;
  1456. my $win = Irssi::active_win;
  1457. my $number = $win->{refnum};
  1458. my $name = as_uni($win->{name});
  1459. my $active = as_uni($win->get_active_name) // '';
  1460. my $tag = $win->{active} && $win->{active}{server} ? as_uni($win->{active}{server}{tag}) // '' : '';
  1461. my %detach_aht;
  1462. if ($S{detach_aht}) {
  1463. %detach_aht = (map { ( lc1459(as_uni($_)) => undef ) }
  1464. split ' ', Irssi::settings_get_str('activity_hide_targets'));
  1465. }
  1466. my @setting;
  1467. my @kills = (length $name ? $name : undef,
  1468. length $tag && length $active ? "$tag/$active" : undef,
  1469. length $active ? $active : undef,
  1470. $number);
  1471. my @was_unhidden = (0) x @kills;
  1472. for my $s (split ' ', $S{detach}) {
  1473. my ($k, $v) = (split /(?:,(-?\d+))$/, $s)[0, 1];
  1474. my $k2 = lc1459(as_uni($k));
  1475. my $kill;
  1476. for my $ki (0..$#kills) {
  1477. if (defined $kills[$ki] && $k2 eq lc1459($kills[$ki])) {
  1478. $kill = $ki;
  1479. }
  1480. }
  1481. if (defined $kill) {
  1482. if (defined $v && 0 == abs $v) {
  1483. $was_unhidden[$kill] = 1;
  1484. push @setting, defined $v ? "$k,$v" : $k;
  1485. } else {
  1486. $win->print("Unhiding window $kills[$kill]");
  1487. }
  1488. }
  1489. else {
  1490. push @setting, defined $v ? "$k,$v" : $k;
  1491. }
  1492. }
  1493. my @is_hidden = (defined $kills[0] && (exists $detach_map{"*"} || exists $detach_map{"::all"}),
  1494. defined $kills[1] && (exists $detach_map{lc1459("$tag/*")} || exists $detach_map{lc1459("$tag/::all")}
  1495. || exists $detach_map{"*"} || exists $detach_map{"::all"}),
  1496. defined $kills[2] && (exists $detach_map{"*"} || exists $detach_map{"::all"}),
  1497. (exists $detach_map{"*"} || exists $detach_map{"::all"})
  1498. );
  1499. for my $ki (1, 2, 0, 3) {
  1500. if ($is_hidden[$ki]) {
  1501. unless ($was_unhidden[$ki]) {
  1502. $win->print("Unhiding window $kills[$ki]");
  1503. push @setting, "$kills[$ki],0";
  1504. $was_unhidden[$ki] = 1;
  1505. }
  1506. last;
  1507. }
  1508. }
  1509. my @is_hidden_aht = (defined $kills[0] && (exists $detach_aht{lc1459($name)}
  1510. || exists $detach_aht{"*"} || exists $detach_aht{"::all"}),
  1511. defined $kills[1] && (exists $detach_aht{lc1459("$tag/$active")}
  1512. || exists $detach_aht{lc1459($active)}
  1513. || exists $detach_aht{lc1459("$tag/*")} || exists $detach_aht{lc1459("$tag/::all")}
  1514. || exists $detach_aht{"*"} || exists $detach_aht{"::all"}),
  1515. defined $kills[2] && (exists $detach_aht{lc1459($active)}
  1516. || exists $detach_aht{"*"} || exists $detach_aht{"::all"}),
  1517. (exists $detach_aht{$number} || exists $detach_aht{"*"} || exists $detach_aht{"::all"})
  1518. );
  1519. for my $ki (1, 2, 0, 3) {
  1520. if ($is_hidden_aht[$ki]) {
  1521. unless ($was_unhidden[$ki]) {
  1522. $win->print("Unhiding window $kills[$ki], it is hidden because ".(set 'detach_aht')." is ON");
  1523. push @setting, "$kills[$ki],0";
  1524. $was_unhidden[$ki] = 1;
  1525. }
  1526. last;
  1527. }
  1528. }
  1529. if (@setting) {
  1530. Irssi::command("^set ".(set 'detach')." @setting");
  1531. } else {
  1532. Irssi::command("^set -clear ".(set 'detach'));
  1533. }
  1534. }
  1535. sub ack_window {
  1536. my ($data, $server, $witem) = @_;
  1537. my $win = Irssi::active_win;
  1538. my $number = $win->{refnum};
  1539. if (grep { $_->{cmd} eq 'ack' } Irssi::commands) {
  1540. my $Orig_Irssi_windows = \&Irssi::windows;
  1541. local *Irssi::windows = sub () { grep { !_is_detached($_, $number) } $Orig_Irssi_windows->() };
  1542. Irssi::command("ack" . (length $data ? " $data" : ""));
  1543. } else {
  1544. my $ignore_refnum = Irssi::settings_get_bool('active_window_ignore_refnum');
  1545. my $max_win;
  1546. my $max_act = 0;
  1547. my $max_ref = 0;
  1548. for my $rec (Irssi::windows) {
  1549. next if _is_detached($rec, $number);
  1550. # ignore refnum
  1551. if ($ignore_refnum &&
  1552. $rec->{data_level} > 0 && $max_act < $rec->{data_level}) {
  1553. $max_act = $rec->{data_level};
  1554. $max_win = $rec;
  1555. }
  1556. # windows with lower refnums break ties
  1557. elsif (!$ignore_refnum &&
  1558. $rec->{data_level} > 0 &&
  1559. ($rec->{data_level} > $max_act ||
  1560. ($rec->{data_level} == $max_act && $rec->{refnum} < $max_ref))) {
  1561. $max_act = $rec->{data_level};
  1562. $max_win = $rec;
  1563. $max_ref = $rec->{refnum};
  1564. }
  1565. }
  1566. $max_win->set_active if defined $max_win;
  1567. }
  1568. }
  1569. sub refnum_changed {
  1570. my ($win, $old_refnum) = @_;
  1571. my @old_setting = split ' ', $S{detach};
  1572. my @setting = map {
  1573. my ($k, $v) = (split /(?:,(-?\d+))$/, $_)[0, 1];
  1574. if ($k eq $old_refnum) {
  1575. $win->{refnum} . (defined $v ? ",$v" : "")
  1576. }
  1577. else {
  1578. $_
  1579. }
  1580. } @old_setting;
  1581. if ("@old_setting" ne "@setting") {
  1582. $S{detach} = "@setting";
  1583. Irssi::settings_set_str(set 'detach', "@setting");
  1584. &setup_changed;
  1585. }
  1586. else {
  1587. &wl_changed;
  1588. }
  1589. }
  1590. sub window_destroyed {
  1591. my ($win) = @_;
  1592. my @old_setting = split ' ', $S{detach};
  1593. my @setting = grep {
  1594. my ($k, $v) = (split /(?:,(-?\d+))$/, $_)[0, 1];
  1595. if ($k eq $win->{refnum}) {
  1596. 0;
  1597. }
  1598. else {
  1599. 1;
  1600. }
  1601. } @old_setting;
  1602. if ("@old_setting" ne "@setting") {
  1603. $S{detach} = "@setting";
  1604. Irssi::settings_set_str(set 'detach', "@setting");
  1605. &setup_changed;
  1606. }
  1607. else {
  1608. &awins_changed;
  1609. }
  1610. }
  1611. sub stop_mouse_tracking {
  1612. print STDERR "\e[?1005l\e[?1000l";
  1613. }
  1614. sub start_mouse_tracking {
  1615. print STDERR "\e[?1000h\e[?1005h";
  1616. }
  1617. sub install_mouse {
  1618. Irssi::command_bind('mouse_xterm' => 'mouse_xterm');
  1619. Irssi::command('^bind meta-[M command mouse_xterm');
  1620. Irssi::signal_add_first('gui key pressed' => 'mouse_key_hook');
  1621. start_mouse_tracking();
  1622. }
  1623. sub uninstall_mouse {
  1624. stop_mouse_tracking();
  1625. Irssi::signal_remove('gui key pressed' => 'mouse_key_hook');
  1626. Irssi::command('^bind -delete meta-[M');
  1627. Irssi::command_unbind('mouse_xterm' => 'mouse_xterm');
  1628. }
  1629. sub awl_mouse_event {
  1630. return if $VIEWER_MODE;
  1631. if ((($_[0] == 3 and $_[3] == 0)
  1632. || $_[0] == 64 || $_[0] == 65) and
  1633. $_[1] == $_[4] and $_[2] == $_[5]) {
  1634. my $top = lc $S{placement} eq 'top';
  1635. my ($pos, $line) = @_[1 .. 2];
  1636. unless ($top) {
  1637. $line -= $screenHeight;
  1638. $line += $currentLines;
  1639. $line += $S{mouse_offset};
  1640. }
  1641. else {
  1642. $line -= $S{mouse_offset};
  1643. }
  1644. $pos -= $sb_base_width_pre;
  1645. return if $line < 0 || $line >= $currentLines;
  1646. if ($_[0] == 64) {
  1647. Irssi::command('window up');
  1648. }
  1649. elsif ($_[0] == 65) {
  1650. Irssi::command('window down');
  1651. }
  1652. elsif (exists $mouse_coords{$line}{$pos}) {
  1653. my $win = $mouse_coords{$line}{$pos};
  1654. Irssi::command('window ' . $win);
  1655. }
  1656. Irssi::signal_stop;
  1657. }
  1658. }
  1659. sub mouse_scroll_event {
  1660. return unless $S{mouse_scroll};
  1661. if (($_[3] == 64 or $_[3] == 65) and
  1662. $_[0] == $_[3] and $_[1] == $_[4] and $_[2] == $_[5]) {
  1663. my $cmd = 'scrollback goto ' . ($_[3] == 64 ? '-' : '+') . $S{mouse_scroll};
  1664. Irssi::active_win->command($cmd);
  1665. Irssi::signal_stop;
  1666. }
  1667. elsif ($_[0] == 64 or $_[0] == 65) {
  1668. Irssi::signal_stop;
  1669. }
  1670. }
  1671. sub mouse_escape {
  1672. return unless $S{mouse_escape} > 0;
  1673. if ($_[0] == 3) {
  1674. my $tm = $S{mouse_escape};
  1675. $tm *= 1000 if $tm < 1000;
  1676. stop_mouse_tracking();
  1677. Irssi::timeout_add_once($tm, 'start_mouse_tracking', undef);
  1678. Irssi::signal_stop;
  1679. }
  1680. }
  1681. sub UNLOAD {
  1682. @actString = ();
  1683. killOldStatus();
  1684. stop_viewer() if $VIEWER_MODE;
  1685. uninstall_mouse() if $MOUSE_ON;
  1686. }
  1687. sub addPrintTextHook { # update on print text
  1688. return unless defined $^S;
  1689. return if $BLOCK_ALL;
  1690. return unless $print_text_activity;
  1691. return if $_[0]->{level} == 262144 and $_[0]->{target} eq ''
  1692. and !defined($_[0]->{server});
  1693. &wl_changed;
  1694. }
  1695. sub block_event_window_change {
  1696. Irssi::signal_stop;
  1697. }
  1698. sub update_awins {
  1699. my @wins = Irssi::windows;
  1700. local $BLOCK_ALL = 1;
  1701. Irssi::signal_add_first('window changed' => 'block_event_window_change');
  1702. my $bwin =
  1703. my $awin = Irssi::active_win;
  1704. my $lwin;
  1705. my $defer_irssi_broken_last;
  1706. unless ($wins[0]{refnum} == $awin->{refnum}) {
  1707. # special case: more than 1 last win, so /win last;
  1708. # /win last doesn't come back to the current window. eg. after
  1709. # connect & autojoin; we can't handle this situation, bail out
  1710. $defer_irssi_broken_last = 1;
  1711. }
  1712. else {
  1713. $awin->command('window last');
  1714. $lwin = Irssi::active_win;
  1715. $lwin->command('window last');
  1716. $defer_irssi_broken_last = $lwin->{refnum} == $bwin->{refnum};
  1717. }
  1718. my $awin_counter = 0;
  1719. Irssi::signal_remove('window changed' => 'block_event_window_change');
  1720. unless ($defer_irssi_broken_last) {
  1721. # we need to keep the fe-windows code running here
  1722. Irssi::signal_add_priority('window changed' => 'block_event_window_change', -99);
  1723. %awins = %wnmap_exp = ();
  1724. do {
  1725. Irssi::active_win->command('window up');
  1726. $awin = Irssi::active_win;
  1727. $awins{$awin->{refnum}} = undef;
  1728. ++$awin_counter;
  1729. } until ($awin->{refnum} == $bwin->{refnum} || $awin_counter >= @wins);
  1730. Irssi::signal_remove('window changed' => 'block_event_window_change');
  1731. Irssi::signal_add_first('window changed' => 'block_event_window_change');
  1732. for my $key (keys %wnmap) {
  1733. next unless Irssi::window_find_name($key) || Irssi::window_find_item($key);
  1734. $awin->command("window goto $key");
  1735. my $cwin = Irssi::active_win;
  1736. $wnmap_exp{ $cwin->{refnum} } = $wnmap{$key};
  1737. $cwin->command('window last')
  1738. if $cwin->{refnum} != $awin->{refnum};
  1739. }
  1740. for my $win (reverse @wins) { # restore original window order
  1741. Irssi::active_win->command('window '.$win->{refnum});
  1742. }
  1743. $awin->command('window '.$lwin->{refnum}); # restore last win
  1744. Irssi::active_win->command('window last');
  1745. Irssi::signal_remove('window changed' => 'block_event_window_change');
  1746. }
  1747. $CHANGED{WL} = 1;
  1748. }
  1749. sub resizeTerm {
  1750. if (defined (my $r = `stty size 2>/dev/null`)) {
  1751. ($screenHeight, $screenWidth) = split ' ', $r;
  1752. $CHANGED{SETUP} = 1;
  1753. }
  1754. else {
  1755. $CHANGED{SIZE} = 1;
  1756. }
  1757. }
  1758. sub awl_refresh {
  1759. $globTime = undef;
  1760. resizeTerm() if delete $CHANGED{SIZE};
  1761. reset_awl() if delete $CHANGED{SETUP};
  1762. update_awins() if delete $CHANGED{AWINS};
  1763. update_wl() if delete $CHANGED{WL};
  1764. }
  1765. sub termsize_changed { $CHANGED{SIZE} = 1; &queue_refresh; }
  1766. sub setup_changed { $CHANGED{SETUP} = 1; &queue_refresh; }
  1767. sub awins_changed { $CHANGED{AWINS} = 1; &queue_refresh; }
  1768. sub wl_changed { $CHANGED{WL} = 1; &queue_refresh; }
  1769. sub window_changed {
  1770. &awins_changed if $_[1];
  1771. }
  1772. sub queue_refresh {
  1773. return if $BLOCK_ALL;
  1774. Irssi::timeout_remove($globTime)
  1775. if defined $globTime; # delay the update further
  1776. $globTime = Irssi::timeout_add_once(GLOB_QUEUE_TIMER, 'awl_refresh', undef);
  1777. }
  1778. sub awl_init {
  1779. termsize_changed();
  1780. setup_changed();
  1781. update_keymap();
  1782. Irssi::timeout_remove($globTime)
  1783. if defined $globTime;
  1784. awl_refresh();
  1785. termsize_changed();
  1786. }
  1787. sub runsub {
  1788. my $cmd = shift;
  1789. sub {
  1790. my ($data, $server, $item) = @_;
  1791. Irssi::command_runsub($cmd, $data, $server, $item);
  1792. };
  1793. }
  1794. Irssi::signal_register({
  1795. 'gui mouse' => [qw/int int int int int int/],
  1796. });
  1797. { my $broken_expandos = (Irssi::version >= 20081128 && Irssi::version < 20110210)
  1798. ? sub { my $x = shift; $x =~ s/\$\{cumode_space\}/ /; $x } : undef;
  1799. Irssi::theme_register([
  1800. map { $broken_expandos ? $broken_expandos->($_) : $_ }
  1801. set 'display_nokey' => '$N${cumode_space}$H$C$S',
  1802. set 'display_key' => '$Q${cumode_space}$H$C$S',
  1803. set 'display_nokey_visible' => '%2$N${cumode_space}$H$C$S',
  1804. set 'display_key_visible' => '%2$Q${cumode_space}$H$C$S',
  1805. set 'display_nokey_active' => '%1$N${cumode_space}$H$C$S',
  1806. set 'display_key_active' => '%1$Q${cumode_space}$H$C$S',
  1807. set 'display_header' => '%8$C|${N}',
  1808. set 'name_display' => '$0',
  1809. set 'separator' => ' ',
  1810. set 'separator2' => '',
  1811. set 'abbrev_chars' => "~\x{301c}",
  1812. set 'viewer_item_bg' => sb_format_expand('{sb_background}'),
  1813. set 'title' => '\V'.setc().'\:',
  1814. ]);
  1815. }
  1816. Irssi::settings_add_bool(setc, set 'prefer_name', 0); #
  1817. Irssi::settings_add_int( setc, set 'hide_empty', 0); #
  1818. Irssi::settings_add_int( setc, set 'hide_data', 0); #
  1819. Irssi::settings_add_str( setc, set 'detach', ''); #
  1820. Irssi::settings_add_int( setc, set 'detach_data', -3); #
  1821. Irssi::settings_add_bool(setc, set 'detach_aht', 0); #
  1822. Irssi::settings_add_int( setc, set 'hide_name_data', 0); #
  1823. Irssi::settings_add_int( setc, set 'maxlines', 9); #
  1824. Irssi::settings_add_int( setc, set 'maxcolumns', 4); #
  1825. Irssi::settings_add_int( setc, set 'block', 15); #
  1826. Irssi::settings_add_bool(setc, set 'sbar_maxlength', 1); #
  1827. Irssi::settings_add_int( setc, set 'height_adjust', 2); #
  1828. Irssi::settings_add_str( setc, set 'sort', 'refnum'); #
  1829. Irssi::settings_add_str( setc, set 'placement', 'bottom'); #
  1830. Irssi::settings_add_int( setc, set 'position', 0); #
  1831. Irssi::settings_add_bool(setc, set 'all_disable', 1); #
  1832. Irssi::settings_add_bool(setc, set 'viewer', 1); #
  1833. Irssi::settings_add_str( setc, set 'shared_sbar', 'OFF'); #
  1834. Irssi::settings_add_bool(setc, set 'mouse', 0); #
  1835. Irssi::settings_add_str( setc, set 'path', Irssi::get_irssi_dir . '/_windowlist'); #
  1836. Irssi::settings_add_str( setc, set 'custom_xform', ''); #
  1837. Irssi::settings_add_time(setc, set 'last_line_shade', '0'); #
  1838. Irssi::settings_add_int( setc, set 'mouse_offset', 1); #
  1839. Irssi::settings_add_int( setc, 'mouse_scroll', 3); #
  1840. Irssi::settings_add_int( setc, 'mouse_escape', 1); #
  1841. Irssi::settings_add_str( setc, 'banned_channels', '');
  1842. Irssi::settings_add_bool(setc, 'banned_channels_on', 1);
  1843. Irssi::settings_add_str( setc, 'fancy_abbrev', 'fancy'); #
  1844. Irssi::settings_add_bool(setc, set 'no_mode_hint', 0); #
  1845. Irssi::settings_add_bool(setc, set 'viewer_launch', 1); #
  1846. Irssi::settings_add_str( setc, set 'viewer_launch_env', ''); #
  1847. Irssi::settings_add_str( setc, set 'viewer_tmux_position', 'left'); #
  1848. Irssi::settings_add_str( setc, set 'viewer_xwin_command', 'xterm +sb -e %A'); #
  1849. Irssi::settings_add_str( setc, set 'viewer_custom_command', ''); #
  1850. Irssi::signal_add_last({
  1851. 'setup changed' => 'setup_changed',
  1852. 'print text' => 'addPrintTextHook',
  1853. 'terminal resized' => 'termsize_changed',
  1854. 'setup reread' => 'screenFullRedraw',
  1855. 'window hilight' => 'wl_changed',
  1856. 'command format' => 'wl_changed',
  1857. });
  1858. Irssi::signal_add({
  1859. 'window changed' => 'window_changed',
  1860. 'window item changed' => 'wl_changed',
  1861. 'window changed automatic' => 'window_changed',
  1862. 'window created' => 'awins_changed',
  1863. 'window destroyed' => 'window_destroyed',
  1864. 'window name changed' => 'wl_changed',
  1865. 'window refnum changed' => 'refnum_changed',
  1866. });
  1867. Irssi::signal_add_last('gui mouse' => 'mouse_escape');
  1868. Irssi::signal_add_last('gui mouse' => 'mouse_scroll_event');
  1869. Irssi::signal_add_last('gui mouse' => 'awl_mouse_event');
  1870. Irssi::command_bind( setc() => runsub(setc()) );
  1871. Irssi::command_bind( setc() . ' redraw' => 'screenFullRedraw' );
  1872. Irssi::command_bind( setc() . ' restart' => 'restartViewerServer' );
  1873. Irssi::command_bind( setc() . ' attach' => 'unhide_window' );
  1874. Irssi::command_bind( setc() . ' detach' => 'hide_window' );
  1875. Irssi::command_bind( setc() . ' ack' => 'ack_window' );
  1876. {
  1877. my $l = set 'shared';
  1878. {
  1879. no strict 'refs';
  1880. *{$l} = $awl_shared_empty;
  1881. }
  1882. Irssi::statusbar_item_register($l, '$0', $l);
  1883. }
  1884. awl_init();
  1885. # Mouse script based on irssi mouse patch by mirage
  1886. { my $mouse_status = -1; # -1:off 0,1,2:filling mouse_combo
  1887. my @mouse_combo; # 0:button 1:x 2:y
  1888. my @mouse_previous; # previous contents of mouse_combo
  1889. sub mouse_xterm_off {
  1890. $mouse_status = -1;
  1891. }
  1892. sub mouse_xterm {
  1893. $mouse_status = 0;
  1894. Irssi::timeout_add_once(10, 'mouse_xterm_off', undef);
  1895. }
  1896. sub mouse_key_hook {
  1897. my ($key) = @_;
  1898. if ($mouse_status != -1) {
  1899. if ($mouse_status == 0) {
  1900. @mouse_previous = @mouse_combo;
  1901. #if @mouse_combo && $mouse_combo[0] < 64;
  1902. }
  1903. $mouse_combo[$mouse_status] = $key - 32;
  1904. $mouse_status++;
  1905. if ($mouse_status == 3) {
  1906. $mouse_status = -1;
  1907. # match screen coordinates
  1908. $mouse_combo[1]--;
  1909. $mouse_combo[2]--;
  1910. Irssi::signal_emit('gui mouse', @mouse_combo[0 .. 2], @mouse_previous[0 .. 2]);
  1911. }
  1912. Irssi::signal_stop;
  1913. }
  1914. }
  1915. }
  1916. sub string_LCSS {
  1917. my $str = join "\0", @_;
  1918. (sort { length $b <=> length $a } $str =~ /(?=(.+).*\0.*\1)/g)[0]
  1919. }
  1920. # workaround for issue #271
  1921. { package Irssi::Nick }
  1922. # workaround for issue #572
  1923. @Irssi::UI::Exec::ISA = 'Irssi::Windowitem'
  1924. if Irssi::version >= 20140822 && Irssi::version <= 20161101 && !@Irssi::UI::Exec::ISA;
  1925. UNITCHECK
  1926. { package AwlViewer;
  1927. use strict;
  1928. use warnings;
  1929. no warnings 'redefine';
  1930. use Encode;
  1931. use IO::Socket::UNIX;
  1932. use IO::Select;
  1933. use List::Util qw(max);
  1934. use constant BLOCK_SIZE => 1024;
  1935. use constant RECONNECT_TIME => 5;
  1936. my $sockpath;
  1937. our $VERSION = '0.8';
  1938. our ($got_int, $resized, $timeout);
  1939. my %vars;
  1940. my (%c2w, @seqlist);
  1941. my %mouse_coords;
  1942. my (@mouse, @last_mouse);
  1943. my ($err, $sock, $loop);
  1944. my ($keybuf, $rcvbuf);
  1945. my @screen;
  1946. my ($screenHeight, $screenWidth);
  1947. my ($disp_update, $fs_open, $one_shot_integration, $one_shot_resize);
  1948. my $integration_position;
  1949. my $show_title_bar;
  1950. sub connect_it {
  1951. $sock = IO::Socket::UNIX->new(
  1952. Type => SOCK_STREAM,
  1953. Peer => $sockpath,
  1954. );
  1955. unless ($sock) {
  1956. $err = $!;
  1957. return;
  1958. }
  1959. $sock->blocking(0);
  1960. $loop->add($sock);
  1961. }
  1962. sub remove_conn {
  1963. my $fh = shift;
  1964. $loop->remove($fh);
  1965. $fh->close;
  1966. $sock = undef;
  1967. %vars = ();
  1968. @screen = ();
  1969. }
  1970. { package Terminfo; # xterm
  1971. sub civis { "\e[?25l" }
  1972. sub sc { "\e7" }
  1973. sub cup { "\e[" . ($_[0] + 1) . ';' . ($_[1] + 1) . 'H' }
  1974. sub el { "\e[K" }
  1975. sub rc { "\e8" }
  1976. sub cnorm { "\e[?25h" }
  1977. sub setab { "\e[4" . $_[0] . 'm' }
  1978. sub setaf { "\e[3" . $_[0] . 'm' }
  1979. sub setaf16 { "\e[9" . $_[0] . 'm' }
  1980. sub setab16 { "\e[10" . $_[0] . 'm' }
  1981. sub setaf256 { "\e[38;5;" . $_[0] . 'm' }
  1982. sub setab256 { "\e[48;5;" . $_[0] . 'm' }
  1983. sub sgr0 { "\e[0m" }
  1984. sub bold { "\e[1m" }
  1985. sub it { "\e[3m" }
  1986. sub ul { "\e[4m" }
  1987. sub blink { "\e[5m" }
  1988. sub rev { "\e[7m" }
  1989. sub op { "\e[39;49m" }
  1990. sub exit_bold { "\e[22m" }
  1991. sub exit_it { "\e[23m" }
  1992. sub exit_ul { "\e[24m" }
  1993. sub exit_blink { "\e[25m" }
  1994. sub exit_rev { "\e[27m" }
  1995. sub smcup { "\e[?1049h" }
  1996. sub rmcup { "\e[?1049l" }
  1997. sub smmouse { "\e[?1000h\e[?1005h" }
  1998. sub rmmouse { "\e[?1005l\e[?1000l" }
  1999. }
  2000. sub init {
  2001. $sockpath = shift // "$ENV{HOME}/.irssi/_windowlist";
  2002. STDOUT->autoflush(1);
  2003. printf "\r%swaiting for %s...", Terminfo::sc, $::IRSSI{name};
  2004. `stty -icanon -echo`;
  2005. $loop = IO::Select->new;
  2006. STDIN->blocking(0);
  2007. $loop->add(\*STDIN);
  2008. $SIG{INT} = sub {
  2009. $got_int = 1
  2010. };
  2011. $SIG{WINCH} = sub {
  2012. $resized = 1
  2013. };
  2014. $resized = 3;
  2015. $disp_update = 2;
  2016. $show_title_bar = 1;
  2017. }
  2018. sub enter_fs {
  2019. return if $fs_open;
  2020. safe_print(Terminfo::rc, Terminfo::smcup, Terminfo::civis, Terminfo::smmouse);
  2021. $fs_open = 1;
  2022. }
  2023. sub leave_fs {
  2024. return unless $fs_open;
  2025. safe_print(Terminfo::rmmouse, Terminfo::cnorm, Terminfo::rmcup);
  2026. safe_print(sprintf "\r%swaiting for %s...", Terminfo::sc, $::IRSSI{name}) if $_[0];
  2027. $fs_open = 0;
  2028. }
  2029. sub end_prog {
  2030. leave_fs();
  2031. STDIN->blocking(1);
  2032. `stty sane`;
  2033. printf "\r%s%sthanks for using %s\n", Terminfo::rc, Terminfo::el, $::IRSSI{name};
  2034. }
  2035. sub safe_print {
  2036. my $st = STDIN->blocking(1);
  2037. print @_;
  2038. STDIN->blocking($st);
  2039. }
  2040. sub safe_qx {
  2041. my $st = STDIN->blocking(1);
  2042. my $ret = `$_[0]`;
  2043. STDIN->blocking($st);
  2044. $ret
  2045. }
  2046. sub safe_print_sock {
  2047. return unless $sock;
  2048. my $was = $sock->blocking(1);
  2049. $sock->print(@_);
  2050. $sock->blocking($was);
  2051. }
  2052. sub process_recv {
  2053. my $need = 0;
  2054. while ($rcvbuf =~ s/\n(.+)_BEGIN\n((?: .*\n)*)\1_END\n//) {
  2055. my $var = lc $1;
  2056. my $data = $2;
  2057. my @data = split "\n ", "\n$data ", -1;
  2058. shift @data; pop @data;
  2059. my $itembg = $vars{itembg};
  2060. if ($var =~ s/list$//) {
  2061. $vars{$var} = \@data;
  2062. }
  2063. elsif ($var =~ s/map$//) {
  2064. $vars{$var} = +{ @data };
  2065. }
  2066. else {
  2067. $vars{$var} = join "\n", @data;
  2068. }
  2069. $need = 1 if $var eq 'win';
  2070. $need = 1 if $var eq 'redraw' && $vars{$var};
  2071. if (($itembg//'') ne ($vars{itembg}//'')) {
  2072. $need = $vars{redraw} = 1;
  2073. }
  2074. _build_keymap() if $var eq 'key2';
  2075. }
  2076. $need
  2077. }
  2078. { my %ansi_table;
  2079. my ($i, $j, $k) = (0, 0, 0);
  2080. my %term_state;
  2081. sub reset_term_state { my %old_term = %term_state; %term_state = (); %old_term }
  2082. sub set_term_state { my %old_term = %term_state; %term_state = @_; %old_term }
  2083. %ansi_table = (
  2084. # fe-common::core::formats.c:format_expand_styles
  2085. (map { my $t = $i++; ($_ => sub { my $n = $term_state{hicolor} ? \&Terminfo::setab16 : \&Terminfo::setab;
  2086. $n->($t) }) } (split //, '01234567' )),
  2087. (map { my $t = $j++; ($_ => sub { my $n = $term_state{hicolor} ? \&Terminfo::setaf16 : \&Terminfo::setaf;
  2088. $n->($t) }) } (split //, 'krgybmcw' )),
  2089. (map { my $t = $k++; ($_ => sub { my $n = $term_state{hicolor} ? \&Terminfo::setaf : \&Terminfo::setaf16;
  2090. $n->($t) }) } (split //, 'KRGYBMCW')),
  2091. # reset
  2092. n => sub { $term_state{hicolor} = 0; my $r = Terminfo::op;
  2093. for (qw(blink rev bold)) {
  2094. $r .= Terminfo->can("exit_$_")->() if delete $term_state{$_};
  2095. }
  2096. {
  2097. local $ansi_table{n} = $ansi_table{N};
  2098. $r .= formats_to_ansi_basic($vars{itembg});
  2099. }
  2100. $r
  2101. },
  2102. N => sub { reset_term_state(); Terminfo::sgr0 },
  2103. # flash/bright
  2104. F => sub { my $n = 'blink'; my $e = ($term_state{$n} ^= 1) ? $n : "exit_$n"; Terminfo->can($e)->() },
  2105. # reverse
  2106. 8 => sub { my $n = 'rev'; my $e = ($term_state{$n} ^= 1) ? $n : "exit_$n"; Terminfo->can($e)->() },
  2107. # bold
  2108. "_" => sub { my $n = 'bold'; my $e = ($term_state{$n} ^= 1) ? $n : "exit_$n"; Terminfo->can($e)->() },
  2109. # underline
  2110. U => sub { my $n = 'ul'; my $e = ($term_state{$n} ^= 1) ? $n : "exit_$n"; Terminfo->can($e)->() },
  2111. # italic
  2112. I => sub { my $n = 'it'; my $e = ($term_state{$n} ^= 1) ? $n : "exit_$n"; Terminfo->can($e)->() },
  2113. # bold, used as colour modifier if AWL_HI9 is set
  2114. 9 => $ENV{AWL_HI9} ? sub { $term_state{hicolor} ^= 1; '' }
  2115. : sub { my $n = 'bold'; my $e = ($term_state{$n} ^= 1) ? $n : "exit_$n"; Terminfo->can($e)->() },
  2116. # delete other stuff
  2117. (map { $_ => sub { '' } } (split //, ':|>#[')),
  2118. # escape
  2119. (map { my $close = $_; $_ => sub { $close } } (split //, '{}%')),
  2120. );
  2121. for my $base (0 .. 15) {
  2122. my $close = $base;
  2123. my $idx = ($close&8) | ($close&4)>>2 | ($close&2) | ($close&1)<<2;
  2124. $ansi_table{ (sprintf "x0%x", $close) } =
  2125. $ansi_table{ (sprintf "x0%X", $close) } =
  2126. sub { Terminfo::setab256($idx) };
  2127. $ansi_table{ (sprintf "X0%x", $close) } =
  2128. $ansi_table{ (sprintf "X0%X", $close) } =
  2129. sub { Terminfo::setaf256($idx) };
  2130. }
  2131. for my $plane (1 .. 6) {
  2132. for my $coord (0 .. 35) {
  2133. my $close = 16 + ($plane-1) * 36 + $coord;
  2134. my $ch = $coord < 10 ? $coord : chr( $coord - 10 + ord 'a' );
  2135. $ansi_table{ "x$plane$ch" } =
  2136. $ansi_table{ "x$plane\U$ch" } =
  2137. sub { Terminfo::setab256($close) };
  2138. $ansi_table{ "X$plane$ch" } =
  2139. $ansi_table{ "X$plane\U$ch" } =
  2140. sub { Terminfo::setaf256($close) };
  2141. }
  2142. }
  2143. for my $gray (0 .. 23) {
  2144. my $close = 232 + $gray;
  2145. my $ch = chr( $gray + ord 'a' );
  2146. $ansi_table{ "x7$ch" } =
  2147. $ansi_table{ "x7\U$ch" } =
  2148. sub { Terminfo::setab256($close) };
  2149. $ansi_table{ "X7$ch" } =
  2150. $ansi_table{ "X7\U$ch" } =
  2151. sub { Terminfo::setaf256($close) };
  2152. }
  2153. sub formats_to_ansi_basic {
  2154. my $o = shift;
  2155. $o =~ s/(%(X..|x..|.))/exists $ansi_table{$2} ? $ansi_table{$2}->() : $1/gex;
  2156. $o
  2157. }
  2158. }
  2159. sub _header {
  2160. my $str = $vars{title} // uc ::setc();
  2161. my $ccs = qr/%(?:X(?:[1-6][0-9A-Z]|7[A-X])|[0-9BCFGIKMNRUWY_])/i;
  2162. (my $stripstr = $str) =~ s/($ccs)//g;
  2163. my $space = int( ((abs $vars{block}) - length $stripstr) / (1 + length $stripstr));
  2164. if ($space > 0) {
  2165. my $ss = ' ' x $space;
  2166. my @x = $str =~ /((?:$ccs)*\X(?:(?:$ccs)*$)?)/g;
  2167. $str = join $ss, '', @x, '';
  2168. }
  2169. ($stripstr = $str) =~ s/($ccs)//g;
  2170. my $pad = max 0, (abs $vars{block}) - length $stripstr;
  2171. $str = ' ' x ($pad/2) . $str . ' ' x ($pad/2 + $pad%2);
  2172. $str
  2173. }
  2174. sub _add_item {
  2175. my ($i, $j, $c, $wi, $screen, $mouse) = @_;
  2176. $screen->[$i][$j] = "%N%n$wi";
  2177. if (exists $vars{mouse}{$c - 1}) {
  2178. $mouse->[$i][$j] = $vars{mouse}{$c - 1};
  2179. }
  2180. }
  2181. sub update_screen {
  2182. $disp_update = 0;
  2183. unless ($sock && exists $vars{seplen} && exists $vars{block}) {
  2184. leave_fs(1);
  2185. return;
  2186. }
  2187. enter_fs();
  2188. @screen = () if delete $vars{redraw};
  2189. %mouse_coords = ();
  2190. my $ncols = ($vars{seplen} + abs $vars{block}) ?
  2191. int( ($screenWidth + $vars{seplen}) / ($vars{seplen} + abs $vars{block}) ) : 0;
  2192. my $xenl = ($vars{seplen} + abs $vars{block})
  2193. && $ncols > int( ($screenWidth + $vars{seplen} - 1) / ($vars{seplen} + abs $vars{block}) );
  2194. my $nrows = $screenHeight - $vars{ha};
  2195. my @wi = @{$vars{win}//[]};
  2196. my $max_items = $ncols * $nrows;
  2197. my $c = $show_title_bar ? 1 : 0;
  2198. my $items = @wi + $c;
  2199. my $titems = $items > $max_items ? $max_items : $items;
  2200. my $i = 0;
  2201. my $j = 0;
  2202. my @new_screen;
  2203. my @new_mouse;
  2204. $new_screen[0][0] = _header() #. ' ' x $vars{seplen}
  2205. if $show_title_bar;
  2206. unless ($nrows > $ncols) { # line layout
  2207. ++$j if $show_title_bar;
  2208. for my $wi (@wi) {
  2209. if ($j >= $ncols) {
  2210. $j = 0;
  2211. ++$i;
  2212. }
  2213. last if $i >= $nrows;
  2214. _add_item($i, $j, $show_title_bar ? $c : $c + 1,
  2215. $wi, \@new_screen, \@new_mouse);
  2216. if ($c + 1 < $titems && $j + 1 < $ncols) {
  2217. $new_screen[$i][$j] .= $vars{separator};
  2218. }
  2219. ++$j;
  2220. ++$c;
  2221. }
  2222. }
  2223. else { # column layout
  2224. ++$i if $show_title_bar;
  2225. for my $wi (@wi) {
  2226. if ($i >= $nrows) {
  2227. $i = 0;
  2228. ++$j;
  2229. }
  2230. last if $j >= $ncols;
  2231. _add_item($i, $j, $show_title_bar ? $c : $c + 1,
  2232. $wi, \@new_screen, \@new_mouse);
  2233. if ($c + $nrows < $titems) {
  2234. $new_screen[$i][$j] .= $vars{separator};
  2235. }
  2236. ++$i;
  2237. ++$c;
  2238. }
  2239. }
  2240. my $step = $vars{seplen} + abs $vars{block};
  2241. $i = 0;
  2242. my $str = Terminfo::sc . Terminfo::sgr0;
  2243. for (my $i = 0; $i < @new_screen; ++$i) {
  2244. for (my $j = 0; $j < @{$new_screen[$i]}; ++$j) {
  2245. if (defined $new_mouse[$i] && defined $new_mouse[$i][$j]) {
  2246. my $from = $j * $step;
  2247. $mouse_coords{$i}{$_} = $new_mouse[$i][$j]
  2248. for $from .. $from + abs $vars{block};
  2249. }
  2250. next if defined $screen[$i] && defined $screen[$i][$j]
  2251. && $screen[$i][$j] eq $new_screen[$i][$j];
  2252. $str .= Terminfo::cup($i, $j * $step)
  2253. . formats_to_ansi_basic($new_screen[$i][$j])
  2254. . Terminfo::sgr0;
  2255. $str .= Terminfo::el if $j == $#{$new_screen[$i]} && (!$xenl || $j + 1 != $ncols);
  2256. }
  2257. }
  2258. for (@new_screen .. $screenHeight - 1) {
  2259. if (!@screen || defined $screen[$_]) {
  2260. $str .= Terminfo::cup($_, 0) . Terminfo::sgr0 . Terminfo::el;
  2261. }
  2262. }
  2263. $str .= Terminfo::rc;
  2264. safe_print $str;
  2265. @screen = @new_screen;
  2266. }
  2267. sub handle_resize {
  2268. if (defined (my $r = safe_qx('stty size'))) {
  2269. ($screenHeight, $screenWidth) = split ' ', $r;
  2270. $resized = 0;
  2271. @screen = ();
  2272. $disp_update = 1;
  2273. if ($one_shot_integration == 2) {
  2274. $one_shot_resize--;
  2275. }
  2276. }
  2277. else {
  2278. }
  2279. }
  2280. sub _build_keymap {
  2281. %c2w = reverse( %{$vars{key}}, %{$vars{key2}} );
  2282. if (!grep { /^[+-]./ } keys %c2w) {
  2283. %c2w = (%c2w, map { ("-$_" => $c2w{$_}) } grep { !/^\^./ } keys %c2w);
  2284. }
  2285. %c2w = map {
  2286. my $key = $_;
  2287. s{^(-)?(\+)?(\^)?(.)}{
  2288. join '', (
  2289. ($1 ? "\e" : ''),
  2290. ($2 ? "\e\e" : ''),
  2291. ($3 ? "$4"^"@" : $4)
  2292. )
  2293. }e;
  2294. $_ => $c2w{$key}
  2295. } keys %c2w;
  2296. @seqlist = sort { length $b <=> length $a } keys %c2w;
  2297. }
  2298. sub _match_tmux {
  2299. length $ENV{TMUX} && exists $vars{irssienv}{tmux_srv} && length $vars{irssienv}{tmux_pane}
  2300. && $ENV{TMUX} eq $vars{irssienv}{tmux_srv}
  2301. }
  2302. sub process_keys {
  2303. Encode::_utf8_on($keybuf);
  2304. my $win;
  2305. my $use_mouse;
  2306. my $maybe;
  2307. KEY: while (length $keybuf && !$maybe) {
  2308. $maybe = 0;
  2309. if ($keybuf =~ s/^\e\[M(.)(.)(.)//) {
  2310. @last_mouse = @mouse;# if @mouse && $mouse[0] < 64;
  2311. @mouse = map { -32 + ord } ($1, $2, $3);
  2312. $use_mouse = 1;
  2313. next KEY;
  2314. }
  2315. for my $s (@seqlist) {
  2316. if ($keybuf =~ s/^\Q$s//) {
  2317. $win = $c2w{$s};
  2318. $use_mouse = 0;
  2319. next KEY;
  2320. }
  2321. elsif (length $keybuf < length $s && $s =~ /^\Q$keybuf/) {
  2322. $maybe = 1;
  2323. }
  2324. }
  2325. unless ($maybe) {
  2326. substr $keybuf, 0, 1, '';
  2327. }
  2328. }
  2329. if ($use_mouse && @mouse && @last_mouse &&
  2330. $mouse[2] == $last_mouse[2] &&
  2331. $mouse[1] == $last_mouse[1] &&
  2332. ($mouse[0] == 3 || $mouse[0] == 64 || $mouse[0] == 65)) {
  2333. if ($mouse[0] == 64) {
  2334. $win = 'up';
  2335. }
  2336. elsif ($mouse[0] == 65) {
  2337. $win = 'down';
  2338. }
  2339. elsif (exists $mouse_coords{$mouse[2] - 1}{$mouse[1] - 1}) {
  2340. $win = $mouse_coords{$mouse[2] - 1}{$mouse[1] - 1};
  2341. }
  2342. elsif ($mouse[2] == 1 && $mouse[1] <= abs $vars{block}) {
  2343. $win = $last_mouse[0] != 0 ? 'last' : 'active';
  2344. }
  2345. else {
  2346. }
  2347. }
  2348. if (defined $win) {
  2349. $win =~ s/^_//;
  2350. safe_print_sock("$win\n");
  2351. if (!exists $ENV{AWL_AUTOFOCUS} || $ENV{AWL_AUTOFOCUS}) {
  2352. if (_match_tmux()) {
  2353. safe_qx("tmux selectp -t $vars{irssienv}{tmux_pane} 2>&1");
  2354. }
  2355. elsif (exists $vars{irssienv}{xwinid}) {
  2356. safe_qx("wmctrl -ia $vars{irssienv}{xwinid} 2>/dev/null");
  2357. }
  2358. }
  2359. }
  2360. Encode::_utf8_off($keybuf);
  2361. }
  2362. sub check_integration {
  2363. return unless $vars{irssienv};
  2364. return unless $sock && exists $vars{seplen} && exists $vars{block};
  2365. if ($one_shot_integration == 1) {
  2366. my $nrows = $screenHeight - $vars{ha};
  2367. my $ncols = ($vars{seplen} + abs $vars{block}) ? int( ($screenWidth + $vars{seplen}) / ($vars{seplen} + abs $vars{block}) ) : 0;
  2368. my $items = ($show_title_bar ? 1 : 0) + @{$vars{win}//[]};
  2369. my $dcols_required = $nrows ? int($items/$nrows) + !!($items%$nrows) : 0;
  2370. my $rows_required = $ncols ? int($items/$ncols) + !!($items%$ncols) : 0;
  2371. $rows_required = abs $vars{ml}
  2372. if ($vars{ml} < 0 || ($vars{ml} > 0 && $rows_required > $vars{ml}));
  2373. $dcols_required = abs $vars{mc}
  2374. if ($vars{mc} < 0 || ($vars{mc} > 0 && $dcols_required > $vars{mc}));
  2375. my $rows = $rows_required + $vars{ha};
  2376. my $cols = ($dcols_required * ($vars{seplen} + abs $vars{block})) - $vars{seplen};
  2377. if (_match_tmux()) {
  2378. # int( ($screenWidth + $vars{seplen}) / ($vars{seplen} + abs $vars{block}) );
  2379. my ($pos_flag, $before);
  2380. if ($integration_position eq 'left') {
  2381. $pos_flag = 'h';
  2382. $before = 1;
  2383. }
  2384. elsif ($integration_position eq 'top') {
  2385. $pos_flag = 'v';
  2386. $before = 1;
  2387. }
  2388. elsif ($integration_position eq 'right') {
  2389. $pos_flag = 'h';
  2390. }
  2391. else {
  2392. $pos_flag = 'v';
  2393. }
  2394. my @cmd = "joinp -d$pos_flag -s $ENV{TMUX_PANE} -t $vars{irssienv}{tmux_pane}";
  2395. push @cmd, "swapp -d -t $ENV{TMUX_PANE} -s $vars{irssienv}{tmux_pane}"
  2396. if $before;
  2397. $cols = max($cols, 2);
  2398. $rows = max($rows, 2);
  2399. safe_qx("tmux " . (join " \\\; ", @cmd) . " 2>&1");
  2400. }
  2401. else {
  2402. $resized = 1;
  2403. #safe_qx("resize -s $screenHeight $cols 2>&1")
  2404. # if $cols > 0;
  2405. }
  2406. $one_shot_integration++;
  2407. if ($resized == 1) {
  2408. handle_resize();
  2409. resize_integration();
  2410. }
  2411. }
  2412. elsif ($one_shot_integration == 2) {
  2413. resize_integration(1);
  2414. }
  2415. }
  2416. sub resize_integration {
  2417. return unless $one_shot_integration;
  2418. return unless ($one_shot_resize//0) < 0 || shift;
  2419. return if ($one_shot_resize//0) > 0;
  2420. my $nrows = $screenHeight - $vars{ha};
  2421. my $ncols = ($vars{seplen} + abs $vars{block}) ? int( ($screenWidth + $vars{seplen}) / ($vars{seplen} + abs $vars{block}) ) : 0;
  2422. my $items = ($show_title_bar ? 1 : 0) + @{$vars{win}//[]};
  2423. my $dcols_required = $nrows ? (int($items/$nrows) + !!($items%$nrows)) : 0;
  2424. my $rows_required = $ncols ? int($items/$ncols) + !!($items%$ncols) : 0;
  2425. $rows_required = abs $vars{ml}
  2426. if ($vars{ml} < 0 || ($vars{ml} > 0 && $rows_required > $vars{ml}));
  2427. $dcols_required = abs $vars{mc}
  2428. if ($vars{mc} < 0 || ($vars{mc} > 0 && $dcols_required > $vars{mc}));
  2429. my $rows = $rows_required + $vars{ha};
  2430. my $cols = ($dcols_required * ($vars{seplen} + abs $vars{block})) - $vars{seplen};
  2431. if (_match_tmux()) {
  2432. my $pos_flag;
  2433. my $before = 0;
  2434. if ($integration_position eq 'left') {
  2435. $pos_flag = 'h';
  2436. $before = 1;
  2437. }
  2438. elsif ($integration_position eq 'top') {
  2439. $pos_flag = 'v';
  2440. $before = 1;
  2441. }
  2442. elsif ($integration_position eq 'right') {
  2443. $pos_flag = 'h';
  2444. }
  2445. else {
  2446. $pos_flag = 'v';
  2447. }
  2448. my @cmd;
  2449. # hard tmux limits
  2450. $cols = max($cols, 2);
  2451. $rows = max($rows, 2);
  2452. if ($pos_flag eq 'h' && $cols != $screenWidth) {
  2453. my $change = $screenWidth - $cols;
  2454. my $dir = ($before ^ ($change<0)) ? 'L' : 'R';
  2455. push @cmd, "resizep -$dir -t $ENV{TMUX_PANE} @{[abs $change]}";
  2456. #push @cmd, "resizep -x $cols -t $ENV{TMUX_PANE}";
  2457. $one_shot_resize = 1;
  2458. }
  2459. if ($pos_flag eq 'v' && $rows != $screenHeight) {
  2460. #push @cmd, "resizep -y $rows -t $ENV{TMUX_PANE}";
  2461. my $change = $screenHeight - $rows;
  2462. my $dir = ($before ^ ($change<0)) ? 'U' : 'D';
  2463. push @cmd, "resizep -$dir -t $ENV{TMUX_PANE} @{[abs $change]}";
  2464. $one_shot_resize = 1;
  2465. }
  2466. safe_qx("tmux " . (join " \\\; ", @cmd) . " 2>&1")
  2467. if @cmd;
  2468. }
  2469. else {
  2470. $cols = max($cols, 1);
  2471. $rows = max($rows, 1);
  2472. unless ($nrows > $ncols) { # line layout
  2473. if ($rows != $screenHeight) {
  2474. safe_qx("resize -s $rows $screenWidth 2>&1");
  2475. $one_shot_resize = 1;
  2476. }
  2477. }
  2478. else {
  2479. if ($cols != $screenWidth) {
  2480. safe_qx("resize -s $screenHeight $cols 2>&1");
  2481. $one_shot_resize = 1;
  2482. }
  2483. }
  2484. }
  2485. if ($resized == 1) {
  2486. handle_resize();
  2487. }
  2488. }
  2489. sub init_integration {
  2490. return unless $one_shot_integration;
  2491. if (_match_tmux()) {
  2492. }
  2493. else {
  2494. }
  2495. safe_print("\e]2;".(uc ::setc())."\e\\");
  2496. }
  2497. sub main {
  2498. require Getopt::Std;
  2499. my %opts;
  2500. Getopt::Std::getopts('1p:', \%opts);
  2501. my $one_shot = $opts{1};
  2502. $integration_position = $opts{p};
  2503. $one_shot_integration = 0+!!$one_shot;
  2504. #shift if @_ && $_[0] eq '--';
  2505. &init;
  2506. $show_title_bar = 0 if $ENV{AWL_NOTITLE};
  2507. init_integration();
  2508. until ($got_int) {
  2509. $timeout = undef;
  2510. if ($resized) {
  2511. if ($resized == 1) {
  2512. $timeout = 1;
  2513. $resized++;
  2514. }
  2515. else {
  2516. handle_resize();
  2517. resize_integration();
  2518. }
  2519. }
  2520. unless ($sock || $timeout) {
  2521. connect_it();
  2522. }
  2523. $timeout ||= RECONNECT_TIME unless $sock;
  2524. update_screen() if $disp_update;
  2525. SELECT: while (my @read = $loop->can_read($timeout)) {
  2526. for my $fh (@read) {
  2527. if ($fh == \*STDIN) {
  2528. if (read STDIN, my $buf, BLOCK_SIZE) {
  2529. do {
  2530. $keybuf .= $buf;
  2531. } while read STDIN, $buf, BLOCK_SIZE;
  2532. }
  2533. else {
  2534. $got_int = 1;
  2535. last SELECT;
  2536. }
  2537. }
  2538. else {
  2539. if ($fh->read(my $buf, BLOCK_SIZE)) {
  2540. do {
  2541. $rcvbuf .= $buf;
  2542. } while $fh->read($buf, BLOCK_SIZE);
  2543. }
  2544. else {
  2545. $disp_update = 1;
  2546. remove_conn($fh);
  2547. if ($one_shot) {
  2548. $got_int = 1;
  2549. last SELECT;
  2550. }
  2551. $timeout ||= RECONNECT_TIME;
  2552. }
  2553. }
  2554. }
  2555. $disp_update |= process_recv() if length $rcvbuf;
  2556. process_keys() if length $keybuf;
  2557. check_integration() if $one_shot;
  2558. update_screen() if $disp_update;
  2559. }
  2560. continue {
  2561. }
  2562. }
  2563. end_prog();
  2564. }
  2565. }
  2566. 1;
  2567. # Changelog
  2568. # =========
  2569. # 1.8
  2570. # - use string_width in Irssi 1.2.0
  2571. #
  2572. # 1.7
  2573. # - fix crash on invalid /set awl_sort, introduced in 1.6, reported by
  2574. # tpetazzoni
  2575. # - delay viewer initialisation
  2576. # - improve race condition on tmux resize integration
  2577. #
  2578. # 1.6
  2579. # - add detach setting to hide windows
  2580. # - fix race condition when loading the script, reported by madduck
  2581. # - improve compatibility with irssi 1.2
  2582. # - add special value lru to awl_sort to sort windows by usage
  2583. #
  2584. # 1.5
  2585. # - improve compat. with sideways splits
  2586. #
  2587. # 1.4
  2588. # - fix line wrapping in some themes, reported by justanotherbody
  2589. # - fix named window key detection, reported by madduck
  2590. # - make title (in viewer and shared_sbar) configurable
  2591. #
  2592. # 1.3
  2593. # - workaround for irssi issue #572
  2594. #
  2595. # 1.2
  2596. # - new format to choose abbreviation character
  2597. #
  2598. # 1.1
  2599. # - infinite loop on shortening certain window names reported by Kalan
  2600. #
  2601. # 1.0
  2602. # - new awl_viewer_launch setting and an array of related settings
  2603. # - fixed regression bug /exec -interactive
  2604. # - fixed some warnings in perl 5.10 reported by kl3
  2605. # - workaround for crash due to infinite recursion in irssi's Perl
  2606. # error handling
  2607. #
  2608. # 0.9
  2609. # - fix endless loop in awin detection code!
  2610. # - correct colour swap in awl_viewer
  2611. # - fix passing of alternate socket path to the viewer
  2612. # - potential undefinedness in mouse refnum hinted at by Canopus
  2613. # - fixed regression bug /exec -interactive
  2614. # - add case-insensitive modifier to awl_sort
  2615. # - run custom_xform on awl_prefer_name also
  2616. # - avoid inconsistent active window state after awin detection
  2617. # reported by ss
  2618. # - revert %9-hack in the viewer prompted by discussion with pierrot
  2619. # - fix new warning in perl 5.22
  2620. #
  2621. # 0.8
  2622. # - replace fifo mode with external viewer script
  2623. # - remove bundled cpan modules
  2624. # - work around bogus irssi warning
  2625. # - improve mouse support
  2626. # - workaround for broken cumode in irssi 0.8.15
  2627. # - fix handling of non-meta windows (uninitialized warning)
  2628. # - add 256 colour support, strip true colour codes
  2629. # - fix totally bogus $N padding reported by Ed S.
  2630. # - make /window goto #name mappings work but ignore non-existant ones
  2631. # - improve incomplete reads reported by bcode
  2632. # - fix single % in awl_viewer reported by bcode
  2633. # - add support for key bindings by nike and ferret
  2634. # - coerce utf8 key binds
  2635. # - add settings: custom_xform, last_line_shade, hide_name_data
  2636. # - abbreviations were broken in some cases
  2637. # - fix some misuse of / as cmdchar in mouse script reported by bcode
  2638. # - add shared status bar mode
  2639. # - ${type} variables for custom_xform setting
  2640. # - crash if custom_xform had runtime error
  2641. # - update sorting documentation
  2642. # - fix odd case in size calculation noted by lasers
  2643. # - add missing font styles to the viewer reported by ishanyx
  2644. # - add italic
  2645. #
  2646. # 0.7g
  2647. # - remove screen support and replace it with fifo support
  2648. # - add double-width support to the shortener
  2649. # - correct documentation regarding $T vs. display_header
  2650. # - add missing refresh for window item changed (thanks vague)
  2651. # - add visible windows
  2652. # - add exemptions for active window
  2653. # - workaround for hiding the window changes from trackbar
  2654. # - hack to force 16colours in screen mode
  2655. # - remember last window (reported by earthnative)
  2656. # - wrong window focus on new queries (reported by emsid)
  2657. # - dataloss bug on trying to remember last window
  2658. #
  2659. # 0.6d+
  2660. # - add support for network headers
  2661. # - fixed regression bug /exec -interactive
  2662. #
  2663. # 0.6ca+
  2664. # - add screen support (from nicklist.pl)
  2665. # - names can now have a max length and window names can be used
  2666. # - fixed a bug with block display in screen mode and status bar mode
  2667. # - added space handling to ir_fe and removed it again
  2668. # - now handling formats on my own
  2669. # - started to work on $tag display
  2670. # - added warning about missing sb_act_none abstract leading to
  2671. # - display*active settings
  2672. # - added warning about the bug in awl_display_(no)key_active settings
  2673. # - mouse hack
  2674. #
  2675. # 0.5d
  2676. # - add setting to also hide the last status bar if empty (awl_all_disable)
  2677. # - reverted to old utf8 code to also calculate broken utf8 length correctly
  2678. # - simplified dealing with status bars in wlreset
  2679. # - added a little tweak for the renamed term_type somewhere after Irssi 0.8.9
  2680. # - fixed bug in handling channel #$$
  2681. # - reset background colour at the beginning of an entry
  2682. #
  2683. # 0.4d
  2684. # - fixed order of disabling status bars
  2685. # - several attempts at special chars, without any real success
  2686. # and much more weird new bugs caused by this
  2687. # - setting to specify sort order
  2688. # - reduced timeout values
  2689. # - added awl_hide_data
  2690. # - make it so the dynamic sub is actually deleted
  2691. # - fix a bug with removing of the last separator
  2692. # - take into consideration parse_special
  2693. #
  2694. # 0.3b
  2695. # - automatically kill old status bars
  2696. # - reset on /reload
  2697. # - position/placement settings
  2698. #
  2699. # 0.2
  2700. # - automated retrieval of key bindings (thanks grep.pl authors)
  2701. # - improved removing of status bars
  2702. # - got rid of status chop
  2703. #
  2704. # 0.1
  2705. # - Based on chanact.pl which was apparently based on lightbar.c and
  2706. # nicklist.pl with various other ideas from random scripts.