-
-
Notifications
You must be signed in to change notification settings - Fork 390
/
helm-utils.el
1229 lines (1130 loc) · 51.1 KB
/
helm-utils.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
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
;;; helm-utils.el --- Utilities Functions for helm. -*- lexical-binding: t -*-
;; Copyright (C) 2012 ~ 2023 Thierry Volpiatto
;; 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/>.
;;; Code:
(require 'cl-lib)
(require 'helm)
(require 'helm-help)
(declare-function helm-find-files-1 "helm-files" (fname &optional preselect))
(declare-function helm-grep-split-line "helm-grep" (line))
(declare-function markdown-show-entry "ext:markdown-mode.el")
(declare-function outline-show-subtree "outline")
(declare-function org-reveal "org")
(declare-function hs-show-block "hideshow.el")
(declare-function hs-show-all "hideshow.el")
(declare-function tab-bar-tabs "tab-bar")
(declare-function tab-bar-select-tab "tab-bar")
(declare-function dired-goto-file "dired")
(declare-function bookmark-get-filename "bookmark")
(declare-function package-installed-p "package")
(declare-function package-desc-dir "package")
(defvar hs-minor-mode)
(defvar hs-show-hook)
(defvar org-directory)
(defvar winner-boring-buffers)
(defvar bookmark-alist)
(defvar dired-buffers)
(defvar helm-show-completion-overlay)
(defvar helm-buffers-maybe-switch-to-tab)
(defvar helm-ff-transformer-show-only-basename)
(defvar helm-popup-tip-mode)
(defvar helm-ff-last-expanded-candidate-regexp)
(defvar helm-mode-find-file-target-alist)
(defgroup helm-utils nil
"Utilities routines for Helm."
:group 'helm)
(defcustom helm-su-or-sudo "sudo"
"What command to use for root access."
:type 'string
:group 'helm-utils)
(defcustom helm-default-kbsize 1024.0
"Default Kbsize to use for showing files size.
It is a float, usually 1024.0 but could be 1000.0 on some systems."
:group 'helm-utils
:type 'float)
(define-obsolete-variable-alias
'helm-highlight-number-lines-around-point
'helm-highlight-matches-around-point-max-lines
"20160119")
(defcustom helm-highlight-matches-around-point-max-lines '(15 . 15)
"Number of lines around point where matched items are highlighted.
Possible value are:
- A cons cell (x . y)
Match x lines before point and y lines after point.
- An integer
Positive means this number lines after point.
Negative means this number lines before point.
A zero value means highlight only inside matched lines.
- The symbol never
Means do not highlight matched items. "
:group 'helm-utils
:type '(choice (cons (integer :tag "Match before")
(integer :tag "Match after"))
(const :tag "Match in line only" 0)
(integer :tag "Match after or before (+/-)")
(const :tag "Never match" never)))
(defcustom helm-highlight-only-all-matches nil
"Highlight only when all items match on the line when non nil.
See `helm-highlight-current-line'."
:group 'helm-utils
:type 'boolean)
(defcustom helm-buffers-to-resize-on-pa nil
"A list of helm buffers where the helm-window should be reduced on PA.
Where PA means persistent action."
:group 'helm-utils
:type '(repeat (choice string)))
(defcustom helm-resize-on-pa-text-height 12
"The size of the helm-window when resizing on persistent action."
:group 'helm-utils
:type 'integer)
(defcustom helm-html-decode-entities-function #'helm-html-decode-entities-string
"Function used to decode HTML entities in HTML bookmarks.
Helm comes by default with `helm-html-decode-entities-string', if
you need something more sophisticated you can use
`w3m-decode-entities-string' if available.
In Emacs itself org-entities seem broken and `xml-substitute-numeric-entities'
supports only numeric entities."
:group 'helm-utils
:type 'function)
(defvar helm-goto-line-before-hook '(helm-save-current-pos-to-mark-ring)
"Run before jumping to line.
This hook runs when jumping from `helm-goto-line', `helm-etags-default-action',
`helm-imenu-default-action' and `helm-mark-ring-default-action' itself.
This allows you to retrieve a previous position after using the different helm
tools for searching or retrieving a position (etags, grep, gid, (m)occur etc...).
By default positions are added to `mark-ring'.
You can also add to register by using (or adding)
`helm-save-pos-to-register-before-jump' instead. In this case
last position is added to the register `helm-save-pos-before-jump-register'.
Note that when using a register only one position is saved globally to registers
whereas when using `mark-ring' all positions are saved locally in each buffer.")
(defvar helm-save-pos-before-jump-register ?_
"The register where `helm-save-pos-to-register-before-jump' saves position.")
(defconst helm-html-entities-alist
'((""" . 34) ;; "
(">" . 62) ;; >
("<" . 60) ;; <
("&" . 38) ;; &
("€" . 8364) ;; €
("Ÿ" . 89) ;; Y
("¡" . 161) ;; ¡
("¢" . 162) ;; ¢
("£" . 163) ;; £
("¤" . 164) ;; ¤
("¥" . 165) ;; ¥
("¦" . 166) ;; ¦
("§" . 167) ;; §
("¨" . 32) ;; SPC
(" " . 160) ;; (non breaking space)
("©" . 169) ;; ©
("ª" . 97) ;; a
("«" . 171) ;; «
("¬" . 172) ;; ¬
("&masr;" . 174) ;; ®
("°" . 176) ;; °
("±" . 177) ;; ±
("²" . 50) ;; 2
("³" . 51) ;; 3
("´" . 39) ;; '
("µ" . 956) ;; μ
("¶" . 182) ;; ¶
("·" . 183) ;; ·
("¸" . 32) ;; SPC
("¹" . 49) ;; 1
("º" . 111) ;; o
("»" . 187) ;; »
("¼" . 49) ;; 1
("½" . 49) ;; 1
("¾" . 51) ;; 3
("¿" . 191) ;; ¿
("À" . 192) ;; À
("Á" . 193) ;; Á
("Â" . 194) ;; Â
("Ã" . 195) ;; Ã
("Ä" . 196) ;; Ä
("Å" . 197) ;; Å
("&Aelig" . 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) ;; ÿ
("®" . 174) ;; ®
("­" . 173)) ;;
"Table of html character entities and values.
See https://www.freeformatter.com/html-entities.html")
(defvar helm-find-many-files-after-hook nil
"Hook that runs at end of `helm-find-many-files'.")
(defvar helm-marked-buffer-name "*helm marked*")
;;; Faces.
;;
(defface helm-selection-line
`((t ,@(and (>= emacs-major-version 27) '(:extend t))
:inherit highlight :distant-foreground "black"))
"Face used in the `helm-current-buffer' when jumping to a candidate."
:group 'helm-faces)
(defface helm-match-item
`((t ,@(and (>= emacs-major-version 27) '(:extend t))
:inherit isearch))
"Face used to highlight the item matched in a selected line."
:group 'helm-faces)
;;; Utils functions
;;
;;
(defcustom helm-window-prefer-horizontal-split nil
"Maybe switch to other window vertically when non nil.
Possible values are t, nil and `decide'.
When t switch vertically.
When nil switch horizontally.
When `decide' try to guess if it is possible to switch vertically
according to the setting of `split-width-threshold' and the size of
the window from where splitting is done.
Note that when using `decide' and `split-width-threshold' is nil, the
behavior is the same as with a nil value."
:group 'helm-utils
:type '(choice
(const :tag "Split window vertically" t)
(const :tag "Split window horizontally" nil)
(symbol :tag "Guess how to split window" 'decide)))
(defcustom helm-window-show-buffers-function #'helm-window-decide-split-fn
"The default function to use when opening several buffers at once.
It is typically used to rearrange windows."
:group 'helm-utils
:type '(choice
(function :tag "Decide how to split according to number of candidates"
helm-window-decide-split-fn)
(function :tag "Split windows vertically or horizontally"
helm-window-default-split-fn)
(function :tag "Split in alternate windows"
helm-window-alternate-split-fn)
(function :tag "Split windows in mosaic"
helm-window-mosaic-fn)))
(defun helm-window-show-buffers (buffers &optional other-window)
"Show BUFFERS.
With more than one buffer marked switch to these buffers in separate windows.
If OTHER-WINDOW is non-nil, keep current buffer and switch to other buffers
in separate windows.
If a prefix arg is given split windows vertically."
(let ((initial-ow-fn (if (cdr (window-list))
#'switch-to-buffer-other-window
#'helm-window-other-window)))
(if (cdr buffers)
(funcall helm-window-show-buffers-function buffers
(and other-window initial-ow-fn))
(if other-window
(funcall initial-ow-fn (car buffers))
(helm-buffers-switch-to-buffer-or-tab (car buffers))))))
(defvar tab-bar-tab-name-function)
(declare-function tab-bar-switch-to-tab "tab-bar.el")
(declare-function tab-bar-tab-name-all "tab-bar.el")
(defun helm-buffers-maybe-switch-to-buffer-in-tab (buffer fallback-fn)
"Switch to BUFFER in its tab if possible, otherwise call FALLBACK-FN."
(let* (;; Normally `helm-buffers-maybe-switch-to-tab' custom set function
;; bounded `tab-bar-tab-name-function' to `tab-bar-tab-name-all'
;; but in case user bounded this with setq ensure it works
;; at least partially by let-bounding it here.
(tab-bar-tab-name-function #'tab-bar-tab-name-all)
(tabs (tab-bar-tabs))
(tab-names (mapcar (lambda (tab)
(cdr (assq 'name tab)))
tabs))
(bname (buffer-name (get-buffer buffer)))
(tab (helm-buffers--get-tab-from-name bname tabs)))
(if (helm-buffers--buffer-in-tab-p bname tab-names)
(progn
(tab-bar-switch-to-tab (alist-get 'name tab))
(select-window (get-buffer-window bname)))
(funcall fallback-fn buffer))))
(defun helm-buffers-switch-to-buffer-or-tab (buffer)
"Switch to BUFFER in its tab if some."
(if (and (fboundp 'tab-bar-mode)
helm-buffers-maybe-switch-to-tab
tab-bar-mode)
(helm-buffers-maybe-switch-to-buffer-in-tab
buffer #'switch-to-buffer)
(switch-to-buffer buffer)))
(defun helm-buffers--get-tab-from-name (tab-name tabs)
"Return tab from TABS when it contains TAB-NAME."
(cl-loop for tab in tabs
when (member tab-name (split-string (cdr (assq 'name tab)) ", " t))
return tab))
(defun helm-buffers-switch-buffers-in-tab-1 (buffers)
"Display BUFFERS in a new tab.
If only one buffer in BUFFERS, try to switch to it in its
tab if some, otherwise, display it in a new tab.
When a double prefix arg is given (C-u C-u), display each buffer of BUFFERS in
new tabs. When a single buffer is given, a single prefix arg will force
displaying buffer in a new tab even if it is already present in a tab.
When there is more than one buffer in BUFFERS a single prefix arg display the
buffers vertically according to `helm-window-show-buffers-function'."
(when (fboundp 'switch-to-buffer-other-tab)
(if (cdr buffers)
(if (equal helm-current-prefix-arg '(16))
(cl-loop for b in buffers
do (switch-to-buffer-other-tab b))
(switch-to-buffer-other-tab (car buffers))
(helm-window-show-buffers buffers))
(if helm-current-prefix-arg
(switch-to-buffer-other-tab (car buffers))
(helm-buffers-maybe-switch-to-buffer-in-tab
(car buffers) #'switch-to-buffer-other-tab)))))
(defun helm--get-tab-names ()
(let ((tab-bar-tab-name-function #'tab-bar-tab-name-all))
(mapcar (lambda (tab)
(cdr (assq 'name tab)))
(tab-bar-tabs))))
(defun helm-buffers--buffer-in-tab-p (buffer-name &optional tab-names)
"Check if BUFFER-NAME is in TAB-NAMES list."
(cl-loop for name in (or tab-names (helm--get-tab-names))
;; Buf names are separated with "," in TAB-NAMES
;; e.g. '("tab-bar.el" "*scratch*, helm-buffers.el").
thereis (member buffer-name (split-string name ", " t))))
(defun helm-window-decide-split-fn (candidates &optional other-window-fn)
"Try to find the best split window fn according to the number of CANDIDATES."
(let ((fn (cond (;; 4 or more.
(>= (length candidates) 4)
#'helm-window-mosaic-fn)
;; 3 only.
((= (length candidates) 3)
#'helm-window-alternate-split-fn)
;; 2 or less.
(t #'helm-window-default-split-fn))))
(funcall fn candidates other-window-fn)))
(defun helm-window-default-split-fn (candidates &optional other-window-fn)
"Split windows in one direction and balance them.
Direction can be controlled via `helm-window-prefer-horizontal-split'.
If a prefix arg is given split windows the other direction.
This function is suitable for `helm-window-show-buffers-function'."
(if other-window-fn
(funcall other-window-fn (car candidates))
(switch-to-buffer (car candidates)))
(save-selected-window
(cl-loop with nosplit
for b in (cdr candidates)
when nosplit return
(message "Too many buffers to visit simultaneously")
do (condition-case _err
(helm-window-other-window b 'balance)
(error (setq nosplit t) nil)))))
(defun helm-window-alternate-split-fn (candidates &optional other-window-fn)
"Split windows horizontally and vertically in alternate fashion.
Direction can be controlled via `helm-window-prefer-horizontal-split'.
If a prefix arg is given split windows the other direction.
This function is suitable for `helm-window-show-buffers-function'."
(if other-window-fn
(funcall other-window-fn (car candidates))
(switch-to-buffer (car candidates)))
(let (right-side)
(save-selected-window
(cl-loop with nosplit
for b in (cdr candidates)
when nosplit return
(message "Too many buffers to visit simultaneously")
do (condition-case _err
(let ((helm-current-prefix-arg right-side))
(helm-window-other-window b)
(setq right-side (not right-side)))
(error (setq nosplit t) nil))))))
(defun helm-window-mosaic-fn (candidates &optional other-window-fn)
"Make an as-square-as-possible window mosaic of the CANDIDATES buffers.
If rectangular, the long side is in the direction given by
`helm-window-prefer-horizontal-split': if non-nil, it is horizontal, vertical
otherwise.
If OTHER-WINDOW-FN is non-nil, current windows are included in the mosaic.
This function is suitable for `helm-window-show-buffers-function'."
(when other-window-fn
(setq candidates (append (mapcar 'window-buffer (window-list)) candidates)))
(delete-other-windows)
(let* ((helm-window-prefer-horizontal-split
(if (eq helm-window-prefer-horizontal-split 'decide)
(and (numberp split-width-threshold)
(>= (window-width (selected-window))
split-width-threshold))
helm-window-prefer-horizontal-split))
mosaic-length-tile-count
mosaic-width-tile-count
mosaic-length-tile-size
mosaic-width-tile-size
next-window)
;; If 4 tiles, make 2x2 mosaic.
;; If 5-6 tiles, make 2x3 mosaic with direction depending on `helm-window-prefer-horizontal-split'.
;; If 7-9 tiles, make 3x3 mosaic. And so on.
(setq mosaic-length-tile-count (ceiling (sqrt (length candidates))))
(setq mosaic-width-tile-count
(if (<= (length candidates) (* mosaic-length-tile-count (1- mosaic-length-tile-count)))
(1- mosaic-length-tile-count)
mosaic-length-tile-count))
;; We lower-bound the tile size, otherwise the function would
;; fail during the first inner split.
;; There is consequently no need to check for errors when
;; splitting.
(let ((frame-mosaic-length-direction-size (frame-height))
(frame-mosaic-width-direction-size (frame-width))
(window-mosaic-length-direction-min-size window-min-height)
(window-mosaic-width-direction-min-size window-min-width))
(if helm-window-prefer-horizontal-split
(setq frame-mosaic-length-direction-size (frame-width)
frame-mosaic-width-direction-size (frame-height)
window-mosaic-length-direction-min-size window-min-width
window-mosaic-width-direction-min-size window-min-height))
(setq mosaic-length-tile-size (max
(/ frame-mosaic-length-direction-size mosaic-length-tile-count)
window-mosaic-length-direction-min-size)
mosaic-width-tile-size (max
(/ frame-mosaic-width-direction-size mosaic-width-tile-count)
window-mosaic-width-direction-min-size))
;; Shorten `candidates' to `max-tiles' elements.
(let ((max-tiles (* (/ frame-mosaic-length-direction-size mosaic-length-tile-size)
(/ frame-mosaic-width-direction-size mosaic-width-tile-size))))
(when (> (length candidates) max-tiles)
(message "Too many buffers to visit simultaneously")
(setcdr (nthcdr (- max-tiles 1) candidates) nil))))
;; Make the mosaic.
(while candidates
(when (> (length candidates) mosaic-length-tile-count)
(setq next-window (split-window nil
mosaic-width-tile-size
(not helm-window-prefer-horizontal-split))))
(switch-to-buffer (pop candidates))
(dotimes (_ (min (1- mosaic-length-tile-count) (length candidates)))
(select-window (split-window nil
mosaic-length-tile-size
helm-window-prefer-horizontal-split))
(switch-to-buffer (pop candidates)))
(when next-window
(select-window next-window)))))
(defun helm-window-other-window (buffer-or-name &optional balance)
"Switch to BUFFER-OR-NAME in other window.
Direction can be controlled via `helm-window-prefer-horizontal-split'.
If a prefix arg is given split windows the other direction.
When argument BALANCE is provided `balance-windows'."
(let* ((helm-window-prefer-horizontal-split
(if (eq helm-window-prefer-horizontal-split 'decide)
(and (numberp split-width-threshold)
(>= (window-width (selected-window))
split-width-threshold))
helm-window-prefer-horizontal-split))
(right-side (if helm-window-prefer-horizontal-split
(not helm-current-prefix-arg)
helm-current-prefix-arg)))
(select-window (split-window nil nil right-side))
(and balance (balance-windows))
(switch-to-buffer buffer-or-name)))
(cl-defun helm-current-buffer-narrowed-p (&optional
(buffer helm-current-buffer))
"Check if BUFFER is narrowed.
Default is `helm-current-buffer'."
(with-current-buffer buffer
(let ((beg (point-min))
(end (point-max))
(total (buffer-size)))
(or (/= beg 1) (/= end (1+ total))))))
(defun helm-goto-char (loc)
"Go to char, revealing if necessary."
(goto-char loc)
(let ((fn (cond ((eq major-mode 'org-mode)
;; On some old Emacs versions org may not be loaded.
(require 'org)
#'org-reveal)
((and (boundp 'outline-minor-mode)
outline-minor-mode)
#'outline-show-subtree)
((and (boundp 'hs-minor-mode)
hs-minor-mode)
#'hs-show-all)
((and (boundp 'markdown-mode-map)
(derived-mode-p 'markdown-mode))
#'markdown-show-entry)))
(hs-show-hook (list (lambda () (goto-char loc)))))
;; outline may fail in some conditions e.g. with markdown enabled
;; (Bug#1919).
(condition-case-unless-debug nil
(and fn (funcall fn))
(error nil))))
(defun helm-goto-line (lineno &optional noanim)
"Goto LINENO opening only outline headline if needed.
Animation is used unless NOANIM is non--nil."
(helm-log-run-hook "helm-goto-line"
'helm-goto-line-before-hook)
(helm-match-line-cleanup)
(unless helm-alive-p
(with-helm-current-buffer
(unless helm-yank-point (setq helm-yank-point (point)))))
(goto-char (point-min))
(helm-goto-char (pos-bol lineno))
(unless noanim
(helm-highlight-current-line)))
(defun helm-save-pos-to-register-before-jump ()
"Save current buffer position to `helm-save-pos-before-jump-register'.
To use this add it to `helm-goto-line-before-hook'."
(point-to-register helm-save-pos-before-jump-register))
(defun helm-save-current-pos-to-mark-ring ()
"Save current buffer position to mark ring.
To use this add it to `helm-goto-line-before-hook'."
(set-marker (mark-marker) (point))
(push-mark (point) 'nomsg))
(defun helm-displaying-source-names ()
"Return the list of sources name for this helm session."
(with-current-buffer helm-buffer
(goto-char (point-min))
(cl-loop with pos
while (setq pos (next-single-property-change (point) 'helm-header))
do (goto-char pos)
collect (buffer-substring-no-properties (pos-bol)(pos-eol))
do (forward-line 1))))
(defun helm-handle-winner-boring-buffers ()
"Add `helm-buffer' to `winner-boring-buffers' when quitting/exiting helm.
Add this function to `helm-cleanup-hook' when you don't want to see helm buffers
after running winner-undo/redo."
(require 'winner)
(cl-pushnew helm-buffer winner-boring-buffers :test 'equal))
(add-hook 'helm-cleanup-hook #'helm-handle-winner-boring-buffers)
(defun helm-quit-and-find-file ()
"Drop into `helm-find-files' from `helm'.
If current selection is a buffer or a file, `helm-find-files'
from its directory."
(interactive)
(with-helm-alive-p
(require 'helm-grep)
(require 'helm-elisp)
(require 'bookmark) ; For bookmark-alist
(let ((src (helm-get-current-source)))
(helm-run-after-exit
(lambda (f)
;; Ensure specifics `helm-execute-action-at-once-if-one'
;; fns don't run here.
(let (helm-execute-action-at-once-if-one
helm-actions-inherit-frame-settings) ; use this-command
(if (file-exists-p f)
(helm-find-files-1 (file-name-directory f)
(format
helm-ff-last-expanded-candidate-regexp
(regexp-quote
(if helm-ff-transformer-show-only-basename
(helm-basename f) f))))
(helm-find-files-1 f))))
(helm--quit-and-find-file-default-file src)))))
(put 'helm-quit-and-find-file 'helm-only t)
(defun helm--quit-and-find-file-default-file (source)
(let ((target-fn (or (helm-get-attr 'find-file-target source)
(assoc-default (helm-get-attr 'name source)
helm-mode-find-file-target-alist))))
;; target-fn function may return nil, in this case fallback to default.
(helm-aif (and target-fn (funcall target-fn source))
it
(let* ((sel (helm-get-selection nil nil source))
(default-preselection (or (helm-default-directory)
(buffer-file-name helm-current-buffer)
default-directory)))
(cond
((and (stringp sel) (or (file-remote-p sel)
(file-exists-p sel)))
(expand-file-name sel))
;; Url.
((and (stringp sel)
helm--url-regexp
(string-match helm--url-regexp sel))
sel)
;; Exit brutally from a `with-helm-show-completion'
((and helm-show-completion-overlay
(overlayp helm-show-completion-overlay))
(delete-overlay helm-show-completion-overlay)
(remove-hook 'helm-move-selection-after-hook 'helm-show-completion)
(expand-file-name default-preselection))
;; Default.
(t (expand-file-name default-preselection)))))))
(defun helm-generic-sort-fn (s1 s2)
"Sort predicate function for helm candidates.
Args S1 and S2 can be single or (display . real) candidates,
that is sorting is done against real value of candidate."
(let* ((qpattern (regexp-quote helm-pattern))
(reg1 (concat "\\_<" qpattern "\\_>"))
(reg2 (concat "\\_<" qpattern))
(reg3 helm-pattern)
(split (helm-remove-if-match
"\\`!" (helm-mm-split-pattern helm-pattern)))
(str1 (if (consp s1) (cdr s1) s1))
(str2 (if (consp s2) (cdr s2) s2))
(score (lambda (str r1 r2 r3 lst)
(condition-case nil
(+ (if (string-match (concat "\\`" qpattern) str) 1 0)
(cond ((string-match r1 str) 5)
((and (string-match " " qpattern)
(car lst)
(string-match
(concat "\\_<" (regexp-quote (car lst))) str)
(cl-loop for r in (cdr lst)
always (string-match r str)))
4)
((and (string-match " " qpattern)
(cl-loop for r in lst
always (string-match r str)))
3)
((string-match r2 str) 2)
((string-match r3 str) 1)
(t 0)))
(invalid-regexp 0))))
(sc1 (get-text-property 0 'completion-score str1))
(sc2 (get-text-property 0 'completion-score str2))
(sc3 (if sc1 0 (funcall score str1 reg1 reg2 reg3 split)))
(sc4 (if sc2 0 (funcall score str2 reg1 reg2 reg3 split))))
(cond ((and sc1 sc2) ; helm-flex style.
(> sc1 sc2))
((or (zerop (string-width qpattern))
(and (zerop sc3) (zerop sc4)))
(string-lessp str1 str2))
((= sc3 sc4)
(< (length str1) (length str2)))
(t (> sc3 sc4)))))
(cl-defun helm-file-human-size (size &optional (kbsize helm-default-kbsize))
"Return a string showing SIZE of a file in human readable form.
SIZE can be an integer or a float depending on it's value.
`file-attributes' will take care of that to avoid overflow error.
KBSIZE is a floating point number, defaulting to `helm-default-kbsize'."
(cl-loop with result = (cons "B" size)
for i in '("k" "M" "G" "T" "P" "E" "Z" "Y")
while (>= (cdr result) kbsize)
do (setq result (cons i (/ (cdr result) kbsize)))
finally return
(helm-acase (car result)
("B" (format "%s" size))
(t (format "%.1f%s" (cdr result) it)))))
(defun helm-directory-size (directory &optional recursive human)
"Return the resulting size of the sum of all files in DIRECTORY.
If RECURSIVE is non nil return the size of all files in DIRECTORY and
its subdirectories. With arg HUMAN format the size in a human
readable format,see `helm-file-human-size'."
(cl-loop with files = (if recursive
(helm-walk-directory
directory
:path 'full
:directories t)
(directory-files
directory t
directory-files-no-dot-files-regexp))
for file in files
sum (nth 7 (file-attributes file)) into total
finally return (if human
(helm-file-human-size total)
total)))
(defun helm-format-time-string (time)
"Format the time value TIME.
If year of TIME is inferior to current year
use \"%b %e %Y\" otherwise use \"%b %e %H:%M\"."
(let ((current-year (nth 5 (decode-time)))
(year (nth 5 (decode-time time)))
(fmt-opt '("%b %e %H:%M"
"%b %e %Y")))
(if (< year current-year)
(format-time-string (nth 1 fmt-opt) time)
(format-time-string (nth 0 fmt-opt) time))))
(cl-defun helm-file-attributes
(file &key type links uid gid access-time modif-time
status size mode gid-change inode device-num dired human-size
mode-type mode-owner mode-group mode-other octal (string t))
"Return `file-attributes' elements of FILE separately according to key value.
Availables keys are:
- TYPE: Same as nth 0 `files-attributes' if STRING is nil
otherwise return either symlink, directory or file (default).
- LINKS: See nth 1 `files-attributes'.
- UID: See nth 2 `files-attributes'.
- GID: See nth 3 `files-attributes'.
- ACCESS-TIME: See nth 4 `files-attributes', however format time
when STRING is non--nil (the default).
- MODIF-TIME: See nth 5 `files-attributes', same as above.
- STATUS: See nth 6 `files-attributes', same as above.
- SIZE: See nth 7 `files-attributes'.
- MODE: See nth 8 `files-attributes'.
- GID-CHANGE: See nth 9 `files-attributes'.
- INODE: See nth 10 `files-attributes'.
- DEVICE-NUM: See nth 11 `files-attributes'.
- DIRED: A line similar to what \\='ls -l' return.
- HUMAN-SIZE: The size in human form, see `helm-file-human-size'.
- MODE-TYPE, mode-owner,mode-group, mode-other: Split what
nth 8 `files-attributes' return in four categories.
- OCTAL: The octal value of MODE-OWNER+MODE-GROUP+MODE-OTHER.
- STRING: When non--nil (default) `helm-file-attributes' return
more friendly values.
If you want the same behavior as `files-attributes' ,
\(but with return values in proplist) use a nil value for STRING.
However when STRING is non--nil, time and type value are different from what
you have in `file-attributes'."
(helm-aif (file-attributes file string)
(let* ((all (cl-destructuring-bind
(type links uid gid access-time modif-time
status size mode gid-change inode device-num)
it
(list :type (if string
(cond ((stringp type) "symlink") ; fname
(type "directory") ; t
(t "file")) ; nil
type)
:links links
:uid uid
:gid gid
:access-time (if string
(helm-format-time-string access-time)
access-time)
:modif-time (if string
(helm-format-time-string modif-time)
modif-time)
:status (if string
(format-time-string
"%Y-%m-%d %R" status)
status)
:size size
:mode mode
:gid-change gid-change
:inode inode
:device-num device-num)))
(perms (cl-getf all :mode))
(modes (helm-split-mode-file-attributes perms nil t)))
(cond (type (cl-getf all :type))
(links (cl-getf all :links))
(uid (cl-getf all :uid))
(gid (cl-getf all :gid))
(access-time (cl-getf all :access-time))
(modif-time (cl-getf all :modif-time))
(status (cl-getf all :status))
(size (cl-getf all :size))
(mode perms)
(gid-change (cl-getf all :gid-change))
(inode (cl-getf all :inode))
(device-num (cl-getf all :device-num))
(dired (helm-file-attributes-dired-line
all human-size octal))
(human-size (helm-file-human-size (cl-getf all :size)))
(mode-type (cl-getf modes :mode-type))
(mode-owner (cl-getf modes :user))
(mode-group (cl-getf modes :group))
(mode-other (cl-getf modes :other))
(octal (cl-getf modes :octal))
(t (append all modes))))))
(defun helm-file-attributes-dired-line (all &optional human-size show-octal)
(format "%s %s %s:%s %s %s"
(helm-split-mode-file-attributes
(cl-getf all :mode) t show-octal)
(number-to-string (cl-getf all :links))
(cl-getf all :uid)
(cl-getf all :gid)
(if human-size
(helm-file-human-size (cl-getf all :size))
(int-to-string (cl-getf all :size)))
(cl-getf all :modif-time)))
(defun helm-split-mode-file-attributes (modes &optional string show-octal)
"Split MODES in a list of modes.
MODES is same as what (nth 8 (file-attributes \"foo\")) would return."
(if (string-match "\\`\\(.\\)\\(...\\)\\(...\\)\\(...\\)\\'" modes)
(let* ((type (match-string 1 modes))
(user (match-string 2 modes))
(group (match-string 3 modes))
(other (match-string 4 modes))
(octal (and show-octal
(helm-ff-numeric-permissions
(list user group other)))))
(if string
(mapconcat 'identity (list type user group other (or octal "")) "")
(list :mode-type type :user user
:group group :other other
:octal octal)))
(error "Wrong modes specification for %s" modes)))
(defun helm-ff-numeric-permissions (perms)
"Return the numeric representation of PERMS.
PERMS is the list of permissions for owner, group and others."
;; `file-modes-symbolic-to-number' interpret its MODES argument as what would
;; result when calling such mode on a file with chmod, BTW we have to remove
;; all "-" like read-file-modes does.
(helm-aand (listp perms)
(apply 'format "u=%s,g=%s,o=%s" perms)
(replace-regexp-in-string "-" "" it)
(format "%o" (file-modes-symbolic-to-number it))))
(defun helm-format-columns-of-files (files)
"Same as `dired-format-columns-of-files'.
Inlined here for compatibility."
(let ((beg (point)))
(completion--insert-strings files)
(put-text-property beg (point) 'mouse-face nil)))
(defmacro with-helm-display-marked-candidates (buffer-or-name candidates &rest body)
(declare (indent 0) (debug t))
(helm-with-gensyms (buffer window winconf)
`(let* ((,buffer (temp-buffer-window-setup ,buffer-or-name))
(,winconf helm-last-frame-or-window-configuration)
(helm-always-two-windows t)
(helm-split-window-default-side
(if (eq helm-split-window-default-side 'same)
'below helm-split-window-default-side))
helm-split-window-inside-p
helm-reuse-last-window-split-state
,window)
(with-current-buffer ,buffer
(helm-format-columns-of-files ,candidates))
(unwind-protect
(with-selected-window
(setq ,window (temp-buffer-window-show
,buffer
'(display-buffer-below-selected
(window-height . fit-window-to-buffer))))
(progn ,@body))
(quit-window 'kill ,window)
(and ,winconf (set-window-configuration ,winconf))))))
;;; Persistent Action Helpers
;;
;;
;; Internal
(defvar helm-match-line-overlay nil)
(defvar helm--match-item-overlays nil)
(cl-defun helm-highlight-current-line (&optional start end buf face)
"Highlight current line and all matching items around it.
The number of lines around matched line where the matching items are
highlighted are defined by `helm-highlight-matches-around-point-max-lines'.
When the variable `helm-highlight-only-all-matches' is non nil only
the lines containing all matches (in case of multi match) are highlighted.
Optional arguments START, END and FACE are only here for debugging purpose."
(let* ((start (or start (line-beginning-position)))
(end (or end (1+ (line-end-position))))
start-match end-match
(args (list start end buf))
(case-fold-search (if helm-alive-p
(helm-set-case-fold-search)
case-fold-search)))
;; Highlight the current line.
(if (not helm-match-line-overlay)
(setq helm-match-line-overlay (apply 'make-overlay args))
(apply 'move-overlay helm-match-line-overlay args))
(overlay-put helm-match-line-overlay
'face (or face 'helm-selection-line))
;; Now highlight matches only if we are in helm session, we are
;; maybe coming from helm-grep-mode or helm-moccur-mode buffers.
(when helm-alive-p
(helm-acase helm-highlight-matches-around-point-max-lines
;; Next 2 clauses must precede others otherwise
;; `helm-highlight-matches-around-point-max-lines' is
;; compared as a number by other clauses and return an error.
(never (cl-return-from helm-highlight-current-line))
((guard (consp it))
(setq start-match (save-excursion (forward-line (- (car it))) (pos-bol))
end-match (save-excursion (forward-line (cdr it)) (pos-bol))))
((guard (or (null it) (zerop it)))
(setq start-match start
end-match end))
((guard (< it 0))
(setq start-match (save-excursion (forward-line it) (pos-bol))
end-match start))
((guard (> it 0))
(setq start-match start
end-match (save-excursion (forward-line it) (pos-bol)))))
(catch 'empty-line
(let* ((regex-list (helm-remove-if-match
"\\`!" (helm-mm-split-pattern
(if (with-helm-buffer
;; Needed for highlighting AG matches.
(assq 'pcre (helm-get-current-source)))
(helm--translate-pcre-to-elisp helm-input)
helm-input))))
(num-regex (length regex-list)))
(save-excursion
(goto-char start-match)
(while (< (point) end-match)
(let* ((start-line (line-beginning-position))
(end-line (line-end-position))
all-matches)
(dolist (r regex-list)
(let ((match-list '()))
(save-excursion
(goto-char start-line)
(while (condition-case _err
(and (not (= start-line end-line))
(if helm-migemo-mode
(helm-mm-migemo-forward r end-line t)
(re-search-forward r end-line t)))
(invalid-regexp nil))
(let ((s (match-beginning 0))
(e (match-end 0)))
(if (= s e)
(throw 'empty-line nil)
(push (cons s e) match-list)))))
(when match-list
(push match-list all-matches))))
(when (and all-matches
(or (not helm-highlight-only-all-matches)
(eql (length all-matches) num-regex)))
(cl-loop for ml in all-matches
do (cl-loop for (s . e) in ml
for ov = (make-overlay s e)
do (progn
(push ov helm--match-item-overlays)
(overlay-put ov 'face 'helm-match-item)
(overlay-put ov 'priority 1))))))
(forward-line 1))))))
(recenter)))
(defun helm--translate-pcre-to-elisp (regexp)
"Should translate pcre REGEXP to elisp regexp.
Assume regexp is a pcre based regexp."