首页 > Elisp, 中级 > 针对Emacs中文本编辑的编程简介

针对Emacs中文本编辑的编程简介

2010年3月6日 ahei 发表评论 阅读评论

作者: laihj

Elisp是Emacs下的Lisp方言,而Emacs是一款编辑器。那么针对于Emacs,Lisp要做的很重要一部分工作当然就是对编辑的自动化支持。比如移动鼠标,输入句子,查找替换,代码高亮等等,简单地说,就是为更好更方便地支持文本编辑提供支持。而把这些函数都重合起来,就起成了Emacs mode。如我们常用的C++ mode,python mode。还有之前我一直在介绍的org-mode等,都是由Elisp拼成的。

学习Elisp,可以满足对Emacs进行定制的需求,你可以把想要的功能写在.el文件里面,让Emacs来调用。一方面可以实现一些简单的轻量级的功能,而不需要为此寻找和安装一个完整的mode。另一方面,可以修改现有的mode,使得更符合自己的习惯。这符合开源的作法,不爽即改。

Elisp函数的简单例子

光标位置

1
2
3
4
5
6
7
8
9
;; 返回当前光标的位置
(point)
;; region的头跟尾
(region-beginning)
(region-end)
;; 最大光标位置(即文件尾),elisp还提供最小光标位置(point-min),不过我觉得那应该都是1吧。
(point-max)
;; 返回buffer结尾的绝对位置,无视narrow-to-region
(buffer-end 1)

移动光标和搜索

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
;移动光标到392
(goto-char 392)
 
; 向前/向后移动n字符
(forward-char n)
(backward-char n)
 
; 跳过所有\n\t,即把光标移动第一个不是换行或制表符那里
; 返回移动的字符数
(skip-chars-forward "\n\t")
(skip-chars-backward "\n\t")
 
; 移动光标到myStr后面,向前和向后
; 返回新的光标位置
(search-forward myStr)
(search-backward myStr)
 
; 同上,但是参数是正则表达式,myRegex
; 返回新的光标位置
(re-search-forward myRegex)
(re-search-backward myRegex)

文本编辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
; 删除光标后的九个字符
(delete-char 9)
 
; 删除选中的两点之前的文本
(delete-region mystartpos myendpos)
 
; 在当前光标位置插入一字符串
(insert "Forza Inter")
 
; 从buffer中获得一个字符串并赋给mystr
(setq Mystr (buffer-substring mystartpos myendpos))
 
; 改变字符大小写
;这个例子是指当前光标之前的20个字符
(rapitalize-region (- (point) 20) (point))

字符串

1
2
3
4
5
6
7
8
; 长度
(length "abc") ; returns 3
 
; 获取一个子串
(substring myStr startIndex endIndex)
 
; 替换,以正则方式
(replace-regexp-in-string myRegex myReplacement myStr)

Buffers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
; 当前buffer的名字
(buffer-name)
 
; 文件名(全路径)
(buffer-file-name)
 
; 设定一个buffer名
(set-buffer myBufferName)
 
; 保存
(save-buffer)
 
; 关闭指定的buffer
(kill-buffer myBuffName)
 
; 关闭当前buffer
(kill-this-buffer)
 
; 临时指定一个buffer作为当前buffer
(with-current-buffer myBuffer
  ;; do something here ...
)

Files

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
; 打开一个文件
(find-file myPath)
 
; 另存
; 会关闭当前的buffer,打开另存好的文件
(write-file myPath)
 
; 把一个文件内容插入到当前位置 
(insert-file-contents myPath)
 
; 将选中的评论文本加到某个文件后面
(append-to-file myStartPos myEndPos myPath)
 
; 重命名
(rename-file fileName newName)
 
; 复制
(copy-file oldName newName)
 
; 删除
(delete-file fileName)
 
; 获取路径
(file-name-directory myFullPath)
 
; 获取文件名(不含路径)
(file-name-nondirectory myFullPath)
 
; 得到文件名后缀
(file-name-extension myFileName)
 
; 获得不含后缀的文件名。
(file-name-sans-extension "abc.htm")

简单的例子

1
2
3
4
5
(defun insert-p-tag ()
  "Insert <p></p> at cursor point."
  (interactive)
  (insert "<p></p>")
  (backward-char 4))

在当前光标处插入一个p tag。做法是插放一个串,然后将光标移回4位。

编写一个mode

理论上来讲,知道上面这些东西,你就有能力编写一个mode了。但是,编写mode毕竟是一个复杂的工作,需要编写者对elisp编程具有”hello world”以上很多级的熟练度。

在李杀网,作者有一个系列文章来讨论如何为一个编程语言编写mode。

The End. Have fun!

分享家:Addthis中国
GD Star Rating
loading...
针对Emacs中文本编辑的编程简介, 7.7 out of 10 based on 3 ratings 标签:C/C++, cursor, Emacs, laihj, org, python, screenshot, 光标, 配色, 鼠标

相关日志

分类: Elisp, 中级
  1. happybabe
    2010年3月6日14:27 | #1

    嘿,你也来写了!

    [回复]

    ahei 回复:

    @happybabe, 哈哈,原来大家都认识啊

    [回复]

  2. klt
    2011年5月6日22:07 | #2

    文本编辑,嘿,占个位置问个问题哈
    我现在有一个函数,是用来copy当光标前所在位置的单词,现在我想把它用在”此处”,但却没能解决
    Mark Set之后移动光标,会高亮显示经过的字符,此时使用alt+w可以copy高亮部分,以后进行黏贴
    我想如果没有高亮显示的部分,那么alt+w就copy当前所在位置的单词,因为如果没有高亮显示的部分时
    使用alt+w 复制的是之前的mark set等部分,如果光标不在原来的位置,使用此操作会感觉莫名奇妙,特别是使用ctrl+w时(ctrl+w和alt+w类似)
    所以想设置为一个自定义函数,但现在就是判断不好,我是想根据mark等标记对比当前point等等,但是没能实现,有什么好的建议吗,我对elisp了解还太少了 : )

    [回复]

    ahei 回复:

    @klt, …..好绕

    [回复]

  3. yueduz
    2012年5月6日23:01 | #3

    请问如何读取一个文件中的内容保存到字符串

    我想要写的代码如下.就只知道这些方法.望多指教.
    (message (concat
    “curl -d \”title=”
    (concat (file-name-sans-extension b) “&”)
    (concat “content=” (insert-file-1 (concat “cat ~/org/html/” (concat (file-name-sans-extension b) “.html&”)) ))
    “YN=1&”
    (concat “tabs=” tabs “\”")
    http://0.0.0.0:3000/addblog
    ))

    [回复]

  4. 2015年7月18日14:11 | #4

    たびは と同様 私は小さい読み取るために使用される彼らの動機をクリアし、それがまた起こっています今私が読んでいるポストこれで。
    格安おすすめ http://encshows.com/?store9901=25451

    [回复]

  5. 2016年4月7日02:56 | #5

    Bueno Y Sano coach outlet The case drew renewed attention in January when Levinson was not part of a prisoner swap between the U. g michael kors outlet online
    is $10. michael kors outlet “This is December ball,” Freeney said.
    Phelan M. Ebenhack/Associated Press coach bags If you aren’t able to catch the show on Feb. u http://www.louisvuittonhandbagsbuy.co.uk
    The Blackhawks were coming off disappointing losses to Minnesota and Nashville, but the defending Stanley Cup champions got a boost from two of their newest players acquired in a flurry of moves last week. louis vuitton outlet Still, it does seem unfair if colleges conduct these searches somewhat randomly, and not all students applying to the school are subject to the same level of scrutiny.
    j Concordia michael kors outlet online Rules vary from one ruck to the next. michael kors outlet online
    Sales of Ford’s best seller, the F-Series pickup, were up 10 percent. michael kors handbags Kiana Gonsalves, Basha; Kamaile Hiapo, Campo Verde; Julia Turpin, Chandler Prep; Hannah Weber, Chandler Prep; Landry Armstrong, Combs; Emma West, Corona de Sol; Camy Cohn, Desert Ridge; Hailey Dirrigl, Desert Vista; Rachael Kramer, Desert Vista; Sam Boever, Gilbert; Hailey Babcock, Hamilton; Sydney Lustumo, Hamilton; Giuliana Castranova, Horizon Honors; Alexa Smythe? Horizon Honors; Annie Hatch, Mesa; Brianna Hawkins, Mesquite; Nicolette Clark, Mountain Pointe; Ellery Anderson, Mountain View;?Ryann Davis, Perry; Kirsten Companik, Seton Catholic; Halle Johnson, Seton Catholic; Mary Shroll, Tempe Prep; Courtney Dyer, Valley Christian. http://www.louisvuittonhandbagsbuy.us.com
    Fox: XFINITY Series race michael kors outlet online Artists will submit ideas for murals to be painted on 18 Little Rock storm drains. h louis vuitton handbags
    DY, joined Sportstalk to give a heartfelt goodbye to the fans http://www.coachhandbags.net.co Arizona State Sun Devils running back Marion Grice (1) runs against Oregon State during the first half of an NCAA college football game on Saturday, Nov.
    However, for that to happen the district will have to get the state to pony up 100 percent of site development and facilities funding, $24. http://www.louisvuittonbagsbuy.us.com But not today.
    Speaking in London after having escaped her captors, she appealed for help for those who remained trapped and for her Yazidi community, a minority which IS has targeted for massacres, enslavement and rape in northern Iraq. coach outlet The Broncos are in their second Super Bowl in three years; they lost 43-8 to Seattle in 2014 in New Jersey.
    This gives them time to break that bond and dissolve thesolids, permitting them to be easily wiped away. louis vuitton handbags The federal rules require applications to be processed within 45 days.
    u The leaf photo you sent shows a nutrient deficiency, and that could cause the trees to drop leaves. louis vuitton outlet Los Angeles escaped with two overtime wins at home and then took complete command inside Madison Square Garden to take a 3-0 series edge.
    One can climb down to the original spot where the spring emerges and there is a bench down there, but it is really like sitting in a deep hole in the ground. coach outlet online Space Fury at StarFighters Arcade”The journey thus far has been great,” said Mike, “and when we see families and friends of all ages connect while playing some of the classics it makes all the hard work and effort worth it. i louis vuitton handbags
    Among registered Michigan voters, 50 percent approve of the job Barack Obama is doing as president; 42 percent disapprove and 8 percent are unsure. michael kors outlet “I have not gone into (a fight with) someone with that high of a level, so I’m looking at it as going into a challenge,” Kamegai said.
    m Beaverton at Tualatin, KUIK (1360) louis vuitton handbags Tiger Woods was on hand for the opening Wednesday of The Playgrounds at Bluejack National, a course he helped design, in Montgomery, Texas. v
    That should be interesting, especially since the National Highway Traffic Safety Administration recently determined that for regulatory purposes, autonomous vehicle software is a “driver. louis vuitton handbags “It can be tested as reliably as any other doping agent. coach outlet store
    Great Lakes Orvis at 1144 East Paris Ave. SE near Bill and Paul’s Sporthaus closed its doors in 2008. coach factory Goldwater was smart and blunt. z coach outlet
    The defeat ends the Rainbow Wahine’s 16-game home conference winning streak, and is UH’s first loss to the Highlanders in program history coach outlet online Gila Ridge performed well at the Adam Donnenfield Tournament, highlighted by a win over Brophy, to join the rankings at No.
    In 33 starts this season, he posted a 3.66 ERA (up from his career 3.32 ERA) with a 7.3 strikeout per nine innings mark. He threw 201 2/3 innings last year. http://www.michaelkorshandbagsbuy.us.com economist at High Frequency Economics Ltd. x cheap oakleys
    Baltimore’s first-round pick, Central Florida wide receiver Breshad Perriman, is the son of former NFL receiver Brett Perriman coach outlet online It costs $4.

    [回复]

  6. 2016年7月12日08:36 | #6

    What’s up to every single one, it’s in fact a nice for me to pay a quick visit this website, it includes helpful Information. Sbobet http://arenabolabet.com/

    [回复]

  7. 2017年3月5日19:55 | #7

    针对Emacs中文本编辑的编程简介 | Emacs中文网 http://www.seoterbaik.com/2013/11/selamat-datang-di-layanan-seo-indonesia.html

    [回复]

  8. 2017年4月18日21:10 | #8

    Great place to order 59 Wesley Woodyard Jersey by Paypal.
    11 Sebastian Janikowski Jersey

    [回复]

  1. 本文目前尚无任何 trackbacks 和 pingbacks.
:wink: :-| :-x :twisted: :) 8-O :( :roll: :-P :oops: :-o :mrgreen: :lol: :idea: :-D :evil: :cry: 8) :arrow: :-? :?: :!: