LScarpinati

load packages and utils in slime hrough minibuffer

Sep 22nd, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 9.75 KB | None | 0 0
  1. ;;;; ido-ql-quickload.el
  2. ;;;;
  3. ;;;; ido-ql-quickload is available under the MIT license;
  4. ;;;; see LICENSE for details
  5. ;;;;
  6. ;;;; For a detailed introduction see: README.md
  7. ;;;;
  8. ;;;; Copyright (C) 2013 Sviridov Alexander <[email protected]>
  9. ;;;;
  10. ;;;; Change Log:
  11. ;;;;
  12. ;;;; 1.0 First released
  13. ;;;; 1.1 Added `ido-ql-quickload-suppress-output'
  14.  
  15. ;;;; CONTRIB
  16. ;;;; Added the loadprj function to load packages and asdf project
  17. ;;;; located in personnal *source-registry*
  18. ;;;; AUTHOR of this contrib Luigi Scarpinati <[email protected]>
  19.  
  20. (require 'ido)
  21. (require 'cl)
  22. (require 'slime)
  23. (require 'slime-repl)
  24.  
  25. ;;;=================================================================================================
  26.  
  27. (defgroup ido-ql-quickload nil
  28.   "ql:quickload interface with Ido-style fuzzy matching and ranking heuristics"
  29.   :group 'slime
  30.   :version "1.1"
  31.   :link '(emacs-library-link :tag "Lisp File" "ido-ql-quickload.el"))
  32.  
  33. (defcustom ido-ql-quickload-save-file "~/.ido-ql-quickload"
  34.   "File in which the ido-ql-quickload state is saved between Emacs sessions"
  35.   :type 'string
  36.   :group 'ido-ql-quickload)
  37.  
  38. (defvar ido-ql-quickload--statistics (make-hash-table :test 'equal)
  39.   "Variable in which the ido-ql-quickload statistics is stored")
  40.  
  41. (defvar ido-ql-quickload--history nil
  42.   "Variable in which the ido-ql-quickload history is stored")
  43.  
  44. (defcustom ido-ql-quickload-max-history-size 5
  45.   "Variable that defines the ido-ql-quickload history maximum size"
  46.   :type 'integer
  47.   :group 'ido-ql-quickload)
  48.  
  49. (defcustom ido-ql-quickload-ignore-local-projects-priority nil
  50.   "If `ido-ql-quickload-ignore-local-projects-priority' is T then `ql:quickload' doesn't
  51.   take into account the location of the projects"
  52.   :type 'boolean
  53.   :group 'ido-ql-quickload)
  54.  
  55. (defcustom ido-ql-quickload-suppress-output nil
  56.   "If `ido-ql-quickload-suppress-output' is T then `ql:quickload' doesn't
  57.   switch to `slime-repl' buffer and doesn't print (ql:quickload ...) into it"
  58.   :type 'boolean
  59.   :group 'ido-ql-quickload)
  60.  
  61. (defvar ido-ql-quickload--initialized-p nil)
  62.  
  63. ;;;=================================================================================================
  64.  
  65. (defun ido-ql-quickload-drop-extra-history-items ()
  66.   "Drops extra items from tail of `ido-ql-quickload--history'"
  67.   (when (< ido-ql-quickload-max-history-size (length ido-ql-quickload--history))
  68.     (setf ido-ql-quickload--history
  69.           (butlast ido-ql-quickload--history
  70.                    (- (length ido-ql-quickload--history)
  71.                       (max 0 ido-ql-quickload-max-history-size))))))
  72.  
  73. ;;;=================================================================================================
  74.  
  75. (defun ido-ql-quickload-initialize ()
  76.   "Reads the contents of the `ido-ql-quickload-save-file'
  77.   into `ido-ql-quickload--history' and `ido-ql-quickload--statistics'"
  78.   (when (and (file-readable-p ido-ql-quickload-save-file)
  79.              (not ido-ql-quickload--initialized-p))
  80.     (with-temp-buffer
  81.       (insert-file-contents ido-ql-quickload-save-file)
  82.       (setf ido-ql-quickload--history (read (current-buffer))
  83.             ido-ql-quickload--statistics (read (current-buffer))
  84.             ido-ql-quickload--initialized-p t))
  85.  
  86.     ;; In case the user has reduced the value of the `ido-ql-quickload-max-history-size'
  87.     ;; between sessions
  88.     (ido-ql-quickload-drop-extra-history-items)))
  89.  
  90. ;;;=================================================================================================
  91.  
  92. (defun ido-ql-quickload-update-system-score (system)
  93.   "Increments `system' score.
  94.   For new `system' sets score to 1"
  95.   (incf (gethash system ido-ql-quickload--statistics 0)))
  96.  
  97. ;;;=================================================================================================
  98.  
  99. (defun ido-ql-quickload-update-history (system)
  100.   "Moves `system' to first position at `ido-ql-quickload--history'.
  101.   If (`length' `ido-ql-quickload--history') = `ido-ql-quickload-max-history-size'
  102.   and (`not' (`member' `system' `ido-ql-quickload--history')) drops the last item of
  103.   `ido-ql-quickload--history'"
  104.   (when (plusp ido-ql-quickload-max-history-size)
  105.     (setf ido-ql-quickload--history (cons system (remove system ido-ql-quickload--history)))
  106.     (ido-ql-quickload-drop-extra-history-items)))
  107.  
  108. ;;;=================================================================================================
  109.  
  110. (defun ido-ql-quickload-save-to-file ()
  111.   "Saves `ido-ql-quickload--history' and `ido-ql-quickload--statistics'
  112.   into `ido-ql-quickload-save-file'"
  113.   (interactive)
  114.   (ido-ql-quickload-drop-extra-history-items)
  115.   (with-temp-file (expand-file-name ido-ql-quickload-save-file)
  116.     (print ido-ql-quickload--history (current-buffer))
  117.     (print ido-ql-quickload--statistics (current-buffer))))
  118.    
  119. (add-hook 'kill-emacs-hook 'ido-ql-quickload-save-to-file)
  120.  
  121. ;;;=================================================================================================
  122.  
  123. (defun ido-ql-quickload-sort-systems-names (systems-names)
  124.   "Sorts `systems-names' list by:
  125.   1. Score
  126.   2. Aplhabet"
  127.   (let ((grouped-by-score-names-table (make-hash-table))
  128.         (result nil))
  129.     (dolist (system-name systems-names)
  130.       (push system-name
  131.             (gethash (gethash system-name ido-ql-quickload--statistics 0)
  132.                      grouped-by-score-names-table)))
  133.     (maphash (lambda (score names)
  134.                (push (cons score (sort names #'string-lessp)) result))
  135.       grouped-by-score-names-table)
  136.     (mapcan #'rest
  137.       (sort result (lambda (prev next) (> (car prev) (car next)))))))
  138.  
  139. ;;;=================================================================================================
  140.  
  141. (defun ido-ql-quickload-select-system ()
  142.   "Asks the user to select `system' to `ql:quickload' with `ido'.
  143.   Systems by default are sorted in order:
  144.   1. `ido-ql-quickload-max-history-size' number of last selected systems
  145.   2. Quicklisp local systems sorted by score and name
  146.   3. Other Quicklisp systems sorted by score and name"
  147.   (ido-ql-quickload-drop-extra-history-items)
  148.   (let* ((ido-enable-flex-matching t)
  149.          
  150.          (local-systems (nset-difference (slime-eval '(ql:list-local-systems))
  151.                                          ido-ql-quickload--history
  152.                                          :test #'string-equal))
  153.  
  154.          (quicklisp-systems (nset-difference
  155.                              (slime-eval '(cl:mapcar (cl:function ql-dist:name)
  156.                                                      (ql:system-list)))
  157.                              (append ido-ql-quickload--history
  158.                                      local-systems)
  159.                              :test #'string-equal))
  160.  
  161.          (systems-list (if ido-ql-quickload-ignore-local-projects-priority
  162.                            (append ido-ql-quickload--history
  163.                                    (ido-ql-quickload-sort-systems-names
  164.                                      (append local-systems quicklisp-systems)))
  165.                            (append ido-ql-quickload--history
  166.                                    (ido-ql-quickload-sort-systems-names local-systems)
  167.                                    (ido-ql-quickload-sort-systems-names quicklisp-systems))))
  168.  
  169.          (system (ido-completing-read "" systems-list)))
  170.  
  171.     (ido-ql-quickload-update-system-score system)
  172.     (ido-ql-quickload-update-history system)
  173.  
  174.     system))
  175.  
  176. ;;;=================================================================================================
  177.  
  178. (defun ql:quickload ()
  179.   (interactive)
  180.   (if ido-ql-quickload-suppress-output
  181.  
  182.       (slime-eval-async
  183.        `(cl:with-open-stream (cl:*standard-output* (cl:make-broadcast-stream))
  184.           (ql:quickload ,(ido-ql-quickload-select-system)))
  185.         (lambda (system)
  186.           (message "Loaded: %s" (first system))))
  187.  
  188.     (let ((slime-buffer (find-if (lambda (buffer) (string-match-p "slime-repl" (buffer-name buffer)))
  189.                                  (buffer-list)))
  190.           (buffer (current-buffer)))
  191.       (switch-to-buffer slime-buffer)
  192.       (end-of-buffer)
  193.       (slime-repl-kill-input)
  194.       (insert "(ql:quickload :")
  195.       (condition-case err
  196.           (let ((system (ido-ql-quickload-select-system)))
  197.             (end-of-line)
  198.             (insert system)
  199.             (insert ")")
  200.             (slime-repl-return))
  201.         (quit (when (string-equal (buffer-substring (- (point) 15) (point))
  202.                                   "(ql:quickload :")
  203.                 (backward-delete-char 15))))
  204.       (switch-to-buffer buffer))))
  205.  
  206. (defun ql:loadprj ()
  207.   (interactive)
  208.   (if ido-ql-quickload-suppress-output
  209.  
  210.       (slime-eval-async
  211.        `(cl:with-open-stream (cl:*standard-output* (cl:make-broadcast-stream))
  212.           (ql:quickload ,(ido-ql-quickload-select-system)))
  213.         (lambda (system)
  214.           (message "Loaded: %s" (first system))))
  215.  
  216.     (let ((slime-buffer (find-if (lambda (buffer) (string-match-p "slime-repl" (buffer-name buffer)))
  217.                                  (buffer-list)))
  218.           (buffer (current-buffer)))
  219.       (switch-to-buffer slime-buffer)
  220.       (end-of-buffer)
  221.       (slime-repl-kill-input)
  222.       (insert "(ql:quickload ")
  223.       (condition-case err
  224.           (let ((system (read-from-minibuffer "project name : ")))
  225.             (end-of-line)
  226.             (insert system)
  227.             (insert ")")
  228.             (slime-repl-return))
  229.         (quit (when (string-equal (buffer-substring (- (point) 15) (point))
  230.                                   "(ql:quickload :")
  231.                 (backward-delete-char 15))))
  232.       (switch-to-buffer buffer))))
  233.  
  234. ;;;=================================================================================================
  235.  
  236. (provide 'ido-ql-quickload)
  237.  
  238. ;;;=================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment