-
Notifications
You must be signed in to change notification settings - Fork 8
/
signature-motto.el
124 lines (118 loc) · 5.08 KB
/
signature-motto.el
1
2
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
;; -*- coding: utf-8 -*-
;; File: signature-motto.el
;;
;; Author: Denny Zhang(https://www.dennyzhang.com/contact)
;; Copyright 2020, https://DennyZhang.com
;; Created:2008-10-01
;; Updated: Time-stamp: <2020-02-03 15:37:41>
;;
;; --8<-------------------------- separator ------------------------>8--
(setq common-tail-signature "Denny Zhang(张巍)
Email: [email protected]
Website: https://www.dennyzhang.com
LinkedIn: https://linkedin.com/in/dennyzhang001
GitHub: https://github.com/DennyZhang")
(defun get-mail-signature ()
(format "%s\n\n%s" common-tail-signature (generate-mail-signature)))
(defun get-short-mail-signature ()
(format "%s\n\n%s" "Denny Zhang(张巍)" (generate-mail-signature)))
(defun generate-mail-signature()
(let* (signature-string cowsay-file command-string)
(if (member mode-name '("Message" "Article"))
;; if in the buffer of sending mail, conditionally pick English motto
(save-excursion
(goto-char (point-min))
(if (search-forward-regexp
"From: .*\\(@qq.com\\|@163.com\\|@126.com\\)"
nil t )
(setq signature-string (get-motto))
;; only English motto
(setq signature-string (get-motto 't)))
)
(setq signature-string (get-motto)))
;; cowsay doesn't support Unicode in release version
;; see http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=254557
(if (and (member (detect-coding-string signature-string)
'((undecided-unix) (undecided)))
(unless (null (executable-find "cowthink"))))
(progn
(setq cowsay-file (get-random-cowsay ".*.cow"))
(setq command-string (format "cowthink -f %s \"%s\""
cowsay-file
(decode-coding-string
signature-string (intern
"utf-8"))
))
(format "%s" (shell-command-to-string command-string)))
;; for Unicode like Chinese, just render cowsay with empty content
(progn
(with-temp-buffer
(insert signature-string)
(goto-char (point-min))
(fill-paragraph)
(setq signature-string
(buffer-substring-no-properties (point-min) (point-max))))
(setq cowsay-file (get-random-cowsay ".*.txt"))
(format "%s\n%s" signature-string
(with-temp-buffer
(insert-file-contents cowsay-file)
(buffer-string)))
)
)
))
(defun get-random-cowsay (match)
(let ((files (directory-files (concat CONF-DENNY-EMACS "/cowsay") t match)))
(nth (random (length files)) files)))
(defun get-all-motto ()
(let ((old-agenda-files org-agenda-files) signature-list)
(setq org-agenda-files (list (concat CONF-SHARE-DIR "/org_data/life/motto.org")))
(setq signature-list (org-tags-view-list "Motto"))
(setq org-agenda-files old-agenda-files)
(setq signature-list signature-list) ;; TODO more graceful way to return value
))
(setq motto-list (get-all-motto)) ;; variable to hold all motto
(defun get-motto(&optional englishp &optional max-length)
(let (signature-string (signature-list motto-list))
(if (null englishp) (setq englishp nil))
(if (null max-length) (setq max-length 500))
;; remove item longer than max-length
(setq signature-list
(remove-if #'(lambda(x) (> (length x) max-length)) signature-list))
;; remove item for non-English
(if englishp
(setq signature-list
(remove-if #'(lambda(x)
(not (equal '(undecided)
(detect-coding-string x)))) signature-list)))
(setq signature-string (random-string signature-list))
(eval signature-string)
))
(defun refresh-signature()
"auto refresh mail signature"
(interactive)
(save-excursion
(goto-char (point-min))
(if (re-search-forward common-tail-signature nil t)
(delete-region (- (point) (length common-tail-signature)) (point-max))
(goto-char (point-max)))
(insert (get-mail-signature))))
(define-key message-mode-map (kbd "<f5>") 'refresh-signature)
(defun random-string (string-list)
"get random string from a string-list"
(let ((rn (random (length string-list))))
(concat (nth rn string-list) "\n")))
(defun cowsay-quote-region (begin end)
"Quote a region with a cowsay figure."
(interactive "r")
(let ((content (buffer-substring-no-properties begin end)))
(if (member (detect-coding-string content) '((undecided-unix) (undecided)))
(progn
(setq command-string (format "cowthink -f %s -W %d \"%s\""
(get-random-cowsay ".*.cow")
(max-line-length begin end)
content))
(shell-command command-string))
(message "Error: don't supported for Unicode characters")
)))
;; --8<-------------------------- separator ------------------------>8--
;; File: signature-motto.el ends here