You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
François Pinard f379055a86 README.org: Better document a few bugs or suggestions. 13 years ago
Makefile Initial commit, from files in the works. 13 years ago
README.org README.org: Better document a few bugs or suggestions. 13 years ago
poporg.el poporg.el: Allow simultaneous edits. Add hooks. UI details. 13 years ago
rebox.el Initial commit, from files in the works. 13 years ago

README.org

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 all those edits before killing the original buffer.

Functions added to poporg-edit-hook are run once the PopOrg editing buffer has been set up with its contents, with the common prefix already removed, these functions may further modify the buffer contents. Functions added to poporg-edit-exit-hook are run when PopOrg is about to reinstate the common prefix and move back the editing buffer contents into the original programming buffer, these functions may alter the contents as needed. (I did not need these hooks, so let's talk if you need them to be defined differenty!)

Known bugs

The following list is organized in decreasing order of approximative or subjective priority. You may also check if there are any issues on GitHub.

  • If the cursor is located immediately before the opening delimiter of a string before poporg-dwim, some extraneous text to edit may be collected from before the cursor.
  • 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, while the edition is pending, the characters after the region are preserved when the user completes its PopOrg 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.
  • Ideally, the region being edited should be read-only but not intangible, in that the cursor could be moved into it, from where a poporg-dwim command would popup the associated edit buffer. This would be particularly useful when a user has many pending PopOrg edits.
  • It has been suggested, and rightly so, that C-c C-c would be a nice keybinding for completing a PopOrg edit. The problem with this is that the edit buffer uses Org mode, where C-c C-c is overcrowded with many functionnalities already; some care would be needed to make sure this command, used with another intent, does not unexpectedly close the edition.

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.

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: msgstr strings 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 that msgid and msgstr fields, and the reminder of the PO file, could be using different character sets.
  • 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 Rebox documentation for more historial context.