Bulk-reverse the order of headers in Emacs org-mode? - emacs

Is there a way to bulk-reverse the order of headings in Emacs org-mode?
I'd like to change this kind of list (but much bigger, not just two or three items, thus "bulk"):
* personal computer
* Windows
* Mac
* Linux
* StackExchange
* stackoverflow
* countries
* people
to this:
* people
* countries
* StackExchange
* stackoverflow
* personal computer
* Windows
* Mac
* Linux

Select the whole buffer and use M-x org-sort-entries RET F point RET
org-sort-entries sorts all items at a certain level in the tree. 'F' tells it that you want to sort in reverse order according to a function that you specify. Using the function point gives each entry a value corresponding to buffer position on which to sort.
To do the same thing from elisp, the equivalent function call is (org-sort-entries nil ?F 'point)

C-c ^ to invoke org-sort, which will Call org-sort-entries, org-table-sort-lines or org-sort-list accordingly.
Then you can either:
f point RET > RET
F point RET < RET
I don't know the org-sort-entries' api changed or what, when I followed #clatter's answer, it asked me for a compactor. So here is an answer basing on his.

Sure, M-S-<up> and M-S-<down> move subtrees up and down inside their common parent. There are a bunch of other similar commands as well; check out the info page for a list of them (M-x info "(org) Structure Editing").
Edit: Actually reversing the order of subheadings is not something you can do out of the box, I think. You might be able to do it by calling sort-subr with cleverly chosen arguments, however.

Related

Jumping to a specified window in Emacs doesn't work when done programatically

I am trying to select the third window (I have opened 6 in my frame) in Emacs.
The problem is that this:
(select-window 3)
doesn't work.
It throws this error in debugger:
(wrong-type-argument window-live-p 3)
How to make it work?
select-window takes a window object as argument, not a number. The function window-list gives you a list of window objects in the current frame. You can select one of them to pass to select-window, e.g.
(select-window (nth 2 (window-list)))
will select the window that is the third[1] element of the list that window-list returns.
Do C-h f nth and C-h f window-list for more information on these functions.
EDIT: in response to the comment...
What I know about Windows (which is not much), I've learned by reading parts of the Windows chapter in the Emacs Lisp Reference manual.
Do NOT expect the window number to correspond to anything that you think as "first" or "second" or ...
When you destroy windows and then create new ones, the numbers are going to change unpredictably (at least AFAIK), e.g. I had three windows with numbers 51, 83 and 99. I destroyed window #99 and created a new one: now window-list tells me I have windows with numbers 51, 105 and 83 in that order. I believe (but I may be wrong) that Emacs uses the list returned by window-list in order to implement other-window (C-x o), so the order of the list that window-list returns depends on what your selected window is currently: that' always the first element of the list, so the window you'll select with C-x o is the second element of the current list, but when you get there and do ESC ESC : (window-list), the returned list will have changed - it will be rotated so that where you are is the first element and where you came from is now the last element. See Cyclic Ordering of Windows in the Emacs Lisp Reference manual.
You are correct that the upper case WINDOW in the function description is a hint of its type. For typographic conventions, see the section Conventions in the Emacs Lisp Reference manual and in particular, the subsection A Sample Function Description.
[1] Note that list elements are indexed from 0.

emacs kill multiple buffers by using regex [duplicate]

This question already has answers here:
Killing buffers whose names start with a particular string
(6 answers)
Closed 5 years ago.
I want to kill all the buffers which start with info, I typed C-x-k info*<*>, it shows "no match".
So I know that I cannot use * to stand for all characters.
What is the correct expression it should be?
Any help would be grateful.
This is my screenshot. Please have a look.
emacs buffer list window
M-x kill-matching-buffers is a standard command for this.
How do I answer y automatically (kill-matching-buffers asks if I should kill a modified buffer)? may also be of interest (although I'm not actually recommending doing that in general).
So I know that I cannot use * to stand for all characters.
.* will match zero or more non-newline characters, but you don't need to use that here, as kill-matching-buffers command does not require a match on the entire name. ^info would match your (stated) requirement.
I encourage you to read C-hig (emacs) Regexps to learn about the syntax of regular expressions in Emacs.
Your buffers have names *info* and *info*<N>, N = 3..16.
C-x k *info TAB TAB shows you all matches for the buffers you want to kill. But C-x k lets you kill only one at a time.
If you use library Icicles then C-x k is a multi-command, which means that it can kill multiple buffers, individually or together. (And Icicles lets you pattern-match using regular expressions. But you don't need that here.)
In this case, you just do C-x k *info TAB C-!, to kill all of the *info buffers.
C-! applies the action to all objects that match your current input pattern.

How to completely erase Emacs Calc's state?

This has got to be a stupid simple question, but I haven't been able to find an answer despite searching Google multiple times in multiple ways, and digging through the Calc documentation.
GIVEN an instance of Emacs, with Calc running, and having performed multiple calculations including storing variables and equations, some of which may have come from the "~/.emacs.d/calc.el" file,
HOW do you return Calc to a pristine state without restarting Emacs?
Pristine: Nothing on the stack. Nothing in the trail. No stored variables or equations. Etc.
M-x calc-reset, which is also bound to C-x * 0, is what you need. From the info manual:
The C-x * 0' command ('calc-reset'; that's 'C-x *' followed by a
zero) resets the Calculator to its initial state. This clears the
stack, resets all the modes to their initial values (the values that
were saved withm m' (calc-save-modes')), clears the caches (*note
Caches::), and so on. (It does _not_ erase the values of any
variables.) With an argument of 0, Calc will be reset to its default
state; namely, the modes will be given their default values. With a
positive prefix argument,C-x * 0' preserves the contents of the stack
but resets everything else to its initial state; with a negative prefix
argument, `C-x * 0' preserves the contents of the stack but resets
everything else to its default state.
EDIT: Oops. Even that doesn't clear variables. I'm not sure if there is a straightforward way to get all the way back to pristine :(
EDIT 2: It looks like Calc stores all variables, including 'built-ins' like pi and e, as global variables with the prefix 'var-'. As far as I can tell, it doesn't keep track of which variables were set by the mode (like pi), and which were set by users. Furthermore, the default user variables are stored as var-q0, var-q1 etc. So in order to clear out all the variables, you'd need to compile a list of variables and states present at startup, erase everything not in that list, and then restore the original values of the variables in that list. That's certainly possible, but a little tedious.
Edit 3: Here's my attempt. I took another look at calc-mode, and at start up it defines the variables I've added to my-calc-builtin-vars below. The second line will remove all variables in Emacs that start with the prefix 'var-' and are not in this list. This will include any variables defined by you, or in another package. So let's hope no-one else uses the prefix 'var-'. And it will not reset the value of the built-in variables, so if you have redefined pi to 3, it will remain 3.
(setq my-calc-builtin-vars
'("var-nan" "var-uinf" "var-sym" "var-lines" "var-Modes"
"var-EvalRules" "var-inf" "var-phi" "var-pi" "var-gamma" "var-π"
"var-φ" "var-γ" "var-spec" "var-e" "var-i"))
(defun really-reset-calc ()
(interactive)
(calc-reset nil)
(mapc #'(lambda (el) (unintern el))
(remove nil (mapcar
#'(lambda (el) (unless (member el my-calc-builtin-vars) el))
(all-completions "var-" obarray)))))
UPDATE: August 6, 2016
Current built-in vars list:
(setq my-calc-builtin-vars
'("var-CommuteRules" "var-Decls" "var-DistribRules" "var-EvalRules"
"var-FactorRules" "var-FitRules" "var-Holidays" "var-IntegAfterRules"
"var-IntegLimit" "var-InvertRules" "var-JumpRules" "var-MergeRules"
"var-Modes" "var-NegateRules" "var-e" "var-gamma" "var-i" "var-phi"
"var-pi" "var-γ" "var-π" "var-φ"))
M-# 0
That should be all you need to do.

Emacs: help me understand file/buffer management

I've been using emacs for all of my text editing needs for the past three years now. When I'm in a single file, working on code or whatnot, I'm fairly efficient. With two files, I can "C-x b RET" between them and I do fine. However, whenever I need to be working on more than two files at a time, I tend to get lost.
Here are some of the problems that I'd like to work on:
I forget what some of my buffers are called, but I don't understand why C-x C-b splits my window into two buffers and exits the mini buffer. Sure I can switch buffers and choose a buffer to visit, but this feels unintuitive, and leaves me with two buffers open.
When I visit a directory rather than a file, I have a convenient list of all of the files and directories. I usually want to do one of two things with this: 1) Open a single file and never see this buffer again OR 2) Open a bunch of files and never see this buffer again. I don't really know how to do this, as moving the point to a file and hitting return doesn't do either of these things.
I know that my buffers aren't like tabs, but I have an inclination to want to scroll through them to find what I want. I don't know of any key-bindings for this, but I'd like it to be M-n / M-p or the like. Then again, this may be a horribly inefficient way to switch buffers.
When I open interactive help of any kind (for example in ESS), I have a habit of switching back to the buffer I was working in and using C-x 1 to get back to a single buffer. When I do this, however, the help buffer hangs around in my buffer list, further confusing me. I know I can switch to that buffer, kill it, switch back, and then go back to a single buffer, but this seems wrong.
The way I've dealt with this so far involves using a tiling window manager and a few emacs windows in different work-spaces, rather than actually learn the best way to manage a number of files in emacs. I don't necessarily want to change emacs to better fit my needs (although I am open to that if it fits in with what I'm about to say), instead, I'd like to grok the thought process behind handling files/buffers the way that emacs does, and how I can be more efficient with it.
Any answer that would help me understand the correct way, or a more efficient way to manage my buffers or files would be greatly appreciated.
Bind C-x C-b to ibuffer. This is a better buffer listing facility with many advanced features, and its default behaviour is to replace the current buffer with the buffer listing, and then bury the listing when you select a buffer (leaving you with the newly-selected buffer in place of the original one).
You can simply use C-x b to enter your selection in the mini-buffer, of course; however the tab-completion (which is needed to make this a viable option, IMO) does open a new window temporarily, at which point I think you might as well familiarise yourself with something with more features.
Use a instead of RET when selecting from dired. This kills the dired buffer instead of leaving it behind. C-h m in any buffer will show you the help for its major mode (followed by help for the minor modes), and you can read about all the available dired key bindings there.
http://www.emacswiki.org/cgi-bin/wiki/TabBarMode ? (edit: I prefer Rémi's answer for this one, but TabBarMode would give you the visual tab element if you were particularly keen on that.)
q is bound to a 'quit' function in a great many major modes. Generally it buries the buffer rather than killing it, but I certainly find that fine.
To elaborate a little on #1, ibuffer has lots of nice features, and M-x customize-group ibuffer RET will give you some idea of how you can customise it to your liking.
Furthermore, you can filter the buffer list by many criteria (again, use C-h m to see its help page), and then generate a 'group' definition from the current filters, and save your custom filters and groups for future usage.
For example:
/ f ^/var/www/ RET: filter buffer list to show only filenames starting with /var/www/.
/ s Web filters RET: name and save active filter set to your init file.
/ g Web development RET: create a named group from the active filters.
/ S My groups RET: name and save group definitions to your init file.
/ r Web filters RET: invoke the "Web filters" filters.
/ R My groups RET: invoke the "My groups" groups.
RET on a group name to collapse or expand it.
C-k and C-y to kill and yank groups, to re-arrange them.
C-h m for more information...
This way you can have a single Emacs instance running, and create filters and groups for different types of task, and easily switch between them.
I think you will really enjoy Ido for dealing with multiple buffers who's names you can't exactly remember. When you type C-x b it shows a list of open buffers in most used order. As you type some of the characters in a buffer name the list is filtered. The characters you type don't have to be at the begging of the name or contiguous. Using C-f, C-b or left/right arrow keys cycles through the buffer choices.
Also see Smex for Ido like functionality for M-x
Closing windows is done with C-x 0. Intentionally splitting the window is done with C-x 2 for horizontal, C-x 3 for vertical. I love this feature, since it allows me to have test and production code visible at the same time. C-x o takes me to the other window.
I use C-x right (or C-x C-right) and C-x left (or C-x C-right) to go to the next and previous buffer. I don't mind anymore off the few buffer that lay around in Emacs but you could use k in the buffer list to kill the buffer you don't use anymore.
You can also try Iswitchb mode which provides auto-completion for buffer names when you switch buffers via C-x b.
To activate:
M-x iswitchb-mode
Or add to your .emacs file:
(iswitchb-mode)
It is similar to Ido mode for buffer switching but a bit more lightweight.
Also, if you want a more customizable listing of your buffers then use M-x bs-show as an alternative to C-x C-b. In that buffer type ? to get a list of actions you can perform.
I think the number one most useful extension for flipping through buffers is Anything. It lets you start typing part of a buffer (or file!) name and it will figure out what you want. I've rebound C-x b to anything-for-buffers. It makes life so much better.
As always, there are many ways to help you with this; it depends a bit on personal preference what works best, here are some links with explanations:
ibuffer; which is an updated buffer menu (C-x C-b)
ido, which let's you have more powerful autocompletion to switch through buffers. It's a kind-of 'better iswitchb'.
These two are enough for me; but you may also be interested in the tabbar-mode, which gives you rudimentary tabs (like firefox has them).

Emacs c-mode fill-paragraph with Doxygen Comments

I have a question that is very similar to Getting Emacs fill-paragraph to play nice with javadoc-like comments, but I wasn't sure if I would get many answers in a year old thread.
Anyhow, I have C code that has some Doxygen comments that look like the following:
/**
* Description
*
* #param[in,out] var1 : <Long description that needs to be wrapped.>
* #param[in,out] var2 : <Description2>
*/
Now, when I use M-q in emacs, I want the following:
/**
* Description
*
* #param[in,out] var1 : <Long description that needs
* to be wrapped.>
* #param[in,out] var2 : <Description2>
*/
But, current I get the following:
/**
* Description
*
* #param[in,out] var1 : <Long description that needs
* to be wrapped.> #param[in,out] var2 : <Description2>
*/
Doing some research, it looked like I needed to set the paragraph-start variable in emacs to recognize the "#param." I found another question on stack overflow (Getting Emacs fill-paragraph to play nice with javadoc-like comments), that had a sample regular expression. I modified it a bit to fit my requirements, and I tested it inside of Search->Regex Forward, and it highlighted each #param sentence correctly.
I used the following regular expression "^\s-*\*\s-*\(#param\).*$"
So, I tried setting the given regular expression as my paragraph-start (with the added \'s required for the elisp syntax) in my .emacs file. When I opened a new emacs window and tried out the M-q, the same error was occurring. Is there something I am missing? Is M-q used differently in c-mode? Should I check my .emacs file for something that may be causing an error here? Any help would be appreciated.
Thanks,
Ryan
Regarding your question, "Is M-q used differently in c-mode?", describe-key (bound to C-h k) is your friend. While visiting the buffer with the C file, type C-h k M-q and it will tell you exactly what function M-q is bound to. In this case, it is c-fill-paragraph, which ultimately uses paragraph-start, the variable you found in that other question.
I found that this regular expression used as paragraph-start will wrap lines and treat each #param as a new paragraph:
"^[ ]*\\(//+\\|\\**\\)[ ]*\\([ ]*$\\|#param\\)\\|^\f"
However, it will not indent the wrappedlines as you want. It will make your example look like this:
/**
* Description
*
* #param[in,out] var1 : <Long description that needs
* to be wrapped.>
* #param[in,out] var2 : <Description2>
*/
I hope it still works better for you. Let me know if you figure out the indenting.