-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.el
611 lines (531 loc) · 26.6 KB
/
funcs.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
;; -*- mode: emacs-lisp; lexical-binding: t -*-
;;
;; Top-level file for custom elisp functions. Loaded from init.el.
(defun my/funcs-init ()
"Top-level file for custom elisp functions. Loaded from init.el."
;; my-flyspell-correct-lucky needs flyspell-correct to be loaded so that
;; flyspell-correct-interface is defvar'd (otherwise the let, in
;; effect, does nothing).
(require 'flyspell-correct)
;; TODO: bad Spacemacs style to require modules directly; how to set up lazy autoload for helm-swoop like "SPC s s" does?
(require 'helm-swoop) ; for helm-multi-swoop-this-mode
(require 'ansi-color)) ; for display-ansi-colors
;; https://www.masteringemacs.org/article/searching-buffers-occur-mode
;;
;; with-current-buffer is slow; use buffer-local-value instead; see:
;; https://alphapapa.github.io/emacs-package-dev-handbook/
;;
;; helm-swoop.el already defines this and automatically invokes buffer-name on each result.
;; (defun get-buffers-matching-mode (mode)
;; "Returns a list of buffers where their major-mode is equal to MODE"
;; (let ((buffer-mode-matches '()))
;; (dolist (buf (buffer-list))
;; (with-current-buffer buf
;; (if (eq mode major-mode)
;; (add-to-list 'buffer-mode-matches buf))))
;; buffer-mode-matches))
;;
;; (defun multi-occur-this-mode ()
;; "Show all lines matching REGEXP in buffers with this major mode."
;; (interactive)
;; (multi-occur
;; (get-buffers-matching-mode major-mode)
;; (car (occur-read-primary-args))))
;; For an example how to do things like this with helm-swoop, see helm-multi-swoop-projectile
;; TODO: make a dynamic-let macro for temporarily changing values of special variables (or try harder to find the standard approach on the internet)
;; TODO: just use a regular let here, Elisp should notice helm-swoop-prompt is a special variable and dynamically let-assign it.
(defun helm-multi-swoop-this-mode (&optional $query)
"Helm-swoop in buffers matching current major mode."
(interactive)
(let (($p helm-swoop-prompt))
(unwind-protect
(progn
;; change the **dynamic** binding with (set 'var val) so that helm-swoop sees it, if we (setq var val) it changes the lexical binding
;; https://emacs.stackexchange.com/questions/27581/why-do-setq-and-set-quote-act-differently-on-let-bound-variables-with-lexical-sc
(set 'helm-swoop-prompt "Swoop (in current major mode):")
(setq helm-multi-swoop-query (helm-multi-swoop--get-query $query))
(helm-multi-swoop--exec nil
:$query helm-multi-swoop-query
:$buflist (get-buffers-matching-mode major-mode)))
(set 'helm-swoop-prompt $p))))
;; https://emacs.stackexchange.com/questions/7742/what-is-the-easiest-way-to-open-the-folder-containing-the-current-file-by-the-de
;; need xsel under Linux; xclip has some problem when copying under Linux
(defun copy-yank-str (msg &optional clipboard-only)
(unless clipboard-only (kill-new msg))
(cond
;; display-graphic-p need windows 23.3.1
((and (display-graphic-p) select-enable-clipboard)
(gui-set-selection 'CLIPBOARD msg))
(t (with-temp-buffer
(insert msg)
(shell-command-on-region (point-min) (point-max)
(cond
((eq system-type 'cygwin) "putclip")
((eq system-type 'darwin) "pbcopy")
(t "xsel -ib")))))))
(defun copy-fullpath-of-current-buffer ()
"Show the absolute filename of current buffer and copy it into the kill ring and OS clipboard."
(interactive)
(when buffer-file-name
(let ((absolute-filename (file-truename buffer-file-name)))
(copy-yank-str absolute-filename)
(message "`%s'" absolute-filename))))
;; https://www.emacswiki.org/emacs/UnfillParagraph
;; Stefan Monnier <foo at acm.org>. It is the opposite of fill-paragraph.
(defun unfill-paragraph (&optional region)
"Takes a multi-line paragraph and makes it into a single line of text."
(interactive (progn (barf-if-buffer-read-only) '(t)))
(let ((fill-column (point-max))
;; This would override `fill-column' if it's an integer.
(emacs-lisp-docstring-fill-column t))
(fill-paragraph nil region)))
(defvar dedicated-terminal-command-template "gnome-terminal --working-directory='%s'" "Command used by `dedicated-terminal-f'. Must contain exactly one `\%s', for the path to open the terminal in.")
(defun dedicated-terminal-f (&optional directory)
"Open a terminal window, setting cwd to directory (default: directory of current buffer)."
(let (($dir (expand-file-name (if directory directory default-directory))))
(if (and $dir (file-directory-p $dir))
(progn
(message (format "terminal in `%s'" $dir))
(start-process-shell-command "emacs-dedicated-terminal" nil (format dedicated-terminal-command-template $dir)))
(message "no such directory `%s'" $dir))))
(defvar dedicated-file-browser-template "/usr/bin/nemo '%s'" "Command used by `browse-file-f'. Must contain exactly one `\%s', for the path to open the file browser in.")
(defun browse-file-f (&optional directory)
"Open a file browser in directory (default: directory of current buffer)."
(let (($dir (expand-file-name (if directory directory default-directory))))
(if (and $dir (file-directory-p $dir))
(progn
(message (format "file browser in `%s'" $dir))
(start-process-shell-command "emacs-dedicated-file-browser" nil (format dedicated-file-browser-template $dir)))
(message "no such directory `%s'" $dir))))
(defun pyan-f (&optional directory)
"Run Pyan, the Python static call graphing tool. Visualize results in xdot."
(interactive)
(let (($dir (expand-file-name (if directory directory default-directory))))
(if (and $dir (file-directory-p $dir))
(progn
(message (format "Python call graph analysis in `%s'" $dir))
;; (start-process-shell-command "pyan" nil (format "cd \"%s\" && shopt -s globstar && pyan3 -nuca --dot **/*.py | xdot" $dir))
(start-process-shell-command "pyan" nil (format "cd \"%s\" && find . -not -iwholename '*/test*/*' -name '*.py' | xargs pyan3 -nuca --dot | xdot" $dir))
)
(message "no such directory `%s'" $dir))))
;; TODO: detect whether projectile is available (using condition-case)
(defun call-in-current-or-project-directory (func in-project-root)
"Call `func' with one argument, set to the directory of the current buffer or the project root from Projectile.
If `in-project-root' is `nil', use the currect directory. If non-nil, use the project root."
(if in-project-root
(let (($dir (projectile-project-root)))
(if $dir
(funcall func (expand-file-name $dir))
(message "not in a project")))
(if default-directory
(funcall func (expand-file-name default-directory))
(message "current buffer not associated with a directory"))))
(defun copy-fullpath-of-current-directory (&optional in-project-root)
"Show the path of current buffer and copy the path to the kill ring and OS clipboard.
If `in-project-root' is non-nil (can be set interactively by setting the
universal argument), show and copy root path of project (queried from Projectile)."
(interactive "P")
(defun $copy-and-msg (directory)
(copy-yank-str directory)
(message "`%s'" directory))
(call-in-current-or-project-directory '$copy-and-msg in-project-root))
(defun browse-file-directory (&optional in-project-root)
"Open the directory of the current buffer however the OS would.
If `in-project-root' is non-nil (can be set interactively by
setting the universal argument), instead open root directory of
project (queried from Projectile).
On WSL, instead of opening the directory as the OS would, this
calls `browse-file-f', because the Linux filesystem is not
visible to Windows, and so the directories in it cannot be
browsed in Windows Explorer.
"
(interactive "P")
;; Emacs 28.1 changed browse-url, adding `browse-url-handlers'.
;; Using `browse-url-of-file', directories now open Dired by default.
;; Tried this customization, but it didn't work:
;; (list (cons #'file-directory-p #'browse-file-f))
;; so we now bypass browse-url, and always use our custom `browse-file-f'.
;; Note our `dedicated-file-browser-template' must be correctly set for this to work.
(call-in-current-or-project-directory
'browse-file-f
in-project-root))
(defun open-dedicated-terminal (&optional in-project-root)
"Open a system terminal window.
With no argument or `nil', set terminal cwd to directory of current buffer.
If `in-project-root' is non-nil (can be set interactively by setting the
universal argument), set terminal cwd to root directory of project (queried
from Projectile)."
(interactive "P")
(call-in-current-or-project-directory 'dedicated-terminal-f in-project-root))
(defun run-pyan (&optional in-project-root)
"Run Pyan, the Python static call graphing tool. Visualize results in xdot.
With no argument or `nil', set terminal cwd to directory of current buffer.
If `in-project-root' is non-nil (can be set interactively by setting the
universal argument), set terminal cwd to root directory of project (queried
from Projectile)."
(interactive "P")
(call-in-current-or-project-directory 'pyan-f in-project-root))
(defun run-buffer-in-python ()
"Start an inferior-python and send the current buffer to it.
Only has an effect if the current buffer is in the 'python-mode major mode.
Reuses existing python shell if already running.
"
(interactive)
(when (eq major-mode 'python-mode)
(let (($b (current-buffer)))
(message "Running buffer contents in python shell...")
(run-python)
(with-current-buffer $b
(python-shell-send-buffer 'send-main)))))
;; (defun ansi-term-bash ()
;; "Run /bin/bash in ansi-term."
;; (interactive)
;; (message "bash in ansi-term")
;; (ansi-term "/bin/bash")
;; )
;; based on https://emacs.wordpress.com/2007/01/17/eval-and-replace-anywhere/
(defun eval-and-replace-sexp ()
"Replace the preceding sexp with its value.
To support modes that automatically match parens, if point is on a closing
paren, it is first moved forward until it isn't, to eliminate the need
to type (and hence skip over) the closing paren before invoking
eval-and-replace-sexp.
Useful as an inline calculator.
"
(interactive)
(while (and (char-after) (char-equal (char-after) ?\))) ; HACK for convenience in Lisp mode (no need to type closing paren first)
(forward-char))
(backward-kill-sexp)
(condition-case nil
(progn
(message "eval & replace sexp, original => kill ring")
(prin1 (eval (read (current-kill 0)))
(current-buffer)))
(error (message "eval-and-replace-sexp: invalid expression")
(insert (current-kill 0)))))
;; TODO: see if someone has already implemented this
(defun my-make-repeatable-interactive-command (command &optional new-name)
"Make a repeatable version of an existing interactive command.
COMMAND is the command name as a symbol.
If NEW-NAME (a symbol) is given, globally bind that function
name (at the Lisp level) to the new function, as if the new
function was DEFUN'd with that name.
Return the new command, which behaves exactly as the original,
except that it accepts a numeric prefix argument to specify how
many times to repeat.
Prefix argument semantics for the new command:
With no prefix argument, run the original command once.
With the universal prefix argument, run the original command
once, passing the universal prefix argument to it.
NOTE: currently more than one `C-u' is not supported.
If the prefix argument is numeric and positive, repeat the
original command that many times.
If the prefix argument is numeric and negative, repeat the
original command as many times as the absolute value of the
argument. Each time, pass the universal argument to the
original command.
If the prefix argument is numeric and zero, do nothing.
"
(let (($newcmd (lambda (&optional num)
;; https://emacs.stackexchange.com/questions/44407/how-to-implement-a-numeric-prefix-argument-in-emacs
;; TODO: support several C-u prefixes
(interactive (list (if current-prefix-arg
(if (equal current-prefix-arg '(4)) ;; C-u
-1
(prefix-numeric-value current-prefix-arg))
1)))
(let (($parg (if (< num 0) '(4) nil)) ;; simulate C-u
($n (abs num)))
(dotimes ($k $n nil) ;; TODO: return the return value from the final repeat
(funcall command $parg)))))) ;; TODO: should probably use (call-interactively command), but how to pass the prefix arg then?
(when new-name
;; https://www.gnu.org/software/emacs/manual/html_node/elisp/Function-Cells.html#Function-Cells
(fset new-name $newcmd))
$newcmd))
(defun switch-to-next-file (&optional backwards)
"Switch current window to next file.
If `backwards' is non-nil (can be set interactively by setting the
universal argument), switch to previous file.
A buffer is skipped as not representing a file, if:
- its name begins and ends with `*'
- its purpose is \"general\", \"dired\" or \"minibuf\"
- it matches a special case, e.g. a Dired view or the file
\".custom-settings\", which Spacemacs uses as a staging area
to update `dotspacemacs/emacs-custom-settings' in init.el
(so it occasionally automatically opens in the background).
"
(interactive "P")
(catch 'stnf-exit
(let (($seen (make-hash-table))
($orig (current-buffer)))
(defun $next ()
(if backwards (previous-buffer) (next-buffer))
(when (gethash (buffer-name) $seen) ; looped around?
(message "no files open")
(switch-to-buffer $orig)
(throw 'stnf-exit nil))
(puthash (buffer-name) 1 $seen))
(defun $rejectp ()
(let* (($n (buffer-name))
($p (purpose-buffer-purpose $n)))
(and
(not (s-starts-with? "untitled" $n))
(or
(and (s-starts-with? "*" $n) (s-ends-with? "*" $n))
(s-starts-with? "Dired " $n)
(s-starts-with? ".custom-settings" $n)
(equal $p 'general)
(equal $p 'dired)
(equal $p 'minibuf)))
))
($next)
(while ($rejectp)
($next))
(if (eq (current-buffer) $orig)
(message "no other files open")
(message "%s file" (if backwards "previous" "next"))))))
;; this version is not as useful, since (buffer-list) gives the buffers in most-recently-used order.
;; (defun switch-to-next-file (&optional backwards)
;; "Switch current window to next file.
;;
;; If `backwards' is non-nil (can be set interactively by setting the
;; universal argument), switch to previous file.
;;
;; A buffer is skipped as not representing a file, if:
;; - its name begins and ends with `*'
;; - its purpose is `general', `dired' or `minibuf'
;; "
;; (interactive "P")
;; (catch 'stnf-exit
;; (defun $rejectp (buf)
;; (let (($n (string-trim (buffer-name buf)))
;; ($p (purpose-buffer-purpose buf)))
;; (and
;; (not (s-starts-with? "untitled" $n))
;; (or
;; (and (s-starts-with? "*" $n) (s-ends-with? "*" $n))
;; (s-starts-with? "Dired " $n)
;; (equal $p 'general)
;; (equal $p 'dired)
;; (equal $p 'minibuf)))))
;; (let (($bufs (cdr (buffer-list (selected-frame))))) ; current buffer is first, skip it
;; (when backwards (setq $bufs (reverse $bufs)))
;; (dolist ($buf $bufs nil)
;; (when (not ($rejectp $buf))
;; (message "%s file" (if backwards "previous" "next"))
;; (switch-to-buffer $buf)
;; (throw 'stnf-exit nil)))
;; (message (if ($rejectp (current-buffer)) "no files open" "no other files open"))
;; )))
(defun switch-to-previous-file (&optional forwards)
"Switch current window to previous file.
Shorthand for `(switch-to-next-file (not forwards))',
i.e. `(switch-to-next-file t)' when no arg given.
The optional argument is there to support negative numeric args
for `switch-to-previous-file-repeatable'.
"
(interactive)
(switch-to-next-file (not forwards)))
(my-make-repeatable-interactive-command 'switch-to-next-file 'switch-to-next-file-repeatable)
(my-make-repeatable-interactive-command 'switch-to-previous-file 'switch-to-previous-file-repeatable)
;; based on https://www.emacswiki.org/emacs/BookmarkPlus#toc63
(defun my-auto-l+c-name (position)
"Return a name for POSITION that uses line & column numbers."
(let* ((line (line-number-at-pos position))
(col0 (save-excursion
(goto-char position) (current-column)))
(col (if (bound-and-true-p column-number-indicator-zero-based) col0 (1+ col0)))
(fn0 (condition-case nil
(which-function)
(error nil)))
(fn (if fn0 (format ":%s" fn0) "")))
(format "%s:%d,%d%s" (buffer-name) line col fn)))
;; https://emacs.stackexchange.com/questions/42529/insert-date-using-a-calendar
(defun calendar-insert-date ()
"Capture the date at point, exit the Calendar, insert the date."
(interactive)
(seq-let (month day year) (save-match-data (calendar-cursor-to-date))
(calendar-exit)
(insert (format "%d-%02d-%02d" year month day))))
(defun toggle-minimap ()
"Toggle the minimap."
(interactive)
;; (minimap-mode 'toggle)
(sublimity-mode 'toggle))
(defun select-all (&optional leave-point-at-end)
"Mark all text in the current buffer.
With no args, leave point at start of buffer. With the universal arg,
leave point at end of buffer."
(interactive "P")
(if leave-point-at-end
(progn
(goto-char (point-min))
(push-mark (point) 'nomsg 'activate)
(goto-char (point-max)))
(progn
(goto-char (point-max))
(push-mark (point) 'nomsg 'activate)
(goto-char (point-min))))
(message "Whole buffer marked"))
;; https://stackoverflow.com/questions/8606954/path-and-exec-path-set-but-emacs-does-not-find-executable
;; see also https://github.com/purcell/exec-path-from-shell
(defun set-exec-path-from-shell-PATH ()
"Set up Emacs' `exec-path' and PATH environment variable to match that used by the user's shell.
This is particularly useful under Mac OSX, where GUI apps are not started from a shell."
(interactive)
(let ((path-from-shell (replace-regexp-in-string "[ \t\n]*$" "" (shell-command-to-string "$SHELL --login -i -c 'echo $PATH'"))))
(setenv "PATH" path-from-shell)
(setq exec-path (split-string path-from-shell path-separator))))
;; --------------------------------------------------------------------------------
(defun my-flyspell-correct-lucky (&rest args)
"`I'm feeling lucky' mode for flyspell-correct-previous.
Accept the first suggestion without prompting.
"
(interactive "P")
(let ((misspelled nil)
(candidate nil))
(let ((flyspell-correct-interface (lambda (candidates misspelled-word)
(setq misspelled misspelled-word)
(setq candidate (car candidates))
candidate)))
(call-interactively 'flyspell-correct-previous)
(let ((unlucky-key (substitute-command-keys "\\[my-flyspell-correct-unlucky]")))
(if (eq candidate nil)
(progn
;; Prevent undo of unrelated stuff in unlucky mode.
;; `this-command' becomes `last-command' when the *next* command runs.
(set 'this-command 'my-flyspell-correct-lucky--failed)
(message (format "No suggestions for last typo before point (%s); hit %s to correct interactively" misspelled unlucky-key)))
(progn
(set 'this-command 'my-flyspell-correct-lucky--success)
(message (format "Last typo before point autozapped (%s → %s); hit %s to re-correct interactively" misspelled candidate unlucky-key))))))))
(defun my-flyspell-correct-unlucky (&rest args)
"Wrapper for flyspell-correct-previous.
If invoked right after a `my-flyspell-correct-lucky', undo first.
This allows first hitting \\[my-flyspell-correct-lucky], which is
often sufficient, but if the default suggestion was not correct,
it is then possible to immediately hit \\[my-flyspell-correct-unlucky]
to correct interactively with minimum keypresses."
(interactive "P")
(when (eq last-command 'my-flyspell-correct-lucky--success)
(undo-tree-undo)
;; Undoing the edit does not restore the misspelled-word status.
;; Force flyspell to update its overlay.
;; TODO: we assume the word that used to be misspelled is now on screen.
(flyspell-region (window-start) (window-end)))
(call-interactively 'flyspell-correct-previous))
(defun my-flyspell-correct (&rest args)
"One-key typo zapper.
Combines the functionality of `my-flyspell-correct-lucky' and
`my-flyspell-correct-unlucky'.
It is recommended to bind this function to a key. When the key is
first hit, this auto-zaps the last typo before point, using the first
suggestion provided by `flyspell'. If immediately hit again, you'll
get a prompt to choose which correction to use.
"
(interactive "P")
(if (or (eq last-command 'my-flyspell-correct--success) (eq last-command 'my-flyspell-correct--failed))
(progn
(when (eq last-command 'my-flyspell-correct--success)
(undo-tree-undo)
;; Undoing the edit does not restore the misspelled-word status.
;; Force flyspell to update its overlay.
;; TODO: we assume the word that used to be misspelled is now on screen.
(flyspell-region (window-start) (window-end)))
(call-interactively 'flyspell-correct-previous))
(progn
(let ((misspelled nil)
(candidate nil))
(let ((flyspell-correct-interface (lambda (candidates misspelled-word)
(setq misspelled misspelled-word)
(setq candidate (car candidates))
candidate)))
(call-interactively 'flyspell-correct-previous)
(let ((our-key (substitute-command-keys "\\[my-flyspell-correct]")))
(if (eq candidate nil)
(progn
(set 'this-command 'my-flyspell-correct--failed)
(message (format "No suggestions for last typo before point (%s); hit %s again to correct interactively" misspelled our-key)))
(progn
(set 'this-command 'my-flyspell-correct--success)
(message (format "Last typo before point autozapped (%s → %s); hit %s again to re-correct interactively" misspelled candidate our-key))))))))))
;; --------------------------------------------------------------------------------
(defun unparenthesize-python-return-stmts ()
"Unparenthesize all return values in Python return statements from point forward."
(interactive)
(while (search-forward "return(" nil t 1)
(backward-char)
(insert-char #x20 1 t)
(sp-unwrap-sexp)))
(defun newline-and-indent-relative ()
"Start a new line and call indent-relative.
Useful when writing Python docstrings, where anaconda-mode
insists on indenting to the level of the def, even in the
parameter list in the docstring."
(interactive)
(if electric-indent-mode
(electric-indent-just-newline nil)
(newline))
(indent-relative))
(defun insert-key-description ()
"Insert a `(kbd ...)' compatible description string for a key sequence.
Prompts for the key sequence."
(interactive)
(let* (($raw (read-key-sequence "Key sequence to insert:" nil t nil nil))
($k (key-description $raw)))
(insert (concat "\"" $k "\""))
(message "")))
;; TODO: Where does the second "View command" prompt come from, it's not in the AUCTeX sources?
;; TODO: Call the original if called with a prefix argument.
(defun my-default-TeX-command (&optional override-confirm)
"Like TeX-command-master, but use the default command."
(interactive "P")
(TeX-command (TeX-command-default (TeX-master-file nil nil t))
'TeX-master-file override-confirm))
;; By StackOverflow user juanleon, from:
;; https://stackoverflow.com/questions/23378271/how-do-i-display-ansi-color-codes-in-emacs-for-any-mode
(defun display-ansi-colors-region ()
"Interpret ANSI color sequences in the active region.
The sequences become overlays, making the text colored,
just like in the terminal. Other overlays in the region
are discarded.
Useful for reading colored log files and similar.
Works also in read-only buffers."
(interactive)
(let ((inhibit-read-only t))
(ansi-color-apply-on-region (point-min) (point-max))))
;; --------------------------------------------------------------------------------
;; Scale org-mode LaTeX fragments in a frame zoomed using Spacemacs's `zoom-frm.el'.
;;
;; https://emacs.stackexchange.com/questions/3387/how-to-enlarge-latex-fragments-in-org-mode-at-the-same-time-as-the-buffer-text
;;
;; Also based on the code of `zoom-frm.el' and `frame-cmds.el' in Spacemacs.
;;
(defun my/get-current-fontsize ()
"Get the font size of the current frame, in points, as an integer."
(let ((fontname (cdr (assq 'font (frame-parameters (selected-frame))))))
(let ((xlfd-fields (x-decompose-font-name fontname)))
(unless xlfd-fields (error "Cannot decompose font name"))
(string-to-number (aref xlfd-fields xlfd-regexp-pixelsize-subnum)))))
(defun my/get-font-scaling-factor ()
"Compute the font scaling factor used by Spacemacs's `zoom-frm.el'."
;; zoom-frm.el's `zoom-factor' is just the number of zoom steps taken
;; above (positive) or below (negative) the original base font size.
;; So to get the actual font scaling factor, we compare the current size
;; to the base size.
;;
;; See `zoom-frm-in' in `zoom-frem.el', and `enlarge-font' in `frame-cmds.el'.
(let* ((zoom-factor (or (frame-parameter (selected-frame) 'zoomed) 0))
(current-font-size (my/get-current-fontsize))
(base-font-size (- current-font-size zoom-factor)))
(/ (float current-font-size) (float base-font-size))))
;; TODO: where to hook this? zoom-frm.el has no hooks.
;; TODO: For now, this must be called manually as `M-x my/scale-latex-fragments'.
(defun my/scale-latex-fragments ()
"Scale org-mode LaTeX fragments to match Spacemacs's current frame zoom state."
(interactive)
(org-latex-preview '(64)) ;; clear LaTeX previews
(plist-put org-format-latex-options :scale (my/get-font-scaling-factor))
(org-latex-preview '(16))) ;; (create if needed and) display LaTeX previews
;; --------------------------------------------------------------------------------
(my/funcs-init)