Importing single functions in racket & definition window behavior - racket

The question is this... After I (provide f) in "file a" and (require "file a") in "file b". If I used expressions "file a's" definition window then those expressions are also imported into "file b's" interaction window.
Is there a way around that, or should I just not use expressions in the definition window unless I intend to use them?

There's not a way around that. Using require runs the file that it required, and running your "ch7.rkt" prints the result of that expression.

Related

wrap s-expression in slime (emacs)

Is there a short way to wrap an s-expression using slime?
Suppose i have finished some piece of code and I realize I need a variable (e.g. for efficiency reasons) and therefore want to wrap it with let or want to make it tail recursive and need to wrap it with labels, what is the fastest way to do this? Is there a shortcut?
IntelliJ (a Java IDE) allows things like:
x > 3.if + TAB
yielding
if (x > 3) {
}
So are there any "sexp-wrapping" shortcuts (postfix or prefix) in slime/emacs?
The bar represents the cursor.
With paredit
There is a wrap-round command, bound to M-(. But when adding context to an expression, I generally do as follows:
|code
Open the parenthesis (the closing one is added automatically):
(|)code
... type ...
(let |)code
Call paredit-forward-slurp-sexp (C-right)
(let | code)
See also the The Animated Guide to Paredit.
Without paredit
|code
Call mark-sexp (C-M-Space), then kill the region; type and yank (paste) the text where you need it.
See also Expressions with Balanced Parentheses (e.g. forward-sexp), available in Emacs.

How do I use defjump in emacs lisp to jump to a function in a file?

What I'd like that to do, is when I'm on a line of source such as: foo
And I hit a "jump" key, it should match href="foo.html" and open the file c:/project/root/templates/foo.html.
I've found jump.el (package 'jump' in emacs 24) and am trying to get defjump to work:
(require 'jump)
(defjump
'my-follow-ref
'(("href=\"\\1\"" . "templates/\\1"))
"c:/project/root/"
"Follow a logical link from one part of the source to another.")
My code is based on the example in the help, but I'm getting stuck on a pretty basic emacs lisp error:
mapcar: Wrong type argument: listp, quote
What am I doing wrong?
There is no jump.el in the emacs 24 source tree, and google is no help, but, I guess, your problem is unnecessary quoting: defjump is probably a macro.
Chances are this will work:
(defjump
my-follow-ref
(("href=\"\\1\"" . "templates/\\1"))
"c:/project/root/"
"Follow a logical link from one part of the source to another.")

Emacs and Clojure method call hierarchy

I use eclipse to develop in Java, because it has a lots of useful features. Some frequently used are "Open Declaration" and "Open Call hierarchy".
Open Declaration - If i use somewhere some function i just press F3 (hotkey) and get the file with cursor pointed to its definition.
Call Hierarchy - shows the tree of calls this method.
For programming on clojure i use emacs as IDE.
I'm interested if Emacs support such functionality or exists some plugins to add this?
Note: I can perform regex search on method name to find definition but it not always gives unique results.
In emacs with SLIME/swank-clojure, you can go to the definition of a var (including function declarations) by pressing M-. which calls slime-edit-definition
There's also a slime-who-calls function under C-c C-w C-c which may be some kind of substitute for call hierarchy

GNU Emacs: skeleton-mode, is it still used?

given all the possible solutions to have a template system with GNU Emacs, what do people use today ? I am still using skeleton-mode but as I read it here and there, we must be really few to do so.
What are you using and why ? (maybe I could switch to a more popular tool).
For example, given this snippet:
(define-skeleton mwe:cl-defpackage-skeleton
"Inserts a Common Lisp DEFPACKAGE skeleton."
(skeleton-read "Package: " (if v1
(file-name-sans-extension
(file-name-nondirectory
(buffer-file-name)))))
(if (setq v1 (bobp)) ";;; -*- Mode:Lisp; Syntax:ANSI-Common-Lisp;")
& (if buffer-file-coding-system
(concat " Coding:"
(symbol-name
(coding-system-get buffer-file-coding-system
'mime-charset))))
& " -*-"
& \n
& \n "(defpackage #:" str
\n "(:nicknames" ("Nickname: " " #:" str) & ")" | '(kill-whole-line -1)
\n "(:use #:CL" ((slime-read-package-name "USEd package: ") " #:" str) ")"
")" \n
\n
(if v1 "(in-package #:") & str & ")" & \n &
\n
_)
(credits: http://www.foldr.org/~michaelw/log/programming/lisp/defpackage-skeleton)
which (modern) template mode could do the same (and how ;)) ?
Cheers
For the record: even another 7 years later I am still very happy with skeletons. Lots of them available over there in my init files: https://github.com/ska2342/ska-init-files/blob/master/dot.emacs.d/init.el
I use yasnippet.
In my emacs I have this:
(require 'yasnippet-bundle)
In my hook for each mode where I want to use snippets (like my c-mode hook, etc), I have this:
(yas/minor-mode-on)
The "static" snippets I use are available, in the directory structure I use, here:
http://cheeso.members.winisp.net/srcview.aspx?dir=emacs&file=snippets.zip
You need to create the bundle .el file mentioned above, once, when any of the snippets change. do it this way:
(require 'yasnippet)
(yas/compile-bundle
; the starting point
"c:/your/path/yasnippet.el"
; the bundle file to generate
"c:/your/path/yasnippet-bundle.el"
; the snippet dir from which to generate the bundle
"c:/your/path/snippets")
That's it!
Then, when I'm in a C# file and type for<TAB>, I get a template with a for loop. And so on.
I also use yasnippet with dynamic snippet templates. A C# code-completion module I wrote calls yas/expand-snippet with a dynamically constructed string that defines the template to expand.
So, you can type
MyType.Method(<COMPLETE>
...where <COMPLETE> is the code-completion key, and the code-completion module does the lookup on the MyType.Method(, then builds a menu of choices, and pops it up. When the user selects a choice from the menu, the code-completion module builds the template, containing fields for each of the arguments for the selected method. Then it calls yas/expand-snippet and that template is injected into the buffer, just as if it had been a static template. In the dynamically-generated template, each argument to the method gets a "typeover" field, and I just fill it in, tabbing through the fields. Pretty nice.
This "dynamic snippet" idea would work with any code-completion engine. You just need a way to map from a method or function signature, like this:
function(int arg1, string arg2, char arg3)
to a yasnippet template definition string, which looks like this:
function(${1:int arg1}, ${2:string arg2}, ${3:char arg3})
And that's a pretty trivial piece of elisp.
I haven't used skeleton mode much, but I use YASnippet while coding in Ruby and C. Its pretty useful, but I suspect skeleton mode is far more powerful.
The emacs wiki lists Yasnippet as a possible replacement for skeleton. The snippets that come with yasnippet are pretty good, but you should really write your own, as the true power lies there.

Using Emacs to recursively find and replace in text files not already open

As a follow-up to this question, it's trying to find out how to do something like this which should be easy, that especially stops me from getting more used to using Emacs and instead starting up the editor I'm already familiar with. I use the example here fairly often in editing multiple files.
In Ultraedit I'd do Alt+s then p to display a dialog box with the options: Find (includes using regular expressions across multiple lines), Replace with, In Files/Types, Directory, Match Case, Match Whole Word Only, List Changed Files and Search Sub Directories. Usually I'll first use the mouse to click-drag select the text that I want to replace.
Using only Emacs itself (on Windows XP), without calling any external utility, how to replace all foo\nbar with bar\nbaz in *.c and *.h files in some folder and all folders beneath it. Maybe Emacs is not the best tool to do this with, but how can it be done easily with a minimal command?
M-x find-name-dired: you will be prompted for a root directory and a filename pattern.
Press t to "toggle mark" for all files found.
Press Q for "Query-Replace in Files...": you will be prompted for query/substitution regexps.
Proceed as with query-replace-regexp: SPACE to replace and move to next match, n to skip a match, etc.
Press C-x s to save buffers. (You can then press y for yes, n for no, or ! for yes for all)
M-x find-name-dired RET
it may take some time for all the files to appear in the list, scroll to bottom (M->) until "find finished" appears to make sure they all have loaded
Press t to "toggle mark" for all files found
Press Q for "Query-Replace in Files...": you will be prompted for query/substitution regexps.
Proceed as with query-replace-regexp: SPACE or y to replace and move to next match, n to skip a match, etc.
Type ! to replace all occurrences in current file without asking, N to skip all possible replacement for rest of the current file. (N is emacs 23+ only)
To do the replacement on all files without further asking, type Y.
Call “ibuffer” (C-x C-b if bound to ibuffer, or M-x ibuffer RET) to list all opened files.
Type * u to mark all unsaved files, type S to save all marked files
* * RET to unmark all marks, or type D to close all marked files
This answer is combined from this answer, from this site, and from my own notes. Using Emacs 23+.
Projectile is really nice:
C-c p r runs the command projectile-replace
The answers provided are great, however I thought I'd add a slightly different approach.
It's a more interactive method, and requires wgrep, rgrep and iedit. Both iedit and wgrep must be installed via MELPA or Marmalade (using M-x package-list-packages)
First run M-x rgrep to find the string you're looking for.
You'll be able to specify file types/pattern and the folder to recurse.
Next you'll need to run wgrep start it with C-s C-p.
Wgrep will let you edit the rgrep results, so set a region on the string to match and start iedit-mode with C-; (depending on your terminal you may need to re-bind this)
All occurrences will be editable at once. C-x C-s to commit wgrep. Then C-x s ! to save the changed files.
The main benefit of this method is that you can use iedit-mode to toggle off certain matches M-;. You can also use the results in rgrep to jump into the files, for example if you have an unexpected match.
I find it very useful for doing source edits and renaming symbols (variables, function names etc.) across a project.
If you don't already know/use iedit mode it's a very handy tool, I strongly recommend you give it a look.
I generally use other tools to perform this task, and it seems like many of the approaches mentioned at EmacsWiki's Find and Replace Across Files entry shell out, but the Findr Package looks very promising.
Stealing part of the source file:
(defun findr-query-replace (from to name dir)
"Do `query-replace-regexp' of FROM with TO, on each file found by findr.
Source of information: 1
For emacs pro users:
Call dired to list files in dir, or call find-dired if you need all subdirectories.
Mark the files you want. You can mark by regex by typing 【% m】.
Type Q to call dired-do-query-replace-regexp.
Type your find regex and replace string. 〔☛ common elisp regex pattern〕
For each occurrence, type y to replace, n to skip. Type 【Ctrl+g】 to abort the whole operation.
Type ! to replace all occurrences in current file without asking, N to skip all possible replacement for rest of the current file. (N is emacs 23 only)
To do the replacement on all files without further asking, type Y. (Emacs 23 only)
Call ibuffer to list all opened files. Type 【* u】 to mark all unsaved files, type S to save all marked files, type D to close them all.
Step-by-Step Guide for Emacs Beginners
Select Target Files
Start emacs by typing “emacs” in the command line interface prompt. (Or, double click the Emacs icon if you are in a Graphics User Interface environment)
Selecting Files in a Directory
First you need to select the files you want to do the replace. Use the graphical menu 〖File ▸ Open Directory〗. Emacs will ask you for a directory path. Type the directory path, then press Enter.
Now, you will be shown the list of files, and now you need to mark the files you want the regex find/replace to work on. You mark a file by moving the cursor to the file you want, then press m. Unmark it by pressing u. (To list subdirectories, move your cursor to the directory and press i. The sub-directory's content will be listed at the bottom.) To mark all files by a regex, type 【% m】, then type your regex pattern. For example, if you want to mark all HTML files, then type 【% m】 then .html$. (You can find a list of the mark commands in the graphical menu “Mark” (this menu appears when you are in the dired mode).)
Selecting Files in a Directory and All Its Sub-Directories
If you want to do find/replace on files inside a directory, including hundreds of subdirectories, here's a method to select all these files.
Call find-dired. (you call a command by pressing 【Alt+x】) Then, type a directory name, ⁖ /Users/mary/myfiles
Note: if you are using emacs on a unix non-graphical text terminal, and if 【Alt+x】 does not work, the equivalent key stroke is 【Esc x】.
Emacs will ask you with the prompt “Run find (with args): ”. If you need to do the replacement on all HTML files, then type -name "*html". If you don't care about what kind of file but simply all files under that dir, then give “-type f”.
Now, mark the files as described above.
Interactive Find/Replace
Now, you are ready to do the interactive find replace. For simplicity, let's say you just want to replace the word “quick” by “super”. Now, call dired-do-query-replace-regexp. It will prompt you for the regex string and the replacement string. Type “quick”, enter, then “super”.
Now, emacs will use your pattern and check the files, and stop and show you whenever a match occurred. When this happens, emacs will prompt you, and you have a choice of making the change or skip the change. To make the change, type y. To skip, type n. If you simply want emacs to go ahead and make all such changes to the current file, type !.
If you want to cancel the whole operation without saving any changes you've made, type 【Ctrl+g】, then exit emacs using the menu 〖File ▸ Exit Emacs〗.
Saving the Changed Files
Now, after you went through the above ordeal, there is one more step you need to do, and that is saving the changed files.
If you are using emacs version 22 or later, then call ibuffer to go into a buffer listing mode, then type 【* u】 to mark all unsaved files, then type S to save them all. (that's shift-s)
If you are using a emacs version 21, then you can do this: call list-buffers, then move the cursor to the file you want to save and type s. It will mark the file for later save action. Type u to unmark. Once you are done, type x to execute the saving of all files marked for save. (in emacs, opened file is called “buffer”. Disregard other things there.)
Alternative to the above options, you can also call save-some-buffers 【Ctrl+x s】. Then emacs will display each unsaved file and ask if you want it saved.
Note: emacs's regex is not the same as Perl or Python's, but similar. For a summary and common patterns, see: Emacs Regex.
Using dired to recurse down a deep directory tree is going to be a bit slow for this task. You might consider using tags-query-replace. This does mean shelling out to create a tags table, but that is often useful anyway, and it's quick.
For open buffers, this is what I do :
(defun px-query-replace-in-open-buffers (arg1 arg2)
"query-replace in all open files"
(interactive "sRegexp:\nsReplace with:")
(mapcar
(lambda (x)
(find-file x)
(save-excursion
(goto-char (point-min))
(query-replace-regexp arg1 arg2)))
(delq
nil
(mapcar
(lambda (x)
(buffer-file-name x))
(buffer-list)))))
M-X Dired, and t to mark all files, and Q to query replace text in all of them.
You can expand a sub directory by using the i command before the query-replace.
They key info I'm adding is that if you give a prefix (control-u) to the i command,
it will prompt you for arg, and -R argument will recursively expand all subdirs
into the dired buffer. So now you can query-search every file in an entire directory.
Another option is to use Icicles search. This is a different kind of incremental search that uses completion of your minibuffer input against search hits. As you modify your current input the set of matching hits is updated in buffer *Completions*.
You can search any number of files, buffers, or bookmarked locations, which you can choose using minibuffer pattern (e.g. regexp) matching.
When you visit a search hit you can replace on demand either the entire hit or just the part of it that matches your current minibuffer input. Replacement on demand means you are not queried about each search hit in turn; you access the hits you want directly, in any order. This approach can be more effective than query-replace if you have a limited number of replacements to make: you skip the exhaustive y/n prompting.
Search is over search contexts that you define -- you are not limited to searching all of the text in the target files (e.g., you can skip comments or particular kinds of program sections). A simple example of a search context is a line, as in grep, but a context can be any pattern-matched block of text you like. Typically you define the search contexts using a regexp, but you can instead use a function. In addition to defining your own, there are predefined Icicles search commands for different kinds of contexts: blocks of text properties or overlay properties, thing-at-point things, etc.
You can also sort the search hits in various sort orders for easier access/navigation.
find-name-dired is OK, but:
All of the files you get match the same, single regexp.
find-dired is more flexible in that regard, but it too is made for using general rules (even if they can be arbitrarily complex). And of course find has its own, complex language.
if you then want to act on only some of the files whose names were collected in the find(-name)-dired buffer, you need to either mark them or delete/omit the lines of those you do not want to act on.
An alternative is to use Dired+ commands that act on (a) the marked files and (b) all marked files (or all files, if none are marked) in the marked subdirectories ... found recursively. This gives you both generality and easy control over file choice. These "here-and-below" commands are all on prefix key M-+ in Dired mode.
For example, M-+ Q is the same as Q --- query-replace, but the target files are all of those marked in the current dir and in any marked subdirs, down, down, down...
Yes, an alternative to using such here-and-below commands is to insert all subdirs and their subdirs, recursively, and then use a top-level command such as Q. But it can often be convenient not to bother with inserted subdirs.
And to do that you anyway need a quick way to insert all such subdirs recursively. Here too, Dired+ can help. M-+ M-i inserts all marked subdirs and their own marked subdirs, recursively. That is, it is like M-i (which inserts the marked subdirs in Dired+), but it acts recursively on subdirs.
(All such "here-and-below" Dired+ commands are on menu Multiple > Marked Here and Below.)
You can also perform Dired operations on an Emacs fileset, which is a saved set of names of files located anywhere. And if you use Icicles then you can open a Dired buffer for just the files in a fileset or other types of saved file lists.
You can also bookmark any Dired buffer, including one that you create using find(-name)-dired. This gives you a quick way to return to such a set (e.g. a project set) later. And if you use Bookmark+ then bookmarking a Dired buffer records (a) its ls switches, (b) which files are marked, (c) which subdirectories are inserted, and (d) which (sub)directories are hidden. All of that is restored when you "jump" to the bookmark. Bookmark+ also lets you bookmark an entire tree of Dired buffers --- jumping to the bookmark restores all of the buffers in the tree.
M-x project-query-replace-regexp RET (make sure your files are saved!).
M-x rgrep RET then query-replace within each buffer. Nice because you can keep track of all occurrences, and because it let's you limit search to certain extensions.
I would like to suggest one more great tool which has not been mentioned yet, namely Helm.
It is a great replacement for many standard Emacs operations involving completion, searching etc. In particular, helm-find-files allows for performing query replace (including regexp) within multiple selected files.
Just open helm-find-files, mark the relevant files with M-SPC and then use F6 or F7 to run query replace or query replace regexp in the selected files.
It's not Emacs, but xxdiff comes with a tool called xx-rename which will do that for multiple strings at a time (e.g. From To from to FROM TO), with interactive prompting, save backups of all the modified files, and produce a short log of changes made with context. That's what I tend to use when I do large/global renamings.