How can I make a singleton bookmark in my org file? - emacs

I have an org-mode task list that I keep in version control. I would like to press a key and turn the current position into a bookmark target that I will be able to access anywhere I have the list checked out, regardless of other changes that have been made to the document. This is why (bookmark-set) will not work.
Additionally I would like to ensure that this target only occurs once in the file. If I put the target at a different position I want the original target to go away.
Essentially, I want to combine features of Emacs bookmarks (the singleton aspect), and org-mode links (more robust persistence). What's the best way to do this?

You want to read up about Markers:
M-: (info "(elisp) Markers") RET
This feature is what allows the mark ring to retain its relative locations regardless of buffer changes, for example.
If you want the marker to be stored within the file itself, then you might want to include it as a local variable in the file itself, and use before-save-hook to update that to the current value.
The local variable may need to be an integer, in which case you would need to translate it on loading and saving.
(This is a little speculative, but I suspect it will do the trick.)

The best solution I've been able to come up with is to use the text <<<BOOKMARK>>>, and to search for it when necessary. At some point I might write some functions that place this bookmark and delete it from elsewhere.

Related

Is there an Emacs read-only or view mode that allows inserting some text?

Here's the use case: I'm writing a novel in Emacs (in org-mode). One part of my writing/editing flow is to read over some large portion of what I've written, collecting notes/possible edits/etc as I go. The sort of thing you'd do, on paper, by printing it all out and then writing notes in the margin.
I want to prevent myself from, as I do this kind of review, actually doing any writing -- but that's surprisingly hard. Like, if the buffer is editable, I start to type a brief note about a fix, then find myself starting to restructure / fix a sentence, and next thing I know, I've spent five minute polishing
a single paragraph. This not only slows me down, it breaks my ability to imagine a reader's response.
I've tried just putting the buffer in view-mode, and that sort of works -- but then it's laborious to try to identify the places I want to go back and review/fix up.
My ideal would be, to have something in view-mode, which I genuinely can't edit, but which, as I move the cursor through it, I could hit some key combination, and it would allow me to enter a brief note in the minibuffer, which would then get inserted into the main buffer, at point, possibly inside brackets or a comment or some such.
Does anyone know of something like that? Or have any pointers to something similar which I could try to adapt?
You can easily set bookmarks at any locations. And bookmarks can contain annotations.
If you use library Bookmark+:
The annotations are in Org Mode by default, and they can even be separate files (by default they are part of the bookmarks themselves, so stored in your bookmarks file).
You can bookmark not just a position but also a region of text, whether a sentence, paragraph, page, or an arbitrary span of text.
You can automatically name bookmarks as you set them, if you don't care about the names.
Updated after OP's comment saying "I prefer to shove the comments/questions/notes directly into the text of the novel. Because I end up adding/deleting/moving text a ton, and I want the notes to move with the text":
Bookmarks move with the surrounding text. That is, they generally get relocated automatically, since the surrounding text is recorded as part of the bookmark, and when jumping to a bookmark that text is looked for.
Occasionally the context has changed so much that a bookmark can't be relocated automatically, and you are prompted to relocate it manually.
But yes, bookmarks are stored in a bookmark file, separately from the files they target. There are both advantages and disadvantages to this feature. Advantages include (1) removing clutter from the text (annotations, including notes about possible text changes are metadata), (2) immediate access to particular text locations from anywhere, (3) a separate, persistent record/history of work or thoughts on it, (4) you can have multiple, separate sets of bookmarks/annotations for the same target text.
One thing you might find handy, when using bookmarks especially for annotating a particular file: C-x p C-l switches to a bookmark file that has only bookmarks for the current file or buffer, creating such a file on the fly if none
exists. (This is available only with Bookmark+.)

Running a function before opening/finding a file

Im trying to make a function that should run before a file is opened/shown in the buffer. This function really only needs to run once.
I try to set a mode depending on the content of the file that is about to be viewed. I know about add-to-list 'auto-mode-alist <filename-regexp> . <mode>
But this is not enough. Ideally i would want to parse the file (or just a small part of it) and check the file content before setting the mode. Usually the view-file (think .js/.php etc. in a mvc framework) has the same extension as the files in "pure" code, so i cant just check for the file extension.
Why?
Basically when using any template language i normally want to use web-mode and when doing "pure" code i want to use the mode for the language.
So basically i would want to parse the file before, and check for some regexp, and if it matches i would set web-mode and if not i would set the language-mode i want to use.
I know i can easily change modes manually, but i would rather have this done by Emacs.
You can use magic-mode-alist for that. You'll probably want to use a MATCH-FUNCTION which checks the buffer's file-name and its content.
You might want to put your function into find-file-hook.
When it is run, the major-mode is already set, but you can change it.
If you own the file in question, you might also want to explore file variables.

Emacs: How to associate different initialization configurations with different file formats?

What's the best way to associate file extensions with my own customizations? For example, when I open a .py file the frame would be bigger and split into 2 windows, but when a .tex file is opened the frame would be smaller with just one window. Should I split my .emacs and write all configurations associated with python in a .el file (key bindings, python shell = ipython, etc ...) and for latex in another .el file (load auctex, pdf mode = default, etc ...)? How would I "call" the files and make them work appropriately (if that'a possible and good solution)?
(First, +1 to #phils's comment. You will get better help if you are more specific about what you need/want.)
Depending on just what you need/want, see also variable (not option) file-name-handler-alist. You might not need it, but you might.
You can make use of it if you intend all or particular operations on the files to involve additional actions (such as those you describe). For any operations where you do not need special treatment, just provide the default behavior. For the others, provide the default behavior plus the extra behavior (in whichever order is appropriate).
See (elisp) Magic File Names for more information.

emacs package for working with tagged regions?

I'd like to be able to apply tags to regions of code and text and to be able to use these to narrow visibility to navigate - ideally across multiple files. So for example if I was researching C16th European history I could tag insert and and zoom around around cromwellish stuff quickly, limit my searches to cromwellish regions, etc. It's important that tags pairs shouldn't have to be "well-formed" because I might want to start an region inside of a but end first. And, obviously, I'd to be able to list all my tags, and to search or narrow on combinations of them.
I can't find anything like this. Org-mode comes closest, but its tags are headline based, which makes stopping one tag while continuing other painful - plus I need headlines as headlines, and I'd like to be able to tag code this way too.
Is there anything like this? Or any toolkit that would be a good start to write this?
Library Bookmark+ provides what you are asking for, I think. It enhances standard Emacs bookmarking in several ways, including some that respond to your request.
You can bookmark locations of all sorts, and the recorded bookmark information can include the region limits/context. (If the region is active when you set the bookmark then the region info is recorded.)
When you jump to the bookmark, the region is re-activated (if the region was recorded).
If the content of the destination file (or whatever) has been modified since the bookmark was last updated, so that the text around the recorded region has changed, the bookmark and region are automatically relocated (assuming that the new destination and limits are recognizable and thus locatable).
Bookmark+ also lets you tag any bookmark (or file), in multiple ways. Tags are arbitrary, free-form strings (but you can also optionally associate arbitrary Lisp values with them.)
And you can list all tags, search and narrow on tag combinations (unions, intersections, etc.). Tags are one of the most important Bookmark+ features.
Library Icicles provides additional enhancements regarding the use of Bookmark+ bookmarks and tags.

Creating vim buffer (NarrowRegions) from multiple files

I was wondering if there's a way to allow a buffer to edit multiple files at once.
Recently, I got vim working with eclim. But now I was wondering if I could edit multiple files at once in one buffer. For example, say I have an interface and a class file where I need to update a method signature is there a way I can load both of them into the same buffer and edit them simultaneously. Narrow region for multiple file regions. It would also be awesome to remember my settings but that could be a future iteration.
I saw this solution but it seems inconvenient to create a separate file to handle this interaction.
You can open all files as split windows (so you see all of them together), and :windo, :bufdo, :argdo allow you to perform mass-operations (like a :substitute) on all of them at once. There's usually no need for such artificial concatenation schemes, and as the linked article shows, it has its downsides over keeping the files separate.