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 馃槂.