In emacs how does the "hyperlink" in the *Help* buffer work? - emacs

I'm trying to figure out how the "hyperlink" works in the *Help* buffer (so that I can implement something similar in the output of M-x compile). Here is a more detailed description of the behaviour I want to imitate:
M-x describe-function find-file opens up a *Help* buffer. The first line of this buffer (for me) shows: find-file is an interactive Lisp function in files.el. files.el is underlined and clicking (or hitting enter when point is there) opens a new buffer with files.el and the point positioned at the definition of find-file. Doing a describe-key for says invokes push-button in button.el, which tells me Perform the action specified by a button at location pos - but how do I define a button and associate an action with that button in my own buffer?

The major mode of the *Help* buffers is help-mode. In its source code (help-mode.el), you find function help-make-xrefs that "Parse[s] and hyperlink[s] documentation cross-references in the given BUFFER". You may check it how it was implemented.
Otherwise, I'd suggest using org-mode instead, which uses the simple form [[URI][caption]] to mark hyperlinks.

Related

Clear only one Latex Fragment in Emacs Org-mode

Intro: The function org-preview-latex-fragment either renders the latex in entire section OR only one latex fragment (if the point is in it). However, the clear (C-c C-c) always removes all the images.
Q: is there a way to remove only one image (the one pointed to)?
C-c C-x C-l runs org-toggle-latex-fragment and will toggle on/off the fragment that you are on at the time or with a prefix argument, all fragmens in the current subtree, or all fragments in the buffer. See the documentation of the function for more details.
Note that org-preview-latex-fragment is obsolete since org 8.3 and is just an alias to org-toggle-latex-fragment. Also note that C-c C-c does not do anything useful for fragments any longer: this is an attempt to reduce the overload that this particular key sequence currently carries.

emacs with nrepl.el for Clojure - how to hide a popup buffer?

I'm using emacs 23 (acquamacs) and have installed nrepl.el.
I've evaluated some Clojure code with
C-c C-p
(this key combo is to Evaluate the form preceding point and display the result in a popup buffer.)
Running it has produced a nice popup buffer for me to see the results.
How do I hide this popup buffer? (I don't want to kill it - just get my fullscreen back).
FYI Re this command - I've run this with
C-h k C-c C-p
and got
^C ^P runs the command nrepl-pprint-eval-last-expression, which is an interactive compiled Lisp function in `nrepl.el'.
It is bound to ^C ^P, <menu-bar> <nREPL> <Eval last expression in popup buffer>.
(nrepl-pprint-eval-last-expression)
Evaluate the expression preceding point and pprint its value in a popup buffer.
You haven't provided enough information for anyone to test (that's not a standard global binding), and you haven't even indicated whether the new window is selected; but there's a generic solution which will work regardless.
Add (winner-mode 1) to your init file, and then you can always use C-c<left> to undo the most recent window configuration change (or changes plural, if you repeat the command).
Either that or just press q. I just tried it out with the help buffer and it dismisses the window but not the buffer. However, popup buffers in the style of ac-nrepl does for its autocompletion are meant to simply be dismissed
Always, when asking Emacs questions, say what the key sequence does. C-c C-p does nothing here. TO find out what it does press C-h k C-c C-p and report back. In this case we're invoking 'describe-key and then telling it to describe what C-c C-p does.

Emacs Lisp: How to, an interactive buffer with REPL?

Imagine the dunnet.el game, the eliza doctor or the IELM mode...; This is, evaluating answers and prompting new questions on the main buffer, then making some actions according to their elisp rules.
I would to create something like an interactive assistant, BUT NOT prompting from minibuffer as (interactive) does (not doing M-x)... I need a kind of REPL loop. So the question i how to get interaction on the working buffer (the main screen), like any text conversational/adventure, but for call my defun'ed functions on this way.
So i need a basic elisp skeleton, to make a custom REPL. The target is to make an assistant with a natural language, then parsing the anwsers, and making some actions according the rules.
Thank you for your time,
Steve,
Check out ielm, which is an elisp repl included with emacs.
Depending on what can go on in your buffer, can you just give it a major mode with an appropriate re-binding of return? (This answer vaguely inspired by Lisp-Interaction-Mode's C-j)
You should bind all keys that use for moving in buffer like C-p C-n, Up Down Arrows/ page up/down just bind them to (lambda ()). change function for C-b and C-f (Left Right Arrows) if cursor position is in beginning or end of the line. And bind Return key to function that read current line from end to optional prompt (you can use Regex to remove the prompt) do whatever you want with that string, and then go to the next line in the buffer and print your result.

Simple Emacs keybindings

I have two operations that I do all the time in Emacs:
Create a new buffer and paste the clipboard. C-S-n
Close the current buffer. C-S-w
Switch to the last viewed buffer. C-TAB
I feel like a keyboard acrobat when doing the first two operations. I think it would be worth trying some custom key bindings and macros.
A few questions about this customization:
How would I make a macro for #1?
Are these good key bindings? (I know this is a bit subjective, but they might be used by something popular that I don't use.)
Has anyone found a C-TAB macro that will act like Alt+Tab in Linux/Windows? Specifically, I want to have a stack of buffers according to the last viewed timestamp (most recent on top). I want to continue cycling through the stack until I let go of the Ctrl key. When the Ctrl key is released, I want the current buffer to get an updated position on the stack.
Have you tried using vertically or horizontally split windows for this (via C-x 3 or C-x 2)? It seems like it would give you fewer steps - even if you implement something like you're talking about.
I find split windows really speed up copy and pasting operations. I use the arrow keys on my num pad to switch among windows (windmove-left/-right/-up/-down), so it's only one key to press and you go to the window you want.
I guess this is a little different from what you're asking for, but it sounds like it might help speed things along a bit.
C-x left and C-x right cycle through buffers, but you have to hit it multiple times, you can't just keep the key pressed down.
For creating a macro for #1, you just start a macro, hit the keys you usually do to create a new buffer, and stop the macro.
So it would be something like:
C-x ( C-x b NEW RET C-x )
You can then save NEW to a file once you're done pasting, so you can use the macro again to create a new buffer. C-x e to try out the macro. If it works you can save it into your init.el file. This is done with:
M-x name-last-kbd-macro
Then you'll get a prompt to enter the name of your choice. This is only good for the current session. Then you save the named macro to your initialization file. First you open your .emacs or init.el file. Then you place point where you want the macro definition to go, then you type:
M-x insert-kbd-macro
Now you can run your macro using its name via M-x <macroname> . You can bind your macro to keys too (in your .emacs or init.el file):
(global-set-key (kbd "C-c a") '<macroname>)
For example this is how your init.el would look after creating a macro that opens a new buffer called NEW that is not associated with a file and binding this macro to C-c n:
;; Creates a new unassociated buffer called NEW
(fset 'new-buffer "\C-xbNEW\C-m");
;; Shortcut for new-buffer
(global-set-key (kbd "C-c n") 'new-buffer)
You can also throw in the paste, buffer close, and buffer switching operations. I guess you'd have to save the buffer to a file manually.
Some resources
Information about macros on EmacsWiki
Possibly useful: Swap text between buffers
start by invoking start-kbd-macro, finish by with end-kbd-macro. Afterwards you may immediately test the new macro with call-last-kbd-macro. If you're happy with the result you might want to save the macro.
Emacs generally doesn't use C-S keybindings and they are easy to use, so I'd call them good. They might cause problems if you're using the terminal version of Emacs, but I assume that's not the case with you.
I use this simple snippet:
(global-set-key (kbd "<C-tab>") 'bury-buffer)
bury-buffer basically makes the current buffer the last in the buffer-list so you'll be able to cycle buffers in a predictable order.
I wouldn't make a macro for that but write a function like someone else posted on this page. Instead of (cua-paste nil) you could also use (yank). I'm not sure which one's better and why.
I don't like them that much. For things that I use often I'd like to do as little finger acrobatics as possible, so that would mean modifier+key instead of modifier1+modifier2+key.. or use a function key if you don't feel tied to the homerow.
no comment

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.