pcmpl-linux.el 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. ;;; pcmpl-linux.el --- functions for dealing with GNU/Linux completions
  2. ;; Copyright (C) 1999-2017 Free Software Foundation, Inc.
  3. ;; Package: pcomplete
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; These functions are for use with GNU/Linux. Since they depend on a
  17. ;; certain knowledge of the layout of such systems, they probably
  18. ;; won't work very well on other operating systems.
  19. ;;; Code:
  20. (provide 'pcmpl-linux)
  21. (require 'pcomplete)
  22. ;; Unused.
  23. ;;; (defgroup pcmpl-linux nil
  24. ;;; "Functions for dealing with GNU/Linux completions."
  25. ;;; :group 'pcomplete)
  26. ;; Functions:
  27. ;;;###autoload
  28. (defun pcomplete/kill ()
  29. "Completion for GNU/Linux `kill', using /proc filesystem."
  30. (if (pcomplete-match "^-\\(.*\\)" 0)
  31. (pcomplete-here
  32. (pcomplete-uniqify-list
  33. (split-string
  34. (pcomplete-process-result "kill" "-l")))
  35. (pcomplete-match-string 1 0)))
  36. (while (pcomplete-here
  37. (if (file-directory-p "/proc")
  38. (directory-files "/proc" nil "\\`[0-9]+\\'"))
  39. nil 'identity)))
  40. ;;;###autoload
  41. (defun pcomplete/umount ()
  42. "Completion for GNU/Linux `umount'."
  43. (pcomplete-opt "hVafrnvt(pcmpl-linux-fs-types)")
  44. (while (pcomplete-here (pcmpl-linux-mounted-directories)
  45. nil 'identity)))
  46. ;;;###autoload
  47. (defun pcomplete/mount ()
  48. "Completion for GNU/Linux `mount'."
  49. (pcomplete-opt "hVanfFrsvwt(pcmpl-linux-fs-types)o?L?U?")
  50. (while (pcomplete-here (pcomplete-entries) nil 'identity)))
  51. (defun pcmpl-linux-fs-types ()
  52. "Return a list of available fs modules on GNU/Linux systems."
  53. (let ((kernel-ver (pcomplete-process-result "uname" "-r")))
  54. (directory-files
  55. (concat "/lib/modules/" kernel-ver "/kernel/fs/"))))
  56. (defun pcmpl-linux-mounted-directories ()
  57. "Return a list of mounted directory names."
  58. (let (points)
  59. (when (file-readable-p "/etc/mtab")
  60. (with-temp-buffer
  61. (insert-file-contents-literally "/etc/mtab")
  62. (while (not (eobp))
  63. (let* ((line (buffer-substring (point) (line-end-position)))
  64. (args (split-string line " ")))
  65. (setq points (cons (nth 1 args) points)))
  66. (forward-line)))
  67. (pcomplete-uniqify-list points))))
  68. (defun pcomplete-pare-list (l r)
  69. "Destructively remove from list L all elements matching any in list R.
  70. Test is done using `equal'."
  71. (while (and l (and r (member (car l) r)))
  72. (setq l (cdr l)))
  73. (let ((m l))
  74. (while m
  75. (while (and (cdr m)
  76. (and r (member (cadr m) r)))
  77. (setcdr m (cddr m)))
  78. (setq m (cdr m))))
  79. l)
  80. (defun pcmpl-linux-mountable-directories ()
  81. "Return a list of mountable directory names."
  82. (let (points)
  83. (when (file-readable-p "/etc/fstab")
  84. (with-temp-buffer
  85. (insert-file-contents-literally "/etc/fstab")
  86. (while (not (eobp))
  87. (let* ((line (buffer-substring (point) (line-end-position)))
  88. (args (split-string line "\\s-+")))
  89. (setq points (cons (nth 1 args) points)))
  90. (forward-line)))
  91. (pcomplete-pare-list
  92. (pcomplete-uniqify-list points)
  93. (cons "swap" (pcmpl-linux-mounted-directories))))))
  94. ;;; pcmpl-linux.el ends here