博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用lisp函数控制cursor
阅读量:5990 次
发布时间:2019-06-20

本文共 5972 字,大约阅读时间需要 19 分钟。

hot3.png

Getting Cursor Position

;; current cursor position (point) ;; returns the position of the beginning/end of region (region-beginning)(region-end) ;; returns the position for the end of buffer ;; (taking account of narrow-to-region) (point-max) ;; beginning of buffer has point of 1. ;; Use (point-min) to get beginning position of buffer when narrow-to-region is in effect (point-min)

Moving Cursor and Searching

;; move cursor to position 392 (goto-char 392) ;; move cursor by n chars (forward-char n)(backward-char n) ;; move cursor to the first char that's not a newline or tab ;; Returns the distance traveled (skip-chars-forward "\n\t")(skip-chars-backward "\n\t") ;; move cursor to the location of myStr ;; returns the new position (search-forward myStr)(search-backward myStr) ;; move cursor to the location matched by a regex ;; returns the new position (search-forward-regex myRegex)(search-backward-regex myRegex) ;; move to beginning/end of line (beginning-of-line)(end-of-line)

Text Editing

The following functions all have the Current Buffer as target.

Inserting Text

;; insert string at current cursor position (insert "i ♥ u") ;; insert part of a buffer (insert-buffer-substring-no-properties myBuffer myStartPos myEndPos) ;; insert a file's content (insert-file-contents myPath)

Deleting Text

;; delete 9 chars starting at current cursor pos (delete-char 9) ;; deleting text (delete-region myStartPos myEndPos) ;; delete whole buffer content (erase-buffer)

Find/Replace Text in Buffer

A typical idiom is to use search-forward or “search-forward-regex”, then use replace-match. Use while if you want to do more than one replacement. Example:

;; idiom for string replacement in whole buffer  (let ((case-fold-search t)) ; or nil  (goto-char (point-min))  (while (search-forward "myStr" nil t)    (replace-match "myReplaceStr" t t))) ;; for regex, use search-forward-regex

To control the letter case of search, set “case-fold-search” to t or nil with let, as in the above. To control letter case of the replacement, use the optional arguments in your replace-match function.

;; the second captured string (match-string 2) ;; get the positions of the 2nd captured string (match-beginning 2) (match-end 2)

Grabbing Text

Functions that grab text in a buffer into a string.

;; get the string from buffer (setq myStr (buffer-substring myStartPos myEndPos))(setq myStr (buffer-substring-no-properties myStartPos myEndPos))
;; grab a thing at point. The “thing” is a semantic unit. It can be a ; word, symbol, line, sentence, filename, URL and others.  ;; grab the current word (setq myStr (thing-at-point 'word)) ;; grab the current word with hyphens or underscore (setq myStr (thing-at-point 'symbol)) ;; grab the current line (setq myStr (thing-at-point 'line)) ;; grab the start and end positions of a word (setq myBoundaries (bounds-of-thing-at-point 'word))

For more about thing-at-point, see: .

Strings

Functions that takes a string argument.

;; length (length "abc") ; returns 3  ;; Extract a substring (substring myStr myStartPos myEndPos) ;; join strings (concat "some" "thing") ;; check if a string matches a pattern (string-match myRegex myStr) ;; get captured match ;; second argument is optional, but required if the last match is done by “string-match” (match-string 1 myStr) ;; change a given string using regex. Returns changed string. (replace-regexp-in-string myRegex myReplacement myStr) ;; split a string into parts, returns a list (split-string "ry_007_cardioid" "_")(string-to-number "3") ; change datatype (number-to-string 3) ; convert to string (format "%d" 3) ; similar to number-to-string but with fine control

Emacs has only very few functions that takes a string as argument. Any non-trivial string processing is done with a buffer. Use with-temp-buffer, then insert your string, process it, then use buffer-string to get the whole buffer content.

Preserving point, mark, buffer, narrow-to-region

When your command moves cursor to do some work, you usually want to preserve the cursor position before the command starts, so that when user calls your code, the cursor won't end up somewhere unexpected.

;; preserve {point, mark, current buffer} (save-excursion ;; lisp code here involving moving cursor, mark, changing buffer. ) ;; preserve user's narrow-to-region ;; useful when you want to narrow-to-region in your code to work in just that region (save-restriction (narrow-to-region START END) ;; lisp code here )

Buffers

Functions that acts on the buffer data type. Most buffer function assume the current buffer if no argument is given. Some requires a argument. The argument can usually be a buffer's name, or a buffer object.

;; return the name of current buffer (buffer-name) ;; return the full path of the file (buffer-file-name) ;; switch to a given buffer (set-buffer myBuffer) ;; close a given buffer (kill-buffer myBuffer) ;; use a temp buffer to manipulate string (with-temp-buffer (insert myStr) ;; manipulate the string here  (buffer-string) ; get result )

Files

Open, Append, Write

;; open a file (returns a buffer) (find-file myPath) ;; save current buffer (write to the associated file) (save-buffer) ;; same as “Save As”. Close current buffer and open the new saved (write-file myPath) ;; append a text block to file (append-to-file myStartPos myEndPos myPath) ;; close a buffer (kill-buffer myBuffName)

Rename, Copy, Delete

(rename-file fileName newName)(copy-file oldName newName)(delete-file fileName)(copy-directory dirPath newDirPath) ;; delete a whole dir. (new in emacs 23) (delete-directory dirPath t)

File Name Manipulation

;; get dir path (file-name-directory myFullPath) ;; get filename part (file-name-nondirectory myFullPath) ;; get filename's suffix (file-name-extension myFileName) ;; get filename sans suffix (file-name-sans-extension myFileName) ;; get relative path (file-relative-name myFilePath dirPath) ;; get full path (expand-file-name myFilePath)

转载于:https://my.oschina.net/u/575122/blog/165057

你可能感兴趣的文章
白硕:区块链+开源数据库≥商业数据库
查看>>
springboot项目错误页面的设置
查看>>
PostgreSQL 10.1 手册_部分 III. 服务器管理_第 19 章 服务器配置_19.5. 预写式日志
查看>>
11月23日云栖精选夜读 | Java开发者福音 阿里巴巴宣布连任Java全球管理组织席位...
查看>>
PHP获取IP地址的方法,防止伪造IP地址注入攻击
查看>>
PostgreSQL 10.1 手册_部分 III. 服务器管理_第 18 章 服务器设置和操作_18.5. 关闭服务器...
查看>>
IntelliJ 创建Spring Boot项目
查看>>
redis数据结构实现(四)
查看>>
cordova 打包工具
查看>>
Android DialogFragment使用
查看>>
Android-来填写一个验证码吧!(一)
查看>>
Android 网络请求原理以及原始数据包
查看>>
百度echarts的项目实际应用
查看>>
自定义View -简单的 SwitchView
查看>>
iOS11适配UIToolbar无法点击问题
查看>>
Python 3.8.0a2 发布,面向对象编程语言
查看>>
Java 编程
查看>>
Hadoop单词统计
查看>>
读《白金数据》
查看>>
基于qml创建最简单的图像处理程序(3)-使用opencv&qml进行图像处理
查看>>