• Aucun résultat trouvé

CREATING A NEW STYLE

Dans le document 0 () 0 0 (Page 146-153)

O active. commands

OPERATIONS ON STYLES

6.3 CREATING A NEW STYLE

To create a new Editor style, you first make a new style object. Yo~

then bind in the new style all the features that you want it to have.

Creating a new style brings together all the techniques discussed so far in this manual:

• Defining new commands, variables, and attributes as nec~ssary to perform text operations, display operations, and other Editor operations.

• Binding keys, pointer actions, variables, and attributes in the new style

• Activating the new style in any or all buffers

You can also include in a new style some specially defined functionsO that are invoked whenever the style is activated or deactivated in a buffer. These "hook functions" create some useful feature in buffers where the style is active and remove that feature whenever the style

is deactivated.

All these procedures are illustrated in this section·in relation to a new Editor style.

0

6.3.1 Making a Style Object

To create a new style object, you use the macro MAKE-STYLE. This macro is described in full in Part III of this manual. Its format is:

MAKE-STYLE name &OPTIONAL documentation

&KEY :ACTIVATION-HOOK :DEACTIVATION-HOOK For example:

(MAKE-STYLE (TEXT-MODE :DISPLAY-NAME "Text")

" Used when editing narrative text. It emulates the behavior of word-processing programs. in formatting text.")

0

If you evaluate this form, the Editor will have a new style named TEXT-MODE or "Text". The style will contain no bindings until you add them with calls to BIND-VARIABLE, BIND-ATTRIBUTE, BIND-COMMAND, or BIND-POINTER-COMMAND.

Before you make the style, it to have activation and attached to a style in the later.

however, you should decide whether you want deactivation hooks. These functions can b e o

MAKE-STYLE form; they cannot be added

u

0

0

0

0

OPERATIONS ON STYLES

6.3.2 Style Activation and Deactivation Hooks

A style activation hook can be used to perform some operation that you want done every time the style is activated.

For instance, in a text-related style you need to be able to set margins -- the character positions in each line where text is to begin and end. You can define new Editor variables to store margin settings and then bind the variables in each buffer that has "Text" style active. This action enables each buffer to store its own margin settings.

The new variables might look like:

And,

(DEFINE-EDITOR-VARIABLE (LOCAL-LEFT-MARGIN

:DISPLAY-NAME "Local Left Margin")

" Specifies the first character position where text can begin in each line.")

(DEFINE-EDITOR-VARIABLE (LOCAL-RIGHT-MARGIN

:DISPLAY-NAME "Local Right Margin")

" Specifies the last character position that text can occupy in each line.")

It would be convenient to have these variables bound automatically in each buffer that has "Text" style active. To achieve this, you can define a function that binds the variables in a buffer and then specify that function as the activation hook of the style "Text".

The style activation hook is invoked whenever its style is activated in a buffer. The hook function is called with two arguments -- the style and the buffer. A function that binds the above variables on a per-buffer basis might look like:

(DEFUN BIND-MARGINS (STYLE BUFFER)

;; The function does not use the STYLE paramenter ..

(DECLARE (IGNORE STYLE))

;; CONTEXT is the buffer where the style is being activated.

(LET ((CONTEXT (LIST :BUFFER BUFFER)))

;; Make the left margin O in the buffer.

(BIND-VARIABLE "Local Left Margin"

:CONTEXT CONTEXT :INITIAL-VALUE 0)

OPERATIONS ON STYLES

ii Make the right margin 1 less than the screen width in

ii the buffer.

Q

(BIND-VARIABLE "Local Right Margin"

:CONTEXT CONTEXT

:INITIAL-VALUE (1- (SCREEN-WIDTH)))))

A style deactivation hook is similar: it is invoked whenever a style is made inactive in a buffer. If you deactivate "Text" style in a given buffer, you would have no further use for the margin settings.

A deactivation hook that unbinds the margin variables might look like:

(DEFUN UNBIND-MARGINS (STYLE BUFFER) (DECLARE (IGNORE STYLE))

(LET ((CONTEXT (LIST :BUFFER BUFFER)))

(UNBIND-VARIABLE "Local Left Margin" CONTEXT) (UNBIND-VARIABLE "Local Right Margin" CONTEXT)))

Once you have defined the hook functions and the variables they

O

reference, you are ready to create the new style "Text":

(MAKE-STYLE (TEXT-MODE :DISPLAY-NAME "Text")

" Used when editing narrative text . . It( e~ulat

7

s the

behavior of word-processing programs 1n formatting text."

:ACTIVATION-HOOK #'BIND-MARGINS :DEACTIVATION-HOOK #'UNBIND-MARGINS)

This form creates the new "Text" style and establishes BIND-MARGINS and UNBIND-MARGINS as its activation and deactivation hooks.

6.3.3 Adding Capabilities to the Style

0

Once you have created a new style, you can add features to it at any

Q

time. You can add capabilities to a style by:

• Binding keys or pointer actions to commands in that style.

This may involve defining new functions and commands; you can also use existing commands.

• Binding Editor variables in the style. The variables to be bound can be new or existing variables •. Any variable that is

referenced by a command used in the new style must be bound in that style, unless a binding will be visible from another context when the style is active.

• Binding Editor attributes in the style. The attributes to be bound can be new or existing attributes. Any attribute that

Q

is referenced by a command used in the new style must be bound in that style, unless a binding will be visible from another

0

(DEFINE-COMMAND (SET-MARGINS-COMMAND :DISPLAY-NAME "Set Margins") (PREFIX)

Enter new value: "NEW-MARGIN) NEW-MARGIN))

(UNLESS (INTEGERP NEW-MARGIN)

(SETF (VARIABLE-VALUE "Local Left Margin") (PARSE-INTEGER NEW-MARGIN)))

Enter new value: "NEW-MARGIN) NEW-MARGIN) )

(UNLESS (INTEGERP NEW-MARGIN)

(SETF (VARIABLE-VALUE "Local Right Margin") (PARSE-INTEGER NEW-MARGIN)))

(CLEAR-INFORMATION-AREA)

(FORMAT *INFORMATION-AREA-OUTPUT-STREAM*.

"Left margin -D, right margin

-o"

"Local Left Margin" "Local Right Margin")))

With the new text-formatting style in mind, you could also write commands that wrap text, that move to the left margin to begin new lines, and that fill and justify text to the right margin.

Once the new commands are defined, you can bind keys to them in "Text"

style:

Q

(LET ((CONTEXT (LIST :STYLE "Text")))

(BIND-COMMAND "Set Margins" '#(#\X #\M) CONTEXT) (BIND-COMMAND )

(BIND-COMMAND ... ))

6.3.4 Activating the Style

Once your style has enough capabilities bound in it to be useful, you can then decide how and where you want to activate the style.

As a special-purpose style, "Text" is suitable for minor activation.

You would not want to assign it to "Default Minor Styles", since its

Q

behavior (wrapping, filling, and so on) is inappropriate for most code editing. It would be best to activate "Text" in buffers associated with the filetypes you normally use for narrative text editing.

Recall that the value of "Default Filetype Minor Styles" is an association list. You can add items to this list with PUSH:

(PUSH '("TXT" . "Text")

(VARIABLE-VALUE "Default Filetype Minor Styles")) (PUSH '("RNO" . "Text")

(VARIABLE-VALUE "Default File type Minor Styles'.'))

These forms establish "Text" a~ the last-activated (first-searched) of the minor styles in buffers associated with filetypes TXT and RNO.

0

0

0

0

0

PART II

CONCEPTS IN EDITOR PROGRAMMING

0

0

0

0

0

0.

0

0

0

0

0

0

Dans le document 0 () 0 0 (Page 146-153)