-
Notifications
You must be signed in to change notification settings - Fork 2
/
helm-orgcard.el
140 lines (121 loc) · 4.33 KB
/
helm-orgcard.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
;;; helm-orgcard.el --- browse the orgcard by helm -*- lexical-binding: t -*-
;; Description: browse the orgcard by helm.
;; Author: Yuhei Maeda <yuhei.maeda_at_gmail.com>
;; Maintainer: Yuhei Maeda
;; Copyright (C) 2015 Yuhei Maeda all rights reserved.
;; Created: :2013-06-05
;; Version: 0.2
;; Keywords: convenience, helm, org
;; URL: https://github.com/emacs-jp/helm-orgcard
;; Package-Requires: ((helm-core "3.6.0"))
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Put the helm-orgcard.el to your
;; load-path.
;; Add to init.el:
;; (require 'helm-orgcard)
;;
;; Original version is anything-orgcard, and port to helm.
;; https://gist.github.com/kiwanami/1345100
;;
;;; Code:
(require 'helm-core)
(defgroup helm-orgcard nil
"Browse the orgcard by helm"
:group 'org)
(defcustom hoc-lang-selector 'en
"Select orgcard language. English or Japanese."
:type '(choice
(const :tag "English" en)
(const :tag "Japanese" ja))
:group 'helm-orgcard)
(defvar hoc-orgcard-url '((en "http://orgmode.org/orgcard.txt")
(ja "http://orgmode.jp/orgcard-ja.txt"))
"URL to the orgcard.")
(defvar hoc-orgcard-file '((en "~/.emacs.d/orgcard.txt")
(ja "~/.emacs.d/orgcard.ja.txt"))
"Path to the orgcard.")
(defun hoc-try-file ()
"[internal] Check the local file. If it does not exist, this
function retrieves from the URL."
(let ((file (expand-file-name
(cadr (assoc hoc-lang-selector hoc-orgcard-file))))
(url (cadr (assoc hoc-lang-selector hoc-orgcard-url))))
(unless (file-exists-p file)
(let ((buf (url-retrieve-synchronously url)))
(when buf
(with-current-buffer buf
(write-file file))
(kill-buffer buf))))
(unless (file-exists-p file)
(error "Can not get the orgcard file!"))))
(defun hoc-readline ()
"[internal] read a line."
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(defun hoc-create-sources ()
"[internal] create an helm source for orgcard."
(let (heads
cur-title
cur-subtitle
cur-records
(file (expand-file-name
(cadr (assoc hoc-lang-selector hoc-orgcard-file)))))
(with-temp-buffer
(insert-file-contents file)
(goto-char (point-min))
(forward-line 4) ; skip title
(while
(let ((line (hoc-readline)))
(cond
((equal "" line) nil) ; do nothing
((equal ?= (aref line 0)) ; header
(when cur-title ; flush records
(push
`((name . ,cur-title)
(candidates ,@cur-records)
(action
. (("Echo" . hoc-echo-action))))
heads))
(forward-line 1)
(setq cur-title (hoc-readline))
(setq cur-records nil cur-subtitle nil)
(forward-line 1))
((equal ?- (aref line 0)) ; subtitle
(forward-line 1)
(setq cur-subtitle (concat (hoc-readline) "# " ))
(forward-line 1))
(t ; normal line
(push (concat cur-subtitle line) cur-records)))
(forward-line 1)
(not (eobp)))))
(when cur-title ; flush the last records
(push
`((name . ,cur-title)
(candidates ,cur-records)
(action . (("Echo" . hoc-echo-action))))
heads))
(reverse heads)))
(defun hoc-echo-action (entry)
"[internal] popup an entry of orgcard."
(message entry)
;;(popup-tip entry)
nil)
;;;###autoload
(defun helm-orgcard ()
"Anything command for orgcard."
(interactive)
(hoc-try-file)
(helm (hoc-create-sources)))
(provide 'helm-orgcard)
;;; helm-orgcard.el ends here