
`string-replace` is easier to understand, less error-prone, much faster, and results in shorter Lisp and byte code. Use it where applicable and obviously safe (erring on the conservative side). * admin/authors.el (authors-scan-change-log): * lisp/autoinsert.el (auto-insert-alist): * lisp/calc/calc-prog.el (calc-edit-macro-combine-alg-ent) (calc-edit-macro-combine-ext-command) (calc-edit-macro-combine-var-name): * lisp/calc/calc-units.el (math-make-unit-string): * lisp/calendar/cal-html.el (cal-html-comment): * lisp/calendar/cal-tex.el (cal-tex-comment): * lisp/calendar/icalendar.el (icalendar--convert-string-for-export) (icalendar--convert-string-for-import): * lisp/calendar/iso8601.el (iso8601--concat-regexps) (iso8601--full-time-match, iso8601--combined-match): * lisp/calendar/time-date.el (format-seconds): * lisp/calendar/todo-mode.el (todo-filter-items-filename): * lisp/cedet/cedet-files.el (cedet-directory-name-to-file-name) (cedet-file-name-to-directory-name): * lisp/comint.el (comint-watch-for-password-prompt): * lisp/dired-aux.el (dired-do-chmod): * lisp/dired-x.el (dired-man): * lisp/dired.el (dired-insert-directory, dired-goto-file-1): * lisp/emacs-lisp/comp.el (comp-c-func-name): * lisp/emacs-lisp/re-builder.el (reb-copy): * lisp/erc/erc-dcc.el (erc-dcc-unquote-filename): * lisp/erc/erc.el (erc-quit-reason-zippy, erc-part-reason-zippy) (erc-update-mode-line-buffer, erc-message-english-PART): * lisp/files.el (make-backup-file-name-1, files--transform-file-name) (read-file-modes): * lisp/fringe.el (fringe-mode): * lisp/gnus/gnus-art.el (gnus-button-handle-info-url): * lisp/gnus/gnus-group.el (gnus-group-completing-read): * lisp/gnus/gnus-icalendar.el (gnus-icalendar-event-from-ical): * lisp/gnus/gnus-mlspl.el (gnus-group-split-fancy): * lisp/gnus/gnus-search.el (gnus-search-query-parse-date) (gnus-search-transform-expression, gnus-search-run-search): * lisp/gnus/gnus-start.el (gnus-dribble-enter): * lisp/gnus/gnus-sum.el (gnus-summary-refer-article): * lisp/gnus/gnus-util.el (gnus-mode-string-quote): * lisp/gnus/message.el (message-put-addresses-in-ecomplete) (message-parse-mailto-url, message-mailto-1): * lisp/gnus/mml-sec.el (mml-secure-epg-sign): * lisp/gnus/mml-smime.el (mml-smime-epg-verify): * lisp/gnus/mml2015.el (mml2015-epg-verify): * lisp/gnus/nnmaildir.el (nnmaildir--system-name) (nnmaildir-request-list, nnmaildir-retrieve-groups) (nnmaildir-request-group, nnmaildir-retrieve-headers): * lisp/gnus/nnrss.el (nnrss-node-text): * lisp/gnus/spam-report.el (spam-report-gmane-internal) (spam-report-user-mail-address): * lisp/ibuffer.el (name): * lisp/image-dired.el (image-dired-pngnq-thumb) (image-dired-pngcrush-thumb, image-dired-optipng-thumb) (image-dired-create-thumb-1): * lisp/info.el (Info-set-mode-line): * lisp/international/mule-cmds.el (describe-language-environment): * lisp/mail/rfc2231.el (rfc2231-parse-string): * lisp/mail/rfc2368.el (rfc2368-parse-mailto-url): * lisp/mail/rmail.el (rmail-insert-inbox-text) (rmail-simplified-subject-regexp): * lisp/mail/rmailout.el (rmail-output-body-to-file): * lisp/mail/undigest.el (rmail-digest-rfc1153): * lisp/man.el (Man-default-man-entry): * lisp/mouse.el (minor-mode-menu-from-indicator): * lisp/mpc.el (mpc--debug): * lisp/net/browse-url.el (browse-url-mail): * lisp/net/eww.el (eww-update-header-line-format): * lisp/net/newst-backend.el (newsticker-save-item): * lisp/net/rcirc.el (rcirc-sentinel): * lisp/net/soap-client.el (soap-decode-date-time): * lisp/nxml/rng-cmpct.el (rng-c-literal-2-re): * lisp/nxml/xmltok.el (let*): * lisp/obsolete/nnir.el (nnir-run-swish-e, nnir-run-hyrex) (nnir-run-find-grep): * lisp/play/dunnet.el (dun-doassign): * lisp/play/handwrite.el (handwrite): * lisp/proced.el (proced-format-args): * lisp/profiler.el (profiler-report-header-line-format): * lisp/progmodes/gdb-mi.el (gdb-mi-quote): * lisp/progmodes/make-mode.el (makefile-bsdmake-rule-action-regex) (makefile-make-font-lock-keywords): * lisp/progmodes/prolog.el (prolog-guess-fill-prefix): * lisp/progmodes/ruby-mode.el (ruby-toggle-string-quotes): * lisp/progmodes/sql.el (sql-remove-tabs-filter, sql-str-literal): * lisp/progmodes/which-func.el (which-func-current): * lisp/replace.el (query-replace-read-from) (occur-engine, replace-quote): * lisp/select.el (xselect--encode-string): * lisp/ses.el (ses-export-tab): * lisp/subr.el (shell-quote-argument): * lisp/term/pc-win.el (msdos-show-help): * lisp/term/w32-win.el (w32--set-selection): * lisp/term/xterm.el (gui-backend-set-selection): * lisp/textmodes/picture.el (picture-tab-search): * lisp/thumbs.el (thumbs-call-setroot-command): * lisp/tooltip.el (tooltip-show-help-non-mode): * lisp/transient.el (transient-format-key): * lisp/url/url-mailto.el (url-mailto): * lisp/vc/log-edit.el (log-edit-changelog-ours-p): * lisp/vc/vc-bzr.el (vc-bzr-status): * lisp/vc/vc-hg.el (vc-hg--glob-to-pcre): * lisp/vc/vc-svn.el (vc-svn-after-dir-status): * lisp/xdg.el (xdg-desktop-strings): * test/lisp/electric-tests.el (defun): * test/lisp/term-tests.el (term-simple-lines): * test/lisp/time-stamp-tests.el (formatz-mod-del-colons): * test/lisp/wdired-tests.el (wdired-test-bug32173-01) (wdired-test-unfinished-edit-01): * test/src/json-tests.el (json-parse-with-custom-null-and-false-objects): Use `string-replace` instead of `replace-regexp-in-string`.
111 lines
4.1 KiB
EmacsLisp
111 lines
4.1 KiB
EmacsLisp
;;; cedet-files.el --- Common routines dealing with file names. -*- lexical-binding: t; -*-
|
|
|
|
;; Copyright (C) 2007-2021 Free Software Foundation, Inc.
|
|
|
|
;; Author: Eric M. Ludlam <zappo@gnu.org>
|
|
;; Package: cedet
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
|
;; it under the terms of the GNU General Public License as published by
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
;; (at your option) any later version.
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
;; GNU General Public License for more details.
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
;;; Commentary:
|
|
;;
|
|
;; Various useful routines for dealing with file names in the tools
|
|
;; which are a part of CEDET.
|
|
|
|
;;; Code:
|
|
|
|
(defun cedet-directory-name-to-file-name (referencedir &optional testmode)
|
|
"Convert the REFERENCEDIR (a full path name) into a filename.
|
|
Convert directory separation characters into ! characters.
|
|
Optional argument TESTMODE is used by tests to avoid conversion
|
|
to the file's truename, and dodging platform tricks."
|
|
(let ((file referencedir))
|
|
;; Expand to full file name
|
|
(when (not testmode)
|
|
(setq file (file-truename file)))
|
|
;; If FILE is a directory, then force it to end in /.
|
|
(when (file-directory-p file)
|
|
(setq file (file-name-as-directory file)))
|
|
;; Handle Windows Special cases
|
|
(when (or (memq system-type '(windows-nt ms-dos)) testmode)
|
|
;; Replace any invalid file-name characters (for the
|
|
;; case of backing up remote files).
|
|
(when (not testmode)
|
|
(setq file (expand-file-name (convert-standard-filename file))))
|
|
;; Normalize DOSish file names.
|
|
(if (eq (aref file 1) ?:)
|
|
(setq file (concat "/"
|
|
"drive_"
|
|
(char-to-string (downcase (aref file 0)))
|
|
(if (eq (aref file 2) ?/)
|
|
""
|
|
"/")
|
|
(substring file 2)))))
|
|
;; Make the name unique by substituting directory
|
|
;; separators. It may not really be worth bothering about
|
|
;; doubling `!'s in the original name...
|
|
(setq file (subst-char-in-string
|
|
?/ ?!
|
|
(string-replace "!" "!!" file)))
|
|
file))
|
|
|
|
(defun cedet-file-name-to-directory-name (referencefile &optional testmode)
|
|
"Reverse the process of `cedet-directory-name-to-file-name'.
|
|
Convert REFERENCEFILE to a directory name replacing ! with /.
|
|
Optional TESTMODE is used in tests to avoid doing some platform
|
|
specific conversions during tests."
|
|
(let ((file referencefile))
|
|
;; Replace the ! with /
|
|
(setq file (subst-char-in-string ?! ?/ file))
|
|
;; Occurrences of // meant there was once a single !.
|
|
(setq file (string-replace "//" "!" file))
|
|
|
|
;; Handle Windows special cases
|
|
(when (or (memq system-type '(windows-nt ms-dos)) testmode)
|
|
|
|
;; Handle drive letters from DOSish file names.
|
|
(when (string-match "^/drive_\\([a-z]\\)/" file)
|
|
(let ((driveletter (match-string 1 file))
|
|
)
|
|
(setq file (concat driveletter ":"
|
|
(substring file (match-end 1))))))
|
|
|
|
;; Handle the \\file\name nomenclature on some Windows boxes.
|
|
(when (string-match "^!" file)
|
|
(setq file (concat "//" (substring file 1)))))
|
|
file))
|
|
|
|
(defun cedet-files-list-recursively (dir re)
|
|
"Return list of files in directory matching to given regex."
|
|
(when (file-accessible-directory-p dir)
|
|
(let ((files (directory-files dir t))
|
|
matched)
|
|
(dolist (file files matched)
|
|
(let ((fname (file-name-nondirectory file)))
|
|
(cond
|
|
((or (string= fname ".")
|
|
(string= fname "..")) nil)
|
|
((and (file-regular-p file)
|
|
(string-match re fname))
|
|
(setq matched (cons file matched)))
|
|
((file-directory-p file)
|
|
(let ((tfiles (cedet-files-list-recursively file re)))
|
|
(when tfiles (setq matched (append matched tfiles)))))))))))
|
|
|
|
|
|
(provide 'cedet-files)
|
|
|
|
;;; cedet-files.el ends here
|