Define a function for yasnippet - emacs

I've got a yasnippet for creating a subsection banner comment in c style code:
# -*- mode: snippet -*-
# name: Subsection Comment Block
# key: comm-subsec
# --
/*${1:$(make-string (floor (/ (- fill-column 8 (string-width yas-text)) 2.0)) ?\=)} ${1:Comment} ${1:$(make-string (ceiling (/ (- fill-column 8 (string-width yas-text)) 2.0)) ?\=)}*/
$0
It creates a comment and automatically pads out the line to the fill-column width of the current buffer. It's a little long and tough to read so I was wondering if I could somehow define a functions in this snippet.
I was able to create a defun within backticks like so:
`(defun add-padding (x) x)`
but the function name was returned and entered in the rendered snippet, not ideal.
Should I simply define the function in my init.el file? Is that that best solution? Or is there another way to create what I'm doing?

Related

Append to default value of a list in emacs-lisp: specifically, dotspacemacs-configuration-layers

Goal
I wish to further extend modularity of my spacemacs configuration by referencing files user-config.org and user-layers.org
Template
I have achieved the former by adding the following to my dotspacemacs/user-config:
(defun dotspacemacs/user-config ()
(org-babel-load-file (expand-file-name
"user-config.org" dotspacemacs-directory)))
I can safely experiment with, clone-sync, organize, version-control user-config.org
Problem
Now,
dotspacemacs/user-layers is a variable (default-)set in a function in the following manner:
(defun dotspacemacs/layers ()
(setq-default
dotspacemacs-distribution 'spacemacs ;; not relevant
;;
;; many such default variable-value lines in between, skipped.
;;
dotspacemacs-configuration-layers
'(
;;
;; many layers auto-added/managed by spacemacs
;;
git
(python :variables python-backend 'lsp
python-lsp-server 'pyright
;; other such python configurations
python-formatter 'yapf))
;; other such default varialbe-value declarations
)
)
Attempt
I tried adding a similar inheretance (as with user-config) at the end of the setq-default in the following manner,
N.B. dotspacemacs-configuration-layers cannot be modified outside the function dotspacemacs/layers, hence, reference to the file has to be called within that function.
(defun dotspacemacs/layers ()
(setq-default
dotspacemacs-distribution 'spacemacs
;;
;; many such default variable-value lines in between, skipped.
;;
dotspacemacs-configuration-layers
'(
;; whatever spacemacs wants to auto-add
)
;; other such default variable-value declarations
)
(org-babel-load-file (expand-file-name
"user-layers.org" dotspacemacs-directory))
)
—such that user-layers.org is of the following form:
Referenced org-mode file:
dotspacemacs layers
#+BEGIN_SRC emacs-lisp
(append dotspacemacs-configuration-layers
'(
;;
;; Other layers managed by the user
;;
git
(python :variables python-backend 'lsp
python-lsp-server 'pyright
;; other such python configurations
python-formatter 'yapf)))
#+END_SRC
Failure
However, append does not appear to add elements to the seq-default value.
I guess it is appending to the variable's local setq value.
(I may be wrong at this, as lisp isn't my motherbored tongue, python is.)
Question:
Is there a way to extend the default value of a list in emacs-lisp by adding elements from another list?
clarification:
—such that after addition, the default value of the list contains the newly added elements.
Something like this should work:
(setq-default dotspacemacs-configuration-layers
(append (default-value 'dotspacemacs-configuration-layers)
'(git ...)))
Note that append doesn't modify the original list - it creates a new list with the extra values added. To add values to a list contained in a variable you need to assign back the new list:
*** Welcome to IELM *** Type (describe-mode) for help.
ELISP> (setq x (list 1 2 3))
(1 2 3)
ELISP> (setq y (list 4 5 6))
(4 5 6)
ELISP> (append x y)
(1 2 3 4 5 6)
ELISP> x
(1 2 3)
ELISP> (setq x (append x y))
(1 2 3 4 5 6)
ELISP> x
(1 2 3 4 5 6)

Why is lexical binding not working in this example in emacs?

I copy-pasted this example from the emacs wiki: https://www.emacswiki.org/emacs/DynamicBindingVsLexicalBinding#toc2
The wiki says that the printed thing should be 1, but for me it prints 2, indicating that lexical binding doesn't work. Does anyone have a clue why that might be?
I'm on Emacs v26. Debian.
Here is what is literally in my buffer. I am evaluating it with M-x eval-buffer.
;; -*- lexical-binding: t;-*-
(let ((a 1)) ; binding (1)
(let ((f (lambda () (print a))))
(let ((a 2)) ; binding (2)
(funcall f))))
I would appreciate your help.
You set lexical-binding as a file variable. That gets set when the file is opened, so if you just add the text to the file without closing it and reopening it, the value of lexical-binding will still be nil (do C-h v lexical-binding RET and see for yourself). So you are using dynamic binding and you get 2 for the value.
If you close and reopen the file, the file variable will be set (check it!) and when you evaluate the form, you will get 1.

How to rewrite a native defun in Emacs?

Premises:
My Emacs has a small bug (cannot go up from inside of "") in one of its original defun (up-list). This defun is vital to one of my frequently used command (backward-up-list) which is bound to C-M-u.
Therefore, I wanted to achieve below objectives:
Write a NEW defun named my-up-list which has the bug fix;
RE-write the native defun backward-up-list to call the above new defun (instead of the native buggy up-list). (By RE-writing under the same defun name, I intend to preserve its original convenient key bindings.)
By following the Emacs Tutorial Here, I implemented it as below:
I wrote a new defun my-up-list inside .emacs file; (see code in the end)
I rewrote the defun backward-up-list under a the same name inside .emacs file; (see code in the end).
However, when wI tested it out by trying it in "|" (| is the cursor position), I encounter below error:
backward-up-list: Wrong number of arguments: (lambda nil (interactive) (let ((s (syntax-ppss))) (if (nth 3 s) (progn (goto-char (nth 8 s))))) (condition-case nil (progn (up-list)) (error nil))), 1 [2 times]
Question:
Is it the correct way to re-write a native defun just by putting the
new implementation with the same name in .emacs file?
What may went wrong in my code?
Reference:
(The my-up-list is from here)
;; I only changed "up-list" to "my-up-list" -modeller
(defun backward-up-list (&optional arg)
"Move backward out of one level of parentheses.
With ARG, do this that many times.
A negative argument means move forward but still to a less deep spot.
This command assumes point is not in a string or comment."
(interactive "^p")
(my-up-list (- (or arg 1))))
;; copied from solution to another question - modeller
(defun my-up-list ()
(interactive)
(let ((s (syntax-ppss)))
(when (nth 3 s)
(goto-char (nth 8 s))))
(ignore-errors (up-list)))
I guess your my-up-list function needs to accept the same arguments as the original up-list, and call up-list with them?
The simplest way to do this is with the "advice" system. This provides some simple ways to wrap and extend existing functions. There's a whole section in the elisp manual explaining how to do it.

how to update evaluation result comments of Emacs Lisp forms easilly

Say I have this code which shows usage examples of mapcar
(mapcar #'1+ (list 10 20 30)) ; ⇒ (11 21 31)
(mapcar (lambda (it)
(* 2 it))
(list 0 1 2 3))
;; ⇒ (0 2 4 6)
(require cl-lib)
(cl-mapcar #'+
'(1 2 3)
'(10 20 30))
;; ⇒ (11 22 33)
I may be keeping that code somewhere written so that I can use it on a tutorial or so that whenever I forget how mapcar works, I can quickly read the code.
Now suppose I want to update some of the examples in the code. For example, I may change (list 0 1 2 3) in the second example to some other list. Right after I change the example, the corresponding result comment is then outdated. The result comment need to be updated as well. So I evaluate the form, copy the result, and replace the old result in the comment with new result. Is there a package I can use to help me do that all easily and less tediously? This is a different problem than problems that the litable or ielm package solve because this is simply about updating existing example code.
Right now what I use is:
(defun my-insert-eval-last-sexp ()
(interactive)
(let ((beg (point)))
(let ((current-prefix-arg '(4)))
(call-interactively 'eval-last-sexp))
(goto-char beg)
(if (looking-back ")")
(insert " ; "))
(insert "⇒ ")
(move-end-of-line 1)))
which still isn't enough because it simply adds the result comment rather than updating an old one, and has a bug of odd stuff getting inserted when the form evaluates to a number:
(+ 1 2)
;; ⇒ 3 (#o3, #x3)
Well, I'm not sure I want to encourage this kind of thing ;-), but this will get you a little closer to what you are trying to do, IIUC:
(defun my-insert-eval-last-sexp ()
(interactive)
(let ((this-command 'eval-print-last-sexp))
(save-excursion (eval-last-sexp-1 t)))
(when (looking-back ")") (insert " ; "))
(insert "⇒ ")
(move-end-of-line 1))
You don't need to save point and then explicitly go back to it --- use save-excursion.
You don't need to bind the prefix arg and call the command interactively. Just call it (or its helper function) directly, passing the arg you want.
You need to tweak the behavior to prevent it from thinking this is the second occurrence of the command, which is what causes it to print the octal etc. number info. The let binding does that (but is an ugly little hack).
Respective thing your function does is implemented in org-mode, i.e. org-babel.
See in Info, Org Mode, 14 Working with source code

Emacs: the code in the body of a defun or defmacro cannot refer to surrounding lexical variables?

Update 2013 May: As of GNU Emacs 24.3.1, (let .. (defun..)) bytecompiles just fine without warning and the bytecompiled code works the same as not-compiled code. Just don't forget to add the file variable lexical-binding: t to the file to be bytecompiled. Workarounds at the end of this question is now not necessary.
Lexical Binding - Emacs Lisp Manual has this paragraph:
Note that functions like symbol-value, boundp, and set only retrieve or modify a variable's dynamic binding (i.e. the contents of its symbol's value cell). Also, the code in the body of a defun or defmacro cannot refer to surrounding lexical variables.
I am not sure if I am getting the meaning of the second sentence right. In the following code which should be run in lexical binding mode, the code in the body of a defun is successfully referring to the lexical binding value of the name n.
(let ((n 0))
(defun my-counter ()
(incf n)))
(my-counter) ;; 1
(my-counter) ;; 2
Is the sentence simply saying that (let .. (defun ..)) is a bad practice?
Workarounds:
;; -*- lexical-binding: t -*-
;; a way to define the counter function without byte-compile error or warning
(defvar my--counter-func
(let ((n 0))
(lambda ()
(setq n (1+ n)))))
(defun my-counter ()
(funcall my--counter-func))
;; another way to define the counter function, again without byte-compile error or warning
(fset 'my-another-counter
(let ((n 0))
(lambda ()
(setq n (1+ n)))))
And here's the code for testing the above code:
;; run:
;; emacs -q --load path-to-the-el-file-of-this-code.el
(load "path-to-file-defining-my-counter.elc") ;; loading the ELC file to test if byte-compiled code runs as expected.
(print (my-counter)) ;; 1
(print (my-counter)) ;; 2
(print (my-another-counter)) ;; 1
(print (my-another-counter)) ;; 2
The code does not byte-compile well at least in Emacs 24.1.1. I saved the following code in the foo.el file, which uses setq in place of incf in order to avoid any possible effects by the cl library:
;; -*- lexical-binding: t -*-
(let ((n 0))
(defun my-counter ()
(setq n (1+ n))))
When I tried to byte-compile it (M-x byte-compile-filefoo.el), I got the following warning messages:
foo.el:3:1:Warning: Function my-counter will ignore its context (n)
foo.el:3:1:Warning: Unused lexical variable `n'
foo.el:5:11:Warning: reference to free variable `n'
foo.el:5:17:Warning: assignment to free variable `n'
All of the messages are indicating that the code in the body of the defun construct cannot refer to the surrounding lexical variable n as the manual claims.
Actually, when I loaded the byte-compiled code (M-x load-filefoo.elc) and evaluted the (my-counter) form, I got the following erorr:
Debugger entered--Lisp error: (void-variable n)
...
Unfortunately, I'm not sure why the code appears to work when evaluated in the form of source code.
As I replied on gnu.emacs.help, you can use (defalias 'foo (lambda ...)) to work around that limitation.
And that limitation is lifted in Emacs's development code.
It's perfectly fine to refer to variables in lexical scope(*) from within a defun, just as you are doing above, and just as the "my-ticker" example on that manual page does as well.
Either I am missing something the line in the manual that says:
the code in the body of a defun or defmacro cannot refer to
surrounding lexical variables.
should say something more like:
code in the body of a defun can only access lexical variables if they are defined within the same lexical scope.
NOTE: There are comments in the other answers about problems byte-compiling this kind of code. Those should be fixed in the latest emacs. I've verified in v24.2.50.1 that this byte compiles and loads correctly.