init.lua 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. -- Copyright 2007-2017 Mitchell mitchell.att.foicica.com. See LICENSE.
  2. local M = {}
  3. --[[ This comment is for LuaDoc.
  4. ---
  5. -- The YAML module.
  6. -- It provides utilities for editing YAML documents.
  7. --
  8. -- ## Key Bindings
  9. --
  10. -- + `Ctrl+&` (`⌘&` | `M-&`)
  11. -- Jump to the anchor for the alias under the caret.
  12. module('_M.yaml')]]
  13. local _, lyaml = pcall(require, 'yaml.lyaml')
  14. M.lyaml = lyaml
  15. -- Always use spaces.
  16. events.connect(events.LEXER_LOADED, function(lexer)
  17. if lexer == 'yaml' then
  18. buffer.use_tabs = false
  19. buffer.word_chars = table.concat{
  20. 'abcdefghijklmnopqrstuvwxyz',
  21. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  22. '1234567890-*'
  23. }
  24. end
  25. end)
  26. -- Commands.
  27. -- Verify syntax.
  28. events.connect(events.FILE_AFTER_SAVE, function()
  29. if buffer:get_lexer() ~= 'yaml' or not M.lyaml then return end
  30. buffer:annotation_clear_all()
  31. local ok, error = pcall(M.lyaml.load, buffer:get_text())
  32. if ok then return end
  33. local msg, line, col =
  34. error:match('^(.-) at document: %d+, line: (%d+), column: (%d+)')
  35. if not line or not col then line, col, msg = 1, 1, error end
  36. buffer.annotation_text[line - 1] = msg
  37. buffer.annotation_style[line - 1] = 8 -- error style number
  38. buffer:goto_pos(buffer:find_column(line - 1, col - 1))
  39. end)
  40. ---
  41. -- Jumps to the anchor for the alias underneath the caret.
  42. -- @name goto_anchor
  43. function M.goto_anchor()
  44. local s = buffer:word_start_position(buffer.current_pos, true)
  45. local e = buffer:word_end_position(buffer.current_pos)
  46. local anchor = buffer:text_range(s, e):match('^%*(.+)$')
  47. if anchor then
  48. buffer:target_whole_document()
  49. buffer.search_flags = buffer.FIND_WHOLEWORD
  50. if buffer:search_in_target('&'..anchor) >= 0 then
  51. buffer:goto_pos(buffer.target_start)
  52. end
  53. end
  54. end
  55. ---
  56. -- Container for YAML-specific key bindings.
  57. -- @class table
  58. -- @name _G.keys.yaml
  59. keys.yaml = {
  60. [not OSX and not CURSES and 'c&' or 'm&'] = M.goto_anchor
  61. }
  62. -- Snippets.
  63. if type(snippets) == 'table' then
  64. ---
  65. -- Container for YAML-specific snippets.
  66. -- @class table
  67. -- @name _G.snippets.yaml
  68. snippets.yaml = {
  69. }
  70. end
  71. return M