emacs tabbar customisation, making unsaved changes visible - emacs

i want to mark buffers, that have unsaved changes, in the tabbar tab bar in emacs. Also i want to assign F1 - F12 to the buffer and open them by pressing the according button.
But since i have no programming practice in lisp, i have no idea where to start. Also i got the feeling that learning lisp to know how to configure emacs on a low lwevel is an incredible usefull skill.
So my question is not really how to archieve that, but rather where to start looking. And maybe how to start learning Lisp.
Thanks

This following code works with stock versions of Emacs and tabbar.el -- it creates + sign at the beginning of the modified buffer name in the tab.
;; BUFFER MODIFICATION STATE INDICATOR
(defadvice tabbar-buffer-tab-label (after fixup_tab_label_space_and_flag activate)
(setq ad-return-value
(if (and (buffer-modified-p (tabbar-tab-value tab))
(buffer-file-name (tabbar-tab-value tab)))
(concat " + " (concat ad-return-value " "))
(concat " " (concat ad-return-value " ")))))
(defun ztl-modification-state-change ()
(tabbar-set-template tabbar-current-tabset nil)
(tabbar-display-update))
(defun ztl-on-buffer-modification ()
(set-buffer-modified-p t)
(ztl-modification-state-change))
(add-hook 'after-save-hook 'ztl-modification-state-change)
(add-hook 'first-change-hook 'ztl-on-buffer-modification)
If you want to take it one step further, look at the source code for aquamacs-tabbar.el -- it contains customizable options such as tabbar-unselected-modified and tabbar-selected-modified. You would either need to use Aquamacs for the options mentioned above, or you would need to make a few revisions to the following files so that they work with a stock version of Emacs: aquamacs-tabbar.el, tabbar.el, and tabbar-window.el:
https://github.com/davidswelt/aquamacs-emacs/tree/master/aquamacs/src/site-lisp/tabbar
See also this example of possibilities to further customize the look and feel.
(source: lawlist.com)

As sds mentioned, you'll have to go through the info pages. You could do C-h i m Emacs Lisp RET as mentioned by sds, or you could do M-x info and then find the Emacs Lisp Intro from there.

You raised too many issues in a single question.
I will respond briefly to each, but you should ask a separate question if you are not clear.
i want to mark buffers, that have unsaved changes, in the tabbar tab bar in emacs.
No, you do not want that.
This is already done in the mode line.
i want to assign F1 - F12 to the buffer and open them by pressing the according button
No, you do not want that.
Keys are precious, you do not want to waste them like that.
Use Mouse Buffers menu or list-buffers.
how to start learning Lisp
In Emacs, type C-h i m Emacs Lisp Intro RET and start reading.
Emacs is superbly customizable, and you can make it do exactly what you want.
However, you must realize that it has been used for over 30 years by many smart people, so, whenever your wish is reasonable, chances are this can be done out of the box, and if not, you will have a lot of fun implementing it yourself.

You can also look at tabbar.el to try and figure out where the actual text of the bar is created (i.e. where you'll want to add the "unsaved" indicators). To figure out if a buffer has unsaved changes, you can use the buffer-modified-p function.

Related

How to control which window to be used for new contents in Emacs?

Often I have multiples windows carefully arranged within a frame.
However, certain command, say M-x man RET will grab one of the visible windows to display its own content. Sometimes this is annoying because the window that was taken away is one that I need to keep it visible.
e.g. I have 3 windows on screen, one useful source-code window and two useless windows. I want to keep the soure-code window visible while checking the man page. But very often Emacs just take away the code-window for the newly opened man page.
One way I can think of is to display the (chronological) open order of each window, so that I can focus point on n-th window and be confident that Emacs will grab (n+1)-th window for new content.
Is there a way to display such order, e.g. in the mode-line of each window?
Or is there another way for better control for displaying new window?
A little late to the party but as discussed in the comments, using dedicated windows is a good way to control where new content is displayed (+1 to #lawlist for bringing it up and to #phils for mentioning toggling!).
I'm pretty sure you'd be able to implement a command that toggles dedicatedness yourself at this point, but since I have the code for this handy I'll share it anyway:
(defun toggle-window-dedicated ()
"Control whether or not Emacs is allowed to display another
buffer in current window."
(interactive)
(message
(if (let (window (get-buffer-window (current-buffer)))
; set-window-dedicated-p returns FLAG that was passed as
; second argument, thus can be used as COND for if:
(set-window-dedicated-p window (not (window-dedicated-p window))))
"%s: Can't touch this!"
"%s is up for grabs.")
(current-buffer)))
(global-set-key (kbd "C-c d") 'toggle-window-dedicated)
Now, in a multi-window setup you can simply press C-c d in each window you would like to "protect".
I'll throw this example usage of display-buffer-alist into the mix, given that it was mentioned in the comments, and is indeed a general mechanism for controlling how and where buffers are displayed (even though, as Drew indicates, it is far from trivial).
Start with the help for the variable itself:
C-hv display-buffer-alist RET
From there you'll get to the help for display-buffer, and no doubt acquire some idea of the complexities involved :)
In any case, the following is an example of using a custom function to decide how to display buffers named *Buffer List*.
You can easily see that if you can write a function which can figure out how to display a buffer where you want it to, then you can use display-buffer-alist to make it happen.
(defun my-display-buffer-pop-up-same-width-window (buffer alist)
"A `display-buffer' ACTION forcing a vertical window split.
See `split-window-sensibly' and `display-buffer-pop-up-window'."
(let ((split-width-threshold nil)
(split-height-threshold 0))
(display-buffer-pop-up-window buffer alist)))
(add-to-list 'display-buffer-alist
'("\\*Buffer List\\*" my-display-buffer-pop-up-same-width-window))
A key quote regarding the ACTION functions is:
Each such FUNCTION should accept two arguments: the buffer to
display and an alist. Based on those arguments, it should
display the buffer and return the window.

emacs: control tab buffer cycling, or stack buffer cycling, similar to alt-tab between windows

I have consulted this resource: http://www.emacswiki.org/cgi-bin/wiki/ControlTABbufferCycling, and tried buffer-stack.el, which is useful, but I find the user experience slightly awkward without being able to visualize other buffers further down the stack. It is also outdated (2002), so I wonder if there is an improved version. Most other links on that page are broken or very old as well.
I am seeking a ctrl +tab stack-based buffer cycling, similar to the way alt + tab works with windows. Ideally it would include an indicator for my current location in the buffer list. I don't think my desired feature is too specific, since many IDE's already have this feature.
I would imagine a tool that is similar to how buffer cycling works in Eclipse. Pick below:
I have explored other options, but nothing seems to mimic the functionality that has already been implemented across other IDE's like Eclipse.
I am aware of ido-mode, although it does not suit my needs for rapid stack-based switching.
I am also aware of other non-stack solutions like the one below:
(global-set-key [C-tab] 'next-buffer)
(global-set-key [C-S-iso-lefttab] 'previous-buffer);Linux
(global-set-key [C-S-tab] 'previous-buffer);Windows/Linux
but I prefer a stack-based switch.
Any suggestions? Is this feature available somewhere, or even in development? Especially with an index indicator, as shown in Eclipse above. That would be awesome - I imagine it would be as a pop-up or in the mini-buffer.
Try out this snippet:
(defun ctrltab ()
"List buffers and give it focus"
(interactive)
(if (string= "*Buffer List*" (buffer-name))
;; Go to next line. Go to first line if end is reached.
(progn
(revert-buffer)
(if (>= (line-number-at-pos)
(count-lines (point-min) (point-max)))
(goto-char (point-min))
(forward-line)))
(list-buffers)
(switch-to-buffer "*Buffer List*")
(delete-other-windows)
(forward-line)))
(global-set-key [C-tab] 'ctrltab)
It's often inferior to ido-switch-buffer, but it does its job anyway.
elscreen does a little bit what you seek:
http://wikemacs.org/index.php/Elscreen
You have to create screens (tabs) on demand and you can call M-x elscreen-select-and-goto to select a screen from a list in the minibuffer.
Fortunately it is already coupled with helm: helm-elscreen. That gives a good looking and handy choice list:
fuzzy matching
scrollable list
choose actions (press TAB and choose "Change screen/Delete/Only").
However, you still have to create a screen manually (but I like it because I can organize sort of work areas -it is possible to isolate buffers per screen. A tab per buffer would be way too much + emacs creates many internal buffers so they can get on the way).
helm: https://github.com/emacs-helm/helm/wiki
ps: helm-buffers-list is close to the interface you want for switching buffers, without tabs…
I have been maintaining a personal copy of buffer-stack. It works really well -- to the extent that I haven't thought about it for years until recently when I had to fix a legacy workaround which caused a bug.
I will put my fork up on github when time allows. I'm not averse to adding some sort of index display although it does not matter much in my use-case -- helm is great to choose from a list of buffers.

Saving the org-agenda

I'd like to save the output of org-agenda to a text file, every time that the org-agenda is calculated. This way, I can use an external program (like ATNotes on windows or conky on linux), to pick up this text file and display it on my desktop.
How can I do this?
I feel like I'm raining on your parade after you went to the trouble to write this code snipped (and used a piece of around advice, too!), but actually this feature is already baked into org-mode, and documented in the manual. The command you want is org-write-agenda (C-x C-w in an agenda buffer). See the section of the org-mode info entitled "Exporting Agenda Views."
If you want to do it while you have emacs open, you can just call save-buffer on the *Agenda* buffer via M-x save-buffer (since orgmode binds C-x C-s to org-save-all-org-buffer. You could bind save-buffer to something else in the org-mode-map if you wanted.
If you want to do it via a cron, you should be able to use the snippet in this thread on the org-mode mailing list to pipe the output to a file. I've used this in the past:
emacs -batch -eval '(org-batch-agenda "a" org-agenda-ndays 7 org-agenda-include-diary nil org-agenda-files (quote ("~/org/todo.org")))' > agenda.txt
So I finally decided to open the emacs lisp manual and figure this out myself. I wrote this bit of code, which seems to be working just fine! :)
;; Save the org-agenda for display with conky
(defadvice org-todo-list (after saveorgagenda activate)
"save this output to my todo file"
(get-buffer-create "todo")
(with-current-buffer "todo"
(set-buffer-modified-p nil))
(kill-buffer "todo")
(write-file "~/todo"))
EDIT REASONS:
1) Without kill-buffer, the defadvice creates a new todo buffer on every execution of org-todo-list. This gets pretty irritating.
2) Without the get-buffer-create function, kill-buffers fails the first time since there is no buffer named todo at that time.
3) Without set-buffer-modified-p, the function will keep telling you "todo buffer is modified. Really kill it? (y or n)" which would defeat the whole purpose really.
Whew! I'm so happy I actually took the time and effort to figure this out! :D

How do I close an automatically opened window in Emacs?

This is probably a very naive Emacs question - I'm new to it.
When I'm evaluating a lisp expression, if there's an error the debugger automatically comes up in another window. If I have *scratch* and *info* open (the former for trying out lisp and the latter for reading about it), then the debugger opens up in the window that *info* was in. At the moment, I have to switch to that window, then change it back to *info*, before returning to *scratch*. (The same thing happens if I do C-x C-b for a list of buffers.) I'm guessing there has to be a way to just close that window without this long sequence of commands. Can anyone enlighten me?
At least here on my emacs (22.3), when the debugger pops up, its window becomes the active one. There, pressing q just quits the debugger, if that's what you want. At that point, it also gets out of recursive editing.
From what I understand, you want to close the buffer in the other window without moving your cursor from the current window.
I don't any existing function does that, so I rolled my own.
(defun other-window-kill-buffer ()
"Kill the buffer in the other window"
(interactive)
;; Window selection is used because point goes to a different window
;; if more than 2 windows are present
(let ((win-curr (selected-window))
(win-other (next-window)))
(select-window win-other)
(kill-this-buffer)
(select-window win-curr)))
You can bind it to something like "C-x K" or some other somewhat difficult-to-press key so you won't press it by mistake.
(global-set-key (kbd "C-x K") 'other-window-kill-buffer)
I use this a LOT! (for Help buffers, Compilation buffers, Grep buffers, and just plain old buffers I want to close now, without moving the point)
I'm usually using the delete-other-windows command. C-x 1.
It's so regullar, that I rebinded to F4.
Official docs: https://www.gnu.org/software/emacs/manual/html_node/emacs/Change-Window.html#Change-Window
HTH
Using winner mode, you can use the keybinding C-C left arrow to return to the previous window configuration. Of course, this doesn't actually kill the new buffer, but it does hide it.
I think you're looking for C-x 4 C-o, which displays a buffer in the "other" window without switching to it.
As mentioned above, in the case of the backtrace buffer you probably want to exit from it with q, to get out of the recursive edit.
Try to use popwin-mode. It is really good about closing windows automatically.
I want to describe how mechanism work|
You open undo-tree-visualize than you find your correct branch, after that when you change your active window which has cursor via switch-window undo-tree related buffer will close other window automatically.
One possible solution is to use a window management package. Store your "preferred" window arrangement, and after the debugger/buffer window pops up and you're done with it, revert to your preferred arrangement.
There are a bunch of packages to choose from, see: switching window configurations on the wiki.
Additionally, you might want to figure out the actions you commonly do that trigger the extra (unwanted) window popping up, and use that to trigger saving your window configuration just before the window pops up.
If you want to roll your own (it's pretty easy), you can just save off the window configuration, and restore it like so:
(setq the-window-configuration-i-want (current-window-configuration))
(global-set-key (kbd "<f7>")
(lambda () (interactive)
(set-window-configuration the-window-configuration-i-want)))
The trick is figuring out where to put the setting of the-window-configuration-i-want.
Thanks go to #spk for a nice function. I merely modified it to also close the window too and bound it to the already nice set of window control functions found in evil-windows (via "C-w"):
(use-package evil
:bind (:map evil-window-map ("O" . delete-most-recent-window))
:preface
(defun delete-most-recent-window ()
"Kill the buffer in the most recent window."
(interactive)
;; Window selection is used because point goes to a different window
;; if more than 2 windows are present
(let ((win-curr (selected-window))
(win-other (next-window)))
(select-window win-other)
(kill-this-buffer)
(delete-window) ; <-- Added this
(select-window win-curr))))

Pin Emacs buffers to windows (for cscope)

For my day job, I live in Emacs. Utterly. I also have become pretty dependent on CScope to help me find things in the code.
Normally, I have 2 windows in a split (C-x 3):
alt text http://bitthicket.com/files/emacs-2split.JPG
And I use the right window for code buffers and the left window for the CScope search buffer. When you do a CScope search and select a result, it automatically updates the right-side window to show the buffer referred to by the result. This is all well and good, except that it causes me to lose my place in some other buffer that I was studying. Sometimes this is no biggie, because [C-s u] gets me back to where I was.
What would be better, though, is to have 3 split windows like this ([C-x 2] in the left window):
alt text http://bitthicket.com/files/emacs-3split.jpg
And have the bottom left window contain the CScope search buffer, and the top left window be the only buffer that CScope ever updates. That way, I can see my CScope searches and navigate around the code without losing the buffer I'm focused on.
Anyone know how I can do that?
Put this in your .emacs file:
;; Toggle window dedication
(defun toggle-window-dedicated ()
"Toggle whether the current active window is dedicated or not"
(interactive)
(message
(if (let (window (get-buffer-window (current-buffer)))
(set-window-dedicated-p window
(not (window-dedicated-p window))))
"Window '%s' is dedicated"
"Window '%s' is normal")
(current-buffer)))
Then bind it to some key - I use the Pause key:
(global-set-key [pause] 'toggle-window-dedicated)
And then use it to "dedicate" the window you want locked. then cscope can only open files from its result window in some OTHER window. Works a charm. I specifically use it for exactly this purpose - keeping one source file always on screen, while using cscope in a second buffer/window, and looking at cscope results in a third.
Well, I decided to not be a reputation-whore and find the answer myself. I looked in cscope.el as shown on the Emacs wiki, as well as the xcscope.el that comes with the cscope RPM package on RHEL.
Neither appear to give a way to do what I'm wanting. The way is probably to edit the ELisp by adding a package variable like *browse-buffer* or something and just initialize that variable if not already initialized the first time the user does [C-c C-s g] or whatever, and always have the resulting code shown in *browse-buffer*. Then the user can put the *browse-buffer* wherever he wants it.