GNU emacs Lisp小结3

发布时间:2019-08-10 08:23:50编辑:auto阅读(1617)

    chapter4 与缓冲区有关的函数
    4.1 查找更多的信息
    C-h f 函数名   ;查询函数
    C-h v 变量名   ;查询变量
    find-tags 函数 ;跳到响应函数
    M-. 函数名     ;同上
    上面函数需要定义一个标记表(tags table),这是一个名为"TAGS"的文件。
    可以使用M-x visit-tages-table来指定
    C-h p 命令让你用主题关键字搜索Emacs Lisp标准库。

    4.2 简化的beginning-of-buffer函数定义
    beginning-of-buffer => M-<
    end-of-buffer => M->

    (defun simple-beginning-of-buffer ()
    "Move point to the beginning of the buffer;
    leave mark at previous position."
    (interactive)
    (push-mark)
    (goto-char (point-min)))
    你可以使用C-h f fun来查询具体函数。
    C-x C-x可以回到原来位置。
    end-of-buffer 只需要把point-min换成point-max.

    4.3 mark-whole-buffer函数
    快捷键:C-x h

    (defun mark-whole-buffer ()
    "Put point at beginning and mark at end of buffer."
    (interactive)
    (push-mark (point))
    (push-mark (point-max))
    (goto-char (point-min)))

    4.4 append-to-buffer函数的定义
    (defun append-to-buffer (buffer start end)
    "Append to specified buffer the next of the region.
    It is insert into that buffer before its point.
    When calling from a program, give three arguments:
    a buffer or the name of one, and two character numbers
    specifying the portion of the current buffer to be copied."
    (interactive "BAppend to buffer:\nr")
    (let ((oldbuf (current-buffer)))        
    (save-excursion
    (set-buffer (get-buffer-create buffer))
    (insert-buffer-substring oldbuf start end))))

    4.5回顾
    1.descibe-function, describe-variable
    C-h f, C-h v
    2.find-tag
    M-.
    3.save-excursion
    保存当前的位点,标记,缓冲区,执行参数,最后返回原状态。
    4.push-mark
    在指定位置设置一个标记,并在标记环中记录原来标记的值。
    5.goto-char
    将位点设置为由参量指定的位置。
    6.insert-buffer-substring
    将一个来自缓冲区的文本域拷贝到当前缓冲区。
    7.mark-whole-buffer
    C-x h
    8.set-buffer
    将Emacs的注意力转移到另一个缓冲区,但是不改变显示的窗口。
    9.get-buffer-create, get-buffer
    寻找一个已指定名字的缓冲区,或当指定名字的缓冲区不存在时就创建它。


    chapter5 更复杂的函数
    5.1 copy-to-buffer函数的定义
    (defun copy-to-buffer (buffer, start, end)
    "...."
    (interactive "BCopy to buffer:\nr")
    (let ((oldbuf (current-buffer)))
     (save-excursion
       (set-buffer (get-buffer-create buffer))
       (erase-buffer)
       (save-excursion
         (insert-buffer-substring oldbuf start end)))))

    5.2 insert-buffer函数的定义
    (defun insert-buffer (buffer)
    "Insert after point the contents of BUFFER.
    Puts mark after the inserted text.
    BUFFER may be a buffer or a buffer name."
     (interactive "*bInsert buffer:")
     (or (bufferp buffer))
       (setq buffer (get-buffer buffer))
     (let (start end newmark)
       (save-excursion
         (save-excursion
           (set-buffer buffer)
           (setq start (point-min) end (point-max)))
         (insert-buffer-substring buffer start end)
         (setq newmark (point)))
       (push-mark newmark)))

    5.2.1 insert-buffer函数中的交互表达式
    1.只读缓冲区
    “*”用于缓冲区是一个只读缓冲区。
    2.交互表达式中的“b”
    传送给insert-buffer函数的参量应是一个存在的缓冲区或者这个缓冲区的名字。
    大写的“B”可以允许参量传送不存在的缓存区。

    5.2.2 insert-buffer 函数体
    or表达式的目地是为了确保buffer参量真正与一个缓冲区绑定在一起,而不是绑定缓冲区的名字。

    5.2.3 用if表达式(而不是or表达式)编写的insert-buffer函数
    (if (not (bufferp buffer))                ;if-part
       (setq buffer (get-buffer buffer)))    ;then-part

    5.2.4 函数体中的or表达式
    一个or函数可以有很多参量。它逐一对每一个参量并返回第一个其值不是nil的参量的值。
    一旦遇到其值不是nil的参量之后,or表达式就不再对后续的参量的求值。

    5.2.5 insert-buffer 函数中的let表达式

    5.3 biginning-of-buffer函数的完整定义
    C-u 7 M-<  将光标移动到从缓冲区开始的这个缓冲区的70%处,如果大于10,则移到末尾。

    (defun beginning-of-buffer (&optional arg)
     "Move point to the beginning of the buffer;
     leave mark at previous position.
     With arg N, put point N/10 of the way
     from the true beginning.
     Don't use this in Lisp programs!
     \(goto-char (point-min)) is faster
     and does not set the mark."
     (interactive "P")
     (push-mark)
     (goto-char
       (if arg
         (if  (> (buffer-size) 10000)
            ;;Avoid overflow for large buffer sizes!
            (* (prefix-numeric-value arg) (/ (buffer-size) 10))
            (/ (+ 10 (* (buffer-size) (prefix-numeric-value arg))) 10))
         (point-min)))
     (if arg (forward-line 1)))

    5.4 回顾
    1.or
    逐一对每一个参量求值,直到返回第一个非空值。
    2.and
    逐一对每一个参量求值,直到有一个参量的值是nil
    3.&optional
    在函数定义中用于指出一个参量是可选参量。
    4.prefix-numeric-value
    将一个由(interactive "P")产生的未加工的前缀参量转换成一个数值。
    5.forward-line
    将光标移到下一行的行首,如果参数大于1,则移动多行。
    6.erase-buffer
    删除当前缓冲区的全部内容
    7.bufferp
    如果其参量是一个缓冲区则返回“真”,否则返回“假”。

    chapter 6 变窄和曾宽
    6.1 save-restriction特殊表
    跟踪变窄开启的部分。
    (save-restriction
    body....)

    (save-excursion
    (save-restriction
    body...))
    如果需要同时使用,顺序不能错。

    6.2 what-line函数
    这个函数告诉你光标所在的行数。
    (defun what-line ()
     "Print the current line number (in the buffer) of point."
     (interactive)
     (save-restriction
       (widen)
       (save-recursion
         (beginning-of-line)
         (message "Line %d" (1+(count-lines 1 (point)))))))


    增补:
    C-x n n
       Narrow down to between point and mark (narrow-to-region).
    C-x n w
       Widen to make the entire buffer accessible again (widen).
    C-x n p
       Narrow down to the current page (narrow-to-page).
    C-x n d
       Narrow down to the current defun (narrow-to-defun).



关键字

上一篇: ntfs-3g 集成

下一篇: 3:elasticsearch服务编写