Python plugin: Add examples
[claws.git] / src / plugins / python / examples / compose / Macro-Expansion
1 # -*- coding: utf-8 -*-
2
3 # If the cursor in the compose window body is on a word
4 # that is listed in this table as a key (element before the colon),
5 # this word is replaced with the value (element after the colon).
6 # To add key/value pairs, just add more lines.
7 replacement_table = {
8     "dsm"   : "Dear Sir/Madam,\n",
9     "th"    : "Thanks,\nHolger",
10     "blake" : "And did those feet in ancient time\nWalk upon England's mountains green?\nAnd was the holy Lamb of God\nOn England's pleasant pastures seen?",
11 }
12
13 # helper function to get the current word under the cursor
14 def get_current_word(buffer):
15     start = buffer.get_iter_at_mark(buffer.get_insert())
16     end = buffer.get_iter_at_mark(buffer.get_insert())
17     if not start.starts_word():
18         start.backward_word_start()
19     if not end.ends_word():
20         end.forward_word_end()
21     return (start.get_text(end), start, end)
22     
23
24 buffer = clawsmail.compose_window.text.get_buffer()         # get text buffer of body editor
25 (current_word, start, end) = get_current_word(buffer)       # get current word under the cursor
26 if current_word in replacement_table:                       # if current word is a key in the replacement table...
27     buffer.delete(start, end)                               # delete the current word
28     buffer.insert(start, replacement_table[current_word])   # and insert the replacement in its place