6.6 KiB
PopOrg — Editing program comments or strings with Org mode
Introduction
PopOrg is a small Emacs Lisp project, to help editing program string or comments using Org mode.
Literate programming with Org is often presented as mixing programs snippets within an Org document, with tools to extract pure programs out of the Org files. I would prefer it the other way around: mixing documentation snippets within program source code, with tools to extract pure Org documentation from the source files.
Emacs does not nicely handle multiple major modes in a single buffer. So far, many solutions have been implemented, all yielding some level of happiness, but none are perfect. The PopOrg approach avoids the problem by extracting the block comment or the string, from a buffer using a major programming mode, into a separate buffer to be edited in Org mode, but containing only that block comment or that string. Once the edit is completed, the modified comment or string gets re-integrated in the buffer containing the program, replacing the original contents.
Installation
To install PopOrg, move files poporg.el and rebox.el at a place where Emacs will find them. You might byte-compile the files if you want.
To use PopOrg, you need to pick some unused keybinding and add a few lines to your ~/.emacs file. For one, I picked C-c e o and added these lines:
(autoload 'poporg-dwim "poporg" nil t)
(global-set-key "\C-ceo" 'poporg-dwim)
Usage
While editing a buffer containing a program, you may edit a comment block or a string (often a doc-string) in Org mode by placing the cursor within or nearby that comment or string, and calling poporg-dwim using you selected keybinding. This pops another buffer in Org Mode (hence PopOrg for the project name), containing the comment or string. Once your edition is done, right in the popped up editing buffer, call poporg-dwim again to complete the edition, or merely kill that buffer to abandon the edition.
More precisely, if the cursor is within a comment block or a string, this is what gets edited. If the cursor is not within a comment block or a string, a comment or string following the cursor gets selected instead. Otherwise, this is the comment or string which precedes the cursor which is selected for edition.
While the comment or string is being copied in the editing buffer and until the edition is completed, the original comment or string in the original buffer is greyed out and protected against accidental modification. PopOrg asks for confirmation when the user attempts to kill an editing buffer which has modifications. PopOrg also prevents the original buffer from being killed while there are pending PopOrg edits, the user should either complete or abandon those edits first.
Known bugs
- The protective measures against losing a pending edition do not work when the user plainly exits Emacs.
- If characters are added immediately before or immediately after the region being edited, the characters after the region are preserved when the user completes its side edition, but the characters before the region are lost, while they should have been preserved.
- Even if a region being edited is intangible (meaning that the cursor cannot be pushed into it), it is not read-only and could have its contents deleted by editing from either end of the region. I suspect (without being sure) that this bug, and the preceding one, come from the fact overlays and text-properties do not behave the same.
- One cannot edit more than one comment or string at a time, and most FIXMEs in the code relate to this problem. Solving it requires that I get a firmer grasp of buffer-local variables, which apparently and mysteriously do not behave like I expect.
- Also, maybe, check for issues on GitHub if any.
History
PopOrg recycles a few ideas from two previous Emacs projects:
- my PO mode (source and documentation), for the idea of using separate buffers for edition. For PO files, the need is quite clear:
msgstrstrings use escaping which is easy to get wrong, so the idea of a separate buffer is a way to remove that concern from the user, PO mode unquotes before presenting the string to the user, and requotes it once the editing is completed. This was also solving the problem thatmsgidandmsgstrfields, and the reminder of the PO file, could be using different character sets. I intend to recycle some ideas from PO mode for editing many comments and strings at once, instead of necessarily one after another. - my Rebox tool (source and documentation), for finding the boundaries of block comments. Originally in Emacs Lisp, this tool has later rewritten in Python at the time I was developing Pymacs, with a few minor improvements while doing so. Le Wang, starting from my old Emacs Lisp, wrote a much enhanced version (source and video). For PopOrg, however, the needs are modest, so it includes the old Emacs Lisp version. See the very last section of the documentation for more historial context.
Caveats
- I do not much like to depend on Rebox, however, which is a complex piece of code compared to the reminder of PopOrg. For comments, Rebox studies the file contents to guess comment delimiters and box styles, while for strings, PopOrg rather relies the syntax analysis previously made by the programming major mode, and expressed through faces. These approaches are too different, maybe both are wrong anyway. One advantage of using Rebox, however, is that it brings PopOrg closer to the capability of editing Org mode comments in complex boxing cases.
- Once the string and comment is back into the programming buffer, we loose Org mode highlighting and presentation details, which is unfortunate. Multiple editing modes in Emacs are not able to properly highlight sections of a file according to the intended mode for each section: there is a single mode for the whole buffer in fact. Org mode, on the other hand, has the virtue of properly highlighting the code snippets it contains, so surely, there is a way to do things properly, that might be understood and recycled, I'm not sure.
- PopOrg should ideally be accompanied by a set of conventions and some tools for proper extraction of an Org file out of program sources.