-
Notifications
You must be signed in to change notification settings - Fork 0
/
emacs_drag-drop_pdfs_paste_html_custom_templates.html
371 lines (327 loc) · 17.1 KB
/
emacs_drag-drop_pdfs_paste_html_custom_templates.html
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<!DOCTYPE html>
<html lang="en">
<head>
<!-- 2022-07-23 Sat 05:11 -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Emacs drag-drop pdfs, paste html, custom templates</title>
<meta name="generator" content="Org mode">
<meta name="author" content="ivanaf">
<link rel="stylesheet" type="text/css" href="css/org.css"/>
<link rel="icon" href="ico/favicon.ico" type="image/x- icon">
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2020 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<div class="head">
<div class="title">
<p>
<a href="index.html">Ivanaf</a>
</p>
</div>
<menu>
<ul class="org-ul">
<li><a href="index.html">Home</a></li>
<li><a href="journal.html">Journal</a></li>
<li><a href="about.html">About</a></li>
<li><a href="resume.html">Resume</a></li>
<li><a href="portfolio.html">Portfolio</a></li>
<li><a href="contact.html">Contact</a></li>
<li><a href="projects_ideas.html">Messy Ideas</a></li>
</ul>
</menu>
</div>
<p>
</p><h1>
Emacs drag-drop pdfs, paste html, custom templates
</h1><p>
</p>
<p>
<span class=page-date> <small>
2018-08-19, updated 2020-07-26 — <a href='journal.html#projects' class='projects tagbutton'>projects</a> <a href='journal.html#emacs' class='emacs tagbutton'>emacs</a> <a href='journal.html#tech' class='tech tagbutton'>tech</a> <a href='journal.html#blog' class='blog tagbutton'>blog</a>   <a href="simple_proof_weak_master_theorem.html">⇦Simple proof weak master theorem</a> – <a href="transfer_functions_in_python.html">Transfer Functions in python⇨</a>
</small> </span>
</p>
<nav id="table-of-contents">
<input id="toggle-toc" style="display: none; visibility: hidden;" type="checkbox">
<label for="toggle-toc">
<h2> <b> Table of Contents </b> </h2>
</label>
<div id="text-table-of-contents">
<ul>
<li><a href="#update">1. UPDATE:</a></li>
<li><a href="#pdf-files-drag-drop">2. PDF files drag-drop</a></li>
<li><a href="#custom-easy-templates">3. Custom easy templates</a></li>
<li><a href="#pasting-html-in-org-mode">4. Pasting HTML in org mode</a></li>
</ul>
</div>
</nav>
<div id="outline-container-update" class="outline-2">
<h2 id="update"><span class="section-number-2">1</span> UPDATE:</h2>
<div class="outline-text-2" id="text-1">
<p>
This documents my first experience with programming in elisp. In restrospect, I was really bad at it. This post then shows that you can still make something with elisp even if you are bad at it.
</p>
<p>
A slightly better example of making something with elisp is on my package clipboard2org, a tiny package to paste html or picture. See <a href="https://github.com/itf/clipboard2org/blob/master/clipboard2org.el">clipboard2org</a>
</p>
</div>
</div>
<div id="outline-container-pdf_files_drag-drop" class="outline-2">
<h2 id="pdf-files-drag-drop"><span class="section-number-2">2</span> PDF files drag-drop</h2>
<div class="outline-text-2" id="text-pdf-files-drag-drop">
<p>
I wanted to be able to drag and drop pdf files, as well as drag and drop
pdf URLs to emacs. The result of dragging should be to download the file
to a directory under the same directory as the org file you are editing
and then create a link to the file. It also allows for drag and drop of
image files.
</p>
<p>
The code was based on <a href="https://github.com/abo-abo/org-download">org download</a>. If it is not a pdf, or if it is but the download fails, it
fails back to the original drag and drop code.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span class="org-keyword">defun</span> <span class="org-function-name">org-file-copy-pdf</span> (fname)
(<span class="org-keyword">let*</span> ((path (substring fname 5))
(org-file-pdf-directory <span class="org-string">"./pdflib"</span>)
(name (file-name-nondirectory (file-name-sans-extension path))))
(make-directory org-file-pdf-directory <span class="org-builtin">:parents</span>)
(<span class="org-keyword">condition-case</span> nill
(copy-file (dnd-unescape-uri path) (expand-file-name (format <span class="org-string">"%s.pdf"</span> name) org-file-pdf-directory))
(<span class="org-warning">error</span>
())
)
(file-relative-name (expand-file-name (format <span class="org-string">"%s.pdf"</span> name) org-file-pdf-directory) <span class="org-string">"./"</span>))
)
(<span class="org-keyword">defun</span> <span class="org-function-name">org-file-insert</span> (fname)
(<span class="org-keyword">let*</span> ((img-regexp <span class="org-string">"</span><span class="org-string"><span class="org-regexp-grouping-backslash">\\</span></span><span class="org-string"><span class="org-regexp-grouping-construct">(</span></span><span class="org-string">[pP][nN][gG]</span><span class="org-string"><span class="org-regexp-grouping-backslash">\\</span></span><span class="org-string"><span class="org-regexp-grouping-construct">|</span></span><span class="org-string">[jJ][pP][eE]?[gG]</span><span class="org-string"><span class="org-regexp-grouping-backslash">\\</span></span><span class="org-string"><span class="org-regexp-grouping-construct">)</span></span><span class="org-string">\\>"</span>)
(pdf-regexp <span class="org-string">"</span><span class="org-string"><span class="org-regexp-grouping-backslash">\\</span></span><span class="org-string"><span class="org-regexp-grouping-construct">(</span></span><span class="org-string">[pP][dD][fF]</span><span class="org-string"><span class="org-regexp-grouping-backslash">\\</span></span><span class="org-string"><span class="org-regexp-grouping-construct">)</span></span><span class="org-string">\\>"</span>))
(<span class="org-keyword">cond</span>
((string-match img-regexp fname)
(insert <span class="org-string">"#+ATTR_ORG: :width 300\n"</span>)
(insert (concat <span class="org-string">"#+CAPTION: "</span> <span class="org-string">"\n"</span>))
(insert (format <span class="org-string">"[[%s]]"</span> fname))
(org-display-inline-images t t))
((string-match pdf-regexp fname)
(insert (format <span class="org-string">"[[file:%s][%s]]\n"</span> (org-file-copy-pdf fname) (file-name-nondirectory (file-name-sans-extension path)))))
(t (insert (format <span class="org-string">"[[%s]]\n"</span> fname))
)
)
)
)
(<span class="org-keyword">defun</span> <span class="org-function-name">org-file-dnd-fallback</span> (uri action)
(<span class="org-keyword">let</span> ((dnd-protocol-alist
(rassq-delete-all
'org-file-dnd-protocol
(copy-alist dnd-protocol-alist))))
(dnd-handle-one-url nil action uri)))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-file-dnd-protocol</span> (uri action)
(<span class="org-keyword">cond</span> ((eq major-mode 'org-mode)
(<span class="org-keyword">condition-case</span> nil
(org-file-insert uri)
(<span class="org-warning">error</span>
(org-file-dnd-fallback uri action))))
(t
(org-file-dnd-fallback uri action))))
(add-to-list 'dnd-protocol-alist '(<span class="org-string">"^file:"</span> . org-file-dnd-protocol))
(<span class="org-keyword">require</span> '<span class="org-constant">url</span>)
(<span class="org-keyword">defun</span> <span class="org-function-name">org-file-pdf-url-insert</span> (url)
(<span class="org-keyword">let*</span> ((org-file-pdf-directory <span class="org-string">"./pdflib"</span>)
(name (file-name-nondirectory url))
(path (expand-file-name (file-name-nondirectory url) org-file-pdf-directory))
(relative-path (file-relative-name path <span class="org-string">"./"</span>)))
(make-directory org-file-pdf-directory <span class="org-builtin">:parents</span>)
(url-copy-file url path t)
(insert (format <span class="org-string">"[[file:%s][%s]]\n"</span> relative-path name)))
)
(<span class="org-keyword">defun</span> <span class="org-function-name">org-file-pdf-url-dnd-fallback</span> (uri action)
(<span class="org-keyword">let</span> ((dnd-protocol-alist
(rassq-delete-all
'org-file-pdf-url-dnd-protocol
(copy-alist dnd-protocol-alist))))
(dnd-handle-one-url nil action uri)))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-file-pdf-url-dnd-protocol</span> (uri action)
(<span class="org-keyword">cond</span> ((eq major-mode 'org-mode)
(<span class="org-keyword">condition-case</span> nil
(org-file-pdf-url-insert uri)
(<span class="org-warning">error</span>
(org-file-pdf-url-dnd-fallback uri action))))
(t
(org-file-pdf-url-dnd-fallback uri action))))
(add-to-list 'dnd-protocol-alist '(<span class="org-string">"^https?.*\\.pdf"</span> . org-file-pdf-url-dnd-protocol))
</pre>
</div>
</div>
</div>
<div id="outline-container-custom_easy_templates" class="outline-2">
<h2 id="custom-easy-templates"><span class="section-number-2">3</span> Custom easy templates</h2>
<div class="outline-text-2" id="text-custom-easy-templates">
<p>
Easy templates cannot run arbitrary code. They are handled by org-cycle,
which is a function that handles every action that TAB performs in org
mode.
</p>
<p>
So, I created a function and added an advice to org-cycle, This detects
strings of the type "<code>>[a-zA-Z]+</code>", i.e. > followed by letters and then
runs a function.
</p>
<p>
In order to create other templates, add another block similar to
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">((string= key <span class="org-string">"t"</span>)
(insert-todays-date)
t)
</pre>
</div>
<p>
under it.
</p>
<p>
This particular template inserts today's date when someone writes >t and
presses tab.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span class="org-keyword">defun</span> <span class="org-function-name">insert-todays-date</span> ()
(<span class="org-keyword">interactive</span>)
(insert (format-time-string <span class="org-string">"<%Y-%m-%d>"</span>)))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-try-my-template-function</span> (<span class="org-type">&optional</span> arg)
<span class="org-doc">"Try to complete a structure template before point.</span>
<span class="org-doc">This looks for strings like \"<e\" on an otherwise empty line and</span>
<span class="org-doc">expands them."</span>
(<span class="org-keyword">interactive</span>)
(<span class="org-keyword">let</span> ((l (buffer-substring (point-at-bol) (point)))
a)
(<span class="org-keyword">if</span> (<span class="org-keyword">and</span> (looking-at <span class="org-string">"[ \t]*$"</span>)
(string-match <span class="org-string">"^[ \t]*></span><span class="org-string"><span class="org-regexp-grouping-backslash">\\</span></span><span class="org-string"><span class="org-regexp-grouping-construct">(</span></span><span class="org-string">[a-zA-Z]+</span><span class="org-string"><span class="org-regexp-grouping-backslash">\\</span></span><span class="org-string"><span class="org-regexp-grouping-construct">)</span></span><span class="org-string">$"</span> l))
(<span class="org-keyword">let*</span> ((key (match-string 1 l))
(start (point-at-bol))
(end (point)))
(<span class="org-keyword">when</span>
(<span class="org-keyword">cond</span>
((string= key <span class="org-string">"t"</span>)
(insert-todays-date)
t)
)
(delete-region start end)
t)
))))
(<span class="org-keyword">require</span> '<span class="org-constant">org</span>)
(<span class="org-keyword">with-eval-after-load</span> 'org
(advice-add #'org-cycle <span class="org-builtin">:before-until</span> #' org-try-my-template-function))
</pre>
</div>
</div>
</div>
<div id="outline-container-pasting_html_in_org_mode" class="outline-2">
<h2 id="pasting-html-in-org-mode"><span class="section-number-2">4</span> Pasting HTML in org mode</h2>
<div class="outline-text-2" id="text-pasting-html-in-org-mode">
<p>
This was inspired by
<a href="https://emacs.stackexchange.com/questions/12121/org-mode-parsing-rich-html-directly-when-pasting">this stack overflow question</a>. It suggested using <code>xclip</code> to access the
clipboard.
</p>
<p>
The problem is that <code>xclip</code> causes emacs to hang, because it forks a
child that keeps stdout open. So instead, one can use the emacs backed
for getting the xselection from the clipboard.
</p>
<p>
This code requests a text/html selection and checks if it exists. If it
exists, it first decodes it using whatever encoding was being used, and
then uses pandoc to convert the html to org mode.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span class="org-keyword">defun</span> <span class="org-function-name">html2org-clipboard</span> ()
<span class="org-doc">"Convert clipboard contents from HTML to Org and then paste (yank)."</span>
(<span class="org-keyword">interactive</span>)
(<span class="org-keyword">let*</span> (
(text_html (gui-backend-get-selection 'PRIMARY 'text/html))
(text_raw (gui-get-selection))
(text_html (<span class="org-keyword">when</span> text_html
(decode-coding-string text_html 'unix)))
(text_html (<span class="org-keyword">when</span> text_html
(shell-command-to-string (concat <span class="org-string">"echo "</span> (shell-quote-argument text_html) <span class="org-string">"|timeout 2 pandoc -f html-native_divs-native_spans -t org"</span>))))
(text (<span class="org-keyword">or</span> text_html
text_raw))
)
(<span class="org-keyword">progn</span> (kill-new text) (yank))))
(<span class="org-keyword">with-eval-after-load</span> 'org
(define-key org-mode-map (kbd <span class="org-string">"C-y "</span>) 'html2org-clipboard)
(define-key org-mode-map (kbd <span class="org-string">"C-<tab>"</span>) '(lambda() (<span class="org-keyword">interactive</span>) (<span class="org-keyword">save-excursion</span> (org-back-to-heading)
(org-cycle))))
)
</pre>
</div>
</div></div>
<br>
<div class="comments">
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'ivanaf'; // Required - Replace '<example>' with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
var showComments = function() {
var button = document.getElementById('comment-button')
button.style.display = 'none'
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
};
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<button id="comment-button" onclick="showComments()">Show comments</button>
</div>
<div><div>
</div>
</div>
</div>
<div id="postamble" class="status">
<p class="author">Author: Ivan Tadeu Ferreira Antunes Filho</p>
<p class="date">Date: 2022-07-23 Sat 05:11</p>
<p class="author">Github: <a href="https://github.com/itf/">github.com/itf</a></p>
<p class="creator">Made with <a href="https://www.gnu.org/software/emacs/">Emacs</a> 27.1 (<a href="https://orgmode.org">Org</a> mode 9.3) and <a href="https://github.com/itf/org-export-head">Org export head</a> </p>
</div>
</body>
</html>