Skip to content

Latest commit

 

History

History
237 lines (164 loc) · 6.7 KB

README.md

File metadata and controls

237 lines (164 loc) · 6.7 KB

An Emacs Mode for the q language - q-mode

Updates for 2023. I have given up with XEmacs support and supporting operation on Windows. I now use the system under Spacemacs as a layer.

Features

q-mode is a major mode for editing the "q" language in Emacs.

The q language is a functional language developed by Kx Systems) for its columnar database product kdb+. Together they are described as "q/kdb+". The q language is implemented in an another low-level called "k". This is partly supported by kdbp-mode, which is also provided and is used as the parent class of this mode.

The major features of q-mode include:

  • syntax highlighting and electric indentation
  • interaction with inferior q instance using comint

Provenance

There is another q-mode on GitHub that is more fully-featured [psaris/q-mode]. I branched from that a long time ago and changed the implementation substantially.

I only describe the minimal set of features I've used since then. There are two features in that implementation that I not implemented.

  • q documentation using info
  • remote operation with gnuclient

Extensions

I've added commands that can paste to the inferior q buffer from the source code buffer. If you are prototyping a system, you would usually "Start Q" and this should start an inferior Q process. If not check your path and the q group options.

Once you are able to start a q process, you can then write statements and paste them easily from the editor to the process using Run Group.

Quirks

The quirks are if you use an = sign it causes problems with the indentation. You can cancel it with a semi-colon.

// so this =

will cause indentation problems in the file. But, this

// so this = ;

will cancel it.

Installation

To load q-mode on-demand, instead of at startup, add this to your initialization file

(autoload 'q-mode "q-mode")

The add the following to your initialization file to open all .k and .q files with q-mode as major mode automatically:

(add-to-list 'auto-mode-alist '("\\.[kq]\\'" . q-mode))

If you load ess-mode, it will attempt to associate the .q extension with S-mode. To stop this, add the following lines to your initialization file.

(defun remove-ess-q-extn ()
 (when (assoc "\\.[qsS]\\'" auto-mode-alist)
  (setq auto-mode-alist
        (remassoc "\\.[qsS]\\'" auto-mode-alist))))
(add-hook 'ess-mode-hook 'remove-ess-q-extn)
(add-hook 'inferior-ess-mode-hook 'remove-ess-q-extn)

Usage

Q mode defined in `q-mode.el': Major mode for editing Q scripts.

Provides the q-run-script' (C-c C-l) command to run the interpreter on the script in the current buffer. It will be verified that the buffer has a file associated with it, and you will be prompted to save edited buffers when invoking this command. Special commands to quickly locate the main script and the input line of the Q eval buffer, and to visit the source lines shown in compiler/debugger messages are provided as well (see q-eval-mode').

These operations can be selected from the Q mode menu (accessible from the menu bar), which also provides commands for reading the online help and customizing the Q/Q-Eval mode setup.

Command list:

key binding


C-c Prefix Command TAB q-indent-line ESC Prefix Command ( .. ) q-electric-delim ; q-electric-delim = q-electric-delim [ q-electric-delim ] q-electric-delim | q-electric-delim

C-M-i q-move-to-indent-column C-M-q q-indent-current-rule

C-c C-a q-first-msg C-c C-b q-do-buf C-c C-c q-do-cmd C-c C-e q-do-cmdf C-c C-f q-find-script C-c C-h q-help C-c C-l q-run-script C-c RET q-run-main C-c C-n q-next-msg C-c C-p q-prev-msg C-c C-u q-current-msg C-c C-v q-goto-input-line C-c C-x q-quit-eval C-c C-z q-last-msg

Entry to this mode calls the value of q-mode-hook if that value is non-nil.

Typical use

I noted above, I don't really use that many of these features.

Typically, I would load a .q script into Emacs. Put in a comment break somewhere in it, that's a forward slash, like this.

/

Run the script with C-c C-l - say "yes" to the prompt to revert the buffer. It will execute code up to the comment. Go back to the edit buffer, find the code after the comment and then step through it with C-c C-c.

Customization

M-x customize-group can be used to customize the q group.

Key to these are the q-prog-name, q-prog-opts and q-prog-args.

There are a great many other options, that are not in use in this implementation of q-mode.

The font-lock-mode with syntax highlighting doesn't work anymore. There is a `kdbp-mode' that does that.

The q-gnuclient and q-info methods don't work.

Controlling Execution

You can also use the local varialbes block to specify how to run the Q interpreter.

/  Local Variables: 
/  mode:q
/  q-prog-args: "last d -p 5016 -t 1000"
/  fill-column: 75
/  comment-column:50
/  comment-start: "/  "
/  comment-end: ""
/  End:

About functions

Although this mode is useful, the step mode C-c C-c doesn't support multi-line function definitions. The q interpreter doesn't have a "paste" mode like Python or Scala, so if evaluates each line, even when defining a function.

So

Either make the function a single line,

Or put any multi-line function into a separate script and source them with \l.

It is mentioned in the other q-mode by psaris that there is support for that.

Postamble

I do still use this mode, so I may be able to fix it up if you need a feature.

History

I mentioned above that there is another q-mode by psaris.

https://github.com/psaris/q-mode

I haven't used this directly. There is another q-mode by little-arhat

https://github.com/little-arhat/q-mode

And I've followed arhat's work, but added some features from psaris.

arhat used a basic q-minor-mode to interact with the q interpreter and put the syntax highlighting into kdbp-mode.

I promoted arhat's q-minor-mode to be a major-mode, so there is no syntax-highlighting.

I used some of psaris' q interpreter interaction in this q-mode.

If you want syntax highlighting, you can still use the kdbp-mode, but the syntax is out-of-date now.

I've now separated the kdbp-mode I use with this q-mode so that this q-mode will work on its own.

So the kdbp-mode I use is this one.

https://github.com/eepgwde/kdbp-mode

The notes I made there say that I took it from Alvin Shih, but it looks very similar to arhat.

" -->