3

I am having difficulties composing a regexp that will highlight certain folder extensions -- including the preceding dot -- with colors that are different than the rest of the folder -- e.g., .app would be colored red; and .git would be colored blue.

Examples:

  • The folder Emacs.app would be colorized with Emacs being a gray color and .app would be a red color.

  • The folder .0.context-menu.git would be colorized with .0.context-menu being a gray color and .git would be a blue color.

I have a regexp that gets me from the left margin all the way to the beginning of the folder -- the variable for that regexp is named folder-listing-before-filename-regexp.

The problem I am having is that the regex .* swallows up the folder extensions. If I add [^.], that prevents me from using the dot later on with extensions like .app and .git -- I want the extension to include the dot when colored.

The regexp that leads up to the folder looks like this:

(defvar folder-listing-before-filename-regexp
  (concat
    "\\(^ +d[slrwxt+-]+\\)"    ;; 1  permissions
    "\\(\s\\)"                 ;; 2
    "\\([0-9]+\\|\s+[0-9]+\\)" ;; 3  some small numbers
    "\\(\s\\)"                 ;; 4
    "\\([a-zA-Z]+\\)"          ;; 5  group
    "\\(\s\\)"                 ;; 6
    "\\([a-zA-Z]+\\)"          ;; 7  user
    "\\(\s\\)"                 ;; 8
    "\\([\s0-9.kKMGT]+\\)"     ;; 9  size
    "\\(\s\\)"                 ;; 10
    "\\([0-9-]+\\)"            ;; 11 date
    "\\(\s\\)"                 ;; 12
    "\\([0-9:]+\\)"            ;; 13 time
    "\\(\s\\)"))               ;; 14

Here is some test data:

  drwxr-xr-x  79 HOME staff 2.7K 10-30-2014 16:42:18 .
  drwxr-xr-x  27 HOME staff  918 10-28-2014 13:08:59 ..
  drwxr-xr-x 162 HOME staff 5.4K 10-11-2014 11:34:16 .0.apel_flim_simi
  drwxr-xr-x 432 HOME staff  15K 10-29-2014 15:37:44 .0.backup
  drwxr-xr-x   5 HOME staff  170 10-19-2014 09:34:37 .0.context-menu.git
  drwxr-xr-x   5 HOME staff  170 10-19-2014 13:06:36 .0.dired-read-filename.git
  drwx------   4 HOME staff  136 04-29-2014 17:40:13 .0.eshell
  drwxr-xr-x   3 HOME staff  102 10-11-2014 10:29:03 .0.gh
  drwxr-xr-x   5 HOME staff  170 10-09-2014 21:16:35 .0.lorg-calendar.git
  drwxr-xr-x   5 HOME staff  170 10-09-2014 20:06:09 .0.lorg-search.git
  drwxr-xr-x  10 HOME staff  340 01-31-2014 19:37:31 .0.mail
  drwxr-xr-x  25 HOME staff  850 10-14-2014 13:23:31 .0.snippets
  drwxr-xr-x  23 HOME staff  782 12-03-2013 17:15:50 .0.sound
  drwxr-xr-x  27 HOME staff  918 10-18-2014 11:53:56 .0.sources
  drwxr-xr-x  67 HOME staff 2.3K 05-05-2014 23:19:12 .0.w3m
  drwxr-xr-x 146 HOME staff 4.9K 09-08-2014 10:54:40 .0.wl
  drwxr-xr-x   3 HOME staff  102 06-03-2014 11:37:02 Emacs.app
  drwxr-xr-x   3 HOME staff  102 06-01-2014 10:39:02 Emacs_06_01_2014.app
  drwxr-xr-x   3 HOME staff  102 10-01-2014 07:31:45 Emacs_10_01_2014.app
  drwxr-xr-x 195 HOME staff 6.5K 10-18-2014 12:06:25 bin
  drwxr-xr-x   3 HOME staff  102 06-26-2013 07:59:44 etc
  drwxr-xr-x  10 HOME staff  340 09-30-2014 18:07:42 include
  drwxr-xr-x   9 HOME staff  306 06-05-2013 22:50:05 info
  drwxr-xr-x  27 HOME staff  918 10-18-2014 12:06:42 lib
  drwxr-xr-x  12 HOME staff  408 09-30-2014 18:10:42 libexec
  drwxr-xr-x   4 root staff  136 09-30-2014 18:07:42 man
  drwxr-xr-x   4 root staff  136 09-30-2014 18:10:42 sbin
  drwxr-xr-x   9 HOME staff  306 09-30-2014 18:10:42 share

Example

  • Have you tried looking into other options? Such as Dired+? – Luke Oct 31 '14 at 0:23
  • Why would [^.]* prevent you from using the dot? Something like [^.]*\\(\\..*\\) should match your request and group the entire extension including the dot. Show us what you've tried. – Malabarba Oct 31 '14 at 0:24
  • @Malabarba -- your suggestion almost does it, with the exception of the folders beginning with a dot -- e.g., .0.context-menu.git I'll work on it a little later on this evening using your example as a starting point. – lawlist Oct 31 '14 at 0:27
  • I see. And does .*\\(\\.[^.]*\\)$ not work either? (I'm not at the pc right now so I can't test myself). – Malabarba Oct 31 '14 at 0:31
  • @King Shimkus -- yes, thank you -- the regex for the permission colors and file size are from Dired+, but I broke some of the functionality by adding a custom time-style. My custom time-style is also not compatible with directory-listing-before-filename-regexp. Dired mode is not designed for a custom time-style like the one I'm using, so I'm essentially creating a custom version of dired-mode. – lawlist Oct 31 '14 at 0:31
1

The following is just a tweak to Dired+ -- adopting the same manner to highlight folder extensions as is already done with file extensions; with the additional tweak to the beginning regexp to get me over to the files / folders. My custom time-style of ls is not compatible with dired-mode or the corresponding regexp in files.el, so I composed my own regexp. The section below relating to diredp-font-lock-keywords-1 is just the relevant excerpt.

Over the next few days, I'll continue to think about the regexp proposed by @Malabarba .*\\(\\.[^.]*\\)$ to see if I can create a whopper regexp that covers the majority of situations. I ended up with about 5 different regexp strings, which is not as clean as Dired+ presently offers.

(defvar folder-listing-before-filename-regexp
  (concat
    "\\(^ +d[slrwxt+-]+\\)"    ;; 1  permissions
    "\\(\s\\)"                 ;; 2
    "\\([0-9]+\\|\s+[0-9]+\\)" ;; 3  some small numbers
    "\\(\s\\)"                 ;; 4
    "\\([a-zA-Z]+\\)"          ;; 5  user
    "\\(\s\\)"                 ;; 6
    "\\([a-zA-Z]+\\)"          ;; 7  group
    "\\(\s\\)"                 ;; 8
    "\\([\s0-9.kKMGT]+\\)"     ;; 9  size
    "\\(\s\\)"                 ;; 10
    "\\([0-9-]+\\)"            ;; 11 date
    "\\(\s\\)"                 ;; 12
    "\\([0-9:]+\\)"            ;; 13 time
    "\\(\s\\)"))               ;; 14

(defvar file-listing-before-filename-regexp
  (concat
    "\\(^ +[^d][slrwxt+-]+\\)" ;; 1  permissions
    "\\(\s\\)"                 ;; 2
    "\\([0-9]+\\|\s+[0-9]+\\)" ;; 3  some small numbers
    "\\(\s\\)"                 ;; 4
    "\\([a-zA-Z]+\\)"          ;; 5  user
    "\\(\s\\)"                 ;; 6
    "\\([a-zA-Z]+\\)"          ;; 7  group
    "\\(\s\\)"                 ;; 8
    "\\([\s0-9.kKMGT]+\\)"     ;; 9  size
    "\\(\s\\)"                 ;; 10
    "\\([0-9-]+\\)"            ;; 11 date
    "\\(\s\\)"                 ;; 12
    "\\([0-9:]+\\)"            ;; 13 time
    "\\(\s\\)"))               ;; 14

(defvar diredp-font-lock-keywords-1
  (list
    '("[^ .]\\.\\(git\\)$" 1 'diredp-git-face t)
    '("[^ .]\\.\\(app\\)$" 1 'diredp-app-face t)

  (list folder-listing-before-filename-regexp
    (list "\\(.+\\)$" nil nil (list 0 diredp-dir-priv 'keep t))) ; folder-name

  (list file-listing-before-filename-regexp
    (list "\\(.+\\)$" nil nil (list 0 diredp-file-name 'keep t))) ; file-name
  ))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.