Problem with org-mode code structure "#+begin_src lisp" in spacemacs - lisp

when I'm writting a code in lisp in a org-mode file. I started a block
#+begin_src lisp
#+end_src
When I write the fist line no problem, functions names highlight correctly.
#+begin_src lisp
(defun myappend (L1 L2)
(cond
#+end_src
but when I press enter on the second line, it starts to be weird.
#+begin_src lisp
(d efun myappend (L1 L2)
(c ond
#+end_src
Indentation is inserted in the code. Do you know what I'm doing wrong?

Related

Doom Emacs: Sly Vs. Slime issue

I have an org file with source block:
#+begin_src lisp
(defun palindromp (l)
(equal l (reverse l)))
(palindromp '(a b c b a))
#+end_src
When I type C-c C-c to evaluate this block, I get the following message.
org-babel-execute:lisp: Cannot open load file: No such file or directory, sly
My Doom configuration (given by r\hlissner) is:
;; in ~/.doom.d/packages.el
(package! sly :disable t)
(package! sly-macrostep :disable t)
(package! sly-repl-ansi-color :disable t)
(package! slime)
Do I have to add anything more here in my configuration. I did
(add-to-list 'org-src-lang-modes '("lisp" . slime))
but it is not working.
The default value of org-eval-lisp-eval-fn is sly-eval.
I changed it to slime-eval. Now, it is working but requires slime to run in the background.

Remove the suffix of a returned filename

I set the yasnippet of elisp src as
#+begin_src emacs-lisp :session `(current-buffer)` :lexical t
$0
#+end_src
Which set the current-buffer as session name
#+begin_src emacs-lisp :current-file-name sicp :lexical t
(current-buffer)
#+end_src
#+RESULTS:
: #<buffer yasnippet-offprint.org>
A minor problem is to remove suffix org from yasnippet-offprint.org.
This might be trivial, but search the elisp manual, thus did not got an immediate solution.
How could remove the suffix the yasnippet-offprint.org
The result I desire is
#+begin_src emacs-lisp :session yasnippet-offprint :lexical t
#+end_src
You can use file-name-sans-extension, e.g.,
(file-name-sans-extension "yasnippet-offprint.org")
;; => "yasnippet-offprint"
You can use C-h f (M-x describe-function) to read its docstring. And (info "(elisp) File Name Components") lists functions on manipulating filenames.

Bind F1 to smart contextual documentation (help buffers)

Is there a command which I could bind to F1 in Emacs that would give me (in
a temporary or persistent buffer) contextual documentation on the "symbol" under
point?
IIUC, that'd give the same results as:
C-c C-v RET in ESS buffers (= ess-display-help-on-object)
C-h f/C-h v RET in Emacs Lisp buffers (= describe-function/describe-variable)
C-h S in Shell buffers (= info-lookup-symbol) + M-x man-follow
??? in AWK, C, Java
??? in AUCTeX buffers (well, that may be a big dream ;-))
...
Somehow, Auto-Complete does that job (at least in ESS and Emacs Lisp buffers),
as it displays the right help (on functions, on variables, etc.) in its "quick
help" tooltip. Though, it's only displayed during the completion process, not after.
I first thought that info-lookup-symbol would be (part of) the solution, but it
does NOT work in ESS, so it's pretty limited...
What should I bind to F1, then?
I found this snippet somewhere on the internet a while ago, which can do this for Emacs lisp:
(defun describe-symbol-at-point ()
"Get help for the symbol at point."
(interactive)
(let ((sym (intern-soft (current-word))))
(unless
(cond ((null sym))
((not (eq t (help-function-arglist sym)))
(describe-function sym))
((boundp sym)
(describe-variable sym)))
(message "nothing"))))

In Emacs Lisp, how to find where the symbol is defined

When I do C-h f or C-h v, Help tells me in which file the symbol is defined or where it will be autoloaded from. How can I find the same information programmatically?
Some digging reveals that
(find-lisp-object-file-name object type)
Should do the trick. As an example:
(find-lisp-object-file-name 'goto-line 'function)
;; => "/usr/local/Cellar/emacs/24.3/share/emacs/24.3/lisp/simple.el"
EDIT: How I discovered this information:
First I did C-h k C-h f to figure out what C-h f is bound to. The result is describe-function, so let's do C-h f describe-function to see the source for that. I noticed that it was essentially an interactive wrapper around describe-function-1, so I jumped to the source for that. There's a lot of stuff in there, but the pertinent line is:
(file-name (find-lisp-object-file-name function def))
Revealing that find-lisp-object-file-name is the function used to do this work internally.
To add to James Porter's answer
;;; run from: emacs -q
(require 'cl) ; for incf
(print (list
;; goto-line is a function defined in simple.el
(find-lisp-object-file-name 'goto-line (symbol-function 'goto-line))
;; print is a function defined in C
(find-lisp-object-file-name 'print (symbol-function 'print))
;; rx is an autoload from rx.el
(find-lisp-object-file-name 'rx (symbol-function 'rx))
;; incf is an alias for cl-incf defined in cl.el
(find-lisp-object-file-name 'incf (symbol-function 'incf))
;; cl-incf is defined in cl-lib.el
(find-lisp-object-file-name 'cl-incf (symbol-function 'cl-incf))))
;; => ("c:/run/Emacs/lisp/simple.el" C-source
;; "c:/run/Emacs/lisp/emacs-lisp/rx.el" "c:/run/Emacs/lisp/emacs-lisp/cl.el"
;; "c:/run/Emacs/lisp/emacs-lisp/cl-lib.el")
(print (list
;; print-circle is a variable defined in C
(find-lisp-object-file-name 'print-circle 'defvar)
;; indent-line-function is a variable defined in indent.el
(find-lisp-object-file-name 'indent-line-function 'defvar)))
;; => (C-source "c:/run/Emacs/lisp/indent.el")

emacs macro doesn't define command

I have written a macro that makes an alias and generates global keybinding based on the name.
I expect that if I add to my emacs config the following code for example:
(defkey-alias cool-function make-directory)
I will have command my-cool-function which creates a directory and a keybinding C-c c f to it. But after evaluation I have keybindings but have no command my-cool-function.
And if I do C-h k C-c c f I see:
C-c c f runs the command my-cool-function, which is an alias for `make-directory'.
I can not evaluate (my-cool-function) in scratch either.
But if I try to (macroexpand '(defkey-alias cool-function make-directory)) and then evaluate expanded s-expr it works.
What is the difference between calling macro and calling macroexpanding and then evaluation? And why alias is not callable?
Thank you.
Emacs version is GNU Emacs 24.2.1, Windows 7
The code:
;;; defining keys
(defun name-to-key(funname)
" works like:
'my-cool-function -> \"\C-cmcl\" "
(apply 'concat
"\C-c"
(mapcar (lambda(str)(substring str 0 1))
(split-string (symbol-name funname)
"-"))))
(defmacro defkey-alias(alias function)
"works like defalias but you should not quote symbols and sets global key mapping
Usage: (defkey-alias mkdir make-directory)"
(let ((myalias (make-symbol (concat "my-" (symbol-name alias)))))
`(progn
(defalias ',myalias ',function)
(global-set-key ,(name-to-key alias) ',myalias))))
UPDATED: Using (defun ...(interactive)(call-interactively 'function)) also does not work
The reason is simple: (make-symbol (concat "my-" (symbol-name alias))) returns a non-interned symbol. I.e. it returns a symbol whose name is my-cool-function and yet it is a different symbol from the one you get when you write (my-cool-function). So instead of make-symbol you want to use intern.