<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")))