-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathemacs-config.org
8081 lines (6923 loc) · 289 KB
/
emacs-config.org
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; -*- mode: emacs-lisp; eval: (outline-minor-mode 1); lexical-binding: t -*-
;; ^ for opening this file with loaded this config
;; ##### First opening guide: ##########
;; #
;; # C-c @ C-t PREPARE outline-hide-body
;; # C-c @ C-s SHOW LINE outline-show-subtree
;; #
;; #####################################
#+end_src
* Notes
#+begin_src elisp
;; + outline connfiguration for first opening at the bottom
;; + [rooted] - means tested for usage under root console.
;; + GNU Emacs 29.4
#+end_src
** Keys after loading this file.
#+begin_src elisp
;; [ TAB ] - show, hide header.
;; [ Shift+TAB ] (backtab) [ C-c TAB ] - hide all, show only headers
;; [ C-c C-e ] - hide other headers, leave current
#+end_src
** List of external files
#+begin_src elisp
;; .emacs - main config
;; .emacs.d - directory, (not valuable)
;; .MyEmacsBackups - directory
;; .signature - file for notmuch - footer for output email
;; .mailcap - file for notmuch
;; .authinfo - notmuch credentials and password
;; .tramp_history - file, (not valuable)
;; .emacs.d/cotrib/lisp/myholidays.el - holidays
#+end_src
* Automatic Variables
#+begin_src elisp
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(ansi-color-faces-vector
[default default default italic underline success warning error])
'(auto-compression-mode nil)
'(auto-encryption-mode nil)
'(c-default-style
'((c-mode . "stroustrup")
(java-mode . "java")
(awk-mode . "awk")
(other . "gnu")))
'(custom-enabled-themes '(wombat manoj-dark))
'(delete-selection-mode t)
'(display-time-mode t)
'(global-eldoc-mode -1)
'(inhibit-startup-screen t)
'(menu-bar-mode nil)
'(mouse-wheel-mode nil)
'(org-agenda-files '("~/.emacs.d/todo.org"))
'(org-hide-leading-stars t)
'(org-image-actual-width '(300))
'(org-link-descriptive nil)
'(org-return-follows-link t)
'(org-src-preserve-indentation t)
'(org-startup-folded t)
'(package-selected-packages
'(editorconfig selected-window-accent-mode circadian dockerfile-mode ox-html5slide org-inline-anim epresent org-present rainbow-identifiers dired-duplicates marginalia vertico multiple-cursors tab-bar-buffers diredc elpher command-log-mode pinyin-search idle-highlight-mode csv-mode free-keys flycheck-aspell lua-mode julia-mode json-mode hidepw multitran company-math flycheck flymake-python-pyflakes company-jedi ob-http company-restclient vlf markdown-mode ggtags projectile flymake-yamllint smtpmail-multi))
'(safe-local-variable-values '((org-image-actual-width . 500)))
'(size-indication-mode t)
'(speedbar-show-unknown-files t))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(highlight-changes ((t (:background nil :foreground "hot pink" :weight bold))))
'(highlight-changes-delete ((t (:background nil :foreground "red"))))
'(org-hide ((((background dark)) (:foreground "dark blue")) (((background light)) (:foreground "gray"))))
'(org-level-1 ((t (:weight bold :height 1.1))))
'(org-level-2 ((t (:weight bold :height 1.1))))
'(org-level-3 ((t (:weight bold :height 1.1))))
'(whitespace-tab ((t (:foreground "hot pink"))))
'(whitespace-trailing ((t (:extend t :background "dark red")))))
;; '(org-hide ((((background dark)) (:foreground "dark slate blue"))
;; (((background light)) (:foreground "gray"))))
;; '(org-hide ((((background dark)) (:foreground "dark slate blue"))
;; (((background light)) (:foreground "gray"))))
;; dark:
;; ;; ;; '(highlight-changes ((t (:background nil :foreground "pink"))))
;; ;; '(highlight-changes-delete ((t (:foreground "red"))))
;; ;; '(whitespace-tab ((t (:foreground "#636363"))))
;; ;; '(whitespace-trailing ((t (:extend t :background "dark red"))))
;; white:
;; ;; '(highlight-changes ((t (:background nil :foreground "red"))))
;; ;; '(highlight-changes-delete ((t (:foreground "red"))))
;; ;; '(whitespace-tab ((t (:foreground "#636363"))))
;; ;; '(whitespace-trailing ((t (:extend t :background "pink"))))
#+end_src
* Enable commands (automatc added)
#+begin_src elisp
(put 'scroll-left 'disabled nil)
;; (put 'erase-buffer 'disabled nil)
;; - MELPA
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
;; Comment/uncomment this line to enable MELPA Stable if desired. See `package-archive-priorities`
;; and `package-pinned-packages`. Most users will not need or want to do this.
(add-to-list 'package-archives '("melpa-stable" . "https://stable.melpa.org/packages/") t)
(package-initialize) ; use `package-load-list' variable, dafault: (all)
; and `package-activated-list' variable
#+end_src
* Proxy configuration - for what types of connections? HTTP/HTTPS?
#+begin_src elisp
(require 'socks)
(setq url-gateway-method 'socks)
(setq socks-password "")
(setq socks-server '("Default server" "127.0.0.1" 9090 5)) ;; M-x customize socks
;; (url-proxy-services
;; test proxy:
(require 'url)
(require 'url-vars)
(require 'browse-url)
(defun my/testproxy ()
(let (
;; (url-mime-accept-string "application/xml")
(url-request-extra-headers
'(("User-Agent" . "curl/8.7.1")
("Accept" . "*/*")))
(url "http://ipinfo.io/ip")
)
(display-buffer (url-retrieve-synchronously "http://ipinfo.io/ip"))
))
;; (browse-url url)
;; (my/testproxy)
;; (with-current-buffer
;; (goto-char (point-min))
;; (re-search-forward "^$")
;; (delete-region (point) (point-min))
;; (buffer-string))
;; )
;; #+end_src
* gui and new version dependent configurations
#+begin_src elisp
;; ;; gui
(tool-bar-mode -1) ;; отключаем tool-bar
(scroll-bar-mode -1) ;; отключаем полосу прокрутки
;; ;; path for info
;; (require 'info)
;; (add-to-list 'Info-directory-list "/usr/local/src/elisp")
#+end_src
* Paths and file extensions, loading process
#+begin_src elisp
;; - Package Management
(setopt load-prefer-newer t)
;; must not have subfolders
(add-to-list 'load-path "~/.emacs.d/contrib/lisp/ediffnw")
(add-to-list 'load-path "~/.emacs.d/contrib/lisp")
;; (add-to-list 'load-path "~/.emacs.d/contrib/lisp/ob-yaml.el")
;; (add-to-list 'load-path "~/.emacs.d/contrib/lisp/ob-yamlmy.el")
;; ;; (add-to-list 'load-path "~/.emacs.d/contrib/lisp/emacs-jedi")
;; (add-to-list 'load-path "~/.emacs.d/contrib/lisp/lsp-bridge")
;; conf-mode for /etc
(add-to-list 'auto-mode-alist '("/etc/.*" . conf-unix-mode))
;; images
(add-to-list 'auto-mode-alist '("\\.jpg\\'" . image-mode))
(add-to-list 'auto-mode-alist '("\\.png\\'" . image-mode))
(add-to-list 'auto-mode-alist '("\\.gif\\'" . image-mode))
;; YAML
(add-to-list 'auto-mode-alist '("\\.yml\\'" . yaml-ts-mode))
;; ;; dont ask for unsafe local variables in knewn files
;; (setq inhibit-local-variables-regexps
;; info
;; (add-to-list 'Info-directory-list "/usr/local/info")
;; (add-hook 'Info-mode-hook '(lambda ()
;; (add-to-list 'Info-directory-list "/usr/local/info")
;; ))
;; (add-to-list 'Info-directory-list "/usr/share/info/emacs-28/elisp.info.bz2")
;; /usr/share/info/emacs-28/elisp.info.bz2
;; (setq auto-mode-alist
;; (append auto-mode-alist
;; '(("\\.jpg\\'" . image-mode)
;; ("\\.png\\'" . image-mode)
;; ("\\.gif\\'" . image-mode))))
#+end_src
* Backup
#+begin_src elisp
#+end_src
** Single or Numbered Backups.
#+begin_src elisp
;; backup dir
(setopt backup-directory-alist '(("." . "~/.MyEmacsBackups")))
(setopt version-control t) ; save versins with foo.~1~, foo.~2~, foo.~3~, …, foo.~259~
(setopt delete-old-versions t) ; nil, the default - asks , t - deletes the excess backup files silently
(setopt kept-old-versions 6)
(setopt kept-new-versions 6)
#+end_src
** Auto save files "#file#" (Auto-save)
#+begin_src elisp
#+end_src
*** Difference and remove
#+begin_src elisp
(defun my/diff-auto-save-file ()
"Get auto-save #file# difference with current buffer."
(interactive)
(diff (make-auto-save-file-name) (current-buffer) nil 'noasync))
(defun my/auto-save-file-remove ()
"Delete auto-save #file# if exist."
(interactive)
(let ((filename (make-auto-save-file-name)))
(if (not (file-exists-p filename))
(message (concat "File " filename " don't exist"))
;; else
(delete-file filename nil) ; no trash
(message (concat "File " filename " succesfully removed."))
)))
#+end_src
*** Fix not visible message about #file# exist (old)
#+begin_src elisp
;; (defun my/hook-fix-auto-save-was-found(&optional proc files nowait commands dontkill frame tty-name)
;; ;; No need to warn if buffer is auto-saved
;; ;; under the name of the visited file.
;; (when (not (and buffer-file-name
;; auto-save-visited-file-name))
;; (if (file-newer-than-file-p (or buffer-auto-save-file-name
;; (make-auto-save-file-name))
;; buffer-file-name)
;; (message (format "%s has auto save data; consider M-x recover-this-file"
;; (file-name-nondirectory buffer-file-name)))
;; ;; else
;; (if (file-exists-p (or buffer-auto-save-file-name
;; (make-auto-save-file-name)))
;; (message (format "%s has auto save data, but older than original; consider M-x recover-this-file"
;; (file-name-nondirectory buffer-file-name)))))))
;; ;; this fix hidding of message about auto saved #file# when there is local variables defined in file.
;; (add-hook 'find-file-hook 'my/hook-fix-auto-save-was-found)
;; ;; and when emacsclient is used and hide by message "When done with a buffer, type C-x #"
;; (advice-add 'server-execute :after #'my/hook-fix-auto-save-was-found)
#+end_src
*** Fix reciver-this-file to recover old files (old, not used)
#+begin_src elisp
;; (defun my/recover-this-file ()
;; "Recover the visited file--get contents from its last auto-save file.
;; Visit file FILE, but get contents from its last auto-save file."
;; (interactive) ;; TODO: (interactive "P")
;; (or buffer-file-name
;; (user-error "This buffer is not visiting a file"))
;; (defvar-local file buffer-file-name)
;; ;; Actually putting the file name in the minibuffer should be used
;; ;; only rarely.
;; ;; Not just because users often use the default.
;; (interactive "FRecover file: ")
;; (setq file (expand-file-name file))
;; (if (auto-save-file-name-p (file-name-nondirectory file))
;; (user-error "%s is an auto-save file" (abbreviate-file-name file)))
;; (let ((file-name (let ((buffer-file-name file))
;; (make-auto-save-file-name))))
;; (cond ((and (file-exists-p file)
;; (not (file-exists-p file-name)))
;; (error "Auto save file %s does not exist"
;; (abbreviate-file-name file-name)))
;; ((and (if (file-exists-p file)
;; (not (file-newer-than-file-p file-name file))
;; (not (file-exists-p file-name)))
;; (not (yes-or-no-p "Auto-save file not current, are you shure?" )))
;; (user-error "Auto-save file %s not current"
;; (abbreviate-file-name file-name)))
;; ((with-temp-buffer-window
;; "*Directory*" nil
;; #'(lambda (window _value)
;; (with-selected-window window
;; (unwind-protect
;; (yes-or-no-p (format "Recover auto save file %s? " file-name))
;; (when (window-live-p window)
;; (quit-restore-window window 'kill)))))
;; (with-current-buffer standard-output
;; (let ((switches dired-listing-switches))
;; (if (file-symlink-p file)
;; (setq switches (concat switches " -L")))
;; ;; Use insert-directory-safely, not insert-directory,
;; ;; because these files might not exist. In particular,
;; ;; FILE might not exist if the auto-save file was for
;; ;; a buffer that didn't visit a file, such as "*mail*".
;; ;; The code in v20.x called `ls' directly, so we need
;; ;; to emulate what `ls' did in that case.
;; (insert-directory-safely file switches)
;; (insert-directory-safely file-name switches))))
;; (switch-to-buffer (find-file-noselect file t))
;; (let ((inhibit-read-only t)
;; ;; Keep the current buffer-file-coding-system.
;; (coding-system buffer-file-coding-system)
;; ;; Auto-saved file should be read with special coding.
;; (coding-system-for-read 'auto-save-coding))
;; (erase-buffer)
;; (insert-file-contents file-name nil)
;; (set-buffer-file-coding-system coding-system)
;; (set-buffer-auto-saved))
;; (after-find-file nil nil t))
;; (t (user-error "Recover-file canceled")))))
#+end_src
* Called externally with: emacs*eval "()"
#+begin_src elisp
#+end_src
** Agenda and diary
#+begin_src elisp
(require 'org-agenda)
(defun my/agenda-split()
"called with (call-interactively 'my/agenda-split)"
(interactive)
(org-agenda nil "a")
(delete-other-windows)
(split-window-right)
(other-window 1)
(org-cycle-agenda-files)
(other-window 1)
(split-window-below)
(calendar)
;; (other-window 1)
;; (switch-to-buffer "diary")
;; (diary-show
;; (other-window 1)
;; (other-window 1)
)
#+end_src
** Open link
#+begin_src elisp
;; usage in ~/.bash_aliases: alias iaa='emacsclient*alternate-editor=emacs*create-frame*eval "(my/open-link \"file:~/nix::<<config_kernel_gentoo>>\")"'
(defun my/open-link(arg)
"Open org link, generated by `my/copy-link-to-clipboard'."
(let
((display-buffer-base-action
(list '(
display-buffer-reuse-window ; pop up bottom window
display-buffer--maybe-pop-up-frame-or-window ;; create window
;; If all else fails, pop up a new frame.
display-buffer-pop-up-frame ))))
(org-link-open-from-string arg)))
#+end_src
** Find file in right frame
#+begin_src elisp
;; (defun my/find-file-frame (filename)
;; "Open file in frame with the same mode buffer. If no frame was
;; found the new one will be created. Used with `tab-line-mode'
;; with (setopt tab-line-tabs-function 'tab-line-tabs-mode-buffers).
;; Steps: 1) create buffer. 2) found buffer in frame with tab-line-mode.
;; 3) raise frame."
;; (let* ((b (find-file-noselect filename))
;; (f (cdr
;; (seq-find (lambda (x) (seq-contains-p (car x) b))
;; (mapcar (lambda (x) (select-frame x)
;; (cons (funcall tab-line-tabs-function) x)) (frame-list-z-order))))))
;; (if f
;; (progn
;; (raise-frame f)
;; (switch-to-buffer b))
;; ;; else
;; (switch-to-buffer-other-frame b))))
;;
;;
(defun my/find-file-frame (file-name)
"Open file in frame with the same mode buffer. If no frame was
found the new one will be created. Used with `tab-line-mode'
with (setopt tab-line-tabs-function 'tab-line-tabs-mode-buffers).
Steps: 1) create buffer. 2) found frame with same major mode.
3) select buffer in that frame and raise it."
(raise-frame (selected-frame)) ; fix bug of cmd*eval
;; find buffer by filename
(let* ((fname (expand-file-name file-name))
(b (seq-find (lambda (b)
(and
(/= (aref (buffer-name b) 0) ?\s) ; not system buffers
(buffer-live-p b) ; ensure alive
(string-equal (buffer-file-name b)
(expand-file-name file-name))))
(buffer-list)))
mf
f)
(if b
(switch-to-buffer-other-frame b)
;; else
(find-file file-name)
)
;; (when b
;; ;; find frame somehow
;; ;; (setq mf (mapcar (lambda (x) (with-current-buffer (car x) (cons major-mode (cdr x))))
;; ;; (seq-filter (lambda (x) (car x)) ; filter nil
;; ;; ;; ((buffer . frame)...)
;; ;; (mapcar (lambda (x) (cons (window-buffer (frame-root-window x)) x))
;; ;; (frame-list-z-order)))))
;; ;; (setq f (cdr (with-current-buffer b
;; ;; (seq-find (lambda (x) (derived-mode-p (car x))) mf))))
;; (setq mf (mapcar (lambda (x) (cons (buffer-file-name (car x)) (cdr x)))
;; (seq-filter (lambda (x) (car x)) ; filter nil
;; ;; ((buffer . frame)...)
;; (mapcar (lambda (x) (cons (window-buffer (frame-root-window x)) x))
;; (frame-list-z-order)))))
;; (setq f (cdr (seq-find (lambda (x) (string-equal (car x) file-name)) mf)))
;; )
;; (print (list "frame" f))
;; (if f
;; (progn
;; ;; (print (list "frame" f))
;; (select-frame f)
;; (switch-to-buffer b)
;; (raise-frame f))
;; ;; else
;; (switch-to-buffer-other-frame b))
))
;; (my/find-file-frame "a.org")
#+end_src
** Open multiple files in splitted windows
#+begin_src elisp
(defun my/open-files-in-windows (files)
"Open each file in FILES in a new window.
FILES is a single string with pathes separated by white space.
We expand firstly because `find-file' change default-directory."
(let ((expanded-files (mapcar (lambda (file)
(expand-file-name file default-directory))
(split-string files))))
;; (switch-to-buffer-other-frame
(find-file (car expanded-files))
;; )
(dolist (file (cdr expanded-files))
(split-window-right)
(other-window 1)
(find-file file))))
#+end_src
* Global TAB key indent.el indent-for-tab-command replacement
#+begin_src elisp
(defun my/indent-for-tab-command-step-2-insert-tab (arg)
"Insert a tab character if necessary."
(when (or (eq indent-line-function 'indent-to-left-margin)
(and (not tab-always-indent)
(or (> (current-column) (current-indentation))
(eq this-command last-command))))
(insert-tab arg)))
(defun my/indent-for-tab-command-step-3-indent-line (arg)
"Indent the current line using the indent-line-function.
Should return non-nil if indentation occur of arrempt was made.
`indent-line-function' should return 'noindent to signal that it
is batter to do something other here, but not indent."
(let ((old-tick (buffer-chars-modified-tick))
(old-point (point))
(old-indent (current-indentation))
(indnet-ret (indent--funcall-widened indent-line-function)))
(or (not (eq indnet-ret 'noindent))
(indent--default-inside-comment)
(when (or (<= (current-column) (current-indentation))
(not (eq tab-always-indent 'complete)))
(indent--funcall-widened (default-value 'indent-line-function))))
;; - Return non-nil that signal that indentation occured or was
;; - attempt of it
(or (eq indnet-ret 'noindent)
(not (eql old-point (point)))
(not (eql old-tick (buffer-chars-modified-tick))))))
(defun my/indent-for-tab-command-step-4-completion (arg)
"Perform completion if necessary."
(when (and (eq tab-always-indent 'complete)
(or (eq last-command this-command)
(let ((syn (syntax-class (syntax-after (point)))))
(pcase tab-first-completion
('nil t)
('eol (eolp))
('word (not (eql 2 syn)))
('word-or-paren (not (memq syn '(2 4 5))))
('word-or-paren-or-punct (not (memq syn '(2 4 5 1))))))))
(completion-at-point)))
(defun my/indent-for-tab-command-step-5-rigid-indent (arg)
"Rigidly indent the following sexp if a prefix argument was given."
(when arg
(let ((end-marker
(save-excursion
(forward-line 0) (forward-sexp) (point-marker)))
(indentation-change (- (current-indentation) old-indent)))
(save-excursion
(forward-line 1)
(when (and (not (zerop indentation-change))
(< (point) end-marker))
(indent-rigidly (point) end-marker indentation-change))))))
(defcustom my/indent-for-tab-command-steps
(list
'my/indent-for-tab-command-step-2-insert-tab
'my/indent-for-tab-command-step-3-indent-line
'my/indent-for-tab-command-step-4-completion
'my/indent-for-tab-command-step-5-rigid-indent)
"List of steps to perform in the indent-for-tab-command function."
:type '(repeat function)
:group 'indent-for-tab-command)
;; Redefine the main function to use the
(defun my/indent-for-tab-command (arg)
"Indent the current line or region, or insert a tab, as appropriate.
Steps excuted sucessess."
(interactive "P")
(if (use-region-p)
;; Indent the region if it's active.
(indent-region (region-beginning) (region-end))
;; else
(seq-find (lambda(step)
;; (print step)
(funcall step arg))
my/indent-for-tab-command-steps)))
(advice-add 'indent-for-tab-command :override #'my/indent-for-tab-command)
#+end_src
* Global Hooks
#+begin_src elisp
#+end_src
** Delete white spaces at save
#+begin_src elisp
(add-hook 'before-save-hook #'delete-trailing-whitespace)
(add-hook 'after-save-hook (lambda ()
(if highlight-changes-mode
(progn
(call-interactively 'highlight-changes-mode)
(call-interactively 'highlight-changes-mode)
)
)))
#+end_src
** emacsclient file1 file2: open each file in separate window
#+begin_src elisp
;; (defvar server-visit-files-custom-find:buffer-count)
;; (defadvice server-visit-files
;; (around server-visit-files-custom-find
;; activate compile)
;; "Maintain a counter of visited files from a single client call."
;; (let ((server-visit-files-custom-find:buffer-count 0))
;; ad-do-it))
;; (defun server-visit-hook-custom-find ()
;; "Arrange to visit the files from a client call in separate windows."
;; (if (zerop server-visit-files-custom-find:buffer-count)
;; (progn
;; (delete-other-windows)
;; (switch-to-buffer (current-buffer)))
;; (let ((buffer (current-buffer))
;; (window (split-window-sensibly)))
;; (switch-to-buffer buffer)
;; (balance-windows)))
;; (setq server-visit-files-custom-find:buffer-count
;; (1+ server-visit-files-custom-find:buffer-count)))
;; (add-hook 'server-visit-hook 'server-visit-hook-custom-find)
#+end_src
** ipynb
#+begin_src elisp
(require 'markdown-mode nil t)
(require 'json)
(defun ipynb-to-markdown (file)
(interactive "f")
(let* ((data (with-temp-buffer
(insert-file-contents-literally file)
(json-parse-string (buffer-string)
:object-type 'alist
:array-type 'list)))
(metadata (alist-get 'metadata data))
(kernelspec (alist-get 'kernelspec metadata))
(language (alist-get 'language kernelspec)))
(pop-to-buffer "ipynb-as-markdown")
;; (when (featurep 'markdown-mode)
;; (markdown-mode))
(dolist (c (alist-get 'cells data))
(let* ((contents (alist-get 'source c))
(outputs (alist-get 'outputs c)))
(pcase (alist-get 'cell_type c)
("markdown"
(when contents
(mapcar #'insert contents)
(insert "\n\n")))
("code"
(when contents
(insert "```")
(insert language)
(insert "\n")
(mapcar #'insert contents)
(insert "\n```\n\n")
(dolist (x outputs)
(when-let (text (alist-get 'text x))
(insert "```stdout\n")
(insert (mapconcat #'identity text ""))
(insert "\n```\n\n"))
(when-let (data (alist-get 'data x))
(when-let (im64 (alist-get 'image/png data))
(let ((imdata (base64-decode-string im64)))
(insert-image (create-image imdata 'png t)))))
(insert "\n\n")))))))))
#+end_src
** Copy current file path and line number to clipboard
#+begin_src elisp
(defun my/copy-link-to-clipboard ()
"Copy to clipboard:
- for org mode files: link generated by `org-store-link'
- for other files '</path/to/file>:<line-number>'."
(interactive)
(let ((link))
(if (derived-mode-p 'org-mode)
(setq link (substring-no-properties (org-store-link nil)))
;; else - not Org file
(setq link
(concat "file:" (buffer-file-name) "::" (number-to-string (line-number-at-pos)))))
(kill-new link)
(message (concat link "\t- copied to clipboard"))))
;; shadow `append-next-kill'
(define-key global-map (kbd "C-c w") #'my/copy-link-to-clipboard)
#+end_src
* GUI
#+begin_src elisp
#+end_src
** common
#+begin_src elisp
;; Disable GUI components
(tooltip-mode -1)
(menu-bar-mode -1) ; отключаем графическое меню
(setopt fringe-mode 12) ; Give some breathing room
(setopt visible-bell t) ; Set up the visible bell
;; (setq ring-bell-function 'ignore) ;; отключить звуковой сигнал
;; (column-number-mode)
(setopt use-dialog-box nil) ;; никаких графических диалогов и окон - все через минибуфер
(setopt redisplay-dont-pause t) ;; лучшая отрисовка буфера
;; dont depand yes
;; (defalias 'yes-or-no-p 'y-or-n-p)
;; russian with Ctrl+\*
(setopt default-input-method "russian-computer")
;; remember cursor position, for emacs 25.1 or later
(save-place-mode 1)
;; default scratch buffer mode
(setopt initial-major-mode 'org-mode)
;; For ex. (yes-or-no-p "Close buffer?"), answer shortly: y not yes.
(setq use-short-answers t)
#+end_src
** simple
#+begin_src elisp
(setopt idle-update-delay 0.2) ; 0.5
(setopt mark-ring-max 32) ; 16
(setopt global-mark-ring-max 32) ; 16
#+end_src
** Scrolling
#+begin_src elisp
(setopt
;; smooth scrolling
scroll-conservatively 10000 ;; dont jump when cursor at the bottom or top
scroll-margin 1 ;; scroll N lines to screen edge - it has problems with term
;; cursor keeps its screen position while scrolling
scroll-preserve-screen-position t
;; to reach top and bottom with cursor eventyally
scroll-error-top-bottom t
;; slower
next-screen-context-lines 12
)
#+end_src
** Indentation
#+begin_src elisp
;; We don't change `tab-width' at global scale.
;; padding from edge of window
;; (setq-default left-margin-width 14 right-margin-width 14)
;; (set-window-fringes (selected-window) 14 0)
;; no tabs by default, useful for artist-mode
(setopt indent-tabs-mode nil) ; t by default
;; Tab indentation + completion
(setopt tab-always-indent 'complete) ; t by default
;; Make the backspace properly erase the tab instead of
;; removing 1 space at a time.
(setopt backward-delete-char-untabify-method 'hungry)
#+end_src
** minibuffer
#+begin_src elisp
;; (add-to-list 'completion-styles 'initials t) ; E.g. can complete M-x lch to list-command-history
;; minibuffer delay for F1 and C-x
(setq echo-keystrokes 0.2)
#+end_src
** filling text, line wrapping
#+begin_src elisp
;; - display fill-column
(setopt global-display-fill-column-indicator-mode t)
;; - don't wrap long lines
(setopt truncate-lines t)
;; (add-hook 'text-mode-hook 'visual-line-mode)
;; (add-hook 'prog-mode-hook 'toggle-truncate-lines)
;; used in `fill-paragraph'
(add-hook 'text-mode-hook (lambda () (setq-local fill-prefix " "))) ;; buffer-local
;; - auto wrap mode with by fill-column
;; (require 'visual-fill-column)
;; (add-hook 'visual-line-mode-hook #'visual-fill-column-mode)
;; (add-hook 'text-mode-hook 'visual-line-mode)
#+end_src
** Make tabs visiable
#+begin_src elisp
;; You can also do M-x whitespace-report
;; whitespace-toggle-options too
;; whitespace-new-line-mode, to display new lines
(setopt whitespace-style '(face tabs tab-mark trailing))
(setopt whitespace-display-mappings
'((tab-mark 9 [124 9] [92 9]))) ; 124 is the ascii ID for '\|'
(global-whitespace-mode)
#+end_src
** Time
#+begin_src elisp
(setopt display-time-24hr-format t)
(setopt display-time-mode t)
#+end_src
** window title
#+begin_src elisp
;; (setq-default frame-title-format '(
;; ""
;; (:eval (cond ((not buffer-read-only) "%% ")
;; ((buffer-modified-p) "* ")))
;; 'frame-list
;; "%b [%m] %f"))
(setopt frame-title-format
'((:eval
(buffer-name)
;; (if (buffer-file-name)
;; (abbreviate-file-name (buffer-file-name))
;; ;; "%b [%m] %f"
;; "%f"
;; )
)
(:eval (if (buffer-modified-p)
" •"))
;; " Emacs"
)
)
;; (if (and (not window-system)
;; (string-match "^xterm" (getenv "TERM")))
;; (progn
;; (require 'xterm-frobs)
;; (defun my-xterm-title-hook ()
;; (xterm-set-window-title
;; (concat
;; (cond (buffer-read-only "% ")
;; ((buffer-modified-p) "* "))
;; (buffer-name)
;; " (" invocation-name "@" system-name ")")))
;; (add-hook 'post-command-hook 'my-xterm-title-hook))
;; (setq frame-title-format
;; '(""
;; (:eval (cond (buffer-read-only "%% ")
;; ((buffer-modified-p) "* ")))
;; "%b (" invocation-name "@" system-name ")")))
#+end_src
** window size
#+begin_src elisp
(add-to-list 'default-frame-alist '(height . 35))
(add-to-list 'default-frame-alist '(width . 130))
;; (add-to-list 'default-frame-alist '(left . 0))
;; (add-to-list 'default-frame-alist '(top . 0))
#+end_src
** Modeline todo
#+begin_src elisp
(line-number-mode t)
(column-number-mode t)
#+end_src
** Modeline: current path
#+begin_src elisp
(setq global-mode-string
(cond ((consp global-mode-string)
(add-to-list 'global-mode-string 'default-directory 'APPEND))
((not global-mode-string)
(list 'default-directory))
((stringp global-mode-string)
(list global-mode-string 'default-directory))))
#+end_src
** transparecy (old, not working)
#+begin_src elisp
;; ;; Set frame transparency
;; (defvar efs/frame-transparency '(90 . 90))
;; (set-frame-parameter (selected-frame) 'alpha efs/frame-transparency)
;; (add-to-list 'default-frame-alist `(alpha . ,efs/frame-transparency))
;; ;; ;; (set-frame-parameter (selected-frame) 'fullscreen 'maximized)
;; ;; ;; (add-to-list 'default-frame-alist '(fullscreen . maximized))
#+end_src
* Functions
#+begin_src elisp
#+end_src
** toggle-window-split
#+begin_src elisp
(defun my/toggle-window-split ()
(interactive)
(if (= (count-windows) 2)
(let* ((this-win-buffer (window-buffer))
(next-win-buffer (window-buffer (next-window)))
(this-win-edges (window-edges (selected-window)))
(next-win-edges (window-edges (next-window)))
(this-win-2nd (not (and (<= (car this-win-edges)
(car next-win-edges))
(<= (cadr this-win-edges)
(cadr next-win-edges)))))
(splitter
(if (= (car this-win-edges)
(car (window-edges (next-window))))
'split-window-horizontally
'split-window-vertically)))
(delete-other-windows)
(let ((first-win (selected-window)))
(funcall splitter)
(if this-win-2nd (other-window 1))
(set-window-buffer (selected-window) this-win-buffer)
(set-window-buffer (next-window) next-win-buffer)
(select-window first-win)
(if this-win-2nd (other-window 1))))))
;; (defun my/add-to-list ()
;; (interactive)
;; (add-to-list 'load-path default-directory)
;; (message "now load file with M-x load-library"))
#+end_src
** exec-language
#+begin_src elisp
(defun my/exec-language (exec-file-command buffer) ; "*Org Src"
"Execute current buffer content in terminal with EXEC-FILE-COMMAND.
First it save content to temporary file.
If we in Org src with C-c ' we create tmp file, write buffer and execute,
else just execute current file
EXEC-FILE-COMMAND for example may be: \"PYTHONPATH=. python\"."
(let ((langnamestr (car (split-string (symbol-name major-mode) "-")))
fnv ;; tmp file for C-c '
fn)
(setq fnv (org-babel-temp-file langnamestr))
;; If we in C-c '
;; (if (string-prefix-p "*Org Src" (buffer-name) t)
(with-current-buffer buffer
;; save body to fnv buffer
(let ((body (buffer-substring-no-properties (point-min) (point-max))))
(with-temp-file fnv (insert body))))
;; if C-c use tmp file, else use file by itself
(setq fn (if (string-prefix-p "*Org Src" (buffer-name) t)
fnv
;; else
(buffer-file-name)))
(with-temp-buffer
;; (setq-local default-directory (buffer-local-value 'default-directory (get-buffer "*Messages*")))
(message (concat "default-directory" default-directory))
;; "xfce4-terminal*initial-title \"emacs_python\" -e \"bash -c 'PYTHONPATH=. python " fn " ; bash ;'\""
;; (print (list "fnfile" fn))
;; in case of TRAMP execute remote:
(if (file-remote-p default-directory)
(let (
(host (substring fn 5 (1- (string-match "/" fn 5)))) ; 5 is "/ssh:" length
(file (substring fn (1- (match-end 0))))
(tramp-mode nil) ; disable TRAMP
(default-directory "/tmp"))
;; (print match-end)
;; (print (list "host file" host file))
(setq exec-file-command (concat "ssh " host " " exec-file-command " " file))
;; xfce4-terminal -e "tmux new-session \"bash -c 'ls; exec bash'\""
(print (concat "xfce4-terminal*title \"emacs_" langnamestr "\" -e \"tmux new-session \\\"bash -c '" exec-file-command " " fn " ; bash ;'\\\"\""))
(async-shell-command (concat "xfce4-terminal*title \"emacs_" langnamestr "\" -e \"tmux new-session \\\"bash -c '" exec-file-command " " fn " ; bash ;'\\\"\""))
;; (shell-command (concat "xfce4-terminal*title \"emacs_" langnamestr "\" -e \"bash -c '" exec-file-command " " fn " ; bash ;'\""))
)
;; else - not TRAMP
(progn
;; (print exec-file-command)
(shell-command (concat "xdotool search emacs_" langnamestr " windowclose")) ;; alala is window name that we use to close and open again
(async-shell-command (concat "xfce4-terminal*title \"emacs_" langnamestr "\" -e \"tmux new-session \\\"bash -c '" exec-file-command " " fn " ; bash ;'\\\"\""))
;; (shell-command (concat "xfce4-terminal*title \"emacs_" langnamestr "\" -e \"bash -c '" exec-file-command " " fn " ; bash ;'\"")))
)
)
)))
#+end_src
** Python REPL remotely
#+begin_src elisp
(defun python-repl-remote(machine venv-path)
"Open xfce4-terminal with remote python interactive REPL.
MACHINE is ip or .ssh/config name.
VENV-PATH is /some/path/venv ."
(let* ((command (concat "ssh " machine " \\\"source " venv-path "/bin/activate; " venv-path "/bin/python -i \\\""))
(cc (concat "xfce4-terminal*title remote -e \"bash -c \'" command "; bash ;\'\"")))
(message cc)
(shell-command cc )))
#+end_src
** Detect Org source block
#+begin_src elisp
(defvar org-src-detect--last-post-command-position 0
"Holds the cursor position from the last run of post-command-hooks.")
(defun org-src-detect-check ()
"Return t if cursor at src-block, fixed-width or comment line.
Based on `org-edit-src-code'."
(let* ((element (org-element-at-point))
(el-type (org-element-type element))
(line (buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))
(if (or
(string-match-p "^: " line) ; 'fixed-width
(string-match-p "^#" line) ; 'comments and other org
(and (memq el-type '(example-block src-block))
(org-src--on-datum-p element)))
t)))
(defcustom org-src-detect-in-hook nil
"Hook run at every cursor movement in source block or comment line."
:type 'hook
:group 'matching)
(defcustom org-src-detect-out-hook nil
"Hook run at every cursor movement outside of source block or comment line."
:type 'hook
:group 'matching)