边读 Emacs Lisp Intro 边做题(一)

本文介绍了如何在EmacsLisp中实践练习题,包括编写带有可选参数的测试函数、显示缓冲区前60字符、搜索字符串和处理kill环等交互式功能。
摘要由CSDN通过智能技术生成

<2022-08-18 周四>

边读Emacs Lisp Intro边做题(一)

打开emacs,按C-h i打开Info页,找到Emacs Lisp Intro。我做了几个练习题,觉得应该把它记录在笔记中,如下:

在这里插入图片描述

说明,对照上图,以后函数的名称以章节编号,比如下面的函数名为exercise-5.5,函数的注释为题目的英文描述,如下:

(defun exercise-5.5 (&optional arg)
  "Write an interactive function with an optional argument that tests
whether its argument, a number, is greater than or equal to, or else,
less than the value of ‘fill-column’, and tells you which, in a message.
However, if you do not pass an argument to the function, use 56 as a
default value."
  (interactive "p")
  (or (numberp arg)
      (setq arg '56))
  (if (>= arg fill-column)
      (message "%d >= `fill-column'" arg)
    (message "%d < `fill-column'" arg)))
(defun exercise-6.3 ()
  "Write a function that will display the first 60 characters of the
current buffer, even if you have narrowed the buffer to its latter half
so that the first line is inaccessible.  Restore point, mark, and
narrowing.  For this exercise, you need to use a whole potpourri of
functions, including ‘save-restriction’, ‘widen’, ‘goto-char’,
‘point-min’, ‘message’, and ‘buffer-substring’.

   (‘buffer-substring’ is a previously unmentioned function you will
have to investigate yourself; or perhaps you will have to use
‘buffer-substring-no-properties’ or ‘filter-buffer-substring’ ..., yet
other functions.  Text properties are a feature otherwise not discussed
here.  *Note Text Properties: (elisp)Text Properties.)

   Additionally, do you really need ‘goto-char’ or ‘point-min’?  Or can
you write the function without them?"
  (interactive)
  (save-restriction
    (widen)
    (save-excursion
      (goto-char (point-min))
      (let ((buff (buffer-substring 1 60)))
        (if (stringp buff)
            (message "%s" buff))))))
(defun exercise-8.7-1 (str-to-find)
  "Write an interactive function that searches for a string.  If the
search finds the string, leave point after it and display a message
that says “Found!”.  (Do not use ‘search-forward’ for the name of
this function; if you do, you will overwrite the existing version
of ‘search-forward’ that comes with Emacs.  Use a name such as
‘test-search’ instead.)"
  (interactive "sstring to find: ")
  (if (stringp str-to-find)
      (progn
        (goto-char (search-forward str-to-find))
        (message "Found!"))))
(defun exercise-8.7-2 ()
  "Write a function that prints the third element of the kill ring in
the echo area, if any; if the kill ring does not contain a third
element, print an appropriate message."
  (interactive)
  (if (setq res (car (nthcdr 2 kill-ring)))
      (message res)
    (message "`kill-ring' length < 3")))
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值