Skip to content

Instantly share code, notes, and snippets.

@Tekki
Last active September 21, 2024 07:09
Show Gist options
  • Save Tekki/e94421dca29db13347180dfaa95859b2 to your computer and use it in GitHub Desktop.
Save Tekki/e94421dca29db13347180dfaa95859b2 to your computer and use it in GitHub Desktop.
COBOL copybooks in Emacs
;;; experiment with COBOL copybooks in Emacs
;; On a line with a COPY command, call `cobol/show-copybook' or
;; `cobol/hide-copybook'.
;;
;; To show or hide all copybooks in the buffer, use
;; `cobol/show-all-copybooks' and `cobol/hide-all-copybooks'.
;;
;; If the copybooks are located in a different folder, set variable
;; `cobol/copybook-directory'.
;;
;; For users of `evil-mode', the are some shortcuts that can be
;; activated by uncommenting the last lines of this file.
(defvar cobol/copybook-directory nil
"Additional directory to search for copybooks.")
(defvar cobol/copybook-regex "copy[ ]+\['\"\]"
"Regular expression to identify lines with copybooks.")
(defun cobol/hide-all-copybooks ()
"Hide all copybooks in the current buffer."
(interactive)
(dolist (ol (car (overlay-lists)))
(when (overlay-get ol 'copybook)
(delete-overlay ol))))
(defun cobol/hide-copybook ()
"Hide the copybook for the current line."
(interactive)
(dolist (ol (overlays-in (line-end-position) (1+ (line-end-position))))
(when (overlay-get ol 'copybook)
(delete-overlay ol))))
(defun cobol/show-all-copybooks ()
"Show all copybooks imported into the current buffer."
(interactive)
(save-excursion
(goto-char (point-min))
(while (search-forward-regexp cobol/copybook-regex nil (point-max))
(cobol/do--show-copybook))))
(defun cobol/show-copybook ()
"Show the copybook imported at the current line."
(interactive)
(save-excursion
(goto-char (line-beginning-position))
(if (search-forward-regexp cobol/copybook-regex
(line-end-position) t)
(cobol/do--show-copybook )
(message "No copybook on this line."))))
(defun cobol/do--show-copybook ()
"Import and display the content of the copybook file."
(require 'ffap)
(let ((ol (make-overlay (line-end-position) (line-end-position)))
(filename (ffap-string-at-point))
copybook
content)
(setq copybook
(if (file-regular-p filename)
filename
(when (and cobol/copybook-directory
(file-regular-p (expand-file-name filename cobol/copybook-directory)))
(expand-file-name filename cobol/copybook-directory))))
(with-temp-buffer
(if copybook
(insert-file-contents copybook)
(insert " *> File not found!"))
(setq content (replace-regexp-in-string "\n$" "" (buffer-string))))
(overlay-put ol 'after-string (format "\n%s" content))
(overlay-put ol 'copybook t)))
;;; goodies for evil-mode
;; (evil-define-key 'normal 'global
;; (kbd "]c") 'cobol/show-copybook
;; (kbd "[c") 'cobol/hide-copybook)
;; (evil-ex-define-cmd "cbh[hide-copybooks]" 'cobol/hide-all-copybooks)
;; (evil-ex-define-cmd "cbs[how-copybooks]" 'cobol/show-all-copybooks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment