Xahlee 写的非常好, 一直都在学, 这段时间, 现在也可以 顺利 浏览了, 前阵子 都还要 「酸酸乳」
-
Elisp: Get Buffer String
在前面章节已经进行了学习。略。
-
Elips: Functions on Line elisp 下关于 Line 的操作
-
Get Position of Beginning/End of Line 获取 Line 开头/末尾的位置
测试: minibuffer 里返回了一个位置数值
(line-beginning-position) ;; (line-end-position)
-
Move Cursor to Beginning/End of Line 将光标移动到 Line 的开头/末尾的位置
测试: 光标移动到了行首
(beginning-of-line) ;李杀说该命令优于 (goto-char (line-beginning-position))
测试: 返回 nil,因为光标当时处于行末,光标没有发生移动
(end-of-line) ;;李杀说该命令优于 (goto-char (line-end-position))
李杀提到,这两个命令是基于 C 来写的,所以运行效率比其它基于 elisp 来写的要快。
They work by ewline char. That is, not soft-wrapped line. 这句话还需要理解,因为我不太知道什么叫 newline char
-
Move Cursor to Previous/Next Line 将光标移动到上一行/下一行
(forward-line -1) ;光标移动到上一行,光标出现在行首 (forward-line 1) ;光标移动到下一行,光标出现在行首
-
Get Current Line as String 提出当前行的所有内容到 String
测试: 李杀直接编写了一个名为 myLine 的命令
(setq myLine (buffer-substring-no-properties (line-beginning-position) (line-end-position) ))
-
Get All Lines in a Flie into a List
李杀提供了 2 个命令模板
(defun get-string-from-file (filePath) "Return file content as string" (with-temp-buffer (insert-file-contents filePath) (buffer-string))) (defun read-lines (filePath) "Return a list of lines of a file at filePath" (with-temp-buffer (instert-file-contents filePath) (split-string (buffer-string) "\n" t)))
-
不错,已经收录。继续
mark一下
Elisp:Cut Copy Paste to/from kill-ring kill-ring 与剪贴、复制、粘贴的操作
Copy Region to Kill Ring 复制 Region 到 Kill Ring
关于 Region 的内容,可以看之前 XahLee Elisp/Writing Command 写命令/Elisp: Region, Active Region 测试:返回 nil,因为我在当前 buffer,没有 mark
(copy-region-as-kill 3 22) ;将两个标记的位置里的内容复制到 kill ring
Kill Region to Kill Ring 将 region 里的内容 kill 之后,放进 kill ring
测试:返回 nil,原因还是因为我在当前 buffer,没有 mark
(kill-region 247 528)
String to Kill Ring 把 String 里的内容放进 kill ring
如果你已经有一段 string,可以用命令 kill-new
。
测试:
kill-new
只能跟着命令的第一段 string 放进 kill ring
(kill-new "cute cat")
;; =C-y= 结果只出现了 cute
Append String to Kill Ring
Append 在英文是「附加」的意思,不太理解这个 Append String 的概念。
测试:
C-y
得到结果 cute catcute cat。
也就是说,Append String 意思是复制多一个内容到 Kill Ring,但形式就好像两条内容「黏在一起」一样,不留空格
(kill-append "cute cat" nil)
Paste from Kill Ring 从 Kill Ring 里粘贴
(yank)
Mark a Region 标记一块 Region
第一步:标记处一块 Region,用 (push-mark POSITION)
,这样从光标的位置,到 POSTION 的位置都会标记为新的 Region
第二步:令 Region 处于 Active 状态,用 (setq mark-active t)
在此处键入或粘贴代码
一个建议:可以尝试通过写一些实用、有趣的小函数把这些知识点串联起来,这样学起来更生动一点。当然,这需要投入更多的时间和精力。
顶。
我也有这么想,但不知道怎么下手,不知道有什么题目可以让我去编写一些小函数
Elisp:Get User Input 获取用户输入的内容
这一章节我不太理解,为啥这些命令,是无法直接执行的?
Ask for File Name
read-file-name
测试:李杀直接给了一个命令模板,但我还是想先单独测试一下这个命令
(read-file-name) ;直接执行该命令报错
下面是李杀的模板:
(defun ff ()
"prompt user toenter a file name, with completion and histroy support."
(interactive)
(message "String is %s" (read-file-name "enter file name:")))
从 message 返回文件的名字。 我不太理解这个命令是怎么用的。
Ask for Directory
read-directory-name
(defun ff ()
"prompt user to enter a dir path, with path completion and input history support."
(interactive)
(message "Path is %s" (read-directory-name "Directory:"))
Ask for String
read-string
询问用户输入一段 string
(defun ff ()
"Prompt user to enter a string, with input history support."
(interactive)
(message "String is %s" (read-string "Enter your name:"))
Ask for Regex String
read-regexp
询问用户输入一段正则表达式
(defun ff ()
"Prompt user to enter a elisp regex, with input history support."
(interactive)
(message "Regex is %s" (read-regexp "Type a regex:")))
Ask for Number
read-number
让用户输入一段数字
测试:
如果输入了非数字,会在 minibuffer 上显示【plese insert number】
(defun ff ()
"Prompt user to enter a number, with input history support."
(interactive)
(let (n)
(setq n (read-number "Type a number: " 10))
(message "Number is %s" n)))
Select from a List
ido-completing-read
测试:
C-x C-e
执行之后, M-x
输入 my-pick-one 命令,可以进入一个交互式对话,让你选择输入 cat dog dragon tiger
(require 'ido)
(defun my-pick-one ()
"Prompt user to pick a choice from a list."
(interactive)
(let ((choices '("cat" "dog" "dragon" "tiger")))
(message "%s" (ido-completing-read "Open bookmark:" choices ))))
Query User fo Yes/No
y-or-n-p
询问用户输入 yes 或没问题。如果返回值为 t
,意味着用户输入了 y
,返回 nil
, 因为这用户输入了 n
(if (y-or-n-p "Do it?")
(progn
;; code to do something here
)
(progn
;; code if user answered no.
))
唔,看完好奇了,我简单查了一下,read-file-name
算是一个“前置命令”,供各种交互式文件操作命令使用,每次C-x C-f打开文件的时候都是用它。
对了人家是有一个必要参数PROMPT的啊喂,怎么能直接执行
哦,我就是没看懂 公式 例子里 PROMPT 的意思是啥…
是提示语啦,在光标前面的文字~
get(字数补丁
先问个小白问题:编程里的 argument 是什么意思?
Elisp: Interactive Form 将参数变为命令
EmacsWiki 里解释比较简练:
A Lisp function becomes a command.
interactive
这个命令的作用,就是将某种参数(函数、功能)变为 M-x
下可执行的命令
李杀提到, interactive
这个命令要跟在 doc string 后面,doc string 就是字符类型的 string。
(interactive code_letter_and_promp_string)
Get String as Argument
(interactiv "sPrompt_sting")
(defun ask-name (x)
"Ask name."
(interactive "sEnter your name: ")
(message "Name: %s" x))
测试: 在这里用了 %s 来取值。 interactive 后面跟着的第一个字符,就代表着用户应当输入哪种类型的数据 我把 s 改成 n,就显示必须让我输入数值
Get Number as Argument
(interactive "nPrompt_string")
(defun ask-age (x)
"Ask age."
(interactive "nEnter your age: ")
(message "Name: %d" x))
测试:
用 C-x
执行之后,命令里已有 ask-age, M-x
输入该命令,则在 minibuffer 出现对话框
Get Region Begin End Positions as Argument
(interactive "r")
(defun print-region-boundary (x y)
"Print region strat and end postions"
(interactive "r")
(message “Region begin at: %d, and at: %d" x y))
Passing Interactive a List
(interactive list_expression)
(defun do-something (x y)
"Ask name and age"
(interactive
;; complex code here that returns a list
(list "Mary" 22))
(message "Name is: %s, Age is: %d" x y))
在 minibuffer 里直接显示 Namer is Mary, Age is 22
Ways to Call Interactive
(interactive)
简单地讲某一 function 转变为命令,不包含 argument 的处理
(interactive string)
(interactive list)
参数。自变量。
给楼主推荐一本书 chrome-extension://cdonnmffkdaoajfknoeeecmchibpmkmg/assets/pdf/web/viewer.html?file=http%3A%2F%2Faandds.com%2Fattachments%2Felisp_tutor.pdf
谢谢推荐,可以直接告诉我名字吗?链接应该是给错了
用 chrome 打开就好了《Elisp入门》叶文彬写的
谢谢推荐,也谢楼上直接发了文件