Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

563 lines
20 KiB

4 years ago
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2014 Sébastien Helleu <flashcode@flashtux.org>
  4. # Copyright (C) 2010 m4v <lambdae2@gmail.com>
  5. # Copyright (C) 2011 stfn <stfnmd@googlemail.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. #
  21. # History:
  22. #
  23. # 2019-07-11, Simmo Saan <simmo.saan@gmail.com>
  24. # version 2.6: fix detection of "/input search_text_here"
  25. # 2017-04-01, Sébastien Helleu <flashcode@flashtux.org>:
  26. # version 2.5: add option "buffer_number"
  27. # 2017-03-02, Sébastien Helleu <flashcode@flashtux.org>:
  28. # version 2.4: fix syntax and indentation error
  29. # 2017-02-25, Simmo Saan <simmo.saan@gmail.com>
  30. # version 2.3: fix fuzzy search breaking buffer number search display
  31. # 2016-01-28, ylambda <ylambda@koalabeast.com>
  32. # version 2.2: add option "fuzzy_search"
  33. # 2015-11-12, nils_2 <weechatter@arcor.de>
  34. # version 2.1: fix problem with buffer short_name "weechat", using option
  35. # "use_core_instead_weechat", see:
  36. # https://github.com/weechat/weechat/issues/574
  37. # 2014-05-12, Sébastien Helleu <flashcode@flashtux.org>:
  38. # version 2.0: add help on options, replace option "sort_by_activity" by
  39. # "sort" (add sort by name and first match at beginning of
  40. # name and by number), PEP8 compliance
  41. # 2012-11-26, Nei <anti.teamidiot.de>
  42. # version 1.9: add auto_jump option to automatically go to buffer when it
  43. # is uniquely selected
  44. # 2012-09-17, Sébastien Helleu <flashcode@flashtux.org>:
  45. # version 1.8: fix jump to non-active merged buffers (jump with buffer name
  46. # instead of number)
  47. # 2012-01-03 nils_2 <weechatter@arcor.de>
  48. # version 1.7: add option "use_core_instead_weechat"
  49. # 2012-01-03, Sébastien Helleu <flashcode@flashtux.org>:
  50. # version 1.6: make script compatible with Python 3.x
  51. # 2011-08-24, stfn <stfnmd@googlemail.com>:
  52. # version 1.5: /go with name argument jumps directly to buffer
  53. # Remember cursor position in buffer input
  54. # 2011-05-31, Elián Hanisch <lambdae2@gmail.com>:
  55. # version 1.4: Sort list of buffers by activity.
  56. # 2011-04-25, Sébastien Helleu <flashcode@flashtux.org>:
  57. # version 1.3: add info "go_running" (used by script input_lock.rb)
  58. # 2010-11-01, Sébastien Helleu <flashcode@flashtux.org>:
  59. # version 1.2: use high priority for hooks to prevent conflict with other
  60. # plugins/scripts (WeeChat >= 0.3.4 only)
  61. # 2010-03-25, Elián Hanisch <lambdae2@gmail.com>:
  62. # version 1.1: use a space to match the end of a string
  63. # 2009-11-16, Sébastien Helleu <flashcode@flashtux.org>:
  64. # version 1.0: add new option to display short names
  65. # 2009-06-15, Sébastien Helleu <flashcode@flashtux.org>:
  66. # version 0.9: fix typo in /help go with command /key
  67. # 2009-05-16, Sébastien Helleu <flashcode@flashtux.org>:
  68. # version 0.8: search buffer by number, fix bug when window is split
  69. # 2009-05-03, Sébastien Helleu <flashcode@flashtux.org>:
  70. # version 0.7: eat tab key (do not complete input, just move buffer
  71. # pointer)
  72. # 2009-05-02, Sébastien Helleu <flashcode@flashtux.org>:
  73. # version 0.6: sync with last API changes
  74. # 2009-03-22, Sébastien Helleu <flashcode@flashtux.org>:
  75. # version 0.5: update modifier signal name for input text display,
  76. # fix arguments for function string_remove_color
  77. # 2009-02-18, Sébastien Helleu <flashcode@flashtux.org>:
  78. # version 0.4: do not hook command and init options if register failed
  79. # 2009-02-08, Sébastien Helleu <flashcode@flashtux.org>:
  80. # version 0.3: case insensitive search for buffers names
  81. # 2009-02-08, Sébastien Helleu <flashcode@flashtux.org>:
  82. # version 0.2: add help about Tab key
  83. # 2009-02-08, Sébastien Helleu <flashcode@flashtux.org>:
  84. # version 0.1: initial release
  85. #
  86. """
  87. Quick jump to buffers.
  88. (this script requires WeeChat 0.3.0 or newer)
  89. """
  90. from __future__ import print_function
  91. SCRIPT_NAME = 'go'
  92. SCRIPT_AUTHOR = 'Sébastien Helleu <flashcode@flashtux.org>'
  93. SCRIPT_VERSION = '2.6'
  94. SCRIPT_LICENSE = 'GPL3'
  95. SCRIPT_DESC = 'Quick jump to buffers'
  96. SCRIPT_COMMAND = 'go'
  97. IMPORT_OK = True
  98. try:
  99. import weechat
  100. except ImportError:
  101. print('This script must be run under WeeChat.')
  102. print('Get WeeChat now at: http://www.weechat.org/')
  103. IMPORT_OK = False
  104. import re
  105. # script options
  106. SETTINGS = {
  107. 'color_number': (
  108. 'yellow,magenta',
  109. 'color for buffer number (not selected)'),
  110. 'color_number_selected': (
  111. 'yellow,red',
  112. 'color for selected buffer number'),
  113. 'color_name': (
  114. 'black,cyan',
  115. 'color for buffer name (not selected)'),
  116. 'color_name_selected': (
  117. 'black,brown',
  118. 'color for a selected buffer name'),
  119. 'color_name_highlight': (
  120. 'red,cyan',
  121. 'color for highlight in buffer name (not selected)'),
  122. 'color_name_highlight_selected': (
  123. 'red,brown',
  124. 'color for highlight in a selected buffer name'),
  125. 'message': (
  126. 'Go to: ',
  127. 'message to display before list of buffers'),
  128. 'short_name': (
  129. 'off',
  130. 'display and search in short names instead of buffer name'),
  131. 'sort': (
  132. 'number,beginning',
  133. 'comma-separated list of keys to sort buffers '
  134. '(the order is important, sorts are performed in the given order): '
  135. 'name = sort by name (or short name), ',
  136. 'hotlist = sort by hotlist order, '
  137. 'number = first match a buffer number before digits in name, '
  138. 'beginning = first match at beginning of names (or short names); '
  139. 'the default sort of buffers is by numbers'),
  140. 'use_core_instead_weechat': (
  141. 'off',
  142. 'use name "core" instead of "weechat" for core buffer'),
  143. 'auto_jump': (
  144. 'off',
  145. 'automatically jump to buffer when it is uniquely selected'),
  146. 'fuzzy_search': (
  147. 'off',
  148. 'search buffer matches using approximation'),
  149. 'buffer_number': (
  150. 'on',
  151. 'display buffer number'),
  152. }
  153. # hooks management
  154. HOOK_COMMAND_RUN = {
  155. 'input': ('/input *', 'go_command_run_input'),
  156. 'buffer': ('/buffer *', 'go_command_run_buffer'),
  157. 'window': ('/window *', 'go_command_run_window'),
  158. }
  159. hooks = {}
  160. # input before command /go (we'll restore it later)
  161. saved_input = ''
  162. saved_input_pos = 0
  163. # last user input (if changed, we'll update list of matching buffers)
  164. old_input = None
  165. # matching buffers
  166. buffers = []
  167. buffers_pos = 0
  168. def go_option_enabled(option):
  169. """Checks if a boolean script option is enabled or not."""
  170. return weechat.config_string_to_boolean(weechat.config_get_plugin(option))
  171. def go_info_running(data, info_name, arguments):
  172. """Returns "1" if go is running, otherwise "0"."""
  173. return '1' if 'modifier' in hooks else '0'
  174. def go_unhook_one(hook):
  175. """Unhook something hooked by this script."""
  176. global hooks
  177. if hook in hooks:
  178. weechat.unhook(hooks[hook])
  179. del hooks[hook]
  180. def go_unhook_all():
  181. """Unhook all."""
  182. go_unhook_one('modifier')
  183. for hook in HOOK_COMMAND_RUN:
  184. go_unhook_one(hook)
  185. def go_hook_all():
  186. """Hook command_run and modifier."""
  187. global hooks
  188. priority = ''
  189. version = weechat.info_get('version_number', '') or 0
  190. # use high priority for hook to prevent conflict with other plugins/scripts
  191. # (WeeChat >= 0.3.4 only)
  192. if int(version) >= 0x00030400:
  193. priority = '2000|'
  194. for hook, value in HOOK_COMMAND_RUN.items():
  195. if hook not in hooks:
  196. hooks[hook] = weechat.hook_command_run(
  197. '%s%s' % (priority, value[0]),
  198. value[1], '')
  199. if 'modifier' not in hooks:
  200. hooks['modifier'] = weechat.hook_modifier(
  201. 'input_text_display_with_cursor', 'go_input_modifier', '')
  202. def go_start(buf):
  203. """Start go on buffer."""
  204. global saved_input, saved_input_pos, old_input, buffers_pos
  205. go_hook_all()
  206. saved_input = weechat.buffer_get_string(buf, 'input')
  207. saved_input_pos = weechat.buffer_get_integer(buf, 'input_pos')
  208. weechat.buffer_set(buf, 'input', '')
  209. old_input = None
  210. buffers_pos = 0
  211. def go_end(buf):
  212. """End go on buffer."""
  213. global saved_input, saved_input_pos, old_input
  214. go_unhook_all()
  215. weechat.buffer_set(buf, 'input', saved_input)
  216. weechat.buffer_set(buf, 'input_pos', str(saved_input_pos))
  217. old_input = None
  218. def go_match_beginning(buf, string):
  219. """Check if a string matches the beginning of buffer name/short name."""
  220. if not string:
  221. return False
  222. esc_str = re.escape(string)
  223. if re.search(r'^#?' + esc_str, buf['name']) \
  224. or re.search(r'^#?' + esc_str, buf['short_name']):
  225. return True
  226. return False
  227. def go_match_fuzzy(name, string):
  228. """Check if string matches name using approximation."""
  229. if not string:
  230. return False
  231. name_len = len(name)
  232. string_len = len(string)
  233. if string_len > name_len:
  234. return False
  235. if name_len == string_len:
  236. return name == string
  237. # Attempt to match all chars somewhere in name
  238. prev_index = -1
  239. for i, char in enumerate(string):
  240. index = name.find(char, prev_index+1)
  241. if index == -1:
  242. return False
  243. prev_index = index
  244. return True
  245. def go_now(buf, args):
  246. """Go to buffer specified by args."""
  247. listbuf = go_matching_buffers(args)
  248. if not listbuf:
  249. return
  250. # prefer buffer that matches at beginning (if option is enabled)
  251. if 'beginning' in weechat.config_get_plugin('sort').split(','):
  252. for index in range(len(listbuf)):
  253. if go_match_beginning(listbuf[index], args):
  254. weechat.command(buf,
  255. '/buffer ' + str(listbuf[index]['full_name']))
  256. return
  257. # jump to first buffer in matching buffers by default
  258. weechat.command(buf, '/buffer ' + str(listbuf[0]['full_name']))
  259. def go_cmd(data, buf, args):
  260. """Command "/go": just hook what we need."""
  261. global hooks
  262. if args:
  263. go_now(buf, args)
  264. elif 'modifier' in hooks:
  265. go_end(buf)
  266. else:
  267. go_start(buf)
  268. return weechat.WEECHAT_RC_OK
  269. def go_matching_buffers(strinput):
  270. """Return a list with buffers matching user input."""
  271. global buffers_pos
  272. listbuf = []
  273. if len(strinput) == 0:
  274. buffers_pos = 0
  275. strinput = strinput.lower()
  276. infolist = weechat.infolist_get('buffer', '', '')
  277. while weechat.infolist_next(infolist):
  278. short_name = weechat.infolist_string(infolist, 'short_name')
  279. if go_option_enabled('short_name'):
  280. name = weechat.infolist_string(infolist, 'short_name')
  281. else:
  282. name = weechat.infolist_string(infolist, 'name')
  283. if name == 'weechat' \
  284. and go_option_enabled('use_core_instead_weechat') \
  285. and weechat.infolist_string(infolist, 'plugin_name') == 'core':
  286. name = 'core'
  287. number = weechat.infolist_integer(infolist, 'number')
  288. full_name = weechat.infolist_string(infolist, 'full_name')
  289. if not full_name:
  290. full_name = '%s.%s' % (
  291. weechat.infolist_string(infolist, 'plugin_name'),
  292. weechat.infolist_string(infolist, 'name'))
  293. pointer = weechat.infolist_pointer(infolist, 'pointer')
  294. matching = name.lower().find(strinput) >= 0
  295. if not matching and strinput[-1] == ' ':
  296. matching = name.lower().endswith(strinput.strip())
  297. if not matching and go_option_enabled('fuzzy_search'):
  298. matching = go_match_fuzzy(name.lower(), strinput)
  299. if not matching and strinput.isdigit():
  300. matching = str(number).startswith(strinput)
  301. if len(strinput) == 0 or matching:
  302. listbuf.append({
  303. 'number': number,
  304. 'short_name': short_name,
  305. 'name': name,
  306. 'full_name': full_name,
  307. 'pointer': pointer,
  308. })
  309. weechat.infolist_free(infolist)
  310. # sort buffers
  311. hotlist = []
  312. infolist = weechat.infolist_get('hotlist', '', '')
  313. while weechat.infolist_next(infolist):
  314. hotlist.append(
  315. weechat.infolist_pointer(infolist, 'buffer_pointer'))
  316. weechat.infolist_free(infolist)
  317. last_index_hotlist = len(hotlist)
  318. def _sort_name(buf):
  319. """Sort buffers by name (or short name)."""
  320. return buf['name']
  321. def _sort_hotlist(buf):
  322. """Sort buffers by hotlist order."""
  323. try:
  324. return hotlist.index(buf['pointer'])
  325. except ValueError:
  326. # not in hotlist, always last.
  327. return last_index_hotlist
  328. def _sort_match_number(buf):
  329. """Sort buffers by match on number."""
  330. return 0 if str(buf['number']) == strinput else 1
  331. def _sort_match_beginning(buf):
  332. """Sort buffers by match at beginning."""
  333. return 0 if go_match_beginning(buf, strinput) else 1
  334. funcs = {
  335. 'name': _sort_name,
  336. 'hotlist': _sort_hotlist,
  337. 'number': _sort_match_number,
  338. 'beginning': _sort_match_beginning,
  339. }
  340. for key in weechat.config_get_plugin('sort').split(','):
  341. if key in funcs:
  342. listbuf = sorted(listbuf, key=funcs[key])
  343. if not strinput:
  344. index = [i for i, buf in enumerate(listbuf)
  345. if buf['pointer'] == weechat.current_buffer()]
  346. if index:
  347. buffers_pos = index[0]
  348. return listbuf
  349. def go_buffers_to_string(listbuf, pos, strinput):
  350. """Return string built with list of buffers found (matching user input)."""
  351. string = ''
  352. strinput = strinput.lower()
  353. for i in range(len(listbuf)):
  354. selected = '_selected' if i == pos else ''
  355. buffer_name = listbuf[i]['name']
  356. index = buffer_name.lower().find(strinput)
  357. if index >= 0:
  358. index2 = index + len(strinput)
  359. name = '%s%s%s%s%s' % (
  360. buffer_name[:index],
  361. weechat.color(weechat.config_get_plugin(
  362. 'color_name_highlight' + selected)),
  363. buffer_name[index:index2],
  364. weechat.color(weechat.config_get_plugin(
  365. 'color_name' + selected)),
  366. buffer_name[index2:])
  367. elif go_option_enabled("fuzzy_search") and \
  368. go_match_fuzzy(buffer_name.lower(), strinput):
  369. name = ""
  370. prev_index = -1
  371. for char in strinput.lower():
  372. index = buffer_name.lower().find(char, prev_index+1)
  373. if prev_index < 0:
  374. name += buffer_name[:index]
  375. name += weechat.color(weechat.config_get_plugin(
  376. 'color_name_highlight' + selected))
  377. if prev_index >= 0 and index > prev_index+1:
  378. name += weechat.color(weechat.config_get_plugin(
  379. 'color_name' + selected))
  380. name += buffer_name[prev_index+1:index]
  381. name += weechat.color(weechat.config_get_plugin(
  382. 'color_name_highlight' + selected))
  383. name += buffer_name[index]
  384. prev_index = index
  385. name += weechat.color(weechat.config_get_plugin(
  386. 'color_name' + selected))
  387. name += buffer_name[prev_index+1:]
  388. else:
  389. name = buffer_name
  390. string += ' '
  391. if go_option_enabled('buffer_number'):
  392. string += '%s%s' % (
  393. weechat.color(weechat.config_get_plugin(
  394. 'color_number' + selected)),
  395. str(listbuf[i]['number']))
  396. string += '%s%s%s' % (
  397. weechat.color(weechat.config_get_plugin(
  398. 'color_name' + selected)),
  399. name,
  400. weechat.color('reset'))
  401. return ' ' + string if string else ''
  402. def go_input_modifier(data, modifier, modifier_data, string):
  403. """This modifier is called when input text item is built by WeeChat.
  404. This is commonly called after changes in input or cursor move: it builds
  405. a new input with prefix ("Go to:"), and suffix (list of buffers found).
  406. """
  407. global old_input, buffers, buffers_pos
  408. if modifier_data != weechat.current_buffer():
  409. return ''
  410. names = ''
  411. new_input = weechat.string_remove_color(string, '')
  412. new_input = new_input.lstrip()
  413. if old_input is None or new_input != old_input:
  414. old_buffers = buffers
  415. buffers = go_matching_buffers(new_input)
  416. if buffers != old_buffers and len(new_input) > 0:
  417. if len(buffers) == 1 and go_option_enabled('auto_jump'):
  418. weechat.command(modifier_data, '/wait 1ms /input return')
  419. buffers_pos = 0
  420. old_input = new_input
  421. names = go_buffers_to_string(buffers, buffers_pos, new_input.strip())
  422. return weechat.config_get_plugin('message') + string + names
  423. def go_command_run_input(data, buf, command):
  424. """Function called when a command "/input xxx" is run."""
  425. global buffers, buffers_pos
  426. if command.startswith('/input search_text') or command.startswith('/input jump'):
  427. # search text or jump to another buffer is forbidden now
  428. return weechat.WEECHAT_RC_OK_EAT
  429. elif command == '/input complete_next':
  430. # choose next buffer in list
  431. buffers_pos += 1
  432. if buffers_pos >= len(buffers):
  433. buffers_pos = 0
  434. weechat.hook_signal_send('input_text_changed',
  435. weechat.WEECHAT_HOOK_SIGNAL_STRING, '')
  436. return weechat.WEECHAT_RC_OK_EAT
  437. elif command == '/input complete_previous':
  438. # choose previous buffer in list
  439. buffers_pos -= 1
  440. if buffers_pos < 0:
  441. buffers_pos = len(buffers) - 1
  442. weechat.hook_signal_send('input_text_changed',
  443. weechat.WEECHAT_HOOK_SIGNAL_STRING, '')
  444. return weechat.WEECHAT_RC_OK_EAT
  445. elif command == '/input return':
  446. # switch to selected buffer (if any)
  447. go_end(buf)
  448. if len(buffers) > 0:
  449. weechat.command(
  450. buf, '/buffer ' + str(buffers[buffers_pos]['full_name']))
  451. return weechat.WEECHAT_RC_OK_EAT
  452. return weechat.WEECHAT_RC_OK
  453. def go_command_run_buffer(data, buf, command):
  454. """Function called when a command "/buffer xxx" is run."""
  455. return weechat.WEECHAT_RC_OK_EAT
  456. def go_command_run_window(data, buf, command):
  457. """Function called when a command "/window xxx" is run."""
  458. return weechat.WEECHAT_RC_OK_EAT
  459. def go_unload_script():
  460. """Function called when script is unloaded."""
  461. go_unhook_all()
  462. return weechat.WEECHAT_RC_OK
  463. def go_main():
  464. """Entry point."""
  465. if not weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION,
  466. SCRIPT_LICENSE, SCRIPT_DESC,
  467. 'go_unload_script', ''):
  468. return
  469. weechat.hook_command(
  470. SCRIPT_COMMAND,
  471. 'Quick jump to buffers', '[name]',
  472. 'name: directly jump to buffer by name (without argument, list is '
  473. 'displayed)\n\n'
  474. 'You can bind command to a key, for example:\n'
  475. ' /key bind meta-g /go\n\n'
  476. 'You can use completion key (commonly Tab and shift-Tab) to select '
  477. 'next/previous buffer in list.',
  478. '%(buffers_names)',
  479. 'go_cmd', '')
  480. # set default settings
  481. version = weechat.info_get('version_number', '') or 0
  482. for option, value in SETTINGS.items():
  483. if not weechat.config_is_set_plugin(option):
  484. weechat.config_set_plugin(option, value[0])
  485. if int(version) >= 0x00030500:
  486. weechat.config_set_desc_plugin(
  487. option, '%s (default: "%s")' % (value[1], value[0]))
  488. weechat.hook_info('go_running',
  489. 'Return "1" if go is running, otherwise "0"',
  490. '',
  491. 'go_info_running', '')
  492. if __name__ == "__main__" and IMPORT_OK:
  493. go_main()