I'm using clojure-mode and clojure.test/with-test, which looks like this:
(with-test
(def... some expression defining a var)
;; comment for a test
(is (= 0 (- 1 1)))
(is ...))
Where the expressions following the first one in (with-test ...) are test cases for the first form. This is useful since it keeps tests close to their relevant code.
However, the list of test cases may be pretty long, obscuring the rest of the "real" code. I would love to have some way of hiding all of the test code in a buffer. Something like outline/org-mode's folding abilities:
(with-test
(def some expression defining a var)
...)
Or even
(def some expression defining a var)
with some kind of marker to show there are hidden test cases.
The tricky part about this is that the (def...) form that I want to focus on is inside the (with-test ...) form that I want to hide.
I've looked around and as far as I can see I won't be able to make this work with outline-mode or hideshow-mode. I'm assuming I need some folding library that is expression-aware to get something like this to work. Does something like this already exist, am I thinking about this the wrong way, do you have any tips?
Update
I was able to make it work with hideshow. See the hs-hide-all-clojure-tests in my emacs config repo
I believe, that you can use the HideShow package for this task, but you'll need to add some configuration so it will hide only tests...
Related
The TLDR is the code:
(while (?????)
(org-next-visible-heading 1)
(org-todo 'todo)
)
What should I put in the condition ????? in order to stop at the end of the file, or after the last visible heading?
The longer context:
I'm using trying to use elisp to modify org-mode files for emacs. I'm new to elisp and finding it hard to find the relevant documentation for some basic things.
I want to iterate over the headings in an org-mode buffer and modify the status (i.e. todo) keyword. I've seen it suggested that if I want to modify the buffer, it is better not to use org-element-map but instead to use something like org-next-visible-heading or org-forward-same-level, and at each point use org-todo to modify the heading.
I know how to write a while loop, but I don't know how to write the condition as I haven't been able to find out how to detect if I'm at the end of the buffer, even though this seems to be a trivial thing to want to do.
I'm happy to be told there is a better approach to my problem but I would still like to know how to detect the end of a buffer in general.
Rather than looping, you can use org-map-entries. Here is an example, pretty much from the doc, that marks each headline as a TODO item:
(org-map-entries '(org-todo "TODO") t 'file 'archive 'comment)
It specifies a function to apply to each headline: (org-todo "TODO"); no tag or property matching; file scope; and skips archive and comment headlines.
Read the linked doc and also do C-h f org-map-entries, but don't get too scared by the doc string of the function: org-map-entries is very powerful, but it will take some time to understand how to do things with it and harness its power.
Just FYI, I am new to the .emacs file.
I would lik to set up my .emacs file to auto-indent and auto-pair a certain way to make writing code a little faster. I have found some info as to how to do these things independently but I'm not sure how to put it all together for the emacs version that I have. Ultimately, I would like to set up these definitions specific to which ever language I am coding in. Just to get me started I will use java as an example.
Obviously auto-pairing for ", (, ' are pretty straigforward. I would just like it to auto insert a closing ", ), ' and place the cursor in the middle.
For {, I would like it auto insert two newlines and the closing } whith the cursor in the middle.
Example
while (true) {
<--- cursor would be here with auto-indent of 2 spaces
}
I would also like this to work for nested curly braces which the appropriate indentation.
Example
while(true) {
if (...) {
}
}
Here is what I have so far in my .emacs file:
(defun java-autoindent ()
(when (and (eq major-mode 'java-mode) (looking-back "[{;]"))
(newline-and-indent)))
(add-hook 'post-self-insert-hook 'java-autoindent)
Obviously this just inserts a line and auto indents, but I also want the closed } to be included on the line below. I also tried using electric-pair but that didn't work.
My wish list may be a little unrealistic. I'm not even sure that this is possible, but I would be happy with the closest that I could get.
Any help to get me going in the right direction would be greatly appreciated.
Emacs defines modes for each type of language you code in. Some modes are derived from others and there is a mode called prog-mode which most programming modes are derived from.
The mode for a language is where things like indentation are defined because these tend to be language specific. The rules for indentation can be quite complicated, which is why people often use a mode with similar indentation style as the parent and derive a new mode from that.
Have a look at modes and derived mode in the emacs elisp manual.
With respect to adding matching/closing delimiters, have a look at electric-pair-mode (I think it was in emacs 24.4 - I'm running 25 and forget when it was introduced).
With respect to your requirement to enter some code, some newlines and position the cursor in a specific place, you probably want to look at one of emacs' template solutions. yasnippet is a popular choice and it is easy to define new templates in it. There are also many existing packaged yasnippet templates you can download/install. If you don't like yasnippet, google emacs template and have a look there are quitre a few frameworks.
Let's say I want to create a new document, and cycle quickly through a list.
If it's only for one "word", I think there should be a general way to do this.
For instance:
"blue orange red yellow black white"
Does anyone know a way how to cycle through those items when I create:
\begin{orange}
... and I want to press a key to cycle through this list, replacing orange with the next item on the list (doing this procedure in the opposite direction wouldn't be hard then)?
I tried many different ideas with macro's (placing the list on the top of the document, and doing a whole bunch of i-searches), but that doesn't cut it.
I'd be willing to put the list in an elisp file, though I have no clue how to use that variable from elisp in, let's say, a LaTeX document (.tex).
Well, this might be possible, but depends on how much effort you are willing to put into writing eLisp code to make it work. It's not possible by just some configuration option. I would be looking into extending autocomplete by adding new sources to it, something like:
(defvar tex-tag-ac-sources
'((init . tex-tag-ac-init)
(requires . 0)
(candidates . tex-tag-ac-candidates)
(document . tex-tag-ac-documentation)
(match . tex-tag-completion-filter)
(prefix . tex-tag-ac-prefix-matcher)
(symbol . "s"))
"The source generator for autocompletion needed for interaction
with auto-complete")
Where tex-tag-ac-candidates, tex-tag-ac-documentation, tex-tag-completion-filter and tex-tag-ac-prefix-matcher are function that do autocompletion. I.e. init function is called once when the autocompletion process starts for a specified prefix. It's called w/o arguments. The candidates is the function that is responsible for showing the filtered list of candidates, it's called w/o arguments, you would filter the candidates in the filter function, it is called with the prefix collected so far and the list of candidates so far. Lastly, the matcher function is invoked on the text of the file to see if the completion is needed at point. So, if it returns t, the init is called, and then loops through filter-candidates as you type.
While this is a bit involved... you'd definitely have a completion for anything you want. Obviously, if those functions in source are defined by you, then, if you wanted to, you could read completion arguments dynamically or have them generated dynamically in some way.
Ah, you would add the sources to autocomplete by something like:
(auto-complete (list tex-tag-ac-sources))
if doing it on per call basis, or
(setq ac-sources (list tex-tag-ac-sources <other sources>))
You can find more info here: http://cx4a.org/software/auto-complete/manual.html#Using_Source
EDIT: I translated the macro into a function.
Here is a way I did it. I created a file called "list.list" where my "lists" are saved. I saved the LaTeX templates for Beamer in there. I inserted them like this:
Antibes Bergen Berkeley Berlin ..... Antibes
Note that you should always put the first entry in twice to allow it to loop.
Here is the code:
(defun cycle-list-word ()
(interactive)
(right-word)
(backward-kill-word 1)
(find-file "/emacs-24.1/list.list")
(search-forward (substring-no-properties (car kill-ring)) nil t)
(right-word)
(backward-kill-word 1)
(bury-buffer)
(yank)
)
I have wrote simple macro to define interactive function and bind it to key at same time
(That what my previous question related)
The only thing, that annoy me, that it looks ugly without highlighting.
It called such way:
(define-and-bind-command foo "C-x £" (message "Hello world"))
I want define-and-bind-command and foo be highlighted.
Well, to get define-and-bind-command highlighted, I can use wrapper around defmacro,
altho is not so pretty, and I have no idea about foo.
I know it is possible, because argument of require is highlighted with const face.
Or, probably, I am inventing wheel, and there is an another lisp mode with more
advanced highlighting?
highlighting the 'define-and-bind-command' can be achieved very easily using 'font-lock-add-keywords', usage would be
(defun my-elisp-mode-keywords()
(font-lock-add-keywords nil
'(
("\\<\\(define-and-bind-command\\)" . 'font-lock-keyword-face)
)
)
)
Of course you could change the 'font-lock-keyword-face' to any face of your liking or create and use your own face. You can find the name for a face already used very easy by moving the point (cursor) over the font-locked part of text and entering 'describe-face' followed by a return.
Edit2: Oh and of course you need to hook that defun to some hook, for elisp mode that would be:
(add-hook 'emacs-lisp-mode-hook 'my-elisp-mode-keywords)
Highlighting the foo part can be done using a regexp.
Unfortunately I can not yet help you with that part because I'm not sure how to match a regexp containing the 'define-and-bind-command' but highlighting only the word after.
Edit: unless you want to highlight both the 'define-and-bind-command' and the foo part in the same face, then it should be very easy. But I guess you want them to have different faces?
Edit3: Corrected my code, stackoverflow ate some braces before...
Edit4: Okay, I have a solution for matching the second part only, I have not tested it extensively but it seems to work. I have allowed for foo to contains any character but a space (and newline I think), I guess one could restrict that event futher to a-zA-z0-9 and "-", so feel free to change that to your liking. The Code responsible for matching only the foo part is
("\\bdefine-and-bind-command\s\\([^\s]*\\)" 1 'font-lock-function-name-face t)
Insert that in the line after the other font-lock keyword in the above function and you're good to go. The regexp matches 'define-and-bind-command' which must begin a word (that's the \b for) followed by a space and then it returns (font-locks) everything followed until a space exists.
Hope this helps!
font-lock-add-keywords can take a symbol, the mode to which to apply the new keywords. So you could do
(font-lock-add-keywords
'emacs-lisp-mode
'(("\\<\\(define-and-bind-command\\)" . 'font-lock-keyword-face)))
The disadvantage compared to using a function and a hook is that this doesn't work for derived modes; that is, if you have a mode which is derived from emacs-lisp-mode, it will not inherit these keywords.
Is there a way to display what the docs call level 2 hidden files in the speedbar? E.g. .emacs
Long Answer
It turns out that the unshown files are controlled by a regular expression
Which is usually "^\\(\\..*\\)\\'", which I think means everything that starts with a dot
Setting that regexp with:
(setq speedbar-directory-unshown-regexp "^$")
does the trick of showing everything.
While looking at this, I discovered that there is a bug in the default value of
speedbar-directory-unshown-regexp causing the problem. It should be redefined like this:
(setq speedbar-directory-unshown-regexp "^\\(CVS\\|RCS\\|SCCS\\|\\.\\.*$\\)\\'")
or to whatever you think makes the most sense.