elisp

elisp macro to write a function?

天大地大妈咪最大 提交于 2019-12-04 10:09:32
问题 I have written a few nearly identical functions, except for their names. For example: ; x is name, such as function/paragraph/line/etc. (defun my-x-function (interactive) (mark-x) (do-more-stuff) (modify-x)) Is there a way to automatically generate such functions? I have a feeling this is what macros do, but I am not sure how to use them. Any help, maybe including a small example would be great. Thanks! 回答1: Yep, that's exactly what macros do. Here's a straightforward macro that builds

Converting from camelcase to _ in emacs

旧城冷巷雨未停 提交于 2019-12-04 09:57:20
问题 Is there an emacs function to convert a camel-cased word to underscore? Something, like: longVariableName M-x to-underscore long_variable_name 回答1: (progn (replace-regexp "\\([A-Z]\\)" "_\\1" nil (region-beginning) (region-end)) (downcase-region (region-beginning) (region-end))) 回答2: Use the string-inflection package, available on MELPA, or at https://github.com/akicho8/string-inflection. Useful keyboard shortcuts, copied from https://www.emacswiki.org/emacs/CamelCase : ;; Cycle between snake

How to run hook depending on file location

你。 提交于 2019-12-04 09:16:37
I am involved in python project where tabs are used, however i am not using them in every other code i write, it is vital to use them in that particular project. Projects are located in one directory under specific directories. I.E: \main_folder \project1 \project2 \project3 ...etc I have couple functions/hooks on file open and save that untabify and tabify whole buffer i work on. ;; My Functions (defun untabify-buffer () "Untabify current buffer" (interactive) (untabify (point-min) (point-max))) (defun tabify-buffer () "Tabify current buffer" (interactive) (tabify (point-min) (point-max))) ;;

Model/View-like editing in Emacs

心不动则不痛 提交于 2019-12-04 09:06:12
I have some JSON files and I'm writing a mode that allows editing a single property of the JSON object independently from the rest. For example: foo.json: { "creation_timestamp": "1411210038.000000", "description": "lorem ipsum.\ndolor sit amet.", "version": 4 } Opening foo.json results in this buffer: lorem ipsum. dolor sit amet. Changing the first line to "foo bar" and saving the file results in a foo.json with only the description field updated: { "creation_timestamp": "1411210038.000000", "description": "foo bar.\ndolor sit amet.", "version": 4 } What's the best strategy for this? My

What does “s-[keyname]” refer to in Emacs, and how do I tell Emacs to ignore it?

房东的猫 提交于 2019-12-04 08:56:07
问题 Background information: I'm on a Mac, and I've just upgraded to Emacs 23.1 via http://emacsformacosx.com/. There are a few issues, notably the lack of full screen ability. I've attempted to get around this last issue by installing Megazoomer, which adds a global input manager bound to Cmd-return . This causes the currently forward application to maximise. However, Emacs reports that <s-return> is undefined . I've never seen an s-[key] mentioned before, and Google isn't forthcoming with an

Writing “Hello World” in Emacs?

狂风中的少年 提交于 2019-12-04 07:54:35
问题 I would like to write a few Unix scripts in Emacs Lisp. However, there doesn't seem to be a clean way to write to STDOUT so I can redirect the results to a file or pipe the output to another command. The print function places double quotes around the output strings so I get "Hello world!" instead of Hello world! . Here's the emacs script. #!/usr/bin/emacs --script ;; ;; Run me from a Unix shell: ./hello.el > x.txt ;; (message "Hello world! I'm writing to STDERR.") (print "Hello world! I'm

How to create temporary files `.#filename` in `/tmp`, not working directory

眉间皱痕 提交于 2019-12-04 07:54:33
When files are being modified in Emacs, a temporary file is created in the working directory that looks like this: .#filename . The file is deleted when the buffer is saved. I found a few of these types of temporary files in my Git remote repositories, and I thought it might be better to nip the bud at the source instead of configuring Git to ignore them for every project. How can we configure Emacs to create those files in the /tmp directory instead of the working directory? lawlist The file at issue is called a lock file -- commencing with Emacs version 24.3, it can be controlled with the

Why is there no tail recursion optimization in Emacs lisp, not but like other scheme?

落花浮王杯 提交于 2019-12-04 07:48:25
Emacs lisp is a dialect of LISP and especially Scheme. Most of scheme interpreters do have a optimization of Tail Recursion, but emacs lisp doens't. I searched the reason in `info elisp' for a while, but I fail to find it. P.S. Yes, there is other iteration syntax in elisp like `while', but I still cannot find a good reason why they didn't implement tail recursion like other scheme interpreters. Emacs Lisp was created in the 1980's. The Lisp dialect that the Emacs author (Richard Stallman) was most familiar with at the time was MIT Maclisp, and Emacs Lisp is very similar to it. It used dynamic

elisp: capturing variable from inner function

被刻印的时光 ゝ 提交于 2019-12-04 06:51:56
My lovely function: (defun f (x) (lambda (y) (+ x y))) Then, I expect this: (funcall (f 2) 2) To return 4. But alas, I got this instead: Debugger entered--Lisp error: (void-variable x) So how can I capture variable from inner function? You've been bitten by elisp's dynamic scoping. The x in the lambda refers to the variable x that is in scope when the lambda is called (and since in this case there is no x in scope when you call it, you get an error), not to the x which is in scope when you create the lambda. Some ways of simulating lexical closures in elisp are explained on this page on the

How to get the start/end of the current buffer info with emacs/elisp?

我的未来我决定 提交于 2019-12-04 06:02:52
I have the following code that runs figlet that has input as a range. How can I modify this code to check if b or e is not specified, make b to the start of the current buffer, and e end of the current buffer? (defun figlet-region (&optional b e) (interactive "r") (shell-command-on-region b e "/opt/local/bin/figlet" (current-buffer) t) (comment-region (mark) (point))) (global-set-key (kbd "C-c C-x") 'figlet-region) ADDED Sean helped me to get an answer to this question (defun figlet-region (&optional b e) (interactive) (let ((b (if mark-active (min (point) (mark)) (point-min))) (e (if mark