I'm using emacs and I have written a script which uses "current-buffer". However the emacs system doesn't recognise "current-buffer". When I try "M - x current-buffer" i get the response:
no match
: Any idea what I'm doing wrong?
current-buffer is not an interactive function. That is, can't be invoked interactively via M-x as you've tried to do. You can execute non-interactive lisp-code directly by using eval-expression as follows:
M-: (current-buffer) RET
Notice that you have to enter a proper lisp expression. If you want to capture the value in a variable, something like this
M-: (setq xyzzy (current-buffer)) RET
will store the current buffer into the variable xyzzy.
Do I interpret you correct that you have created a function named current-buffer that you want to be available with M-x current-buffer?
To enable functions to be called by M-x function-name the function needs to be marked as interactive.
A sample from the emacs manual:
(defun multiply-by-seven (number) ; Interactive version.
"Multiply NUMBER by seven."
(interactive "p")
(message "The result is %d" (* 7 number)))
The (interactive "p") part makes the function callable from the minibuffer (through M-x).
I sounds like you would (also) like to know how to get the name of the current buffer interactively. Use M-: (buffer-name).
Related
I've defined a major mode for my language.
I'm trying to redefine C-x C-e so that when I'm in my major mode, it'd evaluate the expression using "my" custom interpreter.
Suppose my interpreter is just a command-line program that could be invoked like this:
$my-interpreter <some expression>
I imagine, all I need to do is to do a system call, passing the expression "before point" as argument and print the return value in the echo area.
How hard could it be, right?
Problem: I have no idea where to start!
Any hint?
Thanks.
You can take a look at shell-command and its relatives, along with thing-at-point. Here's a really simple example that uses an "interpreter" (just the shell echo command) to echo the word at point:
(defun my-interpreter ()
(interactive)
(let ((arg (thing-at-point 'word)))
(when arg
(shell-command (concat "echo " arg)))))
(Edit in response to comment.)
If you have defined a keymap for your major mode, you can bind C-x C-e in you major mode with a call to define-key. Otherwise, you can just bind it locally with (local-set-key (kbd "C-x C-e") 'my-interpreter).
It occurred to me that you might be interested in building in interactive functionality like a REPL. To do so, you might consider comint-mode; see the EmacsWiki and this post from Mastering Emacs to get inspired.
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?
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.
As I known, "C-u M-x shell" can be used to run multiple shells.
But how to define a function to do the same thing as "C-u M-x shell" do ?
(defun my-named-shell ()
"Equivalent to C-u M-x shell RET"
(interactive)
(shell (get-buffer
(read-buffer
"Shell buffer: "
(generate-new-buffer-name "*shell*")))))
I used describe-function and find-function to examine the behaviour of shell, and its interactive declaration in particular, and then I just copied the necessary code to turn that into an argument for a non-interactive call to the shell function (but wrapping it in get-buffer so as to provide a buffer argument).
I've actually left out some code which dealt with remote files, because the comments in that code seemed a bit confused. If you weren't in the habit of using C-u M-x shell in buffers accessing remote files via Tramp, that omission won't affect you.
That all said, an even simpler (and more complete) approach is simply:
(defun my-named-shell ()
"Equivalent to C-u M-x shell RET"
(interactive)
(let ((current-prefix-arg '(4)))
(call-interactively 'shell)))
For more information, refer to https://stackoverflow.com/a/9388058/324105
In this instance current-prefix-arg could be any non-nil value, but I think it's a good habit to use a value that C-u actually generates.
As an Emacs beginner, I am working on writing a minor mode. My current (naive) method of programming elisp consists of making a change, closing out Emacs, restarting Emacs, and observing the change. How can I streamline this process? Is there a command to refresh everything?
You might try using M-C-x (eval-defun), which will re-evaluate the top-level form around point. Unlike M-x eval-buffer or C-x C-e (exal-last-sexp), this will reset variables declared with defvar and defcustom to their initial values, which might be what's tripping you up.
Also try out C-u C-M-x which evaluates the definition at point and sets a breakpoint there, so you get dropped into the debugger when you hit that function.
M-x ielm is also very useful as a more feature-rich Lisp REPL when developing Emacs code.
M-x eval-buffer should do it.
What Sean said. In addition, I have (eval-defun) bound to a key, along with a test. The development loop then becomes: 1) edit function, 2) press eval-and-test key, 3) observe results, 4) repeat. This is extremely fast.
During development I write a test, bind it to jmc-test, then use the above key to run it on my just-edited function. I edit more, then press key again, testing it again. When the function works, I zap jmc-test, edit another function, and write another jmc-test function. They're nearly always one line of code, so easy to just bang out.
(defun jmc-eval-and-test ()
(interactive)
(eval-defun nil)
(jmc-test))
(define-key emacs-lisp-mode-map (kbd "<kp-enter>") 'jmc-eval-and-test)
(when t
(defun myfunc (beer yum)
(+ beer yum))
(defun jmc-test () (message "out: %s" (myfunc 1 2))))
When editing "myfunc", if I hit keypad enter, it prints "out: 3".
It all depends on what you're writing and how you've written it. Toggling the mode should get you the new behavior. If you're using [define-minor-mode][1], you can add code in the body of the macro that keys off the mode variable:
(define-minor-mode my-minor-mode
"doc string"
nil
""
nil
(if my-minor-mode
(progn
;; do something when minor mode is on
)
;; do something when minor mode is off
)
But, another way to check it quickly would be to spawn a new Emacs from your existing one:
M-x shell-command emacs&
I just define a function called ldf (short for load-file) in my .emacs file,
like this:
(defun ldf (arg) (interactive "P") (load-file (buffer-file-name)))
As you can see, this little function looks up the filename of the current buffer and then loads the file. Whenever I need to reload the current buffer elisp file, just type "M-x ldf"
Let's say I have the following line in .emacs file.
(setq-default default-directory "~/Desktop/mag")
How can I check the value for `default-directory' in elisp?
Added
I asked this question as I need to check the value of default-directory based on this question.
The elisp code should change the default directory when I click C-x C-f, but I still get ~/, not ~/Desktop/mag. So, I need to check what value the default-directory has.
If you're at the console you can type C-h v, which will prompt you for a variable name. Type in default-directory (or any other name) and you'll get a buffer with some info about that variable, including its value.
The elisp function you're running is describe-variable:
(describe-variable VARIABLE)
I figured this out by C-h k C-h v. C-h k shows you what function the next key or key sequence would call.
If you just want to check the value, you can run the following from the *scratch* buffer:
(print default-directory) <ctrl-j>
The *scratch* buffer allows you to evaluate lisp on the fly. You must hit ctrl-j after to evaluate.
As previously stated, C-h v is the easiest way to find out a variables value. To make it even better, place your cursor on the variable you want to know about, and then run C-h v, and it will default to the word under the cursor. Really handy.
Try:
(print default-directory)
write the above code in one line inside of emacs, got to the end of the line and hit C-x C-e
If you just want to see the variable value in the echo
area (less of a mess), try:
(defun describe-variable-short (var)
(interactive "vVariable: ")
(message (format "%s: %s" (symbol-name var) (symbol-value var))) )
(global-set-key "\C-hV" 'describe-variable-short)