;;; eev-brurl.el -- `code-brxxx' functions, based on Emacs's
;;; `browse-url', and on eev's `code-c-d'; these functions generate
;;; Lisp code with definitions and evaluate it, just like `code-c-d'
;;; does. Note: this file contains protype-ish code, that is intented
;;; to replace some parts of eev in a near future - BUT THIS IS NOT
;;; YET LOADED BY DEFAULT!


;; (code-brfile 'find-fline                   :local 'brfl)
;; (code-brfile 'find-xpdfpage                :local 'brxpdf :dired 'brxpdfd)
;; (code-brurl  'find-firefox  :remote 'brm  :local 'brml   :dired 'brmd)

;; (find-efunction 'ee-template)
;; (find-elnode "Index" "* defvaralias:")
;; ee-hp

;; (find-efunctionpp 'brm)
;; (find-efunctionpp 'brml)
;; (find-efunctionpp 'brmd)
;; (find-efunction 'ee-concat)

;; (format "%s" 'foo)
;; (format "%s" "foo")
;; (format "%s" 123)
;; (ee-intern "foo%s" 123)

;; Internal functions (generic)
;;
(defun ee-template0 (templatestr)
  "Example:  (ee-template0 \"a{(+ 2 3)}b\")  -->  \"a5b\""
  (replace-regexp-in-string
   "{\\([^{}]+\\)}"
   (lambda (_code_) (format "%s" (eval (read (substring _code_ 1 -1)))))
   templatestr 'fixedcase))

(defun ee-intern (string &rest objects)
  "The result of (format STRING OBJECTS), converted to a symbol"
  (intern (apply 'format string objects)))



;;;                _            _               
;;;   ___ ___   __| | ___      | |__  _ ____/\__
;;;  / __/ _ \ / _` |/ _ \_____| '_ \| '__\    /
;;; | (_| (_) | (_| |  __/_____| |_) | |  /_  _\
;;;  \___\___/ \__,_|\___|     |_.__/|_|    \/  
;;;                                             

;; Internal functions (specific, low-level)
;;
(defun ee-code-brfile-:local (filefunction brxxxl)
  (ee-template0 "
\(defun {brxxxl} (url &rest ignore)
  \"Apply `{filefunction}' on the local file name associated to URL.\"
  (interactive (browse-url-interactive-arg \"URL: \"))
  (setq browse-url-browser-function '{brxxxl})
  (let ((fname (eeurl-u-to-f url)))
    (list '{filefunction} fname '-> ({filefunction} fname))))\n"))

(defun ee-code-brfile-:dired (filefunction brxxxd)
  (ee-template0 "
\(defun {brxxxd} (&rest ignore)
  \"Apply `{filefunction}' on the dired file at point.\"
  (interactive)
  (let ((fname (eeurl-dired-file-name-at-point)))
    (message (format \"%S\" (list '{filefunction} fname '->
                                ({filefunction} fname))))))\n"))

(defun ee-code-brurl-:remote (urlfunction brxxx)
  (ee-template0 "
\(defun {brxxx} (url &rest ignore)
  \"Apply `{urlfunction}' on URL.\"
  (interactive (browse-url-interactive-arg \"URL: \"))
  (setq browse-url-browser-function '{brxxx})
  (list '{urlfunction} url '->
        ({urlfunction} url)))\n"))

(defun ee-code-brurl-:local (urlfunction brxxxl)
  (ee-template0 "
\(defun {brxxxl} (url &rest ignore)
  \"Apply `{urlfunction}' on the local url associated to URL.\"
  (interactive (browse-url-interactive-arg \"URL: \"))
  (setq browse-url-browser-function '{brxxxl})
  (setq url (eeurl-u-to-u-l url))
  (list '{urlfunction} url '->
        ({urlfunction} url)))\n"))

(defun ee-code-brurl-:dired (urlfunction brxxxd)
  (ee-template0 "
\(defun {brxxxd} (&rest ignore)
  \"Apply `{urlfunction}' on the url of the dired file at point.\"
  (interactive)
  (let ((url (eeurl-f-to-u (eeurl-dired-file-name-at-point))))
    (message
     (format \"%S\"
             (list '{urlfunction} url '->
                   ({urlfunction} url))))))\n"))

;; Internal functions (specific, high-level)
;;
(defun ee-code-brfile (f &optional key brf &rest rest)
"Generate code for a family of functions from a function that operates on files"
  (if (not key) ""
    (concat (funcall (ee-intern "ee-code-brfile-%s" key) f brf)
            (apply              'ee-code-brfile          f rest))))

(defun ee-code-brurl (f &optional key brf &rest rest)
"Generate code for a family of functions from a function that operates on URLs"
  (if (not key) ""
    (concat (funcall (ee-intern "ee-code-brurl-%s" key) f brf)
            (apply              'ee-code-brurl          f rest))))

;; Top-level functions
;;
(defun code-brfile (f &optional key brf &rest rest)
  "Define a family of brxxx functions from a function that operates on files"
  (eval (ee-read (apply 'ee-code-brfile f key brf rest))))

(defun code-brurl  (f &optional key brf &rest rest)
  "Define a family of brxxx functions from a function that operates on files"
  (eval (ee-read (apply 'ee-code-brurl  f key brf rest))))

(defun find-code-brfile (&rest args)
  (find-estring-elisp (apply 'ee-code-brfile args)))

(defun find-code-brurl  (&rest args)
  (find-estring-elisp (apply 'ee-code-brurl  args)))

;;;                _            _                 _            _       
;;;   ___ ___   __| | ___      | |__  _ ____/\__ | |_ ___  ___| |_ ___ 
;;;  / __/ _ \ / _` |/ _ \_____| '_ \| '__\    / | __/ _ \/ __| __/ __|
;;; | (_| (_) | (_| |  __/_____| |_) | |  /_  _\ | ||  __/\__ \ |_\__ \
;;;  \___\___/ \__,_|\___|     |_.__/|_|    \/    \__\___||___/\__|___/
;;;                                                                    

;; (find-code-brurl 'find-w3m :remote 'brw :local 'brwl :dired 'brwd)
        (code-brurl 'find-w3m :remote 'brw :local 'brwl :dired 'brwd)

;; (find-code-brurl 'find-djvupage :local 'brdjvul :dired 'brdjvud)
        (code-brurl 'find-djvupage :local 'brdjvul :dired 'brdjvud)

;; (find-eev "eev-browse-url.el" "many-br-functions")
;;
(code-brfile 'find-fline
             :local         'brfl)
(code-brfile 'eecd
             :local         'brcdl)
(code-brfile 'find-pspage
             :local         'brgvl
             :dired         'brgvd)
(code-brfile 'find-dvipage
             :local         'brxdvil
             :dired         'brxdvid)
(code-brfile 'find-xpdfpage
             :local         'brxpdfl
             :dired         'brxpdfd)
(code-brfile 'find-pdftotext
             :local         'brpdftxtl
             :dired         'brpdftxtd)
(code-brfile 'find-djvupage
             :local         'brdjvul
             :dired         'brdjvud)

(code-brurl  'browse-url-firefox
             :remote        'brm
             :local         'brml
             :dired         'brmd)
(code-brurl  'find-w3m
             :remote        'brw
             :local         'brwl
             :dired         'brwd)
(code-brurl  'eepsne
             :remote        'brp)
(code-brurl  'eetmpwget
             :remote        'brtmpwget)

;; (eeurl-define-from :fname->action: 'find-djvupage
;;                    :local:         'brdjvul
;;                    :dired:         'brdjvud)
;; (eeurl-define-from :url->action:   'browse-url-firefox
;;                    :remote:        'brm
;;                    :local:         'brml
;;                    :dired:         'brmd)



;; (find-efunction 'eeurl-dired-file-name-at-point)
;; ee-dired-file-name-at-point
;; ee-url-to-local-file
;; ee-url-to-local-url
;; ee-url-to-local-file-url
;; ee-file-to-fileurl
;; ee-url-to-fileurl
;; snarffname
;; snarfurl

;; (find-efunctionpp 'brxpdfl)
;; (find-estring-elisp (ee-code-brfile-:local: "find-xpdfpage" "brxpdfl"))
;; (find-estring-elisp (ee-code-brfile-:local: 'find-xpdfpage 'brxpdfl))
;;
;; (find-efunctionpp 'brxpdfd)
;; (find-estring-elisp (ee-code-brfile-:dired: 'find-xpdfpage "brxpdfd"))
;;
;; (find-efunctionpp 'brm)
;; (find-code-brurl 'browse-url-firefox :remote: 'brm)
;; (find-efunctionpp 'brml)
;; (find-efunctionpp 'brmd)

;; (find-efunctionpp 'find-djvupage)
;; (find-efunctionpp 'brdjvul)
;; (find-efunctionpp 'brdjvud)
;; (find-efunctionpp 'browse-url-firefox)
;; (find-efunctionpp 'brm)
;; (find-efunctionpp 'brml)
;; (find-efunctionpp 'brmd)





;; Local Variables:
;; coding:            raw-text-unix
;; ee-anchor-format:  "\253%s\273"
;; ee-anchor-format:  "defun %s "
;; no-byte-compile:   t
;; End: