A basic function for emacs

☆樱花仙子☆ 提交于 2019-12-11 12:47:23

问题


I have never written an emacs function before and was wondering if anyone could help me get started. I would like to have a function that takes a highlighted region parses it (by ",") then evaluates each chunk with another function already built into emacs.

The highlighted code may look something like this: x <- function(w=NULL,y=1,z=20){} (r code), and I would like to scrape out w=NULL, y=1, and z=20 then pass each one a function already included with emacs. Any suggestions on how to get started?


回答1:


A lisp function is defined using defun (you really should read the elisp intro, it will save you a lot of time - "a pint of sweat saves a gallon of blood").

To turn a mere function into an interactive command (which can be called using M-x or bound to a key), you use interactive.

To pass the region (selection) to the function, you use the "r" code:

(defun my-command (beg end)
  "Operate on each word in the region."
  (interactive "r")
  (mapc #'the-emacs-function-you-want-to-call-on-each-arg
        ;; split the string on any sequence of spaces and commas
        (split-string (buffer-substring-no-properties beg end) "[ ,]+")))

Now, copy the form above to the *scratch* emacs buffer, place the point (cursor) on a function, say, mapc or split-string, then hit C-h f RET and you will see a *Help* buffer explaining what the function does.

You can evaluate the function definition by hitting C-M-x while the point is on it (don't forget to replace the-emacs-function-you-want-to-call-on-each-arg with something meaningful), and then test is by selecting w=NULL,y=1,z=20 and hitting M-x my-command RET.

Incidentally, C-h f my-command RET will now show Operate on each word in the region in the *Help* buffer.



来源:https://stackoverflow.com/questions/17076646/a-basic-function-for-emacs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!