pcmpl-linux.el 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. ;;; pcmpl-linux.el --- functions for dealing with GNU/Linux completions
  2. ;; Copyright (C) 1999-2012 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. (defgroup pcmpl-linux nil
  23. "Functions for dealing with GNU/Linux completions."
  24. :group 'pcomplete)
  25. ;; Functions:
  26. ;;;###autoload
  27. (defun pcomplete/kill ()
  28. "Completion for GNU/Linux `kill', using /proc filesystem."
  29. (if (pcomplete-match "^-\\(.*\\)" 0)
  30. (pcomplete-here
  31. (pcomplete-uniqify-list
  32. (split-string
  33. (pcomplete-process-result "kill" "-l")))
  34. (pcomplete-match-string 1 0)))
  35. (while (pcomplete-here
  36. (if (file-directory-p "/proc")
  37. (directory-files "/proc" nil "\\`[0-9]+\\'"))
  38. nil 'identity)))
  39. ;;;###autoload
  40. (defun pcomplete/umount ()
  41. "Completion for GNU/Linux `umount'."
  42. (pcomplete-opt "hVafrnvt(pcmpl-linux-fs-types)")
  43. (while (pcomplete-here (pcmpl-linux-mounted-directories)
  44. nil 'identity)))
  45. ;;;###autoload
  46. (defun pcomplete/mount ()
  47. "Completion for GNU/Linux `mount'."
  48. (pcomplete-opt "hVanfFrsvwt(pcmpl-linux-fs-types)o?L?U?")
  49. (while (pcomplete-here (pcomplete-entries) nil 'identity)))
  50. (defun pcmpl-linux-fs-types ()
  51. "Return a list of available fs modules on GNU/Linux systems."
  52. (let ((kernel-ver (pcomplete-process-result "uname" "-r")))
  53. (directory-files
  54. (concat "/lib/modules/" kernel-ver "/kernel/fs/"))))
  55. (defun pcmpl-linux-mounted-directories ()
  56. "Return a list of mounted directory names."
  57. (let (points)
  58. (when (file-readable-p "/etc/mtab")
  59. (with-temp-buffer
  60. (insert-file-contents-literally "/etc/mtab")
  61. (while (not (eobp))
  62. (let* ((line (buffer-substring (point) (line-end-position)))
  63. (args (split-string line " ")))
  64. (setq points (cons (nth 1 args) points)))
  65. (forward-line)))
  66. (pcomplete-uniqify-list points))))
  67. (defun pcomplete-pare-list (l r)
  68. "Destructively remove from list L all elements matching any in list R.
  69. Test is done using `equal'."
  70. (while (and l (and r (member (car l) r)))
  71. (setq l (cdr l)))
  72. (let ((m l))
  73. (while m
  74. (while (and (cdr m)
  75. (and r (member (cadr m) r)))
  76. (setcdr m (cddr m)))
  77. (setq m (cdr m))))
  78. l)
  79. (defun pcmpl-linux-mountable-directories ()
  80. "Return a list of mountable directory names."
  81. (let (points)
  82. (when (file-readable-p "/etc/fstab")
  83. (with-temp-buffer
  84. (insert-file-contents-literally "/etc/fstab")
  85. (while (not (eobp))
  86. (let* ((line (buffer-substring (point) (line-end-position)))
  87. (args (split-string line "\\s-+")))
  88. (setq points (cons (nth 1 args) points)))
  89. (forward-line)))
  90. (pcomplete-pare-list
  91. (pcomplete-uniqify-list points)
  92. (cons "swap" (pcmpl-linux-mounted-directories))))))
  93. ;;; pcmpl-linux.el ends here