|
58 | 58 | ;;; Notes:
|
59 | 59 | ;;
|
60 | 60 | ;; TODO:
|
61 |
| -;; - closing `]' will go back to the start for some reason |
62 | 61 | ;; - only highlight transforming and eval functions after parentheses added
|
63 | 62 | ;; - I need to go through and actually test things at some point, but I should probably prioritise releasing this and doing small touch-ups later
|
64 | 63 | ;; - Electric indent mode (see below)
|
|
367 | 366 |
|
368 | 367 |
|
369 | 368 |
|
370 |
| -;;; Indentation and Electricity |
371 |
| - |
372 |
| -;; Handle indentation for SPL code. |
373 |
| -;; |
374 |
| -;; Inspired by similar functions from groovy-mode.el: |
375 |
| -;; - https://github.com/Groovy-Emacs-Modes/groovy-emacs-modes/blob/7b8520b2/groovy-mode.el#L751-L770 |
376 |
| -;; - https://github.com/Groovy-Emacs-Modes/groovy-emacs-modes/blob/7b8520b2/groovy-mode.el#L837-L986 |
377 |
| -;; |
378 |
| -;; Refs: |
379 |
| -;; - https://www.gnu.org/software/emacs/manual/html_node/elisp/Position-Parse.html |
380 |
| -;; - https://www.gnu.org/software/emacs/manual/html_node/elisp/Parser-State.html |
381 |
| -;; - https://www.gnu.org/software/emacs/manual/html_node/eintr/nth.html |
382 |
| - |
383 |
| -(defcustom splunk-indent-offset 4 |
384 |
| - "Indentation amount for Splunk." |
385 |
| - :type 'natnum |
386 |
| - :safe #'natnum |
387 |
| - :group 'splunk-mode) |
388 |
| - |
389 |
| -;; Always indent with spaces |
390 |
| -(setq-local indent-tabs-mode nil) |
391 |
| - |
392 |
| - |
393 |
| -;; NOTE: I don't know how this function works. I had to reverse the logic for it |
394 |
| -;; to work as expected, but I would have thought it should work without the `not' |
395 |
| -(defun splunk--is-square-bracket-p (pos) |
396 |
| - "Check if the parenthesis at `pos' is a square bracket." |
397 |
| - (let ((parse-state (parse-partial-sexp (point-min) pos))) |
398 |
| - (not (= (char-after (nth 8 parse-state)) ?\[)))) |
399 |
| - |
400 |
| -(defun splunk--effective-depth (pos) |
401 |
| - "Get effective depth at `pos' in SPL query." |
402 |
| - (let ((paren-depth 0) |
403 |
| - (syntax (syntax-ppss pos)) |
404 |
| - (current-line (line-number-at-pos pos))) |
405 |
| - (save-excursion |
406 |
| - ;; Keep going whilst we're inside parentheses |
407 |
| - (while (> (nth 0 syntax) 0) |
408 |
| - ;; Go to the most recent enclosing open parenthesis |
409 |
| - (goto-char (nth 1 syntax)) |
410 |
| - |
411 |
| - ;; Count this paren, but only if it was on another line |
412 |
| - ;; TODO: doc: or if it was open square |
413 |
| - (let ((new-line (line-number-at-pos (point)))) |
414 |
| - (unless (or (= new-line current-line) |
415 |
| - (splunk--is-square-bracket-p (point))) |
416 |
| - (setq paren-depth (1+ paren-depth)) |
417 |
| - (setq current-line new-line))) |
418 |
| - |
419 |
| - (setq syntax (syntax-ppss (point))))) |
420 |
| - paren-depth)) |
421 |
| - |
422 |
| -(defun splunk--current-line () |
423 |
| - "The current line enclosing point." |
424 |
| - (buffer-substring-no-properties |
425 |
| - (line-beginning-position) (line-end-position))) |
426 |
| - |
427 |
| -(defun splunk-indent-line () |
428 |
| - "Indent the current line according to the number of parentheses." |
429 |
| - (interactive) |
430 |
| - ;; TODO: consider indenting line if it is not the first line in the search. |
431 |
| - ;; However, this is a personal stylistic preference rather than standard |
432 |
| - (let* ((current-depth (splunk--effective-depth (line-beginning-position))) |
433 |
| - (current-line (s-trim (splunk--current-line))) |
434 |
| - ) |
435 |
| - |
436 |
| - ;; If this line starts with a closing paren, unindent by one level |
437 |
| - (when (s-starts-with-p "]" current-line) |
438 |
| - (setq current-depth (1- current-depth))) |
439 |
| - |
440 |
| - ;; `current-depth' should never be negative, unless the source |
441 |
| - ;; has unbalanced brackets. Ensure we handle this robustly |
442 |
| - (when (< current-depth 0) |
443 |
| - (setq current-depth 0)) |
444 |
| - |
445 |
| - ;; Indent region! |
446 |
| - (let ((indent-level current-depth)) |
447 |
| - (indent-line-to (* splunk-indent-offset indent-level))) |
448 |
| -)) |
449 |
| - |
450 |
| -;; Handle automatic new lines TODO OOOO)))OOOOOOOOOOOOO doc |
451 |
| - |
452 |
| - |
453 |
| -;; https://www.emacswiki.org/emacs/Electricity |
454 |
| - |
455 |
| -(defvar electric-pair-inhibit-predicate) |
456 |
| -(defvar electric-pair-skip-self) ;; TODO |
457 |
| -(defvar electric-indent-chars) |
458 |
| - |
459 |
| -;; (defun splunk--insert-newline-after-open-bracket () |
460 |
| - ;; "" |
461 |
| - ;; (interactive) |
462 |
| - ;; (insert "[") |
463 |
| - ;; (if (eq (char-before) ?\[) |
464 |
| - ;; (progn (newline) (indent-according-to-mode)))) |
465 |
| - |
466 |
| - |
467 |
| - |
468 |
| -(defun splunk--insert-pipe () |
469 |
| - "Insert a new line and auto-indent after a `|' character." |
470 |
| - (interactive) |
471 |
| - (insert "|") |
472 |
| - (newline)) |
473 |
| - |
474 |
| -(defun splunk--insert-open-bracket () |
475 |
| - "Insert a new line and auto-indent after a `\[' character." |
476 |
| - (interactive) |
477 |
| - (insert "[") |
478 |
| - (newline-and-indent)) |
479 |
| - |
480 |
| -;; (defun splunk--electric-indent () |
481 |
| - ;; "Custom Electric Indent rules for Splunk mode." |
482 |
| - ;; (setq-local electric-indent-chars (append electric-indent-chars '(?| ?\[)))) |
483 |
| - |
484 |
| -;; (defun splunk--electric-indent () |
485 |
| - ;; "Custom electric indent function for Splunk mode." |
486 |
| - ;; (interactive) |
487 |
| - ;; (let ((char (char-before))) |
488 |
| - ;; (if (or (eq char ?|) (eq char ?[)) |
489 |
| - ;; (progn |
490 |
| - ;; (newline) |
491 |
| - ;; (indent-according-to-mode)) |
492 |
| - ;; (self-insert-command 1)))) |
493 |
| - |
494 |
| -;; (add-hook 'splunk-mode-hook 'electric-indent-mode-hook) |
495 |
| -;; (add-hook 'splunk-mode-hook 'splunk--electric-indent) |
496 |
| - |
497 |
| -;; (defvar splunk-mode-map |
498 |
| - ;; (let ((m (make-sparse-keymap))) |
499 |
| - ;; (unless (boundp 'electric-indent-chars) |
500 |
| - ;; (define-key m "[" #'splunk--insert-open-bracket) |
501 |
| - ;; (define-key m "|" #'splunk--insert-pipe)) |
502 |
| - ;; m) |
503 |
| - ;; "Keymap used by ‘splunk-mode’.") |
504 |
| - |
505 |
| -(defun splunk--indent-post-self-insert-function () |
506 |
| - "" |
507 |
| - (when (and electric-indent-mode |
508 |
| - (eq (char-before) last-command-event)) |
509 |
| - (cond |
510 |
| - ;; Electric indent inside parens |
511 |
| - |
512 |
| - ) |
513 |
| - )) |
514 |
| - |
515 |
| - |
516 |
| - |
517 |
| -;; (defconst splunk-electric-layout-rules |
518 |
| - ;; (list |
519 |
| - ;; (cons ?\[ . before) |
520 |
| - ;; )) |
521 |
| - |
522 |
| - |
523 |
| - |
524 |
| - |
525 | 369 | ;;; Mode
|
526 | 370 |
|
527 | 371 | ;;;###autoload
|
|
531 | 375 | \\{splunk-mode-map}"
|
532 | 376 | :syntax-table splunk-mode-syntax-table
|
533 | 377 | (setq-local font-lock-defaults '(splunk-font-lock-keywords))
|
534 |
| - ;; (setq-local comment-start "//") |
| 378 | + (setq-local comment-start "//") |
535 | 379 | (setq-local indent-line-function 'splunk-indent-line)
|
536 |
| - ;; (electric-indent-local-mode 1) |
537 |
| - ;; (electric-indent-mode 1) |
538 |
| - ;; set electric characters |
539 |
| - ;; (setq-local electric-indent-chars |
540 |
| - ;; (append "|[]" electric-indent-chars)) |
541 |
| - ;; (setq-local electric-indent-chars (append electric-indent-chars '(?| ?\[))) ;; THIS ONE |
542 |
| - ;; Auto indent on | |
543 |
| - ;; (setq-local electric-indent-chars |
544 |
| - ;; (cons ?| (and (boundp 'electric-indent-chars) |
545 |
| - ;; electric-indent-chars))) |
546 |
| - |
547 |
| - |
548 |
| - |
549 |
| - ;; (add-hook 'post-self-insert-hook |
550 |
| - ;; #'splunk--indent-post-self-insert-function 'append 'local) |
551 |
| - |
552 |
| - ;; (when (boundp 'electric-indent-chars) |
553 |
| - ;; (set (make-local-variable 'electric-indent-chars) '(?| ?\[)) |
554 |
| - ;; (add-hook 'electric-indent-functions #'splunk--electric-indent-function nil t) |
555 |
| - ;; ) |
556 |
| - |
557 |
| - ;; (electric-indent-mode 1) |
558 |
| - ;; Closing square bracket should re-indent (i.e. unindent) the line |
559 |
| - ;; FOR electric-indent-mode THIS ONE |
560 |
| - ;; (when (boundp 'electric-indent-chars) |
561 |
| - ;; (setq-local electric-indent-chars |
562 |
| - ;; (append electric-indent-chars '(?|)))) |
563 |
| - |
564 |
| - ;; FOR electric-layout-mode |
565 |
| - ;; (setq-local electric-layout-rules splunk-electric-layout-rules) |
566 |
| - ;; requires electric-layout-mode |
567 |
| - (setq electric-layout-rules '((?\[ . before) (?| . before))) |
568 |
| - ;; (add-to-list 'electric-layout-rules |
569 |
| - ;; '((?\[ . before) |
570 |
| - ;; (?| . before))) |
571 |
| - |
572 |
| - (add-to-list 'electric-layout-rules '(?| . before)) |
573 |
| - ;; requires electric-indent-mode |
574 |
| - (setq-local electric-indent-chars |
575 |
| - (cons ?\] (and (boundp 'electric-indent-chars) |
576 |
| - electric-indent-chars))) |
577 |
| - |
578 | 380 | (font-lock-fontify-buffer))
|
579 | 381 |
|
580 |
| -;; (add-hook 'lisp-mode-hook #'(lambda () |
581 |
| -;; (local-set-key (kbd "RET") 'newline-and-indent))) |
582 |
| - |
583 | 382 | ;;;###autoload
|
584 | 383 | (add-to-list 'auto-mode-alist '("\\.spl\\'" . splunk-mode))
|
585 | 384 | (add-to-list 'auto-mode-alist '("\\.splunk\\'" . splunk-mode))
|
|
594 | 393 | ;; End:
|
595 | 394 |
|
596 | 395 | ;;; splunk-mode.el ends here
|
597 |
| - |
598 |
| - |
599 |
| - |
600 |
| - |
601 |
| -;; (setq electric-layout-rules ) |
602 |
| - |
603 |
| - |
604 |
| - |
605 |
| - |
606 |
| - |
607 |
| - |
608 |
| -;; electric-indent-just-newline |
609 |
| -;; electric-layout-rules |
610 |
| -;; electric-pair-mode |
0 commit comments