2424; ;; Commentary:
2525
2626; ; ROOT (https://root.cern/) is a framework for performing data
27- ; ; analysis. This package means to integrate this framework within the
28- ; ; ecosystem of Emacs. More specifically, root.el provides functions
27+ ; ; analysis. This package means to integrate this framework within the
28+ ; ; ecosystem of Emacs. More specifically, root.el provides functions
2929; ; to run C++ code within the ROOT REPL and execute org-mode source
3030; ; code blocks, replicating the jupyter environment in `root
3131; ; --notebook'.
3939(require 'vterm nil 'noerror )
4040
4141(defcustom root-mode nil
42- " Major-mode for running C++ code with ROOT"
42+ " Major-mode for running C++ code with ROOT. "
4343 :group 'languages
4444 :type 'mode )
4545
4646(defcustom root-filepath " root"
47- " Path to the ROOT executable"
47+ " Path to the ROOT executable. "
4848 :type 'string
4949 :group 'root-mode )
5050
5151(defcustom root-command-options " "
52- " Command line options for running ROOT"
52+ " Command line options for running ROOT. "
5353 :type 'string
5454 :group 'root-mode )
5555
5959 :group 'root-mode )
6060
6161(defcustom root-terminal-backend 'inferior
62- " Type of terminal to use when running ROOT"
62+ " Type of terminal to use when running ROOT. "
6363 :type 'symbol
6464 :options '(inferior vterm)
6565 :group 'root-mode )
6666
6767(defcustom root-buffer-name " *ROOT*"
68- " Name of the newly create buffer for ROOT"
68+ " Name of the newly create buffer for ROOT. "
6969 :type 'string
7070 :group 'root-mode )
7171
7272; ;; end of user variables
7373
7474(defmacro root--remembering-position (&rest body )
75+ " Execute BODY and return to exact position in buffer and window."
7576 `(save-window-excursion (save-excursion ,@body )))
7677
7778(defun root--push-new (element lst )
79+ " Push ELEMENT onto LST if it doesn't yet exist in LST."
7880 (if (memq element lst)
7981 lst
8082 (push element lst)))
8183
8284(defun root--pluck-item (el lst )
85+ " Return the value of EL in the plist LST."
8386 (cdr (assoc el lst)))
8487
8588(defun root--make-earmuff (name )
86- " Give a string earmuffs, i.e. some-name -> *some-name*"
89+ " Give a NAME earmuffs, i.e. some-name -> *some-name*. "
8790 (if (or (not (stringp name)) ; ; only defined for strings
8891 (= (length name) 0 ) ; ; but not empty strings
8992 (and (string= " *" (substring name 0 1 ))
9295 (format " *%s * " name)))
9396
9497(defun root--make-no-earmuff (name )
95- " Remove earmuffs from a string if it has them, *some-name* -> some-name"
98+ " Remove earmuffs from a NAME if it has them, *some-name* -> some-name. "
9699 (if (and (stringp name)
97100 (> (length name) 0 )
98101 (string= " *" (substring name 0 1 ))
101104 name))
102105
103106(defun root-->> (&rest body )
107+ " Process BODY sequentially."
104108 (cl-reduce (lambda (prev next ) (funcall (eval next) prev))
105109 (cdr body)
106110 :initial-value (eval (car body))))
114118 (send-function . root--send-inferior)
115119 (previous-prompt . comint-previous-prompt)
116120 (next-prompt . comint-next-prompt))))
117- " Mapping from terminal type to various specific functions" )
121+ " Mapping from terminal type to various specific functions. " )
118122
119123(defun root--get-functions-for-terminal (terminal )
124+ " Get all functions defined for TERMINAL."
120125 (root--pluck-item terminal root--backend-functions))
121126
122127(defun root--get-function-for-terminal (terminal function-type )
128+ " Get function of FUNCTION-TYPE for TERMINAL."
123129 (root--pluck-item function-type (root--get-functions-for-terminal terminal)))
124130
125131(defun root--get-function-for-current-terminal (function-type )
132+ " Get function of FUNCTION-TYPE for currently defined terminal."
126133 (root--get-function-for-terminal root-terminal-backend function-type))
127134
128135(defalias 'root--ctfun 'root--get-function-for-current-terminal
129136 " ctfun -- current terminal function" )
130137
131138(defun root--send-vterm (proc input )
132- " Send a string to the vterm REPL."
139+ " Send INPUT ( a string) to the vterm REPL running as PROC ."
133140 (root--remembering-position
134141 (root-switch-to-repl)
135142 (when (fboundp 'vterm-send-string )
136143 (vterm-send-string input)
137144 (vterm-send-return))))
138145
139146(defun root--send-inferior (proc input )
140- " Send a string to an inferior REPL."
147+ " Send INPUT to an inferior REPL running as PROC ."
141148 (comint-send-string proc (format " %s \n " input)))
142149
143150(defun root--preinput-clean (input )
151+ " Clean INPUT before sending to the process."
144152 ; ; move the template definition onto the same line as the function declaration
145153 (replace-regexp-in-string " template<\\ (.*\\ )>\n " " template<\\ 1>" (format " %s " input)))
146154
147155(defun root--send-string (proc input )
148- " Send a string to the ROOT repl."
156+ " Send INPUT to the ROOT repl running as PROC ."
149157 (funcall (root--ctfun 'send-function ) proc (root--preinput-clean input)))
150158
151159(defun root--start-vterm ()
152- " Run an instance of ROOT in vterm"
160+ " Run an instance of ROOT in vterm. "
153161 (let ((vterm-shell root-filepath))
154162 (vterm root-buffer-name)))
155163
156164(defun root--start-inferior ()
157- " Run an inferior instance of ROOT"
165+ " Run an inferior instance of ROOT. "
158166 (let ((root-exe root-filepath)
159167 (buffer (comint-check-proc (root--make-no-earmuff root-buffer-name)))
160168 (created-vars (root--set-env-vars)))
175183; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
176184
177185(defun root--make-text-property (input )
186+ " Add initial text-properties to INPUT."
178187 (propertize " root_input_2"
179188 'buffer-state input
180189 'face 'kaiti-red
181190 'read-only t ))
182191
183192(defun root--text-property-get-contents (text )
193+ " Get the buffer-state text-property from TEXT."
184194 (get-text-property 0 'buffer-state text))
185195
186196(defun root--make-temporary-buffer (text )
197+ " Create a temporary file and populate it with TEXT."
187198 (root--remembering-position
188199 (let* ((temp-file (format " %s .C" (make-temp-file " root" )))
189200 (buf (find-file temp-file)))
190- (switch-to-buffer buf)
201+ (switch-to-buffer buf)
191202 (insert (root--text-property-get-contents text))
192203 (write-file temp-file nil )
193204 (kill-buffer buf)
194205 temp-file)))
195206
196207(defun root--send-input-buffer (input )
208+ " Send INPUT to the currently running process."
197209 (let* ((text (root--make-text-property input))
198210 (file (root--make-temporary-buffer text)))
199211 (root--remembering-position
220232(defvar root--rcfile " ./.rootrc" )
221233
222234(defun root--set-env-vars ()
223- " Setup the environment variables so that no colours or bold
224- fonts will be used in the REPL. This prevents comint from
225- creating duplicated input in trying to render the ascii colour
226- codes.
235+ " Set the environment variable for current directory.
227236
228- Function returns t if the variables have been set, else nil. This
237+ Setup the environment variables so that no colours or bold fonts
238+ will be used in the REPL. This prevents comint from creating
239+ duplicated input in trying to render the ascii colour codes.
240+
241+ Function returns t if the variables have been set, else nil. This
229242return value is very useful for deciding if the variables should
230243be unset, as we will want not want to remove the user's existing
231244rcfiles."
@@ -240,17 +253,19 @@ rcfiles."
240253 nil ))
241254
242255(defun root--unset-env-vars ()
256+ " Remove the environment file from the directory."
243257 (delete-file root--rcfile))
244258
245259(defun root--initialise ()
260+ " Set the comint variables for buffer."
246261 (setq comint-process-echoes t
247262 comint-use-prompt-regexp t ))
248263
249264(defvar root-mode-map
250265 (let ((map (nconc (make-sparse-keymap ) comint-mode-map)))
251266 (define-key map " \t " 'completion-at-point )
252267 map)
253- " Basic mode map for ROOT" )
268+ " Basic mode map for ROOT. " )
254269
255270(define-derived-mode root-mode comint-mode
256271 " ROOT"
@@ -279,6 +294,7 @@ rcfiles."
279294(defvar root--completion-buffer-name " *ROOT Completions*" )
280295
281296(defun root--get-last-output ()
297+ " Get output of the last command."
282298 ; ; TODO: needs improvement to better capture the last output
283299 (root--remembering-position
284300 (root-switch-to-repl)
@@ -291,27 +307,33 @@ rcfiles."
291307 (buffer-substring-no-properties pp np))))
292308
293309(defun root--completion-filter-function (text )
310+ " Set the list of keywords to be the output of tab (TEXT)."
294311 (setf root--keywords text))
295312
296313(defun root--clear-completions ()
314+ " Clear the output of completions from the comint buffer."
297315 (when (get-buffer root--completion-buffer-name)
298316 (with-current-buffer root--completion-buffer-name
299317 (erase-buffer ))))
300318
301319(defun root--get-partial-input (beg end )
320+ " Get the partially entered input from the buffer starting from BEG to END."
302321 (buffer-substring-no-properties beg end))
303322
304323(defun root--remove-ansi-escape-codes (string )
324+ " Remove ansi escape codes from STRING."
305325 (let ((regex " \\ [[0-9;^kD]+m?" ))
306326 (replace-regexp-in-string regex " " string)))
307327
308328(defun root--get-completions-from-buffer ()
329+ " Get the list of possible completions from the comint buffer."
309330 (with-current-buffer root--completion-buffer-name
310331 (while (not comint-redirect-completed)
311332 (sleep-for 0.01 ))
312333 (setq root--keywords (split-string (root--remove-ansi-escape-codes (buffer-string )) " \n " ))))
313334
314335(defun root--comint-dynamic-completion-function ()
336+ " Return the list of possible completions. UNUSED."
315337 (cl-return )
316338 (when-let* ((bound (bounds-of-thing-at-point 'symbol ))
317339 (beg (car bound))
@@ -333,7 +355,7 @@ rcfiles."
333355
334356;;;### autoload
335357(defun org-babel-execute:root (body params )
336- " Execute a C++ org-mode source code block with ROOT."
358+ " Execute a C++ ` org-mode' source code block (BODY) with PARAMS in ROOT REPL ."
337359 (message " Executing C++ source code block with ROOT " )
338360 (print params)
339361 (let ((session (root--pluck-item :session params)))
@@ -342,18 +364,23 @@ rcfiles."
342364 (root--org-babel-execute-temp-session body params))))
343365
344366(defun root--org-babel-cmdline-clean-result (string filename )
367+ " Clean the STRING of running ROOT from the command line using FILENAME."
345368 (replace-regexp-in-string (format " \n Processing %s ...\n " filename) " " string))
346369
347370(defun root--org-babel-cmdline-simple-wrapper (body func )
371+ " Wrap the BODY within a void FUNC call."
348372 (format " void %s () {\n %s \n } " func body))
349373
350374(defun root--org-babel-kill-session ()
375+ " Kill the current ROOT session."
351376 (root--send-string root-buffer-name " .q" ))
352377
353378(defun root--org-babel-start-session ()
379+ " Start a root session."
354380 (root--remembering-position (root-run)))
355381
356382(defun root--org-babel-execute-temp-session (body params )
383+ " Run BODY in a temporary session, ignoring PARAMS."
357384 (ignore params)
358385 (unwind-protect
359386 (progn
@@ -366,6 +393,7 @@ rcfiles."
366393 (root--org-babel-kill-session)))
367394
368395(defun root--org-babel-execute-session (session body params )
396+ " Run BODY in SESSION, ignoring PARAMS."
369397 (ignore params)
370398 (let ((root-buffer-name (root--make-earmuff session)))
371399 (unless (get-buffer root-buffer-name)
@@ -376,6 +404,7 @@ rcfiles."
376404 (root--get-last-output)))
377405
378406(defun root--org-babel-execute-no-session (body params )
407+ " Run BODY with no session, ignoring PARAMS."
379408 (ignore params)
380409 (let* ((file (org-babel-temp-file " root" " .C" ))
381410 (func (replace-regexp-in-string " .C" " " (car (last (split-string file " /" )))))
@@ -391,80 +420,80 @@ rcfiles."
391420
392421;;;### autoload
393422(defun root-run ()
394- " Run an inferior instance of ROOT"
423+ " Run an inferior instance of ROOT. "
395424 (interactive )
396425 (funcall (root--ctfun 'start-terminal )))
397426
398427;;;### autoload
399428(defun root-run-other-window ()
400- " Run an inferior instance of ROOT in an different window"
429+ " Run an inferior instance of ROOT in an different window. "
401430 (interactive )
402431 (split-window-sensibly )
403432 (other-window 1 )
404433 (root-run))
405434
406435(defun root-switch-to-repl ()
407- " Switch to the ROOT REPL"
436+ " Switch to the ROOT REPL. "
408437 (interactive )
409438 (let ((win (get-buffer-window root-buffer-name)))
410439 (if win
411440 (select-window win)
412441 (switch-to-buffer root-buffer-name))))
413442
414443(defun root-eval-region (beg end )
415- " Evaluate a region in ROOT"
444+ " Evaluate a region from BEG to END in ROOT. "
416445 (interactive " r" )
417446 (kill-ring-save beg end)
418447 (let ((string (format " %s " (buffer-substring beg end))))
419448 (root-switch-to-repl)
420449 (root--send-string root-buffer-name string)))
421450
422451(defun root-eval-string (string )
423- " Send and evaluate a string in the ROOT REPL."
452+ " Send and evaluate a STRING in the ROOT REPL."
424453 (root--send-string root-buffer-name string))
425454
426455(defun root-eval-line ()
427- " Evaluate this line in ROOT"
456+ " Evaluate this line in ROOT. "
428457 (interactive )
429458 (root--remembering-position
430459 (let ((beg (progn (beginning-of-line ) (point )))
431460 (end (progn (end-of-line ) (point ))))
432461 (root-eval-region beg end))))
433462
434463(defun root-eval-defun ()
435- " Evaluate a function in ROOT"
464+ " Evaluate a function in ROOT. "
436465 (interactive )
437466 (root--remembering-position
438467 (c-mark-function )
439468 (root-eval-region (region-beginning ) (region-end ))))
440469
441470(defun root-eval-defun-maybe ()
442- " Evaluate a defun in ROOT if in declaration else just the line"
471+ " Evaluate a defun in ROOT if in declaration else just the line. "
443472 (interactive )
444473 (condition-case err
445474 (root-eval-defun)
446475 ('error (progn (ignore err)
447476 (root-eval-line)))))
448477
449478(defun root-eval-buffer ()
450- " Evaluate the buffer in ROOT"
479+ " Evaluate the buffer in ROOT. "
451480 (interactive )
452481 (root--remembering-position
453482 (root-eval-region (point-min ) (point-max ))))
454483
455484(defun root-eval-file (filename )
456- " Evaluate a file in ROOT"
485+ " Evaluate FILENAME in ROOT. "
457486 (interactive " fFile to load: " )
458487 (comint-send-string root-buffer-name (concat " .U " filename " \n " ))
459488 (comint-send-string root-buffer-name (concat " .L " filename " \n " )))
460489
461490(defun root-change-working-directory (dir )
462- " Change the working directory of ROOT"
491+ " Change the working directory of ROOT to DIR. "
463492 (interactive " DChange to directory: " )
464493 (comint-send-string root-buffer-name (concat " gSystem->cd(\" " (expand-file-name dir) " \" )\n " )))
465494
466495(defun root-list-input-history ()
467- " List the history of previously entered statements"
496+ " List the history of previously entered statements. "
468497 (interactive )
469498 (comint-dynamic-list-input-ring ))
470499
0 commit comments