;;; load-module.el --- Load a CIP module into Emacs -*- lexical-binding: t; -*- ;; Copyright (C) 2025 Philip Kaludercic ;; Author: Philip Kaludercic ;; Maintainer: Philip Kaludercic ;; URL: https://wwwcip.cs.fau.de/~oj14ozun/src+etc/load-module.el ;; Version: $Id: load-module.el,v 1.4 2025/05/30 15:00:57 oj14ozun Exp $ ;; Package-Version: 1 ;; Keywords: convenience, unix ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . ;;; Commentary: ;; A Emacs-specific "module load " replacement inside of Emacs. ;;; Code: (defun load-module--exec (&rest commands) "Invoke the \"module\" shell function with COMMANDS. The output will be injected after the point." (let ((cmd (mapconcat #'shell-quote-argument (cons "module" commands) " "))) (unless (zerop (call-process "bash" nil t nil "-c" cmd)) (error "Module command %S failed" cmd)) (goto-char (point-min)))) ;;;###autoload (defun load-module (module) "Inject the environment of a CIP MODULE into Emacs. Note that this is not directly reversible. You will have to manually remove the changes if necessary." (interactive (with-temp-buffer (load-module--exec "spider") (let ((names nil)) (while (search-forward-regexp (rx bol (+ space) (group (+ (any alnum ?-))) ?:) nil t) (push (match-string 1) names)) (list (completing-read "Load module: " names))))) (with-temp-buffer (load-module--exec "show" module) (goto-char (point-min)) (while (search-forward-regexp (rx-let ((str (* (not ?\")))) (rx bol (or (: (group-n 1 (or "prepend_path" "setenv")) "(" ?\" (group-n 2 str) ?\" "," ?\" (group-n 3 str) ?\" ")")) eol)) nil t) (pcase (cl-loop repeat (/ (length (match-data)) 2) for i from 1 when (match-string i) collect it) (`("prepend_path" ,var ,val) (setenv var (concat val ":" (getenv var))) (when (string= var "PATH") (add-to-list 'exec-path val))) (`("setenv" ,var ,val) (setenv var val)))))) (provide 'load-module) ;;; load-module.el ends here