9

Quite a few Emacs modes use (something like) forms: the customization interface is one of them, the Message mode (read: its header part) is another one; there are more of them, unfortunately not always very similar. By a "form" I mean here a buffer with read-only parts and places designated to enter text; it may or may not contain buttons (it probably should accept C-c C-c as the confirmation/committing key, irrespective of the presence of an OK button).

I am particularly interested in auto-completion in the fields (and in particular, enabling fields with lists of values from some designated set, say, comma-separated). My question is: is there any Emacs library enabling easy (by "easy", I mean not reinventing the wheel) creation of such forms, or should I code everything myself?

(I imagine that what I should do in the latter case would be (1) set some parts of the buffer read-only, (2) prepare a special major mode, so that e.g. TAB and S-TAB switch between "fields", and (3) maybe use something like Icicles or Helm for autocompletion.)

Drew
  • 75,699
  • 9
  • 109
  • 225
mbork
  • 1,647
  • 1
  • 13
  • 23

1 Answers1

7

See libraries cus-edit.el and wid-edit.el (and their top-level libraries custom.el and widget.el, and associated component libraries), which are included with GNU Emacs. The former makes use of the latter. The latter defines basic (and not-so-basic) form-entry thingies, called "widgets", in a hierarchy. You can use these predefined widgets and functions in the library to create your own widgets and widget types.

However, be warned that the code of these libraries, particularly that of wid-edit.el, is not very easy to follow. Your best bet is to start by reusing existing widgets or using existing widget-definition code as a guide.

(Two minor extension libraries for this are cus-edit+.el and wid-edit+.el. They also present some usage examples.)

There is an Info manual for widgets, distributed with GNU Emacs, entitled "The Emacs Widget Library".

There is also library forms.el, included with GNU Emacs, but I am not familiar with it. Perhaps someone else has something useful to say about it. There is a manual for forms, distributed with GNU Emacs, entitled "Forms Mode".

Drew
  • 75,699
  • 9
  • 109
  • 225
  • 2
    A little more on forms.el: http://stackoverflow.com/questions/10166600/forms-mode-in-emacs-what-is-its-purpose-has-anyone-used-it – phils Oct 28 '14 at 00:23
  • 1
    @Drew: thanks for your answer! Could you clarify the relation between `\(custom\|widget\).el` and `\(cus\|wid\)-edit.el` a bit? I find especially this part from the Widgets manual incomprehensible: `(require 'widget) (eval-when-compile (require 'wid-edit))`. – mbork Oct 28 '14 at 00:40
  • 1
    First, I'm no expert on this. The times when I needed to change something wrt Customize I dipped into `cus-edit.el` and `wid-edit.el`. The other files you mention are top-level files; the `*-edit.el` files have the code that I was interested in. Dunno much more than that; sorry. The real difficulty is the code itself, which uses a particular semi-OOP style that does not lend itself well to either Emacs self-documentation (to put it mildly) or to the Emacs debugger. You can investigate on your own, or perhaps try to contact the author (good luck with that!). Maybe others here will be some help. – Drew Oct 28 '14 at 01:57