/
init-funcs.el
721 lines (617 loc) · 22.7 KB
/
init-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
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
;; init-funcs.el --- Define functions. -*- lexical-binding: t -*-
;; Copyright (C) 2018-2024 Vincent Zhang
;; Author: Vincent Zhang <seagle0128@gmail.com>
;; URL: https://github.com/seagle0128/.emacs.d
;; This file is not part of GNU Emacs.
;;
;; 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, 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;; Commentary:
;;
;; Define some useful functions.
;;
;;; Code:
(require 'cl-lib)
;; Suppress warnings
(eval-when-compile
(require 'init-const)
(require 'init-custom))
(defvar socks-noproxy)
(defvar socks-server)
(declare-function browse-url-interactive-arg "browse-url")
(declare-function chart-bar-quickie "chart")
(declare-function consult-theme "ext:consult")
(declare-function nerd-icons-install-fonts "ext:nerd-icons")
(declare-function xwidget-buffer "xwidget")
(declare-function xwidget-webkit-current-session "xwidget")
;; Font
(defun font-installed-p (font-name)
"Check if font with FONT-NAME is available."
(find-font (font-spec :name font-name)))
;; Dos2Unix/Unix2Dos
(defun dos2unix ()
"Convert the current buffer to UNIX file format."
(interactive)
(set-buffer-file-coding-system 'undecided-unix nil))
(defun unix2dos ()
"Convert the current buffer to DOS file format."
(interactive)
(set-buffer-file-coding-system 'undecided-dos nil))
(defun delete-dos-eol ()
"Delete `' characters in current region or buffer.
Same as '`replace-string' `C-q' `C-m' `RET' `RET''."
(interactive)
(save-excursion
(when (region-active-p)
(narrow-to-region (region-beginning) (region-end)))
(goto-char (point-min))
(let ((count 0))
(while (search-forward "\r" nil t)
(replace-match "" nil t)
(setq count (1+ count)))
(message "Removed %d " count))
(widen)))
;; File and buffer
(defun revert-this-buffer ()
"Revert the current buffer."
(interactive)
(unless (minibuffer-window-active-p (selected-window))
(revert-buffer t t)
(message "Reverted this buffer")))
(defun delete-this-file ()
"Delete the current file, and kill the buffer."
(interactive)
(unless (buffer-file-name)
(error "No file is currently being edited"))
(when (yes-or-no-p (format "Really delete '%s'?"
(file-name-nondirectory buffer-file-name)))
(delete-file (buffer-file-name))
(kill-this-buffer)))
(defun rename-this-file (new-name)
"Renames both current buffer and file it's visiting to NEW-NAME."
(interactive "sNew name: ")
(let ((name (buffer-name))
(filename (buffer-file-name)))
(unless filename
(error "Buffer '%s' is not visiting a file!" name))
(progn
(when (file-exists-p filename)
(rename-file filename new-name 1))
(set-visited-file-name new-name)
(rename-buffer new-name))))
(defun browse-this-file ()
"Open the current file as a URL using `browse-url'."
(interactive)
(let ((file-name (buffer-file-name)))
(if (and (fboundp 'tramp-tramp-file-p)
(tramp-tramp-file-p file-name))
(error "Cannot open tramp file")
(browse-url (concat "file://" file-name)))))
(defun copy-file-name ()
"Copy the current buffer file name to the clipboard."
(interactive)
(let ((filename (if (equal major-mode 'dired-mode)
default-directory
(buffer-file-name))))
(if filename
(progn
(kill-new filename)
(message "Copied '%s'" filename))
(warn "Current buffer is not attached to a file!"))))
;; Browse URL
(defun centaur-webkit-browse-url (url &optional pop-buffer new-session)
"Browse URL with xwidget-webkit' and switch or pop to the buffer.
POP-BUFFER specifies whether to pop to the buffer.
NEW-SESSION specifies whether to create a new xwidget-webkit session."
(interactive (progn
(require 'browse-url)
(browse-url-interactive-arg "xwidget-webkit URL: ")))
(or (featurep 'xwidget-internal)
(user-error "Your Emacs was not compiled with xwidgets support"))
(xwidget-webkit-browse-url url new-session)
(let ((buf (xwidget-buffer (xwidget-webkit-current-session))))
(when (buffer-live-p buf)
(and (eq buf (current-buffer)) (quit-window))
(if pop-buffer
(pop-to-buffer buf)
(switch-to-buffer buf)))))
;; Mode line
(defun mode-line-height ()
"Get the height of the mode-line."
(- (elt (window-pixel-edges) 3)
(elt (window-inside-pixel-edges) 3)
(if (bound-and-true-p window-divider-mode)
window-divider-default-bottom-width
0)))
;; Reload configurations
(defun reload-init-file ()
"Reload Emacs configurations."
(interactive)
(load user-init-file))
(defalias 'centaur-reload-init-file #'reload-init-file)
;; Browse the homepage
(defun browse-homepage ()
"Browse the Github page of Centaur Emacs."
(interactive)
(browse-url centaur-homepage))
;; Open custom file
(defun find-custom-file()
"Open custom files."
(interactive)
(unless (file-exists-p custom-file)
(if (file-exists-p centaur-custom-example-file)
(copy-file centaur-custom-example-file custom-file)
(user-error "The file `%s' doesn't exist" centaur-custom-example-file)))
(when (file-exists-p custom-file)
(find-file custom-file))
(when (file-exists-p centaur-custom-post-file)
(find-file-other-window centaur-custom-post-file)))
;; Misc
(defun create-scratch-buffer ()
"Create a scratch buffer."
(interactive)
(switch-to-buffer (get-buffer-create "*scratch*"))
(lisp-interaction-mode))
(defun save-buffer-as-utf8 (coding-system)
"Revert a buffer with `CODING-SYSTEM' and save as UTF-8."
(interactive "zCoding system for visited file (default nil):")
(revert-buffer-with-coding-system coding-system)
(set-buffer-file-coding-system 'utf-8)
(save-buffer))
(defun save-buffer-gbk-as-utf8 ()
"Revert a buffer with GBK and save as UTF-8."
(interactive)
(save-buffer-as-utf8 'gbk))
(defun byte-compile-elpa ()
"Compile packages in elpa directory. Useful if you switch Emacs versions."
(interactive)
(if (fboundp 'async-byte-recompile-directory)
(async-byte-recompile-directory package-user-dir)
(byte-recompile-directory package-user-dir 0 t)))
(defun byte-compile-site-lisp ()
"Compile packages in site-lisp directory."
(interactive)
(let ((dir (locate-user-emacs-file "site-lisp")))
(if (fboundp 'async-byte-recompile-directory)
(async-byte-recompile-directory dir)
(byte-recompile-directory dir 0 t))))
(defun native-compile-elpa ()
"Native-compile packages in elpa directory."
(interactive)
(if (fboundp 'native-compile-async)
(native-compile-async package-user-dir t)))
(defun native-compile-site-lisp ()
"Native compile packages in site-lisp directory."
(interactive)
(let ((dir (locate-user-emacs-file "site-lisp")))
(if (fboundp 'native-compile-async)
(native-compile-async dir t))))
(defun icons-displayable-p ()
"Return non-nil if icons are displayable."
(and centaur-icon
(or (featurep 'nerd-icons)
(require 'nerd-icons nil t))))
(defun centaur-treesit-available-p ()
"Check whether tree-sitter is available.
Native tree-sitter is introduced since 29.1."
(and centaur-tree-sitter
(fboundp 'treesit-available-p)
(treesit-available-p)))
(defun centaur-set-variable (variable value &optional no-save)
"Set the VARIABLE to VALUE, and return VALUE.
Save to option `custom-file' if NO-SAVE is nil."
(customize-set-variable variable value)
(when (and (not no-save)
(file-writable-p custom-file))
(with-temp-buffer
(insert-file-contents custom-file)
(goto-char (point-min))
(while (re-search-forward
(format "^[\t ]*[;]*[\t ]*(setq %s .*)" variable)
nil t)
(replace-match (format "(setq %s '%s)" variable value) nil nil))
(write-region nil nil custom-file)
(message "Saved %s (%s) to %s" variable value custom-file))))
(defun too-long-file-p ()
"Check whether the file is too long."
(or (> (buffer-size) 500000)
(and (fboundp 'buffer-line-statistics)
(> (car (buffer-line-statistics)) 10000))))
(define-minor-mode centaur-read-mode
"Minor Mode for better reading experience."
:init-value nil
:group centaur
(if centaur-read-mode
(progn
(and (fboundp 'olivetti-mode) (olivetti-mode 1))
(and (fboundp 'mixed-pitch-mode) (mixed-pitch-mode 1))
(text-scale-set +1))
(progn
(and (fboundp 'olivetti-mode) (olivetti-mode -1))
(and (fboundp 'mixed-pitch-mode) (mixed-pitch-mode -1))
(text-scale-set 0))))
;; Pakcage repository (ELPA)
(defun set-package-archives (archives &optional refresh async no-save)
"Set the package ARCHIVES (ELPA).
REFRESH is non-nil, will refresh archive contents.
ASYNC specifies whether to perform the downloads in the background.
Save to option `custom-file' if NO-SAVE is nil."
(interactive
(list
(intern
(completing-read "Select package archives: "
(mapcar #'car centaur-package-archives-alist)))))
;; Set option
(centaur-set-variable 'centaur-package-archives archives no-save)
;; Refresh if need
(and refresh (package-refresh-contents async))
(message "Set package archives to `%s'" archives))
(defalias 'centaur-set-package-archives #'set-package-archives)
;; Refer to https://emacs-china.org/t/elpa/11192
(defun centaur-test-package-archives (&optional no-chart)
"Test connection speed of all package archives and display on chart.
Not displaying the chart if NO-CHART is non-nil.
Return the fastest package archive."
(interactive)
(let* ((durations (mapcar
(lambda (pair)
(let ((url (concat (cdr (nth 2 (cdr pair)))
"archive-contents"))
(start (current-time)))
(message "Fetching %s..." url)
(ignore-errors
(url-copy-file url null-device t))
(float-time (time-subtract (current-time) start))))
centaur-package-archives-alist))
(fastest (car (nth (cl-position (apply #'min durations) durations)
centaur-package-archives-alist))))
;; Display on chart
(when (and (not no-chart)
(require 'chart nil t)
(require 'url nil t))
(chart-bar-quickie
'vertical
"Speed test for the ELPA mirrors"
(mapcar (lambda (p) (symbol-name (car p))) centaur-package-archives-alist)
"ELPA"
(mapcar (lambda (d) (* 1e3 d)) durations) "ms"))
(message "`%s' is the fastest package archive" fastest)
;; Return the fastest
fastest))
(defun set-from-minibuffer (sym)
"Set SYM value from minibuffer."
(eval-expression
(minibuffer-with-setup-hook
(lambda ()
(add-function :before-until (local 'eldoc-documentation-function)
#'elisp-eldoc-documentation-function)
(run-hooks 'eval-expression-minibuffer-setup-hook)
(goto-char (minibuffer-prompt-end))
(forward-char (length (format "(setq %S " sym))))
(read-from-minibuffer
"Eval: "
(let ((sym-value (symbol-value sym)))
(format
(if (or (consp sym-value)
(and (symbolp sym-value)
(not (null sym-value))
(not (keywordp sym-value))))
"(setq %s '%S)"
"(setq %s %S)")
sym sym-value))
read-expression-map t
'read-expression-history))))
;; WORKAROUND: fix blank screen issue on macOS.
(defun fix-fullscreen-cocoa ()
"Address blank screen issue with child-frame in fullscreen.
This issue has been addressed in 28."
(and sys/mac-cocoa-p
(not emacs/>=28p)
(bound-and-true-p ns-use-native-fullscreen)
(setq ns-use-native-fullscreen nil)))
;; Update
(defun update-config ()
"Update Centaur Emacs configurations to the latest version."
(interactive)
(let ((dir (expand-file-name user-emacs-directory)))
(unless (file-exists-p dir)
(user-error "\"%s\" doesn't exist" dir))
(message "Updating configurations...")
(cd dir)
(shell-command "git pull")
(message "Updating configurations...done")))
(defalias 'centaur-update-config #'update-config)
(defun update-packages ()
"Refresh package contents and update all packages."
(interactive)
(message "Updating packages...")
(package-upgrade-all)
(message "Updating packages...done"))
(defalias 'centaur-update-packages #'update-packages)
(defun update-config-and-packages()
"Update confgiurations and packages."
(interactive)
(update-config)
(update-packages))
(defalias 'centaur-update #'update-config-and-packages)
(defun update-dotfiles ()
"Update the dotfiles to the latest version."
(interactive)
(let ((dir (or (getenv "DOTFILES")
(expand-file-name "~/.dotfiles/"))))
(if (file-exists-p dir)
(progn
(message "Updating dotfiles...")
(cd dir)
(shell-command "git pull")
(message "Updating dotfiles...done"))
(message "\"%s\" doesn't exist" dir))))
(defalias 'centaur-update-dotfiles #'update-dotfiles)
(defun update-org ()
"Update Org files to the latest version."
(interactive)
(let ((dir (expand-file-name "~/org/")))
(if (file-exists-p dir)
(progn
(message "Updating org files...")
(cd dir)
(shell-command "git pull")
(message "Updating org files...done"))
(message "\"%s\" doesn't exist" dir))))
(defalias 'centaur-update-org #'update-org)
(defun update-all()
"Update dotfiles, org files, configurations and packages to the latest."
(interactive)
(update-org)
(update-dotfiles)
(update-config-and-packages))
(defalias 'centaur-update-all #'update-all)
;; Fonts
(defun centaur-install-fonts ()
"Install necessary fonts."
(interactive)
(nerd-icons-install-fonts))
;; UI
(defvar after-load-theme-hook nil
"Hook run after a color theme is loaded using `load-theme'.")
(defun run-after-load-theme-hook (&rest _)
"Run `after-load-theme-hook'."
(run-hooks 'after-load-theme-hook))
(advice-add #'load-theme :after #'run-after-load-theme-hook)
(defun childframe-workable-p ()
"Whether childframe is workable."
(not (or noninteractive
emacs-basic-display
(not (display-graphic-p)))))
(defun childframe-completion-workable-p ()
"Whether childframe completion is workable."
(and (eq centaur-completion-style 'childframe)
(childframe-workable-p)))
(defun centaur--theme-name (theme)
"Return internal THEME name."
(or (alist-get theme centaur-theme-alist) theme 'doom-one))
(defun centaur-compatible-theme-p (theme)
"Check if the THEME is compatible. THEME is a symbol."
(or (memq theme '(auto random system))
(string-prefix-p "doom" (symbol-name (centaur--theme-name theme)))))
(defun centaur-dark-theme-p ()
"Check if the current theme is a dark theme."
(eq (frame-parameter nil 'background-mode) 'dark))
(defun centaur-theme-enable-p (theme)
"The THEME is enabled or not."
(and theme
(not (memq centaur-theme '(auto random system)))
(memq (centaur--theme-name theme) custom-enabled-themes)))
(defun centaur--load-theme (theme)
"Disable others and enable new THEME."
(when-let ((theme (centaur--theme-name theme)))
(mapc #'disable-theme custom-enabled-themes)
(load-theme theme t)))
(defun centaur--load-system-theme (appearance)
"Load theme, taking current system APPEARANCE into consideration."
(centaur--load-theme (alist-get appearance centaur-system-themes)))
(defun centaur-load-random-theme ()
"Load the random theme."
(interactive)
(let* ((themes (mapcar #'cdr centaur-theme-alist))
(theme (nth (random (length themes)) themes)))
(if (eq theme centaur-theme)
(centaur-load-random-theme)
(centaur--load-theme theme))))
(defun centaur-load-theme (theme &optional no-save)
"Load color THEME. Save to option `custom-file' if NO-SAVE is nil."
(interactive
(list
(intern
(completing-read "Load theme: "
`(auto
random
system
,@(mapcar #'car centaur-theme-alist))))))
;; Disable time-switching themes
(when (fboundp #'circadian-activate-latest-theme)
(cancel-function-timers #'circadian-activate-latest-theme))
;; Disable system theme
(when (bound-and-true-p auto-dark-mode)
(setq auto-dark--last-dark-mode-state 'unknown)
(auto-dark-mode -1))
(pcase theme
('auto
;; Time-switching themes
(use-package circadian
:ensure t
:commands circadian-setup circadian-activate-latest-theme
:custom (circadian-themes centaur-auto-themes)
:init (circadian-setup)))
('system
;; System-appearance themes
(use-package auto-dark
:ensure t
:diminish
:commands auto-dark-mode
:init
(setq auto-dark-light-theme (alist-get 'light centaur-system-themes)
auto-dark-dark-theme (alist-get 'dark centaur-system-themes))
(when (and sys/macp (not (display-graphic-p)))
(setq auto-dark-detection-method 'osascript))
(auto-dark-mode 1)))
('random
(centaur-load-random-theme))
(_
(centaur--load-theme theme)))
;; Set option
(centaur-set-variable 'centaur-theme theme no-save))
(advice-add #'consult-theme :after
(lambda (theme)
"Save theme."
(centaur-set-variable 'centaur-theme theme)))
;; Frame
(defvar centaur-frame--geometry nil)
(defun centaur-frame--save-geometry ()
"Save current frame's geometry."
(setq centaur-frame--geometry
`((left . ,(frame-parameter nil 'left))
(top . ,(frame-parameter nil 'top))
(width . ,(frame-parameter nil 'width))
(height . ,(frame-parameter nil 'height))
(fullscreen))))
(defun centaur-frame--fullscreen-p ()
"Return Non-nil if the frame is fullscreen."
(memq (frame-parameter nil 'fullscreen) '(fullscreen fullboth)))
(defun centaur-frame-maximize ()
"Maximize the frame."
(interactive)
(centaur-frame--save-geometry)
(unless (eq (frame-parameter nil 'fullscreen) 'maximized)
(set-frame-parameter nil 'fullscreen 'maximized)))
(defun centaur-frame-restore ()
"Restore the frame's size and position."
(interactive)
(modify-frame-parameters nil centaur-frame--geometry))
(defun centaur-frame-left-half ()
"Put the frame to the left-half."
(interactive)
(unless (centaur-frame--fullscreen-p)
(centaur-frame--save-geometry)
(let* ((attr (frame-monitor-workarea))
(width (- (/ (nth 2 attr) 2) 20))
(height (- (nth 3 attr) 30))
(left (nth 0 attr))
(top (nth 1 attr)))
(set-frame-parameter nil 'fullscreen nil)
(set-frame-position nil left top)
(set-frame-size nil width height t))))
(defun centaur-frame-right-half ()
"Put the frame to the right-half."
(interactive)
(unless (centaur-frame--fullscreen-p)
(centaur-frame--save-geometry)
(let* ((attr (frame-monitor-workarea))
(width (- (/ (nth 2 attr) 2) 20))
(height (- (nth 3 attr) 30))
(left (+ (nth 0 attr) width 20))
(top (nth 1 attr)))
(set-frame-parameter nil 'fullscreen nil)
(set-frame-position nil left top)
(set-frame-size nil width height t))))
(defun centaur-frame-top-half ()
"Put the frame to the top-half."
(interactive)
(unless (centaur-frame--fullscreen-p)
(centaur-frame--save-geometry)
(let* ((attr (frame-monitor-workarea))
(width (- (nth 2 attr) 20))
(height (- (/ (nth 3 attr) 2) 30))
(left (nth 0 attr))
(top (nth 1 attr)))
(set-frame-parameter nil 'fullscreen nil)
(set-frame-position nil left top)
(set-frame-size nil width height t))))
(defun centaur-frame-bottom-half ()
"Put the frame to the bottom-half."
(interactive)
(unless (centaur-frame--fullscreen-p)
(centaur-frame--save-geometry)
(let* ((attr (frame-monitor-workarea))
(width (- (nth 2 attr) 20))
(height (- (/ (nth 3 attr) 2) 30))
(left (nth 0 attr))
(top (+ (nth 1 attr) height 30)))
(set-frame-parameter nil 'fullscreen nil)
(set-frame-position nil left top)
(set-frame-size nil width height t))))
;; Network Proxy
(defun proxy-http-show ()
"Show HTTP/HTTPS proxy."
(interactive)
(if url-proxy-services
(message "Current HTTP proxy is `%s'" centaur-proxy)
(message "No HTTP proxy")))
(defun proxy-http-enable ()
"Enable HTTP/HTTPS proxy."
(interactive)
(setq url-proxy-services
`(("http" . ,centaur-proxy)
("https" . ,centaur-proxy)
("no_proxy" . "^\\(localhost\\|192.168.*\\|10.*\\)")))
(proxy-http-show))
(defun proxy-http-disable ()
"Disable HTTP/HTTPS proxy."
(interactive)
(setq url-proxy-services nil)
(proxy-http-show))
(defun proxy-http-toggle ()
"Toggle HTTP/HTTPS proxy."
(interactive)
(if (bound-and-true-p url-proxy-services)
(proxy-http-disable)
(proxy-http-enable)))
(defun proxy-socks-show ()
"Show SOCKS proxy."
(interactive)
(if (bound-and-true-p socks-noproxy)
(message "Current SOCKS%d proxy is %s:%s"
(cadddr socks-server) (cadr socks-server) (caddr socks-server))
(message "No SOCKS proxy")))
(defun proxy-socks-enable ()
"Enable SOCKS proxy."
(interactive)
(require 'socks)
(setq url-gateway-method 'socks
socks-noproxy '("localhost"))
(let* ((proxy (split-string centaur-socks-proxy ":"))
(host (car proxy))
(port (string-to-number (cadr proxy))))
(setq socks-server `("Default server" ,host ,port 5)))
(setenv "all_proxy" (concat "socks5://" centaur-socks-proxy))
(proxy-socks-show))
(defun proxy-socks-disable ()
"Disable SOCKS proxy."
(interactive)
(setq url-gateway-method 'native
socks-noproxy nil
socks-server nil)
(setenv "all_proxy" "")
(proxy-socks-show))
(defun proxy-socks-toggle ()
"Toggle SOCKS proxy."
(interactive)
(if (bound-and-true-p socks-noproxy)
(proxy-socks-disable)
(proxy-socks-enable)))
(provide 'init-funcs)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; init-funcs.el ends here