一直以来配置emacs都是从网上抄的,想写一些东西来配置一下emacs使之更加的方便,但是elisp的括号让人望而生畏。所以还是用python来扩展吧。幸好有pymacs这个强大的东东。

官方对其的介绍:

Pymacs is a powerful tool which, once started from Emacs, allows both-way communication between Emacs Lisp and Python. Pymacs aims Python as an extension language for Emacs rather than the other way around, and this asymmetry is reflected in some design choices. Within Emacs Lisp code, one may load and use Python modules. Python functions may themselves use Emacs services, and handle Emacs Lisp objects kept in Emacs Lisp space.

archlinux下安装pymacs:

1
sudo pacman -S pymacs

配置pymacs, 在.emacs中添加:

1
2
3
4
5
6
7
(autoload 'pymacs-apply "pymacs")
(autoload 'pymacs-call "pymacs")
(autoload 'pymacs-eval "pymacs" nil t)
(autoload 'pymacs-exec "pymacs" nil t)
(autoload 'pymacs-load "pymacs" nil t)
(eval-after-load "pymacs"
  '(add-to-list 'pymacs-load-path "~/.emacs.d/my-lisp"))

现在我要将elisp的函数

 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
(defun proxyfunc(server port)
  (setenv "http_proxy" (concat "http://"server":"port))
  (setenv "https_proxy" (concat "https://"server":"port))
  (setenv "ftp_proxy" (concat "http://"server":"port))
)

(defun proxyoff()
  (interactive)
  (setenv "http_proxy" "")
  (setenv "https_proxy" "")
  (setenv "ftp_proxy" "")
)

(defun proxyjpn()
  (interactive)
  (proxyfunc "16.146.32.100" "8080")
)


转换为Python, 如setproxy.py(将文件放到PYTHONPATH中

```python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
    
import os
    
def setproxy(server, port):
    os.environ['http_proxy'] = 'http://'+server+':'+port
    os.environ['https_proxy'] = 'https://'+server+':'+port
	os.environ['ftp_proxy'] = 'http://'+server+':'+port
    
def setproxyoff():
    os.environ['http_proxy'] = ""
	os.environ['https_proxy'] = ""
	os.environ['ftp_proxy'] = ""
    
def proxyjpn():
    setproxy('16.146.32.100', '8080')
    
def proxysgn():
    setproxy('16.158.235.10', '8080')
    
# need to set the property interaction
proxyjpn.interaction = ""
setproxyoff.interaction = ""
proxysgn.interaction = ""

通过pymacs-load加载python模块

1
2
M-x pymacs-load RET MODULE RET PREFIX RET
# e.g. M-x pymacs-load setproxy RET setproxy- RET

调用python函数

1
M-x setproxy-proxyjpn

对于pymacs的详细教程,中文版可以参考这里