Opening definition of Emacs command - emacs

is it possible to open in emacs elisp file with command definition, to see, how it is defined?

Yes you can call M-x find-function
Some functions are implemented in C. To be able to find C function you have to download C sources (if you have not yet done so) and add the following line to your .emacs
(setq find-function-C-source-directory "/path/to/c-source")

Another way:
C-h f foo RET to see the documentation for the function foo.
The documentation will tell you, on the first line, where and how the function is defined. Click the link (or hit RET with cursor on it) to the source file where the command is defined. You have to have the Lisp sources (for Lisp code) or the C sources (for C code) installed on your system in order for this to work.
You can also start from a key, without knowing what its command is: C-h k.

Related

Slime Autocompletion when editing file

I am new to Lisp. I installed LIsp in a box to learn Common Lisp. In the slime-repl I can use autocompletion using the tab key. However I want to do so in other buffers - files - I edit. For instance I want to do the following:
C-x b ;; Here I switch to the buffer corresponding to my file. S I am not in the repl anymore.
(remove-i ;; Here I use the tab key, but nothing occurs.
In the repl buffer, I can type (remove-i and I will see matching functions such as remove-if and remove-if-not.
How can I add lisp autocompletion while editing any other file?
C-M-i (translated from <M-tab>) runs the command slime-complete-symbol (found
in slime-mode-indirect-map), which is an interactive compiled Lisp function in
‘slime.el’.
It is bound to C-c TAB, C-M-i, <menu-bar> <SLIME> <Complete Symbol>.
(slime-complete-symbol)
Complete the symbol at point.
Completion is performed by ‘slime-completion-at-point-functions’.
You can bind this function to TAB, if you want.
Peter

Function Definition - Don't know how to find

I use SlimeNav to read elisp code. It works good mostly, but for inbuilt functions, at times, it does not work.
(local-file (file-relative-name
temp-file
(file-name-directory buffer-file-name))))
In this snippet, when i press Alt + . on local-file function, it says,
Don't know how to find 'local-file'
local-file is not a function anywhere in the core. It's used as a let-bound variable 7 times though.
Maybe you confused it.
Also remember that when accessing the documentation on a function (eg with C-h f), you should see a link you can click to get to the source code. Works also with the C sources if they are available in a way Emacs can find.
As an example, the documentation for find-file looks like this:
find-file is an interactive compiled Lisp function in `files.el'.
It is bound to <open>, C-x C-f, <menu-bar> <file> <new-file>.
(find-file FILENAME &optional WILDCARDS)
Edit file FILENAME.
...
You should be able to see that `files.el' comes out in a different colour, this you can click and it will bring you to the definition of that function.

Emacs - how to see/how to debug a single elisp function/emacs command

There is one thing that I don't like on table function in Org-mode for emacs. I would like to see all the functions that get executed by function that I run as Emacs command.
What is the best way to do that? Any tips how to get started with debuging elisp code, especially single command of interest?
C-hf function name to find the source code for the function.
C-uC-M-x to instrument the function for Edebug.
Whenever the function is called, Emacs will drop into Edebug which makes it easy to execute the function step by step, inspect variables, and do other typical debugging tasks. See (info "(Elisp)Edebug") for more information.
I prefer the traditional Emacs debugger to edebug. To use it:
M-x debug-on-entry the-function RET
Then, whenever the-function is invoked, the debugger is entered. Use d to step through the evaluation, and c if you want to skip through a step (not dive into its details.
It helps to view the definition of the-function in another window/frame while you step through it.
You can cancel debug-on-entry using M-x cancel-debug-on-entry.
C-h f to go to function help mode, then type the name of the function. If it is an elisp function, you can then view the source and look for yourself what functions it calls.
If you want a programmatic way to see the source of a function (akin to Clojure's source macro) you can use the symbol-function subroutine.
For instance, there is a defun do-math in my .emacs file. To see its source, I can do the following
(symbol-function 'do-math)
and it gives me
ELISP> (symbol-function 'do-math)
(lambda
(expression)
(interactive "sexpression:")
(insert
(number-to-string
(eval
(read expression)))))
See also :
https://www.gnu.org/software/emacs/manual/html_node/elisp/Function-Indirection.html
See also also :
http://ergoemacs.org/emacs/elisp_symbol.html

How to save all functions I entered in LispBox/Slime?

Situation: I entered several functions while working with REPL in Emacs.
Problem: There is junk like "; Evaluation aborted" when I'm simply saving buffer.
What I want: clear descriptions of all the functions I entered in their latest revision.
Can I do that? Thanks.
I don't get it. Are you entering definitions at the REPL and expecting to recover them later? Just save a source file as you would in any other language. Use C-x 2 to split your Emacs window in two. Open a source file in one of them C-x C-f foo.lisp. Use C-c C-k, C-c C-r and friends (see SLIME menu) to compile / evaluate regions of your source code in the REPL.
I've looked for something like this in the past and have been unable to find it. You're best off writing all your definitions in a separate buffer and using SLIME's extensive evaluation/compilation functions (C-c C-k loads an entire file, C-x C-e evaluates the last expression, C-c C-r evaluates a region, etc.), only directly entering into the REPL things you don't want to save.
Um, C-x o or C-x b to get to the SLIME REPL buffer, then C-x w or C-x C-s to save it to a file. All the SLIME/CL stuff is a reader comment; you can either write a reader hack to reload the file treating the prompts as comments, or you can go through the file yourself to capture the pieces you want to save.
I agree that the best work flow method is to write your code in a separate buffer and evaluate in that, rather than enter the functions in the repl.
Assuming you have gone the repl way, I guess, C. Martin's solution to save the repl log and manually go through it are your only options.
If you entered the functions and vars into a separate package, you could go through the symbols in the package to help you decide what you want to keep.
E.g. to see all symbols created in the cl-user package:
(let ((p (find-package :cl-user)))
(loop
for s being the symbols in p
when (eq p (symbol-package s))
do (format t "~a~%" s)))

Is there a (repeat-last-command) in Emacs?

Frequently, I've dug into apropos and docs looking for something like the following only to give up to get back to the task at hand:
(repeat-last-command)
do the last C- or M- command I just executed (to be rebound to a fn key)
or sometimes the related:
(describe-last-function)
what keystroke did I just mistakenly issue, the effect of which I'd like to add to my bag of tricks. describe-key is close, but requires knowing what I typed.
Am I simply asking too much from my trusty sidekick?
Repeat functionality is provided by the repeat.el Emacs Lisp package, which is included with standard Emacs distributions. From repeat.el's documentation:
This package defines a command that
repeats the preceding command,
whatever that was, including its
arguments, whatever they were. This
command is connected to the key C-x z.
To repeat the previous command once,
type C-x z. To repeat it a second time
immediately after, type just z. By
typing z again and again, you can
repeat the command over and over.
To see additional information about the repeat command, type C-h F repeat RET from within Emacs.
Repeat last command
C-xz
Once you pressed it, just press only
z
after that and it will repeat (without having to press C-x again).
Yes, there is a repeat command. It's called repeat:
You can repeat commands with C-x z, and hit z to keep repeating.
A bit shocking nobody mentioned repeat-complex-command, available from the key binding C-x ESC ESC.
with regards to 'describe-last-function':
There's a variable last-command which is set to a symbol representative of the last thing you did. So this elisp snippet - (describe-function last-command) - ought to bring up the documentation for the thing that immediately happened.
So you could make a trivial working describe-last-function like so
(defun describe-last-function()
(interactive)
(describe-function last-command))
Put that elisp in .emacs or equivalent, and you'll have a M-x describe-last-function.
If you've banged on a few keys or done something that modified last-command since the thing you're interested in, the command-history function might be of interest. You can get that by M-x command-history
Also, M-x view-lossage shows you the last hundred(?) keystrokes you entered. So, you'll be able to see where the command is. It's what i used until i just right now found out about M-x command-history which i think i'll be using with C-h w now.
I'm not really sure, but maybe you are searching for this one?
The command C-xz (repeat) provides another way to repeat an
Emacs command many times. This command repeats the previous Emacs
command, whatever that was. Repeating a command uses the same arguments
that were used before; it does not read new arguments each time.
Emacs Manual, 8.11 Repeating a Command
May be this would help too...
From emacs Help verbatim:
C-x M-ESC runs the command repeat-complex-command
which is an interactive compiled Lisp function in `simple.el'.
It is bound to <again>, <redo>, C-x M-:, C-x M-ESC.
(repeat-complex-command ARG)
Edit and re-evaluate last complex command, or ARGth from last.
A complex command is one which used the minibuffer.
The command is placed in the minibuffer as a Lisp form for editing.
The result is executed, repeating the command as changed.
If the command has been changed or is not the most recent previous command
it is added to the front of the command history.
You can use the minibuffer history commands M-n and M-p
to get different commands to edit and resubmit.
Personally I found Sebastian's idea useful. Here is a working version
(global-set-key "\C-r" #'(lambda () (interactive)
(eval (car command-history))))
This is old, but Google pops post this up first when I was looking to retrieve the last command I typed at the Emacs prompt. None of these answers worked for me so I decided to put in my two cents for those who might stumble upon this later on as I did. I'm using Portacle, but I found what I was looking for in here so I'm hoping it's generic enough to work with different setups. Anyway, what worked for me is using C-&uparrow; and C-&downarrow; to cycle through the history. Using M-p and M-n worked as well, but I prefer using the arrows since I use Bash quite a bit.
dot-mode is a way to repeat the last command(s).
From its commentary:
It emulates the vi `redo' command, repeating the
immediately preceding sequence of commands. This is done by
recording input commands which change the buffer, i.e. not motion
commands.