Class: Styles

Inherits:
Object show all
Defined in:
lib/xiki/styles.rb

Class Method Summary collapse

Class Method Details

.apply(pattern, *styles) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/xiki/styles.rb', line 152

def self.apply(pattern, *styles)
  # Make back-slashes double
  pattern.gsub!("\\", '\\\\\\\\')
  code = "(font-lock-add-keywords nil '(( \"#{pattern}\"\n"
  styles.each_with_index do |f, i|
    code << "  (#{i} '#{f.to_s.gsub("_", "-")})\n" if f
  end
  code << "  )))"
  $el.el4r_lisp_eval code
  $el.font_lock_mode 1
  nil
end

.attribute(style, attribute) ⇒ Object

Styles.attribute “mode-line”, “background” Styles.attribute “mode-line”, “box”



198
199
200
# File 'lib/xiki/styles.rb', line 198

def self.attribute style, attribute
  $el.el4r_lisp_eval "(face-attribute '#{style} :#{attribute})"
end

.clearObject

Don’t format quotes (it can override other styles)



186
187
188
# File 'lib/xiki/styles.rb', line 186

def self.clear
  $el.el4r_lisp_eval "(setq font-lock-defaults '(nil t))"
end

.dark_bg?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/xiki/styles.rb', line 97

def self.dark_bg?
  Styles.attribute(:default, :background)[1..1][/[0-7]/]
end

.define(name, options) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/xiki/styles.rb', line 113

def self.define name, options

  return if ! $el

  self.exand_colors options, [:bg, :fg]

  code = "(set-face-attribute (make-face '#{name.to_s.gsub("_", "-")}) nil\n"
  code << "  :background \"##{options[:bg]}\"\n" if options[:bg]
  code << "  :foreground \"##{options[:fg]}\"\n" if options[:fg]
  code << "  :family \"#{options[:face]}\"\n" if options[:face]

  if options[:size]
    size = options[:size]
    size = self.size(size.to_i) if size.is_a? String   # If a string, convert to relative size
    code << "  :height #{size}\n"
  end

  code += options[:strike] ? "  :strike-through t\n" : "  :strike-through nil\n" if options.has_key?(:strike)

  code += options[:underline] ? "  :underline t\n" : "  :underline nil\n" if options.has_key?(:underline)
  code += options[:overline] ? "  :overline t\n" : "  :overline nil\n" if options.has_key?(:overline)

  code += options[:bold] ? "  :weight 'bold\n" : "  :weight 'normal\n" if options.has_key?(:bold)
  if options[:border]
    border = options[:border]
    code <<
      if border.class == Symbol
        ":box nil\n"
      elsif border.class == String
        ":box '(:line-width 1 :color \"##{border}\")\n"
      elsif border.class == Array
        ":box '(:line-width #{border[1]} :color \"##{border[0]}\" :style #{border[2] || 'nil'})\n"
      end
  end
  code << "  )"

  $el.el4r_lisp_eval code
end

.exand_colors(options, keys) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/xiki/styles.rb', line 101

def self.exand_colors options, keys
  keys.each do |key|
    next if ! options[key]
    options[key].sub! /^#/, ''
    options[key].sub! /^(.)(.)(.)$/, "\\1\\1\\2\\2\\3\\3"
  end
end

.font_size(size = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/xiki/styles.rb', line 44

def self.font_size size=nil

  # If nothing passed, show default sizes

  if ! size
    return "
      - original) #{Styles.height}
      - 110
      - 120
      - 135
      - 160
      - 200
      - 250
      "
  end

  Styles.define :default, :size=>size.to_i

  # To resize mode lines
  Notes.define_styles
  FileTree.define_styles

  nil
end

.heightObject



190
191
192
# File 'lib/xiki/styles.rb', line 190

def self.height
  $el.el4r_lisp_eval "(face-attribute 'default :height)"
end

.initObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/xiki/styles.rb', line 165

def self.init
  return if ! $el

  # Make 'arial black' work in Linux
  $el.el4r_lisp_eval %`
    (custom-set-variables
      '(face-font-family-alternatives '(
        ("arial black" "arial" "DejaVu Sans")
        ("arial" "DejaVu Sans")
        ("verdana" "DejaVu Sans")
        ;("verdana" "arial")
        ))
      '(global-font-lock-mode t nil (font-lock))
      ;'(font-lock-defaults '(nil t))  ; messes up Open List Faces
      '(show-trailing-whitespace t)
      '(font-lock-keywords-case-fold-search t)
      )
    `
end

.list_facesObject



88
89
90
91
92
93
94
95
# File 'lib/xiki/styles.rb', line 88

def self.list_faces
  $el.with(:save_window_excursion) do
    $el.list_faces_display
  end
  View.to_buffer "*Faces*"

  nil
end


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/xiki/styles.rb', line 3

def self.menu

  '
  - .font size/
  - .list faces/
  - api/
    > Summary
    | How to use the Styles class.  You can change the color and font of text.
    | You define styles, then make them apply to the text that matches
    | regular expression.
    |
    > Define
    | Styles.define :red, :bg => "d77"
    |
    > Apply
    | Styles.apply "apply", :red
    |
    > Define more complex font
    | Styles.define :blueish,
    |   :fg => "99e",
    |   :face => "verdana",
    |   :size => 90,
    |   :bold => true
    |
    > See
    | For styling specific text (not just a pattern):
    <<< @overlay/
  - see/
    <<< @css/list/
    <<< @themes/
  '
  #       > Apply multiple fonts and groups

end

.method_missing(*args) ⇒ Object



109
110
111
# File 'lib/xiki/styles.rb', line 109

def self.method_missing *args
  self.define *args
end

.reload_stylesObject



38
39
40
41
42
# File 'lib/xiki/styles.rb', line 38

def self.reload_styles
  Notes.define_styles
  FileTree.define_styles
  Color.define_styles
end

.size(relative = 0) ⇒ Object



202
203
204
205
206
207
# File 'lib/xiki/styles.rb', line 202

def self.size relative=0
  # Cache so subsequent calls don't have to look up from elisp
  size = $el.el4r_lisp_eval "(face-attribute 'default :height)"
  size ||= 90
  size + relative * 5
end

.toggleObject



80
81
82
83
84
85
86
# File 'lib/xiki/styles.rb', line 80

def self.toggle
  $el.elvar.font_lock_mode ?
    $el.font_lock_mode(0) :   # plain text
    $el.font_lock_mode(true)   # stylized text

  nil
end

.zoom(options = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/xiki/styles.rb', line 69

def self.zoom options={}

  increment = options[:out] ? -10 : 10
  increment *= 10 if Keys.prefix_u

  height = self.height
  height += increment
  self.font_size height
end