reddit / emacs

Weird Dired Error

Ethan045627 路 2022-07-23 15:36
I wrote a function which generates compile-command by seeing the file extension I use this function in my .dir-locals.el where all my files are kept. (Note : You might feel like the code is noobish) Function: `(define ethan/file-compile-command ()` `(interactive)` `(let* ((file-name-full buffer-file-name)` `(file-ext (file-name-extension buffer-file-name))` `(file-name-without-extension (file-name-sans-extension buffer-file-name))` `(file-name-without-extension-and-path (file-name-base buffer-file-name)))` `(cond ((string= file-ext "c") (setq ccommand (format "gcc -o %s %s && ./%s"` `file-name-without-extension` `file-name-full` `file-name-without-extension-and-path)))` `((string= file-ext "cpp") (setq ccommand (format "g++ -o %s %s && ./%s"` `file-name-without-extension` `file-name-full` `file-name-without-extension-and-path)))` `((string= file-ext "java") (setq ccommand (format "javac %s && java %s"` `file-name-full` `file-name-without-extension)))` `((string= file-ext "kt") (setq ccommand (format "kotlinc %s -include-runtime -d %s.jar && java -jar %s.jar"` `file-name-full` `file-name-without-extension` `file-name-without-extension)))))` `(setq compile-command ccommand)` but when I try to access tired it gives this error 1> from mini buffer `Debugger entered--Lisp error: (wrong-type-argument stringp nil)` `file-name-nondirectory(nil)` `file-name-extension(nil)` `(let* ((file-name-full buffer-file-name) (file-ext (file-name-extension buffer-file-name)) (file-name-without-extension (file-name-sans-extension buffer-file-name)) (file-name-without-extension-and-path (file-name-base buffer-file-name))) (cond ((string= file-ext "c") (setq ccommand (format "gcc -o %s %s && ./%s" file-name-without-extension file-name-full file-name-without-extension-and-path))) ((string= file-ext "cpp") (setq ccommand (format "g++ -o %s %s && ./%s" file-name-without-extension file-name-full file-name-without-extension-and-path))) ((string= file-ext "java") (setq ccommand (format "javac %s && java %s" file-name-full file-name-without-extension))) ((string= file-ext "kt") (setq ccommand (format "kotlinc %s -include-runtime -d %s.jar && java -jar..." file-name-full file-name-without-extension file-name-without-extension)))))` `ethan/file-compile-command()` `eval((ethan/file-compile-command) t)` `hack-one-local-variable(eval (ethan/file-compile-command))` `hack-local-variables-apply()` `hack-dir-local-variables-non-file-buffer()` `dired-mode("~/Projects/Extra/c++/" "-alhg")` `dired-internal-noselect("~/Projects/Extra/c++/" nil)` `dired-noselect("/Users/abhijeetsingh/Projects/Extra/c++/" nil)` `#f(compiled-function (dirname &optional switches) "\"Edit\" directory DIRNAME--delete, rename, print, etc. some files in it.\nOptional second argument SWITCHES specifies the \`ls' options used.\n(Interactively, use a prefix argument to be able to specify SWITCHES.)\n\nIf DIRNAME is a string, Dired displays a list of files in DIRNAME (which\nmay also have shell wildcards appended to select certain files).\n\nIf DIRNAME is a cons, its first element is taken as the directory name\nand the rest as an explicit list of files to make directory entries for.\nIn this case, SWITCHES are applied to each of the files separately, and\ntherefore switches that control the order of the files in the produced\nlisting have no effect.\n\n\\<dired-mode-map>You can flag files for deletion with \\[dired-flag-file-deletion] and then\ndelete them by typing \\[dired-do-flagged-delete].\nType \\[describe-mode] after entering Dired for more info.\n\nIf DIRNAME is already in a Dired buffer, that buffer is used without refresh." (interactive (dired-read-dir-and-switches "")) #<bytecode -0x127122f449790446>)("/Users/abhijeetsingh/Projects/Extra/c++/" nil)` `ls-lisp--dired(#f(compiled-function (dirname &optional switches) "\"Edit\" directory DIRNAME--delete, rename, print, etc. some files in it.\nOptional second argument SWITCHES specifies the \`ls' options used.\n(Interactively, use a prefix argument to be able to specify SWITCHES.)\n\nIf DIRNAME is a string, Dired displays a list of files in DIRNAME (which\nmay also have shell wildcards appended to select certain files).\n\nIf DIRNAME is a cons, its first element is taken as the directory name\nand the rest as an explicit list of files to make directory entries for.\nIn this case, SWITCHES are applied to each of the files separately, and\ntherefore switches that control the order of the files in the produced\nlisting have no effect.\n\n\\<dired-mode-map>You can flag files for deletion with \\[dired-flag-file-deletion] and then\ndelete them by typing \\[dired-do-flagged-delete].\nType \\[describe-mode] after entering Dired for more info.\n\nIf DIRNAME is already in a Dired buffer, that buffer is used without refresh." (interactive (dired-read-dir-and-switches "")) #<bytecode -0x127122f449790446>) "/Users/abhijeetsingh/Projects/Extra/c++/")` `apply(ls-lisp--dired #f(compiled-function (dirname &optional switches) "\"Edit\" directory DIRNAME--delete, rename, print, etc. some files in it.\nOptional second argument SWITCHES specifies the \`ls' options used.\n(Interactively, use a prefix argument to be able to specify SWITCHES.)\n\nIf DIRNAME is a string, Dired displays a list of files in DIRNAME (which\nmay also have shell wildcards appended to select certain files).\n\nIf DIRNAME is a cons, its first element is taken as the directory name\nand the rest as an explicit list of files to make directory entries for.\nIn this case, SWITCHES are applied to each of the files separately, and\ntherefore switches that control the order of the files in the produced\nlisting have no effect.\n\n\\<dired-mode-map>You can flag files for deletion with \\[dired-flag-file-deletion] and then\ndelete them by typing \\[dired-do-flagged-delete].\nType \\[describe-mode] after entering Dired for more info.\n\nIf DIRNAME is already in a Dired buffer, that buffer is used without refresh." (interactive (dired-read-dir-and-switches "")) #<bytecode -0x127122f449790446>) "/Users/abhijeetsingh/Projects/Extra/c++/")` `dired("/Users/abhijeetsingh/Projects/Extra/c++/")` `dired-jump(nil nil)` `funcall-interactively(dired-jump nil nil)` `call-interactively(dired-jump nil nil)` `command-execute(dired-jump)` 2> from Messages buffer `dired-insert-subdir: Wrong type argument: integer-or-marker-p, nil` `dired-current-directory: No subdir-alist in c++` Its night in my area so I might reply late, sorry for inconvenience and thanks for help 馃槂.
_viz_ 路 2022-07-23 15:44
The error says it all: buffer-file-name is nil. If you're trying to compile the file under point in dired, then use dired-get-filename. buffer-file-name is only set for file-visiting buffers and dired is not visiting a file.
Ethan045627 路 2022-07-23 15:49
No brother, I just want to go to that directory in dired but it doesn't allow me that too. But I think it is because I have this function under nil cons pair in .dir-locals.el that why I should shift it to prog-mode thus this function runs only for the program files and not for dired. Am I correct?
_viz_ 路 2022-07-23 16:00
Unless you show the dir-locals entry, I cannot tell what you're doing.
Ethan045627 路 2022-07-23 16:05
((prog-mode . ((ethan/file-compile-command)))) BTW is there any other way to achieve this type of functionality
_viz_ 路 2022-07-23 16:12
>BTW is there any other way to achieve this type of functionality You could write major-mode hooks that set the compile-command variable then use M-x compile RET and M-x recompile RET to compile and recompile resp.
Ethan045627 路 2022-07-23 16:14
That's a good idea and what abt my .dir-locals.el its not working 馃槄 &#x200B; You mean like this `(add-hook 'prog-mode (lambda () (eval . (ethan/file-compile-command))))` &#x200B; This seems to work bro \> .dir-locals.el `((prog-mode . ((eval . (ethan/file-compile-command)))))`