How to yank in a search in an emacs lisp function - function

(defun search-for-what-is-just-killed ()
(interactive)
(search-forward latestkillringvariable? nil t)
)
How to use "yank" in an emacs lisp function?

You can access directly the kill-ring list to access the latest kill:
(substring-no-properties (car kill-ring))
The substring-no-properties bit is important since text is kept in the kill ring with additional properties (like fontification specific to a particular mode and you'll probably want to strip those).

Related

How to auto indent multiple files?

Currently, I generate static files and those files aren't indented properly. Emacs' auto indent works great with c-x h tab, but that's per file. I want to auto indent multiple files (Like 50 or so, thus it's not feasible to do it manually).
Is there any way to accomplish this? Whether it be using a different text editor or a script or etc. If it helps, most of the files are .html.
I tested the code below with some HTML files, it works well.
(defun indent-file (file-name)
(save-window-excursion
(find-file file-name)
(indent-region (point-min) (point-max))
(write-region nil nil file-name)))
;; argv is a list stores command line option. In this case, it will be ("/your/directory/path").
;; directory-files-recursively will find files recursively, and it needs emacs 25.1 or later.
(let* ((target-dir (car argv))
(target-file-names (directory-files-recursively target-dir ".*.html$")))
(dolist (file-name target-file-names)
(indent-file file-name)))
Save the code above as 'indent-files.el'
Run emacs --script indent-files.el "/your/directory/path" in terminal
It will be tricky if you want to use emacs lisp as a common script language, although it actually could indeed. Here are some tips: https://swsnr.de/blog/2014/08/12/emacs-script-pitfalls/

How to tell Proof General that ".csv" != ".v"

Every time I open a .csv file in an Emacs buffer, Proof General starts up (unless it's already started) and resets my windows. This really throws off my Emacs groove and needs to stop.
The only part of my init.el that deals with Proof General is this:
(load-file "~/.emacs.d/ProofGeneral-4.2/generic/proof-site.el")
(setq auto-mode-alist (cons '("\.v$" . coq-mode) auto-mode-alist))
(autoload 'coq-mode "coq" "Major mode for editing Coq vernacular." t)
Your regex to match names is after initial string translation .v$ because single backslash escape belongs to the string reader. As a result, every name that has atleast two characters and the last one is a v, is assigned to coq-mode.
The fix is easy: use double backslashes in the pattern.

Run Lisp functions on load

Whenever I (load "program.lisp") using the following code I get the error: "Error: Unexpected end of #input stream "program.lisp"
(defun theProgram ()
(reset)
(print "Hello Kappa")
(setentries)
(startloop)
(loop for x in mylist collect (splitremove))
(loop for x in numlist collect (getgrades))
(loop for x in namelist collect (getprint))
(loop for x in printlist collect (andprint)))
(theProgram)
I know the last line is the problem and it will work fine if it is not included however I need the program to startup on load, how can I achieve this?
Edit: I should note that setentries calls a (read) and so does startloop. I am using GNU common lisp 2.6 with gcl interpreter.
The actual error was that the (read) part in the functions did not have stream declared so it was reading the input from the script as it was running rather than from the terminal.

How to bind F1-F12 key as keyboard macro in Emacs?

I want to bind F1-F12 key as keyboard macro. Starting kbd-macro with Shift-[F1-F12] and execute it with [F1-F12]. Each key can have different macro. Is there any way to do this?
Try this bit of code:
(mapc (lambda(x) (global-set-key x 'auto-name-macro))
'([f5] [f6] [f7] [f8]))
(defun auto-name-macro (arg)
(interactive "p")
(if defining-kbd-macro
(progn
(kmacro-end-or-call-macro arg)
(fset
(intern (format "mcr-%s" last-command-event))
last-kbd-macro))
(execute-kbd-macro
(intern
(format "mcr-%s" last-command-event)))))
You start macro definitions with f3 as before, but now
f5, f6, f7 and f8 function in the same
way as f4, except each remembers their own macro.
You finish definition with the key and then call it again with the same key. Exactly like f4.
Macros also get names, e.g. mcr-f5, mcr-f6, ..., so you can call them with M-x instead.
UPD: macros can reference each-other and be bound to any key
There's more than a few lines of code so I've put it at https://github.com/abo-abo/centimacro.
The setup is just
(require 'centimacro)
f5 will call centi-assign.
It prompts you where you'd like your next binding to be, so you press f6,
for instance.
The macro is now recording, and pressing f6 will stop it and assign
the new macro to f6.
Now you can use what you defined in f6 for other macro, say f7, but it could be C-e, it's not restricted to functional keys.
Here's an example:
<f5><f6>foo<f6> ;; Now <f6> inserts "foo".
<f5><f7><f6>bar<f7> ;; Now <f7> inserts "foobar".
<f5><f8><f6>-<f7>-<f6><f8> ;; Now <f8> inserts "foo-foobar-foo".
<f5><f6>omg<f6> ;; Now <f6> inserts "omg",
;; <f7> - "omgbar",
;; <f8> - "omgbar-omg-omg".
Well assigning a macro to a key is trivial. Just record it as usual, then type:
C-xC-kb
I'd also suggest that you don't want to waste 12 useful bindings on "record a macro" when one (pre-existing) binding will do, so you may prefer to adapt to this approach rather than pursuing the original request.
Lastly, note that F5-F9 are all reserved for the end user, but other function keys are not, and already have useful bindings (including keys for recording keyboard macros, funnily enough), so I would focus on those. (If you're running out, you can always make one or more of them a prefix binding.)
I don't think that you can do this exctly as you specified, because any function you write to help with this has to have (start-kbd-macro) as the last call in it - that's the way that start-kbd-macro works. This prevents it from naming the keyboard macro after the user finishes recording it.
The only way I can see to make something similar would be to have shift-F1 be the key that you use after you finish recording the macro that is to be stored in F1.
The user would have to start recording a kbd macro the usual way, but hit shift F1 after he completes it. You could then easily put a function on shift-F1 that renames the macro to the function that you have bound to F1.
The code would be something like this:
(defun assign-macro-to-f1 () (interactive)
(name-last-kbd-macro 'f1-kbd-macro)
(global-set-key[(f1)] (execute-kbd-macro `f1-kbd-macro))
)
(global-set-key [(shift f1)] `assign-macro-to-f1)
(note - I tried also ending the kbd macro in the shift-f1 function, but it seems like messing around with kbd macros in functions is problematic ... better to let them record the macro the normal way, and save it with this function)
Here is a solution I have used:
After recording the macro, name it using C-xC-kn.
Then you can use M-xinsert-kbd-macro and give it the name of the macro you just defined. That will insert some lisp code which defines the macro. You can then put the generated lisp code in your init.el file, followed by a global-set-key to set the macro to whatever key you want.
Here's a sample of the generated lisp and keybinding (the macro just enters the word "test"):
(fset 'test-macro
(lambda (&optional arg) "Keyboard macro." (interactive "p")
(kmacro-exec-ring-item (quote ([116 101 115 116 41 backspace] 0 "%d")) arg)))
(global-set-key '[(f5)] 'test-macro)

Emacs function automaticly accepting prompt

I am trying to find a way to automatically accept the first proposal from the minibuffer.
(defun find-file-at-point-without-prompt ()
(interactive )
(find-file-at-point)
)
Calling results in the prompt: "Find file or URL: ......". I just want an automatic "yes".
Passing arguments does not work. It might be interesting for other cases as well. I used a macro before that would just call find-file-at-point followed by a RET.
It seems there is no variable to automatically accept the prompt.
You can redefine a function ffap-read-file-or-url by removing a part which is doing the prompt. It remains something like this
(defun ffap-read-file-or-url (prompt guess)
"Read file or URL from minibuffer, with PROMPT and initial GUESS."
(or guess (setq guess default-directory))
(let (dir)
;; Tricky: guess may have or be a local directory, like "w3/w3.elc"
;; or "w3/" or "../el/ffap.el" or "../../../"
(or (ffap-url-p guess)
(progn
(or (ffap-file-remote-p guess)
(setq guess
(abbreviate-file-name (expand-file-name guess))
))
(setq dir (file-name-directory guess))))
;; Do file substitution like (interactive "F"), suggested by MCOOK.
(or (ffap-url-p guess) (setq guess (substitute-in-file-name guess)))
;; Should not do it on url's, where $ is a common (VMS?) character.
;; Note: upcoming url.el package ought to handle this automatically.
guess))