1
0

settings.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright 2010-2014 Wincent Colaiuta. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are met:
  5. #
  6. # 1. Redistributions of source code must retain the above copyright notice,
  7. # this list of conditions and the following disclaimer.
  8. # 2. Redistributions in binary form must reproduce the above copyright notice,
  9. # this list of conditions and the following disclaimer in the documentation
  10. # and/or other materials provided with the distribution.
  11. #
  12. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  13. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  14. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  15. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  16. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  17. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  18. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  19. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  20. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  21. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  22. # POSSIBILITY OF SUCH DAMAGE.
  23. module CommandT
  24. # Convenience class for saving and restoring global settings.
  25. class Settings
  26. # Settings which apply globally and so must be manually saved and restored
  27. GLOBAL_SETTINGS = %w[
  28. equalalways
  29. hlsearch
  30. insertmode
  31. report
  32. showcmd
  33. sidescroll
  34. sidescrolloff
  35. timeout
  36. timeoutlen
  37. updatetime
  38. ]
  39. # Settings which can be made locally to the Command-T buffer or window
  40. LOCAL_SETTINGS = %w[
  41. bufhidden
  42. buflisted
  43. buftype
  44. colorcolumn
  45. concealcursor
  46. conceallevel
  47. cursorline
  48. foldcolumn
  49. foldlevel
  50. list
  51. modifiable
  52. number
  53. relativenumber
  54. spell
  55. swapfile
  56. synmaxcol
  57. textwidth
  58. wrap
  59. ]
  60. KNOWN_SETTINGS = GLOBAL_SETTINGS + LOCAL_SETTINGS
  61. def initialize
  62. @settings = []
  63. end
  64. def set(setting, value)
  65. raise "Unknown setting #{setting}" unless KNOWN_SETTINGS.include?(setting)
  66. case value
  67. when TrueClass, FalseClass
  68. @settings.push([setting, get_bool(setting)]) if global?(setting)
  69. set_bool setting, value
  70. when Numeric
  71. @settings.push([setting, get_number(setting)]) if global?(setting)
  72. set_number setting, value
  73. when String
  74. @settings.push([setting, get_string(setting)]) if global?(setting)
  75. set_string setting, value
  76. end
  77. end
  78. def restore
  79. @settings.each do |setting, value|
  80. case value
  81. when TrueClass, FalseClass
  82. set_bool setting, value
  83. when Numeric
  84. set_number setting, value
  85. when String
  86. set_string setting, value
  87. end
  88. end
  89. end
  90. private
  91. def global?(setting)
  92. GLOBAL_SETTINGS.include?(setting)
  93. end
  94. def get_bool(setting)
  95. ::VIM::evaluate("&#{setting}").to_i == 1
  96. end
  97. def get_number(setting)
  98. ::VIM::evaluate("&#{setting}").to_i
  99. end
  100. def get_string(name)
  101. ::VIM::evaluate("&#{name}").to_s
  102. end
  103. def set_bool(setting, value)
  104. command = global?(setting) ? 'set' : 'setlocal'
  105. setting = value ? setting : "no#{setting}"
  106. ::VIM::command "#{command} #{setting}"
  107. end
  108. def set_number(setting, value)
  109. command = global?(setting) ? 'set' : 'setlocal'
  110. ::VIM::command "#{command} #{setting}=#{value}"
  111. end
  112. alias set_string set_number
  113. end # class Settings
  114. end # module CommandT