File 2715-Make-Emacs-erlang-mode-download-man-pages-if-man-pag.patch of Package erlang

From ff07b4ae4c7633d5d9ff374d85c8818fb68cf88a Mon Sep 17 00:00:00 2001
From: Kjell Winblad <kjellwinblad@gmail.com>
Date: Wed, 9 Oct 2019 15:45:12 +0200
Subject: [PATCH 05/10] Make Emacs erlang-mode download man pages if man pages
 can't be found

This commit changes the Emacs lisp erlang-mode functions
erlang-man-function and erlang-man-function-no-prompt so that the user
will be asked if Erlang man pages should be downloaded when they can't
be found in the system.
---
 lib/tools/emacs/erlang.el | 121 +++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 108 insertions(+), 13 deletions(-)

diff --git a/lib/tools/emacs/erlang.el b/lib/tools/emacs/erlang.el
index 4e30a0eaf8..835d09763b 100644
--- a/lib/tools/emacs/erlang.el
+++ b/lib/tools/emacs/erlang.el
@@ -91,11 +91,23 @@
   "The version number of Erlang mode.")
 
 (defcustom erlang-root-dir nil
-  "The directory where the Erlang system is installed.
-The name should not contain the trailing slash.
+  "The directory where the Erlang man pages are installed. The
+name should not contain a trailing slash.
 
 Should this variable be nil, no manual pages will show up in the
-Erlang mode menu."
+Erlang mode menu unless man pages have been downloaded by Erlang
+mode (see below).
+
+You can download the Erlang man pages automatically by placing
+the following lines in your Emacs init file or by executing the
+Emacs command `M-x erlang-man-download-ask RET' (the download URL
+can be customized with the Emacs variable
+erlang-man-download-url):
+
+    (require 'erlang)
+    (erlang-man-download)
+
+"
   :group 'erlang
   :type '(restricted-sexp :match-alternatives (stringp 'nil))
   :safe (lambda (val) (or (eq nil val) (stringp val))))
@@ -1058,6 +1070,7 @@ behaviour.")
     (define-key map "\C-c\C-y"  'erlang-clone-arguments)
     (define-key map "\C-c\C-a"  'erlang-align-arrows)
     (define-key map "\C-c\C-z"  'erlang-shell-display)
+    (define-key map "\C-c\C-d"  'erlang-man-function-no-prompt)
     map)
   "Keymap used in Erlang mode.")
 (defvar erlang-mode-abbrev-table nil
@@ -1916,7 +1929,7 @@ The format is described in the documentation of `erlang-man-dirs'."
       (setq dir (cond ((nth 2 (car dir-list))
                        ;; Relative to `erlang-root-dir'.
                        (and (stringp erlang-root-dir)
-                            (erlang-man-dir (nth 1 (car dir-list)))))
+                            (erlang-man-dir (nth 1 (car dir-list)) t)))
                       (t
                        ;; Absolute
                        (nth 1 (car dir-list)))))
@@ -1934,17 +1947,88 @@ The format is described in the documentation of `erlang-man-dirs'."
       '(("Man Pages"
          (("Error! Why?" erlang-man-describe-error)))))))
 
-(defun erlang-man-dir (subdir)
-  (let ((default_man_dir (concat (file-name-as-directory erlang-root-dir)
-                                 (file-name-as-directory "lib")
-                                 (file-name-as-directory "erlang") subdir)))
-    (if (or (equal erlang-root-dir nil) (file-directory-p default_man_dir))
-        default_man_dir
-      (concat (file-name-as-directory erlang-root-dir) subdir)
+
+(defcustom erlang-man-download-url "http://erlang.org/download/otp_doc_man_22.1.tar.gz"
+  "The URL from which the erlang-man-download function will
+  download Erlang man pages ")
+
+(defun erlang-man-user-local-emacs-dir ()
+  "Returns the directory where man pages that are downloaded by
+the functions erlang-man-download and erlang-man-download-ask are
+stored."
+  (concat (file-name-as-directory (locate-user-emacs-file "cache"))
+          (file-name-as-directory "erlang_mode_man_pages"))
+  )
+
+(defun erlang-man-download (&optional download-url-param)
+  "Downloads the Erlang man pages into the
+\"cache/erlang_mode_man_pages\" subdirectory under the user's
+Emacs directory, if the man pages haven't been downloaded
+already. The URL from which the man pages are downloaded can be
+configured with the variable \"erlang-man-download-url\""
+  (interactive)
+  (let ((download-url (or download-url-param erlang-man-download-url))
+        (downloaded-man-dir (erlang-man-user-local-emacs-dir))
+        (downloaded-man-check-dir (concat
+                                   (file-name-as-directory (erlang-man-user-local-emacs-dir))
+                                   (file-name-as-directory "man"))))
+    (if (file-directory-p downloaded-man-check-dir)
+        downloaded-man-dir
+      (let ((man-file (concat (file-name-as-directory downloaded-man-dir) "man.tar.gz")))
+        (message "Downloading: %s to %s" download-url man-file)
+        (require 'url)
+        (mkdir downloaded-man-dir t)
+        (url-copy-file download-url man-file t)
+                                        ; url-copy-file unpacks the
+                                        ; zip archive (at least on my
+                                        ; system) but this behavior is
+                                        ; undocumented so do a tar
+                                        ; with the z flag as well
+        (message "Note that %s will only be unpacked automatically if your system has the tar tool in its path" man-file)
+        (shell-command (format "tar -x -z -f %s -C %s" man-file downloaded-man-dir))
+        (message "The error message above can be ignored if everything works fine")
+        (shell-command (format "tar -x -f %s -C %s" man-file downloaded-man-dir))
+        (message "Restarting erlang-mode")
+        (erlang-mode)
+        downloaded-man-dir
+        )
       )
     )
   )
 
+(defun erlang-man-download-ask (&optional subdir)
+  "Downloads the Erlang man pages into the
+\"cache/erlang_mode_man_pages\" subdirectory under the user's
+Emacs directory, if the man pages haven't been downloaded
+already. This function ask the user to confirm before downloading
+and lets the user edit the download URL. The function
+erlang-man-download downloads the man pages without prompting the
+user."
+  (interactive)
+  (if (y-or-n-p "Could not find Erlang man pages on your system. Do you want to download them?")
+      (let ((download-url (read-string "URL to download man pages from:" erlang-man-download-url)))
+        (concat (directory-file-name (erlang-man-download download-url)) (or subdir "")))))
+
+
+(defun erlang-man-dir (subdir &optional no_download)
+  (message subdir)
+  (let ((default-man-dir (if erlang-root-dir
+                             (concat (directory-file-name (concat
+                                                           (file-name-as-directory erlang-root-dir)
+                                                           (file-name-as-directory "lib")
+                                                           (file-name-as-directory "erlang")))
+                                     subdir)))
+        (alt-man-dir (if erlang-root-dir
+                         (concat (directory-file-name erlang-root-dir) subdir)))
+        (downloaded-man-dir (erlang-man-user-local-emacs-dir)))
+    (if (and erlang-root-dir (file-directory-p default-man-dir))
+        default-man-dir
+      (if (and erlang-root-dir (file-directory-p alt-man-dir))
+          alt-man-dir
+        (if (file-directory-p downloaded-man-dir)
+            (concat (directory-file-name downloaded-man-dir) subdir)
+          (and (not no_download) (erlang-man-download-ask subdir)))))))
+
 ;; Should the menu be to long, let's split it into a number of
 ;; smaller menus.  Warning, this code contains beautiful
 ;; destructive operations!
@@ -2211,8 +2295,19 @@ the site init file:
 For example:
     (setq erlang-root-dir \"/usr/local/erlang\")
 
-After installing the line, kill and restart Emacs, or restart Erlang
-mode with the command `M-x erlang-mode RET'.")))
+Alternatively, you can download the Erlang man pages
+automatically by placing the following lines in your Emacs init
+file or by executing the Emacs command `M-x
+erlang-man-download-ask RET' (the download URL can be customized
+with the Emacs variable erlang-man-download-url):
+
+    (require 'erlang)
+    (erlang-man-download)
+
+After installing the line/lines in your Emacs init file or after
+running the command `M-x erlang-man-download-ask RET', kill and
+restart Emacs, or restart Erlang mode with the command `M-x
+erlang-mode RET'.")))
 
 ;; Skeleton code:
 
-- 
2.16.4

openSUSE Build Service is sponsored by