diff --git a/src/emacs.py b/src/emacs.py new file mode 100644 index 0000000..7eb74cf --- /dev/null +++ b/src/emacs.py @@ -0,0 +1,72 @@ +""" +IRPF90 is a Fortran90 preprocessor written in Python for programming using +the Implicit Reference to Parameters (IRP) method. +Copyright (C) 2009 Anthony SCEMAMA + +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 2 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, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +This script handles the files needed to use the IRPF90 Emacs mode. This +config used the emacs package `use-package` + (https://jwiegley.github.io/use-package/). +The user is responsible for its installing and configuring it. +Contributed by Ramon L. Panades-Barrueta (https://tinyurl.com/y6qvjmxg ). + +""" +import os +import shutil + + +def install(): + """Installs the irp-mode in Emacs""" + # Check user Emacs config + emacs_init = [*map(lambda x: os.environ["HOME"] + x, + ["/.emacs.d/inil.el", "/.emacs"])] + + emacs_dir = os.environ["HOME"] + "/.emacs.d" + os.makedirs(f"{emacs_dir}/lib", exist_ok=True) + + emacs = emacs_init[0] if os.access( + emacs_init[0], os.F_OK) else emacs_init[1] + + # Add support for irp-mode and Yasnippets + with open(emacs, "a") as emfil: + emfil.write("\n") + emfil.write(";; Use Yasnippets\n") + emfil.write("(use-package yasnippet\n") + emfil.write(" :ensure t\n") + emfil.write(" :config\n") + emfil.write(" (yas-global-mode 1))\n") + emfil.write("\n") + emfil.write(";; Use irp-mode\n") + emfil.write("(use-package irp-mode\n") + emfil.write(r""" :mode ("\\.irp.f\\'")""") + emfil.write(f"""\n :load-path "{emacs_dir}/lib")\n""") + + # Copy irp-mode files + workd = os.path.abspath(os.path.dirname(__file__)) + with open(f"{workd}/irp-mode.el", 'r') as f_in: + with open(f"{emacs_dir}/lib/irp-mode.el", 'w') as f_out: + f_out.write(f_in.read()) + + # Snippets + os.makedirs(f"{emacs_dir}/snippets/irp-mode/", exist_ok=True) + snips = os.listdir(f"{workd}/irp_snippets") + for fil in snips: + shutil.copy(f"{workd}/irp_snippets/{fil}", + f"{emacs_dir}/snippets/irp-mode/") + + +if __name__ == "__main__": + install() diff --git a/src/irp-mode.el b/src/irp-mode.el new file mode 100644 index 0000000..cd5d837 --- /dev/null +++ b/src/irp-mode.el @@ -0,0 +1,41 @@ +;;; irp-mode.el --- A major mode for dealing with IRPF90 files + +;;; Commentary: +;; An attempt to support Scemama's IRPF90 in Emacs + +;;; Code: + +;; Define IRPF90 extended FORTRAN syntax + +(defvar irp-font-lock-keywords) + +(setq irp-font-lock-keywords + (let* ( + ;; Define different keywords + (x-keywords '("BEGIN_PROVIDER" "END_PROVIDER" "ASSERT" + "FREE" "PROVIDE" "BEGIN_TEMPLATE" + "END_TEMPLATE" "BEGIN_SHELL" + "END_SHELL" "IRP_IF" "IRP_ELSE")) + (x-types '("double precision" "integer")) + (x-comments '("BEGIN_DOC" "END_DOC")) + + ;; Generate regex + (x-keywords-regexp (regexp-opt x-keywords 'words)) + (x-types-regexp (regexp-opt x-types 'words)) + (x-comments-regexp (regexp-opt x-comments 'words))) + + `( + (,x-types-regexp . font-lock-type-face) + (,x-keywords-regexp . font-lock-preprocessor-face) + (,x-comments-regexp . font-lock-comment-face) + ))) + +;;;###autoload +(define-derived-mode irp-mode f90-mode "irp mode" + "Major mode for editing IRPF90 files." + :syntax-table nil + :abbrev-table nil + (font-lock-add-keywords nil irp-font-lock-keywords)) + +(provide 'irp-mode) +;;; irp-mode.el ends here diff --git a/src/irp_snippets/irp_as b/src/irp_snippets/irp_as new file mode 100644 index 0000000..443fd79 --- /dev/null +++ b/src/irp_snippets/irp_as @@ -0,0 +1,5 @@ +# -*- mode: snippet -*- +# name: ASSERT +# key: ass +# -- +ASSERT ($0) \ No newline at end of file diff --git a/src/irp_snippets/irp_doc b/src/irp_snippets/irp_doc new file mode 100644 index 0000000..8ddc080 --- /dev/null +++ b/src/irp_snippets/irp_doc @@ -0,0 +1,7 @@ +# -*- mode: snippet -*- +# name: BEGIN_DOC ... END_DOC +# key: bc +# -- +BEGIN_DOC +! $0 +END_DOC \ No newline at end of file diff --git a/src/irp_snippets/irp_prov b/src/irp_snippets/irp_prov new file mode 100644 index 0000000..237ef51 --- /dev/null +++ b/src/irp_snippets/irp_prov @@ -0,0 +1,7 @@ +# -*- mode: snippet -*- +# name: BEGIN_PROVIDER ... END_PROVIDER +# key: bp +# -- +BEGIN_PROVIDER [${1:integer}, ${2:var}] + $0 +END_PROVIDER \ No newline at end of file diff --git a/src/irp_snippets/irp_sh b/src/irp_snippets/irp_sh new file mode 100644 index 0000000..952c90a --- /dev/null +++ b/src/irp_snippets/irp_sh @@ -0,0 +1,8 @@ +# -*- mode: snippet -*- +# -*- mode: snippet -*- +# name: BEGIN_SHELL ... END_SHELL +# key: bsh +# -- +BEGIN_SHELL [ ${1:/bin/bash} ] + $0 +END_SHELL \ No newline at end of file